Skip to content

Commit 0ee1f1f

Browse files
committed
Attempting changes.
1 parent 0569c3f commit 0ee1f1f

File tree

4 files changed

+80
-39
lines changed

4 files changed

+80
-39
lines changed

Diff for: riscv/src/large_field/runtime.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,11 @@ impl Runtime {
283283
None,
284284
"poseidon2_gl",
285285
vec!["memory", "MIN_DEGREE", "LARGE_SUBMACHINES_MAX_DEGREE"],
286-
[r#"instr poseidon2_gl X, Y
286+
[r#"instr poseidon2_gl X, Y, Z
287287
link ~> tmp1_col = regs.mload(X, STEP)
288288
link ~> tmp2_col = regs.mload(Y, STEP + 1)
289-
link ~> poseidon2_gl.poseidon2_permutation(tmp1_col, tmp2_col, STEP)
289+
link ~> tmp?_col = regs.mload(Z, STEP + 2)
290+
link ~> poseidon2_gl.permute(tmp1_col, STEP, tmp2_col, STEP + 1, tmp?_col)
290291
{
291292
// make sure tmp1_col and tmp2_col are aligned memory addresses
292293
tmp3_col * 4 = tmp1_col,
@@ -307,7 +308,7 @@ impl Runtime {
307308
r#"instr split_gl_vec X, Y
308309
link ~> tmp1_col = regs.mload(X, STEP)
309310
link ~> tmp2_col = regs.mload(Y, STEP + 1)
310-
link ~> split_gl_vec.split(tmp1_col, tmp2_col, STEP + 2)
311+
link ~> split_gl_vec.split(tmp1_col, tmp2_col, STEP)
311312
{
312313
// make sure tmp1_col and tmp2_col are aligned memory addresses
313314
tmp3_col * 4 = tmp1_col,
@@ -343,7 +344,7 @@ impl Runtime {
343344
// they can overlap.
344345
self.add_syscall(
345346
Syscall::Poseidon2GL,
346-
std::iter::once("poseidon2_gl 10, 11;".to_string()),
347+
std::iter::once(format!("{} 10, 11, 12;", Syscall::Poseidon2GL.name())),
347348
);
348349

349350
self.add_syscall(

Diff for: std/machines/hash/poseidon2_gl.asm

+49-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::array;
22
use std::check::assert;
33
use std::utils::unchanged_until;
4+
use std::utils::new_bool;
45
use std::utils::force_bool;
56
use std::utils::sum;
67
use std::convert::expr;
@@ -19,8 +20,8 @@ use super::poseidon2_common::poseidon2;
1920
// state size of 8 field elements instead of 12, matching Plonky3's implementation.
2021
//
2122
// This machine assumes each memory word contains a full field element, and it
22-
// writes one field element per memory word. Use SplitGLVec8 to split the output
23-
// into 32-bit words.
23+
// writes one field element per memory word. Use SplitGLVec4 to split the output
24+
// into 32-bit words.
2425
machine Poseidon2GL(mem: Memory) with
2526
latch: latch,
2627
// Allow this machine to be connected via a permutation
@@ -33,19 +34,32 @@ machine Poseidon2GL(mem: Memory) with
3334
// The input data is passed via a memory pointer: the machine will read STATE_SIZE
3435
// field elements from memory.
3536
//
36-
// Similarly, the output data is written to memory at the provided pointer.
37+
// Similarly, the output data is written to memory at the provided pointer. We don't
38+
// have any use for writing the full state as output, so depending on the operation,
39+
// it will either write the first half of the state (used in sponge squeeze) or the
40+
// second half (used in sponge absorb and on merkle tree compression).
3741
//
38-
// Reads happen at the provided time step; writes happen at the next time step.
42+
// Memory reads happen at input_time_step and memory writes happens at output_time_step.
3943
//
4044
// The addresses must be multiple of 4.
41-
operation poseidon2_permutation
45+
//
46+
// This operation can output any combination of the first and second half of the final
47+
// state, depending on the value of output_halves:
48+
// 0: no output
49+
// 1: first half
50+
// 2: second half
51+
// 3: the entire state
52+
operation permute
4253
input_addr,
54+
input_time_step,
4355
output_addr,
44-
time_step ->;
56+
output_time_step,
57+
output_halves ->;
4558

4659
let latch = 1;
4760

48-
let time_step;
61+
let input_time_step;
62+
let output_time_step;
4963

5064
// Poseidon2 parameters, compatible with our powdr-plonky3 implementation.
5165
//
@@ -120,14 +134,14 @@ machine Poseidon2GL(mem: Memory) with
120134
let input: col[STATE_SIZE];
121135

122136
// TODO: when link is available inside functions, we can turn this into array operations.
123-
link if is_used ~> input[0] = mem.mload(input_addr + 0, time_step);
124-
link if is_used ~> input[1] = mem.mload(input_addr + 4, time_step);
125-
link if is_used ~> input[2] = mem.mload(input_addr + 8, time_step);
126-
link if is_used ~> input[3] = mem.mload(input_addr + 12, time_step);
127-
link if is_used ~> input[4] = mem.mload(input_addr + 16, time_step);
128-
link if is_used ~> input[5] = mem.mload(input_addr + 20, time_step);
129-
link if is_used ~> input[6] = mem.mload(input_addr + 24, time_step);
130-
link if is_used ~> input[7] = mem.mload(input_addr + 28, time_step);
137+
link if is_used ~> input[0] = mem.mload(input_addr + 0, input_time_step);
138+
link if is_used ~> input[1] = mem.mload(input_addr + 4, input_time_step);
139+
link if is_used ~> input[2] = mem.mload(input_addr + 8, input_time_step);
140+
link if is_used ~> input[3] = mem.mload(input_addr + 12, input_time_step);
141+
link if is_used ~> input[4] = mem.mload(input_addr + 16, input_time_step);
142+
link if is_used ~> input[5] = mem.mload(input_addr + 20, input_time_step);
143+
link if is_used ~> input[6] = mem.mload(input_addr + 24, input_time_step);
144+
link if is_used ~> input[7] = mem.mload(input_addr + 28, input_time_step);
131145

132146
// Generate the Poseidon2 permutation
133147
let output = poseidon2(
@@ -143,16 +157,25 @@ machine Poseidon2GL(mem: Memory) with
143157
input,
144158
);
145159

146-
// Write the output to memory at the next time step
147-
let output_addr;
160+
// Decide which halves to output:
161+
let output_halves;
162+
let output_first_half = new_bool();
163+
let output_second_half = new_bool();
164+
output_halves = output_first_half + 2 * output_second_half;
148165

149-
// TODO: turn this into array operations
150-
link if is_used ~> mem.mstore(output_addr + 0, time_step + 1, output[0]);
151-
link if is_used ~> mem.mstore(output_addr + 4, time_step + 1, output[1]);
152-
link if is_used ~> mem.mstore(output_addr + 8, time_step + 1, output[2]);
153-
link if is_used ~> mem.mstore(output_addr + 12, time_step + 1, output[3]);
154-
link if is_used ~> mem.mstore(output_addr + 16, time_step + 1, output[4]);
155-
link if is_used ~> mem.mstore(output_addr + 20, time_step + 1, output[5]);
156-
link if is_used ~> mem.mstore(output_addr + 24, time_step + 1, output[6]);
157-
link if is_used ~> mem.mstore(output_addr + 28, time_step + 1, output[7]);
166+
// TODO: turn these into array operations:
167+
168+
// Write the first half of the output
169+
let output_addr;
170+
link if is_used * output_first_half ~> mem.mstore(output_addr + 0, output_time_step, output[0]);
171+
link if is_used * output_first_half ~> mem.mstore(output_addr + 4, output_time_step, output[1]);
172+
link if is_used * output_first_half ~> mem.mstore(output_addr + 8, output_time_step, output[2]);
173+
link if is_used * output_first_half ~> mem.mstore(output_addr + 12, output_time_step, output[3]);
174+
175+
// Write the second half of the output
176+
let second_half_addr = output_addr + 16 * output_first_half;
177+
link if is_used * output_second_half ~> mem.mstore(second_half_addr + 0, output_time_step, output[4]);
178+
link if is_used * output_second_half ~> mem.mstore(second_half_addr + 4, output_time_step, output[5]);
179+
link if is_used * output_second_half ~> mem.mstore(second_half_addr + 8, output_time_step, output[6]);
180+
link if is_used * output_second_half ~> mem.mstore(second_half_addr + 12, output_time_step, output[7]);
158181
}

Diff for: std/machines/hash/poseidon_bb.asm

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::array;
22
use std::check::assert;
33
use std::utils::unchanged_until;
44
use std::utils::force_bool;
5-
use std::utils::new_bool;
65
use std::utils::sum;
76
use std::convert::expr;
87
use std::machines::small_field::memory::Memory;

Diff for: test_data/std/poseidon2_gl_test.asm

+26-8
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ machine Main with degree: main_degree {
1616
reg ADDR1[<=];
1717
reg ADDR2[<=];
1818

19-
// Increase the time step by 2 in each row, so that the poseidon machine
19+
// Increase the time step by 4 in each row, so that the poseidon machine
2020
// can read in the given time step and write in the next time step.
21-
col fixed STEP(i) { 2 * i };
21+
col fixed STEP(i) { 4 * i };
2222
Byte2 byte2;
2323
Memory memory(byte2, memory_degree, memory_degree);
2424

2525
instr mstore ADDR1, X1 ->
2626
link ~> memory.mstore(ADDR1, STEP, X1);
2727

2828
Poseidon2GL poseidon2(memory, poseidon2_degree, poseidon2_degree);
29-
instr poseidon2 ADDR1, ADDR2 -> link ~> poseidon2.poseidon2_permutation(ADDR1, ADDR2, STEP);
29+
instr poseidon2 ADDR1, ADDR2, X1 ->
30+
link ~> poseidon2.permute(ADDR1, STEP, ADDR2, STEP + 2, X1);
3031

3132
col witness val;
3233
instr assert_eq ADDR1, X1 ->
@@ -50,7 +51,24 @@ machine Main with degree: main_degree {
5051
mstore 24, 0;
5152
mstore 28, 0;
5253

53-
poseidon2 0, 0;
54+
// Test only second half output
55+
poseidon2 0, 100, 2;
56+
57+
assert_eq 100, 5905145432652609062;
58+
assert_eq 104, 9814446752588696081;
59+
assert_eq 108, 13759450385053274731;
60+
assert_eq 112, 2402148582355896469;
61+
62+
// Test only first half output
63+
poseidon2 0, 100, 1;
64+
65+
assert_eq 100, 14905565590733827480;
66+
assert_eq 104, 640905753703258831;
67+
assert_eq 108, 4579128623722792381;
68+
assert_eq 112, 158153743058056413;
69+
70+
// Test full state output
71+
poseidon2 0, 0, 3;
5472

5573
assert_eq 0, 14905565590733827480;
5674
assert_eq 4, 640905753703258831;
@@ -72,7 +90,7 @@ machine Main with degree: main_degree {
7290
mstore 24, 1;
7391
mstore 28, 1;
7492

75-
poseidon2 0, 0;
93+
poseidon2 0, 0, 3;
7694

7795
assert_eq 0, 18201552556563266798;
7896
assert_eq 4, 6814935789744812745;
@@ -94,7 +112,7 @@ machine Main with degree: main_degree {
94112
mstore 24, 0xffffffff00000000;
95113
mstore 28, 0xffffffff00000000;
96114

97-
poseidon2 0, 0;
115+
poseidon2 0, 0, 3;
98116

99117
assert_eq 0, 13601391594672984423;
100118
assert_eq 4, 7799837486760213030;
@@ -116,7 +134,7 @@ machine Main with degree: main_degree {
116134
mstore 24, 1456 * 2**32 + 1394942011;
117135
mstore 28, 2087;
118136

119-
poseidon2 0, 0;
137+
poseidon2 0, 0, 3;
120138

121139
assert_eq 0, 14498150941209346562;
122140
assert_eq 4, 8038616707062714447;
@@ -138,7 +156,7 @@ machine Main with degree: main_degree {
138156
mstore 124, 0;
139157
mstore 128, 0;
140158

141-
poseidon2 100, 104;
159+
poseidon2 100, 104, 3;
142160

143161
assert_eq 104, 14905565590733827480;
144162
assert_eq 108, 640905753703258831;

0 commit comments

Comments
 (0)