authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-09-07 21:02:52+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-09-22 13:32:43+02:00
log199a5a2700eeb8f3eaed36255fd3f0c0756f0935
tree35dfaac220b573d3541088c4a799f527119fe33a
parent53b508142ee9d53fd503541b7e4583b36529106c

crypto.ff: add functions to access the modulus and encoded length

Add encodedLen() to size serialization buffers, and toUint() to get the modulus as an integer.

1 files changed, 8 insertions(+), 0 deletions(-)

lib/std/crypto/ff.zig+8
...@@ -424,6 +424,10 @@ pub fn Modulus(comptime max_bits: comptime_int) type {...@@ -424,6 +424,10 @@ pub fn Modulus(comptime max_bits: comptime_int) type {
424 return self.limbs_count() * t_bits - self.leading;424 return self.limbs_count() * t_bits - self.leading;
425 }425 }
426426
427 pub fn encodedLen(self: Self) usize {
428 return @divCeil(self.bits(), 8);
429 }
430
427 /// Returns the element `1`.431 /// Returns the element `1`.
428 pub fn one(self: Self) Fe {432 pub fn one(self: Self) Fe {
429 var fe = self.zero;433 var fe = self.zero;
...@@ -487,6 +491,10 @@ pub fn Modulus(comptime max_bits: comptime_int) type {...@@ -487,6 +491,10 @@ pub fn Modulus(comptime max_bits: comptime_int) type {
487 return self.v.toBytes(bytes, endian);491 return self.v.toBytes(bytes, endian);
488 }492 }
489493
494 pub fn toUint(self: Self) FeUint {
495 return self.v;
496 }
497
490 /// Rejects field elements that are not in the canonical form.498 /// Rejects field elements that are not in the canonical form.
491 pub fn rejectNonCanonical(self: Self, fe: Fe) error{NonCanonical}!void {499 pub fn rejectNonCanonical(self: Self, fe: Fe) error{NonCanonical}!void {
492 if (fe.limbs_count() != self.limbs_count() or ct.limbsCmpGeq(fe.v, self.v)) {500 if (fe.limbs_count() != self.limbs_count() or ct.limbsCmpGeq(fe.v, self.v)) {