Skip to content

Commit

Permalink
Summonerd: Use uncompressed storage of elements consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Oct 16, 2023
1 parent 218aa68 commit 8827a7d
Showing 1 changed file with 67 additions and 71 deletions.
138 changes: 67 additions & 71 deletions crates/crypto/proof-setup/src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,10 +34,12 @@ fn to_bytes<T: CanonicalSerialize>(t: &T) -> Result<Vec<u8>> {
Ok(out)
}

fn to_bytes_uncompressed<T: CanonicalSerialize>(t: &T) -> Result<Vec<u8>> {
let mut out = Vec::new();
t.serialize_uncompressed(&mut out)?;
Ok(out)
fn from_bytes<T: CanonicalDeserialize>(data: &[u8]) -> Result<T> {
Ok(T::deserialize_uncompressed(data)?)
}

fn from_bytes_unchecked<T: CanonicalDeserialize>(data: &[u8]) -> Result<T> {
Ok(T::deserialize_uncompressed_unchecked(data)?)
}

pub const NUM_CIRCUITS: usize = 7;
Expand Down Expand Up @@ -97,13 +101,13 @@ impl TryFrom<pb::CeremonyCrs> for Phase2RawCeremonyCRS {

fn try_from(value: pb::CeremonyCrs) -> std::result::Result<Self, Self::Error> {
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::<Phase2RawCRSElements>(value.spend.as_slice())?,
from_bytes::<Phase2RawCRSElements>(value.output.as_slice())?,
from_bytes::<Phase2RawCRSElements>(value.delegator_vote.as_slice())?,
from_bytes::<Phase2RawCRSElements>(value.undelegate_claim.as_slice())?,
from_bytes::<Phase2RawCRSElements>(value.swap.as_slice())?,
from_bytes::<Phase2RawCRSElements>(value.swap_claim.as_slice())?,
from_bytes::<Phase2RawCRSElements>(value.nullifer_derivation_crs.as_slice())?,
]))
}
}
Expand Down Expand Up @@ -195,15 +199,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase2RawCeremonyContrib
.spend
.as_slice(),
)?,
new_elements: Phase2RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase2RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.spend
.as_slice(),
)?,
linking_proof: DLogProof::deserialize_compressed(
linking_proof: from_bytes::<DLogProof>(
value
.update_proofs
.as_ref()
Expand All @@ -221,15 +225,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase2RawCeremonyContrib
.output
.as_slice(),
)?,
new_elements: Phase2RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase2RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.output
.as_slice(),
)?,
linking_proof: DLogProof::deserialize_compressed(
linking_proof: from_bytes::<DLogProof>(
value
.update_proofs
.as_ref()
Expand All @@ -247,15 +251,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase2RawCeremonyContrib
.delegator_vote
.as_slice(),
)?,
new_elements: Phase2RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase2RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.delegator_vote
.as_slice(),
)?,
linking_proof: DLogProof::deserialize_compressed(
linking_proof: from_bytes::<DLogProof>(
value
.update_proofs
.as_ref()
Expand All @@ -273,15 +277,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase2RawCeremonyContrib
.undelegate_claim
.as_slice(),
)?,
new_elements: Phase2RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase2RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.undelegate_claim
.as_slice(),
)?,
linking_proof: DLogProof::deserialize_compressed(
linking_proof: from_bytes::<DLogProof>(
value
.update_proofs
.as_ref()
Expand All @@ -299,15 +303,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase2RawCeremonyContrib
.swap
.as_slice(),
)?,
new_elements: Phase2RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase2RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.swap
.as_slice(),
)?,
linking_proof: DLogProof::deserialize_compressed(
linking_proof: from_bytes::<DLogProof>(
value
.update_proofs
.as_ref()
Expand All @@ -325,15 +329,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase2RawCeremonyContrib
.swap_claim
.as_slice(),
)?,
new_elements: Phase2RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase2RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.swap_claim
.as_slice(),
)?,
linking_proof: DLogProof::deserialize_compressed(
linking_proof: from_bytes::<DLogProof>(
value
.update_proofs
.as_ref()
Expand All @@ -351,15 +355,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase2RawCeremonyContrib
.nullifer_derivation_crs
.as_slice(),
)?,
new_elements: Phase2RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase2RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.nullifer_derivation_crs
.as_slice(),
)?,
linking_proof: DLogProof::deserialize_compressed(
linking_proof: from_bytes::<DLogProof>(
value
.update_proofs
.as_ref()
Expand Down Expand Up @@ -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<Self> {
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::<Phase1RawCRSElements>(value.spend.as_slice())?,
from_bytes_unchecked::<Phase1RawCRSElements>(value.output.as_slice())?,
from_bytes_unchecked::<Phase1RawCRSElements>(value.delegator_vote.as_slice())?,
from_bytes_unchecked::<Phase1RawCRSElements>(value.undelegate_claim.as_slice())?,
from_bytes_unchecked::<Phase1RawCRSElements>(value.swap.as_slice())?,
from_bytes_unchecked::<Phase1RawCRSElements>(value.swap_claim.as_slice())?,
from_bytes_unchecked::<Phase1RawCRSElements>(value.nullifer_derivation_crs.as_slice())?,
]))
}
}
Expand All @@ -500,13 +498,13 @@ impl TryInto<pb::CeremonyCrs> for Phase1RawCeremonyCRS {

fn try_into(self) -> Result<pb::CeremonyCrs> {
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])?,
})
}
}
Expand All @@ -516,15 +514,13 @@ impl TryFrom<pb::CeremonyCrs> for Phase1RawCeremonyCRS {

fn try_from(value: pb::CeremonyCrs) -> std::result::Result<Self, Self::Error> {
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::<Phase1RawCRSElements>(value.spend.as_slice())?,
from_bytes::<Phase1RawCRSElements>(value.output.as_slice())?,
from_bytes::<Phase1RawCRSElements>(value.delegator_vote.as_slice())?,
from_bytes::<Phase1RawCRSElements>(value.undelegate_claim.as_slice())?,
from_bytes::<Phase1RawCRSElements>(value.swap.as_slice())?,
from_bytes::<Phase1RawCRSElements>(value.swap_claim.as_slice())?,
from_bytes::<Phase1RawCRSElements>(value.nullifer_derivation_crs.as_slice())?,
]))
}
}
Expand Down Expand Up @@ -616,15 +612,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase1RawCeremonyContrib
.spend
.as_slice(),
)?,
new_elements: Phase1RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase1RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.spend
.as_slice(),
)?,
linking_proof: LinkingProof::deserialize_compressed(
linking_proof: from_bytes::<LinkingProof>(
value
.update_proofs
.as_ref()
Expand All @@ -642,15 +638,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase1RawCeremonyContrib
.output
.as_slice(),
)?,
new_elements: Phase1RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase1RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.output
.as_slice(),
)?,
linking_proof: LinkingProof::deserialize_compressed(
linking_proof: from_bytes::<LinkingProof>(
value
.update_proofs
.as_ref()
Expand All @@ -668,15 +664,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase1RawCeremonyContrib
.delegator_vote
.as_slice(),
)?,
new_elements: Phase1RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase1RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.delegator_vote
.as_slice(),
)?,
linking_proof: LinkingProof::deserialize_compressed(
linking_proof: from_bytes::<LinkingProof>(
value
.update_proofs
.as_ref()
Expand All @@ -694,15 +690,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase1RawCeremonyContrib
.undelegate_claim
.as_slice(),
)?,
new_elements: Phase1RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase1RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.undelegate_claim
.as_slice(),
)?,
linking_proof: LinkingProof::deserialize_compressed(
linking_proof: from_bytes::<LinkingProof>(
value
.update_proofs
.as_ref()
Expand All @@ -720,15 +716,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase1RawCeremonyContrib
.swap
.as_slice(),
)?,
new_elements: Phase1RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase1RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.swap
.as_slice(),
)?,
linking_proof: LinkingProof::deserialize_compressed(
linking_proof: from_bytes::<LinkingProof>(
value
.update_proofs
.as_ref()
Expand All @@ -746,15 +742,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase1RawCeremonyContrib
.swap_claim
.as_slice(),
)?,
new_elements: Phase1RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase1RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.swap_claim
.as_slice(),
)?,
linking_proof: LinkingProof::deserialize_compressed(
linking_proof: from_bytes::<LinkingProof>(
value
.update_proofs
.as_ref()
Expand All @@ -772,15 +768,15 @@ impl TryFrom<pb::participate_request::Contribution> for Phase1RawCeremonyContrib
.nullifer_derivation_crs
.as_slice(),
)?,
new_elements: Phase1RawCRSElements::deserialize_compressed(
new_elements: from_bytes::<Phase1RawCRSElements>(
value
.updated
.as_ref()
.ok_or(anyhow!("no updated"))?
.nullifer_derivation_crs
.as_slice(),
)?,
linking_proof: LinkingProof::deserialize_compressed(
linking_proof: from_bytes::<LinkingProof>(
value
.update_proofs
.as_ref()
Expand Down Expand Up @@ -873,11 +869,11 @@ pub struct AllExtraTransitionInformation([ExtraTransitionInformation; NUM_CIRCUI

impl AllExtraTransitionInformation {
pub fn to_bytes(&self) -> Result<Vec<u8>> {
to_bytes_uncompressed(self)
to_bytes(self)
}

pub fn from_bytes(data: &[u8]) -> Result<Self> {
Ok(Self::deserialize_uncompressed_unchecked(data)?)
Ok(from_bytes_unchecked::<Self>(data)?)
}
}

Expand Down

0 comments on commit 8827a7d

Please sign in to comment.