forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.rs
133 lines (108 loc) · 4.19 KB
/
test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::execute_script_without_stack_limit;
use crate::groth16::verifier::Verifier;
use ark_bn254::Bn254;
use ark_crypto_primitives::snark::{CircuitSpecificSetupSNARK, SNARK};
use ark_ec::pairing::Pairing;
use ark_ff::{BigInt, PrimeField};
use ark_groth16::Groth16;
use ark_relations::lc;
use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystemRef, SynthesisError};
use ark_std::{end_timer, start_timer, test_rng, UniformRand};
use bitcoin_script::script;
use rand::{RngCore, SeedableRng};
#[derive(Copy, Clone)]
struct DummyCircuit<F: PrimeField> {
pub a: Option<F>,
pub b: Option<F>,
pub num_variables: usize,
pub num_constraints: usize,
}
impl<F: PrimeField> ConstraintSynthesizer<F> for DummyCircuit<F> {
fn generate_constraints(self, cs: ConstraintSystemRef<F>) -> Result<(), SynthesisError> {
let a = cs.new_witness_variable(|| self.a.ok_or(SynthesisError::AssignmentMissing))?;
let b = cs.new_witness_variable(|| self.b.ok_or(SynthesisError::AssignmentMissing))?;
let c = cs.new_input_variable(|| {
let a = self.a.ok_or(SynthesisError::AssignmentMissing)?;
let b = self.b.ok_or(SynthesisError::AssignmentMissing)?;
Ok(a * b)
})?;
for _ in 0..(self.num_variables - 3) {
let _ = cs.new_witness_variable(|| self.a.ok_or(SynthesisError::AssignmentMissing))?;
}
for _ in 0..self.num_constraints - 1 {
cs.enforce_constraint(lc!() + a, lc!() + b, lc!() + c)?;
}
cs.enforce_constraint(lc!(), lc!(), lc!())?;
Ok(())
}
}
#[test]
fn test_hinted_groth16_verifier() {
type E = Bn254;
let k = 6;
let mut rng = ark_std::rand::rngs::StdRng::seed_from_u64(test_rng().next_u64());
let circuit = DummyCircuit::<<E as Pairing>::ScalarField> {
a: Some(<E as Pairing>::ScalarField::rand(&mut rng)),
b: Some(<E as Pairing>::ScalarField::rand(&mut rng)),
num_variables: 10,
num_constraints: 1 << k,
};
let (pk, vk) = Groth16::<E>::setup(circuit, &mut rng).unwrap();
let c = circuit.a.unwrap() * circuit.b.unwrap();
let proof = Groth16::<E>::prove(&pk, circuit, &mut rng).unwrap();
let (hinted_groth16_verifier, hints) = Verifier::hinted_verify(&[c], &proof, &vk);
println!(
"hinted_groth16_verifier: {:?} bytes",
hinted_groth16_verifier.len()
);
let start = start_timer!(|| "collect_script");
let script = script! {
for hint in hints {
{ hint.push() }
}
{ hinted_groth16_verifier }
};
end_timer!(start);
println!("groth16::test_hinted_verify_proof = {} bytes", script.len());
let start = start_timer!(|| "execute_script");
let exec_result = execute_script_without_stack_limit(script);
end_timer!(start);
assert!(exec_result.success);
}
#[test]
fn test_hinted_groth16_verifier_small_public() {
type E = Bn254;
let k = 6;
let mut rng = ark_std::rand::rngs::StdRng::seed_from_u64(test_rng().next_u64());
let circuit = DummyCircuit::<<E as Pairing>::ScalarField> {
a: Some(
<E as Pairing>::ScalarField::from_bigint(BigInt::from(u32::rand(&mut rng))).unwrap(),
),
b: Some(
<E as Pairing>::ScalarField::from_bigint(BigInt::from(u32::rand(&mut rng))).unwrap(),
),
num_variables: 10,
num_constraints: 1 << k,
};
let (pk, vk) = Groth16::<E>::setup(circuit, &mut rng).unwrap();
let c = circuit.a.unwrap() * circuit.b.unwrap();
let proof = Groth16::<E>::prove(&pk, circuit, &mut rng).unwrap();
let (hinted_groth16_verifier, hints) = Verifier::hinted_verify(&[c], &proof, &vk);
println!(
"hinted_groth16_verifier: {:?} bytes",
hinted_groth16_verifier.len()
);
let start = start_timer!(|| "collect_script");
let script = script! {
for hint in hints {
{ hint.push() }
}
{ hinted_groth16_verifier }
};
end_timer!(start);
println!("groth16::test_hinted_verify_proof = {} bytes", script.len());
let start = start_timer!(|| "execute_script");
let exec_result = execute_script_without_stack_limit(script);
end_timer!(start);
assert!(exec_result.success);
}