Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 61 additions & 48 deletions src/primitives/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,6 @@ pub trait Field:
/// prime factors which each appearing once, this array would have size `2^n`.
const MULTIPLICATIVE_ORDER_FACTORS: &'static [usize];

/// Adds a value to `self`. This is a helper function for implementing the
/// [`ops::Add`] and [`ops::AddAssign`] traits, which should probably be called
/// instead of calling this.
fn _add(&self, other: &Self) -> Self;

/// Subtracts a value from `self`. This is a helper function for implementing the
/// [`ops::Sub`] and [`ops::SubAssign`] traits, which should probably be called
/// instead of calling this.
fn _sub(&self, other: &Self) -> Self;

/// Multiplies a value by `self`. This is a helper function for implementing the
/// [`ops::Mul`] and [`ops::MulAssign`] traits, which should probably be called
/// instead of calling this.
fn _mul(&self, other: &Self) -> Self;

/// Divides a value from `self`. This is a helper function for implementing the
/// [`ops::Div`] and [`ops::DivAssign`] traits, which should probably be called
/// instead of calling this.
fn _div(&self, other: &Self) -> Self;

/// Computes the additive inverse of an element.
fn _neg(self) -> Self;

/// Computes the multiplicative inverse of an element.
fn multiplicative_inverse(self) -> Self;

Expand Down Expand Up @@ -141,169 +118,205 @@ pub trait ExtensionField: Field + From<Self::BaseField> {
const EXT_ELEM: Self;
}

mod private {
/// Sealing trait.
pub trait Sealed {}
Comment on lines +138 to +139
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clever 👌


impl Sealed for crate::Fe32 {}
impl Sealed for crate::Fe1024 {}
impl Sealed for crate::Fe32768 {}
}

/// Sealed trait which extends [`Field`] with extra functionality
/// needed internally to this library.
///
/// This trait should not be used directly by users of the library.
pub trait Bech32Field: private::Sealed + Sized {
/// Adds a value to `self`. This is a helper function for implementing the
/// [`ops::Add`] and [`ops::AddAssign`] traits.
fn _add(&self, other: &Self) -> Self;

/// Subtracts a value from `self`. This is a helper function for implementing the
/// [`ops::Sub`] and [`ops::SubAssign`] traits.
fn _sub(&self, other: &Self) -> Self {
self._add(other) // all fields in this library are binary fields
}

/// Multiplies a value by `self`. This is a helper function for implementing the
/// [`ops::Mul`] and [`ops::MulAssign`] traits.
fn _mul(&self, other: &Self) -> Self;

/// Divides a value from `self`. This is a helper function for implementing the
/// [`ops::Div`] and [`ops::DivAssign`] traits.
fn _div(&self, other: &Self) -> Self;

/// Computes the additive inverse of an element.
fn _neg(self) -> Self;
}

