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
9dd8f7ad
Commit
9dd8f7ad
authored
Oct 03, 2021
by
Thomas Hoffmann
Browse files
Merge branch 'main' of gitlab.au.dk:au583826/crycomp into main
parents
a989090f
186f27d8
Changes
5
Hide whitespace changes
Inline
Side-by-side
cmd/handin5/main.go
View file @
9dd8f7ad
...
...
@@ -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,14 +61,13 @@ 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
)
...
...
@@ -73,12 +76,12 @@ func RunProtocol(x, y int, outputMode int) (bool, error) {
fmt
.
Printf
(
"Running protocol with [%d,%d]
\n
"
,
x
,
y
)
go
func
(
Bob
*
Party
){
// 1. Garble
Bob
.
GenerateCircuit
()
Bob
.
GenerateCircuit
()
Bob
.
CircuitChan
<-
*
Bob
.
Circuit
.
F
fmt
.
Printf
(
"Bob has generated the circuit and sent F to alice
\n
"
)
// 2. Encode Bobs input
Bob
.
EncInput
=
garbled
.
Enc
(
Bob
.
Circuit
.
E
[
:
3
],
Bob
.
x
)
Bob
.
InChan
<-
Bob
.
EncInput
fmt
.
Printf
(
"Bob has garbled his input and sent it to alice
\n
"
)
// 3. Encode Alices input
...
...
@@ -101,12 +104,11 @@ func RunProtocol(x, y int, outputMode int) (bool, error) {
//go func (Alice *Party) {
// 1. Garble
F
:=
<-
Alice
.
CircuitChan
Alice
.
Circuit
=
&
garbled
.
GCircuit
{
F
:
&
F
}
fmt
.
Printf
(
"Alice has received F
\n
"
)
// 2. Encode Bobs input
Alice
.
EncInput
=
<-
Alice
.
InChan
fmt
.
Printf
(
"Alice has received Bobs input
\n
"
)
Alice
.
EncInput
=
<-
Alice
.
InChan
// 3. Encode Alices input
X
,
_
:=
Alice
.
OTReceive
()
Alice
.
EncInput
=
append
(
Alice
.
EncInput
,
X
...
)
...
...
@@ -120,7 +122,7 @@ func RunProtocol(x, y int, outputMode int) (bool, error) {
// 5. Output
if
outputMode
!=
1
{
// Alice learns
// Bob sends d
Alice
.
result
=
garbled
.
Decode
(
<-
Alice
.
InChan
,
Z
)
Alice
.
result
=
garbled
.
Decode
(
<-
Alice
.
InChan
,
Z
)
fmt
.
Printf
(
"Alice has learned the output
\n
"
)
}
if
outputMode
!=
0
{
// Bob learns
...
...
@@ -147,4 +149,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 @
9dd8f7ad
...
...
@@ -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 @
9dd8f7ad
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 @
9dd8f7ad
...
...
@@ -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 @
9dd8f7ad
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