Module field

Module field 

Source
Expand description

This module provides common arithmetics to work with finite fields. Implementations of some used fields provided in the instance module.

Abstractions and api in this module are similar to Arkworks Algebra ark-ff library.

Here is an example operations over a prime finite field (aka Fp) with a prime modulus 17 and generator element 3.

§Examples

use openzeppelin_crypto::{
    arithmetic::uint::U64,
    field::{
        fp::{Fp64, FpParams, LIMBS_64},
        group::AdditiveGroup,
        Field,
    },
    fp_from_num,
    from_num,
};

pub type ExampleField = Fp64<FpParam>;
pub struct FpParam;
impl FpParams<LIMBS_64> for FpParam {
    const MODULUS: U64 = from_num!("17");
    const GENERATOR: Fp64<FpParam> = fp_from_num!("3");
}

let a = ExampleField::from(9);
let b = ExampleField::from(10);

assert_eq!(a, ExampleField::from(26));          // 26 =  9 mod 17
assert_eq!(a - b, ExampleField::from(16));      // -1 = 16 mod 17
assert_eq!(a + b, ExampleField::from(2));       // 19 =  2 mod 17
assert_eq!(a * b, ExampleField::from(5));       // 90 =  5 mod 17
assert_eq!(a.square(), ExampleField::from(13)); // 81 = 13 mod 17
assert_eq!(b.double(), ExampleField::from(3));  // 20 =  3 mod 17
assert_eq!(a / b, a * b.inverse().unwrap());    // need to unwrap since `b` could be 0 which is not invertible

Modules§

fp
This module contains the implementation of a prime field element Fp, altogether with exact implementations Fp64 for 64-bit, Fp128 for 128-bit elements and so on.
group
This module provides a generic interface for groups with additive notation.
instance
This module contains the field instances for some popular curves.
prime
This module provides a generic interface for finite prime fields.

Traits§

Field
Defines an abstract field. Types implementing Field support common field operations such as addition, subtraction, multiplication, and inverses.