~edwargix/tallyard

8fe402221452b336ec1ac8a15bae59e9b26c0f3e — David Florness 5 years ago 9879527
Store ballot in LocalVoter
2 files changed, 17 insertions(+), 11 deletions(-)

M poly.go
M voter.go
M poly.go => poly.go +9 -5
@@ 24,12 24,16 @@ func RandomBigInt(numBytes uint, allowAllZeros bool) (*big.Int, error) {
	return bi, nil
}

func NewPoly(coefficients []*big.Int, ballot []byte) *Poly {
	constant := new(big.Int).SetBytes(ballot)
	return &Poly{constant, coefficients}
}

// creates a random polynomial of the given degree, uses at least `entropy' bits
// of entropy for the random coefficients, and assigns the ballot to the
// constant term
func NewRandomPoly(degree uint, entropy uint, ballot []byte) *Poly {
	constant := new(big.Int).SetBytes(ballot)
	p := &Poly{constant, make([]*big.Int, degree)}
	coefficients := make([]*big.Int, degree)

	// number of bits per coefficient
	numBits := uint(math.Ceil(float64(entropy) / float64(degree)))


@@ 41,14 45,14 @@ func NewRandomPoly(degree uint, entropy uint, ballot []byte) *Poly {
	}

	var err error
	for i := range p.coefs {
		p.coefs[i], err = RandomBigInt(numBytes, false)
	for i := range coefficients {
		coefficients[i], err = RandomBigInt(numBytes, false)
		if err != nil {
			panic(err)
		}
	}

	return p
	return NewPoly(coefficients, ballot)
}

func (p *Poly) Eval(input *big.Int) *big.Int {

M voter.go => voter.go +8 -6
@@ 33,9 33,10 @@ type Voter struct {
type LocalVoter struct {
	Voter
	host.Host
	ctx  context.Context
	kdht *dht.IpfsDHT
	poly *Poly
	ctx    context.Context
	ballot []byte
	kdht   *dht.IpfsDHT
	poly   *Poly

	// mutexs only used for atomicity; atomicity.Value sucks because we lose
	// type safety with interface{}


@@ 282,12 283,13 @@ func (election *Election) StartVoting() {
	}
	logger.Infof("our input: %s", localVoter.input)

	ballot := vote(election.Candidates)
	logger.Infof("our ballot: %v", ballot)
	localVoter.ballot = vote(election.Candidates)
	logger.Infof("our ballot: %v", localVoter.ballot)

	// no +1 since we want degree k-1 where k is total number of voters
	localVoter.polyMu.Lock()
	localVoter.poly = NewRandomPoly(uint(len(election.remoteVoters)), 1024, ballot)
	localVoter.poly = NewRandomPoly(uint(len(election.remoteVoters)),
		1024, localVoter.ballot)
	localVoter.polyMu.Unlock()
	logger.Infof("our constant: %s", localVoter.poly.constant)