This commit is contained in:
Sergio Chouhy 2025-09-11 16:37:28 -03:00
parent c3b2f4691b
commit 32910e76e3
9 changed files with 95 additions and 90 deletions

View File

@ -14,6 +14,8 @@ pub struct Account {
pub nonce: Nonce, pub nonce: Nonce,
} }
/// A fingerprint of the owner of an account. This can be, for example, an `Address` in case the account
/// is public, or a `NullifierPublicKey` in case the account is private.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] #[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(any(feature = "host", test), derive(Debug))] #[cfg_attr(any(feature = "host", test), derive(Debug))]
pub struct FingerPrint([u8; 32]); pub struct FingerPrint([u8; 32]);
@ -31,6 +33,17 @@ pub struct AccountWithMetadata {
pub fingerprint: FingerPrint, pub fingerprint: FingerPrint,
} }
#[cfg(feature = "host")]
impl AccountWithMetadata {
pub fn new(account: Account, is_authorized: bool, fingerprint: impl Into<FingerPrint>) -> Self {
Self {
account,
is_authorized,
fingerprint: fingerprint.into(),
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::program::DEFAULT_PROGRAM_ID; use crate::program::DEFAULT_PROGRAM_ID;
@ -64,4 +77,20 @@ mod tests {
assert_eq!(new_acc.program_owner, DEFAULT_PROGRAM_ID); assert_eq!(new_acc.program_owner, DEFAULT_PROGRAM_ID);
} }
#[test]
fn test_account_with_metadata_constructor() {
let account = Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
balance: 1337,
data: b"testing_account_with_metadata_constructor".to_vec(),
nonce: 0xdeadbeef,
};
let fingerprint = FingerPrint::new([8; 32]);
let new_acc_with_metadata =
AccountWithMetadata::new(account.clone(), true, fingerprint.clone());
assert_eq!(new_acc_with_metadata.account, account);
assert!(new_acc_with_metadata.is_authorized);
assert_eq!(new_acc_with_metadata.fingerprint, fingerprint);
}
} }

View File

