Skip to content

Commit

Permalink
Move truncated_blake2s to blake2s module
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Jan 4, 2024
1 parent 031b204 commit caab43c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
16 changes: 16 additions & 0 deletions src/common/blake2s.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
use core::integer::u32_wrapping_add;
use cairo_verifier::common::array_append::ArrayAppendTrait;
use cairo_verifier::common::flip_endianness::FlipEndiannessTrait;


fn blake2s(data: Array<u32>) -> u256 {
let mut state = blake2s_init();
state = blake2s_update(state, data);
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::<u32>::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 {
Expand Down
11 changes: 10 additions & 1 deletion src/common/tests/test_blake2s.cairo
Original file line number Diff line number Diff line change
@@ -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<u32> {
let mut arr = ArrayTrait::new();
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 2 additions & 10 deletions src/vector_commitment/tests/test_vector_commitment.cairo
Original file line number Diff line number Diff line change
@@ -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<VectorQuery> {
array![
Expand Down
15 changes: 1 addition & 14 deletions src/vector_commitment/vector_commitment.cairo
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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::<u32>::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()
}

0 comments on commit caab43c

Please sign in to comment.