Skip to content

Commit

Permalink
Merge pull request #6 from Kirillvs/bip146-using-lower-s
Browse files Browse the repository at this point in the history
BIP-146 using lower S at start
  • Loading branch information
mrtnetwork committed Aug 20, 2024
2 parents e37a7a8 + 40e0b85 commit 27052ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@

dist/
.idea
.tmp
17 changes: 5 additions & 12 deletions ecc/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func SingDer(message []byte, privateKey []byte, entryPointes []byte) []byte {
edr := new(big.Int).Add(e, dr)
s = new(big.Int).Mod(new(big.Int).Mul(kinv, edr), n)

if s.Cmp(new(big.Int).Div(n, big.NewInt(2))) > 0 {
s = new(big.Int).Sub(n, s)
}

if s.Cmp(big.NewInt(0)) != 0 {
break
}
Expand Down Expand Up @@ -118,21 +122,10 @@ func SingInput(privateKey []byte, message []byte, sigHash int) string {
R := signature[4 : 4+lengthR]
lengthS := int(signature[5+lengthR])
S := signature[5+lengthR+1:]
sAsBigint := formating.BytesToInt(S)

var newS []byte

if lengthS == 33 {
newSAsBigint := new(big.Int).Sub(P256k1().Params().N, sAsBigint)
newS = encodeBigInt(newSAsBigint)
lengthS -= 1
lengthTotal -= 1
} else {
newS = S
}
newSignature := append([]byte{derPrefix, byte(lengthTotal), byte(derTypeInt), byte(lengthR)}, R...)
newSignature = append(newSignature, byte(derTypeInt), byte(lengthS))
newSignature = append(newSignature, newS...)
newSignature = append(newSignature, S...)
newSignature = append(newSignature, byte(sigHash))
return formating.BytesToHex(newSignature)
}
Expand Down

0 comments on commit 27052ae

Please sign in to comment.