authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-09-07 21:02:55+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-09-24 20:06:04+02:00
log4c93bfe390719b8f42295bb9130213c8ad730809
tree7de178905d6e2c64746400e69a05d94f774cd07b
parent392b17125475fd955ae591107ae92458c4bc1c4f

crypto.ff: compare full Uint buffers and add a isOne() function

Compare the full zero-padded buffers so we can support different active widths and still keep the comparison length fixed at comptime. Also add a constant-time isOne() check, which is quite useful for a long of applications using finite field arithemtic.

1 files changed, 7 insertions(+), 2 deletions(-)

lib/std/crypto/ff.zig+7-2
...@@ -207,8 +207,8 @@ pub fn Uint(comptime max_bits: comptime_int) type {...@@ -207,8 +207,8 @@ pub fn Uint(comptime max_bits: comptime_int) type {
207 pub fn compare(x: Self, y: Self) math.Order {207 pub fn compare(x: Self, y: Self) math.Order {
208 return crypto.timing_safe.compare(208 return crypto.timing_safe.compare(
209 Limb,209 Limb,
210 x.limbsConst(),210 &x.limbs_buffer,
211 y.limbsConst(),211 &y.limbs_buffer,
212 .little,212 .little,
213 );213 );
214 }214 }
...@@ -223,6 +223,11 @@ pub fn Uint(comptime max_bits: comptime_int) type {...@@ -223,6 +223,11 @@ pub fn Uint(comptime max_bits: comptime_int) type {
223 return @as(u1, @truncate(x.limbsConst()[0])) != 0;223 return @as(u1, @truncate(x.limbsConst()[0])) != 0;
224 }224 }
225225
226 pub fn isOne(x: Self) bool {
227 const x_limbs = x.limbsConst();
228 return ct.eql((x_limbs[0] ^ 1) | orLimbs(x_limbs[1..]), 0);
229 }
230
226 /// Adds `y` to `x`, and returns `true` if the operation overflowed.231 /// Adds `y` to `x`, and returns `true` if the operation overflowed.
227 pub fn addWithOverflow(x: *Self, y: Self) u1 {232 pub fn addWithOverflow(x: *Self, y: Self) u1 {
228 return x.conditionalAddWithOverflow(true, y);233 return x.conditionalAddWithOverflow(true, y);