Skip to content

Commit 34a5f57

Browse files
starknet_os: change all encoding to le in naive blake
1 parent 820105f commit 34a5f57

File tree

5 files changed

+29
-34
lines changed

5 files changed

+29
-34
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from starkware.cairo.common.registers import get_fp_and_pc
66
from starkware.starknet.core.os.naive_blake import (
77
calc_blake_hash,
88
naive_encode_felt252s_to_u32s,
9-
u256_to_felt,
9+
felt_from_le_u32s,
1010
create_initial_state_for_blake2s,
1111
blake_with_opcode_for_single_16_length_word,
1212
)
@@ -197,21 +197,22 @@ func encrypt_inner{range_check_ptr, encrypted_dst: felt*}(
197197
assert blake_output[6] = encoded_symmetric_key[6];
198198
assert blake_output[7] = encoded_symmetric_key[7];
199199
let blake_output = &blake_output[8];
200-
// Write encoded index to blake output - since index is small, manually encode as [0, 0, 0, 0, 0, 0, 0, index].
201-
assert blake_output[0] = 0;
200+
// Write encoded index to blake output - since index is small,
201+
// manually encode in little-endian notion as [index, 0, 0, 0, 0, 0, 0, 0].
202+
assert blake_output[0] = index;
202203
assert blake_output[1] = 0;
203204
assert blake_output[2] = 0;
204205
assert blake_output[3] = 0;
205206
assert blake_output[4] = 0;
206207
assert blake_output[5] = 0;
207208
assert blake_output[6] = 0;
208-
assert blake_output[7] = index;
209+
assert blake_output[7] = 0;
209210
let blake_output = &blake_output[8];
210211
// Calculate blake hash modulo prime.
211212
blake_with_opcode_for_single_16_length_word(
212213
data=blake_encoding_start, out=blake_output, initial_state=initial_state
213214
);
214-
let hash = u256_to_felt(u256=blake_output);
215+
let hash = felt_from_le_u32s(u256=blake_output);
215216
let blake_output = &blake_output[8];
216217

217218
// Encrypt the current element.

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
from starkware.cairo.common.alloc import alloc
22
from starkware.cairo.common.cairo_blake2s.blake2s import blake_with_opcode
33

4+
// Gets a felt that represent a 256-bit unsigned integer stored as an array of eight 32-bit unsigned integers
5+
// represented in little-endian notation. Return the felt representation of the integer modulo prime.
6+
func felt_from_le_u32s(u256: felt*) -> felt {
7+
let hash = u256[7] * 2 ** 224 + u256[6] * 2 ** 192 + u256[5] * 2 ** 160 + u256[4] * 2 ** 128 +
8+
u256[3] * 2 ** 96 + u256[2] * 2 ** 64 + u256[1] * 2 ** 32 + u256[0];
9+
return hash;
10+
}
11+
412
// Computes blake2s of `input` of size 16 felts, representing 32 bits each.
513
// The initial state is the standard BLAKE2s IV XORed with the parameter block P[0] = 0x01010020.
614
func blake_with_opcode_for_single_16_length_word(data: felt*, out: felt*, initial_state: felt*) {
@@ -51,10 +59,9 @@ func create_initial_state_for_blake2s() -> (initial_state: felt*) {
5159
}
5260

5361
// Encodes a list of felt252s to a list of u32s, each felt is mapped to eight u32s.
54-
// Returns the length of the resulting list of u32s.
5562
func naive_encode_felt252s_to_u32s(
5663
packed_values_len: felt, packed_values: felt*, unpacked_u32s: felt*
57-
) -> felt {
64+
) {
5865
alloc_locals;
5966
6067
local end: felt* = &packed_values[packed_values_len];
@@ -65,41 +72,29 @@ func naive_encode_felt252s_to_u32s(
6572
6673
loop:
6774
if (end == packed_values) {
68-
return out - unpacked_u32s;
75+
return ();
6976
}
7077
7178
// TODO(Noa): Assert that the limbs represent a number in the range [0, PRIME-1].
7279
// Assert that the limbs represent the number.
73-
assert packed_values[0] = (
74-
(out[7] + (2 ** 32 * out[6])) +
75-
2 ** (32 * 2) * (out[5] + 2 ** 32 * out[4]) +
76-
2 ** (32 * 4) * (out[3] + 2 ** 32 * out[2]) +
77-
2 ** (32 * 6) * (out[1] + 2 ** 32 * out[0])
78-
);
80+
let actual_value = felt_from_le_u32s(u256=out);
81+
assert packed_values[0] = actual_value;
7982
8083
tempvar out = &out[8];
8184
tempvar packed_values = &packed_values[1];
8285
jmp loop;
8386
}
8487

85-
// Gets a felt that represent a 256-bit unsigned integer stored as an array of eight 32-bit unsigned integers
86-
// represented in little-endian notation. Return the felt representation of the integer modulo prime.
87-
func u256_to_felt(u256: felt*) -> felt {
88-
let hash = u256[7] * 2 ** 224 + u256[6] * 2 ** 192 + u256[5] * 2 ** 160 + u256[4] * 2 ** 128 +
89-
u256[3] * 2 ** 96 + u256[2] * 2 ** 64 + u256[1] * 2 ** 32 + u256[0];
90-
return hash;
91-
}
92-
9388
// / Encodes a slice of `Felt` values into 32-bit words, then hashes the resulting byte stream
9489
// / with Blake2s-256 and returns the 256-bit digest to a 252-bit field element `Felt`.
9590
func calc_blake_hash{range_check_ptr: felt}(data_len: felt, data: felt*) -> (hash: felt) {
9691
alloc_locals;
9792
let (local encoded_data: felt*) = alloc();
98-
let encoded_data_len = naive_encode_felt252s_to_u32s(
93+
naive_encode_felt252s_to_u32s(
9994
packed_values_len=data_len, packed_values=data, unpacked_u32s=encoded_data
10095
);
10196
let (local blake_output: felt*) = alloc();
102-
blake_with_opcode(len=encoded_data_len, data=encoded_data, out=blake_output);
103-
let hash = u256_to_felt(u256=blake_output);
97+
blake_with_opcode(len=8 * data_len, data=encoded_data, out=blake_output);
98+
let hash = felt_from_le_u32s(u256=blake_output);
10499
return (hash=hash);
105100
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"os": "0x7f1ad76d2bc6845ae61c5ccb2f8aae2e2ff1418b5572bebcdf2dd832556a6b0",
3-
"aggregator": "0x31a2d2cf5b673b718d9a9ed527fc343c2744ec3bd1daf1a7b9f1a4a1fa3d21d",
4-
"aggregator_with_prefix": "0x4460a2cd0996f05489f8339d50f317d07b06f26dd2ecd5a5540368d2cef2a9d"
2+
"os": "0x411bcd3d0448fcf9ced52a49731e9e81ecbb52d86c862d575716b8b2c38aed1",
3+
"aggregator": "0x11009a80eecd0085c7890ee149d4c837f5c08ab1f076ad2ec714bd29eb77052",
4+
"aggregator_with_prefix": "0x228933088f4fd745ab9c1bee3571779d6ca2a86117fa8b5a17cabeddf01404e"
55
}

crates/starknet_os/src/hints/hint_implementation/blake2s/implementation.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ pub(crate) fn naive_unpack_felt252s_to_u32s(
9595
HintArgs { vm, ids_data, ap_tracking, .. }: HintArgs<'_>,
9696
) -> OsHintResult {
9797
let (unpacked_u32s, vals) = unpack_setup(vm, ids_data, ap_tracking)?;
98-
9998
let out: Vec<MaybeRelocatable> = vals
10099
.into_iter()
101100
.map(|val| val.to_biguint())
102101
.flat_map(|mut val| {
103102
let mut limbs = vec![BigUint::from(0_u32); 8];
104-
for limb in limbs.iter_mut().rev() {
103+
for limb in limbs.iter_mut() {
105104
let (q, r) = val.div_rem(&POW2_32);
106105
*limb = r;
107106
val = q;

crates/starknet_os/src/hints/hint_implementation/state_diff_encryption/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ fn calc_blake_hash(data: &[Felt]) -> Felt {
118118
pub fn naive_encode_felts_to_u32s(felts: Vec<Felt>) -> Vec<u32> {
119119
let mut unpacked_u32s = Vec::new();
120120
for felt in felts {
121-
let felt_as_be_bytes = felt.to_bytes_be();
122-
// big: 8 limbs, big‐endian order.
123-
for chunk in felt_as_be_bytes.chunks_exact(4) {
124-
unpacked_u32s.push(u32::from_be_bytes(chunk.try_into().unwrap()));
121+
let felt_as_le_bytes = felt.to_bytes_le();
122+
// big: 8 limbs, little-endian order.
123+
for chunk in felt_as_le_bytes.chunks_exact(4) {
124+
unpacked_u32s.push(u32::from_le_bytes(chunk.try_into().unwrap()));
125125
}
126126
}
127127
unpacked_u32s

0 commit comments

Comments
 (0)