-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1b8d89
commit 205303e
Showing
5 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
scarb nightly-2024-04-24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
mod fields; | ||
mod vcs; | ||
|
||
pub type BaseField = fields::m31::M31; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use core::array::ArrayTrait; | ||
use core::option::OptionTrait; | ||
use core::poseidon::poseidon_hash_span; | ||
use stwo_cairo_verifier::BaseField; | ||
|
||
// A Merkle node hash is a hash of: | ||
// [left_child_hash, right_child_hash], column0_value, column1_value, ... | ||
// "[]" denotes optional values. | ||
// The largest Merkle layer has no left and right child hashes. The rest of the layers have | ||
// children hashes. | ||
// At each layer, the tree may have multiple columns of the same length as the layer. | ||
// Each node in that layer contains one value from each column. | ||
pub trait MerkleHasher { | ||
type Hash; | ||
// Hashes a single Merkle node. | ||
fn hash_node( | ||
children_hashes: Option<(Self::Hash, Self::Hash)>, column_values: Array<BaseField>, | ||
) -> Self::Hash; | ||
} | ||
|
||
const M31_ELS_IN_HASH_BLOCK: usize = 8; | ||
const M31_ELS_IN_HASH_BLOCK_MINUS1: usize = 7; | ||
pub impl PoseidonMerkleHasher of MerkleHasher { | ||
type Hash = felt252; | ||
|
||
fn hash_node( | ||
children_hashes: Option<(Self::Hash, Self::Hash)>, mut column_values: Array<BaseField>, | ||
) -> Self::Hash { | ||
let mut hash_array: Array<felt252> = Default::default(); | ||
if let Option::Some((x, y)) = children_hashes { | ||
hash_array.append(x); | ||
hash_array.append(y); | ||
} | ||
|
||
// Pad column_values to a multiple of 8. | ||
let mut pad_len = M31_ELS_IN_HASH_BLOCK_MINUS1 | ||
- ((column_values.len() + 7) % M31_ELS_IN_HASH_BLOCK); | ||
while pad_len > 0 { | ||
column_values.append(core::num::traits::Zero::zero()); | ||
pad_len = M31_ELS_IN_HASH_BLOCK_MINUS1 | ||
- ((column_values.len() + M31_ELS_IN_HASH_BLOCK_MINUS1) % M31_ELS_IN_HASH_BLOCK); | ||
}; | ||
|
||
while !column_values.is_empty() { | ||
let mut word = 0; | ||
word = word * 0x80000000 + column_values.pop_front().unwrap().inner.into(); | ||
word = word * 0x80000000 + column_values.pop_front().unwrap().inner.into(); | ||
word = word * 0x80000000 + column_values.pop_front().unwrap().inner.into(); | ||
word = word * 0x80000000 + column_values.pop_front().unwrap().inner.into(); | ||
word = word * 0x80000000 + column_values.pop_front().unwrap().inner.into(); | ||
word = word * 0x80000000 + column_values.pop_front().unwrap().inner.into(); | ||
word = word * 0x80000000 + column_values.pop_front().unwrap().inner.into(); | ||
word = word * 0x80000000 + column_values.pop_front().unwrap().inner.into(); | ||
}; | ||
|
||
poseidon_hash_span(hash_array.span()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::PoseidonMerkleHasher; | ||
use stwo_cairo_verifier::fields::m31::{m31}; | ||
|
||
#[test] | ||
fn test_m31() { | ||
assert_eq!( | ||
PoseidonMerkleHasher::hash_node(Option::None, array![m31(0), m31(1)]), | ||
973835572668429495915136902981656666590582180872133591629269551720657739196 | ||
); | ||
} | ||
} |