Skip to content
Draft
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
130 changes: 130 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pbjson = "0.7.0"
pin-project = "1.1"
prost = "0.13.4"
rand = "0.9.0"
rkyv = { version = "0.8", features = ["hashbrown-0_15", "std", "bytecheck"] }
rs_merkle = { version = "1.4", default-features = false }
rstest = "0.25.0"
serde = { version = "1.0", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions crates/agglayer-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ arbitrary = { workspace = true, optional = true }
byteorder.workspace = true
derive_more.workspace = true
k256.workspace = true
rkyv.workspace = true
serde.workspace = true
hex.workspace = true
rand = { workspace = true, optional = true }
Expand Down
17 changes: 16 additions & 1 deletion crates/agglayer-primitives/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
pub use alloy_primitives::Address as AlloyAddress;

#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[rkyv(remote = AlloyAddress)]
pub struct AddressDef([u8; 20]);

impl From<AddressDef> for AlloyAddress {
#[inline]
fn from(value: AddressDef) -> Self {
AlloyAddress::from_slice(&value.0)
}
}

/// A wrapper over [alloy_primitives::Address] that allows us to add custom
/// methods and trait implementations.
#[derive(
Expand All @@ -20,14 +31,18 @@ pub use alloy_primitives::Address as AlloyAddress;
derive_more::Display,
derive_more::LowerHex,
derive_more::UpperHex,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
)]
#[cfg_attr(feature = "testutils", derive(arbitrary::Arbitrary))]
#[from(AlloyAddress, [u8; 20])]
#[into(AlloyAddress, [u8; 20])]
#[as_ref(AlloyAddress, [u8; 20], [u8])]
#[repr(transparent)]
#[serde(rename = "agglayer_primitives::Address")]
pub struct Address(AlloyAddress);

pub struct Address(#[rkyv(with = AddressDef)] pub AlloyAddress);

impl Address {
pub const ZERO: Self = Self::from_alloy(AlloyAddress::ZERO);
Expand Down
14 changes: 13 additions & 1 deletion crates/agglayer-primitives/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ use crate::{
U256,
};

#[derive(Default, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[derive(
Default,
Hash,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
)]
#[cfg_attr(feature = "testutils", derive(arbitrary::Arbitrary))]
pub struct Digest(pub [u8; 32]);

Expand Down
2 changes: 1 addition & 1 deletion crates/agglayer-primitives/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait Hasher {
}

/// A Keccak hasher with a 256-bit security level.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, rkyv::Archive)]
pub struct Keccak256Hasher;

impl Hasher for Keccak256Hasher {
Expand Down
2 changes: 1 addition & 1 deletion crates/agglayer-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ pub mod keccak;
mod signature;
mod utils;

pub use address::Address;
pub use address::{Address, AddressDef};
pub use digest::Digest;
pub use utils::{FromBool, FromU256, Hashable};
68 changes: 65 additions & 3 deletions crates/agglayer-primitives/src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,74 @@
use alloy_primitives::Signature as AlloySignature;
use k256::ecdsa;
use rkyv::{Archive, Deserialize, Serialize};

use super::{Address, SignatureError, B256, U256};

#[derive(
Copy,
Debug,
Hash,
PartialEq,
Eq,
Clone,
serde::Serialize,
serde::Deserialize,
Archive,
Serialize,
Deserialize,
)]
#[rkyv(remote = AlloySignature)]
struct AlloySignatureDef {
#[rkyv(getter = AlloySignature::v)]
y_parity: bool,
#[rkyv(getter = AlloySignature::r, with = U256Def)]
r: U256,
#[rkyv(getter = AlloySignature::s, with = U256Def)]
s: U256,
}

impl From<AlloySignatureDef> for AlloySignature {
#[inline]
fn from(value: AlloySignatureDef) -> Self {
AlloySignature::new(value.r, value.s, value.y_parity)
}
}

#[derive(Archive, Serialize, Deserialize)]
#[rkyv(remote = U256)]
pub struct U256Def {
#[rkyv(getter = U256::as_limbs)]
limbs: [u64; 4],
}

impl From<U256Def> for U256 {
#[inline]
fn from(value: U256Def) -> Self {
U256::from_limbs(value.limbs)
}
}

/// A wrapper over [AlloySignature] with custom serialization.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(
Clone,
Copy,
Debug,
Hash,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
)]
#[cfg_attr(feature = "testutils", derive(arbitrary::Arbitrary))]
#[serde(from = "compat::Signature", into = "compat::Signature")]
pub struct Signature(AlloySignature);
pub struct Signature(
#[serde(with = "AlloySignatureDef")]
#[rkyv(with = AlloySignatureDef)]
AlloySignature,
);

impl Signature {
#[inline]
Expand All @@ -22,7 +83,8 @@ impl Signature {

#[inline]
pub fn recover_address_from_prehash(&self, prehash: &B256) -> Result<Address, SignatureError> {
self.0
let signature: AlloySignature = self.0.into();
signature
.recover_address_from_prehash(prehash)
.map(Address::from)
}
Expand Down
1 change: 1 addition & 0 deletions crates/agglayer-tries/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true
agglayer-primitives.workspace = true

hex.workspace = true
rkyv.workspace = true
serde.workspace = true
serde_with.workspace = true
thiserror.workspace = true
Expand Down
Loading
Loading