Skip to content

Commit

Permalink
docs: add docs for fp and fp2
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Jun 20, 2024
1 parent e0fbf33 commit 1bcd44e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::util::{adc, mac, sbb};
// integers in little-endian order. `Fp` values are always in
// Montgomery form; i.e., Scalar(a) = aR mod p, with R = 2^384.

#[allow(missing_docs)]
#[derive(Copy, Clone)]
/// Represents an element in the finite field Fp.
pub struct Fp(pub(crate) [u64; 6]);

impl fmt::Debug for Fp {
Expand Down Expand Up @@ -158,11 +158,10 @@ impl<'a, 'b> Mul<&'b Fp> for &'a Fp {
impl_binops_additive!(Fp, Fp);
impl_binops_multiplicative!(Fp, Fp);

#[allow(missing_docs)]
impl Fp {
/// Builds an element of `Fp` from a little-endian byte representation.
pub fn new_unsafe(bytes: [u64; 6]) -> Self {
Fp(bytes)
/// Builds an element of `Fp` from little-endian limbs.
pub fn new_unsafe(limbs: [u64; 6]) -> Self {
Fp(limbs)
}

/// Returns zero, the additive identity.
Expand All @@ -177,6 +176,7 @@ impl Fp {
R
}

/// Checks if this element is zero.
pub fn is_zero(&self) -> Choice {
self.ct_eq(&Fp::zero())
}
Expand Down Expand Up @@ -328,6 +328,7 @@ impl Fp {
}

#[inline]
/// Computes the square root of this field element.
pub fn sqrt(&self) -> CtOption<Self> {
// We use Shank's method, as p = 3 (mod 4). This means
// we only need to exponentiate by (p+1)/4. This only
Expand Down Expand Up @@ -386,6 +387,7 @@ impl Fp {
}

#[inline]
/// Add two field elements together.
pub const fn add(&self, rhs: &Fp) -> Fp {
let (d0, carry) = adc(self.0[0], rhs.0[0], 0);
let (d1, carry) = adc(self.0[1], rhs.0[1], carry);
Expand All @@ -400,6 +402,7 @@ impl Fp {
}

#[inline]
/// Returns the negation of this field element.
pub const fn neg(&self) -> Fp {
let (d0, borrow) = sbb(MODULUS[0], self.0[0], 0);
let (d1, borrow) = sbb(MODULUS[1], self.0[1], borrow);
Expand All @@ -425,6 +428,7 @@ impl Fp {
}

#[inline]
/// Squares this element.
pub const fn sub(&self, rhs: &Fp) -> Fp {
(&rhs.neg()).add(self)
}
Expand Down Expand Up @@ -569,6 +573,7 @@ impl Fp {
}

#[inline]
/// Multiplies two field elements, returning the result in the Montgomery domain.
pub const fn mul(&self, rhs: &Fp) -> Fp {
let (t0, carry) = mac(0, self.0[0], rhs.0[0], 0);
let (t1, carry) = mac(0, self.0[0], rhs.0[1], carry);
Expand Down
16 changes: 15 additions & 1 deletion src/fp2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! This module implements arithmetic over the quadratic extension field Fp2.
#![allow(missing_docs)]
use core::fmt;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
Expand All @@ -9,8 +8,11 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
use crate::fp::Fp;

#[derive(Copy, Clone)]
/// Represents an element in the field Fp2.
pub struct Fp2 {
/// The first component of the Fp2 element.
pub c0: Fp,
/// The second component of the Fp2 element.
pub c1: Fp,
}

Expand Down Expand Up @@ -110,6 +112,7 @@ impl_binops_additive!(Fp2, Fp2);
impl_binops_multiplicative!(Fp2, Fp2);

impl Fp2 {
/// Returns the zero element of Fp2.
#[inline]
pub const fn zero() -> Fp2 {
Fp2 {
Expand All @@ -118,6 +121,7 @@ impl Fp2 {
}
}

/// Returns the one element of Fp2.
#[inline]
pub const fn one() -> Fp2 {
Fp2 {
Expand All @@ -126,10 +130,12 @@ impl Fp2 {
}
}

/// Checks if this element is zero.
pub fn is_zero(&self) -> Choice {
self.c0.is_zero() & self.c1.is_zero()
}

/// Generates a random element in Fp2.
pub(crate) fn random(mut rng: impl RngCore) -> Fp2 {
Fp2 {
c0: Fp::random(&mut rng),
Expand All @@ -145,6 +151,7 @@ impl Fp2 {
self.conjugate()
}

/// Computes the conjugate of this element.
#[inline(always)]
pub fn conjugate(&self) -> Self {
Fp2 {
Expand All @@ -153,6 +160,7 @@ impl Fp2 {
}
}

/// Multiplies this element by the non-residue.
#[inline(always)]
pub fn mul_by_nonresidue(&self) -> Fp2 {
// Multiply a + bu by u + 1, getting
Expand Down Expand Up @@ -180,6 +188,7 @@ impl Fp2 {
| (self.c1.is_zero() & self.c0.lexicographically_largest())
}

/// Computes the square of this element.
pub const fn square(&self) -> Fp2 {
// Complex squaring:
//
Expand All @@ -203,6 +212,7 @@ impl Fp2 {
}
}

/// Multiplies this element by another element.
pub fn mul(&self, rhs: &Fp2) -> Fp2 {
// F_{p^2} x F_{p^2} multiplication implemented with operand scanning (schoolbook)
// computes the result as:
Expand All @@ -222,27 +232,31 @@ impl Fp2 {
}
}

/// Adds another element to this element.
pub const fn add(&self, rhs: &Fp2) -> Fp2 {
Fp2 {
c0: (&self.c0).add(&rhs.c0),
c1: (&self.c1).add(&rhs.c1),
}
}

/// Subtracts another element from this element.
pub const fn sub(&self, rhs: &Fp2) -> Fp2 {
Fp2 {
c0: (&self.c0).sub(&rhs.c0),
c1: (&self.c1).sub(&rhs.c1),
}
}

/// Negates this element.
pub const fn neg(&self) -> Fp2 {
Fp2 {
c0: (&self.c0).neg(),
c1: (&self.c1).neg(),
}
}

/// Computes the square root of this element.
pub fn sqrt(&self) -> CtOption<Self> {
// Algorithm 9, https://eprint.iacr.org/2012/685.pdf
// with constant time modifications.
Expand Down

0 comments on commit 1bcd44e

Please sign in to comment.