some more improvements (stylistic + traits)

This commit is contained in:
Balazs Komuves 2026-01-28 00:49:22 +01:00
parent 6234888649
commit dd278668a2
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
3 changed files with 45 additions and 62 deletions

View File

@ -22,11 +22,6 @@ use crate::bn254::constant::{PRIME_ARRAY};
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct BigInt<const N: usize>([u32; N]);
#[inline(always)]
pub fn mkBigInt<const N: usize>(ls: [u32; N]) -> BigInt<N> {
BigInt(ls)
}
pub type BigInt256 = BigInt<8>;
pub type BigInt512 = BigInt<16>;
@ -35,10 +30,10 @@ pub type BigInt512 = BigInt<16>;
impl<const N: usize> fmt::Display for BigInt<N> {
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<const N: usize> Into<[u32; N]> for BigInt<N> {
}
//------------------------------------------------------------------------------
// small values
impl<const N: usize> Default for BigInt<N> {
fn default() -> Self { Self([0; N]) }
}
impl<const N: usize> From<u32> for BigInt<N> {
fn from(x: u32) -> Self { Self::from_u32(x) }
}
//------------------------------------------------------------------------------
// internal implementations
impl<const N: usize> BigInt<N> {
@ -192,29 +199,9 @@ impl<const N: usize> BigInt<N> {
// comparison
pub fn is_zero(big: &BigInt<N>) -> 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<N>, big2: &BigInt<N>) -> 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<N>, big2: &BigInt<N>) -> Ordering {
let mut res : Ordering = Ordering::Equal;
for i in (0..N).rev() {
@ -230,24 +217,6 @@ impl<const N: usize> BigInt<N> {
res
}
/*
pub fn is_lt(big1: &BigInt<N>, big2: &BigInt<N>) -> bool {
BigInt::cmp(&big1, &big2) == Ordering::Less
}
pub fn is_gt(big1: &BigInt<N>, big2: &BigInt<N>) -> bool {
BigInt::cmp(&big1, &big2) == Ordering::Greater
}
pub fn is_le(big1: &BigInt<N>, big2: &BigInt<N>) -> bool {
!BigInt::is_gt(&big1, &big2)
}
pub fn is_ge(big1: &BigInt<N>, big2: &BigInt<N>) -> bool {
!BigInt::is_lt(&big1, &big2)
}
*/
//------------------------------------
// addition and subtraction

View File

@ -80,6 +80,18 @@ impl Into<BigInt<8>> for Felt {
}
//------------------------------------------------------------------------------
// small values
impl Default for Felt {
fn default() -> Self { Felt(BigInt::zero()) }
}
impl From<u32> 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()

View File

@ -71,6 +71,18 @@ impl Mul for Mont {
}
//------------------------------------------------------------------------------
// small values
impl Default for Mont {
fn default() -> Self { Mont(BigInt::zero()) }
}
impl From<u32> 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
// <https://en.wikipedia.org/wiki/Montgomery_modular_multiplication#Montgomery_arithmetic_on_multiprecision_integers>
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]`