Skip to content

Commit f0aa0d0

Browse files
committed
Use rand::OsRng in non-test builds
During tests, use a seedable rng, to get reproducible witness/proof
1 parent 0370a5c commit f0aa0d0

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

ledger/src/proofs/transaction.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,27 +1225,15 @@ impl<F: FieldWitness> InnerCurve<F> {
12251225
Self { inner }
12261226
}
12271227

1228-
fn fake_random() -> Self {
1229-
// static SEED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
1230-
// dbg!(SEED.load(std::sync::atomic::Ordering::Relaxed));
1228+
pub fn random() -> Self {
1229+
// NOTE: Not random in `cfg(test)`
1230+
let mut rng = get_rng();
12311231

1232-
let mut rng: rand::rngs::StdRng = rand::SeedableRng::seed_from_u64(
1233-
0,
1234-
// SEED.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
1235-
);
1232+
// Both `proj` below are the same type, but we use `into()` to make it generic
12361233
let proj: GroupProjective<F::Parameters> = ark_ff::UniformRand::rand(&mut rng);
12371234
let proj: F::Projective = proj.into();
1238-
Self { inner: proj }
1239-
}
1240-
1241-
pub fn random() -> Self {
1242-
Self::fake_random()
1243-
// // Both `proj` below are the same type, but we use `into()` to make it generic
1244-
// let rng = &mut rand::rngs::OsRng;
1245-
// let proj: GroupProjective<F::Parameters> = ark_ff::UniformRand::rand(rng);
1246-
// let proj: F::Projective = proj.into();
12471235

1248-
// Self { inner: proj }
1236+
Self { inner: proj }
12491237
}
12501238
}
12511239

@@ -3877,6 +3865,17 @@ fn make_prover_index<C: ProofConstants, F: FieldWitness>(
38773865
index
38783866
}
38793867

3868+
/// During tests, we don't want randomness, to get reproducible witness/proofs
3869+
/// TODO: Are there other cases where we don't want randomness ?
3870+
#[cfg(test)]
3871+
fn get_rng() -> rand::rngs::StdRng {
3872+
<rand::rngs::StdRng as rand::SeedableRng>::seed_from_u64(0)
3873+
}
3874+
#[cfg(not(test))]
3875+
fn get_rng() -> rand::rngs::OsRng {
3876+
rand::rngs::OsRng
3877+
}
3878+
38803879
pub fn create_proof<C: ProofConstants, F: FieldWitness>(
38813880
prover: &Prover<F>,
38823881
prev_challenges: Vec<RecursionChallenge<F::OtherCurve>>,
@@ -3887,7 +3886,9 @@ pub fn create_proof<C: ProofConstants, F: FieldWitness>(
38873886
let computed_witness: [Vec<F>; COLUMNS] = compute_witness::<C, _>(prover, w);
38883887
let prover_index: &ProverIndex<F::OtherCurve> = &prover.index;
38893888

3890-
let mut rng: rand::rngs::StdRng = rand::SeedableRng::seed_from_u64(0);
3889+
// NOTE: Not random in `cfg(test)`
3890+
let mut rng = get_rng();
3891+
38913892
let now = std::time::Instant::now();
38923893
let group_map = kimchi::groupmap::GroupMap::<F::Scalar>::setup();
38933894
let proof = kimchi::proof::ProverProof::create_recursive::<F::FqSponge, EFrSponge<F>>(

0 commit comments

Comments
 (0)