Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crypto/tss/integration/tss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ func sign(homoFunc func() (homo.Crypto, error), threshold, num int, dkgResult *r
Expect(err).Should(BeNil())
// All R and S should be the same.
if r != nil {
Expect(r).Should(Equal(signerResult.R))
Expect(r).Should(Equal(signerResult.R.GetX()))
Expect(s).Should(Equal(signerResult.S))
} else {
r = signerResult.R
r = signerResult.R.GetX()
s = signerResult.S
}
}
Expand Down
32 changes: 30 additions & 2 deletions crypto/tss/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,38 @@ import (
)

type Result struct {
R *big.Int
R *pt.ECPoint
S *big.Int
}

// EthSignature returns the eth signature
// Use github.com/ethereum/go-ethereum/core/types.Transaction.WithSignature(signer, signatures)
// to get the signed transaction. Suggest to use types.Signer155.
func (r *Result) EthSignature() []byte {
n := r.R.GetCurve().Params().N
s := new(big.Int).Set(r.S)

// 1. Modify s to 0 < s < N /2
// ref: condition 283 in https://ethereum.github.io/yellowpaper/paper.pdf
// 2. Calculate recovery id
// https://ethereum.stackexchange.com/questions/42455/during-ecdsa-signing-how-do-i-generate-the-recovery-id
id := r.R.GetY().Bit(0)
if s.Cmp(new(big.Int).Rsh(n, 1)) > 0 {
s = new(big.Int).Neg(s)
s = s.Add(n, s)
id = id ^ 1
}

// The signature is 65 bytes, [R (32 bytes)|S (32 bytes)|recovery id (1 byte)]
sig := make([]byte, 65)
rBytes := r.R.GetX().Bytes()
copy(sig[32-len(rBytes):32], rBytes)
sBytes := s.Bytes()
copy(sig[64-len(sBytes):64], sBytes)
sig[64] = byte(id)
return sig
}

type Signer struct {
ph *pubkeyHandler
*message.MsgMain
Expand Down Expand Up @@ -84,7 +112,7 @@ func (s *Signer) GetResult() (*Result, error) {
}

return &Result{
R: new(big.Int).Set(rh.r.GetX()),
R: rh.r.Copy(),
S: new(big.Int).Set(rh.s),
}, nil
}
4 changes: 2 additions & 2 deletions crypto/tss/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ var _ = Describe("Signer", func() {
Expect(err).Should(BeNil())
// All R and S should be the same
if r != nil {
Expect(r).Should(Equal(result.R))
Expect(r).Should(Equal(result.R.GetX()))
Expect(s).Should(Equal(result.S))
} else {
r = result.R
r = result.R.GetX()
s = result.S
}
}
Expand Down