Skip to content

Commit

Permalink
use available method on Program to hash it
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Jul 5, 2023
1 parent cb0176b commit 59f2c10
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ documentation = "https://triton-vm.org/spec/"

[workspace.dependencies]
twenty-first = "0.30"
triton-opcodes = "0.30"
triton-opcodes = "0.31"
triton-profiler = "0.30"
itertools = "0.10"
syn = "2.0"
Expand Down
3 changes: 1 addition & 2 deletions triton-vm/benches/prove_fib_100.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use criterion::BenchmarkId;
use criterion::Criterion;
use triton_opcodes::program::Program;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::util_types::algebraic_hasher::AlgebraicHasher;

use triton_profiler::prof_start;
use triton_profiler::prof_stop;
Expand Down Expand Up @@ -42,7 +41,7 @@ fn prove_fib_100(criterion: &mut Criterion) {
let parameters = StarkParameters::default();
let claim = Claim {
input: public_input,
program_digest: StarkHasher::hash_varlen(&program.to_bwords()),
program_digest: program.hash::<StarkHasher>(),
output,
};
let proof = Stark::prove(&parameters, &claim, &aet, &mut maybe_profiler);
Expand Down
3 changes: 1 addition & 2 deletions triton-vm/benches/prove_halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use triton_profiler::prof_start;
use triton_profiler::prof_stop;
use triton_profiler::triton_profiler::Report;
use triton_profiler::triton_profiler::TritonProfiler;
use twenty_first::util_types::algebraic_hasher::AlgebraicHasher;

use triton_vm::proof::Claim;
use triton_vm::shared_tests::save_proof;
Expand Down Expand Up @@ -38,7 +37,7 @@ fn prove_halt(criterion: &mut Criterion) {
let parameters = StarkParameters::default();
let claim = Claim {
input: vec![],
program_digest: StarkHasher::hash_varlen(&program.to_bwords()),
program_digest: program.hash::<StarkHasher>(),
output,
};
let proof = Stark::prove(&parameters, &claim, &aet, &mut maybe_profiler);
Expand Down
5 changes: 2 additions & 3 deletions triton-vm/benches/verify_halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use criterion::BenchmarkId;
use criterion::Criterion;
use triton_opcodes::program::Program;
use triton_profiler::triton_profiler::TritonProfiler;
use twenty_first::shared_math::tip5::Tip5;
use twenty_first::util_types::algebraic_hasher::AlgebraicHasher;

use triton_vm::proof::Claim;
use triton_vm::shared_tests::load_proof;
use triton_vm::shared_tests::proof_file_exists;
use triton_vm::shared_tests::save_proof;
use triton_vm::stark::Stark;
use triton_vm::stark::StarkHasher;
use triton_vm::stark::StarkParameters;
use triton_vm::vm::simulate;

Expand All @@ -24,7 +23,7 @@ fn verify_halt(criterion: &mut Criterion) {
let parameters = StarkParameters::default();
let claim = Claim {
input: vec![],
program_digest: Tip5::hash_varlen(&program.to_bwords()),
program_digest: program.hash::<StarkHasher>(),
output: vec![],
};

Expand Down
11 changes: 5 additions & 6 deletions triton-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use anyhow::Result;
use triton_opcodes::program::Program;
pub use twenty_first::shared_math::b_field_element::BFieldElement;
pub use twenty_first::shared_math::tip5::Digest;
use twenty_first::shared_math::tip5::Tip5;
use twenty_first::util_types::algebraic_hasher::AlgebraicHasher;

pub use crate::proof::Claim;
pub use crate::proof::Proof;
use crate::stark::Stark;
use crate::stark::StarkHasher;
pub use crate::stark::StarkParameters;

pub mod arithmetic_domain;
Expand Down Expand Up @@ -83,7 +82,7 @@ pub fn prove_from_source(
let (aet, public_output) = vm::simulate(&program, public_input.clone(), secret_input)?;

// Hash the program to obtain its digest.
let program_digest = Tip5::hash_varlen(&program.to_bwords());
let program_digest = program.hash::<StarkHasher>();

// The default parameters give a (conjectured) security level of 160 bits.
let parameters = StarkParameters::default();
Expand All @@ -110,7 +109,7 @@ pub fn prove(
program: &Program,
secret_input: &[BFieldElement],
) -> Result<Proof> {
let program_digest = Tip5::hash_varlen(&program.to_bwords());
let program_digest = program.hash::<StarkHasher>();
if program_digest != claim.program_digest {
bail!("Program digest must match claimed program digest.");
}
Expand Down Expand Up @@ -176,7 +175,7 @@ mod public_interface_tests {
"Prover must return default STARK parameters"
);
let program = Program::from_code(source_code).unwrap();
let expected_program_digest = StarkHasher::hash_varlen(&program.to_bwords());
let expected_program_digest = program.hash::<StarkHasher>();
assert_eq!(
expected_program_digest, claim.program_digest,
"program digest must match program"
Expand All @@ -202,7 +201,7 @@ mod public_interface_tests {
let program = Program::from_code(source_code).unwrap();

let claim = Claim {
program_digest: StarkHasher::hash_varlen(&program.to_bwords()),
program_digest: program.hash::<StarkHasher>(),
input: vec![],
output: vec![],
};
Expand Down
3 changes: 1 addition & 2 deletions triton-vm/src/shared_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use anyhow::anyhow;
use anyhow::Result;
use triton_opcodes::program::Program;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::util_types::algebraic_hasher::AlgebraicHasher;

use triton_profiler::prof_start;
use triton_profiler::prof_stop;
Expand Down Expand Up @@ -53,7 +52,7 @@ pub fn parse_simulate_prove(

let claim = Claim {
input: public_input,
program_digest: StarkHasher::hash_varlen(&aet.program.to_bwords()),
program_digest: aet.program.hash::<StarkHasher>(),
output: public_output,
};

Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ pub(crate) mod triton_stark_tests {

let claim = Claim {
input: stdin,
program_digest: StarkHasher::hash_varlen(&aet.program.to_bwords()),
program_digest: aet.program.hash::<StarkHasher>(),
output: stdout,
};
let padded_height = MasterBaseTable::padded_height(&aet, parameters.num_trace_randomizers);
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table/processor_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,7 @@ impl ExtProcessorTable {
match instruction {
Pop => ExtProcessorTable::instruction_pop(circuit_builder),
Push(_) => ExtProcessorTable::instruction_push(circuit_builder),
Divine(_) => ExtProcessorTable::instruction_divine(circuit_builder),
Divine => ExtProcessorTable::instruction_divine(circuit_builder),
Dup(_) => ExtProcessorTable::instruction_dup(circuit_builder),
Swap(_) => ExtProcessorTable::instruction_swap(circuit_builder),
Nop => ExtProcessorTable::instruction_nop(circuit_builder),
Expand Down
7 changes: 3 additions & 4 deletions triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use twenty_first::shared_math::tip5::Tip5State;
use twenty_first::shared_math::tip5::DIGEST_LENGTH;
use twenty_first::shared_math::traits::Inverse;
use twenty_first::shared_math::x_field_element::XFieldElement;
use twenty_first::util_types::algebraic_hasher::AlgebraicHasher;
use twenty_first::util_types::algebraic_hasher::Domain;
use twenty_first::util_types::algebraic_hasher::SpongeHasher;

Expand Down Expand Up @@ -129,7 +128,7 @@ impl<'pgm> VMState<'pgm> {
public_input: Vec<BFieldElement>,
secret_input: Vec<BFieldElement>,
) -> Self {
let program_digest = StarkHasher::hash_varlen(&program.to_bwords());
let program_digest = program.hash::<StarkHasher>();

Self {
program: &program.instructions,
Expand Down Expand Up @@ -244,7 +243,7 @@ impl<'pgm> VMState<'pgm> {
self.instruction_pointer += 2;
}

Divine(_) => {
Divine => {
let elem = self.secret_input.pop_front().ok_or(anyhow!(
"Instruction `divine`: secret input buffer is empty."
))?;
Expand Down Expand Up @@ -1019,7 +1018,7 @@ impl AlgebraicExecutionTrace {
// consistency check
let program_digest = program_sponge.state[..DIGEST_LENGTH].try_into().unwrap();
let program_digest = Digest::new(program_digest);
let expected_digest = StarkHasher::hash_varlen(&self.program.to_bwords());
let expected_digest = self.program.hash::<StarkHasher>();
assert_eq!(expected_digest, program_digest);
}

Expand Down

0 comments on commit 59f2c10

Please sign in to comment.