fix: consistently serialize signer (#334)

- serialize the signer in base58 both as Value and as the signer embedded
in the SignedPod json data field.
- Implement serialization/deserialization for Signature
This commit is contained in:
Eduard S. 2025-07-10 11:48:47 +02:00 committed by GitHub
parent 0750dbeaff
commit aeedf55bad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 33 deletions

View file

@ -20,16 +20,18 @@ use plonky2::{
plonk::circuit_builder::CircuitBuilder, plonk::circuit_builder::CircuitBuilder,
}; };
use rand::rngs::OsRng; use rand::rngs::OsRng;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::curve::Point; use super::curve::Point;
use crate::{ use crate::{
backends::plonky2::{ backends::plonky2::{
circuits::common::CircuitBuilderPod, circuits::common::CircuitBuilderPod,
deserialize_bytes,
primitives::ec::{ primitives::ec::{
bits::{BigUInt320Target, CircuitBuilderBits}, bits::{BigUInt320Target, CircuitBuilderBits},
curve::{CircuitBuilderElliptic, PointTarget, WitnessWriteCurve, GROUP_ORDER}, curve::{CircuitBuilderElliptic, PointTarget, WitnessWriteCurve, GROUP_ORDER},
}, },
Error, serialize_bytes, Error,
}, },
middleware::RawValue, middleware::RawValue,
}; };
@ -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. /// Targets for Schnorr signature over ecGFp5.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SignatureTarget { pub struct SignatureTarget {

View file

@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
backends::plonky2::{ backends::plonky2::{
deserialize_bytes,
error::{Error, Result}, error::{Error, Result},
primitives::{ primitives::{
ec::{ ec::{
@ -16,7 +15,6 @@ use crate::{
}, },
merkletree::MerkleTree, merkletree::MerkleTree,
}, },
serialize_bytes,
}, },
middleware::{ middleware::{
containers::Dictionary, AnchoredKey, Hash, Key, Params, Pod, PodId, PodSigner, PodType, containers::Dictionary, AnchoredKey, Hash, Key, Params, Pod, PodId, PodSigner, PodType,
@ -76,8 +74,8 @@ pub struct SignedPod {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct Data { struct Data {
signer: String, signer: Point,
signature: String, signature: Signature,
kvs: Dictionary, kvs: Dictionary,
} }
@ -93,27 +91,10 @@ fn dummy() -> SignedPod {
impl SignedPod { impl SignedPod {
pub(crate) fn deserialize(id: PodId, data: serde_json::Value) -> Result<Box<dyn Pod>> { pub(crate) fn deserialize(id: PodId, data: serde_json::Value) -> Result<Box<dyn Pod>> {
let data: Data = serde_json::from_value(data)?; 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 { Ok(Box::new(Self {
id, id,
signature, signature: data.signature,
signer, signer: data.signer,
dict: data.kvs, dict: data.kvs,
})) }))
} }
@ -189,16 +170,9 @@ impl Pod for SignedPod {
} }
fn serialize_data(&self) -> serde_json::Value { 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 { serde_json::to_value(Data {
signer, signer: self.signer,
signature, signature: self.signature.clone(),
kvs: self.dict.clone(), kvs: self.dict.clone(),
}) })
.expect("serialization to json") .expect("serialization to json")