-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hashing): Improve support for already boxed values
- Loading branch information
1 parent
792635a
commit e242d54
Showing
4 changed files
with
133 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
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,4 @@ | ||
mod hash_atomic_values; | ||
mod hash_boxed_fields; | ||
mod hash_boxed_values; | ||
mod sample_scalars; |
95 changes: 95 additions & 0 deletions
95
src/tests_and_benchmarks/ozk/programs/algebraic_hasher/hash_boxed_fields.rs
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,95 @@ | ||
use tasm_lib::prelude::TasmObject; | ||
use tasm_lib::triton_vm::prelude::*; | ||
use twenty_first::prelude::AlgebraicHasher; | ||
|
||
use crate::tests_and_benchmarks::ozk::rust_shadows as tasm; | ||
|
||
const SIMPLE_STRUCTS_BFIELD_CODEC_START_ADDRESS: u64 = 84; | ||
|
||
#[derive(TasmObject, BFieldCodec)] | ||
struct OneDField { | ||
a: Vec<BFieldElement>, | ||
} | ||
|
||
fn one_dynamically_sized_field() { | ||
let test_struct: Box<OneDField> = | ||
OneDField::decode(&tasm::load_from_memory(BFieldElement::new(84))).unwrap(); | ||
let ts_digest: Digest = Tip5::hash(&test_struct.a); | ||
|
||
tasm::tasmlib_io_write_to_stdout___digest(ts_digest); | ||
|
||
return; | ||
} | ||
|
||
#[derive(TasmObject, BFieldCodec)] | ||
struct OneSField { | ||
a: Digest, | ||
} | ||
|
||
fn one_statically_sized_field() { | ||
let test_struct: Box<OneSField> = | ||
OneSField::decode(&tasm::load_from_memory(BFieldElement::new(84))).unwrap(); | ||
let ts_digest: Digest = Tip5::hash(&test_struct.a); | ||
tasm::tasmlib_io_write_to_stdout___digest(ts_digest); | ||
return; | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use rand::random; | ||
use tasm::wrap_main_with_io; | ||
use tasm_lib::triton_vm::prelude::*; | ||
use twenty_first::math::other::random_elements; | ||
|
||
use crate::tests_and_benchmarks::ozk::ozk_parsing::EntrypointLocation; | ||
use crate::tests_and_benchmarks::test_helpers::shared_test::init_memory_from; | ||
use crate::tests_and_benchmarks::test_helpers::shared_test::TritonVMTestCase; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn one_dynamically_sized_field_test() { | ||
let test_struct = OneDField { | ||
a: random_elements(2), | ||
}; | ||
let non_determinism = init_memory_from( | ||
&test_struct, | ||
BFieldElement::new(SIMPLE_STRUCTS_BFIELD_CODEC_START_ADDRESS), | ||
); | ||
let native_output = | ||
wrap_main_with_io(&one_dynamically_sized_field)(vec![], non_determinism.clone()); | ||
|
||
let entrypoint = EntrypointLocation::disk( | ||
"algebraic_hasher", | ||
"hash_boxed_fields", | ||
"one_dynamically_sized_field", | ||
); | ||
let vm_output = TritonVMTestCase::new(entrypoint.clone()) | ||
.with_non_determinism(non_determinism) | ||
.execute() | ||
.unwrap(); | ||
assert_eq!(native_output, vm_output.public_output); | ||
} | ||
|
||
#[test] | ||
fn one_statically_sized_field_test() { | ||
let test_struct = OneSField { a: random() }; | ||
let non_determinism = init_memory_from( | ||
&test_struct, | ||
BFieldElement::new(SIMPLE_STRUCTS_BFIELD_CODEC_START_ADDRESS), | ||
); | ||
let native_output = | ||
wrap_main_with_io(&one_statically_sized_field)(vec![], non_determinism.clone()); | ||
|
||
let entrypoint = EntrypointLocation::disk( | ||
"algebraic_hasher", | ||
"hash_boxed_fields", | ||
"one_statically_sized_field", | ||
); | ||
let vm_output = TritonVMTestCase::new(entrypoint.clone()) | ||
.with_non_determinism(non_determinism) | ||
.execute() | ||
.unwrap(); | ||
assert_eq!(native_output, vm_output.public_output); | ||
} | ||
} |