tasm_lib/verifier/claim/
instantiate_fiat_shamir_with_claim.rsuse triton_vm::prelude::LabelledInstruction;
use triton_vm::prelude::*;
use triton_vm::twenty_first::math::tip5::Digest;
use crate::data_type::DataType;
use crate::hashing::absorb_multiple::AbsorbMultiple;
use crate::library::Library;
use crate::traits::basic_snippet::BasicSnippet;
use crate::verifier::claim::shared::claim_type;
pub struct InstantiateFiatShamirWithClaim;
impl BasicSnippet for InstantiateFiatShamirWithClaim {
fn inputs(&self) -> Vec<(DataType, String)> {
let claim_type = claim_type();
vec![(DataType::StructRef(claim_type), "*claim".to_owned())]
}
fn outputs(&self) -> Vec<(DataType, String)> {
vec![]
}
fn entrypoint(&self) -> String {
"tasmlib_verifier_claim_instantiate_fiat_shamir_with_claim".to_owned()
}
fn code(&self, library: &mut Library) -> Vec<LabelledInstruction> {
let entrypoint = self.entrypoint();
let calculate_claim_size = triton_asm!(
read_mem 1
dup 1
addi 2
add
read_mem 1
pop 1
addi {Digest::LEN + 1 + 2} add
);
let absorb_multiple = library.import(Box::new(AbsorbMultiple));
triton_asm!(
{entrypoint}:
dup 0
{&calculate_claim_size}
call {absorb_multiple}
return
)
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use rand::prelude::*;
use triton_vm::twenty_first::math::other::random_elements;
use triton_vm::twenty_first::util_types::algebraic_hasher::Sponge;
use super::*;
use crate::memory::encode_to_memory;
use crate::rust_shadowing_helper_functions::claim::load_claim_from_memory;
use crate::snippet_bencher::BenchmarkCase;
use crate::traits::procedure::Procedure;
use crate::traits::procedure::ProcedureInitialState;
impl Procedure for InstantiateFiatShamirWithClaim {
fn rust_shadow(
&self,
stack: &mut Vec<BFieldElement>,
memory: &mut std::collections::HashMap<BFieldElement, BFieldElement>,
_nondeterminism: &NonDeterminism,
_public_input: &[BFieldElement],
sponge: &mut Option<crate::VmHasher>,
) -> Vec<BFieldElement> {
let claim_pointer = stack.pop().unwrap();
let claim = load_claim_from_memory(claim_pointer, memory);
sponge.as_mut().unwrap().pad_and_absorb_all(&claim.encode());
vec![]
}
fn pseudorandom_initial_state(
&self,
seed: [u8; 32],
bench_case: Option<BenchmarkCase>,
) -> ProcedureInitialState {
let mut rng: StdRng = SeedableRng::from_seed(seed);
let (input_length, output_length) = match bench_case {
Some(BenchmarkCase::CommonCase) => (0, 0),
Some(BenchmarkCase::WorstCase) => (100, 100),
None => (rng.gen_range(0..1000), rng.gen_range(0..1000)),
};
let claim = Claim::new(rng.gen())
.with_input(random_elements(input_length))
.with_output(random_elements(output_length));
let mut memory = HashMap::default();
let claim_pointer = rng.gen();
encode_to_memory(&mut memory, claim_pointer, &claim);
let sponge: Tip5 = Tip5 { state: rng.gen() };
ProcedureInitialState {
stack: [self.init_stack_for_isolated_run(), vec![claim_pointer]].concat(),
nondeterminism: NonDeterminism::default().with_ram(memory),
public_input: vec![],
sponge: Some(sponge),
}
}
fn corner_case_initial_states(&self) -> Vec<ProcedureInitialState> {
let empty_everything = {
let minimal_claim = Claim::new(Digest::default());
let claim_pointer = thread_rng().gen();
let mut memory = HashMap::default();
encode_to_memory(&mut memory, claim_pointer, &minimal_claim);
ProcedureInitialState {
stack: [self.init_stack_for_isolated_run(), vec![claim_pointer]].concat(),
nondeterminism: NonDeterminism::default().with_ram(memory),
public_input: vec![],
sponge: Some(Tip5::init()),
}
};
vec![empty_everything]
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::traits::procedure::ShadowedProcedure;
use crate::traits::rust_shadow::RustShadow;
#[test]
fn test() {
ShadowedProcedure::new(InstantiateFiatShamirWithClaim).test();
}
}
#[cfg(test)]
mod benches {
use super::*;
use crate::traits::procedure::ShadowedProcedure;
use crate::traits::rust_shadow::RustShadow;
#[test]
fn benchmark() {
ShadowedProcedure::new(InstantiateFiatShamirWithClaim).bench();
}
}