Skip to content

Commit

Permalink
Move derive_nullifier function into derive_nullifier.rs file
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstanceBeguier committed Jul 17, 2024
1 parent 9325ef1 commit 18b4452
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 134 deletions.
3 changes: 2 additions & 1 deletion src/circuit/circuit_vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ use crate::{

use super::{
commit_ivk::{self, CommitIvkChip},
gadget::{add_chip::AddChip, assign_free_advice, AddInstruction},
gadget::{add_chip::AddChip, assign_free_advice},
note_commit::NoteCommitChip,
Circuit, OrchardCircuit, ANCHOR, CMX, CV_NET_X, CV_NET_Y, ENABLE_OUTPUT, ENABLE_SPEND, NF_OLD,
RK_X, RK_Y,
};

mod derive_nullifier;
mod gadget;
mod note_commit;
mod value_commit_orchard;
Expand Down
54 changes: 54 additions & 0 deletions src/circuit/circuit_vanilla/derive_nullifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Derive nullifier logic for the Orchard circuit (Vanilla variation).

pub(in crate::circuit) mod gadgets {
use pasta_curves::pallas;

use crate::{circuit::gadget::AddInstruction, constants::OrchardFixedBases};
use halo2_gadgets::{
ecc::{chip::EccPoint, EccInstructions, Point, X},
poseidon::{
primitives::{self as poseidon, ConstantLength},
PoseidonSpongeInstructions,
},
};
use halo2_proofs::{
circuit::{AssignedCell, Layouter},
plonk,
};

/// `DeriveNullifier` from [Section 4.16: Note Commitments and Nullifiers].
///
/// [Section 4.16: Note Commitments and Nullifiers]: https://zips.z.cash/protocol/protocol.pdf#commitmentsandnullifiers
#[allow(clippy::too_many_arguments)]
pub(in crate::circuit) fn derive_nullifier<
PoseidonChip: PoseidonSpongeInstructions<pallas::Base, poseidon::P128Pow5T3, ConstantLength<2>, 3, 2>,
AddChip: AddInstruction<pallas::Base>,
EccChip: EccInstructions<
pallas::Affine,
FixedPoints = OrchardFixedBases,
Point = EccPoint,
Var = AssignedCell<pallas::Base, pallas::Base>,
>,
>(
layouter: &mut impl Layouter<pallas::Base>,
poseidon_chip: PoseidonChip,
add_chip: AddChip,
ecc_chip: EccChip,
rho: AssignedCell<pallas::Base, pallas::Base>,
psi: &AssignedCell<pallas::Base, pallas::Base>,
cm: &Point<pallas::Affine, EccChip>,
nk: AssignedCell<pallas::Base, pallas::Base>,
) -> Result<X<pallas::Affine, EccChip>, plonk::Error> {
crate::circuit::gadget::derive_nullifier(
layouter,
poseidon_chip,
add_chip,
ecc_chip,
rho,
psi,
cm,
nk,
)
.map(|res| res.extract_p())
}
}
53 changes: 1 addition & 52 deletions src/circuit/circuit_vanilla/gadget.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,6 @@
//! Gadgets used in the Orchard circuit (Vanilla variation).

use pasta_curves::pallas;

use super::AddInstruction;
use crate::constants::OrchardFixedBases;
use halo2_gadgets::{
ecc::{chip::EccPoint, EccInstructions, Point, X},
poseidon::{
primitives::{self as poseidon, ConstantLength},
PoseidonSpongeInstructions,
},
};
use halo2_proofs::{
circuit::{AssignedCell, Layouter},
plonk,
};

/// `DeriveNullifier` from [Section 4.16: Note Commitments and Nullifiers].
///
/// [Section 4.16: Note Commitments and Nullifiers]: https://zips.z.cash/protocol/protocol.pdf#commitmentsandnullifiers
#[allow(clippy::too_many_arguments)]
pub(in crate::circuit) fn derive_nullifier<
PoseidonChip: PoseidonSpongeInstructions<pallas::Base, poseidon::P128Pow5T3, ConstantLength<2>, 3, 2>,
AddChip: AddInstruction<pallas::Base>,
EccChip: EccInstructions<
pallas::Affine,
FixedPoints = OrchardFixedBases,
Point = EccPoint,
Var = AssignedCell<pallas::Base, pallas::Base>,
>,
>(
layouter: &mut impl Layouter<pallas::Base>,
poseidon_chip: PoseidonChip,
add_chip: AddChip,
ecc_chip: EccChip,
rho: AssignedCell<pallas::Base, pallas::Base>,
psi: &AssignedCell<pallas::Base, pallas::Base>,
cm: &Point<pallas::Affine, EccChip>,
nk: AssignedCell<pallas::Base, pallas::Base>,
) -> Result<X<pallas::Affine, EccChip>, plonk::Error> {
crate::circuit::gadget::derive_nullifier(
layouter,
poseidon_chip,
add_chip,
ecc_chip,
rho,
psi,
cm,
nk,
)
.map(|res| res.extract_p())
}

pub(in crate::circuit) use super::commit_ivk::gadgets::commit_ivk;
pub(in crate::circuit) use super::derive_nullifier::gadgets::derive_nullifier;
pub(in crate::circuit) use super::note_commit::gadgets::note_commit;
pub(in crate::circuit) use super::value_commit_orchard::gadgets::value_commit_orchard;
6 changes: 2 additions & 4 deletions src/circuit/circuit_zsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ use crate::{

use super::{
commit_ivk::{self, CommitIvkChip},
gadget::{
add_chip::AddChip, assign_free_advice, assign_is_native_asset, assign_split_flag,
AddInstruction,
},
gadget::{add_chip::AddChip, assign_free_advice, assign_is_native_asset, assign_split_flag},
note_commit::NoteCommitChip,
Circuit, OrchardCircuit, ANCHOR, CMX, CV_NET_X, CV_NET_Y, ENABLE_OUTPUT, ENABLE_SPEND,
ENABLE_ZSA, NF_OLD, RK_X, RK_Y,
};

mod derive_nullifier;
pub mod gadget;
mod note_commit;
mod value_commit_orchard;
Expand Down
78 changes: 78 additions & 0 deletions src/circuit/circuit_zsa/derive_nullifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! Derive nullifier logic for the Orchard circuit (ZSA variation).

pub(in crate::circuit) mod gadgets {
use group::Curve;
use pasta_curves::{arithmetic::CurveExt, pallas};

use crate::{circuit::gadget::AddInstruction, constants::OrchardFixedBases};
use halo2_gadgets::{
ecc::{chip::EccPoint, EccInstructions, Point, X},
poseidon::{
primitives::{self as poseidon, ConstantLength},
PoseidonSpongeInstructions,
},
utilities::cond_swap::CondSwapChip,
};
use halo2_proofs::{
circuit::{AssignedCell, Layouter},
plonk,
};

/// `DeriveNullifier` from [Section 4.16: Note Commitments and Nullifiers].
///
/// [Section 4.16: Note Commitments and Nullifiers]: https://zips.z.cash/protocol/protocol.pdf#commitmentsandnullifiers
#[allow(clippy::too_many_arguments)]
pub(in crate::circuit) fn derive_nullifier<
PoseidonChip: PoseidonSpongeInstructions<pallas::Base, poseidon::P128Pow5T3, ConstantLength<2>, 3, 2>,
AddChip: AddInstruction<pallas::Base>,
EccChip: EccInstructions<
pallas::Affine,
FixedPoints = OrchardFixedBases,
Point = EccPoint,
Var = AssignedCell<pallas::Base, pallas::Base>,
>,
>(
layouter: &mut impl Layouter<pallas::Base>,
poseidon_chip: PoseidonChip,
add_chip: AddChip,
ecc_chip: EccChip,
cond_swap_chip: CondSwapChip<pallas::Base>,
rho: AssignedCell<pallas::Base, pallas::Base>,
psi: &AssignedCell<pallas::Base, pallas::Base>,
cm: &Point<pallas::Affine, EccChip>,
nk: AssignedCell<pallas::Base, pallas::Base>,
split_flag: AssignedCell<pallas::Base, pallas::Base>,
) -> Result<X<pallas::Affine, EccChip>, plonk::Error> {
let nf = crate::circuit::gadget::derive_nullifier(
layouter,
poseidon_chip,
add_chip,
ecc_chip.clone(),
rho,
psi,
cm,
nk,
)?;

// Add NullifierL to nf
// split_note_nf = NullifierL + nf
let nullifier_l = Point::new_from_constant(
ecc_chip.clone(),
layouter.namespace(|| "witness NullifierL constant"),
pallas::Point::hash_to_curve("z.cash:Orchard")(b"L").to_affine(),
)?;
let split_note_nf = nullifier_l.add(layouter.namespace(|| "split_note_nf"), &nf)?;

// Select the desired nullifier according to split_flag
Ok(Point::from_inner(
ecc_chip,
cond_swap_chip.mux_on_points(
layouter.namespace(|| "mux on nf"),
&split_flag,
nf.inner(),
split_note_nf.inner(),
)?,
)
.extract_p())
}
}
78 changes: 1 addition & 77 deletions src/circuit/circuit_zsa/gadget.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,6 @@
//! Gadgets used in the Orchard circuit (ZSA variation).

use group::Curve;
use pasta_curves::arithmetic::CurveExt;
use pasta_curves::pallas;

use super::AddInstruction;
use crate::constants::OrchardFixedBases;
use halo2_gadgets::{
ecc::{chip::EccPoint, EccInstructions, Point, X},
poseidon::{
primitives::{self as poseidon, ConstantLength},
PoseidonSpongeInstructions,
},
utilities::cond_swap::CondSwapChip,
};
use halo2_proofs::{
circuit::{AssignedCell, Layouter},
plonk,
};

/// `DeriveNullifier` from [Section 4.16: Note Commitments and Nullifiers].
///
/// [Section 4.16: Note Commitments and Nullifiers]: https://zips.z.cash/protocol/protocol.pdf#commitmentsandnullifiers
#[allow(clippy::too_many_arguments)]
pub(in crate::circuit) fn derive_nullifier<
PoseidonChip: PoseidonSpongeInstructions<pallas::Base, poseidon::P128Pow5T3, ConstantLength<2>, 3, 2>,
AddChip: AddInstruction<pallas::Base>,
EccChip: EccInstructions<
pallas::Affine,
FixedPoints = OrchardFixedBases,
Point = EccPoint,
Var = AssignedCell<pallas::Base, pallas::Base>,
>,
>(
layouter: &mut impl Layouter<pallas::Base>,
poseidon_chip: PoseidonChip,
add_chip: AddChip,
ecc_chip: EccChip,
cond_swap_chip: CondSwapChip<pallas::Base>,
rho: AssignedCell<pallas::Base, pallas::Base>,
psi: &AssignedCell<pallas::Base, pallas::Base>,
cm: &Point<pallas::Affine, EccChip>,
nk: AssignedCell<pallas::Base, pallas::Base>,
split_flag: AssignedCell<pallas::Base, pallas::Base>,
) -> Result<X<pallas::Affine, EccChip>, plonk::Error> {
let nf = crate::circuit::gadget::derive_nullifier(
layouter,
poseidon_chip,
add_chip,
ecc_chip.clone(),
rho,
psi,
cm,
nk,
)?;

// Add NullifierL to nf
// split_note_nf = NullifierL + nf
let nullifier_l = Point::new_from_constant(
ecc_chip.clone(),
layouter.namespace(|| "witness NullifierL constant"),
pallas::Point::hash_to_curve("z.cash:Orchard")(b"L").to_affine(),
)?;
let split_note_nf = nullifier_l.add(layouter.namespace(|| "split_note_nf"), &nf)?;

// Select the desired nullifier according to split_flag
Ok(Point::from_inner(
ecc_chip,
cond_swap_chip.mux_on_points(
layouter.namespace(|| "mux on nf"),
&split_flag,
nf.inner(),
split_note_nf.inner(),
)?,
)
.extract_p())
}

pub(in crate::circuit) use super::commit_ivk::gadgets::commit_ivk;
pub(in crate::circuit) use super::derive_nullifier::gadgets::derive_nullifier;
pub(in crate::circuit) use super::note_commit::gadgets::note_commit;
pub(in crate::circuit) use super::value_commit_orchard::gadgets::value_commit_orchard;

0 comments on commit 18b4452

Please sign in to comment.