Merge pull request #4 from keep-network/master

Extract BSON/JSON code to separate package
This commit was merged in pull request #4.
This commit is contained in:
didier amyot authored and GitHub committed 2018-06-23 09:11:19 -04:00
commit d3037179a5
14 files changed
+646 -369

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
package bson
import (
"errors"
"fmt"
"math/big"
"github.com/keep-network/paillier"
"gopkg.in/mgo.v2/bson"
)
type SerializableCypher 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(SerializableCypher)
if err := bson.Unmarshal(data, serializable); err != nil {
return nil, err
}
return toOriginalCypher(serializable), nil
}
func toSerializableCypher(cypher *paillier.Cypher) *SerializableCypher {
serializable := SerializableCypher(*cypher)
return &serializable
}
func toOriginalCypher(serializable *SerializableCypher) *paillier.Cypher {
original := paillier.Cypher(*serializable)
return &original
}
type dbCypher struct {
C string
}
func (cypher *SerializableCypher) GetBSON() (interface{}, error) {
return &dbCypher{fmt.Sprintf("%x", cypher.C)}, nil
}
func (cypher *SerializableCypher) 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
}
+32
View File
@@ -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,
)
}
}
+129
View File
@@ -0,0 +1,129 @@
package bson
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"github.com/keep-network/paillier"
"gopkg.in/mgo.v2/bson"
)
type SerializablePartialDecryptionZKP 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(SerializablePartialDecryptionZKP)
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(SerializablePartialDecryptionZKP)
if err := json.Unmarshal(data, serializable); err != nil {
return nil, err
}
return toOriginalPartialDecryptionZKP(serializable), nil
}
func toSerializablePartialDecryptionZKP(pdzkp *paillier.PartialDecryptionZKP) *SerializablePartialDecryptionZKP {
serializable := SerializablePartialDecryptionZKP(*pdzkp)
return &serializable
}
func toOriginalPartialDecryptionZKP(serializable *SerializablePartialDecryptionZKP) *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 *SerializablePartialDecryptionZKP) GetBSON() (interface{}, error) {
db := new(dbPartialDecryptionZKP)
db.fromPartialDecryptionZKP(pdzkp)
return db, nil
}
func (pdzkp *SerializablePartialDecryptionZKP) 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 *SerializablePartialDecryptionZKP) {
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 *SerializablePartialDecryptionZKP) 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
}
@@ -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,
)
}
}
+75
View File
@@ -0,0 +1,75 @@
package bson
import (
"fmt"
"github.com/keep-network/paillier"
"gopkg.in/mgo.v2/bson"
)
type SerializablePrivateKey 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(SerializablePrivateKey)
if err := bson.Unmarshal(data, serializable); err != nil {
return nil, err
}
return toOriginalPrivateKey(serializable), nil
}
func toSerializablePrivateKey(key *paillier.PrivateKey) *SerializablePrivateKey {
serializable := SerializablePrivateKey(*key)
return &serializable
}
func toOriginalPrivateKey(serializable *SerializablePrivateKey) *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 *SerializablePrivateKey) 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 *SerializablePrivateKey) 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
}
+35
View File
@@ -0,0 +1,35 @@
package bson
import (
"reflect"
"testing"
"github.com/keep-network/paillier"
)
func TestPrivateKeyBsonSerialization(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,
)
}
}
+60
View File
@@ -0,0 +1,60 @@
package bson
import (
"fmt"
"github.com/keep-network/paillier"
"gopkg.in/mgo.v2/bson"
)
type SerializablePublicKey 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(SerializablePublicKey)
if err := bson.Unmarshal(data, serializable); err != nil {
return nil, err
}
return toOriginalPublicKey(serializable), nil
}
func toSerializablePublicKey(publicKey *paillier.PublicKey) *SerializablePublicKey {
serializable := SerializablePublicKey(*publicKey)
return &serializable
}
func toOriginalPublicKey(serializable *SerializablePublicKey) *paillier.PublicKey {
original := paillier.PublicKey(*serializable)
return &original
}
type dbPublicKey struct {
N string `bson:",omitempty"`
}
func (publicKey *SerializablePublicKey) GetBSON() (interface{}, error) {
m := make(map[string]string)
m["n"] = fmt.Sprintf("%x", publicKey.N)
return m, nil
}
func (publicKey *SerializablePublicKey) 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
}
+32
View File
@@ -0,0 +1,32 @@
package bson
import (
"reflect"
"testing"
"github.com/keep-network/paillier"
)
func TestPublicKeyBsonSerialization(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,
)
}
}
+91
View File
@@ -0,0 +1,91 @@
package bson
import (
"errors"
"fmt"
"math/big"
"github.com/keep-network/paillier"
"gopkg.in/mgo.v2/bson"
)
type SerializableThresholdPublicKey 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(SerializableThresholdPublicKey)
if err := bson.Unmarshal(data, serializable); err != nil {
return nil, err
}
return toOriginalThresholdPublicKey(serializable), nil
}
func toSerializableThresholdPublicKey(key *paillier.ThresholdPublicKey) *SerializableThresholdPublicKey {
serializable := SerializableThresholdPublicKey(*key)
return &serializable
}
func toOriginalThresholdPublicKey(serializable *SerializableThresholdPublicKey) *paillier.ThresholdPublicKey {
original := paillier.ThresholdPublicKey(*serializable)
return &original
}
func (thresholdPublicKey *SerializableThresholdPublicKey) GetBSON() (interface{}, error) {
r := new(dbThresholdKey)
r.fromThresholdPublicKey(thresholdPublicKey)
return r, nil
}
func (thresholdPublicKey *SerializableThresholdPublicKey) 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 *SerializableThresholdPublicKey) {
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 *SerializableThresholdPublicKey) 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
}
+37
View File
@@ -0,0 +1,37 @@
package bson
import (
"math/big"
"reflect"
"testing"
"github.com/keep-network/paillier"
)
func TestThresholdKeySerialization(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,
)
}
}
+29
View File
@@ -0,0 +1,29 @@
package bson
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
}
func b(i int) *big.Int {
return big.NewInt(int64(i))
}
-236
View File
@@ -1,236 +0,0 @@
package paillier
// all the methods relative to the set/get json/bson should be stored
// in this file.
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"gopkg.in/mgo.v2/bson"
)
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)
}
-127
View File
@@ -1,127 +0,0 @@
package paillier
import (
"crypto/rand"
"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.Fail()
}
}
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.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))
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)
}
-6
View File
@@ -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)
}