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 455c6ea commit ebc8234
Showing 1 changed file with 64 additions and 70 deletions.
134 changes: 64 additions & 70 deletions crates/crypto/proof-setup/src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,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 +99,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 +197,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 +223,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 +249,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 +275,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 +301,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 +327,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 +353,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 +480,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 +496,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 +512,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 +610,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 +636,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 +662,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 +688,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 +714,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 +740,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 +766,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 +867,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 ebc8234

Please sign in to comment.