Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/backends/plonky2/primitives/ec/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ use plonky2::{
plonk::circuit_builder::CircuitBuilder,
};
use rand::rngs::OsRng;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::curve::Point;
use crate::{
backends::plonky2::{
circuits::common::CircuitBuilderPod,
deserialize_bytes,
primitives::ec::{
bits::{BigUInt320Target, CircuitBuilderBits},
curve::{CircuitBuilderElliptic, PointTarget, WitnessWriteCurve, GROUP_ORDER},
},
Error,
serialize_bytes, Error,
},
middleware::RawValue,
};
Expand Down Expand Up @@ -76,6 +78,28 @@ impl Signature {
}
}

impl Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let signature_b64 = serialize_bytes(&self.as_bytes());
serializer.serialize_str(&signature_b64)
}
}

impl<'de> Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let signature_b64 = String::deserialize(deserializer)?;
let signature_bytes =
deserialize_bytes(&signature_b64).map_err(serde::de::Error::custom)?;
Signature::from_bytes(&signature_bytes).map_err(serde::de::Error::custom)
}
}

/// Targets for Schnorr signature over ecGFp5.
#[derive(Clone, Debug)]
pub struct SignatureTarget {
Expand Down
38 changes: 6 additions & 32 deletions src/backends/plonky2/signedpod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};

use crate::{
backends::plonky2::{
deserialize_bytes,
error::{Error, Result},
primitives::{
ec::{
Expand All @@ -16,7 +15,6 @@ use crate::{
},
merkletree::MerkleTree,
},
serialize_bytes,
},
middleware::{
containers::Dictionary, AnchoredKey, Hash, Key, Params, Pod, PodId, PodSigner, PodType,
Expand Down Expand Up @@ -76,8 +74,8 @@ pub struct SignedPod {

#[derive(Serialize, Deserialize)]
struct Data {
signer: String,
signature: String,
signer: Point,
signature: Signature,
kvs: Dictionary,
}

Expand All @@ -93,27 +91,10 @@ fn dummy() -> SignedPod {
impl SignedPod {
pub(crate) fn deserialize(id: PodId, data: serde_json::Value) -> Result<Box<dyn Pod>> {
let data: Data = serde_json::from_value(data)?;
let signer_bytes = deserialize_bytes(&data.signer)?;
let signature_bytes = deserialize_bytes(&data.signature)?;

if signer_bytes.len() != 40 {
return Err(Error::custom(
"Invalid byte encoding of signed POD signer.".to_string(),
));
}
if signature_bytes.len() != 80 {
return Err(Error::custom(
"Invalid byte encoding of signed POD signature.".to_string(),
));
}

let signer = Point::from_bytes_into_subgroup(&signer_bytes)?;
let signature = Signature::from_bytes(&signature_bytes)?;

Ok(Box::new(Self {
id,
signature,
signer,
signature: data.signature,
signer: data.signer,
dict: data.kvs,
}))
}
Expand Down Expand Up @@ -189,16 +170,9 @@ impl Pod for SignedPod {
}

fn serialize_data(&self) -> serde_json::Value {
let signer = serialize_bytes(
&self
.signer
.as_bytes_from_subgroup()
.expect("Signer public key must lie in EC subgroup."),
);
let signature = serialize_bytes(&self.signature.as_bytes());
serde_json::to_value(Data {
signer,
signature,
signer: self.signer,
signature: self.signature.clone(),
kvs: self.dict.clone(),
})
.expect("serialization to json")
Expand Down
Loading