Skip to content

Update to PrimeField::from_repr() changes #1209

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
12 changes: 4 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
# https://github.com/RustCrypto/signatures/pull/972
# https://github.com/RustCrypto/traits/pull/1869
# https://github.com/zkcrypto/ff/pull/137
ecdsa = { git = "https://github.com/RustCrypto/signatures.git", rev = "2adc05328a2d7fb6f62421748f8e340936d3a18d" }
elliptic-curve = { git = "https://github.com/RustCrypto/traits.git", rev = "2ec3e144f69af5d3836d5d2b545b36105f6d69f9" }
ff = { git = "https://github.com/zkcrypto/ff.git", rev = "8e139e2fb25ab61a5d362394af0a34b10c03d59b" }
6 changes: 3 additions & 3 deletions bign256/benches/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ use hex_literal::hex;

fn test_scalar_x() -> Scalar {
Scalar::from_repr(
hex!("519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464").into(),
&hex!("519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464").into(),
)
.unwrap()
}

fn test_scalar_y() -> Scalar {
Scalar::from_repr(
hex!("0f56db78ca460b055c500064824bed999a25aaf48ebb519ac201537b85479813").into(),
&hex!("0f56db78ca460b055c500064824bed999a25aaf48ebb519ac201537b85479813").into(),
)
.unwrap()
}

fn bench_point_mul<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let p = ProjectivePoint::GENERATOR;
let m = test_scalar_x();
let s = Scalar::from_repr(m.into()).unwrap();
let s = Scalar::from_repr(&m.into()).unwrap();
group.bench_function("point-scalar mul", |b| b.iter(|| p * s));
}

