github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/mpcService/step/generator/lagrange_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/log"
     7  	"github.com/ethereum/go-ethereum/mpcService/crypto"
     8  	"errors"
     9  )
    10  
    11  type lagrangeGenerator struct {
    12  	seed        *big.Int
    13  	message     map[uint64]*big.Int
    14  	result      *big.Int
    15  	resultKey   string
    16  	preValueKey string
    17  }
    18  
    19  func CreateLagrangeGenerator(preValueKey,resultKey string) *lagrangeGenerator {
    20  	return &lagrangeGenerator{message: make(map[uint64]*big.Int), preValueKey: preValueKey,resultKey:resultKey}
    21  }
    22  
    23  func (lag *lagrangeGenerator) Initialize(peers []protocol.PeerInfo, result protocol.MpcResultInterface) error {
    24  	log.Info("lagrangeGenerator.initialize begin")
    25  
    26  	value, err := result.GetValue(lag.preValueKey)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	lag.seed = value.(*big.Int)
    32  
    33  	log.Info("lagrangeGenerator.initialize succeed")
    34  	return nil
    35  }
    36  
    37  func (lag *lagrangeGenerator) CalculateResult() error {
    38  	log.Info("lagrangeGenerator.calculateResult begin")
    39  
    40  	f := []*big.Int{}
    41  	seed := []*big.Int{}
    42  	for key, value := range lag.message {
    43  		f = append(f, value)
    44  		seed = append(seed, new(big.Int).SetUint64(key))
    45  	}
    46  
    47  	lag.result = crypto.Lagrange(f, seed)
    48  	return nil
    49  }
    50  func (lag *lagrangeGenerator)GetMessageData(int)interface{}{
    51  	return lag.seed
    52  }
    53  func (lag *lagrangeGenerator) SetMessageData(seed uint64,value interface{})error{
    54  	_, exist := lag.message[seed]
    55  	if exist {
    56  		log.Error("MpcPoint_Step.HandleMessage, get msg from seed fail.")
    57  		return errors.New("MpcPoint_Step.HandleMessage, get msg from seed fail.")
    58  	}
    59  
    60  	lag.message[seed] = value.(*big.Int)
    61  	return nil
    62  }
    63  func (lag *lagrangeGenerator)GetResultData()*protocol.MpcData{
    64  //	if lag.resultKey == protocol.MpcSignARResult{
    65  //		log.Error("ARVALUE1","ar",lag.result)
    66  //	}
    67  	return &protocol.MpcData{lag.resultKey,lag.result}
    68  }