fix R and S may not be 32 bytes (#130)

* fix R and S may not be 32 bytes

* refactor according to review comments

(cherry picked from commit 49366aa435b3d370283820d567c75fe0ea30079b)

# Conflicts:
#	ecdsa/signing/finalize.go
#	ecdsa/signing/local_party_test.go
This commit is contained in:
cong
2021-04-02 13:49:14 +08:00
committed by Cong Zhao
parent 221ef902d3
commit acfc4a91f8
2 changed files with 8 additions and 7 deletions

View File

@@ -56,8 +56,9 @@ func (round *finalization) Start() *tss.Error {
}
// save the signature for final output
round.data.R = fillTo32BytesInPlace(round.temp.rx.Bytes())
round.data.S = fillTo32BytesInPlace(sumS.Bytes())
bitSizeInBytes := tss.EC().Params().BitSize / 8
round.data.R = padToLengthBytesInPlace(round.temp.rx.Bytes(), bitSizeInBytes)
round.data.S = padToLengthBytesInPlace(sumS.Bytes(), bitSizeInBytes)
round.data.Signature = append(round.data.R, round.data.S...)
round.data.SignatureRecovery = []byte{byte(recid)}
round.data.M = round.temp.m.Bytes()
@@ -91,10 +92,10 @@ func (round *finalization) NextRound() tss.Round {
return nil // finished!
}
func fillTo32BytesInPlace(src []byte) []byte {
func padToLengthBytesInPlace(src []byte, length int) []byte {
oriLen := len(src)
if oriLen < 32 {
for i := 0; i < 32-oriLen; i++ {
if oriLen < length {
for i := 0; i < length-oriLen; i++ {
src = append([]byte{0}, src...)
}
}

View File

@@ -97,7 +97,7 @@ signing:
case <-endCh:
atomic.AddInt32(&ended, 1)
if atomic.LoadInt32(&ended) == int32(len(signPIDs)) {
t.Logf("Done. Received save data from %d participants", ended)
t.Logf("Done. Received signature data from %d participants", ended)
R := parties[0].temp.bigR
r := parties[0].temp.rx
fmt.Printf("sign result: R(%s, %s), r=%s\n", R.X().String(), R.Y().String(), r.String())
@@ -132,7 +132,7 @@ signing:
func TestFillTo32BytesInPlace(t *testing.T) {
s := big.NewInt(123456789)
normalizedS := fillTo32BytesInPlace(s.Bytes())
normalizedS := padToLengthBytesInPlace(s.Bytes(), 32)
assert.True(t, big.NewInt(0).SetBytes(normalizedS).Cmp(s) == 0)
assert.Equal(t, 32, len(normalizedS))
assert.NotEqual(t, 32, len(s.Bytes()))