feat: bootstrap moon-i2p core protocol foundation
check / check (ubuntu-latest) (push) Failing after 30s
check / check (macos-latest) (push) Failing after 33s
check / check (windows-latest) (push) Failing after 32s

This commit is contained in:
Antigravity committed 2026-11-27 15:37:42 +00:00
commit e527b84bc3
47 files changed
+5350

No files matched your search

+488
View File
@@ -0,0 +1,488 @@
///|
/// Errors raised when encoding, decoding, or validating I2P Common Structures.
#derive(Eq, Debug)
pub suberror CommonError {
Encoding(@encoding.EncodingError)
InvalidLength(String)
InvalidFormat(String)
}
///|
/// A 32-byte SHA-256 hash used throughout I2P for RouterIdentities, Destinations, and NetDB keys.
#derive(Eq, Debug)
pub struct Hash {
bytes : Bytes
}
///|
pub fn Hash::from_bytes(bytes : Bytes) -> Hash raise CommonError {
if bytes.length() != 32 {
raise InvalidLength("I2P Hash must be 32 bytes, got \{bytes.length()}")
}
{ bytes, }
}
///|
pub fn Hash::sha256(data : Bytes) -> Hash {
{ bytes: @crypto.sha256(data), }
}
///|
pub fn Hash::as_bytes(self : Hash) -> Bytes {
self.bytes
}
///|
pub fn Hash::to_base32(self : Hash) -> String {
@encoding.base32_encode(self.bytes)
}
///|
pub fn Hash::to_b32_address(self : Hash) -> String {
self.to_base32() + ".b32.i2p"
}
///|
pub fn Hash::to_base64(self : Hash) -> String {
@encoding.base64_encode(self.bytes)
}
///|
pub fn Hash::to_hex(self : Hash) -> String {
@encoding.hex_encode(self.bytes)
}
///|
pub fn Hash::encode(self : Hash, w : @encoding.Writer) -> Unit {
w.write_bytes(self.bytes)
}
///|
pub fn Hash::decode(r : @encoding.Reader) -> Hash raise CommonError {
let bytes = r.read_bytes(32) catch { e => raise Encoding(e) }
{ bytes, }
}
///|
/// 32-byte symmetric session key.
#derive(Eq, Debug)
pub struct SessionKey {
bytes : Bytes
}
///|
pub fn SessionKey::from_bytes(bytes : Bytes) -> SessionKey raise CommonError {
if bytes.length() != 32 {
raise InvalidLength("SessionKey must be 32 bytes, got \{bytes.length()}")
}
{ bytes, }
}
///|
/// Session tag (8 bytes for ECIES-X25519-AEAD-Ratchet, or 32 bytes for legacy ElGamal/AES).
#derive(Eq, Debug)
pub struct SessionTag {
bytes : Bytes
}
///|
pub fn SessionTag::from_bytes(bytes : Bytes) -> SessionTag raise CommonError {
if bytes.length() != 8 && bytes.length() != 32 {
raise InvalidLength(
"SessionTag must be 8 or 32 bytes, got \{bytes.length()}",
)
}
{ bytes, }
}
///|
/// I2P Cryptography Public Key types (per I2P Common Structures specification).
#derive(Eq, Debug)
pub(all) enum CryptoPublicKeyType {
ElGamal // 0 (256 bytes)
P256 // 1 (64 bytes)
P384 // 2 (96 bytes)
P521 // 3 (132 bytes)
X25519 // 4 (32 bytes) - modern default
Unknown(UInt32)
}
///|
pub fn CryptoPublicKeyType::to_u16(self : CryptoPublicKeyType) -> UInt32 {
match self {
ElGamal => 0U
P256 => 1U
P384 => 2U
P521 => 3U
X25519 => 4U
Unknown(v) => v
}
}
///|
pub fn CryptoPublicKeyType::from_u16(v : UInt32) -> CryptoPublicKeyType {
match v {
0U => ElGamal
1U => P256
2U => P384
3U => P521
4U => X25519
_ => Unknown(v)
}
}
///|
pub fn CryptoPublicKeyType::key_length(self : CryptoPublicKeyType) -> Int32 {
match self {
ElGamal => 256
P256 => 64
P384 => 96
P521 => 132
X25519 => 32
Unknown(_) => 0
}
}
///|
/// I2P Signing Public Key types (per I2P Common Structures specification).
#derive(Eq, Debug)
pub(all) enum SigningPublicKeyType {
DsaSha1 // 0 (128B key, 40B sig)
EcdsaSha256P256 // 1 (64B key, 64B sig)
EcdsaSha384P384 // 2 (96B key, 96B sig)
EcdsaSha512P521 // 3 (132B key, 132B sig)
RsaSha256_2048 // 4 (256B key, 256B sig)
RsaSha384_3072 // 5 (384B key, 384B sig)
RsaSha512_4096 // 6 (512B key, 512B sig)
EdDsaSha512Ed25519 // 7 (32B key, 64B sig) - modern default
EdDsaSha512Ed25519ph // 8 (32B key, 64B sig)
RedDsaSha512Ed25519 // 11 (32B key, 64B sig)
Unknown(UInt32)
}
///|
pub fn SigningPublicKeyType::to_u16(self : SigningPublicKeyType) -> UInt32 {
match self {
DsaSha1 => 0U
EcdsaSha256P256 => 1U
EcdsaSha384P384 => 2U
EcdsaSha512P521 => 3U
RsaSha256_2048 => 4U
RsaSha384_3072 => 5U
RsaSha512_4096 => 6U
EdDsaSha512Ed25519 => 7U
EdDsaSha512Ed25519ph => 8U
RedDsaSha512Ed25519 => 11U
Unknown(v) => v
}
}
///|
pub fn SigningPublicKeyType::from_u16(v : UInt32) -> SigningPublicKeyType {
match v {
0U => DsaSha1
1U => EcdsaSha256P256
2U => EcdsaSha384P384
3U => EcdsaSha512P521
4U => RsaSha256_2048
5U => RsaSha384_3072
6U => RsaSha512_4096
7U => EdDsaSha512Ed25519
8U => EdDsaSha512Ed25519ph
11U => RedDsaSha512Ed25519
_ => Unknown(v)
}
}
///|
pub fn SigningPublicKeyType::public_key_length(
self : SigningPublicKeyType,
) -> Int32 {
match self {
DsaSha1 => 128
EcdsaSha256P256 => 64
EcdsaSha384P384 => 96
EcdsaSha512P521 => 132
RsaSha256_2048 => 256
RsaSha384_3072 => 384
RsaSha512_4096 => 512
EdDsaSha512Ed25519 | EdDsaSha512Ed25519ph | RedDsaSha512Ed25519 => 32
Unknown(_) => 0
}
}
///|
pub fn SigningPublicKeyType::signature_length(
self : SigningPublicKeyType,
) -> Int32 {
match self {
DsaSha1 => 40
EcdsaSha256P256 => 64
EcdsaSha384P384 => 96
EcdsaSha512P521 => 132
RsaSha256_2048 => 256
RsaSha384_3072 => 384
RsaSha512_4096 => 512
EdDsaSha512Ed25519 | EdDsaSha512Ed25519ph | RedDsaSha512Ed25519 => 64
Unknown(_) => 0
}
}
///|
/// Key Certificate payload (Certificate type 5).
#derive(Eq, Debug)
pub(all) struct KeyCertificate {
sig_type : SigningPublicKeyType
crypto_type : CryptoPublicKeyType
excess_signing_key : Bytes
excess_crypto_key : Bytes
}
///|
pub fn KeyCertificate::new(
sig_type : SigningPublicKeyType,
crypto_type : CryptoPublicKeyType,
excess_signing_key~ : Bytes = b"",
excess_crypto_key~ : Bytes = b"",
) -> KeyCertificate {
{ sig_type, crypto_type, excess_signing_key, excess_crypto_key, }
}
///|
/// I2P Certificate structure (1-byte type, 2-byte big-endian length, payload).
#derive(Eq, Debug)
pub(all) enum Certificate {
Null // 0
HashCash(Bytes) // 1
Hidden // 2
Signed(Bytes) // 3
Multiple(Bytes) // 4
Key(KeyCertificate) // 5
Unknown(Byte, Bytes)
}
///|
pub fn Certificate::encode(self : Certificate, w : @encoding.Writer) -> Unit {
match self {
Null => {
w.write_u8(b'\x00')
w.write_u16_be(0U)
}
HashCash(p) => {
w.write_u8(b'\x01')
w.write_u16_be(p.length().reinterpret_as_uint())
w.write_bytes(p)
}
Hidden => {
w.write_u8(b'\x02')
w.write_u16_be(0U)
}
Signed(p) => {
w.write_u8(b'\x03')
w.write_u16_be(p.length().reinterpret_as_uint())
w.write_bytes(p)
}
Multiple(p) => {
w.write_u8(b'\x04')
w.write_u16_be(p.length().reinterpret_as_uint())
w.write_bytes(p)
}
Key(kc) => {
let payload_len = 4 +
kc.excess_signing_key.length() +
kc.excess_crypto_key.length()
w.write_u8(b'\x05')
w.write_u16_be(payload_len.reinterpret_as_uint())
w.write_u16_be(kc.sig_type.to_u16())
w.write_u16_be(kc.crypto_type.to_u16())
w.write_bytes(kc.excess_signing_key)
w.write_bytes(kc.excess_crypto_key)
}
Unknown(t, p) => {
w.write_u8(t)
w.write_u16_be(p.length().reinterpret_as_uint())
w.write_bytes(p)
}
}
}
///|
pub fn Certificate::decode(
r : @encoding.Reader,
) -> Certificate raise CommonError {
let cert_type = r.read_u8() catch { e => raise Encoding(e) }
let payload_len = (r.read_u16_be() catch { e => raise Encoding(e) }).reinterpret_as_int()
let payload = r.read_bytes(payload_len) catch { e => raise Encoding(e) }
match cert_type.to_int() {
0 => {
if payload_len != 0 {
raise InvalidLength("Null certificate must have 0-byte payload")
}
Null
}
1 => HashCash(payload)
2 => Hidden
3 => Signed(payload)
4 => Multiple(payload)
5 => {
if payload_len < 4 {
raise InvalidLength("Key certificate payload must be at least 4 bytes")
}
let pr = @encoding.Reader::new(payload)
let sig_type = SigningPublicKeyType::from_u16(
pr.read_u16_be() catch {
e => raise Encoding(e)
},
)
let crypto_type = CryptoPublicKeyType::from_u16(
pr.read_u16_be() catch {
e => raise Encoding(e)
},
)
let sig_excess = if sig_type.public_key_length() > 128 {
sig_type.public_key_length() - 128
} else {
0
}
let excess_sig = if sig_excess > 0 && pr.remaining() >= sig_excess {
pr.read_bytes(sig_excess) catch {
e => raise Encoding(e)
}
} else {
b""
}
let excess_crypto = pr.read_remaining()
Key({
sig_type,
crypto_type,
excess_signing_key: excess_sig,
excess_crypto_key: excess_crypto,
})
}
_ => Unknown(cert_type, payload)
}
}
///|
/// I2P Mapping: 2-byte big-endian total byte length followed by `key=value;` pairs
/// where keys and values are I2P Strings. Canonical encoding sorts entries by key.
#derive(Eq, Debug)
pub struct Mapping {
entries : Array[(String, String)]
}
///|
pub fn Mapping::new() -> Mapping {
{ entries: [], }
}
///|
pub fn Mapping::from_entries(entries : Array[(String, String)]) -> Mapping {
let copy : Array[(String, String)] = Array(capacity=entries.length())
for entry in entries {
copy.push(entry)
}
{ entries: copy, }
}
///|
pub fn Mapping::set(self : Mapping, key : String, value : String) -> Unit {
for i = 0; i < self.entries.length(); i = i + 1 {
if self.entries[i].0 == key {
self.entries[i] = (key, value)
return
}
}
self.entries.push((key, value))
}
///|
pub fn Mapping::get(self : Mapping, key : String) -> String? {
for entry in self.entries {
if entry.0 == key {
return Some(entry.1)
}
}
None
}
///|
pub fn Mapping::length(self : Mapping) -> Int32 {
self.entries.length()
}
///|
fn bytes_lex_gt(a : Bytes, b : Bytes) -> Bool {
let min_len = if a.length() < b.length() { a.length() } else { b.length() }
for i = 0; i < min_len; i = i + 1 {
let ai = a[i].to_int()
let bi = b[i].to_int()
if ai != bi {
return ai > bi
}
}
a.length() > b.length()
}
///|
/// Encodes the Mapping in canonical I2P order (sorted ascending by UTF-8 key bytes).
pub fn Mapping::encode(
self : Mapping,
w : @encoding.Writer,
) -> Unit raise CommonError {
let sorted : Array[(String, String)] = Array(capacity=self.entries.length())
for entry in self.entries {
sorted.push(entry)
}
// Insertion sort by UTF-8 key bytes (canonical I2P Mapping sort order)
for i = 1; i < sorted.length(); i = i + 1 {
let cur = sorted[i]
let cur_bytes = @encoding.string_to_utf8_bytes(cur.0)
let mut j = i - 1
while j >= 0 &&
bytes_lex_gt(@encoding.string_to_utf8_bytes(sorted[j].0), cur_bytes) {
sorted[j + 1] = sorted[j]
j -= 1
}
sorted[j + 1] = cur
}
let body = @encoding.Writer::new()
for entry in sorted {
body.write_i2p_string(entry.0) catch {
e => raise Encoding(e)
}
body.write_u8(b'\x3d') // '='
body.write_i2p_string(entry.1) catch {
e => raise Encoding(e)
}
body.write_u8(b'\x3b') // ';'
}
let raw = body.to_bytes()
if raw.length() > 65535 {
raise InvalidLength("Mapping exceeds 65535 bytes: \{raw.length()}")
}
w.write_u16_be(raw.length().reinterpret_as_uint())
w.write_bytes(raw)
}
///|
pub fn Mapping::decode(r : @encoding.Reader) -> Mapping raise CommonError {
let byte_len = (r.read_u16_be() catch { e => raise Encoding(e) }).reinterpret_as_int()
let raw = r.read_bytes(byte_len) catch { e => raise Encoding(e) }
let mr = @encoding.Reader::new(raw)
let entries : Array[(String, String)] = []
while !mr.is_eof() {
let k = mr.read_i2p_string() catch { e => raise Encoding(e) }
let eq = mr.read_u8() catch { e => raise Encoding(e) }
if eq != b'\x3d' {
raise InvalidFormat("expected '=' in Mapping, got \{eq}")
}
let v = mr.read_i2p_string() catch { e => raise Encoding(e) }
let semi = mr.read_u8() catch { e => raise Encoding(e) }
if semi != b'\x3b' {
raise InvalidFormat("expected ';' in Mapping, got \{semi}")
}
entries.push((k, v))
}
{ entries, }
}