Skip to content

Commit aa51db2

Browse files
committed
clean up
1 parent 0583497 commit aa51db2

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

src/backends/plonky2/mock/signedpod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use crate::{
1010
},
1111
constants::MAX_DEPTH,
1212
middleware::{
13-
containers::Dictionary, hash_str, AnchoredKey, DynError, Hash, Key, Params, Pod, PodId,
14-
PodSigner, PodType, RawValue, Statement, Value, KEY_SIGNER, KEY_TYPE, SELF,
13+
containers::Dictionary, hash_str, serialization::ordered_map, AnchoredKey, DynError, Hash,
14+
Key, Params, Pod, PodId, PodSigner, PodType, RawValue, Statement, Value, KEY_SIGNER,
15+
KEY_TYPE, SELF,
1516
},
1617
};
1718

@@ -59,6 +60,7 @@ pub struct MockSignedPod {
5960
#[derive(Serialize, Deserialize)]
6061
struct Data {
6162
signature: String,
63+
#[serde(serialize_with = "ordered_map")]
6264
kvs: HashMap<Key, Value>,
6365
}
6466

src/backends/plonky2/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@ pub static STANDARD_REC_MAIN_POD_CIRCUIT_DATA: LazyLock<CircuitData> = LazyLock:
4040
)
4141
});
4242

43-
pub fn deserialize_proof(common: &CommonCircuitData, proof: &str) -> Result<Proof, Error> {
44-
let decoded = BASE64_STANDARD.decode(proof).map_err(|e| {
43+
pub fn serialize_bytes(bytes: &[u8]) -> String {
44+
BASE64_STANDARD.encode(bytes)
45+
}
46+
47+
pub fn deserialize_bytes(data: &str) -> Result<Vec<u8>> {
48+
BASE64_STANDARD.decode(data).map_err(|e| {
4549
Error::custom(format!(
46-
"Failed to decode proof from base64: {}. Value: {}",
47-
e, proof
50+
"Failed to decode data from base64: {}. Value: {}",
51+
e, data
4852
))
49-
})?;
53+
})
54+
}
55+
56+
pub fn deserialize_proof(common: &CommonCircuitData, proof: &str) -> Result<Proof> {
57+
let decoded = deserialize_bytes(proof)?;
5058
let mut buf = Buffer::new(&decoded);
5159
let proof = buf.read_proof(common).map_err(|e| {
5260
Error::custom(format!(
@@ -62,5 +70,5 @@ pub fn serialize_proof(proof: &Proof) -> String {
6270
let mut buffer = Vec::new();
6371
use plonky2::util::serialization::Write;
6472
buffer.write_proof(proof).unwrap();
65-
BASE64_STANDARD.encode(buffer)
73+
serialize_bytes(&buffer)
6674
}

src/backends/plonky2/signedpod.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::collections::HashMap;
22

3-
use base64::{prelude::BASE64_STANDARD, Engine};
43
use itertools::Itertools;
54
use num_bigint::RandBigInt;
65
use rand::rngs::OsRng;
76
use serde::{Deserialize, Serialize};
87

98
use crate::{
109
backends::plonky2::{
10+
deserialize_bytes,
1111
error::{Error, Result},
1212
primitives::{
1313
ec::{
@@ -16,6 +16,7 @@ use crate::{
1616
},
1717
merkletree::MerkleTree,
1818
},
19+
serialize_bytes,
1920
},
2021
constants::MAX_DEPTH,
2122
middleware::{
@@ -71,8 +72,9 @@ pub struct SignedPod {
7172

7273
#[derive(Serialize, Deserialize)]
7374
struct Data {
74-
signer_signature: String,
75-
dict: Dictionary,
75+
signer: String,
76+
signature: String,
77+
kvs: Dictionary,
7678
}
7779

7880
impl SignedPod {
@@ -116,30 +118,28 @@ impl SignedPod {
116118

117119
pub(crate) fn deserialize(id: PodId, data: serde_json::Value) -> Result<Box<dyn Pod>> {
118120
let data: Data = serde_json::from_value(data)?;
119-
let signer_signature_bytes =
120-
BASE64_STANDARD
121-
.decode(&data.signer_signature)
122-
.map_err(|e| {
123-
Error::custom(format!(
124-
"Failed to decode signer_signature from base64: {}. Value: {}",
125-
e, data.signer_signature
126-
))
127-
})?;
128-
129-
if signer_signature_bytes.len() != 160 {
121+
let signer_bytes = deserialize_bytes(&data.signer)?;
122+
let signature_bytes = deserialize_bytes(&data.signature)?;
123+
124+
if signer_bytes.len() != 80 {
125+
return Err(Error::custom(
126+
"Invalid byte encoding of signed POD signer.".to_string(),
127+
));
128+
}
129+
if signature_bytes.len() != 80 {
130130
return Err(Error::custom(
131-
"Invalid byte encoding of signed POD signer_signature.".to_string(),
131+
"Invalid byte encoding of signed POD signature.".to_string(),
132132
));
133133
}
134134

135-
let signer = Point::from_bytes(&signer_signature_bytes[..80])?;
136-
let signature = Signature::from_bytes(&signer_signature_bytes[80..])?;
135+
let signer = Point::from_bytes(&signer_bytes)?;
136+
let signature = Signature::from_bytes(&signature_bytes)?;
137137

138138
Ok(Box::new(Self {
139139
id,
140140
signature,
141141
signer,
142-
dict: data.dict,
142+
dict: data.kvs,
143143
}))
144144
}
145145
}
@@ -174,11 +174,12 @@ impl Pod for SignedPod {
174174
}
175175

176176
fn serialize_data(&self) -> serde_json::Value {
177-
let signer_signature_bytes = [self.signer.as_bytes(), self.signature.as_bytes()].concat();
178-
let signer_signature = BASE64_STANDARD.encode(&signer_signature_bytes);
177+
let signer = serialize_bytes(&self.signer.as_bytes());
178+
let signature = serialize_bytes(&self.signature.as_bytes());
179179
serde_json::to_value(Data {
180-
signer_signature,
181-
dict: self.dict.clone(),
180+
signer,
181+
signature,
182+
kvs: self.dict.clone(),
182183
})
183184
.expect("serialization to json")
184185
}

0 commit comments

Comments
 (0)