mirror of
https://github.com/saymrwulf/swisspost-evoting-go-poc.git
synced 2026-09-04 20:23:55 +00:00
29 lines
596 B
Go
29 lines
596 B
Go
|
|
package returncodes
|
||
|
|
|
||
|
|
import (
|
||
|
|
"math/big"
|
||
|
|
)
|
||
|
|
|
||
|
|
// DecodeVote factorizes a vote product back into the indices of selected options.
|
||
|
|
func DecodeVote(product *big.Int, primes []*big.Int) []int {
|
||
|
|
remaining := new(big.Int).Set(product)
|
||
|
|
var selected []int
|
||
|
|
|
||
|
|
for idx, p := range primes {
|
||
|
|
for {
|
||
|
|
quo, rem := new(big.Int).DivMod(remaining, p, new(big.Int))
|
||
|
|
if rem.Sign() == 0 {
|
||
|
|
selected = append(selected, idx)
|
||
|
|
remaining = quo
|
||
|
|
} else {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if remaining.Cmp(big.NewInt(1)) != 0 {
|
||
|
|
panic("factorization failed: remaining = " + remaining.String())
|
||
|
|
}
|
||
|
|
return selected
|
||
|
|
}
|