Skip to content

Commit

Permalink
chore: upgrade go-tss to tss-lib v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Aug 23, 2024
1 parent 024e94a commit fd192f6
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cmd/zetaclientd/gen_pre_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"time"

"github.com/bnb-chain/tss-lib/ecdsa/keygen"
"github.com/bnb-chain/tss-lib/v2/ecdsa/keygen"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"time"

ecdsakeygen "github.com/bnb-chain/tss-lib/ecdsa/keygen"
ecdsakeygen "github.com/bnb-chain/tss-lib/v2/ecdsa/keygen"
"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/types"
Expand Down
45 changes: 45 additions & 0 deletions cmd/zetaclientd/pre_params_upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © 2019 Binance, threshold-network
//
// This file is part of Binance. The full Binance copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
//
// derived from https://github.com/threshold-network/tss-lib/blob/2e712689cfbeefede15f95a0ec7112227d86f702/crypto/paillier/paillier.go#L296

package main

import (
"math/big"

"github.com/bnb-chain/tss-lib/v2/ecdsa/keygen"
)

func isPreParamsUpgradable(preParams *keygen.LocalPreParams) bool {
return preParams.PaillierSK.P == nil
}

// upgradePreParams derives the missing PaillierSK parameters from the existing ones
func upgradePreParams(preParams *keygen.LocalPreParams) {
n := preParams.PaillierSK.PublicKey.N
phiN := preParams.PaillierSK.PhiN

m := new(big.Int).Sub(n, phiN) // pq - (p-1)(q-1) = p + q - 1
m.Add(m, big.NewInt(1)) // (p + q - 1) + 1 = p + q
m.Div(m, big.NewInt(2)) // (p + q) / 2

m2 := new(big.Int).Mul(m, m) // (p + q)^2 / 4
// = (pp + qq + 2pq) / 4
m2subN := new(big.Int).Sub(m2, n) // ((p + q)^2 / 4) - n = ((p + q)^2 / 4) - pq
// = (pp + qq + 2pq) / 4 - pq
// = (pp + qq + 2pq - 4pq) / 4
// = (pp - 2pq + qq) / 4
// = (p - q)^2 / 4
s := new(big.Int).Sqrt(m2subN) // = sqrt((p - q)^2 / 4)
// = |p - q| / 2

p := new(big.Int).Add(m, s) // (p + q) / 2 + |p - q| / 2, assuming p >= q
q := new(big.Int).Sub(m, s) // (p + q) / 2 - |p - q| / 2, assuming p >= q

preParams.PaillierSK.P = p
preParams.PaillierSK.Q = q
}
41 changes: 23 additions & 18 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,24 +392,29 @@ func initPeers(peer string) ([]maddr.Multiaddr, error) {
}

func initPreParams(path string) {
if path != "" {
path = filepath.Clean(path)
log.Info().Msgf("pre-params file path %s", path)
preParamsFile, err := os.Open(path)
if err != nil {
log.Error().Err(err).Msg("open pre-params file failed; skip")
} else {
bz, err := io.ReadAll(preParamsFile)
if err != nil {
log.Error().Err(err).Msg("read pre-params file failed; skip")
} else {
err = json.Unmarshal(bz, &preParams)
if err != nil {
log.Error().Err(err).Msg("unmarshal pre-params file failed; skip and generate new one")
preParams = nil // skip reading pre-params; generate new one instead
}
}
}
if path == "" {
return
}
path = filepath.Clean(path)
log.Info().Msgf("pre-params file path %s", path)
preParamsFile, err := os.Open(path)
if err != nil {
log.Error().Err(err).Msg("open pre-params file failed; skip")
return
}
bz, err := io.ReadAll(preParamsFile)
if err != nil {
log.Error().Err(err).Msg("read pre-params file failed; skip")
return
}
err = json.Unmarshal(bz, &preParams)
if err != nil {
log.Error().Err(err).Msg("unmarshal pre-params file failed; skip and generate new one")
preParams = nil // skip reading pre-params; generate new one instead
return
}
if isPreParamsUpgradable(preParams) {
upgradePreParams(preParams)
}
}

Expand Down
13 changes: 6 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
cosmossdk.io/math v1.3.0
github.com/99designs/keyring v1.2.1
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcec/v2 v2.3.3
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
github.com/emicklei/proto v1.11.1
github.com/evmos/ethermint v0.22.0
Expand All @@ -56,7 +56,7 @@ require (
require (
cosmossdk.io/simapp v0.0.0-20230608160436-666c345ad23d
cosmossdk.io/tools/rosetta v0.2.1
github.com/bnb-chain/tss-lib v1.3.5
github.com/bnb-chain/tss-lib/v2 v2.0.2
github.com/btcsuite/btcd/btcutil v1.1.5
github.com/cockroachdb/errors v1.11.1
github.com/cometbft/cometbft v0.37.4
Expand Down Expand Up @@ -89,7 +89,7 @@ require (
github.com/cosmos/rosetta-sdk-go v0.10.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.0 // indirect
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.3 // indirect
github.com/dgraph-io/badger/v4 v4.2.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand All @@ -113,7 +113,6 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/zstdpool-freelist v0.0.0-20201229113212-927304c0c3b1 // indirect
github.com/onsi/gomega v1.27.7 // indirect
github.com/otiai10/mint v1.3.2 // indirect
github.com/prometheus/tsdb v0.7.1 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down Expand Up @@ -289,7 +288,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/otiai10/primes v0.0.0-20180210170552-f6d2a1ba97c4 // indirect
github.com/otiai10/primes v0.0.0-20210501021515-f1b2be525a11 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
Expand Down Expand Up @@ -362,6 +361,6 @@ replace github.com/evmos/ethermint => github.com/zeta-chain/ethermint v0.0.0-202

replace github.com/libp2p/go-libp2p => github.com/zeta-chain/go-libp2p v0.0.0-20240710192637-567fbaacc2b4

replace gitlab.com/thorchain/tss/go-tss => github.com/zeta-chain/go-tss v0.0.0-20240819223347-fce649aca224
replace gitlab.com/thorchain/tss/go-tss => github.com/zeta-chain/go-tss v0.0.0-20240821171408-85ab1a3d8129

replace github.com/bnb-chain/tss-lib => github.com/zeta-chain/tss-lib v0.0.0-20240819214903-774ccaf59fca
replace github.com/bnb-chain/tss-lib/v2 => github.com/zeta-chain/tss-lib/v2 v2.0.0-20240821171246-2e65a5558b5a
Loading

0 comments on commit fd192f6

Please sign in to comment.