Skip to content

Commit

Permalink
test_u256_into_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Sep 7, 2024
1 parent c39d888 commit dc9ba17
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/utils/hash.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use core::fmt::{Display, Formatter, Error};
use core::to_byte_array::AppendFormattedToByteArray;
use core::integer::u128_byte_reverse;
use super::bit_shifts::{shr};
use core::hash::{Hash, HashStateTrait};

/// 256-bit hash digest.
Expand Down Expand Up @@ -48,47 +47,45 @@ pub impl DigestIntoByteArray of Into<Digest, ByteArray> {
}
}

const POW_2_32: u128 = 0x100000000;
const POW_2_64: u128 = 0x10000000000000000;
const POW_2_96: u128 = 0x1000000000000000000000000;
const NZ_POW2_32_128: NonZero<u128> = 0x100000000;
const NZ_POW2_32_64: NonZero<u64> = 0x100000000;

/// Converts a `u256` value into a `Digest` type and reverse bytes order.
/// u256 is big-endian like in explorer, while Digest is little-endian order.
pub impl U256IntoDigest of Into<u256, Digest> {
fn into(self: u256) -> Digest {
let mut result: Array<u32> = array![];

let mut low: u128 = u128_byte_reverse(self.high);
let mut high: u128 = u128_byte_reverse(self.low);

for _ in 0_u32
..4 {
result.append((low & 0xffffffff).try_into().unwrap());
low = shr(low, 32_u32);
};
let (q_96, high_32_0) = DivRem::div_rem(high, NZ_POW2_32_128);
let (q_64, high_64_32) = DivRem::div_rem(q_96, NZ_POW2_32_128);
let q_64_t: u64 = q_64.try_into().unwrap();
let (high_128_96, high_96_64) = DivRem::div_rem(q_64_t, NZ_POW2_32_64);

for _ in 0_u32
..4 {
result.append((high & 0xffffffff).try_into().unwrap());
high = shr(high, 32_u32);
};
let (q_96, low_32_0) = DivRem::div_rem(low, NZ_POW2_32_128);
let (q_64, low_64_32) = DivRem::div_rem(q_96, NZ_POW2_32_128);
let q_64_t: u64 = q_64.try_into().unwrap();
let (low_128_96, low_96_64) = DivRem::div_rem(q_64_t, NZ_POW2_32_64);

Digest {
value: [
*result[7],
*result[6],
*result[5],
*result[4],
*result[3],
*result[2],
*result[1],
*result[0],
high_128_96.try_into().unwrap(),
high_96_64.try_into().unwrap(),
high_64_32.try_into().unwrap(),
high_32_0.try_into().unwrap(),
low_128_96.try_into().unwrap(),
low_96_64.try_into().unwrap(),
low_64_32.try_into().unwrap(),
low_32_0.try_into().unwrap(),
]
}
}
}


const POW_2_32: u128 = 0x100000000;
const POW_2_64: u128 = 0x10000000000000000;
const POW_2_96: u128 = 0x1000000000000000000000000;

/// Converts a `Digest` value into a `u256` type and reverse bytes order.
/// Digest is little-endian order, while u256 is big-endian like in explorer.
pub impl DigestIntoU256 of Into<Digest, u256> {
Expand Down

0 comments on commit dc9ba17

Please sign in to comment.