Skip to content

Commit

Permalink
all: use slices.Backward from Go 1.23
Browse files Browse the repository at this point in the history
And simplify the surrounding code slightly.
  • Loading branch information
mvdan authored and altergui committed Sep 17, 2024
1 parent eb711f8 commit a8a0a05
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
11 changes: 6 additions & 5 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math"
"math/big"
"reflect"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -206,13 +207,13 @@ func encodeEVMResultsArgs(electionId common.Hash, organizationId common.Address,

// decryptVotePackage decrypts a vote package using the given private keys and indexes.
func decryptVotePackage(vp []byte, privKeys []string, indexes []uint32) ([]byte, error) {
for i := len(indexes) - 1; i >= 0; i-- {
if indexes[i] >= uint32(len(privKeys)) {
return nil, fmt.Errorf("invalid key index %d", indexes[i])
for _, index := range slices.Backward(indexes) {
if index >= uint32(len(privKeys)) {
return nil, fmt.Errorf("invalid key index %d", index)
}
priv, err := nacl.DecodePrivate(privKeys[indexes[i]])
priv, err := nacl.DecodePrivate(privKeys[index])
if err != nil {
return nil, fmt.Errorf("cannot decode encryption key with index %d: (%s)", indexes[i], err)
return nil, fmt.Errorf("cannot decode encryption key with index %d: (%s)", index, err)
}
vp, err = priv.Decrypt(vp)
if err != nil {
Expand Down
21 changes: 8 additions & 13 deletions tree/arbo/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"fmt"
"math"
"slices"

"go.vocdoni.io/dvote/db"
)
Expand Down Expand Up @@ -173,21 +174,15 @@ func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool,
}

path := getPath(len(siblings), keyPath)
for i := len(siblings) - 1; i >= 0; i-- {
for i, sibling := range slices.Backward(siblings) {
if path[i] {
key, _, err = newIntermediate(hashFunc, siblings[i], key)
if err != nil {
return false, err
}
key, _, err = newIntermediate(hashFunc, sibling, key)
} else {
key, _, err = newIntermediate(hashFunc, key, siblings[i])
if err != nil {
return false, err
}
key, _, err = newIntermediate(hashFunc, key, sibling)
}
if err != nil {
return false, err
}
}
if bytes.Equal(key, root) {
return true, nil
}
return false, nil
return bytes.Equal(key, root), nil
}
5 changes: 3 additions & 2 deletions vochain/indexer/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"math/big"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -138,8 +139,8 @@ func unmarshalVote(VotePackage []byte, keys []string) (*state.VotePackage, error
// if encryption keys, decrypt the vote
if len(keys) > 0 {
rawVote = bytes.Clone(VotePackage)
for i := len(keys) - 1; i >= 0; i-- {
priv, err := nacl.DecodePrivate(keys[i])
for i, key := range slices.Backward(keys) {
priv, err := nacl.DecodePrivate(key)
if err != nil {
return nil, fmt.Errorf("cannot create private key cipher: (%s)", err)
}
Expand Down
17 changes: 8 additions & 9 deletions vochain/results/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -93,15 +94,13 @@ func unmarshalVote(VotePackage []byte, keys []string) (*state.VotePackage, error
var vote state.VotePackage
rawVote := bytes.Clone(VotePackage)
// if encryption keys, decrypt the vote
if len(keys) > 0 {
for i := len(keys) - 1; i >= 0; i-- {
priv, err := nacl.DecodePrivate(keys[i])
if err != nil {
return nil, fmt.Errorf("cannot create private key cipher: (%s)", err)
}
if rawVote, err = priv.Decrypt(rawVote); err != nil {
return nil, fmt.Errorf("cannot decrypt vote with index key %d: %w", i, err)
}
for i, key := range slices.Backward(keys) {
priv, err := nacl.DecodePrivate(key)
if err != nil {
return nil, fmt.Errorf("cannot create private key cipher: (%s)", err)
}
if rawVote, err = priv.Decrypt(rawVote); err != nil {
return nil, fmt.Errorf("cannot decrypt vote with index key %d: %w", i, err)
}
}
if err := vote.Decode(rawVote); err != nil {
Expand Down

0 comments on commit a8a0a05

Please sign in to comment.