diff --git a/src/common/blake2s.cairo b/src/common/blake2s.cairo index c49248194..a0a78fc8f 100644 --- a/src/common/blake2s.cairo +++ b/src/common/blake2s.cairo @@ -1,4 +1,7 @@ use core::integer::u32_wrapping_add; +use cairo_verifier::common::array_append::ArrayAppendTrait; +use cairo_verifier::common::flip_endianness::FlipEndiannessTrait; + fn blake2s(data: Array) -> u256 { let mut state = blake2s_init(); @@ -6,6 +9,19 @@ fn blake2s(data: Array) -> u256 { blake2s_final(state) } +// A 160 LSB truncated version of blake2s. +// hash: +// blake2s(x, y) & ~((1<<96) - 1). +fn truncated_blake2s(x: felt252, y: felt252) -> felt252 { + let mut data = ArrayTrait::::new(); + data.append_big_endian(x); + data.append_big_endian(y); + + // Truncate hash - convert value to felt, by taking the least significant 160 bits. + let hash = blake2s(data).flip_endianness() % 0x10000000000000000000000000000000000000000; + hash.try_into().unwrap() +} + // internals: fn load32(p0: u8, p1: u8, p2: u8, p3: u8) -> u32 { diff --git a/src/common/tests/test_blake2s.cairo b/src/common/tests/test_blake2s.cairo index 44ac89523..52f7a1e3b 100644 --- a/src/common/tests/test_blake2s.cairo +++ b/src/common/tests/test_blake2s.cairo @@ -1,4 +1,4 @@ -use cairo_verifier::common::blake2s::{blake2s, load32}; +use cairo_verifier::common::blake2s::{blake2s, truncated_blake2s, load32}; fn get_arr_v1(n: u32) -> Array { let mut arr = ArrayTrait::new(); @@ -154,6 +154,15 @@ fn test_blake2s_v2() { ); } +#[test] +#[available_gas(9999999999)] +fn test_truncated_blake2s() { + let x = 1157029198022238202306346125123666191662554108005; + let y = 129252051435949032402481343903845417193011527432; + let out = truncated_blake2s(x, y); + assert(out == 642191007116032514313255519742888271333651019057, 'invalid truncated_blake2s'); +} + // notice: this test takes a lot of time and computation // uncomment it when making changes to blake2s // #[test] diff --git a/src/vector_commitment/tests/test_vector_commitment.cairo b/src/vector_commitment/tests/test_vector_commitment.cairo index ea58ace58..f5e5f8945 100644 --- a/src/vector_commitment/tests/test_vector_commitment.cairo +++ b/src/vector_commitment/tests/test_vector_commitment.cairo @@ -1,18 +1,10 @@ use cairo_verifier::vector_commitment::vector_commitment::{ - truncated_blake2s, VectorCommitment, VectorCommitmentConfig, VectorCommitmentWitness, - vector_commit, VectorQuery, vector_commitment_decommit, validate_vector_commitment + VectorCommitment, VectorCommitmentConfig, VectorCommitmentWitness, vector_commit, VectorQuery, + vector_commitment_decommit, validate_vector_commitment }; use cairo_verifier::channel::channel::Channel; use core::debug::PrintTrait; -#[test] -#[available_gas(9999999999)] -fn test_truncated_blake2s() { - let x = 1157029198022238202306346125123666191662554108005; - let y = 129252051435949032402481343903845417193011527432; - let out = truncated_blake2s(x, y); - assert(out == 642191007116032514313255519742888271333651019057, 'invalid truncated_blake2s'); -} fn get_queries() -> Span { array![ diff --git a/src/vector_commitment/vector_commitment.cairo b/src/vector_commitment/vector_commitment.cairo index 6fc0bfbac..255cc3e38 100644 --- a/src/vector_commitment/vector_commitment.cairo +++ b/src/vector_commitment/vector_commitment.cairo @@ -1,5 +1,5 @@ use cairo_verifier::common::{ - array_append::ArrayAppendTrait, blake2s::blake2s, math::pow, + array_append::ArrayAppendTrait, blake2s::blake2s, math::pow, blake2s::truncated_blake2s, flip_endianness::FlipEndiannessTrait, math::DivRemFelt252, math::Felt252PartialOrd }; use cairo_verifier::channel::channel::{Channel, ChannelImpl}; @@ -160,16 +160,3 @@ fn hash_blake_or_poseidon(x: felt252, y: felt252, is_verifier_friendly: bool) -> truncated_blake2s(x, y) } } - -// A 160 LSB truncated version of blake2s. -// hash: -// blake2s(x, y) & ~((1<<96) - 1). -fn truncated_blake2s(x: felt252, y: felt252) -> felt252 { - let mut data = ArrayTrait::::new(); - data.append_big_endian(x); - data.append_big_endian(y); - - // Truncate hash - convert value to felt, by taking the least significant 160 bits. - let hash = blake2s(data).flip_endianness() % 0x10000000000000000000000000000000000000000; - hash.try_into().unwrap() -}