Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ssz::{Decode, DecodeError, Encode};
use std::ops::{Deref, DerefMut};

use crate::F;
use crate::serialization::Serializable;
use p3_field::{PrimeCharacteristicRing, PrimeField32, RawDataSerializable};

/// A wrapper around an array of field elements that implements SSZ Encode/Decode.
Expand Down Expand Up @@ -86,6 +87,8 @@ impl<const N: usize> Decode for FieldArray<N> {
}
}

impl<const N: usize> Serializable for FieldArray<N> {}

impl<const N: usize> Serialize for FieldArray<N> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
6 changes: 3 additions & 3 deletions src/inc_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rand::Rng;
use serde::{Serialize, de::DeserializeOwned};
use std::fmt::Debug;

use crate::MESSAGE_LENGTH;
use crate::serialization::Serializable;

/// Trait to model incomparable encoding schemes.
/// These schemes allow to encode a message into a codeword.
Expand All @@ -17,8 +17,8 @@ use crate::MESSAGE_LENGTH;
/// x = (x_1,..,x_k) and x' = (x'_1,..,x'_k) we have
/// x_i > x'_i for all i = 1,...,k.
pub trait IncomparableEncoding {
type Parameter: Serialize + DeserializeOwned;
type Randomness: Serialize + DeserializeOwned;
type Parameter: Serializable;
type Randomness: Serializable;
type Error: Debug;

/// number of entries in a codeword
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) type PackedF = <F as Field>::Packing;
pub(crate) mod array;
pub(crate) mod hypercube;
pub(crate) mod inc_encoding;
pub mod serialization;
pub mod signature;
pub(crate) mod simd_utils;
pub(crate) mod symmetric;
Expand Down
45 changes: 45 additions & 0 deletions src/serialization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! A unified serialization implementation

use serde::{Serialize, de::DeserializeOwned};
use ssz::{Decode, DecodeError, Encode};

/// A supertrait combining all serialization capabilities needed for leanSig types.
pub trait Serializable: Serialize + DeserializeOwned + Encode + Decode + Sized {
/// Converts this object to a canonical byte representation.
///
/// # Canonical Format
///
/// - All field elements are converted to canonical `u32` form (not Montgomery)
/// - All `u32` values are encoded as 4 bytes in little-endian order
///
/// # Returns
///
/// A `Vec<u8>` containing the canonical byte representation of this object.
fn to_bytes(&self) -> Vec<u8> {
// TODO: Update this to not use SSZ internally.
self.as_ssz_bytes()
}

/// Parses an object from its canonical byte representation.
///
/// # Canonical Format
///
/// The input bytes must follow the same canonical format as `to_bytes()`:
/// - Field elements as canonical `u32` values (4 bytes, little-endian)
/// - Composite structures following SSZ layout rules
///
/// # Arguments
///
/// * `bytes` - The canonical binary data to parse
///
/// # Returns
///
/// - `Ok(Self)` if the bytes represent a valid object
/// - `Err(DecodeError)` if the bytes are malformed or invalid
fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
// TODO: Update this to not use SSZ internally.
Self::from_ssz_bytes(bytes)
}
}

impl Serializable for [u8; 32] {}
13 changes: 8 additions & 5 deletions src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::ops::Range;

use crate::MESSAGE_LENGTH;
use crate::serialization::Serializable;
use rand::Rng;
use serde::{Serialize, de::DeserializeOwned};
use ssz::{Decode, Encode};
use thiserror::Error;

/// Error enum for the signing process.
Expand Down Expand Up @@ -99,17 +98,21 @@ pub trait SignatureScheme {
/// The key must be serializable to allow for network transmission and storage.
///
/// We must support SSZ encoding for Ethereum consensus layer compatibility.
type PublicKey: Serialize + DeserializeOwned + Encode + Decode;
type PublicKey: Serializable;

/// The secret key used for signing.
///
/// The key must be serializable for persistence and secure backup.
type SecretKey: SignatureSchemeSecretKey + Serialize + DeserializeOwned;
///
/// We must support SSZ encoding for Ethereum consensus layer compatibility.
type SecretKey: SignatureSchemeSecretKey + Serializable;

/// The signature object produced by the signing algorithm.
///
/// The signature must be serializable to allow for network transmission and storage.
type Signature: Serialize + DeserializeOwned;
///
/// We must support SSZ encoding for Ethereum consensus layer compatibility.
type Signature: Serializable;

/// The maximum number of epochs supported by this signature scheme configuration,
/// denoted as $L$ in the literature [DKKW25a, DKKW25b].
Expand Down
Loading