import 'dart:typed_data'; import 'package:crypto/crypto.dart' as crypto; /// Ed25519 signature verification (RFC 8032 section 5.1.7), verify only. /// /// The update feed is checked once per update, so plain [BigInt] arithmetic /// is fast enough and keeps this small and auditable; it never handles a /// private key, so constant-time arithmetic is not required. Tested against /// the RFC 8032 vectors in `test/features/updates/ed25519_test.dart`. bool ed25519Verify({ required List publicKey, required List message, required List signature, }) { if (publicKey.length != 32 || signature.length != 64) return false; final a = _decode(publicKey); final r = _decode(signature.sublist(0, 32)); if (a == null || r == null) return false; final s = _littleEndian(signature.sublist(32)); if (s >= _l) return false; final digest = crypto.sha512.convert([ ...signature.sublist(0, 32), ...publicKey, ...message, ]); final k = _littleEndian(digest.bytes) % _l; final left = _multiply(_base, s); final right = _add(r, _multiply(a, k)); return _equal(_encode(left), _encode(right)); } final BigInt _p = (BigInt.one << 255) - BigInt.from(19); final BigInt _l = (BigInt.one << 252) + BigInt.parse('27742317777372353535851937790883648493'); final BigInt _d = (BigInt.from(-121665) * BigInt.from(121666).modInverse(_p)) % _p; final BigInt _sqrtMinusOne = BigInt.two.modPow((_p - BigInt.one) >> 2, _p); final _Point _base = _decode(_baseEncoding)!; // The base point: y = 4/5, x even. final List _baseEncoding = _bytes( (BigInt.from(4) * BigInt.from(5).modInverse(_p)) % _p, ); /// Extended coordinates (X:Y:Z:T) with x = X/Z, y = Y/Z, x*y = T/Z. class _Point { const _Point(this.x, this.y, this.z, this.t); final BigInt x, y, z, t; } final _Point _identity = _Point( BigInt.zero, BigInt.one, BigInt.one, BigInt.zero, ); BigInt _littleEndian(List bytes) { var value = BigInt.zero; for (var i = bytes.length - 1; i >= 0; i--) { value = (value << 8) | BigInt.from(bytes[i]); } return value; } List _bytes(BigInt value) { final out = Uint8List(32); var rest = value; for (var i = 0; i < 32; i++) { out[i] = (rest & BigInt.from(0xff)).toInt(); rest >>= 8; } return out; } _Point? _decode(List encoded) { final bytes = List.of(encoded); final sign = bytes[31] >> 7; bytes[31] &= 0x7f; final y = _littleEndian(bytes); if (y >= _p) return null; final y2 = y * y % _p; final u = (y2 - BigInt.one) % _p; final v = (_d * y2 + BigInt.one) % _p; final v3 = v * v % _p * v % _p; final v7 = v3 * v3 % _p * v % _p; var x = u * v3 % _p * (u * v7 % _p).modPow((_p - BigInt.from(5)) >> 3, _p) % _p; final check = v * x % _p * x % _p; if (check == u) { // x is a square root of u/v. } else if (check == (_p - u) % _p) { x = x * _sqrtMinusOne % _p; } else { return null; } if (x == BigInt.zero && sign == 1) return null; if (x.isOdd != (sign == 1)) x = _p - x; return _Point(x, y, BigInt.one, x * y % _p); } List _encode(_Point point) { final inverse = point.z.modInverse(_p); final x = point.x * inverse % _p; final y = point.y * inverse % _p; final out = _bytes(y); if (x.isOdd) out[31] |= 0x80; return out; } // The unified addition law (RFC 8032 section 5.1.4); it also doubles. _Point _add(_Point p, _Point q) { final a = (p.y - p.x) * (q.y - q.x) % _p; final b = (p.y + p.x) * (q.y + q.x) % _p; final c = p.t * BigInt.two * _d % _p * q.t % _p; final d = p.z * BigInt.two * q.z % _p; final e = (b - a) % _p, f = (d - c) % _p, g = (d + c) % _p, h = (b + a) % _p; return _Point(e * f % _p, g * h % _p, f * g % _p, e * h % _p); } _Point _multiply(_Point point, BigInt scalar) { var result = _identity; var addend = point; var rest = scalar; while (rest > BigInt.zero) { if (rest.isOdd) result = _add(result, addend); addend = _add(addend, addend); rest >>= 1; } return result; } bool _equal(List a, List b) { if (a.length != b.length) return false; for (var i = 0; i < a.length; i++) { if (a[i] != b[i]) return false; } return true; }