ic-pub-key 0.3.0

A package created for the Internet Computer Protocol for (offline) derivation of threshold public keys
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#![forbid(unsafe_code)]
#![forbid(missing_docs)]
#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(future_incompatible)]
#![warn(rust_2018_idioms)]
#![warn(rustdoc::broken_intra_doc_links)]
#![warn(rustdoc::missing_crate_level_docs)]
#![deny(unused_must_use)]
#![deny(unused_results)]

//! A crate for performing derivation of threshold public keys
//!
//! The Internet Computer platform offers a threshold signing interface
//! supporting ECDSA, Ed25519, BIP340-Schnorr, and BLS. The signing interface
//! allows signing under many different keys which are derived from a master key
//! using a derivation algorithm.
//!
//! The management canister interface allows for *online* derivation of public
//! keys: a canister running on the IC can invoke the call to determine what
//! public key will be used for any particular context argument.
//!
//! This crate mirrors that functionality in an offline way: it can be called either
//! by canisters that do not wish to call the management canister interface, or
//! by applications not running on that IC at all.
//!
//! As an extension of the management canister interface, this crate also allows
//! deriving keys relative to the hardcoded test master keys used by PocketIC.

#[cfg(not(any(feature = "secp256k1", feature = "ed25519", feature = "vetkeys")))]
compile_error!("At least one of the features (secp256k1, ed25519, vetkeys) must be enabled");

pub use ic_management_canister_types::{
    CanisterId, EcdsaCurve, EcdsaKeyId, EcdsaPublicKeyArgs, EcdsaPublicKeyResult, SchnorrAlgorithm,
    SchnorrKeyId, SchnorrPublicKeyArgs, SchnorrPublicKeyResult, VetKDCurve, VetKDKeyId,
    VetKDPublicKeyArgs, VetKDPublicKeyResult,
};

/// Error that can occur during public key derivation
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error {
    /// The specified master public key is not known
    UnknownKeyIdentifier,
    /// The algorithm is not supported (possibly due to an unset feature)
    AlgorithmNotSupported,
    /// The canister must be specified in the argument structs since the library
    /// has no other way of determining the correct value, and the canister id
    /// is a necessary component of the key derivation.
    CanisterIdMissing,
    /// The derivation path is not valid for this algorithm
    ///
    /// This mostly affects VetKD derivation, which only supports a single
    /// context string rather than a sequence of them.
    InvalidPath,
}

enum MasterPublicKeyInner {
    #[cfg(feature = "secp256k1")]
    EcdsaSecp256k1(ic_secp256k1::PublicKey),
    #[cfg(feature = "secp256k1")]
    Bip340Secp256k1(ic_secp256k1::PublicKey),
    #[cfg(feature = "ed25519")]
    Ed25519(ic_ed25519::PublicKey),
    #[cfg(feature = "vetkeys")]
    VetKD(ic_vetkeys::MasterPublicKey),
}

/// The master public key of a threshold signature system
pub struct MasterPublicKey {
    inner: MasterPublicKeyInner,
}

impl MasterPublicKey {
    /// Derive the master key for a canister
    pub fn derive_canister_key(&self, canister_id: &CanisterId) -> CanisterMasterKey {
        let inner = match &self.inner {
            #[cfg(feature = "secp256k1")]
            MasterPublicKeyInner::EcdsaSecp256k1(mk) => {
                let path = ic_secp256k1::DerivationPath::new(vec![ic_secp256k1::DerivationIndex(
                    canister_id.as_slice().to_vec(),
                )]);
                DerivedPublicKeyInner::EcdsaSecp256k1(mk.derive_subkey(&path))
            }
            #[cfg(feature = "secp256k1")]
            MasterPublicKeyInner::Bip340Secp256k1(mk) => {
                let path = ic_secp256k1::DerivationPath::new(vec![ic_secp256k1::DerivationIndex(
                    canister_id.as_slice().to_vec(),
                )]);
                DerivedPublicKeyInner::Bip340Secp256k1(mk.derive_subkey(&path))
            }
            #[cfg(feature = "ed25519")]
            MasterPublicKeyInner::Ed25519(mk) => {
                let path = ic_ed25519::DerivationPath::new(vec![ic_ed25519::DerivationIndex(
                    canister_id.as_slice().to_vec(),
                )]);
                DerivedPublicKeyInner::Ed25519(mk.derive_subkey(&path))
            }
            #[cfg(feature = "vetkeys")]
            MasterPublicKeyInner::VetKD(mk) => {
                DerivedPublicKeyInner::VetKD(mk.derive_canister_key(canister_id.as_slice()))
            }
        };
        CanisterMasterKey { inner }
    }
}

