Skip to content

Commit

Permalink
Switched from errors.new to fmt.Errorf
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoAndreSa committed Mar 11, 2020
1 parent 31c25d5 commit 8861342
Show file tree
Hide file tree
Showing 29 changed files with 139 additions and 142 deletions.
16 changes: 8 additions & 8 deletions app/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package appunlynx

import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -32,7 +32,7 @@ func startQuery(el *onet.Roster, proofs bool, sum []string, count bool, whereQue

grp, aggr, err := client.SendSurveyResultsQuery(*surveyID)
if err != nil {
return errors.New("service could not output the results: " + err.Error())
return fmt.Errorf("service could not output the results: %v", err)
}

// Print Output
Expand Down Expand Up @@ -77,7 +77,7 @@ func openGroupToml(tomlFileName string) (*onet.Roster, error) {
}

if len(el.Roster.List) <= 0 {
return nil, errors.New("empty or invalid unlynx group file:" + tomlFileName)
return nil, fmt.Errorf("empty or invalid unlynx group file: %v", tomlFileName)
}

return el.Roster, nil
Expand All @@ -91,15 +91,15 @@ func checkRegex(input, expression string) bool {
func parseQuery(el *onet.Roster, sum string, count bool, where, predicate, groupBy string) ([]string, bool, []libunlynx.WhereQueryAttribute, string, []string, error) {

if sum == "" || (where != "" && predicate == "") || (where == "" && predicate != "") {
return nil, false, nil, "", nil, errors.New("wrong query! please check the sum, where and the predicate parameters")
return nil, false, nil, "", nil, fmt.Errorf("wrong query! please check the sum, where and the predicate parameters")
}

sumRegex := "{s[0-9]+(,\\s*s[0-9]+)*}"
whereRegex := "{(w[0-9]+(,\\s*[0-9]+))*(,\\s*w[0-9]+(,\\s*[0-9]+))*}"
groupByRegex := "{g[0-9]+(,\\s*g[0-9]+)*}"

if !checkRegex(sum, sumRegex) {
return nil, false, nil, "", nil, errors.New("error parsing the sum parameter(s)")
return nil, false, nil, "", nil, fmt.Errorf("error parsing the sum parameter(s)")
}
sum = strings.Replace(sum, " ", "", -1)
sum = strings.Replace(sum, "{", "", -1)
Expand All @@ -115,12 +115,12 @@ func parseQuery(el *onet.Roster, sum string, count bool, where, predicate, group
}

if !check {
return nil, false, nil, "", nil, errors.New("no 'count' attribute in the sum variables")
return nil, false, nil, "", nil, fmt.Errorf("no 'count' attribute in the sum variables")
}
}

if !checkRegex(where, whereRegex) {
return nil, false, nil, "", nil, errors.New("error parsing the where parameter(s)")
return nil, false, nil, "", nil, fmt.Errorf("error parsing the where parameter(s)")
}
where = strings.Replace(where, " ", "", -1)
where = strings.Replace(where, "{", "", -1)
Expand All @@ -145,7 +145,7 @@ func parseQuery(el *onet.Roster, sum string, count bool, where, predicate, group
}

if !checkRegex(groupBy, groupByRegex) {
return nil, false, nil, "", nil, errors.New("error parsing the groupBy parameter(s)")
return nil, false, nil, "", nil, fmt.Errorf("error parsing the groupBy parameter(s)")
}
groupBy = strings.Replace(groupBy, " ", "", -1)
groupBy = strings.Replace(groupBy, "{", "", -1)
Expand Down
8 changes: 4 additions & 4 deletions app/unlynx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package appunlynx

import (
"errors"
"fmt"
"os"

"github.com/ldsec/unlynx/lib"
Expand Down Expand Up @@ -123,7 +123,7 @@ func main() {
Usage: "Start unlynx server",
Action: func(c *cli.Context) error {
if err := runServer(c); err != nil {
return errors.New("error during runServer(): " + err.Error())
return fmt.Errorf("error during runServer(): %v", err)
}
return nil
},
Expand All @@ -135,10 +135,10 @@ func main() {
Usage: "Setup server configuration (interactive)",
Action: func(c *cli.Context) error {
if c.String(optionConfig) != "" {
return errors.New("[-] Configuration file option cannot be used for the 'setup' command")
return fmt.Errorf("[-] configuration file option cannot be used for the 'setup' command")
}
if c.GlobalIsSet("debug") {
return errors.New("[-] Debug option cannot be used for the 'setup' command")
return fmt.Errorf("[-] debug option cannot be used for the 'setup' command")
}
app.InteractiveConfig(libunlynx.SuiTe, BinaryName)
return nil
Expand Down
5 changes: 2 additions & 3 deletions data/handle_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dataunlynx

import (
"bufio"
"errors"
"fmt"
"math"
"math/rand"
Expand Down Expand Up @@ -78,7 +77,7 @@ func GenerateData(numDPs, numEntries, numEntriesFiltered, numGroupsClear, numGro
numWhereClear, numWhereEnc, numAggrClear, numAggrEnc int64, numType []int64, randomGroups bool) (map[string][]libunlynx.DpClearResponse, error) {

if int64(len(numType)) != (numGroupsClear + numGroupsEnc) {
return nil, errors.New("specify the correct number of group types for each grouping attribute")
return nil, fmt.Errorf("specify the correct number of group types for each grouping attribute")
}

testData := make(map[string][]libunlynx.DpClearResponse)
Expand All @@ -94,7 +93,7 @@ func GenerateData(numDPs, numEntries, numEntriesFiltered, numGroupsClear, numGro
group := make([]int64, 0)
AllPossibleGroups(numType[:], group, 0, &groups)
} else {
return nil, errors.New("the number of groups is different from the number of entries")
return nil, fmt.Errorf("the number of groups is different from the number of entries")
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/add_rm/add_rm_proofs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package libunlynxaddrm

import (
"errors"
"fmt"
"math"
"sync"

Expand Down Expand Up @@ -65,7 +65,7 @@ func AddRmProofCreation(cBef, cAft libunlynx.CipherText, K kyber.Point, k kyber.
proofTmp, err := proof.HashProve(libunlynx.SuiTe, "proofTest", prover)

if err != nil {
return PublishedAddRmProof{}, errors.New("---------Prover:" + err.Error())
return PublishedAddRmProof{}, fmt.Errorf("---------prover: %v", err)
}

return PublishedAddRmProof{Proof: proofTmp, CtBef: cBef, CtAft: cAft, RB: rB}, nil
Expand Down
29 changes: 14 additions & 15 deletions lib/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package libunlynx
import (
"encoding"
"encoding/base64"
"errors"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -635,7 +634,7 @@ func (c *CipherText) Serialize() (string, error) {
func (c *CipherText) Deserialize(b64Encoded string) error {
decoded, err := base64.URLEncoding.DecodeString(b64Encoded)
if err != nil {
return errors.New("Invalid CipherText (decoding failed): " + err.Error())
return fmt.Errorf("invalid ciphertext (decoding failed): %v", err)
}
err = (*c).FromBytes(decoded)
if err != nil {
Expand All @@ -648,7 +647,7 @@ func (c *CipherText) Deserialize(b64Encoded string) error {
func SerializeElement(el encoding.BinaryMarshaler) (string, error) {
bytes, err := el.MarshalBinary()
if err != nil {
return "", errors.New("Error marshalling element: " + err.Error())
return "", fmt.Errorf("error marshalling element: %v", err)
}
return base64.URLEncoding.EncodeToString(bytes), nil
}
Expand All @@ -665,31 +664,31 @@ func SerializeScalar(scalar encoding.BinaryMarshaler) (string, error) {

// DeserializePoint deserializes a point using base64 encoding
func DeserializePoint(encodedPoint string) (kyber.Point, error) {
decoded, errD := base64.URLEncoding.DecodeString(encodedPoint)
if errD != nil {
return nil, errors.New("Error decoding point: " + errD.Error())
decoded, err := base64.URLEncoding.DecodeString(encodedPoint)
if err != nil {
return nil, fmt.Errorf("error decoding point: %v", err)
}

point := SuiTe.Point()
errM := point.UnmarshalBinary(decoded)
if errM != nil {
return nil, errors.New("Error unmarshalling point: " + errM.Error())
err = point.UnmarshalBinary(decoded)
if err != nil {
return nil, fmt.Errorf("error unmarshalling point: %v", err)
}

return point, nil
}

// DeserializeScalar deserializes a scalar using base64 encoding
func DeserializeScalar(encodedScalar string) (kyber.Scalar, error) {
decoded, errD := base64.URLEncoding.DecodeString(encodedScalar)
if errD != nil {
return nil, errors.New("Error decoding scalar: " + errD.Error())
decoded, err := base64.URLEncoding.DecodeString(encodedScalar)
if err != nil {
return nil, fmt.Errorf("error decoding scalar: %v", err)
}

scalar := SuiTe.Scalar()
errM := scalar.UnmarshalBinary(decoded)
if errM != nil {
return nil, errors.New("Error unmarshalling scalar: " + errM.Error())
err = scalar.UnmarshalBinary(decoded)
if err != nil {
return nil, fmt.Errorf("error unmarshalling scalar: %v", err)
}

return scalar, nil
Expand Down
6 changes: 3 additions & 3 deletions lib/deterministic_tag/deterministic_tag_proofs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package libunlynxdetertag

import (
"errors"
"fmt"
"math"
"reflect"
"sync"
Expand Down Expand Up @@ -82,7 +82,7 @@ func DeterministicTagCrProofCreation(ctBef, ctAft libunlynx.CipherText, K kyber.
prover := predicate.Prover(libunlynx.SuiTe, sval, pval, nil) // computes: commitment, challenge, response
Proof, err := proof.HashProve(libunlynx.SuiTe, "proofTest", prover)
if err != nil {
return PublishedDDTCreationProof{}, errors.New("---------Prover: " + err.Error())
return PublishedDDTCreationProof{}, fmt.Errorf("---------prover: %v", err)
}

return PublishedDDTCreationProof{Proof: Proof, Ciminus11Si: ciminus11Si, CTbef: ctBef, CTaft: ctAft}, nil
Expand Down Expand Up @@ -191,7 +191,7 @@ func DeterministicTagAdditionProofCreation(c1 kyber.Point, s kyber.Scalar, c2 ky
prover := predicate.Prover(libunlynx.SuiTe, sval, pval, nil) // computes: commitment, challenge, response
Proof, err := proof.HashProve(libunlynx.SuiTe, "proofTest", prover)
if err != nil {
return PublishedDDTAdditionProof{}, errors.New("---------Prover: " + err.Error())
return PublishedDDTAdditionProof{}, fmt.Errorf("---------prover: %v", err)
}

return PublishedDDTAdditionProof{Proof: Proof, C1: c1, C2: c2, R: r}, nil
Expand Down
4 changes: 2 additions & 2 deletions lib/key_switch/key_switch_proofs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package libunlynxkeyswitch

import (
"errors"
"fmt"
"math"
"sync"

Expand Down Expand Up @@ -68,7 +68,7 @@ func KeySwitchProofCreation(K, Q kyber.Point, k kyber.Scalar, viB, ks2, rBNeg ky
prover := predicate.Prover(libunlynx.SuiTe, sval, pval, nil) // computes: commitment, challenge, response
proofKS, err := proof.HashProve(libunlynx.SuiTe, "proofTest", prover)
if err != nil {
return PublishedKSProof{}, errors.New("---------Prover: " + err.Error())
return PublishedKSProof{}, fmt.Errorf("---------prover: %v", err)
}

return PublishedKSProof{Proof: proofKS, K: K, ViB: viB, Ks2: ks2, RbNeg: rBNeg, Q: Q}, nil
Expand Down
8 changes: 4 additions & 4 deletions lib/shuffle/shuffle_proofs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package libunlynxshuffle

import (
"errors"
"fmt"
"math"
"sync"

Expand Down Expand Up @@ -84,7 +84,7 @@ func ShuffleProofCreation(originalList, shuffledList []libunlynx.CipherVector, g
// do k-shuffle of ElGamal on the (Xhat,Yhat) and check it
k = len(Xhat)
if k != len(Yhat) {
return PublishedShufflingProof{}, errors.New("X,Y vectors have inconsistent lengths")
return PublishedShufflingProof{}, fmt.Errorf("X,Y vectors have inconsistent lengths")
}
ps := shuffleKyber.PairShuffle{}
ps.Init(libunlynx.SuiTe, k)
Expand All @@ -95,7 +95,7 @@ func ShuffleProofCreation(originalList, shuffledList []libunlynx.CipherVector, g

prf, err := proof.HashProve(libunlynx.SuiTe, "PairShuffle", prover)
if err != nil {
return PublishedShufflingProof{}, errors.New("Shuffle proof failed: " + err.Error())
return PublishedShufflingProof{}, fmt.Errorf("shuffle proof failed: %v", err)
}
return PublishedShufflingProof{originalList, shuffledList, g, h, prf}, nil
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func compressCipherVector(ciphervector libunlynx.CipherVector, e []kyber.Scalar)

// check that e and cipher vectors have the same size
if len(e) != k {
return libunlynx.CipherText{}, errors.New("e is not the right size")
return libunlynx.CipherText{}, fmt.Errorf("e is not the right size")
}

ciphertext := *libunlynx.NewCipherText()
Expand Down
7 changes: 3 additions & 4 deletions lib/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package libunlynxtools
import (
"encoding/binary"
"encoding/gob"
"errors"
"fmt"
"os"
"strconv"
Expand All @@ -27,7 +26,7 @@ func SendISMOthers(s *onet.ServiceProcessor, el *onet.Roster, msg interface{}) e
}
var err error
if len(errStrs) > 0 {
err = errors.New(strings.Join(errStrs, "\n"))
err = fmt.Errorf(strings.Join(errStrs, "\n"))
}
return err
}
Expand Down Expand Up @@ -119,7 +118,7 @@ func WriteToGobFile(path string, object interface{}) error {
return err
}
} else {
return errors.New("Could not write Gob file:" + err.Error())
return fmt.Errorf("could not write Gob file: %v", err)
}

return nil
Expand All @@ -137,7 +136,7 @@ func ReadFromGobFile(path string, object interface{}) error {
return err
}
} else {
return errors.New("Could not read Gob file:" + err.Error())
return fmt.Errorf("could not read Gob file: %v", err)
}

return nil
Expand Down
18 changes: 9 additions & 9 deletions protocols/collective_aggregation_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package protocolsunlynx

import (
"errors"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -118,16 +118,16 @@ func NewCollectiveAggregationProtocol(n *onet.TreeNodeInstance) (onet.ProtocolIn

err := pap.RegisterChannel(&pap.DataReferenceChannel)
if err != nil {
return nil, errors.New("couldn't register data reference channel: " + err.Error())
return nil, fmt.Errorf("couldn't register data reference channel: %v", err)
}

err = pap.RegisterChannel(&pap.ChildDataChannel)
if err != nil {
return nil, errors.New("couldn't register child-data channel: " + err.Error())
return nil, fmt.Errorf("couldn't register child-data channel: %v", err)
}

if err := pap.RegisterChannel(&pap.LengthNodeChannel); err != nil {
return nil, errors.New("couldn't register data reference channel: " + err.Error())
return nil, fmt.Errorf("couldn't register data reference channel: %v", err)
}

return pap, nil
Expand Down Expand Up @@ -200,7 +200,7 @@ func (p *CollectiveAggregationProtocol) aggregationAnnouncementPhase() error {
p.SendToChildren(&dataReferenceMessage.DataReferenceMessage)
}
case <-time.After(libunlynx.TIMEOUT):
return errors.New(p.ServerIdentity().String() + " didn't get the <dataReferenceMessage> on time.")
return fmt.Errorf(p.ServerIdentity().String() + " didn't get the <dataReferenceMessage> on time")
}
return nil
}
Expand Down Expand Up @@ -281,10 +281,10 @@ func (p *CollectiveAggregationProtocol) ascendingAggregationPhase(cvMap map[libu
}

if err := p.SendToParent(&CADBLengthMessage{gacbLength, aabLength, dtbLength}); err != nil {
return nil, errors.New("Error sending <CADBLengthMessage>:" + err.Error())
return nil, fmt.Errorf("error sending <CADBLengthMessage>: %v", err)
}
if err := p.SendToParent(&message); err != nil {
return nil, errors.New("Error sending <ChildAggregatedDataMessage>:" + err.Error())
return nil, fmt.Errorf("error sending <ChildAggregatedDataMessage>: %v", err)
}
}

Expand All @@ -295,10 +295,10 @@ func (p *CollectiveAggregationProtocol) ascendingAggregationPhase(cvMap map[libu
func (p *CollectiveAggregationProtocol) checkData() error {
// If no data is passed to the collection protocol
if p.GroupedData == nil && p.SimpleData == nil {
return errors.New("no data reference is provided")
return fmt.Errorf("no data reference is provided")
// If both data entry points are used
} else if p.GroupedData != nil && p.SimpleData != nil {
return errors.New("two data references are given in the struct")
return fmt.Errorf("two data references are given in the struct")
// If we are using the GroupedData keep everything as is
} else if p.GroupedData != nil {
return nil
Expand Down
Loading

0 comments on commit 8861342

Please sign in to comment.