///| /// 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)) } } }