Skip to content

Commit

Permalink
after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik1999 committed Nov 9, 2023
1 parent 86a448b commit 3704134
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 88 deletions.
7 changes: 5 additions & 2 deletions miden-lib/asm/eoa/basic.masm
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...]

Expand Down
20 changes: 7 additions & 13 deletions miden-tx/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -90,31 +86,29 @@ impl DataStore for MockDataStore {

// HELPER FUNCTIONS
// ================================================================================================
pub fn get_new_key_pair_with_advice_map() -> (KeyPair, ([Felt; 4], Vec<Felt>)) {
pub fn get_new_key_pair_with_advice_map() -> (Word, Vec<Felt>) {
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::<Vec<Felt>>();
let advice_map_tupel: ([Felt; 4], Vec<Felt>) = (pk, to_adv_map.into());
let pk_sk_felts: Vec<Felt> =
pk_sk_bytes.iter().map(|a| Felt::new(*a as u64)).collect::<Vec<Felt>>();

(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<Asset>,
) -> Account {
let account_code_src = DEFAULT_ACCOUNT_CODE;
let account_code_ast = ModuleAst::parse(account_code_src).unwrap();
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(),
Expand Down
37 changes: 14 additions & 23 deletions miden-tx/tests/test_miden_faucet_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// --------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand All @@ -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)
// --------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand All @@ -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),
);
Expand Down Expand Up @@ -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<u64>,
) -> Account {
Expand All @@ -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 =
Expand Down
90 changes: 53 additions & 37 deletions miden-tx/tests/test_miden_note_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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,
);

Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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,
);

Expand All @@ -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;
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 3704134

Please sign in to comment.