@@ 17,8 17,7 @@ func RandomBigInt(numBytes uint, allowAllZeros bool) (*big.Int, error) {
if err != nil {
return nil, err
}
- bi := big.Int{}
- bi.SetBytes(randBytes)
+ bi := new(big.Int).SetBytes(randBytes)
if bi.Cmp(big.NewInt(0)) == 0 {
return RandomBigInt(numBytes, allowAllZeros)
}
@@ 29,8 28,7 @@ func RandomBigInt(numBytes uint, allowAllZeros bool) (*big.Int, error) {
// of entropy for the random coefficients, and assigns the ballot to the
// constant term
func NewRandomPoly(degree uint, entropy uint, ballot []byte) *Poly {
- constant := &big.Int{}
- constant.SetBytes(ballot)
+ constant := new(big.Int).SetBytes(ballot)
p := &Poly{constant, make([]*big.Int, degree)}
// number of bits per coefficient
@@ 54,13 52,11 @@ func NewRandomPoly(degree uint, entropy uint, ballot []byte) *Poly {
}
func (p *Poly) Eval(input *big.Int) *big.Int {
- res := &big.Int{}
- res.Set(p.constant)
+ res := new(big.Int).Set(p.constant)
for i, coef := range p.coefs {
degree := big.NewInt(int64(i + 1))
- term := &big.Int{}
- term.Exp(input, degree, nil)
+ term := new(big.Int).Exp(input, degree, nil)
term.Mul(term, coef)
res.Add(res, term)
}
@@ 130,9 130,15 @@ func handleCmd(cmd string, rw *bufio.ReadWriter, stream network.Stream) {
stream.Conn().RemotePeer())
return
}
- input := &big.Int{}
- input.SetBytes(inputBytes)
- output := me.poly.Eval(input)
+ peer, exists := election.remoteVoters[stream.Conn().RemotePeer()]
+ if !exists {
+ logger.Warning("receiving eval command from unrecognized peer")
+ return
+ }
+ peer.Lock()
+ peer.input = new(big.Int).SetBytes(inputBytes)
+ peer.Unlock()
+ output := me.poly.Eval(peer.input)
rw.WriteString(base58.Encode(output.Bytes()))
rw.Flush()
case "sum":
@@ 257,9 263,7 @@ retry:
printErr(err, "couldn't base58-decode contents from stream")
goto retry
}
- bi := &big.Int{}
- bi.SetBytes(retBytes)
- return bi
+ return new(big.Int).SetBytes(retBytes)
}
func startVoting() {