impl TryFrom<&EcdsaKeyId> for MasterPublicKey {
    type Error = Error;

    fn try_from(key_id: &EcdsaKeyId) -> Result<Self, Self::Error> {
        if key_id.curve != EcdsaCurve::Secp256k1 {
            return Err(Error::AlgorithmNotSupported);
        }

        #[cfg(feature = "secp256k1")]
        {
            let mk = match (key_id.curve, key_id.name.as_ref()) {
                (EcdsaCurve::Secp256k1, "key_1") => {
                    ic_secp256k1::PublicKey::mainnet_key(ic_secp256k1::MasterPublicKeyId::EcdsaKey1)
                }
                (EcdsaCurve::Secp256k1, "test_key_1") => ic_secp256k1::PublicKey::mainnet_key(
                    ic_secp256k1::MasterPublicKeyId::EcdsaTestKey1,
                ),
                (EcdsaCurve::Secp256k1, "pocketic_key_1") => ic_secp256k1::PublicKey::pocketic_key(
                    ic_secp256k1::PocketIcMasterPublicKeyId::EcdsaKey1,
                ),
                (EcdsaCurve::Secp256k1, "pocketic_test_key_1") => {
                    ic_secp256k1::PublicKey::pocketic_key(
                        ic_secp256k1::PocketIcMasterPublicKeyId::EcdsaTestKey1,
                    )
                }
                (EcdsaCurve::Secp256k1, "dfx_test_key") => ic_secp256k1::PublicKey::pocketic_key(
                    ic_secp256k1::PocketIcMasterPublicKeyId::EcdsaDfxTestKey,
                ),
                (_, _) => return Err(Error::UnknownKeyIdentifier),
            };

            let inner = MasterPublicKeyInner::EcdsaSecp256k1(mk);
            Ok(Self { inner })
        }

        #[cfg(not(feature = "secp256k1"))]
        {
            Err(Error::AlgorithmNotSupported)
        }
    }
}

impl TryFrom<&SchnorrKeyId> for MasterPublicKey {
    type Error = Error;

    fn try_from(key_id: &SchnorrKeyId) -> Result<Self, Self::Error> {
        #[cfg(feature = "secp256k1")]
        {
            if key_id.algorithm == SchnorrAlgorithm::Bip340secp256k1 {
                let mk = match key_id.name.as_ref() {
                    "key_1" => ic_secp256k1::PublicKey::mainnet_key(
                        ic_secp256k1::MasterPublicKeyId::SchnorrKey1,
                    ),
                    "test_key_1" => ic_secp256k1::PublicKey::mainnet_key(
                        ic_secp256k1::MasterPublicKeyId::SchnorrTestKey1,
                    ),
                    "pocketic_key_1" => ic_secp256k1::PublicKey::pocketic_key(
                        ic_secp256k1::PocketIcMasterPublicKeyId::SchnorrKey1,
                    ),
                    "pocketic_test_key_1" => ic_secp256k1::PublicKey::pocketic_key(
                        ic_secp256k1::PocketIcMasterPublicKeyId::SchnorrTestKey1,
                    ),
                    "dfx_test_key" => ic_secp256k1::PublicKey::pocketic_key(
                        ic_secp256k1::PocketIcMasterPublicKeyId::SchnorrDfxTestKey,
                    ),
                    _ => return Err(Error::UnknownKeyIdentifier),
                };

                let inner = MasterPublicKeyInner::Bip340Secp256k1(mk);
                return Ok(Self { inner });
            }
        }

        #[cfg(feature = "ed25519")]
        {
            if key_id.algorithm == SchnorrAlgorithm::Ed25519 {
                let mk = match key_id.name.as_ref() {
                    "key_1" => {
                        ic_ed25519::PublicKey::mainnet_key(ic_ed25519::MasterPublicKeyId::Key1)
                    }
                    "test_key_1" => {
                        ic_ed25519::PublicKey::mainnet_key(ic_ed25519::MasterPublicKeyId::TestKey1)
                    }
                    "pocketic_key_1" => ic_ed25519::PublicKey::pocketic_key(
                        ic_ed25519::PocketIcMasterPublicKeyId::Key1,
                    ),
                    "pocketic_test_key_1" => ic_ed25519::PublicKey::pocketic_key(
                        ic_ed25519::PocketIcMasterPublicKeyId::TestKey1,
                    ),
                    "dfx_test_key" => ic_ed25519::PublicKey::pocketic_key(
                        ic_ed25519::PocketIcMasterPublicKeyId::DfxTestKey,
                    ),
                    _ => return Err(Error::UnknownKeyIdentifier),
                };

                let inner = MasterPublicKeyInner::Ed25519(mk);
                return Ok(Self { inner });
            }
        }

        Err(Error::AlgorithmNotSupported)
    }
}

