Skip to content

Commit 2a0c6f1

Browse files
committed
Use serde_bytes to (de)serialize Vec<u8>s.
1 parent 08592fe commit 2a0c6f1

File tree

7 files changed

+42
-39
lines changed

7 files changed

+42
-39
lines changed

Cargo.lock

Lines changed: 25 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

arm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rustler = { version = "0.36.2", optional = true }
2020
bytemuck = { version = "1.12", features = ["derive"] }
2121
alloy-primitives = "1.0.23"
2222
alloy-sol-types = "1.0.23"
23+
serde_bytes = "0.11.19"
2324

2425
[features]
2526
default = ["transaction", "prove"]

arm/src/compliance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub struct ComplianceWitness {
4646
/// The created resource
4747
pub created_resource: Resource,
4848
/// Random scalar for delta commitment
49+
#[serde(with = "serde_bytes")]
4950
pub rcv: Vec<u8>,
5051
// TODO: If we want to add function privacy, include:
5152
// pub input_resource_logic_cm_r: [u8; DATA_BYTES],

arm/src/compliance_unit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use crate::{compliance::ComplianceWitness, constants::COMPLIANCE_PK, proving_sys
1313
#[cfg_attr(feature = "nif", serde(rename = "Elixir.Anoma.Arm.ComplianceUnit"))]
1414
pub struct ComplianceUnit {
1515
// vk is a constant in the compliance unit, so we don't place it here.
16+
#[serde(with = "serde_bytes")]
1617
pub proof: Vec<u8>,
18+
#[serde(with = "serde_bytes")]
1719
pub instance: Vec<u8>,
1820
}
1921

arm/src/delta_proof.rs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ use k256::{
33
elliptic_curve::PublicKey, elliptic_curve::ScalarPrimitive, ProjectivePoint, Scalar, SecretKey,
44
};
55
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
6-
use serde::de::{SeqAccess};
76

87
use sha3::{Digest, Keccak256};
9-
use std::marker::PhantomData;
10-
use std::fmt;
11-
use serde::de::Visitor;
8+
use serde_bytes::ByteArray;
129

1310
#[derive(Clone, Debug, Deserialize, Serialize)]
1411
#[cfg_attr(feature = "nif", serde(rename = "Elixir.Anoma.Arm.DeltaProof"))]
@@ -20,38 +17,11 @@ pub struct DeltaProof {
2017
}
2118

2219
fn serialize_signature<S>(t: &Signature, s: S) -> Result<S::Ok, S::Error> where S: Serializer {
23-
s.serialize_bytes(&t.to_bytes())
20+
ByteArray::<64>::new(t.to_bytes().into()).serialize(s)
2421
}
2522

2623
fn deserialize_signature<'de, D>(deserializer: D) -> Result<Signature, D::Error> where D: Deserializer<'de> {
27-
const LEN: usize = 64;
28-
struct ArrayVisitor<T> {
29-
element: PhantomData<T>,
30-
}
31-
32-
impl<'de, T> Visitor<'de> for ArrayVisitor<T>
33-
where T: Default + Copy + Deserialize<'de>
34-
{
35-
type Value = [T; LEN];
36-
37-
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
38-
write!(formatter, "an array of length {}", LEN)
39-
}
40-
41-
fn visit_seq<A>(self, mut seq: A) -> Result<[T; LEN], A::Error>
42-
where A: SeqAccess<'de>
43-
{
44-
let mut arr = [T::default(); LEN];
45-
for i in 0..LEN {
46-
arr[i] = seq.next_element()?
47-
.ok_or_else(|| de::Error::invalid_length(i, &self))?;
48-
}
49-
Ok(arr)
50-
}
51-
}
52-
53-
let visitor = ArrayVisitor { element: PhantomData };
54-
let b: [u8; LEN] = deserializer.deserialize_tuple(LEN, visitor)?;
24+
let b: [u8; 64] = ByteArray::deserialize(deserializer)?.into_array();
5525
Signature::from_bytes(&b.into()).map_err(de::Error::custom)
5626
}
5727

@@ -72,11 +42,11 @@ pub struct DeltaWitness {
7242
}
7343

7444
fn serialize_signing_key<S>(t: &SigningKey, s: S) -> Result<S::Ok, S::Error> where S: Serializer {
75-
s.serialize_bytes(&t.to_bytes())
45+
ByteArray::<32>::new(t.to_bytes().into()).serialize(s)
7646
}
7747

7848
fn deserialize_signing_key<'de, D>(deserializer: D) -> Result<SigningKey, D::Error> where D: Deserializer<'de> {
79-
let b: [u8; 32] = Deserialize::deserialize(deserializer)?;
49+
let b: [u8; 32] = ByteArray::deserialize(deserializer)?.into_array();
8050
SigningKey::from_bytes(&b.into()).map_err(de::Error::custom)
8151
}
8252

arm/src/logic_proof.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ pub trait LogicProver: Default + Clone + Serialize + for<'de> Deserialize<'de> {
4848
#[derive(Clone, Debug, Deserialize, Serialize)]
4949
#[cfg_attr(feature = "nif", serde(rename = "Elixir.Anoma.Arm.LogicVerifier"))]
5050
pub struct LogicVerifier {
51+
#[serde(with = "serde_bytes")]
5152
pub proof: Vec<u8>,
53+
#[serde(with = "serde_bytes")]
5254
pub instance: Vec<u8>,
5355
pub verifying_key: Vec<u32>,
5456
}
@@ -59,6 +61,7 @@ pub struct LogicVerifierInputs {
5961
pub tag: Vec<u32>,
6062
pub verifying_key: Vec<u32>,
6163
pub app_data: AppData,
64+
#[serde(with = "serde_bytes")]
6265
pub proof: Vec<u8>,
6366
}
6467

arm/src/resource.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@ use serde::{Deserialize, Serialize};
2929
#[cfg_attr(feature = "nif", serde(rename = "Elixir.Anoma.Arm.Resource"))]
3030
pub struct Resource {
3131
// a succinct representation of the predicate associated with the resource
32+
#[serde(with = "serde_bytes")]
3233
pub logic_ref: Vec<u8>,
3334
// specifies the fungibility domain for the resource
35+
#[serde(with = "serde_bytes")]
3436
pub label_ref: Vec<u8>,
3537
// number representing the quantity of the resource
3638
pub quantity: u128,
3739
// the fungible value reference of the resource
40+
#[serde(with = "serde_bytes")]
3841
pub value_ref: Vec<u8>,
3942
// flag that reflects the resource ephemerality
4043
pub is_ephemeral: bool,
4144
// guarantees the uniqueness of the resource computable components
45+
#[serde(with = "serde_bytes")]
4246
pub nonce: Vec<u8>,
4347
// commitment to nullifier key
4448
pub nk_commitment: NullifierKeyCommitment,
4549
// randomness seed used to derive whatever randomness needed
50+
#[serde(with = "serde_bytes")]
4651
pub rand_seed: Vec<u8>,
4752
}
4853

0 commit comments

Comments
 (0)