diff --git a/src/bls12_381/ec.rs b/src/bls12_381/ec.rs index f2aefa2..f441cca 100644 --- a/src/bls12_381/ec.rs +++ b/src/bls12_381/ec.rs @@ -518,7 +518,7 @@ macro_rules! curve_impl { (*self).into() } - fn recommended_wnaf_for_scalar(scalar: ::Repr) -> Option { + fn recommended_wnaf_for_scalar(scalar: ::Repr) -> usize { Self::empirical_recommended_wnaf_for_scalar(scalar) } @@ -859,20 +859,17 @@ pub mod g1 { } impl G1 { - fn empirical_recommended_wnaf_for_scalar(scalar: FrRepr) -> Option + fn empirical_recommended_wnaf_for_scalar(scalar: FrRepr) -> usize { - const RECOMMENDATIONS: [usize; 3] = [12, 34, 130]; - - let mut ret = None; let num_bits = scalar.num_bits() as usize; - for (i, r) in RECOMMENDATIONS.iter().enumerate() { - if *r >= num_bits { - ret = Some(i + 2) - } + if num_bits >= 130 { + 4 + } else if num_bits >= 34 { + 3 + } else { + 2 } - - ret } fn empirical_recommended_wnaf_for_num_scalars(num_scalars: usize) -> usize @@ -1398,20 +1395,17 @@ pub mod g2 { } impl G2 { - fn empirical_recommended_wnaf_for_scalar(scalar: FrRepr) -> Option + fn empirical_recommended_wnaf_for_scalar(scalar: FrRepr) -> usize { - const RECOMMENDATIONS: [usize; 3] = [13, 37, 103]; - - let mut ret = None; let num_bits = scalar.num_bits() as usize; - for (i, r) in RECOMMENDATIONS.iter().enumerate() { - if *r >= num_bits { - ret = Some(i + 2) - } + if num_bits >= 103 { + 4 + } else if num_bits >= 37 { + 3 + } else { + 2 } - - ret } fn empirical_recommended_wnaf_for_num_scalars(num_scalars: usize) -> usize diff --git a/src/lib.rs b/src/lib.rs index 8b6c4b0..a3c4a2e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,10 +145,9 @@ pub trait CurveProjective: PartialEq + /// Converts this element into its affine representation. fn into_affine(&self) -> Self::Affine; - /// Recommends a wNAF window table size given a scalar. Returns `None` if normal - /// scalar multiplication is encouraged. If `Some` is returned, it will be between - /// 2 and 22, inclusive. - fn recommended_wnaf_for_scalar(scalar: ::Repr) -> Option; + /// Recommends a wNAF window table size given a scalar. Always returns a number + /// between 2 and 22, inclusive. + fn recommended_wnaf_for_scalar(scalar: ::Repr) -> usize; /// Recommends a wNAF window size given the number of scalars you intend to multiply /// a base by. Always returns a number between 2 and 22, inclusive. diff --git a/src/wnaf.rs b/src/wnaf.rs index 9b00989..03c0274 100644 --- a/src/wnaf.rs +++ b/src/wnaf.rs @@ -122,7 +122,7 @@ impl Wnaf<(), Vec, Vec> { ) -> Wnaf, &'a [i64]> { // Compute the appropriate window size for the scalar. - let window_size = G::recommended_wnaf_for_scalar(scalar).unwrap_or(2); // TODO + let window_size = G::recommended_wnaf_for_scalar(scalar); // Compute the wNAF form of the scalar. wnaf_form(&mut self.scalar, scalar, window_size);