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

     1  package generator
     2  
     3  import (
     4  	"math/big"
     5  	"github.com/ethereum/go-ethereum/mpcService/protocol"
     6  	"github.com/ethereum/go-ethereum/mpcService/crypto"
     7  	"github.com/ethereum/go-ethereum/log"
     8  	"errors"
     9  )
    10  
    11  type RandomPolynomialValue struct {
    12  	RandCoefficient []*big.Int          //coefficient
    13  	message         map[uint64]big.Int //Polynomil result
    14  	PolyValue       []*big.Int
    15  	Result          *big.Int
    16  	resultKey       string
    17  	bJRSS           bool
    18  }
    19  
    20  func CreateJRSSValue(degree int, peerNum int,resultKey string) *RandomPolynomialValue {
    21  	return &RandomPolynomialValue{make([]*big.Int, degree+1), make(map[uint64]big.Int),
    22  	make([]*big.Int, peerNum), nil, resultKey,true}
    23  }
    24  
    25  func CreateJZSSValue(degree int, peerNum int,resultKey string) *RandomPolynomialValue {
    26  	return &RandomPolynomialValue{make([]*big.Int, degree+1), make(map[uint64]big.Int),
    27  	make([]*big.Int, peerNum), nil, resultKey,false}
    28  }
    29  
    30  func (poly *RandomPolynomialValue) Initialize(peers []protocol.PeerInfo, result protocol.MpcResultInterface) error {
    31  	cof, err := crypto.GetRandCoefficients(len(poly.RandCoefficient))
    32  	if err != nil {
    33  		log.Error("RandomPolynomialValue, GetRandCoefficients fail. err:%s", err.Error())
    34  		return err
    35  	}
    36  
    37  	copy(poly.RandCoefficient, cof)
    38  	if !poly.bJRSS {
    39  		poly.RandCoefficient[0] = big.NewInt(0)
    40  	}
    41  
    42  	for i := 0; i < len(poly.PolyValue); i++ {
    43  		poly.PolyValue[i] = crypto.EvaluatePoly(poly.RandCoefficient, new(big.Int).SetUint64(uint64(peers[i].Seed)))
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func (poly *RandomPolynomialValue) CalculateResult() error {
    50  	poly.Result = big.NewInt(0)
    51  	for _, value := range poly.message {
    52  		poly.Result.Add(poly.Result, &value)
    53  		poly.Result.Mod(poly.Result, crypto.Secp256k1N)
    54  	}
    55  
    56  	return nil
    57  }
    58  func (poly *RandomPolynomialValue)GetMessageData(i int)interface{} {
    59  	return poly.PolyValue[i]
    60  }
    61  func (poly *RandomPolynomialValue) SetMessageData(seed uint64,value interface{})error{
    62  	_, exist := poly.message[seed]
    63  	if exist {
    64  		log.Error("MpcPoint_Step.HandleMessage, get msg from seed fail.")
    65  		return errors.New("MpcPoint_Step.HandleMessage, get msg from seed fail.")
    66  	}
    67  
    68  	poly.message[seed] = *(value.(*big.Int))
    69  	return nil
    70  }
    71  func (poly *RandomPolynomialValue)GetResultData()*protocol.MpcData {
    72  	return &protocol.MpcData{poly.resultKey,poly.Result}
    73  }