Skip to content

Add curve support for Grumpkin #40

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/ecdh/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ecdh"
type = "lib"
authors = ["YashBit", "signorecello"]
authors = ["YashBit", "signorecello","karl"]

[dependencies]
ec = { tag = "v0.1.2", git = "https://github.com/noir-lang/ec" }
40 changes: 40 additions & 0 deletions packages/ecdh/src/gpk.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::ECDHSWTrait;
use ec::swcurve::affine::{Curve, Point};

pub struct Grumpkin {
pub private_key: Field,
pub curve: Curve,
}

impl ECDHSWTrait for Grumpkin {
fn new(private_key: Field) -> Self {
let curve = create_grumpkin_curve();
Self { curve, private_key }
}

fn derive_public_key(self) -> Point {
// Use the stored curve instance
self.curve.mul(self.private_key, self.curve.gen)
}

fn derive_shared_key(self, public_key: Point) -> Field {
// Use the stored curve instance
let shared_point = self.curve.mul(self.private_key,public_key);

// Return x-coordinate as the shared secret
shared_point.x
}
}


fn create_grumpkin_curve() -> Curve {
// Let Noir handle the field arithmetic
let a = 0;
let b = -17;

let gen_x = 1;
let gen_y = 0x2cf135e7506a45d632d270d45f1181294833fc48d823f272c;

let generator = Point::new(gen_x, gen_y);
Curve::new(a, b, generator)
}
11 changes: 10 additions & 1 deletion packages/ecdh/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
mod bjj;
use ec::tecurve::affine::Point;
use ec::tecurve::affine::Point ;

mod gpk;
use ec::swcurve::affine::Point as SWPoint;

pub trait ECDHTrait {
fn new(private_key: Field) -> Self;
fn derive_public_key(self) -> Point;
fn derive_shared_key(self, public_key: Point) -> Field;
}

pub trait ECDHSWTrait {
fn new(private_key: Field) -> Self;
fn derive_public_key(self) -> SWPoint;
fn derive_shared_key(self, public_key: SWPoint) -> Field;
}
4 changes: 3 additions & 1 deletion tests/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type = "lib"
authors = ["signorecello"]

[dependencies]
bignum = { git = "https://github.com/noir-lang/noir-bignum", tag = "v0.6.0" }
bignum = { git = "https://github.com/noir-lang/noir-bignum", tag = "v0.6.1" }
trees = { path = "../packages/merkle-trees" }
ecdh = { path = "../packages/ecdh" }
ec = { tag = "v0.1.2", git = "https://github.com/noir-lang/ec" }

38 changes: 38 additions & 0 deletions tests/src/ecdh/mod.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use ecdh::{bjj::BJJ, ECDHTrait, Point};
use ecdh::{gpk::Grumpkin, ECDHSWTrait};
use ec::swcurve::affine::Point as swPoint;

global alice_sk: Field = 0x60c0102756aac2cf5d7277792a469bff83dfe3d3e7e50ad5a55383f3a89283e;
global bob_sk: Field = 0x86cdaad8886954a2eb20142fb98468d476a2d6c7b2c571af42cdc041b1a923c;

global grumpkin_alice_sk: Field = 0x12a4d37d71d32255c4e9b3d5f6c8c1b0e9a4c7d1b3e5f6c8a2d4e7f1b2c3d5e;
global grumpkin_bob_sk: Field = 0x2c8d3e7a1f5b9d2e6c4a8b1d3f5e7c9a2d4b6e8f1a3c5d7e9b2d4f6a8c3e1;

#[test]
fn test_pk() {
let ecdh = BJJ::new(alice_sk);
Expand All @@ -13,6 +18,20 @@ fn test_pk() {
let pk = ecdh.derive_public_key();
assert(pk == expected_pk);
}
#[test]
fn test_grumpkin_pk() {
let ecdh = Grumpkin::new(grumpkin_alice_sk);

let expected_pk = swPoint::new(
0x159cf346346bebd155d7fc84f67bfb29cd9d26a920c4652823b1fc7b327490ca,
0x2869bca8dd2c9d498c9ca4463c91f5f3e32cb40af3cfe3a7c5e8357436642b33,
);

let pk = ecdh.derive_public_key();

assert(pk.x == expected_pk.x);
assert(pk.y == expected_pk.y);
}

#[test]
fn test_shared_k() {
Expand All @@ -27,3 +46,22 @@ fn test_shared_k() {

assert(shared_key_with_bob == shared_key_with_alice);
}

#[test]
fn test_grumpkin_shared_k() {
// Create ECDH instances for Alice and Bob
let alice_ecdh = Grumpkin::new(grumpkin_alice_sk);
let bob_ecdh = Grumpkin::new(grumpkin_bob_sk);

// Derive public keys
let alice_pk: swPoint = alice_ecdh.derive_public_key();
let bob_pk: swPoint = bob_ecdh.derive_public_key();

// Compute shared keys
let shared_key_with_bob = alice_ecdh.derive_shared_key(bob_pk);
let shared_key_with_alice = bob_ecdh.derive_shared_key(alice_pk);

// Assert equality
assert_eq(shared_key_with_bob, shared_key_with_alice);
}