-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1181 from 0xPolygonMiden/andrew-blake3-internal-e…
…xample Implement internal BLAKE3 hashing example
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 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
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,87 @@ | ||
use super::Example; | ||
use miden::{Assembler, DefaultHost, MemAdviceProvider, Program, StackInputs}; | ||
use stdlib::StdLibrary; | ||
use vm_core::utils::group_slice_elements; | ||
|
||
// CONSTANTS | ||
// ================================================================================================ | ||
|
||
const INITIAL_HASH_VALUE: [u32; 8] = [u32::MAX; 8]; | ||
|
||
// EXAMPLE BUILDER | ||
// ================================================================================================ | ||
|
||
pub fn get_example(n: usize) -> Example<DefaultHost<MemAdviceProvider>> { | ||
// generate the program and expected results | ||
let program = generate_blake3_program(n); | ||
let expected_result = compute_hash_chain(n); | ||
println!( | ||
"Generated a program to compute {}-th iteration of BLAKE3 1-to-1 hash; expected result: {:?}", | ||
n, expected_result | ||
); | ||
|
||
Example { | ||
program, | ||
stack_inputs: StackInputs::try_from_values(INITIAL_HASH_VALUE.iter().map(|&v| v as u64)) | ||
.unwrap(), | ||
host: DefaultHost::default(), | ||
expected_result, | ||
num_outputs: 8, | ||
} | ||
} | ||
|
||
/// Generates a program to compute the `n`-th hash of blake3 1-to-1 hash chain | ||
fn generate_blake3_program(n: usize) -> Program { | ||
let program = format!( | ||
" | ||
use.std::crypto::hashes::blake3 | ||
begin | ||
repeat.{} | ||
exec.blake3::hash_1to1 | ||
end | ||
end", | ||
n | ||
); | ||
|
||
Assembler::default() | ||
.with_library(&StdLibrary::default()) | ||
.unwrap() | ||
.compile(&program) | ||
.unwrap() | ||
} | ||
|
||
/// Computes the `n`-th hash of blake3 1-to-1 hash chain | ||
fn compute_hash_chain(n: usize) -> Vec<u64> { | ||
let mut bytes: [u8; 32] = INITIAL_HASH_VALUE | ||
.iter() | ||
.flat_map(|v| v.to_le_bytes()) | ||
.collect::<Vec<u8>>() | ||
.try_into() | ||
.unwrap(); | ||
|
||
for _ in 0..n { | ||
let hasher = blake3::hash(&bytes); | ||
bytes = *hasher.as_bytes(); | ||
} | ||
|
||
group_slice_elements::<u8, 4>(&bytes) | ||
.iter() | ||
.map(|&bytes| u32::from_le_bytes(bytes) as u64) | ||
.collect::<Vec<u64>>() | ||
} | ||
|
||
// EXAMPLE TESTER | ||
// ================================================================================================ | ||
|
||
#[test] | ||
fn test_blake3_example() { | ||
let example = get_example(2); | ||
super::test_example(example, false); | ||
} | ||
|
||
#[test] | ||
fn test_blake3_example_fail() { | ||
let example = get_example(2); | ||
super::test_example(example, true); | ||
} |
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