Skip to content

Commit

Permalink
Merge pull request #7 from dfns-labs/stark-curve
Browse files Browse the repository at this point in the history
Update elliptic-curves version. Add stark-curve
  • Loading branch information
maurges authored Sep 20, 2023
2 parents f6ac5f4 + 803c275 commit bb2e710
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 101 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name: Check README
on:
pull_request:
branches: [ "*" ]

env:
CARGO_TERM_COLOR: always
CARGO_NET_GIT_FETCH_WITH_CLI: true

jobs:
check_readme:
Expand All @@ -13,5 +17,8 @@ jobs:
uses: baptiste0928/cargo-install@v1
with:
crate: cargo-rdme
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.STARK_CURVE_KEY }}
- name: Check that readme matches lib.rs
run: cargo rdme -w generic-ec -r README.md --check
run: cargo rdme -w generic-ec -r README.md --check
9 changes: 7 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ name: Rust
on:
pull_request:
branches: [ "*" ]
schedule:
- cron: '0 5 * * *'

env:
CARGO_TERM_COLOR: always
CARGO_NET_GIT_FETCH_WITH_CLI: true

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.STARK_CURVE_KEY }}
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: "true"
Expand All @@ -37,6 +39,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.STARK_CURVE_KEY }}
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: "true"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Cargo.lock
/.idea

.helix/
.rstags
11 changes: 7 additions & 4 deletions generic-ec-curves/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ subtle = { version = "2.4", default-features = false }
rand_core = { version = "0.6", default-features = false }
zeroize = { version = "1", default-features = false }

elliptic-curve = { version = "0.12", features = ["sec1", "hash2curve"], optional = true }
k256 = { version = "0.12", optional = true, features = ["hash2curve"] }
p256 = { version = "0.12", optional = true, features = ["hash2curve"] }
crypto-bigint = { version = "0.5", optional = true }
elliptic-curve = { version = "0.13", features = ["sec1", "hash2curve"], optional = true }
k256 = { version = "0.13", optional = true, features = ["hash2curve"] }
p256 = { version = "0.13", optional = true, features = ["hash2curve"] }
sha2 = { version = "0.10", optional = true }
stark-curve = { git = "https://github.com/dfns-labs/stark-curve", optional = true }

[features]
default = []

rust-crypto = ["elliptic-curve"]
rust-crypto = ["elliptic-curve", "crypto-bigint"]
secp256k1 = ["rust-crypto", "k256", "sha2"]
secp256r1 = ["rust-crypto", "p256", "sha2"]
stark = ["rust-crypto", "stark-curve", "sha2"]
3 changes: 3 additions & 0 deletions generic-ec-curves/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ pub use rust_crypto::Secp256k1;

#[cfg(feature = "secp256r1")]
pub use rust_crypto::Secp256r1;

#[cfg(feature = "stark")]
pub use rust_crypto::Stark;
18 changes: 9 additions & 9 deletions generic-ec-curves/src/rust_crypto/affine_coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use elliptic_curve::generic_array::GenericArray;
use elliptic_curve::sec1::{
CompressedPointSize, Coordinates, EncodedPoint, FromEncodedPoint, Tag, ToEncodedPoint,
};
use elliptic_curve::{AffineArithmetic, FieldSize, ProjectiveArithmetic};
use elliptic_curve::{CurveArithmetic, FieldBytesSize};
use generic_ec_core::coords::{HasAffineX, HasAffineXAndParity, HasAffineXY, HasAffineY, Parity};

use super::{RustCryptoCurve, RustCryptoPoint};

impl<C, X> HasAffineX for RustCryptoCurve<C, X>
where
C: ProjectiveArithmetic + AffineArithmetic,
FieldSize<C>: elliptic_curve::sec1::ModulusSize,
C: CurveArithmetic,
FieldBytesSize<C>: elliptic_curve::sec1::ModulusSize,
C::AffinePoint: ToEncodedPoint<C> + From<C::ProjectivePoint>,
RustCryptoCurve<C, X>: generic_ec_core::Curve<
Point = RustCryptoPoint<C>,
Expand All @@ -33,8 +33,8 @@ where

impl<C, X> HasAffineXAndParity for RustCryptoCurve<C, X>
where
C: ProjectiveArithmetic + AffineArithmetic,
FieldSize<C>: elliptic_curve::sec1::ModulusSize,
C: CurveArithmetic,
FieldBytesSize<C>: elliptic_curve::sec1::ModulusSize,
C::AffinePoint: ToEncodedPoint<C>
+ FromEncodedPoint<C>
+ From<C::ProjectivePoint>
Expand Down Expand Up @@ -76,8 +76,8 @@ where

impl<C, X> HasAffineY for RustCryptoCurve<C, X>
where
C: ProjectiveArithmetic + AffineArithmetic,
FieldSize<C>: elliptic_curve::sec1::ModulusSize,
C: CurveArithmetic,
FieldBytesSize<C>: elliptic_curve::sec1::ModulusSize,
C::AffinePoint: ToEncodedPoint<C> + From<C::ProjectivePoint>,
RustCryptoCurve<C, X>: generic_ec_core::Curve<
Point = RustCryptoPoint<C>,
Expand All @@ -100,8 +100,8 @@ where

