From 4c93bfe390719b8f42295bb9130213c8ad730809 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 7 Sep 2026 21:02:55 +0200 Subject: [PATCH] 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. --- lib/std/crypto/ff.zig | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/std/crypto/ff.zig b/lib/std/crypto/ff.zig index 4e599b67c48fff0e8f7241e821398ce36b2972e8..ea97d69f27aa4bb2ea27a8d35b3079a554028083 100644 --- a/lib/std/crypto/ff.zig +++ b/lib/std/crypto/ff.zig @@ -207,8 +207,8 @@ pub fn Uint(comptime max_bits: comptime_int) type { pub fn compare(x: Self, y: Self) math.Order { return crypto.timing_safe.compare( Limb, - x.limbsConst(), - y.limbsConst(), + &x.limbs_buffer, + &y.limbs_buffer, .little, ); } @@ -223,6 +223,11 @@ pub fn Uint(comptime max_bits: comptime_int) type { return @as(u1, @truncate(x.limbsConst()[0])) != 0; } + pub fn isOne(x: Self) bool { + const x_limbs = x.limbsConst(); + return ct.eql((x_limbs[0] ^ 1) | orLimbs(x_limbs[1..]), 0); + } + /// Adds `y` to `x`, and returns `true` if the operation overflowed. pub fn addWithOverflow(x: *Self, y: Self) u1 { return x.conditionalAddWithOverflow(true, y); -- 2.54.0