Skip to content

Commit e798e26

Browse files
authored
crypto/secp256k1: use ReadBits from common/math (#32430)
1 parent 25cce4d commit e798e26

File tree

2 files changed

+7
-24
lines changed

2 files changed

+7
-24
lines changed

crypto/secp256k1/curve.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,10 @@ package secp256k1
3535
import (
3636
"crypto/elliptic"
3737
"math/big"
38-
)
3938

40-
const (
41-
// number of bits in a big.Word
42-
wordBits = 32 << (uint64(^big.Word(0)) >> 63)
43-
// number of bytes in a big.Word
44-
wordBytes = wordBits / 8
39+
"github.com/ethereum/go-ethereum/common/math"
4540
)
4641

47-
// readBits encodes the absolute value of bigint as big-endian bytes. Callers
48-
// must ensure that buf has enough space. If buf is too short the result will
49-
// be incomplete.
50-
func readBits(bigint *big.Int, buf []byte) {
51-
i := len(buf)
52-
for _, d := range bigint.Bits() {
53-
for j := 0; j < wordBytes && i > 0; j++ {
54-
i--
55-
buf[i] = byte(d)
56-
d >>= 8
57-
}
58-
}
59-
}
60-
6142
// This code is from https://github.com/ThePiachu/GoBit and implements
6243
// several Koblitz elliptic curves over prime fields.
6344
//
@@ -257,8 +238,8 @@ func (bitCurve *BitCurve) Marshal(x, y *big.Int) []byte {
257238
byteLen := (bitCurve.BitSize + 7) >> 3
258239
ret := make([]byte, 1+2*byteLen)
259240
ret[0] = 4 // uncompressed point flag
260-
readBits(x, ret[1:1+byteLen])
261-
readBits(y, ret[1+byteLen:])
241+
math.ReadBits(x, ret[1:1+byteLen])
242+
math.ReadBits(y, ret[1+byteLen:])
262243
return ret
263244
}
264245

crypto/secp256k1/scalar_mult_cgo.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package secp256k1
1010
import (
1111
"math/big"
1212
"unsafe"
13+
14+
"github.com/ethereum/go-ethereum/common/math"
1315
)
1416

1517
/*
@@ -34,8 +36,8 @@ func (bitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int,
3436

3537
// Do the multiplication in C, updating point.
3638
point := make([]byte, 64)
37-
readBits(Bx, point[:32])
38-
readBits(By, point[32:])
39+
math.ReadBits(Bx, point[:32])
40+
math.ReadBits(By, point[32:])
3941

4042
pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
4143
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))

0 commit comments

Comments
 (0)