From 1594f0935b7da199aa3add7fa446f8c77a445a5a Mon Sep 17 00:00:00 2001 From: HydroH Date: Mon, 5 May 2025 20:31:30 +0800 Subject: [PATCH] std: fix compile errors in `std.crypto.ecc` Implemented `neg()` method for `AffineCoordinates` struct of p256, p384 and secp256k1 curves. Resolves: #20505 (partially) --- lib/std/crypto/pcurves/p256.zig | 4 ++++ lib/std/crypto/pcurves/p384.zig | 4 ++++ lib/std/crypto/pcurves/secp256k1.zig | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/lib/std/crypto/pcurves/p256.zig b/lib/std/crypto/pcurves/p256.zig index ec176f78c5d0..aa9226ae0d91 100644 --- a/lib/std/crypto/pcurves/p256.zig +++ b/lib/std/crypto/pcurves/p256.zig @@ -471,6 +471,10 @@ pub const AffineCoordinates = struct { /// Identity element in affine coordinates. pub const identityElement = AffineCoordinates{ .x = P256.identityElement.x, .y = P256.identityElement.y }; + pub fn neg(p: AffineCoordinates) AffineCoordinates { + return .{ .x = p.x, .y = p.y.neg() }; + } + fn cMov(p: *AffineCoordinates, a: AffineCoordinates, c: u1) void { p.x.cMov(a.x, c); p.y.cMov(a.y, c); diff --git a/lib/std/crypto/pcurves/p384.zig b/lib/std/crypto/pcurves/p384.zig index 3ab95da5e8f7..61632f61edf4 100644 --- a/lib/std/crypto/pcurves/p384.zig +++ b/lib/std/crypto/pcurves/p384.zig @@ -471,6 +471,10 @@ pub const AffineCoordinates = struct { /// Identity element in affine coordinates. pub const identityElement = AffineCoordinates{ .x = P384.identityElement.x, .y = P384.identityElement.y }; + pub fn neg(p: AffineCoordinates) AffineCoordinates { + return .{ .x = p.x, .y = p.y.neg() }; + } + fn cMov(p: *AffineCoordinates, a: AffineCoordinates, c: u1) void { p.x.cMov(a.x, c); p.y.cMov(a.y, c); diff --git a/lib/std/crypto/pcurves/secp256k1.zig b/lib/std/crypto/pcurves/secp256k1.zig index 945abea931cd..c891f414f5c7 100644 --- a/lib/std/crypto/pcurves/secp256k1.zig +++ b/lib/std/crypto/pcurves/secp256k1.zig @@ -549,6 +549,10 @@ pub const AffineCoordinates = struct { /// Identity element in affine coordinates. pub const identityElement = AffineCoordinates{ .x = Secp256k1.identityElement.x, .y = Secp256k1.identityElement.y }; + pub fn neg(p: AffineCoordinates) AffineCoordinates { + return .{ .x = p.x, .y = p.y.neg() }; + } + fn cMov(p: *AffineCoordinates, a: AffineCoordinates, c: u1) void { p.x.cMov(a.x, c); p.y.cMov(a.y, c);