Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Thomas Hoffmann
CryComp
Commits
186f27d8
Commit
186f27d8
authored
Oct 03, 2021
by
Anders Jensen Løvig
Browse files
Change testing a bit
parent
552f26be
Changes
5
Hide whitespace changes
Inline
Side-by-side
cmd/handin5/main.go
View file @
186f27d8
...
...
@@ -3,8 +3,8 @@ package main
import
(
"crycomp/internal/blood"
"crycomp/internal/crypto/oblivious"
"crycomp/internal/crypto/garbled"
"crycomp/internal/crypto/oblivious"
"crycomp/internal/util"
"fmt"
)
...
...
@@ -12,24 +12,24 @@ import (
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
type
Party
struct
{
x
[]
bool
Circuit
*
garbled
.
GCircuit
InChan
chan
[][]
byte
x
[]
bool
Circuit
*
garbled
.
GCircuit
InChan
chan
[][]
byte
CircuitChan
chan
garbled
.
Table
EncInput
[][]
byte
EncInput
[][]
byte
*
oblivious
.
Party
result
bool
result
bool
outChan
chan
[]
byte
}
func
NewParty
(
x
int
,
inChan
chan
[][]
byte
,
circuitChan
chan
garbled
.
Table
,
outChan
chan
[]
byte
,
party
*
oblivious
.
Party
)
(
p
*
Party
){
func
NewParty
(
x
int
,
inChan
chan
[][]
byte
,
circuitChan
chan
garbled
.
Table
,
outChan
chan
[]
byte
,
party
*
oblivious
.
Party
)
(
p
*
Party
)
{
return
&
Party
{
x
:
util
.
Int2Bools
(
x
,
3
),
EncInput
:
make
([][]
byte
,
6
),
InChan
:
inChan
,
outChan
:
outChan
,
CircuitChan
:
circuitChan
,
Party
:
party
,
x
:
util
.
Int2Bools
(
x
,
3
),
EncInput
:
make
([][]
byte
,
6
),
InChan
:
inChan
,
outChan
:
outChan
,
CircuitChan
:
circuitChan
,
Party
:
party
,
}
}
...
...
@@ -43,12 +43,16 @@ func (p *Party) OTSend() {
}
}
func
(
p
*
Party
)
OTReceive
()
(
res
[][]
byte
,
err
error
){
func
(
p
*
Party
)
OTReceive
()
(
res
[][]
byte
,
err
error
)
{
res
=
make
([][]
byte
,
3
)
var
val
int
for
i
:=
range
p
.
x
{
if
p
.
x
[
i
]
{
val
=
1
}
else
{
val
=
0
}
var
result
,
err
=
p
.
ObliviousTransfer
(
2
,
val
)
if
p
.
x
[
i
]
{
val
=
1
}
else
{
val
=
0
}
var
result
,
err
=
p
.
ObliviousTransfer
(
2
,
val
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -57,27 +61,26 @@ func (p *Party) OTReceive() (res [][]byte, err error){
return
res
,
nil
}
func
OTransfer
(
sender
,
receiver
*
Party
)
{
func
OTransfer
(
sender
,
receiver
*
Party
)
{
sender
.
Party
,
receiver
.
Party
=
oblivious
.
NewPartyPair
()
//sender.Listen(sender.Circuit.e)
}
// RunProtocol runs the protocol between receiving blood type x and donor blood
// type y.
// type y.
// outputMode: 0: Alice learns, 1: Bob learns else both learns
func
RunProtocol
(
x
,
y
int
,
outputMode
int
)
(
bool
,
error
)
{
inCh
,
circCh
,
outCh
:=
make
(
chan
[][]
byte
),
make
(
chan
garbled
.
Table
),
make
(
chan
[]
byte
)
p1
,
p2
:=
oblivious
.
NewPartyPair
()
Alice
,
Bob
:=
NewParty
(
x
,
inCh
,
circCh
,
outCh
,
p1
),
NewParty
(
y
,
inCh
,
circCh
,
outCh
,
p2
)
go
func
(
Bob
*
Party
){
go
func
(
Bob
*
Party
)
{
// 1. Garble
Bob
.
GenerateCircuit
()
Bob
.
GenerateCircuit
()
Bob
.
CircuitChan
<-
*
Bob
.
Circuit
.
F
// 2. Encode Bobs input
Bob
.
EncInput
=
garbled
.
Enc
(
Bob
.
Circuit
.
E
[
:
3
],
Bob
.
x
)
Bob
.
InChan
<-
Bob
.
EncInput
// 3. Encode Alices input
Bob
.
OTSend
()
...
...
@@ -88,33 +91,33 @@ func RunProtocol(x, y int, outputMode int) (bool, error) {
}
if
outputMode
!=
0
{
// Bob learns
// Alice sends Z
Bob
.
result
=
garbled
.
Decode
(
Bob
.
Circuit
.
D
,
<-
Bob
.
outChan
)
Bob
.
result
=
garbled
.
Decode
(
Bob
.
Circuit
.
D
,
<-
Bob
.
outChan
)
}
}(
Bob
)
go
func
(
Alice
*
Party
)
{
go
func
(
Alice
*
Party
)
{
// 1. Garble
F
:=
<-
Alice
.
CircuitChan
Alice
.
Circuit
=
&
garbled
.
GCircuit
{
F
:
&
F
}
// 2. Encode Bobs input
Alice
.
EncInput
=
<-
Alice
.
InChan
Alice
.
EncInput
=
<-
Alice
.
InChan
// 3. Encode Alices input
X
,
_
:=
Alice
.
OTReceive
()
Alice
.
EncInput
=
append
(
Alice
.
EncInput
,
X
...
)
// 4. Evaluation
Z
:=
garbled
.
Eval
(
Alice
.
Circuit
.
F
,
Alice
.
EncInput
)
Z
:=
garbled
.
Eval
(
Alice
.
Circuit
.
F
,
Alice
.
EncInput
)
// 5. Output
if
outputMode
!=
1
{
// Alice learns
// Bob sends d
Alice
.
result
=
garbled
.
Decode
(
<-
Alice
.
InChan
,
Z
)
Alice
.
result
=
garbled
.
Decode
(
<-
Alice
.
InChan
,
Z
)
}
if
outputMode
!=
0
{
// Bob learns
// Alice sends Z
Alice
.
outChan
<-
Z
}
}(
Alice
)
if
outputMode
!=
1
{
if
outputMode
!=
1
{
return
Alice
.
result
,
nil
}
else
{
return
Bob
.
result
,
nil
...
...
@@ -131,4 +134,4 @@ func main() {
}
else
{
fmt
.
Printf
(
"Protocol failed, output was %t, but should be %t"
,
z
,
blood
.
Table
[
bloodA
][
bloodB
])
}
}
\ No newline at end of file
}
cmd/handin5/main_test.go
View file @
186f27d8
...
...
@@ -6,20 +6,6 @@ import (
"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
)
...
...
internal/blood/blood_test.go
0 → 100644
View file @
186f27d8
package
blood
import
"testing"
func
TestBloodTable
(
t
*
testing
.
T
)
{
// Check the dimensions of BloodTable.
if
len
(
Table
)
!=
8
{
t
.
Fatalf
(
"Expected 8 rows, got %d"
,
len
(
Table
))
}
for
i
:=
range
Table
{
if
len
(
Table
[
i
])
!=
8
{
t
.
Fatalf
(
"Expected columns in row %d, got %d"
,
i
,
len
(
Table
))
}
}
}
internal/util/util.go
View file @
186f27d8
...
...
@@ -23,13 +23,14 @@ func Int2Bools(x, n int) []bool {
return
output
}
func
XOR
(
a
,
b
[]
byte
)
(
c
[]
byte
)
{
// XOR computes the byte-wise XOR of two byte slices. Slices must hve equal length.
func
XOR
(
a
,
b
[]
byte
)
(
c
[]
byte
)
{
if
len
(
a
)
!=
len
(
b
)
{
panic
(
"
Parameters
length must be equal"
)
panic
(
"length must be equal"
)
}
c
=
make
([]
byte
,
len
(
a
))
for
i
:=
range
a
{
c
[
i
]
=
a
[
i
]
^
b
[
i
]
}
return
}
\ No newline at end of file
}
internal/util/util_test.go
0 → 100644
View file @
186f27d8
package
util
import
(
"crycomp/internal/blood"
"testing"
)
func
AssertBloodType
(
t
*
testing
.
T
,
bloodType
int
,
expected
[]
bool
)
{
actual
:=
Int2Bools
(
bloodType
,
3
)
if
len
(
actual
)
!=
len
(
expected
)
{
t
.
Fatalf
(
"Expected length to be %d, was %d"
,
len
(
expected
),
len
(
actual
))
}
for
i
:=
0
;
i
<
3
;
i
++
{
if
actual
[
i
]
!=
expected
[
i
]
{
t
.
Errorf
(
"Expected %t at index %d, got %t"
,
expected
[
i
],
i
,
actual
[
i
])
}
}
}
func
TestInt2Bools
(
t
*
testing
.
T
)
{
t
.
Run
(
"O-"
,
func
(
t
*
testing
.
T
)
{
AssertBloodType
(
t
,
blood
.
Type_On
,
[]
bool
{
false
,
false
,
false
})
})
t
.
Run
(
"O+"
,
func
(
t
*
testing
.
T
)
{
AssertBloodType
(
t
,
blood
.
Type_Op
,
[]
bool
{
true
,
false
,
false
})
})
t
.
Run
(
"A-"
,
func
(
t
*
testing
.
T
)
{
AssertBloodType
(
t
,
blood
.
Type_An
,
[]
bool
{
false
,
true
,
false
})
})
t
.
Run
(
"A+"
,
func
(
t
*
testing
.
T
)
{
AssertBloodType
(
t
,
blood
.
Type_Ap
,
[]
bool
{
true
,
true
,
false
})
})
t
.
Run
(
"B-"
,
func
(
t
*
testing
.
T
)
{
AssertBloodType
(
t
,
blood
.
Type_Bn
,
[]
bool
{
false
,
false
,
true
})
})
t
.
Run
(
"B+"
,
func
(
t
*
testing
.
T
)
{
AssertBloodType
(
t
,
blood
.
Type_Bp
,
[]
bool
{
true
,
false
,
true
})
})
t
.
Run
(
"AB-"
,
func
(
t
*
testing
.
T
)
{
AssertBloodType
(
t
,
blood
.
Type_ABn
,
[]
bool
{
false
,
true
,
true
})
})
t
.
Run
(
"AB+"
,
func
(
t
*
testing
.
T
)
{
AssertBloodType
(
t
,
blood
.
Type_ABp
,
[]
bool
{
true
,
true
,
true
})
})
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment