diff --git a/artifacts/program_methods/amm.bin b/artifacts/program_methods/amm.bin index 3dd89048..42bbcb46 100644 Binary files a/artifacts/program_methods/amm.bin and b/artifacts/program_methods/amm.bin differ diff --git a/artifacts/program_methods/authenticated_transfer.bin b/artifacts/program_methods/authenticated_transfer.bin index 8a506394..e446efe4 100644 Binary files a/artifacts/program_methods/authenticated_transfer.bin and b/artifacts/program_methods/authenticated_transfer.bin differ diff --git a/artifacts/program_methods/pinata.bin b/artifacts/program_methods/pinata.bin index 741d2848..80d76dd5 100644 Binary files a/artifacts/program_methods/pinata.bin and b/artifacts/program_methods/pinata.bin differ diff --git a/artifacts/program_methods/pinata_token.bin b/artifacts/program_methods/pinata_token.bin index d48a764d..187ccf32 100644 Binary files a/artifacts/program_methods/pinata_token.bin and b/artifacts/program_methods/pinata_token.bin differ diff --git a/artifacts/program_methods/privacy_preserving_circuit.bin b/artifacts/program_methods/privacy_preserving_circuit.bin index 24396d4d..08c02325 100644 Binary files a/artifacts/program_methods/privacy_preserving_circuit.bin and b/artifacts/program_methods/privacy_preserving_circuit.bin differ diff --git a/artifacts/program_methods/token.bin b/artifacts/program_methods/token.bin index f67348f4..776d886d 100644 Binary files a/artifacts/program_methods/token.bin and b/artifacts/program_methods/token.bin differ diff --git a/artifacts/test_program_methods/burner.bin b/artifacts/test_program_methods/burner.bin index bbff49aa..eb178431 100644 Binary files a/artifacts/test_program_methods/burner.bin and b/artifacts/test_program_methods/burner.bin differ diff --git a/artifacts/test_program_methods/chain_caller.bin b/artifacts/test_program_methods/chain_caller.bin index 3495435a..3a427bf6 100644 Binary files a/artifacts/test_program_methods/chain_caller.bin and b/artifacts/test_program_methods/chain_caller.bin differ diff --git a/artifacts/test_program_methods/changer_claimer.bin b/artifacts/test_program_methods/changer_claimer.bin index 7c1cfb5b..f9842054 100644 Binary files a/artifacts/test_program_methods/changer_claimer.bin and b/artifacts/test_program_methods/changer_claimer.bin differ diff --git a/artifacts/test_program_methods/claimer.bin b/artifacts/test_program_methods/claimer.bin index dc622f89..93d4f57d 100644 Binary files a/artifacts/test_program_methods/claimer.bin and b/artifacts/test_program_methods/claimer.bin differ diff --git a/artifacts/test_program_methods/data_changer.bin b/artifacts/test_program_methods/data_changer.bin index 38ec9d78..db4ae8ac 100644 Binary files a/artifacts/test_program_methods/data_changer.bin and b/artifacts/test_program_methods/data_changer.bin differ diff --git a/artifacts/test_program_methods/extra_output.bin b/artifacts/test_program_methods/extra_output.bin index 3f36d446..247c8600 100644 Binary files a/artifacts/test_program_methods/extra_output.bin and b/artifacts/test_program_methods/extra_output.bin differ diff --git a/artifacts/test_program_methods/malicious_authorization_changer.bin b/artifacts/test_program_methods/malicious_authorization_changer.bin index 4ccb216c..43ec5f79 100644 Binary files a/artifacts/test_program_methods/malicious_authorization_changer.bin and b/artifacts/test_program_methods/malicious_authorization_changer.bin differ diff --git a/artifacts/test_program_methods/minter.bin b/artifacts/test_program_methods/minter.bin index 040a9dbf..014adb2c 100644 Binary files a/artifacts/test_program_methods/minter.bin and b/artifacts/test_program_methods/minter.bin differ diff --git a/artifacts/test_program_methods/missing_output.bin b/artifacts/test_program_methods/missing_output.bin index e04ab41d..f67d67a2 100644 Binary files a/artifacts/test_program_methods/missing_output.bin and b/artifacts/test_program_methods/missing_output.bin differ diff --git a/artifacts/test_program_methods/modified_transfer.bin b/artifacts/test_program_methods/modified_transfer.bin index 7871d07c..8d1ffa1f 100644 Binary files a/artifacts/test_program_methods/modified_transfer.bin and b/artifacts/test_program_methods/modified_transfer.bin differ diff --git a/artifacts/test_program_methods/nonce_changer.bin b/artifacts/test_program_methods/nonce_changer.bin index bf10cc24..83bc712b 100644 Binary files a/artifacts/test_program_methods/nonce_changer.bin and b/artifacts/test_program_methods/nonce_changer.bin differ diff --git a/artifacts/test_program_methods/noop.bin b/artifacts/test_program_methods/noop.bin index 71080ca3..addbd5f5 100644 Binary files a/artifacts/test_program_methods/noop.bin and b/artifacts/test_program_methods/noop.bin differ diff --git a/artifacts/test_program_methods/program_owner_changer.bin b/artifacts/test_program_methods/program_owner_changer.bin index 3d3f528a..6d4a53e2 100644 Binary files a/artifacts/test_program_methods/program_owner_changer.bin and b/artifacts/test_program_methods/program_owner_changer.bin differ diff --git a/artifacts/test_program_methods/simple_balance_transfer.bin b/artifacts/test_program_methods/simple_balance_transfer.bin index 74f82078..7290b984 100644 Binary files a/artifacts/test_program_methods/simple_balance_transfer.bin and b/artifacts/test_program_methods/simple_balance_transfer.bin differ diff --git a/integration_tests/src/config.rs b/integration_tests/src/config.rs index 136df85e..f23c9825 100644 --- a/integration_tests/src/config.rs +++ b/integration_tests/src/config.rs @@ -4,7 +4,10 @@ use anyhow::{Context, Result}; use indexer_service::{BackoffConfig, BedrockClientConfig, ChannelId, IndexerConfig}; use key_protocol::key_management::KeyChain; use nssa::{Account, AccountId, PrivateKey, PublicKey}; -use nssa_core::{account::{Data, Nonce}, program::DEFAULT_PROGRAM_ID}; +use nssa_core::{ + account::{Data, Nonce}, + program::DEFAULT_PROGRAM_ID, +}; use sequencer_core::config::{ AccountInitialData, BedrockConfig, CommitmentsInitialData, SequencerConfig, }; diff --git a/integration_tests/tests/tps.rs b/integration_tests/tests/tps.rs index ec2dfac0..5d829c34 100644 --- a/integration_tests/tests/tps.rs +++ b/integration_tests/tests/tps.rs @@ -15,7 +15,7 @@ use nssa::{ }; use nssa_core::{ MembershipProof, NullifierPublicKey, - account::{AccountWithMetadata, data::Data, Nonce}, + account::{AccountWithMetadata, Nonce, data::Data}, encryption::IncomingViewingPublicKey, }; use tokio::test; diff --git a/nssa/core/src/account.rs b/nssa/core/src/account.rs index 1c988a4a..2c7e0fde 100644 --- a/nssa/core/src/account.rs +++ b/nssa/core/src/account.rs @@ -3,7 +3,7 @@ use std::{fmt::Display, str::FromStr}; use base58::{FromBase58, ToBase58}; use borsh::{BorshDeserialize, BorshSerialize}; pub use data::Data; -use risc0_zkvm::{guest::sha::guest::Impl, sha::Sha256}; +use risc0_zkvm::sha::{Impl, Sha256}; use serde::{Deserialize, Serialize}; use serde_with::{DeserializeFromStr, SerializeDisplay}; @@ -11,7 +11,18 @@ use crate::{NullifierPublicKey, NullifierSecretKey, program::ProgramId}; pub mod data; -#[derive(Copy, Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize, BorshDeserialize, BorshSerialize)] +#[derive( + Copy, + Debug, + Default, + Clone, + Eq, + PartialEq, + Serialize, + Deserialize, + BorshDeserialize, + BorshSerialize, +)] pub struct Nonce(pub u128); impl Nonce { @@ -20,27 +31,25 @@ impl Nonce { } pub fn private_account_nonce_init(self, npk: &NullifierPublicKey) -> Nonce { - let mut bytes = Vec::new(); - bytes.extend_from_slice(&npk.to_byte_array()); - let bytes = Impl::hash_bytes(&bytes).as_bytes(); - let bytes = bytes.first_chunk::<16>().unwrap(); - - Nonce(u128::from_le_bytes(*bytes)) + let mut bytes: [u8; 64] = [0u8; 64]; + bytes[..32].copy_from_slice(&npk.0); + let result: [u8; 32] = Impl::hash_bytes(&bytes).as_bytes().try_into().unwrap(); + let result = result.first_chunk::<16>().unwrap(); + + Nonce(u128::from_le_bytes(*result)) } pub fn private_account_nonce_increment(self, nsk: &NullifierSecretKey) -> Nonce { - let mut bytes = Vec::new(); - bytes.extend_from_slice(nsk); - let bytes = Impl::hash_bytes(&bytes).as_bytes(); - let bytes = bytes.first_chunk::<16>().unwrap(); - - Nonce(u128::from_le_bytes(*bytes)) + let mut bytes: [u8; 64] = [0u8; 64]; + bytes[..32].copy_from_slice(nsk); + bytes[32..48].copy_from_slice(&self.0.to_le_bytes()); + let result: [u8; 32] = Impl::hash_bytes(&bytes).as_bytes().try_into().unwrap(); + let result = result.first_chunk::<16>().unwrap(); + + Nonce(u128::from_le_bytes(*result)) } - } - - /// Account to be used both in public and private contexts #[derive( Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize, BorshSerialize, BorshDeserialize, diff --git a/nssa/core/src/circuit_io.rs b/nssa/core/src/circuit_io.rs index 7f8efa6e..8893cd2d 100644 --- a/nssa/core/src/circuit_io.rs +++ b/nssa/core/src/circuit_io.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use crate::{ Commitment, CommitmentSetDigest, MembershipProof, Nullifier, NullifierPublicKey, NullifierSecretKey, SharedSecretKey, - account::{Account, AccountWithMetadata, Nonce}, + account::{Account, AccountWithMetadata}, encryption::Ciphertext, program::{ProgramId, ProgramOutput}, }; @@ -53,7 +53,7 @@ mod tests { use super::*; use crate::{ Commitment, Nullifier, NullifierPublicKey, - account::{Account, AccountId, AccountWithMetadata}, + account::{Account, AccountId, AccountWithMetadata, Nonce}, }; #[test] diff --git a/nssa/core/src/encoding.rs b/nssa/core/src/encoding.rs index 1b256e61..cb80a9a9 100644 --- a/nssa/core/src/encoding.rs +++ b/nssa/core/src/encoding.rs @@ -160,9 +160,8 @@ impl AccountId { #[cfg(test)] mod tests { - use crate::account::Nonce; - use super::*; + use crate::account::Nonce; #[test] fn test_enconding() { diff --git a/nssa/core/src/program.rs b/nssa/core/src/program.rs index 0c6ad50a..5b907d6e 100644 --- a/nssa/core/src/program.rs +++ b/nssa/core/src/program.rs @@ -327,9 +327,8 @@ impl WrappedBalanceSum { #[cfg(test)] mod tests { - use crate::account::Nonce; - use super::*; + use crate::account::Nonce; #[test] fn test_post_state_new_with_claim_constructor() { diff --git a/nssa/src/state.rs b/nssa/src/state.rs index 626c5d75..ef3b79ab 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -939,7 +939,6 @@ pub mod tests { sender_private_account: &Account, recipient_keys: &TestPrivateKeys, balance_to_move: u128, - new_nonces: [Nonce; 2], state: &V02State, ) -> PrivacyPreservingTransaction { let program = Program::authenticated_transfer_program(); @@ -992,7 +991,6 @@ pub mod tests { sender_private_account: &Account, recipient_account_id: &AccountId, balance_to_move: u128, - new_nonce: Nonce, state: &V02State, ) -> PrivacyPreservingTransaction { let program = Program::authenticated_transfer_program(); @@ -1077,10 +1075,11 @@ pub mod tests { #[test] fn test_transition_from_privacy_preserving_transaction_private() { let sender_keys = test_private_account_keys_1(); + let sender_nonce = Nonce(0xdeadbeef); let sender_private_account = Account { program_owner: Program::authenticated_transfer_program().id(), balance: 100, - nonce: Nonce(0xdeadbeef), + nonce: sender_nonce, data: Data::default(), }; let recipient_keys = test_private_account_keys_2(); @@ -1095,7 +1094,6 @@ pub mod tests { &sender_private_account, &recipient_keys, balance_to_move, - [Nonce(0xcafecafe), Nonce(0xfecafeca)], &state, ); @@ -1103,7 +1101,7 @@ pub mod tests { &sender_keys.npk(), &Account { program_owner: Program::authenticated_transfer_program().id(), - nonce: Nonce(0), //TODO update + nonce: sender_nonce.private_account_nonce_increment(&sender_keys.nsk), balance: sender_private_account.balance - balance_to_move, data: Data::default(), }, @@ -1117,7 +1115,7 @@ pub mod tests { &recipient_keys.npk(), &Account { program_owner: Program::authenticated_transfer_program().id(), - nonce: Nonce(0), + nonce: Nonce::default().private_account_nonce_init(&recipient_keys.npk()), balance: balance_to_move, ..Account::default() }, @@ -1143,6 +1141,7 @@ pub mod tests { #[test] fn test_transition_from_privacy_preserving_transaction_deshielded() { let sender_keys = test_private_account_keys_1(); + let sender_nonce = Nonce(0xdeadbeef); let sender_private_account = Account { program_owner: Program::authenticated_transfer_program().id(), balance: 100, @@ -1170,7 +1169,6 @@ pub mod tests { &sender_private_account, &recipient_keys.account_id(), balance_to_move, - Nonce(0xcafecafe), &state, ); @@ -1178,7 +1176,7 @@ pub mod tests { &sender_keys.npk(), &Account { program_owner: Program::authenticated_transfer_program().id(), - nonce: Nonce(0xcafecafe), + nonce: sender_nonce.private_account_nonce_increment(&sender_keys.nsk), balance: sender_private_account.balance - balance_to_move, data: Data::default(), }, @@ -1516,7 +1514,6 @@ pub mod tests { AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk()); // Setting only one nonce for an execution with two private accounts. - let private_account_nonces = [Nonce(0xdeadbeef1)]; let result = execute_and_prove( vec![private_account_1, private_account_2], Program::serialize_instruction(10u128).unwrap(), @@ -1980,7 +1977,6 @@ pub mod tests { // Setting three new private account nonces for a circuit execution with only two private // accounts. - let private_account_nonces = [Nonce(0xdeadbeef1), Nonce(0xdeadbeef2), Nonce(0xdeadbeef3)]; let result = execute_and_prove( vec![private_account_1, private_account_2], Program::serialize_instruction(10u128).unwrap(), @@ -2096,10 +2092,11 @@ pub mod tests { #[test] fn test_private_accounts_can_only_be_initialized_once() { let sender_keys = test_private_account_keys_1(); + let sender_nonce = Nonce(0xdeadbeef); let sender_private_account = Account { program_owner: Program::authenticated_transfer_program().id(), balance: 100, - nonce: Nonce(0xdeadbeef), + nonce: sender_nonce, data: Data::default(), }; let recipient_keys = test_private_account_keys_2(); @@ -2114,7 +2111,6 @@ pub mod tests { &sender_private_account, &recipient_keys, balance_to_move, - [Nonce(0xcafecafe), Nonce(0xfecafeca)], &state, ); @@ -2125,7 +2121,7 @@ pub mod tests { let sender_private_account = Account { program_owner: Program::authenticated_transfer_program().id(), balance: 100 - balance_to_move, - nonce: Nonce(0xcafecafe), + nonce: sender_nonce.private_account_nonce_increment(&sender_keys.nsk), data: Data::default(), }; @@ -2134,7 +2130,6 @@ pub mod tests { &sender_private_account, &recipient_keys, balance_to_move, - [Nonce(0x1234), Nonce(0x5678)], &state, ); @@ -2205,9 +2200,13 @@ pub mod tests { ..Account::default() }; - let message = - public_transaction::Message::try_new(program.id(), vec![from, to], vec![Nonce(0)], amount) - .unwrap(); + let message = public_transaction::Message::try_new( + program.id(), + vec![from, to], + vec![Nonce(0)], + amount, + ) + .unwrap(); let witness_set = public_transaction::WitnessSet::for_message(&message, &[&from_key]); let tx = PublicTransaction::new(message, witness_set); @@ -3793,8 +3792,8 @@ pub mod tests { dependencies.insert(auth_transfers.id(), auth_transfers); let program_with_deps = ProgramWithDependencies::new(chain_caller, dependencies); - let from_new_nonce = Nonce(0xdeadbeef1); - let to_new_nonce = Nonce(0xdeadbeef2); + let from_new_nonce = Nonce::default().private_account_nonce_increment(&from_keys.nsk); + let to_new_nonce = Nonce::default().private_account_nonce_increment(&to_keys.nsk); let from_expected_post = Account { balance: initial_balance - number_of_calls as u128 * amount, @@ -4046,8 +4045,6 @@ pub mod tests { // Balance to initialize the account with (0 for a new account) let balance: u128 = 0; - let nonce = Nonce(0xdeadbeef1); - // Execute and prove the circuit with the authorized account but no commitment proof let (output, proof) = execute_and_prove( vec![authorized_account], @@ -4098,7 +4095,6 @@ pub mod tests { let epk = EphemeralPublicKey::from_scalar(esk); let balance: u128 = 0; - let nonce = Nonce(0xdeadbeef1); // Step 2: Execute claimer program to claim the account with authentication let (output, proof) = execute_and_prove( @@ -4145,8 +4141,6 @@ pub mod tests { let esk2 = [4; 32]; let shared_secret2 = SharedSecretKey::new(&esk2, &private_keys.ivk()); - let nonce2 = Nonce(0xdeadbeef2); - // Step 3: Try to execute noop program with authentication but without initialization let res = execute_and_prove( vec![account_metadata], @@ -4299,8 +4293,6 @@ pub mod tests { dependencies.insert(auth_transfers.id(), auth_transfers); let program_with_deps = ProgramWithDependencies::new(malicious_program, dependencies); - let recipient_new_nonce = Nonce(0xdeadbeef1); - // Act - execute the malicious program - this should fail during proving let result = execute_and_prove( vec![sender_account, recipient_account], diff --git a/wallet/src/helperfunctions.rs b/wallet/src/helperfunctions.rs index afe9f27a..eda0bb2f 100644 --- a/wallet/src/helperfunctions.rs +++ b/wallet/src/helperfunctions.rs @@ -4,8 +4,6 @@ use anyhow::Result; use base64::{Engine, engine::general_purpose::STANDARD as BASE64}; use key_protocol::key_protocol_core::NSSAUserData; use nssa::Account; -use nssa_core::account::Nonce; -use rand::{RngCore, rngs::OsRng}; use serde::Serialize; use crate::{ diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index 30680ecd..7c144550 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -22,7 +22,7 @@ use tokio::io::AsyncWriteExt; use crate::{ config::{PersistentStorage, WalletConfigOverrides}, - helperfunctions::{produce_data_for_storage}, + helperfunctions::produce_data_for_storage, poller::TxPoller, }; diff --git a/wallet/src/program_facades/amm.rs b/wallet/src/program_facades/amm.rs index 7da5e86c..5b459c0f 100644 --- a/wallet/src/program_facades/amm.rs +++ b/wallet/src/program_facades/amm.rs @@ -80,7 +80,10 @@ impl Amm<'_> { let message = nssa::public_transaction::Message::try_new( program.id(), account_ids, - nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .unwrap(); @@ -187,7 +190,10 @@ impl Amm<'_> { let message = nssa::public_transaction::Message::try_new( program.id(), account_ids, - nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .unwrap(); @@ -274,7 +280,10 @@ impl Amm<'_> { let message = nssa::public_transaction::Message::try_new( program.id(), account_ids, - nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .unwrap(); @@ -356,7 +365,10 @@ impl Amm<'_> { let message = nssa::public_transaction::Message::try_new( program.id(), account_ids, - nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .unwrap(); diff --git a/wallet/src/program_facades/native_token_transfer/public.rs b/wallet/src/program_facades/native_token_transfer/public.rs index 5d921e49..34572fe0 100644 --- a/wallet/src/program_facades/native_token_transfer/public.rs +++ b/wallet/src/program_facades/native_token_transfer/public.rs @@ -25,9 +25,16 @@ impl NativeTokenTransfer<'_> { let account_ids = vec![from, to]; let program_id = Program::authenticated_transfer_program().id(); - let message = - Message::try_new(program_id, account_ids, - nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), balance_to_move).unwrap(); + let message = Message::try_new( + program_id, + account_ids, + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), + balance_to_move, + ) + .unwrap(); let signing_key = self.0.storage.user_data.get_pub_account_signing_key(from); @@ -56,7 +63,16 @@ impl NativeTokenTransfer<'_> { let instruction: u128 = 0; let account_ids = vec![from]; let program_id = Program::authenticated_transfer_program().id(); - let message = Message::try_new(program_id, account_ids, nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), instruction).unwrap(); + let message = Message::try_new( + program_id, + account_ids, + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), + instruction, + ) + .unwrap(); let signing_key = self.0.storage.user_data.get_pub_account_signing_key(from); diff --git a/wallet/src/program_facades/token.rs b/wallet/src/program_facades/token.rs index 8630d82f..7061e956 100644 --- a/wallet/src/program_facades/token.rs +++ b/wallet/src/program_facades/token.rs @@ -139,7 +139,10 @@ impl Token<'_> { let message = nssa::public_transaction::Message::try_new( program_id, account_ids, - nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .unwrap(); @@ -338,7 +341,10 @@ impl Token<'_> { let message = nssa::public_transaction::Message::try_new( Program::token().id(), account_ids, - nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .expect("Instruction should serialize"); @@ -470,7 +476,10 @@ impl Token<'_> { let message = nssa::public_transaction::Message::try_new( Program::token().id(), account_ids, - nonces.iter().map(|x|nssa_core::account::Nonce(*x)).collect(), + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .unwrap();