Skip to content

Commit

Permalink
refactor: Use wrapper for triton_vm::verify
Browse files Browse the repository at this point in the history
Replace calls to `triton_vm::verify` with the proof abstraction
wrapper. Make functions async as necessary.

Exceptions:
 - The call lives in a non-async function and it does not make sense
   to make it async, such as tests.
 - The call lives in an assert inside a non-async function.
  • Loading branch information
aszepieniec authored and Sword-Smith committed Jan 20, 2025
1 parent 85a79d5 commit e170c92
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 65 deletions.
17 changes: 10 additions & 7 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,15 +884,14 @@ pub(crate) mod mine_loop_tests {
use crate::models::blockchain::type_scripts::neptune_coins::NeptuneCoins;
use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::timestamp::Timestamp;
use crate::models::proof_abstractions::verifier::verify;
use crate::models::state::mempool::TransactionOrigin;
use crate::models::state::wallet::transaction_output::TxOutput;
use crate::models::state::wallet::utxo_notification::UtxoNotificationMedium;
use crate::tests::shared::dummy_expected_utxo;
use crate::tests::shared::make_mock_transaction_with_mutator_set_hash;
use crate::tests::shared::mock_genesis_global_state;
use crate::tests::shared::random_transaction_kernel;
use crate::triton_vm;
use crate::triton_vm::stark::Stark;
use crate::util_types::test_shared::mutator_set::pseudorandom_addition_record;
use crate::util_types::test_shared::mutator_set::random_mmra;
use crate::util_types::test_shared::mutator_set::random_mutator_set_accumulator;
Expand Down Expand Up @@ -1165,11 +1164,15 @@ pub(crate) mod mine_loop_tests {
let cb_txkmh = transaction_empty_mempool.kernel.mast_hash();
let cb_tx_claim = SingleProof::claim(cb_txkmh);
assert!(
triton_vm::verify(
Stark::default(),
&cb_tx_claim,
&transaction_empty_mempool.proof.clone().into_single_proof()
),
verify(
cb_tx_claim.clone(),
transaction_empty_mempool
.proof
.clone()
.into_single_proof()
.clone()
)
.await,
"Transaction proof for coinbase transaction must be valid."
);

Expand Down
12 changes: 6 additions & 6 deletions src/models/blockchain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::tasm::program::ConsensusProgram;
use crate::models::proof_abstractions::tasm::program::TritonVmProofJobOptions;
use crate::models::proof_abstractions::timestamp::Timestamp;
use crate::models::proof_abstractions::verifier::verify;
use crate::models::proof_abstractions::SecretWitness;
use crate::models::state::wallet::address::ReceivingAddress;
use crate::models::state::wallet::expected_utxo::ExpectedUtxo;
use crate::models::state::wallet::expected_utxo::UtxoNotifier;
use crate::models::state::wallet::WalletSecret;
use crate::prelude::twenty_first;
use crate::triton_vm;
use crate::util_types::mutator_set::addition_record::AdditionRecord;
use crate::util_types::mutator_set::commit;
use crate::util_types::mutator_set::mutator_set_accumulator::MutatorSetAccumulator;
Expand Down Expand Up @@ -283,11 +283,11 @@ impl Block {
) -> anyhow::Result<Block> {
let tx_claim = SingleProof::claim(transaction.kernel.mast_hash());
assert!(
triton_vm::verify(
Stark::default(),
&tx_claim,
&transaction.proof.clone().into_single_proof()
),
verify(
tx_claim.clone(),
transaction.proof.clone().into_single_proof().clone()
)
.await,
"Transaction proof must be valid to generate a block"
);
assert!(
Expand Down
8 changes: 2 additions & 6 deletions src/models/blockchain/block/validity/block_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use tasm_lib::memory::FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS;
use tasm_lib::prelude::DataType;
use tasm_lib::prelude::Digest;
use tasm_lib::prelude::Library;
use tasm_lib::triton_vm;
use tasm_lib::triton_vm::isa::triton_asm;
use tasm_lib::triton_vm::isa::triton_instr;
use tasm_lib::triton_vm::prelude::BFieldCodec;
Expand All @@ -21,7 +20,6 @@ use tasm_lib::triton_vm::proof::Claim;
use tasm_lib::triton_vm::proof::Proof;
use tasm_lib::triton_vm::stark::Stark;
use tasm_lib::verifier::stark_verify::StarkVerify;
use tokio::task;
use tracing::debug;

use super::block_proof_witness::BlockProofWitness;
Expand All @@ -35,6 +33,7 @@ use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::tasm::builtins as tasmlib;
use crate::models::proof_abstractions::tasm::builtins::verify_stark;
use crate::models::proof_abstractions::tasm::program::ConsensusProgram;
use crate::models::proof_abstractions::verifier::verify;

/// Verifies that all claims listed in the appendix are true.
///
Expand All @@ -60,10 +59,7 @@ impl BlockProgram {
let proof_clone = proof.clone();

debug!("** Calling triton_vm::verify to verify block proof ...");
let verdict =
task::spawn_blocking(move || triton_vm::verify(Stark::default(), &claim, &proof_clone))
.await
.expect("should be able to verify block proof in new tokio task");
let verdict = verify(claim, proof_clone).await;
debug!("** Call to triton_vm::verify to verify block proof completed; verdict: {verdict}.");

verdict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl BlockProofWitness {
}

fn with_claim(mut self, claim: Claim, proof: Proof) -> Self {
assert!(triton_vm::verify(Stark::default(), &claim, &proof));
debug_assert!(triton_vm::verify(Stark::default(), &claim, &proof));
self.claims.push(claim);
self.proofs.push(proof);

Expand Down
11 changes: 5 additions & 6 deletions src/models/blockchain/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ use serde::Deserialize;
use serde::Serialize;
use tasm_lib::prelude::Digest;
use tasm_lib::prelude::TasmObject;
use tasm_lib::triton_vm;
use tasm_lib::triton_vm::prelude::Tip5;
use tasm_lib::triton_vm::stark::Stark;
use tasm_lib::twenty_first::util_types::mmr::mmr_successor_proof::MmrSuccessorProof;
use tracing::info;
use twenty_first::math::b_field_element::BFieldElement;
Expand All @@ -48,6 +46,7 @@ use self::primitive_witness::PrimitiveWitness;
use self::transaction_kernel::TransactionKernel;
use self::transaction_kernel::TransactionKernelModifier;
use self::transaction_kernel::TransactionKernelProxy;
use crate::models::proof_abstractions::verifier::verify;
use crate::triton_vm::proof::Claim;
use crate::triton_vm::proof::Proof;
use crate::util_types::mutator_set::addition_record::AdditionRecord;
Expand Down Expand Up @@ -160,10 +159,10 @@ impl TransactionProof {
}
TransactionProof::SingleProof(single_proof) => {
let claim = SingleProof::claim(kernel_mast_hash);
triton_vm::verify(Stark::default(), &claim, single_proof)
verify(claim, single_proof.clone()).await
}
TransactionProof::ProofCollection(proof_collection) => {
proof_collection.verify(kernel_mast_hash)
proof_collection.verify(kernel_mast_hash).await
}
}
}
Expand Down Expand Up @@ -370,8 +369,8 @@ impl Transaction {
#[cfg(test)]
mod tests {
use tasm_lib::prelude::Digest;
use tasm_lib::triton_vm::prelude::Tip5;
use tests::primitive_witness::SaltedUtxos;
use triton_vm::prelude::Tip5;

use super::*;
use crate::models::blockchain::type_scripts::neptune_coins::NeptuneCoins;
Expand Down Expand Up @@ -511,9 +510,9 @@ mod transaction_tests {
use proptest::prelude::Strategy;
use proptest::test_runner::TestRunner;
use rand::random;
use tasm_lib::triton_vm::prelude::Tip5;
use tracing_test::traced_test;
use transaction_tests::utxo::Utxo;
use triton_vm::prelude::Tip5;

use super::*;
use crate::config_models::network::Network;
Expand Down
61 changes: 30 additions & 31 deletions src/models/blockchain/transaction/validity/proof_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use serde::Deserialize;
use serde::Serialize;
use tasm_lib::prelude::Digest;
use tasm_lib::structure::tasm_object::TasmObject;
use tasm_lib::triton_vm;
use tasm_lib::triton_vm::prelude::*;
use tasm_lib::triton_vm::proof::Claim;
use tasm_lib::triton_vm::stark::Stark;
use tracing::debug;
use tracing::info;
use tracing::trace;
Expand All @@ -28,6 +26,7 @@ use crate::models::blockchain::transaction::BFieldCodec;
use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::tasm::program::ConsensusProgram;
use crate::models::proof_abstractions::tasm::program::TritonVmProofJobOptions;
use crate::models::proof_abstractions::verifier::verify;
use crate::models::proof_abstractions::SecretWitness;
use crate::triton_vm::proof::Proof;

Expand Down Expand Up @@ -209,7 +208,7 @@ impl ProofCollection {
})
}

pub fn verify(&self, txk_mast_hash: Digest) -> bool {
pub(crate) async fn verify(&self, txk_mast_hash: Digest) -> bool {
debug!("verifying, txk hash: {}", txk_mast_hash);
debug!("verifying, salted inputs hash: {}", self.salted_inputs_hash);
debug!(
Expand Down Expand Up @@ -279,44 +278,44 @@ impl ProofCollection {

// verify
debug!("verifying removal records integrity ...");
let rri = triton_vm::verify(
Stark::default(),
&removal_records_integrity_claim,
&self.removal_records_integrity,
);
let rri = verify(
removal_records_integrity_claim.clone(),
self.removal_records_integrity.clone(),
)
.await;
debug!("{rri}");
debug!("verifying kernel to outputs ...");
let k2o = triton_vm::verify(
Stark::default(),
&kernel_to_outputs_claim,
&self.kernel_to_outputs,
);
let k2o = verify(
kernel_to_outputs_claim.clone(),
self.kernel_to_outputs.clone(),
)
.await;
debug!("{k2o}");
debug!("verifying collect lock scripts ...");
let cls = triton_vm::verify(
Stark::default(),
&collect_lock_scripts_claim,
&self.collect_lock_scripts,
);
let cls = verify(
collect_lock_scripts_claim.clone(),
self.collect_lock_scripts.clone(),
)
.await;
debug!("{cls}");
debug!("verifying collect type scripts ...");
let cts = triton_vm::verify(
Stark::default(),
&collect_type_scripts_claim,
&self.collect_type_scripts,
);
let cts = verify(
collect_type_scripts_claim.clone(),
self.collect_type_scripts.clone(),
)
.await;
debug!("{cts}");
debug!("verifying that all lock scripts halt ...");
let lsh = lock_script_claims
.iter()
.zip(self.lock_scripts_halt.iter())
.all(|(cl, pr)| triton_vm::verify(Stark::default(), cl, pr));
let mut lsh = true;
for (cl, pr) in lock_script_claims.iter().zip(self.lock_scripts_halt.iter()) {
lsh &= verify(cl.clone(), pr.clone()).await;
}
debug!("{lsh}");
debug!("verifying that all type scripts halt ...");
let tsh = type_script_claims
.iter()
.zip(self.type_scripts_halt.iter())
.all(|(cl, pr)| triton_vm::verify(Stark::default(), cl, pr));
let mut tsh = true;
for (cl, pr) in type_script_claims.iter().zip(self.type_scripts_halt.iter()) {
tsh &= verify(cl.clone(), pr.clone()).await;
}
debug!("{tsh}");

// and all bits together and return
Expand Down
4 changes: 2 additions & 2 deletions src/models/blockchain/transaction/validity/single_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ mod test {
)
.await
.unwrap();
assert!(proof_collection.verify(txk_mast_hash));
assert!(proof_collection.verify(txk_mast_hash).await);

let witness = SingleProofWitness::from_collection(proof_collection);
let claim = witness.claim();
Expand Down Expand Up @@ -898,7 +898,7 @@ mod test {
)
.await
.unwrap();
assert!(proof_collection.verify(txk_mast_hash));
assert!(proof_collection.verify(txk_mast_hash).await);

let witness = SingleProofWitness::from_collection(proof_collection);
let claim = witness.claim();
Expand Down
8 changes: 2 additions & 6 deletions src/models/blockchain/type_scripts/native_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,9 +1205,7 @@ pub mod test {
use proptest::strategy::ValueTree;
use proptest::test_runner::TestRunner;
use proptest_arbitrary_interop::arb;
use tasm_lib::triton_vm;
use tasm_lib::triton_vm::proof::Claim;
use tasm_lib::triton_vm::stark::Stark;
use test_strategy::proptest;

use super::*;
Expand All @@ -1224,6 +1222,7 @@ pub mod test {
use crate::models::proof_abstractions::tasm::program::test::consensus_program_negative_test;
use crate::models::proof_abstractions::tasm::program::ConsensusError;
use crate::models::proof_abstractions::timestamp::Timestamp;
use crate::models::proof_abstractions::verifier::verify;

fn assert_both_rust_and_tasm_halt_gracefully(
native_currency_witness: NativeCurrencyWitness,
Expand Down Expand Up @@ -1492,10 +1491,7 @@ pub mod test {
)
.await
.unwrap();
assert!(
triton_vm::verify(Stark::default(), &claim, &proof),
"proof fails"
);
assert!(verify(claim, proof).await, "proof fails");
}

#[proptest]
Expand Down

0 comments on commit e170c92

Please sign in to comment.