From dd278668a2b0c3f41668948504955e50089755c3 Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Wed, 28 Jan 2026 00:49:22 +0100 Subject: [PATCH] some more improvements (stylistic + traits) --- src/bn254/bigint.rs | 65 +++++++++++------------------------------ src/bn254/field.rs | 18 ++++++++---- src/bn254/montgomery.rs | 24 ++++++++++----- 3 files changed, 45 insertions(+), 62 deletions(-) diff --git a/src/bn254/bigint.rs b/src/bn254/bigint.rs index 4949e98..eb17182 100644 --- a/src/bn254/bigint.rs +++ b/src/bn254/bigint.rs @@ -22,11 +22,6 @@ use crate::bn254::constant::{PRIME_ARRAY}; #[derive(Copy, Clone, PartialEq, Eq)] pub struct BigInt([u32; N]); -#[inline(always)] -pub fn mkBigInt(ls: [u32; N]) -> BigInt { - BigInt(ls) -} - pub type BigInt256 = BigInt<8>; pub type BigInt512 = BigInt<16>; @@ -35,10 +30,10 @@ pub type BigInt512 = BigInt<16>; impl fmt::Display for BigInt { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let _ = f.write_str("0x"); - for i in 0..N { - let _ = f.write_fmt(format_args!("{:08x}",self.0[N-1-i])); - } + write!(f, "0x")?; + for i in (0..N).rev() { + write!(f, "{:08x}", self.0[i])?; + } Ok(()) } } @@ -82,6 +77,18 @@ impl Into<[u32; N]> for BigInt { } //------------------------------------------------------------------------------ +// small values + +impl Default for BigInt { + fn default() -> Self { Self([0; N]) } +} + +impl From for BigInt { + fn from(x: u32) -> Self { Self::from_u32(x) } +} + +//------------------------------------------------------------------------------ +// internal implementations impl BigInt { @@ -192,29 +199,9 @@ impl BigInt { // comparison pub fn is_zero(big: &BigInt) -> bool { - let mut ok : bool = true; - for i in 0..N { - if big.0[i] != 0 { - ok = false; - break; - } - } - ok + big.0.iter().all(|&x| x == 0) } -/* - pub fn is_equal(big1: &BigInt, big2: &BigInt) -> bool { - let mut ok : bool = true; - for i in 0..N { - if big1.0[i] != big2.0[i] { - ok = false; - break; - } - } - ok - } -*/ - pub fn cmp(big1: &BigInt, big2: &BigInt) -> Ordering { let mut res : Ordering = Ordering::Equal; for i in (0..N).rev() { @@ -230,24 +217,6 @@ impl BigInt { res } -/* - pub fn is_lt(big1: &BigInt, big2: &BigInt) -> bool { - BigInt::cmp(&big1, &big2) == Ordering::Less - } - - pub fn is_gt(big1: &BigInt, big2: &BigInt) -> bool { - BigInt::cmp(&big1, &big2) == Ordering::Greater - } - - pub fn is_le(big1: &BigInt, big2: &BigInt) -> bool { - !BigInt::is_gt(&big1, &big2) - } - - pub fn is_ge(big1: &BigInt, big2: &BigInt) -> bool { - !BigInt::is_lt(&big1, &big2) - } -*/ - //------------------------------------ // addition and subtraction diff --git a/src/bn254/field.rs b/src/bn254/field.rs index a34ba4f..a5116ea 100644 --- a/src/bn254/field.rs +++ b/src/bn254/field.rs @@ -80,6 +80,18 @@ impl Into> for Felt { } //------------------------------------------------------------------------------ +// small values + +impl Default for Felt { + fn default() -> Self { Felt(BigInt::zero()) } +} + +impl From for Felt { + fn from(x: u32) -> Self { Self::from_u32(x) } +} + +//------------------------------------------------------------------------------ +// internal implementations impl Felt { @@ -148,12 +160,6 @@ impl Felt { Felt(BigInt::from_u32(x)) } -/* - pub fn is_equal(fld1: &Felt, fld2: &Felt) -> bool { - BigInt::is_equal(&fld1.0, &fld2.0) - } -*/ - pub fn neg(fld: &Felt) -> Felt { if BigInt::is_zero(&fld.0) { Felt::zero() diff --git a/src/bn254/montgomery.rs b/src/bn254/montgomery.rs index aba1224..ecacd32 100644 --- a/src/bn254/montgomery.rs +++ b/src/bn254/montgomery.rs @@ -71,6 +71,18 @@ impl Mul for Mont { } //------------------------------------------------------------------------------ +// small values + +impl Default for Mont { + fn default() -> Self { Mont(BigInt::zero()) } +} + +impl From for Mont { + fn from(x: u32) -> Self { Self::convert_from_u32(x) } +} + +//------------------------------------------------------------------------------ +// internal implementations impl Mont { @@ -124,12 +136,6 @@ impl Mont { Mont(BigInt::zero()) } -/* - pub fn is_equal(mont1: &Mont, mont2: &Mont) -> bool { - BigInt::is_equal(&mont1.0, &mont2.0) - } -*/ - pub fn neg(mont: &Mont) -> Mont { if BigInt::is_zero(&mont.0) { Mont::zero() @@ -166,6 +172,7 @@ impl Mont { //------------------------------------ // reduction and multiplication +/* // the Montgomery reduction algorithm // fn redc_safe(input: BigInt<16>) -> Big { @@ -204,10 +211,11 @@ impl Mont { BigInt::truncate1(&B) } } +*/ // we can abuse the fact that we know the prime number `p`, // for which `p < 2^254` so we won't overflow in the 17th word - + #[unroll_for_loops] fn redc(input: BigInt<16>) -> Big { @@ -253,7 +261,7 @@ impl Mont { } //------------------------------------ - // conversions + // conversions to/from standard bigint representation // this does conversion from the standard representation // we assume the input is in the range `[0..p-1]`