Skip to content

Commit

Permalink
Merge pull request #390 from DaniPopes/num-integer
Browse files Browse the repository at this point in the history
feat: implement support for num-integer
  • Loading branch information
prestwich authored Jul 28, 2024
2 parents c4616bd + 58f90a0 commit ff2d1a6
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ ark-ff-04 = { version = "0.4.0", package = "ark-ff", optional = true, default-fe
bn-rs = { version = "0.2", optional = true, default-features = true }
fastrlp = { version = "0.3", optional = true, default-features = false, features = ["alloc"] }
num-bigint = { version = "0.4", optional = true, default-features = false }
num-integer = { version = "0.1", optional = true, default-features = false }
num-traits = { version = "0.2.16", optional = true, default-features = false }
parity-scale-codec = { version = "3", optional = true, features = [
"derive",
Expand Down Expand Up @@ -100,6 +101,7 @@ std = [
"bytes?/std",
"fastrlp?/std",
"num-bigint?/std",
"num-integer?/std",
"num-traits?/std",
"parity-scale-codec?/std",
"primitive-types?/std",
Expand All @@ -125,6 +127,7 @@ ark-ff-04 = ["dep:ark-ff-04"]
bn-rs = ["dep:bn-rs", "std"]
fastrlp = ["dep:fastrlp", "alloc"]
num-bigint = ["dep:num-bigint", "alloc"]
num-integer = ["dep:num-integer", "num-traits", "alloc"]
num-traits = ["dep:num-traits", "alloc"]
parity-scale-codec = ["dep:parity-scale-codec", "alloc"]
primitive-types = ["dep:primitive-types"]
Expand Down
1 change: 1 addition & 0 deletions src/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod bn_rs;
mod bytemuck;
mod fastrlp;
mod num_bigint;
mod num_integer;
mod num_traits;
pub mod postgres;
mod primitive_types;
Expand Down
103 changes: 103 additions & 0 deletions src/support/num_integer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//! Support for the [`num-integer`](https://crates.io/crates/num-integer) crate.

#![cfg(feature = "num-integer")]

use crate::Uint;
use num_integer::{ExtendedGcd, Integer};

impl<const BITS: usize, const LIMBS: usize> Integer for Uint<BITS, LIMBS> {
#[inline]
#[must_use]
#[track_caller]
fn div_floor(&self, other: &Self) -> Self {
Self::wrapping_div(*self, *other)
}

#[inline]
#[must_use]
#[track_caller]
fn mod_floor(&self, other: &Self) -> Self {
Self::wrapping_rem(*self, *other)
}

#[inline]
#[must_use]
fn gcd(&self, other: &Self) -> Self {
<Self>::gcd(*self, *other)
}

#[inline]
#[must_use]
#[track_caller]
fn lcm(&self, other: &Self) -> Self {
<Self>::lcm(*self, *other).unwrap()
}

#[inline]
fn is_multiple_of(&self, other: &Self) -> bool {
if other.is_zero() {
return self.is_zero();
}
*self % *other == Self::ZERO
}

#[inline]
fn is_even(&self) -> bool {
!self.bit(0)
}

#[inline]
fn is_odd(&self) -> bool {
self.bit(0)
}

#[inline]
#[track_caller]
fn div_rem(&self, other: &Self) -> (Self, Self) {
<Self>::div_rem(*self, *other)
}

#[inline]
#[track_caller]
fn div_ceil(&self, other: &Self) -> Self {
<Self>::div_ceil(*self, *other)
}

#[inline]
#[track_caller]
fn div_mod_floor(&self, other: &Self) -> (Self, Self) {
// Same as `div_rem` for unsigned integers.
<Self>::div_rem(*self, *other)
}

#[inline]
fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self> {
let (gcd, x, y, _sign) = <Self>::gcd_extended(*self, *other);
ExtendedGcd { gcd, x, y }
}

#[inline]
fn dec(&mut self) {
*self -= Self::from(1);
}

#[inline]
fn inc(&mut self) {
*self += Self::from(1);
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_even() {
let mut a = Uint::<64, 1>::from(0u32);
for _ in 0..10 {
a.inc();
assert_eq!(a.is_even(), a.to::<u64>() % 2 == 0);
assert_eq!(a.is_odd(), a.to::<u64>() % 2 != 0);
}
}
}

0 comments on commit ff2d1a6

Please sign in to comment.