Skip to content
GitLab
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
3efc310f
Commit
3efc310f
authored
Sep 19, 2021
by
Anders Jensen Løvig
Browse files
TestProtocol
parent
fa385a77
Changes
2
Hide whitespace changes
Inline
Side-by-side
cmd/handin3/main.go
View file @
3efc310f
...
...
@@ -18,6 +18,8 @@ const (
BloodType_ABp
=
7
)
var
BloodNames
[]
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.
//
...
...
@@ -33,8 +35,8 @@ var BloodTable = [][]bool{
{
true
,
true
,
true
,
true
,
true
,
true
,
true
,
true
},
}
//
Int2Bools
return x as a slice containing the binary representation.
func
Int2Bools
(
x
,
bits
int
)
[]
bool
{
//
EncodeBloodType
return x as a slice containing the binary representation.
func
EncodeBloodType
(
x
,
bits
int
)
[]
bool
{
output
:=
make
([]
bool
,
0
)
tmp
:=
0
for
i
:=
bits
-
1
;
i
>=
0
;
i
--
{
...
...
@@ -223,22 +225,31 @@ func NewProtocol() *Protocol {
}
}
func
(
P
*
Protocol
)
Run
(
x
,
y
[]
bool
)
bool
{
//alice := P.A
//bob := P.B
if
len
(
x
)
!=
len
(
y
)
{
return
false
// Run the protocol for blood type compatibility using a passive secure BeDOZa
// protocol.
//
// The function computed is the following:
// f(x,y) = (x_sign OR NOT y_sign) AND
// (x_A OR NOT y_A) AND
// (x_B OR NOT y_B)
//
// The inputs are the binary encoding of the blood types, with x being Alice's
// blood type and y being Bob's blood type. The encodings must have length 3,
// where index 2 indiciates blood sign, index 1 indicates if bloods has A and
// index 0 indicates if blood have B.
//
func
(
P
*
Protocol
)
Run
(
x
,
y
[]
bool
)
(
bool
,
error
)
{
if
lenX
:=
len
(
x
);
lenX
!=
3
{
return
false
,
fmt
.
Errorf
(
"length of x-encoding must be 3, was %d"
,
lenX
)
}
if
lenY
:=
len
(
y
);
lenY
!=
3
{
return
false
,
fmt
.
Errorf
(
"length of y-encoding must be 3, was %d"
,
lenY
)
}
//Input round
var
idx_s
,
_
=
P
.
Input
(
x
[
2
],
y
[
2
])
var
idx_A
,
_
=
P
.
Input
(
x
[
1
],
y
[
1
])
var
idx_B
,
_
=
P
.
Input
(
x
[
0
],
y
[
0
])
//
Compute f(x,y) =
// (x_sign OR NOT y_sign) AND
// (x_A OR NOT y_A) AND
// (x_B OR NOT y_B
)
//
Input round
idx_s
,
_
:=
P
.
Input
(
x
[
0
],
y
[
0
])
idx_A
,
_
:=
P
.
Input
(
x
[
1
],
y
[
1
])
idx_B
,
_
:=
P
.
Input
(
x
[
2
],
y
[
2
]
)
//a. Compute (x_sign OR NOT y_sign) = NOT (NOT x_sign AND y_sign)
//var idx_f1 = alice.XORC(idx_s, true , true) //compute NOT x_sign
...
...
@@ -260,25 +271,26 @@ func (P *Protocol) Run(x, y []bool) bool {
var
idx_f
=
P
.
AND
(
idx_f1
,
idx_f2
)
idx_f
=
P
.
AND
(
idx_f
,
idx_f3
)
var
output
=
P
.
Output
(
idx_f
,
true
,
false
)
return
output
[
0
]
return
output
[
0
]
,
nil
}
func
RunProtocol
(
x
,
y
int
)
bool
{
func
RunProtocol
(
x
,
y
int
)
(
bool
,
error
)
{
protocol
:=
NewProtocol
()
encodingX
:=
Int2Bools
(
x
,
3
)
encodingY
:=
Int2Bools
(
y
,
3
)
encodingX
:=
EncodeBloodType
(
x
,
3
)
encodingY
:=
EncodeBloodType
(
y
,
3
)
return
protocol
.
Run
(
encodingX
,
encodingY
)
}
func
main
()
{
bloodA
,
bloodB
:=
BloodType_ABn
,
BloodType_ABp
z
:=
RunProtocol
(
bloodA
,
bloodB
)
z
,
err
:=
RunProtocol
(
bloodA
,
bloodB
)
if
z
==
BloodTable
[
bloodA
][
bloodB
]
{
if
err
!=
nil
{
fmt
.
Println
(
"Protocol failed: "
,
err
)
}
else
if
z
==
BloodTable
[
bloodA
][
bloodB
]
{
fmt
.
Println
(
"Protocol succeded"
)
fmt
.
Println
(
z
)
}
else
{
fmt
.
Printf
(
"Protocol failed, output was %t, but should be %t"
,
z
,
BloodTable
[
bloodA
][
bloodB
])
}
...
...
cmd/handin3/main_test.go
View file @
3efc310f
package
main
import
(
"fmt"
"testing"
)
func
CheckBloodType
(
t
*
testing
.
T
,
bloodType
int
,
expected
[]
bool
)
{
actual
:=
Int2Bools
(
bloodType
,
3
)
actual
:=
EncodeBloodType
(
bloodType
,
3
)
if
len
(
actual
)
!=
len
(
expected
)
{
t
.
Fatalf
(
" Expected length to be %d, was %d"
,
len
(
expected
),
len
(
actual
))
...
...
@@ -20,23 +21,30 @@ func CheckBloodType(t *testing.T, bloodType int, expected []bool) {
func
TestInt2Bools
(
t
*
testing
.
T
)
{
t
.
Run
(
"O-"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_On
,
[]
bool
{
false
,
false
,
false
})
})
t
.
Run
(
"O+"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_Op
,
[]
bool
{
false
,
false
,
true
})
})
t
.
Run
(
"O+"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_Op
,
[]
bool
{
true
,
false
,
false
})
})
t
.
Run
(
"A-"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_An
,
[]
bool
{
false
,
true
,
false
})
})
t
.
Run
(
"A+"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_Ap
,
[]
bool
{
fals
e
,
true
,
tru
e
})
})
t
.
Run
(
"B-"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_Bn
,
[]
bool
{
true
,
false
,
false
})
})
t
.
Run
(
"A+"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_Ap
,
[]
bool
{
tru
e
,
true
,
fals
e
})
})
t
.
Run
(
"B-"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_Bn
,
[]
bool
{
false
,
false
,
true
})
})
t
.
Run
(
"B+"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_Bp
,
[]
bool
{
true
,
false
,
true
})
})
t
.
Run
(
"AB-"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_ABn
,
[]
bool
{
true
,
true
,
false
})
})
t
.
Run
(
"AB-"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_ABn
,
[]
bool
{
false
,
true
,
true
})
})
t
.
Run
(
"AB+"
,
func
(
t
*
testing
.
T
)
{
CheckBloodType
(
t
,
BloodType_ABp
,
[]
bool
{
true
,
true
,
true
})
})
}
func
TestProtocol
(
t
*
testing
.
T
)
{
// Runs the protocol for all combinations of recipient and donor blood types.
n
:=
len
(
BloodTable
)
for
x
:=
0
;
x
<
n
;
x
++
{
for
y
:=
0
;
y
<
n
;
y
++
{
if
z
:=
RunProtocol
(
x
,
y
);
z
!=
BloodTable
[
x
][
y
]
{
t
.
Fatalf
(
"Failed blood compatibility test for index [%d,%d]. Expected %t, got %t"
,
x
,
y
,
!
z
,
z
)
}
testName
:=
fmt
.
Sprintf
(
"(x=%s,y=%s)"
,
BloodNames
[
x
],
BloodNames
[
y
])
t
.
Run
(
testName
,
func
(
t
*
testing
.
T
)
{
z
,
err
:=
RunProtocol
(
x
,
y
)
if
err
!=
nil
{
t
.
Fatal
(
fmt
.
Sprintf
(
"Failed blood compatibility test for index [%d,%d]"
,
x
,
y
),
err
)
}
else
if
z
!=
BloodTable
[
x
][
y
]
{
t
.
Fatalf
(
"Failed blood compatibility test for index [%d,%d]. Expected %t, got %t"
,
x
,
y
,
!
z
,
z
)
}
})
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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