From bee45d5dbd2b65042ec175b2b36ebc408895df1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 11 Sep 2024 20:37:04 +0100 Subject: [PATCH] all: use slices.Backward from Go 1.23 And simplify the surrounding code slightly. --- api/helpers.go | 11 ++++++----- tree/arbo/proof.go | 21 ++++++++------------- vochain/indexer/vote.go | 5 +++-- vochain/results/compute.go | 17 ++++++++--------- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/api/helpers.go b/api/helpers.go index a7431c296..f961c2e9e 100644 --- a/api/helpers.go +++ b/api/helpers.go @@ -7,6 +7,7 @@ import ( "fmt" "math" "math/big" + "slices" "strconv" "strings" "time" @@ -158,13 +159,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 { diff --git a/tree/arbo/proof.go b/tree/arbo/proof.go index dbf54bf06..ad19f671d 100644 --- a/tree/arbo/proof.go +++ b/tree/arbo/proof.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "fmt" "math" + "slices" "go.vocdoni.io/dvote/db" ) @@ -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 } diff --git a/vochain/indexer/vote.go b/vochain/indexer/vote.go index ca66e59d3..61d13c92c 100644 --- a/vochain/indexer/vote.go +++ b/vochain/indexer/vote.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "math/big" + "slices" "strings" "time" @@ -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) } diff --git a/vochain/results/compute.go b/vochain/results/compute.go index cc59b932e..5157c4281 100644 --- a/vochain/results/compute.go +++ b/vochain/results/compute.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math/big" + "slices" "sync" "time" @@ -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 {