Skip to content

Commit

Permalink
mv ecdsa -> ecc, add ecdh
Browse files Browse the repository at this point in the history
  • Loading branch information
jtriley-eth committed Jul 1, 2024
1 parent 60bb255 commit 39405c6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/ecc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Elliptic Curve Cryptography

Elliptic curve cryptography takes advantage of the intractability of the elliptic curve discrete logarithm problem.

Let $E$ be an elliptic curve defined over a Galois (finite) field $\mathbb{F}_q$. Point addition forms a cyclic group
on $E(\mathbb{F}_q)$ with a generator point $G \in E(\mathbb{F}_q)$ and a point at infinite $\mathcal{O}$ such that:

$$
\forall P, Q \in E(\mathbb{F}_q) : P + Q = R \in E(\mathbb{F}_q)
\forall P \in E(\mathbb{F}_q) : P + \mathcal{O} = P
\forall P \in E(\mathbb{F}_q) : P + (-P) = \mathcal{O}
$$

Scalar multiplication is defined as iterative point addition such that:

$$
\forall P \in E(\mathbb{F}_q), k \in \mathbb{Z} : k \times P = P_k \in E(\mathbb{F}_q)
\forall P_k \in E(\mathbb{F}_q), \exists k \in \mathbb{Z} : k \times G = P_k
$$

## Application

The discrete logarithm problem and algebraic structure of elliptic curve point addition and scalar multiplication
allows for public key cryptography schemes such as digital signatures
([ECDSA](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm)) and key exchanges
([ECDH](https://en.wikipedia.org/wiki/Elliptic-curve_Diffie%E2%80%93Hellman)).
56 changes: 56 additions & 0 deletions src/ecc/ecdh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! ECDH key exchange

use self::field::prime::PlutoScalarField;
use super::*;

// PARAMETERS
// *******************************************
// CURVE the elliptic curve field and equation used
// G a point on the curve that generates a subgroup of large prime order n
// n integer order of G, means that n × G = O, n must also be prime.
// d_A the local private key (randomly selected) (scaler in F_n)
// Q_A the local public key d_A × G = Q_A (point on the curve)
// d_B the foreign private key (randomly selected) (scaler in F_n)
// Q_B the foreign public key d_B × G = Q_B (point on the curve)
// S the shared secret point S = d_A × Q_B = d_B × Q_A

/// SHARED SECRET COMPUTATION
/// *******************************************
/// 1. Compute the shared secret point S = d_A × Q_B.
///
/// ## Notes:
/// Elliptic Curve Diffie Hellman (ECDH) exchanges a shared secret over an insecure channel via the
/// commutativity and associativity of elliptic curve point multiplication.
///
/// d_A × (d_B × G) = d_B × (d_A × G)
/// d_B × Q_A = d_A × Q_B
pub fn compute_shared_secret(
d_a: PlutoScalarField,
q_b: AffinePoint<PlutoBaseCurve>,
) -> AffinePoint<PlutoBaseCurve> {
q_b * d_a
}

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

#[test]
fn test_key_exchange() {
// secret keys
let mut rns = rand::rngs::OsRng;
let d_a = PlutoScalarField::new(rand::Rng::gen_range(&mut rns, 1..=PlutoScalarField::ORDER));
let d_b = PlutoScalarField::new(rand::Rng::gen_range(&mut rns, 1..=PlutoScalarField::ORDER));

// public keys
let q_a = AffinePoint::<PlutoBaseCurve>::generator() * d_a;
let q_b = AffinePoint::<PlutoBaseCurve>::generator() * d_b;

// shared secret
let s_a = compute_shared_secret(d_a, q_b);
let s_b = compute_shared_secret(d_b, q_a);

println!("shared secrets = [\n\t{:?},\n\t{:?}\n]", s_a, s_b);
assert_eq!(s_a, s_b);
}
}
File renamed without changes.
5 changes: 5 additions & 0 deletions src/ecc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Elliptic curve cryptography primitives
use super::*;

pub mod ecdh;
pub mod ecdsa;
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
pub mod codes;
pub mod compiler;
pub mod curve;
pub mod ecdsa;
pub mod ecc;
pub mod encryption;
pub mod field;
pub mod hashes;
Expand Down

0 comments on commit 39405c6

Please sign in to comment.