Expand Down
4 changes: 2 additions & 2 deletions bign256/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ impl PrimeField for FieldElement {
type Repr = FieldBytes;

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions bign256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl IsHigh for Scalar {
impl PrimeField for Scalar {
type Repr = FieldBytes;

fn from_repr(repr: Self::Repr) -> CtOption<Self> {
Self::from_bytes(&repr)
fn from_repr(repr: &Self::Repr) -> CtOption<Self> {
Self::from_bytes(repr)
}

fn to_repr(&self) -> Self::Repr {
Expand Down
2 changes: 1 addition & 1 deletion bign256/src/ecdsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl PrehashSigner<Signature> for SigningKey {
let h = Scalar::reduce_bytes(&h_word);

//2. Generate 𝑘 ← rand(1,..,𝑞-1)
let k = Scalar::from_repr(rfc6979::generate_k::<BeltHash, _>(
let k = Scalar::from_repr(&rfc6979::generate_k::<BeltHash, _>(
&self.secret_scalar.to_repr(),
&FieldBytesEncoding::<BignP256>::encode_field_bytes(&BignP256::ORDER),
&h.to_bytes(),
Expand Down
4 changes: 2 additions & 2 deletions bp256/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ impl PrimeField for FieldElement {
const DELTA: Self = Self::from_u64(121);

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions bp256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ impl PrimeField for Scalar {
const DELTA: Self = Self::from_u64(9);

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions bp384/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ impl PrimeField for FieldElement {
const DELTA: Self = Self::from_u64(9);

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions bp384/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ impl PrimeField for Scalar {
const DELTA: Self = Self::from_u64(16);

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion k256/benches/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::hint::black_box;
fn test_scalar_d() -> NonZeroScalar {
NonZeroScalar::new(
Scalar::from_repr(
[
&[
0xbb, 0x48, 0x8a, 0xef, 0x41, 0x6a, 0x41, 0xd7, 0x68, 0x0d, 0x1c, 0xf0, 0x1d, 0x70,
0xf5, 0x9b, 0x60, 0xd7, 0xf5, 0xf7, 0x7e, 0x30, 0xe7, 0x8b, 0x8b, 0xf9, 0xd2, 0xd8,
0x82, 0xf1, 0x56, 0xa6,
Expand Down
8 changes: 4 additions & 4 deletions k256/benches/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::hint::black_box;

fn test_scalar_x() -> Scalar {
Scalar::from_repr(
[
&[
0xbb, 0x48, 0x8a, 0xef, 0x41, 0x6a, 0x41, 0xd7, 0x68, 0x0d, 0x1c, 0xf0, 0x1d, 0x70,
0xf5, 0x9b, 0x60, 0xd7, 0xf5, 0xf7, 0x7e, 0x30, 0xe7, 0x8b, 0x8b, 0xf9, 0xd2, 0xd8,
0x82, 0xf1, 0x56, 0xa6,
Expand All @@ -24,7 +24,7 @@ fn test_scalar_x() -> Scalar {

fn test_scalar_y() -> Scalar {
Scalar::from_repr(
[
&[
0x67, 0xe2, 0xf6, 0x80, 0x71, 0xed, 0x82, 0x81, 0xe8, 0xae, 0xd6, 0xbc, 0xf1, 0xc5,
0x20, 0x7c, 0x5e, 0x63, 0x37, 0x22, 0xd9, 0x20, 0xaf, 0xd6, 0xae, 0x22, 0xd0, 0x6e,
0xeb, 0x80, 0x35, 0xe3,
Expand All @@ -37,7 +37,7 @@ fn test_scalar_y() -> Scalar {
fn bench_point_mul<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
let p = ProjectivePoint::GENERATOR;
let m = hex!("AA5E28D6A97A2479A65527F7290311A3624D4CC0FA1578598EE3C2613BF99522");
let s = Scalar::from_repr(m.into()).unwrap();
let s = Scalar::from_repr(&m.into()).unwrap();
group.bench_function("point-scalar mul", |b| {
b.iter(|| black_box(p) * black_box(s))
});
Expand All @@ -46,7 +46,7 @@ fn bench_point_mul<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
fn bench_point_lincomb<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
let p = ProjectivePoint::GENERATOR;
let m = hex!("AA5E28D6A97A2479A65527F7290311A3624D4CC0FA1578598EE3C2613BF99522");
let s = Scalar::from_repr(m.into()).unwrap();
let s = Scalar::from_repr(&m.into()).unwrap();
group.bench_function("lincomb via mul+add", |b| {
b.iter(|| black_box(p) * black_box(s) + black_box(p) * black_box(s))
});
Expand Down
4 changes: 2 additions & 2 deletions k256/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ impl PrimeField for FieldElement {
]));
const DELTA: Self = Self::from_u64(9);

fn from_repr(repr: Self::Repr) -> CtOption<Self> {
Self::from_bytes(&repr)
fn from_repr(repr: &Self::Repr) -> CtOption<Self> {
Self::from_bytes(repr)
}

fn to_repr(&self) -> Self::Repr {
Expand Down
2 changes: 1 addition & 1 deletion k256/src/arithmetic/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ mod tests {
MUL_TEST_VECTORS
.iter()
.cloned()
.map(|(k, x, y)| (Scalar::from_repr(k.into()).unwrap(), (x, y))),
.map(|(k, x, y)| (Scalar::from_repr(&k.into()).unwrap(), (x, y))),
)
{
let res = (generator * &k).to_affine();
Expand Down
10 changes: 5 additions & 5 deletions k256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl Field for Scalar {
// TODO: pre-generate several scalars to bring the probability of non-constant-timeness down?
loop {
rng.try_fill_bytes(&mut bytes)?;
if let Some(scalar) = Scalar::from_repr(bytes).into() {
if let Some(scalar) = Scalar::from_repr(&bytes).into() {
return Ok(scalar);
}
}
Expand Down Expand Up @@ -329,8 +329,8 @@ impl PrimeField for Scalar {
///
/// Returns None if the byte array does not contain a big-endian integer in the range
/// [0, p).
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
let inner = U256::from_be_byte_array(bytes);
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
let inner = U256::from_be_byte_array(*bytes);
CtOption::new(Self(inner), inner.ct_lt(&Secp256k1::ORDER))
}

Expand Down Expand Up @@ -904,7 +904,7 @@ mod tests {
fn from(x: &BigUint) -> Self {
debug_assert!(x < &Scalar::modulus_as_biguint());
let bytes = biguint_to_bytes(x);
Self::from_repr(bytes.into()).unwrap()
Self::from_repr(&bytes.into()).unwrap()
}
}

Expand Down Expand Up @@ -1221,7 +1221,7 @@ mod tests {
proptest! {
#[test]
fn fuzzy_roundtrip_to_bytes(a in scalar()) {
let a_back = Scalar::from_repr(a.to_bytes()).unwrap();
let a_back = Scalar::from_repr(&a.to_bytes()).unwrap();
assert_eq!(a, a_back);
}

Expand Down
4 changes: 2 additions & 2 deletions p192/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ impl PrimeField for FieldElement {
const DELTA: Self = Self::from_u64(121);

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions p192/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ impl PrimeField for Scalar {
const DELTA: Self = Self::from_u64(43046721);

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions p224/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ impl PrimeField for FieldElement {
Self::from_hex("00000000697b16135c4a62fca5c4f35ea6d5784cf3808e775aad34ec3d046867");

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions p224/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ impl PrimeField for Scalar {
const DELTA: Self = Self::from_u64(16);

#[inline]
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions p256/benches/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ use p256::{ProjectivePoint, Scalar, elliptic_curve::group::ff::PrimeField};

fn test_scalar_x() -> Scalar {
Scalar::from_repr(
hex!("519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464").into(),
&hex!("519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464").into(),
)
.unwrap()
}

fn test_scalar_y() -> Scalar {
Scalar::from_repr(
hex!("0f56db78ca460b055c500064824bed999a25aaf48ebb519ac201537b85479813").into(),
&hex!("0f56db78ca460b055c500064824bed999a25aaf48ebb519ac201537b85479813").into(),
)
.unwrap()
}

fn bench_point_mul<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
let p = ProjectivePoint::GENERATOR;
let m = test_scalar_x();
let s = Scalar::from_repr(m.into()).unwrap();
let s = Scalar::from_repr(&m.into()).unwrap();
group.bench_function("point-scalar mul", |b| b.iter(|| p * s));
}

Expand Down
4 changes: 2 additions & 2 deletions p256/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ impl PrimeField for FieldElement {
const ROOT_OF_UNITY_INV: Self = Self::ROOT_OF_UNITY.invert_unchecked();
const DELTA: Self = Self::from_u64(36);

fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
Self::from_bytes(&bytes)
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
Self::from_bytes(bytes)
}

fn to_repr(&self) -> FieldBytes {
Expand Down
8 changes: 4 additions & 4 deletions p256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl Field for Scalar {
// iterations is vanishingly small.
loop {
rng.try_fill_bytes(&mut bytes)?;
if let Some(scalar) = Scalar::from_repr(bytes).into() {
if let Some(scalar) = Scalar::from_repr(&bytes).into() {
return Ok(scalar);
}
}
Expand Down Expand Up @@ -292,8 +292,8 @@ impl PrimeField for Scalar {
///
/// Returns None if the byte array does not contain a big-endian integer in the range
/// [0, p).
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
let inner = U256::from_be_byte_array(bytes);
fn from_repr(bytes: &FieldBytes) -> CtOption<Self> {
let inner = U256::from_be_byte_array(*bytes);
CtOption::new(Self(inner), inner.ct_lt(&NistP256::ORDER))
}

Expand Down Expand Up @@ -736,7 +736,7 @@ mod tests {
let mut bytes = FieldBytes::default();
bytes[24..].copy_from_slice(k.to_be_bytes().as_ref());

let scalar = Scalar::from_repr(bytes).unwrap();
let scalar = Scalar::from_repr(&bytes).unwrap();
assert_eq!(bytes, scalar.to_bytes());
}

Expand Down
6 changes: 3 additions & 3 deletions p384/benches/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ use hex_literal::hex;
use p384::{ProjectivePoint, Scalar, elliptic_curve::group::ff::PrimeField};

fn test_scalar_x() -> Scalar {
Scalar::from_repr(
Scalar::from_repr(&
hex!("201b432d8df14324182d6261db3e4b3f46a8284482d52e370da41e6cbdf45ec2952f5db7ccbce3bc29449f4fb080ac97").into()
).unwrap()
}

fn test_scalar_y() -> Scalar {
Scalar::from_repr(
Scalar::from_repr(&
hex!("23d9f4ea6d87b7d6163d64256e3449255db14786401a51daa7847161bf56d494325ad2ac8ba928394e01061d882c3528").into()
).unwrap()
}

fn bench_point_mul<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
let p = ProjectivePoint::GENERATOR;
let m = test_scalar_x();
let s = Scalar::from_repr(m.into()).unwrap();
let s = Scalar::from_repr(&m.into()).unwrap();
group.bench_function("point-scalar mul", |b| b.iter(|| p * s));
}

Expand Down
Loading