From e2f2ab89ecc1bb4e496fbf171415de8b36c686de Mon Sep 17 00:00:00 2001 From: Georgio Nicolas Date: Wed, 4 Jun 2025 23:33:45 +0200 Subject: [PATCH] dkg: add CryptoRng trait requirement --- common/dkg/benches/benchmarks.rs | 5 +++-- common/dkg/src/bte/encryption.rs | 8 ++++++-- common/dkg/src/bte/keys.rs | 8 ++++++-- common/dkg/src/bte/proof_chunking.rs | 8 +++++--- common/dkg/src/bte/proof_discrete_log.rs | 7 ++++++- common/dkg/src/bte/proof_sharing.rs | 6 ++++-- common/dkg/src/dealing.rs | 3 ++- common/dkg/src/interpolation/polynomial.rs | 3 ++- 8 files changed, 34 insertions(+), 14 deletions(-) diff --git a/common/dkg/benches/benchmarks.rs b/common/dkg/benches/benchmarks.rs index 2951a57a7c..37217bf45d 100644 --- a/common/dkg/benches/benchmarks.rs +++ b/common/dkg/benches/benchmarks.rs @@ -14,6 +14,7 @@ use nym_dkg::bte::{ }; use nym_dkg::interpolation::polynomial::Polynomial; use nym_dkg::{combine_shares, Dealing, NodeIndex, Share, Threshold}; +use rand::CryptoRng; use rand_core::{RngCore, SeedableRng}; use std::collections::BTreeMap; @@ -31,7 +32,7 @@ pub fn precomputing_g2_generator_for_miller_loop(c: &mut Criterion) { } fn prepare_keys( - mut rng: impl RngCore, + mut rng: impl RngCore + CryptoRng, nodes: usize, ) -> (BTreeMap, Vec) { let params = setup(); @@ -50,7 +51,7 @@ fn prepare_keys( } fn prepare_resharing( - mut rng: impl RngCore, + mut rng: impl RngCore + CryptoRng, params: &Params, nodes: usize, threshold: Threshold, diff --git a/common/dkg/src/bte/encryption.rs b/common/dkg/src/bte/encryption.rs index 8cd6a3e774..56fdd148cd 100644 --- a/common/dkg/src/bte/encryption.rs +++ b/common/dkg/src/bte/encryption.rs @@ -9,6 +9,7 @@ use crate::{Chunk, ChunkedShare, Share}; use bls12_381::{G1Affine, G1Projective, G2Prepared, G2Projective, Gt, Scalar}; use ff::Field; use group::{Curve, Group, GroupEncoding}; +use rand::CryptoRng; use rand_core::RngCore; use std::collections::HashMap; use std::ops::Neg; @@ -191,7 +192,7 @@ impl HazmatRandomness { pub fn encrypt_shares( shares: &[(&Share, &PublicKey)], params: &Params, - mut rng: impl RngCore, + mut rng: impl RngCore + CryptoRng, ) -> (Ciphertexts, HazmatRandomness) { let g1 = G1Projective::generator(); @@ -574,7 +575,10 @@ mod tests { #[test] fn ciphertexts_roundtrip() { - fn random_ciphertexts(mut rng: impl RngCore, num_receivers: usize) -> Ciphertexts { + fn random_ciphertexts( + mut rng: impl RngCore + CryptoRng, + num_receivers: usize, + ) -> Ciphertexts { Ciphertexts { rr: (0..NUM_CHUNKS) .map(|_| G1Projective::random(&mut rng)) diff --git a/common/dkg/src/bte/keys.rs b/common/dkg/src/bte/keys.rs index 132f161f5e..20d896a547 100644 --- a/common/dkg/src/bte/keys.rs +++ b/common/dkg/src/bte/keys.rs @@ -9,11 +9,15 @@ use bls12_381::{G1Projective, G2Projective, Scalar}; use ff::Field; use group::GroupEncoding; use nym_pemstore::traits::{PemStorableKey, PemStorableKeyPair}; +use rand::CryptoRng; use rand_core::RngCore; use zeroize::Zeroize; // produces public key and a decryption key for the root of the tree -pub fn keygen(params: &Params, mut rng: impl RngCore) -> (DecryptionKey, PublicKeyWithProof) { +pub fn keygen( + params: &Params, + mut rng: impl RngCore + CryptoRng, +) -> (DecryptionKey, PublicKeyWithProof) { let g1 = G1Projective::generator(); let g2 = G2Projective::generator(); @@ -244,7 +248,7 @@ pub struct KeyPair { } impl KeyPair { - pub fn new(params: &Params, rng: impl RngCore) -> Self { + pub fn new(params: &Params, rng: impl RngCore + CryptoRng) -> Self { let (dk, pk) = keygen(params, rng); Self { private_key: dk, diff --git a/common/dkg/src/bte/proof_chunking.rs b/common/dkg/src/bte/proof_chunking.rs index d756f08733..1021ac9fb4 100644 --- a/common/dkg/src/bte/proof_chunking.rs +++ b/common/dkg/src/bte/proof_chunking.rs @@ -10,7 +10,7 @@ use crate::utils::{deserialize_scalar, RandomOracleBuilder}; use bls12_381::{G1Projective, Scalar}; use ff::Field; use group::{Group, GroupEncoding}; -use rand::Rng; +use rand::{CryptoRng, Rng}; use rand_core::{RngCore, SeedableRng}; const CHUNKING_ORACLE_DOMAIN: &[u8] = @@ -95,7 +95,7 @@ impl ProofOfChunking { // Scalar(-1) would in reality be Scalar(q - 1), which is greater than Scalar(1) and opposite to // what we wanted. pub fn construct( - mut rng: impl RngCore, + mut rng: impl RngCore + CryptoRng, instance: Instance, witness_r: &[Scalar; NUM_CHUNKS], witnesses_s: &[Share], @@ -721,7 +721,9 @@ mod tests { ciphertext_chunks: Vec<[G1Projective; NUM_CHUNKS]>, } - fn setup(mut rng: impl RngCore) -> (OwnedInstance, [Scalar; NUM_CHUNKS], Vec) { + fn setup( + mut rng: impl RngCore + CryptoRng, + ) -> (OwnedInstance, [Scalar; NUM_CHUNKS], Vec) { let g1 = G1Projective::generator(); let mut pks = Vec::with_capacity(NODES); diff --git a/common/dkg/src/bte/proof_discrete_log.rs b/common/dkg/src/bte/proof_discrete_log.rs index 95eef6eb42..3956e3837d 100644 --- a/common/dkg/src/bte/proof_discrete_log.rs +++ b/common/dkg/src/bte/proof_discrete_log.rs @@ -5,6 +5,7 @@ use crate::utils::hash_to_scalar; use bls12_381::{G1Projective, Scalar}; use ff::Field; use group::GroupEncoding; +use rand::CryptoRng; use rand_core::RngCore; use zeroize::Zeroize; @@ -20,7 +21,11 @@ pub struct ProofOfDiscreteLog { } impl ProofOfDiscreteLog { - pub fn construct(mut rng: impl RngCore, public: &G1Projective, witness: &Scalar) -> Self { + pub fn construct( + mut rng: impl RngCore + CryptoRng, + public: &G1Projective, + witness: &Scalar, + ) -> Self { let mut rand_x = Scalar::random(&mut rng); let rand_commitment = G1Projective::generator() * rand_x; let challenge = Self::compute_challenge(public, &rand_commitment); diff --git a/common/dkg/src/bte/proof_sharing.rs b/common/dkg/src/bte/proof_sharing.rs index 4b843bb8f4..fe088c772f 100644 --- a/common/dkg/src/bte/proof_sharing.rs +++ b/common/dkg/src/bte/proof_sharing.rs @@ -9,6 +9,7 @@ use crate::{NodeIndex, Share}; use bls12_381::{G1Projective, G2Projective, Scalar}; use ff::Field; use group::GroupEncoding; +use rand::CryptoRng; use rand_core::RngCore; use std::collections::BTreeMap; @@ -87,7 +88,7 @@ pub struct ProofOfSecretSharing { impl ProofOfSecretSharing { pub fn construct( - mut rng: impl RngCore, + mut rng: impl RngCore + CryptoRng, instance: Instance, witness_r: &Scalar, witnesses_s: &[Share], @@ -309,13 +310,14 @@ mod tests { use super::*; use crate::interpolation::polynomial::Polynomial; use group::Group; + use rand::CryptoRng; use rand_core::SeedableRng; const NODES: u64 = 50; const THRESHOLD: u64 = 40; fn setup( - mut rng: impl RngCore, + mut rng: impl RngCore + CryptoRng, ) -> ( BTreeMap, PublicCoefficients, diff --git a/common/dkg/src/dealing.rs b/common/dkg/src/dealing.rs index a051f9f88c..fac5f2a9e6 100644 --- a/common/dkg/src/dealing.rs +++ b/common/dkg/src/dealing.rs @@ -13,6 +13,7 @@ use crate::utils::deserialize_g2; use crate::{NodeIndex, Share, Threshold}; use bls12_381::{G2Projective, Scalar}; use group::GroupEncoding; +use rand::CryptoRng; use rand_core::RngCore; use std::collections::BTreeMap; use zeroize::Zeroize; @@ -94,7 +95,7 @@ impl Dealing { // I'm not a big fan of this function signature, but I'm not clear on how to improve it while // allowing the dealer to skip decryption of its own share if it was also one of the receivers pub fn create( - mut rng: impl RngCore, + mut rng: impl RngCore + CryptoRng + CryptoRng, params: &Params, dealer_index: NodeIndex, threshold: Threshold, diff --git a/common/dkg/src/interpolation/polynomial.rs b/common/dkg/src/interpolation/polynomial.rs index 671b8e6832..f3874806e1 100644 --- a/common/dkg/src/interpolation/polynomial.rs +++ b/common/dkg/src/interpolation/polynomial.rs @@ -6,6 +6,7 @@ use crate::utils::deserialize_g2; use bls12_381::{G2Projective, Scalar}; use ff::Field; use group::GroupEncoding; +use rand::CryptoRng; use rand_core::RngCore; use std::ops::{Add, Index, IndexMut}; use zeroize::Zeroize; @@ -120,7 +121,7 @@ impl Polynomial { // for polynomial of degree n, we generate n+1 values // (for example for degree 1, like y = x + 2, we need [2,1]) /// Creates new pseudorandom polynomial of specified degree. - pub fn new_random(mut rng: impl RngCore, degree: u64) -> Self { + pub fn new_random(mut rng: impl RngCore + CryptoRng + CryptoRng, degree: u64) -> Self { Polynomial { coefficients: (0..=degree).map(|_| Scalar::random(&mut rng)).collect(), }