clean up and fixed tests

This commit is contained in:
jonesmarvin8 2026-02-16 19:53:32 -05:00
parent d965b7c25d
commit 889326d2ad
32 changed files with 102 additions and 65 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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,
};

View File

@ -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;

View File

@ -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,

View File

@ -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]

View File

@ -160,9 +160,8 @@ impl AccountId {
#[cfg(test)]
mod tests {
use crate::account::Nonce;
use super::*;
use crate::account::Nonce;
#[test]
fn test_enconding() {

View File

@ -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() {

View File

@ -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],

View File

@ -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::{

View File

@ -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,
};

View File

@ -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();

View File

@ -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);

View File

@ -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();