• smasher164's avatar
    math/bits: make Add and Sub fallbacks constant time · 5ca44dc4
    smasher164 authored
    Make the extended precision add-with-carry and sub-with-carry operations
    take a constant amount of time to execute, regardless of input.
    
    name             old time/op  new time/op  delta
    Add-4            1.16ns ±11%  1.51ns ± 5%  +30.52%  (p=0.008 n=5+5)
    Add32-4          1.08ns ± 0%  1.03ns ± 1%   -4.86%  (p=0.029 n=4+4)
    Add64-4          1.09ns ± 1%  1.95ns ± 3%  +79.23%  (p=0.008 n=5+5)
    Add64multiple-4  4.03ns ± 1%  4.55ns ±11%  +13.07%  (p=0.008 n=5+5)
    Sub-4            1.08ns ± 1%  1.50ns ± 0%  +38.17%  (p=0.016 n=5+4)
    Sub32-4          1.09ns ± 2%  1.53ns ±10%  +40.26%  (p=0.008 n=5+5)
    Sub64-4          1.10ns ± 1%  1.47ns ± 1%  +33.39%  (p=0.008 n=5+5)
    Sub64multiple-4  4.30ns ± 2%  4.08ns ± 4%   -5.07%  (p=0.032 n=5+5)
    
    Fixes #31267
    
    Change-Id: I1824b1b3ab8f09902ce8b5fef84ce2fdb8847ed9
    Reviewed-on: https://go-review.googlesource.com/c/go/+/170758Reviewed-by: default avatarFilippo Valsorda <filippo@golang.org>
    Reviewed-by: default avatarKeith Randall <khr@golang.org>
    Run-TryBot: Filippo Valsorda <filippo@golang.org>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    5ca44dc4
bits.go 14.7 KB