lssa/nssa/core/src/encoding.rs

262 lines
7.6 KiB
Rust
Raw Normal View History

2025-08-18 18:18:16 -03:00
// TODO: Consider switching to deriving Borsh
2025-08-18 14:28:26 -03:00
#[cfg(feature = "host")]
use std::io::Cursor;
#[cfg(feature = "host")]
2026-03-04 18:42:33 +03:00
use std::io::Read as _;
2025-08-18 14:28:26 -03:00
2025-11-26 00:27:20 +03:00
#[cfg(feature = "host")]
use crate::Nullifier;
2025-08-26 14:53:02 -03:00
#[cfg(feature = "host")]
use crate::encryption::shared_key_derivation::Secp256k1Point;
2025-08-18 14:28:26 -03:00
#[cfg(feature = "host")]
use crate::error::NssaCoreError;
2025-11-26 00:27:20 +03:00
use crate::{
Commitment, NullifierPublicKey,
account::{Account, AccountId},
encryption::Ciphertext,
};
2025-08-26 14:53:02 -03:00
2025-08-18 09:21:07 -03:00
impl Account {
2026-03-03 23:21:08 +03:00
/// Serializes the account to bytes.
#[must_use]
2025-08-18 09:21:07 -03:00
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
for word in &self.program_owner {
bytes.extend_from_slice(&word.to_le_bytes());
}
bytes.extend_from_slice(&self.balance.to_le_bytes());
2026-02-12 19:22:03 -05:00
bytes.extend_from_slice(&self.nonce.0.to_le_bytes());
2026-03-18 10:28:52 -04:00
let data_length: u32 = u32::try_from(self.data.len()).expect("Invalid u32");
2025-08-18 14:28:26 -03:00
bytes.extend_from_slice(&data_length.to_le_bytes());
bytes.extend_from_slice(self.data.as_ref());
2025-08-18 09:21:07 -03:00
bytes
}
2025-08-18 14:28:26 -03:00
2026-03-03 23:21:08 +03:00
/// Deserializes an account from a cursor.
2025-08-18 14:28:26 -03:00
#[cfg(feature = "host")]
pub fn from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Self, NssaCoreError> {
2026-02-12 19:22:03 -05:00
use crate::account::{Nonce, data::Data};
2026-03-04 18:42:33 +03:00
let mut u32_bytes = [0_u8; 4];
let mut u128_bytes = [0_u8; 16];
2025-08-18 14:28:26 -03:00
// program owner
2026-03-04 18:42:33 +03:00
let mut program_owner = [0_u32; 8];
2025-08-18 14:28:26 -03:00
for word in &mut program_owner {
cursor.read_exact(&mut u32_bytes)?;
*word = u32::from_le_bytes(u32_bytes);
}
// balance
cursor.read_exact(&mut u128_bytes)?;
let balance = u128::from_le_bytes(u128_bytes);
// nonce
cursor.read_exact(&mut u128_bytes)?;
2026-02-12 19:22:03 -05:00
let nonce = Nonce(u128::from_le_bytes(u128_bytes));
2025-08-18 14:28:26 -03:00
2025-11-26 00:27:20 +03:00
// data
let data = Data::from_cursor(cursor)?;
2025-08-18 14:28:26 -03:00
Ok(Self {
program_owner,
balance,
data,
nonce,
})
}
2025-08-18 09:21:07 -03:00
}
2025-08-18 09:50:11 -03:00
impl Commitment {
2026-03-03 23:21:08 +03:00
#[must_use]
2026-03-09 18:27:56 +03:00
pub const fn to_byte_array(&self) -> [u8; 32] {
2025-08-18 09:50:11 -03:00
self.0
}
#[cfg(feature = "host")]
2026-03-03 23:21:08 +03:00
#[must_use]
2026-03-09 18:27:56 +03:00
pub const fn from_byte_array(bytes: [u8; 32]) -> Self {
Self(bytes)
}
2026-03-03 23:21:08 +03:00
/// Deserializes a commitment from a cursor.
2025-08-18 14:28:26 -03:00
#[cfg(feature = "host")]
pub fn from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Self, NssaCoreError> {
2026-03-04 18:42:33 +03:00
let mut bytes = [0_u8; 32];
2025-08-18 14:28:26 -03:00
cursor.read_exact(&mut bytes)?;
Ok(Self(bytes))
}
}
2025-08-18 09:50:11 -03:00
impl NullifierPublicKey {
2026-03-03 23:21:08 +03:00
#[must_use]
2026-03-09 18:27:56 +03:00
pub const fn to_byte_array(&self) -> [u8; 32] {
2025-08-18 09:50:11 -03:00
self.0
}
}
2025-08-18 14:28:26 -03:00
#[cfg(feature = "host")]
impl Nullifier {
2026-03-03 23:21:08 +03:00
#[must_use]
2026-03-09 18:27:56 +03:00
pub const fn to_byte_array(&self) -> [u8; 32] {
2025-08-18 14:28:26 -03:00
self.0
}
#[cfg(feature = "host")]
2026-03-03 23:21:08 +03:00
#[must_use]
2026-03-09 18:27:56 +03:00
pub const fn from_byte_array(bytes: [u8; 32]) -> Self {
Self(bytes)
}
2026-03-03 23:21:08 +03:00
/// Deserializes a nullifier from a cursor.
2025-08-18 14:28:26 -03:00
pub fn from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Self, NssaCoreError> {
2026-03-04 18:42:33 +03:00
let mut bytes = [0_u8; 32];
2025-08-18 14:28:26 -03:00
cursor.read_exact(&mut bytes)?;
Ok(Self(bytes))
}
}
2025-08-26 14:53:02 -03:00
impl Ciphertext {
2026-03-03 23:21:08 +03:00
/// Serializes the ciphertext to bytes.
#[must_use]
2025-08-26 14:53:02 -03:00
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
2026-03-03 23:21:08 +03:00
let ciphertext_length: u32 =
u32::try_from(self.0.len()).expect("ciphertext length fits in u32");
2025-08-26 14:53:02 -03:00
bytes.extend_from_slice(&ciphertext_length.to_le_bytes());
bytes.extend_from_slice(&self.0);
bytes
}
#[cfg(feature = "host")]
2026-03-03 23:21:08 +03:00
#[must_use]
pub fn into_inner(self) -> Vec<u8> {
self.0
}
#[cfg(feature = "host")]
2026-03-03 23:21:08 +03:00
#[must_use]
2026-03-09 18:27:56 +03:00
pub const fn from_inner(inner: Vec<u8>) -> Self {
Self(inner)
}
2025-08-26 14:53:02 -03:00
#[cfg(feature = "host")]
2026-03-03 23:21:08 +03:00
/// Deserializes ciphertext from a cursor.
2025-08-26 14:53:02 -03:00
pub fn from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Self, NssaCoreError> {
let mut u32_bytes = [0; 4];
cursor.read_exact(&mut u32_bytes)?;
let ciphertext_lenght = u32::from_le_bytes(u32_bytes);
2026-03-04 18:42:33 +03:00
let ciphertext_length =
usize::try_from(ciphertext_lenght).expect("ciphertext length fits in usize");
let mut ciphertext = vec![0; ciphertext_length];
2025-08-26 14:53:02 -03:00
cursor.read_exact(&mut ciphertext)?;
Ok(Self(ciphertext))
}
}
#[cfg(feature = "host")]
impl Secp256k1Point {
2026-03-03 23:21:08 +03:00
/// Converts the point to bytes.
#[must_use]
2025-08-26 14:53:02 -03:00
pub fn to_bytes(&self) -> [u8; 33] {
self.0.clone().try_into().unwrap()
}
2026-03-03 23:21:08 +03:00
/// Deserializes a secp256k1 point from a cursor.
2025-08-26 14:53:02 -03:00
pub fn from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Self, NssaCoreError> {
let mut value = vec![0; 33];
cursor.read_exact(&mut value)?;
Ok(Self(value))
}
}
2025-09-12 15:18:25 -03:00
impl AccountId {
2026-03-03 23:21:08 +03:00
#[must_use]
2026-03-09 18:27:56 +03:00
pub const fn to_bytes(&self) -> [u8; 32] {
2025-09-30 16:45:25 -03:00
*self.value()
2025-09-12 15:18:25 -03:00
}
}
2025-08-18 09:21:07 -03:00
#[cfg(test)]
mod tests {
2025-08-18 14:28:26 -03:00
use super::*;
2025-08-18 09:21:07 -03:00
#[test]
2026-03-04 18:42:33 +03:00
fn enconding() {
2025-08-18 09:21:07 -03:00
let account = Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
2026-03-18 13:10:36 -04:00
balance: 123_456_789_012_345_678_901_234_567_890_123_456,
nonce: 42_u128.into(),
data: b"hola mundo".to_vec().try_into().unwrap(),
2025-08-18 09:21:07 -03:00
};
2025-08-27 18:02:10 -03:00
// program owner || balance || nonce || data_len || data
2025-08-18 09:21:07 -03:00
let expected_bytes = [
1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8,
0, 0, 0, 192, 186, 220, 114, 113, 65, 236, 234, 222, 15, 215, 191, 227, 198, 23, 0, 42,
2025-08-18 14:28:26 -03:00
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 104, 111, 108, 97, 32, 109,
117, 110, 100, 111,
2025-08-18 09:21:07 -03:00
];
let bytes = account.to_bytes();
assert_eq!(bytes, expected_bytes);
}
2025-08-18 14:28:26 -03:00
#[test]
2026-03-04 18:42:33 +03:00
fn commitment_to_bytes() {
2025-08-18 14:28:26 -03:00
let commitment = Commitment((0..32).collect::<Vec<u8>>().try_into().unwrap());
let expected_bytes: [u8; 32] = (0..32).collect::<Vec<u8>>().try_into().unwrap();
let bytes = commitment.to_byte_array();
assert_eq!(expected_bytes, bytes);
}
2025-08-27 16:24:20 -03:00
#[cfg(feature = "host")]
2025-08-18 14:28:26 -03:00
#[test]
2026-03-04 18:42:33 +03:00
fn nullifier_to_bytes() {
2025-08-18 14:28:26 -03:00
let nullifier = Nullifier((0..32).collect::<Vec<u8>>().try_into().unwrap());
let expected_bytes: [u8; 32] = (0..32).collect::<Vec<u8>>().try_into().unwrap();
let bytes = nullifier.to_byte_array();
assert_eq!(expected_bytes, bytes);
}
#[cfg(feature = "host")]
#[test]
2026-03-04 18:42:33 +03:00
fn commitment_to_bytes_roundtrip() {
2025-08-18 14:28:26 -03:00
let commitment = Commitment((0..32).collect::<Vec<u8>>().try_into().unwrap());
let bytes = commitment.to_byte_array();
let mut cursor = Cursor::new(bytes.as_ref());
let commitment_from_cursor = Commitment::from_cursor(&mut cursor).unwrap();
assert_eq!(commitment, commitment_from_cursor);
}
#[cfg(feature = "host")]
#[test]
2026-03-04 18:42:33 +03:00
fn nullifier_to_bytes_roundtrip() {
2025-08-18 14:28:26 -03:00
let nullifier = Nullifier((0..32).collect::<Vec<u8>>().try_into().unwrap());
let bytes = nullifier.to_byte_array();
let mut cursor = Cursor::new(bytes.as_ref());
let nullifier_from_cursor = Nullifier::from_cursor(&mut cursor).unwrap();
assert_eq!(nullifier, nullifier_from_cursor);
}
#[cfg(feature = "host")]
#[test]
2026-03-04 18:42:33 +03:00
fn account_to_bytes_roundtrip() {
2025-08-18 14:28:26 -03:00
let account = Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
2026-03-18 13:10:36 -04:00
balance: 123_456_789_012_345_678_901_234_567_890_123_456,
nonce: 42_u128.into(),
data: b"hola mundo".to_vec().try_into().unwrap(),
2025-08-18 14:28:26 -03:00
};
let bytes = account.to_bytes();
let mut cursor = Cursor::new(bytes.as_ref());
let account_from_cursor = Account::from_cursor(&mut cursor).unwrap();
assert_eq!(account, account_from_cursor);
}
2025-08-18 09:21:07 -03:00
}