diff --git a/nssa/core/src/circuit_io.rs b/nssa/core/src/circuit_io.rs index dc6e7723..eeaf2fa0 100644 --- a/nssa/core/src/circuit_io.rs +++ b/nssa/core/src/circuit_io.rs @@ -5,7 +5,7 @@ use crate::{ NullifierSecretKey, SharedSecretKey, account::{Account, AccountWithMetadata}, encryption::Ciphertext, - program::{BlockId, ProgramId, ProgramOutput, Timestamp, ValidityWindow}, + program::{BlockValidityWindow, ProgramId, ProgramOutput, TimestampValidityWindow}, }; #[derive(Serialize, Deserialize)] @@ -36,8 +36,8 @@ pub struct PrivacyPreservingCircuitOutput { pub ciphertexts: Vec, pub new_commitments: Vec, pub new_nullifiers: Vec<(Nullifier, CommitmentSetDigest)>, - pub block_validity_window: ValidityWindow, - pub timestamp_validity_window: ValidityWindow, + pub block_validity_window: BlockValidityWindow, + pub timestamp_validity_window: TimestampValidityWindow, } #[cfg(feature = "host")] @@ -104,7 +104,7 @@ mod tests { [0xab; 32], )], block_validity_window: (Some(1u64), None).try_into().unwrap(), - timestamp_validity_window: ValidityWindow::new_unbounded(), + timestamp_validity_window: TimestampValidityWindow::new_unbounded(), }; let bytes = output.to_bytes(); let output_from_slice: PrivacyPreservingCircuitOutput = from_slice(&bytes).unwrap(); diff --git a/nssa/core/src/program.rs b/nssa/core/src/program.rs index f60ec9bf..a7fe9f07 100644 --- a/nssa/core/src/program.rs +++ b/nssa/core/src/program.rs @@ -157,6 +157,9 @@ pub type BlockId = u64; /// Unix timestamp in milliseconds. pub type Timestamp = u64; +pub type BlockValidityWindow = ValidityWindow; +pub type TimestampValidityWindow = ValidityWindow; + #[derive(Clone, Copy, Serialize, Deserialize)] #[cfg_attr( any(feature = "host", test), @@ -271,9 +274,9 @@ pub struct ProgramOutput { /// The list of chained calls to other programs. pub chained_calls: Vec, /// The block ID window where the program output is valid. - pub block_validity_window: ValidityWindow, + pub block_validity_window: BlockValidityWindow, /// The timestamp window where the program output is valid. - pub timestamp_validity_window: ValidityWindow, + pub timestamp_validity_window: TimestampValidityWindow, } impl ProgramOutput { @@ -302,14 +305,14 @@ impl ProgramOutput { } /// Sets the block ID validity window from an infallible range conversion (`1..`, `..5`, `..`). - pub fn with_block_validity_window>>(mut self, window: W) -> Self { + pub fn with_block_validity_window>(mut self, window: W) -> Self { self.block_validity_window = window.into(); self } /// Sets the block ID validity window from a fallible range conversion (`1..5`). /// Returns `Err` if the range is empty. - pub fn try_with_block_validity_window, Error = InvalidWindow>>( + pub fn try_with_block_validity_window>( mut self, window: W, ) -> Result { @@ -318,14 +321,14 @@ impl ProgramOutput { } /// Sets the timestamp validity window from an infallible range conversion. - pub fn with_timestamp_validity_window>>(mut self, window: W) -> Self { + pub fn with_timestamp_validity_window>(mut self, window: W) -> Self { self.timestamp_validity_window = window.into(); self } /// Sets the timestamp validity window from a fallible range conversion. /// Returns `Err` if the range is empty. - pub fn try_with_timestamp_validity_window, Error = InvalidWindow>>( + pub fn try_with_timestamp_validity_window>( mut self, window: W, ) -> Result { diff --git a/nssa/src/privacy_preserving_transaction/message.rs b/nssa/src/privacy_preserving_transaction/message.rs index b59cdcd3..85f4a202 100644 --- a/nssa/src/privacy_preserving_transaction/message.rs +++ b/nssa/src/privacy_preserving_transaction/message.rs @@ -3,7 +3,7 @@ use nssa_core::{ Commitment, CommitmentSetDigest, Nullifier, NullifierPublicKey, PrivacyPreservingCircuitOutput, account::{Account, Nonce}, encryption::{Ciphertext, EphemeralPublicKey, ViewingPublicKey}, - program::{BlockId, Timestamp, ValidityWindow}, + program::{BlockValidityWindow, TimestampValidityWindow}, }; use sha2::{Digest as _, Sha256}; @@ -53,8 +53,8 @@ pub struct Message { pub encrypted_private_post_states: Vec, pub new_commitments: Vec, pub new_nullifiers: Vec<(Nullifier, CommitmentSetDigest)>, - pub block_validity_window: ValidityWindow, - pub timestamp_validity_window: ValidityWindow, + pub block_validity_window: BlockValidityWindow, + pub timestamp_validity_window: TimestampValidityWindow, } impl std::fmt::Debug for Message { @@ -126,7 +126,7 @@ pub mod tests { Commitment, EncryptionScheme, Nullifier, NullifierPublicKey, SharedSecretKey, account::Account, encryption::{EphemeralPublicKey, ViewingPublicKey}, - program::ValidityWindow, + program::{BlockValidityWindow, TimestampValidityWindow}, }; use sha2::{Digest as _, Sha256}; @@ -169,8 +169,8 @@ pub mod tests { encrypted_private_post_states, new_commitments, new_nullifiers, - block_validity_window: ValidityWindow::new_unbounded(), - timestamp_validity_window: ValidityWindow::new_unbounded(), + block_validity_window: BlockValidityWindow::new_unbounded(), + timestamp_validity_window: TimestampValidityWindow::new_unbounded(), } } diff --git a/nssa/src/privacy_preserving_transaction/transaction.rs b/nssa/src/privacy_preserving_transaction/transaction.rs index 2baff0c5..2fbd08fc 100644 --- a/nssa/src/privacy_preserving_transaction/transaction.rs +++ b/nssa/src/privacy_preserving_transaction/transaction.rs @@ -7,7 +7,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; use nssa_core::{ Commitment, CommitmentSetDigest, Nullifier, PrivacyPreservingCircuitOutput, account::{Account, AccountWithMetadata}, - program::{BlockId, Timestamp, ValidityWindow}, + program::{BlockId, BlockValidityWindow, Timestamp, TimestampValidityWindow}, }; use sha2::{Digest as _, digest::FixedOutput as _}; @@ -185,8 +185,8 @@ fn check_privacy_preserving_circuit_proof_is_valid( encrypted_private_post_states: &[EncryptedAccountData], new_commitments: &[Commitment], new_nullifiers: &[(Nullifier, CommitmentSetDigest)], - block_validity_window: &ValidityWindow, - timestamp_validity_window: &ValidityWindow, + block_validity_window: &BlockValidityWindow, + timestamp_validity_window: &TimestampValidityWindow, ) -> Result<(), NssaError> { let output = PrivacyPreservingCircuitOutput { public_pre_states: public_pre_states.to_vec(), diff --git a/nssa/src/state.rs b/nssa/src/state.rs index 5bf3cb99..97da2af7 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -344,7 +344,7 @@ pub mod tests { Commitment, Nullifier, NullifierPublicKey, NullifierSecretKey, SharedSecretKey, account::{Account, AccountId, AccountWithMetadata, Nonce, data::Data}, encryption::{EphemeralPublicKey, Scalar, ViewingPublicKey}, - program::{BlockId, PdaSeed, ProgramId, Timestamp, ValidityWindow}, + program::{BlockId, BlockValidityWindow, PdaSeed, ProgramId, Timestamp, TimestampValidityWindow}, }; use crate::{ @@ -3021,7 +3021,7 @@ pub mod tests { validity_window: (Option, Option), block_id: BlockId, ) { - let block_validity_window: ValidityWindow = validity_window.try_into().unwrap(); + let block_validity_window: BlockValidityWindow = validity_window.try_into().unwrap(); let validity_window_program = Program::validity_window(); let account_keys = test_public_account_keys_1(); let pre = AccountWithMetadata::new(Account::default(), false, account_keys.account_id()); @@ -3030,7 +3030,7 @@ pub mod tests { let account_ids = vec![pre.account_id]; let nonces = vec![]; let program_id = validity_window_program.id(); - let instruction = (block_validity_window, ValidityWindow::::new_unbounded()); + let instruction = (block_validity_window, TimestampValidityWindow::new_unbounded()); let message = public_transaction::Message::try_new(program_id, account_ids, nonces, instruction) .unwrap(); @@ -3068,7 +3068,7 @@ pub mod tests { validity_window: (Option, Option), timestamp_ms: Timestamp, ) { - let timestamp_validity_window: ValidityWindow = + let timestamp_validity_window: TimestampValidityWindow = validity_window.try_into().unwrap(); let validity_window_program = Program::validity_window(); let account_keys = test_public_account_keys_1(); @@ -3079,7 +3079,7 @@ pub mod tests { let nonces = vec![]; let program_id = validity_window_program.id(); let instruction = - (ValidityWindow::::new_unbounded(), timestamp_validity_window); + (BlockValidityWindow::new_unbounded(), timestamp_validity_window); let message = public_transaction::Message::try_new(program_id, account_ids, nonces, instruction) .unwrap(); @@ -3118,7 +3118,7 @@ pub mod tests { validity_window: (Option, Option), block_id: BlockId, ) { - let block_validity_window: ValidityWindow = validity_window.try_into().unwrap(); + let block_validity_window: BlockValidityWindow = validity_window.try_into().unwrap(); let validity_window_program = Program::validity_window(); let account_keys = test_private_account_keys_1(); let pre = AccountWithMetadata::new(Account::default(), false, &account_keys.npk()); @@ -3128,7 +3128,7 @@ pub mod tests { let shared_secret = SharedSecretKey::new(&esk, &account_keys.vpk()); let epk = EphemeralPublicKey::from_scalar(esk); - let instruction = (block_validity_window, ValidityWindow::::new_unbounded()); + let instruction = (block_validity_window, TimestampValidityWindow::new_unbounded()); let (output, proof) = circuit::execute_and_prove( vec![pre], Program::serialize_instruction(instruction).unwrap(), @@ -3183,7 +3183,7 @@ pub mod tests { validity_window: (Option, Option), timestamp_ms: Timestamp, ) { - let timestamp_validity_window: ValidityWindow = + let timestamp_validity_window: TimestampValidityWindow = validity_window.try_into().unwrap(); let validity_window_program = Program::validity_window(); let account_keys = test_private_account_keys_1(); @@ -3195,7 +3195,7 @@ pub mod tests { let epk = EphemeralPublicKey::from_scalar(esk); let instruction = - (ValidityWindow::::new_unbounded(), timestamp_validity_window); + (BlockValidityWindow::new_unbounded(), timestamp_validity_window); let (output, proof) = circuit::execute_and_prove( vec![pre], Program::serialize_instruction(instruction).unwrap(), diff --git a/program_methods/guest/src/bin/privacy_preserving_circuit.rs b/program_methods/guest/src/bin/privacy_preserving_circuit.rs index 3e43410b..51910bde 100644 --- a/program_methods/guest/src/bin/privacy_preserving_circuit.rs +++ b/program_methods/guest/src/bin/privacy_preserving_circuit.rs @@ -10,8 +10,9 @@ use nssa_core::{ account::{Account, AccountId, AccountWithMetadata, Nonce}, compute_digest_for_path, program::{ - AccountPostState, BlockId, ChainedCall, DEFAULT_PROGRAM_ID, MAX_NUMBER_CHAINED_CALLS, - ProgramId, ProgramOutput, Timestamp, ValidityWindow, validate_execution, + AccountPostState, BlockValidityWindow, ChainedCall, DEFAULT_PROGRAM_ID, + MAX_NUMBER_CHAINED_CALLS, ProgramId, ProgramOutput, TimestampValidityWindow, + validate_execution, }, }; use risc0_zkvm::{guest::env, serde::to_vec}; @@ -20,8 +21,8 @@ use risc0_zkvm::{guest::env, serde::to_vec}; struct ExecutionState { pre_states: Vec, post_states: HashMap, - block_validity_window: ValidityWindow, - timestamp_validity_window: ValidityWindow, + block_validity_window: BlockValidityWindow, + timestamp_validity_window: TimestampValidityWindow, } impl ExecutionState { @@ -44,12 +45,12 @@ impl ExecutionState { .filter_map(|output| output.timestamp_validity_window.end()) .min(); - let block_validity_window: ValidityWindow = (block_valid_from, block_valid_until) + let block_validity_window: BlockValidityWindow = (block_valid_from, block_valid_until) .try_into() .expect( "There should be non empty intersection in the program output block validity windows", ); - let timestamp_validity_window: ValidityWindow = + let timestamp_validity_window: TimestampValidityWindow = (ts_valid_from, ts_valid_until) .try_into() .expect( diff --git a/test_program_methods/guest/src/bin/validity_window.rs b/test_program_methods/guest/src/bin/validity_window.rs index 71237055..a0ff9f36 100644 --- a/test_program_methods/guest/src/bin/validity_window.rs +++ b/test_program_methods/guest/src/bin/validity_window.rs @@ -1,9 +1,9 @@ use nssa_core::program::{ - AccountPostState, BlockId, ProgramInput, ProgramOutput, Timestamp, ValidityWindow, + AccountPostState, BlockValidityWindow, ProgramInput, ProgramOutput, TimestampValidityWindow, read_nssa_inputs, }; -type Instruction = (ValidityWindow, ValidityWindow); +type Instruction = (BlockValidityWindow, TimestampValidityWindow); fn main() { let ( diff --git a/test_program_methods/guest/src/bin/validity_window_chain_caller.rs b/test_program_methods/guest/src/bin/validity_window_chain_caller.rs index 1a4a1d2c..9acce64b 100644 --- a/test_program_methods/guest/src/bin/validity_window_chain_caller.rs +++ b/test_program_methods/guest/src/bin/validity_window_chain_caller.rs @@ -1,6 +1,6 @@ use nssa_core::program::{ - AccountPostState, BlockId, ChainedCall, ProgramId, ProgramInput, ProgramOutput, Timestamp, - ValidityWindow, read_nssa_inputs, + AccountPostState, BlockValidityWindow, ChainedCall, ProgramId, ProgramInput, ProgramOutput, + TimestampValidityWindow, read_nssa_inputs, }; use risc0_zkvm::serde::to_vec; @@ -9,9 +9,9 @@ use risc0_zkvm::serde::to_vec; /// /// Instruction: (`window`, `chained_program_id`, `chained_window`) /// The initial output uses `window` and chains to `chained_program_id` with `chained_window`. -/// The chained program (validity_window) expects `(ValidityWindow, ValidityWindow)` +/// The chained program (validity_window) expects `(BlockValidityWindow, TimestampValidityWindow)` /// so an unbounded timestamp window is appended automatically. -type Instruction = (ValidityWindow, ProgramId, ValidityWindow); +type Instruction = (BlockValidityWindow, ProgramId, BlockValidityWindow); fn main() { let ( @@ -27,7 +27,7 @@ fn main() { let chained_instruction = to_vec(&( chained_block_validity_window, - ValidityWindow::::new_unbounded(), + TimestampValidityWindow::new_unbounded(), )) .unwrap(); let chained_call = ChainedCall {