github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/mpcService/step/generator/mpc_point_generator.go (about)

     1  package generator
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"math/big"
     6  	"github.com/ethereum/go-ethereum/mpcService/protocol"
     7  	"github.com/ethereum/go-ethereum/crypto"
     8  	"github.com/ethereum/go-ethereum/log"
     9  	mpcCrypto "github.com/ethereum/go-ethereum/mpcService/crypto"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type mpcPointGenerator struct {
    14  	seed        [2]*big.Int
    15  	message     map[uint64][2]*big.Int
    16  	result      [2]*big.Int
    17  	preValueKey string
    18  	resultKey   string
    19  }
    20  
    21  func CreatePointGenerator(preValueKey string,resultKey string) *mpcPointGenerator {
    22  	return &mpcPointGenerator{message: make(map[uint64][2]*big.Int), preValueKey: preValueKey,resultKey:resultKey}
    23  }
    24  
    25  func (point *mpcPointGenerator) Initialize(peers []protocol.PeerInfo, result protocol.MpcResultInterface) error {
    26  	log.Info("mpcPointGenerator.initialize begin")
    27  
    28  	value, err := result.GetValue(point.preValueKey)
    29  	if err != nil {
    30  		log.Error("mpcPointGenerator.initialize get preValueKey fail")
    31  		return err
    32  	}
    33  	private := value.(*big.Int)
    34  	curve := crypto.S256()
    35  	x, y := curve.ScalarBaseMult(private.Bytes())
    36  	if x == nil || y == nil {
    37  		log.Error("mpcPointGenerator.ScalarBaseMult fail. err:%s", protocol.ErrPointZero.Error())
    38  		return protocol.ErrPointZero
    39  	}
    40  
    41  	point.seed[0],point.seed[1] = x, y
    42  
    43  	log.Info("mpcPointGenerator.initialize succeed")
    44  	return nil
    45  }
    46  
    47  func (point *mpcPointGenerator) CalculateResult() error {
    48  	log.Info("mpcPointGenerator.calculateResult begin")
    49  
    50  	result := new(ecdsa.PublicKey)
    51  	result.Curve = crypto.S256()
    52  	i := 0
    53  	for _, value := range point.message {
    54  		if i == 0 {
    55  			result.X = new(big.Int).Set(value[0])
    56  			result.Y = new(big.Int).Set(value[1])
    57  			i++
    58  		} else {
    59  			result.X, result.Y = crypto.S256().Add(result.X, result.Y, value[0], value[1])
    60  		}
    61  	}
    62  
    63  	if !mpcCrypto.ValidatePublicKey(result) {
    64  		log.Error("mpcPointGenerator.ValidatePublicKey fail.","error", protocol.ErrPointZero)
    65  		for _, value := range point.message {
    66  			log.Error("mpcPointGenerator.ValidatePublicKey fail.","X",value[0],"Y",value[1])
    67  		}
    68  		return protocol.ErrPointZero
    69  	}
    70  
    71  	point.result[0],point.result[1] = result.X, result.Y
    72  
    73  	log.Info("mpcPointGenerator.calculateResult succeed")
    74  	return nil
    75  }
    76  func (point *mpcPointGenerator) GetMessageData(int)interface{}{
    77  	return point.seed
    78  }
    79  func (point *mpcPointGenerator) SetMessageData(seed uint64,value interface{})error{
    80  	_, exist := point.message[seed]
    81  	if exist {
    82  		log.Error("MpcPoint_Step.HandleMessage, get msg from seed fail.")
    83  		return errors.New("MpcPoint_Step.HandleMessage, get msg from seed fail.")
    84  	}
    85  
    86  	point.message[seed] = value.([2]*big.Int)
    87  	return nil
    88  }
    89  func (point *mpcPointGenerator) GetResultData()*protocol.MpcData{
    90  	return &protocol.MpcData{point.resultKey,point.result}
    91  }