From 37041346510b5c70a35dc6866caa275241085206 Mon Sep 17 00:00:00 2001 From: Dominik1999 Date: Thu, 9 Nov 2023 12:16:49 +0100 Subject: [PATCH] after review --- miden-lib/asm/eoa/basic.masm | 7 +- miden-tx/tests/common/mod.rs | 20 ++--- miden-tx/tests/test_miden_faucet_contract.rs | 37 +++----- miden-tx/tests/test_miden_note_scripts.rs | 90 ++++++++++++-------- miden-tx/tests/test_miden_wallet.rs | 23 +++-- 5 files changed, 89 insertions(+), 88 deletions(-) diff --git a/miden-lib/asm/eoa/basic.masm b/miden-lib/asm/eoa/basic.masm index aa3539b81..fefe40215 100644 --- a/miden-lib/asm/eoa/basic.masm +++ b/miden-lib/asm/eoa/basic.masm @@ -8,21 +8,24 @@ use.std::crypto::dsa::rpo_falcon512 # Slot in account storage at which the public key is stored. const.PUBLIC_KEY_SLOT=0 -#! Authenticate a transaction using Falcon +#! Authenticate a transaction using the Falcon signature scheme #! Stack: [] #! Output: [] #! export.auth_tx_rpo_falcon512 - # Get commitments to consumed and created notes, current nonce, and ID + # Get commitments to created notes exec.tx::get_output_notes_hash # => [OUTPUT_NOTES_HASH, ...] + # Get commitments to consumed notes exec.tx::get_input_notes_hash # => [INPUT_NOTES_HASH, OUTPUT_NOTES_HASH, ...] + # Get current nonce of the account and pad exec.account::get_nonce push.0.0.0 # => [0, 0, 0, nonce, INPUT_NOTES_HASH, OUTPUT_NOTES_HASH, ...] + # Get current AccountID and pad exec.account::get_id push.0.0.0 # => [0, 0, 0, account_id, 0, 0, 0, nonce, INPUT_NOTES_HASH, OUTPUT_NOTES_HASH, ...] diff --git a/miden-tx/tests/common/mod.rs b/miden-tx/tests/common/mod.rs index 7cc598955..a0ce9bc81 100644 --- a/miden-tx/tests/common/mod.rs +++ b/miden-tx/tests/common/mod.rs @@ -4,11 +4,7 @@ use miden_objects::{ assembly::ModuleAst, assembly::ProgramAst, assets::{Asset, FungibleAsset}, - crypto::{ - dsa::rpo_falcon512::{KeyPair, PublicKey}, - merkle::MerkleStore, - utils::Serializable, - }, + crypto::{dsa::rpo_falcon512::KeyPair, merkle::MerkleStore, utils::Serializable}, notes::{Note, NoteOrigin, NoteScript, RecordedNote}, BlockHeader, ChainMmr, Felt, StarkField, Word, }; @@ -90,21 +86,21 @@ impl DataStore for MockDataStore { // HELPER FUNCTIONS // ================================================================================================ -pub fn get_new_key_pair_with_advice_map() -> (KeyPair, ([Felt; 4], Vec)) { +pub fn get_new_key_pair_with_advice_map() -> (Word, Vec) { let keypair: KeyPair = KeyPair::new().unwrap(); let pk: Word = keypair.public_key().into(); let pk_sk_bytes = keypair.to_bytes(); - let to_adv_map = pk_sk_bytes.iter().map(|a| Felt::new(*a as u64)).collect::>(); - let advice_map_tupel: ([Felt; 4], Vec) = (pk, to_adv_map.into()); + let pk_sk_felts: Vec = + pk_sk_bytes.iter().map(|a| Felt::new(*a as u64)).collect::>(); - (keypair, advice_map_tupel) + (pk, pk_sk_felts) } #[allow(dead_code)] pub fn get_account_with_default_account_code( account_id: AccountId, - public_key: PublicKey, + public_key: Word, assets: Option, ) -> Account { let account_code_src = DEFAULT_ACCOUNT_CODE; @@ -112,9 +108,7 @@ pub fn get_account_with_default_account_code( let mut account_assembler = assembler(); let account_code = AccountCode::new(account_code_ast.clone(), &mut account_assembler).unwrap(); - - let pub_key_word: Word = public_key.into(); - let account_storage = AccountStorage::new(vec![(0, pub_key_word)], MerkleStore::new()).unwrap(); + let account_storage = AccountStorage::new(vec![(0, public_key)], MerkleStore::new()).unwrap(); let account_vault = match assets { Some(asset) => AccountVault::new(&vec![asset.into()]).unwrap(), diff --git a/miden-tx/tests/test_miden_faucet_contract.rs b/miden-tx/tests/test_miden_faucet_contract.rs index f3493165f..f9684be5a 100644 --- a/miden-tx/tests/test_miden_faucet_contract.rs +++ b/miden-tx/tests/test_miden_faucet_contract.rs @@ -21,12 +21,9 @@ use common::{ #[test] fn test_faucet_contract_mint_fungible_asset_succeeds() { - let (faucet_keypair, faucet_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); - let faucet_account = get_faucet_account_with_max_supply_and_total_issuance( - faucet_keypair.public_key().clone(), - 200, - None, - ); + let (faucet_pub_key, faucet_keypair_felts) = get_new_key_pair_with_advice_map(); + let faucet_account = + get_faucet_account_with_max_supply_and_total_issuance(faucet_pub_key, 200, None); // CONSTRUCT AND EXECUTE TX (Success) // -------------------------------------------------------------------------------------------- @@ -69,7 +66,7 @@ fn test_faucet_contract_mint_fungible_asset_succeeds() { ) .unwrap(); let tx_script = executor - .compile_tx_script(tx_script_code, vec![faucet_keypair_to_advice_map], vec![]) + .compile_tx_script(tx_script_code, vec![(faucet_pub_key, faucet_keypair_felts)], vec![]) .unwrap(); // Execute the transaction and get the witness @@ -95,12 +92,9 @@ fn test_faucet_contract_mint_fungible_asset_succeeds() { #[test] fn test_faucet_contract_mint_fungible_asset_fails_exceeds_max_supply() { - let (faucet_keypair, faucet_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); - let faucet_account = get_faucet_account_with_max_supply_and_total_issuance( - faucet_keypair.public_key().clone(), - 200, - None, - ); + let (faucet_pub_key, faucet_keypair_felts) = get_new_key_pair_with_advice_map(); + let faucet_account = + get_faucet_account_with_max_supply_and_total_issuance(faucet_pub_key.clone(), 200, None); // CONSTRUCT AND EXECUTE TX (Failure) // -------------------------------------------------------------------------------------------- @@ -143,7 +137,7 @@ fn test_faucet_contract_mint_fungible_asset_fails_exceeds_max_supply() { ) .unwrap(); let tx_script = executor - .compile_tx_script(tx_script_code, vec![faucet_keypair_to_advice_map], vec![]) + .compile_tx_script(tx_script_code, vec![(faucet_pub_key, faucet_keypair_felts)], vec![]) .unwrap(); // Execute the transaction and get the witness @@ -159,9 +153,9 @@ fn test_faucet_contract_mint_fungible_asset_fails_exceeds_max_supply() { #[test] fn test_faucet_contract_burn_fungible_asset_succeeds() { - let (faucet_keypair, _faucet_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); + let (faucet_pub_key, _faucet_keypair_felts) = get_new_key_pair_with_advice_map(); let faucet_account = get_faucet_account_with_max_supply_and_total_issuance( - faucet_keypair.public_key().clone(), + faucet_pub_key.clone(), 200, Some(100), ); @@ -265,7 +259,7 @@ fn test_faucet_contract_creation() { } fn get_faucet_account_with_max_supply_and_total_issuance( - public_key: PublicKey, + public_key: Word, max_supply: u64, total_issuance: Option, ) -> Account { @@ -277,13 +271,10 @@ fn get_faucet_account_with_max_supply_and_total_issuance( let faucet_account_code = AccountCode::new(faucet_account_code_ast.clone(), &mut account_assembler).unwrap(); - let pub_key_word: Word = public_key.into(); let faucet_storage_slot_1 = [Felt::new(max_supply), Felt::new(0), Felt::new(0), Felt::new(0)]; - let mut faucet_account_storage = AccountStorage::new( - vec![(0, pub_key_word), (1, faucet_storage_slot_1)], - MerkleStore::new(), - ) - .unwrap(); + let mut faucet_account_storage = + AccountStorage::new(vec![(0, public_key), (1, faucet_storage_slot_1)], MerkleStore::new()) + .unwrap(); if total_issuance.is_some() { let faucet_storage_slot_255 = diff --git a/miden-tx/tests/test_miden_note_scripts.rs b/miden-tx/tests/test_miden_note_scripts.rs index d5aa6af58..fe86d0065 100644 --- a/miden-tx/tests/test_miden_note_scripts.rs +++ b/miden-tx/tests/test_miden_note_scripts.rs @@ -33,12 +33,9 @@ fn test_p2id_script() { let target_account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN).unwrap(); - let (target_keypair, target_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); - let target_account = get_account_with_default_account_code( - target_account_id, - target_keypair.public_key().clone(), - None, - ); + let (target_pub_key, target_sk_pk_felt) = get_new_key_pair_with_advice_map(); + let target_account = + get_account_with_default_account_code(target_account_id, target_pub_key.clone(), None); // Create the note let p2id_script = Script::P2ID { @@ -79,7 +76,11 @@ fn test_p2id_script() { ) .unwrap(); let tx_script_target = executor - .compile_tx_script(tx_script_code.clone(), vec![target_keypair_to_advice_map], vec![]) + .compile_tx_script( + tx_script_code.clone(), + vec![(target_pub_key, target_sk_pk_felt)], + vec![], + ) .unwrap(); // Execute the transaction and get the witness @@ -103,10 +104,10 @@ fn test_p2id_script() { let malicious_account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN + 1).unwrap(); - let (malicious_keypair, malicious_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); + let (malicious_pub_key, malicious_keypair_felt) = get_new_key_pair_with_advice_map(); let malicious_account = get_account_with_default_account_code( malicious_account_id, - malicious_keypair.public_key().clone(), + malicious_pub_key.clone(), None, ); @@ -115,7 +116,11 @@ fn test_p2id_script() { let mut executor_2 = TransactionExecutor::new(data_store_malicious_account.clone()); executor_2.load_account(malicious_account_id).unwrap(); let tx_script_malicious = executor - .compile_tx_script(tx_script_code, vec![malicious_keypair_to_advice_map], vec![]) + .compile_tx_script( + tx_script_code, + vec![(malicious_pub_key, malicious_keypair_felt)], + vec![], + ) .unwrap(); let block_ref = data_store_malicious_account.block_header.block_num().as_int() as u32; @@ -153,12 +158,9 @@ fn test_p2id_script_multiple_assets() { let target_account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN).unwrap(); - let (target_keypair, target_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); - let target_account = get_account_with_default_account_code( - target_account_id, - target_keypair.public_key().clone(), - None, - ); + let (target_pub_key, target_keypair_felt) = get_new_key_pair_with_advice_map(); + let target_account = + get_account_with_default_account_code(target_account_id, target_pub_key.clone(), None); // Create the note let p2id_script = Script::P2ID { @@ -199,7 +201,11 @@ fn test_p2id_script_multiple_assets() { ) .unwrap(); let tx_script_target = executor - .compile_tx_script(tx_script_code.clone(), vec![target_keypair_to_advice_map], vec![]) + .compile_tx_script( + tx_script_code.clone(), + vec![(target_pub_key, target_keypair_felt)], + vec![], + ) .unwrap(); // Execute the transaction and get the witness @@ -223,10 +229,10 @@ fn test_p2id_script_multiple_assets() { let malicious_account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN + 1).unwrap(); - let (malicious_keypair, malicious_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); + let (malicious_pub_key, malicious_keypair_felt) = get_new_key_pair_with_advice_map(); let malicious_account = get_account_with_default_account_code( malicious_account_id, - malicious_keypair.public_key().clone(), + malicious_pub_key.clone(), None, ); @@ -235,7 +241,11 @@ fn test_p2id_script_multiple_assets() { let mut executor_2 = TransactionExecutor::new(data_store_malicious_account.clone()); executor_2.load_account(malicious_account_id).unwrap(); let tx_script_malicious = executor - .compile_tx_script(tx_script_code.clone(), vec![malicious_keypair_to_advice_map], vec![]) + .compile_tx_script( + tx_script_code.clone(), + vec![(malicious_pub_key, malicious_keypair_felt)], + vec![], + ) .unwrap(); let block_ref = data_store_malicious_account.block_header.block_num().as_int() as u32; @@ -271,30 +281,24 @@ fn test_p2idr_script() { // Create sender and target and malicious account let sender_account_id = AccountId::try_from(ACCOUNT_ID_SENDER).unwrap(); - let (sender_keypair, sender_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); - let sender_account = get_account_with_default_account_code( - sender_account_id, - sender_keypair.public_key().clone(), - None, - ); + let (sender_pub_key, sender_keypair_felt) = get_new_key_pair_with_advice_map(); + let sender_account = + get_account_with_default_account_code(sender_account_id, sender_pub_key.clone(), None); // Now create the target account let target_account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN).unwrap(); - let (target_keypair, target_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); - let target_account = get_account_with_default_account_code( - target_account_id, - target_keypair.public_key().clone(), - None, - ); + let (target_pub_key, target_keypair_felt) = get_new_key_pair_with_advice_map(); + let target_account = + get_account_with_default_account_code(target_account_id, target_pub_key.clone(), None); // Now create the malicious account let malicious_account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN + 1).unwrap(); - let (malicious_keypair, malicious_keypair_to_advice_map) = get_new_key_pair_with_advice_map(); + let (malicious_pub_key, malicious_keypair_felt) = get_new_key_pair_with_advice_map(); let malicious_account = get_account_with_default_account_code( malicious_account_id, - malicious_keypair.public_key().clone(), + malicious_pub_key.clone(), None, ); @@ -365,7 +369,11 @@ fn test_p2idr_script() { ) .unwrap(); let tx_script_target = executor_1 - .compile_tx_script(tx_script_code.clone(), vec![target_keypair_to_advice_map], vec![]) + .compile_tx_script( + tx_script_code.clone(), + vec![(target_pub_key, target_keypair_felt)], + vec![], + ) .unwrap(); // Execute the transaction and get the witness @@ -397,7 +405,11 @@ fn test_p2idr_script() { let mut executor_2 = TransactionExecutor::new(data_store_2.clone()); executor_2.load_account(sender_account_id).unwrap(); let tx_script_sender = executor_2 - .compile_tx_script(tx_script_code.clone(), vec![sender_keypair_to_advice_map], vec![]) + .compile_tx_script( + tx_script_code.clone(), + vec![(sender_pub_key, sender_keypair_felt)], + vec![], + ) .unwrap(); let block_ref_2 = data_store_2.block_header.block_num().as_int() as u32; @@ -425,7 +437,11 @@ fn test_p2idr_script() { let mut executor_3 = TransactionExecutor::new(data_store_3.clone()); executor_3.load_account(malicious_account_id).unwrap(); let tx_script_malicious = executor_3 - .compile_tx_script(tx_script_code, vec![malicious_keypair_to_advice_map], vec![]) + .compile_tx_script( + tx_script_code, + vec![(malicious_pub_key, malicious_keypair_felt)], + vec![], + ) .unwrap(); let block_ref_3 = data_store_3.block_header.block_num().as_int() as u32; diff --git a/miden-tx/tests/test_miden_wallet.rs b/miden-tx/tests/test_miden_wallet.rs index 2dbea1539..4a4ea278d 100644 --- a/miden-tx/tests/test_miden_wallet.rs +++ b/miden-tx/tests/test_miden_wallet.rs @@ -35,12 +35,9 @@ fn test_receive_asset_via_wallet() { let target_account_id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN).unwrap(); - let (keypair, keypair_to_advice_map) = get_new_key_pair_with_advice_map(); - let target_account = get_account_with_default_account_code( - target_account_id, - keypair.public_key().clone(), - None, - ); + let (target_pub_key, target_keypair_felt) = get_new_key_pair_with_advice_map(); + let target_account = + get_account_with_default_account_code(target_account_id, target_pub_key.clone(), None); // Create the note let note_script_ast = ProgramAst::parse( @@ -90,7 +87,7 @@ fn test_receive_asset_via_wallet() { ) .unwrap(); let tx_script = executor - .compile_tx_script(tx_script_code, vec![keypair_to_advice_map], vec![]) + .compile_tx_script(tx_script_code, vec![(target_pub_key, target_keypair_felt)], vec![]) .unwrap(); // Execute the transaction and get the witness @@ -103,7 +100,7 @@ fn test_receive_asset_via_wallet() { // clone account info let account_storage = - AccountStorage::new(vec![(0, keypair.public_key().into())], MerkleStore::new()).unwrap(); + AccountStorage::new(vec![(0, target_pub_key.into())], MerkleStore::new()).unwrap(); let account_code = target_account.code().clone(); // vault delta let target_account_after: Account = Account::new( @@ -126,10 +123,10 @@ fn test_send_asset_via_wallet() { let fungible_asset_1: Asset = FungibleAsset::new(faucet_id_1, 100).unwrap().into(); let sender_account_id = AccountId::try_from(ACCOUNT_ID_SENDER).unwrap(); - let (keypair, keypair_to_advice_map) = get_new_key_pair_with_advice_map(); + let (sender_pub_key, sender_keypair_felt) = get_new_key_pair_with_advice_map(); let sender_account = get_account_with_default_account_code( sender_account_id, - keypair.public_key().clone(), + sender_pub_key.clone(), fungible_asset_1.clone().into(), ); @@ -170,7 +167,7 @@ fn test_send_asset_via_wallet() { ) .unwrap(); let tx_script = executor - .compile_tx_script(tx_script_code, vec![keypair_to_advice_map], vec![]) + .compile_tx_script(tx_script_code, vec![(sender_pub_key, sender_keypair_felt)], vec![]) .unwrap(); // Execute the transaction and get the witness @@ -180,7 +177,7 @@ fn test_send_asset_via_wallet() { // clones account info let sender_account_storage = - AccountStorage::new(vec![(0, keypair.public_key().into())], MerkleStore::new()).unwrap(); + AccountStorage::new(vec![(0, sender_pub_key.into())], MerkleStore::new()).unwrap(); let sender_account_code = sender_account.code().clone(); // vault delta @@ -215,7 +212,7 @@ fn test_wallet_creation() { // sender_account_id not relevant here, just to create a default account code let sender_account_id = AccountId::try_from(ACCOUNT_ID_SENDER).unwrap(); let expected_code_root = - get_account_with_default_account_code(sender_account_id, pub_key, None) + get_account_with_default_account_code(sender_account_id, pub_key.into(), None) .code() .root();