From 1056c38b490cd709b1b39cca148d304c455022c1 Mon Sep 17 00:00:00 2001 From: Martin Fraga Date: Tue, 9 Apr 2024 17:47:56 -0300 Subject: [PATCH] try replicating changes from first two commits from #529 --- Cargo.lock | 12 ++++++------ miden-lib/src/transaction/mod.rs | 4 ++-- miden-tx/src/prover/mod.rs | 10 +++------- objects/src/accounts/mod.rs | 10 ++++------ objects/src/transaction/proven_tx.rs | 13 +++++++------ objects/src/transaction/transaction_id.rs | 7 ++++--- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b837aa3a3..9fdb05940 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,9 +134,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.0.90" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" +checksum = "2678b2e3449475e95b0aa6f9b506a28e61b3dc8996592b983695e8ebb58a8b41" dependencies = [ "jobserver", "libc", @@ -402,9 +402,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.12" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" dependencies = [ "cfg-if", "libc", @@ -419,9 +419,9 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "half" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5eceaaeec696539ddaf7b333340f1af35a5aa87ae3e4f3ead0532f72affab2e" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" dependencies = [ "cfg-if", "crunchy", diff --git a/miden-lib/src/transaction/mod.rs b/miden-lib/src/transaction/mod.rs index 51420bca1..418a87eea 100644 --- a/miden-lib/src/transaction/mod.rs +++ b/miden-lib/src/transaction/mod.rs @@ -102,13 +102,13 @@ impl TransactionKernel { /// tuples for the notes consumed by the transaction. pub fn build_input_stack( acct_id: AccountId, - init_acct_hash: Digest, + init_acct_hash: Option, input_notes_hash: Digest, block_hash: Digest, ) -> StackInputs { let mut inputs: Vec = Vec::with_capacity(13); inputs.extend(input_notes_hash); - inputs.extend_from_slice(init_acct_hash.as_elements()); + inputs.extend_from_slice(init_acct_hash.unwrap_or_default().as_elements()); inputs.push(acct_id.into()); inputs.extend_from_slice(block_hash.as_elements()); StackInputs::new(inputs) diff --git a/miden-tx/src/prover/mod.rs b/miden-tx/src/prover/mod.rs index 57ce2f369..0fc00564e 100644 --- a/miden-tx/src/prover/mod.rs +++ b/miden-tx/src/prover/mod.rs @@ -1,3 +1,5 @@ +use core::ops::Not; + use miden_lib::transaction::{ToTransactionKernelInputs, TransactionKernel}; use miden_objects::{ notes::Nullifier, @@ -48,7 +50,7 @@ impl TransactionProver { let input_notes: InputNotes = (tx_witness.tx_inputs().input_notes()).into(); let account_id = tx_witness.account().id(); - let initial_account_hash = tx_witness.account().hash(); + let initial_hash = tx_witness.account().is_new().not().then(|| tx_witness.account().hash()); let block_hash = tx_witness.block_header().hash(); let tx_script_root = tx_witness.tx_args().tx_script().map(|script| *script.hash()); @@ -65,12 +67,6 @@ impl TransactionProver { TransactionKernel::from_transaction_parts(&stack_outputs, &map.into(), output_notes) .map_err(TransactionProverError::InvalidTransactionOutput)?; - let initial_hash = if tx_witness.account().is_new() { - Digest::default() - } else { - initial_account_hash - }; - let builder = ProvenTransactionBuilder::new( account_id, initial_hash, diff --git a/objects/src/accounts/mod.rs b/objects/src/accounts/mod.rs index a7bd4ae86..3c4acdea6 100644 --- a/objects/src/accounts/mod.rs +++ b/objects/src/accounts/mod.rs @@ -1,3 +1,5 @@ +use core::ops::Not; + use crate::{ assembly::{Assembler, AssemblyContext, ModuleAst}, assets::AssetVault, @@ -96,12 +98,8 @@ impl Account { /// against 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() - } + pub fn proof_init_hash(&self) -> Option { + self.is_new().not().then(|| self.hash()) } /// Returns unique identifier of this account. diff --git a/objects/src/transaction/proven_tx.rs b/objects/src/transaction/proven_tx.rs index b47cdd818..e5c1d36de 100644 --- a/objects/src/transaction/proven_tx.rs +++ b/objects/src/transaction/proven_tx.rs @@ -34,7 +34,7 @@ pub struct ProvenTransaction { /// The hash of the account before the transaction was executed. /// /// Set to `Digest::default()` for new accounts. - initial_account_hash: Digest, + initial_account_hash: Option, /// The hash of the account after the transaction was executed. final_account_hash: Digest, @@ -74,7 +74,7 @@ impl ProvenTransaction { } /// Returns the initial account state hash. - pub fn initial_account_hash(&self) -> Digest { + pub fn initial_account_hash(&self) -> Option { self.initial_account_hash } @@ -129,7 +129,7 @@ impl ProvenTransaction { )) }, Some(ref details) => { - let is_new_account = self.initial_account_hash == Digest::default(); + let is_new_account = self.initial_account_hash.is_none(); match (is_new_account, details) { (true, AccountDetails::Delta(_)) => { @@ -180,7 +180,7 @@ pub struct ProvenTransactionBuilder { account_id: AccountId, /// The hash of the account before the transaction was executed. - initial_account_hash: Digest, + initial_account_hash: Option, /// The hash of the account after the transaction was executed. final_account_hash: Digest, @@ -211,11 +211,12 @@ impl ProvenTransactionBuilder { /// Returns a [ProvenTransactionBuilder] used to build a [ProvenTransaction]. pub fn new( account_id: AccountId, - initial_account_hash: Digest, + initial_account_hash: Option, final_account_hash: Digest, block_ref: Digest, proof: ExecutionProof, ) -> Self { + debug_assert_ne!(initial_account_hash, Some(Digest::default())); Self { account_id, initial_account_hash, @@ -346,7 +347,7 @@ impl Serializable for ProvenTransaction { impl Deserializable for ProvenTransaction { fn read_from(source: &mut R) -> Result { let account_id = AccountId::read_from(source)?; - let initial_account_hash = Digest::read_from(source)?; + let initial_account_hash = >::read_from(source)?; let final_account_hash = Digest::read_from(source)?; let account_details = >::read_from(source)?; diff --git a/objects/src/transaction/transaction_id.rs b/objects/src/transaction/transaction_id.rs index 944f6268c..1f2307b50 100644 --- a/objects/src/transaction/transaction_id.rs +++ b/objects/src/transaction/transaction_id.rs @@ -24,13 +24,14 @@ pub struct TransactionId(Digest); impl TransactionId { /// Returns a new [TransactionId] instantiated from the provided transaction components. pub fn new( - init_account_hash: Digest, + init_account_hash: Option, final_account_hash: Digest, input_notes_hash: Digest, output_notes_hash: Digest, ) -> Self { + debug_assert_ne!(init_account_hash, Some(Digest::default())); let mut elements = [ZERO; 4 * WORD_SIZE]; - elements[..4].copy_from_slice(init_account_hash.as_elements()); + elements[..4].copy_from_slice(init_account_hash.unwrap_or_default().as_elements()); elements[4..8].copy_from_slice(final_account_hash.as_elements()); elements[8..12].copy_from_slice(input_notes_hash.as_elements()); elements[12..].copy_from_slice(output_notes_hash.as_elements()); @@ -89,7 +90,7 @@ impl From<&ExecutedTransaction> for TransactionId { let input_notes_hash = tx.input_notes().commitment(); let output_notes_hash = tx.output_notes().commitment(); Self::new( - tx.initial_account().hash(), + tx.initial_account().proof_init_hash(), tx.final_account().hash(), input_notes_hash, output_notes_hash,