Skip to content

Commit

Permalink
Rename undelegate_claim to convert in all relevant places
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby authored and redshiftzero committed Jan 5, 2024
1 parent 23897d5 commit abe8585
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 121 deletions.
4 changes: 2 additions & 2 deletions crates/bench/benches/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ark_relations::r1cs::{
use decaf377::{Fq, Fr};
use penumbra_asset::{Balance, Value, STAKING_TOKEN_ASSET_ID};
use penumbra_num::{fixpoint::U128x128, Amount};
use penumbra_proof_params::{DummyWitness, UNDELEGATECLAIM_PROOF_PROVING_KEY};
use penumbra_proof_params::{DummyWitness, CONVERT_PROOF_PROVING_KEY};
use penumbra_shielded_pool::{
ConvertCircuit, ConvertProof, ConvertProofPrivate, ConvertProofPublic,
};
Expand All @@ -14,7 +14,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
use rand_core::OsRng;

fn prove(r: Fq, s: Fq, public: ConvertProofPublic, private: ConvertProofPrivate) {
let _proof = ConvertProof::prove(r, s, &UNDELEGATECLAIM_PROOF_PROVING_KEY, public, private)
let _proof = ConvertProof::prove(r, s, &CONVERT_PROOF_PROVING_KEY, public, private)
.expect("can generate proof");
}

Expand Down
16 changes: 8 additions & 8 deletions crates/bin/pcli/tests/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ use penumbra_governance::{
use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey};
use penumbra_num::Amount;
use penumbra_proof_params::{
DELEGATOR_VOTE_PROOF_PROVING_KEY, DELEGATOR_VOTE_PROOF_VERIFICATION_KEY,
NULLIFIER_DERIVATION_PROOF_PROVING_KEY, NULLIFIER_DERIVATION_PROOF_VERIFICATION_KEY,
OUTPUT_PROOF_PROVING_KEY, OUTPUT_PROOF_VERIFICATION_KEY, SPEND_PROOF_PROVING_KEY,
SPEND_PROOF_VERIFICATION_KEY, SWAPCLAIM_PROOF_PROVING_KEY, SWAPCLAIM_PROOF_VERIFICATION_KEY,
SWAP_PROOF_PROVING_KEY, SWAP_PROOF_VERIFICATION_KEY, UNDELEGATECLAIM_PROOF_PROVING_KEY,
UNDELEGATECLAIM_PROOF_VERIFICATION_KEY,
CONVERT_PROOF_PROVING_KEY, CONVERT_PROOF_VERIFICATION_KEY, DELEGATOR_VOTE_PROOF_PROVING_KEY,
DELEGATOR_VOTE_PROOF_VERIFICATION_KEY, NULLIFIER_DERIVATION_PROOF_PROVING_KEY,
NULLIFIER_DERIVATION_PROOF_VERIFICATION_KEY, OUTPUT_PROOF_PROVING_KEY,
OUTPUT_PROOF_VERIFICATION_KEY, SPEND_PROOF_PROVING_KEY, SPEND_PROOF_VERIFICATION_KEY,
SWAPCLAIM_PROOF_PROVING_KEY, SWAPCLAIM_PROOF_VERIFICATION_KEY, SWAP_PROOF_PROVING_KEY,
SWAP_PROOF_VERIFICATION_KEY,
};
use penumbra_sct::Nullifier;
use penumbra_shielded_pool::Note;
Expand Down Expand Up @@ -409,8 +409,8 @@ fn nullifier_derivation_parameters_vs_current_nullifier_derivation_circuit() {

#[test]
fn undelegate_claim_parameters_vs_current_undelegate_claim_circuit() {
let pk = &*UNDELEGATECLAIM_PROOF_PROVING_KEY;
let vk = &*UNDELEGATECLAIM_PROOF_VERIFICATION_KEY;
let pk = &*CONVERT_PROOF_PROVING_KEY;
let vk = &*CONVERT_PROOF_VERIFICATION_KEY;

let mut rng = OsRng;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{ensure, Result};
use async_trait::async_trait;
use cnidarium::{StateRead, StateWrite};
use penumbra_chain::component::StateReadExt;
use penumbra_proof_params::UNDELEGATECLAIM_PROOF_VERIFICATION_KEY;
use penumbra_proof_params::CONVERT_PROOF_VERIFICATION_KEY;

use crate::UndelegateClaim;
use crate::{action_handler::ActionHandler, StateReadExt as _, UnbondingToken};
Expand All @@ -17,7 +17,7 @@ impl ActionHandler for UndelegateClaim {
UnbondingToken::new(self.body.validator_identity, self.body.start_epoch_index).id();

self.proof.verify(
&UNDELEGATECLAIM_PROOF_VERIFICATION_KEY,
&CONVERT_PROOF_VERIFICATION_KEY,
self.body.balance_commitment,
unbonding_id,
self.body.penalty,
Expand Down
2 changes: 1 addition & 1 deletion crates/core/component/stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod unbonding_token;
pub use delegation_token::DelegationToken;
pub use governance_key::GovernanceKey;
pub use identity_key::IdentityKey;
pub use penalty::{Penalty, PenaltyVar};
pub use penalty::Penalty;
pub use unbonding_token::UnbondingToken;

pub use changes::DelegationChanges;
Expand Down
80 changes: 2 additions & 78 deletions crates/core/component/stake/src/penalty.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
use ark_ff::ToConstraintField;
use ark_r1cs_std::prelude::*;
use ark_relations::r1cs::SynthesisError;
use decaf377::Fq;
use penumbra_proto::{penumbra::core::component::stake::v1alpha1 as pbs, DomainType};
use serde::{Deserialize, Serialize};

use penumbra_asset::{
asset::{self, AssetIdVar},
balance::BalanceVar,
Balance, Value, ValueVar, STAKING_TOKEN_ASSET_ID,
};
use penumbra_num::{
fixpoint::{U128x128, U128x128Var},
Amount, AmountVar,
};
use penumbra_asset::{asset, Balance, Value, STAKING_TOKEN_ASSET_ID};
use penumbra_num::{fixpoint::U128x128, Amount};

/// Tracks slashing penalties applied to a validator in some epoch.
///
Expand Down Expand Up @@ -124,73 +115,6 @@ impl<'a> TryFrom<&'a [u8]> for Penalty {
}
}

/// One explicit choice in this in circuit representation is that we
/// DO NOT CHECK THAT THE PENALTY IS <= 1 IN CIRCUIT. This is in practice
/// the UndelegateClaim circuit is the ONLY consumer of this type, and
/// in the context of that circuit, the penalty is checked out of circuit
/// to conform to a real value which will be <= 1.
///
/// I repeat myself: IF YOU EVER USE THIS IN A DIFFERENT CIRCUIT, MAKE ABSOLUTELY
/// CERTAIN THAT A PENALTY BEING > 1 IN CIRCUIT IS NOT AN ISSUE.
pub struct PenaltyVar {
inner: U128x128Var,
}

impl AllocVar<Penalty, Fq> for PenaltyVar {
fn new_variable<T: std::borrow::Borrow<Penalty>>(
cs: impl Into<ark_relations::r1cs::Namespace<Fq>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
mode: ark_r1cs_std::prelude::AllocationMode,
) -> Result<Self, SynthesisError> {
assert!(
matches!(mode, ark_r1cs_std::prelude::AllocationMode::Input),
"Penalty must be an input variable"
);
Ok(Self {
inner: U128x128Var::new_variable(cs, || Ok(f()?.borrow().0), mode)?,
})
}
}

impl PenaltyVar {
pub fn apply_to(&self, amount: AmountVar) -> Result<AmountVar, SynthesisError> {
U128x128Var::from_amount_var(amount)?
.checked_mul(&self.inner)?
.round_down_to_amount()
}

pub fn balance_for_claim(
&self,
unbonding_id: AssetIdVar,
unbonding_amount: AmountVar,
) -> Result<BalanceVar, SynthesisError> {
let negative_value = BalanceVar::from_negative_value_var(ValueVar {
amount: unbonding_amount.clone(),
asset_id: unbonding_id,
});
let staking_token_asset_id_var =
AssetIdVar::new_witness(self.cs(), || Ok(*STAKING_TOKEN_ASSET_ID))?;

let positive_value = BalanceVar::from_positive_value_var(ValueVar {
amount: self.apply_to(unbonding_amount)?,
asset_id: staking_token_asset_id_var,
});
Ok(negative_value + positive_value)
}
}

impl R1CSVar<Fq> for PenaltyVar {
type Value = Penalty;

fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<Fq> {
self.inner.cs()
}

fn value(&self) -> Result<Self::Value, SynthesisError> {
Ok(Penalty(self.inner.value()?))
}
}

impl DomainType for Penalty {
type Proto = pbs::Penalty;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/component/stake/src/undelegate_claim/plan.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use decaf377::{FieldExt, Fq, Fr};
use penumbra_asset::{asset, balance, Balance};
use penumbra_num::Amount;
use penumbra_proof_params::UNDELEGATECLAIM_PROOF_PROVING_KEY;
use penumbra_proof_params::CONVERT_PROOF_PROVING_KEY;
use penumbra_proto::{penumbra::core::component::stake::v1alpha1 as pb, DomainType};

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -55,7 +55,7 @@ impl UndelegateClaimPlan {
UndelegateClaimProof::prove(
self.proof_blinding_r,
self.proof_blinding_s,
&UNDELEGATECLAIM_PROOF_PROVING_KEY,
&CONVERT_PROOF_PROVING_KEY,
self.unbonding_amount,
self.balance_blinding,
self.balance_commitment(),
Expand Down
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
"src/gen/spend_pk.bin",
"src/gen/swap_pk.bin",
"src/gen/swapclaim_pk.bin",
"src/gen/undelegateclaim_pk.bin",
"src/gen/convert_pk.bin",
"src/gen/delegator_vote_pk.bin",
"src/gen/nullifier_derivation_pk.bin",
];
Expand All @@ -18,7 +18,7 @@ fn main() {
"src/gen/spend_vk.param",
"src/gen/swap_vk.param",
"src/gen/swapclaim_vk.param",
"src/gen/undelegateclaim_vk.param",
"src/gen/convert_vk.param",
"src/gen/delegator_vote_vk.param",
"src/gen/nullifier_derivation_vk.param",
];
Expand Down
26 changes: 13 additions & 13 deletions crates/crypto/proof-params/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,24 @@ pub mod swapclaim {
include!("gen/swapclaim_id.rs");
}

/// Proving key for the undelegateclaim proof.
pub static UNDELEGATECLAIM_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let undelegate_claim_proving_key = LazyProvingKey::new(undelegateclaim::PROVING_KEY_ID);
/// Proving key for the convert proof.
pub static CONVERT_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let convert_proving_key = LazyProvingKey::new(convert::PROVING_KEY_ID);

#[cfg(feature = "bundled-proving-keys")]
undelegate_claim_proving_key
.try_load(include_bytes!("gen/undelegateclaim_pk.bin"))
convert_proving_key
.try_load(include_bytes!("gen/convert_pk.bin"))
.expect("bundled proving key is valid");

undelegate_claim_proving_key
convert_proving_key
});

/// Verification key for the undelegateclaim proof.
pub static UNDELEGATECLAIM_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| undelegateclaim_verification_parameters().into());
/// Verification key for the convert proof.
pub static CONVERT_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| convert_verification_parameters().into());

pub mod undelegateclaim {
include!("gen/undelegateclaim_id.rs");
pub mod convert {
include!("gen/convert_id.rs");
}

/// Proving key for the delegator vote proof.
Expand Down Expand Up @@ -260,8 +260,8 @@ fn swapclaim_verification_parameters() -> VerifyingKey<Bls12_377> {
.expect("can deserialize VerifyingKey")
}

fn undelegateclaim_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/undelegateclaim_vk.param");
fn convert_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/convert_vk.param");
VerifyingKey::deserialize_uncompressed_unchecked(&vk_params[..])
.expect("can deserialize VerifyingKey")
}
Expand Down
8 changes: 4 additions & 4 deletions crates/wasm/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use wasm_bindgen::prelude::*;
use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey};
use penumbra_keys::{Address, FullViewingKey};
use penumbra_proof_params::{
DELEGATOR_VOTE_PROOF_PROVING_KEY, NULLIFIER_DERIVATION_PROOF_PROVING_KEY,
OUTPUT_PROOF_PROVING_KEY, SPEND_PROOF_PROVING_KEY, SWAPCLAIM_PROOF_PROVING_KEY,
SWAP_PROOF_PROVING_KEY, UNDELEGATECLAIM_PROOF_PROVING_KEY,
CONVERT_PROOF_PROVING_KEY, DELEGATOR_VOTE_PROOF_PROVING_KEY,
NULLIFIER_DERIVATION_PROOF_PROVING_KEY, OUTPUT_PROOF_PROVING_KEY, SPEND_PROOF_PROVING_KEY,
SWAPCLAIM_PROOF_PROVING_KEY, SWAP_PROOF_PROVING_KEY,
};
use penumbra_proto::{core::keys::v1alpha1 as pb, serializers::bech32str, DomainType};
use wasm_bindgen_futures::js_sys::Uint8Array;
Expand All @@ -35,7 +35,7 @@ pub fn load_proving_key(parameters: JsValue, key_type: &str) -> WasmResult<()> {
"nullifier_derivation" => &NULLIFIER_DERIVATION_PROOF_PROVING_KEY,
"swap" => &SWAP_PROOF_PROVING_KEY,
"swap_claim" => &SWAPCLAIM_PROOF_PROVING_KEY,
"undelegate_claim" => &UNDELEGATECLAIM_PROOF_PROVING_KEY,
"convert" => &CONVERT_PROOF_PROVING_KEY,
_ => return Err(anyhow::anyhow!("Unsupported key type").into()),
};

Expand Down
6 changes: 2 additions & 4 deletions crates/wasm/tests/test_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ mod tests {
let swap_key: &[u8] = include_bytes!("../../crypto/proof-params/src/gen/swap_pk.bin");
let swap_claim_key: &[u8] =
include_bytes!("../../crypto/proof-params/src/gen/swapclaim_pk.bin");
let undelegate_claim_key: &[u8] =
include_bytes!("../../crypto/proof-params/src/gen/undelegateclaim_pk.bin");
let convert_key: &[u8] = include_bytes!("../../crypto/proof-params/src/gen/convert_pk.bin");

// Serialize &[u8] to JsValue.
let spend_key_js: JsValue = serde_wasm_bindgen::to_value(&spend_key).unwrap();
Expand All @@ -62,8 +61,7 @@ mod tests {
serde_wasm_bindgen::to_value(&nullifier_derivation_key).unwrap();
let swap_key_js: JsValue = serde_wasm_bindgen::to_value(&swap_key).unwrap();
let swap_claim_key_js: JsValue = serde_wasm_bindgen::to_value(&swap_claim_key).unwrap();
let undelegate_claim_key_js: JsValue =
serde_wasm_bindgen::to_value(&undelegate_claim_key).unwrap();
let undelegate_claim_key_js: JsValue = serde_wasm_bindgen::to_value(&convert_key).unwrap();

// Dynamically load the proving keys at runtime for each key type.
load_proving_key(spend_key_js, "spend").expect("can load spend key");
Expand Down
8 changes: 4 additions & 4 deletions tools/parameter-setup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ fn main() -> Result<()> {
write_params(&target_dir, "swap", &swap_pk, &swap_vk)?;
let (swapclaim_pk, swapclaim_vk) = generate_parameters::<SwapClaimCircuit>();
write_params(&target_dir, "swapclaim", &swapclaim_pk, &swapclaim_vk)?;
let (undelegateclaim_pk, undelegateclaim_vk) = generate_parameters::<ConvertCircuit>();
let (convert_pk, convert_vk) = generate_parameters::<ConvertCircuit>();
write_params(
&target_dir,
"undelegateclaim",
&undelegateclaim_pk,
&undelegateclaim_vk,
"convert",
&convert_pk,
&convert_vk,
)?;
let (delegator_vote_pk, delegator_vote_vk) = generate_parameters::<DelegatorVoteCircuit>();
write_params(
Expand Down
2 changes: 1 addition & 1 deletion tools/summonerd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl Opt {
"spend",
"output",
"delegator_vote",
"undelegateclaim",
"convert",
"swap",
"swapclaim",
"nullifier_derivation",
Expand Down

0 comments on commit abe8585

Please sign in to comment.