374 lines
9.7 KiB
MoonBit
374 lines
9.7 KiB
MoonBit
///|
|
|
/// Pure-MoonBit RFC 8032 Ed25519 digital signatures (EdDSA over Edwards25519 with SHA-512).
|
|
/// Reuses 16-limb field arithmetic mod 2^255 - 19 from `x25519.mbt`.
|
|
|
|
///|
|
|
/// Curve constant d = -121665 / 121666 mod p.
|
|
fn ed_d() -> FixedArray[Int64] {
|
|
[
|
|
0x78a3L, 0x1359L, 0x4dcaL, 0x75ebL, 0xd8abL, 0x4141L, 0x0a4dL, 0x0070L, 0xe898L,
|
|
0x7779L, 0x4079L, 0x8cc7L, 0xfe73L, 0x2b6fL, 0x6ceeL, 0x5203L,
|
|
]
|
|
}
|
|
|
|
///|
|
|
/// 2 * d mod p (used in extended twisted Edwards point addition).
|
|
fn ed_2d() -> FixedArray[Int64] {
|
|
let d = ed_d()
|
|
let r = gf_add(d, d)
|
|
gf_carry(r)
|
|
r
|
|
}
|
|
|
|
///|
|
|
/// Square root of -1 mod p: I = 2^((p - 1) / 4) mod p.
|
|
fn ed_sqrt_m1() -> FixedArray[Int64] {
|
|
[
|
|
0xa0b0L, 0x4a0eL, 0x1b27L, 0xc4eeL, 0xe478L, 0xad2fL, 0x1806L, 0x2f43L, 0xd7a7L,
|
|
0x3dfbL, 0x0099L, 0x2b4dL, 0xdf0bL, 0x4fc1L, 0x2480L, 0x2b83L,
|
|
]
|
|
}
|
|
|
|
///|
|
|
/// Extended twisted Edwards point (X, Y, Z, T) where x = X/Z, y = Y/Z, xy = T/Z.
|
|
priv struct EdPoint {
|
|
x : FixedArray[Int64]
|
|
y : FixedArray[Int64]
|
|
z : FixedArray[Int64]
|
|
t : FixedArray[Int64]
|
|
}
|
|
|
|
///|
|
|
fn ed_identity() -> EdPoint {
|
|
{ x: gf_zero(), y: gf_one(), z: gf_one(), t: gf_zero(), }
|
|
}
|
|
|
|
///|
|
|
fn ed_basepoint() -> EdPoint {
|
|
let bx : FixedArray[Int64] = [
|
|
0xd51aL, 0x8f25L, 0x2d60L, 0xc956L, 0xa7b2L, 0x9525L, 0xc760L, 0x692cL, 0xdc5cL,
|
|
0xfdd6L, 0xe231L, 0xc0a4L, 0x53feL, 0xcd6eL, 0x36d3L, 0x2169L,
|
|
]
|
|
let by : FixedArray[Int64] = [
|
|
0x6658L, 0x6666L, 0x6666L, 0x6666L, 0x6666L, 0x6666L, 0x6666L, 0x6666L, 0x6666L,
|
|
0x6666L, 0x6666L, 0x6666L, 0x6666L, 0x6666L, 0x6666L, 0x6666L,
|
|
]
|
|
{ x: bx, y: by, z: gf_one(), t: gf_mul(bx, by), }
|
|
}
|
|
|
|
///|
|
|
/// Unified, complete extended twisted Edwards point addition (a = -1).
|
|
fn ed_add(p : EdPoint, q : EdPoint) -> EdPoint {
|
|
let a = gf_mul(gf_sub(p.y, p.x), gf_sub(q.y, q.x))
|
|
let b = gf_mul(gf_add(p.y, p.x), gf_add(q.y, q.x))
|
|
let c = gf_mul(gf_mul(p.t, q.t), ed_2d())
|
|
let dz = gf_mul(p.z, q.z)
|
|
let d = gf_add(dz, dz)
|
|
let e = gf_sub(b, a)
|
|
let f = gf_sub(d, c)
|
|
let g = gf_add(d, c)
|
|
let h = gf_add(b, a)
|
|
{ x: gf_mul(e, f), y: gf_mul(g, h), z: gf_mul(f, g), t: gf_mul(e, h), }
|
|
}
|
|
|
|
///|
|
|
fn ed_cswap(p : EdPoint, q : EdPoint, b : Int64) -> Unit {
|
|
gf_cswap(p.x, q.x, b)
|
|
gf_cswap(p.y, q.y, b)
|
|
gf_cswap(p.z, q.z, b)
|
|
gf_cswap(p.t, q.t, b)
|
|
}
|
|
|
|
///|
|
|
/// Constant-time scalar multiplication [s]P for a 32-byte little-endian scalar `s`.
|
|
fn ed_scalarmult(p : EdPoint, s : Bytes) -> EdPoint {
|
|
let mut r = ed_identity()
|
|
let mut q = {
|
|
x: gf_copy(p.x),
|
|
y: gf_copy(p.y),
|
|
z: gf_copy(p.z),
|
|
t: gf_copy(p.t),
|
|
}
|
|
for i = 255; i >= 0; i = i - 1 {
|
|
let bit = ((s[i >> 3].to_int() >> (i & 7)) & 1).to_int64()
|
|
ed_cswap(r, q, bit)
|
|
q = ed_add(q, r)
|
|
r = ed_add(r, r)
|
|
ed_cswap(r, q, bit)
|
|
}
|
|
r
|
|
}
|
|
|
|
///|
|
|
/// Encodes an EdPoint to its 32-byte RFC 8032 compressed representation.
|
|
fn ed_pack(p : EdPoint) -> Bytes {
|
|
let zi = gf_inv(p.z)
|
|
let tx = gf_mul(p.x, zi)
|
|
let ty = gf_mul(p.y, zi)
|
|
let xb = gf_pack(tx)
|
|
let yb = gf_pack(ty)
|
|
let out = FixedArray::make(32, b'\x00')
|
|
for i = 0; i < 32; i = i + 1 {
|
|
out[i] = yb[i]
|
|
}
|
|
out[31] = (out[31].to_int() | ((xb[0].to_int() & 1) << 7)).to_byte()
|
|
Bytes::from_array(out)
|
|
}
|
|
|
|
///|
|
|
/// Computes z^((p - 5) / 8) = z^(2^252 - 3) mod p.
|
|
fn gf_pow2523(z : FixedArray[Int64]) -> FixedArray[Int64] {
|
|
let mut c = gf_copy(z)
|
|
for i = 250; i >= 0; i = i - 1 {
|
|
c = gf_sq(c)
|
|
if i != 1 {
|
|
c = gf_mul(c, z)
|
|
}
|
|
}
|
|
c
|
|
}
|
|
|
|
///|
|
|
fn is_zero_bytes(b : Bytes) -> Bool {
|
|
let mut acc = 0
|
|
for i = 0; i < b.length(); i = i + 1 {
|
|
acc = acc | b[i].to_int()
|
|
}
|
|
acc == 0
|
|
}
|
|
|
|
///|
|
|
/// Decodes a 32-byte RFC 8032 compressed point, validating canonical y < p and curve equation.
|
|
fn ed_unpack(s : Bytes) -> EdPoint? {
|
|
if s.length() != 32 {
|
|
return None
|
|
}
|
|
let sign = (s[31].to_int() >> 7) & 1
|
|
let masked = FixedArray::make(32, b'\x00')
|
|
for i = 0; i < 32; i = i + 1 {
|
|
masked[i] = s[i]
|
|
}
|
|
masked[31] = (masked[31].to_int() & 0x7f).to_byte()
|
|
let y = gf_unpack(s)
|
|
// Reject non-canonical y >= p
|
|
if !ct_eq(gf_pack(y), Bytes::from_array(masked)) {
|
|
return None
|
|
}
|
|
let y2 = gf_sq(y)
|
|
let u = gf_sub(y2, gf_one())
|
|
let v = gf_add(gf_mul(ed_d(), y2), gf_one())
|
|
let v3 = gf_mul(gf_sq(v), v)
|
|
let v7 = gf_mul(gf_sq(v3), v)
|
|
let uv3 = gf_mul(u, v3)
|
|
let uv7 = gf_mul(u, v7)
|
|
let mut x = gf_mul(uv3, gf_pow2523(uv7))
|
|
let vx2 = gf_pack(gf_mul(v, gf_sq(x)))
|
|
let u_packed = gf_pack(u)
|
|
if !ct_eq(vx2, u_packed) {
|
|
let neg_u = gf_pack(gf_sub(gf_zero(), u))
|
|
if ct_eq(vx2, neg_u) {
|
|
x = gf_mul(x, ed_sqrt_m1())
|
|
} else {
|
|
return None
|
|
}
|
|
}
|
|
let x_packed = gf_pack(x)
|
|
if is_zero_bytes(x_packed) {
|
|
if sign == 1 {
|
|
return None
|
|
}
|
|
} else if (x_packed[0].to_int() & 1) != sign {
|
|
x = gf_sub(gf_zero(), x)
|
|
}
|
|
Some({ x, y, z: gf_one(), t: gf_mul(x, y), })
|
|
}
|
|
|
|
///|
|
|
/// Group order L = 2^252 + 27742317777372353535851937790883648493 (little-endian bytes).
|
|
let ed_l : FixedArray[Int64] = [
|
|
0xedL, 0xd3L, 0xf5L, 0x5cL, 0x1aL, 0x63L, 0x12L, 0x58L, 0xd6L, 0x9cL, 0xf7L, 0xa2L,
|
|
0xdeL, 0xf9L, 0xdeL, 0x14L, 0x00L, 0x00L, 0x00L, 0x00L, 0x00L, 0x00L, 0x00L, 0x00L,
|
|
0x00L, 0x00L, 0x00L, 0x00L, 0x00L, 0x00L, 0x00L, 0x10L,
|
|
]
|
|
|
|
///|
|
|
/// Reduces a 64-element little-endian byte-limb array modulo L into a 32-byte scalar.
|
|
fn mod_l(x : FixedArray[Int64]) -> Bytes {
|
|
for i = 63; i >= 32; i = i - 1 {
|
|
let mut carry = 0L
|
|
let mut j = i - 32
|
|
let k = i - 12
|
|
while j < k {
|
|
x[j] = x[j] + carry - 16L * x[i] * ed_l[j - (i - 32)]
|
|
carry = (x[j] + 128L) >> 8
|
|
x[j] = x[j] - (carry << 8)
|
|
j += 1
|
|
}
|
|
x[j] = x[j] + carry
|
|
x[i] = 0L
|
|
}
|
|
let mut carry = 0L
|
|
for j = 0; j < 32; j = j + 1 {
|
|
x[j] = x[j] + carry - (x[31] >> 4) * ed_l[j]
|
|
carry = x[j] >> 8
|
|
x[j] = x[j] & 255L
|
|
}
|
|
for j = 0; j < 32; j = j + 1 {
|
|
x[j] = x[j] - carry * ed_l[j]
|
|
}
|
|
let r = FixedArray::make(32, b'\x00')
|
|
for i = 0; i < 32; i = i + 1 {
|
|
if i < 31 {
|
|
x[i + 1] = x[i + 1] + (x[i] >> 8)
|
|
}
|
|
r[i] = (x[i] & 255L).to_byte()
|
|
}
|
|
Bytes::from_array(r)
|
|
}
|
|
|
|
///|
|
|
fn reduce_64_mod_l(h : Bytes) -> Bytes {
|
|
let x = FixedArray::make(64, 0L)
|
|
for i = 0; i < 64; i = i + 1 {
|
|
x[i] = h[i].to_int64()
|
|
}
|
|
mod_l(x)
|
|
}
|
|
|
|
///|
|
|
/// Checks that a 32-byte little-endian scalar `s` is strictly less than L.
|
|
fn scalar_is_canonical(s : Bytes) -> Bool {
|
|
for i = 31; i >= 0; i = i - 1 {
|
|
let sb = s[i].to_int64()
|
|
let lb = ed_l[i]
|
|
if sb < lb {
|
|
return true
|
|
}
|
|
if sb > lb {
|
|
return false
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
///|
|
|
fn clamp_ed25519_scalar(h : Bytes) -> Bytes {
|
|
let a = FixedArray::make(32, b'\x00')
|
|
for i = 0; i < 32; i = i + 1 {
|
|
a[i] = h[i]
|
|
}
|
|
a[0] = (a[0].to_int() & 248).to_byte()
|
|
a[31] = ((a[31].to_int() & 127) | 64).to_byte()
|
|
Bytes::from_array(a)
|
|
}
|
|
|
|
///|
|
|
/// Derives the 32-byte Ed25519 public key from a 32-byte private seed (RFC 8032 Section 5.1.5).
|
|
pub fn ed25519_public_key(seed : Bytes) -> Bytes raise CryptoError {
|
|
if seed.length() != 32 {
|
|
raise InvalidKeyLength(
|
|
"Ed25519 seed must be 32 bytes, got \{seed.length()}",
|
|
)
|
|
}
|
|
let h = sha512(seed)
|
|
let a = clamp_ed25519_scalar(h)
|
|
ed_pack(ed_scalarmult(ed_basepoint(), a))
|
|
}
|
|
|
|
///|
|
|
/// Signs `message` with a 32-byte Ed25519 private `seed`, returning a 64-byte signature
|
|
/// (RFC 8032 Section 5.1.6).
|
|
pub fn ed25519_sign(seed : Bytes, message : Bytes) -> Bytes raise CryptoError {
|
|
if seed.length() != 32 {
|
|
raise InvalidKeyLength(
|
|
"Ed25519 seed must be 32 bytes, got \{seed.length()}",
|
|
)
|
|
}
|
|
let h = sha512(seed)
|
|
let a = clamp_ed25519_scalar(h)
|
|
let prefix = FixedArray::make(32, b'\x00')
|
|
for i = 0; i < 32; i = i + 1 {
|
|
prefix[i] = h[32 + i]
|
|
}
|
|
let pub_key = ed_pack(ed_scalarmult(ed_basepoint(), a))
|
|
|
|
// r = SHA-512(prefix || message) mod L
|
|
let r_hasher = Sha512::new()
|
|
r_hasher.update(Bytes::from_array(prefix))
|
|
r_hasher.update(message)
|
|
let r = reduce_64_mod_l(r_hasher.finalize())
|
|
|
|
// R = [r]B
|
|
let r_enc = ed_pack(ed_scalarmult(ed_basepoint(), r))
|
|
|
|
// k = SHA-512(R || A || message) mod L
|
|
let k_hasher = Sha512::new()
|
|
k_hasher.update(r_enc)
|
|
k_hasher.update(pub_key)
|
|
k_hasher.update(message)
|
|
let k = reduce_64_mod_l(k_hasher.finalize())
|
|
|
|
// S = (r + k * a) mod L
|
|
let x = FixedArray::make(64, 0L)
|
|
for i = 0; i < 32; i = i + 1 {
|
|
x[i] = r[i].to_int64()
|
|
}
|
|
for i = 0; i < 32; i = i + 1 {
|
|
let ki = k[i].to_int64()
|
|
for j = 0; j < 32; j = j + 1 {
|
|
x[i + j] = x[i + j] + ki * a[j].to_int64()
|
|
}
|
|
}
|
|
let s_enc = mod_l(x)
|
|
|
|
let sig = FixedArray::make(64, b'\x00')
|
|
for i = 0; i < 32; i = i + 1 {
|
|
sig[i] = r_enc[i]
|
|
sig[32 + i] = s_enc[i]
|
|
}
|
|
Bytes::from_array(sig)
|
|
}
|
|
|
|
///|
|
|
/// Verifies a 64-byte Ed25519 signature over `message` against a 32-byte `public_key`
|
|
/// (RFC 8032 Section 5.1.7). Returns `false` on any malformed key, signature, or mismatch.
|
|
pub fn ed25519_verify(
|
|
public_key : Bytes,
|
|
message : Bytes,
|
|
signature : Bytes,
|
|
) -> Bool {
|
|
if public_key.length() != 32 || signature.length() != 64 {
|
|
return false
|
|
}
|
|
let a_point = match ed_unpack(public_key) {
|
|
Some(pt) => pt
|
|
None => return false
|
|
}
|
|
let r_bytes = FixedArray::make(32, b'\x00')
|
|
let s_bytes = FixedArray::make(32, b'\x00')
|
|
for i = 0; i < 32; i = i + 1 {
|
|
r_bytes[i] = signature[i]
|
|
s_bytes[i] = signature[32 + i]
|
|
}
|
|
let r_enc = Bytes::from_array(r_bytes)
|
|
let s_enc = Bytes::from_array(s_bytes)
|
|
if !scalar_is_canonical(s_enc) {
|
|
return false
|
|
}
|
|
let r_point = match ed_unpack(r_enc) {
|
|
Some(pt) => pt
|
|
None => return false
|
|
}
|
|
// k = SHA-512(R || A || message) mod L
|
|
let k_hasher = Sha512::new()
|
|
k_hasher.update(r_enc)
|
|
k_hasher.update(public_key)
|
|
k_hasher.update(message)
|
|
let k = reduce_64_mod_l(k_hasher.finalize())
|
|
|
|
// Check [S]B == R + [k]A
|
|
let lhs = ed_pack(ed_scalarmult(ed_basepoint(), s_enc))
|
|
let rhs = ed_pack(ed_add(r_point, ed_scalarmult(a_point, k)))
|
|
ct_eq(lhs, rhs)
|
|
}
|