Documentation improvements, code cleaned up #2

Merged
pdyraga merged 31 commits from master-with-bson into master 2018-06-05 19:08:37 -04:00
5 changed files with 76 additions and 78 deletions
Showing only changes of commit 83928fcadb - Show all commits

No files matched your search

+2 -15
View File
@@ -117,18 +117,13 @@ type dbPrivateKey struct {
func (this *PrivateKey) GetBSON() (interface{}, error) { func (this *PrivateKey) GetBSON() (interface{}, error) {
m := make(map[string]string) m := make(map[string]string)
if this.G != nil {
m["g"] = fmt.Sprintf("%x", this.G)
}
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)
} }
if this.Mu != nil {
m["mu"] = fmt.Sprintf("%x", this.Mu)
}
return m, nil return m, nil
} }
@@ -143,21 +138,13 @@ func (this *PrivateKey) SetBSON(raw bson.Raw) error {
return err return err
} }
} }
if c.G != "" {
this.G, err = fromHex(c.G)
if err != nil {
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
} }
} }
if c.Mu != "" {
this.Mu, err = fromHex(c.Mu)
}
return err return err
} }
+1 -1
View File
@@ -118,6 +118,6 @@ func TestPartialDecryptionZKPBSONification(t *testing.T) {
} }
func TestThresholdKeyBSON(t *testing.T) { func TestThresholdKeyBSON(t *testing.T) {
key := &ThresholdKey{PublicKey{b(9), b(8), nil}, 7, 6, b(3), []*big.Int{b(2), b(34)}} key := &ThresholdKey{PublicKey{b(9)}, 7, 6, b(3), []*big.Int{b(2), b(34)}, b(8)}
AssertBSONIsGood(key, new(ThresholdKey), t) AssertBSONIsGood(key, new(ThresholdKey), t)
} }
+42 -38
View File
@@ -7,29 +7,28 @@ import (
) )
type PublicKey struct { type PublicKey struct {
N, G *big.Int // usually G is set to N+1 N *big.Int
n2 *big.Int // the cache value of N^2
} }
func (this *PublicKey) GetBSON() (interface{}, error) { func (this *PublicKey) GetBSON() (interface{}, error) {
m := make(map[string]string) m := make(map[string]string)
m["g"] = fmt.Sprintf("%x", this.G)
m["n"] = fmt.Sprintf("%x", this.N) m["n"] = fmt.Sprintf("%x", this.N)
return m, nil return m, nil
} }
func (this *PublicKey) GetNSquare() *big.Int { func (this *PublicKey) GetNSquare() *big.Int {
if this.n2 != nil { return new(big.Int).Mul(this.N, this.N)
return this.n2
}
this.n2 = new(big.Int).Mul(this.N, this.N)
return this.n2
} }
// Encode a plain text in a cypher one. The plain text must be smaller that // Encode a plaintext in a cypher one. The plain text must be smaller that
// N and bigger or equal than zero. random is usually rand.Reader from the // N and bigger or equal than zero. random is usually rand.Reader from the
// package crypto/rand. // package crypto/rand.
// //
// m - plaintext to encrypt
// E(m, r) = [(1 + N) r^N] mod N^2
//
// See [KL 08] construction 11.32, page 414.
//
// Returns an error if an error has be returned by io.Reader. // Returns an error if an error has be returned by io.Reader.
func (pub *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Cypher, error) { func (pub *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Cypher, error) {
r, err := GetRandomNumberInMultiplicativeGroup(pub.N, random) r, err := GetRandomNumberInMultiplicativeGroup(pub.N, random)
@@ -38,7 +37,8 @@ func (pub *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Cypher, error) {
} }
nSquare := pub.GetNSquare() nSquare := pub.GetNSquare()
gm := new(big.Int).Exp(pub.G, m, nSquare) g := new(big.Int).Add(pub.N, big.NewInt(1))
gm := new(big.Int).Exp(g, m, nSquare)
rn := new(big.Int).Exp(r, pub.N, nSquare) rn := new(big.Int).Exp(r, pub.N, nSquare)
return &Cypher{new(big.Int).Mod(new(big.Int).Mul(rn, gm), nSquare)}, nil return &Cypher{new(big.Int).Mod(new(big.Int).Mul(rn, gm), nSquare)}, nil
} }
@@ -52,20 +52,21 @@ func (this *PublicKey) Add(cypher1, cypher2 *Cypher) *Cypher {
type PrivateKey struct { type PrivateKey struct {
PublicKey PublicKey
Lambda, Mu *big.Int Lambda *big.Int
}
func (this *PrivateKey) String() string {
ret := fmt.Sprintf("g : %x", this.G)
ret += fmt.Sprintf("n : %x", this.N)
ret += fmt.Sprintf("lambda: %x", this.Lambda)
ret += fmt.Sprintf("mu : %x", this.Mu)
return ret
} }
// Decodes ciphertext into a plaintext message.
//
// c - cyphertext to decrypt
// N, lambda - key attributes
//
// 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 *PrivateKey) 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()) tmp := new(big.Int).Exp(cypher.C, priv.Lambda, priv.GetNSquare())
msg = new(big.Int).Mod(new(big.Int).Mul(L(tmp, priv.N), priv.Mu), priv.N) msg = new(big.Int).Mod(new(big.Int).Mul(L(tmp, priv.N), mu), priv.N)
return return
} }
@@ -82,35 +83,38 @@ func L(u, n *big.Int) *big.Int {
return new(big.Int).Div(t, n) return new(big.Int).Div(t, n)
} }
func LCM(x, y *big.Int) *big.Int {
return new(big.Int).Mul(new(big.Int).Div(x, new(big.Int).GCD(nil, nil, x, y)), y)
}
func minusOne(x *big.Int) *big.Int { func minusOne(x *big.Int) *big.Int {
return new(big.Int).Add(x, big.NewInt(-1)) return new(big.Int).Add(x, big.NewInt(-1))
} }
func computeMu(g, lambda, n *big.Int) *big.Int { func computePhi(p, q *big.Int) *big.Int {
n2 := new(big.Int).Mul(n, n) return new(big.Int).Mul(minusOne(p), minusOne(q))
u := new(big.Int).Exp(g, lambda, n2)
return new(big.Int).ModInverse(L(u, n), n)
}
func computeLamda(p, q *big.Int) *big.Int {
return LCM(minusOne(p), minusOne(q))
} }
// CreatePrivateKey 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,
// page 414 which is compatible with one described in [DJN 10], section 3.2
// except that instead of generating Lambda private key component from LCM
// of p and q we use Euler's totient function as suggested in [KL 08].
//
// [KL 08]: Jonathan Katz, Yehuda Lindell, (2008)
// Introduction to Modern Cryptography: Principles and Protocols,
// Chapman & Hall/CRC
//
// [DJN 10]: Ivan Damgard, Mads Jurik, Jesper Buus Nielsen, (2010)
// 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 CreatePrivateKey(p, q *big.Int) *PrivateKey {
n := new(big.Int).Mul(p, q) n := new(big.Int).Mul(p, q)
lambda := new(big.Int).Mul(minusOne(p), minusOne(q)) lambda := computePhi(p, q)
g := new(big.Int).Add(n, big.NewInt(1))
mu := new(big.Int).ModInverse(lambda, n)
return &PrivateKey{ return &PrivateKey{
PublicKey: PublicKey{ PublicKey: PublicKey{
N: n, N: n,
G: g,
}, },
Lambda: lambda, Lambda: lambda,
Mu: mu,
} }
} }
+30 -24
View File
@@ -7,35 +7,42 @@ import (
"testing" "testing"
) )
func TestLCM(t *testing.T) { func TestComputeL(t *testing.T) {
a := big.NewInt(2 * 3 * 3 * 3 * 5 * 5)
b := big.NewInt(3 * 3 * 5 * 5 * 57 * 11)
exp := big.NewInt(3 * 3 * 5 * 5)
if reflect.DeepEqual(exp, LCM(a, b)) {
t.Fail()
}
}
func TestL(t *testing.T) {
u := big.NewInt(21) u := big.NewInt(21)
n := big.NewInt(3) n := big.NewInt(3)
exp := big.NewInt(6)
if !reflect.DeepEqual(exp, L(u, n)) { expected := big.NewInt(6)
t.Error("L function is not good") actual := L(u, n)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Unexpected L function result [%v]", actual)
} }
} }
func TestComputeMu(t *testing.T) { func TestComputePhi(t *testing.T) {
p := big.NewInt(13) a := big.NewInt(5)
q := big.NewInt(11) b := big.NewInt(7)
lambda := computeLamda(p, q) expected := big.NewInt(24)
g := big.NewInt(5000) actual := computePhi(a, b)
n := new(big.Int).Mul(p, q)
exp := big.NewInt(3) if !reflect.DeepEqual(expected, actual) {
if !reflect.DeepEqual(computeMu(g, lambda, n), exp) { t.Errorf("Unexpected phi value [%v]", actual)
t.Error("lambda is not well computed") }
}
func TestCreatePrivateKey(t *testing.T) {
p := big.NewInt(463)
q := big.NewInt(631)
privateKey := CreatePrivateKey(p, q)
if !reflect.DeepEqual(privateKey.N, big.NewInt(292153)) {
t.Errorf("Unexpected N PublicKey value [%v]", privateKey.N)
}
if !reflect.DeepEqual(privateKey.Lambda, big.NewInt(291060)) {
t.Errorf("Unexpected Lambda Public key value [%v]", privateKey.Lambda)
} }
} }
@@ -52,10 +59,9 @@ func TestEncryptDecryptSmall(t *testing.T) {
} }
returnedValue := privateKey.Decrypt(cypher) returnedValue := privateKey.Decrypt(cypher)
if !reflect.DeepEqual(inicialValue, returnedValue) { if !reflect.DeepEqual(inicialValue, returnedValue) {
t.Error("wrond decryption ", returnedValue, " is not ", inicialValue) t.Error("wrong decryption ", returnedValue, " is not ", inicialValue)
} }
} }
} }
func TestAddCypher(t *testing.T) { func TestAddCypher(t *testing.T) {
+1
View File
@@ -14,6 +14,7 @@ type ThresholdKey struct {
Threshold int Threshold int
V *big.Int V *big.Int
Vi []*big.Int Vi []*big.Int
G *big.Int // usually G is set to N+1
} }
// returns the value of (4*delta**2)** -1 mod n // returns the value of (4*delta**2)** -1 mod n