impl<C, X> HasAffineXY for RustCryptoCurve<C, X>
where
C: ProjectiveArithmetic + AffineArithmetic,
FieldSize<C>: elliptic_curve::sec1::ModulusSize,
C: CurveArithmetic,
FieldBytesSize<C>: elliptic_curve::sec1::ModulusSize,
C::AffinePoint: ToEncodedPoint<C>
+ FromEncodedPoint<C>
+ From<C::ProjectivePoint>
Expand Down
5 changes: 5 additions & 0 deletions generic-ec-curves/src/rust_crypto/curve_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ impl CurveName for p256::NistP256 {
impl CurveName for k256::Secp256k1 {
const CURVE_NAME: &'static str = "secp256k1";
}

#[cfg(feature = "stark")]
impl CurveName for stark_curve::StarkCurve {
const CURVE_NAME: &'static str = "stark";
}
8 changes: 4 additions & 4 deletions generic-ec-curves/src/rust_crypto/hash_to_curve.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use elliptic_curve::{
group::cofactor::CofactorGroup,
hash2curve::{ExpandMsg, FromOkm, GroupDigest},
ProjectiveArithmetic,
CurveArithmetic,
};
use generic_ec_core::hash_to_curve::{HashToCurve, Tag};

use super::{RustCryptoCurve, RustCryptoPoint, RustCryptoScalar};

impl<C, X> HashToCurve for RustCryptoCurve<C, X>
where
C: ProjectiveArithmetic + GroupDigest,
C: CurveArithmetic + GroupDigest,
C::ProjectivePoint: CofactorGroup,
C::Scalar: FromOkm,
for<'a> X: ExpandMsg<'a>,
RustCryptoCurve<C, X>:
generic_ec_core::Curve<Point = RustCryptoPoint<C>, Scalar = RustCryptoScalar<C>>,
{
fn hash_to_curve(ctx: Tag, msgs: &[&[u8]]) -> Result<Self::Point, generic_ec_core::Error> {
let point = <C as GroupDigest>::hash_from_bytes::<X>(msgs, ctx.as_bytes())
let point = <C as GroupDigest>::hash_from_bytes::<X>(msgs, &[ctx.as_bytes()])
.or(Err(generic_ec_core::Error))?;
Ok(RustCryptoPoint(point))
}

fn hash_to_scalar(ctx: Tag, msgs: &[&[u8]]) -> Result<Self::Scalar, generic_ec_core::Error> {
let scalar = <C as GroupDigest>::hash_to_scalar::<X>(msgs, ctx.as_bytes())
let scalar = <C as GroupDigest>::hash_to_scalar::<X>(msgs, &[ctx.as_bytes()])
.or(Err(generic_ec_core::Error))?;
Ok(RustCryptoScalar(scalar))
}
Expand Down
19 changes: 10 additions & 9 deletions generic-ec-curves/src/rust_crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ use elliptic_curve::group::cofactor::CofactorGroup;
use elliptic_curve::hash2curve::ExpandMsgXmd;
use elliptic_curve::ops::Reduce;
use elliptic_curve::sec1::{FromEncodedPoint, ModulusSize, ToEncodedPoint};
use elliptic_curve::{
AffineArithmetic, FieldSize, ProjectiveArithmetic, ScalarArithmetic, ScalarCore,
};
use elliptic_curve::{CurveArithmetic, FieldBytesSize, ScalarPrimitive};
use generic_ec_core::{CompressedEncoding, Curve, IntegerEncoding, UncompressedEncoding};
use subtle::{ConditionallySelectable, ConstantTimeEq};
use zeroize::{DefaultIsZeroes, Zeroize};

#[cfg(any(feature = "secp256k1", feature = "secp256r1"))]
#[cfg(any(feature = "secp256k1", feature = "secp256r1", feature = "stark"))]
use sha2::Sha256;

pub use self::{curve_name::CurveName, point::RustCryptoPoint, scalar::RustCryptoScalar};
Expand All @@ -40,9 +38,12 @@ pub type Secp256k1 = RustCryptoCurve<k256::Secp256k1, ExpandMsgXmd<Sha256>>;
#[cfg(feature = "secp256r1")]
pub type Secp256r1 = RustCryptoCurve<p256::NistP256, ExpandMsgXmd<Sha256>>;

#[cfg(feature = "stark")]
pub type Stark = RustCryptoCurve<stark_curve::StarkCurve, ExpandMsgXmd<Sha256>>;

impl<C, X> Curve for RustCryptoCurve<C, X>
where
C: CurveName + ScalarArithmetic + ProjectiveArithmetic + AffineArithmetic,
C: CurveName + CurveArithmetic,
C::ProjectivePoint: From<C::AffinePoint>
+ CofactorGroup
+ Copy
Expand All @@ -55,9 +56,9 @@ where
C::AffinePoint: From<C::ProjectivePoint> + ToEncodedPoint<C> + FromEncodedPoint<C>,
for<'a> &'a C::ProjectivePoint: Mul<&'a C::Scalar, Output = C::ProjectivePoint>,
C::Scalar:
Reduce<C::UInt> + Eq + ConstantTimeEq + ConditionallySelectable + DefaultIsZeroes + Unpin,
for<'a> ScalarCore<C>: From<&'a C::Scalar>,
FieldSize<C>: ModulusSize,
Reduce<C::Uint> + Eq + ConstantTimeEq + ConditionallySelectable + DefaultIsZeroes + Unpin,
for<'a> ScalarPrimitive<C>: From<&'a C::Scalar>,
FieldBytesSize<C>: ModulusSize,
X: 'static,
{
const CURVE_NAME: &'static str = C::CURVE_NAME;
Expand All @@ -83,7 +84,7 @@ impl<C: CurveName, X> fmt::Debug for RustCryptoCurve<C, X> {

impl<C, X> Clone for RustCryptoCurve<C, X> {
fn clone(&self) -> Self {
Self { _ph: PhantomData }
*self
}
}

Expand Down
Loading

0 comments on commit bb2e710

Please sign in to comment.