-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use prio::{ | ||
field::{random_vector, Field64}, | ||
flp::{ | ||
types::{Count, Sum}, | ||
Type, | ||
}, | ||
}; | ||
use rand::Rng; | ||
use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; | ||
|
||
#[test] | ||
fn flp_bool_beta() { | ||
let count = Count::new(); | ||
|
||
// 1. The prover chooses a measurement and secret shares the input. | ||
let input: Vec<Field64> = count.encode_measurement(&1).unwrap(); | ||
let input_0 = input | ||
.iter() | ||
.map(|_| Field64::from(rand::thread_rng().gen::<u64>())) | ||
.collect::<Vec<_>>(); | ||
let input_1 = input | ||
.par_iter() | ||
.zip(input_0.par_iter()) | ||
.map(|(in_0, in_1)| in_0 - in_1) | ||
.collect::<Vec<_>>(); | ||
|
||
// 2. The prover generates prove_rand and query_rand (should be unique per proof). The prover | ||
// uses prover_rand to generate the proof. Finally, the prover secret shares the proof. | ||
let prove_rand = random_vector(count.prove_rand_len()).unwrap(); | ||
let query_rand = random_vector(count.query_rand_len()).unwrap(); | ||
|
||
let proof = count.prove(&input, &prove_rand, &[]).unwrap(); | ||
let proof_0 = proof | ||
.iter() | ||
.map(|_| Field64::from(rand::thread_rng().gen::<u64>())) | ||
.collect::<Vec<_>>(); | ||
let proof_1 = proof | ||
.par_iter() | ||
.zip(proof_0.par_iter()) | ||
.map(|(p_0, p_1)| p_0 - p_1) | ||
.collect::<Vec<_>>(); | ||
|
||
// 3. The verifiers are provided with query_rand (should be the same between the verifiers). | ||
// Each verifier queries the input and proof shares and receives a verifier_share. | ||
let verifier_0 = count | ||
.query(&input_0, &proof_0, &query_rand, &vec![], 2) | ||
.unwrap(); | ||
let verifier_1 = count | ||
.query(&input_1, &proof_1, &query_rand, &vec![], 2) | ||
.unwrap(); | ||
|
||
// 4. The verifiers combined their verifier_shares to check the proof. | ||
let verifier = verifier_0 | ||
.par_iter() | ||
.zip(verifier_1.par_iter()) | ||
.map(|(v1, v2)| v1 + v2) | ||
.collect::<Vec<_>>(); | ||
|
||
assert!(count.decide(&verifier).unwrap()); | ||
} | ||
|
||
#[test] | ||
fn flp_random_beta_in_range() { | ||
let bits = 2; // [0, 2^2) | ||
let sum = Sum::<Field64>::new(bits).unwrap(); | ||
|
||
// TODO(@jimouris): Derive joint randomness correctly. | ||
let joint_rand = vec![Field64::from(0); sum.joint_rand_len()]; | ||
|
||
// 1. The prover chooses a measurement and secret shares the input. | ||
let input: Vec<Field64> = sum.encode_measurement(&2).unwrap(); | ||
let input_0 = input | ||
.iter() | ||
.map(|_| Field64::from(rand::thread_rng().gen::<u64>())) | ||
.collect::<Vec<_>>(); | ||
let input_1 = input | ||
.par_iter() | ||
.zip(input_0.par_iter()) | ||
.map(|(in_0, in_1)| in_0 - in_1) | ||
.collect::<Vec<_>>(); | ||
|
||
// 2. The prover generates prove_rand and query_rand (should be unique per proof). The prover | ||
// uses prover_rand to generate the proof. Finally, the prover secret shares the proof. | ||
let prove_rand = random_vector(sum.prove_rand_len()).unwrap(); | ||
let query_rand = random_vector(sum.query_rand_len()).unwrap(); | ||
|
||
let proof = sum.prove(&input, &prove_rand, &joint_rand).unwrap(); | ||
let proof_0 = proof | ||
.iter() | ||
.map(|_| Field64::from(rand::thread_rng().gen::<u64>())) | ||
.collect::<Vec<_>>(); | ||
let proof_1 = proof | ||
.par_iter() | ||
.zip(proof_0.par_iter()) | ||
.map(|(p_0, p_1)| p_0 - p_1) | ||
.collect::<Vec<_>>(); | ||
|
||
// 3. The verifiers are provided with query_rand (should be the same between the verifiers). | ||
// Each verifier queries the input and proof shares and receives a verifier_share. | ||
let verifier_0 = sum | ||
.query(&input_0, &proof_0, &query_rand, &joint_rand, 2) | ||
.unwrap(); | ||
let verifier_1 = sum | ||
.query(&input_1, &proof_1, &query_rand, &joint_rand, 2) | ||
.unwrap(); | ||
|
||
// 4. The verifiers combined their verifier_shares to check the proof. | ||
let verifier = verifier_0 | ||
.par_iter() | ||
.zip(verifier_1.par_iter()) | ||
.map(|(v1, v2)| v1 + v2) | ||
.collect::<Vec<_>>(); | ||
|
||
assert!(sum.decide(&verifier).unwrap()); | ||
} |