Skip to content

Commit

Permalink
Optimize ed25519 hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
survived committed Apr 19, 2024
1 parent d8dd7fc commit ca49116
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ exclude = [
[patch.crates-io.generic-ec]
git = "https://github.com/dfns/generic-ec"
branch = "faster-multiscalar"

[patch.crates-io.generic-ec-curves]
git = "https://github.com/dfns/generic-ec"
branch = "faster-multiscalar"
5 changes: 4 additions & 1 deletion givre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cggmp21-keygen = { version = "0.1", optional = true }
key-share = { version = "0.2.2", default-features = false }

generic-ec = { version = "0.2.4", default-features = false, features = ["alloc"] }
generic-ec-curves = { version = "0.1", default-features = false, optional = true }

rand_core = { version = "0.6", default-features = false }
digest = { version = "0.10", default-features = false }
Expand All @@ -23,6 +24,8 @@ sha2 = { version = "0.10", default-features = false, optional = true }

serde = { version = "1", default-features = false, features = ["derive"], optional = true }

curve25519-dalek = { version = "4", default-features = false, optional = true }

[dev-dependencies]
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }

Expand All @@ -47,6 +50,6 @@ hd-wallets = ["key-share/hd-wallets", "cggmp21-keygen?/hd-wallets"]

all-ciphersuites = ["ciphersuite-secp256k1", "ciphersuite-ed25519", "ciphersuite-bitcoin"]
ciphersuite-secp256k1 = ["generic-ec/curve-secp256k1", "k256", "sha2", "static_assertions"]
ciphersuite-ed25519 = ["generic-ec/curve-ed25519", "sha2"]
ciphersuite-ed25519 = ["generic-ec/curve-ed25519", "sha2", "curve25519-dalek", "generic-ec-curves/ed25519"]
ciphersuite-bitcoin = ["ciphersuite-secp256k1"]

17 changes: 14 additions & 3 deletions givre/src/ciphersuite/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Ciphersuite for Ed25519 {
}
let hash = hash.finalize();

generic_ec::Scalar::from_le_bytes_mod_order(hash)
reduce_512bits_le_scalar_mod_order(&hash.into())
}

fn compute_challenge(
Expand All @@ -37,7 +37,7 @@ impl Ciphersuite for Ed25519 {
.chain_update(msg)
.finalize();

generic_ec::Scalar::from_le_bytes_mod_order(hash)
reduce_512bits_le_scalar_mod_order(&hash.into())
}

fn h3(msg: &[&[u8]]) -> generic_ec::Scalar<Self::Curve> {
Expand All @@ -49,7 +49,7 @@ impl Ciphersuite for Ed25519 {
}
let hash = hash.finalize();

generic_ec::Scalar::from_le_bytes_mod_order(hash)
reduce_512bits_le_scalar_mod_order(&hash.into())
}

fn h4() -> Self::Digest {
Expand Down Expand Up @@ -100,3 +100,14 @@ impl Ciphersuite for Ed25519 {
Ok(Self::normalize_point(point))
}
}

/// Reduces 512 bits integer mod curve order
///
/// This is a more efficient version of [`generic_ec::Scalar::from_le_bytes_mod_order`]
fn reduce_512bits_le_scalar_mod_order(
bytes: &[u8; 64],
) -> generic_ec::Scalar<generic_ec::curves::Ed25519> {
let out = curve25519_dalek::Scalar::from_bytes_mod_order_wide(bytes);
let out = generic_ec_curves::ed25519::Scalar(out);
generic_ec::as_raw::FromRaw::from_raw(out)
}

0 comments on commit ca49116

Please sign in to comment.