Move from Curve*::negate to Neg operator
This commit is contained in:
parent
8193324986
commit
1a8ec21c03
|
@ -417,10 +417,6 @@ impl CurveProjective for Fr {
|
||||||
AddAssign::add_assign(self, other);
|
AddAssign::add_assign(self, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn negate(&mut self) {
|
|
||||||
self.0 = self.neg().0;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S) {
|
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S) {
|
||||||
let tmp = Fr::from_repr(other.into()).unwrap();
|
let tmp = Fr::from_repr(other.into()).unwrap();
|
||||||
|
|
||||||
|
@ -499,10 +495,6 @@ impl CurveAffine for Fr {
|
||||||
<Fr as Field>::is_zero(self)
|
<Fr as Field>::is_zero(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn negate(&mut self) {
|
|
||||||
self.0 = self.neg().0;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, other: S) -> Self::Projective {
|
fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, other: S) -> Self::Projective {
|
||||||
let mut res = *self;
|
let mut res = *self;
|
||||||
let tmp = Fr::from_repr(other.into()).unwrap();
|
let tmp = Fr::from_repr(other.into()).unwrap();
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
use ff::PrimeField;
|
use ff::PrimeField;
|
||||||
use group::{CurveAffine, CurveProjective};
|
use group::{CurveAffine, CurveProjective};
|
||||||
use pairing::{Engine, PairingCurveAffine};
|
use pairing::{Engine, PairingCurveAffine};
|
||||||
use std::ops::AddAssign;
|
use std::ops::{AddAssign, Neg};
|
||||||
|
|
||||||
use super::{PreparedVerifyingKey, Proof, VerifyingKey};
|
use super::{PreparedVerifyingKey, Proof, VerifyingKey};
|
||||||
|
|
||||||
use crate::SynthesisError;
|
use crate::SynthesisError;
|
||||||
|
|
||||||
pub fn prepare_verifying_key<E: Engine>(vk: &VerifyingKey<E>) -> PreparedVerifyingKey<E> {
|
pub fn prepare_verifying_key<E: Engine>(vk: &VerifyingKey<E>) -> PreparedVerifyingKey<E> {
|
||||||
let mut gamma = vk.gamma_g2;
|
let gamma = vk.gamma_g2.neg();
|
||||||
gamma.negate();
|
let delta = vk.delta_g2.neg();
|
||||||
let mut delta = vk.delta_g2;
|
|
||||||
delta.negate();
|
|
||||||
|
|
||||||
PreparedVerifyingKey {
|
PreparedVerifyingKey {
|
||||||
alpha_g1_beta_g2: E::pairing(vk.alpha_g1, vk.beta_g2),
|
alpha_g1_beta_g2: E::pairing(vk.alpha_g1, vk.beta_g2),
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ff::{PrimeField, PrimeFieldDecodingError, ScalarEngine, SqrtField};
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::{Add, AddAssign, Sub, SubAssign};
|
use std::ops::{Add, AddAssign, Neg, Sub, SubAssign};
|
||||||
|
|
||||||
pub mod tests;
|
pub mod tests;
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@ pub trait CurveProjective:
|
||||||
+ 'static
|
+ 'static
|
||||||
+ Add<Output = Self>
|
+ Add<Output = Self>
|
||||||
+ Sub<Output = Self>
|
+ Sub<Output = Self>
|
||||||
|
+ Neg<Output = Self>
|
||||||
+ for<'a> Add<&'a Self, Output = Self>
|
+ for<'a> Add<&'a Self, Output = Self>
|
||||||
+ for<'a> Sub<&'a Self, Output = Self>
|
+ for<'a> Sub<&'a Self, Output = Self>
|
||||||
+ AddAssign
|
+ AddAssign
|
||||||
|
@ -65,9 +66,6 @@ pub trait CurveProjective:
|
||||||
/// Adds an affine element to this element.
|
/// Adds an affine element to this element.
|
||||||
fn add_assign_mixed(&mut self, other: &Self::Affine);
|
fn add_assign_mixed(&mut self, other: &Self::Affine);
|
||||||
|
|
||||||
/// Negates this element.
|
|
||||||
fn negate(&mut self);
|
|
||||||
|
|
||||||
/// Performs scalar multiplication of this element.
|
/// Performs scalar multiplication of this element.
|
||||||
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S);
|
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S);
|
||||||
|
|
||||||
|
@ -86,7 +84,17 @@ pub trait CurveProjective:
|
||||||
/// Affine representation of an elliptic curve point guaranteed to be
|
/// Affine representation of an elliptic curve point guaranteed to be
|
||||||
/// in the correct prime order subgroup.
|
/// in the correct prime order subgroup.
|
||||||
pub trait CurveAffine:
|
pub trait CurveAffine:
|
||||||
Copy + Clone + Sized + Send + Sync + fmt::Debug + fmt::Display + PartialEq + Eq + 'static
|
Copy
|
||||||
|
+ Clone
|
||||||
|
+ Sized
|
||||||
|
+ Send
|
||||||
|
+ Sync
|
||||||
|
+ fmt::Debug
|
||||||
|
+ fmt::Display
|
||||||
|
+ PartialEq
|
||||||
|
+ Eq
|
||||||
|
+ 'static
|
||||||
|
+ Neg<Output = Self>
|
||||||
{
|
{
|
||||||
type Engine: ScalarEngine<Fr = Self::Scalar>;
|
type Engine: ScalarEngine<Fr = Self::Scalar>;
|
||||||
type Scalar: PrimeField + SqrtField;
|
type Scalar: PrimeField + SqrtField;
|
||||||
|
@ -105,9 +113,6 @@ pub trait CurveAffine:
|
||||||
/// additive identity.
|
/// additive identity.
|
||||||
fn is_zero(&self) -> bool;
|
fn is_zero(&self) -> bool;
|
||||||
|
|
||||||
/// Negates this element.
|
|
||||||
fn negate(&mut self);
|
|
||||||
|
|
||||||
/// Performs scalar multiplication of this element with mixed addition.
|
/// Performs scalar multiplication of this element with mixed addition.
|
||||||
fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, other: S) -> Self::Projective;
|
fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, other: S) -> Self::Projective;
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,7 @@ pub fn curve_tests<G: CurveProjective>() {
|
||||||
|
|
||||||
// Negation edge case with zero.
|
// Negation edge case with zero.
|
||||||
{
|
{
|
||||||
let mut z = G::zero();
|
let z = G::zero().neg();
|
||||||
z.negate();
|
|
||||||
assert!(z.is_zero());
|
assert!(z.is_zero());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,8 +215,7 @@ fn random_negation_tests<G: CurveProjective>() {
|
||||||
t4.add_assign_mixed(&t2.into_affine());
|
t4.add_assign_mixed(&t2.into_affine());
|
||||||
assert!(t4.is_zero());
|
assert!(t4.is_zero());
|
||||||
|
|
||||||
t1.negate();
|
assert_eq!(t1.neg(), t2);
|
||||||
assert_eq!(t1, t2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -440,7 +438,7 @@ fn random_encoding_tests<G: CurveAffine>() {
|
||||||
let de_compressed = compressed.into_affine().unwrap();
|
let de_compressed = compressed.into_affine().unwrap();
|
||||||
assert_eq!(de_compressed, r);
|
assert_eq!(de_compressed, r);
|
||||||
|
|
||||||
r.negate();
|
r = r.neg();
|
||||||
|
|
||||||
let compressed = r.into_compressed();
|
let compressed = r.into_compressed();
|
||||||
let de_compressed = compressed.into_affine().unwrap();
|
let de_compressed = compressed.into_affine().unwrap();
|
||||||
|
|
|
@ -134,6 +134,19 @@ macro_rules! curve_impl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ::std::ops::Neg for $affine {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn neg(self) -> Self {
|
||||||
|
let mut ret = self;
|
||||||
|
if !ret.is_zero() {
|
||||||
|
ret.y = ret.y.neg();
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CurveAffine for $affine {
|
impl CurveAffine for $affine {
|
||||||
type Engine = Bls12;
|
type Engine = Bls12;
|
||||||
type Scalar = $scalarfield;
|
type Scalar = $scalarfield;
|
||||||
|
@ -163,12 +176,6 @@ macro_rules! curve_impl {
|
||||||
self.mul_bits(bits)
|
self.mul_bits(bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn negate(&mut self) {
|
|
||||||
if !self.is_zero() {
|
|
||||||
self.y = self.y.neg();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn into_projective(&self) -> $projective {
|
fn into_projective(&self) -> $projective {
|
||||||
(*self).into()
|
(*self).into()
|
||||||
}
|
}
|
||||||
|
@ -188,6 +195,19 @@ macro_rules! curve_impl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ::std::ops::Neg for $projective {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn neg(self) -> Self {
|
||||||
|
let mut ret = self;
|
||||||
|
if !ret.is_zero() {
|
||||||
|
ret.y = ret.y.neg();
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'r> ::std::ops::Add<&'r $projective> for $projective {
|
impl<'r> ::std::ops::Add<&'r $projective> for $projective {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
|
@ -324,9 +344,7 @@ macro_rules! curve_impl {
|
||||||
|
|
||||||
impl<'r> ::std::ops::SubAssign<&'r $projective> for $projective {
|
impl<'r> ::std::ops::SubAssign<&'r $projective> for $projective {
|
||||||
fn sub_assign(&mut self, other: &Self) {
|
fn sub_assign(&mut self, other: &Self) {
|
||||||
let mut tmp = *other;
|
self.add_assign(&other.neg());
|
||||||
tmp.negate();
|
|
||||||
self.add_assign(&tmp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -566,12 +584,6 @@ macro_rules! curve_impl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn negate(&mut self) {
|
|
||||||
if !self.is_zero() {
|
|
||||||
self.y = self.y.neg();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S) {
|
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S) {
|
||||||
let mut res = Self::zero();
|
let mut res = Self::zero();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue