mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-04 06:13:10 +00:00
add check and test
This commit is contained in:
parent
fea132ef24
commit
8b70da517c
@ -16,7 +16,7 @@ pub struct Account {
|
|||||||
|
|
||||||
/// A fingerprint of the owner of an account. This can be, for example, an `Address` in case the account
|
/// 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.
|
/// is public, or a `NullifierPublicKey` in case the account is private.
|
||||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
|
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
|
||||||
#[cfg_attr(any(feature = "host", test), derive(Debug))]
|
#[cfg_attr(any(feature = "host", test), derive(Debug))]
|
||||||
pub struct AccountId(pub(super) [u8; 32]);
|
pub struct AccountId(pub(super) [u8; 32]);
|
||||||
impl AccountId {
|
impl AccountId {
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use risc0_zkvm::{guest::env, serde::to_vec};
|
use risc0_zkvm::{guest::env, serde::to_vec};
|
||||||
|
|
||||||
use nssa_core::{
|
use nssa_core::{
|
||||||
@ -30,6 +32,11 @@ fn main() {
|
|||||||
post_states,
|
post_states,
|
||||||
} = program_output;
|
} = program_output;
|
||||||
|
|
||||||
|
// Check that there are no repeated account ids
|
||||||
|
if !validate_uniqueness_of_account_ids(&pre_states) {
|
||||||
|
panic!("Repeated account ids found")
|
||||||
|
}
|
||||||
|
|
||||||
// Check that the program is well behaved.
|
// Check that the program is well behaved.
|
||||||
// See the # Programs section for the definition of the `validate_execution` method.
|
// See the # Programs section for the definition of the `validate_execution` method.
|
||||||
if !validate_execution(&pre_states, &post_states, program_id) {
|
if !validate_execution(&pre_states, &post_states, program_id) {
|
||||||
@ -161,3 +168,14 @@ fn main() {
|
|||||||
|
|
||||||
env::commit(&output);
|
env::commit(&output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn validate_uniqueness_of_account_ids(pre_states: &[AccountWithMetadata]) -> bool {
|
||||||
|
let number_of_accounts = pre_states.len();
|
||||||
|
let number_of_account_ids = pre_states
|
||||||
|
.iter()
|
||||||
|
.map(|account| account.account_id.clone())
|
||||||
|
.collect::<HashSet<_>>()
|
||||||
|
.len();
|
||||||
|
|
||||||
|
number_of_accounts == number_of_account_ids
|
||||||
|
}
|
||||||
|
|||||||
@ -1397,10 +1397,10 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 =
|
let private_account_2 =
|
||||||
AccountWithMetadata::new(Account::default(), false, AccountId::new([1; 32]));
|
AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk());
|
||||||
|
|
||||||
// Setting only one nonce for an execution with two private accounts.
|
// Setting only one nonce for an execution with two private accounts.
|
||||||
let private_account_nonces = [0xdeadbeef1];
|
let private_account_nonces = [0xdeadbeef1];
|
||||||
@ -1437,7 +1437,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 =
|
let private_account_2 =
|
||||||
AccountWithMetadata::new(Account::default(), false, AccountId::new([1; 32]));
|
AccountWithMetadata::new(Account::default(), false, AccountId::new([1; 32]));
|
||||||
@ -1472,10 +1472,10 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 =
|
let private_account_2 =
|
||||||
AccountWithMetadata::new(Account::default(), false, AccountId::new([1; 32]));
|
AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk());
|
||||||
|
|
||||||
// Setting no auth key for an execution with one non default private accounts.
|
// Setting no auth key for an execution with one non default private accounts.
|
||||||
let private_account_auth = [];
|
let private_account_auth = [];
|
||||||
@ -1513,10 +1513,10 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 =
|
let private_account_2 =
|
||||||
AccountWithMetadata::new(Account::default(), false, AccountId::new([1; 32]));
|
AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk());
|
||||||
|
|
||||||
let private_account_keys = [
|
let private_account_keys = [
|
||||||
// First private account is the sender
|
// First private account is the sender
|
||||||
@ -1561,7 +1561,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 = AccountWithMetadata::new(
|
let private_account_2 = AccountWithMetadata::new(
|
||||||
Account {
|
Account {
|
||||||
@ -1570,7 +1570,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
AccountId::new([1; 32]),
|
&recipient_keys.npk(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let result = execute_and_prove(
|
let result = execute_and_prove(
|
||||||
@ -1608,7 +1608,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 = AccountWithMetadata::new(
|
let private_account_2 = AccountWithMetadata::new(
|
||||||
Account {
|
Account {
|
||||||
@ -1617,7 +1617,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
AccountId::new([1; 32]),
|
&recipient_keys.npk(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let result = execute_and_prove(
|
let result = execute_and_prove(
|
||||||
@ -1654,7 +1654,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 = AccountWithMetadata::new(
|
let private_account_2 = AccountWithMetadata::new(
|
||||||
Account {
|
Account {
|
||||||
@ -1663,7 +1663,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
AccountId::new([1; 32]),
|
&recipient_keys.npk(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let result = execute_and_prove(
|
let result = execute_and_prove(
|
||||||
@ -1700,7 +1700,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 = AccountWithMetadata::new(
|
let private_account_2 = AccountWithMetadata::new(
|
||||||
Account {
|
Account {
|
||||||
@ -1709,7 +1709,7 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
AccountId::new([1; 32]),
|
&recipient_keys.npk(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let result = execute_and_prove(
|
let result = execute_and_prove(
|
||||||
@ -1747,13 +1747,13 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 = AccountWithMetadata::new(
|
let private_account_2 = AccountWithMetadata::new(
|
||||||
Account::default(),
|
Account::default(),
|
||||||
// This should be set to false in normal circumstances
|
// This should be set to false in normal circumstances
|
||||||
true,
|
true,
|
||||||
AccountId::new([1; 32]),
|
&recipient_keys.npk(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let result = execute_and_prove(
|
let result = execute_and_prove(
|
||||||
@ -1819,10 +1819,10 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 =
|
let private_account_2 =
|
||||||
AccountWithMetadata::new(Account::default(), false, AccountId::new([1; 32]));
|
AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk());
|
||||||
|
|
||||||
// Setting three new private account nonces for a circuit execution with only two private
|
// Setting three new private account nonces for a circuit execution with only two private
|
||||||
// accounts.
|
// accounts.
|
||||||
@ -1861,10 +1861,10 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 =
|
let private_account_2 =
|
||||||
AccountWithMetadata::new(Account::default(), false, AccountId::new([1; 32]));
|
AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk());
|
||||||
|
|
||||||
// Setting three private account keys for a circuit execution with only two private
|
// Setting three private account keys for a circuit execution with only two private
|
||||||
// accounts.
|
// accounts.
|
||||||
@ -1907,10 +1907,10 @@ pub mod tests {
|
|||||||
..Account::default()
|
..Account::default()
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
AccountId::new([0; 32]),
|
&sender_keys.npk(),
|
||||||
);
|
);
|
||||||
let private_account_2 =
|
let private_account_2 =
|
||||||
AccountWithMetadata::new(Account::default(), false, AccountId::new([1; 32]));
|
AccountWithMetadata::new(Account::default(), false, &recipient_keys.npk());
|
||||||
|
|
||||||
// Setting two private account keys for a circuit execution with only one non default
|
// Setting two private account keys for a circuit execution with only one non default
|
||||||
// private account (visibility mask equal to 1 means that auth keys are expected).
|
// private account (visibility mask equal to 1 means that auth keys are expected).
|
||||||
@ -1940,4 +1940,40 @@ pub mod tests {
|
|||||||
|
|
||||||
assert!(matches!(result, Err(NssaError::CircuitProvingError(_))));
|
assert!(matches!(result, Err(NssaError::CircuitProvingError(_))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_circuit_should_fail_if_there_are_repeated_ids() {
|
||||||
|
let program = Program::simple_balance_transfer();
|
||||||
|
let sender_keys = test_private_account_keys_1();
|
||||||
|
let private_account_1 = AccountWithMetadata::new(
|
||||||
|
Account {
|
||||||
|
program_owner: program.id(),
|
||||||
|
balance: 100,
|
||||||
|
..Account::default()
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
&sender_keys.npk(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let visibility_mask = [1, 1];
|
||||||
|
let private_account_auth = [
|
||||||
|
(sender_keys.nsk, (1, vec![])),
|
||||||
|
(sender_keys.nsk, (1, vec![])),
|
||||||
|
];
|
||||||
|
let shared_secret = SharedSecretKey::new(&[55; 32], &sender_keys.ivk());
|
||||||
|
let result = execute_and_prove(
|
||||||
|
&[private_account_1.clone(), private_account_1],
|
||||||
|
&Program::serialize_instruction(100u128).unwrap(),
|
||||||
|
&visibility_mask,
|
||||||
|
&[0xdeadbeef1, 0xdeadbeef2],
|
||||||
|
&[
|
||||||
|
(sender_keys.npk(), shared_secret.clone()),
|
||||||
|
(sender_keys.npk(), shared_secret),
|
||||||
|
],
|
||||||
|
&private_account_auth,
|
||||||
|
&program,
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(matches!(result, Err(NssaError::CircuitProvingError(_))));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user