Skip to content

Commit

Permalink
Minor fixes to unit tests. No functional change to protocols.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Frederico committed Jan 27, 2022
1 parent 2be88a6 commit 2862aa2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions common/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion crypto/zkp/mod/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions ecdsa/signing/local_party_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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()))
Expand Down
14 changes: 2 additions & 12 deletions ecdsa/signing/sign_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
13 changes: 8 additions & 5 deletions eddsa/keygen/local_party_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())

Expand All @@ -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
Expand Down Expand Up @@ -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())

Expand All @@ -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())

Expand Down

0 comments on commit 2862aa2

Please sign in to comment.