|
| 1 | +///| |
| 2 | +/// Edwards25519 curve parameters as big integers (RFC 8032). `p = 2^255 - 19` is |
| 3 | +/// the field prime, `d` the curve coefficient, `l` the group order, `sqrt_m1` a |
| 4 | +/// square root of -1 (for point decompression, since `p ≡ 5 (mod 8)`), and |
| 5 | +/// `(bx, by)` the base point. Core's `BigInt` supplies the arithmetic; the values |
| 6 | +/// were computed from the definition, not transcribed. |
| 7 | +let ed_p : BigInt = BigInt::from_string( |
| 8 | + "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED", |
| 9 | + radix=16, |
| 10 | +) |
| 11 | + |
| 12 | +///| |
| 13 | +let ed_d : BigInt = BigInt::from_string( |
| 14 | + "52036CEE2B6FFE738CC740797779E89800700A4D4141D8AB75EB4DCA135978A3", |
| 15 | + radix=16, |
| 16 | +) |
| 17 | + |
| 18 | +///| |
| 19 | +let ed_l : BigInt = BigInt::from_string( |
| 20 | + "1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED", |
| 21 | + radix=16, |
| 22 | +) |
| 23 | + |
| 24 | +///| |
| 25 | +let ed_sqrt_m1 : BigInt = BigInt::from_string( |
| 26 | + "2B8324804FC1DF0B2B4D00993DFBD7A72F431806AD2FE478C4EE1B274A0EA0B0", |
| 27 | + radix=16, |
| 28 | +) |
| 29 | + |
| 30 | +///| |
| 31 | +let ed_bx : BigInt = BigInt::from_string( |
| 32 | + "216936D3CD6E53FEC0A4E231FDD6DC5C692CC7609525A7B2C9562D608F25D51A", |
| 33 | + radix=16, |
| 34 | +) |
| 35 | + |
| 36 | +///| |
| 37 | +let ed_by : BigInt = BigInt::from_string( |
| 38 | + "6666666666666666666666666666666666666666666666666666666666666658", |
| 39 | + radix=16, |
| 40 | +) |
| 41 | + |
| 42 | +///| |
| 43 | +/// 2^255, for masking off a compressed point's sign bit. |
| 44 | +let ed_2_255 : BigInt = BigInt::from_string( |
| 45 | + "8000000000000000000000000000000000000000000000000000000000000000", |
| 46 | + radix=16, |
| 47 | +) |
| 48 | + |
| 49 | +///| |
| 50 | +/// A point on Edwards25519 in affine coordinates. The addition law is complete |
| 51 | +/// (identity `(0, 1)`, no special cases), so no infinity flag is needed. |
| 52 | +priv struct EdPoint { |
| 53 | + x : BigInt |
| 54 | + y : BigInt |
| 55 | +} |
| 56 | + |
| 57 | +///| |
| 58 | +/// Reduce mod the field prime, normalised to `[0, p)`. |
| 59 | +fn edmod(a : BigInt) -> BigInt { |
| 60 | + let m = a % ed_p |
| 61 | + if m < (0 : BigInt) { |
| 62 | + m + ed_p |
| 63 | + } else { |
| 64 | + m |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +///| |
| 69 | +/// Reduce mod the group order, normalised to `[0, l)`. |
| 70 | +fn edmod_l(a : BigInt) -> BigInt { |
| 71 | + let m = a % ed_l |
| 72 | + if m < (0 : BigInt) { |
| 73 | + m + ed_l |
| 74 | + } else { |
| 75 | + m |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +///| |
| 80 | +/// Field inverse (Fermat: a^(p-2) mod p). |
| 81 | +fn edinv(a : BigInt) -> BigInt { |
| 82 | + edmod(a).pow(ed_p - 2, modulus=ed_p) |
| 83 | +} |
| 84 | + |
| 85 | +///| |
| 86 | +/// Recover the `x` coordinate from `y` on Edwards25519: `x² = (y²-1)/(d·y²+1)`, |
| 87 | +/// with the `p ≡ 5 (mod 8)` square root and the even root chosen. |
| 88 | +fn ed_xrecover(y : BigInt) -> BigInt { |
| 89 | + let yy = edmod(y * y) |
| 90 | + let xx = edmod((yy - 1) * edinv(edmod(ed_d * yy + 1))) |
| 91 | + let mut x = xx.pow((ed_p + 3) / 8, modulus=ed_p) |
| 92 | + if edmod(x * x - xx) != (0 : BigInt) { |
| 93 | + x = edmod(x * ed_sqrt_m1) |
| 94 | + } |
| 95 | + if x % 2 != (0 : BigInt) { |
| 96 | + x = ed_p - x |
| 97 | + } |
| 98 | + x |
| 99 | +} |
| 100 | + |
| 101 | +///| |
| 102 | +/// Edwards25519 point addition (complete twisted-Edwards law, a = -1). |
| 103 | +fn ed_add(pp : EdPoint, qq : EdPoint) -> EdPoint { |
| 104 | + let x1 = pp.x |
| 105 | + let y1 = pp.y |
| 106 | + let x2 = qq.x |
| 107 | + let y2 = qq.y |
| 108 | + let dxy = edmod(ed_d * x1 * x2 * y1 * y2) |
| 109 | + let x3 = edmod((x1 * y2 + x2 * y1) * edinv(edmod(1 + dxy))) |
| 110 | + let y3 = edmod((y1 * y2 + x1 * x2) * edinv(edmod(1 - dxy))) |
| 111 | + { x: x3, y: y3 } |
| 112 | +} |
| 113 | + |
| 114 | +///| |
| 115 | +/// Scalar multiplication `e · pt` by double-and-add. |
| 116 | +fn ed_mul(e : BigInt, pt : EdPoint) -> EdPoint { |
| 117 | + let mut result : EdPoint = { x: 0, y: 1 } |
| 118 | + let mut addend = pt |
| 119 | + let mut k = e |
| 120 | + while k > (0 : BigInt) { |
| 121 | + if k % 2 == (1 : BigInt) { |
| 122 | + result = ed_add(result, addend) |
| 123 | + } |
| 124 | + addend = ed_add(addend, addend) |
| 125 | + k = k / 2 |
| 126 | + } |
| 127 | + result |
| 128 | +} |
| 129 | + |
| 130 | +///| |
| 131 | +/// Interpret `b` as a little-endian unsigned integer (Ed25519's byte order). |
| 132 | +fn ed_le_int(b : BytesView) -> BigInt { |
| 133 | + let buf = Buffer() |
| 134 | + for i = b.length() - 1; i >= 0; i = i - 1 { |
| 135 | + buf.write_byte(b[i]) |
| 136 | + } |
| 137 | + BigInt::from_octets(buf.to_bytes()[:]) |
| 138 | +} |
| 139 | + |
| 140 | +///| |
| 141 | +/// Decompress a 32-byte little-endian Edwards25519 point: `y` is the low 255 |
| 142 | +/// bits, and the top bit selects the sign (parity) of `x`. |
| 143 | +fn ed_decode_point(s : BytesView) -> EdPoint { |
| 144 | + let y = ed_le_int(s) % ed_2_255 |
| 145 | + let mut x = ed_xrecover(y) |
| 146 | + let sign = (s[31].to_int() >> 7) & 1 |
| 147 | + let parity = if x % 2 == (1 : BigInt) { 1 } else { 0 } |
| 148 | + if parity != sign { |
| 149 | + x = ed_p - x |
| 150 | + } |
| 151 | + { x, y } |
| 152 | +} |
| 153 | + |
| 154 | +///| |
| 155 | +/// Ed25519 signature verification (RFC 8032 §5.1.7). `sig` is the 64-byte |
| 156 | +/// `R || S`, `pub_key` the 32-byte compressed public point. Checks `[S]B = R + |
| 157 | +/// [k]A` with `k = SHA-512(R || A || M) mod l`. |
| 158 | +pub fn ed25519_verify(pub_key : Bytes, msg : Bytes, sig : Bytes) -> Bool { |
| 159 | + if pub_key.length() != 32 || sig.length() != 64 { |
| 160 | + return false |
| 161 | + } |
| 162 | + let a_pt = ed_decode_point(pub_key[:]) |
| 163 | + let r_pt = ed_decode_point(sig[0:32]) |
| 164 | + let s = ed_le_int(sig[32:64]) |
| 165 | + if s >= ed_l { |
| 166 | + return false |
| 167 | + } |
| 168 | + let hbuf = Buffer() |
| 169 | + hbuf.write_bytes(sig[0:32]) |
| 170 | + hbuf.write_bytes(pub_key[:]) |
| 171 | + hbuf.write_bytes(msg[:]) |
| 172 | + let k = edmod_l(ed_le_int(sha512(hbuf.to_bytes())[:])) |
| 173 | + let lhs = ed_mul(s, { x: ed_bx, y: ed_by }) |
| 174 | + let rhs = ed_add(r_pt, ed_mul(k, a_pt)) |
| 175 | + lhs.x == rhs.x && lhs.y == rhs.y |
| 176 | +} |
| 177 | + |
| 178 | +///| |
| 179 | +/// An Ed25519 (EdDSA) public key: the 32-byte compressed point. The verification |
| 180 | +/// key for the JWT `EdDSA` algorithm (RFC 8037). |
| 181 | +pub(all) struct Ed25519PublicKey { |
| 182 | + key : Bytes |
| 183 | +} |
| 184 | + |
| 185 | +///| |
| 186 | +/// Build an Ed25519 public key from its 32-byte hex encoding. |
| 187 | +pub fn Ed25519PublicKey::from_hex(hex : String) -> Ed25519PublicKey { |
| 188 | + let buf = Buffer() |
| 189 | + for i = 0; i < hex.length(); i = i + 2 { |
| 190 | + let hi = hex_nibble(hex[i].to_int()) |
| 191 | + let lo = hex_nibble(hex[i + 1].to_int()) |
| 192 | + buf.write_byte(((hi << 4) | lo).to_byte()) |
| 193 | + } |
| 194 | + { key: buf.to_bytes() } |
| 195 | +} |
| 196 | + |
| 197 | +///| |
| 198 | +/// A single hex digit's value, from its character code. |
| 199 | +fn hex_nibble(v : Int) -> Int { |
| 200 | + if v >= 0x30 && v <= 0x39 { |
| 201 | + v - 0x30 |
| 202 | + } else if v >= 0x61 && v <= 0x66 { |
| 203 | + v - 0x61 + 10 |
| 204 | + } else if v >= 0x41 && v <= 0x46 { |
| 205 | + v - 0x41 + 10 |
| 206 | + } else { |
| 207 | + 0 |
| 208 | + } |
| 209 | +} |
0 commit comments