Skip to content

Commit

Permalink
Add PrimeField::MODULUS
Browse files Browse the repository at this point in the history
This is an intentionally-opaque string format for debugging purposes.
  • Loading branch information
str4d committed Nov 24, 2022
1 parent feb2c0c commit c0805cd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and this library adheres to Rust's notion of
- `ff::Field::{sqrt_ratio, sqrt_alt}`
- `core::iter::{Sum, Product}` bounds on `ff::Field`
- `ff::PrimeField::from_u128`
- `ff::PrimeField::TWO_INV`
- `ff::PrimeField::{MODULUS, TWO_INV}`
- Constants related to multiplicative generators:
- `ff::PrimeField::MULTIPLICATIVE_GENERATOR`
- `ff::PrimeField::{ROOT_OF_UNITY, ROOT_OF_UNITY_INV}`
Expand Down
6 changes: 6 additions & 0 deletions ff_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ fn prime_field_constants_and_sqrt(

let r = biguint_to_u64_vec(r, limbs);
let modulus_le_bytes = ReprEndianness::Little.modulus_repr(modulus, limbs * 8);
let modulus_str = format!("0x{}", modulus.to_str_radix(16));
let modulus = biguint_to_real_u64_vec(modulus.clone(), limbs);

// Compute -m^-1 mod 2**64 by exponentiating by totient(2**64) - 1
Expand All @@ -615,6 +616,9 @@ fn prime_field_constants_and_sqrt(
/// This is the modulus m of the prime field in limb form
const MODULUS_LIMBS: #name = #name([#(#modulus,)*]);

/// This is the modulus m of the prime field in hex string form
const MODULUS_STR: &'static str = #modulus_str;

/// The number of bits needed to represent the modulus.
const MODULUS_BITS: u32 = #modulus_num_bits;

Expand Down Expand Up @@ -1237,6 +1241,8 @@ fn prime_field_impl(
::ff::derive::subtle::Choice::from((r.0[0] & 1) as u8)
}

const MODULUS: &'static str = MODULUS_STR;

const NUM_BITS: u32 = MODULUS_BITS;

const CAPACITY: u32 = Self::NUM_BITS - 1;
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ pub trait PrimeField: Field + From<u64> {
!self.is_odd()
}

/// Modulus of the field written as a string for debugging purposes.
///
/// The encoding of the modulus is implementation-specific. Generic users of the
/// `PrimeField` trait should treat this string as opaque.
const MODULUS: &'static str;

/// How many bits are needed to represent an element of this field.
const NUM_BITS: u32;

Expand Down
5 changes: 5 additions & 0 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ mod full_limbs {
fn constants() {
use ff::{Field, PrimeField};

assert_eq!(
Bls381K12Scalar::MODULUS,
"0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",
);

assert_eq!(
Bls381K12Scalar::from(2) * Bls381K12Scalar::TWO_INV,
Bls381K12Scalar::ONE,
Expand Down

0 comments on commit c0805cd

Please sign in to comment.