mirror of
https://github.com/saymrwulf/risc0-curve25519-dalek-source.git
synced 2026-09-08 20:40:32 +00:00
move implementation of x25519 into diffie_hellman
This commit is contained in:
parent
ccf7e5bb57
commit
8730bfbba6
4 changed files with 20 additions and 24 deletions
|
|
@ -57,13 +57,13 @@ shared secret with Bob by doing:
|
||||||
use x25519_dalek::EphemeralPublic;
|
use x25519_dalek::EphemeralPublic;
|
||||||
use x25519_dalek::EphemeralSecret;
|
use x25519_dalek::EphemeralSecret;
|
||||||
|
|
||||||
let shared_secret = EphemeralSecret::diffie_hellman(&alice_secret, &bob_public);
|
let shared_secret = EphemeralSecret::diffie_hellman(alice_secret, &bob_public);
|
||||||
```
|
```
|
||||||
|
|
||||||
Similarly, Bob computes the same shared secret by doing:
|
Similarly, Bob computes the same shared secret by doing:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
let shared_secret = EphemeralSecret::diffie_hellman(&bob_secret, &alice_public);
|
let shared_secret = EphemeralSecret::diffie_hellman(bob_secret, &alice_public);
|
||||||
```
|
```
|
||||||
|
|
||||||
Voilá! Alice and Bob can now use their shared secret to encrypt their
|
Voilá! Alice and Bob can now use their shared secret to encrypt their
|
||||||
|
|
|
||||||
|
|
@ -26,13 +26,14 @@ use x25519_dalek::EphemeralSecret;
|
||||||
|
|
||||||
fn bench_diffie_hellman(c: &mut Criterion) {
|
fn bench_diffie_hellman(c: &mut Criterion) {
|
||||||
let mut csprng: OsRng = OsRng::new().unwrap();
|
let mut csprng: OsRng = OsRng::new().unwrap();
|
||||||
let alice_secret: EphemeralSecret = EphemeralSecret::new(&mut csprng);
|
|
||||||
let bob_secret: EphemeralSecret = EphemeralSecret::new(&mut csprng);
|
let bob_secret: EphemeralSecret = EphemeralSecret::new(&mut csprng);
|
||||||
let bob_public: EphemeralPublic = EphemeralPublic::from(&bob_secret);
|
let bob_public: EphemeralPublic = EphemeralPublic::from(&bob_secret);
|
||||||
|
|
||||||
c.bench_function("diffie_hellman", move |b| {
|
c.bench_function("diffie_hellman", move |b| {
|
||||||
b.iter(||
|
let alice_secret: EphemeralSecret = EphemeralSecret::new(&mut csprng);
|
||||||
EphemeralSecret::diffie_hellman(&alice_secret, &bob_public)
|
b.iter_with_setup(
|
||||||
|
|| EphemeralSecret::new(&mut csprng),
|
||||||
|
|alice_secret| EphemeralSecret::diffie_hellman(alice_secret, &bob_public),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@
|
||||||
//! # let bob_public = EphemeralPublic::from(&bob_secret);
|
//! # let bob_public = EphemeralPublic::from(&bob_secret);
|
||||||
//! #
|
//! #
|
||||||
//! #
|
//! #
|
||||||
//! let shared_secret = EphemeralSecret::diffie_hellman(&alice_secret, &bob_public);
|
//! let shared_secret = EphemeralSecret::diffie_hellman(alice_secret, &bob_public);
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
|
@ -112,7 +112,7 @@
|
||||||
//! # let bob_secret = EphemeralSecret::new(&mut bob_csprng);
|
//! # let bob_secret = EphemeralSecret::new(&mut bob_csprng);
|
||||||
//! # let bob_public = EphemeralPublic::from(&bob_secret);
|
//! # let bob_public = EphemeralPublic::from(&bob_secret);
|
||||||
//! #
|
//! #
|
||||||
//! let shared_secret = EphemeralSecret::diffie_hellman(&bob_secret, &alice_public);
|
//! let shared_secret = EphemeralSecret::diffie_hellman(bob_secret, &alice_public);
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
|
|
||||||
|
|
@ -38,11 +38,13 @@ impl Drop for EphemeralSecret {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EphemeralSecret {
|
impl EphemeralSecret {
|
||||||
/// Utility function to make it easier to call `x25519()` with
|
/// The diffie_hellman function performs scalar multipication on a montegomery point.
|
||||||
/// an ephemeral secret key and montegomery point as input and
|
/// This is the implementation for the x25519 function, as specified in RFC7748.
|
||||||
/// a shared secret as the output.
|
pub fn diffie_hellman(self, their_public: &EphemeralPublic) -> SharedSecret {
|
||||||
pub fn diffie_hellman(&self, their_public: &EphemeralPublic) -> SharedSecret {
|
let k: Scalar = clamp_scalar(self.0.as_bytes());
|
||||||
SharedSecret(x25519(&self.0, &MontgomeryPoint(*their_public.0.as_bytes())))
|
let point: MontgomeryPoint = MontgomeryPoint(*their_public.0.as_bytes());
|
||||||
|
|
||||||
|
SharedSecret(k * point)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate an x25519 `EphemeralSecret` key.
|
/// Generate an x25519 `EphemeralSecret` key.
|
||||||
|
|
@ -105,13 +107,6 @@ fn clamp_scalar(scalar: &[u8; 32]) -> Scalar {
|
||||||
Scalar::from_bits(s)
|
Scalar::from_bits(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The x25519 function, as specified in RFC7748.
|
|
||||||
fn x25519(scalar: &Scalar, point: &MontgomeryPoint) -> MontgomeryPoint {
|
|
||||||
let k: Scalar = clamp_scalar(scalar.as_bytes());
|
|
||||||
|
|
||||||
(k * point)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -119,9 +114,9 @@ mod test {
|
||||||
fn do_rfc7748_ladder_test1(input_scalar: &Scalar,
|
fn do_rfc7748_ladder_test1(input_scalar: &Scalar,
|
||||||
input_point: &MontgomeryPoint,
|
input_point: &MontgomeryPoint,
|
||||||
expected: &[u8; 32]) {
|
expected: &[u8; 32]) {
|
||||||
let result = x25519(&input_scalar, &input_point);
|
let result = EphemeralSecret::diffie_hellman(EphemeralSecret(*input_scalar), &EphemeralPublic(*input_point));
|
||||||
|
|
||||||
assert_eq!(result.0, *expected);
|
assert_eq!(result.0, MontgomeryPoint(*expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -173,12 +168,12 @@ mod test {
|
||||||
|
|
||||||
let mut k: Scalar = Scalar::from_bits(X25519_BASEPOINT.0);
|
let mut k: Scalar = Scalar::from_bits(X25519_BASEPOINT.0);
|
||||||
let mut u: MontgomeryPoint = X25519_BASEPOINT;
|
let mut u: MontgomeryPoint = X25519_BASEPOINT;
|
||||||
let mut result: MontgomeryPoint;
|
let mut result: SharedSecret;
|
||||||
|
|
||||||
macro_rules! do_iterations {
|
macro_rules! do_iterations {
|
||||||
($n:expr) => (
|
($n:expr) => (
|
||||||
for _ in 0..$n {
|
for _ in 0..$n {
|
||||||
result = x25519(&k, &u);
|
result = EphemeralSecret::diffie_hellman(EphemeralSecret(k), &EphemeralPublic(u));
|
||||||
// OBVIOUS THING THAT I'M GOING TO NOTE ANYWAY BECAUSE I'VE
|
// OBVIOUS THING THAT I'M GOING TO NOTE ANYWAY BECAUSE I'VE
|
||||||
// SEEN PEOPLE DO THIS WITH GOLANG'S STDLIB AND YOU SURE AS
|
// SEEN PEOPLE DO THIS WITH GOLANG'S STDLIB AND YOU SURE AS
|
||||||
// HELL SHOULDN'T DO HORRIBLY STUPID THINGS LIKE THIS WITH
|
// HELL SHOULDN'T DO HORRIBLY STUPID THINGS LIKE THIS WITH
|
||||||
|
|
@ -188,7 +183,7 @@ mod test {
|
||||||
//
|
//
|
||||||
// ↓↓ DON'T DO THIS ↓↓
|
// ↓↓ DON'T DO THIS ↓↓
|
||||||
u = MontgomeryPoint(k.as_bytes().clone());
|
u = MontgomeryPoint(k.as_bytes().clone());
|
||||||
k = Scalar::from_bits(result.to_bytes());
|
k = Scalar::from_bits(result.0.to_bytes());
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue