Skip to content

Commit

Permalink
Revert "all: use slices.Backward from Go 1.23"
Browse files Browse the repository at this point in the history
This reverts commit baef75f.

since we downgraded go back to 1.22
  • Loading branch information
altergui committed Sep 18, 2024
1 parent 59b5240 commit ea1285c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
11 changes: 5 additions & 6 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"math"
"math/big"
"reflect"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -207,13 +206,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 _, index := range slices.Backward(indexes) {
if index >= uint32(len(privKeys)) {
return nil, fmt.Errorf("invalid key index %d", index)
for i := len(indexes) - 1; i >= 0; i-- {
if indexes[i] >= uint32(len(privKeys)) {
return nil, fmt.Errorf("invalid key index %d", indexes[i])
}
priv, err := nacl.DecodePrivate(privKeys[index])
priv, err := nacl.DecodePrivate(privKeys[indexes[i]])
if err != nil {
return nil, fmt.Errorf("cannot decode encryption key with index %d: (%s)", index, err)
return nil, fmt.Errorf("cannot decode encryption key with index %d: (%s)", indexes[i], err)
}
vp, err = priv.Decrypt(vp)
if err != nil {
Expand Down
21 changes: 13 additions & 8 deletions tree/arbo/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"fmt"
"math"
"slices"

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

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

Expand Down Expand Up @@ -139,8 +138,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, key := range slices.Backward(keys) {
priv, err := nacl.DecodePrivate(key)
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)
}
Expand Down
17 changes: 9 additions & 8 deletions vochain/results/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"math/big"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -94,13 +93,15 @@ func unmarshalVote(VotePackage []byte, keys []string) (*state.VotePackage, error
var vote state.VotePackage
rawVote := bytes.Clone(VotePackage)
// if encryption keys, decrypt the vote
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 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)
}
}
}
if err := vote.Decode(rawVote); err != nil {
Expand Down

0 comments on commit ea1285c

Please sign in to comment.