Skip to content

Commit

Permalink
Staking: Switch to using kept amount in Penalty representation
Browse files Browse the repository at this point in the history
This saves on constraints: back to 14423

This also makes the circuit tests pass know, not sure why they were failing
before.
  • Loading branch information
cronokirby committed Dec 6, 2023
1 parent 725c2ab commit deff4f7
Show file tree
Hide file tree
Showing 23 changed files with 38 additions and 58 deletions.
42 changes: 11 additions & 31 deletions crates/core/component/stake/src/penalty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Penalty {
Self(U128x128::ratio(bps_squared, 1_0000_0000).expect(&format!(
"{bps_squared} bps^2 should be convertible to a U128x128"
)))
.one_minus_this()
}

fn one_minus_this(&self) -> Penalty {
Expand All @@ -55,24 +56,22 @@ impl Penalty {

/// Compound this `Penalty` with another `Penalty`.
pub fn compound(&self, other: Penalty) -> Penalty {
// We want to compute q sth (1 - q) = (1-p1)(1-p2)
// q = 1 - (1-p1)(1-p2)
Self(
(self.one_minus_this().0 * other.one_minus_this().0)
.expect("compounding penalties will never overflow, both are <= 1"),
)
.one_minus_this()
Self((self.0 * other.0).expect("compounding penalities will not overflow"))
}

/// Apply this `Penalty` to an `Amount` of unbonding tokens.
pub fn apply_to(&self, amount: Amount) -> Amount {
(U128x128::from(amount) * self.one_minus_this().0)
.expect("should not overflow, because penalty is <= 1")
pub fn apply_to_amount(&self, amount: Amount) -> Amount {
self.apply_to(amount)
.round_down()
.try_into()
.expect("converting integral U128xU128 into Amount will succeed")
}

/// Apply this `Penalty` to an some fracton.
pub fn apply_to(&self, amount: impl Into<U128x128>) -> U128x128 {
(amount.into() * self.0).expect("should not overflow, because penalty is <= 1")
}

/// Helper method to compute the effect of an UndelegateClaim on the
/// transaction's value balance, used in planning and (transparent) proof
/// verification.
Expand All @@ -89,7 +88,7 @@ impl Penalty {
asset_id: unbonding_id,
}
+ Value {
amount: self.apply_to(unbonding_amount),
amount: self.apply_to_amount(unbonding_amount),
asset_id: *STAKING_TOKEN_ASSET_ID,
}
}
Expand All @@ -101,12 +100,6 @@ impl ToConstraintField<Fq> for Penalty {
}
}

impl From<Penalty> for U128x128 {
fn from(value: Penalty) -> Self {
value.0
}
}

impl From<Penalty> for [u8; 32] {
fn from(value: Penalty) -> Self {
value.0.into()
Expand Down Expand Up @@ -146,22 +139,9 @@ impl AllocVar<Penalty, Fq> for PenaltyVar {
}

impl PenaltyVar {
fn one_minus_this(&self) -> Result<PenaltyVar, SynthesisError> {
// Calculate 1 - self outside the circuit
let ooc_result = {
let value = self.value().unwrap_or(Penalty::default());
PenaltyVar::new_witness(self.cs(), || Ok(value.one_minus_this()))?
};
// Check that 1 + (1 - self) is self
let one = PenaltyVar::new_constant(self.cs(), Penalty::from_percent(100))?;
self.inner
.enforce_equal(&one.inner.checked_add(&ooc_result.inner)?)?;
Ok(ooc_result)
}

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

Expand Down
10 changes: 5 additions & 5 deletions crates/core/component/stake/src/rate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Staking reward and delegation token exchange rates.
use penumbra_num::fixpoint::U128x128;
use penumbra_num::Amount;
use penumbra_proto::core::component::stake::v1alpha1::CurrentValidatorRateResponse;
use penumbra_proto::{penumbra::core::component::stake::v1alpha1 as pb, DomainType};
Expand Down Expand Up @@ -98,10 +97,11 @@ impl RateData {
let mut slashed = self.clone();
// This will automatically produce a ratio which is multiplied by 1_0000_0000, and so
// rounding down does what we want.
let penalty_times_exchange_rate: u64 = (U128x128::from(self.validator_exchange_rate)
* U128x128::from(penalty))
.and_then(|x| x.round_down().try_into())
.expect("multiplying will not overflow");
let penalty_times_exchange_rate: u64 = penalty
.apply_to(self.validator_exchange_rate)
.round_down()
.try_into()
.expect("multiplying will not overflow");
// (1 - penalty) * exchange_rate
slashed.validator_exchange_rate = self
.validator_exchange_rate
Expand Down
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/delegator_vote_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk1hehwn62m7zwzycpdl8lf4g2x6eyut7y0av4ahnn3vmfdxz9qt85s3mu9u4";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1ph7cd0kp8r5fvnkmesn5f7hmuv5nex095d73l6spm0at83tqk5ss97wnft";
pub const PROVING_KEY_ID: &'static str = "groth16pk1hqv7y72vx5hgzfkzcajf5mg6plc3hvnhjqg8zaclymv4m9lfj99sn7w8wv";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1knhcr7yduj48sdrx4htfrth45ptqmjwtsyywwg5vl4pucrwm368qeqzj84";
2 changes: 1 addition & 1 deletion crates/crypto/proof-params/src/gen/delegator_vote_pk.bin
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/delegator_vote_vk.param
Binary file not shown.
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/nullifier_derivation_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk1rhgr5tunmrkz0wntj42lezj0am93w9tarm00vvttln05mnw597tsj2hka9";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1ynkrptkcd4jvqpn9zz0u7te4cmkjjdmammpf906yfyswhfh0c28qarttna";
pub const PROVING_KEY_ID: &'static str = "groth16pk1706hzsn4sn3ewwhnavgyql7pa289qwkze3eq8533wwdcqfzh5lrsz79klh";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1us3xkxnj8eks0mkk72z5h42mxhs4du5knm3m3llcghv635pn0qequt4yc2";
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/nullifier_derivation_vk.param
Binary file not shown.
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/output_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk1v5d36h5lgtvw3u3r5t5v5y70n9wlqydyr6cts8cwal47s5p4nxgqfg8kqk";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1j7uwrfeqcxz5jyeh2qykrkfra8wqn8qq9qknvtm3s5dh7rxq9vkq3d460q";
pub const PROVING_KEY_ID: &'static str = "groth16pk1cjr04fvt8n59fzwdlmqrs9e9v7j8syfenvjax2krqjjr3ky0jlfqxalshd";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk15hyzxz5zkc5zszg3n0g0247jp3h87w2dgu8zhe5k89wqesy7hxfqntspmu";
2 changes: 1 addition & 1 deletion crates/crypto/proof-params/src/gen/output_pk.bin
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/output_vk.param
Binary file not shown.
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/spend_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk10atmn9a9t8ntnn2t7hn9lrzkj06wpeusytcsdt8jycun4ype52fstf0fdj";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk15t67ksyu3gfcwe8zzthty03hxy8zagp97zhet6jlk6tq2cgd7yjs0a9e05";
pub const PROVING_KEY_ID: &'static str = "groth16pk1fcytclqvsqmleer6w87cnyvfw9gvksydren89r4hejhkmq8njl8qanww5n";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1xtreus06n3ewt2q3wt3jy79umjpytr7c9z9pfggahhzdl848299qxajy7v";
2 changes: 1 addition & 1 deletion crates/crypto/proof-params/src/gen/spend_pk.bin
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/spend_vk.param
Binary file not shown.
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/swap_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk17qn3cw8kmp2xjlk9uhdtdlckrngzgj6203upha4ft0k84u5yunrqn0mu3j";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1e49hj5sd2dc5jntpu3f2wxxuwtmfalntltyak3wlu5qs3yvp4wksz84pzy";
pub const PROVING_KEY_ID: &'static str = "groth16pk1u5xk2s2uxnz7370mt5f84yvcc0qdz42v6lsdjy3w6vyv6gaympms22kn8r";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1qsujtjs2pgumus0sx5unmygv0uq78t8x2eht4ry89qempx8wh4gs4gaped";
2 changes: 1 addition & 1 deletion crates/crypto/proof-params/src/gen/swap_pk.bin
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/swap_vk.param
Binary file not shown.
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/swapclaim_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk1h6k8m496se0fudkvcuvk0yh37u0h2n3kztqagqxfp0r82d4l2alse72l59";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1r3ds2sf5365s6yh6e50degnh0tudx78fd3r9l3u4gq8ncctm82js48984w";
pub const PROVING_KEY_ID: &'static str = "groth16pk1jxrg7qd577py094z6430e5y8864ya5dwevh879aj6qk2qq3r7exq49ftnq";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1rg87uv9a6qphylmk6n66llaj954zq0jpwkx5cw5sfwpgaw0ym4mqkrn2ct";
2 changes: 1 addition & 1 deletion crates/crypto/proof-params/src/gen/swapclaim_pk.bin
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/swapclaim_vk.param
Binary file not shown.
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/undelegateclaim_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

pub const PROVING_KEY_ID: &'static str = "groth16pk1yyep0mkzclzhfl7shner7hqmt4xlafufc9qqrhffzcngnxhsr7jq4pvjx8";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1wjdcyp8psfar83nq9wepavznh2yw596aa8emd50z9rwpecyeksusq7acdt";
pub const PROVING_KEY_ID: &'static str = "groth16pk1kz7ck7fsulndvp23zf6knnt7gvkh3pycryj9q95k4vlc4fmv2ptqq4pw65";
pub const VERIFICATION_KEY_ID: &'static str = "groth16vk1edaemnjzh22hmkms4mqw9mhh9kt79qjvdgtr5gurn4k8yc7860xqx5ecxk";
4 changes: 2 additions & 2 deletions crates/crypto/proof-params/src/gen/undelegateclaim_pk.bin
Git LFS file not shown
Binary file modified crates/crypto/proof-params/src/gen/undelegateclaim_vk.param
Binary file not shown.

0 comments on commit deff4f7

Please sign in to comment.