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 FieldBytesEncoding trait impls #732

Merged
merged 1 commit into from
Feb 1, 2023
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
15 changes: 9 additions & 6 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@ members = [
]

[profile.dev]
opt-level = 2

[patch.crates-io]
elliptic-curve = { git = "https://github.com/RustCrypto/traits.git" }
ecdsa = { git = "https://github.com/RustCrypto/signatures.git" }
opt-level = 2
4 changes: 2 additions & 2 deletions bp256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ edition = "2021"
rust-version = "1.61"

[dependencies]
elliptic-curve = { version = "=0.13.0-pre.2", default-features = false, features = ["hazmat", "sec1"] }
elliptic-curve = { version = "=0.13.0-pre.3", default-features = false, features = ["hazmat", "sec1"] }

# optional dependencies
ecdsa = { version = "=0.16.0-pre", optional = true, default-features = false, features = ["der"] }
ecdsa = { version = "=0.16.0-pre.0", optional = true, default-features = false, features = ["der"] }
sha2 = { version = "0.10", optional = true, default-features = false }

[features]
Expand Down
20 changes: 17 additions & 3 deletions bp256/src/r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#[cfg(feature = "ecdsa")]
pub mod ecdsa;

use elliptic_curve::{bigint::U256, consts::U32};
use elliptic_curve::{
bigint::{ArrayEncoding, U256},
consts::U32,
FieldBytesEncoding,
};

#[cfg(feature = "pkcs8")]
use crate::pkcs8;
Expand Down Expand Up @@ -36,13 +40,23 @@ impl pkcs8::AssociatedOid for BrainpoolP256r1 {
pkcs8::ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.7");
}

/// brainpoolP256r1 SEC1 encoded point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BrainpoolP256r1>;

/// brainpoolP256r1 field element serialized as bytes.
///
/// Byte array containing a serialized field element value (base field or scalar).
pub type FieldBytes = elliptic_curve::FieldBytes<BrainpoolP256r1>;

/// brainpoolP256r1 SEC1 encoded point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BrainpoolP256r1>;
impl FieldBytesEncoding<BrainpoolP256r1> for U256 {
fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
U256::from_be_byte_array(*field_bytes)
}

fn encode_field_bytes(&self) -> FieldBytes {
self.to_be_byte_array()
}
}

/// brainpoolP256r1 secret key.
pub type SecretKey = elliptic_curve::SecretKey<BrainpoolP256r1>;
Expand Down
20 changes: 17 additions & 3 deletions bp256/src/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#[cfg(feature = "ecdsa")]
pub mod ecdsa;

use elliptic_curve::{bigint::U256, consts::U32};
use elliptic_curve::{
bigint::{ArrayEncoding, U256},
consts::U32,
FieldBytesEncoding,
};

#[cfg(feature = "pkcs8")]
use crate::pkcs8;
Expand Down Expand Up @@ -36,13 +40,23 @@ impl pkcs8::AssociatedOid for BrainpoolP256t1 {
pkcs8::ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.8");
}

/// brainpoolP256t1 SEC1 encoded point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BrainpoolP256t1>;

/// brainpoolP256t1 field element serialized as bytes.
///
/// Byte array containing a serialized field element value (base field or scalar).
pub type FieldBytes = elliptic_curve::FieldBytes<BrainpoolP256t1>;

/// brainpoolP256t1 SEC1 encoded point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BrainpoolP256t1>;
impl FieldBytesEncoding<BrainpoolP256t1> for U256 {
fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
U256::from_be_byte_array(*field_bytes)
}

fn encode_field_bytes(&self) -> FieldBytes {
self.to_be_byte_array()
}
}

/// brainpoolP256t1 secret key.
pub type SecretKey = elliptic_curve::SecretKey<BrainpoolP256t1>;
Expand Down
4 changes: 2 additions & 2 deletions bp384/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ edition = "2021"
rust-version = "1.61"

[dependencies]
elliptic-curve = { version = "=0.13.0-pre.2", default-features = false, features = ["hazmat", "sec1"] }
elliptic-curve = { version = "=0.13.0-pre.3", default-features = false, features = ["hazmat", "sec1"] }

# optional dependencies
ecdsa = { version = "=0.16.0-pre", optional = true, default-features = false, features = ["der"] }
ecdsa = { version = "=0.16.0-pre.0", optional = true, default-features = false, features = ["der"] }
sha2 = { version = "0.10", optional = true, default-features = false }