impl TryFrom<&VetKDKeyId> for MasterPublicKey {
    type Error = Error;

    fn try_from(key_id: &VetKDKeyId) -> Result<Self, Self::Error> {
        #[cfg(feature = "vetkeys")]
        {
            if let Some(mk) = ic_vetkeys::MasterPublicKey::for_mainnet_key(key_id) {
                let inner = MasterPublicKeyInner::VetKD(mk);
                return Ok(Self { inner });
            }
        }

        Err(Error::AlgorithmNotSupported)
    }
}

enum DerivedPublicKeyInner {
    #[cfg(feature = "secp256k1")]
    EcdsaSecp256k1((ic_secp256k1::PublicKey, [u8; 32])),
    #[cfg(feature = "secp256k1")]
    Bip340Secp256k1((ic_secp256k1::PublicKey, [u8; 32])),
    #[cfg(feature = "ed25519")]
    Ed25519((ic_ed25519::PublicKey, [u8; 32])),
    #[cfg(feature = "vetkeys")]
    VetKD(ic_vetkeys::DerivedPublicKey),
}

/// The canister's master public key of a threshold signature system
///
/// Each canister gets its own canister master key, which is derived from
/// the system master key.
pub struct CanisterMasterKey {
    inner: DerivedPublicKeyInner,
}

impl CanisterMasterKey {
    /// Derive the public key from a canister key and a single contextual input
    ///
    /// VetKeys requires exactly one contextual input be supplied. In addition,
    /// if that contextual input is the empty bytestring then the "derived key"
    /// is identical to the canister master public key. This matches the
    /// behavior of the management canister interface.
    ///
    /// For other keys, which support a path of inputs, this is equivalent to deriving
    /// using a path of length 1
    pub fn derive_key_with_context(&self, context: &[u8]) -> DerivedPublicKey {
        let inner = match &self.inner {
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::EcdsaSecp256k1(ck) => {
                let path = ic_secp256k1::DerivationPath::new(vec![ic_secp256k1::DerivationIndex(
                    context.to_vec(),
                )]);
                DerivedPublicKeyInner::EcdsaSecp256k1(
                    ck.0.derive_subkey_with_chain_code(&path, &ck.1),
                )
            }
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::Bip340Secp256k1(ck) => {
                let path = ic_secp256k1::DerivationPath::new(vec![ic_secp256k1::DerivationIndex(
                    context.to_vec(),
                )]);
                DerivedPublicKeyInner::Bip340Secp256k1(
                    ck.0.derive_subkey_with_chain_code(&path, &ck.1),
                )
            }
            #[cfg(feature = "ed25519")]
            DerivedPublicKeyInner::Ed25519(ck) => {
                let path = ic_ed25519::DerivationPath::new(vec![ic_ed25519::DerivationIndex(
                    context.to_vec(),
                )]);
                DerivedPublicKeyInner::Ed25519(ck.0.derive_subkey_with_chain_code(&path, &ck.1))
            }
            #[cfg(feature = "vetkeys")]
            DerivedPublicKeyInner::VetKD(ck) => {
                DerivedPublicKeyInner::VetKD(ck.derive_sub_key(context))
            }
        };
        DerivedPublicKey { inner }
    }

