Skip to content

Commit

Permalink
Fix naming (OrchardDomainContex to OrchardDomainBase, Curcuit to Orch…
Browse files Browse the repository at this point in the history
…ardCircuitBase etc.), add use of orchard_flavor structs at the top of modules
  • Loading branch information
dmidem committed Mar 28, 2024
1 parent 7fde17c commit 06f8d42
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 135 deletions.
10 changes: 5 additions & 5 deletions benches/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use orchard::{
bundle::Flags,
circuit::{ProvingKey, VerifyingKey},
keys::{FullViewingKey, Scope, SpendingKey},
orchard_flavor,
orchard_flavor::OrchardZSA,
value::NoteValue,
Anchor, Bundle,
};
Expand All @@ -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::<orchard_flavor::OrchardZSA>();
let pk = ProvingKey::build::<orchard_flavor::OrchardZSA>();
// FIXME: consider adding test for OrchardVanilla as well
let vk = VerifyingKey::build::<OrchardZSA>();
let pk = ProvingKey::build::<OrchardZSA>();

let create_bundle = |num_recipients| {
let mut builder = Builder::new(
Expand All @@ -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()
Expand Down
23 changes: 14 additions & 9 deletions benches/note_decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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<orchard_flavor::OrchardZSA>;
type OrchardDomainZSA = OrchardDomainBase<OrchardZSA>;

fn bench_note_decryption(c: &mut Criterion) {
let rng = OsRng;
// FIXME: consider adding test for orchard_flavor::OrchardVanilla as well
let pk = ProvingKey::build::<orchard_flavor::OrchardZSA>();
// FIXME: consider adding test for OrchardVanilla as well
let pk = ProvingKey::build::<OrchardZSA>();

let fvk = FullViewingKey::from(&SpendingKey::from_bytes([7; 32]).unwrap());
let valid_ivk = fvk.to_ivk(Scope::External);
Expand Down Expand Up @@ -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()
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
24 changes: 13 additions & 11 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -256,7 +256,7 @@ impl ActionInfo {
fn build<D: OrchardDomain>(
self,
mut rng: impl RngCore,
) -> (Action<SigningMetadata, D>, Circuit<D>) {
) -> (Action<SigningMetadata, D>, OrchardCircuitBase<D>) {
assert_eq!(
self.spend.note.asset(),
self.output.asset,
Expand All @@ -282,7 +282,7 @@ impl ActionInfo {
let cm_new = note.commitment();
let cmx = cm_new.into();

let encryptor = NoteEncryption::<OrchardDomainContext<D>>::new(
let encryptor = NoteEncryption::<OrchardDomainBase<D>>::new(
self.output.ovk,
note,
self.output.memo.unwrap_or_else(|| {
Expand Down Expand Up @@ -310,7 +310,9 @@ impl ActionInfo {
parts: SigningParts { ak, alpha },
},
),
Circuit::<D>::from_action_context_unchecked(self.spend, note, alpha, self.rcv),
OrchardCircuitBase::<D>::from_action_context_unchecked(
self.spend, note, alpha, self.rcv,
),
)
}
}
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -644,7 +646,7 @@ impl<P: fmt::Debug, S: InProgressSignatures> Authorization for InProgress<P, S>
/// This struct contains the private data needed to create a [`Proof`] for a [`Bundle`].
#[derive(Clone, Debug)]
pub struct Unproven<D: OrchardCircuit> {
circuits: Vec<Circuit<D>>,
circuits: Vec<OrchardCircuitBase<D>>,
}

impl<S: InProgressSignatures, D: OrchardCircuit> InProgress<Unproven<D>, S> {
Expand Down Expand Up @@ -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,
};
Expand All @@ -1089,8 +1091,8 @@ mod tests {

#[test]
fn shielding_bundle() {
// FIXME: consider adding test for orchard_flavor::OrchardVanilla as well
let pk = ProvingKey::build::<orchard_flavor::OrchardZSA>();
// FIXME: consider adding test for OrchardVanilla as well
let pk = ProvingKey::build::<OrchardZSA>();
let mut rng = OsRng;

let sk = SpendingKey::random(&mut rng);
Expand All @@ -1114,7 +1116,7 @@ mod tests {
let balance: i64 = builder.value_balance().unwrap();
assert_eq!(balance, -5000);

let bundle: Bundle<Authorized, i64, orchard_flavor::OrchardZSA> = builder
let bundle: Bundle<Authorized, i64, OrchardZSA> = builder
.build(&mut rng)
.unwrap()
.create_proof(&pk, &mut rng)
Expand Down
10 changes: 5 additions & 5 deletions src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -331,7 +331,7 @@ impl<A: Authorization, V, D: OrchardDomain> Bundle<A, V, D> {
.iter()
.enumerate()
.filter_map(|(idx, action)| {
let domain = OrchardDomainContext::<D>::for_action(action);
let domain = OrchardDomainBase::<D>::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))
Expand All @@ -351,7 +351,7 @@ impl<A: Authorization, V, D: OrchardDomain> Bundle<A, V, D> {
) -> Option<(Note, Address, [u8; 512])> {
let prepared_ivk = PreparedIncomingViewingKey::new(key);
self.actions.get(action_idx).and_then(move |action| {
let domain = OrchardDomainContext::<D>::for_action(action);
let domain = OrchardDomainBase::<D>::for_action(action);
// let domain = D::for_action(action);
try_note_decryption(&domain, &prepared_ivk, action)
})
Expand All @@ -369,7 +369,7 @@ impl<A: Authorization, V, D: OrchardDomain> Bundle<A, V, D> {
.iter()
.enumerate()
.filter_map(|(idx, action)| {
let domain = OrchardDomainContext::<D>::for_action(action);
let domain = OrchardDomainBase::<D>::for_action(action);
keys.iter().find_map(move |key| {
try_output_recovery_with_ovk(
&domain,
Expand All @@ -393,7 +393,7 @@ impl<A: Authorization, V, D: OrchardDomain> Bundle<A, V, D> {
key: &OutgoingViewingKey,
) -> Option<(Note, Address, [u8; 512])> {
self.actions.get(action_idx).and_then(move |action| {
let domain = OrchardDomainContext::<D>::for_action(action);
let domain = OrchardDomainBase::<D>::for_action(action);
try_output_recovery_with_ovk(
&domain,
key,
Expand Down
21 changes: 10 additions & 11 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ pub trait OrchardCircuit: Sized + Default {

/// Wrapper for configure function of plonk::Circuit trait
fn synthesize(
circuit: &Circuit<Self>,
circuit: &OrchardCircuitBase<Self>,
config: Self::Config,
layouter: impl Layouter<pallas::Base>,
) -> Result<(), plonk::Error>;
}

impl<D: OrchardCircuit> plonk::Circuit<pallas::Base> for Circuit<D> {
impl<D: OrchardCircuit> plonk::Circuit<pallas::Base> for OrchardCircuitBase<D> {
type Config = D::Config;
type FloorPlanner = floor_planner::V1;

Expand All @@ -90,10 +90,9 @@ impl<D: OrchardCircuit> plonk::Circuit<pallas::Base> for Circuit<D> {
}
}

// FIXME: rename to CircuitCommon
/// The Orchard Action circuit.
#[derive(Clone, Debug, Default)]
pub struct Circuit<D> {
pub struct OrchardCircuitBase<D> {
pub(crate) path: Value<[MerkleHashOrchard; MERKLE_DEPTH_ORCHARD]>,
pub(crate) pos: Value<u32>,
pub(crate) g_d_old: Value<NonIdentityPallasPoint>,
Expand All @@ -119,7 +118,7 @@ pub struct Circuit<D> {
phantom: std::marker::PhantomData<D>,
}

impl<D> Circuit<D> {
impl<D> OrchardCircuitBase<D> {
/// 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.
Expand All @@ -140,7 +139,7 @@ impl<D> Circuit<D> {
output_note: Note,
alpha: pallas::Scalar,
rcv: ValueCommitTrapdoor,
) -> Option<Circuit<D>> {
) -> Option<OrchardCircuitBase<D>> {
(spend.note.nullifier(&spend.fvk) == output_note.rho())
.then(|| Self::from_action_context_unchecked(spend, output_note, alpha, rcv))
}
Expand All @@ -150,7 +149,7 @@ impl<D> Circuit<D> {
output_note: Note,
alpha: pallas::Scalar,
rcv: ValueCommitTrapdoor,
) -> Circuit<D> {
) -> OrchardCircuitBase<D> {
let sender_address = spend.note.recipient();
let rho_old = spend.note.rho();
let psi_old = spend.note.rseed().psi(&rho_old);
Expand All @@ -163,7 +162,7 @@ impl<D> Circuit<D> {
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()),
Expand Down Expand Up @@ -202,7 +201,7 @@ impl VerifyingKey {
/// Builds the verifying key.
pub fn build<D: OrchardCircuit>() -> Self {
let params = halo2_proofs::poly::commitment::Params::new(K);
let circuit: Circuit<D> = Default::default();
let circuit: OrchardCircuitBase<D> = Default::default();

let vk = plonk::keygen_vk(&params, &circuit).unwrap();

Expand All @@ -221,7 +220,7 @@ impl ProvingKey {
/// Builds the proving key.
pub fn build<D: OrchardCircuit>() -> Self {
let params = halo2_proofs::poly::commitment::Params::new(K);
let circuit: Circuit<D> = Default::default();
let circuit: OrchardCircuitBase<D> = Default::default();

let vk = plonk::keygen_vk(&params, &circuit).unwrap();
let pk = plonk::keygen_pk(&params, vk, &circuit).unwrap();
Expand Down Expand Up @@ -337,7 +336,7 @@ impl Proof {
/// Creates a proof for the given circuits and instances.
pub fn create<D: OrchardCircuit>(
pk: &ProvingKey,
circuits: &[Circuit<D>],
circuits: &[OrchardCircuitBase<D>],
instances: &[Instance],
mut rng: impl RngCore,
) -> Result<Self, plonk::Error> {
Expand Down
Loading

0 comments on commit 06f8d42

Please sign in to comment.