1
+ use std:: marker:: PhantomData ;
2
+
1
3
use curta:: math:: field:: Field ;
2
4
5
+ use super :: generators:: * ;
3
6
use crate :: frontend:: vars:: Nibbles ;
4
7
use crate :: prelude:: {
5
- ArrayVariable , ByteVariable , Bytes32Variable , CircuitBuilder , CircuitVariable , PlonkParameters ,
6
- U32Variable , Variable ,
8
+ ArrayVariable , ByteVariable , Bytes32Variable , CircuitBuilder , PlonkParameters , Variable ,
7
9
} ;
8
10
9
11
pub fn transform_proof_to_padded < const ENCODING_LEN : usize , const PROOF_LEN : usize > (
@@ -40,6 +42,27 @@ pub fn transform_proof_to_padded<const ENCODING_LEN: usize, const PROOF_LEN: usi
40
42
}
41
43
42
44
impl < L : PlonkParameters < D > , const D : usize > CircuitBuilder < L , D > {
45
+ pub fn byte_to_variable ( & mut self , lhs : ByteVariable ) -> Variable {
46
+ let generator: ByteToVariableGenerator < L , D > = ByteToVariableGenerator {
47
+ lhs,
48
+ output : self . init :: < Variable > ( ) ,
49
+ _phantom : PhantomData ,
50
+ } ;
51
+ self . add_simple_generator ( generator. clone ( ) ) ;
52
+ generator. output
53
+ }
54
+
55
+ pub fn sub_byte ( & mut self , lhs : ByteVariable , rhs : ByteVariable ) -> ByteVariable {
56
+ let generator: ByteSubGenerator < L , D > = ByteSubGenerator {
57
+ lhs,
58
+ rhs,
59
+ output : self . init :: < ByteVariable > ( ) ,
60
+ _phantom : PhantomData ,
61
+ } ;
62
+ self . add_simple_generator ( generator. clone ( ) ) ;
63
+ generator. output
64
+ }
65
+
43
66
const PREFIX_EXTENSION_EVEN : u8 = 0 ;
44
67
const PREFIX_EXTENSION_ODD : u8 = 1 ;
45
68
const PREFIX_LEAF_EVEN : u8 = 2 ;
@@ -50,7 +73,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
50
73
& mut self ,
51
74
key : Bytes32Variable ,
52
75
proof : ArrayVariable < ArrayVariable < ByteVariable , ENCODING_LEN > , PROOF_LEN > ,
53
- len_nodes : ArrayVariable < U32Variable , PROOF_LEN > ,
76
+ len_nodes : ArrayVariable < Variable , PROOF_LEN > ,
54
77
root : Bytes32Variable ,
55
78
value : Bytes32Variable ,
56
79
) {
@@ -68,8 +91,8 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
68
91
let one: Variable = self . one :: < Variable > ( ) ;
69
92
let two = self . constant :: < Variable > ( L :: Field :: from_canonical_u8 ( 2 ) ) ;
70
93
let const_64 = self . constant :: < Variable > ( L :: Field :: from_canonical_u8 ( 64 ) ) ;
71
- let const_32 = self . constant :: < U32Variable > ( 32u32 ) ;
72
- let const_128 = self . constant :: < U32Variable > ( 128u32 ) ;
94
+ let const_32 = self . constant :: < Variable > ( L :: Field :: from_canonical_u8 ( 32 ) ) ;
95
+ let const_128 = self . constant :: < ByteVariable > ( 128 ) ;
73
96
74
97
let mut current_key_idx = self . zero :: < Variable > ( ) ;
75
98
let mut finished = self . _false ( ) ;
@@ -79,7 +102,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
79
102
padded_root. push ( self . constant :: < ByteVariable > ( 0 ) ) ;
80
103
}
81
104
let mut current_node_id = ArrayVariable :: < ByteVariable , ELEMENT_LEN > :: new ( padded_root) ;
82
- let hash_key = self . keccak256_witness ( & key. as_bytes ( ) ) ;
105
+ let hash_key = self . keccak256 ( & key. as_bytes ( ) ) ;
83
106
let key_path: ArrayVariable < ByteVariable , 64 > = hash_key
84
107
. as_bytes ( )
85
108
. to_vec ( )
@@ -89,8 +112,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
89
112
90
113
for i in 0 ..PROOF_LEN {
91
114
let current_node = proof[ i] . clone ( ) ;
92
- let current_node_hash =
93
- self . keccak256_variable_witness ( current_node. as_slice ( ) , len_nodes[ i] . variable ) ;
115
+ let current_node_hash = self . keccak256_variable ( current_node. as_slice ( ) , len_nodes[ i] ) ;
94
116
95
117
if i == 0 {
96
118
self . assert_is_equal ( current_node_hash, root) ;
@@ -103,7 +125,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
103
125
current_node_hash,
104
126
current_node_id. as_slice ( ) [ 0 ..32 ] . into ( ) ,
105
127
) ;
106
- let node_len_le_32 = self . lt ( len_nodes[ i] , const_32) ;
128
+ let node_len_le_32 = self . lte ( len_nodes[ i] , const_32) ;
107
129
let case_len_le_32 = self . and ( node_len_le_32, first_32_bytes_eq) ;
108
130
let inter = self . not ( node_len_le_32) ;
109
131
let case_len_gt_32 = self . and ( inter, hash_eq) ;
@@ -116,7 +138,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
116
138
let ( decoded_list, decoded_element_lens, len_decoded_list) = self
117
139
. decode_element_as_list :: < ENCODING_LEN , LIST_LEN , ELEMENT_LEN > (
118
140
current_node,
119
- len_nodes[ i] . variable ,
141
+ len_nodes[ i] ,
120
142
finished,
121
143
) ;
122
144
@@ -134,7 +156,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
134
156
let offset_odd = self . mul ( prefix_extension_odd. variable , one) ;
135
157
let offset = self . add ( offset_even, offset_odd) ;
136
158
let branch_key = self . select_array ( key_path. clone ( ) . as_slice ( ) , current_key_idx) ;
137
- let branch_key_variable = branch_key . to_variable ( self ) ;
159
+ let branch_key_variable: Variable = self . byte_to_variable ( branch_key ) ; // can be unsafe since nibbles are checked
138
160
139
161
// Case 1
140
162
let is_branch_and_key_terminated = self . and ( is_branch, key_terminated) ;
@@ -187,36 +209,37 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
187
209
finished = self . or ( finished, m) ;
188
210
}
189
211
190
- // Can be unsafe because `current_node_id` comes from a ByteVariable.
191
- let current_node_id_u32 =
192
- U32Variable :: from_variables_unsafe ( & [ current_node_id[ 0 ] . to_variable ( self ) ] ) ;
193
- let current_node_len = self . sub ( current_node_id_u32, const_128) ;
194
- let lhs_offset = self . sub ( const_32, current_node_len) ;
212
+ let current_node_len = self . sub_byte ( current_node_id[ 0 ] , const_128) ;
213
+ let current_node_len_as_var = self . byte_to_variable ( current_node_len) ;
214
+ let lhs_offset = self . sub ( const_32, current_node_len_as_var) ;
195
215
196
216
self . assert_subarray_equal (
197
217
& value. as_bytes ( ) ,
198
- lhs_offset. variable ,
218
+ lhs_offset,
199
219
current_node_id. as_slice ( ) ,
200
220
one,
201
- current_node_len . variable ,
221
+ current_node_len_as_var ,
202
222
) ;
203
223
}
204
224
}
205
225
206
226
#[ cfg( test) ]
207
227
mod tests {
228
+ use curta:: math:: field:: Field ;
208
229
use log:: debug;
209
230
210
231
use super :: super :: utils:: { read_fixture, EIP1186ProofResponse } ;
211
232
use super :: * ;
212
233
use crate :: frontend:: eth:: utils:: u256_to_h256_be;
213
- use crate :: prelude:: DefaultBuilder ;
234
+ use crate :: prelude:: { DefaultBuilder , GoldilocksField } ;
214
235
use crate :: utils;
215
236
216
237
#[ test]
217
238
#[ cfg_attr( feature = "ci" , ignore) ]
218
239
fn test_mpt_circuit ( ) {
219
240
utils:: setup_logger ( ) ;
241
+ type F = GoldilocksField ;
242
+
220
243
let storage_result: EIP1186ProofResponse =
221
244
read_fixture ( "./src/frontend/eth/mpt/fixtures/example.json" ) ;
222
245
@@ -239,16 +262,16 @@ mod tests {
239
262
240
263
let ( proof_as_fixed, lengths_as_fixed) =
241
264
transform_proof_to_padded :: < ENCODING_LEN , PROOF_LEN > ( storage_proof) ;
242
- let len_nodes_value = lengths_as_fixed
265
+ let len_nodes_field_elements = lengths_as_fixed
243
266
. iter ( )
244
- . map ( |x| * x as u32 )
245
- . collect :: < Vec < _ > > ( ) ;
267
+ . map ( |x| F :: from_canonical_usize ( * x ) )
268
+ . collect :: < Vec < F > > ( ) ;
246
269
247
270
let mut builder = DefaultBuilder :: new ( ) ;
248
271
let key_variable = builder. read :: < Bytes32Variable > ( ) ;
249
272
let proof_variable =
250
273
builder. read :: < ArrayVariable < ArrayVariable < ByteVariable , ENCODING_LEN > , PROOF_LEN > > ( ) ;
251
- let len_nodes = builder. read :: < ArrayVariable < U32Variable , PROOF_LEN > > ( ) ;
274
+ let len_nodes = builder. read :: < ArrayVariable < Variable , PROOF_LEN > > ( ) ;
252
275
let root_variable = builder. read :: < Bytes32Variable > ( ) ;
253
276
let value_variable = builder. read :: < Bytes32Variable > ( ) ;
254
277
builder. verify_mpt_proof :: < ENCODING_LEN , PROOF_LEN > (
@@ -265,7 +288,7 @@ mod tests {
265
288
input. write :: < ArrayVariable < ArrayVariable < ByteVariable , ENCODING_LEN > , PROOF_LEN > > (
266
289
proof_as_fixed,
267
290
) ;
268
- input. write :: < ArrayVariable < U32Variable , PROOF_LEN > > ( len_nodes_value ) ;
291
+ input. write :: < ArrayVariable < Variable , PROOF_LEN > > ( len_nodes_field_elements ) ;
269
292
input. write :: < Bytes32Variable > ( root) ;
270
293
input. write :: < Bytes32Variable > ( value_as_h256) ;
271
294
0 commit comments