Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tasm-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ itertools = "0"
ndarray = { version = "0.16" }
num = "0"
num-traits = "0"
rand = "0.9.0"
rand = "0.9.1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
strum = { version = "0.27", features = ["derive"] }
tasm-object-derive.workspace = true
triton-vm = { version = "0.48.0", default-features = false }

# revision "8b372a513c5b0c91578b62c902219818f5d76d2d" is tip of tip of PR branch asz/merkle-tree-index-types on 2025-05-06
twenty-first = { git = "https://github.com/Neptune-Crypto/twenty-first", rev = "8b372a513c5b0c91578b62c902219818f5d76d2d" }

[dev-dependencies.cargo-husky]
version = "1"
default-features = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "tasmlib_mmr_authentication_struct_derive_challenges",
"benchmark_result": {
"clock_cycle_count": 1118,
"hash_table_height": 955,
"u32_table_height": 30,
"op_stack_table_height": 777,
"ram_table_height": 1439
},
"case": "CommonCase"
},
{
"name": "tasmlib_mmr_authentication_struct_derive_challenges",
"benchmark_result": {
"clock_cycle_count": 2042,
"hash_table_height": 1879,
"u32_table_height": 26,
"op_stack_table_height": 1393,
"ram_table_height": 2974
},
"case": "WorstCase"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "tasmlib_mmr_root_from_authentication_struct",
"benchmark_result": {
"clock_cycle_count": 21481,
"hash_table_height": 1759,
"u32_table_height": 3034,
"op_stack_table_height": 33368,
"ram_table_height": 9732
},
"case": "CommonCase"
},
{
"name": "tasmlib_mmr_root_from_authentication_struct",
"benchmark_result": {
"clock_cycle_count": 25081,
"hash_table_height": 1975,
"u32_table_height": 1805,
"op_stack_table_height": 38936,
"ram_table_height": 11316
},
"case": "WorstCase"
}
]
2 changes: 1 addition & 1 deletion tasm-lib/src/arithmetic/bfe/primitive_root_of_unity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use triton_vm::prelude::twenty_first::math::traits::PrimitiveRootOfUnity as PRU;
use triton_vm::prelude::*;
use twenty_first::math::traits::PrimitiveRootOfUnity as PRU;

