Always recommend a window table size.

This commit is contained in:
Sean Bowe 2017-09-28 08:38:13 -06:00
parent 894b44d034
commit bda22db9d5
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
3 changed files with 19 additions and 26 deletions

@ -518,7 +518,7 @@ macro_rules! curve_impl {
(*self).into()
}
fn recommended_wnaf_for_scalar(scalar: <Self::Scalar as PrimeField>::Repr) -> Option<usize> {
fn recommended_wnaf_for_scalar(scalar: <Self::Scalar as PrimeField>::Repr) -> usize {
Self::empirical_recommended_wnaf_for_scalar(scalar)
}
@ -859,22 +859,19 @@ pub mod g1 {
}
impl G1 {
fn empirical_recommended_wnaf_for_scalar(scalar: FrRepr) -> Option<usize>
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
{
const RECOMMENDATIONS: [usize; 12] = [1, 3, 7, 20, 43, 120, 273, 563, 1630, 3128, 7933, 62569];
@ -1398,22 +1395,19 @@ pub mod g2 {
}
impl G2 {
fn empirical_recommended_wnaf_for_scalar(scalar: FrRepr) -> Option<usize>
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
{
const RECOMMENDATIONS: [usize; 11] = [1, 3, 8, 20, 47, 126, 260, 826, 1501, 4555, 84071];

@ -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: <Self::Scalar as PrimeField>::Repr) -> Option<usize>;
/// Recommends a wNAF window table size given a scalar. Always returns a number
/// between 2 and 22, inclusive.
fn recommended_wnaf_for_scalar(scalar: <Self::Scalar as PrimeField>::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.

@ -122,7 +122,7 @@ impl<G: CurveProjective> Wnaf<(), Vec<G>, Vec<i64>> {
) -> Wnaf<usize, &'a mut Vec<G>, &'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);