Skip to content

Commit

Permalink
Merge branch 'starknet-io:main' into expand-api
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana authored Nov 7, 2023
2 parents a60c2ee + 8476f50 commit 8d9b627
Show file tree
Hide file tree
Showing 36 changed files with 9,031 additions and 103 deletions.
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.

24 changes: 22 additions & 2 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
[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}
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks.git", rev = "f940e14ed17370d29fe129951448037d11b65ce8", default-features = false, optional = true}


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 = []
hash = ["dep:lambdaworks-crypto"]
std = []
alloc = ["serde?/alloc"]
arbitrary = ["std", "dep:arbitrary"]

[dev-dependencies]
proptest = "1.1.0"
serde_test = "1.0.1"
45 changes: 42 additions & 3 deletions crates/starknet-types-core/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
# 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.

## Features

### Always on
- 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.
- No_std support ✅

### Serde
- Provides a Serialization and Deserialization implementations for the `Felt` type
- No_std support ✅

### Arbitrary
- Provides an Arbitrary implementations for the `Felt` type

### Curve
- Add the `AffinePoint` and `ProjectivePoint` structs, which represent points on the Stark curve for performing elliptic curve operations.
- No_std support ✅

### Hash
- Implements Pedersen hashing for Felts and Felts array

## 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", default-features = false, features = [
"alloc",
"serde",
"arbitrary",
"curve",
"hash",
] }
```

## 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>);

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())
}
}

#[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

0 comments on commit 8d9b627

Please sign in to comment.