Skip to content

Commit

Permalink
chore(deps): upgrade dependency tasm-lib to use new SpongeHasher
Browse files Browse the repository at this point in the history
…trait

Co-authored-by: sword-smith <[email protected]>
  • Loading branch information
jan-ferdinand and Sword-Smith committed Jan 29, 2024
1 parent 38edc93 commit 0cc6e4f
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 52 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ itertools = "0.12"
num = "0.4"
rand = "0"
syn = { version = "1.0", features = ["full", "extra-traits"] }
tasm-lib = { git = "https://github.com/TritonVM/tasm-lib.git", branch = "sponge-hasher-trait" }
tasm-lib = { git = "https://github.com/TritonVM/tasm-lib.git", rev = "8d146b44" }
quote = "1.0"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ pub(crate) struct MethodCall<T> {
pub(crate) annot: T,

/// To type does this method belong? Not the same
/// same as receiver type, since receiver type can be
/// as receiver type, since receiver type can be
/// `&self` or `Box<Self>`.
pub(crate) associated_type: Option<DataType>,
}
5 changes: 3 additions & 2 deletions src/libraries/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use tasm_lib::triton_vm::prelude::*;
use tasm_lib::twenty_first::prelude::tip5::DIGEST_LENGTH;

use crate::ast;
use crate::ast::FnSignature;
use crate::ast_types::DataType;
Expand All @@ -12,8 +15,6 @@ use crate::libraries::Library;
use crate::subroutine::SubRoutine;
use crate::tasm_code_generator::CompilerState;
use crate::type_checker::CheckState;
use tasm_lib::triton_vm::prelude::*;
use tasm_lib::twenty_first::prelude::tip5::DIGEST_LENGTH;

use super::bfe::BfeLibrary;

Expand Down
62 changes: 27 additions & 35 deletions src/libraries/hasher/sponge_hasher.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use itertools::Itertools;
use num::One;
use tasm_lib::traits::basic_snippet::BasicSnippet;

use crate::ast;
use crate::ast::FnCall;
use crate::ast::UnaryOp;
use crate::ast_types::ArrayType;
use crate::ast_types::DataType;
use crate::graft::Graft;
use crate::libraries::Annotation;
use itertools::Itertools;
use num::One;
use tasm_lib::traits::basic_snippet::BasicSnippet;
use tasm_lib::twenty_first::shared_math::tip5::RATE;

pub(super) const SPONGE_HASHER_INDICATOR: &str = "Tip5::";
const SPONGE_HASHER_INIT_NAME: &str = "Tip5::init";
const SPONGE_HASHER_ABSORB_NAME: &str = "Tip5::absorb_once";
const SPONGE_HASHER_SQUEEZE_NAME: &str = "Tip5::squeeze_once";
const SPONGE_HASHER_ABSORB_NAME: &str = "Tip5::absorb";
const SPONGE_HASHER_SQUEEZE_NAME: &str = "Tip5::squeeze";
const SPONGE_HASHER_PAD_AND_ABSORB_ALL_NAME: &str = "Tip5::pad_and_absorb_all";

