From d46ae2e7180ab789803282511ecd0ac07b0ed777 Mon Sep 17 00:00:00 2001 From: Anjan Roy Date: Tue, 15 Mar 2022 14:49:12 +0530 Subject: [PATCH] chg: as suggested in https://github.com/maticnetwork/miden/pull/123#discussion_r826596950, simplified test running using newly defined macro --- processor/src/tests/stdlib/crypto/blake3.rs | 36 ++++-------------- processor/src/tests/stdlib/crypto/mod.rs | 2 + processor/src/tests/stdlib/crypto/sha256.rs | 42 ++++++--------------- 3 files changed, 21 insertions(+), 59 deletions(-) diff --git a/processor/src/tests/stdlib/crypto/blake3.rs b/processor/src/tests/stdlib/crypto/blake3.rs index c799895893..4e0d08ca1c 100644 --- a/processor/src/tests/stdlib/crypto/blake3.rs +++ b/processor/src/tests/stdlib/crypto/blake3.rs @@ -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::().into_bytes(); @@ -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) diff --git a/processor/src/tests/stdlib/crypto/mod.rs b/processor/src/tests/stdlib/crypto/mod.rs index 850c840c77..c8ff213f0a 100644 --- a/processor/src/tests/stdlib/crypto/mod.rs +++ b/processor/src/tests/stdlib/crypto/mod.rs @@ -1,2 +1,4 @@ +use super::build_test; + mod blake3; mod sha256; diff --git a/processor/src/tests/stdlib/crypto/sha256.rs b/processor/src/tests/stdlib/crypto/sha256.rs index 30199eee9a..ff110b1f5a 100644 --- a/processor/src/tests/stdlib/crypto/sha256.rs +++ b/processor/src/tests/stdlib/crypto/sha256.rs @@ -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::().into_bytes(); @@ -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)