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

Fixing and stuff

parent aabf0978
......@@ -21,13 +21,13 @@ func main() {
}
type Party struct {
conn chan *big.Int
conn chan big.Int
x int
sKey *big.Int
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{
conn: conn,
HEProt : p,
......@@ -43,11 +43,11 @@ func (alice *Party) RunAlice(x int) (result bool, err error) {
// Send encoded input ints to bob
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
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
}
......@@ -62,10 +62,11 @@ func (bob *Party) RunBob(x int) (err error) {
//Receive ciphertexts from alice
c_x := make([]*big.Int, 3)
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
}
......@@ -76,7 +77,7 @@ type Protocol struct {
}
func NewProtocol() (p *Protocol, err error) {
conn := make(chan *big.Int)
conn := make(chan big.Int)
sKey, HEp, err := homomorphic.NewProtocol(3,27)
if err != nil {
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 {
func NewProtocol(d, n int) (sKey *big.Int, pr Protocol, err error) {
s_int, b_int := big.NewInt(27), big.NewInt(27)
// TODO: Ensure large size
sKey, err = rand.Int(rand.Reader, b_int)
sBytes := make([]byte, 250)
_, err = rand.Read(sBytes)
if err != nil {return}
sBytes[len(sBytes)]
sKey = sKey.SetBit(sKey, 0, 1)
q := make([]*big.Int, n)
......@@ -56,21 +57,26 @@ func (p *Protocol) Encode(m int) (c *big.Int) {
return
}
func (p *Protocol) Decode(c, sKey *big.Int,) int {
tmp:= c.Mod(c, sKey)
func (p *Protocol) Decode(c, sKey big.Int) int {
tmp:= c.Mod(&c, &sKey)
m := tmp.Mod(tmp, big.NewInt(2))
return int(m.Int64())
}
// 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)
tmp := make([]*big.Int,3)
for i := 0; i < 3; i++ {
tmp[i].Mul(x[i],y[i])
tmp[i].Add(tmp[i],one)
xi := x[i]
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(res,tmp[2])
return
res.Mul(&res,tmp[2])
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