]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/dsa: change bitwise checks to mod operations
authorKatie Hockman <katie@golang.org>
Fri, 1 Nov 2019 15:15:44 +0000 (11:15 -0400)
committerKatie Hockman <katie@golang.org>
Sat, 2 Nov 2019 15:46:47 +0000 (15:46 +0000)
Even though bitwise operations may be slightly more
performant, the readability improvement of a mod
operation is worth the tradeoff.

Change-Id: I352c92ad355c6eb6ef99e3da00e1eff2d2ea5812
Reviewed-on: https://go-review.googlesource.com/c/go/+/204739
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Run-TryBot: Filippo Valsorda <filippo@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/crypto/dsa/dsa.go

index 575314b1b468908c3bb197ac656e368cb1f5e701..bc8e2f99bdc75a33cd04ba8c89eb357a3cd10147 100644 (file)
@@ -202,7 +202,7 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
        // FIPS 186-3, section 4.6
 
        n := priv.Q.BitLen()
-       if priv.Q.Sign() <= 0 || priv.P.Sign() <= 0 || priv.G.Sign() <= 0 || priv.X.Sign() <= 0 || n&7 != 0 {
+       if priv.Q.Sign() <= 0 || priv.P.Sign() <= 0 || priv.G.Sign() <= 0 || priv.X.Sign() <= 0 || n%8 != 0 {
                err = ErrInvalidPublicKey
                return
        }
@@ -281,7 +281,7 @@ func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
        w := new(big.Int).ModInverse(s, pub.Q)
 
        n := pub.Q.BitLen()
-       if n&7 != 0 {
+       if n%8 != 0 {
                return false
        }
        z := new(big.Int).SetBytes(hash)