diff --git a/Cargo.lock b/Cargo.lock index 6bb8255c..6faf3b1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8971,7 +8971,6 @@ dependencies = [ "nssa", "nssa_core", "optfield", - "rand 0.8.5", "serde", "serde_json", "sha2", diff --git a/artifacts/program_methods/amm.bin b/artifacts/program_methods/amm.bin index a8ca1f4e..d6e08b64 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 0bc3de67..5dc0bf97 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 64aaa346..cd2ffa53 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 a47d90f4..c6a2a8dd 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 8e151aa7..91dd81d5 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 9a1269c5..d047e652 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 b055fbdb..0c0f3089 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 c650f1f9..1abe0774 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 2dea3a0d..8c24294d 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 fb881bbc..674ca600 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 5a7a8600..bd5ea48a 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 fc8dc194..ab13c315 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 99dcd50f..ebe05ff6 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 7ea603a5..407142e1 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 3b2379c7..75c7d69e 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 48900c0c..e78597c9 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 b5ef1b9a..4e7c1f5e 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 c3b1d1d6..3384a65a 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 b697cc70..b88ade13 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 b54383e2..8ae149e0 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/common/src/test_utils.rs b/common/src/test_utils.rs index d5b6a437..720bd2f9 100644 --- a/common/src/test_utils.rs +++ b/common/src/test_utils.rs @@ -68,7 +68,7 @@ pub fn create_transaction_native_token_transfer( signing_key: &nssa::PrivateKey, ) -> NSSATransaction { let account_ids = vec![from, to]; - let nonces = vec![nonce]; + let nonces = vec![nonce.into()]; let program_id = nssa::program::Program::authenticated_transfer_program().id(); let message = nssa::public_transaction::Message::try_new( program_id, diff --git a/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs b/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs index 5e7df2d2..f38443ac 100644 --- a/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs +++ b/examples/program_deployment/src/bin/run_hello_world_with_authorization.rs @@ -3,6 +3,7 @@ use nssa::{ program::Program, public_transaction::{Message, WitnessSet}, }; +use nssa_core::account::Nonce; use wallet::WalletCore; // Before running this example, compile the `hello_world_with_authorization.rs` guest program with: @@ -62,7 +63,13 @@ async fn main() { .await .expect("Node should be reachable to query account data"); let signing_keys = [signing_key]; - let message = Message::try_new(program.id(), vec![account_id], nonces, greeting).unwrap(); + let message = Message::try_new( + program.id(), + vec![account_id], + nonces.iter().map(|x| Nonce(*x)).collect(), + greeting, + ) + .unwrap(); // Pass the signing key to sign the message. This will be used by the node // to flag the pre_state as `is_authorized` when executing the program let witness_set = WitnessSet::for_message(&message, &signing_keys); diff --git a/indexer/service/protocol/src/convert.rs b/indexer/service/protocol/src/convert.rs index cbafa0b3..499baa4c 100644 --- a/indexer/service/protocol/src/convert.rs +++ b/indexer/service/protocol/src/convert.rs @@ -1,5 +1,7 @@ //! Conversions between `indexer_service_protocol` types and `nssa/nssa_core` types. +use nssa_core::account::Nonce; + use crate::{ Account, AccountId, BedrockStatus, Block, BlockBody, BlockHeader, Ciphertext, Commitment, CommitmentSetDigest, Data, EncryptedAccountData, EphemeralPublicKey, HashType, MantleMsgId, @@ -52,7 +54,7 @@ impl From for Account { program_owner: program_owner.into(), balance, data: data.into(), - nonce, + nonce: nonce.0, } } } @@ -72,7 +74,7 @@ impl TryFrom for nssa_core::account::Account { program_owner: program_owner.into(), balance, data: data.try_into()?, - nonce, + nonce: Nonce(nonce), }) } } @@ -250,7 +252,7 @@ impl From for PublicMessage { Self { program_id: program_id.into(), account_ids: account_ids.into_iter().map(Into::into).collect(), - nonces, + nonces: nonces.iter().map(|x| x.0).collect(), instruction_data, } } @@ -267,7 +269,10 @@ impl From for nssa::public_transaction::Message { Self::new_preserialized( program_id.into(), account_ids.into_iter().map(Into::into).collect(), - nonces, + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction_data, ) } @@ -285,7 +290,7 @@ impl From for PrivacyPre } = value; Self { public_account_ids: public_account_ids.into_iter().map(Into::into).collect(), - nonces, + nonces: nonces.iter().map(|x| x.0).collect(), public_post_states: public_post_states.into_iter().map(Into::into).collect(), encrypted_private_post_states: encrypted_private_post_states .into_iter() @@ -314,7 +319,10 @@ impl TryFrom for nssa::privacy_preserving_transaction: } = value; Ok(Self { public_account_ids: public_account_ids.into_iter().map(Into::into).collect(), - nonces, + nonces: nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), public_post_states: public_post_states .into_iter() .map(TryInto::try_into) diff --git a/integration_tests/src/config.rs b/integration_tests/src/config.rs index 0ce55177..4d8539cc 100644 --- a/integration_tests/src/config.rs +++ b/integration_tests/src/config.rs @@ -86,7 +86,7 @@ impl InitialData { balance: 10_000, data: Data::default(), program_owner: DEFAULT_PROGRAM_ID, - nonce: 0, + nonce: 0_u128.into(), }, ), ( @@ -95,7 +95,7 @@ impl InitialData { balance: 20_000, data: Data::default(), program_owner: DEFAULT_PROGRAM_ID, - nonce: 0, + nonce: 0_u128.into(), }, ), ], diff --git a/integration_tests/tests/account.rs b/integration_tests/tests/account.rs index 02813b4c..3f1d0993 100644 --- a/integration_tests/tests/account.rs +++ b/integration_tests/tests/account.rs @@ -30,7 +30,7 @@ async fn get_existing_account() -> Result<()> { ); assert_eq!(account.balance, 10000); assert!(account.data.is_empty()); - assert_eq!(account.nonce, 0); + assert_eq!(account.nonce.0, 0); info!("Successfully retrieved account with correct details"); diff --git a/integration_tests/tests/auth_transfer/public.rs b/integration_tests/tests/auth_transfer/public.rs index 75ae2c12..ce73d62f 100644 --- a/integration_tests/tests/auth_transfer/public.rs +++ b/integration_tests/tests/auth_transfer/public.rs @@ -241,7 +241,7 @@ async fn initialize_public_account() -> Result<()> { Program::authenticated_transfer_program().id() ); assert_eq!(account.balance, 0); - assert_eq!(account.nonce, 1); + assert_eq!(account.nonce.0, 1); assert!(account.data.is_empty()); info!("Successfully initialized public account"); diff --git a/integration_tests/tests/program_deployment.rs b/integration_tests/tests/program_deployment.rs index 76837576..1feb7290 100644 --- a/integration_tests/tests/program_deployment.rs +++ b/integration_tests/tests/program_deployment.rs @@ -63,7 +63,7 @@ async fn deploy_and_execute_program() -> Result<()> { assert_eq!(post_state_account.program_owner, data_changer.id()); assert_eq!(post_state_account.balance, 0); assert_eq!(post_state_account.data.as_ref(), &[0]); - assert_eq!(post_state_account.nonce, 0); + assert_eq!(post_state_account.nonce.0, 0); info!("Successfully deployed and executed program"); diff --git a/integration_tests/tests/tps.rs b/integration_tests/tests/tps.rs index c89ee761..1dee3a85 100644 --- a/integration_tests/tests/tps.rs +++ b/integration_tests/tests/tps.rs @@ -27,7 +27,7 @@ use nssa::{ }; use nssa_core::{ MembershipProof, NullifierPublicKey, - account::{AccountWithMetadata, data::Data}, + account::{AccountWithMetadata, Nonce, data::Data}, encryption::ViewingPublicKey, }; use tokio::test; @@ -78,7 +78,7 @@ impl TpsTestManager { let message = putx::Message::try_new( program.id(), [pair[0].1, pair[1].1].to_vec(), - [0_u128].to_vec(), + [Nonce(0_u128)].to_vec(), amount, ) .unwrap(); @@ -107,7 +107,7 @@ impl TpsTestManager { let key_chain = KeyChain::new_os_random(); let account = Account { balance: 100, - nonce: 0xdead_beef, + nonce: Nonce(0xdead_beef), program_owner: Program::authenticated_transfer_program().id(), data: Data::default(), }; @@ -216,7 +216,7 @@ fn build_privacy_transaction() -> PrivacyPreservingTransaction { let sender_pre = AccountWithMetadata::new( Account { balance: 100, - nonce: 0xdead_beef, + nonce: Nonce(0xdead_beef), program_owner: program.id(), data: Data::default(), }, @@ -250,7 +250,6 @@ fn build_privacy_transaction() -> PrivacyPreservingTransaction { vec![sender_pre, recipient_pre], Program::serialize_instruction(balance_to_move).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ (sender_npk.clone(), sender_ss), (recipient_npk.clone(), recipient_ss), diff --git a/integration_tests/tests/wallet_ffi.rs b/integration_tests/tests/wallet_ffi.rs index 2170734c..a32e4d98 100644 --- a/integration_tests/tests/wallet_ffi.rs +++ b/integration_tests/tests/wallet_ffi.rs @@ -491,7 +491,7 @@ fn test_wallet_ffi_get_account_public() -> Result<()> { ); assert_eq!(account.balance, 10000); assert!(account.data.is_empty()); - assert_eq!(account.nonce, 0); + assert_eq!(account.nonce.0, 0); unsafe { wallet_ffi_free_account_data(&raw mut out_account); @@ -528,7 +528,7 @@ fn test_wallet_ffi_get_account_private() -> Result<()> { ); assert_eq!(account.balance, 10000); assert!(account.data.is_empty()); - assert_eq!(account.nonce, 0); + assert_eq!(account.nonce, 0_u128.into()); unsafe { wallet_ffi_free_account_data(&raw mut out_account); diff --git a/nssa/core/src/account.rs b/nssa/core/src/account.rs index 5eae68ef..0f9248e3 100644 --- a/nssa/core/src/account.rs +++ b/nssa/core/src/account.rs @@ -6,14 +6,89 @@ use std::{ use base58::{FromBase58 as _, ToBase58 as _}; use borsh::{BorshDeserialize, BorshSerialize}; pub use data::Data; +use risc0_zkvm::sha::{Impl, Sha256 as _}; use serde::{Deserialize, Serialize}; use serde_with::{DeserializeFromStr, SerializeDisplay}; -use crate::program::ProgramId; +use crate::{NullifierPublicKey, NullifierSecretKey, program::ProgramId}; pub mod data; -pub type Nonce = u128; +#[derive(Copy, Debug, Default, Clone, Eq, PartialEq)] +pub struct Nonce(pub u128); + +impl Nonce { + pub const fn public_account_nonce_increment(&mut self) { + self.0 = self + .0 + .checked_add(1) + .expect("Overflow when incrementing nonce"); + } + + #[must_use] + pub fn private_account_nonce_init(npk: &NullifierPublicKey) -> Self { + let mut bytes: [u8; 64] = [0_u8; 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(); + + Self(u128::from_le_bytes(*result)) + } + + #[must_use] + pub fn private_account_nonce_increment(self, nsk: &NullifierSecretKey) -> Self { + let mut bytes: [u8; 64] = [0_u8; 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(); + + Self(u128::from_le_bytes(*result)) + } +} + +impl From for Nonce { + fn from(value: u128) -> Self { + Self(value) + } +} + +impl From for u128 { + fn from(value: Nonce) -> Self { + value.0 + } +} + +impl Serialize for Nonce { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + Serialize::serialize(&self.0, serializer) + } +} + +impl<'de> Deserialize<'de> for Nonce { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + Ok(::deserialize(deserializer)?.into()) + } +} + +impl BorshSerialize for Nonce { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { + BorshSerialize::serialize(&self.0, writer) + } +} + +impl BorshDeserialize for Nonce { + fn deserialize_reader(reader: &mut R) -> std::io::Result { + Ok(::deserialize_reader(reader)?.into()) + } +} + pub type Balance = u128; /// Account to be used both in public and private contexts. @@ -154,7 +229,7 @@ mod tests { fn zero_nonce_account_data_creation() { let new_acc = Account::default(); - assert_eq!(new_acc.nonce, 0); + assert_eq!(new_acc.nonce.0, 0); } #[test] @@ -181,7 +256,7 @@ mod tests { .to_vec() .try_into() .unwrap(), - nonce: 0xdead_beef, + nonce: Nonce(0xdead_beef), }; let fingerprint = AccountId::new([8; 32]); let new_acc_with_metadata = AccountWithMetadata::new(account.clone(), true, fingerprint); @@ -228,4 +303,52 @@ mod tests { let expected_account_id = AccountId::new([0; 32]); assert!(default_account_id == expected_account_id); } + + #[test] + fn initialize_private_nonce() { + let npk = NullifierPublicKey([42; 32]); + let nonce = Nonce::private_account_nonce_init(&npk); + let expected_nonce = Nonce(37_937_661_125_547_691_021_612_781_941_709_513_486); + assert_eq!(nonce, expected_nonce); + } + + #[test] + fn increment_private_nonce() { + let nsk: NullifierSecretKey = [42_u8; 32]; + let nonce = Nonce(37_937_661_125_547_691_021_612_781_941_709_513_486) + .private_account_nonce_increment(&nsk); + let expected_nonce = Nonce(327_300_903_218_789_900_388_409_116_014_290_259_894); + assert_eq!(nonce, expected_nonce); + } + + #[test] + fn increment_public_nonce() { + let value = 42_u128; + let mut nonce = Nonce(value); + nonce.public_account_nonce_increment(); + let expected_nonce = Nonce(value + 1); + assert_eq!(nonce, expected_nonce); + } + + #[test] + fn serde_roundtrip_for_nonce() { + let nonce: Nonce = 7_u128.into(); + + let serde_serialized_nonce = serde_json::to_vec(&nonce).unwrap(); + + let nonce_restored = serde_json::from_slice(&serde_serialized_nonce).unwrap(); + + assert_eq!(nonce, nonce_restored); + } + + #[test] + fn borsh_roundtrip_for_nonce() { + let nonce: Nonce = 7_u128.into(); + + let borsh_serialized_nonce = borsh::to_vec(&nonce).unwrap(); + + let nonce_restored = borsh::from_slice(&borsh_serialized_nonce).unwrap(); + + assert_eq!(nonce, nonce_restored); + } } diff --git a/nssa/core/src/circuit_io.rs b/nssa/core/src/circuit_io.rs index e4ff6e2d..56d63022 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}, }; @@ -18,8 +18,6 @@ pub struct PrivacyPreservingCircuitInput { /// - `1` - private account with authentication /// - `2` - private account without authentication pub visibility_mask: Vec, - /// Nonces of private accounts. - pub private_account_nonces: Vec, /// Public keys of private accounts. pub private_account_keys: Vec<(NullifierPublicKey, SharedSecretKey)>, /// Nullifier secret keys for authorized private accounts. @@ -57,7 +55,7 @@ mod tests { use super::*; use crate::{ Commitment, Nullifier, NullifierPublicKey, - account::{Account, AccountId, AccountWithMetadata}, + account::{Account, AccountId, AccountWithMetadata, Nonce}, }; #[test] @@ -69,7 +67,7 @@ mod tests { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], balance: 12_345_678_901_234_567_890, data: b"test data".to_vec().try_into().unwrap(), - nonce: 0xFFFF_FFFF_FFFF_FFFE, + nonce: Nonce(0xFFFF_FFFF_FFFF_FFFE), }, true, AccountId::new([0; 32]), @@ -79,7 +77,7 @@ mod tests { program_owner: [9, 9, 9, 8, 8, 8, 7, 7], balance: 123_123_123_456_456_567_112, data: b"test data".to_vec().try_into().unwrap(), - nonce: 9_999_999_999_999_999_999_999, + nonce: Nonce(9_999_999_999_999_999_999_999), }, false, AccountId::new([1; 32]), @@ -89,7 +87,7 @@ mod tests { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], balance: 100, data: b"post state data".to_vec().try_into().unwrap(), - nonce: 0xFFFF_FFFF_FFFF_FFFF, + nonce: Nonce(0xFFFF_FFFF_FFFF_FFFF), }], ciphertexts: vec![Ciphertext(vec![255, 255, 1, 1, 2, 2])], new_commitments: vec![Commitment::new( diff --git a/nssa/core/src/commitment.rs b/nssa/core/src/commitment.rs index c9fcfce0..36730dd0 100644 --- a/nssa/core/src/commitment.rs +++ b/nssa/core/src/commitment.rs @@ -61,7 +61,7 @@ impl Commitment { this.extend_from_slice(&word.to_le_bytes()); } this.extend_from_slice(&account.balance.to_le_bytes()); - this.extend_from_slice(&account.nonce.to_le_bytes()); + this.extend_from_slice(&account.nonce.0.to_le_bytes()); let hashed_data: [u8; 32] = Impl::hash_bytes(&account.data) .as_bytes() .try_into() diff --git a/nssa/core/src/encoding.rs b/nssa/core/src/encoding.rs index 6abd4c11..ac9317c2 100644 --- a/nssa/core/src/encoding.rs +++ b/nssa/core/src/encoding.rs @@ -25,8 +25,8 @@ impl Account { bytes.extend_from_slice(&word.to_le_bytes()); } bytes.extend_from_slice(&self.balance.to_le_bytes()); - bytes.extend_from_slice(&self.nonce.to_le_bytes()); - let data_length: u32 = u32::try_from(self.data.len()).expect("data length fits in u32"); + bytes.extend_from_slice(&self.nonce.0.to_le_bytes()); + let data_length: u32 = u32::try_from(self.data.len()).expect("Invalid u32"); bytes.extend_from_slice(&data_length.to_le_bytes()); bytes.extend_from_slice(self.data.as_ref()); bytes @@ -35,7 +35,7 @@ impl Account { /// Deserializes an account from a cursor. #[cfg(feature = "host")] pub fn from_cursor(cursor: &mut Cursor<&[u8]>) -> Result { - use crate::account::data::Data; + use crate::account::{Nonce, data::Data}; let mut u32_bytes = [0_u8; 4]; let mut u128_bytes = [0_u8; 16]; @@ -53,7 +53,7 @@ impl Account { // nonce cursor.read_exact(&mut u128_bytes)?; - let nonce = u128::from_le_bytes(u128_bytes); + let nonce = Nonce(u128::from_le_bytes(u128_bytes)); // data let data = Data::from_cursor(cursor)?; @@ -189,7 +189,7 @@ mod tests { let account = Account { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], balance: 123_456_789_012_345_678_901_234_567_890_123_456, - nonce: 42, + nonce: 42_u128.into(), data: b"hola mundo".to_vec().try_into().unwrap(), }; @@ -250,7 +250,7 @@ mod tests { let account = Account { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], balance: 123_456_789_012_345_678_901_234_567_890_123_456, - nonce: 42, + nonce: 42_u128.into(), data: b"hola mundo".to_vec().try_into().unwrap(), }; let bytes = account.to_bytes(); diff --git a/nssa/core/src/program.rs b/nssa/core/src/program.rs index c67a816c..31b76b0f 100644 --- a/nssa/core/src/program.rs +++ b/nssa/core/src/program.rs @@ -348,7 +348,7 @@ mod tests { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], balance: 1337, data: vec![0xde, 0xad, 0xbe, 0xef].try_into().unwrap(), - nonce: 10, + nonce: 10_u128.into(), }; let account_post_state = AccountPostState::new_claimed(account.clone()); @@ -363,7 +363,7 @@ mod tests { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], balance: 1337, data: vec![0xde, 0xad, 0xbe, 0xef].try_into().unwrap(), - nonce: 10, + nonce: 10_u128.into(), }; let account_post_state = AccountPostState::new(account.clone()); @@ -378,7 +378,7 @@ mod tests { program_owner: [1, 2, 3, 4, 5, 6, 7, 8], balance: 1337, data: vec![0xde, 0xad, 0xbe, 0xef].try_into().unwrap(), - nonce: 10, + nonce: 10_u128.into(), }; let mut account_post_state = AccountPostState::new(account.clone()); diff --git a/nssa/src/privacy_preserving_transaction/circuit.rs b/nssa/src/privacy_preserving_transaction/circuit.rs index 084b05db..2ab141a3 100644 --- a/nssa/src/privacy_preserving_transaction/circuit.rs +++ b/nssa/src/privacy_preserving_transaction/circuit.rs @@ -63,12 +63,11 @@ impl From for ProgramWithDependencies { /// Generates a proof of the execution of a NSSA program inside the privacy preserving execution /// circuit. -#[expect(clippy::too_many_arguments, reason = "TODO: fix later")] +/// TODO: too many parameters. pub fn execute_and_prove( pre_states: Vec, instruction_data: InstructionData, visibility_mask: Vec, - private_account_nonces: Vec, private_account_keys: Vec<(NullifierPublicKey, SharedSecretKey)>, private_account_nsks: Vec, private_account_membership_proofs: Vec>, @@ -127,7 +126,6 @@ pub fn execute_and_prove( let circuit_input = PrivacyPreservingCircuitInput { program_outputs, visibility_mask, - private_account_nonces, private_account_keys, private_account_nsks, private_account_membership_proofs, @@ -177,7 +175,7 @@ mod tests { use nssa_core::{ Commitment, DUMMY_COMMITMENT_HASH, EncryptionScheme, Nullifier, - account::{Account, AccountId, AccountWithMetadata, data::Data}, + account::{Account, AccountId, AccountWithMetadata, Nonce, data::Data}, }; use super::*; @@ -215,14 +213,14 @@ mod tests { let expected_sender_post = Account { program_owner: program.id(), balance: 100 - balance_to_move, - nonce: 0, + nonce: Nonce::default(), data: Data::default(), }; let expected_recipient_post = Account { program_owner: program.id(), balance: balance_to_move, - nonce: 0xdead_beef, + nonce: Nonce::private_account_nonce_init(&recipient_keys.npk()), data: Data::default(), }; @@ -235,7 +233,6 @@ mod tests { vec![sender, recipient], Program::serialize_instruction(balance_to_move).unwrap(), vec![0, 2], - vec![0xdead_beef], vec![(recipient_keys.npk(), shared_secret)], vec![], vec![None], @@ -269,10 +266,11 @@ mod tests { let sender_keys = test_private_account_keys_1(); let recipient_keys = test_private_account_keys_2(); + let sender_nonce = Nonce(0xdead_beef); let sender_pre = AccountWithMetadata::new( Account { balance: 100, - nonce: 0xdead_beef, + nonce: sender_nonce, program_owner: program.id(), data: Data::default(), }, @@ -307,13 +305,13 @@ mod tests { let expected_private_account_1 = Account { program_owner: program.id(), balance: 100 - balance_to_move, - nonce: 0xdead_beef1, + nonce: sender_nonce.private_account_nonce_increment(&sender_keys.nsk), ..Default::default() }; let expected_private_account_2 = Account { program_owner: program.id(), balance: balance_to_move, - nonce: 0xdead_beef2, + nonce: Nonce::private_account_nonce_init(&recipient_keys.npk()), ..Default::default() }; let expected_new_commitments = vec![ @@ -331,7 +329,6 @@ mod tests { vec![sender_pre, recipient], Program::serialize_instruction(balance_to_move).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ (sender_keys.npk(), shared_secret_1), (recipient_keys.npk(), shared_secret_2), diff --git a/nssa/src/privacy_preserving_transaction/message.rs b/nssa/src/privacy_preserving_transaction/message.rs index d164606e..4b93e820 100644 --- a/nssa/src/privacy_preserving_transaction/message.rs +++ b/nssa/src/privacy_preserving_transaction/message.rs @@ -140,7 +140,7 @@ pub mod tests { let public_account_ids = vec![AccountId::new([1; 32])]; - let nonces = vec![1, 2, 3]; + let nonces = vec![1_u128.into(), 2_u128.into(), 3_u128.into()]; let public_post_states = vec![Account::default()]; diff --git a/nssa/src/public_transaction/transaction.rs b/nssa/src/public_transaction/transaction.rs index 4c4cd0b3..8c84d83c 100644 --- a/nssa/src/public_transaction/transaction.rs +++ b/nssa/src/public_transaction/transaction.rs @@ -269,7 +269,7 @@ pub mod tests { fn transaction_for_tests() -> PublicTransaction { let (key1, key2, addr1, addr2) = keys_for_tests(); - let nonces = vec![0, 0]; + let nonces = vec![0_u128.into(), 0_u128.into()]; let instruction = 1337; let message = Message::try_new( Program::authenticated_transfer_program().id(), @@ -347,7 +347,7 @@ pub mod tests { fn account_id_list_cant_have_duplicates() { let (key1, _, addr1, _) = keys_for_tests(); let state = state_for_tests(); - let nonces = vec![0, 0]; + let nonces = vec![0_u128.into(), 0_u128.into()]; let instruction = 1337; let message = Message::try_new( Program::authenticated_transfer_program().id(), @@ -367,7 +367,7 @@ pub mod tests { fn number_of_nonces_must_match_number_of_signatures() { let (key1, key2, addr1, addr2) = keys_for_tests(); let state = state_for_tests(); - let nonces = vec![0]; + let nonces = vec![0_u128.into()]; let instruction = 1337; let message = Message::try_new( Program::authenticated_transfer_program().id(), @@ -387,7 +387,7 @@ pub mod tests { fn all_signatures_must_be_valid() { let (key1, key2, addr1, addr2) = keys_for_tests(); let state = state_for_tests(); - let nonces = vec![0, 0]; + let nonces = vec![0_u128.into(), 0_u128.into()]; let instruction = 1337; let message = Message::try_new( Program::authenticated_transfer_program().id(), @@ -408,7 +408,7 @@ pub mod tests { fn nonces_must_match_the_state_current_nonces() { let (key1, key2, addr1, addr2) = keys_for_tests(); let state = state_for_tests(); - let nonces = vec![0, 1]; + let nonces = vec![0_u128.into(), 1_u128.into()]; let instruction = 1337; let message = Message::try_new( Program::authenticated_transfer_program().id(), @@ -428,7 +428,7 @@ pub mod tests { fn program_id_must_belong_to_bulitin_program_ids() { let (key1, key2, addr1, addr2) = keys_for_tests(); let state = state_for_tests(); - let nonces = vec![0, 0]; + let nonces = vec![0_u128.into(), 0_u128.into()]; let instruction = 1337; let unknown_program_id = [0xdead_beef; 8]; let message = diff --git a/nssa/src/public_transaction/witness_set.rs b/nssa/src/public_transaction/witness_set.rs index 49c18b81..d6b32891 100644 --- a/nssa/src/public_transaction/witness_set.rs +++ b/nssa/src/public_transaction/witness_set.rs @@ -67,7 +67,7 @@ mod tests { let pubkey2 = PublicKey::new_from_private_key(&key2); let addr1 = AccountId::from(&pubkey1); let addr2 = AccountId::from(&pubkey2); - let nonces = vec![1, 2]; + let nonces = vec![1_u128.into(), 2_u128.into()]; let instruction = vec![1, 2, 3, 4]; let message = Message::try_new([0; 8], vec![addr1, addr2], nonces, instruction).unwrap(); diff --git a/nssa/src/state.rs b/nssa/src/state.rs index c1f72b4c..8ae26e74 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeSet, HashMap, HashSet}; use borsh::{BorshDeserialize, BorshSerialize}; use nssa_core::{ Commitment, CommitmentSetDigest, DUMMY_COMMITMENT, MembershipProof, Nullifier, - account::{Account, AccountId}, + account::{Account, AccountId, Nonce}, program::ProgramId, }; @@ -172,10 +172,7 @@ impl V02State { for account_id in tx.signer_account_ids() { let current_account = self.get_account_by_id_mut(account_id); - current_account.nonce = current_account - .nonce - .checked_add(1) - .ok_or(NssaError::MaxAccountNonceReached)?; + current_account.nonce.public_account_nonce_increment(); } Ok(()) @@ -215,10 +212,7 @@ impl V02State { // 5. Increment nonces for public signers for account_id in tx.signer_account_ids() { let current_account = self.get_account_by_id_mut(account_id); - current_account.nonce = current_account - .nonce - .checked_add(1) - .ok_or(NssaError::MaxAccountNonceReached)?; + current_account.nonce.public_account_nonce_increment(); } Ok(()) @@ -303,7 +297,7 @@ impl V02State { balance: 1_500_000, // Difficulty: 3 data: vec![3; 33].try_into().expect("should fit"), - nonce: 0, + nonce: Nonce::default(), }, ); } @@ -389,7 +383,7 @@ pub mod tests { ..Account::default() }; let account_with_default_values_except_nonce = Account { - nonce: 37, + nonce: Nonce(37), ..Account::default() }; let account_with_default_values_except_data = Account { @@ -463,7 +457,7 @@ pub mod tests { balance: u128, ) -> PublicTransaction { let account_ids = vec![from, to]; - let nonces = vec![nonce]; + let nonces = vec![Nonce(nonce)]; let program_id = Program::authenticated_transfer_program().id(); let message = public_transaction::Message::try_new(program_id, account_ids, nonces, balance).unwrap(); @@ -577,8 +571,8 @@ pub mod tests { assert_eq!(state.get_account_by_id(from).balance, 95); assert_eq!(state.get_account_by_id(to).balance, 5); - assert_eq!(state.get_account_by_id(from).nonce, 1); - assert_eq!(state.get_account_by_id(to).nonce, 0); + assert_eq!(state.get_account_by_id(from).nonce, Nonce(1)); + assert_eq!(state.get_account_by_id(to).nonce, Nonce(0)); } #[test] @@ -599,8 +593,8 @@ pub mod tests { assert!(matches!(result, Err(NssaError::ProgramExecutionFailed(_)))); assert_eq!(state.get_account_by_id(from).balance, 100); assert_eq!(state.get_account_by_id(to).balance, 0); - assert_eq!(state.get_account_by_id(from).nonce, 0); - assert_eq!(state.get_account_by_id(to).nonce, 0); + assert_eq!(state.get_account_by_id(from).nonce, Nonce(0)); + assert_eq!(state.get_account_by_id(to).nonce, Nonce(0)); } #[test] @@ -622,8 +616,8 @@ pub mod tests { assert_eq!(state.get_account_by_id(from).balance, 192); assert_eq!(state.get_account_by_id(to).balance, 108); - assert_eq!(state.get_account_by_id(from).nonce, 1); - assert_eq!(state.get_account_by_id(to).nonce, 0); + assert_eq!(state.get_account_by_id(from).nonce, Nonce(1)); + assert_eq!(state.get_account_by_id(to).nonce, Nonce(0)); } #[test] @@ -646,9 +640,9 @@ pub mod tests { assert_eq!(state.get_account_by_id(account_id1).balance, 95); assert_eq!(state.get_account_by_id(account_id2).balance, 2); assert_eq!(state.get_account_by_id(account_id3).balance, 3); - assert_eq!(state.get_account_by_id(account_id1).nonce, 1); - assert_eq!(state.get_account_by_id(account_id2).nonce, 1); - assert_eq!(state.get_account_by_id(account_id3).nonce, 0); + assert_eq!(state.get_account_by_id(account_id1).nonce, Nonce(1)); + assert_eq!(state.get_account_by_id(account_id2).nonce, Nonce(1)); + assert_eq!(state.get_account_by_id(account_id3).nonce, Nonce(0)); } #[test] @@ -942,7 +936,6 @@ pub mod tests { vec![sender, recipient], Program::serialize_instruction(balance_to_move).unwrap(), vec![0, 2], - vec![0xdead_beef], vec![(recipient_keys.npk(), shared_secret)], vec![], vec![None], @@ -967,7 +960,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(); @@ -989,7 +981,6 @@ pub mod tests { vec![sender_pre, recipient_pre], Program::serialize_instruction(balance_to_move).unwrap(), vec![1, 2], - new_nonces.to_vec(), vec![ (sender_keys.npk(), shared_secret_1), (recipient_keys.npk(), shared_secret_2), @@ -1021,7 +1012,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(); @@ -1042,7 +1032,6 @@ pub mod tests { vec![sender_pre, recipient_pre], Program::serialize_instruction(balance_to_move).unwrap(), vec![1, 0], - vec![new_nonce], vec![(sender_keys.npk(), shared_secret)], vec![sender_keys.nsk], vec![state.get_proof_for_commitment(&sender_commitment)], @@ -1083,7 +1072,7 @@ pub mod tests { let expected_sender_post = { let mut this = state.get_account_by_id(sender_keys.account_id()); this.balance -= balance_to_move; - this.nonce += 1; + this.nonce.public_account_nonce_increment(); this }; @@ -1107,10 +1096,12 @@ pub mod tests { #[test] fn transition_from_privacy_preserving_transaction_private() { let sender_keys = test_private_account_keys_1(); + let sender_nonce = Nonce(0xdead_beef); + let sender_private_account = Account { program_owner: Program::authenticated_transfer_program().id(), balance: 100, - nonce: 0xdead_beef, + nonce: sender_nonce, data: Data::default(), }; let recipient_keys = test_private_account_keys_2(); @@ -1125,7 +1116,6 @@ pub mod tests { &sender_private_account, &recipient_keys, balance_to_move, - [0xcafe_cafe, 0xfeca_feca], &state, ); @@ -1133,7 +1123,7 @@ pub mod tests { &sender_keys.npk(), &Account { program_owner: Program::authenticated_transfer_program().id(), - nonce: 0xcafe_cafe, + nonce: sender_nonce.private_account_nonce_increment(&sender_keys.nsk), balance: sender_private_account.balance - balance_to_move, data: Data::default(), }, @@ -1147,7 +1137,7 @@ pub mod tests { &recipient_keys.npk(), &Account { program_owner: Program::authenticated_transfer_program().id(), - nonce: 0xfeca_feca, + nonce: Nonce::private_account_nonce_init(&recipient_keys.npk()), balance: balance_to_move, ..Account::default() }, @@ -1173,10 +1163,12 @@ pub mod tests { #[test] fn transition_from_privacy_preserving_transaction_deshielded() { let sender_keys = test_private_account_keys_1(); + let sender_nonce = Nonce(0xdead_beef); + let sender_private_account = Account { program_owner: Program::authenticated_transfer_program().id(), balance: 100, - nonce: 0xdead_beef, + nonce: sender_nonce, data: Data::default(), }; let recipient_keys = test_public_account_keys_1(); @@ -1200,7 +1192,6 @@ pub mod tests { &sender_private_account, &recipient_keys.account_id(), balance_to_move, - 0xcafe_cafe, &state, ); @@ -1208,7 +1199,7 @@ pub mod tests { &sender_keys.npk(), &Account { program_owner: Program::authenticated_transfer_program().id(), - nonce: 0xcafe_cafe, + nonce: sender_nonce.private_account_nonce_increment(&sender_keys.nsk), balance: sender_private_account.balance - balance_to_move, data: Data::default(), }, @@ -1257,7 +1248,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1284,7 +1274,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1311,7 +1300,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1338,7 +1326,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1373,7 +1360,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1400,7 +1386,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1436,7 +1421,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1463,7 +1447,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1499,7 +1482,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1537,7 +1519,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -1561,13 +1542,10 @@ pub mod tests { let private_account_2 = AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk()); - // Setting only one nonce for an execution with two private accounts. - let private_account_nonces = [0xdead_beef1]; let result = execute_and_prove( vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - private_account_nonces.to_vec(), vec![ ( sender_keys.npk(), @@ -1611,7 +1589,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], private_account_keys.to_vec(), vec![sender_keys.nsk], vec![Some((0, vec![]))], @@ -1644,7 +1621,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ ( sender_keys.npk(), @@ -1686,7 +1662,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ ( sender_keys.npk(), @@ -1744,7 +1719,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], private_account_keys.to_vec(), private_account_nsks.to_vec(), private_account_membership_proofs.to_vec(), @@ -1782,7 +1756,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ ( sender_keys.npk(), @@ -1829,7 +1802,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ ( sender_keys.npk(), @@ -1876,7 +1848,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ ( sender_keys.npk(), @@ -1912,7 +1883,7 @@ pub mod tests { let private_account_2 = AccountWithMetadata::new( Account { // Non default nonce - nonce: 0xdead_beef, + nonce: Nonce(0xdead_beef), ..Account::default() }, false, @@ -1923,7 +1894,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ ( sender_keys.npk(), @@ -1968,7 +1938,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], vec![ ( sender_keys.npk(), @@ -2010,7 +1979,6 @@ pub mod tests { vec![], vec![], vec![], - vec![], &program.into(), ); @@ -2034,14 +2002,10 @@ pub mod tests { let private_account_2 = AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk()); - // Setting three new private account nonces for a circuit execution with only two private - // accounts. - let private_account_nonces = [0xdead_beef1, 0xdead_beef2, 0xdead_beef3]; let result = execute_and_prove( vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - private_account_nonces.to_vec(), vec![ ( sender_keys.npk(), @@ -2097,7 +2061,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), vec![1, 2], - vec![0xdead_beef1, 0xdead_beef2], private_account_keys.to_vec(), vec![sender_keys.nsk], vec![Some((0, vec![]))], @@ -2133,7 +2096,6 @@ pub mod tests { vec![private_account_1, private_account_2], Program::serialize_instruction(10_u128).unwrap(), visibility_mask.to_vec(), - vec![0xdead_beef1, 0xdead_beef2], vec![ ( sender_keys.npk(), @@ -2155,10 +2117,12 @@ pub mod tests { #[test] fn private_accounts_can_only_be_initialized_once() { let sender_keys = test_private_account_keys_1(); + let sender_nonce = Nonce(0xdead_beef); + let sender_private_account = Account { program_owner: Program::authenticated_transfer_program().id(), balance: 100, - nonce: 0xdead_beef, + nonce: sender_nonce, data: Data::default(), }; let recipient_keys = test_private_account_keys_2(); @@ -2167,13 +2131,13 @@ pub mod tests { .with_private_account(&sender_keys, &sender_private_account); let balance_to_move = 37; + let balance_to_move_2 = 30; let tx = private_balance_transfer_for_tests( &sender_keys, &sender_private_account, &recipient_keys, balance_to_move, - [0xcafe_cafe, 0xfeca_feca], &state, ); @@ -2183,8 +2147,8 @@ pub mod tests { let sender_private_account = Account { program_owner: Program::authenticated_transfer_program().id(), - balance: 100 - balance_to_move, - nonce: 0xcafe_cafe, + balance: 100, + nonce: sender_nonce, data: Data::default(), }; @@ -2192,8 +2156,7 @@ pub mod tests { &sender_keys, &sender_private_account, &recipient_keys, - balance_to_move, - [0x1234, 0x5678], + balance_to_move_2, &state, ); @@ -2229,7 +2192,6 @@ pub mod tests { vec![private_account_1.clone(), private_account_1], Program::serialize_instruction(100_u128).unwrap(), visibility_mask.to_vec(), - vec![0xdead_beef1, 0xdead_beef2], vec![ (sender_keys.npk(), shared_secret), (sender_keys.npk(), shared_secret), @@ -2265,9 +2227,13 @@ pub mod tests { ..Account::default() }; - let message = - public_transaction::Message::try_new(program.id(), vec![from, to], vec![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); @@ -2307,7 +2273,7 @@ pub mod tests { program.id(), vec![to, from], // The chain_caller program permutes the account order in the chain // call - vec![0], + vec![Nonce(0)], instruction, ) .unwrap(); @@ -2347,7 +2313,7 @@ pub mod tests { program.id(), vec![to, from], // The chain_caller program permutes the account order in the chain // call - vec![0], + vec![Nonce(0)], instruction, ) .unwrap(); @@ -2444,7 +2410,7 @@ pub mod tests { chain_caller.id(), vec![to, from], // The chain_caller program permutes the account order in the chain // call - vec![0], + vec![Nonce(0)], instruction, ) .unwrap(); @@ -2514,8 +2480,8 @@ pub mod tests { dependencies.insert(auth_transfers.id(), auth_transfers); let program_with_deps = ProgramWithDependencies::new(chain_caller, dependencies); - let from_new_nonce = 0xdead_beef1; - let to_new_nonce = 0xdead_beef2; + 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 - u128::from(number_of_calls) * amount, @@ -2536,7 +2502,6 @@ pub mod tests { vec![to_account, from_account], Program::serialize_instruction(instruction).unwrap(), vec![1, 1], - vec![from_new_nonce, to_new_nonce], vec![(from_keys.npk(), to_ss), (to_keys.npk(), from_ss)], vec![from_keys.nsk, to_keys.nsk], vec![ @@ -2732,14 +2697,14 @@ pub mod tests { let expected_sender_post = { let mut this = state.get_account_by_id(sender_id); this.balance = sender_init_balance; - this.nonce = 0; + this.nonce = Nonce(0); this }; let expected_recipient_post = { let mut this = state.get_account_by_id(sender_id); this.balance = recipient_init_balance; - this.nonce = 0; + this.nonce = Nonce(0); this }; @@ -2768,14 +2733,11 @@ pub mod tests { // Balance to initialize the account with (0 for a new account) let balance: u128 = 0; - let nonce = 0xdead_beef1; - // Execute and prove the circuit with the authorized account but no commitment proof let (output, proof) = execute_and_prove( vec![authorized_account], Program::serialize_instruction(balance).unwrap(), vec![1], - vec![nonce], vec![(private_keys.npk(), shared_secret)], vec![private_keys.nsk], vec![None], @@ -2821,14 +2783,12 @@ pub mod tests { let epk = EphemeralPublicKey::from_scalar(esk); let balance: u128 = 0; - let nonce = 0xdead_beef1; // Step 2: Execute claimer program to claim the account with authentication let (output, proof) = execute_and_prove( vec![authorized_account.clone()], Program::serialize_instruction(balance).unwrap(), vec![1], - vec![nonce], vec![(private_keys.npk(), shared_secret)], vec![private_keys.nsk], vec![None], @@ -2869,14 +2829,11 @@ pub mod tests { let esk2 = [4; 32]; let shared_secret2 = SharedSecretKey::new(&esk2, &private_keys.vpk()); - let nonce2 = 0xdead_beef2; - // Step 3: Try to execute noop program with authentication but without initialization let res = execute_and_prove( vec![account_metadata], Program::serialize_instruction(()).unwrap(), vec![1], - vec![nonce2], vec![(private_keys.npk(), shared_secret2)], vec![private_keys.nsk], vec![None], @@ -2946,7 +2903,6 @@ pub mod tests { vec![private_account], Program::serialize_instruction(instruction).unwrap(), vec![1], - vec![2], vec![( sender_keys.npk(), SharedSecretKey::new(&[3; 32], &sender_keys.vpk()), @@ -2974,7 +2930,6 @@ pub mod tests { vec![private_account], Program::serialize_instruction(instruction).unwrap(), vec![1], - vec![2], vec![( sender_keys.npk(), SharedSecretKey::new(&[3; 32], &sender_keys.vpk()), @@ -3026,14 +2981,11 @@ pub mod tests { dependencies.insert(auth_transfers.id(), auth_transfers); let program_with_deps = ProgramWithDependencies::new(malicious_program, dependencies); - let recipient_new_nonce = 0xdead_beef1; - // Act - execute the malicious program - this should fail during proving let result = execute_and_prove( vec![sender_account, recipient_account], Program::serialize_instruction(instruction).unwrap(), vec![0, 1], - vec![recipient_new_nonce], vec![(recipient_keys.npk(), recipient)], vec![recipient_keys.nsk], vec![state.get_proof_for_commitment(&recipient_commitment)], diff --git a/program_methods/guest/src/bin/privacy_preserving_circuit.rs b/program_methods/guest/src/bin/privacy_preserving_circuit.rs index b74b3e65..99782d7f 100644 --- a/program_methods/guest/src/bin/privacy_preserving_circuit.rs +++ b/program_methods/guest/src/bin/privacy_preserving_circuit.rs @@ -200,7 +200,6 @@ impl ExecutionState { fn compute_circuit_output( execution_state: ExecutionState, visibility_mask: &[u8], - private_account_nonces: &[Nonce], private_account_keys: &[(NullifierPublicKey, SharedSecretKey)], private_account_nsks: &[NullifierSecretKey], private_account_membership_proofs: &[Option], @@ -220,7 +219,6 @@ fn compute_circuit_output( "Invalid visibility mask length" ); - let mut private_nonces_iter = private_account_nonces.iter(); let mut private_keys_iter = private_account_keys.iter(); let mut private_nsks_iter = private_account_nsks.iter(); let mut private_membership_proofs_iter = private_account_membership_proofs.iter(); @@ -246,7 +244,7 @@ fn compute_circuit_output( "AccountId mismatch" ); - let new_nullifier = if account_visibility_mask == 1 { + let (new_nullifier, new_nonce) = if account_visibility_mask == 1 { // Private account with authentication let Some(nsk) = private_nsks_iter.next() else { @@ -270,12 +268,16 @@ fn compute_circuit_output( panic!("Missing membership proof"); }; - compute_nullifier_and_set_digest( + let new_nullifier = compute_nullifier_and_set_digest( membership_proof_opt.as_ref(), &pre_state.account, npk, nsk, - ) + ); + + let new_nonce = pre_state.account.nonce.private_account_nonce_increment(nsk); + + (new_nullifier, new_nonce) } else { // Private account without authentication @@ -300,16 +302,16 @@ fn compute_circuit_output( ); let nullifier = Nullifier::for_account_initialization(npk); - (nullifier, DUMMY_COMMITMENT_HASH) + + let new_nonce = Nonce::private_account_nonce_init(npk); + + ((nullifier, DUMMY_COMMITMENT_HASH), new_nonce) }; output.new_nullifiers.push(new_nullifier); // Update post-state with new nonce let mut post_with_updated_nonce = post_state; - let Some(new_nonce) = private_nonces_iter.next() else { - panic!("Missing private account nonce"); - }; - post_with_updated_nonce.nonce = *new_nonce; + post_with_updated_nonce.nonce = new_nonce; // Compute commitment let commitment_post = Commitment::new(npk, &post_with_updated_nonce); @@ -332,8 +334,6 @@ fn compute_circuit_output( } } - assert!(private_nonces_iter.next().is_none(), "Too many nonces"); - assert!( private_keys_iter.next().is_none(), "Too many private account keys" @@ -386,7 +386,6 @@ fn main() { let PrivacyPreservingCircuitInput { program_outputs, visibility_mask, - private_account_nonces, private_account_keys, private_account_nsks, private_account_membership_proofs, @@ -398,7 +397,6 @@ fn main() { let output = compute_circuit_output( execution_state, &visibility_mask, - &private_account_nonces, &private_account_keys, &private_account_nsks, &private_account_membership_proofs, diff --git a/programs/amm/src/tests.rs b/programs/amm/src/tests.rs index 9509d39e..e1e8698d 100644 --- a/programs/amm/src/tests.rs +++ b/programs/amm/src/tests.rs @@ -446,7 +446,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_a_definition_id(), balance: BalanceForTests::user_token_a_balance(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::user_token_a_id(), @@ -462,7 +462,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_b_definition_id(), balance: BalanceForTests::user_token_b_balance(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::user_token_b_id(), @@ -478,7 +478,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_a_definition_id(), balance: BalanceForTests::vault_a_reserve_init(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_a_id(), @@ -494,7 +494,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_b_definition_id(), balance: BalanceForTests::vault_b_reserve_init(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_b_id(), @@ -510,7 +510,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_a_definition_id(), balance: BalanceForTests::vault_a_reserve_high(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_a_id(), @@ -526,7 +526,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_b_definition_id(), balance: BalanceForTests::vault_b_reserve_high(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_b_id(), @@ -542,7 +542,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_a_definition_id(), balance: BalanceForTests::vault_a_reserve_low(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_a_id(), @@ -558,7 +558,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_b_definition_id(), balance: BalanceForTests::vault_b_reserve_low(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_b_id(), @@ -574,7 +574,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_a_definition_id(), balance: 0, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_a_id(), @@ -590,7 +590,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_b_definition_id(), balance: 0, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_b_id(), @@ -607,7 +607,7 @@ impl AccountWithMetadataForTests { total_supply: BalanceForTests::lp_supply_init(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::token_lp_definition_id(), @@ -624,7 +624,7 @@ impl AccountWithMetadataForTests { total_supply: BalanceForTests::lp_supply_init(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::vault_a_id(), @@ -640,7 +640,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_lp_definition_id(), balance: 0, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::user_token_lp_id(), @@ -656,7 +656,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_lp_definition_id(), balance: BalanceForTests::user_token_lp_balance(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::user_token_lp_id(), @@ -680,7 +680,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -704,7 +704,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -728,7 +728,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -752,7 +752,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -776,7 +776,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -800,7 +800,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -824,7 +824,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -848,7 +848,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -872,7 +872,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -896,7 +896,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -920,7 +920,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: false, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -944,7 +944,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: false, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: AccountId::new([4; 32]), @@ -960,7 +960,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_a_definition_id(), balance: BalanceForTests::vault_a_reserve_init(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: AccountId::new([4; 32]), @@ -976,7 +976,7 @@ impl AccountWithMetadataForTests { definition_id: IdForTests::token_b_definition_id(), balance: BalanceForTests::vault_b_reserve_init(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: AccountId::new([4; 32]), @@ -1000,7 +1000,7 @@ impl AccountWithMetadataForTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -1239,7 +1239,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::user_token_a_holding_init(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1251,7 +1251,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::user_token_b_holding_init(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1271,7 +1271,7 @@ impl AccountsForExeTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1284,7 +1284,7 @@ impl AccountsForExeTests { total_supply: BalanceForExeTests::token_a_supply(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1297,7 +1297,7 @@ impl AccountsForExeTests { total_supply: BalanceForExeTests::token_b_supply(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1310,7 +1310,7 @@ impl AccountsForExeTests { total_supply: BalanceForExeTests::token_lp_supply(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1322,7 +1322,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::vault_a_balance_init(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1334,7 +1334,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::vault_b_balance_init(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1346,7 +1346,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_lp_definition_id(), balance: BalanceForExeTests::user_token_lp_holding_init(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1358,7 +1358,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::vault_a_balance_swap_1(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1370,7 +1370,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::vault_b_balance_swap_1(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1390,7 +1390,7 @@ impl AccountsForExeTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1402,7 +1402,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::user_token_a_holding_swap_1(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1414,7 +1414,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::user_token_b_holding_swap_1(), }), - nonce: 1, + nonce: 1_u128.into(), } } @@ -1426,7 +1426,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::vault_a_balance_swap_2(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1438,7 +1438,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::vault_b_balance_swap_2(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1458,7 +1458,7 @@ impl AccountsForExeTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1470,7 +1470,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::user_token_a_holding_swap_2(), }), - nonce: 1, + nonce: 1_u128.into(), } } @@ -1482,7 +1482,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::user_token_b_holding_swap_2(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1494,7 +1494,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::vault_a_balance_add(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1506,7 +1506,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::vault_b_balance_add(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1526,7 +1526,7 @@ impl AccountsForExeTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1538,7 +1538,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::user_token_a_holding_add(), }), - nonce: 1, + nonce: 1_u128.into(), } } @@ -1550,7 +1550,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::user_token_b_holding_add(), }), - nonce: 1, + nonce: 1_u128.into(), } } @@ -1562,7 +1562,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_lp_definition_id(), balance: BalanceForExeTests::user_token_lp_holding_add(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1575,7 +1575,7 @@ impl AccountsForExeTests { total_supply: BalanceForExeTests::token_lp_supply_add(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1587,7 +1587,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::vault_a_balance_remove(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1599,7 +1599,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::vault_b_balance_remove(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1619,7 +1619,7 @@ impl AccountsForExeTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1631,7 +1631,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::user_token_a_holding_remove(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1643,7 +1643,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::user_token_b_holding_remove(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1655,7 +1655,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_lp_definition_id(), balance: BalanceForExeTests::user_token_lp_holding_remove(), }), - nonce: 1, + nonce: 1_u128.into(), } } @@ -1668,7 +1668,7 @@ impl AccountsForExeTests { total_supply: BalanceForExeTests::token_lp_supply_remove(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1681,7 +1681,7 @@ impl AccountsForExeTests { total_supply: 0, metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1693,7 +1693,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: 0, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1705,7 +1705,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: 0, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1725,7 +1725,7 @@ impl AccountsForExeTests { fees: 0_u128, active: false, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1737,7 +1737,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_a_definition_id(), balance: BalanceForExeTests::user_token_a_holding_new_definition(), }), - nonce: 1, + nonce: 1_u128.into(), } } @@ -1749,7 +1749,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_b_definition_id(), balance: BalanceForExeTests::user_token_b_holding_new_definition(), }), - nonce: 1, + nonce: 1_u128.into(), } } @@ -1761,7 +1761,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_lp_definition_id(), balance: BalanceForExeTests::lp_supply_init(), }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1774,7 +1774,7 @@ impl AccountsForExeTests { total_supply: BalanceForExeTests::lp_supply_init(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1794,7 +1794,7 @@ impl AccountsForExeTests { fees: 0_u128, active: true, }), - nonce: 0, + nonce: 0_u128.into(), } } @@ -1806,7 +1806,7 @@ impl AccountsForExeTests { definition_id: IdForExeTests::token_lp_definition_id(), balance: 0, }), - nonce: 0, + nonce: 0_u128.into(), } } } @@ -2730,7 +2730,7 @@ fn simple_amm_remove() { IdForExeTests::user_token_b_id(), IdForExeTests::user_token_lp_id(), ], - vec![0], + vec![0_u128.into()], instruction, ) .unwrap(); @@ -2808,7 +2808,7 @@ fn simple_amm_new_definition_inactive_initialized_pool_and_uninit_user_lp() { IdForExeTests::user_token_b_id(), IdForExeTests::user_token_lp_id(), ], - vec![0, 0], + vec![0_u128.into(), 0_u128.into()], instruction, ) .unwrap(); @@ -2893,7 +2893,7 @@ fn simple_amm_new_definition_inactive_initialized_pool_init_user_lp() { IdForExeTests::user_token_b_id(), IdForExeTests::user_token_lp_id(), ], - vec![0, 0], + vec![0_u128.into(), 0_u128.into()], instruction, ) .unwrap(); @@ -2966,7 +2966,7 @@ fn simple_amm_new_definition_uninitialized_pool() { IdForExeTests::user_token_b_id(), IdForExeTests::user_token_lp_id(), ], - vec![0, 0], + vec![0_u128.into(), 0_u128.into()], instruction, ) .unwrap(); @@ -3029,7 +3029,7 @@ fn simple_amm_add() { IdForExeTests::user_token_b_id(), IdForExeTests::user_token_lp_id(), ], - vec![0, 0], + vec![0_u128.into(), 0_u128.into()], instruction, ) .unwrap(); @@ -3090,7 +3090,7 @@ fn simple_amm_swap_1() { IdForExeTests::user_token_a_id(), IdForExeTests::user_token_b_id(), ], - vec![0], + vec![0_u128.into()], instruction, ) .unwrap(); @@ -3141,7 +3141,7 @@ fn simple_amm_swap_2() { IdForExeTests::user_token_a_id(), IdForExeTests::user_token_b_id(), ], - vec![0], + vec![0_u128.into()], instruction, ) .unwrap(); diff --git a/programs/token/src/tests.rs b/programs/token/src/tests.rs index db0aa1bb..640d6d76 100644 --- a/programs/token/src/tests.rs +++ b/programs/token/src/tests.rs @@ -37,7 +37,7 @@ impl AccountForTests { total_supply: BalanceForTests::init_supply(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -54,7 +54,7 @@ impl AccountForTests { total_supply: BalanceForTests::init_supply(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: false, account_id: IdForTests::pool_definition_id(), @@ -70,7 +70,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id_diff(), balance: BalanceForTests::holding_balance(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), @@ -86,7 +86,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::holding_balance(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), @@ -102,7 +102,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::holding_balance(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: false, account_id: IdForTests::holding_id(), @@ -118,7 +118,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::init_supply(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: false, account_id: IdForTests::holding_id(), @@ -135,7 +135,7 @@ impl AccountForTests { total_supply: BalanceForTests::init_supply_burned(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -151,7 +151,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::holding_balance_burned(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: false, account_id: IdForTests::holding_id(), @@ -175,7 +175,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::mint_success(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: false, account_id: IdForTests::holding_id(), @@ -191,7 +191,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::holding_balance_mint(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -208,7 +208,7 @@ impl AccountForTests { total_supply: BalanceForTests::init_supply_mint(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -224,7 +224,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::mint_overflow(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -241,7 +241,7 @@ impl AccountForTests { printable_supply: BalanceForTests::printable_copies(), metadata_id: AccountId::new([0; 32]), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -265,7 +265,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::init_supply(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), @@ -282,7 +282,7 @@ impl AccountForTests { total_supply: BalanceForTests::init_supply(), metadata_id: None, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::pool_definition_id(), @@ -298,7 +298,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::init_supply(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), @@ -314,7 +314,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::init_supply(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id_2(), @@ -330,7 +330,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::recipient_post_transfer(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id_2(), @@ -346,7 +346,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), balance: BalanceForTests::sender_post_transfer(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), @@ -362,7 +362,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), print_balance: BalanceForTests::printable_copies(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), @@ -378,7 +378,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), print_balance: 1, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), @@ -394,7 +394,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), print_balance: BalanceForTests::printable_copies() - 1, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), @@ -410,7 +410,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), owned: true, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: false, account_id: IdForTests::holding_id(), @@ -426,7 +426,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), print_balance: BalanceForTests::printable_copies(), }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id_2(), @@ -442,7 +442,7 @@ impl AccountForTests { definition_id: IdForTests::pool_definition_id(), print_balance: 0, }), - nonce: 0, + nonce: 0_u128.into(), }, is_authorized: true, account_id: IdForTests::holding_id(), diff --git a/sequencer_rpc/src/process.rs b/sequencer_rpc/src/process.rs index 3a6a4d60..17c46f03 100644 --- a/sequencer_rpc/src/process.rs +++ b/sequencer_rpc/src/process.rs @@ -237,7 +237,7 @@ impl JsonHandler account_ids .into_iter() - .map(|account_id| state.state().get_account_by_id(account_id).nonce) + .map(|account_id| state.state().get_account_by_id(account_id).nonce.0) .collect() }; diff --git a/test_program_methods/guest/src/bin/nonce_changer.rs b/test_program_methods/guest/src/bin/nonce_changer.rs index d7bea154..52d2e392 100644 --- a/test_program_methods/guest/src/bin/nonce_changer.rs +++ b/test_program_methods/guest/src/bin/nonce_changer.rs @@ -11,7 +11,7 @@ fn main() { let account_pre = &pre.account; let mut account_post = account_pre.clone(); - account_post.nonce = account_post.nonce.overflowing_add(1).0; + account_post.nonce.public_account_nonce_increment(); write_nssa_outputs( instruction_words, diff --git a/wallet-ffi/src/types.rs b/wallet-ffi/src/types.rs index 21942e8d..87c30315 100644 --- a/wallet-ffi/src/types.rs +++ b/wallet-ffi/src/types.rs @@ -222,7 +222,7 @@ impl From for FfiAccount { balance: value.balance.into(), data, data_len, - nonce: value.nonce.into(), + nonce: value.nonce.0.into(), } } } @@ -244,7 +244,7 @@ impl TryFrom<&FfiAccount> for nssa::Account { program_owner: value.program_owner.data, balance: value.balance.into(), data, - nonce: value.nonce.into(), + nonce: nssa_core::account::Nonce(value.nonce.into()), }) } } diff --git a/wallet/src/helperfunctions.rs b/wallet/src/helperfunctions.rs index c7be5811..74f7bab3 100644 --- a/wallet/src/helperfunctions.rs +++ b/wallet/src/helperfunctions.rs @@ -44,7 +44,7 @@ impl From for HumanReadableAccount { balance: account.balance, program_owner, data, - nonce: account.nonce, + nonce: account.nonce.0, } } } @@ -145,12 +145,16 @@ pub fn produce_data_for_storage( } } +#[expect(dead_code, reason = "Maybe used later")] pub(crate) fn produce_random_nonces(size: usize) -> Vec { let mut result = vec![[0; 16]; size]; for bytes in &mut result { OsRng.fill_bytes(bytes); } - result.into_iter().map(Nonce::from_le_bytes).collect() + result + .into_iter() + .map(|x| Nonce(u128::from_le_bytes(x))) + .collect() } pub(crate) fn parse_addr_with_privacy_prefix( diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index a3c58a97..c253797b 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -32,7 +32,7 @@ use tokio::io::AsyncWriteExt as _; use crate::{ config::{PersistentStorage, WalletConfigOverrides}, - helperfunctions::{produce_data_for_storage, produce_random_nonces}, + helperfunctions::produce_data_for_storage, poller::TxPoller, }; @@ -364,7 +364,6 @@ impl WalletCore { pre_states, instruction_data, acc_manager.visibility_mask().to_vec(), - produce_random_nonces(private_account_keys.len()), private_account_keys .iter() .map(|keys| (keys.npk.clone(), keys.ssk)) diff --git a/wallet/src/program_facades/amm.rs b/wallet/src/program_facades/amm.rs index 251970bc..19a51f29 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, + 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, + 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, + 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, + 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 eb2adc9a..eefaa1fe 100644 --- a/wallet/src/program_facades/native_token_transfer/public.rs +++ b/wallet/src/program_facades/native_token_transfer/public.rs @@ -29,8 +29,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, 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); @@ -61,7 +69,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, 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 fe5165ff..bdacae37 100644 --- a/wallet/src/program_facades/token.rs +++ b/wallet/src/program_facades/token.rs @@ -141,7 +141,10 @@ impl Token<'_> { let message = nssa::public_transaction::Message::try_new( program_id, account_ids, - nonces, + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .unwrap(); @@ -342,7 +345,10 @@ impl Token<'_> { let message = nssa::public_transaction::Message::try_new( Program::token().id(), account_ids, - nonces, + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .expect("Instruction should serialize"); @@ -472,7 +478,10 @@ impl Token<'_> { let message = nssa::public_transaction::Message::try_new( Program::token().id(), account_ids, - nonces, + nonces + .iter() + .map(|x| nssa_core::account::Nonce(*x)) + .collect(), instruction, ) .unwrap();