forked from TrueCloudLab/frostfs-crypto
e9d1b1884c
- use elliptic.MarshalCompressed - use elliptic.UnmarshalCompressed - for older go versions use old methods - update dependencies - github.com/mr-tron/base58 v1.2.0 - github.com/pkg/errors v0.9.1 - github.com/stretchr/testify v1.7.0 Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
34 lines
683 B
Go
34 lines
683 B
Go
// +build go1.15
|
|
|
|
package crypto
|
|
|
|
import (
|
|
"crypto/elliptic"
|
|
"math/big"
|
|
)
|
|
|
|
func encodePoint(x, y *big.Int) []byte {
|
|
return elliptic.MarshalCompressed(curve, x, y)
|
|
}
|
|
|
|
func decodePoint(data []byte) (x *big.Int, y *big.Int) {
|
|
// empty data
|
|
if len(data) == 0 {
|
|
return
|
|
}
|
|
|
|
// tries to unmarshal compressed form
|
|
// returns (nil, nil) when:
|
|
// - wrong len(data)
|
|
// - data[0] != 2 && data[0] != 3
|
|
if x, y = elliptic.UnmarshalCompressed(curve, data); x != nil && y != nil {
|
|
return x, y
|
|
}
|
|
|
|
// tries to unmarshal uncompressed form and check that points on curve
|
|
if x, y = unmarshalXY(data); x == nil || y == nil || !curve.IsOnCurve(x, y) {
|
|
x, y = nil, nil
|
|
}
|
|
|
|
return x, y
|
|
}
|