feat: add ratchet, streaming, SU3 reseed, and router floodfill
check / check (windows-latest) (push) Failing after 27s
check / check (ubuntu-latest) (push) Failing after 29s
check / check (macos-latest) (push) Failing after 27s

- Add ratchet/ with ECIES-X25519-AEAD-Ratchet (Proposal 144), Garlic cloves, and ECIES DatabaseLookup replies
- Add streaming/ with Ed25519-signed I2P Streaming (SYN/ACK/NACK/CLOSE/RESET, retransmit, window) and Repliable/Raw Datagrams
- Add common/su3.mbt to parse and verify reseed .su3 bundles
- Add Elligator2 encoding in crypto/ and OfflineSignature support in LeaseSet2 (flags bit 0)
- Router: NTCP2 peer pool, floodfill NetDB crawl, transit tunnel relay, and Garlic/Streaming bridge
- cmd/router: add --peer-dir, --su3, --reseed, --floodfill, --no-clock-skew
- cmd/client: add --dest for persistent destination keys
- Mark Phase 6 complete in README and document the transport scope (NTCP2 only, no SSU2)
This commit is contained in:
nandi committed 2026-11-27 22:23:46 -08:00
1 parent 8eafa11b22
commit ae72b00323
54 files changed
+9607 -704

No files matched your search

