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
8786f516
Commit
8786f516
authored
Oct 10, 2021
by
Anders Jensen Løvig
Browse files
Fix length of p
parent
129dade7
Changes
2
Hide whitespace changes
Inline
Side-by-side
cmd/handin6/main.go
View file @
8786f516
...
...
@@ -2,8 +2,8 @@ package main
import
(
"crycomp/internal/blood"
"crycomp/internal/crypto/util"
"crycomp/internal/crypto/homomorphic"
"crycomp/internal/crypto/util"
"fmt"
"math/big"
)
...
...
@@ -21,42 +21,46 @@ func main() {
}
type
Party
struct
{
conn
chan
big
.
Int
x
int
sKey
*
big
.
Int
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
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
}
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
.
Enc
ode
(
xval
[
i
])
for
i
:=
0
;
i
<
3
;
i
++
{
alice
.
conn
<-
*
alice
.
HEProt
.
Enc
rypt
(
xval
[
i
])
}
//Receive and decode result from bob
result
=
alice
.
HEProt
.
Dec
ode
(
<-
alice
.
conn
,
*
alice
.
sKey
)
==
1
//Convert 1 -> true
result
=
alice
.
HEProt
.
Dec
rypt
(
<-
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
)
{
func
(
bob
*
Party
)
RunBob
(
y
int
)
(
err
error
)
{
c_y
:=
make
([]
*
big
.
Int
,
3
)
for
i
,
val
:=
range
util
.
Int2Bools
(
y
,
3
)
{
bit
:=
0
if
val
{
bit
=
1
}
c_y
[
i
]
=
bob
.
HEProt
.
Encode
(
bit
)
if
val
{
bit
=
1
}
c_y
[
i
]
=
bob
.
HEProt
.
Encrypt
(
bit
)
}
//Receive ciphertexts from alice
...
...
@@ -65,20 +69,18 @@ func (bob *Party) RunBob(x int) (err error) {
val
:=
<-
bob
.
conn
c_x
[
i
]
=
&
val
}
bob
.
conn
<-
*
bob
.
HEProt
.
Eval
(
c_x
,
c_y
)
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
)
params
:=
homomorphic
.
NewParams
(
200
,
100000
,
1000000
,
200
)
params
:=
homomorphic
.
NewParams
(
200
,
100000
,
1000000
,
200
)
sKey
,
HEp
,
err
:=
homomorphic
.
NewProtocol
(
params
)
if
err
!=
nil
{
return
...
...
@@ -118,4 +120,4 @@ func RunProtocol(x, y int) (z bool, err error) {
z
,
err
=
p
.
A
.
RunAlice
(
x
)
return
}
\ No newline at end of file
}
internal/crypto/homomorphic/dhe.go
View file @
8786f516
package
homomorphic
import
(
"crycomp/internal/crypto/util"
"crypto/rand"
"math/big"
mRand
"math/rand"
)
type
Params
struct
{
len_sKey
int
len_q
int
len_r
int
m
int
pLen
int
qLen
int
rLen
int
m
int
}
func
NewParams
(
len_sKey
,
len_q
,
len_r
,
m
int
)
*
Params
{
func
NewParams
(
pLen
,
qLen
,
rLen
,
m
int
)
*
Params
{
return
&
Params
{
len_sKey
:
len_sKey
,
len_q
:
len_q
,
len_r
:
len_r
,
m
:
m
,
pLen
:
pLen
,
qLen
:
qLen
,
rLen
:
rLen
,
m
:
m
,
}
}
...
...
@@ -27,32 +27,62 @@ type Protocol struct {
params
*
Params
}
func
NewProtocol
(
params
*
Params
)
(
sKey
*
big
.
Int
,
pr
Protocol
,
err
error
)
{
//set private key to a random odd 2000-bit integer
res
,
err
:=
util
.
RandInt
(
rand
.
Reader
,
big
.
NewInt
(
int64
(
params
.
len_sKey
)))
if
err
!=
nil
{
return
}
res
.
SetBit
(
res
,
0
,
1
)
sKey
=
res
// Returns a random integer with the specified bit length.
func
randInt
(
bitLen
int
)
(
n
*
big
.
Int
,
err
error
)
{
// Calculate how many bytes are needed to have bitLen bits.
bytesLen
:=
(
bitLen
+
7
)
/
8
// Fill bytes with random data
pBytes
:=
make
([]
byte
,
bytesLen
)
_
,
err
=
rand
.
Read
(
pBytes
)
if
err
!=
nil
{
return
}
// Clear the bits that exceed bitLen
pBytes
[
0
]
&=
0xff
>>
(
8
*
len
(
pBytes
)
-
bitLen
)
n
=
new
(
big
.
Int
)
.
SetBytes
(
pBytes
)
// Set the bitLen-1 bit. This ensures bitLen bits
// and the distribution is maintained (all other bits are random)
n
.
SetBit
(
n
,
bitLen
-
1
,
1
)
return
}
func
NewProtocol
(
params
*
Params
)
(
p
*
big
.
Int
,
pr
Protocol
,
err
error
)
{
// Choose a random big secret odd integer p
p
,
err
=
randInt
(
params
.
pLen
)
if
err
!=
nil
{
return
}
p
.
SetBit
(
p
,
0
,
1
)
// Make p odd.
// res, err := util.RandInt(rand.Reader, big.NewInt(int64(params.pLen)))
// if err != nil {
// return
// }
// res.SetBit(res, 0, 1)
q
:=
make
([]
*
big
.
Int
,
params
.
m
)
r
:=
make
([]
*
big
.
Int
,
params
.
m
)
y
:=
make
([]
*
big
.
Int
,
params
.
m
)
for
i
:=
range
q
{
q
[
i
],
err
=
rand
.
Int
(
rand
.
Reader
,
big
.
NewInt
(
int64
(
params
.
len_q
)))
if
err
!=
nil
{
return
}
r
[
i
],
err
=
rand
.
Int
(
rand
.
Reader
,
big
.
NewInt
(
int64
(
params
.
len_r
)))
if
err
!=
nil
{
return
}
q
[
i
],
err
=
rand
.
Int
(
rand
.
Reader
,
big
.
NewInt
(
int64
(
params
.
qLen
)))
if
err
!=
nil
{
return
}
r
[
i
],
err
=
rand
.
Int
(
rand
.
Reader
,
big
.
NewInt
(
int64
(
params
.
rLen
)))
if
err
!=
nil
{
return
}
right
:=
big
.
NewInt
(
2
)
right
.
Mul
(
right
,
r
[
i
])
left
:=
right
.
Mul
(
sKey
,
q
[
i
])
right
.
Mul
(
right
,
r
[
i
])
left
:=
right
.
Mul
(
p
,
q
[
i
])
y
[
i
]
=
right
.
Add
(
left
,
right
)
}
pr
=
Protocol
{
pubKey
:
y
,
params
:
params
,
pubKey
:
y
,
params
:
params
,
}
return
sKey
,
pr
,
err
return
}
func
(
p
*
Protocol
)
Encode
(
m
int
)
(
c
*
big
.
Int
)
{
...
...
@@ -60,7 +90,7 @@ func (p *Protocol) Encode(m int) (c *big.Int) {
for
i
:=
0
;
i
<
p
.
params
.
m
;
i
++
{
S
[
i
]
=
i
}
mRand
.
Shuffle
(
p
.
params
.
m
,
func
(
i
,
j
int
)
{
S
[
i
],
S
[
j
]
=
S
[
j
],
S
[
i
]})
mRand
.
Shuffle
(
p
.
params
.
m
,
func
(
i
,
j
int
)
{
S
[
i
],
S
[
j
]
=
S
[
j
],
S
[
i
]
})
S
=
S
[
:
p
.
params
.
m
]
c
=
big
.
NewInt
(
int64
(
m
))
for
i
:=
range
S
{
...
...
@@ -70,25 +100,25 @@ func (p *Protocol) Encode(m int) (c *big.Int) {
}
func
(
p
*
Protocol
)
Decode
(
c
,
sKey
big
.
Int
)
int
{
tmp
:=
c
.
Mod
(
&
c
,
&
sKey
)
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
)
*
big
.
Int
{
// Assumes x inputs are negated, so Eval can compute ¬(¬a+b) = a+¬b
func
(
p
*
Protocol
)
Eval
(
x
,
y
[]
*
big
.
Int
)
*
big
.
Int
{
one
:=
p
.
Encode
(
1
)
tmp
:=
make
([]
*
big
.
Int
,
3
)
tmp
:=
make
([]
*
big
.
Int
,
3
)
for
i
:=
0
;
i
<
3
;
i
++
{
xi
:=
x
[
i
]
yi
:=
y
[
i
]
var
val
big
.
Int
val
.
Mul
(
xi
,
yi
)
val
.
Add
(
&
val
,
one
)
val
.
Mul
(
xi
,
yi
)
val
.
Add
(
&
val
,
one
)
tmp
[
i
]
=
&
val
}
var
res
big
.
Int
res
.
Mul
(
tmp
[
0
],
tmp
[
1
])
res
.
Mul
(
&
res
,
tmp
[
2
])
res
.
Mul
(
tmp
[
0
],
tmp
[
1
])
res
.
Mul
(
&
res
,
tmp
[
2
])
return
&
res
}
\ 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