-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmerkle_nohash1.rs
More file actions
165 lines (144 loc) · 5.58 KB
/
Copy pathmerkle_nohash1.rs
File metadata and controls
165 lines (144 loc) · 5.58 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
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 MerkleCircuitNoHash1 {
pub leaf: Value<Fp>,
pub path_elements: Vec<Value<Fp>>,
pub path_indices: Vec<Value<Fp>>,
}
impl Circuit<Fp> for MerkleCircuitNoHash1 {
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("root 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 leaf_cell = layouter.assign_region(
|| "assign leaf",
|mut region| region.assign_advice(|| "assign leaf", config.merkle[0], 0, || self.leaf),
)?;
let mut digest: AssignedCell<Fp, Fp> = leaf_cell;
for i in 0..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 MerkleCircuitNoHash1 {
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)?;
// we enable the hash selector on the second row
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, || left + right)?)
},
)
}
}