Skip to content

Instantly share code, notes, and snippets.

@sshine
Last active December 22, 2021 16:59
Show Gist options
  • Save sshine/21c5e846cbadbed90097eeb6164c3478 to your computer and use it in GitHub Desktop.
Save sshine/21c5e846cbadbed90097eeb6164c3478 to your computer and use it in GitHub Desktop.
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use num_bigint::BigInt;
use num_traits::{One, Zero};
use crate::shared_math::traits::{ModPowU32, PrimeFieldElement};
pub trait PrimeFieldInnerElement {
type Elem: Clone
+ Eq
+ Add<Output = Self::Elem>
+ Mul<Output = Self::Elem>
+ Rem<Output = Self::Elem>
+ Zero
+ One;
const MODULUS: Self::Elem;
}
// pub trait PrimeFieldModulus<T: PrimeFieldInnerElement> {
// const MODULUS: T::Elem;
// }
pub trait PrimeFieldElementGeneric<T: PrimeFieldInnerElement>
where
Self: Sized,
{
// Abstract
fn new(value: T::Elem) -> Self;
fn inner(&self) -> T::Elem;
// Concrete
fn zero() -> Self {
Self::new(T::Elem::zero())
}
fn is_zero(&self) -> bool {
self.inner() == T::Elem::zero()
}
fn one() -> Self {
Self::new(T::Elem::one())
}
fn is_one(&self) -> bool {
self.inner() == T::Elem::one()
}
fn add(&self, other: Self) -> Self {
Self::new((self.inner() + other.inner()) % T::MODULUS)
}
fn mul(&self, other: Self) -> Self {
Self::new((self.inner() * other.inner()) % T::MODULUS)
}
fn rem(&self, other: Self) -> Self {
Self::new(self.inner() % other.inner())
}
}
pub struct PrimeFieldElementBig17 {
pub value: BigInt,
}
impl PrimeFieldInnerElement for PrimeFieldElementBig17 {
type Elem = BigInt;
const MODULUS: Self::Elem = 17.into();
}
// impl PrimeFieldModulus<PrimeFieldElementBig17> for PrimeFieldElementBig17 {
// const MODULUS: PrimeFieldElementBig17::Elem = todo!();
// }
impl PrimeFieldElementGeneric<PrimeFieldElementBig17> for PrimeFieldElementBig17 {
fn new(value: <PrimeFieldElementBig17 as PrimeFieldInnerElement>::Elem) -> Self {
todo!()
}
fn inner(&self) -> <PrimeFieldElementBig17 as PrimeFieldInnerElement>::Elem {
todo!()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment