-
Notifications
You must be signed in to change notification settings - Fork 26
/
cube.rs
159 lines (136 loc) · 4.16 KB
/
cube.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#![allow(unused_imports)]
#![allow(unused_variables)]
extern crate bellman;
extern crate pairing;
extern crate rand;
// For randomness (during paramgen and proof generation)
use self::rand::{thread_rng, Rng};
// Bring in some tools for using pairing-friendly curves
use self::pairing::{
Engine,
Field,
PrimeField
};
// We're going to use the BLS12-381 pairing-friendly elliptic curve.
use self::pairing::bls12_381::{
Bls12,
Fr
};
// We'll use these interfaces to construct our circuit.
use self::bellman::{
Circuit,
ConstraintSystem,
SynthesisError
};
// We're going to use the Groth16 proving system.
use self::bellman::groth16::{
Proof,
generate_random_parameters,
prepare_verifying_key,
create_random_proof,
verify_proof,
};
// proving that I know x such that x^3 + x + 5 == 35
// Generalized: x^3 + x + 5 == out
pub struct CubeDemo<E: Engine> {
pub x: Option<E::Fr>,
}
impl <E: Engine> Circuit<E> for CubeDemo<E> {
fn synthesize<CS: ConstraintSystem<E>>(
self,
cs: &mut CS
) -> Result<(), SynthesisError>
{
// Flattened into quadratic equations (x^3 + x + 5 == 35):
// x * x = tmp_1
// tmp_1 * x = y
// y + x = tmp_2
// tmp_2 + 5 = out
// Resulting R1CS with w = [one, x, tmp_1, y, tmp_2, out]
// Allocate the first private "auxiliary" variable
let x_val = self.x;
let x = cs.alloc(|| "x", || {
x_val.ok_or(SynthesisError::AssignmentMissing)
})?;
// Allocate: x * x = tmp_1
let tmp_1_val = x_val.map(|mut e| {
e.square();
e
});
let tmp_1 = cs.alloc(|| "tmp_1", || {
tmp_1_val.ok_or(SynthesisError::AssignmentMissing)
})?;
// Enforce: x * x = tmp_1
cs.enforce(
|| "tmp_1",
|lc| lc + x,
|lc| lc + x,
|lc| lc + tmp_1
);
// Allocate: tmp_1 * x = y
let x_cubed_val = tmp_1_val.map(|mut e| {
e.mul_assign(&x_val.unwrap());
e
});
let x_cubed = cs.alloc(|| "x_cubed", || {
x_cubed_val.ok_or(SynthesisError::AssignmentMissing)
})?;
// Enforce: tmp_1 * x = y
cs.enforce(
|| "x_cubed",
|lc| lc + tmp_1,
|lc| lc + x,
|lc| lc + x_cubed
);
// Allocating the public "primary" output uses alloc_input
let out = cs.alloc_input(|| "out", || {
let mut tmp = x_cubed_val.unwrap();
tmp.add_assign(&x_val.unwrap());
tmp.add_assign(&E::Fr::from_str("5").unwrap());
Ok(tmp)
})?;
// tmp_2 + 5 = out
// => (tmp_2 + 5) * 1 = out
cs.enforce(
|| "out",
|lc| lc + x_cubed + x + (E::Fr::from_str("5").unwrap(), CS::one()),
|lc| lc + CS::one(),
|lc| lc + out
);
// lc is an inner product of all variables with some vector of coefficients
// bunch of variables added together with some coefficients
// usually if mult by 1 can do more efficiently
// x2 * x = out - x - 5
// mult quadratic constraints
//
Ok(())
}
}
#[test]
fn test_cube_proof(){
// This may not be cryptographically safe, use
// `OsRng` (for example) in production software.
let rng = &mut thread_rng();
println!("Creating parameters...");
// Create parameters for our circuit
let params = {
let c = CubeDemo::<Bls12> {
x: None
};
generate_random_parameters(c, rng).unwrap()
};
// Prepare the verification key (for proof verification)
let pvk = prepare_verifying_key(¶ms.vk);
println!("Creating proofs...");
// Create an instance of circuit
let c = CubeDemo::<Bls12> {
x: Fr::from_str("3")
};
// Create a groth16 proof with our parameters.
let proof = create_random_proof(c, ¶ms, rng).unwrap();
assert!(verify_proof(
&pvk,
&proof,
&[Fr::from_str("35").unwrap()]
).unwrap());
}