Skip to content

Commit

Permalink
Merge branch 'xuyang/simplify_vp_trait' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
mariari committed Aug 28, 2023
2 parents 5892ad0 + eb3a0e2 commit fc9717a
Show file tree
Hide file tree
Showing 23 changed files with 318 additions and 402 deletions.
4 changes: 2 additions & 2 deletions taiga_halo2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ harness = false
name = "vp_proof"
harness = false

[[example]]
name = "taiga_sudoku"
# [[example]]
# name = "taiga_sudoku"

[[example]]
name = "tx_examples"
9 changes: 5 additions & 4 deletions taiga_halo2/benches/vp_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pasta_curves::pallas;
use rand::rngs::OsRng;
use rand::Rng;
use taiga_halo2::{
circuit::{vp_circuit::ValidityPredicateInfo, vp_examples::TrivialValidityPredicateCircuit},
circuit::{vp_circuit::ValidityPredicateCircuit, vp_examples::TrivialValidityPredicateCircuit},
constant::{NUM_NOTE, SETUP_PARAMS_MAP},
note::{Note, NoteType, RandomSeed},
nullifier::{Nullifier, NullifierKeyContainer},
Expand Down Expand Up @@ -87,7 +87,8 @@ fn bench_vp_proof(name: &str, c: &mut Criterion) {
vp_circuit.clone(),
&[public_inputs.inner()],
&mut rng,
);
)
.unwrap();
})
});

