Skip to content

Commit

Permalink
Alter field (#146)
Browse files Browse the repository at this point in the history
* remake `primitive`

* add `ExpPowOf2Reduce` ops

* add `NegOne` and `ConstNegOne` trait

* clippy

* typos

* improve `CarryingMul` and `WideningMul`

* file rename
  • Loading branch information
serendipity-crypto authored Sep 24, 2024
1 parent 3bdd28a commit 4c7cb97
Show file tree
Hide file tree
Showing 50 changed files with 1,441 additions and 589 deletions.
2 changes: 1 addition & 1 deletion algebra/src/baby_bear/babybear_ntt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use num_traits::{pow, Zero};
use rand::{distributions, thread_rng};

use crate::{transformation::prime32::ConcreteTable, Field, NTTField};
use crate::{transformation::prime32::ConcreteTable, Field, NTTField, NegOne};

use super::BabyBear;

Expand Down
22 changes: 11 additions & 11 deletions algebra/src/baby_bear/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
extension::TwoAdicField, field::Field, modulus::to_monty, BabyBear, BinomialExtensionField,
BinomiallyExtendable, HasTwoAdicBionmialExtension,
};
use num_traits::{One, Zero};
use num_traits::{ConstOne, ConstZero};

impl BinomiallyExtendable<4> for BabyBear {
// Verifiable in Sage with
Expand All @@ -17,7 +17,7 @@ impl BinomiallyExtendable<4> for BabyBear {
}

fn ext_generator() -> [Self; 4] {
[Self::new(8), Self::one(), Self::zero(), Self::zero()]
[Self::new(8), Self::ONE, Self::ZERO, Self::ZERO]
}
}

Expand All @@ -29,22 +29,22 @@ impl HasTwoAdicBionmialExtension<4> for BabyBear {

match bits {
29 => [
Self::zero(),
Self::zero(),
Self::zero(),
Self::ZERO,
Self::ZERO,
Self::ZERO,
Self(to_monty(124907976)),
],
28 => [
Self::zero(),
Self::zero(),
Self::ZERO,
Self::ZERO,
Self(to_monty(1996171314)),
Self::zero(),
Self::ZERO,
],
_ => [
Self::two_adic_generator(bits),
Self::zero(),
Self::zero(),
Self::zero(),
Self::ZERO,
Self::ZERO,
Self::ZERO,
],
}
}
Expand Down
88 changes: 50 additions & 38 deletions algebra/src/baby_bear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ use std::{
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};

use num_traits::{Inv, One, Pow, Zero};
use num_traits::{ConstOne, ConstZero, Inv, One, Pow, Zero};

use crate::{
div_ceil,
modulus::{self, from_monty, to_monty, BabyBearModulus, MONTY_NEG_ONE, MONTY_ONE, MONTY_ZERO},
modulus::{
from_monty, to_monty, BabyBearModulus, BABY_BEAR_P, MONTY_NEG_ONE, MONTY_ONE, MONTY_ZERO,
},
reduce::{
AddReduce, AddReduceAssign, DivReduce, DivReduceAssign, InvReduce, MulReduce,
MulReduceAssign, NegReduce, PowReduce, SubReduce, SubReduceAssign,
AddReduce, AddReduceAssign, DivReduce, DivReduceAssign, ExpReduce, InvReduce, MulReduce,
MulReduceAssign, NegReduce, SubReduce, SubReduceAssign,
},
DecomposableField, FheField, Field, Packable, PrimeField, TwoAdicField,
ConstNegOne, DecomposableField, FheField, Field, NegOne, Packable, PrimeField, TwoAdicField,
};

