Skip to content
2 changes: 1 addition & 1 deletion src/backends/plonky2/primitives/ec/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Serialize for Point {
S: Serializer,
{
let point_b58 = format!("{}", self);
serializer.serialize_str(&point_b58)
serializer.serialize_newtype_struct("pod2::Point", &point_b58)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/backends/plonky2/primitives/ec/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Serialize for SecretKey {
S: Serializer,
{
let sk_b64 = serialize_bytes(&self.as_bytes());
serializer.serialize_str(&sk_b64)
serializer.serialize_newtype_struct("pod2::SecretKey", &sk_b64)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(clippy::uninlined_format_args)] // TODO: Remove this in another PR
#![allow(clippy::manual_repeat_n)] // TODO: Remove this in another PR
#![allow(clippy::large_enum_variant)] // TODO: Remove this in another PR
#![feature(macro_metavar_expr_concat)]
#![feature(mapped_lock_guards)]

pub mod backends;
Expand Down
3 changes: 3 additions & 0 deletions src/middleware/basetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub const SELF_ID_HASH: Hash = Hash([F(0x5), F(0xe), F(0x1), F(0xf)]);
pub const EMPTY_HASH: Hash = Hash([F::ZERO, F::ZERO, F::ZERO, F::ZERO]);

#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
// use pod2:: prefix to help ValueSerializer recognize RawValue
// most serializers will ignore the name
#[serde(rename = "pod2::RawValue")]
pub struct RawValue(
#[serde(
serialize_with = "serialize_value_tuple",
Expand Down
1 change: 1 addition & 0 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod error;
mod operation;
mod pod_deserialization;
pub mod serialization;
pub mod serializer;
mod statement;
use std::{any::Any, fmt};

Expand Down
34 changes: 16 additions & 18 deletions src/middleware/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@ use std::{
};

use plonky2::field::types::Field;
use serde::{ser::SerializeSeq, Deserialize, Serialize, Serializer};
use serde::{Deserialize, Serialize, Serializer};

use super::{Key, Value};
use crate::middleware::{F, HASH_SIZE, VALUE_SIZE};

fn serialize_field_tuple<S, const N: usize>(
value: &[F; N],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
pub(super) fn field_array_to_string<const N: usize>(value: &[F; N]) -> String {
// `value` is little-endian in memory. We serialize it as a big-endian hex string
// for human readability.
let s = value
value
.iter()
.rev()
.fold(String::with_capacity(N * 16), |mut s, limb| {
write!(s, "{:016x}", limb.0).unwrap();
s
});
serializer.serialize_str(&s)
})
}

fn serialize_field_tuple<S, const N: usize>(
value: &[F; N],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&field_array_to_string(value))
}

fn deserialize_field_tuple<'de, D, const N: usize>(deserializer: D) -> Result<[F; N], D::Error>
Expand Down Expand Up @@ -131,17 +134,12 @@ where

// Sets are serialized as sequences of elements, which are not ordered by
// default. We want to serialize them in a deterministic way, and we can
// achieve this by sorting the elements. This takes advantage of the fact that
// Value implements Ord.
// achieve this by sorting the elements.
pub fn ordered_set<S>(value: &HashSet<Value>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut set = serializer.serialize_seq(Some(value.len()))?;
let mut sorted_values: Vec<&Value> = value.iter().collect();
sorted_values.sort_by_key(|v| v.raw());
for v in sorted_values {
set.serialize_element(v)?;
}
set.end()
serializer.serialize_newtype_struct("pod2::Set", &sorted_values)
}
Loading