110 lines
4.0 KiB
Rust
Raw Normal View History

2025-08-26 14:53:02 -03:00
use serde::{Deserialize, Serialize};
use crate::{
Commitment, CommitmentSetDigest, MembershipProof, Nullifier, NullifierPublicKey,
NullifierSecretKey, SharedSecretKey,
account::{Account, AccountWithMetadata, Nonce},
encryption::Ciphertext,
program::{ProgramId, ProgramOutput},
};
#[derive(Serialize, Deserialize)]
pub struct PrivacyPreservingCircuitInput {
/// Outputs of the program execution.
2025-11-06 19:35:47 -03:00
pub program_outputs: Vec<ProgramOutput>,
/// Visibility mask for accounts.
///
/// - `0` - public account
/// - `1` - private account with authentication
/// - `2` - private account without authentication
2025-08-26 14:53:02 -03:00
pub visibility_mask: Vec<u8>,
/// Nonces of private accounts.
2025-08-26 14:53:02 -03:00
pub private_account_nonces: Vec<Nonce>,
/// Public keys of private accounts.
2025-08-26 14:53:02 -03:00
pub private_account_keys: Vec<(NullifierPublicKey, SharedSecretKey)>,
/// Nullifier secret keys for authorized private accounts.
2025-11-14 01:28:34 -03:00
pub private_account_nsks: Vec<NullifierSecretKey>,
/// Membership proofs for private accounts. Can be [`None`] for uninitialized accounts.
pub private_account_membership_proofs: Vec<Option<MembershipProof>>,
/// Program ID.
2025-08-26 14:53:02 -03:00
pub program_id: ProgramId,
}
#[derive(Serialize, Deserialize)]
#[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))]
pub struct PrivacyPreservingCircuitOutput {
pub public_pre_states: Vec<AccountWithMetadata>,
pub public_post_states: Vec<Account>,
pub ciphertexts: Vec<Ciphertext>,
pub new_commitments: Vec<Commitment>,
pub new_nullifiers: Vec<(Nullifier, CommitmentSetDigest)>,
}
#[cfg(feature = "host")]
impl PrivacyPreservingCircuitOutput {
pub fn to_bytes(&self) -> Vec<u8> {
bytemuck::cast_slice(&risc0_zkvm::serde::to_vec(&self).unwrap()).to_vec()
}
}
2025-08-27 16:24:20 -03:00
#[cfg(feature = "host")]
2025-08-26 14:53:02 -03:00
#[cfg(test)]
mod tests {
2025-11-26 00:27:20 +03:00
use risc0_zkvm::serde::from_slice;
2025-08-26 14:53:02 -03:00
use super::*;
use crate::{
2025-09-11 15:49:54 -03:00
Commitment, Nullifier, NullifierPublicKey,
2025-09-12 09:39:08 -03:00
account::{Account, AccountId, AccountWithMetadata},
2025-08-26 14:53:02 -03:00
};
#[test]
fn test_privacy_preserving_circuit_output_to_bytes_is_compatible_with_from_slice() {
let output = PrivacyPreservingCircuitOutput {
public_pre_states: vec![
2025-09-11 16:37:28 -03:00
AccountWithMetadata::new(
Account {
2025-08-26 14:53:02 -03:00
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
balance: 12345678901234567890,
data: b"test data".to_vec().try_into().unwrap(),
2025-08-26 14:53:02 -03:00
nonce: 18446744073709551614,
},
2025-09-11 16:37:28 -03:00
true,
2025-09-12 09:18:40 -03:00
AccountId::new([0; 32]),
2025-09-11 16:37:28 -03:00
),
AccountWithMetadata::new(
Account {
2025-08-26 14:53:02 -03:00
program_owner: [9, 9, 9, 8, 8, 8, 7, 7],
balance: 123123123456456567112,
data: b"test data".to_vec().try_into().unwrap(),
2025-08-26 14:53:02 -03:00
nonce: 9999999999999999999999,
},
2025-09-11 16:37:28 -03:00
false,
2025-09-12 09:18:40 -03:00
AccountId::new([1; 32]),
2025-09-11 16:37:28 -03:00
),
2025-08-26 14:53:02 -03:00
],
public_post_states: vec![Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
balance: 100,
data: b"post state data".to_vec().try_into().unwrap(),
2025-08-26 14:53:02 -03:00
nonce: 18446744073709551615,
}],
ciphertexts: vec![Ciphertext(vec![255, 255, 1, 1, 2, 2])],
new_commitments: vec![Commitment::new(
&NullifierPublicKey::from(&[1; 32]),
&Account::default(),
)],
new_nullifiers: vec![(
Nullifier::for_account_update(
2025-08-26 14:53:02 -03:00
&Commitment::new(&NullifierPublicKey::from(&[2; 32]), &Account::default()),
&[1; 32],
),
[0xab; 32],
)],
};
let bytes = output.to_bytes();
let output_from_slice: PrivacyPreservingCircuitOutput = from_slice(&bytes).unwrap();
assert_eq!(output, output_from_slice);
}
}