crypto.edwards25519: optimize rejectLowOrder

Reject low-order points by checking projective coordinates directly
instead of using affine coordinates.

Equivalent, but saves CPU cycles (~254 field multiplications total
before, 3 field multiplications after).
This commit is contained in:
Frank Denis
2026-01-01 23:48:26 +01:00
parent 2bd02883c7
commit 1baa127c65
+4 -6
View File
@@ -127,12 +127,10 @@ pub const Edwards25519 = struct {
/// Check that the point does not generate a low-order group.
/// Return a `WeakPublicKey` error if it does.
pub fn rejectLowOrder(p: Edwards25519) WeakPublicKeyError!void {
const zi = p.z.invert();
const x = p.x.mul(zi);
const y = p.y.mul(zi);
const x_neg = x.neg();
const iy = Fe.sqrtm1.mul(y);
if (x.isZero() or y.isZero() or iy.equivalent(x) or iy.equivalent(x_neg)) {
const y_sqrtm1 = Fe.sqrtm1.mul(p.y);
if (p.x.isZero() or p.y.isZero() or p.z.isZero() or
y_sqrtm1.sub(p.x).isZero() or y_sqrtm1.add(p.x).isZero())
{
return error.WeakPublicKey;
}
}