mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-01-31 07:42:15 +00:00
Merge pull request #190 from str4d/ff-more-ops
More ff::Field operator refactoring
This commit is contained in:
commit
e85a9f309f
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -80,6 +80,7 @@ dependencies = [
|
||||
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_xorshift 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"subtle 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -395,6 +396,7 @@ dependencies = [
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ff_derive 0.4.0",
|
||||
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"subtle 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -617,6 +619,7 @@ dependencies = [
|
||||
"group 0.2.0",
|
||||
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_xorshift 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"subtle 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1020,6 +1023,7 @@ dependencies = [
|
||||
"ripemd160 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"secp256k1 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"subtle 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -22,6 +22,7 @@ crossbeam = { version = "0.7", optional = true }
|
||||
pairing = { version = "0.15.0", path = "../pairing", optional = true }
|
||||
rand_core = "0.5"
|
||||
byteorder = "1"
|
||||
subtle = "2.2.1"
|
||||
|
||||
[dev-dependencies]
|
||||
hex-literal = "0.2"
|
||||
|
@ -63,7 +63,7 @@ impl<E: ScalarEngine, G: Group<E>> EvaluationDomain<E, G> {
|
||||
// Compute omega, the 2^exp primitive root of unity
|
||||
let mut omega = E::Fr::root_of_unity();
|
||||
for _ in exp..E::Fr::S {
|
||||
omega.square();
|
||||
omega = omega.square();
|
||||
}
|
||||
|
||||
// Extend the coeffs vector with zeroes if necessary
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Window table lookup gadgets.
|
||||
|
||||
use ff::{Field, ScalarEngine};
|
||||
use std::ops::AddAssign;
|
||||
use std::ops::{AddAssign, Neg};
|
||||
|
||||
use super::boolean::Boolean;
|
||||
use super::num::{AllocatedNum, Num};
|
||||
@ -16,8 +16,7 @@ where
|
||||
assert_eq!(assignment.len(), 1 << window_size);
|
||||
|
||||
for (i, constant) in constants.into_iter().enumerate() {
|
||||
let mut cur = assignment[i];
|
||||
cur.negate();
|
||||
let mut cur = assignment[i].neg();
|
||||
cur.add_assign(constant);
|
||||
assignment[i] = cur;
|
||||
for (j, eval) in assignment.iter_mut().enumerate().skip(i + 1) {
|
||||
@ -151,7 +150,7 @@ where
|
||||
let y = AllocatedNum::alloc(cs.namespace(|| "y"), || {
|
||||
let mut tmp = coords[*i.get()?].1;
|
||||
if *bits[2].get_value().get()? {
|
||||
tmp.negate();
|
||||
tmp = tmp.neg();
|
||||
}
|
||||
Ok(tmp)
|
||||
})?;
|
||||
@ -281,7 +280,7 @@ mod test {
|
||||
assert_eq!(res.0.get_value().unwrap(), points[index].0);
|
||||
let mut tmp = points[index].1;
|
||||
if c_val {
|
||||
tmp.negate()
|
||||
tmp = tmp.neg()
|
||||
}
|
||||
assert_eq!(res.1.get_value().unwrap(), tmp);
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ where
|
||||
for bit in bits {
|
||||
num = num.add_bool_with_coeff(CS::one(), bit, coeff);
|
||||
|
||||
coeff.double();
|
||||
coeff = coeff.double();
|
||||
}
|
||||
|
||||
let input = cs.alloc_input(|| format!("input {}", i), || Ok(*num.get_value().get()?))?;
|
||||
@ -63,7 +63,7 @@ pub fn compute_multipacking<E: ScalarEngine>(bits: &[bool]) -> Vec<E::Fr> {
|
||||
cur.add_assign(&coeff);
|
||||
}
|
||||
|
||||
coeff.double();
|
||||
coeff = coeff.double();
|
||||
}
|
||||
|
||||
result.push(cur);
|
||||
|
@ -177,7 +177,7 @@ impl<E: ScalarEngine> AllocatedNum<E> {
|
||||
for bit in result.iter().rev() {
|
||||
lc = lc + (coeff, bit.get_variable());
|
||||
|
||||
coeff.double();
|
||||
coeff = coeff.double();
|
||||
}
|
||||
|
||||
lc = lc - self.variable;
|
||||
@ -203,7 +203,7 @@ impl<E: ScalarEngine> AllocatedNum<E> {
|
||||
for bit in bits.iter() {
|
||||
lc = lc + (coeff, bit.get_variable());
|
||||
|
||||
coeff.double();
|
||||
coeff = coeff.double();
|
||||
}
|
||||
|
||||
lc = lc - self.variable;
|
||||
@ -254,8 +254,7 @@ impl<E: ScalarEngine> AllocatedNum<E> {
|
||||
let var = cs.alloc(
|
||||
|| "squared num",
|
||||
|| {
|
||||
let mut tmp = *self.value.get()?;
|
||||
tmp.square();
|
||||
let tmp = self.value.get()?.square();
|
||||
|
||||
value = Some(tmp);
|
||||
|
||||
@ -417,7 +416,7 @@ mod test {
|
||||
use pairing::bls12_381::{Bls12, Fr};
|
||||
use rand_core::SeedableRng;
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use std::ops::SubAssign;
|
||||
use std::ops::{Neg, SubAssign};
|
||||
|
||||
use super::{AllocatedNum, Boolean};
|
||||
use crate::gadgets::test::*;
|
||||
@ -519,8 +518,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_into_bits_strict() {
|
||||
let mut negone = Fr::one();
|
||||
negone.negate();
|
||||
let negone = Fr::one().neg();
|
||||
|
||||
let mut cs = TestConstraintSystem::<Bls12>::new();
|
||||
|
||||
|
@ -6,7 +6,7 @@ use crate::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Write;
|
||||
use std::ops::{AddAssign, MulAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg};
|
||||
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
use std::cmp::Ordering;
|
||||
@ -152,11 +152,7 @@ impl<E: ScalarEngine> TestConstraintSystem<E> {
|
||||
pub fn pretty_print(&self) -> String {
|
||||
let mut s = String::new();
|
||||
|
||||
let negone = {
|
||||
let mut tmp = E::Fr::one();
|
||||
tmp.negate();
|
||||
tmp
|
||||
};
|
||||
let negone = E::Fr::one().neg();
|
||||
|
||||
let powers_of_two = (0..E::Fr::NUM_BITS)
|
||||
.map(|i| E::Fr::from_str("2").unwrap().pow(&[u64::from(i)]))
|
||||
|
@ -330,7 +330,7 @@ impl UInt32 {
|
||||
|
||||
all_constants &= bit.is_constant();
|
||||
|
||||
coeff.double();
|
||||
coeff = coeff.double();
|
||||
}
|
||||
}
|
||||
|
||||
@ -368,7 +368,7 @@ impl UInt32 {
|
||||
|
||||
max_value >>= 1;
|
||||
i += 1;
|
||||
coeff.double();
|
||||
coeff = coeff.double();
|
||||
}
|
||||
|
||||
// Enforce equality between the sum and result
|
||||
|
@ -9,7 +9,8 @@ use rand_core::RngCore;
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt;
|
||||
use std::num::Wrapping;
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
use subtle::{Choice, ConditionallySelectable};
|
||||
|
||||
const MODULUS_R: Wrapping<u32> = Wrapping(64513);
|
||||
|
||||
@ -22,6 +23,27 @@ impl fmt::Display for Fr {
|
||||
}
|
||||
}
|
||||
|
||||
impl ConditionallySelectable for Fr {
|
||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||
Fr(Wrapping(u32::conditional_select(
|
||||
&(a.0).0,
|
||||
&(b.0).0,
|
||||
choice,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Fr {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(mut self) -> Self {
|
||||
if !<Fr as Field>::is_zero(&self) {
|
||||
self.0 = MODULUS_R - self.0;
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Add<&'r Fr> for Fr {
|
||||
type Output = Self;
|
||||
|
||||
@ -129,18 +151,12 @@ impl Field for Fr {
|
||||
(self.0).0 == 0
|
||||
}
|
||||
|
||||
fn square(&mut self) {
|
||||
self.0 = (self.0 * self.0) % MODULUS_R;
|
||||
fn square(&self) -> Self {
|
||||
Fr((self.0 * self.0) % MODULUS_R)
|
||||
}
|
||||
|
||||
fn double(&mut self) {
|
||||
self.0 = (self.0 << 1) % MODULUS_R;
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
if !<Fr as Field>::is_zero(self) {
|
||||
self.0 = MODULUS_R - self.0;
|
||||
}
|
||||
fn double(&self) -> Self {
|
||||
Fr((self.0 << 1) % MODULUS_R)
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Option<Self> {
|
||||
@ -186,22 +202,21 @@ impl SqrtField for Fr {
|
||||
while t != <Fr as Field>::one() {
|
||||
let mut i = 1;
|
||||
{
|
||||
let mut t2i = t;
|
||||
t2i.square();
|
||||
let mut t2i = t.square();
|
||||
loop {
|
||||
if t2i == <Fr as Field>::one() {
|
||||
break;
|
||||
}
|
||||
t2i.square();
|
||||
t2i = t2i.square();
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for _ in 0..(m - i - 1) {
|
||||
c.square();
|
||||
c = c.square();
|
||||
}
|
||||
MulAssign::mul_assign(&mut r, &c);
|
||||
c.square();
|
||||
c = c.square();
|
||||
MulAssign::mul_assign(&mut t, &c);
|
||||
m = i;
|
||||
}
|
||||
@ -401,7 +416,7 @@ impl CurveProjective for Fr {
|
||||
}
|
||||
|
||||
fn double(&mut self) {
|
||||
<Fr as Field>::double(self);
|
||||
self.0 = <Fr as Field>::double(self).0;
|
||||
}
|
||||
|
||||
fn add_assign(&mut self, other: &Self) {
|
||||
@ -413,7 +428,7 @@ impl CurveProjective for Fr {
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
<Fr as Field>::negate(self);
|
||||
self.0 = self.neg().0;
|
||||
}
|
||||
|
||||
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S) {
|
||||
@ -495,7 +510,7 @@ impl CurveAffine for Fr {
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
<Fr as Field>::negate(self);
|
||||
self.0 = self.neg().0;
|
||||
}
|
||||
|
||||
fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, other: S) -> Self::Projective {
|
||||
|
@ -148,7 +148,7 @@ use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::{Add, MulAssign, Sub};
|
||||
use std::ops::{Add, MulAssign, Neg, Sub};
|
||||
|
||||
/// Computations are expressed in terms of arithmetic circuits, in particular
|
||||
/// rank-1 quadratic constraint systems. The `Circuit` trait represents a
|
||||
@ -216,10 +216,8 @@ impl<E: ScalarEngine> Sub<(E::Fr, Variable)> for LinearCombination<E> {
|
||||
type Output = LinearCombination<E>;
|
||||
|
||||
#[allow(clippy::suspicious_arithmetic_impl)]
|
||||
fn sub(self, (mut coeff, var): (E::Fr, Variable)) -> LinearCombination<E> {
|
||||
coeff.negate();
|
||||
|
||||
self + (coeff, var)
|
||||
fn sub(self, (coeff, var): (E::Fr, Variable)) -> LinearCombination<E> {
|
||||
self + (coeff.neg(), var)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,8 +41,7 @@ fn mimc<E: Engine>(mut xl: E::Fr, mut xr: E::Fr, constants: &[E::Fr]) -> E::Fr {
|
||||
for i in 0..MIMC_ROUNDS {
|
||||
let mut tmp1 = xl;
|
||||
tmp1.add_assign(&constants[i]);
|
||||
let mut tmp2 = tmp1;
|
||||
tmp2.square();
|
||||
let mut tmp2 = tmp1.square();
|
||||
tmp2.mul_assign(&tmp1);
|
||||
tmp2.add_assign(&xr);
|
||||
xr = xl;
|
||||
@ -88,8 +87,7 @@ impl<'a, E: Engine> Circuit<E> for MiMCDemo<'a, E> {
|
||||
// tmp = (xL + Ci)^2
|
||||
let tmp_value = xl_value.map(|mut e| {
|
||||
e.add_assign(&self.constants[i]);
|
||||
e.square();
|
||||
e
|
||||
e.square()
|
||||
});
|
||||
let tmp = cs.alloc(
|
||||
|| "tmp",
|
||||
|
@ -14,6 +14,7 @@ edition = "2018"
|
||||
byteorder = "1"
|
||||
ff_derive = { version = "0.4.0", path = "ff_derive", optional = true }
|
||||
rand_core = "0.5"
|
||||
subtle = "2.2.1"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
@ -447,8 +447,7 @@ fn prime_field_constants_and_sqrt(
|
||||
|
||||
let mut a1 = self.pow(#mod_minus_3_over_4);
|
||||
|
||||
let mut a0 = a1;
|
||||
a0.square();
|
||||
let mut a0 = a1.square();
|
||||
a0.mul_assign(self);
|
||||
|
||||
if a0.0 == #repr(#rneg) {
|
||||
@ -484,22 +483,21 @@ fn prime_field_constants_and_sqrt(
|
||||
while t != Self::one() {
|
||||
let mut i = 1;
|
||||
{
|
||||
let mut t2i = t;
|
||||
t2i.square();
|
||||
let mut t2i = t.square();
|
||||
loop {
|
||||
if t2i == Self::one() {
|
||||
break;
|
||||
}
|
||||
t2i.square();
|
||||
t2i = t2i.square();
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for _ in 0..(m - i - 1) {
|
||||
c.square();
|
||||
c = c.square();
|
||||
}
|
||||
r.mul_assign(&c);
|
||||
c.square();
|
||||
c = c.square();
|
||||
t.mul_assign(&c);
|
||||
m = i;
|
||||
}
|
||||
@ -715,7 +713,9 @@ fn prime_field_impl(
|
||||
);
|
||||
|
||||
gen.extend(quote! {
|
||||
self.mont_reduce(#mont_calling);
|
||||
let mut ret = *self;
|
||||
ret.mont_reduce(#mont_calling);
|
||||
ret
|
||||
});
|
||||
|
||||
gen
|
||||
@ -833,6 +833,31 @@ fn prime_field_impl(
|
||||
}
|
||||
}
|
||||
|
||||
impl ::subtle::ConditionallySelectable for #name {
|
||||
fn conditional_select(a: &#name, b: &#name, choice: ::subtle::Choice) -> #name {
|
||||
let mut res = [0u64; #limbs];
|
||||
for i in 0..#limbs {
|
||||
res[i] = u64::conditional_select(&(a.0).0[i], &(b.0).0[i], choice);
|
||||
}
|
||||
#name(#repr(res))
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::ops::Neg for #name {
|
||||
type Output = #name;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> #name {
|
||||
let mut ret = self;
|
||||
if !ret.is_zero() {
|
||||
let mut tmp = MODULUS;
|
||||
tmp.sub_noborrow(&ret.0);
|
||||
ret.0 = tmp;
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> ::std::ops::Add<&'r #name> for #name {
|
||||
type Output = #name;
|
||||
|
||||
@ -1025,21 +1050,16 @@ fn prime_field_impl(
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn double(&mut self) {
|
||||
fn double(&self) -> Self {
|
||||
let mut ret = *self;
|
||||
|
||||
// This cannot exceed the backing capacity.
|
||||
self.0.mul2();
|
||||
ret.0.mul2();
|
||||
|
||||
// However, it may need to be reduced.
|
||||
self.reduce();
|
||||
}
|
||||
ret.reduce();
|
||||
|
||||
#[inline]
|
||||
fn negate(&mut self) {
|
||||
if !self.is_zero() {
|
||||
let mut tmp = MODULUS;
|
||||
tmp.sub_noborrow(&self.0);
|
||||
self.0 = tmp;
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Option<Self> {
|
||||
@ -1103,7 +1123,7 @@ fn prime_field_impl(
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn square(&mut self)
|
||||
fn square(&self) -> Self
|
||||
{
|
||||
#squaring_impl
|
||||
}
|
||||
|
@ -11,7 +11,8 @@ use rand_core::RngCore;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
use subtle::ConditionallySelectable;
|
||||
|
||||
/// This trait represents an element of a field.
|
||||
pub trait Field:
|
||||
@ -24,9 +25,11 @@ pub trait Field:
|
||||
+ fmt::Debug
|
||||
+ fmt::Display
|
||||
+ 'static
|
||||
+ ConditionallySelectable
|
||||
+ Add<Output = Self>
|
||||
+ Sub<Output = Self>
|
||||
+ Mul<Output = Self>
|
||||
+ Neg<Output = Self>
|
||||
+ for<'a> Add<&'a Self, Output = Self>
|
||||
+ for<'a> Mul<&'a Self, Output = Self>
|
||||
+ for<'a> Sub<&'a Self, Output = Self>
|
||||
@ -50,13 +53,12 @@ pub trait Field:
|
||||
fn is_zero(&self) -> bool;
|
||||
|
||||
/// Squares this element.
|
||||
fn square(&mut self);
|
||||
#[must_use]
|
||||
fn square(&self) -> Self;
|
||||
|
||||
/// Doubles this element.
|
||||
fn double(&mut self);
|
||||
|
||||
/// Negates this element.
|
||||
fn negate(&mut self);
|
||||
#[must_use]
|
||||
fn double(&self) -> Self;
|
||||
|
||||
/// Computes the multiplicative inverse of this element, if nonzero.
|
||||
fn inverse(&self) -> Option<Self>;
|
||||
@ -74,7 +76,7 @@ pub trait Field:
|
||||
|
||||
for i in BitIterator::new(exp) {
|
||||
if found_one {
|
||||
res.square();
|
||||
res = res.square();
|
||||
} else {
|
||||
found_one = i;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
use ff::{Field, PrimeField};
|
||||
use rand::SeedableRng;
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use std::ops::Neg;
|
||||
|
||||
use crate::{CurveAffine, CurveProjective, EncodedPoint};
|
||||
|
||||
@ -199,8 +200,7 @@ fn random_negation_tests<G: CurveProjective>() {
|
||||
let r = G::random(&mut rng);
|
||||
|
||||
let s = G::Scalar::random(&mut rng);
|
||||
let mut sneg = s;
|
||||
sneg.negate();
|
||||
let sneg = s.neg();
|
||||
|
||||
let mut t1 = r;
|
||||
t1.mul_assign(s);
|
||||
|
@ -21,6 +21,7 @@ byteorder = "1"
|
||||
ff = { version = "0.5.0", path = "../ff", features = ["derive"] }
|
||||
group = { version = "0.2.0", path = "../group" }
|
||||
rand_core = "0.5"
|
||||
subtle = "2.2.1"
|
||||
|
||||
[dev-dependencies]
|
||||
rand_xorshift = "0.2"
|
||||
|
@ -1,6 +1,6 @@
|
||||
use rand_core::SeedableRng;
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use ff::{Field, PrimeField, PrimeFieldRepr, SqrtField};
|
||||
use pairing::bls12_381::*;
|
||||
@ -210,8 +210,7 @@ fn bench_fq_square(b: &mut ::test::Bencher) {
|
||||
|
||||
let mut count = 0;
|
||||
b.iter(|| {
|
||||
let mut tmp = v[count];
|
||||
tmp.square();
|
||||
let tmp = v[count].square();
|
||||
count = (count + 1) % SAMPLES;
|
||||
tmp
|
||||
});
|
||||
@ -236,7 +235,7 @@ fn bench_fq_inverse(b: &mut ::test::Bencher) {
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_fq_negate(b: &mut ::test::Bencher) {
|
||||
fn bench_fq_neg(b: &mut ::test::Bencher) {
|
||||
const SAMPLES: usize = 1000;
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([
|
||||
@ -248,8 +247,7 @@ fn bench_fq_negate(b: &mut ::test::Bencher) {
|
||||
|
||||
let mut count = 0;
|
||||
b.iter(|| {
|
||||
let mut tmp = v[count];
|
||||
tmp.negate();
|
||||
let tmp = v[count].neg();
|
||||
count = (count + 1) % SAMPLES;
|
||||
tmp
|
||||
});
|
||||
@ -265,11 +263,7 @@ fn bench_fq_sqrt(b: &mut ::test::Bencher) {
|
||||
]);
|
||||
|
||||
let v: Vec<Fq> = (0..SAMPLES)
|
||||
.map(|_| {
|
||||
let mut tmp = Fq::random(&mut rng);
|
||||
tmp.square();
|
||||
tmp
|
||||
})
|
||||
.map(|_| Fq::random(&mut rng).square())
|
||||
.collect();
|
||||
|
||||
let mut count = 0;
|
||||
|
@ -84,8 +84,7 @@ fn bench_fq12_squaring(b: &mut ::test::Bencher) {
|
||||
|
||||
let mut count = 0;
|
||||
b.iter(|| {
|
||||
let mut tmp = v[count];
|
||||
tmp.square();
|
||||
let tmp = v[count].square();
|
||||
count = (count + 1) % SAMPLES;
|
||||
tmp
|
||||
});
|
||||
|
@ -84,8 +84,7 @@ fn bench_fq2_squaring(b: &mut ::test::Bencher) {
|
||||
|
||||
let mut count = 0;
|
||||
b.iter(|| {
|
||||
let mut tmp = v[count];
|
||||
tmp.square();
|
||||
let tmp = v[count].square();
|
||||
count = (count + 1) % SAMPLES;
|
||||
tmp
|
||||
});
|
||||
|
@ -1,6 +1,6 @@
|
||||
use rand_core::SeedableRng;
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use ff::{Field, PrimeField, PrimeFieldRepr, SqrtField};
|
||||
use pairing::bls12_381::*;
|
||||
@ -210,8 +210,7 @@ fn bench_fr_square(b: &mut ::test::Bencher) {
|
||||
|
||||
let mut count = 0;
|
||||
b.iter(|| {
|
||||
let mut tmp = v[count];
|
||||
tmp.square();
|
||||
let tmp = v[count].square();
|
||||
count = (count + 1) % SAMPLES;
|
||||
tmp
|
||||
});
|
||||
@ -236,7 +235,7 @@ fn bench_fr_inverse(b: &mut ::test::Bencher) {
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_fr_negate(b: &mut ::test::Bencher) {
|
||||
fn bench_fr_neg(b: &mut ::test::Bencher) {
|
||||
const SAMPLES: usize = 1000;
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([
|
||||
@ -248,8 +247,7 @@ fn bench_fr_negate(b: &mut ::test::Bencher) {
|
||||
|
||||
let mut count = 0;
|
||||
b.iter(|| {
|
||||
let mut tmp = v[count];
|
||||
tmp.negate();
|
||||
let tmp = v[count].neg();
|
||||
count = (count + 1) % SAMPLES;
|
||||
tmp
|
||||
});
|
||||
@ -265,11 +263,7 @@ fn bench_fr_sqrt(b: &mut ::test::Bencher) {
|
||||
]);
|
||||
|
||||
let v: Vec<Fr> = (0..SAMPLES)
|
||||
.map(|_| {
|
||||
let mut tmp = Fr::random(&mut rng);
|
||||
tmp.square();
|
||||
tmp
|
||||
})
|
||||
.map(|_| Fr::random(&mut rng).square())
|
||||
.collect();
|
||||
|
||||
let mut count = 0;
|
||||
|
@ -54,10 +54,8 @@ macro_rules! curve_impl {
|
||||
// are equal when (X * Z^2) = (X' * Z'^2)
|
||||
// and (Y * Z^3) = (Y' * Z'^3).
|
||||
|
||||
let mut z1 = self.z;
|
||||
z1.square();
|
||||
let mut z2 = other.z;
|
||||
z2.square();
|
||||
let mut z1 = self.z.square();
|
||||
let mut z2 = other.z.square();
|
||||
|
||||
let mut tmp1 = self.x;
|
||||
tmp1.mul_assign(&z2);
|
||||
@ -101,14 +99,12 @@ macro_rules! curve_impl {
|
||||
/// largest y-coordinate be selected.
|
||||
fn get_point_from_x(x: $basefield, greatest: bool) -> Option<$affine> {
|
||||
// Compute x^3 + b
|
||||
let mut x3b = x;
|
||||
x3b.square();
|
||||
let mut x3b = x.square();
|
||||
x3b.mul_assign(&x);
|
||||
x3b.add_assign(&$affine::get_coeff_b());
|
||||
|
||||
x3b.sqrt().map(|y| {
|
||||
let mut negy = y;
|
||||
negy.negate();
|
||||
let negy = y.neg();
|
||||
|
||||
$affine {
|
||||
x: x,
|
||||
@ -123,11 +119,9 @@ macro_rules! curve_impl {
|
||||
true
|
||||
} else {
|
||||
// Check that the point is on the curve
|
||||
let mut y2 = self.y;
|
||||
y2.square();
|
||||
let y2 = self.y.square();
|
||||
|
||||
let mut x3b = self.x;
|
||||
x3b.square();
|
||||
let mut x3b = self.x.square();
|
||||
x3b.mul_assign(&self.x);
|
||||
x3b.add_assign(&Self::get_coeff_b());
|
||||
|
||||
@ -171,7 +165,7 @@ macro_rules! curve_impl {
|
||||
|
||||
fn negate(&mut self) {
|
||||
if !self.is_zero() {
|
||||
self.y.negate();
|
||||
self.y = self.y.neg();
|
||||
}
|
||||
}
|
||||
|
||||
@ -284,8 +278,7 @@ macro_rules! curve_impl {
|
||||
|
||||
// Perform affine transformations
|
||||
for g in v.iter_mut().filter(|g| !g.is_normalized()) {
|
||||
let mut z = g.z; // 1/z
|
||||
z.square(); // 1/z^2
|
||||
let mut z = g.z.square(); // 1/z^2
|
||||
g.x.mul_assign(&z); // x/z^2
|
||||
z.mul_assign(&g.z); // 1/z^3
|
||||
g.y.mul_assign(&z); // y/z^3
|
||||
@ -306,37 +299,32 @@ macro_rules! curve_impl {
|
||||
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
|
||||
|
||||
// A = X1^2
|
||||
let mut a = self.x;
|
||||
a.square();
|
||||
let a = self.x.square();
|
||||
|
||||
// B = Y1^2
|
||||
let mut b = self.y;
|
||||
b.square();
|
||||
let b = self.y.square();
|
||||
|
||||
// C = B^2
|
||||
let mut c = b;
|
||||
c.square();
|
||||
let mut c = b.square();
|
||||
|
||||
// D = 2*((X1+B)2-A-C)
|
||||
let mut d = self.x;
|
||||
d.add_assign(&b);
|
||||
d.square();
|
||||
d = d.square();
|
||||
d.sub_assign(&a);
|
||||
d.sub_assign(&c);
|
||||
d.double();
|
||||
d = d.double();
|
||||
|
||||
// E = 3*A
|
||||
let mut e = a;
|
||||
e.double();
|
||||
let mut e = a.double();
|
||||
e.add_assign(&a);
|
||||
|
||||
// F = E^2
|
||||
let mut f = e;
|
||||
f.square();
|
||||
let f = e.square();
|
||||
|
||||
// Z3 = 2*Y1*Z1
|
||||
self.z.mul_assign(&self.y);
|
||||
self.z.double();
|
||||
self.z = self.z.double();
|
||||
|
||||
// X3 = F-2*D
|
||||
self.x = f;
|
||||
@ -347,9 +335,7 @@ macro_rules! curve_impl {
|
||||
self.y = d;
|
||||
self.y.sub_assign(&self.x);
|
||||
self.y.mul_assign(&e);
|
||||
c.double();
|
||||
c.double();
|
||||
c.double();
|
||||
c = c.double().double().double();
|
||||
self.y.sub_assign(&c);
|
||||
}
|
||||
|
||||
@ -366,12 +352,10 @@ macro_rules! curve_impl {
|
||||
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
|
||||
|
||||
// Z1Z1 = Z1^2
|
||||
let mut z1z1 = self.z;
|
||||
z1z1.square();
|
||||
let z1z1 = self.z.square();
|
||||
|
||||
// Z2Z2 = Z2^2
|
||||
let mut z2z2 = other.z;
|
||||
z2z2.square();
|
||||
let z2z2 = other.z.square();
|
||||
|
||||
// U1 = X1*Z2Z2
|
||||
let mut u1 = self.x;
|
||||
@ -402,9 +386,7 @@ macro_rules! curve_impl {
|
||||
h.sub_assign(&u1);
|
||||
|
||||
// I = (2*H)^2
|
||||
let mut i = h;
|
||||
i.double();
|
||||
i.square();
|
||||
let i = h.double().square();
|
||||
|
||||
// J = H*I
|
||||
let mut j = h;
|
||||
@ -413,15 +395,14 @@ macro_rules! curve_impl {
|
||||
// r = 2*(S2-S1)
|
||||
let mut r = s2;
|
||||
r.sub_assign(&s1);
|
||||
r.double();
|
||||
r = r.double();
|
||||
|
||||
// V = U1*I
|
||||
let mut v = u1;
|
||||
v.mul_assign(&i);
|
||||
|
||||
// X3 = r^2 - J - 2*V
|
||||
self.x = r;
|
||||
self.x.square();
|
||||
self.x = r.square();
|
||||
self.x.sub_assign(&j);
|
||||
self.x.sub_assign(&v);
|
||||
self.x.sub_assign(&v);
|
||||
@ -431,12 +412,12 @@ macro_rules! curve_impl {
|
||||
self.y.sub_assign(&self.x);
|
||||
self.y.mul_assign(&r);
|
||||
s1.mul_assign(&j); // S1 = S1 * J * 2
|
||||
s1.double();
|
||||
s1 = s1.double();
|
||||
self.y.sub_assign(&s1);
|
||||
|
||||
// Z3 = ((Z1+Z2)^2 - Z1Z1 - Z2Z2)*H
|
||||
self.z.add_assign(&other.z);
|
||||
self.z.square();
|
||||
self.z = self.z.square();
|
||||
self.z.sub_assign(&z1z1);
|
||||
self.z.sub_assign(&z2z2);
|
||||
self.z.mul_assign(&h);
|
||||
@ -458,8 +439,7 @@ macro_rules! curve_impl {
|
||||
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
|
||||
|
||||
// Z1Z1 = Z1^2
|
||||
let mut z1z1 = self.z;
|
||||
z1z1.square();
|
||||
let z1z1 = self.z.square();
|
||||
|
||||
// U2 = X2*Z1Z1
|
||||
let mut u2 = other.x;
|
||||
@ -481,13 +461,10 @@ macro_rules! curve_impl {
|
||||
h.sub_assign(&self.x);
|
||||
|
||||
// HH = H^2
|
||||
let mut hh = h;
|
||||
hh.square();
|
||||
let hh = h.square();
|
||||
|
||||
// I = 4*HH
|
||||
let mut i = hh;
|
||||
i.double();
|
||||
i.double();
|
||||
let i = hh.double().double();
|
||||
|
||||
// J = H*I
|
||||
let mut j = h;
|
||||
@ -496,22 +473,21 @@ macro_rules! curve_impl {
|
||||
// r = 2*(S2-Y1)
|
||||
let mut r = s2;
|
||||
r.sub_assign(&self.y);
|
||||
r.double();
|
||||
r = r.double();
|
||||
|
||||
// V = X1*I
|
||||
let mut v = self.x;
|
||||
v.mul_assign(&i);
|
||||
|
||||
// X3 = r^2 - J - 2*V
|
||||
self.x = r;
|
||||
self.x.square();
|
||||
self.x = r.square();
|
||||
self.x.sub_assign(&j);
|
||||
self.x.sub_assign(&v);
|
||||
self.x.sub_assign(&v);
|
||||
|
||||
// Y3 = r*(V-X3)-2*Y1*J
|
||||
j.mul_assign(&self.y); // J = 2*Y1*J
|
||||
j.double();
|
||||
j = j.double();
|
||||
self.y = v;
|
||||
self.y.sub_assign(&self.x);
|
||||
self.y.mul_assign(&r);
|
||||
@ -519,7 +495,7 @@ macro_rules! curve_impl {
|
||||
|
||||
// Z3 = (Z1+H)^2-Z1Z1-HH
|
||||
self.z.add_assign(&h);
|
||||
self.z.square();
|
||||
self.z = self.z.square();
|
||||
self.z.sub_assign(&z1z1);
|
||||
self.z.sub_assign(&hh);
|
||||
}
|
||||
@ -527,7 +503,7 @@ macro_rules! curve_impl {
|
||||
|
||||
fn negate(&mut self) {
|
||||
if !self.is_zero() {
|
||||
self.y.negate()
|
||||
self.y = self.y.neg();
|
||||
}
|
||||
}
|
||||
|
||||
@ -596,8 +572,7 @@ macro_rules! curve_impl {
|
||||
} else {
|
||||
// Z is nonzero, so it must have an inverse in a field.
|
||||
let zinv = p.z.inverse().unwrap();
|
||||
let mut zinv_powered = zinv;
|
||||
zinv_powered.square();
|
||||
let mut zinv_powered = zinv.square();
|
||||
|
||||
// X/Z^2
|
||||
let mut x = p.x;
|
||||
@ -627,7 +602,7 @@ pub mod g1 {
|
||||
use group::{CurveAffine, CurveProjective, EncodedPoint, GroupDecodingError};
|
||||
use rand_core::RngCore;
|
||||
use std::fmt;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
curve_impl!(
|
||||
"G1",
|
||||
@ -849,8 +824,7 @@ pub mod g1 {
|
||||
affine.x.into_repr().write_be(&mut writer).unwrap();
|
||||
}
|
||||
|
||||
let mut negy = affine.y;
|
||||
negy.negate();
|
||||
let negy = affine.y.neg();
|
||||
|
||||
// Set the third most significant bit if the correct y-coordinate
|
||||
// is lexicographically largest.
|
||||
@ -941,15 +915,13 @@ pub mod g1 {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
// y^2 = x^3 + b
|
||||
let mut rhs = x;
|
||||
rhs.square();
|
||||
let mut rhs = x.square();
|
||||
rhs.mul_assign(&x);
|
||||
rhs.add_assign(&G1Affine::get_coeff_b());
|
||||
|
||||
if let Some(y) = rhs.sqrt() {
|
||||
let yrepr = y.into_repr();
|
||||
let mut negy = y;
|
||||
negy.negate();
|
||||
let negy = y.neg();
|
||||
let negyrepr = negy.into_repr();
|
||||
|
||||
let p = G1Affine {
|
||||
@ -1297,7 +1269,7 @@ pub mod g2 {
|
||||
use group::{CurveAffine, CurveProjective, EncodedPoint, GroupDecodingError};
|
||||
use rand_core::RngCore;
|
||||
use std::fmt;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
curve_impl!(
|
||||
"G2",
|
||||
@ -1544,8 +1516,7 @@ pub mod g2 {
|
||||
affine.x.c0.into_repr().write_be(&mut writer).unwrap();
|
||||
}
|
||||
|
||||
let mut negy = affine.y;
|
||||
negy.negate();
|
||||
let negy = affine.y.neg();
|
||||
|
||||
// Set the third most significant bit if the correct y-coordinate
|
||||
// is lexicographically largest.
|
||||
@ -1648,14 +1619,12 @@ pub mod g2 {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
// y^2 = x^3 + b
|
||||
let mut rhs = x;
|
||||
rhs.square();
|
||||
let mut rhs = x.square();
|
||||
rhs.mul_assign(&x);
|
||||
rhs.add_assign(&G2Affine::get_coeff_b());
|
||||
|
||||
if let Some(y) = rhs.sqrt() {
|
||||
let mut negy = y;
|
||||
negy.negate();
|
||||
let negy = y.neg();
|
||||
|
||||
let p = G2Affine {
|
||||
x,
|
||||
|
@ -2,6 +2,9 @@ use super::fq2::Fq2;
|
||||
use ff::{Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr};
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
|
||||
#[cfg(test)]
|
||||
use std::ops::Neg;
|
||||
|
||||
// B coefficient of BLS12-381 curve, 4.
|
||||
pub const B_COEFF: Fq = Fq(FqRepr([
|
||||
0xaa270000000cfff3,
|
||||
@ -456,8 +459,7 @@ fn test_b_coeff() {
|
||||
|
||||
#[test]
|
||||
fn test_frob_coeffs() {
|
||||
let mut nqr = Fq::one();
|
||||
nqr.negate();
|
||||
let nqr = Fq::one().neg();
|
||||
|
||||
assert_eq!(FROBENIUS_COEFF_FQ2_C1[0], Fq::one());
|
||||
assert_eq!(
|
||||
@ -1167,8 +1169,7 @@ fn test_frob_coeffs() {
|
||||
|
||||
#[test]
|
||||
fn test_neg_one() {
|
||||
let mut o = Fq::one();
|
||||
o.negate();
|
||||
let o = Fq::one().neg();
|
||||
|
||||
assert_eq!(NEGATIVE_ONE, o);
|
||||
}
|
||||
@ -1929,7 +1930,7 @@ fn test_fq_mul_assign() {
|
||||
|
||||
#[test]
|
||||
fn test_fq_squaring() {
|
||||
let mut a = Fq(FqRepr([
|
||||
let a = Fq(FqRepr([
|
||||
0xffffffffffffffff,
|
||||
0xffffffffffffffff,
|
||||
0xffffffffffffffff,
|
||||
@ -1938,9 +1939,8 @@ fn test_fq_squaring() {
|
||||
0x19ffffffffffffff,
|
||||
]));
|
||||
assert!(a.is_valid());
|
||||
a.square();
|
||||
assert_eq!(
|
||||
a,
|
||||
a.square(),
|
||||
Fq::from_repr(FqRepr([
|
||||
0x1cfb28fe7dfbbb86,
|
||||
0x24cbe1731577a59,
|
||||
@ -1960,14 +1960,7 @@ fn test_fq_squaring() {
|
||||
for _ in 0..1000000 {
|
||||
// Ensure that (a * a) = a^2
|
||||
let a = Fq::random(&mut rng);
|
||||
|
||||
let mut tmp = a;
|
||||
tmp.square();
|
||||
|
||||
let mut tmp2 = a;
|
||||
tmp2.mul_assign(&a);
|
||||
|
||||
assert_eq!(tmp, tmp2);
|
||||
assert_eq!(a.square(), a * a);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2000,19 +1993,15 @@ fn test_fq_double() {
|
||||
|
||||
for _ in 0..1000 {
|
||||
// Ensure doubling a is equivalent to adding a to itself.
|
||||
let mut a = Fq::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.add_assign(&a);
|
||||
a.double();
|
||||
assert_eq!(a, b);
|
||||
let a = Fq::random(&mut rng);
|
||||
assert_eq!(a.double(), a + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fq_negate() {
|
||||
fn test_fq_neg() {
|
||||
{
|
||||
let mut a = Fq::zero();
|
||||
a.negate();
|
||||
let a = Fq::zero().neg();
|
||||
|
||||
assert!(a.is_zero());
|
||||
}
|
||||
@ -2025,8 +2014,7 @@ fn test_fq_negate() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure (a - (-a)) = 0.
|
||||
let mut a = Fq::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.negate();
|
||||
let b = a.neg();
|
||||
a.add_assign(&b);
|
||||
|
||||
assert!(a.is_zero());
|
||||
@ -2074,10 +2062,8 @@ fn test_fq_sqrt() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure sqrt(a^2) = a or -a
|
||||
let a = Fq::random(&mut rng);
|
||||
let mut nega = a;
|
||||
nega.negate();
|
||||
let mut b = a;
|
||||
b.square();
|
||||
let nega = a.neg();
|
||||
let b = a.square();
|
||||
|
||||
let b = b.sqrt().unwrap();
|
||||
|
||||
@ -2088,10 +2074,8 @@ fn test_fq_sqrt() {
|
||||
// Ensure sqrt(a)^2 = a for random a
|
||||
let a = Fq::random(&mut rng);
|
||||
|
||||
if let Some(mut tmp) = a.sqrt() {
|
||||
tmp.square();
|
||||
|
||||
assert_eq!(a, tmp);
|
||||
if let Some(tmp) = a.sqrt() {
|
||||
assert_eq!(a, tmp.square());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,8 @@ use super::fq2::Fq2;
|
||||
use super::fq6::Fq6;
|
||||
use ff::Field;
|
||||
use rand_core::RngCore;
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
use subtle::{Choice, ConditionallySelectable};
|
||||
|
||||
/// An element of Fq12, represented by c0 + c1 * w.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
@ -20,7 +21,7 @@ impl ::std::fmt::Display for Fq12 {
|
||||
|
||||
impl Fq12 {
|
||||
pub fn conjugate(&mut self) {
|
||||
self.c1.negate();
|
||||
self.c1 = self.c1.neg();
|
||||
}
|
||||
|
||||
pub fn mul_by_014(&mut self, c0: &Fq2, c1: &Fq2, c4: &Fq2) {
|
||||
@ -40,6 +41,26 @@ impl Fq12 {
|
||||
}
|
||||
}
|
||||
|
||||
impl ConditionallySelectable for Fq12 {
|
||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||
Fq12 {
|
||||
c0: Fq6::conditional_select(&a.c0, &b.c0, choice),
|
||||
c1: Fq6::conditional_select(&a.c1, &b.c1, choice),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Fq12 {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Fq12 {
|
||||
c0: self.c0.neg(),
|
||||
c1: self.c1.neg(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Add<&'r Fq12> for Fq12 {
|
||||
type Output = Self;
|
||||
|
||||
@ -172,14 +193,11 @@ impl Field for Fq12 {
|
||||
self.c0.is_zero() && self.c1.is_zero()
|
||||
}
|
||||
|
||||
fn double(&mut self) {
|
||||
self.c0.double();
|
||||
self.c1.double();
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
self.c0.negate();
|
||||
self.c1.negate();
|
||||
fn double(&self) -> Self {
|
||||
Fq12 {
|
||||
c0: self.c0.double(),
|
||||
c1: self.c1.double(),
|
||||
}
|
||||
}
|
||||
|
||||
fn frobenius_map(&mut self, power: usize) {
|
||||
@ -191,7 +209,7 @@ impl Field for Fq12 {
|
||||
self.c1.c2.mul_assign(&FROBENIUS_COEFF_FQ12_C1[power % 12]);
|
||||
}
|
||||
|
||||
fn square(&mut self) {
|
||||
fn square(&self) -> Self {
|
||||
let mut ab = self.c0;
|
||||
ab.mul_assign(&self.c1);
|
||||
let mut c0c1 = self.c0;
|
||||
@ -201,28 +219,22 @@ impl Field for Fq12 {
|
||||
c0.add_assign(&self.c0);
|
||||
c0.mul_assign(&c0c1);
|
||||
c0.sub_assign(&ab);
|
||||
self.c1 = ab;
|
||||
self.c1.add_assign(&ab);
|
||||
let mut c1 = ab;
|
||||
c1.add_assign(&ab);
|
||||
ab.mul_by_nonresidue();
|
||||
c0.sub_assign(&ab);
|
||||
self.c0 = c0;
|
||||
Fq12 { c0, c1 }
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Option<Self> {
|
||||
let mut c0s = self.c0;
|
||||
c0s.square();
|
||||
let mut c1s = self.c1;
|
||||
c1s.square();
|
||||
let mut c0s = self.c0.square();
|
||||
let mut c1s = self.c1.square();
|
||||
c1s.mul_by_nonresidue();
|
||||
c0s.sub_assign(&c1s);
|
||||
|
||||
c0s.inverse().map(|t| {
|
||||
let mut tmp = Fq12 { c0: t, c1: t };
|
||||
tmp.c0.mul_assign(&self.c0);
|
||||
tmp.c1.mul_assign(&self.c1);
|
||||
tmp.c1.negate();
|
||||
|
||||
tmp
|
||||
c0s.inverse().map(|t| Fq12 {
|
||||
c0: t.mul(&self.c0),
|
||||
c1: t.mul(&self.c1).neg(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,8 @@ use super::fq::{Fq, FROBENIUS_COEFF_FQ2_C1, NEGATIVE_ONE};
|
||||
use ff::{Field, SqrtField};
|
||||
use rand_core::RngCore;
|
||||
use std::cmp::Ordering;
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
use subtle::{Choice, ConditionallySelectable};
|
||||
|
||||
/// An element of Fq2, represented by c0 + c1 * u.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
@ -46,16 +47,34 @@ impl Fq2 {
|
||||
|
||||
/// Norm of Fq2 as extension field in i over Fq
|
||||
pub fn norm(&self) -> Fq {
|
||||
let mut t0 = self.c0;
|
||||
let mut t1 = self.c1;
|
||||
t0.square();
|
||||
t1.square();
|
||||
let t0 = self.c0.square();
|
||||
let mut t1 = self.c1.square();
|
||||
t1.add_assign(&t0);
|
||||
|
||||
t1
|
||||
}
|
||||
}
|
||||
|
||||
impl ConditionallySelectable for Fq2 {
|
||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||
Fq2 {
|
||||
c0: Fq::conditional_select(&a.c0, &b.c0, choice),
|
||||
c1: Fq::conditional_select(&a.c1, &b.c1, choice),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Fq2 {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Fq2 {
|
||||
c0: self.c0.neg(),
|
||||
c1: self.c1.neg(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Add<&'r Fq2> for Fq2 {
|
||||
type Output = Self;
|
||||
|
||||
@ -187,48 +206,35 @@ impl Field for Fq2 {
|
||||
self.c0.is_zero() && self.c1.is_zero()
|
||||
}
|
||||
|
||||
fn square(&mut self) {
|
||||
fn square(&self) -> Self {
|
||||
let mut ab = self.c0;
|
||||
ab.mul_assign(&self.c1);
|
||||
let mut c0c1 = self.c0;
|
||||
c0c1.add_assign(&self.c1);
|
||||
let mut c0 = self.c1;
|
||||
c0.negate();
|
||||
let mut c0 = self.c1.neg();
|
||||
c0.add_assign(&self.c0);
|
||||
c0.mul_assign(&c0c1);
|
||||
c0.sub_assign(&ab);
|
||||
self.c1 = ab;
|
||||
self.c1.add_assign(&ab);
|
||||
let mut c1 = ab;
|
||||
c1.add_assign(&ab);
|
||||
c0.add_assign(&ab);
|
||||
self.c0 = c0;
|
||||
Fq2 { c0, c1 }
|
||||
}
|
||||
|
||||
fn double(&mut self) {
|
||||
self.c0.double();
|
||||
self.c1.double();
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
self.c0.negate();
|
||||
self.c1.negate();
|
||||
fn double(&self) -> Self {
|
||||
Fq2 {
|
||||
c0: self.c0.double(),
|
||||
c1: self.c1.double(),
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Option<Self> {
|
||||
let mut t1 = self.c1;
|
||||
t1.square();
|
||||
let mut t0 = self.c0;
|
||||
t0.square();
|
||||
let t1 = self.c1.square();
|
||||
let mut t0 = self.c0.square();
|
||||
t0.add_assign(&t1);
|
||||
t0.inverse().map(|t| {
|
||||
let mut tmp = Fq2 {
|
||||
c0: self.c0,
|
||||
c1: self.c1,
|
||||
};
|
||||
tmp.c0.mul_assign(&t);
|
||||
tmp.c1.mul_assign(&t);
|
||||
tmp.c1.negate();
|
||||
|
||||
tmp
|
||||
t0.inverse().map(|t| Fq2 {
|
||||
c0: self.c0.mul(&t),
|
||||
c1: self.c1.mul(&t).neg(),
|
||||
})
|
||||
}
|
||||
|
||||
@ -257,8 +263,7 @@ impl SqrtField for Fq2 {
|
||||
0x92c6e9ed90d2eb35,
|
||||
0x680447a8e5ff9a6,
|
||||
]);
|
||||
let mut alpha = a1;
|
||||
alpha.square();
|
||||
let mut alpha = a1.square();
|
||||
alpha.mul_assign(self);
|
||||
let mut a0 = alpha;
|
||||
a0.frobenius_map(1);
|
||||
@ -353,34 +358,30 @@ fn test_fq2_squaring() {
|
||||
use super::fq::FqRepr;
|
||||
use ff::PrimeField;
|
||||
|
||||
let mut a = Fq2 {
|
||||
let a = Fq2 {
|
||||
c0: Fq::one(),
|
||||
c1: Fq::one(),
|
||||
}; // u + 1
|
||||
a.square();
|
||||
assert_eq!(
|
||||
a,
|
||||
a.square(),
|
||||
Fq2 {
|
||||
c0: Fq::zero(),
|
||||
c1: Fq::from_repr(FqRepr::from(2)).unwrap(),
|
||||
}
|
||||
); // 2u
|
||||
|
||||
let mut a = Fq2 {
|
||||
let a = Fq2 {
|
||||
c0: Fq::zero(),
|
||||
c1: Fq::one(),
|
||||
}; // u
|
||||
a.square();
|
||||
assert_eq!(a, {
|
||||
let mut neg1 = Fq::one();
|
||||
neg1.negate();
|
||||
assert_eq!(a.square(), {
|
||||
Fq2 {
|
||||
c0: neg1,
|
||||
c0: Fq::one().neg(),
|
||||
c1: Fq::zero(),
|
||||
}
|
||||
}); // -1
|
||||
|
||||
let mut a = Fq2 {
|
||||
let a = Fq2 {
|
||||
c0: Fq::from_repr(FqRepr([
|
||||
0x9c2c6309bbf8b598,
|
||||
0x4eef5c946536f602,
|
||||
@ -400,9 +401,8 @@ fn test_fq2_squaring() {
|
||||
]))
|
||||
.unwrap(),
|
||||
};
|
||||
a.square();
|
||||
assert_eq!(
|
||||
a,
|
||||
a.square(),
|
||||
Fq2 {
|
||||
c0: Fq::from_repr(FqRepr([
|
||||
0xf262c28c538bcf68,
|
||||
@ -694,7 +694,7 @@ fn test_fq2_negation() {
|
||||
use super::fq::FqRepr;
|
||||
use ff::PrimeField;
|
||||
|
||||
let mut a = Fq2 {
|
||||
let a = Fq2 {
|
||||
c0: Fq::from_repr(FqRepr([
|
||||
0x2d0078036923ffc7,
|
||||
0x11e59ea221a3b6d2,
|
||||
@ -713,8 +713,8 @@ fn test_fq2_negation() {
|
||||
0x12d1137b8a6a837,
|
||||
]))
|
||||
.unwrap(),
|
||||
};
|
||||
a.negate();
|
||||
}
|
||||
.neg();
|
||||
assert_eq!(
|
||||
a,
|
||||
Fq2 {
|
||||
@ -745,7 +745,7 @@ fn test_fq2_doubling() {
|
||||
use super::fq::FqRepr;
|
||||
use ff::PrimeField;
|
||||
|
||||
let mut a = Fq2 {
|
||||
let a = Fq2 {
|
||||
c0: Fq::from_repr(FqRepr([
|
||||
0x2d0078036923ffc7,
|
||||
0x11e59ea221a3b6d2,
|
||||
@ -765,9 +765,8 @@ fn test_fq2_doubling() {
|
||||
]))
|
||||
.unwrap(),
|
||||
};
|
||||
a.double();
|
||||
assert_eq!(
|
||||
a,
|
||||
a.double(),
|
||||
Fq2 {
|
||||
c0: Fq::from_repr(FqRepr([
|
||||
0x5a00f006d247ff8e,
|
||||
@ -1000,8 +999,7 @@ fn test_fq2_legendre() {
|
||||
|
||||
assert_eq!(Zero, Fq2::zero().legendre());
|
||||
// i^2 = -1
|
||||
let mut m1 = Fq2::one();
|
||||
m1.negate();
|
||||
let mut m1 = Fq2::one().neg();
|
||||
assert_eq!(QuadraticResidue, m1.legendre());
|
||||
m1.mul_by_nonresidue();
|
||||
assert_eq!(QuadraticNonResidue, m1.legendre());
|
||||
|
@ -2,7 +2,8 @@ use super::fq::{FROBENIUS_COEFF_FQ6_C1, FROBENIUS_COEFF_FQ6_C2};
|
||||
use super::fq2::Fq2;
|
||||
use ff::Field;
|
||||
use rand_core::RngCore;
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
use subtle::{Choice, ConditionallySelectable};
|
||||
|
||||
/// An element of Fq6, represented by c0 + c1 * v + c2 * v^(2).
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
@ -100,6 +101,28 @@ impl Fq6 {
|
||||
}
|
||||
}
|
||||
|
||||
impl ConditionallySelectable for Fq6 {
|
||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||
Fq6 {
|
||||
c0: Fq2::conditional_select(&a.c0, &b.c0, choice),
|
||||
c1: Fq2::conditional_select(&a.c1, &b.c1, choice),
|
||||
c2: Fq2::conditional_select(&a.c2, &b.c2, choice),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Fq6 {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Fq6 {
|
||||
c0: self.c0.neg(),
|
||||
c1: self.c1.neg(),
|
||||
c2: self.c2.neg(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Add<&'r Fq6> for Fq6 {
|
||||
type Output = Self;
|
||||
|
||||
@ -274,16 +297,12 @@ impl Field for Fq6 {
|
||||
self.c0.is_zero() && self.c1.is_zero() && self.c2.is_zero()
|
||||
}
|
||||
|
||||
fn double(&mut self) {
|
||||
self.c0.double();
|
||||
self.c1.double();
|
||||
self.c2.double();
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
self.c0.negate();
|
||||
self.c1.negate();
|
||||
self.c2.negate();
|
||||
fn double(&self) -> Self {
|
||||
Fq6 {
|
||||
c0: self.c0.double(),
|
||||
c1: self.c1.double(),
|
||||
c2: self.c2.double(),
|
||||
}
|
||||
}
|
||||
|
||||
fn frobenius_map(&mut self, power: usize) {
|
||||
@ -295,59 +314,54 @@ impl Field for Fq6 {
|
||||
self.c2.mul_assign(&FROBENIUS_COEFF_FQ6_C2[power % 6]);
|
||||
}
|
||||
|
||||
fn square(&mut self) {
|
||||
let mut s0 = self.c0;
|
||||
s0.square();
|
||||
fn square(&self) -> Self {
|
||||
let s0 = self.c0.square();
|
||||
let mut ab = self.c0;
|
||||
ab.mul_assign(&self.c1);
|
||||
let mut s1 = ab;
|
||||
s1.double();
|
||||
let s1 = ab.double();
|
||||
let mut s2 = self.c0;
|
||||
s2.sub_assign(&self.c1);
|
||||
s2.add_assign(&self.c2);
|
||||
s2.square();
|
||||
s2 = s2.square();
|
||||
let mut bc = self.c1;
|
||||
bc.mul_assign(&self.c2);
|
||||
let mut s3 = bc;
|
||||
s3.double();
|
||||
let mut s4 = self.c2;
|
||||
s4.square();
|
||||
let s3 = bc.double();
|
||||
let s4 = self.c2.square();
|
||||
|
||||
self.c0 = s3;
|
||||
self.c0.mul_by_nonresidue();
|
||||
self.c0.add_assign(&s0);
|
||||
let mut c0 = s3;
|
||||
c0.mul_by_nonresidue();
|
||||
c0.add_assign(&s0);
|
||||
|
||||
self.c1 = s4;
|
||||
self.c1.mul_by_nonresidue();
|
||||
self.c1.add_assign(&s1);
|
||||
let mut c1 = s4;
|
||||
c1.mul_by_nonresidue();
|
||||
c1.add_assign(&s1);
|
||||
|
||||
self.c2 = s1;
|
||||
self.c2.add_assign(&s2);
|
||||
self.c2.add_assign(&s3);
|
||||
self.c2.sub_assign(&s0);
|
||||
self.c2.sub_assign(&s4);
|
||||
let mut c2 = s1;
|
||||
c2.add_assign(&s2);
|
||||
c2.add_assign(&s3);
|
||||
c2.sub_assign(&s0);
|
||||
c2.sub_assign(&s4);
|
||||
|
||||
Fq6 { c0, c1, c2 }
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Option<Self> {
|
||||
let mut c0 = self.c2;
|
||||
c0.mul_by_nonresidue();
|
||||
c0.mul_assign(&self.c1);
|
||||
c0.negate();
|
||||
c0 = c0.neg();
|
||||
{
|
||||
let mut c0s = self.c0;
|
||||
c0s.square();
|
||||
let c0s = self.c0.square();
|
||||
c0.add_assign(&c0s);
|
||||
}
|
||||
let mut c1 = self.c2;
|
||||
c1.square();
|
||||
let mut c1 = self.c2.square();
|
||||
c1.mul_by_nonresidue();
|
||||
{
|
||||
let mut c01 = self.c0;
|
||||
c01.mul_assign(&self.c1);
|
||||
c1.sub_assign(&c01);
|
||||
}
|
||||
let mut c2 = self.c1;
|
||||
c2.square();
|
||||
let mut c2 = self.c1.square();
|
||||
{
|
||||
let mut c02 = self.c0;
|
||||
c02.mul_assign(&self.c2);
|
||||
|
@ -10,6 +10,8 @@ pub struct Fr(FrRepr);
|
||||
use rand_core::SeedableRng;
|
||||
#[cfg(test)]
|
||||
use rand_xorshift::XorShiftRng;
|
||||
#[cfg(test)]
|
||||
use std::ops::Neg;
|
||||
|
||||
#[test]
|
||||
fn test_fr_repr_ordering() {
|
||||
@ -691,16 +693,15 @@ fn test_fr_mul_assign() {
|
||||
|
||||
#[test]
|
||||
fn test_fr_squaring() {
|
||||
let mut a = Fr(FrRepr([
|
||||
let a = Fr(FrRepr([
|
||||
0xffffffffffffffff,
|
||||
0xffffffffffffffff,
|
||||
0xffffffffffffffff,
|
||||
0x73eda753299d7d47,
|
||||
]));
|
||||
assert!(a.is_valid());
|
||||
a.square();
|
||||
assert_eq!(
|
||||
a,
|
||||
a.square(),
|
||||
Fr::from_repr(FrRepr([
|
||||
0xc0d698e7bde077b8,
|
||||
0xb79a310579e76ec2,
|
||||
@ -718,14 +719,7 @@ fn test_fr_squaring() {
|
||||
for _ in 0..1000000 {
|
||||
// Ensure that (a * a) = a^2
|
||||
let a = Fr::random(&mut rng);
|
||||
|
||||
let mut tmp = a;
|
||||
tmp.square();
|
||||
|
||||
let mut tmp2 = a;
|
||||
tmp2.mul_assign(&a);
|
||||
|
||||
assert_eq!(tmp, tmp2);
|
||||
assert_eq!(a.square(), a * a);
|
||||
}
|
||||
}
|
||||
|
||||
@ -758,19 +752,15 @@ fn test_fr_double() {
|
||||
|
||||
for _ in 0..1000 {
|
||||
// Ensure doubling a is equivalent to adding a to itself.
|
||||
let mut a = Fr::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.add_assign(&a);
|
||||
a.double();
|
||||
assert_eq!(a, b);
|
||||
let a = Fr::random(&mut rng);
|
||||
assert_eq!(a.double(), a + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fr_negate() {
|
||||
fn test_fr_neg() {
|
||||
{
|
||||
let mut a = Fr::zero();
|
||||
a.negate();
|
||||
let a = Fr::zero().neg();
|
||||
|
||||
assert!(a.is_zero());
|
||||
}
|
||||
@ -783,8 +773,7 @@ fn test_fr_negate() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure (a - (-a)) = 0.
|
||||
let mut a = Fr::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.negate();
|
||||
let b = a.neg();
|
||||
a.add_assign(&b);
|
||||
|
||||
assert!(a.is_zero());
|
||||
@ -832,10 +821,8 @@ fn test_fr_sqrt() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure sqrt(a^2) = a or -a
|
||||
let a = Fr::random(&mut rng);
|
||||
let mut nega = a;
|
||||
nega.negate();
|
||||
let mut b = a;
|
||||
b.square();
|
||||
let nega = a.neg();
|
||||
let b = a.square();
|
||||
|
||||
let b = b.sqrt().unwrap();
|
||||
|
||||
@ -846,10 +833,8 @@ fn test_fr_sqrt() {
|
||||
// Ensure sqrt(a)^2 = a for random a
|
||||
let a = Fr::random(&mut rng);
|
||||
|
||||
if let Some(mut tmp) = a.sqrt() {
|
||||
tmp.square();
|
||||
|
||||
assert_eq!(a, tmp);
|
||||
if let Some(tmp) = a.sqrt() {
|
||||
assert_eq!(a, tmp.square());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ use super::{Engine, PairingCurveAffine};
|
||||
|
||||
use ff::{BitIterator, Field, ScalarEngine};
|
||||
use group::CurveAffine;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
// The BLS parameter x for BLS12-381 is -0xd201000000010000
|
||||
const BLS_X: u64 = 0xd201000000010000;
|
||||
@ -97,7 +97,7 @@ impl Engine for Bls12 {
|
||||
}
|
||||
}
|
||||
|
||||
f.square();
|
||||
f = f.square();
|
||||
}
|
||||
|
||||
for &mut (p, ref mut coeffs) in &mut pairs {
|
||||
@ -131,8 +131,7 @@ impl Engine for Bls12 {
|
||||
}
|
||||
|
||||
let mut x = BLS_X;
|
||||
let mut y0 = r;
|
||||
y0.square();
|
||||
let y0 = r.square();
|
||||
let mut y1 = y0;
|
||||
exp_by_x(&mut y1, x);
|
||||
x >>= 1;
|
||||
@ -185,41 +184,35 @@ impl G2Prepared {
|
||||
|
||||
fn doubling_step(r: &mut G2) -> (Fq2, Fq2, Fq2) {
|
||||
// Adaptation of Algorithm 26, https://eprint.iacr.org/2010/354.pdf
|
||||
let mut tmp0 = r.x;
|
||||
tmp0.square();
|
||||
let mut tmp0 = r.x.square();
|
||||
|
||||
let mut tmp1 = r.y;
|
||||
tmp1.square();
|
||||
let mut tmp1 = r.y.square();
|
||||
|
||||
let mut tmp2 = tmp1;
|
||||
tmp2.square();
|
||||
let mut tmp2 = tmp1.square();
|
||||
|
||||
let mut tmp3 = tmp1;
|
||||
tmp3.add_assign(&r.x);
|
||||
tmp3.square();
|
||||
tmp3 = tmp3.square();
|
||||
tmp3.sub_assign(&tmp0);
|
||||
tmp3.sub_assign(&tmp2);
|
||||
tmp3.double();
|
||||
tmp3 = tmp3.double();
|
||||
|
||||
let mut tmp4 = tmp0;
|
||||
tmp4.double();
|
||||
let mut tmp4 = tmp0.double();
|
||||
tmp4.add_assign(&tmp0);
|
||||
|
||||
let mut tmp6 = r.x;
|
||||
tmp6.add_assign(&tmp4);
|
||||
|
||||
let mut tmp5 = tmp4;
|
||||
tmp5.square();
|
||||
let tmp5 = tmp4.square();
|
||||
|
||||
let mut zsquared = r.z;
|
||||
zsquared.square();
|
||||
let zsquared = r.z.square();
|
||||
|
||||
r.x = tmp5;
|
||||
r.x.sub_assign(&tmp3);
|
||||
r.x.sub_assign(&tmp3);
|
||||
|
||||
r.z.add_assign(&r.y);
|
||||
r.z.square();
|
||||
r.z = r.z.square();
|
||||
r.z.sub_assign(&tmp1);
|
||||
r.z.sub_assign(&zsquared);
|
||||
|
||||
@ -227,47 +220,41 @@ impl G2Prepared {
|
||||
r.y.sub_assign(&r.x);
|
||||
r.y.mul_assign(&tmp4);
|
||||
|
||||
tmp2.double();
|
||||
tmp2.double();
|
||||
tmp2.double();
|
||||
tmp2 = tmp2.double().double().double();
|
||||
|
||||
r.y.sub_assign(&tmp2);
|
||||
|
||||
tmp3 = tmp4;
|
||||
tmp3.mul_assign(&zsquared);
|
||||
tmp3.double();
|
||||
tmp3.negate();
|
||||
tmp3 = tmp3.double().neg();
|
||||
|
||||
tmp6.square();
|
||||
tmp6 = tmp6.square();
|
||||
tmp6.sub_assign(&tmp0);
|
||||
tmp6.sub_assign(&tmp5);
|
||||
|
||||
tmp1.double();
|
||||
tmp1.double();
|
||||
tmp1 = tmp1.double().double();
|
||||
|
||||
tmp6.sub_assign(&tmp1);
|
||||
|
||||
tmp0 = r.z;
|
||||
tmp0.mul_assign(&zsquared);
|
||||
tmp0.double();
|
||||
tmp0 = tmp0.double();
|
||||
|
||||
(tmp0, tmp3, tmp6)
|
||||
}
|
||||
|
||||
fn addition_step(r: &mut G2, q: &G2Affine) -> (Fq2, Fq2, Fq2) {
|
||||
// Adaptation of Algorithm 27, https://eprint.iacr.org/2010/354.pdf
|
||||
let mut zsquared = r.z;
|
||||
zsquared.square();
|
||||
let zsquared = r.z.square();
|
||||
|
||||
let mut ysquared = q.y;
|
||||
ysquared.square();
|
||||
let ysquared = q.y.square();
|
||||
|
||||
let mut t0 = zsquared;
|
||||
t0.mul_assign(&q.x);
|
||||
|
||||
let mut t1 = q.y;
|
||||
t1.add_assign(&r.z);
|
||||
t1.square();
|
||||
t1 = t1.square();
|
||||
t1.sub_assign(&ysquared);
|
||||
t1.sub_assign(&zsquared);
|
||||
t1.mul_assign(&zsquared);
|
||||
@ -275,12 +262,9 @@ impl G2Prepared {
|
||||
let mut t2 = t0;
|
||||
t2.sub_assign(&r.x);
|
||||
|
||||
let mut t3 = t2;
|
||||
t3.square();
|
||||
let t3 = t2.square();
|
||||
|
||||
let mut t4 = t3;
|
||||
t4.double();
|
||||
t4.double();
|
||||
let t4 = t3.double().double();
|
||||
|
||||
let mut t5 = t4;
|
||||
t5.mul_assign(&t2);
|
||||
@ -295,14 +279,13 @@ impl G2Prepared {
|
||||
let mut t7 = t4;
|
||||
t7.mul_assign(&r.x);
|
||||
|
||||
r.x = t6;
|
||||
r.x.square();
|
||||
r.x = t6.square();
|
||||
r.x.sub_assign(&t5);
|
||||
r.x.sub_assign(&t7);
|
||||
r.x.sub_assign(&t7);
|
||||
|
||||
r.z.add_assign(&t2);
|
||||
r.z.square();
|
||||
r.z = r.z.square();
|
||||
r.z.sub_assign(&zsquared);
|
||||
r.z.sub_assign(&t3);
|
||||
|
||||
@ -315,29 +298,26 @@ impl G2Prepared {
|
||||
|
||||
t0 = r.y;
|
||||
t0.mul_assign(&t5);
|
||||
t0.double();
|
||||
t0 = t0.double();
|
||||
|
||||
r.y = t8;
|
||||
r.y.sub_assign(&t0);
|
||||
|
||||
t10.square();
|
||||
t10 = t10.square();
|
||||
t10.sub_assign(&ysquared);
|
||||
|
||||
let mut ztsquared = r.z;
|
||||
ztsquared.square();
|
||||
let ztsquared = r.z.square();
|
||||
|
||||
t10.sub_assign(&ztsquared);
|
||||
|
||||
t9.double();
|
||||
t9 = t9.double();
|
||||
t9.sub_assign(&t10);
|
||||
|
||||
t10 = r.z;
|
||||
t10.double();
|
||||
t10 = r.z.double();
|
||||
|
||||
t6.negate();
|
||||
t6 = t6.neg();
|
||||
|
||||
t1 = t6;
|
||||
t1.double();
|
||||
t1 = t6.double();
|
||||
|
||||
(t10, t1, t9)
|
||||
}
|
||||
|
@ -189,8 +189,7 @@ fn test_g1_uncompressed_invalid_vectors() {
|
||||
let mut x = Fq::one();
|
||||
|
||||
loop {
|
||||
let mut x3b = x;
|
||||
x3b.square();
|
||||
let mut x3b = x.square();
|
||||
x3b.mul_assign(&x);
|
||||
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
|
||||
|
||||
@ -326,8 +325,7 @@ fn test_g2_uncompressed_invalid_vectors() {
|
||||
let mut x = Fq2::one();
|
||||
|
||||
loop {
|
||||
let mut x3b = x;
|
||||
x3b.square();
|
||||
let mut x3b = x.square();
|
||||
x3b.mul_assign(&x);
|
||||
x3b.add_assign(&Fq2 {
|
||||
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
||||
@ -422,8 +420,7 @@ fn test_g1_compressed_invalid_vectors() {
|
||||
let mut x = Fq::one();
|
||||
|
||||
loop {
|
||||
let mut x3b = x;
|
||||
x3b.square();
|
||||
let mut x3b = x.square();
|
||||
x3b.mul_assign(&x);
|
||||
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
|
||||
|
||||
@ -447,8 +444,7 @@ fn test_g1_compressed_invalid_vectors() {
|
||||
let mut x = Fq::one();
|
||||
|
||||
loop {
|
||||
let mut x3b = x;
|
||||
x3b.square();
|
||||
let mut x3b = x.square();
|
||||
x3b.mul_assign(&x);
|
||||
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
|
||||
|
||||
@ -553,8 +549,7 @@ fn test_g2_compressed_invalid_vectors() {
|
||||
};
|
||||
|
||||
loop {
|
||||
let mut x3b = x;
|
||||
x3b.square();
|
||||
let mut x3b = x.square();
|
||||
x3b.mul_assign(&x);
|
||||
x3b.add_assign(&Fq2 {
|
||||
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
||||
@ -585,8 +580,7 @@ fn test_g2_compressed_invalid_vectors() {
|
||||
};
|
||||
|
||||
loop {
|
||||
let mut x3b = x;
|
||||
x3b.square();
|
||||
let mut x3b = x.square();
|
||||
x3b.mul_assign(&x);
|
||||
x3b.add_assign(&Fq2 {
|
||||
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
||||
|
@ -31,27 +31,24 @@ pub fn random_sqrt_tests<F: SqrtField>() {
|
||||
|
||||
for _ in 0..10000 {
|
||||
let a = F::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.square();
|
||||
let b = a.square();
|
||||
assert_eq!(b.legendre(), LegendreSymbol::QuadraticResidue);
|
||||
|
||||
let b = b.sqrt().unwrap();
|
||||
let mut negb = b;
|
||||
negb.negate();
|
||||
let negb = b.neg();
|
||||
|
||||
assert!(a == b || a == negb);
|
||||
}
|
||||
|
||||
let mut c = F::one();
|
||||
for _ in 0..10000 {
|
||||
let mut b = c;
|
||||
b.square();
|
||||
let mut b = c.square();
|
||||
assert_eq!(b.legendre(), LegendreSymbol::QuadraticResidue);
|
||||
|
||||
b = b.sqrt().unwrap();
|
||||
|
||||
if b != c {
|
||||
b.negate();
|
||||
b = b.neg();
|
||||
}
|
||||
|
||||
assert_eq!(b, c);
|
||||
@ -77,8 +74,7 @@ pub fn random_field_tests<F: Field>() {
|
||||
|
||||
assert!(F::zero().is_zero());
|
||||
{
|
||||
let mut z = F::zero();
|
||||
z.negate();
|
||||
let z = F::zero().neg();
|
||||
assert!(z.is_zero());
|
||||
}
|
||||
|
||||
@ -204,8 +200,7 @@ fn random_subtraction_tests<F: Field, R: RngCore>(rng: &mut R) {
|
||||
fn random_negation_tests<F: Field, R: RngCore>(rng: &mut R) {
|
||||
for _ in 0..10000 {
|
||||
let a = F::random(rng);
|
||||
let mut b = a;
|
||||
b.negate();
|
||||
let mut b = a.neg();
|
||||
b.add_assign(&a);
|
||||
|
||||
assert!(b.is_zero());
|
||||
@ -214,23 +209,15 @@ fn random_negation_tests<F: Field, R: RngCore>(rng: &mut R) {
|
||||
|
||||
fn random_doubling_tests<F: Field, R: RngCore>(rng: &mut R) {
|
||||
for _ in 0..10000 {
|
||||
let mut a = F::random(rng);
|
||||
let mut b = a;
|
||||
a.add_assign(&b);
|
||||
b.double();
|
||||
|
||||
assert_eq!(a, b);
|
||||
let a = F::random(rng);
|
||||
assert_eq!(a + a, a.double());
|
||||
}
|
||||
}
|
||||
|
||||
fn random_squaring_tests<F: Field, R: RngCore>(rng: &mut R) {
|
||||
for _ in 0..10000 {
|
||||
let mut a = F::random(rng);
|
||||
let mut b = a;
|
||||
a.mul_assign(&b);
|
||||
b.square();
|
||||
|
||||
assert_eq!(a, b);
|
||||
let a = F::random(rng);
|
||||
assert_eq!(a * a, a.square());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ rand_core = "0.5.1"
|
||||
ripemd160 = { version = "0.8", optional = true }
|
||||
secp256k1 = { version = "=0.15.0", optional = true }
|
||||
sha2 = "0.8"
|
||||
subtle = "2.2.1"
|
||||
|
||||
[dev-dependencies]
|
||||
hex-literal = "0.2"
|
||||
|
@ -1,5 +1,5 @@
|
||||
use ff::{BitIterator, Field, PrimeField, PrimeFieldRepr, SqrtField};
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use super::{montgomery, JubjubEngine, JubjubParams, PrimeOrder, Unknown};
|
||||
|
||||
@ -107,8 +107,7 @@ impl<E: JubjubEngine> Point<E, Unknown> {
|
||||
// as dy^2 + 1 = 0 has no solution in Fr.
|
||||
|
||||
// tmp1 = y^2
|
||||
let mut tmp1 = y;
|
||||
tmp1.square();
|
||||
let mut tmp1 = y.square();
|
||||
|
||||
// tmp2 = (y^2 * d) + 1
|
||||
let mut tmp2 = tmp1;
|
||||
@ -126,7 +125,7 @@ impl<E: JubjubEngine> Point<E, Unknown> {
|
||||
match tmp1.sqrt() {
|
||||
Some(mut x) => {
|
||||
if x.into_repr().is_odd() != sign {
|
||||
x.negate();
|
||||
x = x.neg();
|
||||
}
|
||||
|
||||
let mut t = x;
|
||||
@ -213,12 +212,9 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
|
||||
// only point of order 2 that is not the neutral element.
|
||||
if y.is_zero() {
|
||||
// This must be the point (0, 0) as above.
|
||||
let mut neg1 = E::Fr::one();
|
||||
neg1.negate();
|
||||
|
||||
Point {
|
||||
x: E::Fr::zero(),
|
||||
y: neg1,
|
||||
y: E::Fr::one().neg(),
|
||||
t: E::Fr::zero(),
|
||||
z: E::Fr::one(),
|
||||
_marker: PhantomData,
|
||||
@ -324,8 +320,8 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
|
||||
pub fn negate(&self) -> Self {
|
||||
let mut p = self.clone();
|
||||
|
||||
p.x.negate();
|
||||
p.t.negate();
|
||||
p.x = p.x.neg();
|
||||
p.t = p.t.neg();
|
||||
|
||||
p
|
||||
}
|
||||
@ -338,27 +334,22 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
|
||||
// http://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
||||
|
||||
// A = X1^2
|
||||
let mut a = self.x;
|
||||
a.square();
|
||||
let a = self.x.square();
|
||||
|
||||
// B = Y1^2
|
||||
let mut b = self.y;
|
||||
b.square();
|
||||
let b = self.y.square();
|
||||
|
||||
// C = 2*Z1^2
|
||||
let mut c = self.z;
|
||||
c.square();
|
||||
c.double();
|
||||
let c = self.z.square().double();
|
||||
|
||||
// D = a*A
|
||||
// = -A
|
||||
let mut d = a;
|
||||
d.negate();
|
||||
let d = a.neg();
|
||||
|
||||
// E = (X1+Y1)^2 - A - B
|
||||
let mut e = self.x;
|
||||
e.add_assign(&self.y);
|
||||
e.square();
|
||||
e = e.square();
|
||||
e.add_assign(&d); // -A = D
|
||||
e.sub_assign(&b);
|
||||
|
||||
|
@ -5,7 +5,8 @@ use ff::{
|
||||
PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField,
|
||||
};
|
||||
use rand_core::RngCore;
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
use subtle::{Choice, ConditionallySelectable};
|
||||
|
||||
use super::ToUniform;
|
||||
|
||||
@ -269,6 +270,31 @@ impl From<Fs> for FsRepr {
|
||||
}
|
||||
}
|
||||
|
||||
impl ConditionallySelectable for Fs {
|
||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||
Fs(FsRepr([
|
||||
u64::conditional_select(&(a.0).0[0], &(b.0).0[0], choice),
|
||||
u64::conditional_select(&(a.0).0[1], &(b.0).0[1], choice),
|
||||
u64::conditional_select(&(a.0).0[2], &(b.0).0[2], choice),
|
||||
u64::conditional_select(&(a.0).0[3], &(b.0).0[3], choice),
|
||||
]))
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Fs {
|
||||
type Output = Self;
|
||||
|
||||
#[inline]
|
||||
fn neg(mut self) -> Self {
|
||||
if !self.is_zero() {
|
||||
let mut tmp = MODULUS;
|
||||
tmp.sub_noborrow(&self.0);
|
||||
self.0 = tmp;
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Add<&'r Fs> for Fs {
|
||||
type Output = Self;
|
||||
|
||||
@ -488,21 +514,16 @@ impl Field for Fs {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn double(&mut self) {
|
||||
fn double(&self) -> Self {
|
||||
let mut ret = *self;
|
||||
|
||||
// This cannot exceed the backing capacity.
|
||||
self.0.mul2();
|
||||
ret.0.mul2();
|
||||
|
||||
// However, it may need to be reduced.
|
||||
self.reduce();
|
||||
}
|
||||
ret.reduce();
|
||||
|
||||
#[inline]
|
||||
fn negate(&mut self) {
|
||||
if !self.is_zero() {
|
||||
let mut tmp = MODULUS;
|
||||
tmp.sub_noborrow(&self.0);
|
||||
self.0 = tmp;
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Option<Self> {
|
||||
@ -566,7 +587,7 @@ impl Field for Fs {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn square(&mut self) {
|
||||
fn square(&self) -> Self {
|
||||
let mut carry = 0;
|
||||
let r1 = mac_with_carry(0, (self.0).0[0], (self.0).0[1], &mut carry);
|
||||
let r2 = mac_with_carry(0, (self.0).0[0], (self.0).0[2], &mut carry);
|
||||
@ -597,7 +618,10 @@ impl Field for Fs {
|
||||
let r5 = adc(r5, 0, &mut carry);
|
||||
let r6 = mac_with_carry(r6, (self.0).0[3], (self.0).0[3], &mut carry);
|
||||
let r7 = adc(r7, 0, &mut carry);
|
||||
self.mont_reduce(r0, r1, r2, r3, r4, r5, r6, r7);
|
||||
|
||||
let mut ret = *self;
|
||||
ret.mont_reduce(r0, r1, r2, r3, r4, r5, r6, r7);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
@ -675,7 +699,7 @@ impl Fs {
|
||||
fn mul_bits<S: AsRef<[u64]>>(&self, bits: BitIterator<S>) -> Self {
|
||||
let mut res = Self::zero();
|
||||
for bit in bits {
|
||||
res.double();
|
||||
res = res.double();
|
||||
|
||||
if bit {
|
||||
res.add_assign(self)
|
||||
@ -727,8 +751,7 @@ impl SqrtField for Fs {
|
||||
0x4199cec0404d0ec0,
|
||||
0x39f6d3a994cebea,
|
||||
]);
|
||||
let mut a0 = a1;
|
||||
a0.square();
|
||||
let mut a0 = a1.square();
|
||||
a0.mul_assign(self);
|
||||
|
||||
if a0 == NEGATIVE_ONE {
|
||||
@ -742,8 +765,7 @@ impl SqrtField for Fs {
|
||||
|
||||
#[test]
|
||||
fn test_neg_one() {
|
||||
let mut o = Fs::one();
|
||||
o.negate();
|
||||
let o = Fs::one().neg();
|
||||
|
||||
assert_eq!(NEGATIVE_ONE, o);
|
||||
}
|
||||
@ -1395,16 +1417,15 @@ fn test_fs_mul_assign() {
|
||||
|
||||
#[test]
|
||||
fn test_fr_squaring() {
|
||||
let mut a = Fs(FsRepr([
|
||||
let a = Fs(FsRepr([
|
||||
0xffffffffffffffff,
|
||||
0xffffffffffffffff,
|
||||
0xffffffffffffffff,
|
||||
0xe7db4ea6533afa8,
|
||||
]));
|
||||
assert!(a.is_valid());
|
||||
a.square();
|
||||
assert_eq!(
|
||||
a,
|
||||
a.square(),
|
||||
Fs::from_repr(FsRepr([
|
||||
0x12c7f55cbc52fbaa,
|
||||
0xdedc98a0b5e6ce9e,
|
||||
@ -1423,8 +1444,7 @@ fn test_fr_squaring() {
|
||||
// Ensure that (a * a) = a^2
|
||||
let a = Fs::random(&mut rng);
|
||||
|
||||
let mut tmp = a;
|
||||
tmp.square();
|
||||
let tmp = a.square();
|
||||
|
||||
let mut tmp2 = a;
|
||||
tmp2.mul_assign(&a);
|
||||
@ -1462,19 +1482,15 @@ fn test_fs_double() {
|
||||
|
||||
for _ in 0..1000 {
|
||||
// Ensure doubling a is equivalent to adding a to itself.
|
||||
let mut a = Fs::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.add_assign(&a);
|
||||
a.double();
|
||||
assert_eq!(a, b);
|
||||
let a = Fs::random(&mut rng);
|
||||
assert_eq!(a.double(), a + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fs_negate() {
|
||||
fn test_fs_neg() {
|
||||
{
|
||||
let mut a = Fs::zero();
|
||||
a.negate();
|
||||
let a = Fs::zero().neg();
|
||||
|
||||
assert!(a.is_zero());
|
||||
}
|
||||
@ -1487,8 +1503,7 @@ fn test_fs_negate() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure (a - (-a)) = 0.
|
||||
let mut a = Fs::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.negate();
|
||||
let b = a.neg();
|
||||
a.add_assign(&b);
|
||||
|
||||
assert!(a.is_zero());
|
||||
@ -1534,10 +1549,8 @@ fn test_fs_sqrt() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure sqrt(a^2) = a or -a
|
||||
let a = Fs::random(&mut rng);
|
||||
let mut nega = a;
|
||||
nega.negate();
|
||||
let mut b = a;
|
||||
b.square();
|
||||
let nega = a.neg();
|
||||
let b = a.square();
|
||||
|
||||
let b = b.sqrt().unwrap();
|
||||
|
||||
@ -1548,10 +1561,8 @@ fn test_fs_sqrt() {
|
||||
// Ensure sqrt(a)^2 = a for random a
|
||||
let a = Fs::random(&mut rng);
|
||||
|
||||
if let Some(mut tmp) = a.sqrt() {
|
||||
tmp.square();
|
||||
|
||||
assert_eq!(a, tmp);
|
||||
if let Some(tmp) = a.sqrt() {
|
||||
assert_eq!(a, tmp.square());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -195,8 +195,7 @@ impl JubjubParams<Bls12> for JubjubBls12 {
|
||||
impl JubjubBls12 {
|
||||
pub fn new() -> Self {
|
||||
let montgomery_a = Fr::from_str("40962").unwrap();
|
||||
let mut montgomery_2a = montgomery_a;
|
||||
montgomery_2a.double();
|
||||
let montgomery_2a = montgomery_a.double();
|
||||
|
||||
let mut tmp_params = JubjubBls12 {
|
||||
// d = -(10240/10241)
|
||||
|
@ -1,5 +1,5 @@
|
||||
use ff::{BitIterator, Field, PrimeField, PrimeFieldRepr, SqrtField};
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use super::{edwards, JubjubEngine, JubjubParams, PrimeOrder, Unknown};
|
||||
|
||||
@ -50,8 +50,7 @@ impl<E: JubjubEngine> Point<E, Unknown> {
|
||||
pub fn get_for_x(x: E::Fr, sign: bool, params: &E::Params) -> Option<Self> {
|
||||
// Given an x on the curve, y = sqrt(x^3 + A*x^2 + x)
|
||||
|
||||
let mut x2 = x;
|
||||
x2.square();
|
||||
let mut x2 = x.square();
|
||||
|
||||
let mut rhs = x2;
|
||||
rhs.mul_assign(params.montgomery_a());
|
||||
@ -62,7 +61,7 @@ impl<E: JubjubEngine> Point<E, Unknown> {
|
||||
match rhs.sqrt() {
|
||||
Some(mut y) => {
|
||||
if y.into_repr().is_odd() != sign {
|
||||
y.negate();
|
||||
y = y.neg();
|
||||
}
|
||||
|
||||
Some(Point {
|
||||
@ -190,7 +189,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
|
||||
pub fn negate(&self) -> Self {
|
||||
let mut p = self.clone();
|
||||
|
||||
p.y.negate();
|
||||
p.y = p.y.neg();
|
||||
|
||||
p
|
||||
}
|
||||
@ -216,24 +215,21 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
|
||||
{
|
||||
let mut tmp = *params.montgomery_a();
|
||||
tmp.mul_assign(&self.x);
|
||||
tmp.double();
|
||||
tmp = tmp.double();
|
||||
delta.add_assign(&tmp);
|
||||
}
|
||||
{
|
||||
let mut tmp = self.x;
|
||||
tmp.square();
|
||||
let mut tmp = self.x.square();
|
||||
delta.add_assign(&tmp);
|
||||
tmp.double();
|
||||
tmp = tmp.double();
|
||||
delta.add_assign(&tmp);
|
||||
}
|
||||
{
|
||||
let mut tmp = self.y;
|
||||
tmp.double();
|
||||
let tmp = self.y.double();
|
||||
delta.mul_assign(&tmp.inverse().expect("y is nonzero so this must be nonzero"));
|
||||
}
|
||||
|
||||
let mut x3 = delta;
|
||||
x3.square();
|
||||
let mut x3 = delta.square();
|
||||
x3.sub_assign(params.montgomery_a());
|
||||
x3.sub_assign(&self.x);
|
||||
x3.sub_assign(&self.x);
|
||||
@ -242,7 +238,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
|
||||
y3.sub_assign(&self.x);
|
||||
y3.mul_assign(&delta);
|
||||
y3.add_assign(&self.y);
|
||||
y3.negate();
|
||||
y3 = y3.neg();
|
||||
|
||||
Point {
|
||||
x: x3,
|
||||
@ -282,8 +278,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
|
||||
);
|
||||
}
|
||||
|
||||
let mut x3 = delta;
|
||||
x3.square();
|
||||
let mut x3 = delta.square();
|
||||
x3.sub_assign(params.montgomery_a());
|
||||
x3.sub_assign(&self.x);
|
||||
x3.sub_assign(&other.x);
|
||||
@ -292,7 +287,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
|
||||
y3.sub_assign(&self.x);
|
||||
y3.mul_assign(&delta);
|
||||
y3.add_assign(&self.y);
|
||||
y3.negate();
|
||||
y3 = y3.neg();
|
||||
|
||||
Point {
|
||||
x: x3,
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::{edwards, montgomery, JubjubEngine, JubjubParams, PrimeOrder};
|
||||
|
||||
use ff::{Field, LegendreSymbol, PrimeField, PrimeFieldRepr, SqrtField};
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use rand_core::{RngCore, SeedableRng};
|
||||
use rand_xorshift::XorShiftRng;
|
||||
@ -20,11 +20,9 @@ pub fn test_suite<E: JubjubEngine>(params: &E::Params) {
|
||||
}
|
||||
|
||||
fn is_on_mont_curve<E: JubjubEngine, P: JubjubParams<E>>(x: E::Fr, y: E::Fr, params: &P) -> bool {
|
||||
let mut lhs = y;
|
||||
lhs.square();
|
||||
let lhs = y.square();
|
||||
|
||||
let mut x2 = x;
|
||||
x2.square();
|
||||
let x2 = x.square();
|
||||
|
||||
let mut x3 = x2;
|
||||
x3.mul_assign(&x);
|
||||
@ -42,11 +40,9 @@ fn is_on_twisted_edwards_curve<E: JubjubEngine, P: JubjubParams<E>>(
|
||||
y: E::Fr,
|
||||
params: &P,
|
||||
) -> bool {
|
||||
let mut x2 = x;
|
||||
x2.square();
|
||||
let x2 = x.square();
|
||||
|
||||
let mut y2 = y;
|
||||
y2.square();
|
||||
let y2 = y.square();
|
||||
|
||||
// -x^2 + y^2
|
||||
let mut lhs = y2;
|
||||
@ -310,15 +306,11 @@ fn test_back_and_forth<E: JubjubEngine>(params: &E::Params) {
|
||||
|
||||
fn test_jubjub_params<E: JubjubEngine>(params: &E::Params) {
|
||||
// a = -1
|
||||
let mut a = E::Fr::one();
|
||||
a.negate();
|
||||
let a = E::Fr::one().neg();
|
||||
|
||||
{
|
||||
// Check that 2A is consistent with A
|
||||
let mut tmp = *params.montgomery_a();
|
||||
tmp.double();
|
||||
|
||||
assert_eq!(&tmp, params.montgomery_2a());
|
||||
assert_eq!(¶ms.montgomery_a().double(), params.montgomery_2a());
|
||||
}
|
||||
|
||||
{
|
||||
@ -339,7 +331,7 @@ fn test_jubjub_params<E: JubjubEngine>(params: &E::Params) {
|
||||
assert!(tmp.inverse().unwrap().legendre() == LegendreSymbol::QuadraticNonResidue);
|
||||
|
||||
// tmp = -d
|
||||
tmp.negate();
|
||||
tmp = tmp.neg();
|
||||
|
||||
// -d is nonsquare
|
||||
assert!(tmp.legendre() == LegendreSymbol::QuadraticNonResidue);
|
||||
@ -350,8 +342,7 @@ fn test_jubjub_params<E: JubjubEngine>(params: &E::Params) {
|
||||
|
||||
{
|
||||
// Check that A^2 - 4 is nonsquare:
|
||||
let mut tmp = params.montgomery_a().clone();
|
||||
tmp.square();
|
||||
let mut tmp = params.montgomery_a().square();
|
||||
tmp.sub_assign(&E::Fr::from_str("4").unwrap());
|
||||
assert!(tmp.legendre() == LegendreSymbol::QuadraticNonResidue);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
use crate::jubjub::*;
|
||||
use ff::{Field, PrimeField, PrimeFieldRepr};
|
||||
use std::ops::AddAssign;
|
||||
use std::ops::{AddAssign, Neg};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum Personalization {
|
||||
@ -58,14 +58,14 @@ where
|
||||
if a {
|
||||
tmp.add_assign(&cur);
|
||||
}
|
||||
cur.double(); // 2^1 * cur
|
||||
cur = cur.double(); // 2^1 * cur
|
||||
if b {
|
||||
tmp.add_assign(&cur);
|
||||
}
|
||||
|
||||
// conditionally negate
|
||||
if c {
|
||||
tmp.negate();
|
||||
tmp = tmp.neg();
|
||||
}
|
||||
|
||||
acc.add_assign(&tmp);
|
||||
@ -75,9 +75,7 @@ where
|
||||
if chunks_remaining == 0 {
|
||||
break;
|
||||
} else {
|
||||
cur.double(); // 2^2 * cur
|
||||
cur.double(); // 2^3 * cur
|
||||
cur.double(); // 2^4 * cur
|
||||
cur = cur.double().double().double(); // 2^4 * cur
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ use crate::jubjub::{edwards::Point, FixedGenerators, JubjubEngine, JubjubParams,
|
||||
use ff::{Field, PrimeField, PrimeFieldRepr};
|
||||
use rand_core::RngCore;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::ops::{AddAssign, MulAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg};
|
||||
|
||||
use crate::util::hash_to_scalar;
|
||||
|
||||
@ -194,7 +194,7 @@ pub fn batch_verify<'a, E: JubjubEngine, R: RngCore>(
|
||||
let z = E::Fs::random(rng);
|
||||
|
||||
s.mul_assign(&z);
|
||||
s.negate();
|
||||
s = s.neg();
|
||||
|
||||
r = r.mul(z, params);
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
use ff::Field;
|
||||
use pairing::Engine;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use bellman::{ConstraintSystem, SynthesisError};
|
||||
|
||||
@ -323,8 +323,7 @@ impl<E: JubjubEngine> EdwardsPoint<E> {
|
||||
|
||||
// Compute C = d*A*A
|
||||
let c = AllocatedNum::alloc(cs.namespace(|| "C"), || {
|
||||
let mut t0 = *a.get_value().get()?;
|
||||
t0.square();
|
||||
let mut t0 = a.get_value().get()?.square();
|
||||
t0.mul_assign(params.edwards_d());
|
||||
|
||||
Ok(t0)
|
||||
@ -340,7 +339,7 @@ impl<E: JubjubEngine> EdwardsPoint<E> {
|
||||
// Compute x3 = (2.A) / (1 + C)
|
||||
let x3 = AllocatedNum::alloc(cs.namespace(|| "x3"), || {
|
||||
let mut t0 = *a.get_value().get()?;
|
||||
t0.double();
|
||||
t0 = t0.double();
|
||||
|
||||
let mut t1 = E::Fr::one();
|
||||
t1.add_assign(c.get_value().get()?);
|
||||
@ -366,8 +365,7 @@ impl<E: JubjubEngine> EdwardsPoint<E> {
|
||||
// Compute y3 = (U - 2.A) / (1 - C)
|
||||
let y3 = AllocatedNum::alloc(cs.namespace(|| "y3"), || {
|
||||
let mut t0 = *a.get_value().get()?;
|
||||
t0.double();
|
||||
t0.negate();
|
||||
t0 = t0.double().neg();
|
||||
t0.add_assign(t.get_value().get()?);
|
||||
|
||||
let mut t1 = E::Fr::one();
|
||||
@ -613,8 +611,7 @@ impl<E: JubjubEngine> MontgomeryPoint<E> {
|
||||
|
||||
// Compute x'' = lambda^2 - A - x - x'
|
||||
let xprime = AllocatedNum::alloc(cs.namespace(|| "xprime"), || {
|
||||
let mut t0 = *lambda.get_value().get()?;
|
||||
t0.square();
|
||||
let mut t0 = lambda.get_value().get()?.square();
|
||||
t0.sub_assign(params.montgomery_a());
|
||||
t0.sub_assign(self.x.get_value().get()?);
|
||||
t0.sub_assign(other.x.get_value().get()?);
|
||||
@ -642,7 +639,7 @@ impl<E: JubjubEngine> MontgomeryPoint<E> {
|
||||
t0.sub_assign(self.x.get_value().get()?);
|
||||
t0.mul_assign(lambda.get_value().get()?);
|
||||
t0.add_assign(self.y.get_value().get()?);
|
||||
t0.negate();
|
||||
t0 = t0.neg();
|
||||
|
||||
Ok(t0)
|
||||
})?;
|
||||
|
@ -245,7 +245,7 @@ impl<'a, E: JubjubEngine> Circuit<E> for Spend<'a, E> {
|
||||
let mut coeff = E::Fr::one();
|
||||
for bit in &value_bits {
|
||||
value_num = value_num.add_bool_with_coeff(CS::one(), bit, coeff);
|
||||
coeff.double();
|
||||
coeff = coeff.double();
|
||||
}
|
||||
|
||||
// Place the value in the note
|
||||
|
@ -268,7 +268,7 @@ impl NoteValue {
|
||||
let mut coeff = E::Fr::one();
|
||||
for b in &self.bits {
|
||||
tmp = tmp + (coeff, b.get_variable());
|
||||
coeff.double();
|
||||
coeff = coeff.double();
|
||||
}
|
||||
|
||||
tmp
|
||||
|
@ -5,7 +5,7 @@ use bellman::{
|
||||
use ff::Field;
|
||||
use pairing::bls12_381::{Bls12, Fr};
|
||||
use rand_core::OsRng;
|
||||
use std::ops::AddAssign;
|
||||
use std::ops::{AddAssign, Neg};
|
||||
use zcash_primitives::{
|
||||
jubjub::{edwards, fs::Fs, FixedGenerators, JubjubBls12, Unknown},
|
||||
primitives::{Diversifier, Note, PaymentAddress, ProofGenerationKey, ValueCommitment},
|
||||
@ -202,8 +202,7 @@ impl SaplingProvingContext {
|
||||
|
||||
// Accumulate the value commitment randomness in the context
|
||||
{
|
||||
let mut tmp = rcv;
|
||||
tmp.negate(); // Outputs subtract from the total.
|
||||
let mut tmp = rcv.neg(); // Outputs subtract from the total.
|
||||
tmp.add_assign(&self.bsk);
|
||||
|
||||
// Update the context
|
||||
|
Loading…
Reference in New Issue
Block a user