@@ -31,14 +31,12 @@ const expectEqualSlices = std.testing.expectEqualSlices;
31
31
pub const PoseidonBuiltinRunner = struct {
32
32
const Self = @This ();
33
33
34
+ pub const INPUT_CELLS_PER_POSEIDON = poseidon_instance_def .INPUT_CELLS_PER_POSEIDON ;
35
+
34
36
/// Base
35
37
base : usize = 0 ,
36
38
/// Ratio
37
39
ratio : ? u32 ,
38
- /// Number of cells per instance
39
- cells_per_instance : u32 = poseidon_instance_def .CELLS_PER_POSEIDON ,
40
- /// Number of input cells
41
- n_input_cells : u32 = poseidon_instance_def .INPUT_CELLS_PER_POSEIDON ,
42
40
/// Stop pointer
43
41
stop_ptr : ? usize = null ,
44
42
/// Included boolean flag
@@ -157,7 +155,7 @@ pub const PoseidonBuiltinRunner = struct {
157
155
158
156
// Calculate the expected stop pointer value based on the number of used instances.
159
157
const stop_ptr = stop_pointer .offset ;
160
- if (stop_ptr != try self .getUsedInstances (segments ) * self . cells_per_instance )
158
+ if (stop_ptr != try self .getUsedInstances (segments ) * poseidon_instance_def . CELLS_PER_POSEIDON )
161
159
return RunnerError .InvalidStopPointer ;
162
160
163
161
// Set the stop pointer and return the address of the stop pointer.
@@ -201,7 +199,7 @@ pub const PoseidonBuiltinRunner = struct {
201
199
return std .math .divCeil (
202
200
usize ,
203
201
try self .getUsedCells (segments ),
204
- self . cells_per_instance ,
202
+ 6 ,
205
203
);
206
204
}
207
205
@@ -226,32 +224,27 @@ pub const PoseidonBuiltinRunner = struct {
226
224
address : Relocatable ,
227
225
memory : * Memory ,
228
226
) ! ? MaybeRelocatable {
227
+ _ = allocator ; // autofix
229
228
// Calculate the index of the memory cell.
230
- const index = @mod (
229
+ const index : usize = @mod (
231
230
@as (usize , @intCast (address .offset )),
232
- @as ( usize , @intCast ( self . cells_per_instance )) ,
231
+ poseidon_instance_def . CELLS_PER_POSEIDON ,
233
232
);
234
233
235
234
// Check if the index corresponds to an input cell, if so, return null.
236
- if (index < self . n_input_cells ) return null ;
235
+ if (index < poseidon_instance_def . INPUT_CELLS_PER_POSEIDON ) return null ;
237
236
238
237
// Check if the cell value is already cached, if so, return it.
239
238
if (self .cache .get (address )) | felt | return .{ .felt = felt };
240
239
241
240
// Calculate the addresses for the first input cell and first output cell.
242
241
const first_input_addr = try address .subUint (index );
243
- const first_output_addr = try first_input_addr .addUint (self . n_input_cells );
242
+ const first_output_addr = try first_input_addr .addUint (poseidon_instance_def . INPUT_CELLS_PER_POSEIDON );
244
243
245
244
// Initialize an array list to store input cell values.
246
- var input_felts = try ArrayList (Felt252 ).initCapacity (allocator , self .n_input_cells );
247
- defer input_felts .deinit ();
248
-
249
245
// Iterate over input cells, retrieve their values, and append them to the array list.
250
- for (0.. self .n_input_cells ) | i | {
251
- const val = memory .get (try first_input_addr .addUint (i )) orelse return null ;
252
- try input_felts .append (val .intoFelt () catch
253
- return RunnerError .BuiltinExpectedInteger );
254
- }
246
+ var input_felts = memory .getFeltRange (first_input_addr , poseidon_instance_def .INPUT_CELLS_PER_POSEIDON ) catch return RunnerError .BuiltinExpectedInteger ;
247
+ defer input_felts .deinit ();
255
248
256
249
// Perform Poseidon permutation computation on the input cells.
257
250
// TODO: optimize to use pointer on state
@@ -261,11 +254,12 @@ pub const PoseidonBuiltinRunner = struct {
261
254
262
255
PoseidonHasher .permuteComp ();
263
256
264
- @memcpy (input_felts .items [0.. 3], PoseidonHasher .state [0.. 3]);
265
-
266
257
// Iterate over input cells and cache their computed values.
267
- for (0.. self .n_input_cells , input_felts .items ) | i , elem | {
268
- try self .cache .put (try first_output_addr .addUint (i ), elem );
258
+ inline for (0.. 3) | i | {
259
+ try self .cache .put (
260
+ try first_output_addr .addUint (i ),
261
+ PoseidonHasher .state [i ],
262
+ );
269
263
}
270
264
271
265
// Return the cached value for the specified memory cell address.
0 commit comments