diff --git a/Cargo.lock b/Cargo.lock index 02fdf9c..39b6705 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -341,6 +341,7 @@ dependencies = [ "base64 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "bencher 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "curve25519-dalek 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index bd28ee6..b946789 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ rayon = "1.0" base64 = "0.11" rand_core = { version = "0.5", default-features = false, features = ["getrandom"] } x25519-dalek = "0.6" +curve25519-dalek = "2.0" num_cpus = "1.0" [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index 31138c5..087fbe4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,3 +16,63 @@ pub fn trial(prefix: &str, start: usize, end: usize) -> Option<(String, String)> None } } + +use curve25519_dalek::{ scalar::Scalar, + constants::{ X25519_BASEPOINT, + ED25519_BASEPOINT_POINT }, +}; + +fn clamp_scalar(mut scalar: [u8; 32]) -> Scalar { + scalar[0] &= 248; + scalar[31] &= 127; + scalar[31] |= 64; + + Scalar::from_bits(scalar) +} +//use rand_core::CryptoRng; +use rand_core::RngCore; + +fn random_scalar() -> Scalar { + let mut scalar1 = [0u8; 32]; + let mut csprng = OsRng; + csprng.fill_bytes(&mut scalar1); + Scalar::from_bytes_mod_order(scalar1) +} + +pub fn testadd() { + let s1 = random_scalar(); + let s2 = random_scalar(); + let s3 = s1 + s2; + println!("s1 {:02x} {:?} {:02x}", s1.to_bytes()[0], s1.to_bytes(), s1.to_bytes()[31]); + println!("s2 {:02x} {:?} {:02x}", s2.to_bytes()[0], s2.to_bytes(), s2.to_bytes()[31]); + println!("s3 {:02x} {:?} {:02x}", s3.to_bytes()[0], s3.to_bytes(), s3.to_bytes()[31]); + + let p1 = s1 * ED25519_BASEPOINT_POINT; + let p2 = s2 * ED25519_BASEPOINT_POINT; + let p3 = s3 * ED25519_BASEPOINT_POINT; + + let p3b = p1 + p2; + assert_eq!(ED25519_BASEPOINT_POINT.to_montgomery(), + X25519_BASEPOINT); + assert_eq!(p3, p3b); + + let priv1 = StaticSecret::from(s3.to_bytes()); + let pub1 = PublicKey::from(p3.to_montgomery().to_bytes()); + let pub1b = PublicKey::from(&priv1); + assert_eq!(pub1.as_bytes(), pub1b.as_bytes()); + //let pub1 = s3 * X25519_BASEPOINT; + + + println!("yep equal") +} + +#[cfg(test)] +mod test { + use super::*; + #[test] + fn test_add() { + println!("testadd"); + testadd(); + } + +}