Skip to content

Commit 24fdb8f

Browse files
committed
implement adapt method for adaptor signature
1 parent 83bda02 commit 24fdb8f

File tree

5 files changed

+71
-21
lines changed

5 files changed

+71
-21
lines changed

crypto/adaptor/adapt.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package adaptor
2+
3+
import (
4+
"github.com/btcsuite/btcd/btcec/v2/schnorr"
5+
"github.com/decred/dcrd/dcrec/secp256k1/v4"
6+
)
7+
8+
// Adapt adapts the given adaptor signature with the specified secret
9+
// Asume that the given adaptor signature is valid
10+
func Adapt(sigBytes []byte, secretBytes []byte) []byte {
11+
rPoint, _ := schnorr.ParsePubKey(sigBytes[0:32])
12+
var R secp256k1.JacobianPoint
13+
rPoint.AsJacobian(&R)
14+
15+
var s secp256k1.ModNScalar
16+
s.SetByteSlice(sigBytes[32:64])
17+
18+
var secret secp256k1.ModNScalar
19+
secret.SetByteSlice(secretBytes)
20+
21+
var adaptorPoint secp256k1.JacobianPoint
22+
secp256k1.ScalarBaseMultNonConst(&secret, &adaptorPoint)
23+
24+
var adaptedR secp256k1.JacobianPoint
25+
secp256k1.AddNonConst(&R, &adaptorPoint, &adaptedR)
26+
adaptedR.ToAffine()
27+
28+
var adaptedS secp256k1.ModNScalar
29+
if !adaptedR.Y.IsOdd() {
30+
adaptedS = *s.Add(&secret)
31+
} else {
32+
adaptedS = *s.Add(secret.Negate())
33+
}
34+
35+
adaptedSig := Signature{
36+
r: adaptedR.X,
37+
s: adaptedS,
38+
}
39+
40+
return adaptedSig.Serialize()
41+
}

crypto/adaptor/types.go

+30-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
// scalarSize is the size of an encoded big endian scalar
88
const scalarSize = 32
99

10-
// Signature is same with schnorr.Signature
10+
// Signature represents the signature
1111
type Signature struct {
1212
r btcec.FieldVal
1313
s btcec.ModNScalar
@@ -28,16 +28,40 @@ func NewSignature(sigBytes []byte) *Signature {
2828
}
2929
}
3030

31-
// NegatePoint negates the given point
32-
func NegatePoint(point *btcec.JacobianPoint) *btcec.JacobianPoint {
33-
result := *point
34-
result.Y.Negate(1).Normalize()
31+
// Serialize serializes the signature
32+
func (s *Signature) Serialize() []byte {
33+
sig := make([]byte, 64)
3534

36-
return &result
35+
rBytes := *s.r.Bytes()
36+
sBytes := s.s.Bytes()
37+
38+
copy(sig[0:32], rBytes[:])
39+
copy(sig[32:64], sBytes[:])
40+
41+
return sig
3742
}
3843

3944
// SerializeScalar serializes the given scalar
4045
func SerializeScalar(scalar *btcec.ModNScalar) []byte {
4146
bz := scalar.Bytes()
4247
return bz[:]
4348
}
49+
50+
// SecretToPubKey gets the serialized public key of the given secret on the secp256k1 curve
51+
func SecretToPubKey(secretBytes []byte) []byte {
52+
var secret btcec.ModNScalar
53+
secret.SetByteSlice(secretBytes)
54+
55+
var result btcec.JacobianPoint
56+
btcec.ScalarBaseMultNonConst(&secret, &result)
57+
58+
return btcec.JacobianToByteSlice(result)
59+
}
60+
61+
// NegatePoint negates the given point
62+
func NegatePoint(point *btcec.JacobianPoint) *btcec.JacobianPoint {
63+
result := *point
64+
result.Y.Negate(1).Normalize()
65+
66+
return &result
67+
}
File renamed without changes.
File renamed without changes.

x/lending/types/utils.go

-15
This file was deleted.

0 commit comments

Comments
 (0)