mirror of
https://github.com/SwingbyProtocol/tss-lib.git
synced 2026-01-11 06:48:22 -05:00
Merge branch 'master' into feature/cggmp21oct-tss-lib-tmp-merge # Conflicts: # .github/workflows/test.yml # Makefile # README.md # crypto/ckd/child_key_derivation.go # crypto/ecpoint.go # crypto/mta/proofs.go # crypto/mta/range_proof.go # crypto/mta/share_protocol.go # crypto/mta/share_protocol_test.go # crypto/paillier/paillier.go # crypto/vss/feldman_vss.go # ecdsa/keygen/ecdsa-keygen.pb.go # ecdsa/keygen/local_party.go # ecdsa/keygen/local_party_test.go # ecdsa/keygen/messages.go # ecdsa/keygen/prepare.go # ecdsa/keygen/round_1.go # ecdsa/keygen/round_2.go # ecdsa/keygen/round_3.go # ecdsa/keygen/round_4.go # ecdsa/keygen/save_data.go # ecdsa/resharing/ecdsa-resharing.pb.go # ecdsa/resharing/local_party_test.go # ecdsa/resharing/messages.go # ecdsa/resharing/round_1_old_step_1.go # ecdsa/resharing/round_4_new_step_2.go # ecdsa/signing/ecdsa-signing.pb.go # ecdsa/signing/finalize.go # ecdsa/signing/key_derivation_util.go # ecdsa/signing/local_party.go # ecdsa/signing/local_party_test.go # ecdsa/signing/messages.go # ecdsa/signing/prepare.go # ecdsa/signing/round_1.go # ecdsa/signing/round_2.go # ecdsa/signing/round_3.go # ecdsa/signing/round_4.go # ecdsa/signing/round_5.go # ecdsa/signing/round_6.go # ecdsa/signing/round_7.go # ecdsa/signing/rounds.go # eddsa/keygen/eddsa-keygen.pb.go # eddsa/keygen/local_party.go # eddsa/keygen/local_party_test.go # eddsa/keygen/messages.go # eddsa/keygen/round_1.go # eddsa/keygen/round_2.go # eddsa/keygen/round_3.go # eddsa/keygen/save_data.go # eddsa/keygen/test_utils.go # eddsa/resharing/eddsa-resharing.pb.go # eddsa/resharing/local_party.go # eddsa/resharing/local_party_test.go # eddsa/resharing/messages.go # eddsa/resharing/round_1_old_step_1.go # eddsa/resharing/round_4_new_step_2.go # eddsa/signing/eddsa-signing.pb.go # eddsa/signing/finalize.go # eddsa/signing/local_party.go # eddsa/signing/local_party_test.go # eddsa/signing/messages.go # eddsa/signing/prepare.go # eddsa/signing/round_1.go # eddsa/signing/round_2.go # eddsa/signing/round_3.go # eddsa/signing/rounds.go # eddsa/signing/utils.go # go.mod # go.sum # protob/ecdsa-keygen.proto # protob/ecdsa-resharing.proto # protob/ecdsa-signing.proto # protob/eddsa-keygen.proto # protob/eddsa-resharing.proto # protob/eddsa-signing.proto # protob/message.proto # protob/signature.proto # test/_ecdsa_fixtures/keygen_data_0.json # test/_ecdsa_fixtures/keygen_data_1.json # test/_ecdsa_fixtures/keygen_data_2.json # test/_ecdsa_fixtures/keygen_data_3.json # test/_ecdsa_fixtures/keygen_data_4.json # test/_eddsa_fixtures/keygen_data_0.json # test/_eddsa_fixtures/keygen_data_1.json # test/_eddsa_fixtures/keygen_data_2.json # test/_eddsa_fixtures/keygen_data_3.json # test/_eddsa_fixtures/keygen_data_4.json # test/config.go # tss/message.pb.go # tss/params.go # tss/party.go # tss/wire.go
103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
// Copyright © 2021 Swingby
|
|
|
|
package ecdsautils
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"encoding/json"
|
|
"math/big"
|
|
|
|
"github.com/binance-chain/tss-lib/common"
|
|
"github.com/binance-chain/tss-lib/crypto/paillier"
|
|
"github.com/binance-chain/tss-lib/crypto/vss"
|
|
"github.com/binance-chain/tss-lib/tss"
|
|
)
|
|
|
|
type ECDSASignature struct {
|
|
R, S *big.Int
|
|
}
|
|
|
|
type AbortTrigger int
|
|
|
|
func HashShare(share *vss.Share) (hash []byte) {
|
|
hash = append(share.ID.Bytes(), share.Share.Bytes()...)
|
|
hash = append(hash, big.NewInt(int64(share.Threshold)).Bytes()...)
|
|
hash = common.SHA512_256(hash)
|
|
return
|
|
}
|
|
|
|
func NewECDSASignature(r, s *big.Int) *ECDSASignature {
|
|
return &ECDSASignature{R: r, S: s}
|
|
}
|
|
|
|
func HashPaillierKey(pk *paillier.PublicKey) (hash []byte) {
|
|
hash = common.SHA512_256i(pk.AsInts()...).Bytes()
|
|
return
|
|
}
|
|
|
|
func (k MarshallableEcdsaPrivateKey) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(struct {
|
|
PublicKey MarshallableEcdsaPublicKey
|
|
D *big.Int
|
|
}{
|
|
PublicKey: (MarshallableEcdsaPublicKey)(k.PublicKey),
|
|
D: k.D,
|
|
})
|
|
}
|
|
|
|
func (k *MarshallableEcdsaPrivateKey) UnmarshalJSON(b []byte) error {
|
|
// PrivateKey represents an ECDSA private key.
|
|
newKey := new(struct {
|
|
PublicKey MarshallableEcdsaPublicKey
|
|
D *big.Int
|
|
})
|
|
if err := json.Unmarshal(b, &newKey); err != nil {
|
|
return err
|
|
}
|
|
k.D = newKey.D
|
|
k.PublicKey = (ecdsa.PublicKey)(newKey.PublicKey)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (k MarshallableEcdsaPublicKey) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(struct {
|
|
X, Y *big.Int
|
|
}{
|
|
X: k.X,
|
|
Y: k.Y,
|
|
})
|
|
}
|
|
|
|
func (k *MarshallableEcdsaPublicKey) UnmarshalJSON(b []byte) error {
|
|
newKey := new(struct {
|
|
X, Y *big.Int
|
|
})
|
|
if err := json.Unmarshal(b, &newKey); err != nil {
|
|
return err
|
|
}
|
|
k.X = newKey.X
|
|
k.Y = newKey.Y
|
|
k.Curve = tss.EC()
|
|
|
|
return nil
|
|
}
|
|
|
|
// We will customize the Json serialization of the public key
|
|
// used for party authentication.
|
|
// The serialization of the Koblitz curve showed problems,
|
|
// as the type does not expose a number of attributes.
|
|
type MarshallableEcdsaPublicKey ecdsa.PublicKey
|
|
|
|
type MarshallableEcdsaPrivateKey ecdsa.PrivateKey
|
|
|
|
func ProofNSquareFree(NTildei *big.Int, p *big.Int, q *big.Int) (*big.Int, *big.Int) {
|
|
randIntProofNSquareFreei := common.GetRandomPositiveInt(NTildei)
|
|
|
|
// Using Euler's totient function: phi(N)=phi(P)(Q)=(P-1)(Q-1)=2p2q
|
|
phiNTildei := new(big.Int).Mul(new(big.Int).Mul(big.NewInt(4), p), q)
|
|
bigM := new(big.Int).ModInverse(NTildei, phiNTildei)
|
|
proofNSquareFree := common.ModInt(NTildei).Exp(randIntProofNSquareFreei, bigM)
|
|
return randIntProofNSquareFreei, proofNSquareFree
|
|
}
|