Commit 93af3bc5 authored by Thomas Hoffmann's avatar Thomas Hoffmann
Browse files

Fixing and stuff

parent aabf0978
...@@ -21,13 +21,13 @@ func main() { ...@@ -21,13 +21,13 @@ 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,
...@@ -43,11 +43,11 @@ func (alice *Party) RunAlice(x int) (result bool, err error) { ...@@ -43,11 +43,11 @@ func (alice *Party) RunAlice(x int) (result bool, err error) {
// 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.Encode(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.Decode(<-alice.conn, *alice.sKey) == 1 //Convert 1 -> true
return return
} }
...@@ -62,10 +62,11 @@ func (bob *Party) RunBob(x int) (err error) { ...@@ -62,10 +62,11 @@ func (bob *Party) RunBob(x int) (err error) {
//Receive ciphertexts from alice //Receive ciphertexts from alice
c_x := make([]*big.Int, 3) c_x := make([]*big.Int, 3)
for i := range c_x { for i := range c_x {
c_x[i] = <-bob.conn val := <-bob.conn
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
} }
...@@ -76,7 +77,7 @@ type Protocol struct { ...@@ -76,7 +77,7 @@ type Protocol struct {
} }
func NewProtocol() (p *Protocol, err error) { func NewProtocol() (p *Protocol, err error) {
conn := make(chan *big.Int) conn := make(chan big.Int)
sKey, HEp, err := homomorphic.NewProtocol(3,27) sKey, HEp, err := homomorphic.NewProtocol(3,27)
if err != nil { if err != nil {
return return
......
package main
import (
"crycomp/internal/blood"
"fmt"
"testing"
)
func TestBloodTable(t *testing.T) {
// Check the dimensions of BloodTable.
if len(blood.Table) != 8 {
t.Fatalf("Expected 8 rows, got %d", len(blood.Table))
}
for i := range blood.Table {
if len(blood.Table[i]) != 8 {
t.Fatalf("Expected columns in row %d, got %d", i, len(blood.Table))
}
}
}
func TestProtocol(t *testing.T) {
// Runs the protocol for all combinations of recipient and donor blood types.
n := len(blood.Table)
for x := 0; x < n; x++ {
for y := 0; y < n; y++ {
testName := fmt.Sprintf("(x=%s,y=%s)", blood.Names[x], blood.Names[y])
t.Run(testName, func(t *testing.T) {
z, err := RunProtocol(x, y)
if err != nil {
t.Errorf("Protocol error: %s", err)
} else if z != blood.Table[x][y] {
t.Fatalf("Failed blood compatibility test for index [%d,%d]. Expected %t, got %t", x, y, !z, z)
}
})
}
}
}
...@@ -14,9 +14,10 @@ type Protocol struct { ...@@ -14,9 +14,10 @@ type Protocol struct {
func NewProtocol(d, n int) (sKey *big.Int, pr Protocol, err error) { func NewProtocol(d, n int) (sKey *big.Int, pr Protocol, err error) {
s_int, b_int := big.NewInt(27), big.NewInt(27) s_int, b_int := big.NewInt(27), big.NewInt(27)
// TODO: Ensure large size sBytes := make([]byte, 250)
sKey, err = rand.Int(rand.Reader, b_int) _, err = rand.Read(sBytes)
if err != nil {return} if err != nil {return}
sBytes[len(sBytes)]
sKey = sKey.SetBit(sKey, 0, 1) sKey = sKey.SetBit(sKey, 0, 1)
q := make([]*big.Int, n) q := make([]*big.Int, n)
...@@ -56,21 +57,26 @@ func (p *Protocol) Encode(m int) (c *big.Int) { ...@@ -56,21 +57,26 @@ func (p *Protocol) Encode(m int) (c *big.Int) {
return return
} }
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) (res *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++ {
tmp[i].Mul(x[i],y[i]) xi := x[i]
tmp[i].Add(tmp[i],one) yi := y[i]
var val big.Int
val.Mul(xi,yi)
val.Add(&val,one)
tmp[i] = &val
} }
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 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