|
| 1 | +package mpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ecdsa" |
| 5 | + "crypto/elliptic" |
| 6 | + "crypto/rand" |
| 7 | + "fmt" |
| 8 | + "math/big" |
| 9 | + |
| 10 | + "github.com/bnb-chain/tss-lib/v2/common" |
| 11 | + "github.com/bnb-chain/tss-lib/v2/crypto" |
| 12 | + "github.com/bnb-chain/tss-lib/v2/crypto/ckd" |
| 13 | + "github.com/bnb-chain/tss-lib/v2/ecdsa/keygen" |
| 14 | + "github.com/fystack/mpcium/pkg/infra" |
| 15 | + "github.com/fystack/mpcium/pkg/logger" |
| 16 | + "github.com/hashicorp/consul/api" |
| 17 | + |
| 18 | + "github.com/btcsuite/btcd/chaincfg" |
| 19 | +) |
| 20 | + |
| 21 | +// Child Key Derivation |
| 22 | +type CKD struct { |
| 23 | + Store infra.ConsulKV |
| 24 | + ChainCode []byte |
| 25 | + Path []uint32 |
| 26 | +} |
| 27 | + |
| 28 | +func NewCKD() *CKD { |
| 29 | + ckd := &CKD{ |
| 30 | + Store: infra.GetConsulClient("development").KV(), |
| 31 | + } |
| 32 | + ckd.initializeChainCode() |
| 33 | + return ckd |
| 34 | +} |
| 35 | + |
| 36 | +func (c *CKD) UpdateSinglePublicKeyAndAdjustBigXj( |
| 37 | + keyDerivationDelta *big.Int, |
| 38 | + key *keygen.LocalPartySaveData, |
| 39 | + extendedChildPk *ecdsa.PublicKey, |
| 40 | + ec elliptic.Curve, |
| 41 | +) error { |
| 42 | + var err error |
| 43 | + |
| 44 | + // Compute g^delta |
| 45 | + gDelta := crypto.ScalarBaseMult(ec, keyDerivationDelta) |
| 46 | + |
| 47 | + // Update the public key |
| 48 | + key.ECDSAPub, err = crypto.NewECPoint(ec, extendedChildPk.X, extendedChildPk.Y) |
| 49 | + if err != nil { |
| 50 | + common.Logger.Errorf("error creating new extended child public key") |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Update each BigXj[i] := BigXj[i] + g^delta |
| 55 | + for j := range key.BigXj { |
| 56 | + key.BigXj[j], err = key.BigXj[j].Add(gDelta) |
| 57 | + if err != nil { |
| 58 | + common.Logger.Errorf("error in delta operation") |
| 59 | + return err |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (c *CKD) Derive(masterPub *crypto.ECPoint, path []uint32, curve elliptic.Curve) (*big.Int, *ckd.ExtendedKey, error) { |
| 67 | + return c.derivingPubkeyFromPath(masterPub, c.ChainCode, path, curve) |
| 68 | +} |
| 69 | + |
| 70 | +func (c *CKD) derivingPubkeyFromPath(masterPub *crypto.ECPoint, chainCode []byte, path []uint32, ec elliptic.Curve) (*big.Int, *ckd.ExtendedKey, error) { |
| 71 | + // build ecdsa key pair |
| 72 | + pk := ecdsa.PublicKey{ |
| 73 | + Curve: ec, |
| 74 | + X: masterPub.X(), |
| 75 | + Y: masterPub.Y(), |
| 76 | + } |
| 77 | + |
| 78 | + net := &chaincfg.MainNetParams |
| 79 | + extendedParentPk := &ckd.ExtendedKey{ |
| 80 | + PublicKey: pk, |
| 81 | + Depth: 0, |
| 82 | + ChildIndex: 0, |
| 83 | + ChainCode: chainCode[:], |
| 84 | + ParentFP: []byte{0x00, 0x00, 0x00, 0x00}, |
| 85 | + Version: net.HDPrivateKeyID[:], |
| 86 | + } |
| 87 | + |
| 88 | + return ckd.DeriveChildKeyFromHierarchy(path, extendedParentPk, ec.Params().N, ec) |
| 89 | +} |
| 90 | + |
| 91 | +func (c *CKD) initializeChainCode() error { |
| 92 | + logger.Info("Initializing chain code") |
| 93 | + |
| 94 | + // Try to get chain code from store |
| 95 | + val, _, err := c.Store.Get("chain_code", nil) |
| 96 | + if err == nil && val != nil && len(val.Value) == 32 { |
| 97 | + // Found existing chain code |
| 98 | + c.ChainCode = make([]byte, 32) |
| 99 | + copy(c.ChainCode, val.Value) |
| 100 | + logger.Info("Loaded existing chain code", "chainCode", c.ChainCode) |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + // Not found or invalid: generate new chain code |
| 105 | + chainCode := make([]byte, 32) |
| 106 | + max := new(big.Int).Lsh(big.NewInt(1), 256) |
| 107 | + max.Sub(max, big.NewInt(1)) |
| 108 | + fillBytes(common.GetRandomPositiveInt(rand.Reader, max), chainCode) |
| 109 | + |
| 110 | + // Save to store |
| 111 | + _, err = c.Store.Put(&api.KVPair{Key: "chain_code", Value: chainCode}, nil) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to store chain code: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Assign to CKD struct |
| 117 | + c.ChainCode = make([]byte, 32) |
| 118 | + copy(c.ChainCode, chainCode) |
| 119 | + logger.Info("Generated new chain code", "chainCode", c.ChainCode) |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func fillBytes(x *big.Int, buf []byte) []byte { |
| 124 | + b := x.Bytes() |
| 125 | + if len(b) > len(buf) { |
| 126 | + panic("buffer too small") |
| 127 | + } |
| 128 | + offset := len(buf) - len(b) |
| 129 | + for i := range buf { |
| 130 | + if i < offset { |
| 131 | + buf[i] = 0 |
| 132 | + } else { |
| 133 | + buf[i] = b[i-offset] |
| 134 | + } |
| 135 | + } |
| 136 | + return buf |
| 137 | +} |
0 commit comments