Let to pass an arbitrary number of cyphers to PublicKey.Add #5

Merged
pdyraga merged 5 commits from master into master 2018-07-03 18:53:09 -04:00
Showing only changes of commit d439831ffd - Show all commits
+10 -10
View File
@@ -10,11 +10,11 @@ type PublicKey struct {
N *big.Int N *big.Int
} }
func (this *PublicKey) GetNSquare() *big.Int { func (pk *PublicKey) GetNSquare() *big.Int {
return new(big.Int).Mul(this.N, this.N) return new(big.Int).Mul(pk.N, pk.N)
} }
// Encode a plaintext in a cypher one. The plain text must be smaller that // Encrypt a plaintext into 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.
// //
@@ -24,19 +24,19 @@ func (this *PublicKey) GetNSquare() *big.Int {
// See [KL 08] construction 11.32, page 414. // 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 (pk *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Cypher, error) {
r, err := GetRandomNumberInMultiplicativeGroup(pub.N, random) r, err := GetRandomNumberInMultiplicativeGroup(pk.N, random)
if err != nil { if err != nil {
return nil, err return nil, err
} }
nSquare := pub.GetNSquare() nSquare := pk.GetNSquare()
// g is _always_ equal n+1 // g is _always_ equal n+1
// Threshold encryption is safe only for g=n+1 choice. // Threshold encryption is safe only for g=n+1 choice.
// See [DJN 10], section 5.1 // See [DJN 10], section 5.1
g := new(big.Int).Add(pub.N, big.NewInt(1)) g := new(big.Int).Add(pk.N, big.NewInt(1))
gm := new(big.Int).Exp(g, m, nSquare) gm := new(big.Int).Exp(g, m, nSquare)
rn := new(big.Int).Exp(r, pub.N, 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 &Cypher{new(big.Int).Mod(new(big.Int).Mul(rn, gm), nSquare)}, nil
} }
@@ -48,13 +48,13 @@ func (pub *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Cypher, error) {
// plaintexts: // plaintexts:
// //
// D( (E(m1) * E(m2) mod n^2) ) = m1 + m2 mod n // D( (E(m1) * E(m2) mod n^2) ) = m1 + m2 mod n
func (pub *PublicKey) Add(cypher ...*Cypher) *Cypher { func (pk *PublicKey) Add(cypher ...*Cypher) *Cypher {
accumulator := big.NewInt(1) accumulator := big.NewInt(1)
for _, c := range cypher { for _, c := range cypher {
accumulator = new(big.Int).Mod( accumulator = new(big.Int).Mod(
new(big.Int).Mul(accumulator, c.C), new(big.Int).Mul(accumulator, c.C),
pub.GetNSquare(), pk.GetNSquare(),
) )
} }