mirror of
https://github.com/onsonr/sonr.git
synced 2025-03-10 21:09:11 +00:00
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
|
package keys
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"math/big"
|
||
|
|
||
|
"github.com/onsonr/sonr/crypto/core/curves"
|
||
|
)
|
||
|
|
||
|
// getEcdsaPoint builds an elliptic curve point from a compressed byte slice
|
||
|
func getEcdsaPoint(pubKey []byte) (*curves.EcPoint, error) {
|
||
|
crv := curves.K256()
|
||
|
x := new(big.Int).SetBytes(pubKey[1:33])
|
||
|
y := new(big.Int).SetBytes(pubKey[33:])
|
||
|
ecCurve, err := crv.ToEllipticCurve()
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error converting curve: %v", err)
|
||
|
}
|
||
|
return &curves.EcPoint{X: x, Y: y, Curve: ecCurve}, nil
|
||
|
}
|
||
|
|
||
|
// SerializeSecp256k1Signature serializes an ECDSA signature into a byte slice
|
||
|
func serializeSignature(sig *curves.EcdsaSignature) ([]byte, error) {
|
||
|
rBytes := sig.R.Bytes()
|
||
|
sBytes := sig.S.Bytes()
|
||
|
|
||
|
sigBytes := make([]byte, 66) // V (1 byte) + R (32 bytes) + S (32 bytes)
|
||
|
sigBytes[0] = byte(sig.V)
|
||
|
copy(sigBytes[33-len(rBytes):33], rBytes)
|
||
|
copy(sigBytes[66-len(sBytes):66], sBytes)
|
||
|
return sigBytes, nil
|
||
|
}
|
||
|
|
||
|
// DeserializeSecp256k1Signature deserializes an ECDSA signature from a byte slice
|
||
|
func deserializeSignature(sigBytes []byte) (*curves.EcdsaSignature, error) {
|
||
|
if len(sigBytes) != 66 {
|
||
|
return nil, errors.New("malformed signature: not the correct size")
|
||
|
}
|
||
|
sig := &curves.EcdsaSignature{
|
||
|
V: int(sigBytes[0]),
|
||
|
R: new(big.Int).SetBytes(sigBytes[1:33]),
|
||
|
S: new(big.Int).SetBytes(sigBytes[33:66]),
|
||
|
}
|
||
|
return sig, nil
|
||
|
}
|