Commit db392838 authored by Anders Jensen Løvig's avatar Anders Jensen Løvig
Browse files

Some more refactoring

parent 505e337c
......@@ -36,8 +36,11 @@ func (alice *Party) RunAlice(x int, priv *homomorphic.PrivateKey) (result bool,
// Send encoded input ints to bob
for i := 0; i < 3; i++ {
alice.conn <- homomorphic.Encrypt(xval[i], &priv.PublicKey)
// alice.conn <- *alice.HEProt.Encode(xval[i])
c, err := homomorphic.Encrypt(rand.Reader, xval[i], &priv.PublicKey)
if err != nil {
return false, err
}
alice.conn <- c
}
//Receive and decode result from bob
......@@ -53,7 +56,10 @@ func (bob *Party) RunBob(y int, pub *homomorphic.PublicKey) (err error) {
if val {
bit = 1
}
c_y[i] = homomorphic.Encrypt(bit, pub)
c_y[i], err = homomorphic.Encrypt(rand.Reader, bit, pub)
if err != nil {
return
}
// c_y[i] = bob.HEProt.Encode(bit)
}
......@@ -63,9 +69,30 @@ func (bob *Party) RunBob(y int, pub *homomorphic.PublicKey) (err error) {
c_x[i] = <-bob.conn
}
bob.conn <- homomorphic.Evaluate(c_x, c_y, pub)
c, err := evaluate(c_x, c_y, pub)
if err != nil {
return
}
bob.conn <- c
// bob.conn <- *bob.HEProt.Eval(c_x, c_y)
return nil
return
}
func evaluate(x, y []*big.Int, pub *homomorphic.PublicKey) (result *big.Int, err error) {
one, err := homomorphic.Encrypt(rand.Reader, 1, pub)
if err != nil {
return
}
tmp := make([]*big.Int, 3)
for i := 0; i < 3; i++ {
v := new(big.Int).Mul(x[i], y[i])
tmp[i] = v.Add(v, one)
}
result = new(big.Int).Mul(tmp[0], tmp[1])
result.Mul(result, tmp[2])
return
}
// RunProtocol runs the protocol between receiving blood type x and donor blood
......
......@@ -4,7 +4,6 @@ import (
"crycomp/internal/crypto/util"
"io"
"math/big"
mRand "math/rand"
)
var two = big.NewInt(2)
......@@ -69,15 +68,19 @@ func GenerateKey(random io.Reader, pLen, qLen, rLen, n int) (priv *PrivateKey, e
return
}
func Encrypt(m int, pub *PublicKey) (c *big.Int) {
func Encrypt(random io.Reader, m int, pub *PublicKey) (c *big.Int, err error) {
// Sample random subset S of [0...n]
S := make([]int, pub.n)
for i := 0; i < pub.n; i++ {
S[i] = i
}
util.Shuffle(pub.n, func(i, j int) { S[i], S[j] = S[j], S[i] })
sLen, err := util.RandIntn(random, big.NewInt(int64(pub.n)))
if err != nil {
return
}
S = S[:sLen.Int64()]
// TODO crypto rand
mRand.Shuffle(pub.n, func(i, j int) { S[i], S[j] = S[j], S[i] })
S = S[:pub.n]
c = big.NewInt(int64(m))
for i := range S {
c = c.Add(c, pub.y[S[i]])
......@@ -90,17 +93,3 @@ func Decrypt(c *big.Int, priv *PrivateKey) int {
m.Mod(m, two)
return int(m.Int64())
}
func Evaluate(x, y []*big.Int, pub *PublicKey) (result *big.Int) {
one := Encrypt(1, pub)
tmp := make([]*big.Int, 3)
for i := 0; i < 3; i++ {
v := new(big.Int).Mul(x[i], y[i])
tmp[i] = v.Add(v, one)
}
result = new(big.Int).Mul(tmp[0], tmp[1])
result.Mul(result, tmp[2])
return
}
......@@ -81,6 +81,10 @@ func Perm(n int) []int {
return mRand.New(&cryptoSource{}).Perm(n)
}
func Shuffle(n int, swap func(i, j int)) {
mRand.New(&cryptoSource{}).Shuffle(n, swap)
}
func (s *cryptoSource) Int63() int64 {
_, err := cRand.Read(s[:])
if err != nil {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment