From 39405c62fed7b7206ba7dc6d7b90c38f68de42d2 Mon Sep 17 00:00:00 2001 From: "jtriley.eth" Date: Mon, 1 Jul 2024 15:18:33 -0500 Subject: [PATCH] mv ecdsa -> ecc, add ecdh --- src/ecc/README.md | 26 ++++++++++++++++++++ src/ecc/ecdh.rs | 56 ++++++++++++++++++++++++++++++++++++++++++ src/{ => ecc}/ecdsa.rs | 0 src/ecc/mod.rs | 5 ++++ src/lib.rs | 2 +- 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/ecc/README.md create mode 100644 src/ecc/ecdh.rs rename src/{ => ecc}/ecdsa.rs (100%) create mode 100644 src/ecc/mod.rs diff --git a/src/ecc/README.md b/src/ecc/README.md new file mode 100644 index 0000000..36e2d19 --- /dev/null +++ b/src/ecc/README.md @@ -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)). diff --git a/src/ecc/ecdh.rs b/src/ecc/ecdh.rs new file mode 100644 index 0000000..6ed736f --- /dev/null +++ b/src/ecc/ecdh.rs @@ -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, +) -> AffinePoint { + 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::::generator() * d_a; + let q_b = AffinePoint::::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); + } +} diff --git a/src/ecdsa.rs b/src/ecc/ecdsa.rs similarity index 100% rename from src/ecdsa.rs rename to src/ecc/ecdsa.rs diff --git a/src/ecc/mod.rs b/src/ecc/mod.rs new file mode 100644 index 0000000..6f4fd45 --- /dev/null +++ b/src/ecc/mod.rs @@ -0,0 +1,5 @@ +//! Elliptic curve cryptography primitives +use super::*; + +pub mod ecdh; +pub mod ecdsa; diff --git a/src/lib.rs b/src/lib.rs index 7dab5ca..e51fa98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;