Skip to content

Commit

Permalink
also bump curve25519-dalek
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekleog-NEAR committed Nov 17, 2023
1 parent d01bd02 commit c99f73c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 27 deletions.
18 changes: 3 additions & 15 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ crossbeam = "0.8"
crossbeam-channel = "0.5.8"
crossbeam-queue = "0.3.8"
csv = "1.2.1"
curve25519-dalek = "3"
curve25519-dalek = { version = "4.1.1", features = ["rand_core"] }
derive-enum-from-into = "0.1.1"
derive_more = "0.99.9"
dirs = "4"
Expand Down
11 changes: 4 additions & 7 deletions core/crypto/src/key_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{signature, vrf, PublicKey};
use curve25519_dalek::edwards::{CompressedEdwardsY, EdwardsPoint};
use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
use std::mem::transmute;

pub fn is_valid_staking_key(public_key: &PublicKey) -> bool {
Expand All @@ -13,7 +12,7 @@ pub fn is_valid_staking_key(public_key: &PublicKey) -> bool {
}

pub fn convert_public_key(key: &signature::ED25519PublicKey) -> Option<vrf::PublicKey> {
let ep: EdwardsPoint = CompressedEdwardsY::from_slice(&key.0).decompress()?;
let ep: EdwardsPoint = CompressedEdwardsY::from_slice(&key.0).ok()?.decompress()?;
// All properly generated public keys are torsion-free. RistrettoPoint type can handle some values that are not torsion-free, but not all.
if !ep.is_torsion_free() {
return None;
Expand All @@ -24,11 +23,9 @@ pub fn convert_public_key(key: &signature::ED25519PublicKey) -> Option<vrf::Publ
}

pub fn convert_secret_key(key: &signature::ED25519SecretKey) -> vrf::SecretKey {
let b = ed25519_dalek::hazmat::ExpandedSecretKey::from(
<&[u8; 32]>::try_from(&key.0[..32]).unwrap(),
)
.scalar;
vrf::SecretKey::from_scalar(Scalar::from_bytes_mod_order(b.to_bytes()))
let b = <&[u8; 32]>::try_from(&key.0[..32]).unwrap();
let s = ed25519_dalek::hazmat::ExpandedSecretKey::from(b).scalar;
vrf::SecretKey::from_scalar(s)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion core/crypto/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Packable for Scalar {
type Packed = [u8; 32];

fn unpack(data: &[u8; 32]) -> Option<Self> {
Scalar::from_canonical_bytes(*data)
Scalar::from_canonical_bytes(*data).into()
}

fn pack(&self) -> [u8; 32] {
Expand Down
6 changes: 3 additions & 3 deletions core/crypto/src/vrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ impl PublicKey {
// manner.
#[allow(clippy::arithmetic_side_effects)]
fn basemul(s: Scalar) -> Point {
&s * &GT
&s * &*GT
}

fn safe_invert(s: Scalar) -> Scalar {
Scalar::conditional_select(&s, &Scalar::one(), s.ct_eq(&Scalar::zero())).invert()
Scalar::conditional_select(&s, &Scalar::ONE, s.ct_eq(&Scalar::ZERO)).invert()
}

impl SecretKey {
Expand Down Expand Up @@ -122,7 +122,7 @@ traits!(SecretKey, 32, |s| s.0.as_bytes(), "secret key");
mod tests {
use super::*;

use rand::rngs::OsRng;
use secp256k1::rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use serde_json::{from_str, to_string};

Expand Down

0 comments on commit c99f73c

Please sign in to comment.