File tree Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Original file line number Diff line number Diff line change @@ -664,15 +664,18 @@ func (x nat) bitLen() int {
664
664
// This function is used in cryptographic operations. It must not leak
665
665
// anything but the Int's sign and bit size through side-channels. Any
666
666
// changes must be reviewed by a security expert.
667
- //
668
- // In particular, bits.Len and bits.LeadingZeros use a lookup table for the
669
- // low-order bits on some architectures.
670
667
if i := len (x ) - 1 ; i >= 0 {
671
- l := i * _W
672
- for top := x [i ]; top != 0 ; top >>= 1 {
673
- l ++
674
- }
675
- return l
668
+ // bits.Len uses a lookup table for the low-order bits on some
669
+ // architectures. Neutralize any input-dependent behavior by setting all
670
+ // bits after the first one bit.
671
+ top := uint (x [i ])
672
+ top |= top >> 1
673
+ top |= top >> 2
674
+ top |= top >> 4
675
+ top |= top >> 8
676
+ top |= top >> 16
677
+ top |= top >> 16 >> 16 // ">> 32" doesn't compile on 32-bit architectures
678
+ return i * _W + bits .Len (top )
676
679
}
677
680
return 0
678
681
}
You can’t perform that action at this time.
0 commit comments