Skip to content

Commit

Permalink
update Legendre impl
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnevadoc committed Nov 29, 2023
1 parent 6e1bfe8 commit 3446308
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 35 deletions.
17 changes: 0 additions & 17 deletions src/pluto_eris/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,6 @@ impl MultiMillerLoop for Pluto {
}
}

// pub fn pairing(g1: &G1Affine, g2: &G2Affine) -> Gt {
// let g2 = G2Prepared::from_affine(*g2);
// let terms: &[(&G1Affine, &G2Prepared)] = &[(g1, &g2)];
// let u = multi_miller_loop(terms);
// u.final_exponentiation()
// }

#[derive(Clone, Debug)]
pub struct Pluto;

Expand All @@ -684,19 +677,9 @@ impl Engine for Pluto {
let terms: &[(&G1Affine, &G2Prepared)] = &[(p, &q)];
let u = Self::multi_miller_loop(terms);
u.final_exponentiation()
// pairing(p, q)
}
}

// impl MultiMillerLoop for Pluto {
// type G2Prepared = G2Prepared;
// type Result = Gt;

// fn multi_miller_loop(terms: &[(&Self::G1Affine, &Self::G2Prepared)]) -> Self::Result {
// multi_miller_loop(terms)
// }
// }

#[cfg(test)]
use rand::SeedableRng;
#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions src/pluto_eris/fields/fp.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::arithmetic::{adc, mac, sbb};
use crate::ff::{FromUniformBytes, PrimeField, WithSmallOrderMulGroup};
use crate::{
field_arithmetic_7_limbs, field_bits_7_limbs, field_common_7_limbs, impl_from_u64_7_limbs,
prime_field_legendre,
extend_field_legendre, field_arithmetic_7_limbs, field_bits_7_limbs, field_common_7_limbs,
impl_from_u64_7_limbs,
};
use crate::{
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
Expand Down Expand Up @@ -202,7 +202,7 @@ field_bits_7_limbs!(Fp, MODULUS);
#[cfg(not(target_pointer_width = "64"))]
field_bits_7_limbs!(Fp, MODULUS, MODULUS_LIMBS_32);

prime_field_legendre!(Fp);
extend_field_legendre!(Fp);

impl Fp {
pub const fn size() -> usize {
Expand Down
23 changes: 11 additions & 12 deletions src/pluto_eris/fields/fp2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::fp::{Fp, MODULUS_STR};
use crate::ff::{Field, FromUniformBytes, PrimeField, WithSmallOrderMulGroup};
use crate::legendre::Legendre;
use crate::ff_ext::Legendre;
use core::convert::TryInto;
use core::ops::{Add, Mul, Neg, Sub};
use rand::RngCore;
Expand Down Expand Up @@ -315,23 +315,22 @@ impl Fp2 {
tmp
})
}
}

impl Legendre for Fp2 {
type BasePrimeField = Fp;
fn legendre_exp() -> &'static [u64] {
Self::BasePrimeField::legendre_exp()
}

/// Norm of Fp2 as extension field in u over Fp
fn norm(&self) -> Self::BasePrimeField {
fn norm(&self) -> Fp {
// norm = self * self.cojungate()
let t0 = self.c0.square();
let t1 = self.c1.square() * U_SQUARE;
t1 - t0
}
}

impl Legendre for Fp2 {
fn legendre(&self) -> i64 {
self.norm().legendre()
}
}

impl Field for Fp2 {
const ZERO: Self = Self::zero();
const ONE: Self = Self::one();
Expand Down Expand Up @@ -699,7 +698,7 @@ pub fn test_sqrt() {
const N_ITER: usize = 1000;
for _ in 0..N_ITER {
let a = Fp2::random(&mut rng);
if a.legendre() == -Fp::ONE {
if a.legendre() == -1 {
assert!(bool::from(a.sqrt().is_none()));
}
}
Expand All @@ -708,7 +707,7 @@ pub fn test_sqrt() {
let a = Fp2::random(&mut rng);
let mut b = a;
b.square_assign();
assert_eq!(b.legendre(), Fp::ONE);
assert_eq!(b.legendre(), 1);

let b = b.sqrt().unwrap();
let mut negb = b;
Expand All @@ -721,7 +720,7 @@ pub fn test_sqrt() {
for _ in 0..N_ITER {
let mut b = c;
b.square_assign();
assert_eq!(b.legendre(), Fp::ONE);
assert_eq!(b.legendre(), 1);

b = b.sqrt().unwrap();

Expand Down
6 changes: 3 additions & 3 deletions src/pluto_eris/fields/fq.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::arithmetic::{adc, mac, sbb};
use crate::ff::{FromUniformBytes, PrimeField, WithSmallOrderMulGroup};
use crate::{
field_arithmetic_7_limbs, field_bits_7_limbs, field_common_7_limbs, impl_from_u64_7_limbs,
prime_field_legendre,
extend_field_legendre, field_arithmetic_7_limbs, field_bits_7_limbs, field_common_7_limbs,
impl_from_u64_7_limbs,
};
use crate::{
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
Expand Down Expand Up @@ -192,7 +192,7 @@ field_bits_7_limbs!(Fq, MODULUS);
#[cfg(not(target_pointer_width = "64"))]
field_bits_7_limbs!(Fq, MODULUS, MODULUS_LIMBS_32);

prime_field_legendre!(Fq);
extend_field_legendre!(Fq);

impl Fq {
/// Return field element size in bytes.
Expand Down
10 changes: 10 additions & 0 deletions src/pluto_eris/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ macro_rules! field_common_7_limbs {
$r
}

// Returns the Jacobi symbol, where the numerator and denominator
// are the element and the characteristic of the field, respectively.
// The Jacobi symbol is applicable to odd moduli
// while the Legendre symbol is applicable to prime moduli.
// They are equivalent for prime moduli.
#[inline(always)]
pub fn jacobi(&self) -> i64 {
$crate::ff_ext::jacobi::jacobi::<8>(&self.0, &$modulus.0)
}

fn from_u512(limbs: [u64; 8]) -> $field {
// We reduce an arbitrary 512-bit number by decomposing it into two 256-bit digits
// with the higher bits multiplied by 2^256. Thus, we perform two reductions
Expand Down

0 comments on commit 3446308

Please sign in to comment.