[features]
Expand Down
20 changes: 17 additions & 3 deletions bp384/src/r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#[cfg(feature = "ecdsa")]
pub mod ecdsa;

use elliptic_curve::{bigint::U384, consts::U48};
use elliptic_curve::{
bigint::{ArrayEncoding, U384},
consts::U48,
FieldBytesEncoding,
};

#[cfg(feature = "pkcs8")]
use crate::pkcs8;
Expand Down Expand Up @@ -36,13 +40,23 @@ impl pkcs8::AssociatedOid for BrainpoolP384r1 {
pkcs8::ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.11");
}

/// brainpoolP384r1 SEC1 encoded point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BrainpoolP384r1>;

/// brainpoolP384r1 field element serialized as bytes.
///
/// Byte array containing a serialized field element value (base field or scalar).
pub type FieldBytes = elliptic_curve::FieldBytes<BrainpoolP384r1>;

/// brainpoolP384r1 SEC1 encoded point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BrainpoolP384r1>;
impl FieldBytesEncoding<BrainpoolP384r1> for U384 {
fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
U384::from_be_byte_array(*field_bytes)
}

fn encode_field_bytes(&self) -> FieldBytes {
self.to_be_byte_array()
}
}

/// brainpoolP384r1 secret key.
pub type SecretKey = elliptic_curve::SecretKey<BrainpoolP384r1>;
Expand Down
20 changes: 17 additions & 3 deletions bp384/src/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#[cfg(feature = "ecdsa")]
pub mod ecdsa;

use elliptic_curve::{bigint::U384, consts::U48};
use elliptic_curve::{
bigint::{ArrayEncoding, U384},
consts::U48,
FieldBytesEncoding,
};

#[cfg(feature = "pkcs8")]
use crate::pkcs8;
Expand Down Expand Up @@ -36,13 +40,23 @@ impl pkcs8::AssociatedOid for BrainpoolP384t1 {
pkcs8::ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.12");
}

/// brainpoolP384t1 SEC1 encoded point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BrainpoolP384t1>;

/// brainpoolP384t1 field element serialized as bytes.
///
/// Byte array containing a serialized field element value (base field or scalar).
pub type FieldBytes = elliptic_curve::FieldBytes<BrainpoolP384t1>;

/// brainpoolP384t1 SEC1 encoded point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<BrainpoolP384t1>;
impl FieldBytesEncoding<BrainpoolP384t1> for U384 {
fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
U384::from_be_byte_array(*field_bytes)
}

fn encode_field_bytes(&self) -> FieldBytes {
self.to_be_byte_array()
}
}

/// brainpoolP384t1 secret key.
pub type SecretKey = elliptic_curve::SecretKey<BrainpoolP384t1>;
Expand Down
6 changes: 3 additions & 3 deletions k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ rust-version = "1.61"

[dependencies]
cfg-if = "1.0"
elliptic-curve = { version = "=0.13.0-pre.2", default-features = false, features = ["hazmat", "sec1"] }
elliptic-curve = { version = "=0.13.0-pre.3", default-features = false, features = ["hazmat", "sec1"] }

