Skip to content

Commit

Permalink
refactor: define DelegatorVoteProof public and private data
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Jan 4, 2024
1 parent 00aed31 commit 76c0f8f
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 182 deletions.
88 changes: 26 additions & 62 deletions crates/bench/benches/delegator_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use ark_relations::r1cs::{
};
use decaf377::{Fq, Fr};
use decaf377_rdsa::{SpendAuth, VerificationKey};
use penumbra_asset::{balance, Value};
use penumbra_governance::{DelegatorVoteCircuit, DelegatorVoteProof};
use penumbra_keys::keys::{Bip44Path, NullifierKey, SeedPhrase, SpendKey};
use penumbra_asset::Value;
use penumbra_governance::{
DelegatorVoteCircuit, DelegatorVoteProof, DelegatorVoteProofPrivate, DelegatorVoteProofPublic,
};
use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey};
use penumbra_proof_params::DELEGATOR_VOTE_PROOF_PROVING_KEY;
use penumbra_sct::Nullifier;
use penumbra_shielded_pool::Note;
Expand All @@ -18,36 +20,10 @@ use criterion::{criterion_group, criterion_main, Criterion};
use rand_core::OsRng;

#[allow(clippy::too_many_arguments)]
fn prove(
r: Fq,
s: Fq,
state_commitment_proof: tct::Proof,
note: Note,
spend_auth_randomizer: Fr,
ak: VerificationKey<SpendAuth>,
nk: NullifierKey,
anchor: tct::Root,
balance_commitment: balance::Commitment,
nullifier: Nullifier,
rk: VerificationKey<SpendAuth>,
start_position: tct::Position,
) {
let _proof = DelegatorVoteProof::prove(
r,
s,
&DELEGATOR_VOTE_PROOF_PROVING_KEY,
state_commitment_proof,
note,
spend_auth_randomizer,
ak,
nk,
anchor,
balance_commitment,
nullifier,
rk,
start_position,
)
.expect("can create proof");
fn prove(r: Fq, s: Fq, public: DelegatorVoteProofPublic, private: DelegatorVoteProofPrivate) {
let _proof =
DelegatorVoteProof::prove(r, s, &DELEGATOR_VOTE_PROOF_PROVING_KEY, public, private)
.expect("can create proof");
}

fn delegator_vote_proving_time(c: &mut Criterion) {
Expand All @@ -73,7 +49,7 @@ fn delegator_vote_proving_time(c: &mut Criterion) {
let v_blinding = Fr::from(0i32);
let balance_commitment = value_to_send.commit(v_blinding);
let rk: VerificationKey<SpendAuth> = rsk.into();
let nf = Nullifier::derive(&nk, state_commitment_proof.position(), &note_commitment);
let nullifier = Nullifier::derive(&nk, state_commitment_proof.position(), &note_commitment);
sct.end_epoch().unwrap();

let first_note_commitment = Note::generate(&mut OsRng, &sender, value_to_send).commit();
Expand All @@ -83,40 +59,28 @@ fn delegator_vote_proving_time(c: &mut Criterion) {

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

c.bench_function("delegator proving", |b| {
b.iter(|| {
prove(
r,
s,
state_commitment_proof.clone(),
note.clone(),
spend_auth_randomizer,
ak,
nk,
anchor,
balance_commitment,
nf,
rk,
start_position,
)
})
});

// Also print out the number of constraints.
let circuit = DelegatorVoteCircuit::new(
let public = DelegatorVoteProofPublic {
anchor,
balance_commitment,
nullifier,
rk,
start_position,
};
let private = DelegatorVoteProofPrivate {
state_commitment_proof,
note,
v_blinding,
spend_auth_randomizer,
ak,
nk,
anchor,
balance_commitment,
nf,
rk,
start_position,
);
};

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

// Also print out the number of constraints.
let circuit = DelegatorVoteCircuit::new(public, private);

let cs = ConstraintSystem::new_ref();
cs.set_optimization_goal(OptimizationGoal::Constraints);
Expand Down
29 changes: 16 additions & 13 deletions crates/bin/pcli/tests/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use penumbra_dex::{
BatchSwapOutputData, TradingPair,
};
use penumbra_fee::Fee;
use penumbra_governance::DelegatorVoteProof;
use penumbra_governance::{
DelegatorVoteProof, DelegatorVoteProofPrivate, DelegatorVoteProofPublic,
};
use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey};
use penumbra_num::Amount;
use penumbra_proof_params::{
Expand Down Expand Up @@ -133,24 +135,25 @@ fn delegator_vote_proof_parameters_vs_current_delegator_vote_circuit() {
let blinding_r = Fq::rand(&mut OsRng);
let blinding_s = Fq::rand(&mut OsRng);

let proof = DelegatorVoteProof::prove(
blinding_r,
blinding_s,
pk,
let public = DelegatorVoteProofPublic {
anchor,
balance_commitment,
nullifier: nf,
rk,
start_position,
};
let private = DelegatorVoteProofPrivate {
state_commitment_proof,
note,
v_blinding: Fr::from(0u64),
spend_auth_randomizer,
ak,
nk,
anchor,
balance_commitment,
nf,
rk,
start_position,
)
.expect("can create proof");
};
let proof = DelegatorVoteProof::prove(blinding_r, blinding_s, pk, public.clone(), private)
.expect("can create proof");

let proof_result = proof.verify(vk, anchor, balance_commitment, nf, rk, start_position);
let proof_result = proof.verify(vk, public);
assert!(proof_result.is_ok());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use penumbra_proof_params::DELEGATOR_VOTE_PROOF_VERIFICATION_KEY;
use penumbra_txhash::TransactionContext;

use crate::{
DelegatorVote, DelegatorVoteBody,
DelegatorVote, DelegatorVoteBody, DelegatorVoteProofPublic,
{component::StateWriteExt, StateReadExt},
};
use cnidarium_component::ActionHandler;
Expand Down Expand Up @@ -40,15 +40,15 @@ impl ActionHandler for DelegatorVote {
.context("delegator vote auth signature failed to verify")?;

// 2. Verify the proof against the provided anchor and start position:
let public = DelegatorVoteProofPublic {
anchor: context.anchor,
balance_commitment: value.commit(Fr::zero()),
nullifier: *nullifier,
rk: *rk,
start_position: *start_position,
};
proof
.verify(
&DELEGATOR_VOTE_PROOF_VERIFICATION_KEY,
context.anchor,
value.commit(Fr::zero()),
*nullifier,
*rk,
*start_position,
)
.verify(&DELEGATOR_VOTE_PROOF_VERIFICATION_KEY, public)
.context("a delegator vote proof did not verify")?;

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion crates/core/component/governance/src/delegator_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ pub mod view;

pub use action::{DelegatorVote, DelegatorVoteBody};
pub use plan::DelegatorVotePlan;
pub use proof::{DelegatorVoteCircuit, DelegatorVoteProof};
pub use proof::{
DelegatorVoteCircuit, DelegatorVoteProof, DelegatorVoteProofPrivate, DelegatorVoteProofPublic,
};
pub use view::DelegatorVoteView;
29 changes: 19 additions & 10 deletions crates/core/component/governance/src/delegator_vote/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use serde::{Deserialize, Serialize};

use crate::delegator_vote::action::{DelegatorVote, DelegatorVoteBody};
use crate::delegator_vote::proof::DelegatorVoteProof;
use crate::DelegatorVoteProofPrivate;
use crate::DelegatorVoteProofPublic;
use crate::{vote::Vote, VotingReceiptToken};

/// A plan to vote as a delegator.
Expand Down Expand Up @@ -103,20 +105,27 @@ impl DelegatorVotePlan {
fvk: &FullViewingKey,
state_commitment_proof: tct::Proof,
) -> DelegatorVoteProof {
let public = DelegatorVoteProofPublic {
anchor: state_commitment_proof.root(),
balance_commitment: self.staked_note.value().commit(Fr::zero()),
nullifier: self.nullifier(fvk),
rk: self.rk(fvk),
start_position: self.start_position,
};
let private = DelegatorVoteProofPrivate {
state_commitment_proof,
note: self.staked_note.clone(),
v_blinding: Fr::from(0),
spend_auth_randomizer: self.randomizer,
ak: *fvk.spend_verification_key(),
nk: *fvk.nullifier_key(),
};
DelegatorVoteProof::prove(
self.proof_blinding_r,
self.proof_blinding_s,
&DELEGATOR_VOTE_PROOF_PROVING_KEY,
state_commitment_proof.clone(),
self.staked_note.clone(),
self.randomizer,
*fvk.spend_verification_key(),
*fvk.nullifier_key(),
state_commitment_proof.root(),
self.staked_note.value().commit(Fr::zero()),
self.nullifier(fvk),
self.rk(fvk),
self.start_position,
public,
private,
)
.expect("can generate ZK delegator vote proof")
}
Expand Down
Loading

0 comments on commit 76c0f8f

Please sign in to comment.