Files
nandi ae72b00323
check / check (windows-latest) (push) Failing after 27s
check / check (ubuntu-latest) (push) Failing after 29s
check / check (macos-latest) (push) Failing after 27s
feat: add ratchet, streaming, SU3 reseed, and router floodfill
- 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)
2026-11-27 22:23:46 -08:00

187 lines
6.1 KiB
MoonBit

///|
fn make_signed_router(
seed_byte : Byte,
caps : String,
published : UInt64,
) -> @common.RouterInfo {
let ed_seed = Bytes::make(32, seed_byte)
let ed_pub = try! @crypto.ed25519_public_key(ed_seed)
let x_priv = Bytes::make(32, (seed_byte.to_int() ^ 0x55).to_byte())
let x_pub = try! @crypto.x25519_public_key(x_priv)
let id = try! @common.RouterIdentity::new_x25519_ed25519(x_pub, ed_pub)
let ri : @common.RouterInfo = {
identity: id,
published,
addresses: [],
options: @common.Mapping::from_entries([
("caps", caps),
("netId", "2"),
("router.version", "0.9.64"),
]),
signature: b"",
}
try! ri.sign_ed25519(ed_seed)
}
///|
test "daily routing key derivation and UTC date formatting" {
assert_eq(@netdb.format_utc_date(0UL), "19700101")
assert_eq(@netdb.format_utc_date(1709164800UL), "20240229") // leap day
assert_eq(@netdb.format_utc_date(1700000000UL), "20231114")
// Known-answer check: routing_key(key, "20240229") == SHA-256(key || "20240229")
let key = @common.Hash::sha256(b"test-destination-key")
let rk = @netdb.routing_key(key, "20240229")
let expected_in = FixedArray::make(40, b'\x00')
let kb = key.as_bytes()
let db = b"20240229"
for i = 0; i < 32; i = i + 1 {
expected_in[i] = kb[i]
}
for i = 0; i < 8; i = i + 1 {
expected_in[32 + i] = db[i]
}
assert_eq(rk, @common.Hash::sha256(Bytes::from_array(expected_in)))
// Different day produces a different routing key
assert_true(rk != @netdb.routing_key(key, "20240301"))
}
///|
test "NetDB Kademlia XOR distance, floodfill selection, and DatabaseLookup" {
let db = @netdb.NetDb::new()
let r1 = make_signed_router(b'\x01', "XfR", 1700000000000UL) // floodfill
let r2 = make_signed_router(b'\x02', "LR", 1700000000000UL) // non-floodfill
let r3 = make_signed_router(b'\x03', "OfR", 1700000000000UL) // floodfill
let r4 = make_signed_router(b'\x04', "XfR", 1700000000000UL) // floodfill
assert_eq(@netdb.is_floodfill(r1), true)
assert_eq(@netdb.is_floodfill(r2), false)
assert_eq(db.store_router_info(r1), true)
assert_eq(db.store_router_info(r2), true)
assert_eq(db.store_router_info(r3), true)
assert_eq(db.store_router_info(r4), true)
assert_eq(db.router_count(), 4)
// Stale RouterInfo update is rejected (returns false)
assert_eq(db.store_router_info(r1), false)
// Newer RouterInfo update is accepted
let r1_newer = make_signed_router(b'\x01', "XfR", 1700000005000UL)
assert_eq(db.store_router_info(r1_newer), true)
// Closest floodfills excludes r2 (non-floodfill) and orders ascending by XOR distance
let target = @common.Hash::sha256(b"lookup-target")
let ffs = db.closest_peers(target, "20231114", 3, floodfills_only=true)
assert_eq(ffs.length(), 3)
let rkey = @netdb.routing_key(target, "20231114")
let d0 = @netdb.xor_distance(ffs[0].identity.hash(), rkey)
let d1 = @netdb.xor_distance(ffs[1].identity.hash(), rkey)
let d2 = @netdb.xor_distance(ffs[2].identity.hash(), rkey)
assert_true(@netdb.compare_distance(d0, d1) <= 0)
assert_true(@netdb.compare_distance(d1, d2) <= 0)
// DatabaseLookup for known RouterInfo returns DatabaseStore
let local_h = r1.identity.hash()
let hit = db.handle_database_lookup(
local_h,
r3.identity.hash(),
b'\x08',
[],
1700000010U,
)
match hit {
DatabaseStore(key~, store_type~, ..) => {
assert_eq(key, r3.identity.hash())
assert_eq(store_type, b'\x00')
}
_ => fail("expected DatabaseStore hit")
}
// DatabaseLookup for unknown key returns DatabaseSearchReply with closest floodfills
let miss = db.handle_database_lookup(
local_h,
target,
b'\x08',
[ffs[0].identity.hash()],
1700000010U,
max_peers=2,
)
match miss {
DatabaseSearchReply(key~, peer_hashes~, from~) => {
assert_eq(key, target)
assert_eq(from, local_h)
assert_eq(peer_hashes.length(), 2)
assert_true(peer_hashes[0] != ffs[0].identity.hash())
}
_ => fail("expected DatabaseSearchReply")
}
}
///|
test "NetDB LeaseSet2 store, .b32.i2p lookup, and expiration pruning" {
let db = @netdb.NetDb::new()
let ed_seed = Bytes::make(32, b'\x77')
let ed_pub = @crypto.ed25519_public_key(ed_seed)
let x_pub = @crypto.x25519_public_key(Bytes::make(32, b'\x88'))
let dest = @common.Destination::new_x25519_ed25519(x_pub, ed_pub)
let ls2 : @common.LeaseSet2 = {
destination: dest,
published_sec: 1700000000U,
expires_offset: 300U,
flags: 0U,
offline_sig: None,
options: @common.Mapping::new(),
encryption_keys: [{ key_type: X25519, key_data: x_pub, }],
leases: [
{
tunnel_gw: @common.Hash::sha256(b"gw"),
tunnel_id: 101U,
end_date_sec: 1700000300U,
},
],
signature: b"",
}
let signed = ls2.sign_ed25519(ed_seed)
assert_eq(db.store_lease_set2(signed, 1700000010U), true)
assert_eq(db.leaseset_count(), 1)
assert_eq(
db.get_lease_set2_by_b32(dest.b32_address(), 1700000100U),
Some(signed),
)
// After expiration (1700000300), lookup returns None and prune removes it
assert_eq(db.get_lease_set2(dest.hash(), 1700000301U), None)
assert_eq(db.prune_expired(1700000301U), 1)
assert_eq(db.leaseset_count(), 0)
}
///|
test "NetDB canonical gzipped DatabaseStore type 0 round-trip" {
let src = @netdb.NetDb::new()
let dst = @netdb.NetDb::new()
let r1 = make_signed_router(b'\x0a', "XfR", 1700000000000UL)
let r2 = make_signed_router(b'\x0b', "LR", 1700000000000UL)
assert_eq(src.store_router_info(r1), true)
assert_eq(src.store_router_info(r2), true)
let resp = src.handle_database_lookup(
r1.identity.hash(),
r2.identity.hash(),
b'\x08',
[],
1700000010U,
)
match resp {
DatabaseStore(key~, store_type~, data~, ..) => {
assert_eq(store_type, b'\x00')
// Canonical DatabaseStore type 0 starts with 2-byte BE length + gzip magic 0x1f 0x8b
assert_eq(data[2], b'\x1f')
assert_eq(data[3], b'\x8b')
assert_eq(
dst.handle_database_store(store_type, key, data, 1700000010U),
true,
)
assert_eq(dst.get_router_info(r2.identity.hash()), Some(r2))
}
_ => fail("expected gzipped DatabaseStore")
}
}