Skip to content

Commit 820105f

Browse files
starknet_os: fix blake2s finalize opcode command in naive blake
1 parent d4010e4 commit 820105f

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/naive_blake.cairo

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
from starkware.cairo.common.alloc import alloc
22
from starkware.cairo.common.cairo_blake2s.blake2s import blake_with_opcode
3-
from starkware.cairo.common.cairo_blake2s.blake2s import BLAKE2S_FINALIZE_INSTRUCTION
43

54
// Computes blake2s of `input` of size 16 felts, representing 32 bits each.
65
// The initial state is the standard BLAKE2s IV XORed with the parameter block P[0] = 0x01010020.
76
func blake_with_opcode_for_single_16_length_word(data: felt*, out: felt*, initial_state: felt*) {
7+
const BLAKE2S_FINALIZE_OPCODE_EXT = 2;
8+
const OP0_REG = 1; // State is fp-based.
9+
const OP1_FP = 3; // Data is fp-based.
10+
const AP_ADD1 = 11; // Increment ap by 1 after the instruction.
11+
const BLAKE2S_FLAGS = 2 ** OP0_REG + 2 ** OP1_FP + 2 ** AP_ADD1;
12+
13+
const STATE_OFFSET = -3;
14+
const MESSAGE_OFFSET = -5;
15+
const COUNTER_OFFSET = -1;
16+
17+
const POS_STATE_OFFSET = 2 ** 15 + STATE_OFFSET;
18+
const POS_MESSAGE_OFFSET = 2 ** 15 + MESSAGE_OFFSET;
19+
const POS_COUNTER_OFFSET = 2 ** 15 + COUNTER_OFFSET;
20+
21+
const BLAKE2S_FINALIZE_INSTRUCTION = POS_COUNTER_OFFSET + POS_STATE_OFFSET * 2 ** 16 +
22+
POS_MESSAGE_OFFSET * 2 ** 32 + BLAKE2S_FLAGS * 2 ** 48 + BLAKE2S_FINALIZE_OPCODE_EXT * 2 **
23+
63;
24+
825
tempvar counter = 64;
9-
[ap] = initial_state, ap++;
10-
[ap] = data, ap++;
11-
[ap] = counter, ap++;
1226
[ap] = out;
27+
static_assert [ap + COUNTER_OFFSET] == counter;
28+
static_assert [fp + STATE_OFFSET] == initial_state;
29+
static_assert [fp + MESSAGE_OFFSET] == data;
1330
dw BLAKE2S_FINALIZE_INSTRUCTION;
14-
// Increment AP after blake opcode.
15-
ap += 1;
1631
return ();
1732
}
1833

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"os": "0x338203798f8c939bae2c9bd06afd80bb63595007601729b33d6739920416c24",
3-
"aggregator": "0x110273c1890c2e09a34e3995eeeaa7e0a10fe5a8bd98c84ccf6243512f7a7e6",
4-
"aggregator_with_prefix": "0x3ca5d4c4724e45c896bf36d26e28a7e585e61d680b3b06962af652cafa166d7"
2+
"os": "0x7f1ad76d2bc6845ae61c5ccb2f8aae2e2ff1418b5572bebcdf2dd832556a6b0",
3+
"aggregator": "0x31a2d2cf5b673b718d9a9ed527fc343c2744ec3bd1daf1a7b9f1a4a1fa3d21d",
4+
"aggregator_with_prefix": "0x4460a2cd0996f05489f8339d50f317d07b06f26dd2ecd5a5540368d2cef2a9d"
55
}

0 commit comments

Comments
 (0)