From 77f125ef657480ff9d4be5c2a9f49131dfac289f Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Mon, 16 Oct 2023 11:15:23 -0700 Subject: [PATCH] Summonerd: Use uncompressed storage of elements consistently --- crates/crypto/proof-setup/src/all.rs | 138 +++++++++++++-------------- 1 file changed, 67 insertions(+), 71 deletions(-) diff --git a/crates/crypto/proof-setup/src/all.rs b/crates/crypto/proof-setup/src/all.rs index aa4843aae2..0d12a46d37 100644 --- a/crates/crypto/proof-setup/src/all.rs +++ b/crates/crypto/proof-setup/src/all.rs @@ -18,7 +18,9 @@ use decaf377::Bls12_377; use penumbra_dex::{swap::proof::SwapCircuit, swap_claim::proof::SwapClaimCircuit}; use penumbra_governance::DelegatorVoteCircuit; use penumbra_proof_params::generate_constraint_matrices; -use penumbra_proto::tools::summoning::v1alpha1::{self as pb}; +use penumbra_proto::tools::summoning::v1alpha1::{self as pb, + participate_request::Contribution as PBContribution, +}; use penumbra_shielded_pool::{NullifierDerivationCircuit, OutputCircuit, SpendCircuit}; use penumbra_stake::UndelegateClaimCircuit; @@ -32,10 +34,12 @@ fn to_bytes(t: &T) -> Result> { Ok(out) } -fn to_bytes_uncompressed(t: &T) -> Result> { - let mut out = Vec::new(); - t.serialize_uncompressed(&mut out)?; - Ok(out) +fn from_bytes(data: &[u8]) -> Result { + Ok(T::deserialize_uncompressed(data)?) +} + +fn from_bytes_unchecked(data: &[u8]) -> Result { + Ok(T::deserialize_uncompressed_unchecked(data)?) } pub const NUM_CIRCUITS: usize = 7; @@ -97,13 +101,13 @@ impl TryFrom for Phase2RawCeremonyCRS { fn try_from(value: pb::CeremonyCrs) -> std::result::Result { Ok(Self([ - Phase2RawCRSElements::deserialize_compressed(value.spend.as_slice())?, - Phase2RawCRSElements::deserialize_compressed(value.output.as_slice())?, - Phase2RawCRSElements::deserialize_compressed(value.delegator_vote.as_slice())?, - Phase2RawCRSElements::deserialize_compressed(value.undelegate_claim.as_slice())?, - Phase2RawCRSElements::deserialize_compressed(value.swap.as_slice())?, - Phase2RawCRSElements::deserialize_compressed(value.swap_claim.as_slice())?, - Phase2RawCRSElements::deserialize_compressed(value.nullifer_derivation_crs.as_slice())?, + from_bytes::(value.spend.as_slice())?, + from_bytes::(value.output.as_slice())?, + from_bytes::(value.delegator_vote.as_slice())?, + from_bytes::(value.undelegate_claim.as_slice())?, + from_bytes::(value.swap.as_slice())?, + from_bytes::(value.swap_claim.as_slice())?, + from_bytes::(value.nullifer_derivation_crs.as_slice())?, ])) } } @@ -195,7 +199,7 @@ impl TryFrom for Phase2RawCeremonyContrib .spend .as_slice(), )?, - new_elements: Phase2RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -203,7 +207,7 @@ impl TryFrom for Phase2RawCeremonyContrib .spend .as_slice(), )?, - linking_proof: DLogProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -221,7 +225,7 @@ impl TryFrom for Phase2RawCeremonyContrib .output .as_slice(), )?, - new_elements: Phase2RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -229,7 +233,7 @@ impl TryFrom for Phase2RawCeremonyContrib .output .as_slice(), )?, - linking_proof: DLogProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -247,7 +251,7 @@ impl TryFrom for Phase2RawCeremonyContrib .delegator_vote .as_slice(), )?, - new_elements: Phase2RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -255,7 +259,7 @@ impl TryFrom for Phase2RawCeremonyContrib .delegator_vote .as_slice(), )?, - linking_proof: DLogProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -273,7 +277,7 @@ impl TryFrom for Phase2RawCeremonyContrib .undelegate_claim .as_slice(), )?, - new_elements: Phase2RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -281,7 +285,7 @@ impl TryFrom for Phase2RawCeremonyContrib .undelegate_claim .as_slice(), )?, - linking_proof: DLogProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -299,7 +303,7 @@ impl TryFrom for Phase2RawCeremonyContrib .swap .as_slice(), )?, - new_elements: Phase2RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -307,7 +311,7 @@ impl TryFrom for Phase2RawCeremonyContrib .swap .as_slice(), )?, - linking_proof: DLogProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -325,7 +329,7 @@ impl TryFrom for Phase2RawCeremonyContrib .swap_claim .as_slice(), )?, - new_elements: Phase2RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -333,7 +337,7 @@ impl TryFrom for Phase2RawCeremonyContrib .swap_claim .as_slice(), )?, - linking_proof: DLogProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -351,7 +355,7 @@ impl TryFrom for Phase2RawCeremonyContrib .nullifer_derivation_crs .as_slice(), )?, - new_elements: Phase2RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -359,7 +363,7 @@ impl TryFrom for Phase2RawCeremonyContrib .nullifer_derivation_crs .as_slice(), )?, - linking_proof: DLogProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -478,19 +482,13 @@ impl Phase1RawCeremonyCRS { /// This should only be used when the data is known to be from a trusted source. pub fn unchecked_from_protobuf(value: pb::CeremonyCrs) -> anyhow::Result { Ok(Self([ - Phase1RawCRSElements::deserialize_uncompressed_unchecked(value.spend.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed_unchecked(value.output.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed_unchecked( - value.delegator_vote.as_slice(), - )?, - Phase1RawCRSElements::deserialize_uncompressed_unchecked( - value.undelegate_claim.as_slice(), - )?, - Phase1RawCRSElements::deserialize_uncompressed_unchecked(value.swap.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed_unchecked(value.swap_claim.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed_unchecked( - value.nullifer_derivation_crs.as_slice(), - )?, + from_bytes_unchecked::(value.spend.as_slice())?, + from_bytes_unchecked::(value.output.as_slice())?, + from_bytes_unchecked::(value.delegator_vote.as_slice())?, + from_bytes_unchecked::(value.undelegate_claim.as_slice())?, + from_bytes_unchecked::(value.swap.as_slice())?, + from_bytes_unchecked::(value.swap_claim.as_slice())?, + from_bytes_unchecked::(value.nullifer_derivation_crs.as_slice())?, ])) } } @@ -500,13 +498,13 @@ impl TryInto for Phase1RawCeremonyCRS { fn try_into(self) -> Result { Ok(pb::CeremonyCrs { - spend: to_bytes_uncompressed(&self.0[0])?, - output: to_bytes_uncompressed(&self.0[1])?, - delegator_vote: to_bytes_uncompressed(&self.0[2])?, - undelegate_claim: to_bytes_uncompressed(&self.0[3])?, - swap: to_bytes_uncompressed(&self.0[4])?, - swap_claim: to_bytes_uncompressed(&self.0[5])?, - nullifer_derivation_crs: to_bytes_uncompressed(&self.0[6])?, + spend: to_bytes(&self.0[0])?, + output: to_bytes(&self.0[1])?, + delegator_vote: to_bytes(&self.0[2])?, + undelegate_claim: to_bytes(&self.0[3])?, + swap: to_bytes(&self.0[4])?, + swap_claim: to_bytes(&self.0[5])?, + nullifer_derivation_crs: to_bytes(&self.0[6])?, }) } } @@ -516,15 +514,13 @@ impl TryFrom for Phase1RawCeremonyCRS { fn try_from(value: pb::CeremonyCrs) -> std::result::Result { Ok(Self([ - Phase1RawCRSElements::deserialize_uncompressed(value.spend.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed(value.output.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed(value.delegator_vote.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed(value.undelegate_claim.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed(value.swap.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed(value.swap_claim.as_slice())?, - Phase1RawCRSElements::deserialize_uncompressed( - value.nullifer_derivation_crs.as_slice(), - )?, + from_bytes::(value.spend.as_slice())?, + from_bytes::(value.output.as_slice())?, + from_bytes::(value.delegator_vote.as_slice())?, + from_bytes::(value.undelegate_claim.as_slice())?, + from_bytes::(value.swap.as_slice())?, + from_bytes::(value.swap_claim.as_slice())?, + from_bytes::(value.nullifer_derivation_crs.as_slice())?, ])) } } @@ -616,7 +612,7 @@ impl TryFrom for Phase1RawCeremonyContrib .spend .as_slice(), )?, - new_elements: Phase1RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -624,7 +620,7 @@ impl TryFrom for Phase1RawCeremonyContrib .spend .as_slice(), )?, - linking_proof: LinkingProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -642,7 +638,7 @@ impl TryFrom for Phase1RawCeremonyContrib .output .as_slice(), )?, - new_elements: Phase1RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -650,7 +646,7 @@ impl TryFrom for Phase1RawCeremonyContrib .output .as_slice(), )?, - linking_proof: LinkingProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -668,7 +664,7 @@ impl TryFrom for Phase1RawCeremonyContrib .delegator_vote .as_slice(), )?, - new_elements: Phase1RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -676,7 +672,7 @@ impl TryFrom for Phase1RawCeremonyContrib .delegator_vote .as_slice(), )?, - linking_proof: LinkingProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -694,7 +690,7 @@ impl TryFrom for Phase1RawCeremonyContrib .undelegate_claim .as_slice(), )?, - new_elements: Phase1RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -702,7 +698,7 @@ impl TryFrom for Phase1RawCeremonyContrib .undelegate_claim .as_slice(), )?, - linking_proof: LinkingProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -720,7 +716,7 @@ impl TryFrom for Phase1RawCeremonyContrib .swap .as_slice(), )?, - new_elements: Phase1RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -728,7 +724,7 @@ impl TryFrom for Phase1RawCeremonyContrib .swap .as_slice(), )?, - linking_proof: LinkingProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -746,7 +742,7 @@ impl TryFrom for Phase1RawCeremonyContrib .swap_claim .as_slice(), )?, - new_elements: Phase1RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -754,7 +750,7 @@ impl TryFrom for Phase1RawCeremonyContrib .swap_claim .as_slice(), )?, - linking_proof: LinkingProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -772,7 +768,7 @@ impl TryFrom for Phase1RawCeremonyContrib .nullifer_derivation_crs .as_slice(), )?, - new_elements: Phase1RawCRSElements::deserialize_compressed( + new_elements: from_bytes::( value .updated .as_ref() @@ -780,7 +776,7 @@ impl TryFrom for Phase1RawCeremonyContrib .nullifer_derivation_crs .as_slice(), )?, - linking_proof: LinkingProof::deserialize_compressed( + linking_proof: from_bytes::( value .update_proofs .as_ref() @@ -873,11 +869,11 @@ pub struct AllExtraTransitionInformation([ExtraTransitionInformation; NUM_CIRCUI impl AllExtraTransitionInformation { pub fn to_bytes(&self) -> Result> { - to_bytes_uncompressed(self) + to_bytes(self) } pub fn from_bytes(data: &[u8]) -> Result { - Ok(Self::deserialize_uncompressed_unchecked(data)?) + Ok(from_bytes_unchecked::(data)?) } }