pluto / ronkathon

Cryptography Educational Foundations
https://pluto.xyz/blog/ronkathon-learn-cryptography-from-first-principles
Apache License 2.0
178 stars 22 forks source link

feat: prime field specializations #99

Closed lonerapier closed 2 months ago

lonerapier commented 3 months ago

use nightly specialisation feature to make field methods default so that it can be overridden

impl<const P: usize> const Add for PrimeField<P> {
  default type Output = Self;

  default fn add(self, rhs: Self) -> Self { Self { value: (self.value + rhs.value) % Self::ORDER } }
}

and then overridden by BinaryField

pub type BinaryField = PrimeField<2>;

impl Add for BinaryField {
  type Output = Self;

  #[allow(clippy::suspicious_arithmetic_impl)]
  fn add(self, rhs: Self) -> Self::Output { BinaryField::new(self.value ^ rhs.value) }
}
lonerapier commented 2 months ago

not needed due to #126