Fix spelling/naming issues, slightly more robust testing of encryption and decryption. #12
No files matched your search
+14
-14
@@ -9,15 +9,15 @@ import (
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
type SerializableCypher paillier.Cypher
|
||||
type SerializableCypher paillier.Ciphertext
|
||||
|
||||
// Serializes Cypher to BSON
|
||||
func SerializeCypher(cypher *paillier.Cypher) ([]byte, error) {
|
||||
return bson.Marshal(toSerializableCypher(cypher))
|
||||
// Serializes Ciphertext to BSON
|
||||
func SerializeCypher(ct *paillier.Ciphertext) ([]byte, error) {
|
||||
return bson.Marshal(toSerializableCypher(ct))
|
||||
}
|
||||
|
||||
// Deserializes BSON to Cypher
|
||||
func DeserializeCypher(data []byte) (*paillier.Cypher, error) {
|
||||
// Deserializes BSON to Ciphertext
|
||||
func DeserializeCypher(data []byte) (*paillier.Ciphertext, error) {
|
||||
serializable := new(SerializableCypher)
|
||||
if err := bson.Unmarshal(data, serializable); err != nil {
|
||||
return nil, err
|
||||
@@ -26,13 +26,13 @@ func DeserializeCypher(data []byte) (*paillier.Cypher, error) {
|
||||
return toOriginalCypher(serializable), nil
|
||||
}
|
||||
|
||||
func toSerializableCypher(cypher *paillier.Cypher) *SerializableCypher {
|
||||
serializable := SerializableCypher(*cypher)
|
||||
func toSerializableCypher(ct *paillier.Ciphertext) *SerializableCypher {
|
||||
serializable := SerializableCypher(*ct)
|
||||
return &serializable
|
||||
}
|
||||
|
||||
func toOriginalCypher(serializable *SerializableCypher) *paillier.Cypher {
|
||||
original := paillier.Cypher(*serializable)
|
||||
func toOriginalCypher(serializable *SerializableCypher) *paillier.Ciphertext {
|
||||
original := paillier.Ciphertext(*serializable)
|
||||
return &original
|
||||
}
|
||||
|
||||
@@ -40,17 +40,17 @@ type dbCypher struct {
|
||||
C string
|
||||
}
|
||||
|
||||
func (cypher *SerializableCypher) GetBSON() (interface{}, error) {
|
||||
return &dbCypher{fmt.Sprintf("%x", cypher.C)}, nil
|
||||
func (ct *SerializableCypher) GetBSON() (interface{}, error) {
|
||||
return &dbCypher{fmt.Sprintf("%x", ct.C)}, nil
|
||||
}
|
||||
|
||||
func (cypher *SerializableCypher) SetBSON(raw bson.Raw) error {
|
||||
func (ct *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)
|
||||
ct.C, ok = new(big.Int).SetString(c.C, 16)
|
||||
if !ok {
|
||||
return errors.New("big int not in hexadecimal format")
|
||||
}
|
||||
|
||||
@@ -8,11 +8,11 @@ import (
|
||||
)
|
||||
|
||||
func TestCypherBsonSerialization(t *testing.T) {
|
||||
cypher := &paillier.Cypher{
|
||||
ct := &paillier.Ciphertext{
|
||||
C: b(5),
|
||||
}
|
||||
|
||||
serialized, err := SerializeCypher(cypher)
|
||||
serialized, err := SerializeCypher(ct)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -22,11 +22,11 @@ func TestCypherBsonSerialization(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cypher, deserialized) {
|
||||
if !reflect.DeepEqual(ct, deserialized) {
|
||||
t.Errorf(
|
||||
"Unexpected serialization result\nActual: %v\nExpected: %v\n",
|
||||
deserialized,
|
||||
cypher,
|
||||
ct,
|
||||
)
|
||||
}
|
||||
}
|
||||
+16
-16
@@ -14,7 +14,7 @@ func (pk *PublicKey) GetNSquare() *big.Int {
|
||||
return new(big.Int).Mul(pk.N, pk.N)
|
||||
}
|
||||
|
||||
// EncryptWithR encrypts a plaintext into a cypher one with random `r` specified
|
||||
// EncryptWithR encrypts a plaintext into a ct one with random `r` specified
|
||||
// in the argument. The plain text must be smaller that N and bigger than or
|
||||
// equal zero. `r` is the randomness used to encrypt the plaintext. `r` must be
|
||||
// a random element from a multiplicative group of integers modulo N.
|
||||
@@ -27,7 +27,7 @@ func (pk *PublicKey) GetNSquare() *big.Int {
|
||||
// E(m, r) = [(1 + N) r^N] mod N^2
|
||||
//
|
||||
// See [KL 08] construction 11.32, page 414.
|
||||
func (pk *PublicKey) EncryptWithR(m *big.Int, r *big.Int) (*Cypher, error) {
|
||||
func (pk *PublicKey) EncryptWithR(m *big.Int, r *big.Int) (*Ciphertext, error) {
|
||||
if m.Cmp(ZERO) == -1 || m.Cmp(pk.N) != -1 { // m < 0 || m >= N ?
|
||||
return nil, fmt.Errorf(
|
||||
"%v is out of allowed plaintext space [0, %v)",
|
||||
@@ -44,10 +44,10 @@ func (pk *PublicKey) EncryptWithR(m *big.Int, r *big.Int) (*Cypher, error) {
|
||||
g := new(big.Int).Add(pk.N, big.NewInt(1))
|
||||
gm := new(big.Int).Exp(g, m, nSquare)
|
||||
rn := new(big.Int).Exp(r, pk.N, nSquare)
|
||||
return &Cypher{new(big.Int).Mod(new(big.Int).Mul(rn, gm), nSquare)}, nil
|
||||
return &Ciphertext{new(big.Int).Mod(new(big.Int).Mul(rn, gm), nSquare)}, nil
|
||||
}
|
||||
|
||||
// Encrypt a plaintext into a cypher one. The plain text must be smaller that
|
||||
// Encrypt a plaintext into a ct one. The plain text must be smaller that
|
||||
// N and bigger than or equal zero. random is usually rand.Reader from the
|
||||
// package crypto/rand.
|
||||
//
|
||||
@@ -57,7 +57,7 @@ func (pk *PublicKey) EncryptWithR(m *big.Int, r *big.Int) (*Cypher, error) {
|
||||
// See [KL 08] construction 11.32, page 414.
|
||||
//
|
||||
// Returns an error if an error has be returned by io.Reader.
|
||||
func (pk *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Cypher, error) {
|
||||
func (pk *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Ciphertext, error) {
|
||||
r, err := GetRandomNumberInMultiplicativeGroup(pk.N, random)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -74,31 +74,31 @@ func (pk *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Cypher, error) {
|
||||
// plaintexts:
|
||||
//
|
||||
// D( (E(m1) * E(m2) mod n^2) ) = m1 + m2 mod n
|
||||
func (pk *PublicKey) Add(cypher ...*Cypher) *Cypher {
|
||||
func (pk *PublicKey) Add(ct ...*Ciphertext) *Ciphertext {
|
||||
accumulator := big.NewInt(1)
|
||||
|
||||
for _, c := range cypher {
|
||||
for _, c := range ct {
|
||||
accumulator = new(big.Int).Mod(
|
||||
new(big.Int).Mul(accumulator, c.C),
|
||||
pk.GetNSquare(),
|
||||
)
|
||||
}
|
||||
|
||||
return &Cypher{
|
||||
return &Ciphertext{
|
||||
C: accumulator,
|
||||
}
|
||||
}
|
||||
|
||||
// Mul returns a product of `cypher` and `scalar` without decrypting `cypher`.
|
||||
// Mul returns a product of `ct` and `scalar` without decrypting `ct`.
|
||||
//
|
||||
// It's possible because Paillier is a homomorphic encryption scheme, where
|
||||
// an encrypted plaintext `m` raised to an integer `k` will decrypt to the
|
||||
// product of the plaintext `m` and `k`:
|
||||
//
|
||||
// D( E(m)^k mod N^2 ) = km mod N
|
||||
func (pk *PublicKey) Mul(cypher *Cypher, scalar *big.Int) *Cypher {
|
||||
return &Cypher{
|
||||
C: new(big.Int).Exp(cypher.C, scalar, pk.GetNSquare()),
|
||||
func (pk *PublicKey) Mul(ct *Ciphertext, scalar *big.Int) *Ciphertext {
|
||||
return &Ciphertext{
|
||||
C: new(big.Int).Exp(ct.C, scalar, pk.GetNSquare()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,18 +115,18 @@ type SecretKey struct {
|
||||
// D(c) = [ ((c^lambda) mod N^2) - 1) / N ] lambda^-1 mod N
|
||||
//
|
||||
// See [KL 08] construction 11.32, page 414.
|
||||
func (sk *SecretKey) Decrypt(cypher *Cypher) (msg *big.Int) {
|
||||
func (sk *SecretKey) Decrypt(ct *Ciphertext) (msg *big.Int) {
|
||||
mu := new(big.Int).ModInverse(sk.Lambda, sk.N)
|
||||
tmp := new(big.Int).Exp(cypher.C, sk.Lambda, sk.GetNSquare())
|
||||
tmp := new(big.Int).Exp(ct.C, sk.Lambda, sk.GetNSquare())
|
||||
msg = new(big.Int).Mod(new(big.Int).Mul(L(tmp, sk.N), mu), sk.N)
|
||||
return
|
||||
}
|
||||
|
||||
type Cypher struct {
|
||||
type Ciphertext struct {
|
||||
C *big.Int
|
||||
}
|
||||
|
||||
func (this *Cypher) String() string {
|
||||
func (this *Ciphertext) String() string {
|
||||
return fmt.Sprintf("%x", this.C)
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -54,11 +54,11 @@ func TestEncryptDecryptSmall(t *testing.T) {
|
||||
SecretKey := CreateSecretKey(p, q)
|
||||
|
||||
initialValue := big.NewInt(100)
|
||||
cypher, err := SecretKey.Encrypt(initialValue, rand.Reader)
|
||||
ct, err := SecretKey.Encrypt(initialValue, rand.Reader)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
returnedValue := SecretKey.Decrypt(cypher)
|
||||
returnedValue := SecretKey.Decrypt(ct)
|
||||
if initialValue.Cmp(returnedValue) != 0 {
|
||||
t.Error("wrong decryption ", returnedValue, " is not ", initialValue)
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
|
||||
|
||||
for testName, test := range tests {
|
||||
t.Run(testName, func(t *testing.T) {
|
||||
cypher, err := SecretKey.Encrypt(test.plaintext, rand.Reader)
|
||||
ct, 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 := SecretKey.Decrypt(cypher)
|
||||
decrypted := SecretKey.Decrypt(ct)
|
||||
if test.plaintext.Cmp(decrypted) != 0 {
|
||||
t.Errorf(
|
||||
"Unexpected decryption\nExpected: %v\nActual: %v",
|
||||
@@ -156,12 +156,12 @@ func TestAddCypherWithSmallKeyModulus(t *testing.T) {
|
||||
func TestMulCypher(t *testing.T) {
|
||||
SecretKey := CreateSecretKey(big.NewInt(17), big.NewInt(13))
|
||||
|
||||
cypher, err := SecretKey.Encrypt(big.NewInt(3), rand.Reader)
|
||||
ct, err := SecretKey.Encrypt(big.NewInt(3), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cypherMultiple := SecretKey.Mul(cypher, big.NewInt(7))
|
||||
cypherMultiple := SecretKey.Mul(ct, big.NewInt(7))
|
||||
multiple := SecretKey.Decrypt(cypherMultiple)
|
||||
|
||||
// 3 * 7 = 21
|
||||
@@ -173,12 +173,12 @@ func TestMulCypher(t *testing.T) {
|
||||
func TestMulCypherWithSmallKeyModulus(t *testing.T) {
|
||||
SecretKey := CreateSecretKey(big.NewInt(7), big.NewInt(5))
|
||||
|
||||
cypher, err := SecretKey.Encrypt(big.NewInt(30), rand.Reader)
|
||||
ct, err := SecretKey.Encrypt(big.NewInt(30), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cypherMultiple := SecretKey.Mul(cypher, big.NewInt(93))
|
||||
cypherMultiple := SecretKey.Mul(ct, big.NewInt(93))
|
||||
multiple := SecretKey.Decrypt(cypherMultiple)
|
||||
|
||||
// (30*93) mod (7*5) = 25
|
||||
|
||||
+2
-2
@@ -181,7 +181,7 @@ type ThresholdSecretKey struct {
|
||||
Share *big.Int
|
||||
}
|
||||
|
||||
// Decrypts the cypher text and returns the partial decryption
|
||||
// Decrypts the ct text and returns the partial decryption
|
||||
func (tpk *ThresholdSecretKey) Decrypt(c *big.Int) *PartialDecryption {
|
||||
ret := new(PartialDecryption)
|
||||
ret.Id = tpk.Id
|
||||
@@ -334,7 +334,7 @@ type PartialDecryptionZKP struct {
|
||||
Key *ThresholdPublicKey // the public key used to encrypt
|
||||
E *big.Int // the challenge
|
||||
Z *big.Int // the value needed to check to verify the decryption
|
||||
C *big.Int // the input cypher text
|
||||
C *big.Int // the input ct text
|
||||
}
|
||||
|
||||
func (pd *PartialDecryptionZKP) verifyPart1() *big.Int {
|
||||
|
||||
@@ -374,15 +374,15 @@ func TestVerifyDecryption(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
expt := b(101)
|
||||
cypher, err := tpks[0].Encrypt(expt, rand.Reader)
|
||||
ct, err := tpks[0].Encrypt(expt, rand.Reader)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
pd1, err := tpks[0].DecryptAndProduceZNP(cypher.C, rand.Reader)
|
||||
pd1, err := tpks[0].DecryptAndProduceZNP(ct.C, rand.Reader)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
pd2, err := tpks[1].DecryptAndProduceZNP(cypher.C, rand.Reader)
|
||||
pd2, err := tpks[1].DecryptAndProduceZNP(ct.C, rand.Reader)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -391,13 +391,13 @@ func TestVerifyDecryption(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err = pk.VerifyDecryption(cypher.C, b(101), pds); err != nil {
|
||||
if err = pk.VerifyDecryption(ct.C, b(101), pds); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = pk.VerifyDecryption(cypher.C, b(100), pds); err == nil {
|
||||
if err = pk.VerifyDecryption(ct.C, b(100), pds); err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = pk.VerifyDecryption(new(big.Int).Add(b(1), cypher.C), b(101), pds); err == nil {
|
||||
if err = pk.VerifyDecryption(new(big.Int).Add(b(1), ct.C), b(101), pds); err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user