Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement remaining public x private refactorings for circuits #3595

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions crates/bench/benches/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,20 @@ use ark_relations::r1cs::{
use decaf377::{Fq, Fr};
use decaf377_fmd as fmd;
use decaf377_ka as ka;
use penumbra_asset::{balance, Balance, Value};
use penumbra_asset::{Balance, Value};
use penumbra_keys::{keys::Diversifier, Address};
use penumbra_proof_params::OUTPUT_PROOF_PROVING_KEY;
use penumbra_shielded_pool::{note, Note, OutputCircuit, OutputProof, Rseed};
use penumbra_proof_params::{DummyWitness, OUTPUT_PROOF_PROVING_KEY};
use penumbra_shielded_pool::{
output::{OutputProofPrivate, OutputProofPublic},
Note, OutputCircuit, OutputProof, Rseed,
};

use criterion::{criterion_group, criterion_main, Criterion};
use rand_core::OsRng;

fn prove(
r: Fq,
s: Fq,
note: Note,
v_blinding: Fr,
balance_commitment: balance::Commitment,
note_commitment: note::StateCommitment,
) {
let _proof = OutputProof::prove(
r,
s,
&OUTPUT_PROOF_PROVING_KEY,
note,
v_blinding,
balance_commitment,
note_commitment,
)
.expect("can generate proof");
fn prove(r: Fq, s: Fq, public: OutputProofPublic, private: OutputProofPrivate) {
let _proof = OutputProof::prove(r, s, &OUTPUT_PROOF_PROVING_KEY, public, private)
.expect("can generate proof");
}

fn output_proving_time(c: &mut Criterion) {
Expand All @@ -49,28 +37,28 @@ fn output_proving_time(c: &mut Criterion) {
let value_to_send = Value::from_str("1upenumbra").expect("valid value");

let note = Note::from_parts(address, value_to_send, Rseed([1u8; 32])).expect("can make a note");
let v_blinding = Fr::from(1);
let balance_commitment = (-Balance::from(value_to_send)).commit(v_blinding);
let balance_blinding = Fr::from(1);
let balance_commitment = (-Balance::from(value_to_send)).commit(balance_blinding);
let note_commitment = note.commit();

let public = OutputProofPublic {
balance_commitment,
note_commitment,
};
let private = OutputProofPrivate {
note,
balance_blinding,
};

let r = Fq::rand(&mut OsRng);
let s = Fq::rand(&mut OsRng);

c.bench_function("output proving", |b| {
b.iter(|| {
prove(
r,
s,
note.clone(),
v_blinding,
balance_commitment,
note_commitment,
)
})
b.iter(|| prove(r, s, public.clone(), private.clone()))
});

// Also print out the number of constraints.
let circuit = OutputCircuit::new(note, v_blinding, balance_commitment);
let circuit = OutputCircuit::with_dummy_witness();

let cs = ConstraintSystem::new_ref();
cs.set_optimization_goal(OptimizationGoal::Constraints);
Expand Down
65 changes: 21 additions & 44 deletions crates/bench/benches/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,23 @@ use ark_relations::r1cs::{
ConstraintSynthesizer, ConstraintSystem, OptimizationGoal, SynthesisMode,
};
use decaf377::{Fq, Fr};
use penumbra_asset::{asset, balance, Balance, Value};
use penumbra_asset::{asset, Balance, Value};
use penumbra_dex::{
swap::proof::{SwapCircuit, SwapProof},
swap::SwapPlaintext,
swap::proof::{SwapCircuit, SwapProof, SwapProofPublic},
swap::{proof::SwapProofPrivate, SwapPlaintext},
TradingPair,
};
use penumbra_fee::Fee;
use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey};
use penumbra_num::Amount;
use penumbra_proof_params::SWAP_PROOF_PROVING_KEY;
use penumbra_tct as tct;
use penumbra_proof_params::{DummyWitness, SWAP_PROOF_PROVING_KEY};

use criterion::{criterion_group, criterion_main, Criterion};
use rand_core::OsRng;

fn prove(
r: Fq,
s: Fq,
swap_plaintext: SwapPlaintext,
fee_blinding: Fr,
balance_commitment: balance::Commitment,
swap_commitment: tct::StateCommitment,
fee_commitment: balance::Commitment,
) {
let _proof = SwapProof::prove(
r,
s,
&SWAP_PROOF_PROVING_KEY,
swap_plaintext,
fee_blinding,
balance_commitment,
swap_commitment,
fee_commitment,
)
.expect("can generate proof");
fn prove(r: Fq, s: Fq, public: SwapProofPublic, private: SwapProofPrivate) {
let _proof = SwapProof::prove(r, s, &SWAP_PROOF_PROVING_KEY, public, private)
.expect("can generate proof");
}

fn swap_proving_time(c: &mut Criterion) {
Expand All @@ -63,7 +45,8 @@ fn swap_proving_time(c: &mut Criterion) {
fee,
claim_address,
);
let fee_commitment = swap_plaintext.claim_fee.commit(Fr::from(0u64));
let fee_blinding = Fr::from(0u64);
let fee_commitment = swap_plaintext.claim_fee.commit(fee_blinding);
let swap_commitment = swap_plaintext.swap_commitment();

let value_1 = Value {
Expand All @@ -84,31 +67,25 @@ fn swap_proving_time(c: &mut Criterion) {
balance -= value_fee;
let balance_commitment = balance.commit(Fr::from(0u64));

let public = SwapProofPublic {
balance_commitment,
swap_commitment,
fee_commitment,
};
let private = SwapProofPrivate {
fee_blinding,
swap_plaintext,
};

let r = Fq::rand(&mut OsRng);
let s = Fq::rand(&mut OsRng);

c.bench_function("swap proving", |b| {
b.iter(|| {
prove(
r,
s,
swap_plaintext.clone(),
Fr::from(0u64),
balance_commitment,
swap_commitment,
fee_commitment,
)
})
b.iter(|| prove(r, s, public.clone(), private.clone()))
});

// Also print out the number of constraints.
let circuit = SwapCircuit::new(
swap_plaintext,
Fr::from(0u64),
balance_commitment,
swap_commitment,
fee_commitment,
);
let circuit = SwapCircuit::with_dummy_witness();

let cs = ConstraintSystem::new_ref();
cs.set_optimization_goal(OptimizationGoal::Constraints);
Expand Down
98 changes: 27 additions & 71 deletions crates/bench/benches/swap_claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,23 @@ use decaf377::Fq;
use penumbra_asset::asset;
use penumbra_dex::{
swap::SwapPlaintext,
swap_claim::{SwapClaimCircuit, SwapClaimProof},
swap_claim::{SwapClaimCircuit, SwapClaimProof, SwapClaimProofPrivate, SwapClaimProofPublic},
BatchSwapOutputData, TradingPair,
};
use penumbra_fee::Fee;
use penumbra_keys::keys::{Bip44Path, NullifierKey, SeedPhrase, SpendKey};
use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey};
use penumbra_num::Amount;
use penumbra_proof_params::SWAPCLAIM_PROOF_PROVING_KEY;
use penumbra_proof_params::{DummyWitness, SWAPCLAIM_PROOF_PROVING_KEY};
use penumbra_sct::Nullifier;
use penumbra_tct as tct;

use criterion::{criterion_group, criterion_main, Criterion};
use rand_core::OsRng;

#[allow(clippy::too_many_arguments)]
fn prove(
r: Fq,
s: Fq,
swap_plaintext: SwapPlaintext,
state_commitment_proof: tct::Proof,
nk: NullifierKey,
anchor: tct::Root,
nullifier: Nullifier,
lambda_1: Amount,
lambda_2: Amount,
note_blinding_1: Fq,
note_blinding_2: Fq,
note_commitment_1: tct::StateCommitment,
note_commitment_2: tct::StateCommitment,
output_data: BatchSwapOutputData,
) {
let _proof = SwapClaimProof::prove(
r,
s,
&SWAPCLAIM_PROOF_PROVING_KEY,
swap_plaintext,
state_commitment_proof,
nk,
anchor,
nullifier,
lambda_1,
lambda_2,
note_blinding_1,
note_blinding_2,
note_commitment_1,
note_commitment_2,
output_data,
)
.expect("can create proof");
fn prove(r: Fq, s: Fq, public: SwapClaimProofPublic, private: SwapClaimProofPrivate) {
let _proof = SwapClaimProof::prove(r, s, &SWAPCLAIM_PROOF_PROVING_KEY, public, private)
.expect("can create proof");
}

fn swap_claim_proving_time(c: &mut Criterion) {
Expand All @@ -80,7 +49,7 @@ fn swap_claim_proving_time(c: &mut Criterion) {
fee,
claim_address,
);
let fee = swap_plaintext.clone().claim_fee;
let claim_fee = swap_plaintext.clone().claim_fee;
let mut sct = tct::Tree::new();
let swap_commitment = swap_plaintext.swap_commitment();
sct.insert(tct::Witness::Keep, swap_commitment).unwrap();
Expand Down Expand Up @@ -111,46 +80,33 @@ fn swap_claim_proving_time(c: &mut Criterion) {
let note_commitment_1 = output_1_note.commit();
let note_commitment_2 = output_2_note.commit();

let r = Fq::rand(&mut OsRng);
let s = Fq::rand(&mut OsRng);

c.bench_function("swap claim proving", |b| {
b.iter(|| {
prove(
r,
s,
swap_plaintext.clone(),
state_commitment_proof.clone(),
nk,
anchor,
nullifier,
lambda_1,
lambda_2,
note_blinding_1,
note_blinding_2,
note_commitment_1,
note_commitment_2,
output_data,
)
})
});

// Also print out the number of constraints.
let circuit = SwapClaimCircuit::new(
let public = SwapClaimProofPublic {
anchor,
nullifier,
claim_fee,
output_data,
note_commitment_1,
note_commitment_2,
};
let private = SwapClaimProofPrivate {
swap_plaintext,
state_commitment_proof,
nk,
lambda_1,
lambda_2,
note_blinding_1,
note_blinding_2,
anchor,
nullifier,
fee,
output_data,
note_commitment_1,
note_commitment_2,
);
};

let r = Fq::rand(&mut OsRng);
let s = Fq::rand(&mut OsRng);

c.bench_function("swap claim proving", |b| {
b.iter(|| prove(r, s, public.clone(), private.clone()))
});

// Also print out the number of constraints.
let circuit = SwapClaimCircuit::with_dummy_witness();

let cs = ConstraintSystem::new_ref();
cs.set_optimization_goal(OptimizationGoal::Constraints);
Expand Down
Loading
Loading