This repository was archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rs
140 lines (108 loc) · 3.56 KB
/
main.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
134
135
136
137
138
139
140
extern crate bellman;
extern crate pairing;
extern crate rand;
extern crate ff;
extern crate franklin_crypto;
use bellman::{Circuit, ConstraintSystem, SynthesisError};
use ff::{Field, PrimeField};
use pairing::{Engine};
use pairing::bn256::{Bn256, Fr};
trait OptionExt<T> {
fn grab(&self) -> Result<T, SynthesisError>;
}
impl<T: Copy> OptionExt<T> for Option<T> {
fn grab(&self) -> Result<T, SynthesisError> {
self.ok_or(SynthesisError::AssignmentMissing)
}
}
struct XorCircuit<E: Engine> {
a: Option<E::Fr>,
b: Option<E::Fr>,
c: Option<E::Fr>,
}
// Implementation of our circuit:
// Given a bit `c`, prove that we know bits `a` and `b` such that `c = a xor b`
impl<E: Engine> Circuit<E> for XorCircuit<E> {
fn synthesize<CS: ConstraintSystem<E>>(self, cs: &mut CS) -> Result<(), SynthesisError> {
// public input: c
// variables (witness): a, b
// constraint system:
// a * a = a
// b * b = b
// 2a * b = a + b - c
let a = cs.alloc(|| "a", || self.a.grab())?;
// a * a = a
cs.enforce(|| "a is a boolean", |lc| lc + a, |lc| lc + a, |lc| lc + a);
let b = cs.alloc(|| "b", || self.b.grab())?;
// b * b = b
cs.enforce(|| "b is a boolean", |lc| lc + b, |lc| lc + b, |lc| lc + b);
// c = a xor b
let c = cs.alloc_input(|| "c", || self.c.grab())?;
// 2a * b = a + b - c
cs.enforce(
|| "xor constraint",
|lc| lc + (E::Fr::from_str("2").unwrap(), a),
|lc| lc + b,
|lc| lc + a + b - c,
);
Ok(())
}
}
// Create some parameters, create a proof, and verify the proof.
fn main() {
use rand::thread_rng;
use bellman::groth16::{
create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof
};
let rng = &mut thread_rng();
let params = {
let c = XorCircuit::<Bn256> {
a: None,
b: None,
c: None,
};
generate_random_parameters(c, rng).unwrap()
};
let pvk = prepare_verifying_key(¶ms.vk);
// here we allocate actual variables
let c = XorCircuit {
a: Some(Fr::one()),
b: Some(Fr::zero()),
c: Some(Fr::one()),
};
// Create a groth16 proof with our parameters.
let proof = create_random_proof(c, ¶ms, rng).unwrap();
// `inputs` slice contains public parameters encoded as field elements Fr
// incorrect input tests
let inputs = &[Fr::from_str("5").unwrap()];
let success = verify_proof(&pvk, &proof, inputs).unwrap();
assert!(!success); // fails, because 5 is not 1 or 0
let inputs = &[Fr::zero()];
let success = verify_proof(&pvk, &proof, inputs).unwrap();
assert!(!success); // fails because 0 != 0 xor 1
// correct input test
let inputs = &[Fr::one()];
let success = verify_proof(&pvk, &proof, inputs).unwrap();
assert!(success);
}
#[cfg(test)]
mod tests {
use super::*;
use franklin_crypto::circuit::test::TestConstraintSystem;
#[test]
fn test_circuit() {
let mut cs = TestConstraintSystem::<Bn256>::new();
let circuit = XorCircuit {
a: Some(Fr::one()),
b: Some(Fr::zero()),
c: Some(Fr::one()),
};
circuit.synthesize(&mut cs).expect("synthesis failed");
//dbg!(cs.find_unconstrained());
dbg!(cs.num_constraints());
dbg!(cs.num_inputs());
if let Some(token) = cs.which_is_unsatisfied() {
eprintln!("Error: {} is unsatisfied", token);
}
}
}