@@ -415,7 +415,7 @@ pub fn elem_exp_consttime<M>(
415
415
base : Elem < M , R > ,
416
416
exponent : & PrivateExponent ,
417
417
m : & Modulus < M > ,
418
- ) -> Result < Elem < M , Unencoded > , error :: Unspecified > {
418
+ ) -> Result < Elem < M , Unencoded > , LimbSliceError > {
419
419
use crate :: { bssl, limb:: Window } ;
420
420
421
421
const WINDOW_BITS : usize = 5 ;
@@ -476,8 +476,7 @@ pub fn elem_exp_consttime<M>(
476
476
let src1 = entry ( previous, src1, num_limbs) ;
477
477
let src2 = entry ( previous, src2, num_limbs) ;
478
478
let dst = entry_mut ( rest, 0 , num_limbs) ;
479
- limbs_mul_mont ( ( dst, src1, src2) , m. limbs ( ) , m. n0 ( ) , m. cpu_features ( ) )
480
- . map_err ( error:: erase :: < LimbSliceError > ) ?;
479
+ limbs_mul_mont ( ( dst, src1, src2) , m. limbs ( ) , m. n0 ( ) , m. cpu_features ( ) ) ?;
481
480
}
482
481
483
482
let tmp = m. zero ( ) ;
@@ -502,12 +501,10 @@ pub fn elem_exp_consttime<M>(
502
501
base : Elem < M , R > ,
503
502
exponent : & PrivateExponent ,
504
503
m : & Modulus < M > ,
505
- ) -> Result < Elem < M , Unencoded > , error:: Unspecified > {
504
+ ) -> Result < Elem < M , Unencoded > , LimbSliceError > {
505
+ use super :: x86_64_mont:: { gather5, mul_mont_gather5_amm, power5_amm, scatter5} ;
506
506
use crate :: { cpu, limb:: LIMB_BYTES } ;
507
507
508
- // Pretty much all the math here requires CPU feature detection to have
509
- // been done. `cpu_features` isn't threaded through all the internal
510
- // functions, so just make it clear that it has been done at this point.
511
508
let cpu_features = m. cpu_features ( ) ;
512
509
513
510
// The x86_64 assembly was written under the assumption that the input data
@@ -534,85 +531,6 @@ pub fn elem_exp_consttime<M>(
534
531
table. split_at_mut ( TABLE_ENTRIES * num_limbs)
535
532
} ;
536
533
537
- fn scatter ( table : & mut [ Limb ] , acc : & [ Limb ] , i : LeakyWindow , num_limbs : usize ) {
538
- prefixed_extern ! {
539
- fn bn_scatter5( a: * const Limb , a_len: c:: size_t, table: * mut Limb , i: LeakyWindow ) ;
540
- }
541
- unsafe { bn_scatter5 ( acc. as_ptr ( ) , num_limbs, table. as_mut_ptr ( ) , i) }
542
- }
543
-
544
- fn gather ( table : & [ Limb ] , acc : & mut [ Limb ] , i : Window , num_limbs : usize ) {
545
- prefixed_extern ! {
546
- fn bn_gather5( r: * mut Limb , a_len: c:: size_t, table: * const Limb , i: Window ) ;
547
- }
548
- unsafe { bn_gather5 ( acc. as_mut_ptr ( ) , num_limbs, table. as_ptr ( ) , i) }
549
- }
550
-
551
- fn limbs_mul_mont_gather5_amm (
552
- table : & [ Limb ] ,
553
- acc : & mut [ Limb ] ,
554
- base : & [ Limb ] ,
555
- m : & [ Limb ] ,
556
- n0 : & N0 ,
557
- i : Window ,
558
- num_limbs : usize ,
559
- ) {
560
- prefixed_extern ! {
561
- fn bn_mul_mont_gather5(
562
- rp: * mut Limb ,
563
- ap: * const Limb ,
564
- table: * const Limb ,
565
- np: * const Limb ,
566
- n0: & N0 ,
567
- num: c:: size_t,
568
- power: Window ,
569
- ) ;
570
- }
571
- unsafe {
572
- bn_mul_mont_gather5 (
573
- acc. as_mut_ptr ( ) ,
574
- base. as_ptr ( ) ,
575
- table. as_ptr ( ) ,
576
- m. as_ptr ( ) ,
577
- n0,
578
- num_limbs,
579
- i,
580
- ) ;
581
- }
582
- }
583
-
584
- fn power_amm (
585
- table : & [ Limb ] ,
586
- acc : & mut [ Limb ] ,
587
- m_cached : & [ Limb ] ,
588
- n0 : & N0 ,
589
- i : Window ,
590
- num_limbs : usize ,
591
- ) {
592
- prefixed_extern ! {
593
- fn bn_power5(
594
- r: * mut Limb ,
595
- a: * const Limb ,
596
- table: * const Limb ,
597
- n: * const Limb ,
598
- n0: & N0 ,
599
- num: c:: size_t,
600
- i: Window ,
601
- ) ;
602
- }
603
- unsafe {
604
- bn_power5 (
605
- acc. as_mut_ptr ( ) ,
606
- acc. as_ptr ( ) ,
607
- table. as_ptr ( ) ,
608
- m_cached. as_ptr ( ) ,
609
- n0,
610
- num_limbs,
611
- i,
612
- ) ;
613
- }
614
- }
615
-
616
534
// These are named `(tmp, am, np)` in BoringSSL.
617
535
let ( acc, base_cached, m_cached) : ( & mut [ Limb ] , & [ Limb ] , & [ Limb ] ) = {
618
536
let ( acc, rest) = state. split_at_mut ( num_limbs) ;
@@ -639,11 +557,10 @@ pub fn elem_exp_consttime<M>(
639
557
m_cached : & [ Limb ] ,
640
558
n0 : & N0 ,
641
559
mut i : LeakyWindow ,
642
- num_limbs : usize ,
643
560
cpu_features : cpu:: Features ,
644
561
) -> Result < ( ) , LimbSliceError > {
645
562
loop {
646
- scatter ( table , acc, i , num_limbs ) ;
563
+ scatter5 ( acc, table , i ) ? ;
647
564
i *= 2 ;
648
565
if i >= TABLE_ENTRIES as LeakyWindow {
649
566
break ;
@@ -657,38 +574,34 @@ pub fn elem_exp_consttime<M>(
657
574
658
575
// acc = table[0] = base**0 (i.e. 1).
659
576
m. oneR ( acc) ;
660
- scatter ( table , acc, 0 , num_limbs ) ;
577
+ scatter5 ( acc, table , 0 ) ? ;
661
578
662
579
// acc = base**1 (i.e. base).
663
580
acc. copy_from_slice ( base_cached) ;
664
581
665
582
// Fill in entries 1, 2, 4, 8, 16.
666
- scatter_powers_of_2 ( table, acc, m_cached, n0, 1 , num_limbs, cpu_features)
667
- . map_err ( error:: erase :: < LimbSliceError > ) ?;
583
+ scatter_powers_of_2 ( table, acc, m_cached, n0, 1 , cpu_features) ?;
668
584
// Fill in entries 3, 6, 12, 24; 5, 10, 20, 30; 7, 14, 28; 9, 18; 11, 22; 13, 26; 15, 30;
669
585
// 17; 19; 21; 23; 25; 27; 29; 31.
670
586
for i in ( 3 ..( TABLE_ENTRIES as LeakyWindow ) ) . step_by ( 2 ) {
671
- limbs_mul_mont_gather5_amm (
672
- table,
673
- acc,
674
- base_cached,
675
- m_cached,
676
- n0,
677
- Window :: from ( i - 1 ) , // Not secret
678
- num_limbs,
679
- ) ;
680
- scatter_powers_of_2 ( table, acc, m_cached, n0, i, num_limbs, cpu_features)
681
- . map_err ( error:: erase :: < LimbSliceError > ) ?;
587
+ let power = Window :: from ( i - 1 ) ;
588
+ assert ! ( power < 32 ) ; // Not secret,
589
+ unsafe {
590
+ mul_mont_gather5_amm ( acc, base_cached, table, m_cached, n0, power, cpu_features)
591
+ } ?;
592
+ scatter_powers_of_2 ( table, acc, m_cached, n0, i, cpu_features) ?;
682
593
}
683
594
684
595
let acc = limb:: fold_5_bit_windows (
685
596
exponent. limbs ( ) ,
686
597
|initial_window| {
687
- gather ( table, acc, initial_window, num_limbs) ;
598
+ unsafe { gather5 ( acc, table, initial_window) }
599
+ . unwrap_or_else ( unwrap_impossible_limb_slice_error) ;
688
600
acc
689
601
} ,
690
602
|acc, window| {
691
- power_amm ( table, acc, m_cached, n0, window, num_limbs) ;
603
+ unsafe { power5_amm ( acc, table, m_cached, n0, window, cpu_features) }
604
+ . unwrap_or_else ( unwrap_impossible_limb_slice_error) ;
692
605
acc
693
606
} ,
694
607
) ;
@@ -766,7 +679,9 @@ mod tests {
766
679
. expect ( "valid exponent" )
767
680
} ;
768
681
let base = into_encoded ( base, & m) ;
769
- let actual_result = elem_exp_consttime ( base, & e, & m) . unwrap ( ) ;
682
+ let actual_result = elem_exp_consttime ( base, & e, & m)
683
+ . map_err ( error:: erase :: < LimbSliceError > )
684
+ . unwrap ( ) ;
770
685
assert_elem_eq ( & actual_result, & expected_result) ;
771
686
772
687
Ok ( ( ) )
0 commit comments