Skip to content

Commit

Permalink
fix rng clone err
Browse files Browse the repository at this point in the history
  • Loading branch information
justcode740 committed Sep 17, 2023
1 parent f0bdeb0 commit 33a1cd7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 9 additions & 4 deletions halo2-ecc/src/secp256k1/tests/schnorr_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,22 @@ pub fn schnorr_signature_test<F: BigPrimeField>(
*res.value()
}

// This function mut rng internal state
pub fn random_schnorr_signature_input(rng: &mut StdRng) -> SchnorrInput {
let sk = <Secp256k1Affine as CurveAffine>::ScalarExt::random(rng.clone());
let mut tmp = rng.clone();
let sk = <Secp256k1Affine as CurveAffine>::ScalarExt::random(&mut tmp);
let pk = Secp256k1Affine::from(Secp256k1Affine::generator() * sk);
let msg_hash = <Secp256k1Affine as CurveAffine>::ScalarExt::random(rng.clone());
let msg_hash = <Secp256k1Affine as CurveAffine>::ScalarExt::random(&mut tmp);

let mut k = <Secp256k1Affine as CurveAffine>::ScalarExt::random(rng.clone());
let mut k = <Secp256k1Affine as CurveAffine>::ScalarExt::random(&mut tmp);

let mut r_point =
Secp256k1Affine::from(Secp256k1Affine::generator() * k).coordinates().unwrap();
let mut x: &Fp = r_point.x();
let mut y: &Fp = r_point.y();
// make sure R.y is even
while fe_to_biguint(y).mod_floor(&BigUint::from(2u64)) != BigUint::from(0u64) {
k = <Secp256k1Affine as CurveAffine>::ScalarExt::random(StdRng::from_seed([0u8; 32]));
k = <Secp256k1Affine as CurveAffine>::ScalarExt::random(&mut tmp);
r_point = Secp256k1Affine::from(Secp256k1Affine::generator() * k).coordinates().unwrap();
x = r_point.x();
y = r_point.y();
Expand All @@ -76,6 +78,9 @@ pub fn random_schnorr_signature_input(rng: &mut StdRng) -> SchnorrInput {
let r = *x;
let s = k + sk * msg_hash;

// change rng internal state
*rng = tmp;

SchnorrInput { r, s, msg_hash, pk }
}

Expand Down
5 changes: 2 additions & 3 deletions halo2-ecc/src/secp256k1/tests/schnorr_signature_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ fn test_schnorr_signature_k_zero(sk: u64, msg_hash: u64, k: u64) {

#[test]
fn test_schnorr_signature_random_valid_inputs() {
for i in 0..10 {
let mut rng = StdRng::seed_from_u64(i);
let mut rng = StdRng::seed_from_u64(0);
for _ in 0..10 {
let input = random_schnorr_signature_input(&mut rng);
println!("{:?}", input);
run_test(input);
}
}
Expand Down

0 comments on commit 33a1cd7

Please sign in to comment.