mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user