feat: add pure-MoonBit SHA-512, Ed25519, AES-256-CBC, and signed RouterInfo/LeaseSet2

This commit is contained in:
nandi committed 2026-11-27 19:21:57 +00:00
1 parent c95e9cfc21
commit dff69dfac6
9 files changed
+1330 -4

No files matched your search

+117 -4
View File
@@ -78,6 +78,52 @@ pub fn RouterInfo::encode(
w.write_bytes(self.signature)
}
///|
pub fn RouterInfo::to_bytes(self : RouterInfo) -> Bytes raise CommonError {
let w = @encoding.Writer::new(capacity=600)
self.encode(w)
w.to_bytes()
}
///|
/// Signs the `RouterInfo` using an Ed25519 32-byte private `seed` and returns
/// a new `RouterInfo` with `signature` populated.
pub fn RouterInfo::sign_ed25519(
self : RouterInfo,
seed : Bytes,
) -> RouterInfo raise CommonError {
if self.identity.signing_type() != EdDsaSha512Ed25519 {
raise InvalidFormat(
"RouterInfo identity signing type must be EdDsaSha512Ed25519",
)
}
let body = self.unsigned_bytes()
let signature = @crypto.ed25519_sign(seed, body) catch {
e => raise Crypto(e)
}
{
identity: self.identity,
published: self.published,
addresses: self.addresses,
options: self.options,
signature,
}
}
///|
/// Verifies the `RouterInfo` Ed25519 signature against `self.identity.signing_public_key()`.
pub fn RouterInfo::verify_signature(self : RouterInfo) -> Bool {
if self.identity.signing_type() != EdDsaSha512Ed25519 {
return false
}
let body = self.unsigned_bytes() catch { _ => return false }
@crypto.ed25519_verify(
self.identity.signing_public_key(),
body,
self.signature,
)
}
///|
pub fn RouterInfo::decode(r : @encoding.Reader) -> RouterInfo raise CommonError {
let identity = RouterIdentity::decode(r)
@@ -175,10 +221,9 @@ pub(all) struct LeaseSet2 {
}
///|
pub fn LeaseSet2::encode(
self : LeaseSet2,
w : @encoding.Writer,
) -> Unit raise CommonError {
/// Encodes the unsigned body of LeaseSet2 (everything before `signature`).
pub fn LeaseSet2::unsigned_bytes(self : LeaseSet2) -> Bytes raise CommonError {
let w = @encoding.Writer::new(capacity=512)
self.destination.encode(w)
w.write_u32_be(self.published_sec)
w.write_u16_be(self.expires_offset)
@@ -194,9 +239,77 @@ pub fn LeaseSet2::encode(
for lease in self.leases {
lease.encode(w)
}
w.to_bytes()
}
///|
/// Returns the exact bytes covered by the LeaseSet2 signature:
/// single-byte DatabaseStore type `0x03` prepended to `unsigned_bytes()` (Proposal 123).
pub fn LeaseSet2::signing_bytes(self : LeaseSet2) -> Bytes raise CommonError {
let body = self.unsigned_bytes()
let w = @encoding.Writer::new(capacity=1 + body.length())
w.write_u8(b'\x03')
w.write_bytes(body)
w.to_bytes()
}
///|
pub fn LeaseSet2::encode(
self : LeaseSet2,
w : @encoding.Writer,
) -> Unit raise CommonError {
w.write_bytes(self.unsigned_bytes())
w.write_bytes(self.signature)
}
///|
pub fn LeaseSet2::to_bytes(self : LeaseSet2) -> Bytes raise CommonError {
let w = @encoding.Writer::new(capacity=600)
self.encode(w)
w.to_bytes()
}
///|
/// Signs the `LeaseSet2` using an Ed25519 32-byte private `seed` (over `0x03 || unsigned_bytes`).
pub fn LeaseSet2::sign_ed25519(
self : LeaseSet2,
seed : Bytes,
) -> LeaseSet2 raise CommonError {
if self.destination.inner.signing_type() != EdDsaSha512Ed25519 {
raise InvalidFormat(
"LeaseSet2 destination signing type must be EdDsaSha512Ed25519",
)
}
let to_sign = self.signing_bytes()
let signature = @crypto.ed25519_sign(seed, to_sign) catch {
e => raise Crypto(e)
}
{
destination: self.destination,
published_sec: self.published_sec,
expires_offset: self.expires_offset,
flags: self.flags,
options: self.options,
encryption_keys: self.encryption_keys,
leases: self.leases,
signature,
}
}
///|
/// Verifies the `LeaseSet2` Ed25519 signature against `self.destination.inner.signing_public_key()`.
pub fn LeaseSet2::verify_signature(self : LeaseSet2) -> Bool {
if self.destination.inner.signing_type() != EdDsaSha512Ed25519 {
return false
}
let to_sign = self.signing_bytes() catch { _ => return false }
@crypto.ed25519_verify(
self.destination.inner.signing_public_key(),
to_sign,
self.signature,
)
}
///|
pub fn LeaseSet2::decode(r : @encoding.Reader) -> LeaseSet2 raise CommonError {
let destination = Destination::decode(r)