# optional dependencies
once_cell = { version = "1.16", optional = true, default-features = false }
ecdsa-core = { version = "=0.16.0-pre", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
ecdsa-core = { version = "=0.16.0-pre.0", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
hex-literal = { version = "0.3", optional = true }
serdect = { version = "0.1", optional = true, default-features = false }
sha2 = { version = "0.10", optional = true, default-features = false }
Expand All @@ -32,7 +32,7 @@ signature = { version = "2", optional = true }
[dev-dependencies]
blobby = "0.3"
criterion = "0.4"
ecdsa-core = { version = "=0.16.0-pre", package = "ecdsa", default-features = false, features = ["dev"] }
ecdsa-core = { version = "=0.16.0-pre.0", package = "ecdsa", default-features = false, features = ["dev"] }
hex-literal = "0.3"
num-bigint = "0.4"
num-traits = "0.2"
Expand Down
6 changes: 3 additions & 3 deletions k256/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ use {
crate::{AffinePoint, FieldBytes, ProjectivePoint, Scalar},
ecdsa_core::hazmat::{SignPrimitive, VerifyPrimitive},
elliptic_curve::{
bigint::U256,
ops::{Invert, MulByGenerator, Reduce},
scalar::IsHigh,
subtle::CtOption,
Curve,
},
};

Expand Down Expand Up @@ -201,7 +201,7 @@ impl SignPrimitive<Secp256k1> for Scalar {
return Err(Error::new());
}

let z = Self::reduce(Secp256k1::decode_field_bytes(z));
let z = <Self as Reduce<U256>>::reduce_bytes(z);

// Compute scalar inversion of 𝑘
let k_inv = Option::<Scalar>::from(k.invert()).ok_or_else(Error::new)?;
Expand All @@ -211,7 +211,7 @@ impl SignPrimitive<Secp256k1> for Scalar {

// Lift x-coordinate of 𝑹 (element of base field) into a serialized big
// integer, then reduce it into an element of the scalar field
let r = Self::reduce(Secp256k1::decode_field_bytes(&R.x.to_bytes()));
let r = <Self as Reduce<U256>>::reduce_bytes(&R.x.to_bytes());

// Compute 𝒔 as a signature over 𝒓 and 𝒛.
let s = k_inv * (z + (r * self));
Expand Down
27 changes: 14 additions & 13 deletions k256/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use elliptic_curve::{
bigint::ArrayEncoding,
consts::{U32, U33, U64},
generic_array::GenericArray,
FieldBytesEncoding,
};

/// Order of the secp256k1 elliptic curve in hexadecimal.
Expand Down Expand Up @@ -90,16 +91,6 @@ impl elliptic_curve::Curve for Secp256k1 {

/// Curve order.
const ORDER: U256 = ORDER;

/// Decode unsigned integer from serialized field element.
fn decode_field_bytes(field_bytes: &FieldBytes) -> U256 {
U256::from_be_byte_array(*field_bytes)
}

/// Encode unsigned integer into serialized field element.
fn encode_field_bytes(uint: &U256) -> FieldBytes {
uint.to_be_byte_array()
}
}

impl elliptic_curve::PrimeCurve for Secp256k1 {}
Expand All @@ -122,17 +113,27 @@ impl pkcs8::AssociatedOid for Secp256k1 {
/// Compressed SEC1-encoded secp256k1 (K-256) curve point.
pub type CompressedPoint = GenericArray<u8, U33>;

/// SEC1-encoded secp256k1 (K-256) curve point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<Secp256k1>;

/// secp256k1 (K-256) field element serialized as bytes.
///
/// Byte array containing a serialized field element value (base field or scalar).
pub type FieldBytes = elliptic_curve::FieldBytes<Secp256k1>;

impl FieldBytesEncoding<Secp256k1> for U256 {
fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
U256::from_be_byte_array(*field_bytes)
}

fn encode_field_bytes(&self) -> FieldBytes {
self.to_be_byte_array()
}
}

/// Bytes used by a wide reduction: twice the width of [`FieldBytes`].
pub type WideBytes = GenericArray<u8, U64>;

/// SEC1-encoded secp256k1 (K-256) curve point.
pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<Secp256k1>;

/// Non-zero secp256k1 (K-256) scalar field element.
#[cfg(feature = "arithmetic")]
pub type NonZeroScalar = elliptic_curve::NonZeroScalar<Secp256k1>;
Expand Down
8 changes: 3 additions & 5 deletions k256/src/schnorr/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

use super::{tagged_hash, Signature, VerifyingKey, AUX_TAG, CHALLENGE_TAG, NONCE_TAG};
use crate::{
AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, PublicKey, Scalar, Secp256k1,
SecretKey,
AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, PublicKey, Scalar, SecretKey,
};
use elliptic_curve::{
bigint::U256,
ops::Reduce,
rand_core::CryptoRngCore,
subtle::ConditionallySelectable,
zeroize::{Zeroize, ZeroizeOnDrop},
Curve,
};
use sha2::{Digest, Sha256};
use signature::{
Expand Down Expand Up @@ -100,13 +98,13 @@ impl SigningKey {
let verifying_point = AffinePoint::from(k.verifying_key);
let r = verifying_point.x.normalize();

let e = <Scalar as Reduce<U256>>::reduce(Secp256k1::decode_field_bytes(
let e = <Scalar as Reduce<U256>>::reduce_bytes(
&tagged_hash(CHALLENGE_TAG)
.chain_update(r.to_bytes())
.chain_update(self.verifying_key.to_bytes())
.chain_update(msg_digest)
.finalize(),
));
);

let s = *secret_key + e * *self.secret_key;
let s = Option::from(NonZeroScalar::new(s)).ok_or_else(Error::new)?;
Expand Down
Loading