Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwample committed Jul 26, 2024
1 parent 2f1ecb0 commit e1a52c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
36 changes: 17 additions & 19 deletions curve25519-elligator2/src/elligator2/subgroup.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use super::*;
use crate::{MontgomeryPoint, traits::IsIdentity};
use crate::scalar::test::BASEPOINT_ORDER_MINUS_ONE;
use crate::{traits::IsIdentity, MontgomeryPoint};

use rand::Rng;
use rand_core::{CryptoRng, RngCore};


// Generates a new Keypair using, and returns the public key representative
// along, with its public key as a newly allocated edwards25519.Point.
fn generate<R:RngCore+CryptoRng>(rng: &mut R) -> ([u8; 32], EdwardsPoint) {
fn generate<R: RngCore + CryptoRng>(rng: &mut R) -> ([u8; 32], EdwardsPoint) {
for _ in 0..63 {
let y_sk = rng.gen::<[u8; 32]>();
let y_sk_tweak = rng.next_u32() as u8;
Expand All @@ -35,12 +34,11 @@ fn generate<R:RngCore+CryptoRng>(rng: &mut R) -> ([u8; 32], EdwardsPoint) {
/// BASEPOINT_ORDER_MINUS_ONE is the same as scMinusOne in filippo.io/edwards25519.
/// https://github.com/FiloSottile/edwards25519/blob/v1.0.0/scalar.go#L34
fn scalar_mult_order(v: &EdwardsPoint) -> EdwardsPoint {
// v * (L - 1) + v => v * L
let p = v * BASEPOINT_ORDER_MINUS_ONE;
p + v
// v * (L - 1) + v => v * L
let p = v * BASEPOINT_ORDER_MINUS_ONE;
p + v
}


#[test]
#[cfg(feature = "elligator2")]
/// pubkey_subgroup_check2 tests that Elligator representatives produced by
Expand Down Expand Up @@ -143,21 +141,23 @@ fn off_subgroup_check_edw() {
let v = scalar_mult_order(&pk);
let pk_off = !v.is_identity();

// ---
// ---

// check if the public key derived from the representative (top bit 0)
// is off the subgroup
let mut yr_255 = repr.clone();
let mut yr_255 = repr;
yr_255[31] &= 0xbf;
let pk_255 = EdwardsPoint::from_representative::<RFC9380>(&yr_255).expect("from_repr_255, should never fail");
let pk_255 = EdwardsPoint::from_representative::<RFC9380>(&yr_255)
.expect("from_repr_255, should never fail");
let v = scalar_mult_order(&pk_255);
let off_255 = !v.is_identity();

// check if the public key derived from the representative (top two bits 0 - as
// our representatives are) is off the subgroup.
let mut yr_254 = repr.clone();
let mut yr_254 = repr;
yr_254[31] &= 0x3f;
let pk_254 = EdwardsPoint::from_representative::<RFC9380>(&yr_254).expect("from_repr_254, should never fail");
let pk_254 = EdwardsPoint::from_representative::<RFC9380>(&yr_254)
.expect("from_repr_254, should never fail");
let v = scalar_mult_order(&pk_254);
let off_254 = !v.is_identity();

Expand All @@ -174,7 +174,7 @@ fn check(pk: MontgomeryPoint) -> bool {

/// check a point in the group, assuming it is a representative and given a
/// variant by which to convert it to a point.
fn check_r<V:MapToPointVariant>(r: [u8;32]) -> bool {
fn check_r<V: MapToPointVariant>(r: [u8; 32]) -> bool {
let pk = MontgomeryPoint::from_representative::<V>(&r).expect("from_representative failed");
check(pk)
}
Expand Down Expand Up @@ -223,15 +223,13 @@ fn off_subgroup_check_custom() {
}
}


/// Direct elligator map translate as accurately as possible from `obfs4-subgroup-check.py`.
fn elligator_dir_map(rb: [u8;32]) -> (FieldElement, FieldElement) {
fn elligator_dir_map(rb: [u8; 32]) -> (FieldElement, FieldElement) {
let r = FieldElement::from_bytes(&rb);
let two = &FieldElement::ONE + &FieldElement::ONE;
let ufactor = &-&two * &SQRT_M1;
let ufactor = &-&two * &SQRT_M1;
let (_, vfactor) = FieldElement::sqrt_ratio_i(&ufactor, &FieldElement::ONE);


let u = r.square();
let t1 = r.square2();
let v = &t1 + &FieldElement::ONE;
Expand All @@ -254,7 +252,7 @@ fn elligator_dir_map(rb: [u8;32]) -> (FieldElement, FieldElement) {
let u = &u * &t3;
let u = &u * &t2;
let u = &u * &t1;
let t1 = -&v;
let v = FieldElement::conditional_select(&v, &t1, is_sq ^ v.is_negative());
let t1 = -&v;
let v = FieldElement::conditional_select(&v, &t1, is_sq ^ v.is_negative());
(u, v)
}
4 changes: 2 additions & 2 deletions curve25519-elligator2/src/montgomery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ mod test {
let mut csprng = rand_core::OsRng;

for _ in 0..100 {
let p_edwards = rand_prime_order_point(&mut csprng);
let p_edwards = rand_prime_order_point(csprng);
let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery();

let s: Scalar = Scalar::random(&mut csprng);
Expand All @@ -527,7 +527,7 @@ mod test {

for _ in 0..100 {
// Make a random prime-order point P
let p_edwards = rand_prime_order_point(&mut csprng);
let p_edwards = rand_prime_order_point(csprng);
let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery();

// Make a random integer b
Expand Down

0 comments on commit e1a52c8

Please sign in to comment.