/// Implementation of BabyBear field.
Expand All @@ -30,12 +31,7 @@ impl Field for BabyBear {
type Value = u32;
type Order = u32;

const MODULUS_VALUE: Self::Value = modulus::BABY_BEAR_P;

#[inline]
fn neg_one() -> Self {
Self(MONTY_NEG_ONE)
}
const MODULUS_VALUE: Self::Value = BABY_BEAR_P;

#[inline]
fn new(value: Self::Value) -> Self {
Expand All @@ -49,20 +45,6 @@ impl DecomposableField for BabyBear {
from_monty(self.0)
}

#[inline]
fn mask(bits: u32) -> Self::Value {
u32::MAX >> (u32::BITS - bits)
}

#[inline]
fn decompose_len(basis: Self::Value) -> usize {
debug_assert!(basis.is_power_of_two() && basis > 1);
div_ceil(
32 - Self::MODULUS_VALUE.leading_zeros(),
basis.trailing_zeros(),
) as usize
}

#[inline]
fn decompose(self, basis: crate::Basis<Self>) -> Vec<Self> {
let mut temp = self.value();
Expand Down Expand Up @@ -269,47 +251,77 @@ impl Pow<u32> for BabyBear {
type Output = Self;
#[inline]
fn pow(self, rhs: u32) -> Self::Output {
Self(self.0.pow_reduce(rhs, BabyBearModulus))
Self(self.0.exp_reduce(rhs, BabyBearModulus))
}
}

impl Zero for BabyBear {
#[inline]
fn is_zero(&self) -> bool {
*self == Self(MONTY_ZERO)
fn zero() -> Self {
Self(MONTY_ZERO)
}

#[inline]
fn set_zero(&mut self) {
*self = Self(MONTY_ZERO);
self.0 = MONTY_ZERO;
}

#[inline]
fn zero() -> Self {
Self(MONTY_ZERO)
fn is_zero(&self) -> bool {
*self == Self(MONTY_ZERO)
}
}

impl ConstZero for BabyBear {
const ZERO: Self = Self(MONTY_ZERO);
}

impl One for BabyBear {
#[inline]
fn one() -> Self {
Self(MONTY_ONE)
}

#[inline]
fn set_one(&mut self) {
self.0 = MONTY_ONE;
}

#[inline]
fn is_one(&self) -> bool
where
Self: PartialEq,
{
*self == Self(MONTY_ONE)
}
}

#[inline]
fn set_one(&mut self) {
*self = Self(MONTY_ONE);
impl ConstOne for BabyBear {
const ONE: Self = Self(MONTY_ONE);
}

impl NegOne for BabyBear {
#[inline(always)]
fn neg_one() -> Self {
Self(MONTY_NEG_ONE)
}

#[inline]
fn one() -> Self {
Self(MONTY_ONE)
fn set_neg_one(&mut self) {
self.0 = MONTY_NEG_ONE;
}

fn is_neg_one(&self) -> bool
where
Self: PartialEq,
{
*self == Self(MONTY_NEG_ONE)
}
}

impl ConstNegOne for BabyBear {
const NEG_ONE: Self = Self(MONTY_NEG_ONE);
}

impl PrimeField for BabyBear {
fn is_prime_field() -> bool {
true
Expand Down
19 changes: 15 additions & 4 deletions algebra/src/decompose_basis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! The basis for decomposition of the [`Field`].
use crate::{DecomposableField, Field};
use num_traits::{ConstOne, One};

use crate::{Bits, ConstBounded, DecomposableField, Field};

/// This basis struct is used for decomposition of the [`Field`].
///
Expand All @@ -27,9 +29,18 @@ impl<F: DecomposableField> Default for Basis<F> {
impl<F: DecomposableField> Basis<F> {
/// Creates a new [`Basis<F>`] with the given basis' bits number.
pub fn new(bits: u32) -> Self {
let mask = F::mask(bits);
let basis = mask + F::one().value();
let decompose_len = F::decompose_len(basis);
let mut modulus_bits =
<F::Value as Bits>::BITS - num_traits::PrimInt::leading_zeros(F::MODULUS_VALUE);

assert!(modulus_bits >= bits);

if num_traits::PrimInt::count_ones(F::MODULUS_VALUE).is_one() {
modulus_bits -= 1;
}

let mask = <F::Value as ConstBounded>::MAX >> (<F::Value as Bits>::BITS - bits);
let basis = <F::Value as ConstOne>::ONE << bits;
let decompose_len = modulus_bits.div_ceil(bits) as usize;

Self {
basis,
Expand Down
Loading

0 comments on commit 4c7cb97

Please sign in to comment.