macro_rules! impl_ops_for_fe {
(impl for $op:ident) => {
// add
impl core::ops::Add<$op> for $op {
type Output = Self;
#[inline]
fn add(self, other: $op) -> $op { $crate::primitives::Field::_add(&self, &other) }
fn add(self, other: $op) -> $op { $crate::primitives::Bech32Field::_add(&self, &other) }
}

impl core::ops::Add<&$op> for $op {
type Output = Self;
#[inline]
fn add(self, other: &$op) -> $op { $crate::primitives::Field::_add(&self, other) }
fn add(self, other: &$op) -> $op { $crate::primitives::Bech32Field::_add(&self, other) }
}

impl core::ops::Add<$op> for &$op {
type Output = $op;
#[inline]
fn add(self, other: $op) -> $op { $crate::primitives::Field::_add(self, &other) }
fn add(self, other: $op) -> $op { $crate::primitives::Bech32Field::_add(self, &other) }
}

impl core::ops::Add<&$op> for &$op {
type Output = $op;
#[inline]
fn add(self, other: &$op) -> $op { $crate::primitives::Field::_add(self, other) }
fn add(self, other: &$op) -> $op { $crate::primitives::Bech32Field::_add(self, other) }
}

impl core::ops::AddAssign for $op {
#[inline]
fn add_assign(&mut self, other: $op) {
*self = $crate::primitives::Field::_add(self, &other)
*self = $crate::primitives::Bech32Field::_add(self, &other)
}
}

impl core::ops::AddAssign<&$op> for $op {
#[inline]
fn add_assign(&mut self, other: &$op) {
*self = $crate::primitives::Field::_add(self, other)
*self = $crate::primitives::Bech32Field::_add(self, other)
}
}

// sub
impl core::ops::Sub<$op> for $op {
type Output = Self;
#[inline]
fn sub(self, other: $op) -> $op { $crate::primitives::Field::_sub(&self, &other) }
fn sub(self, other: $op) -> $op { $crate::primitives::Bech32Field::_sub(&self, &other) }
}

impl core::ops::Sub<&$op> for $op {
type Output = Self;
#[inline]
fn sub(self, other: &$op) -> $op { $crate::primitives::Field::_sub(&self, other) }
fn sub(self, other: &$op) -> $op { $crate::primitives::Bech32Field::_sub(&self, other) }
}

impl core::ops::Sub<$op> for &$op {
type Output = $op;
#[inline]
fn sub(self, other: $op) -> $op { $crate::primitives::Field::_sub(self, &other) }
fn sub(self, other: $op) -> $op { $crate::primitives::Bech32Field::_sub(self, &other) }
}

impl core::ops::Sub<&$op> for &$op {
type Output = $op;
#[inline]
fn sub(self, other: &$op) -> $op { $crate::primitives::Field::_sub(self, other) }
fn sub(self, other: &$op) -> $op { $crate::primitives::Bech32Field::_sub(self, other) }
}

impl core::ops::SubAssign for $op {
#[inline]
fn sub_assign(&mut self, other: $op) {
*self = $crate::primitives::Field::_sub(self, &other)
*self = $crate::primitives::Bech32Field::_sub(self, &other)
}
}

impl core::ops::SubAssign<&$op> for $op {
#[inline]
fn sub_assign(&mut self, other: &$op) {
*self = $crate::primitives::Field::_sub(self, other)
*self = $crate::primitives::Bech32Field::_sub(self, other)
}
}

// mul
impl core::ops::Mul<$op> for $op {
type Output = Self;
#[inline]
fn mul(self, other: $op) -> $op { $crate::primitives::Field::_mul(&self, &other) }
fn mul(self, other: $op) -> $op { $crate::primitives::Bech32Field::_mul(&self, &other) }
}

impl core::ops::Mul<&$op> for $op {
type Output = Self;
#[inline]
fn mul(self, other: &$op) -> $op { $crate::primitives::Field::_mul(&self, other) }
fn mul(self, other: &$op) -> $op { $crate::primitives::Bech32Field::_mul(&self, other) }
}

impl core::ops::Mul<$op> for &$op {
type Output = $op;
#[inline]
fn mul(self, other: $op) -> $op { $crate::primitives::Field::_mul(self, &other) }
fn mul(self, other: $op) -> $op { $crate::primitives::Bech32Field::_mul(self, &other) }
}

impl core::ops::Mul<&$op> for &$op {
type Output = $op;
#[inline]
fn mul(self, other: &$op) -> $op { $crate::primitives::Field::_mul(self, other) }
fn mul(self, other: &$op) -> $op { $crate::primitives::Bech32Field::_mul(self, other) }
}

impl core::ops::MulAssign for $op {
#[inline]
fn mul_assign(&mut self, other: $op) {
*self = $crate::primitives::Field::_mul(self, &other)
*self = $crate::primitives::Bech32Field::_mul(self, &other)
}
}

impl core::ops::MulAssign<&$op> for $op {
#[inline]
fn mul_assign(&mut self, other: &$op) {
*self = $crate::primitives::Field::_mul(self, other)
*self = $crate::primitives::Bech32Field::_mul(self, other)
}
}

// div
impl core::ops::Div<$op> for $op {
type Output = Self;
#[inline]
fn div(self, other: $op) -> $op { $crate::primitives::Field::_div(&self, &other) }
fn div(self, other: $op) -> $op { $crate::primitives::Bech32Field::_div(&self, &other) }
}

impl core::ops::Div<&$op> for $op {
type Output = Self;
#[inline]
fn div(self, other: &$op) -> $op { $crate::primitives::Field::_div(&self, other) }
fn div(self, other: &$op) -> $op { $crate::primitives::Bech32Field::_div(&self, other) }
}

impl core::ops::Div<$op> for &$op {
type Output = $op;
#[inline]
fn div(self, other: $op) -> $op { $crate::primitives::Field::_div(self, &other) }
fn div(self, other: $op) -> $op { $crate::primitives::Bech32Field::_div(self, &other) }
}

impl core::ops::Div<&$op> for &$op {
type Output = $op;
#[inline]
fn div(self, other: &$op) -> $op { $crate::primitives::Field::_div(self, other) }
fn div(self, other: &$op) -> $op { $crate::primitives::Bech32Field::_div(self, other) }
}

impl core::ops::DivAssign for $op {
#[inline]
fn div_assign(&mut self, other: $op) {
*self = $crate::primitives::Field::_div(self, &other)
*self = $crate::primitives::Bech32Field::_div(self, &other)
}
}

impl core::ops::DivAssign<&$op> for $op {
#[inline]
fn div_assign(&mut self, other: &$op) {
*self = $crate::primitives::Field::_div(self, other)
*self = $crate::primitives::Bech32Field::_div(self, other)
}
}

// neg
impl core::ops::Neg for $op {
type Output = Self;
#[inline]
fn neg(self) -> Self { $crate::primitives::Field::_neg(self) }
fn neg(self) -> Self { $crate::primitives::Bech32Field::_neg(self) }
}

// sum
Expand Down
21 changes: 12 additions & 9 deletions src/primitives/gf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use core::{fmt, num};
#[cfg(all(test, mutate))]
use mutagen::mutate;

use super::{Bech32Field, Field};
use crate::error::write_err;

/// Logarithm table of each bech32 element, as a power of alpha = Z.
Expand Down Expand Up @@ -256,16 +257,9 @@ impl AsRef<u8> for Fe32 {
fn as_ref(&self) -> &u8 { &self.0 }
}

impl super::Field for Fe32 {
const ZERO: Self = Fe32::Q;
const ONE: Self = Fe32::P;
const GENERATOR: Self = Fe32::Z;
const MULTIPLICATIVE_ORDER: usize = 31;
const MULTIPLICATIVE_ORDER_FACTORS: &'static [usize] = &[1, 31];

impl Bech32Field for Fe32 {
fn _add(&self, other: &Fe32) -> Fe32 { Fe32(self.0 ^ other.0) }
// Subtraction is the same as addition in a characteristic-2 field
fn _sub(&self, other: &Fe32) -> Fe32 { self._add(other) }

fn _mul(&self, other: &Fe32) -> Fe32 {
if self.0 == 0 || other.0 == 0 {
Fe32(0)
Expand All @@ -276,6 +270,7 @@ impl super::Field for Fe32 {
Fe32(LOG_INV[((log1 + log2) % mult_order) as usize])
}
}

fn _div(&self, other: &Fe32) -> Fe32 {
if self.0 == 0 {
Fe32(0)
Expand All @@ -290,6 +285,14 @@ impl super::Field for Fe32 {
}

fn _neg(self) -> Self { self }
}

impl Field for Fe32 {
const ZERO: Self = Fe32::Q;
const ONE: Self = Fe32::P;
const GENERATOR: Self = Fe32::Z;
const MULTIPLICATIVE_ORDER: usize = 31;
const MULTIPLICATIVE_ORDER_FACTORS: &'static [usize] = &[1, 31];

fn multiplicative_inverse(self) -> Self { Self::ONE._div(&self) }
}
Expand Down
Loading