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

Add Zeroize to fields and elliptic curves #106

Merged
merged 4 commits into from
Dec 3, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- #51 (ark-ff) Removed `unitary_inverse` from `QuadExtField`. Make this change by
replacing `x.unitary_inverse()` with `let mut tmp = x.clone(); tmp.conjugate()`
- #53 (ark-poly) Add `Zero` trait bound to `Polynomial`.
- #106 (ark-ff, ark-ec) Add `Zeroize` trait bound to `Field, ProjectiveGroup, AffineGroup` traits.

### Features
- #20 (ark-poly) Add structs/traits for multivariate polynomials
Expand Down
1 change: 1 addition & 0 deletions ec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ derivative = { version = "2", features = ["use_core"] }
num-traits = { version = "0.2", default-features = false }
rand = { version = "0.7", default-features = false }
rayon = { version = "1", optional = true }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"] }

[dev-dependencies]
rand_xorshift = "0.2"
Expand Down
3 changes: 3 additions & 0 deletions ec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use ark_std::{
vec::Vec,
};
use num_traits::Zero;
use zeroize::Zeroize;

pub mod models;
pub use self::models::*;
Expand Down Expand Up @@ -133,6 +134,7 @@ pub trait ProjectiveCurve:
+ Debug
+ Display
+ UniformRand
+ Zeroize
+ Zero
+ Neg<Output = Self>
+ Add<Self, Output = Self>
Expand Down Expand Up @@ -239,6 +241,7 @@ pub trait AffineCurve:
+ Display
+ Zero
+ Neg<Output = Self>
+ Zeroize
+ From<<Self as AffineCurve>::Projective>
{
const COFACTOR: &'static [u64];
Expand Down
21 changes: 21 additions & 0 deletions ec/src/models/short_weierstrass_jacobian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ark_ff::{
use crate::{models::SWModelParameters as Parameters, AffineCurve, ProjectiveCurve};

use num_traits::{One, Zero};
use zeroize::Zeroize;

use rand::{
distributions::{Distribution, Standard},
Expand Down Expand Up @@ -132,6 +133,16 @@ impl<P: Parameters> GroupAffine<P> {
}
}

impl<P: Parameters> Zeroize for GroupAffine<P> {
// The phantom data does not contain element-specific data
// and thus does not need to be zeroized.
fn zeroize(&mut self) {
self.x.zeroize();
self.y.zeroize();
self.infinity.zeroize();
}
}

impl<P: Parameters> Zero for GroupAffine<P> {
#[inline]
fn zero() -> Self {
Expand Down Expand Up @@ -340,6 +351,16 @@ impl<P: Parameters> GroupProjective<P> {
}
}

impl<P: Parameters> Zeroize for GroupProjective<P> {
// The phantom data does not contain element-specific data
// and thus does not need to be zeroized.
fn zeroize(&mut self) {
self.x.zeroize();
self.y.zeroize();
self.z.zeroize();
}
}

impl<P: Parameters> Zero for GroupProjective<P> {
// The point at infinity is always represented by
// Z = 0.
Expand Down
20 changes: 20 additions & 0 deletions ec/src/models/twisted_edwards_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rand::{
distributions::{Distribution, Standard},
Rng,
};
use zeroize::Zeroize;

use ark_ff::{
bytes::{FromBytes, ToBytes},
Expand Down Expand Up @@ -164,6 +165,15 @@ impl<P: Parameters> AffineCurve for GroupAffine<P> {
}
}

impl<P: Parameters> Zeroize for GroupAffine<P> {
// The phantom data does not contain element-specific data
// and thus does not need to be zeroized.
fn zeroize(&mut self) {
self.x.zeroize();
self.y.zeroize();
}
}

impl<P: Parameters> Neg for GroupAffine<P> {
type Output = Self;

Expand Down Expand Up @@ -393,6 +403,16 @@ impl<P: Parameters> GroupProjective<P> {
}
}
}
impl<P: Parameters> Zeroize for GroupProjective<P> {
// The phantom data does not contain element-specific data
// and thus does not need to be zeroized.
fn zeroize(&mut self) {
self.x.zeroize();
self.y.zeroize();
self.t.zeroize();
self.z.zeroize();
}
}

impl<P: Parameters> Zero for GroupProjective<P> {
fn zero() -> Self {
Expand Down
1 change: 1 addition & 0 deletions ff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ derivative = { version = "2", features = ["use_core"] }
num-traits = { version = "0.2", default-features = false }
rand = { version = "0.7", default-features = false }
rayon = { version = "1", optional = true }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"] }

[build-dependencies]
rustc_version = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion ff/src/biginteger/macros.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
macro_rules! bigint_impl {
($name:ident, $num_limbs:expr) => {
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Hash, Zeroize)]
pub struct $name(pub [u64; $num_limbs]);

impl $name {
Expand Down
2 changes: 2 additions & 0 deletions ff/src/biginteger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rand::{
distributions::{Distribution, Standard},
Rng,
};
use zeroize::Zeroize;

#[macro_use]
mod macros;
Expand Down Expand Up @@ -50,6 +51,7 @@ pub trait BigInteger:
+ Sync
+ 'static
+ UniformRand
+ Zeroize
+ AsMut<[u64]>
+ AsRef<[u64]>
+ From<u64>
Expand Down
8 changes: 8 additions & 0 deletions ff/src/fields/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,5 +544,13 @@ macro_rules! impl_Fp {
self.mul_assign(&other.inverse().unwrap());
}
}

impl<P: $FpParameters> zeroize::Zeroize for $Fp<P> {
// The phantom data does not contain element-specific data
// and thus does not need to be zeroized.
fn zeroize(&mut self) {
self.0.zeroize();
}
}
}
}
4 changes: 3 additions & 1 deletion ff/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use ark_std::{
};

use num_traits::{One, Zero};
use zeroize::Zeroize;

#[macro_use]
pub mod macros;
Expand Down Expand Up @@ -66,11 +67,12 @@ pub trait Field:
+ Send
+ Sync
+ Eq
+ Zero
+ One
+ Ord
+ Neg<Output = Self>
+ UniformRand
+ Zero
+ Zeroize
+ Sized
+ Hash
+ CanonicalSerialize
Expand Down
11 changes: 11 additions & 0 deletions ff/src/fields/models/cubic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ark_std::{
};

use num_traits::{One, Zero};
use zeroize::Zeroize;

use rand::{
distributions::{Distribution, Standard},
Expand Down Expand Up @@ -277,6 +278,16 @@ impl<P: CubicExtParameters> PartialOrd for CubicExtField<P> {
}
}

impl<P: CubicExtParameters> Zeroize for CubicExtField<P> {
// The phantom data does not contain element-specific data
// and thus does not need to be zeroized.
fn zeroize(&mut self) {
self.c0.zeroize();
self.c1.zeroize();
self.c2.zeroize();
}
}

impl<P: CubicExtParameters> From<u128> for CubicExtField<P> {
fn from(other: u128) -> Self {
let fe: P::BaseField = other.into();
Expand Down
10 changes: 10 additions & 0 deletions ff/src/fields/models/quadratic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ark_std::{
};

use num_traits::{One, Zero};
use zeroize::Zeroize;

use rand::{
distributions::{Distribution, Standard},
Expand Down Expand Up @@ -330,6 +331,15 @@ impl<P: QuadExtParameters> PartialOrd for QuadExtField<P> {
}
}

impl<P: QuadExtParameters> Zeroize for QuadExtField<P> {
// The phantom data does not contain element-specific data
// and thus does not need to be zeroized.
fn zeroize(&mut self) {
self.c0.zeroize();
self.c1.zeroize();
}
}

impl<P: QuadExtParameters> From<u128> for QuadExtField<P> {
fn from(other: u128) -> Self {
Self::new(other.into(), P::BaseField::zero())
Expand Down