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

Fix length of p

parent 129dade7
...@@ -2,8 +2,8 @@ package main ...@@ -2,8 +2,8 @@ package main
import ( import (
"crycomp/internal/blood" "crycomp/internal/blood"
"crycomp/internal/crypto/util"
"crycomp/internal/crypto/homomorphic" "crycomp/internal/crypto/homomorphic"
"crycomp/internal/crypto/util"
"fmt" "fmt"
"math/big" "math/big"
) )
...@@ -21,42 +21,46 @@ func main() { ...@@ -21,42 +21,46 @@ func main() {
} }
type Party struct { type Party struct {
conn chan big.Int conn chan big.Int
x int x int
sKey *big.Int sKey *big.Int
HEProt homomorphic.Protocol HEProt homomorphic.Protocol
} }
func NewParty(conn chan big.Int, p homomorphic.Protocol) (*Party, error) { func NewParty(conn chan big.Int, p homomorphic.Protocol) (*Party, error) {
return &Party{ return &Party{
conn: conn, conn: conn,
HEProt : p, HEProt: p,
}, nil }, nil
} }
func (alice *Party) RunAlice(x int) (result bool, err error) { func (alice *Party) RunAlice(x int) (result bool, err error) {
xval := make([]int, 3) xval := make([]int, 3)
//Negate values //Negate values
for i, val := range util.Int2Bools(x,3) { for i, val := range util.Int2Bools(x, 3) {
if !val {xval[i] = 1} if !val {
xval[i] = 1
}
} }
// Send encoded input ints to bob // Send encoded input ints to bob
for i := 0 ; i < 3 ; i++ { for i := 0; i < 3; i++ {
alice.conn <- *alice.HEProt.Encode(xval[i]) alice.conn <- *alice.HEProt.Encrypt(xval[i])
} }
//Receive and decode result from bob //Receive and decode result from bob
result = alice.HEProt.Decode(<-alice.conn, *alice.sKey) == 1 //Convert 1 -> true result = alice.HEProt.Decrypt(<-alice.conn, *alice.sKey) == 1 //Convert 1 -> true
return return
} }
func (bob *Party) RunBob(x int) (err error) { func (bob *Party) RunBob(y int) (err error) {
c_y := make([]*big.Int,3) c_y := make([]*big.Int, 3)
for i, val := range util.Int2Bools(x,3) { for i, val := range util.Int2Bools(y, 3) {
bit := 0 bit := 0
if val {bit = 1} if val {
c_y[i] = bob.HEProt.Encode(bit) bit = 1
}
c_y[i] = bob.HEProt.Encrypt(bit)
} }
//Receive ciphertexts from alice //Receive ciphertexts from alice
...@@ -65,20 +69,18 @@ func (bob *Party) RunBob(x int) (err error) { ...@@ -65,20 +69,18 @@ func (bob *Party) RunBob(x int) (err error) {
val := <-bob.conn val := <-bob.conn
c_x[i] = &val c_x[i] = &val
} }
bob.conn <- *bob.HEProt.Eval(c_x,c_y) bob.conn <- *bob.HEProt.Eval(c_x, c_y)
return nil return nil
} }
type Protocol struct { type Protocol struct {
A, B *Party A, B *Party
} }
func NewProtocol() (p *Protocol, err error) { func NewProtocol() (p *Protocol, err error) {
conn := make(chan big.Int) conn := make(chan big.Int)
params := homomorphic.NewParams(200,100000, 1000000,200) params := homomorphic.NewParams(200, 100000, 1000000, 200)
sKey, HEp, err := homomorphic.NewProtocol(params) sKey, HEp, err := homomorphic.NewProtocol(params)
if err != nil { if err != nil {
return return
...@@ -118,4 +120,4 @@ func RunProtocol(x, y int) (z bool, err error) { ...@@ -118,4 +120,4 @@ func RunProtocol(x, y int) (z bool, err error) {
z, err = p.A.RunAlice(x) z, err = p.A.RunAlice(x)
return return
} }
\ No newline at end of file
package homomorphic package homomorphic
import ( import (
"crycomp/internal/crypto/util"
"crypto/rand" "crypto/rand"
"math/big" "math/big"
mRand "math/rand" mRand "math/rand"
) )
type Params struct { type Params struct {
len_sKey int pLen int
len_q int qLen int
len_r int rLen int
m int m int
} }
func NewParams(len_sKey, len_q, len_r, m int) *Params {
func NewParams(pLen, qLen, rLen, m int) *Params {
return &Params{ return &Params{
len_sKey : len_sKey, pLen: pLen,
len_q : len_q, qLen: qLen,
len_r : len_r, rLen: rLen,
m : m, m: m,
} }
} }
...@@ -27,32 +27,62 @@ type Protocol struct { ...@@ -27,32 +27,62 @@ type Protocol struct {
params *Params params *Params
} }
func NewProtocol(params *Params) (sKey *big.Int, pr Protocol, err error) { // Returns a random integer with the specified bit length.
//set private key to a random odd 2000-bit integer func randInt(bitLen int) (n *big.Int, err error) {
res, err := util.RandInt(rand.Reader, big.NewInt(int64(params.len_sKey))) // Calculate how many bytes are needed to have bitLen bits.
if err != nil {return} bytesLen := (bitLen + 7) / 8
res.SetBit(res, 0, 1) // Fill bytes with random data
sKey = res pBytes := make([]byte, bytesLen)
_, err = rand.Read(pBytes)
if err != nil {
return
}
// Clear the bits that exceed bitLen
pBytes[0] &= 0xff >> (8*len(pBytes) - bitLen)
n = new(big.Int).SetBytes(pBytes)
// Set the bitLen-1 bit. This ensures bitLen bits
// and the distribution is maintained (all other bits are random)
n.SetBit(n, bitLen-1, 1)
return
}
func NewProtocol(params *Params) (p *big.Int, pr Protocol, err error) {
// Choose a random big secret odd integer p
p, err = randInt(params.pLen)
if err != nil {
return
}
p.SetBit(p, 0, 1) // Make p odd.
// res, err := util.RandInt(rand.Reader, big.NewInt(int64(params.pLen)))
// if err != nil {
// return
// }
// res.SetBit(res, 0, 1)
q := make([]*big.Int, params.m) q := make([]*big.Int, params.m)
r := make([]*big.Int, params.m) r := make([]*big.Int, params.m)
y := make([]*big.Int, params.m) y := make([]*big.Int, params.m)
for i := range q { for i := range q {
q[i], err = rand.Int(rand.Reader, big.NewInt(int64(params.len_q))) q[i], err = rand.Int(rand.Reader, big.NewInt(int64(params.qLen)))
if err != nil {return} if err != nil {
r[i], err = rand.Int(rand.Reader, big.NewInt(int64(params.len_r))) return
if err != nil {return} }
r[i], err = rand.Int(rand.Reader, big.NewInt(int64(params.rLen)))
if err != nil {
return
}
right := big.NewInt(2) right := big.NewInt(2)
right.Mul(right,r[i]) right.Mul(right, r[i])
left := right.Mul(sKey,q[i]) left := right.Mul(p, q[i])
y[i] = right.Add(left, right) y[i] = right.Add(left, right)
} }
pr = Protocol{ pr = Protocol{
pubKey : y, pubKey: y,
params : params, params: params,
} }
return sKey, pr, err return
} }
func (p *Protocol) Encode(m int) (c *big.Int) { func (p *Protocol) Encode(m int) (c *big.Int) {
...@@ -60,7 +90,7 @@ func (p *Protocol) Encode(m int) (c *big.Int) { ...@@ -60,7 +90,7 @@ func (p *Protocol) Encode(m int) (c *big.Int) {
for i := 0; i < p.params.m; i++ { for i := 0; i < p.params.m; i++ {
S[i] = i S[i] = i
} }
mRand.Shuffle(p.params.m, func(i,j int) {S[i],S[j] = S[j], S[i]}) mRand.Shuffle(p.params.m, func(i, j int) { S[i], S[j] = S[j], S[i] })
S = S[:p.params.m] S = S[:p.params.m]
c = big.NewInt(int64(m)) c = big.NewInt(int64(m))
for i := range S { for i := range S {
...@@ -70,25 +100,25 @@ func (p *Protocol) Encode(m int) (c *big.Int) { ...@@ -70,25 +100,25 @@ func (p *Protocol) Encode(m int) (c *big.Int) {
} }
func (p *Protocol) Decode(c, sKey big.Int) int { func (p *Protocol) Decode(c, sKey big.Int) int {
tmp:= c.Mod(&c, &sKey) tmp := c.Mod(&c, &sKey)
m := tmp.Mod(tmp, big.NewInt(2)) m := tmp.Mod(tmp, big.NewInt(2))
return int(m.Int64()) return int(m.Int64())
} }
// Assumes x inputs are negated, so Eval can compute ¬(¬a+b) = a+¬b // Assumes x inputs are negated, so Eval can compute ¬(¬a+b) = a+¬b
func (p *Protocol) Eval(x,y []*big.Int) *big.Int { func (p *Protocol) Eval(x, y []*big.Int) *big.Int {
one := p.Encode(1) one := p.Encode(1)
tmp := make([]*big.Int,3) tmp := make([]*big.Int, 3)
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
xi := x[i] xi := x[i]
yi := y[i] yi := y[i]
var val big.Int var val big.Int
val.Mul(xi,yi) val.Mul(xi, yi)
val.Add(&val,one) val.Add(&val, one)
tmp[i] = &val tmp[i] = &val
} }
var res big.Int var res big.Int
res.Mul(tmp[0],tmp[1]) res.Mul(tmp[0], tmp[1])
res.Mul(&res,tmp[2]) res.Mul(&res, tmp[2])
return &res return &res
} }
\ No newline at end of file
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