Skip to content

Commit

Permalink
Use digest(as = ...) attribute
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Varlakov <[email protected]>
  • Loading branch information
survived committed Aug 22, 2024
1 parent 0266195 commit 6061b29
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cggmp21-keygen/src/non_threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct MsgRound2<E: Curve, L: SecurityLevel> {
/// Party contribution to chain code
#[cfg(feature = "hd-wallets")]
#[serde_as(as = "Option<utils::HexOrBin>")]
#[udigest(with = utils::encoding::maybe_bytes)]
#[udigest(as = Option<udigest::Bytes>)]
pub chain_code: Option<slip_10::ChainCode>,
/// $u_i$
#[serde(with = "hex::serde")]
Expand Down
2 changes: 1 addition & 1 deletion cggmp21-keygen/src/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct MsgRound2Broad<E: Curve, L: SecurityLevel> {
/// Party contribution to chain code
#[cfg(feature = "hd-wallets")]
#[serde_as(as = "Option<utils::HexOrBin>")]
#[udigest(with = utils::encoding::maybe_bytes)]
#[udigest(as = Option<udigest::Bytes>)]
pub chain_code: Option<slip_10::ChainCode>,
/// $u_i$
#[serde(with = "hex::serde")]
Expand Down
12 changes: 0 additions & 12 deletions cggmp21-keygen/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,3 @@ where
pub fn iter_peers(i: u16, n: u16) -> impl Iterator<Item = u16> {
(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<B: udigest::Buffer>(
m: &Option<impl AsRef<[u8]>>,
encoder: udigest::encoding::EncodeValue<B>,
) {
use udigest::Digestable;
m.as_ref().map(udigest::Bytes).unambiguously_encode(encoder)
}
}
6 changes: 3 additions & 3 deletions cggmp21/src/key_refresh/aux_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ pub struct MsgRound1<D: Digest> {
#[serde(bound = "")]
pub struct MsgRound2<L: SecurityLevel> {
/// $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
Expand Down
6 changes: 3 additions & 3 deletions cggmp21/src/key_refresh/non_threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ pub struct MsgRound2<E: Curve, L: SecurityLevel> {
/// $\vec A_i$
pub sch_commits_a: Vec<schnorr_pok::Commit<E>>,
/// $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
Expand Down
4 changes: 2 additions & 2 deletions cggmp21/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
23 changes: 7 additions & 16 deletions cggmp21/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B: udigest::Buffer>(
x: &rug::Integer,
encoder: udigest::encoding::EncodeValue<B>,
) {
encoder
.encode_leaf()
.chain(x.to_digits(rug::integer::Order::Msf));
}

pub fn integers_list<B: udigest::Buffer>(
list: impl AsRef<[rug::Integer]>,
encoder: udigest::encoding::EncodeValue<B>,
) {
let mut encoder = encoder.encode_list();
for x in list.as_ref() {
integer(x, encoder.add_item())
pub struct Integer;
impl udigest::DigestAs<rug::Integer> for Integer {
fn digest_as<B: udigest::Buffer>(
x: &rug::Integer,
encoder: udigest::encoding::EncodeValue<B>,
) {
encoder.encode_leaf_value(x.to_digits(rug::integer::Order::Msf))
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions cggmp21/src/zk/ring_pedersen_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ struct Challenge<const M: usize> {
/// 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,
}

Expand All @@ -34,10 +34,10 @@ pub struct Data<'a> {
#[derive(Clone, Serialize, Deserialize, udigest::Digestable)]
pub struct Proof<const M: usize> {
#[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],
}

Expand All @@ -51,7 +51,7 @@ fn derive_challenge<const M: usize, D: Digest>(
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],
}

Expand Down
4 changes: 2 additions & 2 deletions key-share/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub struct DirtyKeyInfo<E: Curve> {
// `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<E>,
/// Public key corresponding to shared secret key. Corresponds to _X_ in paper.
#[cfg_attr(feature = "serde", serde(with = "As::<generic_ec::serde::Compact>"))]
Expand All @@ -231,7 +231,7 @@ pub struct DirtyKeyInfo<E: Curve> {
serde(skip_serializing_if = "Option::is_none"),
serde(with = "As::<Option<utils::HexOrBin>>")
)]
#[cfg_attr(feature = "udigest", udigest(with = utils::encoding::maybe_bytes))]
#[cfg_attr(feature = "udigest", udigest(as = Option<udigest::Bytes>))]
pub chain_code: Option<slip_10::ChainCode>,
}

Expand Down
22 changes: 8 additions & 14 deletions key-share/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@ use hex as _;

#[cfg(feature = "udigest")]
pub mod encoding {
pub fn curve_name<B: udigest::Buffer, E: generic_ec::Curve>(
_value: &generic_ec::serde::CurveName<E>,
encoder: udigest::encoding::EncodeValue<B>,
) {
encoder.encode_leaf_value(E::CURVE_NAME)
}

#[cfg(feature = "hd-wallets")]
pub fn maybe_bytes<B: udigest::Buffer>(
m: &Option<impl AsRef<[u8]>>,
encoder: udigest::encoding::EncodeValue<B>,
) {
use udigest::Digestable;
m.as_ref().map(udigest::Bytes).unambiguously_encode(encoder)
pub struct CurveName;
impl<E: generic_ec::Curve> udigest::DigestAs<generic_ec::serde::CurveName<E>> for CurveName {
fn digest_as<B: udigest::Buffer>(
_value: &generic_ec::serde::CurveName<E>,
encoder: udigest::encoding::EncodeValue<B>,
) {
encoder.encode_leaf_value(E::CURVE_NAME)
}
}
}

Expand Down

0 comments on commit 6061b29

Please sign in to comment.