Skip to content

feat(crypto): CRP-2879 Add support for PocketIC key derivation to ic-pub-key - #7194

Merged
randombit merged 5 commits into
masterfrom
jack/crp-2879-part2
Oct 15, 2025
Merged

randombit merged 5 commits into
masterfrom
jack/crp-2879-part2

Conversation

@randombit

Copy link
Copy Markdown
Contributor

Split from #7148 because ic-pub-key depends on the published ic-ed25519 and ic-secp256k1, so it was required first to add PocketIC support to those crates and publish new versions, prior to making this change.

@randombit
randombit requested a review from a team October 10, 2025 20:39
@github-actions github-actions Bot added the feat label Oct 10, 2025

@fspreiss fspreiss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that we only depend on the published versions of ic-ed25519 and ic-secp256k1 in the Cargo build, but not in the Bazel build. I assume this is intentional.

Comment thread Cargo.toml Resolved
Comment thread packages/ic-pub-key/src/lib.rs Resolved
Comment thread packages/ic-pub-key/tests/tests.rs Resolved
@randombit

Copy link
Copy Markdown
Contributor Author

I noticed that we only depend on the published versions of ic-ed25519 and ic-secp256k1 in the Cargo build, but not in the Bazel build. I assume this is intentional.

Kinda? The situation is somewhat messy here. Note that the replica itself depends on ic-ed25519 and ic-secp256k1 (via clib), and does so via direct reference to the monorepo sources rather than via a version. Since they are referenced by path, ic-ed25519 and ic-secp256k1 do not appear in external_crates.bzl. Additionally, ic-pub-key is published on crates.io and so we must be compatible with a specific numbered version of the dependencies. If we made a change to ic-secp256k1 that was incompatible, updated ic-pub-key to match, and published the new ic-pub-key we would have a unusable release unless we first released a new ic-secp256k1.

If we pin to a specific version of ic-ed25519 via external_crates.bzl this reduces the effectiveness of monorepo changes. Eg, some bugfix is made in ic-ed25519. Currently this applies as soon as the commit merges to master. If we pin, instead we'd have to commit that change, then create a new release of the crate, then create another monorepo commit updating the pinned version.

In the current setup, changes to the crates apply to replica immediately on merge, and we are assured that ic-pub-key compiles both with the most recent releases as well as the current master of ic-secp256k1 and ic-ed25519. It also prevents a situation where a change is made to ic-secp256k1/ic-ed25519 that breaks compatibility in the replica or ic-pub-key - we would only see the build failure later, at the repin step.

tl;dr there are two different consumers of these crates, users via crates.io+cargo, and users via monorepo+bazel. Treating them differently seems to provide the best outcome for both classes.

@randombit
randombit enabled auto-merge October 15, 2025 18:35
@randombit
randombit added this pull request to the merge queue Oct 15, 2025
Merged via the queue into master with commit 8d6d7d2 Oct 15, 2025
@randombit
randombit deleted the jack/crp-2879-part2 branch October 15, 2025 19:03
@fspreiss

Copy link
Copy Markdown
Contributor

Kinda? [...]
tl;dr [...]

Makes sense, thanks!

basvandijk pushed a commit to Ocheretovich/ic that referenced this pull request Oct 23, 2026
dfinity#11895)

### Why

`ic-secp256k1` and `ic-ed25519` ship the mainnet and PocketIC master
public keys of their algorithms, and `ic-pub-key` uses them to derive
keys offline. The mainnet ECDSA keys `key_1` and `test_key_1` now also
exist over secp256r1 (P-256), and PocketIC holds secp256r1 keys too
(dfinity#11741). But `ic-secp256r1` has none of them.

This PR adds the keys and bumps the version, so the crate can be
released. `ic-pub-key` can only use the keys once that release is out
(dfinity#11894). That's the same order as dfinity#7148 before dfinity#7194 for the secp256k1
and Ed25519 PocketIC keys.

### What

* `PublicKey::mainnet_key(MasterPublicKeyId)` returns `key_1` and
`test_key_1`.
* `PublicKey::pocketic_key(PocketIcMasterPublicKeyId)` returns the keys
PocketIC uses for `key_1`, `test_key_1` and `dfx_test_key`.
* `PublicKey::derive_mainnet_key` and `PublicKey::derive_pocketic_key`
derive a canister's public key from those master keys. They are offline
equivalents of `ecdsa_public_key`. For their `&CanisterId` argument, the
crate re-exports `ic_principal::Principal` as `CanisterId`, as
`ic-secp256k1` and `ic-ed25519` do.
* `hex-literal` becomes a regular dependency, to embed the keys, and
`ic_principal` a new one.
* The version goes from 0.2.0 to 0.3.0, with a dated changelog entry,
for a release after merging. In-repo dependents use the crate by path
without a version, so nothing else changes.
* The `cup_explorer` README now lists `ic-secp256r1` among the crates
with hardcoded master keys.

Where the keys come from:

* **Mainnet:** the CUPs of subnets uzr34 (`key_1`) and fuqsr
(`test_key_1`). In that same output, the secp256k1, BIP340, Ed25519 and
VetKD master keys match the values `ic-secp256k1`, `ic-ed25519` and
`ic-vetkeys` already ship.
* **PocketIC:** derived the way the StateMachine does it, from
`PrivateKey::generate_insecure_key_for_testing(42)` and the key name.

### Tests

* `mainnet_keys_round_trip`: the mainnet keys round-trip.
* A test derives the PocketIC keys the StateMachine's way and compares
them with the hardcoded ones.
* `derive_mainnet_key` and `derive_pocketic_key` have known-answer tests
for all five keys. The expected keys are what the replica's
`derive_threshold_public_key` (the function that answers
`ecdsa_public_key`) returns for the same master key, canister and path.
* The SLIP-10 retry for nist256p1 (index 33941) is now tested. It's the
only branch of the derivation the existing tests didn't reach: it runs
when iL >= n, about once in 2^32 steps on P-256. The vector is the one
the replica's own key derivation test uses.
* Locally, in the dev container, all of these pass: `cargo check`,
`cargo fmt`, `rust-lint.sh`, `bazel build //... --nobuild`,
`//bazel:buildifier.check`, and the 37 tests from the depth-2 rdeps
query (this package, `ic-pub-key`, `rs/crypto`, the P-256 basic-sig
crate, `standalone-sig-verifier`, `rs/validator`).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants