Skip to content

Commit d3b2839

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

File tree

15 files changed

+117
-54
lines changed

15 files changed

+117
-54
lines changed

Cargo.lock

Lines changed: 27 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: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,77 @@ 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

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

1310
#[derive(Clone, Debug, Deserialize, Serialize)]
1411
#[cfg_attr(feature = "nif", serde(rename = "Elixir.Anoma.Arm.DeltaProof"))]
1512
pub struct DeltaProof {
16-
#[serde(deserialize_with = "deserialize_signature", serialize_with = "serialize_signature")]
13+
#[serde(
14+
deserialize_with = "deserialize_signature",
15+
serialize_with = "serialize_signature"
16+
)]
1717
pub signature: Signature,
18-
#[serde(deserialize_with = "deserialize_recovery_id", serialize_with = "serialize_recovery_id")]
18+
#[serde(
19+
deserialize_with = "deserialize_recovery_id",
20+
serialize_with = "serialize_recovery_id"
21+
)]
1922
pub recid: RecoveryId,
2023
}
2124

22-
fn serialize_signature<S>(t: &Signature, s: S) -> Result<S::Ok, S::Error> where S: Serializer {
23-
s.serialize_bytes(&t.to_bytes())
25+
fn serialize_signature<S>(t: &Signature, s: S) -> Result<S::Ok, S::Error>
26+
where
27+
S: Serializer,
28+
{
29+
ByteArray::<64>::new(t.to_bytes().into()).serialize(s)
2430
}
2531

26-
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)?;
32+
fn deserialize_signature<'de, D>(deserializer: D) -> Result<Signature, D::Error>
33+
where
34+
D: Deserializer<'de>,
35+
{
36+
let b: [u8; 64] = ByteArray::deserialize(deserializer)?.into_array();
5537
Signature::from_bytes(&b.into()).map_err(de::Error::custom)
5638
}
5739

58-
fn serialize_recovery_id<S>(t: &RecoveryId, s: S) -> Result<S::Ok, S::Error> where S: Serializer {
40+
fn serialize_recovery_id<S>(t: &RecoveryId, s: S) -> Result<S::Ok, S::Error>
41+
where
42+
S: Serializer,
43+
{
5944
s.serialize_u8(t.to_byte())
6045
}
6146

62-
fn deserialize_recovery_id<'de, D>(d: D) -> Result<RecoveryId, D::Error> where D: Deserializer<'de> {
47+
fn deserialize_recovery_id<'de, D>(d: D) -> Result<RecoveryId, D::Error>
48+
where
49+
D: Deserializer<'de>,
50+
{
6351
let s: u8 = de::Deserialize::deserialize(d)?;
6452
RecoveryId::try_from(s).map_err(de::Error::custom)
6553
}
6654

6755
#[derive(Clone, Debug, Deserialize, Serialize)]
6856
#[cfg_attr(feature = "nif", serde(rename = "Elixir.Anoma.Arm.DeltaWitness"))]
6957
pub struct DeltaWitness {
70-
#[serde(deserialize_with = "deserialize_signing_key", serialize_with = "serialize_signing_key")]
58+
#[serde(
59+
deserialize_with = "deserialize_signing_key",
60+
serialize_with = "serialize_signing_key"
61+
)]
7162
pub signing_key: SigningKey,
7263
}
7364

74-
fn serialize_signing_key<S>(t: &SigningKey, s: S) -> Result<S::Ok, S::Error> where S: Serializer {
75-
s.serialize_bytes(&t.to_bytes())
65+
fn serialize_signing_key<S>(t: &SigningKey, s: S) -> Result<S::Ok, S::Error>
66+
where
67+
S: Serializer,
68+
{
69+
ByteArray::<32>::new(t.to_bytes().into()).serialize(s)
7670
}
7771

78-
fn deserialize_signing_key<'de, D>(deserializer: D) -> Result<SigningKey, D::Error> where D: Deserializer<'de> {
79-
let b: [u8; 32] = Deserialize::deserialize(deserializer)?;
72+
fn deserialize_signing_key<'de, D>(deserializer: D) -> Result<SigningKey, D::Error>
73+
where
74+
D: Deserializer<'de>,
75+
{
76+
let b: [u8; 32] = ByteArray::deserialize(deserializer)?.into_array();
8077
SigningKey::from_bytes(&b.into()).map_err(de::Error::custom)
8178
}
8279

arm/src/encryption.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl SecretKey {
2727
}
2828

2929
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
30-
pub struct Ciphertext(Vec<u8>);
30+
pub struct Ciphertext(#[serde(with = "serde_bytes")] Vec<u8>);
3131

3232
impl Ciphertext {
3333
pub fn from_bytes(cipher: Vec<u8>) -> Self {
@@ -88,8 +88,10 @@ impl Ciphertext {
8888
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
8989
struct InnerCiphert {
9090
// AES GCM encrypted message
91+
#[serde(with = "serde_bytes")]
9192
pub cipher: Vec<u8>,
9293
// 96-bits; unique per message
94+
#[serde(with = "serde_bytes")]
9395
pub nonce: [u8; 12],
9496
// Sender's public key
9597
pub pk: AffinePoint,

arm/src/logic_proof.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,23 @@ 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
}
5557

5658
#[derive(Clone, Debug, Deserialize, Serialize)]
57-
#[cfg_attr(feature = "nif", serde(rename = "Elixir.Anoma.Arm.LogicVerifierInputs"))]
59+
#[cfg_attr(
60+
feature = "nif",
61+
serde(rename = "Elixir.Anoma.Arm.LogicVerifierInputs")
62+
)]
5863
pub struct LogicVerifierInputs {
5964
pub tag: Vec<u32>,
6065
pub verifying_key: Vec<u32>,
6166
pub app_data: AppData,
67+
#[serde(with = "serde_bytes")]
6268
pub proof: Vec<u8>,
6369
}
6470

arm/src/nullifier_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
44

55
/// Nullifier key
66
#[derive(Clone, Debug, Serialize, Deserialize)]
7-
pub struct NullifierKey(Vec<u8>);
7+
pub struct NullifierKey(#[serde(with = "serde_bytes")] Vec<u8>);
88

99
impl NullifierKey {
1010
pub fn new(nf_key: &[u8]) -> NullifierKey {
@@ -40,7 +40,7 @@ impl Default for NullifierKey {
4040

4141
/// Commitment to nullifier key
4242
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
43-
pub struct NullifierKeyCommitment(Vec<u8>);
43+
pub struct NullifierKeyCommitment(#[serde(with = "serde_bytes")] Vec<u8>);
4444

4545
impl NullifierKeyCommitment {
4646
pub fn inner(&self) -> &[u8] {

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

arm/src/resource_logic.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ pub trait LogicCircuit: Default + Clone + Serialize + for<'de> Deserialize<'de>
1616
}
1717

1818
#[derive(Clone, Default, Serialize, Deserialize)]
19-
#[cfg_attr(feature = "nif", serde(rename = "Elixir.Anoma.Arm.TrivialLogicWitness"))]
19+
#[cfg_attr(
20+
feature = "nif",
21+
serde(rename = "Elixir.Anoma.Arm.TrivialLogicWitness")
22+
)]
2023
pub struct TrivialLogicWitness {
2124
pub resource: Resource,
2225
pub receive_existence_path: MerklePath,

0 commit comments

Comments
 (0)