Skip to content

Commit

Permalink
Fix new account tx's not able to verify (#458)
Browse files Browse the repository at this point in the history
* ProvenTransaction for new accounts uses Digest::default()

* Digest default if new account .hash() if old account

* Add proof_init_hash() to Account

* Change comment

* Improve comment
  • Loading branch information
phklive authored Feb 12, 2024
1 parent 48ccb32 commit b3dfd19
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
6 changes: 3 additions & 3 deletions miden-lib/src/transaction/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ToTransactionKernelInputs for PreparedTransaction {
let account = self.account();
let stack_inputs = TransactionKernel::build_input_stack(
account.id(),
if account.is_new() { None } else { Some(account.hash()) },
account.proof_init_hash(),
self.input_notes().commitment(),
self.block_header().hash(),
);
Expand All @@ -42,7 +42,7 @@ impl ToTransactionKernelInputs for ExecutedTransaction {
let account = self.initial_account();
let stack_inputs = TransactionKernel::build_input_stack(
account.id(),
if account.is_new() { None } else { Some(account.hash()) },
account.proof_init_hash(),
self.input_notes().commitment(),
self.block_header().hash(),
);
Expand All @@ -59,7 +59,7 @@ impl ToTransactionKernelInputs for TransactionWitness {
let account = self.account();
let stack_inputs = TransactionKernel::build_input_stack(
account.id(),
if account.is_new() { None } else { Some(account.hash()) },
account.proof_init_hash(),
self.input_notes().commitment(),
self.block_header().hash(),
);
Expand Down
4 changes: 2 additions & 2 deletions miden-lib/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ impl TransactionKernel {
/// tuples for the notes consumed by the transaction.
pub fn build_input_stack(
acct_id: AccountId,
init_acct_hash: Option<Digest>,
init_acct_hash: Digest,
input_notes_hash: Digest,
block_hash: Digest,
) -> StackInputs {
let mut inputs: Vec<Felt> = Vec::with_capacity(13);
inputs.extend(input_notes_hash);
inputs.extend_from_slice(init_acct_hash.unwrap_or_default().as_elements());
inputs.extend_from_slice(init_acct_hash.as_elements());
inputs.push(acct_id.into());
inputs.extend_from_slice(block_hash.as_elements());
StackInputs::new(inputs)
Expand Down
8 changes: 6 additions & 2 deletions miden-tx/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use miden_objects::{
};
use miden_prover::prove;
pub use miden_prover::ProvingOptions;
use vm_processor::MemAdviceProvider;
use vm_processor::{Digest, MemAdviceProvider};

use super::{TransactionHost, TransactionProverError};

Expand Down Expand Up @@ -64,7 +64,11 @@ impl TransactionProver {

Ok(ProvenTransaction::new(
account_id,
initial_account_hash,
if tx_witness.account().is_new() {
Digest::default()
} else {
initial_account_hash
},
tx_outputs.account.hash(),
input_notes,
tx_outputs.output_notes.into(),
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl TransactionVerifier {
// build stack inputs and outputs
let stack_inputs = TransactionKernel::build_input_stack(
transaction.account_id(),
Some(transaction.initial_account_hash()),
transaction.initial_account_hash(),
transaction.input_notes().commitment(),
transaction.block_ref(),
);
Expand Down
16 changes: 16 additions & 0 deletions objects/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ impl Account {
)
}

/// Returns hash of this account as used for the initial account state hash in transaction
/// proofs.
///
/// For existing accounts, this is exactly the same as [Account::hash()], however, for new
/// accounts this value is set to [ZERO; 4]. This is because when a transaction is executed
/// agains a new account, public input for the initial account state is set to [ZERO; 4] to
/// distinguish new accounts from existing accounts. The actual hash of the initial account
/// state (and the initial state itself), are provided to the VM via the advice provider.
pub fn proof_init_hash(&self) -> Digest {
if self.is_new() {
Digest::default()
} else {
self.hash()
}
}

/// Returns unique identifier of this account.
pub fn id(&self) -> AccountId {
self.id
Expand Down

0 comments on commit b3dfd19

Please sign in to comment.