Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ edition = "2021"
[dependencies]
bitvec = { version = "1", default-features = false, optional = true }
byteorder = { version = "1", default-features = false, optional = true }
ff_derive = { version = "0.13", path = "ff_derive", optional = true }
ff_derive = { version = "0.13.0", path = "ff_derive", optional = true }
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated
rand_core = { version = "0.6", default-features = false }
subtle = { version = "2.2.1", default-features = false, features = ["i128"] }
zeroize = { version = "1", default-features = false, optional = true }
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated

[dev-dependencies]
blake2b_simd = "1"
Expand All @@ -28,16 +29,22 @@ rand = "0.8"
default = ["bits", "std"]
alloc = []
bits = ["bitvec"]
zero = ["zeroize"]
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated
derive = ["byteorder", "ff_derive"]
std = ["alloc"]
# with MSRV 1.60 this could be merged into bits with ff_derive?/bits
# see PR#72 for more information.
derive_bits = ["bits", "ff_derive/bits"]
derive_bits = ["bits", "derive", "ff_derive/bits"]
derive_zero = ["zero", "derive", "ff_derive/zero"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike above, we do need the ability to enable the ff_derive feature flag for tests. I'm inclined to do so via a dedicated test feature flag:

Suggested change
derive_zero = ["zero", "derive", "ff_derive/zero"]
test-derive = ["derive", "ff_derive/zeroize"]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with test_derive_zeroize, but I can change that if you want.


[[test]]
name = "derive"
required-features = ["derive"]

[[test]]
name = "derive_zero"
required-features = ["derive_zero"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
required-features = ["derive"]
[[test]]
name = "derive_zero"
required-features = ["derive_zero"]
required-features = ["test-derive"]


[badges]
maintenance = { status = "actively-developed" }

Expand Down
2 changes: 2 additions & 0 deletions ff_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ edition = "2021"
[features]
# enabled when generating bitvec code utilizing the version of ff's bitvec
bits = []
# enabled when generating zeroize code utilizing the version of ff's zeroize
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated
zero = []
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated

[lib]
proc-macro = true
Expand Down
19 changes: 19 additions & 0 deletions ff_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,14 @@ fn prime_field_repr_impl(
) -> proc_macro2::TokenStream {
let repr_iter_be = endianness.iter_be();

let prime_field_repr_zero_impl = if cfg!(feature = "zero") {
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated
Some(quote! {
impl ::zeroize::DefaultIsZeroes for #repr {}
})
} else {
None
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
};

quote! {
#[derive(Copy, Clone)]
pub struct #repr(pub [u8; #bytes]);
Expand Down Expand Up @@ -386,6 +394,8 @@ fn prime_field_repr_impl(
&mut self.0
}
}

#prime_field_repr_zero_impl
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated
}
}

Expand Down Expand Up @@ -942,6 +952,14 @@ fn prime_field_impl(
}
};

let prime_field_zero_impl = if cfg!(feature = "zero") {
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated
Some(quote! {
impl ::zeroize::DefaultIsZeroes for #name {}
})
} else {
None
};

let top_limb_index = limbs - 1;

quote! {
Expand Down Expand Up @@ -1261,6 +1279,7 @@ fn prime_field_impl(
}

#prime_field_bits_impl
#prime_field_zero_impl
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated

impl ::ff::Field for #name {
const ZERO: Self = #name([0; #limbs]);
Expand Down
28 changes: 28 additions & 0 deletions tests/derive_zero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! This module exercises the `ff_derive` procedural macros, specifically to ensure
//! zeroize works

#[macro_use]
extern crate ff;

/// The BLS12-381 scalar field.
#[derive(PrimeField)]
#[PrimeFieldModulus = "52435875175126190479447740508185965837690552500527637822603658699938581184513"]
#[PrimeFieldGenerator = "7"]
#[PrimeFieldReprEndianness = "little"]
struct Bls381K12Scalar([u64; 4]);

#[test]
fn zeroize() {
Comment thread
Daniel-Aaron-Bloom marked this conversation as resolved.
Outdated
use ff::{Field, PrimeField};
use rand::rngs::OsRng;
use zeroize::Zeroize;

let mut f = Bls381K12Scalar::random(OsRng);
let mut r = f.to_repr();

f.zeroize();
assert_eq!(f, Bls381K12Scalar::ZERO);

r.zeroize();
assert_eq!(r, Default::default());
}