@ -49,26 +49,26 @@ mod tests {
fn test_privacy_preserving_circuit_output_to_bytes_is_compatible_with_from_slice() { fn test_privacy_preserving_circuit_output_to_bytes_is_compatible_with_from_slice() {
let output = PrivacyPreservingCircuitOutput { let output = PrivacyPreservingCircuitOutput {
public_pre_states: vec![ public_pre_states: vec![
AccountWithMetadata { AccountWithMetadata::new(
account: Account { Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8], program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
balance: 12345678901234567890, balance: 12345678901234567890,
data: b"test data".to_vec(), data: b"test data".to_vec(),
nonce: 18446744073709551614, nonce: 18446744073709551614,
}, },
is_authorized: true, true,
fingerprint: FingerPrint::new([0; 32]), FingerPrint::new([0; 32]),
}, ),
AccountWithMetadata { AccountWithMetadata::new(
account: Account { Account {
program_owner: [9, 9, 9, 8, 8, 8, 7, 7], program_owner: [9, 9, 9, 8, 8, 8, 7, 7],
balance: 123123123456456567112, balance: 123123123456456567112,
data: b"test data".to_vec(), data: b"test data".to_vec(),
nonce: 9999999999999999999999, nonce: 9999999999999999999999,
}, },
is_authorized: false, false,
fingerprint: FingerPrint::new([1; 32]), FingerPrint::new([1; 32]),
}, ),
], ],
public_post_states: vec![Account { public_post_states: vec![Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8], program_owner: [1, 2, 3, 4, 5, 6, 7, 8],

View File

@ -13,12 +13,6 @@ impl From<&NullifierPublicKey> for FingerPrint {
} }
} }
impl From<NullifierPublicKey> for FingerPrint {
fn from(value: NullifierPublicKey) -> Self {
FingerPrint::new(value.0)
}
}
impl From<&NullifierSecretKey> for NullifierPublicKey { impl From<&NullifierSecretKey> for NullifierPublicKey {
fn from(value: &NullifierSecretKey) -> Self { fn from(value: &NullifierSecretKey) -> Self {
let mut bytes = Vec::new(); let mut bytes = Vec::new();

View File

@ -88,12 +88,6 @@ impl From<&Address> for FingerPrint {
} }
} }
impl From<Address> for FingerPrint {
fn from(address: Address) -> Self {
FingerPrint::new(address.value)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{Address, address::AddressError}; use crate::{Address, address::AddressError};

View File

@ -108,20 +108,20 @@ mod tests {
fn prove_privacy_preserving_execution_circuit_public_and_private_pre_accounts() { fn prove_privacy_preserving_execution_circuit_public_and_private_pre_accounts() {
let recipient_keys = test_private_account_keys_1(); let recipient_keys = test_private_account_keys_1();
let program = Program::authenticated_transfer_program(); let program = Program::authenticated_transfer_program();
let sender = AccountWithMetadata { let sender = AccountWithMetadata::new(
account: Account { Account {
balance: 100, balance: 100,
..Account::default() ..Account::default()
}, },
is_authorized: true, true,
fingerprint: FingerPrint::new([0; 32]), FingerPrint::new([0; 32]),
}; );
let recipient = AccountWithMetadata { let recipient = AccountWithMetadata::new(
account: Account::default(), Account::default(),
is_authorized: false, false,
fingerprint: recipient_keys.npk().into(), FingerPrint::from(&recipient_keys.npk()),
}; );
let balance_to_move: u128 = 37; let balance_to_move: u128 = 37;
@ -180,22 +180,22 @@ mod tests {
let sender_keys = test_private_account_keys_1(); let sender_keys = test_private_account_keys_1();
let recipient_keys = test_private_account_keys_2(); let recipient_keys = test_private_account_keys_2();
let sender_pre = AccountWithMetadata { let sender_pre = AccountWithMetadata::new(
account: Account { Account {
balance: 100, balance: 100,
nonce: 0xdeadbeef, nonce: 0xdeadbeef,
..Account::default() ..Account::default()
}, },
is_authorized: true, true,
fingerprint: sender_keys.npk().into(), FingerPrint::from(&sender_keys.npk()),
}; );
let commitment_sender = Commitment::new(&sender_keys.npk(), &sender_pre.account); let commitment_sender = Commitment::new(&sender_keys.npk(), &sender_pre.account);
let recipient = AccountWithMetadata { let recipient = AccountWithMetadata::new(
account: Account::default(), Account::default(),
is_authorized: false, false,
fingerprint: recipient_keys.npk().into(), FingerPrint::from(&recipient_keys.npk()),
}; );
let balance_to_move: u128 = 37; let balance_to_move: u128 = 37;
let mut commitment_set = CommitmentSet::with_capacity(2); let mut commitment_set = CommitmentSet::with_capacity(2);

View File

@ -90,10 +90,12 @@ impl PrivacyPreservingTransaction {
let public_pre_states: Vec<_> = message let public_pre_states: Vec<_> = message
.public_addresses .public_addresses
.iter() .iter()
.map(|address| AccountWithMetadata { .map(|address| {
account: state.get_account_by_address(address), AccountWithMetadata::new(
is_authorized: signer_addresses.contains(address), state.get_account_by_address(address),
fingerprint: address.into(), signer_addresses.contains(address),
address,
)
}) })
.collect(); .collect();

View File

@ -168,19 +168,16 @@ mod tests {
let program = Program::simple_balance_transfer(); let program = Program::simple_balance_transfer();
let balance_to_move: u128 = 11223344556677; let balance_to_move: u128 = 11223344556677;
let instruction_data = Program::serialize_instruction(balance_to_move).unwrap(); let instruction_data = Program::serialize_instruction(balance_to_move).unwrap();
let sender = AccountWithMetadata { let sender = AccountWithMetadata::new(
account: Account { Account {
balance: 77665544332211, balance: 77665544332211,
..Account::default() ..Account::default()
}, },
is_authorized: true, true,
fingerprint: FingerPrint::new([0; 32]), FingerPrint::new([0; 32]),
}; );
let recipient = AccountWithMetadata { let recipient =
account: Account::default(), AccountWithMetadata::new(Account::default(), false, FingerPrint::new([1; 32]));
is_authorized: false,
fingerprint: FingerPrint::new([1; 32]),
};
let expected_sender_post = Account { let expected_sender_post = Account {
balance: 77665544332211 - balance_to_move, balance: 77665544332211 - balance_to_move,

View File

@ -91,10 +91,12 @@ impl PublicTransaction {
let pre_states: Vec<_> = message let pre_states: Vec<_> = message
.addresses .addresses
.iter() .iter()
.map(|address| AccountWithMetadata { .map(|address| {
account: state.get_account_by_address(address), AccountWithMetadata::new(
is_authorized: signer_addresses.contains(address), state.get_account_by_address(address),
fingerprint: address.into(), signer_addresses.contains(address),
address,
)
}) })
.collect(); .collect();

View File

@ -776,19 +776,15 @@ pub mod tests {
balance_to_move: u128, balance_to_move: u128,
state: &V01State, state: &V01State,
) -> PrivacyPreservingTransaction { ) -> PrivacyPreservingTransaction {
let sender = AccountWithMetadata { let sender = AccountWithMetadata::new(
account: state.get_account_by_address(&sender_keys.address()), state.get_account_by_address(&sender_keys.address()),
is_authorized: true, true,
fingerprint: sender_keys.address().into(), &sender_keys.address(),
}; );
let sender_nonce = sender.account.nonce; let sender_nonce = sender.account.nonce;
let recipient = AccountWithMetadata { let recipient = AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk());
account: Account::default(),
is_authorized: false,
fingerprint: recipient_keys.npk().into(),
};
let esk = [3; 32]; let esk = [3; 32];
let shared_secret = SharedSecretKey::new(&esk, &recipient_keys.ivk()); let shared_secret = SharedSecretKey::new(&esk, &recipient_keys.ivk());
@ -827,16 +823,10 @@ pub mod tests {
) -> PrivacyPreservingTransaction { ) -> PrivacyPreservingTransaction {
let program = Program::authenticated_transfer_program(); let program = Program::authenticated_transfer_program();
let sender_commitment = Commitment::new(&sender_keys.npk(), sender_private_account); let sender_commitment = Commitment::new(&sender_keys.npk(), sender_private_account);
let sender_pre = AccountWithMetadata { let sender_pre =
account: sender_private_account.clone(), AccountWithMetadata::new(sender_private_account.clone(), true, &sender_keys.npk());
is_authorized: true, let recipient_pre =
fingerprint: sender_keys.npk().into(), AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk());
};
let recipient_pre = AccountWithMetadata {
account: Account::default(),
is_authorized: false,
fingerprint: recipient_keys.npk().into(),
};
let esk_1 = [3; 32]; let esk_1 = [3; 32];
let shared_secret_1 = SharedSecretKey::new(&esk_1, &sender_keys.ivk()); let shared_secret_1 = SharedSecretKey::new(&esk_1, &sender_keys.ivk());
@ -889,16 +879,13 @@ pub mod tests {
) -> PrivacyPreservingTransaction { ) -> PrivacyPreservingTransaction {
let program = Program::authenticated_transfer_program(); let program = Program::authenticated_transfer_program();
let sender_commitment = Commitment::new(&sender_keys.npk(), sender_private_account); let sender_commitment = Commitment::new(&sender_keys.npk(), sender_private_account);
let sender_pre = AccountWithMetadata { let sender_pre =
account: sender_private_account.clone(), AccountWithMetadata::new(sender_private_account.clone(), true, &sender_keys.npk());
is_authorized: true, let recipient_pre = AccountWithMetadata::new(
fingerprint: sender_keys.npk().into(), state.get_account_by_address(recipient_address),
}; false,
let recipient_pre = AccountWithMetadata { recipient_address,
account: state.get_account_by_address(recipient_address), );
is_authorized: false,
fingerprint: recipient_address.into(),
};
let esk = [3; 32]; let esk = [3; 32];
let shared_secret = SharedSecretKey::new(&esk, &sender_keys.ivk()); let shared_secret = SharedSecretKey::new(&esk, &sender_keys.ivk());