rename PrivateKey -> SecretKey

This commit is contained in:
sachaservan committed 2018-08-18 13:14:21 -04:00
1 parent 753322e473
commit 5adb70bc7b
7 files changed
+87 -87

No files matched your search

+22 -22
View File
@@ -7,65 +7,65 @@ import (
"gopkg.in/mgo.v2/bson"
)
type SerializablePrivateKey paillier.PrivateKey
type SerializableSecretKey paillier.SecretKey
// Serializes PrivateKey to BSON
func SerializePrivateKey(key *paillier.PrivateKey) ([]byte, error) {
return bson.Marshal(toSerializablePrivateKey(key))
// Serializes SecretKey to BSON
func SerializeSecretKey(key *paillier.SecretKey) ([]byte, error) {
return bson.Marshal(toSerializableSecretKey(key))
}
// Deserializes BSON to PrivateKey
func DeserializePrivateKey(data []byte) (*paillier.PrivateKey, error) {
serializable := new(SerializablePrivateKey)
// Deserializes BSON to SecretKey
func DeserializeSecretKey(data []byte) (*paillier.SecretKey, error) {
serializable := new(SerializableSecretKey)
if err := bson.Unmarshal(data, serializable); err != nil {
return nil, err
}
return toOriginalPrivateKey(serializable), nil
return toOriginalSecretKey(serializable), nil
}
func toSerializablePrivateKey(key *paillier.PrivateKey) *SerializablePrivateKey {
serializable := SerializablePrivateKey(*key)
func toSerializableSecretKey(key *paillier.SecretKey) *SerializableSecretKey {
serializable := SerializableSecretKey(*key)
return &serializable
}
func toOriginalPrivateKey(serializable *SerializablePrivateKey) *paillier.PrivateKey {
original := paillier.PrivateKey(*serializable)
func toOriginalSecretKey(serializable *SerializableSecretKey) *paillier.SecretKey {
original := paillier.SecretKey(*serializable)
return &original
}
type dbPrivateKey struct {
type dbSecretKey struct {
N string `bson:",omitempty"`
Lambda string `bson:",omitempty"`
Mu string `bson:",omitempty"`
}
func (privateKey *SerializablePrivateKey) GetBSON() (interface{}, error) {
func (SecretKey *SerializableSecretKey) GetBSON() (interface{}, error) {
m := make(map[string]string)
if privateKey.N != nil {
m["n"] = fmt.Sprintf("%x", privateKey.N)
if SecretKey.N != nil {
m["n"] = fmt.Sprintf("%x", SecretKey.N)
}
if privateKey.Lambda != nil {
m["lambda"] = fmt.Sprintf("%x", privateKey.Lambda)
if SecretKey.Lambda != nil {
m["lambda"] = fmt.Sprintf("%x", SecretKey.Lambda)
}
return m, nil
}
func (privateKey *SerializablePrivateKey) SetBSON(raw bson.Raw) error {
func (SecretKey *SerializableSecretKey) SetBSON(raw bson.Raw) error {
var err error = nil
c := new(dbPrivateKey)
c := new(dbSecretKey)
raw.Unmarshal(c)
if c.N != "" {
privateKey.N, err = fromHex(c.N)
SecretKey.N, err = fromHex(c.N)
if err != nil {
return err
}
}
if c.Lambda != "" {
privateKey.Lambda, err = fromHex(c.Lambda)
SecretKey.Lambda, err = fromHex(c.Lambda)
if err != nil {
return err
}
+4 -4
View File
@@ -7,20 +7,20 @@ import (
"github.com/keep-network/paillier"
)
func TestPrivateKeyBsonSerialization(t *testing.T) {
key := &paillier.PrivateKey{
func TestSecretKeyBsonSerialization(t *testing.T) {
key := &paillier.SecretKey{
PublicKey: paillier.PublicKey{
N: (b(345)),
},
Lambda: b(5),
}
serialized, err := SerializePrivateKey(key)
serialized, err := SerializeSecretKey(key)
if err != nil {
t.Fatal(err)
}
deserialized, err := DeserializePrivateKey(serialized)
deserialized, err := DeserializeSecretKey(serialized)
if err != nil {
t.Fatal(err)
}
+5 -5
View File
@@ -102,7 +102,7 @@ func (pk *PublicKey) Mul(cypher *Cypher, scalar *big.Int) *Cypher {
}
}
type PrivateKey struct {
type SecretKey struct {
PublicKey
Lambda *big.Int
}
@@ -115,7 +115,7 @@ type PrivateKey struct {
// D(c) = [ ((c^lambda) mod N^2) - 1) / N ] lambda^-1 mod N
//
// 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)
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)
@@ -143,7 +143,7 @@ func computePhi(p, q *big.Int) *big.Int {
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.
//
// 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
// with Applications to Electronic Voting
// 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)
lambda := computePhi(p, q)
return &PrivateKey{
return &SecretKey{
PublicKey: PublicKey{
N: n,
},
+33 -33
View File
@@ -32,18 +32,18 @@ func TestComputePhi(t *testing.T) {
}
}
func TestCreatePrivateKey(t *testing.T) {
func TestCreateSecretKey(t *testing.T) {
p := big.NewInt(463)
q := big.NewInt(631)
privateKey := CreatePrivateKey(p, q)
SecretKey := CreateSecretKey(p, q)
if privateKey.N.Cmp(big.NewInt(292153)) != 0 {
t.Errorf("Unexpected N PublicKey value [%v]", privateKey.N)
if SecretKey.N.Cmp(big.NewInt(292153)) != 0 {
t.Errorf("Unexpected N PublicKey value [%v]", SecretKey.N)
}
if privateKey.Lambda.Cmp(big.NewInt(291060)) != 0 {
t.Errorf("Unexpected Lambda Public key value [%v]", privateKey.Lambda)
if SecretKey.Lambda.Cmp(big.NewInt(291060)) != 0 {
t.Errorf("Unexpected Lambda Public key value [%v]", SecretKey.Lambda)
}
}
@@ -51,14 +51,14 @@ func TestEncryptDecryptSmall(t *testing.T) {
p := big.NewInt(13)
q := big.NewInt(11)
for i := 1; i < 10; i++ {
privateKey := CreatePrivateKey(p, q)
SecretKey := CreateSecretKey(p, q)
initialValue := big.NewInt(100)
cypher, err := privateKey.Encrypt(initialValue, rand.Reader)
cypher, err := SecretKey.Encrypt(initialValue, rand.Reader)
if err != nil {
t.Error(err)
}
returnedValue := privateKey.Decrypt(cypher)
returnedValue := SecretKey.Decrypt(cypher)
if initialValue.Cmp(returnedValue) != 0 {
t.Error("wrong decryption ", returnedValue, " is not ", initialValue)
}
@@ -70,7 +70,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
q := big.NewInt(11)
// N = pq = 143 so the plaintext space is [0, 143)
privateKey := CreatePrivateKey(p, q)
SecretKey := CreateSecretKey(p, q)
var tests = map[string]struct {
plaintext *big.Int
@@ -101,7 +101,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
for testName, test := range tests {
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) {
t.Errorf(
"Unexpected error\nExpected: %v\nActual: %v",
@@ -111,7 +111,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
}
if test.expectedError == nil {
decrypted := privateKey.Decrypt(cypher)
decrypted := SecretKey.Decrypt(cypher)
if test.plaintext.Cmp(decrypted) != 0 {
t.Errorf(
"Unexpected decryption\nExpected: %v\nActual: %v",
@@ -125,44 +125,44 @@ func TestCheckPlaintextSpace(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)
cypher2, _ := privateKey.Encrypt(big.NewInt(6), rand.Reader)
cypher3, _ := privateKey.Encrypt(big.NewInt(7), rand.Reader)
cypher4, _ := privateKey.Encrypt(big.NewInt(8), rand.Reader)
cypher5 := privateKey.Add(cypher1, cypher2, cypher3, cypher4)
cypher1, _ := SecretKey.Encrypt(big.NewInt(5), rand.Reader)
cypher2, _ := SecretKey.Encrypt(big.NewInt(6), rand.Reader)
cypher3, _ := SecretKey.Encrypt(big.NewInt(7), rand.Reader)
cypher4, _ := SecretKey.Encrypt(big.NewInt(8), rand.Reader)
cypher5 := SecretKey.Add(cypher1, cypher2, cypher3, cypher4)
m := privateKey.Decrypt(cypher5)
m := SecretKey.Decrypt(cypher5)
if m.Cmp(big.NewInt(26)) != 0 {
t.Errorf("Unexpected decrypted value [%v]", m)
}
}
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)
cypher2, _ := privateKey.Encrypt(big.NewInt(25), rand.Reader)
cypher3, _ := privateKey.Encrypt(big.NewInt(11), rand.Reader)
cypher4 := privateKey.Add(cypher1, cypher2, cypher3)
cypher1, _ := SecretKey.Encrypt(big.NewInt(30), rand.Reader)
cypher2, _ := SecretKey.Encrypt(big.NewInt(25), rand.Reader)
cypher3, _ := SecretKey.Encrypt(big.NewInt(11), rand.Reader)
cypher4 := SecretKey.Add(cypher1, cypher2, cypher3)
m := privateKey.Decrypt(cypher4)
m := SecretKey.Decrypt(cypher4)
if m.Cmp(big.NewInt(31)) != 0 {
t.Errorf("Unexpected decrypted value [%v]", m)
}
}
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 {
t.Fatal(err)
}
cypherMultiple := privateKey.Mul(cypher, big.NewInt(7))
multiple := privateKey.Decrypt(cypherMultiple)
cypherMultiple := SecretKey.Mul(cypher, big.NewInt(7))
multiple := SecretKey.Decrypt(cypherMultiple)
// 3 * 7 = 21
if multiple.Cmp(big.NewInt(21)) != 0 {
@@ -171,15 +171,15 @@ func TestMulCypher(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 {
t.Fatal(err)
}
cypherMultiple := privateKey.Mul(cypher, big.NewInt(93))
multiple := privateKey.Decrypt(cypherMultiple)
cypherMultiple := SecretKey.Mul(cypher, big.NewInt(93))
multiple := SecretKey.Decrypt(cypherMultiple)
// (30*93) mod (7*5) = 25
if multiple.Cmp(big.NewInt(25)) != 0 {
+9 -9
View File
@@ -175,14 +175,14 @@ func (tk *ThresholdPublicKey) VerifyDecryption(encryptedMessage, decryptedMessag
// for the given decryption server.
// `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.
type ThresholdPrivateKey struct {
type ThresholdSecretKey struct {
ThresholdPublicKey
Id int
Share *big.Int
}
// 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.Id = tpk.Id
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
}
func (tpk *ThresholdPrivateKey) copyVi() []*big.Int {
func (tpk *ThresholdSecretKey) copyVi() []*big.Int {
ret := make([]*big.Int, len(tpk.Vi))
for i, vi := range tpk.Vi {
ret[i] = new(big.Int).Add(vi, big.NewInt(0))
@@ -199,7 +199,7 @@ func (tpk *ThresholdPrivateKey) copyVi() []*big.Int {
return ret
}
func (tpk *ThresholdPrivateKey) getThresholdKey() *ThresholdPublicKey {
func (tpk *ThresholdSecretKey) getThresholdKey() *ThresholdPublicKey {
ret := new(ThresholdPublicKey)
ret.Threshold = tpk.Threshold
ret.TotalNumberOfDecryptionServers = tpk.TotalNumberOfDecryptionServers
@@ -209,13 +209,13 @@ func (tpk *ThresholdPrivateKey) getThresholdKey() *ThresholdPublicKey {
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(tmp, tpk.Share)
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.Write(a.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{}))
}
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.Key = tpk.getThresholdKey()
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,
// 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)
if err != nil {
return err
@@ -281,7 +281,7 @@ type PartialDecryption struct {
// A non-interactive ZKP based on the Fiat–Shamir heuristic. This algorithm
// 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).
//
// The Fiat-Shamir is a non-interactive proof of knowledge heuristic.
+7 -7
View File
@@ -240,8 +240,8 @@ func (tkg *ThresholdKeyGenerator) createViArray(shares []*big.Int) (viArray []*b
return viArray
}
func (tkg *ThresholdKeyGenerator) createPrivateKey(i int, share *big.Int, viArray []*big.Int) *ThresholdPrivateKey {
ret := new(ThresholdPrivateKey)
func (tkg *ThresholdKeyGenerator) createSecretKey(i int, share *big.Int, viArray []*big.Int) *ThresholdSecretKey {
ret := new(ThresholdSecretKey)
ret.N = tkg.n
ret.V = tkg.v
@@ -253,22 +253,22 @@ func (tkg *ThresholdKeyGenerator) createPrivateKey(i int, share *big.Int, viArra
return ret
}
func (tkg *ThresholdKeyGenerator) createPrivateKeys() []*ThresholdPrivateKey {
func (tkg *ThresholdKeyGenerator) createSecretKeys() []*ThresholdSecretKey {
shares := tkg.createShares()
viArray := tkg.createViArray(shares)
ret := make([]*ThresholdPrivateKey, tkg.TotalNumberOfDecryptionServers)
ret := make([]*ThresholdSecretKey, tkg.TotalNumberOfDecryptionServers)
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
}
func (tkg *ThresholdKeyGenerator) Generate() ([]*ThresholdPrivateKey, error) {
func (tkg *ThresholdKeyGenerator) Generate() ([]*ThresholdSecretKey, error) {
if err := tkg.initNumerialValues(); err != nil {
return nil, err
}
if err := tkg.generateHidingPolynomial(); err != nil {
return nil, err
}
return tkg.createPrivateKeys(), nil
return tkg.createSecretKeys(), nil
}
+7 -7
View File
@@ -7,7 +7,7 @@ import (
"testing"
)
func getThresholdPrivateKey() *ThresholdPrivateKey {
func getThresholdSecretKey() *ThresholdSecretKey {
tkh, err := GetThresholdKeyGenerator(32, 10, 6, rand.Reader)
if err != nil {
panic(err)
@@ -55,7 +55,7 @@ func TestCombineSharesConstant(t *testing.T) {
}
func TestDecrypt(t *testing.T) {
key := new(ThresholdPrivateKey)
key := new(ThresholdSecretKey)
key.TotalNumberOfDecryptionServers = 10
key.N = b(101 * 103)
key.Share = b(862)
@@ -73,7 +73,7 @@ func TestDecrypt(t *testing.T) {
}
func TestCopyVi(t *testing.T) {
key := new(ThresholdPrivateKey)
key := new(ThresholdSecretKey)
key.Vi = []*big.Int{b(34), b(2), b(29)}
vi := key.copyVi()
if !reflect.DeepEqual(vi, key.Vi) {
@@ -86,7 +86,7 @@ func TestCopyVi(t *testing.T) {
}
func TestEncryptWithThresholdKey(t *testing.T) {
pd := getThresholdPrivateKey()
pd := getThresholdSecretKey()
_, err := pd.Encrypt(big.NewInt(876), rand.Reader)
if err != nil {
t.Fail()
@@ -94,7 +94,7 @@ func TestEncryptWithThresholdKey(t *testing.T) {
}
func TestDecryptWithThresholdKey(t *testing.T) {
pd := getThresholdPrivateKey()
pd := getThresholdSecretKey()
c, err := pd.Encrypt(big.NewInt(876), rand.Reader)
if err != nil {
t.Fail()
@@ -131,7 +131,7 @@ func TestVerifyPart2(t *testing.T) {
}
func TestDecryptAndProduceZNP(t *testing.T) {
pd := getThresholdPrivateKey()
pd := getThresholdSecretKey()
c, err := pd.Encrypt(big.NewInt(876), rand.Reader)
if err != nil {
t.Error(err)
@@ -283,7 +283,7 @@ func TestDecryption(t *testing.T) {
}
func TestValidate(t *testing.T) {
pk := getThresholdPrivateKey()
pk := getThresholdSecretKey()
if err := pk.Validate(rand.Reader); err != nil {
t.Error(err)
}