+48
View File
@@ -238,3 +238,51 @@ test "aes256 FIPS-197 Appendix C.3 and SP 800-38A CBC vectors" {
assert_eq(ct2, expected_ct2)
assert_eq(@crypto.aes256_cbc_decrypt(key2, iv2, ct2), pt2)
}
///|
test "curve25519 elligator2 encode/decode reference vectors and properties" {
// Reference vector cross-checked with Python F_p implementation
let priv_key = @encoding.hex_decode(
"dd8464b2cad5dc7381309cc5d57c9d421a58b7a0648eedb387543a594c1988ac",
)
let pub_u = @crypto.x25519_public_key(priv_key)
assert_eq(
@encoding.hex_encode(pub_u),
"85d7ef41e7a6ded4d12312f796c30183a6325a56b734ae7fd19e416b7a47196b",
)
let rf = match @crypto.elligator2_encode(pub_u, high_bits=b'\xc0') {
Some(r) => r
None => fail("expected Elligator2 encode to succeed")
}
assert_eq(
@encoding.hex_encode(rf),
"0da9aad9ab2472b3d50db797d9d880adcb5c2440ed0db37eb30076f208ab23c2",
)
assert_eq(@crypto.elligator2_decode(rf), pub_u)
let rt = match
@crypto.elligator2_encode(pub_u, high_bits=b'\x80', tweak_sign=true) {
Some(r) => r
None => fail("expected Elligator2 tweaked encode to succeed")
}
assert_eq(
@encoding.hex_encode(rt),
"631c48beab8932fafe3e39ee45099a29e8a461c08bdfb037baf33ac61d42a090",
)
assert_eq(@crypto.elligator2_decode(rt), pub_u)
// Sweep 40 keys and verify round-trip for every encodable key
let mut encodable_count = 0
for i = 0; i < 40; i = i + 1 {
let seed = @crypto.sha256(Bytes::make(4, i.to_byte()))
let pk = @crypto.x25519_public_key(seed)
match @crypto.elligator2_encode(pk, high_bits=b'\xc0') {
Some(rep) => {
encodable_count += 1
assert_eq(@crypto.elligator2_decode(rep), pk)
}
None => ()
}
}
assert_true(encodable_count >= 10)
}
+158
View File
@@ -0,0 +1,158 @@
///|
/// Pure-MoonBit Curve25519 Elligator2 point encoding & decoding (I2P Proposal 144).
/// Maps uniformly random-looking 32-byte representatives (with random high 2 bits)
/// to/from Montgomery Curve25519 u-coordinates (`v^2 = u^3 + 486662*u^2 + u mod 2^255-19`).
///|
/// Curve parameter A = 486662 = 0x76d06 in 16-bit limbs.
fn gf_a() -> FixedArray[Int64] {
let a = gf_zero()
a[0] = 0x6d06L
a[1] = 0x0007L
a
}
///|
/// Constant-time zero check on a field element after canonical `gf_pack`.
fn gf_is_zero(w : FixedArray[Int64]) -> Bool {
ct_eq(gf_pack(w), Bytes::make(32, b'\x00'))
}
///|
/// Computes a square root of `w` modulo `2^255 - 19`, or returns `None` if non-square.
fn gf_sqrt_opt(w : FixedArray[Int64]) -> FixedArray[Int64]? {
if gf_is_zero(w) {
return Some(gf_zero())
}
let x = gf_mul(w, gf_pow2523(w))
let x2_packed = gf_pack(gf_sq(x))
let w_packed = gf_pack(w)
if ct_eq(x2_packed, w_packed) {
return Some(x)
}
let neg_w_packed = gf_pack(gf_sub(gf_zero(), w))
if ct_eq(x2_packed, neg_w_packed) {
return Some(gf_mul(x, ed_sqrt_m1()))
}
None
}
///|
fn gf_is_square(w : FixedArray[Int64]) -> Bool {
gf_sqrt_opt(w) is Some(_)
}
///|
/// Canonical `(p - 1) / 2 = 2^254 - 10` in little-endian bytes for selecting
/// the principal non-negative square root `0 <= r <= (p - 1) / 2`.
let half_p_le : FixedArray[Byte] = [
b'\xf6', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff',
b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff',
b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff',
b'\xff', b'\xff', b'\xff', b'\xff', b'\x3f',
]
///|
fn gf_canonical_half(r : FixedArray[Int64]) -> FixedArray[Byte] {
let packed = gf_pack(r)
let mut gt = false
for i = 31; i >= 0; i = i - 1 {
let rb = packed[i].to_int()
let hb = half_p_le[i].to_int()
if rb > hb {
gt = true
break
}
if rb < hb {
break
}
}
let chosen = if gt { gf_pack(gf_sub(gf_zero(), r)) } else { packed }
let out = FixedArray::make(32, b'\x00')
for i = 0; i < 32; i = i + 1 {
out[i] = chosen[i]
}
out
}
///|
/// Decodes a 32-byte Elligator2 representative (clearing the 2 high padding bits
/// per I2P Proposal 144) into its Curve25519 Montgomery u-coordinate (32 bytes LE).
pub fn elligator2_decode(repr : Bytes) -> Bytes raise CryptoError {
if repr.length() != 32 {
raise InvalidKeyLength(
"Elligator2 representative must be 32 bytes, got \{repr.length()}",
)
}
let buf = FixedArray::make(32, b'\x00')
for i = 0; i < 32; i = i + 1 {
buf[i] = repr[i]
}
buf[31] = (buf[31].to_int() & 0x3F).to_byte()
let r = gf_unpack(Bytes::from_array(buf))
let r2 = gf_sq(r)
let two_r2 = gf_add(r2, r2)
let denom = gf_add(gf_one(), two_r2)
if gf_is_zero(denom) {
return Bytes::make(32, b'\x00')
}
let a = gf_a()
let neg_a = gf_sub(gf_zero(), a)
let d = gf_mul(neg_a, gf_inv(denom))
let d2 = gf_sq(d)
let ad = gf_mul(a, d)
let rhs = gf_mul(d, gf_add(gf_add(d2, ad), gf_one()))
let u = if gf_is_zero(rhs) || gf_is_square(rhs) {
d
} else {
gf_sub(gf_sub(gf_zero(), d), a)
}
gf_pack(u)
}
///|
/// Attempts to encode a Curve25519 public key `pub_u` into a 32-byte Elligator2
/// representative with random `high_bits` injected into bits 254..255 (`& 0xC0`).
/// Returns `None` if `pub_u` is not in the image of Elligator2 (~50% of keys).
pub fn elligator2_encode(
pub_u : Bytes,
high_bits~ : Byte = b'\x00',
tweak_sign~ : Bool = false,
) -> Bytes? raise CryptoError {
if pub_u.length() != 32 {
raise InvalidKeyLength(
"Elligator2 public key must be 32 bytes, got \{pub_u.length()}",
)
}
let u = gf_unpack(pub_u)
let a = gf_a()
if gf_is_zero(u) {
if tweak_sign {
return None
}
let out = FixedArray::make(32, b'\x00')
out[31] = (high_bits.to_int() & 0xC0).to_byte()
return Some(Bytes::from_array(out))
}
let u_plus_a = gf_add(u, a)
if gf_is_zero(u_plus_a) {
return None
}
let r2 = if !tweak_sign {
let num = gf_sub(gf_zero(), u_plus_a)
let den = gf_add(u, u)
gf_mul(num, gf_inv(den))
} else {
let num = gf_sub(gf_zero(), u)
let den = gf_add(u_plus_a, u_plus_a)
gf_mul(num, gf_inv(den))
}
match gf_sqrt_opt(r2) {
None => None
Some(r) => {
let out = gf_canonical_half(r)
out[31] = (out[31].to_int() | (high_bits.to_int() & 0xC0)).to_byte()
Some(Bytes::from_array(out))
}
}
}
+4
View File
@@ -30,6 +30,10 @@ pub fn ed25519_sign(Bytes, Bytes) -> Bytes raise CryptoError
pub fn ed25519_verify(Bytes, Bytes, Bytes) -> Bool
pub fn elligator2_decode(Bytes) -> Bytes raise CryptoError
pub fn elligator2_encode(Bytes, high_bits? : Byte, tweak_sign? : Bool) -> Bytes? raise CryptoError
pub fn hkdf(Bytes, Bytes, Bytes, Int32) -> Bytes raise CryptoError
pub fn hkdf_expand(Bytes, Bytes, Int32) -> Bytes raise CryptoError