mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-13 19:46:39 +00:00
crypto/secp256k1: use ReadBits from common/math (#32430)
This commit is contained in:
parent
25cce4dfe4
commit
e798e26c69
2 changed files with 7 additions and 24 deletions
|
|
@ -35,29 +35,10 @@ package secp256k1
|
||||||
import (
|
import (
|
||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"math/big"
|
"math/big"
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
// number of bits in a big.Word
|
|
||||||
wordBits = 32 << (uint64(^big.Word(0)) >> 63)
|
|
||||||
// number of bytes in a big.Word
|
|
||||||
wordBytes = wordBits / 8
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// readBits encodes the absolute value of bigint as big-endian bytes. Callers
|
|
||||||
// must ensure that buf has enough space. If buf is too short the result will
|
|
||||||
// be incomplete.
|
|
||||||
func readBits(bigint *big.Int, buf []byte) {
|
|
||||||
i := len(buf)
|
|
||||||
for _, d := range bigint.Bits() {
|
|
||||||
for j := 0; j < wordBytes && i > 0; j++ {
|
|
||||||
i--
|
|
||||||
buf[i] = byte(d)
|
|
||||||
d >>= 8
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This code is from https://github.com/ThePiachu/GoBit and implements
|
// This code is from https://github.com/ThePiachu/GoBit and implements
|
||||||
// several Koblitz elliptic curves over prime fields.
|
// several Koblitz elliptic curves over prime fields.
|
||||||
//
|
//
|
||||||
|
|
@ -257,8 +238,8 @@ func (bitCurve *BitCurve) Marshal(x, y *big.Int) []byte {
|
||||||
byteLen := (bitCurve.BitSize + 7) >> 3
|
byteLen := (bitCurve.BitSize + 7) >> 3
|
||||||
ret := make([]byte, 1+2*byteLen)
|
ret := make([]byte, 1+2*byteLen)
|
||||||
ret[0] = 4 // uncompressed point flag
|
ret[0] = 4 // uncompressed point flag
|
||||||
readBits(x, ret[1:1+byteLen])
|
math.ReadBits(x, ret[1:1+byteLen])
|
||||||
readBits(y, ret[1+byteLen:])
|
math.ReadBits(y, ret[1+byteLen:])
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ package secp256k1
|
||||||
import (
|
import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -34,8 +36,8 @@ func (bitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int,
|
||||||
|
|
||||||
// Do the multiplication in C, updating point.
|
// Do the multiplication in C, updating point.
|
||||||
point := make([]byte, 64)
|
point := make([]byte, 64)
|
||||||
readBits(Bx, point[:32])
|
math.ReadBits(Bx, point[:32])
|
||||||
readBits(By, point[32:])
|
math.ReadBits(By, point[32:])
|
||||||
|
|
||||||
pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
|
pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
|
||||||
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
|
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue