Moved bson to separate package
This commit is contained in:
7 files changed
+646
-262
No files matched your search
@@ -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)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package bson2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
func b(i int) *big.Int {
|
||||||
|
return big.NewInt(int64(i))
|
||||||
|
}
|
||||||
+177
-180
@@ -4,12 +4,9 @@ package paillier
|
|||||||
// in this file.
|
// in this file.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"gopkg.in/mgo.v2/bson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func fromHex(hex string) (*big.Int, error) {
|
func fromHex(hex string) (*big.Int, error) {
|
||||||
@@ -30,207 +27,207 @@ func all(oks []bool) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
type dbCypher struct {
|
// type dbCypher struct {
|
||||||
C string
|
// C string
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *Cypher) GetBSON() (interface{}, error) {
|
// func (this *Cypher) GetBSON() (interface{}, error) {
|
||||||
return &dbCypher{fmt.Sprintf("%x", this.C)}, nil
|
// return &dbCypher{fmt.Sprintf("%x", this.C)}, nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (cypher *Cypher) SetBSON(raw bson.Raw) error {
|
// func (cypher *Cypher) SetBSON(raw bson.Raw) error {
|
||||||
c := dbCypher{}
|
// c := dbCypher{}
|
||||||
if err := raw.Unmarshal(&c); err != nil {
|
// if err := raw.Unmarshal(&c); err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
var ok bool
|
// var ok bool
|
||||||
cypher.C, ok = new(big.Int).SetString(c.C, 16)
|
// cypher.C, ok = new(big.Int).SetString(c.C, 16)
|
||||||
if !ok {
|
// if !ok {
|
||||||
return errors.New("big int not in hexadecimal format")
|
// return errors.New("big int not in hexadecimal format")
|
||||||
}
|
// }
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
type dbThresholdKey struct {
|
// type dbThresholdKey struct {
|
||||||
TotalNumberOfDecryptionServers int
|
// TotalNumberOfDecryptionServers int
|
||||||
Threshold int
|
// Threshold int
|
||||||
V string
|
// V string
|
||||||
Vi []string
|
// Vi []string
|
||||||
N string
|
// N string
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *dbThresholdKey) FromThresholdPublicKey(key *ThresholdPublicKey) {
|
// func (this *dbThresholdKey) FromThresholdPublicKey(key *ThresholdPublicKey) {
|
||||||
this.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers
|
// this.TotalNumberOfDecryptionServers = key.TotalNumberOfDecryptionServers
|
||||||
this.Threshold = key.Threshold
|
// this.Threshold = key.Threshold
|
||||||
this.V = fmt.Sprintf("%x", key.V)
|
// this.V = fmt.Sprintf("%x", key.V)
|
||||||
this.N = fmt.Sprintf("%x", key.N)
|
// this.N = fmt.Sprintf("%x", key.N)
|
||||||
this.Vi = make([]string, len(key.Vi))
|
// this.Vi = make([]string, len(key.Vi))
|
||||||
for i, vi := range key.Vi {
|
// for i, vi := range key.Vi {
|
||||||
this.Vi[i] = fmt.Sprintf("%x", vi)
|
// this.Vi[i] = fmt.Sprintf("%x", vi)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *dbThresholdKey) ToThresholdPublicKey(key *ThresholdPublicKey) error {
|
// func (this *dbThresholdKey) ToThresholdPublicKey(key *ThresholdPublicKey) error {
|
||||||
key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers
|
// key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers
|
||||||
key.Threshold = this.Threshold
|
// key.Threshold = this.Threshold
|
||||||
oks := make([]bool, 2)
|
// oks := make([]bool, 2)
|
||||||
key.V, oks[0] = new(big.Int).SetString(this.V, 16)
|
// key.V, oks[0] = new(big.Int).SetString(this.V, 16)
|
||||||
key.N, oks[1] = new(big.Int).SetString(this.N, 16)
|
// key.N, oks[1] = new(big.Int).SetString(this.N, 16)
|
||||||
if !all(oks) {
|
// if !all(oks) {
|
||||||
return errors.New("not hexadecimal")
|
// return errors.New("not hexadecimal")
|
||||||
}
|
// }
|
||||||
key.Vi = make([]*big.Int, len(this.Vi))
|
// key.Vi = make([]*big.Int, len(this.Vi))
|
||||||
var ok bool
|
// var ok bool
|
||||||
for i, vi := range this.Vi {
|
// for i, vi := range this.Vi {
|
||||||
key.Vi[i], ok = new(big.Int).SetString(vi, 16)
|
// key.Vi[i], ok = new(big.Int).SetString(vi, 16)
|
||||||
if !ok {
|
// if !ok {
|
||||||
return errors.New("not hexadecimal")
|
// return errors.New("not hexadecimal")
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
// }
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *ThresholdPublicKey) GetBSON() (interface{}, error) {
|
// func (this *ThresholdPublicKey) GetBSON() (interface{}, error) {
|
||||||
r := new(dbThresholdKey)
|
// r := new(dbThresholdKey)
|
||||||
r.FromThresholdPublicKey(this)
|
// r.FromThresholdPublicKey(this)
|
||||||
return r, nil
|
// return r, nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *ThresholdPublicKey) SetBSON(raw bson.Raw) error {
|
// func (this *ThresholdPublicKey) SetBSON(raw bson.Raw) error {
|
||||||
r := new(dbThresholdKey)
|
// r := new(dbThresholdKey)
|
||||||
if err := raw.Unmarshal(r); err != nil {
|
// if err := raw.Unmarshal(r); err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
return r.ToThresholdPublicKey(this)
|
// return r.ToThresholdPublicKey(this)
|
||||||
}
|
// }
|
||||||
|
|
||||||
type dbPrivateKey struct {
|
// type dbPrivateKey struct {
|
||||||
N string `bson:",omitempty"`
|
// N string `bson:",omitempty"`
|
||||||
Lambda string `bson:",omitempty"`
|
// Lambda string `bson:",omitempty"`
|
||||||
Mu string `bson:",omitempty"`
|
// Mu string `bson:",omitempty"`
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *PrivateKey) GetBSON() (interface{}, error) {
|
// func (this *PrivateKey) GetBSON() (interface{}, error) {
|
||||||
m := make(map[string]string)
|
// m := make(map[string]string)
|
||||||
|
|
||||||
if this.N != nil {
|
// if this.N != nil {
|
||||||
m["n"] = fmt.Sprintf("%x", this.N)
|
// m["n"] = fmt.Sprintf("%x", this.N)
|
||||||
}
|
// }
|
||||||
if this.Lambda != nil {
|
// if this.Lambda != nil {
|
||||||
m["lambda"] = fmt.Sprintf("%x", this.Lambda)
|
// m["lambda"] = fmt.Sprintf("%x", this.Lambda)
|
||||||
}
|
// }
|
||||||
return m, nil
|
// return m, nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *PrivateKey) SetBSON(raw bson.Raw) error {
|
// func (this *PrivateKey) SetBSON(raw bson.Raw) error {
|
||||||
var err error = nil
|
// var err error = nil
|
||||||
c := new(dbPrivateKey)
|
// c := new(dbPrivateKey)
|
||||||
raw.Unmarshal(c)
|
// raw.Unmarshal(c)
|
||||||
|
|
||||||
if c.N != "" {
|
// if c.N != "" {
|
||||||
this.N, err = fromHex(c.N)
|
// this.N, err = fromHex(c.N)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
if c.Lambda != "" {
|
// if c.Lambda != "" {
|
||||||
this.Lambda, err = fromHex(c.Lambda)
|
// this.Lambda, err = fromHex(c.Lambda)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
type dbPartialDecryptionZKP struct {
|
// type dbPartialDecryptionZKP struct {
|
||||||
Z string `json:"z"`
|
// Z string `json:"z"`
|
||||||
E string `json:"e"`
|
// E string `json:"e"`
|
||||||
C string `json:"c"`
|
// C string `json:"c"`
|
||||||
V string `json:"v"`
|
// V string `json:"v"`
|
||||||
N string `json:"n"`
|
// N string `json:"n"`
|
||||||
Vi []string `json:"vi"`
|
// Vi []string `json:"vi"`
|
||||||
Decryption string `json:"decryption"`
|
// Decryption string `json:"decryption"`
|
||||||
Id int `json:"id"`
|
// Id int `json:"id"`
|
||||||
TotalNumberOfDecryptionServers int `json:"total_number_of_decryption_servers"`
|
// TotalNumberOfDecryptionServers int `json:"total_number_of_decryption_servers"`
|
||||||
Threshold int `json:"threshold"`
|
// Threshold int `json:"threshold"`
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) {
|
// func (this *dbPartialDecryptionZKP) FromPartialDecryptionZKP(pd *PartialDecryptionZKP) {
|
||||||
this.Id = pd.Id
|
// this.Id = pd.Id
|
||||||
this.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers
|
// this.TotalNumberOfDecryptionServers = pd.Key.TotalNumberOfDecryptionServers
|
||||||
this.Threshold = pd.Key.Threshold
|
// this.Threshold = pd.Key.Threshold
|
||||||
this.Z = fmt.Sprintf("%x", pd.Z)
|
// this.Z = fmt.Sprintf("%x", pd.Z)
|
||||||
this.E = fmt.Sprintf("%x", pd.E)
|
// this.E = fmt.Sprintf("%x", pd.E)
|
||||||
this.N = fmt.Sprintf("%x", pd.Key.N)
|
// this.N = fmt.Sprintf("%x", pd.Key.N)
|
||||||
this.C = fmt.Sprintf("%x", pd.C)
|
// this.C = fmt.Sprintf("%x", pd.C)
|
||||||
this.V = fmt.Sprintf("%x", pd.Key.V)
|
// this.V = fmt.Sprintf("%x", pd.Key.V)
|
||||||
this.Decryption = fmt.Sprintf("%x", pd.Decryption)
|
// this.Decryption = fmt.Sprintf("%x", pd.Decryption)
|
||||||
this.Vi = make([]string, len(pd.Key.Vi))
|
// this.Vi = make([]string, len(pd.Key.Vi))
|
||||||
for i, vi := range pd.Key.Vi {
|
// for i, vi := range pd.Key.Vi {
|
||||||
this.Vi[i] = fmt.Sprintf("%x", vi)
|
// this.Vi[i] = fmt.Sprintf("%x", vi)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error {
|
// func (this *dbPartialDecryptionZKP) ToPartialDecryptionZKP(pd *PartialDecryptionZKP) error {
|
||||||
pd.Key = new(ThresholdPublicKey)
|
// pd.Key = new(ThresholdPublicKey)
|
||||||
|
|
||||||
var oks = make([]bool, 6)
|
// var oks = make([]bool, 6)
|
||||||
|
|
||||||
pd.Id = this.Id
|
// pd.Id = this.Id
|
||||||
pd.Key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers
|
// pd.Key.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers
|
||||||
pd.Key.Threshold = this.Threshold
|
// pd.Key.Threshold = this.Threshold
|
||||||
pd.Z, oks[0] = new(big.Int).SetString(this.Z, 16)
|
// pd.Z, oks[0] = new(big.Int).SetString(this.Z, 16)
|
||||||
pd.E, oks[1] = new(big.Int).SetString(this.E, 16)
|
// pd.E, oks[1] = new(big.Int).SetString(this.E, 16)
|
||||||
pd.C, oks[2] = new(big.Int).SetString(this.C, 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.V, oks[3] = new(big.Int).SetString(this.V, 16)
|
||||||
pd.Key.N, oks[4] = new(big.Int).SetString(this.N, 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.Decryption, oks[5] = new(big.Int).SetString(this.Decryption, 16)
|
||||||
|
|
||||||
if !all(oks) {
|
// if !all(oks) {
|
||||||
fmt.Println(oks)
|
// fmt.Println(oks)
|
||||||
fmt.Println(this.E, "|", this.Z, "|", this.C, "|", this.V, "|", this.Decryption)
|
// fmt.Println(this.E, "|", this.Z, "|", this.C, "|", this.V, "|", this.Decryption)
|
||||||
return errors.New("numbers not in hexadecimal format")
|
// return errors.New("numbers not in hexadecimal format")
|
||||||
}
|
// }
|
||||||
|
|
||||||
pd.Key.Vi = make([]*big.Int, len(this.Vi))
|
// pd.Key.Vi = make([]*big.Int, len(this.Vi))
|
||||||
var ok bool
|
// var ok bool
|
||||||
for i, vi := range this.Vi {
|
// for i, vi := range this.Vi {
|
||||||
pd.Key.Vi[i], ok = new(big.Int).SetString(vi, 16)
|
// pd.Key.Vi[i], ok = new(big.Int).SetString(vi, 16)
|
||||||
if !ok {
|
// if !ok {
|
||||||
return errors.New("numbers not in hexadecimal format")
|
// return errors.New("numbers not in hexadecimal format")
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *PartialDecryptionZKP) MarshalJSON() ([]byte, error) {
|
// func (this *PartialDecryptionZKP) MarshalJSON() ([]byte, error) {
|
||||||
db := new(dbPartialDecryptionZKP)
|
// db := new(dbPartialDecryptionZKP)
|
||||||
db.FromPartialDecryptionZKP(this)
|
// db.FromPartialDecryptionZKP(this)
|
||||||
return json.Marshal(db)
|
// return json.Marshal(db)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *PartialDecryptionZKP) UnmarshalJSON(data []byte) error {
|
// func (this *PartialDecryptionZKP) UnmarshalJSON(data []byte) error {
|
||||||
db := new(dbPartialDecryptionZKP)
|
// db := new(dbPartialDecryptionZKP)
|
||||||
if err := json.Unmarshal(data, db); err != nil {
|
// if err := json.Unmarshal(data, db); err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
return db.ToPartialDecryptionZKP(this)
|
// return db.ToPartialDecryptionZKP(this)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *PartialDecryptionZKP) GetBSON() (interface{}, error) {
|
// func (this *PartialDecryptionZKP) GetBSON() (interface{}, error) {
|
||||||
db := new(dbPartialDecryptionZKP)
|
// db := new(dbPartialDecryptionZKP)
|
||||||
db.FromPartialDecryptionZKP(this)
|
// db.FromPartialDecryptionZKP(this)
|
||||||
return db, nil
|
// return db, nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (this *PartialDecryptionZKP) SetBSON(raw bson.Raw) error {
|
// func (this *PartialDecryptionZKP) SetBSON(raw bson.Raw) error {
|
||||||
db := new(dbPartialDecryptionZKP)
|
// db := new(dbPartialDecryptionZKP)
|
||||||
if err := raw.Unmarshal(db); err != nil {
|
// if err := raw.Unmarshal(db); err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
return db.ToPartialDecryptionZKP(this)
|
// return db.ToPartialDecryptionZKP(this)
|
||||||
}
|
// }
|
||||||
+75
-76
@@ -1,7 +1,6 @@
|
|||||||
package paillier
|
package paillier
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -21,7 +20,7 @@ func AssertBSONIsGood(object, dump interface{}, t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(object, dump) {
|
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
|
return
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(object, dump) {
|
if !reflect.DeepEqual(object, dump) {
|
||||||
t.Fail()
|
t.Errorf("%v not equal %v", object, dump)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCypherGetBSON(t *testing.T) {
|
// func TestCypherGetBSON(t *testing.T) {
|
||||||
key := CreatePrivateKey(big.NewInt(101), big.NewInt(113))
|
// key := CreatePrivateKey(big.NewInt(101), big.NewInt(113))
|
||||||
cypher, err := key.Encrypt(big.NewInt(100), rand.Reader)
|
// cypher, err := key.Encrypt(big.NewInt(100), rand.Reader)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Error(err)
|
// t.Error(err)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
data, err := bson.Marshal(cypher)
|
// data, err := bson.Marshal(cypher)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Error(err)
|
// t.Error(err)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
cypher2 := new(Cypher)
|
// cypher2 := new(Cypher)
|
||||||
if err = bson.Unmarshal(data, cypher2); err != nil {
|
// if err = bson.Unmarshal(data, cypher2); err != nil {
|
||||||
t.Error(err)
|
// t.Error(err)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
if !reflect.DeepEqual(cypher, cypher2) {
|
// if !reflect.DeepEqual(cypher, cypher2) {
|
||||||
t.Fail()
|
// t.Fail()
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func TestSetPrivateKeyBson(t *testing.T) {
|
func TestSetPrivateKeyBson(t *testing.T) {
|
||||||
key := CreatePrivateKey(big.NewInt(101), big.NewInt(113))
|
key := CreatePrivateKey(big.NewInt(101), big.NewInt(113))
|
||||||
@@ -68,60 +67,60 @@ func TestSetPrivateKeyBson(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetBSONEmptyKey(t *testing.T) {
|
// func TestGetBSONEmptyKey(t *testing.T) {
|
||||||
key := new(PrivateKey)
|
// key := new(PrivateKey)
|
||||||
m, err := key.GetBSON()
|
// m, err := key.GetBSON()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Error(err)
|
// t.Error(err)
|
||||||
}
|
// }
|
||||||
if !reflect.DeepEqual(make(map[string]string), m) {
|
// if !reflect.DeepEqual(make(map[string]string), m) {
|
||||||
t.Error(m)
|
// t.Error(m)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func TestPartialDecryptionZKPJsonification(t *testing.T) {
|
// func TestPartialDecryptionZKPJsonification(t *testing.T) {
|
||||||
pd := new(PartialDecryptionZKP)
|
// pd := new(PartialDecryptionZKP)
|
||||||
pd.Key = new(ThresholdPublicKey)
|
// pd.Key = new(ThresholdPublicKey)
|
||||||
pd.Key.Threshold = 98
|
// pd.Key.Threshold = 98
|
||||||
pd.Key.TotalNumberOfDecryptionServers = 230
|
// pd.Key.TotalNumberOfDecryptionServers = 230
|
||||||
pd.Id = 1
|
// pd.Id = 1
|
||||||
pd.Key.Vi = []*big.Int{b(77), b(67)}
|
// pd.Key.Vi = []*big.Int{b(77), b(67)}
|
||||||
pd.Key.N = b(131)
|
// pd.Key.N = b(131)
|
||||||
pd.Key.V = b(101)
|
// pd.Key.V = b(101)
|
||||||
pd.Decryption = b(171)
|
// pd.Decryption = b(171)
|
||||||
pd.E = b(112)
|
// pd.E = b(112)
|
||||||
pd.C = b(99)
|
// pd.C = b(99)
|
||||||
pd.Key.N = b(345)
|
// pd.Key.N = b(345)
|
||||||
pd.Z = b(88)
|
// pd.Z = b(88)
|
||||||
|
|
||||||
AssertJSONIsGood(pd, new(PartialDecryptionZKP), t)
|
// AssertJSONIsGood(pd, new(PartialDecryptionZKP), t)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func TestPartialDecryptionZKPBSONification(t *testing.T) {
|
// func TestPartialDecryptionZKPBSONification(t *testing.T) {
|
||||||
pd := new(PartialDecryptionZKP)
|
// pd := new(PartialDecryptionZKP)
|
||||||
pd.Key = new(ThresholdPublicKey)
|
// pd.Key = new(ThresholdPublicKey)
|
||||||
pd.Key.Threshold = 98
|
// pd.Key.Threshold = 98
|
||||||
pd.Key.TotalNumberOfDecryptionServers = 230
|
// pd.Key.TotalNumberOfDecryptionServers = 230
|
||||||
pd.Id = 1
|
// pd.Id = 1
|
||||||
pd.Key.Vi = []*big.Int{b(77), b(67)} // vi is 67
|
// pd.Key.Vi = []*big.Int{b(77), b(67)} // vi is 67
|
||||||
pd.Key.N = b(131)
|
// pd.Key.N = b(131)
|
||||||
pd.Key.V = b(101)
|
// pd.Key.V = b(101)
|
||||||
pd.Decryption = b(171)
|
// pd.Decryption = b(171)
|
||||||
pd.E = b(112)
|
// pd.E = b(112)
|
||||||
pd.C = b(99)
|
// pd.C = b(99)
|
||||||
pd.Key.N = b(345)
|
// pd.Key.N = b(345)
|
||||||
pd.Z = b(88)
|
// pd.Z = b(88)
|
||||||
|
|
||||||
AssertBSONIsGood(pd, new(PartialDecryptionZKP), t)
|
// AssertBSONIsGood(pd, new(PartialDecryptionZKP), t)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func TestThresholdKeyBSON(t *testing.T) {
|
// func TestThresholdKeyBSON(t *testing.T) {
|
||||||
key := &ThresholdPublicKey{
|
// key := &ThresholdPublicKey{
|
||||||
PublicKey: PublicKey{b(9)},
|
// PublicKey: PublicKey{b(9)},
|
||||||
TotalNumberOfDecryptionServers: 7,
|
// TotalNumberOfDecryptionServers: 7,
|
||||||
Threshold: 6,
|
// Threshold: 6,
|
||||||
V: b(3),
|
// V: b(3),
|
||||||
Vi: []*big.Int{b(2), b(34)},
|
// Vi: []*big.Int{b(2), b(34)},
|
||||||
}
|
// }
|
||||||
AssertBSONIsGood(key, new(ThresholdPublicKey), t)
|
// AssertBSONIsGood(key, new(ThresholdPublicKey), t)
|
||||||
}
|
// }
|
||||||
@@ -10,12 +10,6 @@ type PublicKey struct {
|
|||||||
N *big.Int
|
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 {
|
func (this *PublicKey) GetNSquare() *big.Int {
|
||||||
return new(big.Int).Mul(this.N, this.N)
|
return new(big.Int).Mul(this.N, this.N)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user