Skip to content

Commit

Permalink
fix(SingleProof): Bug in claim method
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Szepieniec <alan@neptune.cash>
  • Loading branch information
2 people authored and jan-ferdinand committed Nov 21, 2024
1 parent 8ce6c76 commit d2e556c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,9 @@ pub(crate) mod mine_loop_tests {
use crate::config_models::cli_args;
use crate::config_models::network::Network;
use crate::job_queue::triton_vm::TritonVmJobQueue;
use crate::models::blockchain::transaction::validity::single_proof::SingleProof;
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::state::mempool::TransactionOrigin;
use crate::models::state::wallet::transaction_output::TxOutput;
Expand All @@ -708,6 +710,8 @@ pub(crate) mod mine_loop_tests {
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::random_mmra;
use crate::util_types::test_shared::mutator_set::random_mutator_set_accumulator;
use crate::WalletSecret;
Expand Down Expand Up @@ -890,6 +894,17 @@ pub(crate) mod mine_loop_tests {
.unwrap()
};

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()
),
"Transaction proof for coinbase transaction must be valid."
);

assert_eq!(
1,
transaction_empty_mempool.kernel.outputs.len(),
Expand Down
11 changes: 11 additions & 0 deletions src/models/blockchain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use crate::job_queue::triton_vm::TritonVmJobQueue;
use crate::models::blockchain::block::difficulty_control::difficulty_control;
use crate::models::blockchain::shared::Hash;
use crate::models::blockchain::transaction::utxo::Coin;
use crate::models::blockchain::transaction::validity::single_proof::SingleProof;
use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::tasm::program::ConsensusProgram;
use crate::models::proof_abstractions::tasm::program::TritonVmProofJobOptions;
Expand All @@ -64,6 +65,7 @@ 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 @@ -227,6 +229,15 @@ impl Block {
triton_vm_job_queue: &TritonVmJobQueue,
proof_job_options: TritonVmProofJobOptions,
) -> 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()
),
"Transaction proof must be valid to generate a block"
);
let primitive_witness = BlockPrimitiveWitness::new(predecessor.to_owned(), transaction);
let body = primitive_witness.body().to_owned();
let header = Self::template_header(
Expand Down
9 changes: 8 additions & 1 deletion src/models/blockchain/transaction/validity/single_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub struct SingleProof;
impl SingleProof {
/// Not to be confused with SingleProofWitness::claim
pub(crate) fn claim(tx_kernel_mast_hash: Digest) -> Claim {
Claim::new(tx_kernel_mast_hash).with_input(tx_kernel_mast_hash.reversed().values())
Claim::about_program(&Self.program()).with_input(tx_kernel_mast_hash.reversed().values())
}

/// Generate a [SingleProof] for the transaction, given its primitive
Expand Down Expand Up @@ -919,6 +919,13 @@ mod test {
.expect("tasm run should pass");

assert_eq!(rust_result, tasm_result);

// Verify equivalence of claim functions
assert_eq!(
single_proof_witness.claim(),
SingleProof::claim(txk_mast_hash),
"Claim functions must agree"
);
}

#[tokio::test]
Expand Down

0 comments on commit d2e556c

Please sign in to comment.