From 2862aa25d3e2f656650ebcf6241836a8b8cc2447 Mon Sep 17 00:00:00 2001 From: Gustavo Frederico Date: Thu, 27 Jan 2022 16:36:48 -0500 Subject: [PATCH] Minor fixes to unit tests. No functional change to protocols. --- Makefile | 8 ++++---- common/slice.go | 10 ++++++++++ crypto/zkp/mod/mod_test.go | 2 +- ecdsa/signing/local_party_test.go | 14 +++++++------- ecdsa/signing/sign_out.go | 14 ++------------ eddsa/keygen/local_party_test.go | 13 ++++++++----- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index f0acd6ca..2137529a 100644 --- a/Makefile +++ b/Makefile @@ -47,14 +47,14 @@ test_unit_level0: go clean -testcache rm -f ./test/_ecdsa_fixtures/*json rm -f ./test/_eddsa_fixtures/*json - go test -v -timeout 50m $(UT_PACKAGES_LEVEL_0) + go test -timeout 50m $(UT_PACKAGES_LEVEL_0) test_unit: test_unit_level0 @echo "--> Running Unit Tests - Level 1" @echo "!!! WARNING: This will take a long time :)" sleep 2 - go test -v -timeout 60m $(UT_PACKAGES_LEVEL_1) + go test -timeout 60m $(UT_PACKAGES_LEVEL_1) test_unit_race_level0: @echo "--> Running Unit Tests (with Race Detection) - Level 0" @@ -64,13 +64,13 @@ test_unit_race_level0: sleep 10 rm -f ./test/_ecdsa_fixtures/*json rm -f ./test/_eddsa_fixtures/*json - go test -v -timeout 50m -race $(UT_PACKAGES_LEVEL_0) + go test -timeout 50m -race $(UT_PACKAGES_LEVEL_0) test_unit_race: test_unit_race_level0 @echo "--> Running Unit Tests (with Race Detection) - Level 1" @echo "!!! WARNING: This will take a long time :)" sleep 2 - go test -v -timeout 60m -race $(UT_PACKAGES_LEVEL_1) + go test -timeout 60m -race $(UT_PACKAGES_LEVEL_1) test: make test_unit_race diff --git a/common/slice.go b/common/slice.go index f48dc1d4..2162cc8c 100644 --- a/common/slice.go +++ b/common/slice.go @@ -78,3 +78,13 @@ func AnyNonEmptyMultiByte(bzs [][]byte, expectLen ...int) bool { } return false } + +func PadToLengthBytesInPlace(src []byte, length int) []byte { + oriLen := len(src) + if oriLen < length { + for i := 0; i < length-oriLen; i++ { + src = append([]byte{0}, src...) + } + } + return src +} diff --git a/crypto/zkp/mod/mod_test.go b/crypto/zkp/mod/mod_test.go index b2eef112..432c6385 100644 --- a/crypto/zkp/mod/mod_test.go +++ b/crypto/zkp/mod/mod_test.go @@ -17,7 +17,7 @@ import ( ) func TestMod(test *testing.T) { - preParams, err := keygen.GeneratePreParams(time.Minute*10, 8) + preParams, err := keygen.GeneratePreParams(time.Minute*20, 8) assert.NoError(test, err) p, q, N := preParams.P, preParams.Q, preParams.NTildei diff --git a/ecdsa/signing/local_party_test.go b/ecdsa/signing/local_party_test.go index 17fdee27..f6538148 100644 --- a/ecdsa/signing/local_party_test.go +++ b/ecdsa/signing/local_party_test.go @@ -137,8 +137,8 @@ signing: if atomic.LoadInt32(&ended) == int32(len(signPIDs)) { 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()) + // r := parties[0].temp.Rx + // fmt.Printf("sign result: R(%s, %s), r=%s\n", R.X().String(), R.Y().String(), r.String()) modN := common.ModInt(tss.S256().Params().N) @@ -147,7 +147,7 @@ signing: for _, p := range parties { sumS = modN.Add(sumS, p.temp.SigmaShare) } - fmt.Printf("S: %s\n", sumS.String()) + // fmt.Printf("S: %s\n", sumS.String()) // END check s correctness // BEGIN ECDSA verify @@ -246,8 +246,8 @@ signing: if atomic.LoadInt32(&ended) == int32(len(signPIDs)) { 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()) + // r := parties[0].temp.Rx + // fmt.Printf("sign result: R(%s, %s), r=%s\n", R.X().String(), R.Y().String(), r.String()) modN := common.ModInt(tss.S256().Params().N) @@ -256,7 +256,7 @@ signing: for _, p := range parties { sumS = modN.Add(sumS, p.temp.SigmaShare) } - fmt.Printf("S: %s\n", sumS.String()) + // fmt.Printf("S: %s\n", sumS.String()) // END check s correctness // BEGIN ECDSA verify @@ -636,7 +636,7 @@ func TestIdAbortSimulateRound7(test *testing.T) { func TestFillTo32BytesInPlace(t *testing.T) { s := big.NewInt(123456789) - normalizedS := padToLengthBytesInPlace(s.Bytes(), 32) + normalizedS := common.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())) diff --git a/ecdsa/signing/sign_out.go b/ecdsa/signing/sign_out.go index 5d6c2efd..0b640ef6 100644 --- a/ecdsa/signing/sign_out.go +++ b/ecdsa/signing/sign_out.go @@ -74,8 +74,8 @@ func (round *signout) Start() *tss.Error { // save the signature for final output bitSizeInBytes := round.Params().EC().Params().BitSize / 8 - round.data.R = padToLengthBytesInPlace(round.temp.Rx.Bytes(), bitSizeInBytes) - round.data.S = padToLengthBytesInPlace(Sigma.Bytes(), bitSizeInBytes) + round.data.R = common.PadToLengthBytesInPlace(round.temp.Rx.Bytes(), bitSizeInBytes) + round.data.S = common.PadToLengthBytesInPlace(Sigma.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() @@ -109,13 +109,3 @@ func (round *signout) Update() (bool, *tss.Error) { func (round *signout) NextRound() tss.Round { return nil // finished! } - -func padToLengthBytesInPlace(src []byte, length int) []byte { - oriLen := len(src) - if oriLen < length { - for i := 0; i < length-oriLen; i++ { - src = append([]byte{0}, src...) - } - } - return src -} diff --git a/eddsa/keygen/local_party_test.go b/eddsa/keygen/local_party_test.go index 57d2e141..29bce406 100644 --- a/eddsa/keygen/local_party_test.go +++ b/eddsa/keygen/local_party_test.go @@ -121,6 +121,7 @@ keygen: // combine shares for each Pj to get u u := new(big.Int) + modQ := common.ModInt(tss.Edwards().Params().N) for j, Pj := range parties { pShares := make(vss.Shares, 0) for j2, P := range parties { @@ -161,9 +162,8 @@ keygen: assert.NotEqual(t, BigXjX, Pj.temp.vs[0].X()) assert.NotEqual(t, BigXjY, Pj.temp.vs[0].Y()) } - u = new(big.Int).Add(u, uj) + u = modQ.Add(u, uj) } - u = new(big.Int).Mod(u, tss.Edwards().Params().N) scalar := make([]byte, 0, 32) copy(scalar, u.Bytes()) @@ -174,8 +174,10 @@ keygen: X: pkX, Y: pkY, } - println("u len: ", len(u.Bytes())) - sk, _, err := edwards.PrivKeyFromScalar(u.Bytes()) + t.Logf("u len: %v", len(u.Bytes())) + uBytes := common.PadToLengthBytesInPlace(u.Bytes(), edwards.PrivScalarSize) + sk, _, err := edwards.PrivKeyFromScalar(uBytes) + assert.NoError(t, err, "error loading private key") // fmt.Println("err: ", err.Error()) // test pub key, should be on curve and match pkX, pkY @@ -343,6 +345,8 @@ keygen: u = new(big.Int).Add(u, uj) } u = new(big.Int).Mod(u, tss.S256().Params().N) + t.Logf("u len: %v", len(u.Bytes())) + scalar := make([]byte, 0, 32) copy(scalar, u.Bytes()) @@ -353,7 +357,6 @@ keygen: X: pkX, Y: pkY, } - println("u len: ", len(u.Bytes())) sk, _ := btcec.PrivKeyFromBytes(u.Bytes()) // fmt.Println("err: ", err.Error())