Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: use slices.Backward from Go 1.23 #1388

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading