diff --git a/benches/circuit.rs b/benches/circuit.rs index c0fe2f354..61053707f 100644 --- a/benches/circuit.rs +++ b/benches/circuit.rs @@ -12,7 +12,7 @@ use orchard::{ bundle::Flags, circuit::{ProvingKey, VerifyingKey}, keys::{FullViewingKey, Scope, SpendingKey}, - orchard_flavor, + orchard_flavor::OrchardZSA, value::NoteValue, Anchor, Bundle, }; @@ -24,9 +24,9 @@ fn criterion_benchmark(c: &mut Criterion) { let sk = SpendingKey::from_bytes([7; 32]).unwrap(); let recipient = FullViewingKey::from(&sk).address_at(0u32, Scope::External); - // FIXME: consider adding test for orchard_flavor::OrchardVanilla as well - let vk = VerifyingKey::build::(); - let pk = ProvingKey::build::(); + // FIXME: consider adding test for OrchardVanilla as well + let vk = VerifyingKey::build::(); + let pk = ProvingKey::build::(); let create_bundle = |num_recipients| { let mut builder = Builder::new( @@ -44,7 +44,7 @@ fn criterion_benchmark(c: &mut Criterion) { ) .unwrap(); } - let bundle: Bundle<_, i64, orchard_flavor::OrchardZSA> = builder.build(rng).unwrap(); + let bundle: Bundle<_, i64, OrchardZSA> = builder.build(rng).unwrap(); let instances: Vec<_> = bundle .actions() diff --git a/benches/note_decryption.rs b/benches/note_decryption.rs index 6c21493a5..0a625ef0d 100644 --- a/benches/note_decryption.rs +++ b/benches/note_decryption.rs @@ -5,8 +5,8 @@ use orchard::{ circuit::ProvingKey, keys::{FullViewingKey, PreparedIncomingViewingKey, Scope, SpendingKey}, note::AssetBase, - note_encryption::{action::CompactAction, OrchardDomainContext}, - orchard_flavor, + note_encryption::{action::CompactAction, OrchardDomainBase}, + orchard_flavor::OrchardZSA, value::NoteValue, Anchor, Bundle, }; @@ -16,12 +16,12 @@ use zcash_note_encryption_zsa::{batch, try_compact_note_decryption, try_note_dec #[cfg(unix)] use pprof::criterion::{Output, PProfProfiler}; -type OrchardZSA = OrchardDomainContext; +type OrchardDomainZSA = OrchardDomainBase; fn bench_note_decryption(c: &mut Criterion) { let rng = OsRng; - // FIXME: consider adding test for orchard_flavor::OrchardVanilla as well - let pk = ProvingKey::build::(); + // FIXME: consider adding test for OrchardVanilla as well + let pk = ProvingKey::build::(); let fvk = FullViewingKey::from(&SpendingKey::from_bytes([7; 32]).unwrap()); let valid_ivk = fvk.to_ivk(Scope::External); @@ -74,7 +74,7 @@ fn bench_note_decryption(c: &mut Criterion) { None, ) .unwrap(); - let bundle: Bundle<_, i64, orchard_flavor::OrchardZSA> = builder.build(rng).unwrap(); + let bundle: Bundle<_, i64, OrchardZSA> = builder.build(rng).unwrap(); bundle .create_proof(&pk, rng) .unwrap() @@ -83,7 +83,7 @@ fn bench_note_decryption(c: &mut Criterion) { }; let action = bundle.actions().first(); - let domain = OrchardZSA::for_action(action); + let domain = OrchardDomainZSA::for_action(action); let compact = { let mut group = c.benchmark_group("note-decryption"); @@ -124,10 +124,15 @@ fn bench_note_decryption(c: &mut Criterion) { let ivks = 2; let valid_ivks = vec![valid_ivk; ivks]; let actions: Vec<_> = (0..100) - .map(|_| (OrchardZSA::for_action(action), action.clone())) + .map(|_| (OrchardDomainZSA::for_action(action), action.clone())) .collect(); let compact: Vec<_> = (0..100) - .map(|_| (OrchardZSA::for_action(action), CompactAction::from(action))) + .map(|_| { + ( + OrchardDomainZSA::for_action(action), + CompactAction::from(action), + ) + }) .collect(); let mut group = c.benchmark_group("batch-note-decryption"); diff --git a/src/builder.rs b/src/builder.rs index a51e80c33..2db61a6b2 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -16,13 +16,13 @@ use crate::{ action::Action, address::Address, bundle::{Authorization, Authorized, Bundle, Flags}, - circuit::{Circuit, Instance, OrchardCircuit, Proof, ProvingKey}, + circuit::{Instance, OrchardCircuit, OrchardCircuitBase, Proof, ProvingKey}, keys::{ FullViewingKey, OutgoingViewingKey, Scope, SpendAuthorizingKey, SpendValidatingKey, SpendingKey, }, note::{AssetBase, Note, TransmittedNoteCiphertext}, - note_encryption::{OrchardDomain, OrchardDomainContext}, + note_encryption::{OrchardDomain, OrchardDomainBase}, primitives::redpallas::{self, Binding, SpendAuth}, tree::{Anchor, MerklePath}, value::{self, NoteValue, OverflowError, ValueCommitTrapdoor, ValueCommitment, ValueSum}, @@ -256,7 +256,7 @@ impl ActionInfo { fn build( self, mut rng: impl RngCore, - ) -> (Action, Circuit) { + ) -> (Action, OrchardCircuitBase) { assert_eq!( self.spend.note.asset(), self.output.asset, @@ -282,7 +282,7 @@ impl ActionInfo { let cm_new = note.commitment(); let cmx = cm_new.into(); - let encryptor = NoteEncryption::>::new( + let encryptor = NoteEncryption::>::new( self.output.ovk, note, self.output.memo.unwrap_or_else(|| { @@ -310,7 +310,9 @@ impl ActionInfo { parts: SigningParts { ak, alpha }, }, ), - Circuit::::from_action_context_unchecked(self.spend, note, alpha, self.rcv), + OrchardCircuitBase::::from_action_context_unchecked( + self.spend, note, alpha, self.rcv, + ), ) } } @@ -341,7 +343,7 @@ impl Builder { } // FIXME: fix the doc, this line was removed from the doc: - // [`OrchardDomain`]: crate::note_encryption_zsa::OrchardZSADomain + // [`OrchardDomain`]: crate::note_encryption::OrchardDomain /// Adds a note to be spent in this transaction. /// @@ -644,7 +646,7 @@ impl Authorization for InProgress /// This struct contains the private data needed to create a [`Proof`] for a [`Bundle`]. #[derive(Clone, Debug)] pub struct Unproven { - circuits: Vec>, + circuits: Vec>, } impl InProgress, S> { @@ -1080,7 +1082,7 @@ mod tests { constants::MERKLE_DEPTH_ORCHARD, keys::{FullViewingKey, Scope, SpendingKey}, note::AssetBase, - orchard_flavor, + orchard_flavor::OrchardZSA, tree::EMPTY_ROOTS, value::NoteValue, }; @@ -1089,8 +1091,8 @@ mod tests { #[test] fn shielding_bundle() { - // FIXME: consider adding test for orchard_flavor::OrchardVanilla as well - let pk = ProvingKey::build::(); + // FIXME: consider adding test for OrchardVanilla as well + let pk = ProvingKey::build::(); let mut rng = OsRng; let sk = SpendingKey::random(&mut rng); @@ -1114,7 +1116,7 @@ mod tests { let balance: i64 = builder.value_balance().unwrap(); assert_eq!(balance, -5000); - let bundle: Bundle = builder + let bundle: Bundle = builder .build(&mut rng) .unwrap() .create_proof(&pk, &mut rng) diff --git a/src/bundle.rs b/src/bundle.rs index 7df886665..6a2197396 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -21,7 +21,7 @@ use crate::{ circuit::{Instance, Proof, VerifyingKey}, keys::{IncomingViewingKey, OutgoingViewingKey, PreparedIncomingViewingKey}, note::Note, - note_encryption::{OrchardDomain, OrchardDomainContext}, + note_encryption::{OrchardDomain, OrchardDomainBase}, primitives::redpallas::{self, Binding, SpendAuth}, tree::Anchor, value::{ValueCommitTrapdoor, ValueCommitment, ValueSum}, @@ -331,7 +331,7 @@ impl Bundle { .iter() .enumerate() .filter_map(|(idx, action)| { - let domain = OrchardDomainContext::::for_action(action); + let domain = OrchardDomainBase::::for_action(action); prepared_keys.iter().find_map(|(ivk, prepared_ivk)| { try_note_decryption(&domain, prepared_ivk, action) .map(|(n, a, m)| (idx, (*ivk).clone(), n, a, m)) @@ -351,7 +351,7 @@ impl Bundle { ) -> Option<(Note, Address, [u8; 512])> { let prepared_ivk = PreparedIncomingViewingKey::new(key); self.actions.get(action_idx).and_then(move |action| { - let domain = OrchardDomainContext::::for_action(action); + let domain = OrchardDomainBase::::for_action(action); // let domain = D::for_action(action); try_note_decryption(&domain, &prepared_ivk, action) }) @@ -369,7 +369,7 @@ impl Bundle { .iter() .enumerate() .filter_map(|(idx, action)| { - let domain = OrchardDomainContext::::for_action(action); + let domain = OrchardDomainBase::::for_action(action); keys.iter().find_map(move |key| { try_output_recovery_with_ovk( &domain, @@ -393,7 +393,7 @@ impl Bundle { key: &OutgoingViewingKey, ) -> Option<(Note, Address, [u8; 512])> { self.actions.get(action_idx).and_then(move |action| { - let domain = OrchardDomainContext::::for_action(action); + let domain = OrchardDomainBase::::for_action(action); try_output_recovery_with_ovk( &domain, key, diff --git a/src/circuit.rs b/src/circuit.rs index 213adcf98..335fae1c6 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -63,13 +63,13 @@ pub trait OrchardCircuit: Sized + Default { /// Wrapper for configure function of plonk::Circuit trait fn synthesize( - circuit: &Circuit, + circuit: &OrchardCircuitBase, config: Self::Config, layouter: impl Layouter, ) -> Result<(), plonk::Error>; } -impl plonk::Circuit for Circuit { +impl plonk::Circuit for OrchardCircuitBase { type Config = D::Config; type FloorPlanner = floor_planner::V1; @@ -90,10 +90,9 @@ impl plonk::Circuit for Circuit { } } -// FIXME: rename to CircuitCommon /// The Orchard Action circuit. #[derive(Clone, Debug, Default)] -pub struct Circuit { +pub struct OrchardCircuitBase { pub(crate) path: Value<[MerkleHashOrchard; MERKLE_DEPTH_ORCHARD]>, pub(crate) pos: Value, pub(crate) g_d_old: Value, @@ -119,7 +118,7 @@ pub struct Circuit { phantom: std::marker::PhantomData, } -impl Circuit { +impl OrchardCircuitBase { /// This constructor is public to enable creation of custom builders. /// If you are not creating a custom builder, use [`Builder`] to compose /// and authorize a transaction. @@ -140,7 +139,7 @@ impl Circuit { output_note: Note, alpha: pallas::Scalar, rcv: ValueCommitTrapdoor, - ) -> Option> { + ) -> Option> { (spend.note.nullifier(&spend.fvk) == output_note.rho()) .then(|| Self::from_action_context_unchecked(spend, output_note, alpha, rcv)) } @@ -150,7 +149,7 @@ impl Circuit { output_note: Note, alpha: pallas::Scalar, rcv: ValueCommitTrapdoor, - ) -> Circuit { + ) -> OrchardCircuitBase { let sender_address = spend.note.recipient(); let rho_old = spend.note.rho(); let psi_old = spend.note.rseed().psi(&rho_old); @@ -163,7 +162,7 @@ impl Circuit { let psi_new = output_note.rseed().psi(&rho_new); let rcm_new = output_note.rseed().rcm(&rho_new); - Circuit { + OrchardCircuitBase { path: Value::known(spend.merkle_path.auth_path()), pos: Value::known(spend.merkle_path.position()), g_d_old: Value::known(sender_address.g_d()), @@ -202,7 +201,7 @@ impl VerifyingKey { /// Builds the verifying key. pub fn build() -> Self { let params = halo2_proofs::poly::commitment::Params::new(K); - let circuit: Circuit = Default::default(); + let circuit: OrchardCircuitBase = Default::default(); let vk = plonk::keygen_vk(¶ms, &circuit).unwrap(); @@ -221,7 +220,7 @@ impl ProvingKey { /// Builds the proving key. pub fn build() -> Self { let params = halo2_proofs::poly::commitment::Params::new(K); - let circuit: Circuit = Default::default(); + let circuit: OrchardCircuitBase = Default::default(); let vk = plonk::keygen_vk(¶ms, &circuit).unwrap(); let pk = plonk::keygen_pk(¶ms, vk, &circuit).unwrap(); @@ -337,7 +336,7 @@ impl Proof { /// Creates a proof for the given circuits and instances. pub fn create( pk: &ProvingKey, - circuits: &[Circuit], + circuits: &[OrchardCircuitBase], instances: &[Instance], mut rng: impl RngCore, ) -> Result { diff --git a/src/circuit/circuit_vanilla.rs b/src/circuit/circuit_vanilla.rs index 41cd59c53..5ff930997 100644 --- a/src/circuit/circuit_vanilla.rs +++ b/src/circuit/circuit_vanilla.rs @@ -32,7 +32,7 @@ use crate::{ constants::{ OrchardCommitDomains, OrchardFixedBases, OrchardFixedBasesFull, OrchardHashDomains, }, - orchard_flavor, + orchard_flavor::OrchardVanilla, }; use super::{ @@ -41,8 +41,8 @@ use super::{ add_chip::{self, AddChip, AddConfig}, AddInstruction, }, - Circuit, OrchardCircuit, ANCHOR, CMX, CV_NET_X, CV_NET_Y, ENABLE_OUTPUT, ENABLE_SPEND, NF_OLD, - RK_X, RK_Y, + OrchardCircuit, OrchardCircuitBase, ANCHOR, CMX, CV_NET_X, CV_NET_Y, ENABLE_OUTPUT, + ENABLE_SPEND, NF_OLD, RK_X, RK_Y, }; use self::{ @@ -73,7 +73,7 @@ pub struct Config { new_note_commit_config: NoteCommitConfig, } -impl OrchardCircuit for orchard_flavor::OrchardVanilla { +impl OrchardCircuit for OrchardVanilla { type Config = Config; fn configure(meta: &mut plonk::ConstraintSystem) -> Self::Config { @@ -266,7 +266,7 @@ impl OrchardCircuit for orchard_flavor::OrchardVanilla { #[allow(non_snake_case)] fn synthesize( - circuit: &Circuit, + circuit: &OrchardCircuitBase, config: Self::Config, mut layouter: impl Layouter, ) -> Result<(), plonk::Error> { @@ -654,17 +654,17 @@ mod tests { use crate::{ bundle::Flags, - circuit::{Circuit as GenericCircuit, Instance, Proof, ProvingKey, VerifyingKey, K}, + circuit::{Instance, OrchardCircuitBase, Proof, ProvingKey, VerifyingKey, K}, keys::SpendValidatingKey, note::{AssetBase, Note}, - orchard_flavor, + orchard_flavor::OrchardVanilla, tree::MerklePath, value::{ValueCommitTrapdoor, ValueCommitment}, }; - type Circuit = GenericCircuit; + type OrchardCircuitVanilla = OrchardCircuitBase; - fn generate_circuit_instance(mut rng: R) -> (Circuit, Instance) { + fn generate_circuit_instance(mut rng: R) -> (OrchardCircuitVanilla, Instance) { let (_, fvk, spent_note) = Note::dummy(&mut rng, None, AssetBase::native()); let sender_address = spent_note.recipient(); @@ -688,7 +688,7 @@ mod tests { let psi_old = spent_note.rseed().psi(&spent_note.rho()); ( - Circuit { + OrchardCircuitVanilla { path: Value::known(path.auth_path()), pos: Value::known(path.position()), g_d_old: Value::known(sender_address.g_d()), @@ -736,7 +736,7 @@ mod tests { .map(|()| generate_circuit_instance(&mut rng)) .unzip(); - let vk = VerifyingKey::build::(); + let vk = VerifyingKey::build::(); // Test that the pinned verification key (representing the circuit) // is as expected. @@ -778,7 +778,7 @@ mod tests { ); } - let pk = ProvingKey::build::(); + let pk = ProvingKey::build::(); let proof = Proof::create(&pk, &circuits, &instances, &mut rng).unwrap(); assert!(proof.verify(&vk, &instances).is_ok()); assert_eq!(proof.0.len(), expected_proof_size); @@ -788,7 +788,7 @@ mod tests { fn serialized_proof_test_case() { use std::io::{Read, Write}; - let vk = VerifyingKey::build::(); + let vk = VerifyingKey::build::(); fn write_test_case( mut w: W, @@ -857,7 +857,7 @@ mod tests { let (circuit, instance) = generate_circuit_instance(OsRng); let instances = &[instance.clone()]; - let pk = ProvingKey::build::(); + let pk = ProvingKey::build::(); let proof = Proof::create(&pk, &[circuit], instances, &mut rng).unwrap(); assert!(proof.verify(&vk, instances).is_ok()); @@ -888,7 +888,7 @@ mod tests { .titled("Orchard Action Circuit", ("sans-serif", 60)) .unwrap(); - let circuit = Circuit { + let circuit = OrchardCircuitVanilla { path: Value::unknown(), pos: Value::unknown(), g_d_old: Value::unknown(), diff --git a/src/circuit/circuit_zsa.rs b/src/circuit/circuit_zsa.rs index 7f9128a14..72b2ac1c0 100644 --- a/src/circuit/circuit_zsa.rs +++ b/src/circuit/circuit_zsa.rs @@ -38,7 +38,7 @@ use crate::{ constants::OrchardFixedBasesFull, constants::{OrchardCommitDomains, OrchardFixedBases, OrchardHashDomains}, note::AssetBase, - orchard_flavor, + orchard_flavor::OrchardZSA, }; use super::{ @@ -49,8 +49,8 @@ use super::{ add_chip::{self, AddChip, AddConfig}, AddInstruction, }, - Circuit, OrchardCircuit, ANCHOR, CMX, CV_NET_X, CV_NET_Y, ENABLE_OUTPUT, ENABLE_SPEND, - ENABLE_ZSA, NF_OLD, RK_X, RK_Y, + OrchardCircuit, OrchardCircuitBase, ANCHOR, CMX, CV_NET_X, CV_NET_Y, ENABLE_OUTPUT, + ENABLE_SPEND, ENABLE_ZSA, NF_OLD, RK_X, RK_Y, }; use self::{ @@ -83,7 +83,7 @@ pub struct Config { cond_swap_config: CondSwapConfig, } -impl OrchardCircuit for orchard_flavor::OrchardZSA { +impl OrchardCircuit for OrchardZSA { type Config = Config; fn configure(meta: &mut plonk::ConstraintSystem) -> Self::Config { @@ -355,7 +355,7 @@ impl OrchardCircuit for orchard_flavor::OrchardZSA { #[allow(non_snake_case)] fn synthesize( - circuit: &Circuit, + circuit: &OrchardCircuitBase, config: Self::Config, mut layouter: impl Layouter, ) -> Result<(), plonk::Error> { @@ -871,18 +871,18 @@ mod tests { use crate::{ builder::SpendInfo, bundle::Flags, - circuit::{Circuit as GenericCircuit, Instance, Proof, ProvingKey, VerifyingKey, K}, + circuit::{Instance, OrchardCircuitBase, Proof, ProvingKey, VerifyingKey, K}, keys::{FullViewingKey, Scope, SpendValidatingKey, SpendingKey}, note::{commitment::NoteCommitTrapdoor, AssetBase, Note, NoteCommitment, Nullifier}, - orchard_flavor, + orchard_flavor::OrchardZSA, primitives::redpallas::VerificationKey, tree::MerklePath, value::{NoteValue, ValueCommitTrapdoor, ValueCommitment}, }; - type Circuit = GenericCircuit; + type OrchardCircuitZSA = OrchardCircuitBase; - fn generate_dummy_circuit_instance(mut rng: R) -> (Circuit, Instance) { + fn generate_dummy_circuit_instance(mut rng: R) -> (OrchardCircuitZSA, Instance) { let (_, fvk, spent_note) = Note::dummy(&mut rng, None, AssetBase::native()); let sender_address = spent_note.recipient(); @@ -906,7 +906,7 @@ mod tests { let psi_old = spent_note.rseed().psi(&spent_note.rho()); ( - Circuit { + OrchardCircuitZSA { path: Value::known(path.auth_path()), pos: Value::known(path.position()), g_d_old: Value::known(sender_address.g_d()), @@ -954,7 +954,7 @@ mod tests { .map(|()| generate_dummy_circuit_instance(&mut rng)) .unzip(); - let vk = VerifyingKey::build::(); + let vk = VerifyingKey::build::(); // Test that the pinned verification key (representing the circuit) // is as expected. @@ -995,7 +995,7 @@ mod tests { ); } - let pk = ProvingKey::build::(); + let pk = ProvingKey::build::(); let proof = Proof::create(&pk, &circuits, &instances, &mut rng).unwrap(); assert!(proof.verify(&vk, &instances).is_ok()); assert_eq!(proof.0.len(), expected_proof_size); @@ -1006,7 +1006,7 @@ mod tests { use std::fs; use std::io::{Read, Write}; - let vk = VerifyingKey::build::(); + let vk = VerifyingKey::build::(); fn write_test_case( mut w: W, @@ -1076,7 +1076,7 @@ mod tests { let (circuit, instance) = generate_dummy_circuit_instance(OsRng); let instances = &[instance.clone()]; - let pk = ProvingKey::build::(); + let pk = ProvingKey::build::(); let proof = Proof::create(&pk, &[circuit], instances, &mut rng).unwrap(); assert!(proof.verify(&vk, instances).is_ok()); @@ -1107,7 +1107,7 @@ mod tests { .titled("Orchard Action Circuit", ("sans-serif", 60)) .unwrap(); - let circuit = Circuit { + let circuit = OrchardCircuitZSA { path: Value::unknown(), pos: Value::unknown(), g_d_old: Value::unknown(), @@ -1139,7 +1139,11 @@ mod tests { .unwrap(); } - fn check_proof_of_orchard_circuit(circuit: &Circuit, instance: &Instance, should_pass: bool) { + fn check_proof_of_orchard_circuit( + circuit: &OrchardCircuitZSA, + instance: &Instance, + should_pass: bool, + ) { let proof_verify = MockProver::run( K, circuit, @@ -1162,7 +1166,7 @@ mod tests { is_native_asset: bool, split_flag: bool, mut rng: R, - ) -> (Circuit, Instance) { + ) -> (OrchardCircuitZSA, Instance) { // Create asset let asset_base = if is_native_asset { AssetBase::native() @@ -1240,7 +1244,7 @@ mod tests { }; ( - Circuit::from_action_context_unchecked(spend_info, output_note, alpha, rcv), + OrchardCircuitZSA::from_action_context_unchecked(spend_info, output_note, alpha, rcv), Instance { anchor, cv_net, @@ -1310,7 +1314,7 @@ mod tests { // Set cm_old to be a random NoteCommitment // The proof should fail - let circuit_wrong_cm_old = Circuit { + let circuit_wrong_cm_old = OrchardCircuitZSA { path: circuit.path, pos: circuit.pos, g_d_old: circuit.g_d_old, @@ -1368,7 +1372,7 @@ mod tests { // If split_flag = 0 , set psi_nf to be a random Pallas base element // The proof should fail if !split_flag { - let circuit_wrong_psi_nf = Circuit { + let circuit_wrong_psi_nf = OrchardCircuitZSA { path: circuit.path, pos: circuit.pos, g_d_old: circuit.g_d_old, diff --git a/src/note_encryption.rs b/src/note_encryption.rs index b3b481a11..21bbc43da 100644 --- a/src/note_encryption.rs +++ b/src/note_encryption.rs @@ -23,7 +23,7 @@ mod domain; mod note_bytes; mod orchard_domain; -pub use orchard_domain::{OrchardDomain, OrchardDomainContext}; +pub use orchard_domain::{OrchardDomain, OrchardDomainBase}; const PRF_OCK_ORCHARD_PERSONALIZATION: &[u8; 16] = b"Zcash_Orchardock"; diff --git a/src/note_encryption/action.rs b/src/note_encryption/action.rs index ea02f04ec..e33e77502 100644 --- a/src/note_encryption/action.rs +++ b/src/note_encryption/action.rs @@ -4,10 +4,10 @@ use std::fmt; use super::{ Action, EphemeralKeyBytes, ExtractedNoteCommitment, Nullifier, OrchardDomain, - OrchardDomainContext, ShieldedOutput, + OrchardDomainBase, ShieldedOutput, }; -impl ShieldedOutput> for Action { +impl ShieldedOutput> for Action { fn ephemeral_key(&self) -> EphemeralKeyBytes { EphemeralKeyBytes(self.encrypted_note().epk_bytes) } @@ -42,7 +42,7 @@ impl fmt::Debug for CompactAction { impl From<&Action> for CompactAction where - Action: ShieldedOutput>, + Action: ShieldedOutput>, { fn from(action: &Action) -> Self { CompactAction { @@ -54,7 +54,7 @@ where } } -impl ShieldedOutput> for CompactAction { +impl ShieldedOutput> for CompactAction { fn ephemeral_key(&self) -> EphemeralKeyBytes { EphemeralKeyBytes(self.ephemeral_key.0) } diff --git a/src/note_encryption/domain.rs b/src/note_encryption/domain.rs index 006be5116..56d7055d7 100644 --- a/src/note_encryption/domain.rs +++ b/src/note_encryption/domain.rs @@ -17,10 +17,10 @@ use crate::{ }; use super::{ - parse_note_plaintext_without_memo, prf_ock_orchard, Memo, OrchardDomain, OrchardDomainContext, + parse_note_plaintext_without_memo, prf_ock_orchard, Memo, OrchardDomain, OrchardDomainBase, }; -impl Domain for OrchardDomainContext { +impl Domain for OrchardDomainBase { type EphemeralSecretKey = EphemeralSecretKey; type EphemeralPublicKey = EphemeralPublicKey; type PreparedEphemeralPublicKey = PreparedEphemeralPublicKey; @@ -151,7 +151,7 @@ impl Domain for OrchardDomainContext { } } -impl BatchDomain for OrchardDomainContext { +impl BatchDomain for OrchardDomainBase { fn batch_kdf<'a>( items: impl Iterator, &'a EphemeralKeyBytes)>, ) -> Vec> { diff --git a/src/note_encryption/note_encryption_vanilla.rs b/src/note_encryption/note_encryption_vanilla.rs index a96b1a994..be3936420 100644 --- a/src/note_encryption/note_encryption_vanilla.rs +++ b/src/note_encryption/note_encryption_vanilla.rs @@ -2,14 +2,14 @@ //! In-band secret distribution for Orchard bundles. -use crate::{orchard_flavor, Note}; +use crate::{orchard_flavor::OrchardVanilla, Note}; use super::{ build_base_note_plaintext_bytes, note_bytes::NoteBytes, Memo, OrchardDomain, COMPACT_NOTE_SIZE_VANILLA, }; -impl OrchardDomain for orchard_flavor::OrchardVanilla { +impl OrchardDomain for OrchardVanilla { const COMPACT_NOTE_SIZE: usize = COMPACT_NOTE_SIZE_VANILLA; type NotePlaintextBytes = NoteBytes<{ Self::NOTE_PLAINTEXT_SIZE }>; @@ -54,16 +54,16 @@ mod tests { use super::{ super::{ action::CompactAction, note_version, parse_note_plaintext_without_memo, - prf_ock_orchard, OrchardDomainContext, + prf_ock_orchard, OrchardDomainBase, }, - {orchard_flavor, NoteBytes}, + {NoteBytes, OrchardVanilla}, }; - type OrchardVanilla = OrchardDomainContext; + type OrchardDomainVanilla = OrchardDomainBase; /// Implementation of in-band secret distribution for Orchard bundles. pub type OrchardNoteEncryptionVanilla = - zcash_note_encryption_zsa::NoteEncryption; + zcash_note_encryption_zsa::NoteEncryption; proptest! { #[test] @@ -74,10 +74,10 @@ mod tests { let rho = note.rho(); // Encode. - let plaintext = OrchardVanilla::note_plaintext_bytes(¬e, memo); + let plaintext = OrchardDomainVanilla::note_plaintext_bytes(¬e, memo); // Decode. - let domain = OrchardVanilla::for_nullifier(rho); + let domain = OrchardDomainVanilla::for_nullifier(rho); let parsed_version = note_version(plaintext.as_ref()).unwrap(); let (compact, parsed_memo) = domain.extract_memo(&plaintext); @@ -165,7 +165,7 @@ mod tests { // (Tested first because it only requires immutable references.) // - let domain = OrchardVanilla::for_nullifier(rho); + let domain = OrchardDomainVanilla::for_nullifier(rho); match try_note_decryption(&domain, &ivk, &action) { Some((decrypted_note, decrypted_to, decrypted_memo)) => { diff --git a/src/note_encryption/note_encryption_zsa.rs b/src/note_encryption/note_encryption_zsa.rs index 47d38f63d..bf1dec0b3 100644 --- a/src/note_encryption/note_encryption_zsa.rs +++ b/src/note_encryption/note_encryption_zsa.rs @@ -2,14 +2,14 @@ //! In-band secret distribution for Orchard bundles. -use crate::{orchard_flavor, Note}; +use crate::{orchard_flavor::OrchardZSA, Note}; use super::{ build_base_note_plaintext_bytes, note_bytes::NoteBytes, Memo, OrchardDomain, COMPACT_NOTE_SIZE_VANILLA, COMPACT_NOTE_SIZE_ZSA, }; -impl OrchardDomain for orchard_flavor::OrchardZSA { +impl OrchardDomain for OrchardZSA { const COMPACT_NOTE_SIZE: usize = COMPACT_NOTE_SIZE_ZSA; type NotePlaintextBytes = NoteBytes<{ Self::NOTE_PLAINTEXT_SIZE }>; @@ -56,15 +56,15 @@ mod tests { use super::{ super::{ action::CompactAction, note_version, parse_note_plaintext_without_memo, - prf_ock_orchard, OrchardDomainContext, + prf_ock_orchard, OrchardDomainBase, }, - orchard_flavor, NoteBytes, + NoteBytes, OrchardZSA, }; - type OrchardZSA = OrchardDomainContext; + type OrchardDomainZSA = OrchardDomainBase; /// Implementation of in-band secret distribution for Orchard bundles. - pub type OrchardNoteEncryptionZSA = zcash_note_encryption_zsa::NoteEncryption; + pub type OrchardNoteEncryptionZSA = zcash_note_encryption_zsa::NoteEncryption; proptest! { #[test] @@ -75,10 +75,10 @@ mod tests { let rho = note.rho(); // Encode. - let plaintext = OrchardZSA::note_plaintext_bytes(¬e, memo); + let plaintext = OrchardDomainZSA::note_plaintext_bytes(¬e, memo); // Decode. - let domain = OrchardZSA::for_nullifier(rho); + let domain = OrchardDomainZSA::for_nullifier(rho); let parsed_version = note_version(plaintext.as_ref()).unwrap(); let (compact, parsed_memo) = domain.extract_memo(&plaintext); @@ -166,7 +166,7 @@ mod tests { // (Tested first because it only requires immutable references.) // - let domain = OrchardZSA::for_nullifier(rho); + let domain = OrchardDomainZSA::for_nullifier(rho); match try_note_decryption(&domain, &ivk, &action) { Some((decrypted_note, decrypted_to, decrypted_memo)) => { diff --git a/src/note_encryption/orchard_domain.rs b/src/note_encryption/orchard_domain.rs index d764c4c3e..c516084cc 100644 --- a/src/note_encryption/orchard_domain.rs +++ b/src/note_encryption/orchard_domain.rs @@ -38,13 +38,13 @@ pub trait OrchardDomain: fmt::Debug + Clone { /// Orchard-specific note encryption logic. #[derive(Debug, Clone)] -pub struct OrchardDomainContext { +pub struct OrchardDomainBase { /// Represents a nullifier which is used to prevent double spending within the Orchard protocol. pub rho: Nullifier, phantom: std::marker::PhantomData, } -impl OrchardDomainContext { +impl OrchardDomainBase { /// Constructs a domain that can be used to trial-decrypt this action's output note. pub fn for_action(act: &Action) -> Self { Self::for_nullifier(*act.nullifier()) diff --git a/src/orchard_flavor.rs b/src/orchard_flavor.rs index a454782b2..c791e75a2 100644 --- a/src/orchard_flavor.rs +++ b/src/orchard_flavor.rs @@ -1,6 +1,6 @@ //! Defines types and traits for the variations ("flavors") of the Orchard protocol (Vanilla and ZSA). -// FIXME: The circuit and note_encryption modules refer to this orchard_flavor module as well - are such circular references okay? -use crate::{circuit::OrchardCircuit, note_encryption::OrchardDomain}; + +// FIXME: consider renaming this file or move its content to lib /// Represents the standard ("Vanilla") variation ("flavor") of the Orchard protocol. #[derive(Debug, Clone, Default)] @@ -9,10 +9,3 @@ pub struct OrchardVanilla; /// Represents a ZSA variation ("flavor") of the Orchard protocol. #[derive(Debug, Clone, Default)] pub struct OrchardZSA; - -/// A trait binding the common functionality between different Orchard protocol variations -/// ("flavors"). -pub trait OrchardFlavor: OrchardDomain + OrchardCircuit {} - -impl OrchardFlavor for OrchardVanilla {} -impl OrchardFlavor for OrchardZSA {} diff --git a/tests/builder.rs b/tests/builder.rs index fca7fdffb..1767c114d 100644 --- a/tests/builder.rs +++ b/tests/builder.rs @@ -6,8 +6,8 @@ use orchard::{ circuit::{ProvingKey, VerifyingKey}, keys::{FullViewingKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, SpendingKey}, note::{AssetBase, ExtractedNoteCommitment}, - note_encryption::OrchardDomainContext, - orchard_flavor, + note_encryption::OrchardDomainBase, + orchard_flavor::OrchardZSA, tree::{MerkleHashOrchard, MerklePath}, value::NoteValue, Anchor, Bundle, Note, @@ -15,10 +15,10 @@ use orchard::{ use rand::rngs::OsRng; use zcash_note_encryption_zsa::try_note_decryption; -type OrchardZSA = OrchardDomainContext; +type OrchardDomainZSA = OrchardDomainBase; pub fn verify_bundle( - bundle: &Bundle, + bundle: &Bundle, vk: &VerifyingKey, verify_proof: bool, ) { @@ -57,16 +57,16 @@ pub fn build_merkle_path(note: &Note) -> (MerklePath, Anchor) { #[test] fn bundle_chain() { let mut rng = OsRng; - // FIXME: consider adding test for orchard_flavor::OrchardVanilla as well - let pk = ProvingKey::build::(); - let vk = VerifyingKey::build::(); + // FIXME: consider adding test for OrchardVanilla as well + let pk = ProvingKey::build::(); + let vk = VerifyingKey::build::(); let sk = SpendingKey::from_bytes([0; 32]).unwrap(); let fvk = FullViewingKey::from(&sk); let recipient = fvk.address_at(0u32, Scope::External); // Create a shielding bundle. - let shielding_bundle: Bundle<_, i64, orchard_flavor::OrchardZSA> = { + let shielding_bundle: Bundle<_, i64, OrchardZSA> = { // Use the empty tree. let anchor = MerkleHashOrchard::empty_root(32.into()).into(); @@ -91,13 +91,13 @@ fn bundle_chain() { verify_bundle(&shielding_bundle, &vk, true); // Create a shielded bundle spending the previous output. - let shielded_bundle: Bundle<_, i64, orchard_flavor::OrchardZSA> = { + let shielded_bundle: Bundle<_, i64, OrchardZSA> = { let ivk = PreparedIncomingViewingKey::new(&fvk.to_ivk(Scope::External)); let (note, _, _) = shielding_bundle .actions() .iter() .find_map(|action| { - let domain = OrchardZSA::for_action(action); + let domain = OrchardDomainZSA::for_action(action); try_note_decryption(&domain, &ivk, action) }) .unwrap(); diff --git a/tests/zsa.rs b/tests/zsa.rs index 52e3c5588..1b20043fa 100644 --- a/tests/zsa.rs +++ b/tests/zsa.rs @@ -14,8 +14,8 @@ use orchard::{ bundle::Flags, circuit::{ProvingKey, VerifyingKey}, keys::{FullViewingKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, SpendingKey}, - note_encryption::OrchardDomainContext, - orchard_flavor, + note_encryption::OrchardDomainBase, + orchard_flavor::OrchardZSA, value::NoteValue, Address, Anchor, Bundle, Note, }; @@ -23,7 +23,7 @@ use rand::rngs::OsRng; use std::collections::HashSet; use zcash_note_encryption_zsa::try_note_decryption; -type OrchardZSA = OrchardDomainContext; +type OrchardDomainZSA = OrchardDomainBase; #[derive(Debug)] struct Keychain { @@ -56,8 +56,8 @@ impl Keychain { fn prepare_keys() -> Keychain { // FIXME: consider adding test for OrchardDomainVanilla as well - let pk = ProvingKey::build::(); - let vk = VerifyingKey::build::(); + let pk = ProvingKey::build::(); + let vk = VerifyingKey::build::(); let sk = SpendingKey::from_bytes([0; 32]).unwrap(); let fvk = FullViewingKey::from(&sk); @@ -90,7 +90,7 @@ fn build_and_sign_bundle( mut rng: OsRng, pk: &ProvingKey, sk: &SpendingKey, -) -> Bundle { +) -> Bundle { let unauthorized = builder.build(&mut rng).unwrap(); let sighash = unauthorized.commitment().into(); let proven = unauthorized.create_proof(pk, &mut rng).unwrap(); @@ -185,7 +185,7 @@ fn issue_zsa_notes(asset_descr: &str, keys: &Keychain) -> (Note, Note) { fn create_native_note(keys: &Keychain) -> Note { let mut rng = OsRng; - let shielding_bundle: Bundle<_, i64, orchard_flavor::OrchardZSA> = { + let shielding_bundle: Bundle<_, i64, OrchardZSA> = { // Use the empty tree. let anchor = MerkleHashOrchard::empty_root(32.into()).into(); @@ -210,7 +210,7 @@ fn create_native_note(keys: &Keychain) -> Note { .actions() .iter() .find_map(|action| { - let domain = OrchardZSA::for_action(action); + let domain = OrchardDomainZSA::for_action(action); try_note_decryption(&domain, &PreparedIncomingViewingKey::new(&ivk), action) }) .unwrap(); @@ -243,7 +243,7 @@ fn build_and_verify_bundle( keys: &Keychain, ) -> Result<(), String> { let rng = OsRng; - let shielded_bundle: Bundle<_, i64, orchard_flavor::OrchardZSA> = { + let shielded_bundle: Bundle<_, i64, OrchardZSA> = { let mut builder = Builder::new(Flags::from_parts(true, true, true), anchor); spends @@ -271,9 +271,7 @@ fn build_and_verify_bundle( Ok(()) } -fn verify_unique_spent_nullifiers( - bundle: &Bundle, -) -> bool { +fn verify_unique_spent_nullifiers(bundle: &Bundle) -> bool { let mut unique_nulifiers = Vec::new(); let spent_nullifiers = bundle .actions()