Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Unnecessary PhantomData #310

Merged
merged 3 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

### Improvements

- [\#310](https://github.com/arkworks-rs/algebra/pull/310) (ark-ec) Remove unnecessary internal `PhantomData`
- [\#310](https://github.com/arkworks-rs/algebra/pull/310) (ark-ff) Remove unnecessary internal `PhantomData`

### Bug fixes

## v0.3.0
Expand Down
20 changes: 9 additions & 11 deletions ec/src/models/mnt4/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use {
crate::{
models::{ModelParameters, SWModelParameters},
PairingEngine,
},
ark_ff::{
fp2::{Fp2, Fp2Parameters},
fp4::{Fp4, Fp4Parameters},
BitIteratorBE, Field, PrimeField, SquareRootField,
},
num_traits::{One, Zero},
use crate::{
models::{ModelParameters, SWModelParameters},
PairingEngine,
};
use ark_ff::{
fp2::{Fp2, Fp2Parameters},
fp4::{Fp4, Fp4Parameters},
BitIteratorBE, Field, PrimeField, SquareRootField,
};
use num_traits::{One, Zero};

use core::marker::PhantomData;

Expand Down
20 changes: 9 additions & 11 deletions ec/src/models/mnt6/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use {
crate::{
models::{ModelParameters, SWModelParameters},
PairingEngine,
},
ark_ff::{
fp3::{Fp3, Fp3Parameters},
fp6_2over3::{Fp6, Fp6Parameters},
BitIteratorBE, Field, PrimeField, SquareRootField,
},
num_traits::{One, Zero},
use crate::{
models::{ModelParameters, SWModelParameters},
PairingEngine,
};
use ark_ff::{
fp3::{Fp3, Fp3Parameters},
fp6_2over3::{Fp6, Fp6Parameters},
BitIteratorBE, Field, PrimeField, SquareRootField,
};
use num_traits::{One, Zero};

use core::marker::PhantomData;

Expand Down
36 changes: 10 additions & 26 deletions ec/src/models/short_weierstrass_jacobian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ark_std::{
fmt::{Display, Formatter, Result as FmtResult},
hash::{Hash, Hasher},
io::{Read, Result as IoResult, Write},
marker::PhantomData,
ops::{Add, AddAssign, MulAssign, Neg, Sub, SubAssign},
vec::Vec,
};
Expand Down Expand Up @@ -46,8 +45,6 @@ pub struct GroupAffine<P: Parameters> {
pub x: P::BaseField,
pub y: P::BaseField,
pub infinity: bool,
#[derivative(Debug = "ignore")]
_params: PhantomData<P>,
}

impl<P: Parameters> PartialEq<GroupProjective<P>> for GroupAffine<P> {
Expand All @@ -74,12 +71,7 @@ impl<P: Parameters> Display for GroupAffine<P> {

impl<P: Parameters> GroupAffine<P> {
pub fn new(x: P::BaseField, y: P::BaseField, infinity: bool) -> Self {
Self {
x,
y,
infinity,
_params: PhantomData,
}
Self { x, y, infinity }
}

/// Multiply `self` by the cofactor of the curve, `P::COFACTOR`.
Expand All @@ -88,8 +80,8 @@ impl<P: Parameters> GroupAffine<P> {
self.mul_bits(cofactor)
}

/// Multiplies `self` by the scalar represented by `bits`. `bits` must be a big-endian
/// bit-wise decomposition of the scalar.
/// Multiplies `self` by the scalar represented by `bits`. `bits` must be a
/// big-endian bit-wise decomposition of the scalar.
pub(crate) fn mul_bits(&self, bits: impl Iterator<Item = bool>) -> GroupProjective<P> {
let mut res = GroupProjective::zero();
// Skip leading zeros.
Expand Down Expand Up @@ -293,8 +285,8 @@ impl<'a, P: Parameters> core::iter::Sum<&'a Self> for GroupAffine<P> {
}
}

/// Jacobian coordinates for a point on an elliptic curve in short Weierstrass form,
/// over the base field `P::BaseField`. This struct implements arithmetic
/// Jacobian coordinates for a point on an elliptic curve in short Weierstrass
/// form, over the base field `P::BaseField`. This struct implements arithmetic
/// via the Jacobian formulae
#[derive(Derivative)]
#[derivative(
Expand All @@ -307,8 +299,6 @@ pub struct GroupProjective<P: Parameters> {
pub x: P::BaseField,
pub y: P::BaseField,
pub z: P::BaseField,
#[derivative(Debug = "ignore")]
_params: PhantomData<P>,
}

impl<P: Parameters> Display for GroupProjective<P> {
Expand Down Expand Up @@ -390,18 +380,12 @@ impl<P: Parameters> Default for GroupProjective<P> {

impl<P: Parameters> GroupProjective<P> {
pub fn new(x: P::BaseField, y: P::BaseField, z: P::BaseField) -> Self {
Self {
x,
y,
z,
_params: PhantomData,
}
Self { x, y, z }
}
}

impl<P: Parameters> Zeroize for GroupProjective<P> {
fn zeroize(&mut self) {
// `PhantomData` does not contain any data and thus does not need to be zeroized.
self.x.zeroize();
self.y.zeroize();
self.z.zeroize();
Expand Down Expand Up @@ -469,8 +453,8 @@ impl<P: Parameters> ProjectiveCurve for GroupProjective<P> {
}

/// Sets `self = 2 * self`. Note that Jacobian formulae are incomplete, and
/// so doubling cannot be computed as `self + self`. Instead, this implementation
/// uses the following specialized doubling formulae:
/// so doubling cannot be computed as `self + self`. Instead, this
/// implementation uses the following specialized doubling formulae:
/// * [`P::A` is zero](http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l)
/// * [`P::A` is not zero](https://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-2007-bl)
fn double_in_place(&mut self) -> &mut Self {
Expand Down Expand Up @@ -541,8 +525,8 @@ impl<P: Parameters> ProjectiveCurve for GroupProjective<P> {
}
}

/// When `other.is_normalized()` (i.e., `other.z == 1`), we can use a more efficient
/// [formula](http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl)
/// When `other.is_normalized()` (i.e., `other.z == 1`), we can use a more
/// efficient [formula](http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl)
/// to compute `self + other`.
fn add_assign_mixed(&mut self, other: &GroupAffine<P>) {
if other.is_zero() {
Expand Down
39 changes: 9 additions & 30 deletions ec/src/models/twisted_edwards_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
CanonicalSerializeWithFlags, EdwardsFlags, SerializationError,
};
use ark_std::rand::{
distributions::{Distribution, Standard},
Rng,
};
use ark_std::{
fmt::{Display, Formatter, Result as FmtResult},
hash::{Hash, Hasher},
io::{Read, Result as IoResult, Write},
marker::PhantomData,
ops::{Add, AddAssign, MulAssign, Neg, Sub, SubAssign},
rand::{
distributions::{Distribution, Standard},
Rng,
},
vec::Vec,
};
use num_traits::{One, Zero};
Expand Down Expand Up @@ -43,8 +42,6 @@ use rayon::prelude::*;
pub struct GroupAffine<P: Parameters> {
pub x: P::BaseField,
pub y: P::BaseField,
#[derivative(Debug = "ignore")]
_params: PhantomData<P>,
}

impl<P: Parameters> Display for GroupAffine<P> {
Expand All @@ -55,20 +52,16 @@ impl<P: Parameters> Display for GroupAffine<P> {

impl<P: Parameters> GroupAffine<P> {
pub fn new(x: P::BaseField, y: P::BaseField) -> Self {
Self {
x,
y,
_params: PhantomData,
}
Self { x, y }
}

#[must_use]
pub fn scale_by_cofactor(&self) -> <Self as AffineCurve>::Projective {
self.mul_bits(BitIteratorBE::new(P::COFACTOR))
}

/// Multiplies `self` by the scalar represented by `bits`. `bits` must be a big-endian
/// bit-wise decomposition of the scalar.
/// Multiplies `self` by the scalar represented by `bits`. `bits` must be a
/// big-endian bit-wise decomposition of the scalar.
pub(crate) fn mul_bits(&self, bits: impl Iterator<Item = bool>) -> GroupProjective<P> {
let mut res = GroupProjective::zero();
for i in bits.skip_while(|b| !b) {
Expand Down Expand Up @@ -311,8 +304,6 @@ pub struct GroupProjective<P: Parameters> {
pub y: P::BaseField,
pub t: P::BaseField,
pub z: P::BaseField,
#[derivative(Debug = "ignore")]
_params: PhantomData<P>,
}

impl<P: Parameters> PartialEq<GroupProjective<P>> for GroupAffine<P> {
Expand Down Expand Up @@ -398,13 +389,7 @@ impl<P: Parameters> Default for GroupProjective<P> {

impl<P: Parameters> GroupProjective<P> {
pub fn new(x: P::BaseField, y: P::BaseField, t: P::BaseField, z: P::BaseField) -> Self {
Self {
x,
y,
t,
z,
_params: PhantomData,
}
Self { x, y, t, z }
}
}
impl<P: Parameters> Zeroize for GroupProjective<P> {
Expand Down Expand Up @@ -695,8 +680,6 @@ where
pub struct MontgomeryGroupAffine<P: MontgomeryParameters> {
pub x: P::BaseField,
pub y: P::BaseField,
#[derivative(Debug = "ignore")]
_params: PhantomData<P>,
}

impl<P: MontgomeryParameters> Display for MontgomeryGroupAffine<P> {
Expand All @@ -707,11 +690,7 @@ impl<P: MontgomeryParameters> Display for MontgomeryGroupAffine<P> {

impl<P: MontgomeryParameters> MontgomeryGroupAffine<P> {
pub fn new(x: P::BaseField, y: P::BaseField) -> Self {
Self {
x,
y,
_params: PhantomData,
}
Self { x, y }
}
}

Expand Down
3 changes: 1 addition & 2 deletions ec/src/msm/fixed_base.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{AffineCurve, ProjectiveCurve};
use ark_ff::{BigInteger, FpParameters, PrimeField};
use ark_std::vec::Vec;
use ark_std::{cfg_iter, cfg_iter_mut};
use ark_std::{cfg_iter, cfg_iter_mut, vec::Vec};

#[cfg(feature = "parallel")]
use rayon::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion ff-asm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn generate_llvm_asm_mul_string(
) -> String {
let llvm_asm_string = RefCell::new(String::new());

let begin = || llvm_asm_string.borrow_mut().push_str("\"");
let begin = || llvm_asm_string.borrow_mut().push('\"');

let end = || {
llvm_asm_string.borrow_mut().push_str(
Expand Down
31 changes: 17 additions & 14 deletions ff-asm/src/unroll.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
//! An attribute-like procedural macro for unrolling for loops with integer literal bounds.
//! An attribute-like procedural macro for unrolling for loops with integer
//! literal bounds.
//!
//! This crate provides the [`unroll_for_loops`](../attr.unroll_for_loops.html) attribute-like macro that can be applied to
//! functions containing for-loops with integer bounds. This macro looks for loops to unroll and
//! unrolls them at compile time.
//! This crate provides the [`unroll_for_loops`](../attr.unroll_for_loops.html)
//! attribute-like macro that can be applied to functions containing for-loops
//! with integer bounds. This macro looks for loops to unroll and unrolls them
//! at compile time.
//!
//!
//! ## Usage
//!
//! Just add `#[unroll_for_loops]` above the function whose for loops you would like to unroll.
//! Currently all for loops with integer literal bounds will be unrolled, although this macro
//! currently can't see inside complex code (e.g. for loops within closures).
//! Just add `#[unroll_for_loops]` above the function whose for loops you would
//! like to unroll. Currently all for loops with integer literal bounds will be
//! unrolled, although this macro currently can't see inside complex code (e.g.
//! for loops within closures).
//!
//!
//! ## Example
//!
//! The following function computes a matrix-vector product and returns the result as an array.
//! Both of the inner for-loops are unrolled when `#[unroll_for_loops]` is applied.
//! The following function computes a matrix-vector product and returns the
//! result as an array. Both of the inner for-loops are unrolled when
//! `#[unroll_for_loops]` is applied.
//!
//! ```rust
//! use ark_ff_asm::unroll_for_loops;
Expand All @@ -34,10 +38,9 @@
//!
//! This code was adapted from the [`unroll`](https://crates.io/crates/unroll) crate.

use syn::token::Brace;
use syn::{
parse_quote, Block, Expr, ExprBlock, ExprForLoop, ExprIf, ExprLet, ExprLit, ExprRange, Lit,
Pat, PatIdent, RangeLimits, Stmt,
parse_quote, token::Brace, Block, Expr, ExprBlock, ExprForLoop, ExprIf, ExprLet, ExprLit,
ExprRange, Lit, Pat, PatIdent, RangeLimits, Stmt,
};

/// Routine to unroll for loops within a block
Expand All @@ -62,8 +65,8 @@ pub(crate) fn unroll_in_block(block: &Block) -> Block {
}
}

/// Routine to unroll a for loop statement, or return the statement unchanged if it's not a for
/// loop.
/// Routine to unroll a for loop statement, or return the statement unchanged if
/// it's not a for loop.
fn unroll(expr: &Expr) -> Expr {
// impose a scope that we can break out of so we can return stmt without copying it.
if let Expr::ForLoop(for_loop) = expr {
Expand Down
8 changes: 4 additions & 4 deletions ff/src/biginteger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use crate::{
UniformRand,
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
use ark_std::rand::{
distributions::{Distribution, Standard},
Rng,
};
use ark_std::{
convert::TryFrom,
fmt::{Debug, Display},
io::{Read, Result as IoResult, Write},
rand::{
distributions::{Distribution, Standard},
Rng,
},
vec::Vec,
};
use num_bigint::BigUint;
Expand Down
14 changes: 4 additions & 10 deletions ff/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,13 @@ macro_rules! field_new {
)
}};
($name:ident, $c0:expr, $c1:expr $(,)?) => {
$name {
c0: $c0,
c1: $c1,
_parameters: core::marker::PhantomData,
}
$name { c0: $c0, c1: $c1 }
};
($name:ident, $c0:expr, $c1:expr, $c2:expr $(,)?) => {
$name {
c0: $c0,
c1: $c1,
c2: $c2,
_parameters: core::marker::PhantomData,
}
};
}
Expand Down Expand Up @@ -698,11 +693,10 @@ mod no_std_tests {
#[test]
fn test_from_be_bytes_mod_order() {
// Each test vector is a byte array,
// and its tested by parsing it with from_bytes_mod_order, and the num-bigint library.
// The bytes are currently generated from scripts/test_vectors.py.
// and its tested by parsing it with from_bytes_mod_order, and the num-bigint
// library. The bytes are currently generated from scripts/test_vectors.py.
// TODO: Eventually generate all the test vector bytes via computation with the modulus
use ark_std::rand::Rng;
use ark_std::string::ToString;
use ark_std::{rand::Rng, string::ToString};
use num_bigint::BigUint;

let ref_modulus =
Expand Down
Loading