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

Started handin 4

parent 23d526ed
package main
import (
"crycomp/internal/blood"
"fmt"
"math/big"
"golang.org/x/crypto/openpgp/elgamal"
)
type Encryption struct {
C1, C2 *big.Int
}
// Party represents one of the parties in the protocol. They contain a slice
// of all known wires and channels for communication with the other party.
type Party struct {
keyChan chan []*elgamal.PublicKey
encChan chan []*Encryption
x int
}
// NewParty returns a new party that communicates on the specified channels.
func NewParty(x int, keyChan chan []*elgamal.PublicKey, encChan chan []*Encryption) *Party {
return &Party{
x: x,
keyChan: keyChan,
encChan: encChan,
}
}
func (p *Party) Listen() {
_, ok := <-p.keyChan
if !ok {
panic("key channel closed unexpectedly")
}
// TODO enc column y
var encs []*Encryption
p.encChan <- encs
}
func (p *Party) ObliviousTransfer() bool {
return false // TODO
}
type Protocol struct {
Receiver, Sender *Party
}
func NewProtocol(x, y int) *Protocol {
return nil // TODO
}
// RunProtocol runs the protocol between receiving blood type x and donor blood
// type y.
func RunProtocol(x, y int) bool {
p := NewProtocol(x, y)
go func() {
p.Sender.Listen()
}()
return p.Receiver.ObliviousTransfer()
}
func main() {
bloodA, bloodB := blood.Type_ABn, blood.Type_ABp
z := RunProtocol(bloodA, bloodB)
if z == blood.Table[bloodA][bloodB] {
fmt.Println("Protocol succeded")
} else {
fmt.Printf("Protocol failed, output was %t, but should be %t", z, blood.Table[bloodA][bloodB])
}
}
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) {
if z := RunProtocol(x, y); z != blood.Table[x][y] {
t.Fatalf("Failed blood compatibility test for index [%d,%d]. Expected %t, got %t", x, y, !z, z)
}
})
}
}
}
module crycomp
go 1.17
require golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
package blood
// Enumeration of blood types. The 'n'-suffix means negative and 'p'-suffix means positive.
const (
Type_On = 0
Type_Op = 1
Type_An = 2
Type_Ap = 3
Type_Bn = 4
Type_Bp = 5
Type_ABn = 6
Type_ABp = 7
)
var Names []string = []string{"O-", "O+", "A-", "A+", "B-", "B+", "AB-", "AB+"}
// BloodTable contains the blood type compatibility truth table. Rows are the
// recipient's blood type and the columns are the donor's blood type.
//
// The table contains bools, since we compute a boolean function. Duh!
var Table = [][]bool{
{true, false, false, false, false, false, false, false},
{true, true, false, false, false, false, false, false},
{true, false, true, false, false, false, false, false},
{true, true, true, true, false, false, false, false},
{true, false, false, false, true, false, false, false},
{true, true, false, false, true, true, false, false},
{true, false, true, false, true, false, true, false},
{true, true, true, true, true, true, true, true},
}
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