Skip to content

Commit

Permalink
chore(crypto): CRP-2394 Add AlgorithmId for threshold BIP340 Schnorr
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Feb 2, 2024
1 parent 0df4ad0 commit 40717aa
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 6 deletions.
Expand Up @@ -119,7 +119,7 @@ mod common {
use proptest::prelude::{prop, Strategy};
use strum::IntoEnumIterator;

pub(crate) const MAX_ALGORITHM_ID_INDEX: i32 = 17;
pub(crate) const MAX_ALGORITHM_ID_INDEX: i32 = 18;

prop_compose! {
pub fn arb_key_id()(id in uniform32(any::<u8>())) -> KeyId {
Expand Down
Expand Up @@ -4,7 +4,7 @@ use crate::common::MAX_ALGORITHM_ID_INDEX;
#[test]
fn should_be_maximal_algorithm_index_id_to_ensure_all_variants_covered_by_strategy() {
assert_eq!(
AlgorithmId::ThresholdEcdsaSecp256r1,
AlgorithmId::ThresholdSchnorrBip340,
AlgorithmId::from(MAX_ALGORITHM_ID_INDEX)
);
assert_eq!(
Expand Down
6 changes: 5 additions & 1 deletion rs/crypto/tests/integration_test.rs
Expand Up @@ -745,7 +745,7 @@ fn should_fail_check_keys_with_registry_if_no_idkg_key_in_registry() {
/// Ensure the structs are consistent and then update the test below.
#[test]
fn algorithm_id_should_match_algorithm_id_proto() {
let algorithm_id_variants = 18;
let algorithm_id_variants = 19;
assert_eq!(AlgorithmId::iter().count(), algorithm_id_variants);

for i in 0..algorithm_id_variants {
Expand Down Expand Up @@ -822,6 +822,10 @@ fn algorithm_id_should_match_algorithm_id_proto() {
AlgorithmId::ThresholdEcdsaSecp256r1 as i32,
AlgorithmIdProto::ThresholdEcdsaSecp256r1 as i32
);
assert_eq!(
AlgorithmId::ThresholdSchnorrBip340 as i32,
AlgorithmIdProto::ThresholdSchnorrBip340 as i32
);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions rs/protobuf/def/registry/crypto/v1/crypto.proto
Expand Up @@ -25,6 +25,7 @@ enum AlgorithmId {
ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1 = 15;
ALGORITHM_ID_MEGA_SECP_256K1 = 16;
ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1 = 17;
ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340 = 18;
}

// A list of subnets that can sign with this ECDSA key.
Expand Down
3 changes: 3 additions & 0 deletions rs/protobuf/src/gen/crypto/registry.crypto.v1.rs
Expand Up @@ -75,6 +75,7 @@ pub enum AlgorithmId {
ThresholdEcdsaSecp256k1 = 15,
MegaSecp256k1 = 16,
ThresholdEcdsaSecp256r1 = 17,
ThresholdSchnorrBip340 = 18,
}
impl AlgorithmId {
/// String value of the enum field names used in the ProtoBuf definition.
Expand All @@ -101,6 +102,7 @@ impl AlgorithmId {
AlgorithmId::ThresholdEcdsaSecp256k1 => "ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1",
AlgorithmId::MegaSecp256k1 => "ALGORITHM_ID_MEGA_SECP_256K1",
AlgorithmId::ThresholdEcdsaSecp256r1 => "ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1",
AlgorithmId::ThresholdSchnorrBip340 => "ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand All @@ -124,6 +126,7 @@ impl AlgorithmId {
"ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1" => Some(Self::ThresholdEcdsaSecp256k1),
"ALGORITHM_ID_MEGA_SECP_256K1" => Some(Self::MegaSecp256k1),
"ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1" => Some(Self::ThresholdEcdsaSecp256r1),
"ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340" => Some(Self::ThresholdSchnorrBip340),
_ => None,
}
}
Expand Down
3 changes: 3 additions & 0 deletions rs/protobuf/src/gen/registry/registry.crypto.v1.rs
Expand Up @@ -75,6 +75,7 @@ pub enum AlgorithmId {
ThresholdEcdsaSecp256k1 = 15,
MegaSecp256k1 = 16,
ThresholdEcdsaSecp256r1 = 17,
ThresholdSchnorrBip340 = 18,
}
impl AlgorithmId {
/// String value of the enum field names used in the ProtoBuf definition.
Expand All @@ -101,6 +102,7 @@ impl AlgorithmId {
AlgorithmId::ThresholdEcdsaSecp256k1 => "ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1",
AlgorithmId::MegaSecp256k1 => "ALGORITHM_ID_MEGA_SECP_256K1",
AlgorithmId::ThresholdEcdsaSecp256r1 => "ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1",
AlgorithmId::ThresholdSchnorrBip340 => "ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand All @@ -124,6 +126,7 @@ impl AlgorithmId {
"ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1" => Some(Self::ThresholdEcdsaSecp256k1),
"ALGORITHM_ID_MEGA_SECP_256K1" => Some(Self::MegaSecp256k1),
"ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1" => Some(Self::ThresholdEcdsaSecp256r1),
"ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340" => Some(Self::ThresholdSchnorrBip340),
_ => None,
}
}
Expand Down
3 changes: 3 additions & 0 deletions rs/protobuf/src/gen/state/registry.crypto.v1.rs
Expand Up @@ -59,6 +59,7 @@ pub enum AlgorithmId {
ThresholdEcdsaSecp256k1 = 15,
MegaSecp256k1 = 16,
ThresholdEcdsaSecp256r1 = 17,
ThresholdSchnorrBip340 = 18,
}
impl AlgorithmId {
/// String value of the enum field names used in the ProtoBuf definition.
Expand All @@ -85,6 +86,7 @@ impl AlgorithmId {
AlgorithmId::ThresholdEcdsaSecp256k1 => "ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1",
AlgorithmId::MegaSecp256k1 => "ALGORITHM_ID_MEGA_SECP_256K1",
AlgorithmId::ThresholdEcdsaSecp256r1 => "ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1",
AlgorithmId::ThresholdSchnorrBip340 => "ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand All @@ -108,6 +110,7 @@ impl AlgorithmId {
"ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1" => Some(Self::ThresholdEcdsaSecp256k1),
"ALGORITHM_ID_MEGA_SECP_256K1" => Some(Self::MegaSecp256k1),
"ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1" => Some(Self::ThresholdEcdsaSecp256r1),
"ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340" => Some(Self::ThresholdSchnorrBip340),
_ => None,
}
}
Expand Down
3 changes: 3 additions & 0 deletions rs/protobuf/src/gen/types/registry.crypto.v1.rs
Expand Up @@ -75,6 +75,7 @@ pub enum AlgorithmId {
ThresholdEcdsaSecp256k1 = 15,
MegaSecp256k1 = 16,
ThresholdEcdsaSecp256r1 = 17,
ThresholdSchnorrBip340 = 18,
}
impl AlgorithmId {
/// String value of the enum field names used in the ProtoBuf definition.
Expand All @@ -101,6 +102,7 @@ impl AlgorithmId {
AlgorithmId::ThresholdEcdsaSecp256k1 => "ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1",
AlgorithmId::MegaSecp256k1 => "ALGORITHM_ID_MEGA_SECP_256K1",
AlgorithmId::ThresholdEcdsaSecp256r1 => "ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1",
AlgorithmId::ThresholdSchnorrBip340 => "ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand All @@ -124,6 +126,7 @@ impl AlgorithmId {
"ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256K1" => Some(Self::ThresholdEcdsaSecp256k1),
"ALGORITHM_ID_MEGA_SECP_256K1" => Some(Self::MegaSecp256k1),
"ALGORITHM_ID_THRESHOLD_ECDSA_SECP_256R1" => Some(Self::ThresholdEcdsaSecp256r1),
"ALGORITHM_ID_THRESHOLD_SCHNORR_BIP340" => Some(Self::ThresholdSchnorrBip340),
_ => None,
}
}
Expand Down
2 changes: 2 additions & 0 deletions rs/types/types/src/crypto.rs
Expand Up @@ -146,6 +146,7 @@ pub enum AlgorithmId {
ThresholdEcdsaSecp256k1 = 15,
MegaSecp256k1 = 16,
ThresholdEcdsaSecp256r1 = 17,
ThresholdSchnorrBip340 = 18,
}

impl AlgorithmId {
Expand Down Expand Up @@ -213,6 +214,7 @@ impl From<i32> for AlgorithmId {
15 => AlgorithmId::ThresholdEcdsaSecp256k1,
16 => AlgorithmId::MegaSecp256k1,
17 => AlgorithmId::ThresholdEcdsaSecp256r1,
18 => AlgorithmId::ThresholdSchnorrBip340,
_ => AlgorithmId::Placeholder,
}
}
Expand Down
9 changes: 6 additions & 3 deletions rs/types/types/src/crypto/tests.rs
Expand Up @@ -5,7 +5,7 @@ use strum::IntoEnumIterator;
#[test]
fn should_correctly_convert_i32_to_algorithm_id() {
// ensure _all_ algorithm IDs are compared (i.e., no algorithm was forgotten)
assert_eq!(AlgorithmId::iter().count(), 18);
assert_eq!(AlgorithmId::iter().count(), 19);

assert_eq!(AlgorithmId::from(0), AlgorithmId::Placeholder);
assert_eq!(AlgorithmId::from(1), AlgorithmId::MultiBls12_381);
Expand All @@ -25,6 +25,7 @@ fn should_correctly_convert_i32_to_algorithm_id() {
assert_eq!(AlgorithmId::from(15), AlgorithmId::ThresholdEcdsaSecp256k1);
assert_eq!(AlgorithmId::from(16), AlgorithmId::MegaSecp256k1);
assert_eq!(AlgorithmId::from(17), AlgorithmId::ThresholdEcdsaSecp256r1);
assert_eq!(AlgorithmId::from(18), AlgorithmId::ThresholdSchnorrBip340);

// Verify that an unknown i32 maps onto Placeholder
assert_eq!(AlgorithmId::from(42), AlgorithmId::Placeholder);
Expand All @@ -36,7 +37,7 @@ fn should_correctly_convert_i32_to_algorithm_id() {
#[test]
fn should_correctly_convert_algorithm_id_to_i32() {
// ensure _all_ algorithm IDs are compared (i.e., no algorithm was forgotten)
assert_eq!(AlgorithmId::iter().count(), 18);
assert_eq!(AlgorithmId::iter().count(), 19);

assert_eq!(AlgorithmId::Placeholder as i32, 0);
assert_eq!(AlgorithmId::MultiBls12_381 as i32, 1);
Expand All @@ -56,12 +57,13 @@ fn should_correctly_convert_algorithm_id_to_i32() {
assert_eq!(AlgorithmId::ThresholdEcdsaSecp256k1 as i32, 15);
assert_eq!(AlgorithmId::MegaSecp256k1 as i32, 16);
assert_eq!(AlgorithmId::ThresholdEcdsaSecp256r1 as i32, 17);
assert_eq!(AlgorithmId::ThresholdSchnorrBip340 as i32, 18);
}

#[test]
fn should_correctly_convert_algorithm_id_to_u8() {
// ensure _all_ algorithm IDs are compared (i.e., no algorithm was forgotten)
assert_eq!(AlgorithmId::iter().count(), 18);
assert_eq!(AlgorithmId::iter().count(), 19);

let tests: Vec<(AlgorithmId, u8)> = vec![
(AlgorithmId::Placeholder, 0),
Expand All @@ -82,6 +84,7 @@ fn should_correctly_convert_algorithm_id_to_u8() {
(AlgorithmId::ThresholdEcdsaSecp256k1, 15),
(AlgorithmId::MegaSecp256k1, 16),
(AlgorithmId::ThresholdEcdsaSecp256r1, 17),
(AlgorithmId::ThresholdSchnorrBip340, 18),
];

for (algorithm_id, expected_discriminant) in tests {
Expand Down

0 comments on commit 40717aa

Please sign in to comment.