From c0e04321bbf5c8262071f192ceb04ed82bd35d3e Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Mon, 11 Jun 2018 19:15:21 +0200 Subject: [PATCH 1/5] Moved bson to separate package --- bson2/encoding.go | 231 ++++++++++++++++++++++++++ bson2/encoding_test.go | 129 +++++++++++++++ bson2/utils.go | 25 +++ bson2/utils_test.go | 9 ++ encoding.go | 357 ++++++++++++++++++++--------------------- encoding_test.go | 151 +++++++++-------- paillier.go | 6 - 7 files changed, 646 insertions(+), 262 deletions(-) create mode 100644 bson2/encoding.go create mode 100644 bson2/encoding_test.go create mode 100644 bson2/utils.go create mode 100644 bson2/utils_test.go diff --git a/bson2/encoding.go b/bson2/encoding.go new file mode 100644 index 0000000..88230b7 --- /dev/null +++ b/bson2/encoding.go @@ -0,0 +1,231 @@ +package bson2 + +// all the methods relative to the set/get json/bson should be stored +// in this file. + +import ( + "encoding/json" + "errors" + "fmt" + "math/big" + + "github.com/keep-network/paillier" + "gopkg.in/mgo.v2/bson" +) + +type Cypher paillier.Cypher +type ThresholdPublicKey paillier.ThresholdPublicKey +type PublicKey paillier.PublicKey +type PrivateKey paillier.PrivateKey +type PartialDecryptionZKP paillier.PartialDecryptionZKP + +type dbCypher struct { + C string +} + +func (cypher *Cypher) GetBSON() (interface{}, error) { + return &dbCypher{fmt.Sprintf("%x", cypher.C)}, nil +} + +func (cypher *Cypher) SetBSON(raw bson.Raw) error { + c := dbCypher{} + if err := raw.Unmarshal(&c); err != nil { + return err + } + var ok bool + cypher.C, ok = new(big.Int).SetString(c.C, 16) + if !ok { + return errors.New("big int not in hexadecimal format") + } + return nil +} + +type dbThresholdKey struct { + TotalNumberOfDecryptionServers int + Threshold int + V string + Vi []string + N string +} + +func (dbThresholdKey *dbThresholdKey) FromThresholdPublicKey(key *ThresholdPublicKey) { + dbThresholdKey.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers + dbThresholdKey.Threshold = key.Threshold + dbThresholdKey.V = fmt.Sprintf("%x", key.V) + dbThresholdKey.N = fmt.Sprintf("%x", key.N) + dbThresholdKey.Vi = make([]string, len(key.Vi)) + for i, vi := range key.Vi { + dbThresholdKey.Vi[i] = fmt.Sprintf("%x", vi) + } +} + +func (dbThresholdKey *dbThresholdKey) ToThresholdPublicKey(key *ThresholdPublicKey) error { + key.TotalNumberOfDecryptionServers = dbThresholdKey.TotalNumberOfDecryptionServers + key.Threshold = dbThresholdKey.Threshold + oks := make([]bool, 2) + key.V, oks[0] = new(big.Int).SetString(dbThresholdKey.V, 16) + key.N, oks[1] = new(big.Int).SetString(dbThresholdKey.N, 16) + if !all(oks) { + return errors.New("not hexadecimal") + } + key.Vi = make([]*big.Int, len(dbThresholdKey.Vi)) + var ok bool + for i, vi := range dbThresholdKey.Vi { + key.Vi[i], ok = new(big.Int).SetString(vi, 16) + if !ok { + return errors.New("not hexadecimal") + } + + } + return nil +} + +func (thresholdPublicKey *ThresholdPublicKey) GetBSON() (interface{}, error) { + r := new(dbThresholdKey) + r.FromThresholdPublicKey(thresholdPublicKey) + return r, nil +} + +func (thresholdPublicKey *ThresholdPublicKey) SetBSON(raw bson.Raw) error { + r := new(dbThresholdKey) + if err := raw.Unmarshal(r); err != nil { + return err + } + return r.ToThresholdPublicKey(thresholdPublicKey) +} + +func (this *PublicKey) GetBSON() (interface{}, error) { + m := make(map[string]string) + m["n"] = fmt.Sprintf("%x", this.N) + return m, nil +} + +type dbPrivateKey struct { + N string `bson:",omitempty"` + Lambda string `bson:",omitempty"` + Mu string `bson:",omitempty"` +} + +func (privateKey *PrivateKey) GetBSON() (interface{}, error) { + m := make(map[string]string) + + if privateKey.N != nil { + m["n"] = fmt.Sprintf("%x", privateKey.N) + } + if privateKey.Lambda != nil { + m["lambda"] = fmt.Sprintf("%x", privateKey.Lambda) + } + return m, nil +} + +func (this *PrivateKey) SetBSON(raw bson.Raw) error { + var err error = nil + c := new(dbPrivateKey) + raw.Unmarshal(c) + + if c.N != "" { + this.N, err = fromHex(c.N) + if err != nil { + return err + } + } + + if c.Lambda != "" { + this.Lambda, err = fromHex(c.Lambda) + if err != nil { + return err + } + } + + return err +} + +type dbPartialDecryptionZKP struct { + Z string `json:"z"` + E string `json:"e"` + C string `json:"c"` + V string `json:"v"` + N string `json:"n"` + Vi []string `json:"vi"` + Decryption string `json:"decryption"` + Id int `json:"id"` + TotalNumberOfDecryptionServers int `json:"total_number_of_decryption_servers"` + Threshold int `json:"threshold"` +} + +func (this *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) { + this.Id = pd.Id + this.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers + this.Threshold = pd.Key.Threshold + this.Z = fmt.Sprintf("%x", pd.Z) + this.E = fmt.Sprintf("%x", pd.E) + this.N = fmt.Sprintf("%x", pd.Key.N) + this.C = fmt.Sprintf("%x", pd.C) + this.V = fmt.Sprintf("%x", pd.Key.V) + this.Decryption = fmt.Sprintf("%x", pd.Decryption) + this.Vi = make([]string, len(pd.Key.Vi)) + for i, vi := range pd.Key.Vi { + this.Vi[i] = fmt.Sprintf("%x", vi) + } +} + +func (this *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error { + pd.Key = new(paillier.ThresholdPublicKey) + + var oks = make([]bool, 6) + + pd.Id = this.Id + pd.Key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers + pd.Key.Threshold = this.Threshold + pd.Z, oks[0] = new(big.Int).SetString(this.Z, 16) + pd.E, oks[1] = new(big.Int).SetString(this.E, 16) + pd.C, oks[2] = new(big.Int).SetString(this.C, 16) + pd.Key.V, oks[3] = new(big.Int).SetString(this.V, 16) + pd.Key.N, oks[4] = new(big.Int).SetString(this.N, 16) + pd.Decryption, oks[5] = new(big.Int).SetString(this.Decryption, 16) + + if !all(oks) { + fmt.Println(oks) + fmt.Println(this.E, "|", this.Z, "|", this.C, "|", this.V, "|", this.Decryption) + return errors.New("numbers not in hexadecimal format") + } + + pd.Key.Vi = make([]*big.Int, len(this.Vi)) + var ok bool + for i, vi := range this.Vi { + pd.Key.Vi[i], ok = new(big.Int).SetString(vi, 16) + if !ok { + return errors.New("numbers not in hexadecimal format") + } + } + + return nil +} + +func (this *PartialDecryptionZKP) MarshalJSON() ([]byte, error) { + db := new(dbPartialDecryptionZKP) + db.FromPartialDecryptionZKP(this) + return json.Marshal(db) +} + +func (this *PartialDecryptionZKP) UnmarshalJSON(data []byte) error { + db := new(dbPartialDecryptionZKP) + if err := json.Unmarshal(data, db); err != nil { + return err + } + return db.ToPartialDecryptionZKP(this) +} + +func (this *PartialDecryptionZKP) GetBSON() (interface{}, error) { + db := new(dbPartialDecryptionZKP) + db.FromPartialDecryptionZKP(this) + return db, nil +} + +func (this *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { + db := new(dbPartialDecryptionZKP) + if err := raw.Unmarshal(db); err != nil { + return err + } + return db.ToPartialDecryptionZKP(this) +} diff --git a/bson2/encoding_test.go b/bson2/encoding_test.go new file mode 100644 index 0000000..53479d4 --- /dev/null +++ b/bson2/encoding_test.go @@ -0,0 +1,129 @@ +package bson2 + +import ( + "crypto/rand" + "encoding/json" + "math/big" + "reflect" + "testing" + + "github.com/keep-network/paillier" + "gopkg.in/mgo.v2/bson" +) + +func AssertBSONIsGood(object, dump interface{}, t *testing.T) { + data, err := bson.Marshal(object) + if err != nil { + t.Error(err) + return + } + if err = bson.Unmarshal(data, dump); err != nil { + t.Error(err) + return + } + if !reflect.DeepEqual(object, dump) { + t.Errorf("Object: %v doesn't equal dump: %v", object, dump) + } +} + +func AssertJSONIsGood(object, dump interface{}, t *testing.T) { + data, err := json.Marshal(object) + if err != nil { + t.Error(err) + return + } + if err = json.Unmarshal(data, dump); err != nil { + t.Error(err) + return + } + if !reflect.DeepEqual(object, dump) { + t.Errorf("Object: %v doesn't equal dump: %v", object, dump) + } +} + +func TestCypherGetBSON(t *testing.T) { + key := paillier.CreatePrivateKey(big.NewInt(101), big.NewInt(113)) + cypherOriginal, err := key.Encrypt(big.NewInt(100), rand.Reader) + + cypher := new(Cypher) + cypher.C = cypherOriginal.C + + data, err := bson.Marshal(cypher) + if err != nil { + t.Error(err) + return + } + cypher2 := new(Cypher) + if err = bson.Unmarshal(data, cypher2); err != nil { + t.Error(err) + return + } + if !reflect.DeepEqual(cypher, cypher2) { + t.Errorf("%v not equal %v", cypher, cypher2) + } +} + +func TestThresholdKeyBSON(t *testing.T) { + key := &ThresholdPublicKey{ + PublicKey: paillier.PublicKey{b(9)}, + TotalNumberOfDecryptionServers: 7, + Threshold: 6, + V: b(3), + Vi: []*big.Int{b(2), b(34)}, + } + AssertBSONIsGood(key, new(ThresholdPublicKey), t) +} + +func TestSetPrivateKeyBson(t *testing.T) { + key := paillier.CreatePrivateKey(big.NewInt(101), big.NewInt(113)) + dump := new(PrivateKey) + + AssertBSONIsGood(key, dump, t) +} + +func TestGetBSONEmptyKey(t *testing.T) { + key := new(PrivateKey) + m, err := key.GetBSON() + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(make(map[string]string), m) { + t.Error(m) + } +} + +func TestPartialDecryptionZKPJsonification(t *testing.T) { + pd := new(PartialDecryptionZKP) + pd.Key = new(paillier.ThresholdPublicKey) + pd.Key.Threshold = 98 + pd.Key.TotalNumberOfDecryptionServers = 230 + pd.Id = 1 + pd.Key.Vi = []*big.Int{b(77), b(67)} + pd.Key.N = b(131) + pd.Key.V = b(101) + pd.Decryption = b(171) + pd.E = b(112) + pd.C = b(99) + pd.Key.N = b(345) + pd.Z = b(88) + + AssertJSONIsGood(pd, new(PartialDecryptionZKP), t) +} + +func TestPartialDecryptionZKPBSONification(t *testing.T) { + pd := new(PartialDecryptionZKP) + pd.Key = new(paillier.ThresholdPublicKey) + pd.Key.Threshold = 98 + pd.Key.TotalNumberOfDecryptionServers = 230 + pd.Id = 1 + pd.Key.Vi = []*big.Int{b(77), b(67)} // vi is 67 + pd.Key.N = b(131) + pd.Key.V = b(101) + pd.Decryption = b(171) + pd.E = b(112) + pd.C = b(99) + pd.Key.N = b(345) + pd.Z = b(88) + + AssertBSONIsGood(pd, new(PartialDecryptionZKP), t) +} diff --git a/bson2/utils.go b/bson2/utils.go new file mode 100644 index 0000000..ec14111 --- /dev/null +++ b/bson2/utils.go @@ -0,0 +1,25 @@ +package bson2 + +import ( + "errors" + "fmt" + "math/big" +) + +func fromHex(hex string) (*big.Int, error) { + n, err := new(big.Int).SetString(hex, 16) + if !err { + msg := fmt.Sprintf("Cannot convert %s to int as hexadecimal", hex) + return nil, errors.New(msg) + } + return n, nil +} + +func all(oks []bool) bool { + for _, ok := range oks { + if !ok { + return false + } + } + return true +} diff --git a/bson2/utils_test.go b/bson2/utils_test.go new file mode 100644 index 0000000..3caca57 --- /dev/null +++ b/bson2/utils_test.go @@ -0,0 +1,9 @@ +package bson2 + +import ( + "math/big" +) + +func b(i int) *big.Int { + return big.NewInt(int64(i)) +} diff --git a/encoding.go b/encoding.go index d89cc01..3d12cfd 100644 --- a/encoding.go +++ b/encoding.go @@ -4,12 +4,9 @@ package paillier // in this file. import ( - "encoding/json" "errors" "fmt" "math/big" - - "gopkg.in/mgo.v2/bson" ) func fromHex(hex string) (*big.Int, error) { @@ -30,207 +27,207 @@ func all(oks []bool) bool { return true } -type dbCypher struct { - C string -} +// type dbCypher struct { +// C string +// } -func (this *Cypher) GetBSON() (interface{}, error) { - return &dbCypher{fmt.Sprintf("%x", this.C)}, nil -} +// func (this *Cypher) GetBSON() (interface{}, error) { +// return &dbCypher{fmt.Sprintf("%x", this.C)}, nil +// } -func (cypher *Cypher) SetBSON(raw bson.Raw) error { - c := dbCypher{} - if err := raw.Unmarshal(&c); err != nil { - return err - } - var ok bool - cypher.C, ok = new(big.Int).SetString(c.C, 16) - if !ok { - return errors.New("big int not in hexadecimal format") - } - return nil -} +// func (cypher *Cypher) SetBSON(raw bson.Raw) error { +// c := dbCypher{} +// if err := raw.Unmarshal(&c); err != nil { +// return err +// } +// var ok bool +// cypher.C, ok = new(big.Int).SetString(c.C, 16) +// if !ok { +// return errors.New("big int not in hexadecimal format") +// } +// return nil +// } -type dbThresholdKey struct { - TotalNumberOfDecryptionServers int - Threshold int - V string - Vi []string - N string -} +// type dbThresholdKey struct { +// TotalNumberOfDecryptionServers int +// Threshold int +// V string +// Vi []string +// N string +// } -func (this *dbThresholdKey) FromThresholdPublicKey(key *ThresholdPublicKey) { - this.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers - this.Threshold = key.Threshold - this.V = fmt.Sprintf("%x", key.V) - this.N = fmt.Sprintf("%x", key.N) - this.Vi = make([]string, len(key.Vi)) - for i, vi := range key.Vi { - this.Vi[i] = fmt.Sprintf("%x", vi) - } -} +// func (this *dbThresholdKey) FromThresholdPublicKey(key *ThresholdPublicKey) { +// this.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers +// this.Threshold = key.Threshold +// this.V = fmt.Sprintf("%x", key.V) +// this.N = fmt.Sprintf("%x", key.N) +// this.Vi = make([]string, len(key.Vi)) +// for i, vi := range key.Vi { +// this.Vi[i] = fmt.Sprintf("%x", vi) +// } +// } -func (this *dbThresholdKey) ToThresholdPublicKey(key *ThresholdPublicKey) error { - key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers - key.Threshold = this.Threshold - oks := make([]bool, 2) - key.V, oks[0] = new(big.Int).SetString(this.V, 16) - key.N, oks[1] = new(big.Int).SetString(this.N, 16) - if !all(oks) { - return errors.New("not hexadecimal") - } - key.Vi = make([]*big.Int, len(this.Vi)) - var ok bool - for i, vi := range this.Vi { - key.Vi[i], ok = new(big.Int).SetString(vi, 16) - if !ok { - return errors.New("not hexadecimal") - } +// func (this *dbThresholdKey) ToThresholdPublicKey(key *ThresholdPublicKey) error { +// key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers +// key.Threshold = this.Threshold +// oks := make([]bool, 2) +// key.V, oks[0] = new(big.Int).SetString(this.V, 16) +// key.N, oks[1] = new(big.Int).SetString(this.N, 16) +// if !all(oks) { +// return errors.New("not hexadecimal") +// } +// key.Vi = make([]*big.Int, len(this.Vi)) +// var ok bool +// for i, vi := range this.Vi { +// key.Vi[i], ok = new(big.Int).SetString(vi, 16) +// if !ok { +// return errors.New("not hexadecimal") +// } - } - return nil -} +// } +// return nil +// } -func (this *ThresholdPublicKey) GetBSON() (interface{}, error) { - r := new(dbThresholdKey) - r.FromThresholdPublicKey(this) - return r, nil -} +// func (this *ThresholdPublicKey) GetBSON() (interface{}, error) { +// r := new(dbThresholdKey) +// r.FromThresholdPublicKey(this) +// return r, nil +// } -func (this *ThresholdPublicKey) SetBSON(raw bson.Raw) error { - r := new(dbThresholdKey) - if err := raw.Unmarshal(r); err != nil { - return err - } - return r.ToThresholdPublicKey(this) -} +// func (this *ThresholdPublicKey) SetBSON(raw bson.Raw) error { +// r := new(dbThresholdKey) +// if err := raw.Unmarshal(r); err != nil { +// return err +// } +// return r.ToThresholdPublicKey(this) +// } -type dbPrivateKey struct { - N string `bson:",omitempty"` - Lambda string `bson:",omitempty"` - Mu string `bson:",omitempty"` -} +// type dbPrivateKey struct { +// N string `bson:",omitempty"` +// Lambda string `bson:",omitempty"` +// Mu string `bson:",omitempty"` +// } -func (this *PrivateKey) GetBSON() (interface{}, error) { - m := make(map[string]string) +// func (this *PrivateKey) GetBSON() (interface{}, error) { +// m := make(map[string]string) - if this.N != nil { - m["n"] = fmt.Sprintf("%x", this.N) - } - if this.Lambda != nil { - m["lambda"] = fmt.Sprintf("%x", this.Lambda) - } - return m, nil -} +// if this.N != nil { +// m["n"] = fmt.Sprintf("%x", this.N) +// } +// if this.Lambda != nil { +// m["lambda"] = fmt.Sprintf("%x", this.Lambda) +// } +// return m, nil +// } -func (this *PrivateKey) SetBSON(raw bson.Raw) error { - var err error = nil - c := new(dbPrivateKey) - raw.Unmarshal(c) +// func (this *PrivateKey) SetBSON(raw bson.Raw) error { +// var err error = nil +// c := new(dbPrivateKey) +// raw.Unmarshal(c) - if c.N != "" { - this.N, err = fromHex(c.N) - if err != nil { - return err - } - } +// if c.N != "" { +// this.N, err = fromHex(c.N) +// if err != nil { +// return err +// } +// } - if c.Lambda != "" { - this.Lambda, err = fromHex(c.Lambda) - if err != nil { - return err - } - } +// if c.Lambda != "" { +// this.Lambda, err = fromHex(c.Lambda) +// if err != nil { +// return err +// } +// } - return err -} +// return err +// } -type dbPartialDecryptionZKP struct { - Z string `json:"z"` - E string `json:"e"` - C string `json:"c"` - V string `json:"v"` - N string `json:"n"` - Vi []string `json:"vi"` - Decryption string `json:"decryption"` - Id int `json:"id"` - TotalNumberOfDecryptionServers int `json:"total_number_of_decryption_servers"` - Threshold int `json:"threshold"` -} +// type dbPartialDecryptionZKP struct { +// Z string `json:"z"` +// E string `json:"e"` +// C string `json:"c"` +// V string `json:"v"` +// N string `json:"n"` +// Vi []string `json:"vi"` +// Decryption string `json:"decryption"` +// Id int `json:"id"` +// TotalNumberOfDecryptionServers int `json:"total_number_of_decryption_servers"` +// Threshold int `json:"threshold"` +// } -func (this *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) { - this.Id = pd.Id - this.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers - this.Threshold = pd.Key.Threshold - this.Z = fmt.Sprintf("%x", pd.Z) - this.E = fmt.Sprintf("%x", pd.E) - this.N = fmt.Sprintf("%x", pd.Key.N) - this.C = fmt.Sprintf("%x", pd.C) - this.V = fmt.Sprintf("%x", pd.Key.V) - this.Decryption = fmt.Sprintf("%x", pd.Decryption) - this.Vi = make([]string, len(pd.Key.Vi)) - for i, vi := range pd.Key.Vi { - this.Vi[i] = fmt.Sprintf("%x", vi) - } -} +// func (this *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) { +// this.Id = pd.Id +// this.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers +// this.Threshold = pd.Key.Threshold +// this.Z = fmt.Sprintf("%x", pd.Z) +// this.E = fmt.Sprintf("%x", pd.E) +// this.N = fmt.Sprintf("%x", pd.Key.N) +// this.C = fmt.Sprintf("%x", pd.C) +// this.V = fmt.Sprintf("%x", pd.Key.V) +// this.Decryption = fmt.Sprintf("%x", pd.Decryption) +// this.Vi = make([]string, len(pd.Key.Vi)) +// for i, vi := range pd.Key.Vi { +// this.Vi[i] = fmt.Sprintf("%x", vi) +// } +// } -func (this *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error { - pd.Key = new(ThresholdPublicKey) +// func (this *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error { +// pd.Key = new(ThresholdPublicKey) - var oks = make([]bool, 6) +// var oks = make([]bool, 6) - pd.Id = this.Id - pd.Key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers - pd.Key.Threshold = this.Threshold - pd.Z, oks[0] = new(big.Int).SetString(this.Z, 16) - pd.E, oks[1] = new(big.Int).SetString(this.E, 16) - pd.C, oks[2] = new(big.Int).SetString(this.C, 16) - pd.Key.V, oks[3] = new(big.Int).SetString(this.V, 16) - pd.Key.N, oks[4] = new(big.Int).SetString(this.N, 16) - pd.Decryption, oks[5] = new(big.Int).SetString(this.Decryption, 16) +// pd.Id = this.Id +// pd.Key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers +// pd.Key.Threshold = this.Threshold +// pd.Z, oks[0] = new(big.Int).SetString(this.Z, 16) +// pd.E, oks[1] = new(big.Int).SetString(this.E, 16) +// pd.C, oks[2] = new(big.Int).SetString(this.C, 16) +// pd.Key.V, oks[3] = new(big.Int).SetString(this.V, 16) +// pd.Key.N, oks[4] = new(big.Int).SetString(this.N, 16) +// pd.Decryption, oks[5] = new(big.Int).SetString(this.Decryption, 16) - if !all(oks) { - fmt.Println(oks) - fmt.Println(this.E, "|", this.Z, "|", this.C, "|", this.V, "|", this.Decryption) - return errors.New("numbers not in hexadecimal format") - } +// if !all(oks) { +// fmt.Println(oks) +// fmt.Println(this.E, "|", this.Z, "|", this.C, "|", this.V, "|", this.Decryption) +// return errors.New("numbers not in hexadecimal format") +// } - pd.Key.Vi = make([]*big.Int, len(this.Vi)) - var ok bool - for i, vi := range this.Vi { - pd.Key.Vi[i], ok = new(big.Int).SetString(vi, 16) - if !ok { - return errors.New("numbers not in hexadecimal format") - } - } +// pd.Key.Vi = make([]*big.Int, len(this.Vi)) +// var ok bool +// for i, vi := range this.Vi { +// pd.Key.Vi[i], ok = new(big.Int).SetString(vi, 16) +// if !ok { +// return errors.New("numbers not in hexadecimal format") +// } +// } - return nil -} +// return nil +// } -func (this *PartialDecryptionZKP) MarshalJSON() ([]byte, error) { - db := new(dbPartialDecryptionZKP) - db.FromPartialDecryptionZKP(this) - return json.Marshal(db) -} +// func (this *PartialDecryptionZKP) MarshalJSON() ([]byte, error) { +// db := new(dbPartialDecryptionZKP) +// db.FromPartialDecryptionZKP(this) +// return json.Marshal(db) +// } -func (this *PartialDecryptionZKP) UnmarshalJSON(data []byte) error { - db := new(dbPartialDecryptionZKP) - if err := json.Unmarshal(data, db); err != nil { - return err - } - return db.ToPartialDecryptionZKP(this) -} +// func (this *PartialDecryptionZKP) UnmarshalJSON(data []byte) error { +// db := new(dbPartialDecryptionZKP) +// if err := json.Unmarshal(data, db); err != nil { +// return err +// } +// return db.ToPartialDecryptionZKP(this) +// } -func (this *PartialDecryptionZKP) GetBSON() (interface{}, error) { - db := new(dbPartialDecryptionZKP) - db.FromPartialDecryptionZKP(this) - return db, nil -} +// func (this *PartialDecryptionZKP) GetBSON() (interface{}, error) { +// db := new(dbPartialDecryptionZKP) +// db.FromPartialDecryptionZKP(this) +// return db, nil +// } -func (this *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { - db := new(dbPartialDecryptionZKP) - if err := raw.Unmarshal(db); err != nil { - return err - } - return db.ToPartialDecryptionZKP(this) -} +// func (this *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { +// db := new(dbPartialDecryptionZKP) +// if err := raw.Unmarshal(db); err != nil { +// return err +// } +// return db.ToPartialDecryptionZKP(this) +// } diff --git a/encoding_test.go b/encoding_test.go index aa08577..c7c2cec 100644 --- a/encoding_test.go +++ b/encoding_test.go @@ -1,7 +1,6 @@ package paillier import ( - "crypto/rand" "encoding/json" "math/big" "reflect" @@ -21,7 +20,7 @@ func AssertBSONIsGood(object, dump interface{}, t *testing.T) { return } if !reflect.DeepEqual(object, dump) { - t.Fail() + t.Errorf("%v not equal %v", object, dump) } } @@ -36,31 +35,31 @@ func AssertJSONIsGood(object, dump interface{}, t *testing.T) { return } if !reflect.DeepEqual(object, dump) { - t.Fail() + t.Errorf("%v not equal %v", object, dump) } } -func TestCypherGetBSON(t *testing.T) { - key := CreatePrivateKey(big.NewInt(101), big.NewInt(113)) - cypher, err := key.Encrypt(big.NewInt(100), rand.Reader) - if err != nil { - t.Error(err) - return - } - data, err := bson.Marshal(cypher) - if err != nil { - t.Error(err) - return - } - cypher2 := new(Cypher) - if err = bson.Unmarshal(data, cypher2); err != nil { - t.Error(err) - return - } - if !reflect.DeepEqual(cypher, cypher2) { - t.Fail() - } -} +// func TestCypherGetBSON(t *testing.T) { +// key := CreatePrivateKey(big.NewInt(101), big.NewInt(113)) +// cypher, err := key.Encrypt(big.NewInt(100), rand.Reader) +// if err != nil { +// t.Error(err) +// return +// } +// data, err := bson.Marshal(cypher) +// if err != nil { +// t.Error(err) +// return +// } +// cypher2 := new(Cypher) +// if err = bson.Unmarshal(data, cypher2); err != nil { +// t.Error(err) +// return +// } +// if !reflect.DeepEqual(cypher, cypher2) { +// t.Fail() +// } +// } func TestSetPrivateKeyBson(t *testing.T) { key := CreatePrivateKey(big.NewInt(101), big.NewInt(113)) @@ -68,60 +67,60 @@ func TestSetPrivateKeyBson(t *testing.T) { } -func TestGetBSONEmptyKey(t *testing.T) { - key := new(PrivateKey) - m, err := key.GetBSON() - if err != nil { - t.Error(err) - } - if !reflect.DeepEqual(make(map[string]string), m) { - t.Error(m) - } -} +// func TestGetBSONEmptyKey(t *testing.T) { +// key := new(PrivateKey) +// m, err := key.GetBSON() +// if err != nil { +// t.Error(err) +// } +// if !reflect.DeepEqual(make(map[string]string), m) { +// t.Error(m) +// } +// } -func TestPartialDecryptionZKPJsonification(t *testing.T) { - pd := new(PartialDecryptionZKP) - pd.Key = new(ThresholdPublicKey) - pd.Key.Threshold = 98 - pd.Key.TotalNumberOfDecryptionServers = 230 - pd.Id = 1 - pd.Key.Vi = []*big.Int{b(77), b(67)} - pd.Key.N = b(131) - pd.Key.V = b(101) - pd.Decryption = b(171) - pd.E = b(112) - pd.C = b(99) - pd.Key.N = b(345) - pd.Z = b(88) +// func TestPartialDecryptionZKPJsonification(t *testing.T) { +// pd := new(PartialDecryptionZKP) +// pd.Key = new(ThresholdPublicKey) +// pd.Key.Threshold = 98 +// pd.Key.TotalNumberOfDecryptionServers = 230 +// pd.Id = 1 +// pd.Key.Vi = []*big.Int{b(77), b(67)} +// pd.Key.N = b(131) +// pd.Key.V = b(101) +// pd.Decryption = b(171) +// pd.E = b(112) +// pd.C = b(99) +// pd.Key.N = b(345) +// pd.Z = b(88) - AssertJSONIsGood(pd, new(PartialDecryptionZKP), t) -} +// AssertJSONIsGood(pd, new(PartialDecryptionZKP), t) +// } -func TestPartialDecryptionZKPBSONification(t *testing.T) { - pd := new(PartialDecryptionZKP) - pd.Key = new(ThresholdPublicKey) - pd.Key.Threshold = 98 - pd.Key.TotalNumberOfDecryptionServers = 230 - pd.Id = 1 - pd.Key.Vi = []*big.Int{b(77), b(67)} // vi is 67 - pd.Key.N = b(131) - pd.Key.V = b(101) - pd.Decryption = b(171) - pd.E = b(112) - pd.C = b(99) - pd.Key.N = b(345) - pd.Z = b(88) +// func TestPartialDecryptionZKPBSONification(t *testing.T) { +// pd := new(PartialDecryptionZKP) +// pd.Key = new(ThresholdPublicKey) +// pd.Key.Threshold = 98 +// pd.Key.TotalNumberOfDecryptionServers = 230 +// pd.Id = 1 +// pd.Key.Vi = []*big.Int{b(77), b(67)} // vi is 67 +// pd.Key.N = b(131) +// pd.Key.V = b(101) +// pd.Decryption = b(171) +// pd.E = b(112) +// pd.C = b(99) +// pd.Key.N = b(345) +// pd.Z = b(88) - AssertBSONIsGood(pd, new(PartialDecryptionZKP), t) -} +// AssertBSONIsGood(pd, new(PartialDecryptionZKP), t) +// } -func TestThresholdKeyBSON(t *testing.T) { - key := &ThresholdPublicKey{ - PublicKey: PublicKey{b(9)}, - TotalNumberOfDecryptionServers: 7, - Threshold: 6, - V: b(3), - Vi: []*big.Int{b(2), b(34)}, - } - AssertBSONIsGood(key, new(ThresholdPublicKey), t) -} +// func TestThresholdKeyBSON(t *testing.T) { +// key := &ThresholdPublicKey{ +// PublicKey: PublicKey{b(9)}, +// TotalNumberOfDecryptionServers: 7, +// Threshold: 6, +// V: b(3), +// Vi: []*big.Int{b(2), b(34)}, +// } +// AssertBSONIsGood(key, new(ThresholdPublicKey), t) +// } diff --git a/paillier.go b/paillier.go index 2bd1203..9b6fba2 100644 --- a/paillier.go +++ b/paillier.go @@ -10,12 +10,6 @@ type PublicKey struct { N *big.Int } -func (this *PublicKey) GetBSON() (interface{}, error) { - m := make(map[string]string) - m["n"] = fmt.Sprintf("%x", this.N) - return m, nil -} - func (this *PublicKey) GetNSquare() *big.Int { return new(big.Int).Mul(this.N, this.N) } -- 2.55.0 From d038e085a3b1c29046d34a5c7133ffdfd7cff312 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Wed, 13 Jun 2018 11:27:43 -0400 Subject: [PATCH 2/5] Second round of bson extraction --- {bson2 => bson}/encoding.go | 12 +- {bson2 => bson}/encoding_test.go | 9 +- {bson2 => bson}/utils.go | 6 +- bson2/utils_test.go | 9 -- encoding.go | 233 ------------------------------- encoding_test.go | 126 ----------------- 6 files changed, 18 insertions(+), 377 deletions(-) rename {bson2 => bson}/encoding.go (96%) rename {bson2 => bson}/encoding_test.go (93%) rename {bson2 => bson}/utils.go (83%) delete mode 100644 bson2/utils_test.go delete mode 100644 encoding.go delete mode 100644 encoding_test.go diff --git a/bson2/encoding.go b/bson/encoding.go similarity index 96% rename from bson2/encoding.go rename to bson/encoding.go index 88230b7..2a9b2c9 100644 --- a/bson2/encoding.go +++ b/bson/encoding.go @@ -1,4 +1,4 @@ -package bson2 +package bson // all the methods relative to the set/get json/bson should be stored // in this file. @@ -106,14 +106,14 @@ type dbPrivateKey struct { Mu string `bson:",omitempty"` } -func (privateKey *PrivateKey) GetBSON() (interface{}, error) { +func (this *PrivateKey) GetBSON() (interface{}, error) { m := make(map[string]string) - if privateKey.N != nil { - m["n"] = fmt.Sprintf("%x", privateKey.N) + if this.N != nil { + m["n"] = fmt.Sprintf("%x", this.N) } - if privateKey.Lambda != nil { - m["lambda"] = fmt.Sprintf("%x", privateKey.Lambda) + if this.Lambda != nil { + m["lambda"] = fmt.Sprintf("%x", this.Lambda) } return m, nil } diff --git a/bson2/encoding_test.go b/bson/encoding_test.go similarity index 93% rename from bson2/encoding_test.go rename to bson/encoding_test.go index 53479d4..27ad355 100644 --- a/bson2/encoding_test.go +++ b/bson/encoding_test.go @@ -1,4 +1,4 @@ -package bson2 +package bson import ( "crypto/rand" @@ -75,7 +75,12 @@ func TestThresholdKeyBSON(t *testing.T) { } func TestSetPrivateKeyBson(t *testing.T) { - key := paillier.CreatePrivateKey(big.NewInt(101), big.NewInt(113)) + var key = new(PrivateKey) + var keyOriginal = paillier.CreatePrivateKey(big.NewInt(101), big.NewInt(113)) + + key.PublicKey = keyOriginal.PublicKey + key.Lambda = keyOriginal.Lambda + dump := new(PrivateKey) AssertBSONIsGood(key, dump, t) diff --git a/bson2/utils.go b/bson/utils.go similarity index 83% rename from bson2/utils.go rename to bson/utils.go index ec14111..f1b8207 100644 --- a/bson2/utils.go +++ b/bson/utils.go @@ -1,4 +1,4 @@ -package bson2 +package bson import ( "errors" @@ -23,3 +23,7 @@ func all(oks []bool) bool { } return true } + +func b(i int) *big.Int { + return big.NewInt(int64(i)) +} diff --git a/bson2/utils_test.go b/bson2/utils_test.go deleted file mode 100644 index 3caca57..0000000 --- a/bson2/utils_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package bson2 - -import ( - "math/big" -) - -func b(i int) *big.Int { - return big.NewInt(int64(i)) -} diff --git a/encoding.go b/encoding.go deleted file mode 100644 index 3d12cfd..0000000 --- a/encoding.go +++ /dev/null @@ -1,233 +0,0 @@ -package paillier - -// all the methods relative to the set/get json/bson should be stored -// in this file. - -import ( - "errors" - "fmt" - "math/big" -) - -func fromHex(hex string) (*big.Int, error) { - n, err := new(big.Int).SetString(hex, 16) - if !err { - msg := fmt.Sprintf("Cannot convert %s to int as hexadecimal", hex) - return nil, errors.New(msg) - } - return n, nil -} - -func all(oks []bool) bool { - for _, ok := range oks { - if !ok { - return false - } - } - return true -} - -// type dbCypher struct { -// C string -// } - -// func (this *Cypher) GetBSON() (interface{}, error) { -// return &dbCypher{fmt.Sprintf("%x", this.C)}, nil -// } - -// func (cypher *Cypher) SetBSON(raw bson.Raw) error { -// c := dbCypher{} -// if err := raw.Unmarshal(&c); err != nil { -// return err -// } -// var ok bool -// cypher.C, ok = new(big.Int).SetString(c.C, 16) -// if !ok { -// return errors.New("big int not in hexadecimal format") -// } -// return nil -// } - -// type dbThresholdKey struct { -// TotalNumberOfDecryptionServers int -// Threshold int -// V string -// Vi []string -// N string -// } - -// func (this *dbThresholdKey) FromThresholdPublicKey(key *ThresholdPublicKey) { -// this.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers -// this.Threshold = key.Threshold -// this.V = fmt.Sprintf("%x", key.V) -// this.N = fmt.Sprintf("%x", key.N) -// this.Vi = make([]string, len(key.Vi)) -// for i, vi := range key.Vi { -// this.Vi[i] = fmt.Sprintf("%x", vi) -// } -// } - -// func (this *dbThresholdKey) ToThresholdPublicKey(key *ThresholdPublicKey) error { -// key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers -// key.Threshold = this.Threshold -// oks := make([]bool, 2) -// key.V, oks[0] = new(big.Int).SetString(this.V, 16) -// key.N, oks[1] = new(big.Int).SetString(this.N, 16) -// if !all(oks) { -// return errors.New("not hexadecimal") -// } -// key.Vi = make([]*big.Int, len(this.Vi)) -// var ok bool -// for i, vi := range this.Vi { -// key.Vi[i], ok = new(big.Int).SetString(vi, 16) -// if !ok { -// return errors.New("not hexadecimal") -// } - -// } -// return nil -// } - -// func (this *ThresholdPublicKey) GetBSON() (interface{}, error) { -// r := new(dbThresholdKey) -// r.FromThresholdPublicKey(this) -// return r, nil -// } - -// func (this *ThresholdPublicKey) SetBSON(raw bson.Raw) error { -// r := new(dbThresholdKey) -// if err := raw.Unmarshal(r); err != nil { -// return err -// } -// return r.ToThresholdPublicKey(this) -// } - -// type dbPrivateKey struct { -// N string `bson:",omitempty"` -// Lambda string `bson:",omitempty"` -// Mu string `bson:",omitempty"` -// } - -// func (this *PrivateKey) GetBSON() (interface{}, error) { -// m := make(map[string]string) - -// if this.N != nil { -// m["n"] = fmt.Sprintf("%x", this.N) -// } -// if this.Lambda != nil { -// m["lambda"] = fmt.Sprintf("%x", this.Lambda) -// } -// return m, nil -// } - -// func (this *PrivateKey) SetBSON(raw bson.Raw) error { -// var err error = nil -// c := new(dbPrivateKey) -// raw.Unmarshal(c) - -// if c.N != "" { -// this.N, err = fromHex(c.N) -// if err != nil { -// return err -// } -// } - -// if c.Lambda != "" { -// this.Lambda, err = fromHex(c.Lambda) -// if err != nil { -// return err -// } -// } - -// return err -// } - -// type dbPartialDecryptionZKP struct { -// Z string `json:"z"` -// E string `json:"e"` -// C string `json:"c"` -// V string `json:"v"` -// N string `json:"n"` -// Vi []string `json:"vi"` -// Decryption string `json:"decryption"` -// Id int `json:"id"` -// TotalNumberOfDecryptionServers int `json:"total_number_of_decryption_servers"` -// Threshold int `json:"threshold"` -// } - -// func (this *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) { -// this.Id = pd.Id -// this.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers -// this.Threshold = pd.Key.Threshold -// this.Z = fmt.Sprintf("%x", pd.Z) -// this.E = fmt.Sprintf("%x", pd.E) -// this.N = fmt.Sprintf("%x", pd.Key.N) -// this.C = fmt.Sprintf("%x", pd.C) -// this.V = fmt.Sprintf("%x", pd.Key.V) -// this.Decryption = fmt.Sprintf("%x", pd.Decryption) -// this.Vi = make([]string, len(pd.Key.Vi)) -// for i, vi := range pd.Key.Vi { -// this.Vi[i] = fmt.Sprintf("%x", vi) -// } -// } - -// func (this *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error { -// pd.Key = new(ThresholdPublicKey) - -// var oks = make([]bool, 6) - -// pd.Id = this.Id -// pd.Key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers -// pd.Key.Threshold = this.Threshold -// pd.Z, oks[0] = new(big.Int).SetString(this.Z, 16) -// pd.E, oks[1] = new(big.Int).SetString(this.E, 16) -// pd.C, oks[2] = new(big.Int).SetString(this.C, 16) -// pd.Key.V, oks[3] = new(big.Int).SetString(this.V, 16) -// pd.Key.N, oks[4] = new(big.Int).SetString(this.N, 16) -// pd.Decryption, oks[5] = new(big.Int).SetString(this.Decryption, 16) - -// if !all(oks) { -// fmt.Println(oks) -// fmt.Println(this.E, "|", this.Z, "|", this.C, "|", this.V, "|", this.Decryption) -// return errors.New("numbers not in hexadecimal format") -// } - -// pd.Key.Vi = make([]*big.Int, len(this.Vi)) -// var ok bool -// for i, vi := range this.Vi { -// pd.Key.Vi[i], ok = new(big.Int).SetString(vi, 16) -// if !ok { -// return errors.New("numbers not in hexadecimal format") -// } -// } - -// return nil -// } - -// func (this *PartialDecryptionZKP) MarshalJSON() ([]byte, error) { -// db := new(dbPartialDecryptionZKP) -// db.FromPartialDecryptionZKP(this) -// return json.Marshal(db) -// } - -// func (this *PartialDecryptionZKP) UnmarshalJSON(data []byte) error { -// db := new(dbPartialDecryptionZKP) -// if err := json.Unmarshal(data, db); err != nil { -// return err -// } -// return db.ToPartialDecryptionZKP(this) -// } - -// func (this *PartialDecryptionZKP) GetBSON() (interface{}, error) { -// db := new(dbPartialDecryptionZKP) -// db.FromPartialDecryptionZKP(this) -// return db, nil -// } - -// func (this *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { -// db := new(dbPartialDecryptionZKP) -// if err := raw.Unmarshal(db); err != nil { -// return err -// } -// return db.ToPartialDecryptionZKP(this) -// } diff --git a/encoding_test.go b/encoding_test.go deleted file mode 100644 index c7c2cec..0000000 --- a/encoding_test.go +++ /dev/null @@ -1,126 +0,0 @@ -package paillier - -import ( - "encoding/json" - "math/big" - "reflect" - "testing" - - "gopkg.in/mgo.v2/bson" -) - -func AssertBSONIsGood(object, dump interface{}, t *testing.T) { - data, err := bson.Marshal(object) - if err != nil { - t.Error(err) - return - } - if err = bson.Unmarshal(data, dump); err != nil { - t.Error(err) - return - } - if !reflect.DeepEqual(object, dump) { - t.Errorf("%v not equal %v", object, dump) - } -} - -func AssertJSONIsGood(object, dump interface{}, t *testing.T) { - data, err := json.Marshal(object) - if err != nil { - t.Error(err) - return - } - if err = json.Unmarshal(data, dump); err != nil { - t.Error(err) - return - } - if !reflect.DeepEqual(object, dump) { - t.Errorf("%v not equal %v", object, dump) - } -} - -// func TestCypherGetBSON(t *testing.T) { -// key := CreatePrivateKey(big.NewInt(101), big.NewInt(113)) -// cypher, err := key.Encrypt(big.NewInt(100), rand.Reader) -// if err != nil { -// t.Error(err) -// return -// } -// data, err := bson.Marshal(cypher) -// if err != nil { -// t.Error(err) -// return -// } -// cypher2 := new(Cypher) -// if err = bson.Unmarshal(data, cypher2); err != nil { -// t.Error(err) -// return -// } -// if !reflect.DeepEqual(cypher, cypher2) { -// t.Fail() -// } -// } - -func TestSetPrivateKeyBson(t *testing.T) { - key := CreatePrivateKey(big.NewInt(101), big.NewInt(113)) - AssertBSONIsGood(key, new(PrivateKey), t) - -} - -// func TestGetBSONEmptyKey(t *testing.T) { -// key := new(PrivateKey) -// m, err := key.GetBSON() -// if err != nil { -// t.Error(err) -// } -// if !reflect.DeepEqual(make(map[string]string), m) { -// t.Error(m) -// } -// } - -// func TestPartialDecryptionZKPJsonification(t *testing.T) { -// pd := new(PartialDecryptionZKP) -// pd.Key = new(ThresholdPublicKey) -// pd.Key.Threshold = 98 -// pd.Key.TotalNumberOfDecryptionServers = 230 -// pd.Id = 1 -// pd.Key.Vi = []*big.Int{b(77), b(67)} -// pd.Key.N = b(131) -// pd.Key.V = b(101) -// pd.Decryption = b(171) -// pd.E = b(112) -// pd.C = b(99) -// pd.Key.N = b(345) -// pd.Z = b(88) - -// AssertJSONIsGood(pd, new(PartialDecryptionZKP), t) -// } - -// func TestPartialDecryptionZKPBSONification(t *testing.T) { -// pd := new(PartialDecryptionZKP) -// pd.Key = new(ThresholdPublicKey) -// pd.Key.Threshold = 98 -// pd.Key.TotalNumberOfDecryptionServers = 230 -// pd.Id = 1 -// pd.Key.Vi = []*big.Int{b(77), b(67)} // vi is 67 -// pd.Key.N = b(131) -// pd.Key.V = b(101) -// pd.Decryption = b(171) -// pd.E = b(112) -// pd.C = b(99) -// pd.Key.N = b(345) -// pd.Z = b(88) - -// AssertBSONIsGood(pd, new(PartialDecryptionZKP), t) -// } - -// func TestThresholdKeyBSON(t *testing.T) { -// key := &ThresholdPublicKey{ -// PublicKey: PublicKey{b(9)}, -// TotalNumberOfDecryptionServers: 7, -// Threshold: 6, -// V: b(3), -// Vi: []*big.Int{b(2), b(34)}, -// } -// AssertBSONIsGood(key, new(ThresholdPublicKey), t) -// } -- 2.55.0 From 091f257cd5f2b0a624a76466f866c373722bba0e Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Wed, 13 Jun 2018 14:55:20 -0400 Subject: [PATCH 3/5] Changed names of method's receivers --- bson/encoding.go | 86 ++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/bson/encoding.go b/bson/encoding.go index 2a9b2c9..627ce35 100644 --- a/bson/encoding.go +++ b/bson/encoding.go @@ -94,9 +94,9 @@ func (thresholdPublicKey *ThresholdPublicKey) SetBSON(raw bson.Raw) error { return r.ToThresholdPublicKey(thresholdPublicKey) } -func (this *PublicKey) GetBSON() (interface{}, error) { +func (publicKey *PublicKey) GetBSON() (interface{}, error) { m := make(map[string]string) - m["n"] = fmt.Sprintf("%x", this.N) + m["n"] = fmt.Sprintf("%x", publicKey.N) return m, nil } @@ -106,32 +106,32 @@ type dbPrivateKey struct { Mu string `bson:",omitempty"` } -func (this *PrivateKey) GetBSON() (interface{}, error) { +func (privateKey *PrivateKey) GetBSON() (interface{}, error) { m := make(map[string]string) - if this.N != nil { - m["n"] = fmt.Sprintf("%x", this.N) + if privateKey.N != nil { + m["n"] = fmt.Sprintf("%x", privateKey.N) } - if this.Lambda != nil { - m["lambda"] = fmt.Sprintf("%x", this.Lambda) + if privateKey.Lambda != nil { + m["lambda"] = fmt.Sprintf("%x", privateKey.Lambda) } return m, nil } -func (this *PrivateKey) SetBSON(raw bson.Raw) error { +func (privateKey *PrivateKey) SetBSON(raw bson.Raw) error { var err error = nil c := new(dbPrivateKey) raw.Unmarshal(c) if c.N != "" { - this.N, err = fromHex(c.N) + privateKey.N, err = fromHex(c.N) if err != nil { return err } } if c.Lambda != "" { - this.Lambda, err = fromHex(c.Lambda) + privateKey.Lambda, err = fromHex(c.Lambda) if err != nil { return err } @@ -153,46 +153,46 @@ type dbPartialDecryptionZKP struct { Threshold int `json:"threshold"` } -func (this *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) { - this.Id = pd.Id - this.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers - this.Threshold = pd.Key.Threshold - this.Z = fmt.Sprintf("%x", pd.Z) - this.E = fmt.Sprintf("%x", pd.E) - this.N = fmt.Sprintf("%x", pd.Key.N) - this.C = fmt.Sprintf("%x", pd.C) - this.V = fmt.Sprintf("%x", pd.Key.V) - this.Decryption = fmt.Sprintf("%x", pd.Decryption) - this.Vi = make([]string, len(pd.Key.Vi)) +func (dbPDZKP *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) { + dbPDZKP.Id = pd.Id + dbPDZKP.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers + dbPDZKP.Threshold = pd.Key.Threshold + dbPDZKP.Z = fmt.Sprintf("%x", pd.Z) + dbPDZKP.E = fmt.Sprintf("%x", pd.E) + dbPDZKP.N = fmt.Sprintf("%x", pd.Key.N) + dbPDZKP.C = fmt.Sprintf("%x", pd.C) + dbPDZKP.V = fmt.Sprintf("%x", pd.Key.V) + dbPDZKP.Decryption = fmt.Sprintf("%x", pd.Decryption) + dbPDZKP.Vi = make([]string, len(pd.Key.Vi)) for i, vi := range pd.Key.Vi { - this.Vi[i] = fmt.Sprintf("%x", vi) + dbPDZKP.Vi[i] = fmt.Sprintf("%x", vi) } } -func (this *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error { +func (dbPDZKP *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error { pd.Key = new(paillier.ThresholdPublicKey) var oks = make([]bool, 6) - pd.Id = this.Id - pd.Key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers - pd.Key.Threshold = this.Threshold - pd.Z, oks[0] = new(big.Int).SetString(this.Z, 16) - pd.E, oks[1] = new(big.Int).SetString(this.E, 16) - pd.C, oks[2] = new(big.Int).SetString(this.C, 16) - pd.Key.V, oks[3] = new(big.Int).SetString(this.V, 16) - pd.Key.N, oks[4] = new(big.Int).SetString(this.N, 16) - pd.Decryption, oks[5] = new(big.Int).SetString(this.Decryption, 16) + pd.Id = dbPDZKP.Id + pd.Key.TotalNumberOfDecryptionServers = dbPDZKP.TotalNumberOfDecryptionServers + pd.Key.Threshold = dbPDZKP.Threshold + pd.Z, oks[0] = new(big.Int).SetString(dbPDZKP.Z, 16) + pd.E, oks[1] = new(big.Int).SetString(dbPDZKP.E, 16) + pd.C, oks[2] = new(big.Int).SetString(dbPDZKP.C, 16) + pd.Key.V, oks[3] = new(big.Int).SetString(dbPDZKP.V, 16) + pd.Key.N, oks[4] = new(big.Int).SetString(dbPDZKP.N, 16) + pd.Decryption, oks[5] = new(big.Int).SetString(dbPDZKP.Decryption, 16) if !all(oks) { fmt.Println(oks) - fmt.Println(this.E, "|", this.Z, "|", this.C, "|", this.V, "|", this.Decryption) + fmt.Println(dbPDZKP.E, "|", dbPDZKP.Z, "|", dbPDZKP.C, "|", dbPDZKP.V, "|", dbPDZKP.Decryption) return errors.New("numbers not in hexadecimal format") } - pd.Key.Vi = make([]*big.Int, len(this.Vi)) + pd.Key.Vi = make([]*big.Int, len(dbPDZKP.Vi)) var ok bool - for i, vi := range this.Vi { + for i, vi := range dbPDZKP.Vi { pd.Key.Vi[i], ok = new(big.Int).SetString(vi, 16) if !ok { return errors.New("numbers not in hexadecimal format") @@ -202,30 +202,30 @@ func (this *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryption return nil } -func (this *PartialDecryptionZKP) MarshalJSON() ([]byte, error) { +func (pd *PartialDecryptionZKP) MarshalJSON() ([]byte, error) { db := new(dbPartialDecryptionZKP) - db.FromPartialDecryptionZKP(this) + db.FromPartialDecryptionZKP(pd) return json.Marshal(db) } -func (this *PartialDecryptionZKP) UnmarshalJSON(data []byte) error { +func (pd *PartialDecryptionZKP) UnmarshalJSON(data []byte) error { db := new(dbPartialDecryptionZKP) if err := json.Unmarshal(data, db); err != nil { return err } - return db.ToPartialDecryptionZKP(this) + return db.ToPartialDecryptionZKP(pd) } -func (this *PartialDecryptionZKP) GetBSON() (interface{}, error) { +func (pd *PartialDecryptionZKP) GetBSON() (interface{}, error) { db := new(dbPartialDecryptionZKP) - db.FromPartialDecryptionZKP(this) + db.FromPartialDecryptionZKP(pd) return db, nil } -func (this *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { +func (pd *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { db := new(dbPartialDecryptionZKP) if err := raw.Unmarshal(db); err != nil { return err } - return db.ToPartialDecryptionZKP(this) + return db.ToPartialDecryptionZKP(pd) } -- 2.55.0 From e3cb617c6067123ca61c41a812199c543bc14628 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Tue, 19 Jun 2018 14:49:26 +0200 Subject: [PATCH 4/5] Splited encoding per object type, added public methods for serialization --- bson/encoding.go | 231 --------------------- bson/encoding_cypher.go | 62 ++++++ bson/encoding_cypher_test.go | 32 +++ bson/encoding_partialdecryptionzkp.go | 133 ++++++++++++ bson/encoding_partialdecryptionzkp_test.go | 68 ++++++ bson/encoding_privatekey.go | 79 +++++++ bson/encoding_privatekey_test.go | 35 ++++ bson/encoding_publickey.go | 64 ++++++ bson/encoding_publickey_test.go | 32 +++ bson/encoding_test.go | 134 ------------ bson/encoding_thresholdpublickey.go | 95 +++++++++ bson/encoding_thresholdpublickey_test.go | 37 ++++ 12 files changed, 637 insertions(+), 365 deletions(-) delete mode 100644 bson/encoding.go create mode 100644 bson/encoding_cypher.go create mode 100644 bson/encoding_cypher_test.go create mode 100644 bson/encoding_partialdecryptionzkp.go create mode 100644 bson/encoding_partialdecryptionzkp_test.go create mode 100644 bson/encoding_privatekey.go create mode 100644 bson/encoding_privatekey_test.go create mode 100644 bson/encoding_publickey.go create mode 100644 bson/encoding_publickey_test.go delete mode 100644 bson/encoding_test.go create mode 100644 bson/encoding_thresholdpublickey.go create mode 100644 bson/encoding_thresholdpublickey_test.go diff --git a/bson/encoding.go b/bson/encoding.go deleted file mode 100644 index 627ce35..0000000 --- a/bson/encoding.go +++ /dev/null @@ -1,231 +0,0 @@ -package bson - -// all the methods relative to the set/get json/bson should be stored -// in this file. - -import ( - "encoding/json" - "errors" - "fmt" - "math/big" - - "github.com/keep-network/paillier" - "gopkg.in/mgo.v2/bson" -) - -type Cypher paillier.Cypher -type ThresholdPublicKey paillier.ThresholdPublicKey -type PublicKey paillier.PublicKey -type PrivateKey paillier.PrivateKey -type PartialDecryptionZKP paillier.PartialDecryptionZKP - -type dbCypher struct { - C string -} - -func (cypher *Cypher) GetBSON() (interface{}, error) { - return &dbCypher{fmt.Sprintf("%x", cypher.C)}, nil -} - -func (cypher *Cypher) SetBSON(raw bson.Raw) error { - c := dbCypher{} - if err := raw.Unmarshal(&c); err != nil { - return err - } - var ok bool - cypher.C, ok = new(big.Int).SetString(c.C, 16) - if !ok { - return errors.New("big int not in hexadecimal format") - } - return nil -} - -type dbThresholdKey struct { - TotalNumberOfDecryptionServers int - Threshold int - V string - Vi []string - N string -} - -func (dbThresholdKey *dbThresholdKey) FromThresholdPublicKey(key *ThresholdPublicKey) { - dbThresholdKey.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers - dbThresholdKey.Threshold = key.Threshold - dbThresholdKey.V = fmt.Sprintf("%x", key.V) - dbThresholdKey.N = fmt.Sprintf("%x", key.N) - dbThresholdKey.Vi = make([]string, len(key.Vi)) - for i, vi := range key.Vi { - dbThresholdKey.Vi[i] = fmt.Sprintf("%x", vi) - } -} - -func (dbThresholdKey *dbThresholdKey) ToThresholdPublicKey(key *ThresholdPublicKey) error { - key.TotalNumberOfDecryptionServers = dbThresholdKey.TotalNumberOfDecryptionServers - key.Threshold = dbThresholdKey.Threshold - oks := make([]bool, 2) - key.V, oks[0] = new(big.Int).SetString(dbThresholdKey.V, 16) - key.N, oks[1] = new(big.Int).SetString(dbThresholdKey.N, 16) - if !all(oks) { - return errors.New("not hexadecimal") - } - key.Vi = make([]*big.Int, len(dbThresholdKey.Vi)) - var ok bool - for i, vi := range dbThresholdKey.Vi { - key.Vi[i], ok = new(big.Int).SetString(vi, 16) - if !ok { - return errors.New("not hexadecimal") - } - - } - return nil -} - -func (thresholdPublicKey *ThresholdPublicKey) GetBSON() (interface{}, error) { - r := new(dbThresholdKey) - r.FromThresholdPublicKey(thresholdPublicKey) - return r, nil -} - -func (thresholdPublicKey *ThresholdPublicKey) SetBSON(raw bson.Raw) error { - r := new(dbThresholdKey) - if err := raw.Unmarshal(r); err != nil { - return err - } - return r.ToThresholdPublicKey(thresholdPublicKey) -} - -func (publicKey *PublicKey) GetBSON() (interface{}, error) { - m := make(map[string]string) - m["n"] = fmt.Sprintf("%x", publicKey.N) - return m, nil -} - -type dbPrivateKey struct { - N string `bson:",omitempty"` - Lambda string `bson:",omitempty"` - Mu string `bson:",omitempty"` -} - -func (privateKey *PrivateKey) GetBSON() (interface{}, error) { - m := make(map[string]string) - - if privateKey.N != nil { - m["n"] = fmt.Sprintf("%x", privateKey.N) - } - if privateKey.Lambda != nil { - m["lambda"] = fmt.Sprintf("%x", privateKey.Lambda) - } - return m, nil -} - -func (privateKey *PrivateKey) SetBSON(raw bson.Raw) error { - var err error = nil - c := new(dbPrivateKey) - raw.Unmarshal(c) - - if c.N != "" { - privateKey.N, err = fromHex(c.N) - if err != nil { - return err - } - } - - if c.Lambda != "" { - privateKey.Lambda, err = fromHex(c.Lambda) - if err != nil { - return err - } - } - - return err -} - -type dbPartialDecryptionZKP struct { - Z string `json:"z"` - E string `json:"e"` - C string `json:"c"` - V string `json:"v"` - N string `json:"n"` - Vi []string `json:"vi"` - Decryption string `json:"decryption"` - Id int `json:"id"` - TotalNumberOfDecryptionServers int `json:"total_number_of_decryption_servers"` - Threshold int `json:"threshold"` -} - -func (dbPDZKP *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) { - dbPDZKP.Id = pd.Id - dbPDZKP.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers - dbPDZKP.Threshold = pd.Key.Threshold - dbPDZKP.Z = fmt.Sprintf("%x", pd.Z) - dbPDZKP.E = fmt.Sprintf("%x", pd.E) - dbPDZKP.N = fmt.Sprintf("%x", pd.Key.N) - dbPDZKP.C = fmt.Sprintf("%x", pd.C) - dbPDZKP.V = fmt.Sprintf("%x", pd.Key.V) - dbPDZKP.Decryption = fmt.Sprintf("%x", pd.Decryption) - dbPDZKP.Vi = make([]string, len(pd.Key.Vi)) - for i, vi := range pd.Key.Vi { - dbPDZKP.Vi[i] = fmt.Sprintf("%x", vi) - } -} - -func (dbPDZKP *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error { - pd.Key = new(paillier.ThresholdPublicKey) - - var oks = make([]bool, 6) - - pd.Id = dbPDZKP.Id - pd.Key.TotalNumberOfDecryptionServers = dbPDZKP.TotalNumberOfDecryptionServers - pd.Key.Threshold = dbPDZKP.Threshold - pd.Z, oks[0] = new(big.Int).SetString(dbPDZKP.Z, 16) - pd.E, oks[1] = new(big.Int).SetString(dbPDZKP.E, 16) - pd.C, oks[2] = new(big.Int).SetString(dbPDZKP.C, 16) - pd.Key.V, oks[3] = new(big.Int).SetString(dbPDZKP.V, 16) - pd.Key.N, oks[4] = new(big.Int).SetString(dbPDZKP.N, 16) - pd.Decryption, oks[5] = new(big.Int).SetString(dbPDZKP.Decryption, 16) - - if !all(oks) { - fmt.Println(oks) - fmt.Println(dbPDZKP.E, "|", dbPDZKP.Z, "|", dbPDZKP.C, "|", dbPDZKP.V, "|", dbPDZKP.Decryption) - return errors.New("numbers not in hexadecimal format") - } - - pd.Key.Vi = make([]*big.Int, len(dbPDZKP.Vi)) - var ok bool - for i, vi := range dbPDZKP.Vi { - pd.Key.Vi[i], ok = new(big.Int).SetString(vi, 16) - if !ok { - return errors.New("numbers not in hexadecimal format") - } - } - - return nil -} - -func (pd *PartialDecryptionZKP) MarshalJSON() ([]byte, error) { - db := new(dbPartialDecryptionZKP) - db.FromPartialDecryptionZKP(pd) - return json.Marshal(db) -} - -func (pd *PartialDecryptionZKP) UnmarshalJSON(data []byte) error { - db := new(dbPartialDecryptionZKP) - if err := json.Unmarshal(data, db); err != nil { - return err - } - return db.ToPartialDecryptionZKP(pd) -} - -func (pd *PartialDecryptionZKP) GetBSON() (interface{}, error) { - db := new(dbPartialDecryptionZKP) - db.FromPartialDecryptionZKP(pd) - return db, nil -} - -func (pd *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { - db := new(dbPartialDecryptionZKP) - if err := raw.Unmarshal(db); err != nil { - return err - } - return db.ToPartialDecryptionZKP(pd) -} diff --git a/bson/encoding_cypher.go b/bson/encoding_cypher.go new file mode 100644 index 0000000..7452abe --- /dev/null +++ b/bson/encoding_cypher.go @@ -0,0 +1,62 @@ +// Package bson provides functions for serialization and deserialization between +// paillier objects and BSON +package bson + +import ( + "errors" + "fmt" + "math/big" + + "github.com/keep-network/paillier" + "gopkg.in/mgo.v2/bson" +) + +type Cypher paillier.Cypher + +// Serializes Cypher to BSON +func SerializeCypher(cypher *paillier.Cypher) ([]byte, error) { + return bson.Marshal(toSerializableCypher(cypher)) +} + +// Deserializes BSON to Cypher +func DeserializeCypher(data []byte) (*paillier.Cypher, error) { + serializable := new(Cypher) + if err := bson.Unmarshal(data, serializable); err != nil { + return nil, err + } + + return toOriginalCypher(serializable), nil +} + +// Changes original Cypher to serializable Cypher +func toSerializableCypher(cypher *paillier.Cypher) *Cypher { + serializable := Cypher(*cypher) + return &serializable +} + +// Changes serializable Cypher to original Cypher +func toOriginalCypher(serializable *Cypher) *paillier.Cypher { + original := paillier.Cypher(*serializable) + return &original +} + +type dbCypher struct { + C string +} + +func (cypher *Cypher) GetBSON() (interface{}, error) { + return &dbCypher{fmt.Sprintf("%x", cypher.C)}, nil +} + +func (cypher *Cypher) SetBSON(raw bson.Raw) error { + c := dbCypher{} + if err := raw.Unmarshal(&c); err != nil { + return err + } + var ok bool + cypher.C, ok = new(big.Int).SetString(c.C, 16) + if !ok { + return errors.New("big int not in hexadecimal format") + } + return nil +} diff --git a/bson/encoding_cypher_test.go b/bson/encoding_cypher_test.go new file mode 100644 index 0000000..c6d78eb --- /dev/null +++ b/bson/encoding_cypher_test.go @@ -0,0 +1,32 @@ +package bson + +import ( + "reflect" + "testing" + + "github.com/keep-network/paillier" +) + +func TestCypherBsonSerialization(t *testing.T) { + cypher := &paillier.Cypher{ + C: b(5), + } + + serialized, err := SerializeCypher(cypher) + if err != nil { + t.Fatal(err) + } + + deserialized, err := DeserializeCypher(serialized) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(cypher, deserialized) { + t.Errorf( + "Unexpected serialization result\nActual: %v\nExpected: %v\n", + deserialized, + cypher, + ) + } +} diff --git a/bson/encoding_partialdecryptionzkp.go b/bson/encoding_partialdecryptionzkp.go new file mode 100644 index 0000000..d592945 --- /dev/null +++ b/bson/encoding_partialdecryptionzkp.go @@ -0,0 +1,133 @@ +// Package bson provides functions for serialization and deserialization between +// paillier objects and BSON +package bson + +import ( + "encoding/json" + "errors" + "fmt" + "math/big" + + "github.com/keep-network/paillier" + "gopkg.in/mgo.v2/bson" +) + +type PartialDecryptionZKP paillier.PartialDecryptionZKP + +// Serializes PartialDecryptionZKP to BSON +func SerializePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) ([]byte, error) { + return bson.Marshal(toSerializablePartialDecryptionZKP(pdzkp)) +} + +// Deserializes BSON to PartialDecryptionZKP +func DeserializePartialDecryptionZKP(data []byte) (*paillier.PartialDecryptionZKP, error) { + serializable := new(PartialDecryptionZKP) + if err := bson.Unmarshal(data, serializable); err != nil { + return nil, err + } + + return toOriginalPartialDecryptionZKP(serializable), nil +} + +// Serializes PartialDecryptionZKP to JSON +func JsonSerializePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) ([]byte, error) { + return json.Marshal(toSerializablePartialDecryptionZKP(pdzkp)) +} + +// Deserializes JSON to PartialDecryptionZKP +func JsonDeserializePartialDecryptionZKP(data []byte) (*paillier.PartialDecryptionZKP, error) { + serializable := new(PartialDecryptionZKP) + if err := json.Unmarshal(data, serializable); err != nil { + return nil, err + } + + return toOriginalPartialDecryptionZKP(serializable), nil +} + +// Changes original PartialDecryptionZKP to serializable PartialDecryptionZKP +func toSerializablePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) *PartialDecryptionZKP { + serializable := PartialDecryptionZKP(*pdzkp) + return &serializable +} + +// Changes serializable PartialDecryptionZKP to original PartialDecryptionZKP +func toOriginalPartialDecryptionZKP(serializable *PartialDecryptionZKP) *paillier.PartialDecryptionZKP { + original := paillier.PartialDecryptionZKP(*serializable) + return &original +} + +type dbPartialDecryptionZKP struct { + Z string `json:"z"` + E string `json:"e"` + C string `json:"c"` + V string `json:"v"` + N string `json:"n"` + Vi []string `json:"vi"` + Decryption string `json:"decryption"` + Id int `json:"id"` + TotalNumberOfDecryptionServers int `json:"total_number_of_decryption_servers"` + Threshold int `json:"threshold"` +} + +func (pdzkp *PartialDecryptionZKP) GetBSON() (interface{}, error) { + db := new(dbPartialDecryptionZKP) + db.fromPartialDecryptionZKP(pdzkp) + return db, nil +} + +func (pdzkp *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { + db := new(dbPartialDecryptionZKP) + if err := raw.Unmarshal(db); err != nil { + return err + } + return db.toPartialDecryptionZKP(pdzkp) +} + +func (dbPDZKP *dbPartialDecryptionZKP) fromPartialDecryptionZKP(pdzkp *PartialDecryptionZKP) { + dbPDZKP.Id = pdzkp.Id + dbPDZKP.TotalNumberOfDecryptionServers = pdzkp.Key.TotalNumberOfDecryptionServers + dbPDZKP.Threshold = pdzkp.Key.Threshold + dbPDZKP.Z = fmt.Sprintf("%x", pdzkp.Z) + dbPDZKP.E = fmt.Sprintf("%x", pdzkp.E) + dbPDZKP.N = fmt.Sprintf("%x", pdzkp.Key.N) + dbPDZKP.C = fmt.Sprintf("%x", pdzkp.C) + dbPDZKP.V = fmt.Sprintf("%x", pdzkp.Key.V) + dbPDZKP.Decryption = fmt.Sprintf("%x", pdzkp.Decryption) + dbPDZKP.Vi = make([]string, len(pdzkp.Key.Vi)) + for i, vi := range pdzkp.Key.Vi { + dbPDZKP.Vi[i] = fmt.Sprintf("%x", vi) + } +} + +func (dbPDZKP *dbPartialDecryptionZKP) toPartialDecryptionZKP(pdzkp *PartialDecryptionZKP) error { + pdzkp.Key = new(paillier.ThresholdPublicKey) + + var oks = make([]bool, 6) + + pdzkp.Id = dbPDZKP.Id + pdzkp.Key.TotalNumberOfDecryptionServers = dbPDZKP.TotalNumberOfDecryptionServers + pdzkp.Key.Threshold = dbPDZKP.Threshold + pdzkp.Z, oks[0] = new(big.Int).SetString(dbPDZKP.Z, 16) + pdzkp.E, oks[1] = new(big.Int).SetString(dbPDZKP.E, 16) + pdzkp.C, oks[2] = new(big.Int).SetString(dbPDZKP.C, 16) + pdzkp.Key.V, oks[3] = new(big.Int).SetString(dbPDZKP.V, 16) + pdzkp.Key.N, oks[4] = new(big.Int).SetString(dbPDZKP.N, 16) + pdzkp.Decryption, oks[5] = new(big.Int).SetString(dbPDZKP.Decryption, 16) + + if !all(oks) { + fmt.Println(oks) + fmt.Println(dbPDZKP.E, "|", dbPDZKP.Z, "|", dbPDZKP.C, "|", dbPDZKP.V, "|", dbPDZKP.Decryption) + return errors.New("numbers not in hexadecimal format") + } + + pdzkp.Key.Vi = make([]*big.Int, len(dbPDZKP.Vi)) + var ok bool + for i, vi := range dbPDZKP.Vi { + pdzkp.Key.Vi[i], ok = new(big.Int).SetString(vi, 16) + if !ok { + return errors.New("numbers not in hexadecimal format") + } + } + + return nil +} diff --git a/bson/encoding_partialdecryptionzkp_test.go b/bson/encoding_partialdecryptionzkp_test.go new file mode 100644 index 0000000..40b0e1e --- /dev/null +++ b/bson/encoding_partialdecryptionzkp_test.go @@ -0,0 +1,68 @@ +package bson + +import ( + "math/big" + "reflect" + "testing" + + "github.com/keep-network/paillier" +) + +var pdzkp = &paillier.PartialDecryptionZKP{ + PartialDecryption: paillier.PartialDecryption{ + Id: 1, + Decryption: b(171), + }, + Key: &paillier.ThresholdPublicKey{ + PublicKey: paillier.PublicKey{ + N: (b(345)), + }, + TotalNumberOfDecryptionServers: 7, + Threshold: 98, + V: b(101), + Vi: []*big.Int{b(77), b(67)}, + }, + E: b(112), + Z: b(88), + C: b(99), +} + +func TestPdzkpBsonSerialization(t *testing.T) { + serialized, err := SerializePartialDecryptionZKP(pdzkp) + if err != nil { + t.Fatal(err) + } + + deserialized, err := DeserializePartialDecryptionZKP(serialized) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(pdzkp, deserialized) { + t.Errorf( + "Unexpected serialization result\nActual: %v\nExpected: %v\n", + deserialized, + pdzkp, + ) + } +} + +func TestPdzkpJsonSerialization(t *testing.T) { + serialized, err := JsonSerializePartialDecryptionZKP(pdzkp) + if err != nil { + t.Fatal(err) + } + + deserialized, err := JsonDeserializePartialDecryptionZKP(serialized) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(pdzkp, deserialized) { + t.Errorf( + "Unexpected serialization result\nActual: %v\nExpected: %v\n", + deserialized, + pdzkp, + ) + } +} diff --git a/bson/encoding_privatekey.go b/bson/encoding_privatekey.go new file mode 100644 index 0000000..5e81996 --- /dev/null +++ b/bson/encoding_privatekey.go @@ -0,0 +1,79 @@ +// Package bson provides functions for serialization and deserialization between +// paillier objects and BSON +package bson + +import ( + "fmt" + + "github.com/keep-network/paillier" + "gopkg.in/mgo.v2/bson" +) + +type PrivateKey paillier.PrivateKey + +// Serializes PrivateKey to BSON +func SerializePrivateKey(key *paillier.PrivateKey) ([]byte, error) { + return bson.Marshal(toSerializablePrivateKey(key)) +} + +// Deserializes BSON to PrivateKey +func DeserializePrivateKey(data []byte) (*paillier.PrivateKey, error) { + serializable := new(PrivateKey) + if err := bson.Unmarshal(data, serializable); err != nil { + return nil, err + } + + return toOriginalPrivateKey(serializable), nil +} + +// Changes original PrivateKey to serializable PrivateKey +func toSerializablePrivateKey(key *paillier.PrivateKey) *PrivateKey { + serializable := PrivateKey(*key) + return &serializable +} + +// Changes serializable PrivateKey to original PrivateKey +func toOriginalPrivateKey(serializable *PrivateKey) *paillier.PrivateKey { + original := paillier.PrivateKey(*serializable) + return &original +} + +type dbPrivateKey struct { + N string `bson:",omitempty"` + Lambda string `bson:",omitempty"` + Mu string `bson:",omitempty"` +} + +func (privateKey *PrivateKey) GetBSON() (interface{}, error) { + m := make(map[string]string) + + if privateKey.N != nil { + m["n"] = fmt.Sprintf("%x", privateKey.N) + } + if privateKey.Lambda != nil { + m["lambda"] = fmt.Sprintf("%x", privateKey.Lambda) + } + return m, nil +} + +func (privateKey *PrivateKey) SetBSON(raw bson.Raw) error { + var err error = nil + c := new(dbPrivateKey) + raw.Unmarshal(c) + + if c.N != "" { + privateKey.N, err = fromHex(c.N) + if err != nil { + return err + } + } + + if c.Lambda != "" { + privateKey.Lambda, err = fromHex(c.Lambda) + if err != nil { + return err + } + } + + return err +} diff --git a/bson/encoding_privatekey_test.go b/bson/encoding_privatekey_test.go new file mode 100644 index 0000000..9c777a7 --- /dev/null +++ b/bson/encoding_privatekey_test.go @@ -0,0 +1,35 @@ +package bson + +import ( + "reflect" + "testing" + + "github.com/keep-network/paillier" +) + +func TestSetPrivateKeyBson(t *testing.T) { + key := &paillier.PrivateKey{ + PublicKey: paillier.PublicKey{ + N: (b(345)), + }, + Lambda: b(5), + } + + serialized, err := SerializePrivateKey(key) + if err != nil { + t.Fatal(err) + } + + deserialized, err := DeserializePrivateKey(serialized) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(key, deserialized) { + t.Errorf( + "Unexpected serialization result\nActual: %v\nExpected: %v\n", + deserialized, + key, + ) + } +} diff --git a/bson/encoding_publickey.go b/bson/encoding_publickey.go new file mode 100644 index 0000000..a44f623 --- /dev/null +++ b/bson/encoding_publickey.go @@ -0,0 +1,64 @@ +// Package bson provides functions for serialization and deserialization between +// paillier objects and BSON +package bson + +import ( + "fmt" + + "github.com/keep-network/paillier" + "gopkg.in/mgo.v2/bson" +) + +type PublicKey paillier.PublicKey + +// Serializes PublicKey to BSON +func SerializePublicKey(publicKey *paillier.PublicKey) ([]byte, error) { + return bson.Marshal(toSerializablePublicKey(publicKey)) +} + +// Deserializes BSON to PublicKey +func DeserializePublicKey(data []byte) (*paillier.PublicKey, error) { + serializable := new(PublicKey) + if err := bson.Unmarshal(data, serializable); err != nil { + return nil, err + } + + return toOriginalPublicKey(serializable), nil +} + +// Changes original PublicKey to serializable PublicKey +func toSerializablePublicKey(publicKey *paillier.PublicKey) *PublicKey { + serializable := PublicKey(*publicKey) + return &serializable +} + +// Changes serializable PublicKey to original PublicKey +func toOriginalPublicKey(serializable *PublicKey) *paillier.PublicKey { + original := paillier.PublicKey(*serializable) + return &original +} + +type dbPublicKey struct { + N string `bson:",omitempty"` +} + +func (publicKey *PublicKey) GetBSON() (interface{}, error) { + m := make(map[string]string) + m["n"] = fmt.Sprintf("%x", publicKey.N) + return m, nil +} + +func (publicKey *PublicKey) SetBSON(raw bson.Raw) error { + var err error = nil + c := new(dbPublicKey) + raw.Unmarshal(c) + + if c.N != "" { + publicKey.N, err = fromHex(c.N) + if err != nil { + return err + } + } + + return err +} diff --git a/bson/encoding_publickey_test.go b/bson/encoding_publickey_test.go new file mode 100644 index 0000000..b777e2b --- /dev/null +++ b/bson/encoding_publickey_test.go @@ -0,0 +1,32 @@ +package bson + +import ( + "reflect" + "testing" + + "github.com/keep-network/paillier" +) + +func TestSetPublicKeyBson(t *testing.T) { + key := &paillier.PublicKey{ + N: (b(345)), + } + + serialized, err := SerializePublicKey(key) + if err != nil { + t.Fatal(err) + } + + deserialized, err := DeserializePublicKey(serialized) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(key, deserialized) { + t.Errorf( + "Unexpected serialization result\nActual: %v\nExpected: %v\n", + deserialized, + key, + ) + } +} diff --git a/bson/encoding_test.go b/bson/encoding_test.go deleted file mode 100644 index 27ad355..0000000 --- a/bson/encoding_test.go +++ /dev/null @@ -1,134 +0,0 @@ -package bson - -import ( - "crypto/rand" - "encoding/json" - "math/big" - "reflect" - "testing" - - "github.com/keep-network/paillier" - "gopkg.in/mgo.v2/bson" -) - -func AssertBSONIsGood(object, dump interface{}, t *testing.T) { - data, err := bson.Marshal(object) - if err != nil { - t.Error(err) - return - } - if err = bson.Unmarshal(data, dump); err != nil { - t.Error(err) - return - } - if !reflect.DeepEqual(object, dump) { - t.Errorf("Object: %v doesn't equal dump: %v", object, dump) - } -} - -func AssertJSONIsGood(object, dump interface{}, t *testing.T) { - data, err := json.Marshal(object) - if err != nil { - t.Error(err) - return - } - if err = json.Unmarshal(data, dump); err != nil { - t.Error(err) - return - } - if !reflect.DeepEqual(object, dump) { - t.Errorf("Object: %v doesn't equal dump: %v", object, dump) - } -} - -func TestCypherGetBSON(t *testing.T) { - key := paillier.CreatePrivateKey(big.NewInt(101), big.NewInt(113)) - cypherOriginal, err := key.Encrypt(big.NewInt(100), rand.Reader) - - cypher := new(Cypher) - cypher.C = cypherOriginal.C - - data, err := bson.Marshal(cypher) - if err != nil { - t.Error(err) - return - } - cypher2 := new(Cypher) - if err = bson.Unmarshal(data, cypher2); err != nil { - t.Error(err) - return - } - if !reflect.DeepEqual(cypher, cypher2) { - t.Errorf("%v not equal %v", cypher, cypher2) - } -} - -func TestThresholdKeyBSON(t *testing.T) { - key := &ThresholdPublicKey{ - PublicKey: paillier.PublicKey{b(9)}, - TotalNumberOfDecryptionServers: 7, - Threshold: 6, - V: b(3), - Vi: []*big.Int{b(2), b(34)}, - } - AssertBSONIsGood(key, new(ThresholdPublicKey), t) -} - -func TestSetPrivateKeyBson(t *testing.T) { - var key = new(PrivateKey) - var keyOriginal = paillier.CreatePrivateKey(big.NewInt(101), big.NewInt(113)) - - key.PublicKey = keyOriginal.PublicKey - key.Lambda = keyOriginal.Lambda - - dump := new(PrivateKey) - - AssertBSONIsGood(key, dump, t) -} - -func TestGetBSONEmptyKey(t *testing.T) { - key := new(PrivateKey) - m, err := key.GetBSON() - if err != nil { - t.Error(err) - } - if !reflect.DeepEqual(make(map[string]string), m) { - t.Error(m) - } -} - -func TestPartialDecryptionZKPJsonification(t *testing.T) { - pd := new(PartialDecryptionZKP) - pd.Key = new(paillier.ThresholdPublicKey) - pd.Key.Threshold = 98 - pd.Key.TotalNumberOfDecryptionServers = 230 - pd.Id = 1 - pd.Key.Vi = []*big.Int{b(77), b(67)} - pd.Key.N = b(131) - pd.Key.V = b(101) - pd.Decryption = b(171) - pd.E = b(112) - pd.C = b(99) - pd.Key.N = b(345) - pd.Z = b(88) - - AssertJSONIsGood(pd, new(PartialDecryptionZKP), t) -} - -func TestPartialDecryptionZKPBSONification(t *testing.T) { - pd := new(PartialDecryptionZKP) - pd.Key = new(paillier.ThresholdPublicKey) - pd.Key.Threshold = 98 - pd.Key.TotalNumberOfDecryptionServers = 230 - pd.Id = 1 - pd.Key.Vi = []*big.Int{b(77), b(67)} // vi is 67 - pd.Key.N = b(131) - pd.Key.V = b(101) - pd.Decryption = b(171) - pd.E = b(112) - pd.C = b(99) - pd.Key.N = b(345) - pd.Z = b(88) - - AssertBSONIsGood(pd, new(PartialDecryptionZKP), t) -} diff --git a/bson/encoding_thresholdpublickey.go b/bson/encoding_thresholdpublickey.go new file mode 100644 index 0000000..b2317db --- /dev/null +++ b/bson/encoding_thresholdpublickey.go @@ -0,0 +1,95 @@ +// Package bson provides functions for serialization and deserialization between +// paillier objects and BSON +package bson + +import ( + "errors" + "fmt" + "math/big" + + "github.com/keep-network/paillier" + "gopkg.in/mgo.v2/bson" +) + +type ThresholdPublicKey paillier.ThresholdPublicKey + +// Serializes ThresholdPublicKey to BSON +func SerializeThresholdPublicKey(key *paillier.ThresholdPublicKey) ([]byte, error) { + return bson.Marshal(toSerializableThresholdPublicKey(key)) +} + +// Deserializes BSON to ThresholdPublicKey +func DeserializeThresholdPublicKey(data []byte) (*paillier.ThresholdPublicKey, error) { + serializable := new(ThresholdPublicKey) + if err := bson.Unmarshal(data, serializable); err != nil { + return nil, err + } + + return toOriginalThresholdPublicKey(serializable), nil +} + +// Changes original ThresholdPublicKey to serializable ThresholdPublicKey +func toSerializableThresholdPublicKey(key *paillier.ThresholdPublicKey) *ThresholdPublicKey { + serializable := ThresholdPublicKey(*key) + return &serializable +} + +// Changes serializable ThresholdPublicKey to original ThresholdPublicKey +func toOriginalThresholdPublicKey(serializable *ThresholdPublicKey) *paillier.ThresholdPublicKey { + original := paillier.ThresholdPublicKey(*serializable) + return &original +} + +func (thresholdPublicKey *ThresholdPublicKey) GetBSON() (interface{}, error) { + r := new(dbThresholdKey) + r.fromThresholdPublicKey(thresholdPublicKey) + return r, nil +} + +func (thresholdPublicKey *ThresholdPublicKey) SetBSON(raw bson.Raw) error { + r := new(dbThresholdKey) + if err := raw.Unmarshal(r); err != nil { + return err + } + return r.toThresholdPublicKey(thresholdPublicKey) +} + +type dbThresholdKey struct { + TotalNumberOfDecryptionServers int + Threshold int + V string + Vi []string + N string +} + +func (dbThresholdKey *dbThresholdKey) fromThresholdPublicKey(key *ThresholdPublicKey) { + dbThresholdKey.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers + dbThresholdKey.Threshold = key.Threshold + dbThresholdKey.V = fmt.Sprintf("%x", key.V) + dbThresholdKey.N = fmt.Sprintf("%x", key.N) + dbThresholdKey.Vi = make([]string, len(key.Vi)) + for i, vi := range key.Vi { + dbThresholdKey.Vi[i] = fmt.Sprintf("%x", vi) + } +} + +func (dbThresholdKey *dbThresholdKey) toThresholdPublicKey(key *ThresholdPublicKey) error { + key.TotalNumberOfDecryptionServers = dbThresholdKey.TotalNumberOfDecryptionServers + key.Threshold = dbThresholdKey.Threshold + oks := make([]bool, 2) + key.V, oks[0] = new(big.Int).SetString(dbThresholdKey.V, 16) + key.N, oks[1] = new(big.Int).SetString(dbThresholdKey.N, 16) + if !all(oks) { + return errors.New("not hexadecimal") + } + key.Vi = make([]*big.Int, len(dbThresholdKey.Vi)) + var ok bool + for i, vi := range dbThresholdKey.Vi { + key.Vi[i], ok = new(big.Int).SetString(vi, 16) + if !ok { + return errors.New("not hexadecimal") + } + + } + return nil +} diff --git a/bson/encoding_thresholdpublickey_test.go b/bson/encoding_thresholdpublickey_test.go new file mode 100644 index 0000000..c5fbe74 --- /dev/null +++ b/bson/encoding_thresholdpublickey_test.go @@ -0,0 +1,37 @@ +package bson + +import ( + "math/big" + "reflect" + "testing" + + "github.com/keep-network/paillier" +) + +func TestThresholdKeyBSON(t *testing.T) { + key := &paillier.ThresholdPublicKey{ + PublicKey: paillier.PublicKey{b(9)}, + TotalNumberOfDecryptionServers: 7, + Threshold: 6, + V: b(3), + Vi: []*big.Int{b(2), b(34)}, + } + + serialized, err := SerializeThresholdPublicKey(key) + if err != nil { + t.Fatal(err) + } + + deserialized, err := DeserializeThresholdPublicKey(serialized) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(key, deserialized) { + t.Errorf( + "Unexpected serialization result\nActual: %v\nExpected: %v\n", + deserialized, + key, + ) + } +} -- 2.55.0 From d331cd9df5d1b50dbb7dc4c24ee8ed4464b77bf2 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Wed, 20 Jun 2018 12:39:42 +0200 Subject: [PATCH 5/5] Changed Serializable types names, updates tests names --- bson/encoding_cypher.go | 18 +++++++----------- bson/encoding_partialdecryptionzkp.go | 24 ++++++++++-------------- bson/encoding_privatekey.go | 18 +++++++----------- bson/encoding_privatekey_test.go | 2 +- bson/encoding_publickey.go | 18 +++++++----------- bson/encoding_publickey_test.go | 2 +- bson/encoding_thresholdpublickey.go | 22 +++++++++------------- bson/encoding_thresholdpublickey_test.go | 2 +- 8 files changed, 43 insertions(+), 63 deletions(-) diff --git a/bson/encoding_cypher.go b/bson/encoding_cypher.go index 7452abe..aaaeeff 100644 --- a/bson/encoding_cypher.go +++ b/bson/encoding_cypher.go @@ -1,5 +1,3 @@ -// Package bson provides functions for serialization and deserialization between -// paillier objects and BSON package bson import ( @@ -11,7 +9,7 @@ import ( "gopkg.in/mgo.v2/bson" ) -type Cypher paillier.Cypher +type SerializableCypher paillier.Cypher // Serializes Cypher to BSON func SerializeCypher(cypher *paillier.Cypher) ([]byte, error) { @@ -20,7 +18,7 @@ func SerializeCypher(cypher *paillier.Cypher) ([]byte, error) { // Deserializes BSON to Cypher func DeserializeCypher(data []byte) (*paillier.Cypher, error) { - serializable := new(Cypher) + serializable := new(SerializableCypher) if err := bson.Unmarshal(data, serializable); err != nil { return nil, err } @@ -28,14 +26,12 @@ func DeserializeCypher(data []byte) (*paillier.Cypher, error) { return toOriginalCypher(serializable), nil } -// Changes original Cypher to serializable Cypher -func toSerializableCypher(cypher *paillier.Cypher) *Cypher { - serializable := Cypher(*cypher) +func toSerializableCypher(cypher *paillier.Cypher) *SerializableCypher { + serializable := SerializableCypher(*cypher) return &serializable } -// Changes serializable Cypher to original Cypher -func toOriginalCypher(serializable *Cypher) *paillier.Cypher { +func toOriginalCypher(serializable *SerializableCypher) *paillier.Cypher { original := paillier.Cypher(*serializable) return &original } @@ -44,11 +40,11 @@ type dbCypher struct { C string } -func (cypher *Cypher) GetBSON() (interface{}, error) { +func (cypher *SerializableCypher) GetBSON() (interface{}, error) { return &dbCypher{fmt.Sprintf("%x", cypher.C)}, nil } -func (cypher *Cypher) SetBSON(raw bson.Raw) error { +func (cypher *SerializableCypher) SetBSON(raw bson.Raw) error { c := dbCypher{} if err := raw.Unmarshal(&c); err != nil { return err diff --git a/bson/encoding_partialdecryptionzkp.go b/bson/encoding_partialdecryptionzkp.go index d592945..606f7ad 100644 --- a/bson/encoding_partialdecryptionzkp.go +++ b/bson/encoding_partialdecryptionzkp.go @@ -1,5 +1,3 @@ -// Package bson provides functions for serialization and deserialization between -// paillier objects and BSON package bson import ( @@ -12,7 +10,7 @@ import ( "gopkg.in/mgo.v2/bson" ) -type PartialDecryptionZKP paillier.PartialDecryptionZKP +type SerializablePartialDecryptionZKP paillier.PartialDecryptionZKP // Serializes PartialDecryptionZKP to BSON func SerializePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) ([]byte, error) { @@ -21,7 +19,7 @@ func SerializePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) ([]byte // Deserializes BSON to PartialDecryptionZKP func DeserializePartialDecryptionZKP(data []byte) (*paillier.PartialDecryptionZKP, error) { - serializable := new(PartialDecryptionZKP) + serializable := new(SerializablePartialDecryptionZKP) if err := bson.Unmarshal(data, serializable); err != nil { return nil, err } @@ -36,7 +34,7 @@ func JsonSerializePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) ([] // Deserializes JSON to PartialDecryptionZKP func JsonDeserializePartialDecryptionZKP(data []byte) (*paillier.PartialDecryptionZKP, error) { - serializable := new(PartialDecryptionZKP) + serializable := new(SerializablePartialDecryptionZKP) if err := json.Unmarshal(data, serializable); err != nil { return nil, err } @@ -44,14 +42,12 @@ func JsonDeserializePartialDecryptionZKP(data []byte) (*paillier.PartialDecrypti return toOriginalPartialDecryptionZKP(serializable), nil } -// Changes original PartialDecryptionZKP to serializable PartialDecryptionZKP -func toSerializablePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) *PartialDecryptionZKP { - serializable := PartialDecryptionZKP(*pdzkp) +func toSerializablePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) *SerializablePartialDecryptionZKP { + serializable := SerializablePartialDecryptionZKP(*pdzkp) return &serializable } -// Changes serializable PartialDecryptionZKP to original PartialDecryptionZKP -func toOriginalPartialDecryptionZKP(serializable *PartialDecryptionZKP) *paillier.PartialDecryptionZKP { +func toOriginalPartialDecryptionZKP(serializable *SerializablePartialDecryptionZKP) *paillier.PartialDecryptionZKP { original := paillier.PartialDecryptionZKP(*serializable) return &original } @@ -69,13 +65,13 @@ type dbPartialDecryptionZKP struct { Threshold int `json:"threshold"` } -func (pdzkp *PartialDecryptionZKP) GetBSON() (interface{}, error) { +func (pdzkp *SerializablePartialDecryptionZKP) GetBSON() (interface{}, error) { db := new(dbPartialDecryptionZKP) db.fromPartialDecryptionZKP(pdzkp) return db, nil } -func (pdzkp *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { +func (pdzkp *SerializablePartialDecryptionZKP) SetBSON(raw bson.Raw) error { db := new(dbPartialDecryptionZKP) if err := raw.Unmarshal(db); err != nil { return err @@ -83,7 +79,7 @@ func (pdzkp *PartialDecryptionZKP) SetBSON(raw bson.Raw) error { return db.toPartialDecryptionZKP(pdzkp) } -func (dbPDZKP *dbPartialDecryptionZKP) fromPartialDecryptionZKP(pdzkp *PartialDecryptionZKP) { +func (dbPDZKP *dbPartialDecryptionZKP) fromPartialDecryptionZKP(pdzkp *SerializablePartialDecryptionZKP) { dbPDZKP.Id = pdzkp.Id dbPDZKP.TotalNumberOfDecryptionServers = pdzkp.Key.TotalNumberOfDecryptionServers dbPDZKP.Threshold = pdzkp.Key.Threshold @@ -99,7 +95,7 @@ func (dbPDZKP *dbPartialDecryptionZKP) fromPartialDecryptionZKP(pdzkp *PartialDe } } -func (dbPDZKP *dbPartialDecryptionZKP) toPartialDecryptionZKP(pdzkp *PartialDecryptionZKP) error { +func (dbPDZKP *dbPartialDecryptionZKP) toPartialDecryptionZKP(pdzkp *SerializablePartialDecryptionZKP) error { pdzkp.Key = new(paillier.ThresholdPublicKey) var oks = make([]bool, 6) diff --git a/bson/encoding_privatekey.go b/bson/encoding_privatekey.go index 5e81996..f1cca88 100644 --- a/bson/encoding_privatekey.go +++ b/bson/encoding_privatekey.go @@ -1,5 +1,3 @@ -// Package bson provides functions for serialization and deserialization between -// paillier objects and BSON package bson import ( @@ -9,7 +7,7 @@ import ( "gopkg.in/mgo.v2/bson" ) -type PrivateKey paillier.PrivateKey +type SerializablePrivateKey paillier.PrivateKey // Serializes PrivateKey to BSON func SerializePrivateKey(key *paillier.PrivateKey) ([]byte, error) { @@ -18,7 +16,7 @@ func SerializePrivateKey(key *paillier.PrivateKey) ([]byte, error) { // Deserializes BSON to PrivateKey func DeserializePrivateKey(data []byte) (*paillier.PrivateKey, error) { - serializable := new(PrivateKey) + serializable := new(SerializablePrivateKey) if err := bson.Unmarshal(data, serializable); err != nil { return nil, err } @@ -26,14 +24,12 @@ func DeserializePrivateKey(data []byte) (*paillier.PrivateKey, error) { return toOriginalPrivateKey(serializable), nil } -// Changes original PrivateKey to serializable PrivateKey -func toSerializablePrivateKey(key *paillier.PrivateKey) *PrivateKey { - serializable := PrivateKey(*key) +func toSerializablePrivateKey(key *paillier.PrivateKey) *SerializablePrivateKey { + serializable := SerializablePrivateKey(*key) return &serializable } -// Changes serializable PrivateKey to original PrivateKey -func toOriginalPrivateKey(serializable *PrivateKey) *paillier.PrivateKey { +func toOriginalPrivateKey(serializable *SerializablePrivateKey) *paillier.PrivateKey { original := paillier.PrivateKey(*serializable) return &original } @@ -44,7 +40,7 @@ type dbPrivateKey struct { Mu string `bson:",omitempty"` } -func (privateKey *PrivateKey) GetBSON() (interface{}, error) { +func (privateKey *SerializablePrivateKey) GetBSON() (interface{}, error) { m := make(map[string]string) if privateKey.N != nil { @@ -56,7 +52,7 @@ func (privateKey *PrivateKey) GetBSON() (interface{}, error) { return m, nil } -func (privateKey *PrivateKey) SetBSON(raw bson.Raw) error { +func (privateKey *SerializablePrivateKey) SetBSON(raw bson.Raw) error { var err error = nil c := new(dbPrivateKey) raw.Unmarshal(c) diff --git a/bson/encoding_privatekey_test.go b/bson/encoding_privatekey_test.go index 9c777a7..8870bcc 100644 --- a/bson/encoding_privatekey_test.go +++ b/bson/encoding_privatekey_test.go @@ -7,7 +7,7 @@ import ( "github.com/keep-network/paillier" ) -func TestSetPrivateKeyBson(t *testing.T) { +func TestPrivateKeyBsonSerialization(t *testing.T) { key := &paillier.PrivateKey{ PublicKey: paillier.PublicKey{ N: (b(345)), diff --git a/bson/encoding_publickey.go b/bson/encoding_publickey.go index a44f623..1d4b930 100644 --- a/bson/encoding_publickey.go +++ b/bson/encoding_publickey.go @@ -1,5 +1,3 @@ -// Package bson provides functions for serialization and deserialization between -// paillier objects and BSON package bson import ( @@ -9,7 +7,7 @@ import ( "gopkg.in/mgo.v2/bson" ) -type PublicKey paillier.PublicKey +type SerializablePublicKey paillier.PublicKey // Serializes PublicKey to BSON func SerializePublicKey(publicKey *paillier.PublicKey) ([]byte, error) { @@ -18,7 +16,7 @@ func SerializePublicKey(publicKey *paillier.PublicKey) ([]byte, error) { // Deserializes BSON to PublicKey func DeserializePublicKey(data []byte) (*paillier.PublicKey, error) { - serializable := new(PublicKey) + serializable := new(SerializablePublicKey) if err := bson.Unmarshal(data, serializable); err != nil { return nil, err } @@ -26,14 +24,12 @@ func DeserializePublicKey(data []byte) (*paillier.PublicKey, error) { return toOriginalPublicKey(serializable), nil } -// Changes original PublicKey to serializable PublicKey -func toSerializablePublicKey(publicKey *paillier.PublicKey) *PublicKey { - serializable := PublicKey(*publicKey) +func toSerializablePublicKey(publicKey *paillier.PublicKey) *SerializablePublicKey { + serializable := SerializablePublicKey(*publicKey) return &serializable } -// Changes serializable PublicKey to original PublicKey -func toOriginalPublicKey(serializable *PublicKey) *paillier.PublicKey { +func toOriginalPublicKey(serializable *SerializablePublicKey) *paillier.PublicKey { original := paillier.PublicKey(*serializable) return &original } @@ -42,13 +38,13 @@ type dbPublicKey struct { N string `bson:",omitempty"` } -func (publicKey *PublicKey) GetBSON() (interface{}, error) { +func (publicKey *SerializablePublicKey) GetBSON() (interface{}, error) { m := make(map[string]string) m["n"] = fmt.Sprintf("%x", publicKey.N) return m, nil } -func (publicKey *PublicKey) SetBSON(raw bson.Raw) error { +func (publicKey *SerializablePublicKey) SetBSON(raw bson.Raw) error { var err error = nil c := new(dbPublicKey) raw.Unmarshal(c) diff --git a/bson/encoding_publickey_test.go b/bson/encoding_publickey_test.go index b777e2b..130e22b 100644 --- a/bson/encoding_publickey_test.go +++ b/bson/encoding_publickey_test.go @@ -7,7 +7,7 @@ import ( "github.com/keep-network/paillier" ) -func TestSetPublicKeyBson(t *testing.T) { +func TestPublicKeyBsonSerialization(t *testing.T) { key := &paillier.PublicKey{ N: (b(345)), } diff --git a/bson/encoding_thresholdpublickey.go b/bson/encoding_thresholdpublickey.go index b2317db..5af5c89 100644 --- a/bson/encoding_thresholdpublickey.go +++ b/bson/encoding_thresholdpublickey.go @@ -1,5 +1,3 @@ -// Package bson provides functions for serialization and deserialization between -// paillier objects and BSON package bson import ( @@ -11,7 +9,7 @@ import ( "gopkg.in/mgo.v2/bson" ) -type ThresholdPublicKey paillier.ThresholdPublicKey +type SerializableThresholdPublicKey paillier.ThresholdPublicKey // Serializes ThresholdPublicKey to BSON func SerializeThresholdPublicKey(key *paillier.ThresholdPublicKey) ([]byte, error) { @@ -20,7 +18,7 @@ func SerializeThresholdPublicKey(key *paillier.ThresholdPublicKey) ([]byte, erro // Deserializes BSON to ThresholdPublicKey func DeserializeThresholdPublicKey(data []byte) (*paillier.ThresholdPublicKey, error) { - serializable := new(ThresholdPublicKey) + serializable := new(SerializableThresholdPublicKey) if err := bson.Unmarshal(data, serializable); err != nil { return nil, err } @@ -28,25 +26,23 @@ func DeserializeThresholdPublicKey(data []byte) (*paillier.ThresholdPublicKey, e return toOriginalThresholdPublicKey(serializable), nil } -// Changes original ThresholdPublicKey to serializable ThresholdPublicKey -func toSerializableThresholdPublicKey(key *paillier.ThresholdPublicKey) *ThresholdPublicKey { - serializable := ThresholdPublicKey(*key) +func toSerializableThresholdPublicKey(key *paillier.ThresholdPublicKey) *SerializableThresholdPublicKey { + serializable := SerializableThresholdPublicKey(*key) return &serializable } -// Changes serializable ThresholdPublicKey to original ThresholdPublicKey -func toOriginalThresholdPublicKey(serializable *ThresholdPublicKey) *paillier.ThresholdPublicKey { +func toOriginalThresholdPublicKey(serializable *SerializableThresholdPublicKey) *paillier.ThresholdPublicKey { original := paillier.ThresholdPublicKey(*serializable) return &original } -func (thresholdPublicKey *ThresholdPublicKey) GetBSON() (interface{}, error) { +func (thresholdPublicKey *SerializableThresholdPublicKey) GetBSON() (interface{}, error) { r := new(dbThresholdKey) r.fromThresholdPublicKey(thresholdPublicKey) return r, nil } -func (thresholdPublicKey *ThresholdPublicKey) SetBSON(raw bson.Raw) error { +func (thresholdPublicKey *SerializableThresholdPublicKey) SetBSON(raw bson.Raw) error { r := new(dbThresholdKey) if err := raw.Unmarshal(r); err != nil { return err @@ -62,7 +58,7 @@ type dbThresholdKey struct { N string } -func (dbThresholdKey *dbThresholdKey) fromThresholdPublicKey(key *ThresholdPublicKey) { +func (dbThresholdKey *dbThresholdKey) fromThresholdPublicKey(key *SerializableThresholdPublicKey) { dbThresholdKey.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers dbThresholdKey.Threshold = key.Threshold dbThresholdKey.V = fmt.Sprintf("%x", key.V) @@ -73,7 +69,7 @@ func (dbThresholdKey *dbThresholdKey) fromThresholdPublicKey(key *ThresholdPubli } } -func (dbThresholdKey *dbThresholdKey) toThresholdPublicKey(key *ThresholdPublicKey) error { +func (dbThresholdKey *dbThresholdKey) toThresholdPublicKey(key *SerializableThresholdPublicKey) error { key.TotalNumberOfDecryptionServers = dbThresholdKey.TotalNumberOfDecryptionServers key.Threshold = dbThresholdKey.Threshold oks := make([]bool, 2) diff --git a/bson/encoding_thresholdpublickey_test.go b/bson/encoding_thresholdpublickey_test.go index c5fbe74..93d8fe3 100644 --- a/bson/encoding_thresholdpublickey_test.go +++ b/bson/encoding_thresholdpublickey_test.go @@ -8,7 +8,7 @@ import ( "github.com/keep-network/paillier" ) -func TestThresholdKeyBSON(t *testing.T) { +func TestThresholdKeySerialization(t *testing.T) { key := &paillier.ThresholdPublicKey{ PublicKey: paillier.PublicKey{b(9)}, TotalNumberOfDecryptionServers: 7, -- 2.55.0