    /// Derive a public key using a path of contextual inputs
    ///
    /// Note that VetKD does not support derivation paths, but only a single context string,
    /// so VetKD is not supported by this function.
    pub fn derive_key(&self, path: &[Vec<u8>]) -> Result<DerivedPublicKey, Error> {
        let inner = match &self.inner {
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::EcdsaSecp256k1(ck) => {
                let path = ic_secp256k1::DerivationPath::new(
                    path.iter()
                        .cloned()
                        .map(ic_secp256k1::DerivationIndex)
                        .collect(),
                );
                DerivedPublicKeyInner::EcdsaSecp256k1(
                    ck.0.derive_subkey_with_chain_code(&path, &ck.1),
                )
            }
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::Bip340Secp256k1(ck) => {
                let path = ic_secp256k1::DerivationPath::new(
                    path.iter()
                        .cloned()
                        .map(ic_secp256k1::DerivationIndex)
                        .collect(),
                );
                DerivedPublicKeyInner::Bip340Secp256k1(
                    ck.0.derive_subkey_with_chain_code(&path, &ck.1),
                )
            }
            #[cfg(feature = "ed25519")]
            DerivedPublicKeyInner::Ed25519(ck) => {
                let path = ic_ed25519::DerivationPath::new(
                    path.iter()
                        .cloned()
                        .map(ic_ed25519::DerivationIndex)
                        .collect(),
                );
                DerivedPublicKeyInner::Ed25519(ck.0.derive_subkey_with_chain_code(&path, &ck.1))
            }
            #[cfg(feature = "vetkeys")]
            DerivedPublicKeyInner::VetKD(_ck) => {
                // VetKD has a somewhat different design for derivation than used by
                // the other threshold schemes - it supports only a single input rather
                // than a path. To avoid risk of confusing behavior, just reject
                return Err(Error::AlgorithmNotSupported);
            }
        };
        Ok(DerivedPublicKey { inner })
    }

    /// Return the serialized encoding of the canister master public key
    pub fn serialize(&self) -> Vec<u8> {
        match &self.inner {
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::EcdsaSecp256k1(ck) => ck.0.serialize_sec1(true),
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::Bip340Secp256k1(ck) => ck.0.serialize_sec1(true),
            #[cfg(feature = "ed25519")]
            DerivedPublicKeyInner::Ed25519(ck) => ck.0.serialize_raw().to_vec(),
            #[cfg(feature = "vetkeys")]
            DerivedPublicKeyInner::VetKD(ck) => ck.serialize(),
        }
    }

    /// Return the chain code used for further derivation, if relevant
    ///
    /// Returns None if not applicable for this algorithm
    pub fn chain_code(&self) -> Option<Vec<u8>> {
        match &self.inner {
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::EcdsaSecp256k1(ck) => Some(ck.1.to_vec()),
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::Bip340Secp256k1(ck) => Some(ck.1.to_vec()),
            #[cfg(feature = "ed25519")]
            DerivedPublicKeyInner::Ed25519(ck) => Some(ck.1.to_vec()),
            #[cfg(feature = "vetkeys")]
            DerivedPublicKeyInner::VetKD(_ck) => None,
        }
    }
}

/// A public key ultimately derived from a master key
pub struct DerivedPublicKey {
    inner: DerivedPublicKeyInner,
}

impl DerivedPublicKey {
    /// Return the serialized encoding of the derived public key
    pub fn serialize(&self) -> Vec<u8> {
        match &self.inner {
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::EcdsaSecp256k1(ck) => ck.0.serialize_sec1(true),
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::Bip340Secp256k1(ck) => ck.0.serialize_sec1(true),
            #[cfg(feature = "ed25519")]
            DerivedPublicKeyInner::Ed25519(ck) => ck.0.serialize_raw().to_vec(),
            #[cfg(feature = "vetkeys")]
            DerivedPublicKeyInner::VetKD(ck) => ck.serialize(),
        }
    }

