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

Implement starknet_core_types::curve module #8

Merged
merged 32 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
922eee7
Create stark-cuve crate + naive implement ProjectivePoint
pefontana Oct 18, 2023
5bff6a9
Implement AffinePoint
pefontana Oct 18, 2023
05945f1
Add froms
pefontana Oct 18, 2023
0a1bd07
Implement ops::Add<&ProjectivePoint> for &ProjectivePoint
pefontana Oct 18, 2023
b9ff318
remove starknet-types-core
pefontana Oct 20, 2023
79a7461
Update .gitignore
pefontana Oct 20, 2023
c39043a
rename stark-felt -> starknet-types-core && Implement curve mod
pefontana Oct 20, 2023
f541949
remove todo
pefontana Oct 20, 2023
6d92354
Remove crates/stark-curve
pefontana Oct 20, 2023
ffca830
fix ensure_no_std
pefontana Oct 20, 2023
a7bf4eb
Update commit
pefontana Oct 23, 2023
c9a337b
implement x(), y(), z() methods
pefontana Oct 23, 2023
fd14bf5
little fix
pefontana Oct 23, 2023
ee74daa
Divide the curve into two mods projective_point affine_point
pefontana Oct 23, 2023
0ddba75
minor fixes
pefontana Oct 23, 2023
eff975d
Update Readme.md
pefontana Oct 23, 2023
abd9d51
Fix to_affine method
pefontana Oct 24, 2023
6bafc48
Remove AffinePoint operations
pefontana Oct 25, 2023
6862fd6
Add tests
pefontana Oct 25, 2023
b54dbdb
Add Error handle to to_affine method
pefontana Oct 25, 2023
3239971
Add CurveError
pefontana Oct 25, 2023
e5609a2
docs
pefontana Oct 25, 2023
495c0da
Update Cargo.toml
pefontana Oct 25, 2023
5d2a260
docs
pefontana Oct 25, 2023
b4da689
Restore .gitignore
pefontana Oct 25, 2023
93d5f0a
Docs
pefontana Oct 25, 2023
900cea3
Remove crates/.DS_Store
pefontana Oct 25, 2023
15bed72
Add curve feature
pefontana Oct 26, 2023
971bef6
Doc
pefontana Oct 26, 2023
df43f6c
Add #[repr(transparent)]
pefontana Oct 30, 2023
3a20aed
Add struct docs
pefontana Oct 30, 2023
8787772
Re export curve types
pefontana Oct 30, 2023
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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[workspace]
members = [
"crates/stark-felt",
"crates/starknet-types-core",
"crates/starknet-types-rpc",
]
Expand Down
28 changes: 0 additions & 28 deletions crates/stark-felt/Cargo.toml

This file was deleted.

53 changes: 0 additions & 53 deletions crates/stark-felt/README.md

This file was deleted.

22 changes: 20 additions & 2 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
[package]
name = "starknet-types-core"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
license = "MIT"
homepage = "https://github.com/starknet-io/types-rs"
repository = "https://github.com/starknet-io/types-rs"
categories = ["types", "math", "crypto"]
keywords = ["stark", "zkp", "cairo"]
description = "Starknet core types."
description = "Core types representation for Starknet"
readme = "README.md"

[dependencies]
bitvec = { version = "1.0.1", default-features = false }
serde = { version = "1.0.163", optional = true, default-features = false }
lambdaworks-math = { git = "https://github.com/lambdaclass/lambdaworks.git", rev = "f940e14ed17370d29fe129951448037d11b65ce8", default-features = false}


arbitrary = { version = "1.3.0", optional = true, default-features = false }
num-traits = { version = "0.2.16", default-features = false }

[features]
default = ["std", "serde", "curve"]
curve = []
std = []
alloc = ["serde?/alloc"]
arbitrary = ["std", "dep:arbitrary"]
pefontana marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
proptest = "1.1.0"
serde_test = "1.0.1"
25 changes: 22 additions & 3 deletions crates/starknet-types-core/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
# starknet-types-core

