Skip to content

Commit

Permalink
Remove getrandom(), clean up error handling
Browse files Browse the repository at this point in the history
Use `thread_rng().gen::<[u8; SEED_SIZE]>()` to generate seeds rather
than `getrandom()`. Since this method is infallible, we no longer need
to propagate errors from `getrandom()`.
  • Loading branch information
cjpatton committed Jan 16, 2025
1 parent e91490a commit d8e0651
Show file tree
Hide file tree
Showing 24 changed files with 159 additions and 219 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ byteorder = "1.5.0"
ctr = { version = "0.9.2", optional = true }
fiat-crypto = { version = "0.2.9", optional = true }
fixed = { version = "1.27", optional = true }
getrandom = { version = "0.2.14", features = ["std"] }
hex = { version = "0.4.3", features = ["serde"], optional = true }
hmac = { version = "0.12.1", optional = true }
num-bigint = { version = "0.4.6", optional = true, features = ["rand", "serde"] }
Expand Down Expand Up @@ -55,7 +54,6 @@ experimental = ["bitvec", "fiat-crypto", "fixed", "num-bigint", "num-rational",
multithreaded = ["rayon"]
crypto-dependencies = ["aes", "ctr", "hmac", "sha2"]
test-util = ["hex", "serde_json", "zipf"]
wasm-compat = ["getrandom/js"]

[workspace]
members = [".", "binaries"]
Expand Down
2 changes: 1 addition & 1 deletion benches/cycle_counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use prio::{
};

fn prng(size: usize) -> Vec<Field128> {
random_vector(size).unwrap()
random_vector(size)
}

fn prng_16() -> Vec<Field128> {
Expand Down
14 changes: 8 additions & 6 deletions benches/speed_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ fn poly_mul(c: &mut Criterion) {
let m = (size + 1).next_power_of_two();
let mut g: Mul<F> = Mul::new(*size);
let mut outp = vec![F::zero(); 2 * m];
let inp = vec![random_vector(m).unwrap(); 2];
let mut inp = vec![];
inp.push(random_vector(m));
inp.push(random_vector(m));

b.iter(|| {
benchmarked_gadget_mul_call_poly_fft(&mut g, &mut outp, &inp).unwrap();
Expand All @@ -105,7 +107,9 @@ fn poly_mul(c: &mut Criterion) {
let m = (size + 1).next_power_of_two();
let mut g: Mul<F> = Mul::new(*size);
let mut outp = vec![F::zero(); 2 * m];
let inp = vec![random_vector(m).unwrap(); 2];
let mut inp = vec![];
inp.push(random_vector(m));
inp.push(random_vector(m));

b.iter(|| {
benchmarked_gadget_mul_call_poly_direct(&mut g, &mut outp, &inp).unwrap();
Expand Down Expand Up @@ -709,11 +713,10 @@ fn idpf(c: &mut Criterion) {
let input = IdpfInput::from_bools(&bits);

let inner_values = random_vector::<Field64>(size - 1)
.unwrap()
.into_iter()
.map(|random_element| Poplar1IdpfValue::new([Field64::one(), random_element]))
.collect::<Vec<_>>();
let leaf_value = Poplar1IdpfValue::new([Field255::one(), random_vector(1).unwrap()[0]]);
let leaf_value = Poplar1IdpfValue::new([Field255::one(), random_vector(1)[0]]);

let idpf = Idpf::new((), ());
b.iter(|| {
Expand All @@ -732,11 +735,10 @@ fn idpf(c: &mut Criterion) {
let input = IdpfInput::from_bools(&bits);

let inner_values = random_vector::<Field64>(size - 1)
.unwrap()
.into_iter()
.map(|random_element| Poplar1IdpfValue::new([Field64::one(), random_element]))
.collect::<Vec<_>>();
let leaf_value = Poplar1IdpfValue::new([Field255::one(), random_vector(1).unwrap()[0]]);
let leaf_value = Poplar1IdpfValue::new([Field255::one(), random_vector(1)[0]]);

let idpf = Idpf::new((), ());
let (public_share, keys) = idpf
Expand Down
8 changes: 4 additions & 4 deletions src/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ mod tests {
for size in test_sizes.iter() {
let mut tmp = vec![F::zero(); *size];
let mut got = vec![F::zero(); *size];
let want = random_vector(*size).unwrap();
let want = random_vector(*size);

discrete_fourier_transform(&mut tmp, &want, want.len())?;
discrete_fourier_transform_inv(&mut got, &tmp, tmp.len())?;
Expand Down Expand Up @@ -166,7 +166,7 @@ mod tests {
let size = 128;
let mut mem = TestPolyAuxMemory::new(size / 2);

let inp = random_vector(size).unwrap();
let inp = random_vector(size);
let mut want = vec![FieldPrio2::zero(); size];
let mut got = vec![FieldPrio2::zero(); size];

Expand All @@ -191,8 +191,8 @@ mod tests {
fn test_fft_linearity() {
let len = 16;
let num_shares = 3;
let x: Vec<Field64> = random_vector(len).unwrap();
let mut x_shares = split_vector(&x, num_shares).unwrap();
let x: Vec<Field64> = random_vector(len);
let mut x_shares = split_vector(&x, num_shares);

// Just for fun, let's do something different with a subset of the inputs. For the first
// share, every odd element is set to the plaintext value. For all shares but the first,
Expand Down
21 changes: 9 additions & 12 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use crate::{
codec::{CodecError, Decode, Encode},
fp::{FieldOps, FieldParameters, FP128, FP32, FP64},
prng::{Prng, PrngError},
prng::Prng,
};
use rand::{
distributions::{Distribution, Standard},
Expand Down Expand Up @@ -876,31 +876,28 @@ pub(crate) fn merge_vector<F: FieldElement>(

/// Outputs an additive secret sharing of the input.
#[cfg(test)]
pub(crate) fn split_vector<F: FieldElement>(
inp: &[F],
num_shares: usize,
) -> Result<Vec<Vec<F>>, PrngError> {
pub(crate) fn split_vector<F: FieldElement>(inp: &[F], num_shares: usize) -> Vec<Vec<F>> {
if num_shares == 0 {
return Ok(vec![]);
return vec![];
}

let mut outp = Vec::with_capacity(num_shares);
outp.push(inp.to_vec());

for _ in 1..num_shares {
let share: Vec<F> = random_vector(inp.len())?;
let share: Vec<F> = random_vector(inp.len());
for (x, y) in outp[0].iter_mut().zip(&share) {
*x -= *y;
}
outp.push(share);
}

Ok(outp)
outp
}

/// Generate a vector of uniformly distributed random field elements.
pub fn random_vector<F: FieldElement>(len: usize) -> Result<Vec<F>, PrngError> {
Ok(Prng::new()?.take(len).collect())
pub fn random_vector<F: FieldElement>(len: usize) -> Vec<F> {
Prng::new().take(len).collect()
}

/// `encode_fieldvec` serializes a type that is equivalent to a vector of field elements.
Expand Down Expand Up @@ -979,7 +976,7 @@ pub(crate) mod test_utils {
}

pub(crate) fn field_element_test_common<F: TestFieldElementWithInteger>() {
let mut prng: Prng<F, _> = Prng::new().unwrap();
let mut prng: Prng<F, _> = Prng::new();
let int_modulus = F::modulus();
let int_one = F::TestInteger::try_from(1).unwrap();
let zero = F::zero();
Expand Down Expand Up @@ -1174,7 +1171,7 @@ mod tests {
fn field_element_test<F: FftFriendlyFieldElement + Hash>() {
field_element_test_common::<F>();

let mut prng: Prng<F, _> = Prng::new().unwrap();
let mut prng: Prng<F, _> = Prng::new();
let int_modulus = F::modulus();
let int_one = F::Integer::try_from(1).unwrap();
let zero = F::zero();
Expand Down
36 changes: 16 additions & 20 deletions src/flp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
//! // check the proof. The application needs to ensure that the prover
//! // "commits" to the input before this point. In Prio3, the joint
//! // randomness is derived from additive shares of the input.
//! let joint_rand = random_vector(count.joint_rand_len()).unwrap();
//! let joint_rand = random_vector(count.joint_rand_len());
//!
//! // The prover generates the proof.
//! let prove_rand = random_vector(count.prove_rand_len()).unwrap();
//! let prove_rand = random_vector(count.prove_rand_len());
//! let proof = count.prove(&input, &prove_rand, &joint_rand).unwrap();
//!
//! // The verifier checks the proof. In the first step, the verifier "queries"
//! // the input and proof, getting the "verifier message" in response. It then
//! // inspects the verifier to decide if the input is valid.
//! let query_rand = random_vector(count.query_rand_len()).unwrap();
//! let query_rand = random_vector(count.query_rand_len());
//! let verifier = count.query(&input, &proof, &query_rand, &joint_rand, 1).unwrap();
//! assert!(count.decide(&verifier).unwrap());
//! ```
Expand Down Expand Up @@ -178,7 +178,7 @@ pub trait Type: Sized + Eq + Clone + Debug {
///
/// let count = Count::new();
/// let input: Vec<Field64> = count.encode_measurement(&true).unwrap();
/// let joint_rand = random_vector(count.joint_rand_len()).unwrap();
/// let joint_rand = random_vector(count.joint_rand_len());
/// let v = count.valid(&mut count.gadget(), &input, &joint_rand, 1).unwrap();
/// assert!(v.into_iter().all(|f| f == Field64::zero()));
/// ```
Expand Down Expand Up @@ -874,9 +874,9 @@ pub mod test_utils {
);

let mut gadgets = self.flp.gadget();
let joint_rand = random_vector(self.flp.joint_rand_len()).unwrap();
let prove_rand = random_vector(self.flp.prove_rand_len()).unwrap();
let query_rand = random_vector(self.flp.query_rand_len()).unwrap();
let joint_rand = random_vector(self.flp.joint_rand_len());
let prove_rand = random_vector(self.flp.prove_rand_len());
let query_rand = random_vector(self.flp.query_rand_len());
assert_eq!(
self.flp.joint_rand_len(),
joint_rand.len(),
Expand Down Expand Up @@ -999,8 +999,7 @@ pub mod test_utils {
outp.push(inp.to_vec());

for _ in 1..SHARES {
let share: Vec<F> =
random_vector(inp.len()).expect("failed to generate a random vector");
let share: Vec<F> = random_vector(inp.len());
for (x, y) in outp[0].iter_mut().zip(&share) {
*x -= *y;
}
Expand Down Expand Up @@ -1031,21 +1030,18 @@ mod tests {
assert_eq!(input.len(), typ.input_len());

let input_shares: Vec<Vec<Field128>> = split_vector(input.as_slice(), NUM_SHARES)
.unwrap()
.into_iter()
.collect();

let joint_rand = random_vector(typ.joint_rand_len()).unwrap();
let prove_rand = random_vector(typ.prove_rand_len()).unwrap();
let query_rand = random_vector(typ.query_rand_len()).unwrap();
let joint_rand = random_vector(typ.joint_rand_len());
let prove_rand = random_vector(typ.prove_rand_len());
let query_rand = random_vector(typ.query_rand_len());

let proof = typ.prove(&input, &prove_rand, &joint_rand).unwrap();
assert_eq!(proof.len(), typ.proof_len());

let proof_shares: Vec<Vec<Field128>> = split_vector(&proof, NUM_SHARES)
.unwrap()
.into_iter()
.collect();
let proof_shares: Vec<Vec<Field128>> =
split_vector(&proof, NUM_SHARES).into_iter().collect();

let verifier: Vec<Field128> = (0..NUM_SHARES)
.map(|i| {
Expand Down Expand Up @@ -1191,9 +1187,9 @@ mod tests {
let typ: Issue254Type<Field128> = Issue254Type::new();
let input = typ.encode_measurement(&0).unwrap();
assert_eq!(input.len(), typ.input_len());
let joint_rand = random_vector(typ.joint_rand_len()).unwrap();
let prove_rand = random_vector(typ.prove_rand_len()).unwrap();
let query_rand = random_vector(typ.query_rand_len()).unwrap();
let joint_rand = random_vector(typ.joint_rand_len());
let prove_rand = random_vector(typ.prove_rand_len());
let query_rand = random_vector(typ.query_rand_len());
let proof = typ.prove(&input, &prove_rand, &joint_rand).unwrap();
let verifier = typ
.query(&input, &proof, &query_rand, &joint_rand, 1)
Expand Down
8 changes: 4 additions & 4 deletions src/flp/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ mod tests {

#[test]
fn test_poly_eval() {
let poly: Vec<TestField> = random_vector(10).unwrap();
let poly: Vec<TestField> = random_vector(10);

let num_calls = FFT_THRESHOLD / 2;
let mut g: PolyEval<TestField> = PolyEval::new(poly.clone(), num_calls);
Expand Down Expand Up @@ -531,7 +531,7 @@ mod tests {
let degree = g.degree();

// Test that both gadgets evaluate to the same value when run on scalar inputs.
let inp: Vec<TestField> = random_vector(arity).unwrap();
let inp: Vec<TestField> = random_vector(arity);
let result = g.call(&inp).unwrap();
let result_serial = g_serial.call(&inp).unwrap();
assert_eq!(result, result_serial);
Expand All @@ -541,7 +541,7 @@ mod tests {
vec![TestField::zero(); (degree * num_calls + 1).next_power_of_two()];
let mut poly_outp_serial =
vec![TestField::zero(); (degree * num_calls + 1).next_power_of_two()];
let mut prng: Prng<TestField, _> = Prng::new().unwrap();
let mut prng: Prng<TestField, _> = Prng::new();
let poly_inp: Vec<_> = iter::repeat_with(|| {
iter::repeat_with(|| prng.get())
.take(1 + num_calls)
Expand All @@ -562,7 +562,7 @@ mod tests {
/// to evaluating each of the inputs at the same point and applying g.call() on the results.
fn gadget_test<F: FftFriendlyFieldElement, G: Gadget<F>>(g: &mut G, num_calls: usize) {
let wire_poly_len = (1 + num_calls).next_power_of_two();
let mut prng = Prng::new().unwrap();
let mut prng = Prng::new();
let mut inp = vec![F::zero(); g.arity()];
let mut gadget_poly = vec![F::zero(); gadget_poly_fft_mem_len(g.degree(), num_calls)];
let mut wire_polys = vec![vec![F::zero(); wire_poly_len]; g.arity()];
Expand Down
12 changes: 6 additions & 6 deletions src/flp/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,9 +1420,9 @@ mod tests {
let typ: SumVec<TestField, ParallelSum<TestField, _>> = SumVec::new(1, 1000, 31).unwrap();
let input = typ.encode_measurement(&vec![0; 1000]).unwrap();
assert_eq!(input.len(), typ.input_len());
let joint_rand = random_vector(typ.joint_rand_len()).unwrap();
let prove_rand = random_vector(typ.prove_rand_len()).unwrap();
let query_rand = random_vector(typ.query_rand_len()).unwrap();
let joint_rand = random_vector(typ.joint_rand_len());
let prove_rand = random_vector(typ.prove_rand_len());
let query_rand = random_vector(typ.query_rand_len());
let proof = typ.prove(&input, &prove_rand, &joint_rand).unwrap();
let verifier = typ
.query(&input, &proof, &query_rand, &joint_rand, 1)
Expand All @@ -1438,9 +1438,9 @@ mod tests {
SumVec::new(1, 1000, 31).unwrap();
let input = typ.encode_measurement(&vec![0; 1000]).unwrap();
assert_eq!(input.len(), typ.input_len());
let joint_rand = random_vector(typ.joint_rand_len()).unwrap();
let prove_rand = random_vector(typ.prove_rand_len()).unwrap();
let query_rand = random_vector(typ.query_rand_len()).unwrap();
let joint_rand = random_vector(typ.joint_rand_len());
let prove_rand = random_vector(typ.prove_rand_len());
let query_rand = random_vector(typ.query_rand_len());
let proof = typ.prove(&input, &prove_rand, &joint_rand).unwrap();
let verifier = typ
.query(&input, &proof, &query_rand, &joint_rand, 1)
Expand Down
3 changes: 0 additions & 3 deletions src/flp/types/dp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ mod tests {
let mut rng = XofTurboShake128::init(&[0; 32], &[]).into_seed_stream();
let [mut share1, mut share2]: [Vec<Field128>; 2] =
split_vector(&[Field128::zero(); SIZE], 2)
.unwrap()
.try_into()
.unwrap();

Expand Down Expand Up @@ -258,7 +257,6 @@ mod tests {
let mut rng = XofTurboShake128::init(&[1; 32], &[]).into_seed_stream();
let [mut share1, mut share2]: [Vec<Field128>; 2] =
split_vector(&[Field128::zero(); SIZE], 2)
.unwrap()
.try_into()
.unwrap();

Expand Down Expand Up @@ -301,7 +299,6 @@ mod tests {
let mut rng = XofTurboShake128::init(&[2; 32], &[]).into_seed_stream();
let [mut share1, mut share2]: [Vec<Field128>; 2] =
split_vector(&[Field128::zero(); SIZE], 2)
.unwrap()
.try_into()
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/flp/types/fixedpoint_l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ mod tests {

// invalid submission length, should be 3n + (2*n - 2) for a
// 3-element n-bit vector. 3*n bits for 3 entries, (2*n-2) for norm.
let joint_rand = random_vector(vsum.joint_rand_len()).unwrap();
let joint_rand = random_vector(vsum.joint_rand_len());
vsum.valid(
&mut vsum.gadget(),
&vec![one; 3 * n + 2 * n - 1],
Expand Down
Loading

0 comments on commit d8e0651

Please sign in to comment.