mirror of
https://github.com/pezkuwichain/bizinikiwi-bn.git
synced 2026-07-24 05:45:41 +00:00
Performing reconstruction of the codebase.
This commit is contained in:
+210
-143
@@ -1,167 +1,234 @@
|
||||
use rand::Rng;
|
||||
use num::{BigUint, Num};
|
||||
use std::ops::{Mul,Add,Sub,Neg};
|
||||
use std::cmp::{PartialEq, Eq};
|
||||
use std::convert::From;
|
||||
use std::ops::{Add, Sub, Mul, Neg};
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use super::Field;
|
||||
use super::FieldElement;
|
||||
|
||||
pub trait PrimeFieldParams {
|
||||
fn modulus() -> BigUint;
|
||||
fn bits() -> usize;
|
||||
use arith::U256;
|
||||
|
||||
pub trait FpParams {
|
||||
fn name() -> &'static str;
|
||||
fn modulus() -> U256;
|
||||
fn inv() -> u32;
|
||||
fn rsquared() -> U256;
|
||||
fn rcubed() -> U256;
|
||||
fn one() -> U256;
|
||||
}
|
||||
|
||||
pub struct Fp<P: PrimeFieldParams> {
|
||||
value: BigUint,
|
||||
_marker: PhantomData<P>
|
||||
pub struct Fp<P: FpParams>(U256, PhantomData<P>);
|
||||
impl<P: FpParams> Copy for Fp<P> { }
|
||||
impl<P: FpParams> Clone for Fp<P> {
|
||||
fn clone(&self) -> Self { *self }
|
||||
}
|
||||
|
||||
impl<P: PrimeFieldParams> fmt::Debug for Fp<P> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}({})", P::name(), self.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: PrimeFieldParams> Field for Fp<P> {
|
||||
fn zero() -> Self {
|
||||
use num::Zero;
|
||||
|
||||
Fp {
|
||||
value: BigUint::zero(),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
fn one() -> Self {
|
||||
use num::One;
|
||||
|
||||
Fp {
|
||||
value: BigUint::one(),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
fn random<R: Rng>(rng: &mut R) -> Self {
|
||||
use num::num_bigint::RandBigInt;
|
||||
use num::Zero;
|
||||
|
||||
Fp {
|
||||
value: rng.gen_biguint_range(&BigUint::zero(), &P::modulus()),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Self {
|
||||
if self.is_zero() {
|
||||
// TODO: this should likely bleed through the abstraction layers
|
||||
panic!("cannot get the multiplicative inverse of zero")
|
||||
} else {
|
||||
let mut res = Self::one();
|
||||
|
||||
let mut found_one = false;
|
||||
|
||||
let exp = Self::zero() - Self::one() - Self::one();
|
||||
|
||||
for i in (0..P::bits()).rev() {
|
||||
if found_one {
|
||||
res = res.squared();
|
||||
}
|
||||
|
||||
if exp.test_bit(i) {
|
||||
found_one = true;
|
||||
res = self * &res;
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
fn neg(&self) -> Self {
|
||||
use num::Zero;
|
||||
|
||||
Fp {
|
||||
value: if self.value.is_zero() {
|
||||
self.value.clone()
|
||||
} else {
|
||||
P::modulus() - &self.value
|
||||
},
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
fn mul(&self, other: &Self) -> Self {
|
||||
Fp {
|
||||
value: (&self.value * &other.value) % &P::modulus(),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
fn sub(&self, other: &Self) -> Self {
|
||||
if other.value > self.value {
|
||||
Fp {
|
||||
value: (&self.value + P::modulus()) - &other.value,
|
||||
_marker: PhantomData
|
||||
}
|
||||
} else {
|
||||
Fp {
|
||||
value: &self.value - &other.value,
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add(&self, other: &Self) -> Self {
|
||||
let tmp = &self.value + &other.value;
|
||||
if tmp >= P::modulus() {
|
||||
Fp {
|
||||
value: tmp - P::modulus(),
|
||||
_marker: PhantomData
|
||||
}
|
||||
} else {
|
||||
Fp {
|
||||
value: tmp,
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: FpParams> PartialEq for Fp<P> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.value == other.value
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
impl<P: FpParams> Eq for Fp<P> { }
|
||||
impl<P: FpParams> fmt::Debug for Fp<P> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}({:?})", P::name(), self.0)
|
||||
}
|
||||
}
|
||||
impl<P: FpParams> From<Fp<P>> for U256 {
|
||||
fn from(mut a: Fp<P>) -> Self {
|
||||
a.0.mul(&U256::one(), &P::modulus(), P::inv());
|
||||
|
||||
a.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: PrimeFieldParams> Fp<P> {
|
||||
pub fn test_bit(&self, bit: usize) -> bool {
|
||||
// TODO: This is a naive approach.
|
||||
use num::{One, Zero};
|
||||
#[inline]
|
||||
pub fn const_fp<P: FpParams, I: Into<U256>>(i: I) -> Fp<P> {
|
||||
Fp(i.into(), PhantomData)
|
||||
}
|
||||
|
||||
let mut b = BigUint::one();
|
||||
let two = &b + &b;
|
||||
for _ in 0..bit {
|
||||
b = &b + &b;
|
||||
impl<P: FpParams> Fp<P> {
|
||||
pub fn from_str(s: &str) -> Option<Self> {
|
||||
let ints: Vec<_> = {
|
||||
let mut acc = Self::zero();
|
||||
(0..11).map(|_| {let tmp = acc; acc = acc + Self::one(); tmp}).collect()
|
||||
};
|
||||
|
||||
let mut res = Self::zero();
|
||||
for c in s.chars() {
|
||||
match c.to_digit(10) {
|
||||
Some(d) => {
|
||||
res = res * ints[10];
|
||||
res = res + ints[d as usize];
|
||||
},
|
||||
None => {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(&self.value / b) % two != BigUint::zero()
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, P: PrimeFieldParams> From<&'a str> for Fp<P> {
|
||||
fn from(s: &'a str) -> Self {
|
||||
Fp {
|
||||
value: BigUint::from_str_radix(s, 10).unwrap() % P::modulus(),
|
||||
_marker: PhantomData
|
||||
}
|
||||
impl<P: FpParams> Fp<P> {
|
||||
/// Assumes input is mod p, not exposed publicly
|
||||
fn new_checked(mut a: U256) -> Self {
|
||||
a.mul(&P::rsquared(), &P::modulus(), P::inv());
|
||||
|
||||
Fp(a, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: PrimeFieldParams> Clone for Fp<P> {
|
||||
fn clone(&self) -> Self {
|
||||
Fp {
|
||||
value: self.value.clone(),
|
||||
_marker: PhantomData
|
||||
}
|
||||
impl<P: FpParams> FieldElement for Fp<P> {
|
||||
fn zero() -> Self {
|
||||
const_fp(U256::zero())
|
||||
}
|
||||
|
||||
fn one() -> Self {
|
||||
const_fp(P::one())
|
||||
}
|
||||
|
||||
fn random<R: Rng>(rng: &mut R) -> Self {
|
||||
Fp::new_checked(U256::rand(rng, &P::modulus()))
|
||||
}
|
||||
|
||||
fn is_zero(&self) -> bool {
|
||||
self.0.is_zero()
|
||||
}
|
||||
|
||||
fn inverse(mut self) -> Self {
|
||||
assert!(!self.is_zero());
|
||||
|
||||
self.0.invert(&P::modulus());
|
||||
self.0.mul(&P::rcubed(), &P::modulus(), P::inv());
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
forward_ops_to_field_ops!(impl(P: PrimeFieldParams) Fp<P>);
|
||||
impl<P: FpParams> Add for Fp<P> {
|
||||
type Output = Fp<P>;
|
||||
|
||||
fn add(mut self, other: Fp<P>) -> Fp<P> {
|
||||
self.0.add(&other.0, &P::modulus());
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: FpParams> Sub for Fp<P> {
|
||||
type Output = Fp<P>;
|
||||
|
||||
fn sub(mut self, other: Fp<P>) -> Fp<P> {
|
||||
self.0.sub(&other.0, &P::modulus());
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: FpParams> Mul for Fp<P> {
|
||||
type Output = Fp<P>;
|
||||
|
||||
fn mul(mut self, other: Fp<P>) -> Fp<P> {
|
||||
self.0.mul(&other.0, &P::modulus(), P::inv());
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: FpParams> Neg for Fp<P> {
|
||||
type Output = Fp<P>;
|
||||
|
||||
fn neg(mut self) -> Fp<P> {
|
||||
self.0.neg(&P::modulus());
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FrParams;
|
||||
pub type Fr = Fp<FrParams>;
|
||||
impl FpParams for FrParams {
|
||||
fn name() -> &'static str { "Fr" }
|
||||
|
||||
#[inline]
|
||||
fn modulus() -> U256 {
|
||||
// 21888242871839275222246405745257275088548364400416034343698204186575808495617
|
||||
[0xf0000001, 0x43e1f593, 0x79b97091, 0x2833e848, 0x8181585d, 0xb85045b6, 0xe131a029, 0x30644e72].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inv() -> u32 {
|
||||
0xefffffff
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rsquared() -> U256 {
|
||||
// 944936681149208446651664254269745548490766851729442924617792859073125903783
|
||||
[0xae216da7, 0x1bb8e645, 0xe35c59e3, 0x53fe3ab1, 0x53bb8085, 0x8c49833d, 0x7f4e44a5, 0x0216d0b1].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rcubed() -> U256 {
|
||||
// 5866548545943845227489894872040244720403868105578784105281690076696998248512
|
||||
[0xb4bf0040, 0x5e94d8e1, 0x1cfbb6b8, 0x2a489cbe, 0xa19fcfed, 0x893cc664, 0x7fcc657c, 0x0cf8594b].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn one() -> U256 {
|
||||
[0x4ffffffb, 0xac96341c, 0x9f60cd29, 0x36fc7695, 0x7879462e, 0x666ea36f, 0x9a07df2f, 0x0e0a77c1].into()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FqParams;
|
||||
pub type Fq = Fp<FqParams>;
|
||||
impl FpParams for FqParams {
|
||||
fn name() -> &'static str { "Fq" }
|
||||
|
||||
#[inline]
|
||||
fn modulus() -> U256 {
|
||||
// 21888242871839275222246405745257275088696311157297823662689037894645226208583
|
||||
[0xd87cfd47, 0x3c208c16, 0x6871ca8d, 0x97816a91, 0x8181585d, 0xb85045b6, 0xe131a029, 0x30644e72].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inv() -> u32 {
|
||||
0xe4866389
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rsquared() -> U256 {
|
||||
// 3096616502983703923843567936837374451735540968419076528771170197431451843209
|
||||
[0x538afa89, 0xf32cfc5b, 0xd44501fb, 0xb5e71911, 0x0a417ff6, 0x47ab1eff, 0xcab8351f, 0x06d89f71].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rcubed() -> U256 {
|
||||
// 14921786541159648185948152738563080959093619838510245177710943249661917737183
|
||||
[0xda1530df, 0xb1cd6daf, 0xa7283db6, 0x62f210e6, 0x0ada0afb, 0xef7f0b0c, 0x2d592544, 0x20fd6e90].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn one() -> U256 {
|
||||
[0xc58f0d9d, 0xd35d438d, 0xf5c70b3d, 0x0a78eb28, 0x7879462c, 0x666ea36f, 0x9a07df2f, 0x0e0a77c1].into()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rsquared() {
|
||||
let rng = &mut ::rand::thread_rng();
|
||||
|
||||
for _ in 0..1000 {
|
||||
let a = Fr::random(rng);
|
||||
let b: U256 = a.into();
|
||||
let c = Fr::new_checked(b);
|
||||
|
||||
assert_eq!(a, c);
|
||||
}
|
||||
|
||||
for _ in 0..1000 {
|
||||
let a = Fq::random(rng);
|
||||
let b: U256 = a.into();
|
||||
let c = Fq::new_checked(b);
|
||||
|
||||
assert_eq!(a, c);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user