Expand All @@ -105,9 +106,9 @@ fn bench_vp_proof(name: &str, c: &mut Criterion) {
let verifier_name = name.to_string() + "-verifier";
c.bench_function(&verifier_name, |b| {
b.iter(|| {
proof
assert!(proof
.verify(pk.get_vk(), &params, &[public_inputs.inner()])
.is_ok();
.is_ok());
})
});
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ pub struct SudokuVPConfig {
sudoku_config: SudokuConfig,
}

impl ValidityPredicateConfig for SudokuVPConfig {
fn get_note_config(&self) -> NoteConfig {
self.note_config.clone()
}

fn configure(meta: &mut ConstraintSystem<pallas::Base>) -> Self {
let note_config = Self::configure_note(meta);
let sudoku_config = SudokuCircuit::configure(meta);
Self {
note_config,
sudoku_config,
}
}
}

#[derive(Clone, Debug, Default)]
pub struct SudokuVP {
pub sudoku: SudokuCircuit,
Expand All @@ -53,11 +38,9 @@ pub struct SudokuVP {
}

impl ValidityPredicateCircuit for SudokuVP {
type VPConfig = SudokuVPConfig;

fn custom_constraints(
&self,
config: Self::VPConfig,
config: ValidityPredicateConfig,
layouter: impl Layouter<pallas::Base>,
_basic_variables: BasicValidityPredicateVariables,
) -> Result<(), plonk::Error> {
Expand Down
File renamed without changes.
File renamed without changes.
111 changes: 46 additions & 65 deletions taiga_halo2/src/circuit/vp_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use crate::{
vp_vk::ValidityPredicateVerifyingKey,
};
use borsh::{BorshDeserialize, BorshSerialize};
// use byteorder::{ReadBytesExt, WriteBytesExt};
use dyn_clone::{clone_trait_object, DynClone};
use ff::PrimeField;
use group::cofactor::CofactorCurveAffine;
Expand All @@ -56,6 +55,8 @@ use vamp_ir::halo2::synth::{make_constant, Halo2Module, PrimeFieldOps};
use vamp_ir::transform::compile;
use vamp_ir::util::{read_inputs_from_file, Config};

pub type ValidityPredicate = dyn ValidityPredicateVerifyingInfo;

#[derive(Debug, Clone)]
pub struct VPVerifyingInfo {
pub vk: VerifyingKey<vesta::Affine>,
Expand Down Expand Up @@ -190,8 +191,22 @@ impl From<Vec<pallas::Base>> for ValidityPredicatePublicInputs {
}
}

pub trait ValidityPredicateConfig {
fn configure_note(meta: &mut ConstraintSystem<pallas::Base>) -> NoteConfig {
#[derive(Clone, Debug)]
pub struct ValidityPredicateConfig {
pub note_conifg: NoteConfig,
pub advices: [Column<Advice>; 10],
pub instances: Column<Instance>,
pub get_is_input_note_flag_config: GetIsInputNoteFlagConfig,
pub get_owned_note_variable_config: GetOwnedNoteVariableConfig,
pub conditional_equal_config: ConditionalEqualConfig,
pub extended_or_relation_config: ExtendedOrRelationConfig,
pub add_config: AddConfig,
pub sub_config: SubConfig,
pub mul_config: MulConfig,
}

impl ValidityPredicateConfig {
pub fn configure(meta: &mut ConstraintSystem<pallas::Base>) -> Self {
let instances = meta.instance_column();
meta.enable_equality(instances);

Expand All @@ -212,36 +227,7 @@ pub trait ValidityPredicateConfig {
meta.enable_equality(*advice);
}

NoteChip::configure(meta, instances, advices)
}
fn get_note_config(&self) -> NoteConfig;
fn configure(meta: &mut ConstraintSystem<pallas::Base>) -> Self;
}

#[derive(Clone, Debug)]
pub struct GeneralVerificationValidityPredicateConfig {
pub note_conifg: NoteConfig,
pub advices: [Column<Advice>; 10],
pub instances: Column<Instance>,
pub get_is_input_note_flag_config: GetIsInputNoteFlagConfig,
pub get_owned_note_variable_config: GetOwnedNoteVariableConfig,
pub conditional_equal_config: ConditionalEqualConfig,
pub extended_or_relation_config: ExtendedOrRelationConfig,
pub add_config: AddConfig,
pub sub_config: SubConfig,
pub mul_config: MulConfig,
}

impl ValidityPredicateConfig for GeneralVerificationValidityPredicateConfig {
fn get_note_config(&self) -> NoteConfig {
self.note_conifg.clone()
}

fn configure(meta: &mut ConstraintSystem<pallas::Base>) -> Self {
let note_conifg = Self::configure_note(meta);

let advices = note_conifg.advices;
let instances = note_conifg.instances;
let note_conifg = NoteChip::configure(meta, instances, advices);

let get_owned_note_variable_config = GetOwnedNoteVariableConfig::configure(
meta,
Expand Down Expand Up @@ -277,49 +263,22 @@ impl ValidityPredicateConfig for GeneralVerificationValidityPredicateConfig {
}
}

pub trait ValidityPredicateInfo {
fn get_input_notes(&self) -> &[Note; NUM_NOTE];
fn get_output_notes(&self) -> &[Note; NUM_NOTE];
fn get_mandatory_public_inputs(&self) -> Vec<pallas::Base> {
let mut public_inputs = vec![];
self.get_input_notes()
.iter()
.zip(self.get_output_notes().iter())
.for_each(|(input_note, output_note)| {
let nf = input_note.get_nf().unwrap().inner();
public_inputs.push(nf);
let cm = output_note.commitment();
public_inputs.push(cm.get_x());
});
public_inputs.push(self.get_owned_note_pub_id());
public_inputs
}
fn get_public_inputs(&self, rng: impl RngCore) -> ValidityPredicatePublicInputs;
// The owned_note_pub_id is the input_note_nf or the output_note_cm_x
// The owned_note_pub_id is the key to look up the target variables and
// help determine whether the owned note is the input note or not in VP circuit.
fn get_owned_note_pub_id(&self) -> pallas::Base;
}

pub trait ValidityPredicateVerifyingInfo: DynClone {
fn get_verifying_info(&self) -> VPVerifyingInfo;
fn get_vp_vk(&self) -> ValidityPredicateVerifyingKey;
}

clone_trait_object!(ValidityPredicateVerifyingInfo);

pub trait ValidityPredicateCircuit:
Circuit<pallas::Base> + ValidityPredicateInfo + ValidityPredicateVerifyingInfo
{
type VPConfig: ValidityPredicateConfig + Clone;
pub trait ValidityPredicateCircuit: Circuit<pallas::Base> + ValidityPredicateVerifyingInfo {
// Default implementation, constrains the notes integrity.
// TODO: how to enforce the constraints in vp circuit?
fn basic_constraints(
&self,
config: Self::VPConfig,
config: ValidityPredicateConfig,
mut layouter: impl Layouter<pallas::Base>,
) -> Result<BasicValidityPredicateVariables, Error> {
let note_config = config.get_note_config();
let note_config = config.note_conifg;
// Load the Sinsemilla generator lookup table used by the whole circuit.
SinsemillaChip::<NoteCommitmentHashDomain, NoteCommitmentDomain, TaigaFixedBases>::load(
note_config.sinsemilla_config.clone(),
Expand Down Expand Up @@ -402,12 +361,34 @@ pub trait ValidityPredicateCircuit:
// Add custom constraints on basic note variables and user-defined variables.
fn custom_constraints(
&self,
_config: Self::VPConfig,
_config: ValidityPredicateConfig,
mut _layouter: impl Layouter<pallas::Base>,
_basic_variables: BasicValidityPredicateVariables,
) -> Result<(), Error> {
Ok(())
}

fn get_mandatory_public_inputs(&self) -> Vec<pallas::Base> {
let mut public_inputs = vec![];
self.get_input_notes()
.iter()
.zip(self.get_output_notes().iter())
.for_each(|(input_note, output_note)| {
let nf = input_note.get_nf().unwrap().inner();
public_inputs.push(nf);
let cm = output_note.commitment();
public_inputs.push(cm.get_x());
});
public_inputs.push(self.get_owned_note_pub_id());
public_inputs
}
fn get_input_notes(&self) -> &[Note; NUM_NOTE];
fn get_output_notes(&self) -> &[Note; NUM_NOTE];
fn get_public_inputs(&self, rng: impl RngCore) -> ValidityPredicatePublicInputs;
// The owned_note_pub_id is the input_note_nf or the output_note_cm_x
// The owned_note_pub_id is the key to look up the target variables and
// help determine whether the owned note is the input note or not in VP circuit.
fn get_owned_note_pub_id(&self) -> pallas::Base;
}

/// BasicValidityPredicateVariables are generally constrained in ValidityPredicateCircuit::basic_constraints
Expand Down Expand Up @@ -704,7 +685,7 @@ impl BasicValidityPredicateVariables {
macro_rules! vp_circuit_impl {
($name:ident) => {
impl Circuit<pallas::Base> for $name {
type Config = <Self as ValidityPredicateCircuit>::VPConfig;
type Config = ValidityPredicateConfig;
type FloorPlanner = floor_planner::V1;

fn without_witnesses(&self) -> Self {
Expand Down
13 changes: 4 additions & 9 deletions taiga_halo2/src/circuit/vp_examples.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
circuit::vp_circuit::{
GeneralVerificationValidityPredicateConfig, VPVerifyingInfo, ValidityPredicateCircuit,
ValidityPredicateConfig, ValidityPredicateInfo, ValidityPredicatePublicInputs,
ValidityPredicateVerifyingInfo,
VPVerifyingInfo, ValidityPredicateCircuit, ValidityPredicateConfig,
ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo,
},
constant::{NUM_NOTE, SETUP_PARAMS_MAP},
note::{Note, RandomSeed},
Expand Down Expand Up @@ -54,7 +53,7 @@ impl TrivialValidityPredicateCircuit {
}
}

impl ValidityPredicateInfo for TrivialValidityPredicateCircuit {
impl ValidityPredicateCircuit for TrivialValidityPredicateCircuit {
fn get_input_notes(&self) -> &[Note; NUM_NOTE] {
&self.input_notes
}
Expand All @@ -78,10 +77,6 @@ impl ValidityPredicateInfo for TrivialValidityPredicateCircuit {
}
}

impl ValidityPredicateCircuit for TrivialValidityPredicateCircuit {
type VPConfig = GeneralVerificationValidityPredicateConfig;
}

vp_circuit_impl!(TrivialValidityPredicateCircuit);

#[cfg(test)]
Expand Down Expand Up @@ -110,7 +105,7 @@ pub mod tests {

#[test]
fn test_halo2_trivial_vp_circuit() {
use crate::circuit::vp_circuit::ValidityPredicateInfo;
use crate::circuit::vp_circuit::ValidityPredicateCircuit;
use halo2_proofs::dev::MockProver;
use rand::rngs::OsRng;

Expand Down
52 changes: 24 additions & 28 deletions taiga_halo2/src/circuit/vp_examples/cascade_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use crate::{
target_note_variable::{get_is_input_note_flag, get_owned_note_variable},
},
vp_circuit::{
BasicValidityPredicateVariables, GeneralVerificationValidityPredicateConfig,
VPVerifyingInfo, ValidityPredicateCircuit, ValidityPredicateConfig,
ValidityPredicateInfo, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo,
BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit,
ValidityPredicateConfig, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo,
},
},
constant::{NUM_NOTE, SETUP_PARAMS_MAP},
Expand Down Expand Up @@ -54,32 +53,7 @@ impl CascadeIntentValidityPredicateCircuit {
}
}

impl ValidityPredicateInfo for CascadeIntentValidityPredicateCircuit {
fn get_input_notes(&self) -> &[Note; NUM_NOTE] {
&self.input_notes
}

fn get_output_notes(&self) -> &[Note; NUM_NOTE] {
&self.output_notes
}

fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs {
let mut public_inputs = self.get_mandatory_public_inputs();
let padding = ValidityPredicatePublicInputs::get_public_input_padding(
public_inputs.len(),
&RandomSeed::random(&mut rng),
);
public_inputs.extend(padding);
public_inputs.into()
}

fn get_owned_note_pub_id(&self) -> pallas::Base {
self.owned_note_pub_id
}
}

impl ValidityPredicateCircuit for CascadeIntentValidityPredicateCircuit {
type VPConfig = GeneralVerificationValidityPredicateConfig;
// Add custom constraints
fn custom_constraints(
&self,
Expand Down Expand Up @@ -133,6 +107,28 @@ impl ValidityPredicateCircuit for CascadeIntentValidityPredicateCircuit {

Ok(())
}

fn get_input_notes(&self) -> &[Note; NUM_NOTE] {
&self.input_notes
}

fn get_output_notes(&self) -> &[Note; NUM_NOTE] {
&self.output_notes
}

fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs {
let mut public_inputs = self.get_mandatory_public_inputs();
let padding = ValidityPredicatePublicInputs::get_public_input_padding(
public_inputs.len(),
&RandomSeed::random(&mut rng),
);
public_inputs.extend(padding);
public_inputs.into()
}

fn get_owned_note_pub_id(&self) -> pallas::Base {
self.owned_note_pub_id
}
}

vp_circuit_impl!(CascadeIntentValidityPredicateCircuit);
Expand Down
Loading

0 comments on commit fc9717a

Please sign in to comment.