Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Jan 12, 2025
1 parent bbc93af commit add5b3d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,26 @@ use super::poseidon_3::{FULL_ROUNDS, MDS, PARTIAL_ROUNDS, RATE_PLUS_1};

pub const FN_NAME: &str = "Hades";

pub const N_ROUND_CONSTANTS: usize = RATE_PLUS_1 * (FULL_ROUNDS + PARTIAL_ROUNDS);

pub static ROUND_CONSTANTS: LazyLock<[Felt; N_ROUND_CONSTANTS]> =
LazyLock::new(|| core::array::from_fn(hades_ark));
pub static ROUND_CONSTANTS: LazyLock<[Felt; RATE_PLUS_1 * (FULL_ROUNDS + PARTIAL_ROUNDS)]> =
LazyLock::new(|| {
core::array::from_fn(|idx| {
// https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/poseidon_utils.py#L15
let value = format!("{}{}", FN_NAME, idx);

let hash = {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(value);
hasher.finalize()
};

Felt::from_bytes_be(&hash.into())
})
});

pub const STARKNET_HADES_PERM_3: HadesPermutate<RATE_PLUS_1, FULL_ROUNDS, PARTIAL_ROUNDS> =
pub const HADES_PERM_3: HadesPermutate<RATE_PLUS_1, FULL_ROUNDS, PARTIAL_ROUNDS> =
HadesPermutate { mds: MDS };

// https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/poseidon_utils.py#L15
pub fn hades_ark(idx: usize) -> Felt {
let value = format!("{}{}", FN_NAME, idx);

use sha2::Digest;

let hash = {
let mut hasher = sha2::Sha256::new();
hasher.update(value);
hasher.finalize()
};

Felt::from_bytes_be(&hash.into())
}

pub struct HadesPermutate<const DIM: usize, const FULL_ROUNDS: usize, const PARTIAL_ROUNDS: usize> {
pub mds: [[i64; DIM]; DIM],
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod hades;
pub mod poseidon_3;

use hades::STARKNET_HADES_PERM_3;
use hades::HADES_PERM_3;
use starknet::core::types::Felt;

// References:
Expand All @@ -28,7 +28,7 @@ impl Default for Poseidon3Hasher {
impl Poseidon3Hasher {
pub fn write(mut self, value: Felt) -> Self {
if self.odd {
self.state = STARKNET_HADES_PERM_3.hades_permutation([
self.state = HADES_PERM_3.hades_permutation([
self.state[0],
self.state[1] + value,
self.state[2],
Expand All @@ -43,13 +43,13 @@ impl Poseidon3Hasher {

pub fn finish(self) -> Felt {
if self.odd {
STARKNET_HADES_PERM_3.hades_permutation([
HADES_PERM_3.hades_permutation([
self.state[0],
self.state[1] + Felt::ONE,
self.state[2],
])[0]
} else {
STARKNET_HADES_PERM_3.hades_permutation([
HADES_PERM_3.hades_permutation([
self.state[0] + Felt::ONE,
self.state[1],
self.state[2],
Expand All @@ -63,11 +63,7 @@ impl Poseidon3Hasher {
for chunk in span.chunks(2) {
match chunk {
[x, y] => {
state = STARKNET_HADES_PERM_3.hades_permutation([
state[0] + x,
state[1] + y,
state[2],
])
state = HADES_PERM_3.hades_permutation([state[0] + x, state[1] + y, state[2]])
}
[x] => {
return Self {
Expand Down

0 comments on commit add5b3d

Please sign in to comment.