add type aliases

This commit is contained in:
Sergio Chouhy 2026-03-28 03:13:46 -03:00
parent fba95ca2a8
commit b92dcbb711
8 changed files with 45 additions and 41 deletions

View File

@ -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<Ciphertext>,
pub new_commitments: Vec<Commitment>,
pub new_nullifiers: Vec<(Nullifier, CommitmentSetDigest)>,
pub block_validity_window: ValidityWindow<BlockId>,
pub timestamp_validity_window: ValidityWindow<Timestamp>,
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();

View File

@ -157,6 +157,9 @@ pub type BlockId = u64;
/// Unix timestamp in milliseconds.
pub type Timestamp = u64;
pub type BlockValidityWindow = ValidityWindow<BlockId>;
pub type TimestampValidityWindow = ValidityWindow<Timestamp>;
#[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<ChainedCall>,
/// The block ID window where the program output is valid.
pub block_validity_window: ValidityWindow<BlockId>,
pub block_validity_window: BlockValidityWindow,
/// The timestamp window where the program output is valid.
pub timestamp_validity_window: ValidityWindow<Timestamp>,
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<W: Into<ValidityWindow<BlockId>>>(mut self, window: W) -> Self {
pub fn with_block_validity_window<W: Into<BlockValidityWindow>>(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<W: TryInto<ValidityWindow<BlockId>, Error = InvalidWindow>>(
pub fn try_with_block_validity_window<W: TryInto<BlockValidityWindow, Error = InvalidWindow>>(
mut self,
window: W,
) -> Result<Self, InvalidWindow> {
@ -318,14 +321,14 @@ impl ProgramOutput {
}
/// Sets the timestamp validity window from an infallible range conversion.
pub fn with_timestamp_validity_window<W: Into<ValidityWindow<Timestamp>>>(mut self, window: W) -> Self {
pub fn with_timestamp_validity_window<W: Into<TimestampValidityWindow>>(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<W: TryInto<ValidityWindow<Timestamp>, Error = InvalidWindow>>(
pub fn try_with_timestamp_validity_window<W: TryInto<TimestampValidityWindow, Error = InvalidWindow>>(
mut self,
window: W,
) -> Result<Self, InvalidWindow> {

View File

@ -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<EncryptedAccountData>,
pub new_commitments: Vec<Commitment>,
pub new_nullifiers: Vec<(Nullifier, CommitmentSetDigest)>,
pub block_validity_window: ValidityWindow<BlockId>,
pub timestamp_validity_window: ValidityWindow<Timestamp>,
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(),
}
}

View File

@ -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<BlockId>,
timestamp_validity_window: &ValidityWindow<Timestamp>,
block_validity_window: &BlockValidityWindow,
timestamp_validity_window: &TimestampValidityWindow,
) -> Result<(), NssaError> {
let output = PrivacyPreservingCircuitOutput {
public_pre_states: public_pre_states.to_vec(),

View File

@ -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<BlockId>, Option<BlockId>),
block_id: BlockId,
) {
let block_validity_window: ValidityWindow<BlockId> = 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::<Timestamp>::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<Timestamp>, Option<Timestamp>),
timestamp_ms: Timestamp,
) {
let timestamp_validity_window: ValidityWindow<Timestamp> =
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::<BlockId>::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<BlockId>, Option<BlockId>),
block_id: BlockId,
) {
let block_validity_window: ValidityWindow<BlockId> = 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::<Timestamp>::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<Timestamp>, Option<Timestamp>),
timestamp_ms: Timestamp,
) {
let timestamp_validity_window: ValidityWindow<Timestamp> =
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::<BlockId>::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(),

View File

@ -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<AccountWithMetadata>,
post_states: HashMap<AccountId, Account>,
block_validity_window: ValidityWindow<BlockId>,
timestamp_validity_window: ValidityWindow<Timestamp>,
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<BlockId> = (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<Timestamp> =
let timestamp_validity_window: TimestampValidityWindow =
(ts_valid_from, ts_valid_until)
.try_into()
.expect(

View File

@ -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<BlockId>, ValidityWindow<Timestamp>);
type Instruction = (BlockValidityWindow, TimestampValidityWindow);
fn main() {
let (

View File

@ -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<BlockId>, ValidityWindow<Timestamp>)`
/// The chained program (validity_window) expects `(BlockValidityWindow, TimestampValidityWindow)`
/// so an unbounded timestamp window is appended automatically.
type Instruction = (ValidityWindow<BlockId>, ProgramId, ValidityWindow<BlockId>);
type Instruction = (BlockValidityWindow, ProgramId, BlockValidityWindow);
fn main() {
let (
@ -27,7 +27,7 @@ fn main() {
let chained_instruction = to_vec(&(
chained_block_validity_window,
ValidityWindow::<Timestamp>::new_unbounded(),
TimestampValidityWindow::new_unbounded(),
))
.unwrap();
let chained_call = ChainedCall {