From d038e085a3b1c29046d34a5c7133ffdfd7cff312 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Wed, 13 Jun 2018 11:27:43 -0400 Subject: [PATCH] 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) -// }