Skip to content

Commit

Permalink
feat: add elliptic curve cryptography (ECC) library in Rust
Browse files Browse the repository at this point in the history
Introduce a Rust-based library for elliptic curve cryptography (ECC).
The library supports elliptic curve operations such as point addition,
doubling, and scalar multiplication, as well as finite field arithmetic.
It includes support for specific curves like Baby Jubjub.

- `EllipticCurve` struct for representing curves
- `Point` struct for handling points on the curve
- Finite field arithmetic (`FiniteField`)
- Baby Jubjub curve implementation
- Unit tests for curve and point operations

This library provides a foundation for cryptographic operations
involving elliptic curves. It is designed to be flexible and modular,
enabling usage in various cryptographic applications.

- Adds ECC functionality for cryptography projects
- Provides a modular structure with reusable components for future
  extensions
  • Loading branch information
pycckuu committed Sep 27, 2024
1 parent dd530c5 commit 15d4222
Show file tree
Hide file tree
Showing 10 changed files with 481 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
44 changes: 44 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"

[package]
name = "ecc-rust"
version = "0.1.0"
edition = "2021"

[dependencies]
num-bigint = "0.4.6"
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Elliptic Curve Cryptography (ECC) Implementation in Rust

This project implements Elliptic Curve Cryptography (ECC) operations in Rust. It provides a foundation for working with elliptic curves, points on these curves, and various cryptographic operations.

## Features

- Elliptic curve representation and operations
- Point arithmetic on elliptic curves (addition, doubling, scalar multiplication)
- Finite field arithmetic
- Comprehensive test suite for all implemented operations

## Structure

The project is organized into several modules:

### src/lib.rs

This is the main entry point of the library. It re-exports the public items from other modules.

### src/ec.rs

Contains the `EllipticCurve` struct and its implementation, which represents an elliptic curve and provides methods for curve operations.

### src/point.rs

Defines the `Point` struct, representing a point on an elliptic curve, including the point at infinity (Identity).

### src/ff.rs

Implements the `FiniteField` struct with finite field arithmetic operations such as addition, multiplication, and inversion.

### src/curves/mod.rs and src/curves/bjj.rs

These files contain implementations of specific elliptic curves, including the Baby Jubjub (BJJ) curve.

Each module contains its own tests, ensuring the correctness of the implemented operations.

## Usage

To use this library in your Rust project, add it as a dependency in your `Cargo.toml` file:


```toml
[dependencies]
ecc-rust = { git = "https://github.com/pycckuu/ecc-rust.git" }
```

Example usage:

```rust
use ecc_rust::{EllipticCurve, Point, FiniteField};
use num_bigint::BigUint;

fn main() {
// Create a new elliptic curve y^2 = x^3 + 2x + 2 over F_17
let curve = EllipticCurve::new(
BigUint::from(2u32),
BigUint::from(2u32),
BigUint::from(17u32)
);

// Create two points on the curve
let p1 = Point::Coordinates(BigUint::from(6u32), BigUint::from(3u32));
let p2 = Point::Coordinates(BigUint::from(5u32), BigUint::from(1u32));

// Add the points
let sum = curve.add(&p1, &p2);
println!("Sum: {:?}", sum);

// Perform scalar multiplication
let scalar = BigUint::from(5u32);
let product = curve.mul(&p1, &scalar);
println!("5 P1: {:?}", product);

// Demonstrate finite field operations
let a = BigUint::from(7u32);
let b = BigUint::from(10u32);
let p = BigUint::from(17u32);
let sum_ff = FiniteField::add(&a, &b, &p);
let product_ff = FiniteField::mul(&a, &b, &p);

println!("(7 + 10) mod 17 = {}", sum_ff);
println!("(7 * 10) mod 17 = {}", product_ff);
}
```

This example demonstrates:

1. Creating an elliptic curve
2. Defining points on the curve
3. Adding points on the curve
4. Performing scalar multiplication
5. Using finite field operations

Make sure to handle any potential errors or invalid inputs in your actual implementation.


## License

This project is licensed under the MIT License. See the `LICENSE` file for more details.
27 changes: 27 additions & 0 deletions src/curves/bjj.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use num_bigint::BigUint;
use crate::ec::EllipticCurve;
use crate::point::Point;

/// Returns the base point for the BabyJubJub curve
pub fn base8() -> Point {
let value1 = BigUint::parse_bytes(
b"5299619240641551281634865583518297030282874472190772894086521144482721001553",
10,
)
.unwrap();
let value2 = BigUint::parse_bytes(
b"16950150798460657717958625567821834550301663161624707787222815936182638968203",
10,
)
.unwrap();
Point::Coordinates(value1, value2)
}

/// Returns the BabyJubJub curve parameters
pub fn babyjubjub() -> EllipticCurve {
EllipticCurve {
a: BigUint::from(1u32),
b: BigUint::from(0u32),
p: BigUint::from(2u32).pow(255u32) - 19u32,
}
}
1 change: 1 addition & 0 deletions src/curves/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bjj;
Loading

0 comments on commit 15d4222

Please sign in to comment.