    /// Return the chain code used for further derivation, if relevant
    ///
    /// Returns None if not applicable for this algorithm
    pub fn chain_code(&self) -> Option<Vec<u8>> {
        match &self.inner {
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::EcdsaSecp256k1(ck) => Some(ck.1.to_vec()),
            #[cfg(feature = "secp256k1")]
            DerivedPublicKeyInner::Bip340Secp256k1(ck) => Some(ck.1.to_vec()),
            #[cfg(feature = "ed25519")]
            DerivedPublicKeyInner::Ed25519(ck) => Some(ck.1.to_vec()),
            #[cfg(feature = "vetkeys")]
            DerivedPublicKeyInner::VetKD(_ck) => None,
        }
    }
}

/// Derive an ECDSA public key
///
/// This is an offline equivalent to the `ecdsa_public_key` management canister call
///
/// As an extension of the management canister call this function also
/// supports the keys that are used (for testing) by PocketIC.
///
/// See [IC method `ecdsa_public_key`](https://internetcomputer.org/docs/references/ic-interface-spec/#ic-ecdsa_public_key).
///
/// # Supported Keys
///
/// The following key IDs are currently supported
///
/// * `key_1`: The production key used on mainnet
/// * `test_key_1`: The test key used on mainnet
/// * `pocketic_key_1`: The key used by PocketIC when a request to the management canister
///   uses `key_1`.
/// * `pocketic_test_key_1`: The key used by PocketIC when a request to the management canister
///   uses `test_key_1`.
/// * `dfx_test_key`: PocketIC specific test key
pub fn derive_ecdsa_key(args: &EcdsaPublicKeyArgs) -> Result<EcdsaPublicKeyResult, Error> {
    let canister_id = args.canister_id.ok_or(Error::CanisterIdMissing)?;

    let dk = MasterPublicKey::try_from(&args.key_id)?
        .derive_canister_key(&canister_id)
        .derive_key(&args.derivation_path)?;

    Ok(EcdsaPublicKeyResult {
        public_key: dk.serialize(),
        chain_code: dk.chain_code().expect("Missing chain code"),
    })
}

/// Derive a Schnorr public key
///
/// This is an offline equivalent to the `schnorr_public_key` management canister call
///
/// See [IC method `schnorr_public_key`](https://internetcomputer.org/docs/references/ic-interface-spec/#ic-schnorr_public_key).
///
/// # Supported Keys
///
/// Currently this function supports both Ed25519 and BIP340 Schnorr key derivation,
/// using any of the following key names:
///
/// * `key_1`: The production key used on mainnet
/// * `test_key_1`: The test key used on mainnet
/// * `pocketic_key_1`: The key used by PocketIC when a request to the management canister
///   uses `key_1`.
/// * `pocketic_test_key_1`: The key used by PocketIC when a request to the management canister
///   uses `test_key_1`.
/// * `dfx_test_key`: PocketIC specific test key
pub fn derive_schnorr_key(args: &SchnorrPublicKeyArgs) -> Result<SchnorrPublicKeyResult, Error> {
    let canister_id = args.canister_id.ok_or(Error::CanisterIdMissing)?;

    let dk = MasterPublicKey::try_from(&args.key_id)?
        .derive_canister_key(&canister_id)
        .derive_key(&args.derivation_path)?;

    Ok(SchnorrPublicKeyResult {
        public_key: dk.serialize(),
        chain_code: dk.chain_code().expect("Missing chain code"),
    })
}

/// Derive a VetKD public key
///
/// This is an offline equivalent to the `vetkd_public_key` management canister call
///
/// See [IC method `vetkd_public_key`](https://internetcomputer.org/docs/references/ic-interface-spec/#ic-vetkd_public_key)
pub fn derive_vetkd_key(args: &VetKDPublicKeyArgs) -> Result<VetKDPublicKeyResult, Error> {
    let canister_id = args.canister_id.ok_or(Error::CanisterIdMissing)?;

    let ck = MasterPublicKey::try_from(&args.key_id)?.derive_canister_key(&canister_id);

    let dk = ck.derive_key_with_context(&args.context);
    Ok(VetKDPublicKeyResult {
        public_key: dk.serialize(),
    })
}