Skip to content

Commit a46d67d

Browse files
committed
chore: upgrade curve25519-dalek to v4
1 parent ba81886 commit a46d67d

File tree

4 files changed

+27
-63
lines changed

4 files changed

+27
-63
lines changed

Cargo.lock

Lines changed: 11 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stacks-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ features = ["serde", "recovery"]
5454
workspace = true
5555

5656
[dependencies.curve25519-dalek]
57-
version = "=2.0.0"
57+
version = "4.1.3"
5858
features = ["serde"]
5959

6060
[dependencies.time]

stacks-common/src/util/vrf.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::{error, fmt};
2626

2727
use curve25519_dalek::constants::ED25519_BASEPOINT_POINT;
2828
use curve25519_dalek::edwards::{CompressedEdwardsY, EdwardsPoint};
29-
use curve25519_dalek::scalar::Scalar as ed25519_Scalar;
29+
use curve25519_dalek::scalar::{clamp_integer, Scalar as ed25519_Scalar};
3030
use rand;
3131
use sha2::{Digest, Sha512};
3232

@@ -246,7 +246,7 @@ impl VRFProof {
246246

247247
#[allow(clippy::needless_range_loop)]
248248
pub fn check_c(c: &ed25519_Scalar) -> bool {
249-
let c_bytes = c.reduce().to_bytes();
249+
let c_bytes = c.to_bytes();
250250

251251
// upper 16 bytes of c must be 0's
252252
for c_byte in c_bytes[16..32].iter() {
@@ -281,7 +281,9 @@ impl VRFProof {
281281
// 0 32 48 80
282282
// |----------------------------|----------|---------------------------|
283283
// Gamma point c scalar s scalar
284-
let gamma_opt = CompressedEdwardsY::from_slice(&bytes[0..32]).decompress();
284+
let gamma_opt = CompressedEdwardsY::from_slice(&bytes[0..32])
285+
.unwrap()
286+
.decompress();
285287
if gamma_opt.is_none() {
286288
test_debug!("Invalid Gamma");
287289
return None;
@@ -297,8 +299,8 @@ impl VRFProof {
297299

298300
c_buf[..16].copy_from_slice(&bytes[32..(16 + 32)]);
299301
s_buf[..32].copy_from_slice(&bytes[48..(32 + 48)]);
300-
let c = ed25519_Scalar::from_canonical_bytes(c_buf)?;
301-
let s = ed25519_Scalar::from_canonical_bytes(s_buf)?;
302+
let c = ed25519_Scalar::from_canonical_bytes(c_buf).expect("Invalid C scalar");
303+
let s = ed25519_Scalar::from_canonical_bytes(s_buf).expect("Invalid S scalar");
302304

303305
Some(VRFProof { Gamma: gamma, c, s })
304306
}
@@ -324,7 +326,7 @@ impl VRFProof {
324326
"FATAL ERROR: somehow constructed an invalid ECVRF proof"
325327
);
326328

327-
let c_bytes = self.c.reduce().to_bytes();
329+
let c_bytes = self.c.to_bytes();
328330
c_bytes_16[0..16].copy_from_slice(&c_bytes[0..16]);
329331

330332
let gamma_bytes = self.Gamma.compress().to_bytes();
@@ -386,7 +388,7 @@ impl VRF {
386388
}
387389

388390
let y = CompressedEdwardsY::from_slice(&hasher.finalize()[0..32]);
389-
if let Some(h) = y.decompress() {
391+
if let Some(h) = y.unwrap().decompress() {
390392
break h;
391393
}
392394

@@ -445,8 +447,7 @@ impl VRF {
445447
let mut h_32 = [0u8; 32];
446448
h_32.copy_from_slice(&h[0..32]);
447449

448-
let x_scalar_raw = ed25519_Scalar::from_bits(h_32);
449-
let x_scalar = x_scalar_raw.reduce(); // use the canonical scalar for the private key
450+
let x_scalar = ed25519_Scalar::from_bytes_mod_order(clamp_integer(h_32));
450451

451452
trunc_hash.copy_from_slice(&h[32..64]);
452453

@@ -473,7 +474,7 @@ impl VRF {
473474
let mut scalar_buf = [0u8; 32];
474475
scalar_buf[0..16].copy_from_slice(hash128);
475476

476-
ed25519_Scalar::from_bits(scalar_buf)
477+
ed25519_Scalar::from_canonical_bytes(scalar_buf).expect("Invalid scalar")
477478
}
478479

479480
/// ECVRF proof routine
@@ -492,8 +493,7 @@ impl VRF {
492493
let c_hashbuf = VRF::hash_points(&H_point, &Gamma_point, &kB_point, &kH_point);
493494
let c_scalar = VRF::ed25519_scalar_from_hash128(&c_hashbuf);
494495

495-
let s_full_scalar = &k_scalar + &c_scalar * &x_scalar;
496-
let s_scalar = s_full_scalar.reduce();
496+
let s_scalar = &k_scalar + &c_scalar * &x_scalar;
497497

498498
// NOTE: expect() won't panic because c_scalar is guaranteed to have
499499
// its upper 16 bytes as 0
@@ -509,16 +509,16 @@ impl VRF {
509509
#[allow(clippy::op_ref)]
510510
pub fn verify(Y_point: &VRFPublicKey, proof: &VRFProof, alpha: &[u8]) -> Result<bool, Error> {
511511
let H_point = VRF::hash_to_curve(Y_point, alpha);
512-
let s_reduced = proof.s().reduce();
512+
let s_reduced = proof.s();
513513
let Y_point_ed = CompressedEdwardsY(Y_point.to_bytes())
514514
.decompress()
515515
.ok_or(Error::InvalidPublicKey)?;
516516
if proof.Gamma().is_small_order() {
517517
return Err(Error::InvalidPublicKey);
518518
}
519519

520-
let U_point = &s_reduced * &ED25519_BASEPOINT_POINT - proof.c() * Y_point_ed;
521-
let V_point = &s_reduced * &H_point - proof.c() * proof.Gamma();
520+
let U_point = s_reduced * &ED25519_BASEPOINT_POINT - proof.c() * Y_point_ed;
521+
let V_point = s_reduced * &H_point - proof.c() * proof.Gamma();
522522

523523
let c_prime_hashbuf = VRF::hash_points(&H_point, proof.Gamma(), &U_point, &V_point);
524524
let c_prime = VRF::ed25519_scalar_from_hash128(&c_prime_hashbuf);

stackslib/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ features = ["serde", "recovery"]
8383
[dependencies.ed25519-dalek]
8484
workspace = true
8585

86-
[dependencies.curve25519-dalek]
87-
version = "=2.0.0"
88-
features = ["serde"]
89-
9086
[dependencies.time]
9187
version = "0.2.23"
9288
features = ["std"]

0 commit comments

Comments
 (0)