~edwargix/tallyard

47e4d26e698aff51a3963ac498483a21a5adc9c8 — David Florness 5 years ago e18fa4e
Simplify big int code and keep track of a voter's input after eval
2 files changed, 14 insertions(+), 14 deletions(-)

M poly.go
M voter.go
M poly.go => poly.go +4 -8
@@ 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)
	}

M voter.go => voter.go +10 -6
@@ 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() {