22//! `backend_plonky2` feature is enabled.
33//! See src/middleware/basetypes.rs for more details.
44
5+ use crate :: middleware:: serialization:: {
6+ deserialize_hash_tuple, deserialize_value_tuple, serialize_hash_tuple, serialize_value_tuple,
7+ } ;
8+ use crate :: middleware:: { Params , ToFields } ;
59use anyhow:: { anyhow, Error , Result } ;
610use hex:: { FromHex , FromHexError } ;
711use plonky2:: field:: goldilocks_field:: GoldilocksField ;
@@ -10,11 +14,11 @@ use plonky2::hash::poseidon::PoseidonHash;
1014use plonky2:: plonk:: config:: Hasher ;
1115use plonky2:: plonk:: config:: PoseidonGoldilocksConfig ;
1216use plonky2:: plonk:: proof:: Proof as Plonky2Proof ;
17+ use schemars:: JsonSchema ;
18+ use serde:: { Deserialize , Serialize } ;
1319use std:: cmp:: { Ord , Ordering } ;
1420use std:: fmt;
1521
16- use crate :: middleware:: { Params , ToFields } ;
17-
1822use crate :: backends:: counter;
1923
2024/// F is the native field we use everywhere. Currently it's Goldilocks from plonky2
@@ -34,8 +38,18 @@ pub const EMPTY_VALUE: Value = Value([F::ZERO, F::ZERO, F::ZERO, F::ZERO]);
3438pub const SELF_ID_HASH : Hash = Hash ( [ F :: ONE , F :: ZERO , F :: ZERO , F :: ZERO ] ) ;
3539pub const EMPTY_HASH : Hash = Hash ( [ F :: ZERO , F :: ZERO , F :: ZERO , F :: ZERO ] ) ;
3640
37- #[ derive( Clone , Copy , Debug , Default , Hash , PartialEq , Eq ) ]
38- pub struct Value ( pub [ F ; VALUE_SIZE ] ) ;
41+ #[ derive( Clone , Copy , Debug , Default , Hash , PartialEq , Eq , Serialize , Deserialize , JsonSchema ) ]
42+ #[ schemars( rename = "MiddlewareValue" ) ]
43+ pub struct Value (
44+ #[ serde(
45+ serialize_with = "serialize_value_tuple" ,
46+ deserialize_with = "deserialize_value_tuple"
47+ ) ]
48+ // We know that Serde will serialize and deserialize this as a string, so we can
49+ // use the JsonSchema to validate the format.
50+ #[ schemars( with = "String" , regex( pattern = r"^[0-9a-fA-F]{64}$" ) ) ]
51+ pub [ F ; VALUE_SIZE ] ,
52+ ) ;
3953
4054impl ToFields for Value {
4155 fn to_fields ( & self , _params : & Params ) -> Vec < F > {
@@ -117,8 +131,15 @@ impl fmt::Display for Value {
117131 }
118132}
119133
120- #[ derive( Clone , Copy , Debug , Default , Hash , Eq , PartialEq ) ]
121- pub struct Hash ( pub [ F ; HASH_SIZE ] ) ;
134+ #[ derive( Clone , Copy , Debug , Default , Hash , Eq , PartialEq , Serialize , Deserialize , JsonSchema ) ]
135+ pub struct Hash (
136+ #[ serde(
137+ serialize_with = "serialize_hash_tuple" ,
138+ deserialize_with = "deserialize_hash_tuple"
139+ ) ]
140+ #[ schemars( with = "String" , regex( pattern = r"^[0-9a-fA-F]{64}$" ) ) ]
141+ pub [ F ; HASH_SIZE ] ,
142+ ) ;
122143
123144pub fn hash_value ( input : & Value ) -> Hash {
124145 hash_fields ( & input. 0 )
0 commit comments