///| /// Pure-MoonBit RFC 7748 X25519 Elliptic Curve Diffie-Hellman on Curve25519. /// Field elements mod 2^255 - 19 are represented as 16 signed 64-bit limbs (16 bits per limb). ///| fn gf_zero() -> FixedArray[Int64] { FixedArray::make(16, 0L) } ///| fn gf_one() -> FixedArray[Int64] { let r = FixedArray::make(16, 0L) r[0] = 1L r } ///| fn gf_copy(src : FixedArray[Int64]) -> FixedArray[Int64] { let dst = FixedArray::make(16, 0L) for i = 0; i < 16; i = i + 1 { dst[i] = src[i] } dst } ///| fn gf_carry(o : FixedArray[Int64]) -> Unit { for i = 0; i < 16; i = i + 1 { o[i] = o[i] + 65536L let c = o[i] >> 16 if i < 15 { o[i + 1] = o[i + 1] + c - 1L } else { o[0] = o[0] + (c - 1L) * 38L } o[i] = o[i] - (c << 16) } } ///| fn gf_cswap(p : FixedArray[Int64], q : FixedArray[Int64], b : Int64) -> Unit { let mask = -b for i = 0; i < 16; i = i + 1 { let t = mask & (p[i] ^ q[i]) p[i] = p[i] ^ t q[i] = q[i] ^ t } } ///| fn gf_unpack(n : Bytes) -> FixedArray[Int64] { let o = FixedArray::make(16, 0L) for i = 0; i < 16; i = i + 1 { o[i] = n[2 * i].to_int64() + (n[2 * i + 1].to_int64() << 8) } o[15] = o[15] & 0x7fffL o } ///| fn gf_pack(n : FixedArray[Int64]) -> Bytes { let t = gf_copy(n) let m = FixedArray::make(16, 0L) gf_carry(t) gf_carry(t) gf_carry(t) for _j = 0; _j < 2; _j = _j + 1 { m[0] = t[0] - 0xffedL for i = 1; i < 15; i = i + 1 { m[i] = t[i] - 0xffffL - ((m[i - 1] >> 16) & 1L) m[i - 1] = m[i - 1] & 0xffffL } m[15] = t[15] - 0x7fffL - ((m[14] >> 16) & 1L) let b = (m[15] >> 16) & 1L m[14] = m[14] & 0xffffL gf_cswap(t, m, 1L - b) } let out = FixedArray::make(32, b'\x00') for i = 0; i < 16; i = i + 1 { out[2 * i] = (t[i] & 0xffL).to_byte() out[2 * i + 1] = ((t[i] >> 8) & 0xffL).to_byte() } Bytes::from_array(out) } ///| fn gf_add(a : FixedArray[Int64], b : FixedArray[Int64]) -> FixedArray[Int64] { let o = FixedArray::make(16, 0L) for i = 0; i < 16; i = i + 1 { o[i] = a[i] + b[i] } o } ///| fn gf_sub(a : FixedArray[Int64], b : FixedArray[Int64]) -> FixedArray[Int64] { let o = FixedArray::make(16, 0L) for i = 0; i < 16; i = i + 1 { o[i] = a[i] - b[i] } o } ///| fn gf_mul(a : FixedArray[Int64], b : FixedArray[Int64]) -> FixedArray[Int64] { let t = FixedArray::make(31, 0L) for i = 0; i < 16; i = i + 1 { for j = 0; j < 16; j = j + 1 { t[i + j] = t[i + j] + a[i] * b[j] } } for i = 0; i < 15; i = i + 1 { t[i] = t[i] + 38L * t[i + 16] } let o = FixedArray::make(16, 0L) for i = 0; i < 16; i = i + 1 { o[i] = t[i] } gf_carry(o) gf_carry(o) o } ///| fn gf_sq(a : FixedArray[Int64]) -> FixedArray[Int64] { gf_mul(a, a) } ///| fn gf_inv(a : FixedArray[Int64]) -> FixedArray[Int64] { let mut c = gf_copy(a) for i = 253; i >= 0; i = i - 1 { c = gf_sq(c) if i != 2 && i != 4 { c = gf_mul(c, a) } } c } ///| /// Computes the X25519 scalar multiplication `scalar * u_coord` (RFC 7748 Section 5). /// Both `scalar` and `u_coord` must be 32 bytes. pub fn x25519(scalar : Bytes, u_coord : Bytes) -> Bytes raise CryptoError { if scalar.length() != 32 { raise InvalidKeyLength( "X25519 scalar must be 32 bytes, got \{scalar.length()}", ) } if u_coord.length() != 32 { raise InvalidKeyLength( "X25519 u-coordinate must be 32 bytes, got \{u_coord.length()}", ) } let z = FixedArray::make(32, 0) for i = 0; i < 32; i = i + 1 { z[i] = scalar[i].to_int() } // Clamp scalar per RFC 7748 z[0] = z[0] & 248 z[31] = (z[31] & 127) | 64 let x = gf_unpack(u_coord) let mut a = gf_one() let mut b = gf_copy(x) let mut c = gf_zero() let mut d = gf_one() let a24 = gf_zero() a24[0] = 0xdb41L a24[1] = 1L for i = 254; i >= 0; i = i - 1 { let r = ((z[i >> 3] >> (i & 7)) & 1).to_int64() gf_cswap(a, b, r) gf_cswap(c, d, r) let e = gf_add(a, c) let a1 = gf_sub(a, c) let c1 = gf_add(b, d) let b1 = gf_sub(b, d) let d1 = gf_sq(e) let f = gf_sq(a1) let a2 = gf_mul(c1, a1) let c2 = gf_mul(b1, e) let e2 = gf_add(a2, c2) let a3 = gf_sub(a2, c2) let b2 = gf_sq(a3) let c3 = gf_sub(d1, f) let a4 = gf_mul(c3, a24) let a5 = gf_add(a4, d1) let c4 = gf_mul(c3, a5) let a6 = gf_mul(d1, f) let d2 = gf_mul(b2, x) let b3 = gf_sq(e2) a = a6 b = b3 c = c4 d = d2 gf_cswap(a, b, r) gf_cswap(c, d, r) } let inv_c = gf_inv(c) let res = gf_mul(a, inv_c) gf_pack(res) } ///| /// Computes the 32-byte X25519 public key from a 32-byte private scalar. pub fn x25519_public_key(private_key : Bytes) -> Bytes raise CryptoError { let basepoint = FixedArray::make(32, b'\x00') basepoint[0] = b'\x09' x25519(private_key, Bytes::from_array(basepoint)) }