pub(super) fn graft_sponge_hasher_functions(
Expand All @@ -37,8 +36,7 @@ fn graft_init_function_call(
) -> ast::Expr<Annotation> {
assert!(
args.is_empty(),
"{SPONGE_HASHER_INIT_NAME} does not take any arguments. Got: args:\n{:?}",
args
"{SPONGE_HASHER_INIT_NAME} does not take any arguments. Got: args:\n{args:?}",
);

let tasm_lib_snippet = tasm_lib::hashing::sponge_hasher::init::Init;
Expand All @@ -55,22 +53,21 @@ fn graft_absorb_once_function_call(
grafter: &mut Graft,
args: &syn::punctuated::Punctuated<syn::Expr, syn::token::Comma>,
) -> ast::Expr<Annotation> {
// Ignore 1st argument, as it's the `SpongeState` in the source code, but from
// the perspective of the VM, this value is handled through its own co-processor.
// The 2nd argument has its reference stripped, as it's supposed to be an array,
// not a boxed array.
let args = args.iter().map(|x| grafter.graft_expr(x)).collect_vec();
let ast::Expr::Unary(UnaryOp::Ref(_mutable), arg, _) = &args[1] else {
panic!("Incorrect argument to {SPONGE_HASHER_ABSORB_NAME}. Got:\n{args:?}")
// From the perspective of the VM, the Sponge state is handled through its own co-processor.
let [_state, arg] = args.iter().collect_vec()[..] else {
panic!("{SPONGE_HASHER_ABSORB_NAME} expects exactly two arguments");
};
let arg = grafter.graft_expr(arg);

let tasm_lib_snippet = tasm_lib::hashing::sponge_hasher::absorb::Absorb;
let entrypoint = tasm_lib_snippet.entrypoint();

let tasm_lib_snippet = tasm_lib::hashing::sponge_hasher::absorb_once::AbsorbOnce;
ast::Expr::FnCall(FnCall {
name: format!("tasm::{}", tasm_lib_snippet.entrypoint()),
args: vec![*arg.to_owned()],
name: format!("tasm::{entrypoint}"),
args: vec![arg],
type_parameter: None,
arg_evaluation_order: Default::default(),
annot: crate::type_checker::Typing::KnownType(DataType::unit()),
annot: Default::default(),
})
}

Expand All @@ -79,42 +76,37 @@ fn graft_squeeze_once_function_call(
) -> ast::Expr<Annotation> {
// Ignore 1st argument, as it's the `SpongeState` in the source code, but from
// the perspective of the VM, this value is handled through its own co-processor.
assert![
assert!(
args.len().is_one(),
"{SPONGE_HASHER_SQUEEZE_NAME} expects exactly one argument"
];
);

let tasm_lib_snippet = tasm_lib::hashing::sponge_hasher::squeeze_once::SqueezeOnce;
let tasm_lib_snippet = tasm_lib::hashing::sponge_hasher::squeeze::Squeeze;
ast::Expr::FnCall(FnCall {
name: format!("tasm::{}", tasm_lib_snippet.entrypoint()),
args: vec![],
type_parameter: None,
arg_evaluation_order: Default::default(),
annot: crate::type_checker::Typing::KnownType(DataType::Array(ArrayType {
element_type: Box::new(DataType::Bfe),
length: RATE,
})),
annot: Default::default(),
})
}

fn graft_pad_and_absorb_all_function_call(
grafter: &mut Graft,
args: &syn::punctuated::Punctuated<syn::Expr, syn::token::Comma>,
) -> ast::Expr<Annotation> {
assert_eq!(
2,
args.len(),
"{SPONGE_HASHER_PAD_AND_ABSORB_ALL_NAME} expects exactly two arguments"
);
let [_state, arg] = args.iter().collect_vec()[..] else {
panic!("{SPONGE_HASHER_PAD_AND_ABSORB_ALL_NAME} expects exactly two arguments");
};

let tasm_lib_snippet = tasm_lib::hashing::sponge_hasher::pad_and_absorb_all::PadAndAbsorbAll {
list_type: grafter.list_type.into(),
};

// Ignore 1st argument, as it's the `SpongeState` in the source code, but from
// the perspective of the VM, this value is handled through its own co-processor.
let args = args.iter().map(|x| grafter.graft_expr(x)).collect_vec();
let ast::Expr::Unary(UnaryOp::Ref(_mutable), arg, _) = &args[1] else {
panic!("Incorrect argument to {SPONGE_HASHER_ABSORB_NAME}. Got:\n{args:?}")
let ast::Expr::Unary(UnaryOp::Ref(_mutable), arg, _) = grafter.graft_expr(arg) else {
panic!("Incorrect argument to {SPONGE_HASHER_PAD_AND_ABSORB_ALL_NAME}. Got:\n{args:?}");
};

ast::Expr::FnCall(FnCall {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fn absorb_and_squeeze() {
}
}

Tip5::absorb_once(&mut sponge, &preimage);
let squeezed: [BFieldElement; 10] = Tip5::squeeze_once(&mut sponge);
Tip5::absorb(&mut sponge, preimage);
let squeezed: [BFieldElement; 10] = Tip5::squeeze(&mut sponge);
{
let mut i: usize = 0;
while i < 10 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn initialized_sponge_behaves_correctly_deep_in_stack() {
let _j: u128 = 1000;
tasm::tasm_io_write_to_stdout___u128(b);
tasm::tasm_io_write_to_stdout___u128(a);
Tip5::absorb_once(&mut sponge, &preimage);
Tip5::absorb(&mut sponge, preimage);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn pad_and_absorb_all() {
}

Tip5::pad_and_absorb_all(&mut sponge, &preimage);
let produce: [BFieldElement; 10] = Tip5::squeeze_once(&mut sponge);
let produce: [BFieldElement; 10] = Tip5::squeeze(&mut sponge);
{
let mut i: usize = 0;
while i < 10 {
Expand Down
6 changes: 2 additions & 4 deletions src/tests_and_benchmarks/ozk/rust_shadows.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::triton_vm::twenty_first::shared_math::bfield_codec::BFieldCodec;
use crate::triton_vm::twenty_first::shared_math::x_field_element::XFieldElement;
use num::Zero;
use std::cell::RefCell;
use std::collections::HashMap;
use std::thread_local;
use std::vec::Vec;

use num::Zero;
use tasm_lib::triton_vm::prelude::*;
use tasm_lib::twenty_first::prelude::*;

// This module contains functions for interacting with the input/output monad
// implicit in a VM execution. It contains functions for mutating and verifying
Expand Down

0 comments on commit 0cc6e4f

Please sign in to comment.