From 8fe402221452b336ec1ac8a15bae59e9b26c0f3e Mon Sep 17 00:00:00 2001 From: David Florness Date: Sat, 14 Nov 2020 13:17:17 -0500 Subject: [PATCH] Store ballot in LocalVoter --- poly.go | 14 +++++++++----- voter.go | 14 ++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/poly.go b/poly.go index c05505a..d1647ad 100644 --- a/poly.go +++ b/poly.go @@ -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 { diff --git a/voter.go b/voter.go index 35f3fe1..73a01ea 100644 --- a/voter.go +++ b/voter.go @@ -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) -- 2.38.4