From 47e4d26e698aff51a3963ac498483a21a5adc9c8 Mon Sep 17 00:00:00 2001 From: David Florness Date: Mon, 8 Jun 2020 18:34:01 -0600 Subject: [PATCH] Simplify big int code and keep track of a voter's input after eval --- poly.go | 12 ++++-------- voter.go | 16 ++++++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/poly.go b/poly.go index 559a1e3..855072c 100644 --- a/poly.go +++ b/poly.go @@ -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) } diff --git a/voter.go b/voter.go index aedf05e..1dbe809 100644 --- a/voter.go +++ b/voter.go @@ -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() { -- 2.38.4