diff --git a/cggmp21-keygen/src/non_threshold.rs b/cggmp21-keygen/src/non_threshold.rs index 17318dd..d18cd03 100644 --- a/cggmp21-keygen/src/non_threshold.rs +++ b/cggmp21-keygen/src/non_threshold.rs @@ -68,7 +68,7 @@ pub struct MsgRound2 { /// Party contribution to chain code #[cfg(feature = "hd-wallets")] #[serde_as(as = "Option")] - #[udigest(with = utils::encoding::maybe_bytes)] + #[udigest(as = Option)] pub chain_code: Option, /// $u_i$ #[serde(with = "hex::serde")] diff --git a/cggmp21-keygen/src/threshold.rs b/cggmp21-keygen/src/threshold.rs index 30a0feb..614e506 100644 --- a/cggmp21-keygen/src/threshold.rs +++ b/cggmp21-keygen/src/threshold.rs @@ -71,7 +71,7 @@ pub struct MsgRound2Broad { /// Party contribution to chain code #[cfg(feature = "hd-wallets")] #[serde_as(as = "Option")] - #[udigest(with = utils::encoding::maybe_bytes)] + #[udigest(as = Option)] pub chain_code: Option, /// $u_i$ #[serde(with = "hex::serde")] diff --git a/cggmp21-keygen/src/utils.rs b/cggmp21-keygen/src/utils.rs index 8d888e3..02ec485 100644 --- a/cggmp21-keygen/src/utils.rs +++ b/cggmp21-keygen/src/utils.rs @@ -90,15 +90,3 @@ where pub fn iter_peers(i: u16, n: u16) -> impl Iterator { (0..n).filter(move |x| *x != i) } - -/// Unambiguous encoding for different types for which it was not defined -pub mod encoding { - #[cfg(feature = "hd-wallets")] - pub fn maybe_bytes( - m: &Option>, - encoder: udigest::encoding::EncodeValue, - ) { - use udigest::Digestable; - m.as_ref().map(udigest::Bytes).unambiguously_encode(encoder) - } -} diff --git a/cggmp21/src/key_refresh/aux_only.rs b/cggmp21/src/key_refresh/aux_only.rs index f592b7d..8eff76a 100644 --- a/cggmp21/src/key_refresh/aux_only.rs +++ b/cggmp21/src/key_refresh/aux_only.rs @@ -65,13 +65,13 @@ pub struct MsgRound1 { #[serde(bound = "")] pub struct MsgRound2 { /// $N_i$ - #[udigest(with = utils::encoding::integer)] + #[udigest(as = utils::encoding::Integer)] pub N: Integer, /// $s_i$ - #[udigest(with = utils::encoding::integer)] + #[udigest(as = utils::encoding::Integer)] pub s: Integer, /// $t_i$ - #[udigest(with = utils::encoding::integer)] + #[udigest(as = utils::encoding::Integer)] pub t: Integer, /// $\hat \psi_i$ // this should be L::M instead, but no rustc support yet diff --git a/cggmp21/src/key_refresh/non_threshold.rs b/cggmp21/src/key_refresh/non_threshold.rs index a0d4401..53daea4 100644 --- a/cggmp21/src/key_refresh/non_threshold.rs +++ b/cggmp21/src/key_refresh/non_threshold.rs @@ -77,13 +77,13 @@ pub struct MsgRound2 { /// $\vec A_i$ pub sch_commits_a: Vec>, /// $N_i$ - #[udigest(with = utils::encoding::integer)] + #[udigest(as = utils::encoding::Integer)] pub N: Integer, /// $s_i$ - #[udigest(with = utils::encoding::integer)] + #[udigest(as = utils::encoding::Integer)] pub s: Integer, /// $t_i$ - #[udigest(with = utils::encoding::integer)] + #[udigest(as = utils::encoding::Integer)] pub t: Integer, /// $\hat \psi_i$ // this should be L::M instead, but no rustc support yet diff --git a/cggmp21/src/signing.rs b/cggmp21/src/signing.rs index 514614c..d5eee69 100644 --- a/cggmp21/src/signing.rs +++ b/cggmp21/src/signing.rs @@ -153,10 +153,10 @@ pub mod msg { #[udigest(tag = prefixed!("round1"))] pub struct MsgRound1a { /// $K_i$ - #[udigest(with = utils::encoding::integer)] + #[udigest(as = utils::encoding::Integer)] pub K: fast_paillier::Ciphertext, /// $G_i$ - #[udigest(with = utils::encoding::integer)] + #[udigest(as = utils::encoding::Integer)] pub G: fast_paillier::Ciphertext, } diff --git a/cggmp21/src/utils.rs b/cggmp21/src/utils.rs index 0dfdb0b..7bf4831 100644 --- a/cggmp21/src/utils.rs +++ b/cggmp21/src/utils.rs @@ -219,22 +219,13 @@ pub fn generate_blum_prime(rng: &mut impl rand_core::RngCore, bits_size: u32) -> pub mod encoding { use paillier_zk::rug; - pub fn integer( - x: &rug::Integer, - encoder: udigest::encoding::EncodeValue, - ) { - encoder - .encode_leaf() - .chain(x.to_digits(rug::integer::Order::Msf)); - } - - pub fn integers_list( - list: impl AsRef<[rug::Integer]>, - encoder: udigest::encoding::EncodeValue, - ) { - let mut encoder = encoder.encode_list(); - for x in list.as_ref() { - integer(x, encoder.add_item()) + pub struct Integer; + impl udigest::DigestAs for Integer { + fn digest_as( + x: &rug::Integer, + encoder: udigest::encoding::EncodeValue, + ) { + encoder.encode_leaf_value(x.to_digits(rug::integer::Order::Msf)) } } } diff --git a/cggmp21/src/zk/ring_pedersen_parameters.rs b/cggmp21/src/zk/ring_pedersen_parameters.rs index 4cdccee..477bc87 100644 --- a/cggmp21/src/zk/ring_pedersen_parameters.rs +++ b/cggmp21/src/zk/ring_pedersen_parameters.rs @@ -17,11 +17,11 @@ struct Challenge { /// Data to construct proof about #[derive(Clone, Copy, udigest::Digestable)] pub struct Data<'a> { - #[udigest(with = crate::utils::encoding::integer)] + #[udigest(as = &crate::utils::encoding::Integer)] pub N: &'a Integer, - #[udigest(with = crate::utils::encoding::integer)] + #[udigest(as = &crate::utils::encoding::Integer)] pub s: &'a Integer, - #[udigest(with = crate::utils::encoding::integer)] + #[udigest(as = &crate::utils::encoding::Integer)] pub t: &'a Integer, } @@ -34,10 +34,10 @@ pub struct Data<'a> { #[derive(Clone, Serialize, Deserialize, udigest::Digestable)] pub struct Proof { #[serde_as(as = "[_; M]")] - #[udigest(with = crate::utils::encoding::integers_list)] + #[udigest(as = [crate::utils::encoding::Integer; M])] pub commitment: [Integer; M], #[serde_as(as = "[_; M]")] - #[udigest(with = crate::utils::encoding::integers_list)] + #[udigest(as = [crate::utils::encoding::Integer; M])] pub zs: [Integer; M], } @@ -51,7 +51,7 @@ fn derive_challenge( struct Seed<'a, S: udigest::Digestable, const M: usize> { shared_state: &'a S, data: Data<'a>, - #[udigest(with = crate::utils::encoding::integers_list)] + #[udigest(as = &[crate::utils::encoding::Integer; M])] commitment: &'a [Integer; M], } diff --git a/key-share/src/lib.rs b/key-share/src/lib.rs index 44b7688..7bdc02a 100644 --- a/key-share/src/lib.rs +++ b/key-share/src/lib.rs @@ -204,7 +204,7 @@ pub struct DirtyKeyInfo { // `crate::serde_fix` as well! // /// Guard that ensures curve consistency for deseraization - #[cfg_attr(feature = "udigest", udigest(with = utils::encoding::curve_name))] + #[cfg_attr(feature = "udigest", udigest(as = utils::encoding::CurveName))] pub curve: CurveName, /// Public key corresponding to shared secret key. Corresponds to _X_ in paper. #[cfg_attr(feature = "serde", serde(with = "As::"))] @@ -231,7 +231,7 @@ pub struct DirtyKeyInfo { serde(skip_serializing_if = "Option::is_none"), serde(with = "As::>") )] - #[cfg_attr(feature = "udigest", udigest(with = utils::encoding::maybe_bytes))] + #[cfg_attr(feature = "udigest", udigest(as = Option))] pub chain_code: Option, } diff --git a/key-share/src/utils.rs b/key-share/src/utils.rs index c73ddf1..7448f15 100644 --- a/key-share/src/utils.rs +++ b/key-share/src/utils.rs @@ -12,20 +12,14 @@ use hex as _; #[cfg(feature = "udigest")] pub mod encoding { - pub fn curve_name( - _value: &generic_ec::serde::CurveName, - encoder: udigest::encoding::EncodeValue, - ) { - encoder.encode_leaf_value(E::CURVE_NAME) - } - - #[cfg(feature = "hd-wallets")] - pub fn maybe_bytes( - m: &Option>, - encoder: udigest::encoding::EncodeValue, - ) { - use udigest::Digestable; - m.as_ref().map(udigest::Bytes).unambiguously_encode(encoder) + pub struct CurveName; + impl udigest::DigestAs> for CurveName { + fn digest_as( + _value: &generic_ec::serde::CurveName, + encoder: udigest::encoding::EncodeValue, + ) { + encoder.encode_leaf_value(E::CURVE_NAME) + } } }