Skip to content

Commit d334a22

Browse files
committed
Added e2e tests of blake improvement.
1 parent 553fc47 commit d334a22

2 files changed

Lines changed: 301 additions & 14 deletions

File tree

corelib/src/test/hash_test.cairo

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::blake::{blake2s_compress, blake2s_finalize};
1+
use crate::blake::{blake2s_compress, blake2s_finalize, blake2s_finalize_guarantees};
22
use crate::hash::{HashStateExTrait, HashStateTrait};
33
use crate::poseidon::PoseidonTrait;
44
use crate::test::test_utils::assert_eq;
@@ -94,18 +94,12 @@ fn test_blake2s() {
9494
let msg = BoxTrait::new([0_u32; 16]);
9595
let byte_count = 64_u32;
9696
assert_eq!(
97-
blake2s_compress(state, byte_count, msg).unbox(),
98-
[
99-
0xe816e42a, 0x7d9875d8, 0xfda62c55, 0xa2c6f449, 0xca7af611, 0xdd2f7629, 0xbcd92323,
100-
0x15c3ab3b,
101-
],
97+
to_u256(blake2s_compress(state, byte_count, msg)),
98+
0x2ae416e8d875987d552ca6fd49f4c6a211f67aca29762fdd2323d9bc3babc315,
10299
);
103100
assert_eq!(
104-
blake2s_finalize(state, byte_count, msg).unbox(),
105-
[
106-
0x7a59305, 0x56b8b489, 0xbe3bb37e, 0x58ec6ba0, 0x2f53d5d3, 0x26cd7988, 0xde14c740,
107-
0x3e3f372e,
108-
],
101+
to_u256(blake2s_finalize(state, byte_count, msg)),
102+
0x593a50789b4b8567eb33bbea06bec58d3d5532f8879cd2640c714de2e373f3e,
109103
);
110104
}
111105

@@ -122,10 +116,67 @@ fn test_blake2s_with_abc() {
122116
// Message `abc` padded with zeros.
123117
let msg = BoxTrait::new(['cba', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
124118
assert_eq!(
125-
blake2s_finalize(state, 3, msg).unbox(),
119+
to_u256(blake2s_finalize(state, 3, msg)),
120+
0x508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982,
121+
);
122+
}
123+
124+
#[test]
125+
fn test_blake2s_split_and_guarantees() {
126+
// hashing `abc` as it is done in RFC 7693 Appendix B.
127+
// Initial state is the IV, with keylen 0 and output length 32.
128+
let state = BoxTrait::new(
126129
[
127-
0x8c5e8c50, 0xe2147c32, 0xa32ba7e1, 0x2f45eb4e, 0x208b4537, 0x293ad69e, 0x4c9b994d,
128-
0x82596786,
130+
0x6A09E667 ^ (0x01010000 ^ 0x20), 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F,
131+
0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
129132
],
130133
);
134+
assert_eq!(
135+
to_u256(blake2s_finalize_guarantees(state, 3, msg::from_felt252s('cba', 0))),
136+
0x508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982,
137+
);
138+
assert_eq!(
139+
to_u256(
140+
blake2s_finalize_guarantees(
141+
state, 32, msg::from_felt252s('\x0543210zyxwvutsrqponmlkjihgfedcba', 0),
142+
),
143+
),
144+
0x39b7197928a66cd232d8c5b74d02215a21386228e772076eaf544395b5d32c03,
145+
);
146+
}
147+
148+
fn to_u256(value: Box<[u32; 8]>) -> u256 {
149+
let mut result: u256 = 0;
150+
for word in value.unbox().span() {
151+
result *= 0x100000000;
152+
result += (0x1000000 * (*word % 0x100)).into();
153+
result += (0x10000 * (*word / 0x100 % 0x100)).into();
154+
result += (0x100 * (*word / 0x10000 % 0x100)).into();
155+
result += (*word / 0x1000000 % 0x100).into();
156+
}
157+
result
158+
}
159+
160+
mod msg {
161+
#[feature("bounded-int-utils")]
162+
type U32Guarantee =
163+
core::internal::bounded_int::BoundedIntGuarantee<0, 0xffffffff>;
164+
pub extern fn u128_to_u32_guarantees(
165+
value: u128,
166+
) -> (U32Guarantee, U32Guarantee, U32Guarantee, U32Guarantee) nopanic;
167+
168+
pub fn from_felt252s(a: felt252, b: felt252) -> Box<[U32Guarantee; 16]> {
169+
let a: u256 = a.into();
170+
let b: u256 = b.into();
171+
let (a_w0, a_w1, a_w2, a_w3) = u128_to_u32_guarantees(a.low);
172+
let (a_w4, a_w5, a_w6, a_w7) = u128_to_u32_guarantees(a.high);
173+
let (b_w0, b_w1, b_w2, b_w3) = u128_to_u32_guarantees(b.low);
174+
let (b_w4, b_w5, b_w6, b_w7) = u128_to_u32_guarantees(b.high);
175+
BoxTrait::new(
176+
[
177+
a_w0, a_w1, a_w2, a_w3, a_w4, a_w5, a_w6, a_w7, b_w0, b_w1, b_w2, b_w3, b_w4, b_w5,
178+
b_w6, b_w7,
179+
],
180+
)
181+
}
131182
}

0 commit comments

Comments
 (0)