Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
chg: as suggested in 0xPolygonMiden#123 (comment), simplified test ru…
Browse files Browse the repository at this point in the history
…nning using newly defined macro
  • Loading branch information
itzmeanjan committed Mar 15, 2022
1 parent 9ddb4c5 commit d46ae2e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 59 deletions.
36 changes: 8 additions & 28 deletions processor/src/tests/stdlib/crypto/blake3.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::{execute, Felt, FieldElement, ProgramInputs, Script, MIN_STACK_DEPTH};
use super::build_test;
use crate::{Felt, MIN_STACK_DEPTH};
use vm_core::utils::IntoBytes;

#[test]
fn blake3_2_to_1_hash() {
let script = compile(
"
let source = "
use.std::crypto::hashes::blake3
begin
exec.blake3::hash
end
",
);
";

// prepare random input byte array
let i_digest_0: [u8; 32] = rand_utils::rand_array::<Felt, 4>().into_bytes();
Expand Down Expand Up @@ -43,34 +42,15 @@ fn blake3_2_to_1_hash() {
}

// finally execute miden program on VM
let inputs = ProgramInputs::new(&i_words, &[], Vec::new()).unwrap();
let trace = execute(&script, &inputs).unwrap();
let last_state = trace.last_stack_state();

// first 8 elements of stack top holds blake3 digest, while remaining 8 elements
// are zeroed
let digest_on_stack = convert_to_stack(&digest_words);
assert_eq!(digest_on_stack, last_state);
let test = build_test!(source, &i_words);
// first 8 elements of stack top holds blake3 digest,
// while remaining 8 elements are zeroed
test.expect_stack(&digest_words);
}

// HELPER FUNCTIONS
// ================================================================================================

fn compile(source: &str) -> Script {
let assembler = assembly::Assembler::new();
assembler.compile_script(source).unwrap()
}

/// Takes an array of u64 values and builds a stack, perserving their order and converting them to
/// field elements.
fn convert_to_stack(values: &[u64]) -> [Felt; MIN_STACK_DEPTH] {
let mut result = [Felt::ZERO; MIN_STACK_DEPTH];
for (&value, result) in values.iter().zip(result.iter_mut()) {
*result = Felt::new(value);
}
result
}

/// Given a slice of four consecutive little endian bytes, interprets them as 32 -bit unsigned integer
fn from_le_bytes_to_words(le_bytes: &[u8]) -> u32 {
((le_bytes[3] as u32) << 24)
Expand Down
2 changes: 2 additions & 0 deletions processor/src/tests/stdlib/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
use super::build_test;

mod blake3;
mod sha256;
42 changes: 11 additions & 31 deletions processor/src/tests/stdlib/crypto/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::{execute, Felt, FieldElement, ProgramInputs, Script, MIN_STACK_DEPTH};
use super::build_test;
use crate::{Felt, MIN_STACK_DEPTH};
use sha2::{Digest, Sha256};
use vm_core::utils::IntoBytes;

#[test]
fn sha256_2_to_1_hash() {
let script = compile(
"
use.std::crypto::hashes::sha256
let source = "
use.std::crypto::hashes::sha256
begin
exec.sha256::hash
end",
);
begin
exec.sha256::hash
end";

// prepare random input byte array
let i_digest_0: [u8; 32] = rand_utils::rand_array::<Felt, 4>().into_bytes();
Expand Down Expand Up @@ -43,34 +42,15 @@ fn sha256_2_to_1_hash() {
}

// finally execute miden program on VM
let inputs = ProgramInputs::new(&i_words, &[], Vec::new()).unwrap();
let trace = execute(&script, &inputs).unwrap();
let last_state = trace.last_stack_state();

// first 8 elements of stack top holds sha256 digest, while remaining 8 elements
// are zeroed
let digest_on_stack = convert_to_stack(&digest_words);
assert_eq!(digest_on_stack, last_state);
let test = build_test!(source, &i_words);
// first 8 elements of stack top holds sha256 digest,
// while remaining 8 elements are zeroed
test.expect_stack(&digest_words);
}

// HELPER FUNCTIONS
// ================================================================================================

fn compile(source: &str) -> Script {
let assembler = assembly::Assembler::new();
assembler.compile_script(source).unwrap()
}

/// Takes an array of u64 values and builds a stack, perserving their order and converting them to
/// field elements.
fn convert_to_stack(values: &[u64]) -> [Felt; MIN_STACK_DEPTH] {
let mut result = [Felt::ZERO; MIN_STACK_DEPTH];
for (&value, result) in values.iter().zip(result.iter_mut()) {
*result = Felt::new(value);
}
result
}

/// Takes four consecutive big endian bytes and interprets them as a SHA256 word
fn from_be_bytes_to_words(be_bytes: &[u8]) -> u32 {
((be_bytes[0] as u32) << 24)
Expand Down

0 comments on commit d46ae2e

Please sign in to comment.