`starknet-types-core` is a crate focusing on Starknet types related to computation and execution. This crate is part of an initiative to standardize the representation of the `Felt` type in Rust, reducing code complexity and improving performance across the Starknet Rust ecosystem.
Core types representation for Starknet.

The types in this crate require performance and optimization for specific arithmetic and cryptographic operations, making it ideal for computational tasks within the Starknet ecosystem.
## Overview

The `starknet-types-core` crate provides:
* The universal `Felt` (Field Element) type for Cairo and STARK proofs. It was created to reduce the fragmentation in the Starknet Rust ecosystem by providing a standardized representation of the `Felt` type.
* The `AffinePoint` and `ProjectivePoint` structs, which represent points on the Stark curve for performing elliptic curve operations.

## Features

- Standardized `Felt` type: Simplify your codebase by using our standardized `Felt` type.
- Optimized for performance: The `Felt` type has been optimized for high-performance applications.

## Examples

Here are some examples of how to use the `starknet-types-core` types:

```rust
let felt = Felt::from(18);
let projective_point = ProjectivePoint::new(Felt::from(0), Felt::from(1), Felt::from(0));
let affine_point = AffinePoint::new(Felt::from(0), Felt::from(1)).unwrap();
```

## Usage

Include `starknet-types-core` in your library by adding the following to your `Cargo.toml`:

```toml
[dependencies]
starknet-types-core = { version = "0.0.2", git = "https://github.com/starknet-io/types-rs" }
starknet-types-core = { version = "0.0.3", git = "https://github.com/starknet-io/types-rs" }
```

## Build from source
Expand Down
48 changes: 48 additions & 0 deletions crates/starknet-types-core/src/curve/affine_point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::curve::curve_errors::CurveError;
use crate::felt::Felt;
use lambdaworks_math::cyclic_group::IsGroup;
use lambdaworks_math::elliptic_curve::short_weierstrass::curves::stark_curve::StarkCurve;
use lambdaworks_math::elliptic_curve::short_weierstrass::point::ShortWeierstrassProjectivePoint;
use lambdaworks_math::elliptic_curve::traits::FromAffine;

/// Represents a point on the Stark elliptic curve.
/// Doc: https://docs.starkware.co/starkex/crypto/stark-curve.html
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AffinePoint(pub(crate) ShortWeierstrassProjectivePoint<StarkCurve>);
pefontana marked this conversation as resolved.
Show resolved Hide resolved

impl AffinePoint {
pub fn new(x: Felt, y: Felt) -> Result<AffinePoint, CurveError> {
Ok(Self(ShortWeierstrassProjectivePoint::from_affine(
x.0, y.0,
)?))
}

/// The point at infinity.
pub fn identity() -> AffinePoint {
Self(ShortWeierstrassProjectivePoint::neutral_element())
}

/// Returns the `x` coordinate of the point.
pub fn x(&self) -> Felt {
Felt(*self.0.x())
}

/// Returns the `y` coordinate of the point.
pub fn y(&self) -> Felt {
Felt(*self.0.y())
}
tdelabro marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn affine_point_identity() {
let identity = AffinePoint::identity();

assert_eq!(identity.x(), Felt::from(0));
assert_eq!(identity.y(), Felt::from(1));
}
}
13 changes: 13 additions & 0 deletions crates/starknet-types-core/src/curve/curve_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use core::fmt::Debug;
use lambdaworks_math::elliptic_curve::traits::EllipticCurveError;

#[derive(Debug, PartialEq, Eq)]
pub enum CurveError {
EllipticCurveError(EllipticCurveError),
}

impl From<EllipticCurveError> for CurveError {
fn from(error: EllipticCurveError) -> CurveError {
CurveError::EllipticCurveError(error)
}
}
7 changes: 7 additions & 0 deletions crates/starknet-types-core/src/curve/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod affine_point;
mod curve_errors;
mod projective_point;

pub use self::affine_point::*;
pub use self::curve_errors::*;
pub use self::projective_point::*;
Loading