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
aabf0978
Commit
aabf0978
authored
Oct 07, 2021
by
Thomas Hoffmann
Browse files
Untested attempt @ handin 6
parent
fbc0ec4b
Changes
2
Hide whitespace changes
Inline
Side-by-side
cmd/handin6/main.go
0 → 100644
View file @
aabf0978
package
main
import
(
"crycomp/internal/blood"
"crycomp/internal/crypto/util"
"crycomp/internal/crypto/homomorphic"
"fmt"
"math/big"
)
func
main
()
{
bloodA
,
bloodB
:=
blood
.
Type_ABn
,
blood
.
Type_ABn
z
,
err
:=
RunProtocol
(
bloodA
,
bloodB
)
if
err
!=
nil
{
fmt
.
Println
(
"Protocol failed with error:"
,
err
)
}
else
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
])
}
}
type
Party
struct
{
conn
chan
*
big
.
Int
x
int
sKey
*
big
.
Int
HEProt
homomorphic
.
Protocol
}
func
NewParty
(
conn
chan
*
big
.
Int
,
p
homomorphic
.
Protocol
)
(
*
Party
,
error
)
{
return
&
Party
{
conn
:
conn
,
HEProt
:
p
,
},
nil
}
func
(
alice
*
Party
)
RunAlice
(
x
int
)
(
result
bool
,
err
error
)
{
xval
:=
make
([]
int
,
3
)
//Negate values
for
i
,
val
:=
range
util
.
Int2Bools
(
x
,
3
)
{
if
!
val
{
xval
[
i
]
=
1
}
}
// Send encoded input ints to bob
for
i
:=
0
;
i
<
3
;
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
return
}
func
(
bob
*
Party
)
RunBob
(
x
int
)
(
err
error
)
{
c_y
:=
make
([]
*
big
.
Int
,
3
)
for
i
,
val
:=
range
util
.
Int2Bools
(
x
,
3
)
{
bit
:=
0
if
val
{
bit
=
1
}
c_y
[
i
]
=
bob
.
HEProt
.
Encode
(
bit
)
}
//Receive ciphertexts from alice
c_x
:=
make
([]
*
big
.
Int
,
3
)
for
i
:=
range
c_x
{
c_x
[
i
]
=
<-
bob
.
conn
}
bob
.
conn
<-
bob
.
HEProt
.
Eval
(
c_x
,
c_y
)
return
nil
}
type
Protocol
struct
{
A
,
B
*
Party
}
func
NewProtocol
()
(
p
*
Protocol
,
err
error
)
{
conn
:=
make
(
chan
*
big
.
Int
)
sKey
,
HEp
,
err
:=
homomorphic
.
NewProtocol
(
3
,
27
)
if
err
!=
nil
{
return
}
A
,
err
:=
NewParty
(
conn
,
HEp
)
if
err
!=
nil
{
return
nil
,
err
}
A
.
sKey
=
sKey
B
,
err
:=
NewParty
(
conn
,
HEp
)
if
err
!=
nil
{
return
nil
,
err
}
p
=
&
Protocol
{
A
:
A
,
B
:
B
,
}
return
p
,
nil
}
// RunProtocol runs the protocol between receiving blood type x and donor blood
// type y.
func
RunProtocol
(
x
,
y
int
)
(
z
bool
,
err
error
)
{
p
,
err
:=
NewProtocol
()
if
err
!=
nil
{
return
}
// Concurrently run Bob
go
func
()
{
err
:=
p
.
B
.
RunBob
(
y
)
if
err
!=
nil
{
panic
(
err
)
// TODO do not panic
}
}()
z
,
err
=
p
.
A
.
RunAlice
(
x
)
return
}
\ No newline at end of file
internal/crypto/homomorphic/dhe.go
0 → 100644
View file @
aabf0978
package
homomorphic
import
(
"math/big"
"crypto/rand"
mRand
"math/rand"
)
type
Protocol
struct
{
d
,
n
int
pubKey
[]
*
big
.
Int
}
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
)
if
err
!=
nil
{
return
}
sKey
=
sKey
.
SetBit
(
sKey
,
0
,
1
)
q
:=
make
([]
*
big
.
Int
,
n
)
r
:=
make
([]
*
big
.
Int
,
n
)
y
:=
make
([]
*
big
.
Int
,
n
)
for
i
:=
range
q
{
q
[
i
],
err
=
rand
.
Int
(
rand
.
Reader
,
b_int
)
if
err
!=
nil
{
return
}
r
[
i
],
err
=
rand
.
Int
(
rand
.
Reader
,
s_int
)
if
err
!=
nil
{
return
}
right
:=
big
.
NewInt
(
2
)
right
.
Mul
(
right
,
r
[
i
])
left
:=
right
.
Mul
(
sKey
,
q
[
i
])
y
[
i
]
=
right
.
Add
(
left
,
right
)
}
pr
=
Protocol
{
n
:
n
,
d
:
d
,
pubKey
:
y
,
}
return
sKey
,
pr
,
err
}
func
(
p
*
Protocol
)
Encode
(
m
int
)
(
c
*
big
.
Int
)
{
SLen
:=
4
//TODO: Define actual value
S
:=
make
([]
int
,
p
.
n
)
for
i
:=
0
;
i
<
p
.
n
;
i
++
{
S
[
i
]
=
i
}
mRand
.
Shuffle
(
p
.
n
,
func
(
i
,
j
int
)
{
S
[
i
],
S
[
j
]
=
S
[
j
],
S
[
i
]})
S
=
S
[
:
SLen
]
c
=
big
.
NewInt
(
int64
(
m
))
for
i
:=
range
S
{
c
=
c
.
Add
(
c
,
p
.
pubKey
[
S
[
i
]])
}
return
}
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
)
{
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
)
}
res
.
Mul
(
tmp
[
0
],
tmp
[
1
])
res
.
Mul
(
res
,
tmp
[
2
])
return
}
\ No newline at end of file
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