diff --git a/api/helpers.go b/api/helpers.go index a45c04aa6..69819cdd0 100644 --- a/api/helpers.go +++ b/api/helpers.go @@ -10,7 +10,6 @@ import ( "math" "math/big" "reflect" - "slices" "strconv" "strings" "time" @@ -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 { diff --git a/tree/arbo/proof.go b/tree/arbo/proof.go index ad19f671d..dbf54bf06 100644 --- a/tree/arbo/proof.go +++ b/tree/arbo/proof.go @@ -5,7 +5,6 @@ import ( "encoding/binary" "fmt" "math" - "slices" "go.vocdoni.io/dvote/db" ) @@ -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 } diff --git a/vochain/indexer/vote.go b/vochain/indexer/vote.go index 61d13c92c..ca66e59d3 100644 --- a/vochain/indexer/vote.go +++ b/vochain/indexer/vote.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "math/big" - "slices" "strings" "time" @@ -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) } diff --git a/vochain/results/compute.go b/vochain/results/compute.go index 5157c4281..cc59b932e 100644 --- a/vochain/results/compute.go +++ b/vochain/results/compute.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "math/big" - "slices" "sync" "time" @@ -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 {