Fix spelling/naming issues, slightly more robust testing of encryption and decryption. #12
No files matched your search
+22
-22
@@ -7,65 +7,65 @@ import (
|
|||||||
"gopkg.in/mgo.v2/bson"
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SerializablePrivateKey paillier.PrivateKey
|
type SerializableSecretKey paillier.SecretKey
|
||||||
|
|
||||||
// Serializes PrivateKey to BSON
|
// Serializes SecretKey to BSON
|
||||||
func SerializePrivateKey(key *paillier.PrivateKey) ([]byte, error) {
|
func SerializeSecretKey(key *paillier.SecretKey) ([]byte, error) {
|
||||||
return bson.Marshal(toSerializablePrivateKey(key))
|
return bson.Marshal(toSerializableSecretKey(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deserializes BSON to PrivateKey
|
// Deserializes BSON to SecretKey
|
||||||
func DeserializePrivateKey(data []byte) (*paillier.PrivateKey, error) {
|
func DeserializeSecretKey(data []byte) (*paillier.SecretKey, error) {
|
||||||
serializable := new(SerializablePrivateKey)
|
serializable := new(SerializableSecretKey)
|
||||||
if err := bson.Unmarshal(data, serializable); err != nil {
|
if err := bson.Unmarshal(data, serializable); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return toOriginalPrivateKey(serializable), nil
|
return toOriginalSecretKey(serializable), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toSerializablePrivateKey(key *paillier.PrivateKey) *SerializablePrivateKey {
|
func toSerializableSecretKey(key *paillier.SecretKey) *SerializableSecretKey {
|
||||||
serializable := SerializablePrivateKey(*key)
|
serializable := SerializableSecretKey(*key)
|
||||||
return &serializable
|
return &serializable
|
||||||
}
|
}
|
||||||
|
|
||||||
func toOriginalPrivateKey(serializable *SerializablePrivateKey) *paillier.PrivateKey {
|
func toOriginalSecretKey(serializable *SerializableSecretKey) *paillier.SecretKey {
|
||||||
original := paillier.PrivateKey(*serializable)
|
original := paillier.SecretKey(*serializable)
|
||||||
return &original
|
return &original
|
||||||
}
|
}
|
||||||
|
|
||||||
type dbPrivateKey struct {
|
type dbSecretKey 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 (privateKey *SerializablePrivateKey) GetBSON() (interface{}, error) {
|
func (SecretKey *SerializableSecretKey) GetBSON() (interface{}, error) {
|
||||||
m := make(map[string]string)
|
m := make(map[string]string)
|
||||||
|
|
||||||
if privateKey.N != nil {
|
if SecretKey.N != nil {
|
||||||
m["n"] = fmt.Sprintf("%x", privateKey.N)
|
m["n"] = fmt.Sprintf("%x", SecretKey.N)
|
||||||
}
|
}
|
||||||
if privateKey.Lambda != nil {
|
if SecretKey.Lambda != nil {
|
||||||
m["lambda"] = fmt.Sprintf("%x", privateKey.Lambda)
|
m["lambda"] = fmt.Sprintf("%x", SecretKey.Lambda)
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (privateKey *SerializablePrivateKey) SetBSON(raw bson.Raw) error {
|
func (SecretKey *SerializableSecretKey) SetBSON(raw bson.Raw) error {
|
||||||
var err error = nil
|
var err error = nil
|
||||||
c := new(dbPrivateKey)
|
c := new(dbSecretKey)
|
||||||
raw.Unmarshal(c)
|
raw.Unmarshal(c)
|
||||||
|
|
||||||
if c.N != "" {
|
if c.N != "" {
|
||||||
privateKey.N, err = fromHex(c.N)
|
SecretKey.N, err = fromHex(c.N)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Lambda != "" {
|
if c.Lambda != "" {
|
||||||
privateKey.Lambda, err = fromHex(c.Lambda)
|
SecretKey.Lambda, err = fromHex(c.Lambda)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,20 +7,20 @@ import (
|
|||||||
"github.com/keep-network/paillier"
|
"github.com/keep-network/paillier"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPrivateKeyBsonSerialization(t *testing.T) {
|
func TestSecretKeyBsonSerialization(t *testing.T) {
|
||||||
key := &paillier.PrivateKey{
|
key := &paillier.SecretKey{
|
||||||
PublicKey: paillier.PublicKey{
|
PublicKey: paillier.PublicKey{
|
||||||
N: (b(345)),
|
N: (b(345)),
|
||||||
},
|
},
|
||||||
Lambda: b(5),
|
Lambda: b(5),
|
||||||
}
|
}
|
||||||
|
|
||||||
serialized, err := SerializePrivateKey(key)
|
serialized, err := SerializeSecretKey(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
deserialized, err := DeserializePrivateKey(serialized)
|
deserialized, err := DeserializeSecretKey(serialized)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -102,7 +102,7 @@ func (pk *PublicKey) Mul(cypher *Cypher, scalar *big.Int) *Cypher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type PrivateKey struct {
|
type SecretKey struct {
|
||||||
PublicKey
|
PublicKey
|
||||||
Lambda *big.Int
|
Lambda *big.Int
|
||||||
}
|
}
|
||||||
@@ -115,7 +115,7 @@ type PrivateKey struct {
|
|||||||
// D(c) = [ ((c^lambda) mod N^2) - 1) / N ] lambda^-1 mod N
|
// D(c) = [ ((c^lambda) mod N^2) - 1) / N ] lambda^-1 mod N
|
||||||
//
|
//
|
||||||
// See [KL 08] construction 11.32, page 414.
|
// See [KL 08] construction 11.32, page 414.
|
||||||
func (priv *PrivateKey) Decrypt(cypher *Cypher) (msg *big.Int) {
|
func (priv *SecretKey) Decrypt(cypher *Cypher) (msg *big.Int) {
|
||||||
mu := new(big.Int).ModInverse(priv.Lambda, priv.N)
|
mu := new(big.Int).ModInverse(priv.Lambda, priv.N)
|
||||||
tmp := new(big.Int).Exp(cypher.C, priv.Lambda, priv.GetNSquare())
|
tmp := new(big.Int).Exp(cypher.C, priv.Lambda, priv.GetNSquare())
|
||||||
msg = new(big.Int).Mod(new(big.Int).Mul(L(tmp, priv.N), mu), priv.N)
|
msg = new(big.Int).Mod(new(big.Int).Mul(L(tmp, priv.N), mu), priv.N)
|
||||||
@@ -143,7 +143,7 @@ func computePhi(p, q *big.Int) *big.Int {
|
|||||||
return new(big.Int).Mul(minusOne(p), minusOne(q))
|
return new(big.Int).Mul(minusOne(p), minusOne(q))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreatePrivateKey generates a Paillier private key accepting two large prime
|
// CreateSecretKey generates a Paillier private key accepting two large prime
|
||||||
// numbers of equal length or other such that gcd(pq, (p-1)(q-1)) = 1.
|
// numbers of equal length or other such that gcd(pq, (p-1)(q-1)) = 1.
|
||||||
//
|
//
|
||||||
// Algorithm is based on approach described in [KL 08], construction 11.32,
|
// Algorithm is based on approach described in [KL 08], construction 11.32,
|
||||||
@@ -159,11 +159,11 @@ func computePhi(p, q *big.Int) *big.Int {
|
|||||||
// A Generalization of Paillier’s Public-Key System
|
// A Generalization of Paillier’s Public-Key System
|
||||||
// with Applications to Electronic Voting
|
// with Applications to Electronic Voting
|
||||||
// Aarhus University, Dept. of Computer Science, BRICS
|
// Aarhus University, Dept. of Computer Science, BRICS
|
||||||
func CreatePrivateKey(p, q *big.Int) *PrivateKey {
|
func CreateSecretKey(p, q *big.Int) *SecretKey {
|
||||||
n := new(big.Int).Mul(p, q)
|
n := new(big.Int).Mul(p, q)
|
||||||
lambda := computePhi(p, q)
|
lambda := computePhi(p, q)
|
||||||
|
|
||||||
|
|
|||||||
return &PrivateKey{
|
return &SecretKey{
|
||||||
PublicKey: PublicKey{
|
PublicKey: PublicKey{
|
||||||
N: n,
|
N: n,
|
||||||
},
|
},
|
||||||
|
|||||||
+33
-33
@@ -32,18 +32,18 @@ func TestComputePhi(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreatePrivateKey(t *testing.T) {
|
func TestCreateSecretKey(t *testing.T) {
|
||||||
p := big.NewInt(463)
|
p := big.NewInt(463)
|
||||||
q := big.NewInt(631)
|
q := big.NewInt(631)
|
||||||
|
|
||||||
privateKey := CreatePrivateKey(p, q)
|
SecretKey := CreateSecretKey(p, q)
|
||||||
|
|
||||||
if privateKey.N.Cmp(big.NewInt(292153)) != 0 {
|
if SecretKey.N.Cmp(big.NewInt(292153)) != 0 {
|
||||||
t.Errorf("Unexpected N PublicKey value [%v]", privateKey.N)
|
t.Errorf("Unexpected N PublicKey value [%v]", SecretKey.N)
|
||||||
}
|
}
|
||||||
|
|
||||||
if privateKey.Lambda.Cmp(big.NewInt(291060)) != 0 {
|
if SecretKey.Lambda.Cmp(big.NewInt(291060)) != 0 {
|
||||||
t.Errorf("Unexpected Lambda Public key value [%v]", privateKey.Lambda)
|
t.Errorf("Unexpected Lambda Public key value [%v]", SecretKey.Lambda)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,14 +51,14 @@ func TestEncryptDecryptSmall(t *testing.T) {
|
|||||||
p := big.NewInt(13)
|
p := big.NewInt(13)
|
||||||
q := big.NewInt(11)
|
q := big.NewInt(11)
|
||||||
for i := 1; i < 10; i++ {
|
for i := 1; i < 10; i++ {
|
||||||
privateKey := CreatePrivateKey(p, q)
|
SecretKey := CreateSecretKey(p, q)
|
||||||
|
|
||||||
initialValue := big.NewInt(100)
|
initialValue := big.NewInt(100)
|
||||||
cypher, err := privateKey.Encrypt(initialValue, rand.Reader)
|
cypher, err := SecretKey.Encrypt(initialValue, rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
returnedValue := privateKey.Decrypt(cypher)
|
returnedValue := SecretKey.Decrypt(cypher)
|
||||||
if initialValue.Cmp(returnedValue) != 0 {
|
if initialValue.Cmp(returnedValue) != 0 {
|
||||||
t.Error("wrong decryption ", returnedValue, " is not ", initialValue)
|
t.Error("wrong decryption ", returnedValue, " is not ", initialValue)
|
||||||
}
|
}
|
||||||
@@ -70,7 +70,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
|
|||||||
q := big.NewInt(11)
|
q := big.NewInt(11)
|
||||||
|
|
||||||
// N = pq = 143 so the plaintext space is [0, 143)
|
// N = pq = 143 so the plaintext space is [0, 143)
|
||||||
privateKey := CreatePrivateKey(p, q)
|
SecretKey := CreateSecretKey(p, q)
|
||||||
|
|
||||||
var tests = map[string]struct {
|
var tests = map[string]struct {
|
||||||
plaintext *big.Int
|
plaintext *big.Int
|
||||||
@@ -101,7 +101,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
|
|||||||
|
|
||||||
for testName, test := range tests {
|
for testName, test := range tests {
|
||||||
t.Run(testName, func(t *testing.T) {
|
t.Run(testName, func(t *testing.T) {
|
||||||
cypher, err := privateKey.Encrypt(test.plaintext, rand.Reader)
|
cypher, err := SecretKey.Encrypt(test.plaintext, rand.Reader)
|
||||||
if !reflect.DeepEqual(err, test.expectedError) {
|
if !reflect.DeepEqual(err, test.expectedError) {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"Unexpected error\nExpected: %v\nActual: %v",
|
"Unexpected error\nExpected: %v\nActual: %v",
|
||||||
@@ -111,7 +111,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if test.expectedError == nil {
|
if test.expectedError == nil {
|
||||||
decrypted := privateKey.Decrypt(cypher)
|
decrypted := SecretKey.Decrypt(cypher)
|
||||||
if test.plaintext.Cmp(decrypted) != 0 {
|
if test.plaintext.Cmp(decrypted) != 0 {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"Unexpected decryption\nExpected: %v\nActual: %v",
|
"Unexpected decryption\nExpected: %v\nActual: %v",
|
||||||
@@ -125,44 +125,44 @@ func TestCheckPlaintextSpace(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddCyphers(t *testing.T) {
|
func TestAddCyphers(t *testing.T) {
|
||||||
privateKey := CreatePrivateKey(big.NewInt(17), big.NewInt(13))
|
SecretKey := CreateSecretKey(big.NewInt(17), big.NewInt(13))
|
||||||
|
|
||||||
cypher1, _ := privateKey.Encrypt(big.NewInt(5), rand.Reader)
|
cypher1, _ := SecretKey.Encrypt(big.NewInt(5), rand.Reader)
|
||||||
cypher2, _ := privateKey.Encrypt(big.NewInt(6), rand.Reader)
|
cypher2, _ := SecretKey.Encrypt(big.NewInt(6), rand.Reader)
|
||||||
cypher3, _ := privateKey.Encrypt(big.NewInt(7), rand.Reader)
|
cypher3, _ := SecretKey.Encrypt(big.NewInt(7), rand.Reader)
|
||||||
cypher4, _ := privateKey.Encrypt(big.NewInt(8), rand.Reader)
|
cypher4, _ := SecretKey.Encrypt(big.NewInt(8), rand.Reader)
|
||||||
cypher5 := privateKey.Add(cypher1, cypher2, cypher3, cypher4)
|
cypher5 := SecretKey.Add(cypher1, cypher2, cypher3, cypher4)
|
||||||
|
|
||||||
m := privateKey.Decrypt(cypher5)
|
m := SecretKey.Decrypt(cypher5)
|
||||||
if m.Cmp(big.NewInt(26)) != 0 {
|
if m.Cmp(big.NewInt(26)) != 0 {
|
||||||
t.Errorf("Unexpected decrypted value [%v]", m)
|
t.Errorf("Unexpected decrypted value [%v]", m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddCypherWithSmallKeyModulus(t *testing.T) {
|
func TestAddCypherWithSmallKeyModulus(t *testing.T) {
|
||||||
privateKey := CreatePrivateKey(big.NewInt(7), big.NewInt(5))
|
SecretKey := CreateSecretKey(big.NewInt(7), big.NewInt(5))
|
||||||
|
|
||||||
cypher1, _ := privateKey.Encrypt(big.NewInt(30), rand.Reader)
|
cypher1, _ := SecretKey.Encrypt(big.NewInt(30), rand.Reader)
|
||||||
cypher2, _ := privateKey.Encrypt(big.NewInt(25), rand.Reader)
|
cypher2, _ := SecretKey.Encrypt(big.NewInt(25), rand.Reader)
|
||||||
cypher3, _ := privateKey.Encrypt(big.NewInt(11), rand.Reader)
|
cypher3, _ := SecretKey.Encrypt(big.NewInt(11), rand.Reader)
|
||||||
cypher4 := privateKey.Add(cypher1, cypher2, cypher3)
|
cypher4 := SecretKey.Add(cypher1, cypher2, cypher3)
|
||||||
|
|
||||||
m := privateKey.Decrypt(cypher4)
|
m := SecretKey.Decrypt(cypher4)
|
||||||
if m.Cmp(big.NewInt(31)) != 0 {
|
if m.Cmp(big.NewInt(31)) != 0 {
|
||||||
t.Errorf("Unexpected decrypted value [%v]", m)
|
t.Errorf("Unexpected decrypted value [%v]", m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMulCypher(t *testing.T) {
|
func TestMulCypher(t *testing.T) {
|
||||||
privateKey := CreatePrivateKey(big.NewInt(17), big.NewInt(13))
|
SecretKey := CreateSecretKey(big.NewInt(17), big.NewInt(13))
|
||||||
|
|
||||||
cypher, err := privateKey.Encrypt(big.NewInt(3), rand.Reader)
|
cypher, err := SecretKey.Encrypt(big.NewInt(3), rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cypherMultiple := privateKey.Mul(cypher, big.NewInt(7))
|
cypherMultiple := SecretKey.Mul(cypher, big.NewInt(7))
|
||||||
multiple := privateKey.Decrypt(cypherMultiple)
|
multiple := SecretKey.Decrypt(cypherMultiple)
|
||||||
|
|
||||||
// 3 * 7 = 21
|
// 3 * 7 = 21
|
||||||
if multiple.Cmp(big.NewInt(21)) != 0 {
|
if multiple.Cmp(big.NewInt(21)) != 0 {
|
||||||
@@ -171,15 +171,15 @@ func TestMulCypher(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMulCypherWithSmallKeyModulus(t *testing.T) {
|
func TestMulCypherWithSmallKeyModulus(t *testing.T) {
|
||||||
privateKey := CreatePrivateKey(big.NewInt(7), big.NewInt(5))
|
SecretKey := CreateSecretKey(big.NewInt(7), big.NewInt(5))
|
||||||
|
|
||||||
cypher, err := privateKey.Encrypt(big.NewInt(30), rand.Reader)
|
cypher, err := SecretKey.Encrypt(big.NewInt(30), rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cypherMultiple := privateKey.Mul(cypher, big.NewInt(93))
|
cypherMultiple := SecretKey.Mul(cypher, big.NewInt(93))
|
||||||
multiple := privateKey.Decrypt(cypherMultiple)
|
multiple := SecretKey.Decrypt(cypherMultiple)
|
||||||
|
|
||||||
// (30*93) mod (7*5) = 25
|
// (30*93) mod (7*5) = 25
|
||||||
if multiple.Cmp(big.NewInt(25)) != 0 {
|
if multiple.Cmp(big.NewInt(25)) != 0 {
|
||||||
|
|||||||
+9
-9
@@ -175,14 +175,14 @@ func (tk *ThresholdPublicKey) VerifyDecryption(encryptedMessage, decryptedMessag
|
|||||||
// for the given decryption server.
|
// for the given decryption server.
|
||||||
// `Id` is the unique identifier of a decryption server and `Share` is a secret
|
// `Id` is the unique identifier of a decryption server and `Share` is a secret
|
||||||
// share generated from hiding polynomial and is used for a partial share decryption.
|
// share generated from hiding polynomial and is used for a partial share decryption.
|
||||||
type ThresholdPrivateKey struct {
|
type ThresholdSecretKey struct {
|
||||||
ThresholdPublicKey
|
ThresholdPublicKey
|
||||||
Id int
|
Id int
|
||||||
Share *big.Int
|
Share *big.Int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrypts the cypher text and returns the partial decryption
|
// Decrypts the cypher text and returns the partial decryption
|
||||||
func (tpk *ThresholdPrivateKey) Decrypt(c *big.Int) *PartialDecryption {
|
func (tpk *ThresholdSecretKey) Decrypt(c *big.Int) *PartialDecryption {
|
||||||
ret := new(PartialDecryption)
|
ret := new(PartialDecryption)
|
||||||
ret.Id = tpk.Id
|
ret.Id = tpk.Id
|
||||||
exp := new(big.Int).Mul(tpk.Share, new(big.Int).Mul(TWO, tpk.delta()))
|
exp := new(big.Int).Mul(tpk.Share, new(big.Int).Mul(TWO, tpk.delta()))
|
||||||
@@ -191,7 +191,7 @@ func (tpk *ThresholdPrivateKey) Decrypt(c *big.Int) *PartialDecryption {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tpk *ThresholdPrivateKey) copyVi() []*big.Int {
|
func (tpk *ThresholdSecretKey) copyVi() []*big.Int {
|
||||||
ret := make([]*big.Int, len(tpk.Vi))
|
ret := make([]*big.Int, len(tpk.Vi))
|
||||||
for i, vi := range tpk.Vi {
|
for i, vi := range tpk.Vi {
|
||||||
ret[i] = new(big.Int).Add(vi, big.NewInt(0))
|
ret[i] = new(big.Int).Add(vi, big.NewInt(0))
|
||||||
@@ -199,7 +199,7 @@ func (tpk *ThresholdPrivateKey) copyVi() []*big.Int {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tpk *ThresholdPrivateKey) getThresholdKey() *ThresholdPublicKey {
|
func (tpk *ThresholdSecretKey) getThresholdKey() *ThresholdPublicKey {
|
||||||
ret := new(ThresholdPublicKey)
|
ret := new(ThresholdPublicKey)
|
||||||
ret.Threshold = tpk.Threshold
|
ret.Threshold = tpk.Threshold
|
||||||
ret.TotalNumberOfDecryptionServers = tpk.TotalNumberOfDecryptionServers
|
ret.TotalNumberOfDecryptionServers = tpk.TotalNumberOfDecryptionServers
|
||||||
@@ -209,13 +209,13 @@ func (tpk *ThresholdPrivateKey) getThresholdKey() *ThresholdPublicKey {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tpk *ThresholdPrivateKey) computeZ(r, e *big.Int) *big.Int {
|
func (tpk *ThresholdSecretKey) computeZ(r, e *big.Int) *big.Int {
|
||||||
tmp := new(big.Int).Mul(e, tpk.delta())
|
tmp := new(big.Int).Mul(e, tpk.delta())
|
||||||
tmp = new(big.Int).Mul(tmp, tpk.Share)
|
tmp = new(big.Int).Mul(tmp, tpk.Share)
|
||||||
return new(big.Int).Add(r, tmp)
|
return new(big.Int).Add(r, tmp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tpk *ThresholdPrivateKey) computeHash(a, b, c4, ci2 *big.Int) *big.Int {
|
func (tpk *ThresholdSecretKey) computeHash(a, b, c4, ci2 *big.Int) *big.Int {
|
||||||
hash := sha256.New()
|
hash := sha256.New()
|
||||||
hash.Write(a.Bytes())
|
hash.Write(a.Bytes())
|
||||||
hash.Write(b.Bytes())
|
hash.Write(b.Bytes())
|
||||||
@@ -224,7 +224,7 @@ func (tpk *ThresholdPrivateKey) computeHash(a, b, c4, ci2 *big.Int) *big.Int {
|
|||||||
return new(big.Int).SetBytes(hash.Sum([]byte{}))
|
return new(big.Int).SetBytes(hash.Sum([]byte{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tpk *ThresholdPrivateKey) DecryptAndProduceZNP(c *big.Int, random io.Reader) (*PartialDecryptionZKP, error) {
|
func (tpk *ThresholdSecretKey) DecryptAndProduceZNP(c *big.Int, random io.Reader) (*PartialDecryptionZKP, error) {
|
||||||
pd := new(PartialDecryptionZKP)
|
pd := new(PartialDecryptionZKP)
|
||||||
pd.Key = tpk.getThresholdKey()
|
pd.Key = tpk.getThresholdKey()
|
||||||
pd.C = c
|
pd.C = c
|
||||||
@@ -255,7 +255,7 @@ func (tpk *ThresholdPrivateKey) DecryptAndProduceZNP(c *big.Int, random io.Reade
|
|||||||
|
|
||||||
// Verifies if the partial decryption key is well formed. If well formed,
|
// Verifies if the partial decryption key is well formed. If well formed,
|
||||||
// the method return nil else an explicative error is returned.
|
// the method return nil else an explicative error is returned.
|
||||||
func (tpk *ThresholdPrivateKey) Validate(random io.Reader) error {
|
func (tpk *ThresholdSecretKey) Validate(random io.Reader) error {
|
||||||
m, err := rand.Int(random, tpk.N)
|
m, err := rand.Int(random, tpk.N)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -281,7 +281,7 @@ type PartialDecryption struct {
|
|||||||
|
|
||||||
// A non-interactive ZKP based on the Fiat–Shamir heuristic. This algorithm
|
// A non-interactive ZKP based on the Fiat–Shamir heuristic. This algorithm
|
||||||
// proves that the decryption server indeed raised secret to his secret exponent
|
// proves that the decryption server indeed raised secret to his secret exponent
|
||||||
// (`ThresholdPrivateKey.Share`) by comparison with the public verification key
|
// (`ThresholdSecretKey.Share`) by comparison with the public verification key
|
||||||
// (`ThresholdKey.Vi`). Recall that v_i = v^(delta s_i).
|
// (`ThresholdKey.Vi`). Recall that v_i = v^(delta s_i).
|
||||||
//
|
//
|
||||||
// The Fiat-Shamir is a non-interactive proof of knowledge heuristic.
|
// The Fiat-Shamir is a non-interactive proof of knowledge heuristic.
|
||||||
|
|||||||
@@ -240,8 +240,8 @@ func (tkg *ThresholdKeyGenerator) createViArray(shares []*big.Int) (viArray []*b
|
|||||||
return viArray
|
return viArray
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tkg *ThresholdKeyGenerator) createPrivateKey(i int, share *big.Int, viArray []*big.Int) *ThresholdPrivateKey {
|
func (tkg *ThresholdKeyGenerator) createSecretKey(i int, share *big.Int, viArray []*big.Int) *ThresholdSecretKey {
|
||||||
ret := new(ThresholdPrivateKey)
|
ret := new(ThresholdSecretKey)
|
||||||
ret.N = tkg.n
|
ret.N = tkg.n
|
||||||
ret.V = tkg.v
|
ret.V = tkg.v
|
||||||
|
|
||||||
@@ -253,22 +253,22 @@ func (tkg *ThresholdKeyGenerator) createPrivateKey(i int, share *big.Int, viArra
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tkg *ThresholdKeyGenerator) createPrivateKeys() []*ThresholdPrivateKey {
|
func (tkg *ThresholdKeyGenerator) createSecretKeys() []*ThresholdSecretKey {
|
||||||
shares := tkg.createShares()
|
shares := tkg.createShares()
|
||||||
viArray := tkg.createViArray(shares)
|
viArray := tkg.createViArray(shares)
|
||||||
ret := make([]*ThresholdPrivateKey, tkg.TotalNumberOfDecryptionServers)
|
ret := make([]*ThresholdSecretKey, tkg.TotalNumberOfDecryptionServers)
|
||||||
for i := 0; i < tkg.TotalNumberOfDecryptionServers; i++ {
|
for i := 0; i < tkg.TotalNumberOfDecryptionServers; i++ {
|
||||||
ret[i] = tkg.createPrivateKey(i, shares[i], viArray)
|
ret[i] = tkg.createSecretKey(i, shares[i], viArray)
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tkg *ThresholdKeyGenerator) Generate() ([]*ThresholdPrivateKey, error) {
|
func (tkg *ThresholdKeyGenerator) Generate() ([]*ThresholdSecretKey, error) {
|
||||||
if err := tkg.initNumerialValues(); err != nil {
|
if err := tkg.initNumerialValues(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := tkg.generateHidingPolynomial(); err != nil {
|
if err := tkg.generateHidingPolynomial(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return tkg.createPrivateKeys(), nil
|
return tkg.createSecretKeys(), nil
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getThresholdPrivateKey() *ThresholdPrivateKey {
|
func getThresholdSecretKey() *ThresholdSecretKey {
|
||||||
tkh, err := GetThresholdKeyGenerator(32, 10, 6, rand.Reader)
|
tkh, err := GetThresholdKeyGenerator(32, 10, 6, rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -55,7 +55,7 @@ func TestCombineSharesConstant(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDecrypt(t *testing.T) {
|
func TestDecrypt(t *testing.T) {
|
||||||
key := new(ThresholdPrivateKey)
|
key := new(ThresholdSecretKey)
|
||||||
key.TotalNumberOfDecryptionServers = 10
|
key.TotalNumberOfDecryptionServers = 10
|
||||||
key.N = b(101 * 103)
|
key.N = b(101 * 103)
|
||||||
key.Share = b(862)
|
key.Share = b(862)
|
||||||
@@ -73,7 +73,7 @@ func TestDecrypt(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCopyVi(t *testing.T) {
|
func TestCopyVi(t *testing.T) {
|
||||||
key := new(ThresholdPrivateKey)
|
key := new(ThresholdSecretKey)
|
||||||
key.Vi = []*big.Int{b(34), b(2), b(29)}
|
key.Vi = []*big.Int{b(34), b(2), b(29)}
|
||||||
vi := key.copyVi()
|
vi := key.copyVi()
|
||||||
if !reflect.DeepEqual(vi, key.Vi) {
|
if !reflect.DeepEqual(vi, key.Vi) {
|
||||||
@@ -86,7 +86,7 @@ func TestCopyVi(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEncryptWithThresholdKey(t *testing.T) {
|
func TestEncryptWithThresholdKey(t *testing.T) {
|
||||||
pd := getThresholdPrivateKey()
|
pd := getThresholdSecretKey()
|
||||||
_, err := pd.Encrypt(big.NewInt(876), rand.Reader)
|
_, err := pd.Encrypt(big.NewInt(876), rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@@ -94,7 +94,7 @@ func TestEncryptWithThresholdKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDecryptWithThresholdKey(t *testing.T) {
|
func TestDecryptWithThresholdKey(t *testing.T) {
|
||||||
pd := getThresholdPrivateKey()
|
pd := getThresholdSecretKey()
|
||||||
c, err := pd.Encrypt(big.NewInt(876), rand.Reader)
|
c, err := pd.Encrypt(big.NewInt(876), rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@@ -131,7 +131,7 @@ func TestVerifyPart2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDecryptAndProduceZNP(t *testing.T) {
|
func TestDecryptAndProduceZNP(t *testing.T) {
|
||||||
pd := getThresholdPrivateKey()
|
pd := getThresholdSecretKey()
|
||||||
c, err := pd.Encrypt(big.NewInt(876), rand.Reader)
|
c, err := pd.Encrypt(big.NewInt(876), rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -283,7 +283,7 @@ func TestDecryption(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestValidate(t *testing.T) {
|
func TestValidate(t *testing.T) {
|
||||||
pk := getThresholdPrivateKey()
|
pk := getThresholdSecretKey()
|
||||||
if err := pk.Validate(rand.Reader); err != nil {
|
if err := pk.Validate(rand.Reader); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user
Paillier is an asymmetric cryptographic algorithm so the
PrivateKeyis 100% fine for this, there is no need to change it.