Skip to content

Commit

Permalink
fix(core): fix inconsistencies in byte serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
cfcosta committed Jul 30, 2024
1 parent 7d02eb1 commit 9136605
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 55 deletions.
22 changes: 9 additions & 13 deletions core/src/types/event/fission.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Error, Hash, Result, SerializeBytes};
use crate::{Hash, Result, SerializeBytes};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
Expand All @@ -13,20 +13,16 @@ impl SerializeBytes for Fission {
const SIZE: usize = 3 * 32;

fn to_slice(&self, out: &mut [u8]) {
out[..32].copy_from_slice(&*self.a);
out[32..64].copy_from_slice(&*self.b);
out[64..].copy_from_slice(&*self.c);
self.a.to_slice(&mut out[..Hash::SIZE]);
self.b.to_slice(&mut out[Hash::SIZE..Hash::SIZE * 2]);
self.c.to_slice(&mut out[Hash::SIZE * 2..Hash::SIZE * 3]);
}

fn from_slice(input: &[u8]) -> Result<Self> {
if input.len() < Self::SIZE {
return Err(Error::FailedDeserialization);
}

let a = input[..32].try_into()?;
let b = input[Hash::SIZE..Hash::SIZE * 2].try_into()?;
let c = input[Hash::SIZE..Hash::SIZE * 2].try_into()?;

Ok(Self { a, b, c })
Ok(Self {
a: Hash::from_slice(&input[..Hash::SIZE])?,
b: Hash::from_slice(&input[Hash::SIZE..Hash::SIZE * 2])?,
c: Hash::from_slice(&input[Hash::SIZE * 2..Hash::SIZE * 3])?,
})
}
}
22 changes: 9 additions & 13 deletions core/src/types/event/fusion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Error, Hash, Result, SerializeBytes};
use crate::{Hash, Result, SerializeBytes};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
Expand All @@ -13,20 +13,16 @@ impl SerializeBytes for Fusion {
const SIZE: usize = 3 * 32;

fn to_slice(&self, out: &mut [u8]) {
out[..32].copy_from_slice(&*self.a);
out[32..64].copy_from_slice(&*self.b);
out[64..].copy_from_slice(&*self.c);
self.a.to_slice(&mut out[..Hash::SIZE]);
self.b.to_slice(&mut out[Hash::SIZE..Hash::SIZE * 2]);
self.c.to_slice(&mut out[Hash::SIZE * 2..Hash::SIZE * 3]);
}

fn from_slice(input: &[u8]) -> Result<Self> {
if input.len() < Self::SIZE {
return Err(Error::FailedDeserialization);
}

let a = input[..32].try_into()?;
let b = input[Hash::SIZE..Hash::SIZE * 2].try_into()?;
let c = input[Hash::SIZE..Hash::SIZE * 2].try_into()?;

Ok(Self { a, b, c })
Ok(Self {
a: Hash::from_slice(&input[..Hash::SIZE])?,
b: Hash::from_slice(&input[Hash::SIZE..Hash::SIZE * 2])?,
c: Hash::from_slice(&input[Hash::SIZE * 2..Hash::SIZE * 3])?,
})
}
}
36 changes: 10 additions & 26 deletions core/src/types/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,16 @@ impl SerializeBytes for Note {
fn to_slice(&self, out: &mut [u8]) {
self.asset_id.to_slice(&mut out[..Hash::SIZE]);
self.amount
.to_le_bytes()
.copy_from_slice(&mut out[Hash::SIZE..Hash::SIZE + u64::SIZE]);
self.nullifier.to_slice(&mut out[Hash::SIZE + u64::SIZE..]);
.to_slice(&mut out[Hash::SIZE..Hash::SIZE + u64::SIZE]);
self.nullifier
.to_slice(&mut out[Hash::SIZE + u64::SIZE..Self::SIZE]);
}

fn from_slice(input: &[u8]) -> Result<Self> {
if input.len() < Self::SIZE {
return Err(Error::FailedDeserialization);
}

Ok(Self {
asset_id: Hash::from_slice(&input[..Hash::SIZE])?,
amount: u64::from_le_bytes(
input[Hash::SIZE..Hash::SIZE + u64::SIZE]
.try_into()
.unwrap(),
),
nullifier: Signature::from_slice(&input[Hash::SIZE + u64::SIZE..])?,
amount: u64::from_slice(&input[Hash::SIZE..Hash::SIZE + u64::SIZE])?,
nullifier: Signature::from_slice(&input[Hash::SIZE + u64::SIZE..Self::SIZE])?,
})
}
}
Expand Down Expand Up @@ -64,24 +56,16 @@ impl SerializeBytes for BlindedNote {
fn to_slice(&self, out: &mut [u8]) {
self.asset_id.to_slice(&mut out[..Hash::SIZE]);
self.amount
.to_le_bytes()
.copy_from_slice(&mut out[Hash::SIZE..Hash::SIZE + u64::SIZE]);
self.secret.to_slice(&mut out[Hash::SIZE + u64::SIZE..]);
.to_slice(&mut out[Hash::SIZE..Hash::SIZE + u64::SIZE]);
self.secret
.to_slice(&mut out[Hash::SIZE + u64::SIZE..Self::SIZE]);
}

fn from_slice(input: &[u8]) -> Result<Self> {
if input.len() < Self::SIZE {
return Err(Error::FailedDeserialization);
}

Ok(Self {
asset_id: Hash::from_slice(&input[..Hash::SIZE])?,
amount: u64::from_le_bytes(
input[Hash::SIZE..Hash::SIZE + u64::SIZE]
.try_into()
.unwrap(),
),
secret: Hash::from_slice(&input[Hash::SIZE + u64::SIZE..])?,
amount: u64::from_slice(&input[Hash::SIZE..Hash::SIZE + u64::SIZE])?,
secret: Hash::from_slice(&input[Hash::SIZE + u64::SIZE..Self::SIZE])?,
})
}
}
10 changes: 7 additions & 3 deletions core/src/types/operation/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ impl SerializeBytes for Join {
self.inputs[1].to_slice(&mut out[Note::SIZE..]);
}

fn from_slice(bytes: &[u8]) -> Result<Self> {
let (a, b) = <(Note, Note)>::from_slice(bytes)?;
Ok(Self { inputs: [a, b] })
fn from_slice(input: &[u8]) -> Result<Self> {
Ok(Self {
inputs: [
Note::from_slice(&input[..Note::SIZE])?,
Note::from_slice(&input[Note::SIZE..Note::SIZE * 2])?,
],
})
}
}

0 comments on commit 9136605

Please sign in to comment.