Foundation of finite field arithmetic
This commit is contained in:
parent
b60f4f0db0
commit
7f515bbe1c
|
@ -11,3 +11,5 @@ documentation = "https://ebfull.github.io/bn/"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
num = "0.1.32"
|
||||||
|
rand = "0.3.14"
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
use rand::Rng;
|
||||||
|
use num::{BigUint, Num};
|
||||||
|
use std::ops::{Mul,Add,Sub,Neg};
|
||||||
|
use std::convert::From;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
pub trait PrimeFieldParams {
|
||||||
|
fn modulus() -> BigUint;
|
||||||
|
fn bits() -> usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Fp<P: PrimeFieldParams> {
|
||||||
|
value: BigUint,
|
||||||
|
_marker: PhantomData<P>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: PrimeFieldParams> Fp<P> {
|
||||||
|
pub fn zero() -> Self { unimplemented!() }
|
||||||
|
pub fn one() -> Self { unimplemented!() }
|
||||||
|
pub fn random<R: Rng>(rng: &mut R) -> Self { unimplemented!() }
|
||||||
|
|
||||||
|
pub fn is_zero(&self) -> bool { unimplemented!() }
|
||||||
|
pub fn inverse(&self) -> Self { unimplemented!() }
|
||||||
|
pub fn squared(&self) -> Self { unimplemented!() }
|
||||||
|
pub fn pow<P2: PrimeFieldParams>(&self, exp: &Fp<P2>) -> Self { unimplemented!() }
|
||||||
|
pub fn test_bit(&self, bit: usize) -> bool { unimplemented!() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, P: PrimeFieldParams> From<&'a str> for Fp<P> {
|
||||||
|
fn from(s: &'a str) -> Self { unimplemented!() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: PrimeFieldParams> Clone for Fp<P> {
|
||||||
|
fn clone(&self) -> Self { unimplemented!() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b, P: PrimeFieldParams> Add<&'b Fp<P>> for &'a Fp<P> {
|
||||||
|
type Output = Fp<P>;
|
||||||
|
|
||||||
|
fn add(self, other: &Fp<P>) -> Fp<P> { unimplemented!() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b, P: PrimeFieldParams> Sub<&'b Fp<P>> for &'a Fp<P> {
|
||||||
|
type Output = Fp<P>;
|
||||||
|
|
||||||
|
fn sub(self, other: &Fp<P>) -> Fp<P> { unimplemented!() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b, P: PrimeFieldParams> Mul<&'b Fp<P>> for &'a Fp<P> {
|
||||||
|
type Output = Fp<P>;
|
||||||
|
|
||||||
|
fn mul(self, other: &Fp<P>) -> Fp<P> { unimplemented!() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, P: PrimeFieldParams> Neg for &'a Fp<P> {
|
||||||
|
type Output = Fp<P>;
|
||||||
|
|
||||||
|
fn neg(self) -> Fp<P> { unimplemented!() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: PrimeFieldParams> Neg for Fp<P> {
|
||||||
|
type Output = Fp<P>;
|
||||||
|
|
||||||
|
fn neg(self) -> Fp<P> {
|
||||||
|
-(&self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
forward_all_binop_to_ref_ref!(impl(P: PrimeFieldParams) Mul for Fp<P>, mul);
|
|
@ -0,0 +1,7 @@
|
||||||
|
extern crate num;
|
||||||
|
extern crate rand;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
|
mod fp;
|
||||||
|
mod params;
|
|
@ -0,0 +1,46 @@
|
||||||
|
macro_rules! forward_val_val_binop {
|
||||||
|
(impl($($t:ident: $p:ident),*) $imp:ident for $res:ty, $method:ident) => {
|
||||||
|
impl<$($t: $p),*> $imp<$res> for $res {
|
||||||
|
type Output = $res;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn $method(self, other: $res) -> $res {
|
||||||
|
$imp::$method(&self, &other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! forward_ref_val_binop {
|
||||||
|
(impl($($t:ident: $p:ident),*) $imp:ident for $res:ty, $method:ident) => {
|
||||||
|
impl<'a, $($t: $p),*> $imp<$res> for &'a $res {
|
||||||
|
type Output = $res;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn $method(self, other: $res) -> $res {
|
||||||
|
$imp::$method(self, &other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! forward_val_ref_binop {
|
||||||
|
(impl($($t:ident: $p:ident),*) $imp:ident for $res:ty, $method:ident) => {
|
||||||
|
impl<'a, $($t: $p),*> $imp<&'a $res> for $res {
|
||||||
|
type Output = $res;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn $method(self, other: &$res) -> $res {
|
||||||
|
$imp::$method(&self, other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! forward_all_binop_to_ref_ref {
|
||||||
|
(impl($($t:ident: $p:ident),*) $imp:ident for $res:ty, $method:ident) => {
|
||||||
|
forward_val_val_binop!(impl($($t: $p),*) $imp for $res, $method);
|
||||||
|
forward_ref_val_binop!(impl($($t: $p),*) $imp for $res, $method);
|
||||||
|
forward_val_ref_binop!(impl($($t: $p),*) $imp for $res, $method);
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
use num::{Num,BigUint};
|
||||||
|
use fp::PrimeFieldParams;
|
||||||
|
|
||||||
|
pub struct FrParams;
|
||||||
|
|
||||||
|
impl PrimeFieldParams for FrParams {
|
||||||
|
fn modulus() -> BigUint {
|
||||||
|
BigUint::from_str_radix("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10).unwrap()
|
||||||
|
}
|
||||||
|
fn bits() -> usize { 254 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FqParams;
|
||||||
|
|
||||||
|
impl PrimeFieldParams for FqParams {
|
||||||
|
fn modulus() -> BigUint {
|
||||||
|
BigUint::from_str_radix("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10).unwrap()
|
||||||
|
}
|
||||||
|
fn bits() -> usize { 254 }
|
||||||
|
}
|
Loading…
Reference in New Issue