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

Bump p256 to 0.11 #168

Merged
merged 4 commits into from
Aug 22, 2022
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ version = "0.20"
features = ["serde", "rand-std", "global-context"]

[dependencies.p256]
version = "0.9"
features = ["ecdsa", "ecdsa-core", "zeroize"]
version = "0.11.1"
features = ["ecdsa", "ecdsa-core"]

[dev-dependencies]
serde_test = "1.0"
Expand Down
54 changes: 36 additions & 18 deletions src/elliptic/curves/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::convert::TryFrom;

use p256::elliptic_curve::group::ff::PrimeField;
use p256::elliptic_curve::group::prime::PrimeCurveAffine;
use p256::elliptic_curve::ops::Reduce;
use p256::elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint};
use p256::elliptic_curve::Field;
use p256::{AffinePoint, EncodedPoint, FieldBytes, ProjectivePoint, Scalar};

use generic_array::GenericArray;
Expand Down Expand Up @@ -94,8 +96,9 @@ impl ECScalar for Secp256r1Scalar {
let scalar = loop {
let mut bytes = FieldBytes::default();
rng.fill(&mut bytes[..]);
if let Some(scalar) = Scalar::from_repr(bytes) {
break scalar;
let element = Scalar::from_repr(bytes);
if bool::from(element.is_some()) {
break element.unwrap();
}
};
Secp256r1Scalar {
Expand All @@ -107,7 +110,7 @@ impl ECScalar for Secp256r1Scalar {
fn zero() -> Secp256r1Scalar {
Secp256r1Scalar {
purpose: "zero",
fe: Scalar::zero().into(),
fe: Scalar::ZERO.into(),
}
}

Expand All @@ -124,7 +127,7 @@ impl ECScalar for Secp256r1Scalar {

Secp256r1Scalar {
purpose: "from_bigint",
fe: Scalar::from_bytes_reduced(&n_reduced.into()).into(),
fe: Scalar::from_be_bytes_reduced(GenericArray::from(n_reduced)).into(),
}
}

Expand All @@ -139,10 +142,16 @@ impl ECScalar for Secp256r1Scalar {
fn deserialize(bytes: &[u8]) -> Result<Self, DeserializationError> {
let bytes = <[u8; 32]>::try_from(bytes).or(Err(DeserializationError))?;
let bytes = FieldBytes::from(bytes);
Ok(Secp256r1Scalar {
purpose: "deserialize",
fe: Scalar::from_repr(bytes).ok_or(DeserializationError)?.into(),
})
let scalar = Scalar::from_repr(bytes);

if bool::from(scalar.is_some()) {
Ok(Secp256r1Scalar {
purpose: "deserialize",
fe: scalar.unwrap().into(),
})
} else {
Err(DeserializationError)
}
}

fn add(&self, other: &Self) -> Secp256r1Scalar {
Expand Down Expand Up @@ -252,13 +261,16 @@ impl ECPoint for Secp256r1Point {
&x_arr.into(),
&y_arr.into(),
false,
))
.ok_or(NotOnCurve)?;
));

Ok(Secp256r1Point {
purpose: "from_coords",
ge,
})
if bool::from(ge.is_some()) {
Ok(Secp256r1Point {
purpose: "from_coords",
ge: ge.unwrap(),
})
} else {
Err(NotOnCurve)
}
}

fn x_coord(&self) -> Option<BigInt> {
Expand Down Expand Up @@ -304,10 +316,16 @@ impl ECPoint for Secp256r1Point {
})
} else {
let encoded = EncodedPoint::from_bytes(bytes).map_err(|_| DeserializationError)?;
Ok(Secp256r1Point {
purpose: "deserialize",
ge: AffinePoint::from_encoded_point(&encoded).ok_or(DeserializationError)?,
})
let affine_point = AffinePoint::from_encoded_point(&encoded);

if bool::from(affine_point.is_some()) {
Ok(Secp256r1Point {
purpose: "deserialize",
ge: affine_point.unwrap(),
})
} else {
Err(DeserializationError)
}
}
}

Expand Down