-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmerkle_nohash3.rs
More file actions
190 lines (166 loc) · 6.28 KB
/
Copy pathmerkle_nohash3.rs
File metadata and controls
190 lines (166 loc) · 6.28 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use halo2_poseidon::utilities::FieldValue;
use halo2_proofs::{
arithmetic::Field,
circuit::{AssignedCell, Layouter, SimpleFloorPlanner, Value},
halo2curves::pasta::Fp,
plonk::{
Advice, Circuit, Column, ConstraintSystem, ErrorFront, Expression, Instance, Selector,
},
poly::Rotation,
};
#[derive(Debug, Clone)]
pub struct MerkleConfig {
pub merkle: [Column<Advice>; 3],
pub swap_selector: Selector,
pub swap_bit_bool_selector: Selector,
pub hash_selector: Selector,
pub root_hash: Column<Instance>,
}
#[derive(Debug, Default, Clone)]
pub struct MerkleCircuitNoHash3 {
pub leaf: Value<Fp>,
pub path_elements: Vec<Value<Fp>>,
pub path_indices: Vec<Value<Fp>>,
}
impl Circuit<Fp> for MerkleCircuitNoHash3 {
type Config = MerkleConfig;
type FloorPlanner = SimpleFloorPlanner;
fn without_witnesses(&self) -> Self {
Self::default()
}
fn configure(meta: &mut ConstraintSystem<Fp>) -> MerkleConfig {
let advice = [
meta.advice_column(),
meta.advice_column(),
meta.advice_column(),
];
meta.enable_equality(advice[0]);
meta.enable_equality(advice[1]);
meta.enable_equality(advice[2]);
let root_hash = meta.instance_column();
meta.enable_equality(root_hash);
let swap_selector = meta.selector();
let swap_bit_bool_selector = meta.selector();
let hash_selector = meta.selector();
meta.create_gate("bool constraint", |meta| {
let s = meta.query_selector(swap_bit_bool_selector);
let swap_bit = meta.query_advice(advice[2], Rotation::cur());
vec![s * swap_bit.clone() * (Expression::Constant(Fp::from(1)) - swap_bit)]
});
meta.create_gate("swap constraint", |meta| {
let s = meta.query_selector(swap_selector);
let swap_bit = meta.query_advice(advice[2], Rotation::cur());
let left_cur = meta.query_advice(advice[0], Rotation::cur());
let right_cur = meta.query_advice(advice[1], Rotation::cur());
let left_next = meta.query_advice(advice[0], Rotation::next());
let right_next = meta.query_advice(advice[1], Rotation::next());
let constraint1 = s.clone()
* ((right_cur.clone() - left_cur.clone()) * swap_bit.clone() + left_cur.clone()
- left_next);
let constraint2 =
s * ((left_cur - right_cur.clone()) * swap_bit + right_cur - right_next);
vec![constraint1, constraint2]
});
meta.create_gate("hash constraint", |meta| {
let s = meta.query_selector(hash_selector);
let left = meta.query_advice(advice[0], Rotation::cur());
let right = meta.query_advice(advice[1], Rotation::cur());
let hash = meta.query_advice(advice[2], Rotation::cur());
vec![s * (left + right - hash)]
});
MerkleConfig {
merkle: advice,
swap_selector,
swap_bit_bool_selector,
hash_selector,
root_hash,
}
}
fn synthesize(
&self,
config: MerkleConfig,
mut layouter: impl Layouter<Fp>,
) -> Result<(), ErrorFront> {
let hashed_leaf = MerkleCircuitNoHash3::hash(self.leaf.as_ref(), None);
let leaf_hash = layouter.assign_region(
|| "assign leaf",
|mut region| {
region.assign_advice(|| "assign leaf", config.merkle[0], 0, || self.leaf)?;
region.assign_advice(|| "assign leaf hash", config.merkle[2], 0, || hashed_leaf)
},
)?;
let mut digest: AssignedCell<Fp, Fp> = self.merkle_prove_layer(
config.clone(),
layouter.namespace(|| "prove leaf"),
&leaf_hash,
self.path_elements[0],
self.path_indices[0],
)?;
for i in 1..self.path_elements.len() {
digest = self.merkle_prove_layer(
config.clone(),
layouter.namespace(|| "prove tree"),
&digest,
self.path_elements[i],
self.path_indices[i],
)?;
}
layouter.constrain_instance(digest.cell(), config.root_hash, 0)
}
}
impl MerkleCircuitNoHash3 {
pub fn hash(left: Value<&Fp>, right: Option<Value<&Fp>>) -> Value<Fp> {
if let Some(right) = right {
left + right
} else {
left + left
}
}
pub fn merkle_prove_layer(
&self,
config: MerkleConfig,
mut layouter: impl Layouter<Fp>,
node_cell: &AssignedCell<Fp, Fp>,
neighbor: Value<Fp>,
swap_bit: Value<Fp>,
) -> Result<AssignedCell<Fp, Fp>, ErrorFront> {
layouter.assign_region(
|| "merkle prove",
|mut region| {
config.swap_selector.enable(&mut region, 0)?;
config.swap_bit_bool_selector.enable(&mut region, 0)?;
config.hash_selector.enable(&mut region, 1)?;
node_cell.copy_advice(
|| "copy previous node cell",
&mut region,
config.merkle[0],
0,
)?;
region.assign_advice(|| "set neighbor node", config.merkle[1], 0, || neighbor)?;
region.assign_advice(|| "set swap bit", config.merkle[2], 0, || swap_bit)?;
let mut left = node_cell.value().cloned();
let mut right = neighbor;
swap_bit.map(|f| {
(left, right) = if f == Fp::ZERO {
(left, right)
} else {
(right, left)
}
});
region.assign_advice(|| "left node to be hashed", config.merkle[0], 1, || left)?;
region.assign_advice(
|| "right node to be hashed",
config.merkle[1],
1,
|| right,
)?;
Ok(region.assign_advice(
|| "result",
config.merkle[2],
1,
|| MerkleCircuitNoHash3::hash(left.value(), Some(right.value())),
)?)
},
)
}
}