mirror of
https://github.com/pezkuwichain/bizinikiwi-bn.git
synced 2026-07-22 09:45:43 +00:00
Use macros to instantiate finite fields.
This commit is contained in:
+143
-207
@@ -1,238 +1,174 @@
|
||||
use rand::Rng;
|
||||
use std::ops::{Add, Sub, Mul, Neg};
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use super::FieldElement;
|
||||
|
||||
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
||||
|
||||
use arith::U256;
|
||||
|
||||
pub trait FpParams {
|
||||
fn name() -> &'static str;
|
||||
fn modulus() -> U256;
|
||||
fn inv() -> u64;
|
||||
fn rsquared() -> U256;
|
||||
fn rcubed() -> U256;
|
||||
fn one() -> U256;
|
||||
}
|
||||
macro_rules! field_impl {
|
||||
($name:ident, $modulus:expr, $rsquared:expr, $rcubed:expr, $one:expr, $inv:expr) => {
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
#[repr(C)]
|
||||
pub struct $name(U256);
|
||||
|
||||
#[repr(C)]
|
||||
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: FpParams> PartialEq for Fp<P> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
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());
|
||||
impl From<$name> for U256 {
|
||||
#[inline]
|
||||
fn from(mut a: $name) -> Self {
|
||||
a.0.mul(&U256::one(), &U256($modulus), $inv);
|
||||
|
||||
a.0
|
||||
}
|
||||
}
|
||||
|
||||
a.0
|
||||
}
|
||||
}
|
||||
impl Encodable for $name {
|
||||
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
let normalized = U256::from(*self);
|
||||
|
||||
#[inline]
|
||||
pub fn const_fp<P: FpParams, I: Into<U256>>(i: I) -> Fp<P> {
|
||||
Fp(i.into(), PhantomData)
|
||||
}
|
||||
normalized.encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: FpParams> Encodable for Fp<P> {
|
||||
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
let normalized = U256::from(*self);
|
||||
impl Decodable for $name {
|
||||
fn decode<S: Decoder>(s: &mut S) -> Result<$name, S::Error> {
|
||||
$name::new(try!(U256::decode(s))).ok_or_else(|| s.error("integer is not less than modulus"))
|
||||
}
|
||||
}
|
||||
|
||||
normalized.encode(s)
|
||||
}
|
||||
}
|
||||
impl $name {
|
||||
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()
|
||||
};
|
||||
|
||||
impl<P: FpParams> Decodable for Fp<P> {
|
||||
fn decode<S: Decoder>(s: &mut S) -> Result<Fp<P>, S::Error> {
|
||||
Fp::new(try!(U256::decode(s))).ok_or_else(|| s.error("integer is not less than modulus"))
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
};
|
||||
Some(res)
|
||||
}
|
||||
|
||||
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;
|
||||
/// Converts a U256 to an Fp so long as it's below the modulus.
|
||||
pub fn new(mut a: U256) -> Option<Self> {
|
||||
if a < U256($modulus) {
|
||||
a.mul(&U256($rsquared), &U256($modulus), $inv);
|
||||
|
||||
Some($name(a))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
impl FieldElement for $name {
|
||||
#[inline]
|
||||
fn zero() -> Self {
|
||||
$name(U256([0, 0, 0, 0]))
|
||||
}
|
||||
|
||||
impl<P: FpParams> Fp<P> {
|
||||
/// Converts a U256 to an Fp so long as it's below the modulus.
|
||||
pub fn new(mut a: U256) -> Option<Self> {
|
||||
if a < P::modulus() {
|
||||
a.mul(&P::rsquared(), &P::modulus(), P::inv());
|
||||
#[inline]
|
||||
fn one() -> Self {
|
||||
$name(U256($one))
|
||||
}
|
||||
|
||||
fn random<R: Rng>(rng: &mut R) -> Self {
|
||||
$name(U256::random(rng, &U256($modulus)))
|
||||
}
|
||||
|
||||
Some(Fp(a, PhantomData))
|
||||
} else {
|
||||
None
|
||||
#[inline]
|
||||
fn is_zero(&self) -> bool {
|
||||
self.0.is_zero()
|
||||
}
|
||||
|
||||
fn inverse(mut self) -> Option<Self> {
|
||||
if self.is_zero() {
|
||||
None
|
||||
} else {
|
||||
self.0.invert(&U256($modulus));
|
||||
self.0.mul(&U256($rcubed), &U256($modulus), $inv);
|
||||
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for $name {
|
||||
type Output = $name;
|
||||
|
||||
#[inline]
|
||||
fn add(mut self, other: $name) -> $name {
|
||||
self.0.add(&other.0, &U256($modulus));
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for $name {
|
||||
type Output = $name;
|
||||
|
||||
#[inline]
|
||||
fn sub(mut self, other: $name) -> $name {
|
||||
self.0.sub(&other.0, &U256($modulus));
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for $name {
|
||||
type Output = $name;
|
||||
|
||||
#[inline]
|
||||
fn mul(mut self, other: $name) -> $name {
|
||||
self.0.mul(&other.0, &U256($modulus), $inv);
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for $name {
|
||||
type Output = $name;
|
||||
|
||||
#[inline]
|
||||
fn neg(mut self) -> $name {
|
||||
self.0.neg(&U256($modulus));
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: FpParams> FieldElement for Fp<P> {
|
||||
fn zero() -> Self {
|
||||
const_fp(U256::zero())
|
||||
}
|
||||
field_impl!(
|
||||
Fr,
|
||||
[0x43e1f593f0000001, 0x2833e84879b97091, 0xb85045b68181585d, 0x30644e72e131a029],
|
||||
[0x1bb8e645ae216da7, 0x53fe3ab1e35c59e3, 0x8c49833d53bb8085, 0x0216d0b17f4e44a5],
|
||||
[0x5e94d8e1b4bf0040, 0x2a489cbe1cfbb6b8, 0x893cc664a19fcfed, 0x0cf8594b7fcc657c],
|
||||
[0xac96341c4ffffffb, 0x36fc76959f60cd29, 0x666ea36f7879462e, 0xe0a77c19a07df2f],
|
||||
0xc2e1f593efffffff
|
||||
);
|
||||
|
||||
fn one() -> Self {
|
||||
const_fp(P::one())
|
||||
}
|
||||
|
||||
fn random<R: Rng>(rng: &mut R) -> Self {
|
||||
const_fp(U256::random(rng, &P::modulus()))
|
||||
}
|
||||
field_impl!(
|
||||
Fq,
|
||||
[0x3c208c16d87cfd47, 0x97816a916871ca8d, 0xb85045b68181585d, 0x30644e72e131a029],
|
||||
[0xf32cfc5b538afa89, 0xb5e71911d44501fb, 0x47ab1eff0a417ff6, 0x06d89f71cab8351f],
|
||||
[0xb1cd6dafda1530df, 0x62f210e6a7283db6, 0xef7f0b0c0ada0afb, 0x20fd6e902d592544],
|
||||
[0xd35d438dc58f0d9d, 0xa78eb28f5c70b3d, 0x666ea36f7879462c, 0xe0a77c19a07df2f],
|
||||
0x87d20782e4866389
|
||||
);
|
||||
|
||||
fn is_zero(&self) -> bool {
|
||||
self.0.is_zero()
|
||||
}
|
||||
|
||||
fn inverse(mut self) -> Option<Self> {
|
||||
if self.is_zero() {
|
||||
None
|
||||
} else {
|
||||
self.0.invert(&P::modulus());
|
||||
self.0.mul(&P::rcubed(), &P::modulus(), P::inv());
|
||||
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
[0x43e1f593f0000001, 0x2833e84879b97091, 0xb85045b68181585d, 0x30644e72e131a029].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inv() -> u64 {
|
||||
0xc2e1f593efffffff
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rsquared() -> U256 {
|
||||
// 944936681149208446651664254269745548490766851729442924617792859073125903783
|
||||
[0x1bb8e645ae216da7, 0x53fe3ab1e35c59e3, 0x8c49833d53bb8085, 0x0216d0b17f4e44a5].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rcubed() -> U256 {
|
||||
// 5866548545943845227489894872040244720403868105578784105281690076696998248512
|
||||
[0x5e94d8e1b4bf0040, 0x2a489cbe1cfbb6b8, 0x893cc664a19fcfed, 0x0cf8594b7fcc657c].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn one() -> U256 {
|
||||
[0xac96341c4ffffffb, 0x36fc76959f60cd29, 0x666ea36f7879462e, 0xe0a77c19a07df2f].into()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FqParams;
|
||||
pub type Fq = Fp<FqParams>;
|
||||
impl FpParams for FqParams {
|
||||
fn name() -> &'static str { "Fq" }
|
||||
|
||||
#[inline]
|
||||
fn modulus() -> U256 {
|
||||
// 21888242871839275222246405745257275088696311157297823662689037894645226208583
|
||||
[0x3c208c16d87cfd47, 0x97816a916871ca8d, 0xb85045b68181585d, 0x30644e72e131a029].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inv() -> u64 {
|
||||
0x87d20782e4866389
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rsquared() -> U256 {
|
||||
// 3096616502983703923843567936837374451735540968419076528771170197431451843209
|
||||
[0xf32cfc5b538afa89, 0xb5e71911d44501fb, 0x47ab1eff0a417ff6, 0x06d89f71cab8351f].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rcubed() -> U256 {
|
||||
// 14921786541159648185948152738563080959093619838510245177710943249661917737183
|
||||
[0xb1cd6dafda1530df, 0x62f210e6a7283db6, 0xef7f0b0c0ada0afb, 0x20fd6e902d592544].into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn one() -> U256 {
|
||||
[0xd35d438dc58f0d9d, 0xa78eb28f5c70b3d, 0x666ea36f7879462c, 0xe0a77c19a07df2f].into()
|
||||
}
|
||||
#[inline]
|
||||
pub fn const_fq(i: [u64; 4]) -> Fq {
|
||||
Fq(U256(i))
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+7
-7
@@ -1,4 +1,4 @@
|
||||
use fields::{FieldElement, Fq2, Fq, Fq6, const_fp};
|
||||
use fields::{FieldElement, Fq2, Fq, Fq6, const_fq};
|
||||
use std::ops::{Add, Sub, Mul, Neg};
|
||||
use rand::Rng;
|
||||
|
||||
@@ -8,16 +8,16 @@ fn frobenius_coeffs_c1(power: usize) -> Fq2 {
|
||||
match power % 12 {
|
||||
0 => Fq2::one(),
|
||||
1 => Fq2::new(
|
||||
const_fp([12653890742059813127, 14585784200204367754, 1278438861261381767, 212598772761311868]),
|
||||
const_fp([11683091849979440498, 14992204589386555739, 15866167890766973222, 1200023580730561873])
|
||||
const_fq([12653890742059813127, 14585784200204367754, 1278438861261381767, 212598772761311868]),
|
||||
const_fq([11683091849979440498, 14992204589386555739, 15866167890766973222, 1200023580730561873])
|
||||
),
|
||||
2 => Fq2::new(
|
||||
const_fp([14595462726357228530, 17349508522658994025, 1017833795229664280, 299787779797702374]),
|
||||
const_fq([14595462726357228530, 17349508522658994025, 1017833795229664280, 299787779797702374]),
|
||||
Fq::zero()
|
||||
),
|
||||
3 => Fq2::new(
|
||||
const_fp([3914496794763385213, 790120733010914719, 7322192392869644725, 581366264293887267]),
|
||||
const_fp([12817045492518885689, 4440270538777280383, 11178533038884588256, 2767537931541304486])
|
||||
const_fq([3914496794763385213, 790120733010914719, 7322192392869644725, 581366264293887267]),
|
||||
const_fq([12817045492518885689, 4440270538777280383, 11178533038884588256, 2767537931541304486])
|
||||
),
|
||||
_ => unimplemented!()
|
||||
}
|
||||
@@ -96,7 +96,7 @@ impl Fq12 {
|
||||
|
||||
pub fn exp_by_neg_z(&self) -> Fq12 {
|
||||
self.cyclotomic_pow(
|
||||
U256::from([4965661367192848881, 0, 0, 0])
|
||||
U256([4965661367192848881, 0, 0, 0])
|
||||
).unitary_inverse()
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
use fields::{FieldElement, const_fp, Fq};
|
||||
use fields::{FieldElement, const_fq, Fq};
|
||||
use std::ops::{Add, Sub, Mul, Neg};
|
||||
use rand::Rng;
|
||||
|
||||
@@ -8,14 +8,14 @@ use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
||||
fn fq_non_residue() -> Fq {
|
||||
// (q - 1) is a quadratic nonresidue in Fq
|
||||
// 21888242871839275222246405745257275088696311157297823662689037894645226208582
|
||||
const_fp([0x68c3488912edefaa, 0x8d087f6872aabf4f, 0x51e1a24709081231, 0x2259d6b14729c0fa])
|
||||
const_fq([0x68c3488912edefaa, 0x8d087f6872aabf4f, 0x51e1a24709081231, 0x2259d6b14729c0fa])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fq2_nonresidue() -> Fq2 {
|
||||
Fq2::new(
|
||||
const_fp([0xf60647ce410d7ff7, 0x2f3d6f4dd31bd011, 0x2943337e3940c6d1, 0x1d9598e8a7e39857]),
|
||||
const_fp([0xd35d438dc58f0d9d, 0x0a78eb28f5c70b3d, 0x666ea36f7879462c, 0x0e0a77c19a07df2f])
|
||||
const_fq([0xf60647ce410d7ff7, 0x2f3d6f4dd31bd011, 0x2943337e3940c6d1, 0x1d9598e8a7e39857]),
|
||||
const_fq([0xd35d438dc58f0d9d, 0x0a78eb28f5c70b3d, 0x666ea36f7879462c, 0x0e0a77c19a07df2f])
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+11
-11
@@ -1,4 +1,4 @@
|
||||
use fields::{FieldElement, Fq, Fq2, const_fp};
|
||||
use fields::{FieldElement, Fq, Fq2, const_fq};
|
||||
use std::ops::{Add, Sub, Mul, Neg};
|
||||
use rand::Rng;
|
||||
|
||||
@@ -6,16 +6,16 @@ fn frobenius_coeffs_c1(n: usize) -> Fq2 {
|
||||
match n % 6 {
|
||||
0 => Fq2::one(),
|
||||
1 => Fq2::new(
|
||||
const_fp([13075984984163199792, 3782902503040509012, 8791150885551868305, 1825854335138010348]),
|
||||
const_fp([7963664994991228759, 12257807996192067905, 13179524609921305146, 2767831111890561987])
|
||||
const_fq([13075984984163199792, 3782902503040509012, 8791150885551868305, 1825854335138010348]),
|
||||
const_fq([7963664994991228759, 12257807996192067905, 13179524609921305146, 2767831111890561987])
|
||||
),
|
||||
2 => Fq2::new(
|
||||
const_fp([3697675806616062876, 9065277094688085689, 6918009208039626314, 2775033306905974752]),
|
||||
const_fq([3697675806616062876, 9065277094688085689, 6918009208039626314, 2775033306905974752]),
|
||||
Fq::zero()
|
||||
),
|
||||
3 => Fq2::new(
|
||||
const_fp([14532872967180610477, 12903226530429559474, 1868623743233345524, 2316889217940299650]),
|
||||
const_fp([12447993766991532972, 4121872836076202828, 7630813605053367399, 740282956577754197])
|
||||
const_fq([14532872967180610477, 12903226530429559474, 1868623743233345524, 2316889217940299650]),
|
||||
const_fq([12447993766991532972, 4121872836076202828, 7630813605053367399, 740282956577754197])
|
||||
),
|
||||
_ => unimplemented!()
|
||||
}
|
||||
@@ -24,16 +24,16 @@ fn frobenius_coeffs_c2(n: usize) -> Fq2 {
|
||||
match n % 6 {
|
||||
0 => Fq2::one(),
|
||||
1 => Fq2::new(
|
||||
const_fp([8314163329781907090, 11942187022798819835, 11282677263046157209, 1576150870752482284]),
|
||||
const_fp([6763840483288992073, 7118829427391486816, 4016233444936635065, 2630958277570195709])
|
||||
const_fq([8314163329781907090, 11942187022798819835, 11282677263046157209, 1576150870752482284]),
|
||||
const_fq([6763840483288992073, 7118829427391486816, 4016233444936635065, 2630958277570195709])
|
||||
),
|
||||
2 => Fq2::new(
|
||||
const_fp([8183898218631979349, 12014359695528440611, 12263358156045030468, 3187210487005268291]),
|
||||
const_fq([8183898218631979349, 12014359695528440611, 12263358156045030468, 3187210487005268291]),
|
||||
Fq::zero()
|
||||
),
|
||||
3 => Fq2::new(
|
||||
const_fp([4938922280314430175, 13823286637238282975, 15589480384090068090, 481952561930628184]),
|
||||
const_fp([3105754162722846417, 11647802298615474591, 13057042392041828081, 1660844386505564338])
|
||||
const_fq([4938922280314430175, 13823286637238282975, 15589480384090068090, 481952561930628184]),
|
||||
const_fq([3105754162722846417, 11647802298615474591, 13057042392041828081, 1660844386505564338])
|
||||
),
|
||||
_ => unimplemented!()
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ use rand::Rng;
|
||||
use std::ops::{Add, Sub, Mul, Neg};
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub use self::fp::{Fq,Fr,const_fp};
|
||||
pub use self::fp::{Fq,Fr,const_fq};
|
||||
pub use self::fq2::{Fq2, fq2_nonresidue};
|
||||
pub use self::fq6::Fq6;
|
||||
pub use self::fq12::Fq12;
|
||||
|
||||
Reference in New Issue
Block a user