use crate::prelude::*;
use crate::traits::basic_snippet::Reviewer;
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/arithmetic/xfe/mod_pow_u32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl BasicSnippet for XfeModPowU32 {

#[cfg(test)]
pub mod tests {
use twenty_first::math::traits::ModPowU32;
use triton_vm::prelude::twenty_first::math::traits::ModPowU32;

use super::*;
use crate::empty_stack;
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/arithmetic/xfe/to_the_power_of_power_of_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl BasicSnippet for ToThePowerOfPowerOf2 {

#[cfg(test)]
mod tests {
use twenty_first::math::traits::ModPowU32;
use triton_vm::prelude::twenty_first::math::traits::ModPowU32;

use super::*;
use crate::arithmetic::xfe::mod_pow_u32::XfeModPowU32;
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/array/sum_of_xfes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use num::Zero;
use triton_vm::prelude::twenty_first::math::x_field_element::EXTENSION_DEGREE;
use triton_vm::prelude::*;
use twenty_first::math::x_field_element::EXTENSION_DEGREE;

use crate::data_type::ArrayType;
use crate::memory::load_words_from_memory_pop_pointer;
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/hashing/absorb_multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl BasicSnippet for AbsorbMultiple {
mod tests {
use std::collections::VecDeque;

use twenty_first::prelude::Sponge;
use triton_vm::prelude::twenty_first::prelude::Sponge;

use super::*;
use crate::empty_stack;
Expand Down
4 changes: 2 additions & 2 deletions tasm-lib/src/hashing/absorb_multiple_static_size.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use triton_vm::prelude::twenty_first::math::tip5::RATE;
use triton_vm::prelude::*;
use twenty_first::math::tip5::RATE;

use crate::memory::load_words_from_memory_pop_pointer;
use crate::prelude::*;
Expand Down Expand Up @@ -102,7 +102,7 @@ impl BasicSnippet for AbsorbMultipleStaticSize {

#[cfg(test)]
mod tests {
use twenty_first::prelude::Sponge;
use triton_vm::prelude::twenty_first::prelude::Sponge;

use super::*;
use crate::test_prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/hashing/algebraic_hasher/hash_static_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl BasicSnippet for HashStaticSize {

#[cfg(test)]
mod tests {
use twenty_first::prelude::*;
use triton_vm::prelude::twenty_first::prelude::*;

use super::*;
use crate::test_prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/hashing/algebraic_hasher/hash_varlen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl BasicSnippet for HashVarlen {
mod tests {
use std::collections::VecDeque;

use twenty_first::prelude::*;
use triton_vm::prelude::twenty_first::prelude::*;

use super::*;
use crate::empty_stack;
Expand Down
4 changes: 2 additions & 2 deletions tasm-lib/src/hashing/algebraic_hasher/sample_scalar_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl BasicSnippet for SampleScalarOne {

#[cfg(test)]
mod tests {
use twenty_first::math::x_field_element::EXTENSION_DEGREE;
use twenty_first::util_types::sponge::Sponge;
use triton_vm::prelude::twenty_first::math::x_field_element::EXTENSION_DEGREE;
use triton_vm::prelude::twenty_first::util_types::sponge::Sponge;

use super::*;
use crate::test_prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions tasm-lib/src/hashing/algebraic_hasher/sample_scalars.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use triton_vm::prelude::twenty_first::math::x_field_element::EXTENSION_DEGREE;
use triton_vm::prelude::*;
use twenty_first::math::x_field_element::EXTENSION_DEGREE;

use crate::hashing::squeeze_repeatedly::SqueezeRepeatedly;
use crate::list::new::New;
Expand Down Expand Up @@ -80,7 +80,7 @@ impl BasicSnippet for SampleScalars {

#[cfg(test)]
mod tests {
use twenty_first::prelude::*;
use triton_vm::prelude::twenty_first::prelude::*;

use super::*;
use crate::empty_stack;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use triton_vm::prelude::twenty_first::math::tip5::RATE;
use triton_vm::prelude::twenty_first::math::x_field_element::EXTENSION_DEGREE;
use triton_vm::prelude::*;
use twenty_first::math::tip5::RATE;
use twenty_first::math::x_field_element::EXTENSION_DEGREE;

use crate::data_type::ArrayType;
use crate::hashing::squeeze_repeatedly_static_number::SqueezeRepeatedlyStaticNumber;
Expand Down Expand Up @@ -79,7 +79,7 @@ impl BasicSnippet for SampleScalarsStaticLengthDynMalloc {

#[cfg(test)]
mod tests {
use twenty_first::prelude::*;
use triton_vm::prelude::twenty_first::prelude::*;

use super::*;
use crate::memory::dyn_malloc::DYN_MALLOC_FIRST_ADDRESS;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use triton_vm::prelude::twenty_first::math::tip5::RATE;
use triton_vm::prelude::twenty_first::math::x_field_element::EXTENSION_DEGREE;
use triton_vm::prelude::*;
use twenty_first::math::tip5::RATE;
use twenty_first::math::x_field_element::EXTENSION_DEGREE;

use crate::hashing::algebraic_hasher::sample_scalars_static_length_dyn_malloc::SampleScalarsStaticLengthDynMalloc;
use crate::hashing::squeeze_repeatedly_static_number::SqueezeRepeatedlyStaticNumber;
Expand Down Expand Up @@ -84,7 +84,7 @@ impl BasicSnippet for SampleScalarsStaticLengthKMalloc {
pub(crate) mod tests {
use std::ops::Neg;

use twenty_first::util_types::sponge::Sponge;
use triton_vm::prelude::twenty_first::util_types::sponge::Sponge;

use super::*;
use crate::rust_shadowing_helper_functions::array::array_get;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use triton_vm::prelude::twenty_first::math::tip5::RATE;
use triton_vm::prelude::twenty_first::math::x_field_element::EXTENSION_DEGREE;
use triton_vm::prelude::*;
use twenty_first::math::tip5::RATE;
use twenty_first::math::x_field_element::EXTENSION_DEGREE;

use crate::hashing::algebraic_hasher::sample_scalars_static_length_dyn_malloc::SampleScalarsStaticLengthDynMalloc;
use crate::hashing::squeeze_repeatedly_static_number::SqueezeRepeatedlyStaticNumber;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl BasicSnippet for SampleScalarsStaticLengthStaticPointer {

#[cfg(test)]
pub(crate) mod tests {
use twenty_first::util_types::sponge::Sponge;
use triton_vm::prelude::twenty_first::util_types::sponge::Sponge;

use super::*;
use crate::prelude::Tip5;
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/hashing/hash_from_stack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use triton_vm::prelude::twenty_first::prelude::*;
use triton_vm::prelude::*;
use twenty_first::prelude::*;

use crate::prelude::*;

Expand Down
26 changes: 19 additions & 7 deletions tasm-lib/src/hashing/merkle_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ impl BasicSnippet for MerkleRoot {

#[cfg(test)]
mod tests {
use ::twenty_first::util_types::merkle_tree::MerkleTree;
use proptest::collection::vec;
use twenty_first::util_types::merkle_tree::MerkleTree;

use super::*;
use crate::rust_shadowing_helper_functions::dyn_malloc::dynamic_allocator;
Expand Down Expand Up @@ -234,19 +234,31 @@ mod tests {
memory: &mut HashMap<BFieldElement, BFieldElement>,
) {
let leafs_pointer = stack.pop().unwrap();
let leafs = *Vec::decode_from_memory(memory, leafs_pointer).unwrap();
let mt = MerkleTree::par_new(&leafs).unwrap();
let leafs = *Vec::<Digest>::decode_from_memory(memory, leafs_pointer).unwrap();
let leafs_compatible = leafs
.iter()
.map(|d| {
d.values()
.map(|b| ::twenty_first::prelude::BFieldElement::new(b.value()))
})
.map(::twenty_first::prelude::Digest)
.collect_vec();
let mt = MerkleTree::par_new(&leafs_compatible).unwrap();

// mimic snippet: write internal nodes to memory, skipping (dummy) node 0
let tree_pointer = dynamic_allocator(memory);
let num_internal_nodes = leafs.len();

for (node_index, node) in (0..num_internal_nodes).zip(mt.nodes()).skip(1) {
let node_address = tree_pointer + bfe!(node_index * Digest::LEN);
encode_to_memory(memory, node_address, node);
for node_index in 1_u64..(num_internal_nodes as u64) {
let node = mt.node(node_index).unwrap();
let node_address = tree_pointer + bfe!(node_index * (Digest::LEN as u64));
let node_compatible = Digest(node.values().map(|b| BFieldElement::new(b.value())));
encode_to_memory(memory, node_address, &node_compatible);
}

stack.extend(mt.root().reversed().values());
let root = mt.root().reversed().values();
let root_compatible = root.map(|b| BFieldElement::new(b.value()));
stack.extend(root_compatible);
}

fn pseudorandom_initial_state(
Expand Down
40 changes: 30 additions & 10 deletions tasm-lib/src/hashing/merkle_root_from_xfes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use triton_vm::prelude::twenty_first::math::x_field_element::EXTENSION_DEGREE;
use triton_vm::prelude::*;
use twenty_first::math::x_field_element::EXTENSION_DEGREE;

use crate::hashing::merkle_root::MerkleRoot;
use crate::prelude::*;
Expand Down Expand Up @@ -209,8 +209,8 @@ impl BasicSnippet for MerkleRootFromXfes {

#[cfg(test)]
mod tests {
use ::twenty_first::prelude::MerkleTree;
use proptest::collection::vec;
use twenty_first::util_types::merkle_tree::MerkleTree;

use super::*;
use crate::rust_shadowing_helper_functions::dyn_malloc::dynamic_allocator;
Expand Down Expand Up @@ -244,33 +244,53 @@ mod tests {
let leafs_pointer = stack.pop().unwrap();
let leafs = *Vec::<XFieldElement>::decode_from_memory(memory, leafs_pointer).unwrap();
let leafs = leafs.into_iter().map(Digest::from).collect_vec();
let mt = MerkleTree::par_new(&leafs).unwrap();
let leafs_compatible = leafs
.iter()
.map(|d| {
d.values()
.map(|v| v.value())
.map(::twenty_first::prelude::BFieldElement::from)
})
.map(::twenty_first::prelude::Digest)
.collect_vec();
let mt = MerkleTree::par_new(&leafs_compatible).unwrap();

if leafs.len() == 1 {
stack.extend(mt.root().reversed().values());
let root = mt.root().reversed().values();
let root_compatible = root.map(|v| BFieldElement::new(v.value()));
stack.extend(root_compatible);
return;
}

// Write entire Merkle tree to memory, because that's what the VM does
let first_layer_pointer = dynamic_allocator(memory);
list_new(first_layer_pointer, memory);
for node_count in 0..(leafs.len() >> 1) {
let node_index = node_count + (1 << (mt.height() - 1));
let node_index = (node_count as u64) + (1 << (mt.height() - 1));
let node = mt.node(node_index).unwrap();
list_push(first_layer_pointer, node.values().to_vec(), memory)
let node_values = node.values().to_vec();
let node_values_compatible = node_values
.into_iter()
.map(|b| BFieldElement::new(b.value()))
.collect_vec();
list_push(first_layer_pointer, node_values_compatible, memory)
}

let rest_of_tree_pointer = dynamic_allocator(memory);
for layer in 2..=mt.height() {
for node_count in 0..(leafs.len() >> layer) {
let node_index = node_count + (1 << (mt.height() - layer));
let node_index = (node_count as u64) + (1 << (mt.height() - layer));
let node = mt.node(node_index).unwrap();
let pointer = rest_of_tree_pointer + bfe!(node_index * Digest::LEN);
encode_to_memory(memory, pointer, &node);
let pointer = rest_of_tree_pointer + bfe!(node_index * (Digest::LEN as u64));
let node_compatible =
Digest(node.values().map(|b| BFieldElement::new(b.value())));
encode_to_memory(memory, pointer, &node_compatible);
}
}

stack.extend(mt.root().reversed().values());
let root = mt.root().reversed().values();
let root_compatible = root.map(|b| BFieldElement::new(b.value()));
stack.extend(root_compatible);
}

fn pseudorandom_initial_state(
Expand Down
6 changes: 3 additions & 3 deletions tasm-lib/src/hashing/sponge_hasher/absorb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use triton_vm::prelude::twenty_first::math::tip5::RATE;
use triton_vm::prelude::*;
use twenty_first::math::tip5::RATE;

use crate::data_type::ArrayType;
use crate::prelude::*;
Expand Down Expand Up @@ -76,8 +76,8 @@ impl BasicSnippet for Absorb {
mod tests {
use arbitrary::Arbitrary;
use arbitrary::Unstructured;
use twenty_first::math::other::random_elements;
use twenty_first::prelude::Sponge;
use triton_vm::prelude::twenty_first::math::other::random_elements;
use triton_vm::prelude::twenty_first::prelude::Sponge;

use super::*;
use crate::empty_stack;
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/hashing/sponge_hasher/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl BasicSnippet for Init {
mod tests {
use arbitrary::Arbitrary;
use arbitrary::Unstructured;
use twenty_first::prelude::Sponge;
use triton_vm::prelude::twenty_first::prelude::Sponge;

use super::*;
use crate::empty_stack;
Expand Down
Loading
Loading