Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
algorand
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nilak
algorand
Commits
1ccfca8e
Commit
1ccfca8e
authored
Apr 12, 2019
by
THAKARE AKSHAY HARIBHAU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added VRF library
parent
aa110aca
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
270 additions
and
8 deletions
+270
-8
Utility.py
Utility.py
+96
-8
VRF_LIB/README.md
VRF_LIB/README.md
+10
-0
VRF_LIB/RSA_VRF.py
VRF_LIB/RSA_VRF.py
+163
-0
VRF_LIB/requirements.txt
VRF_LIB/requirements.txt
+1
-0
No files found.
Utility.py
View file @
1ccfca8e
from
ecdsa
import
SigningKey
from
ecdsa
import
SigningKey
,
SECP256k1
# import secrets
import
struct
import
random
import
scipy.stats
as
stats
def
pseudoRandomGenerator
(
seed
)
:
random
.
seed
(
seed
)
return
random
.
getrandbits
(
256
)
def
genratePublicPrivateKey
():
sk
=
SigningKey
.
generate
()
# uses NIST192p
sk
=
SigningKey
.
generate
(
curve
=
SECP256k1
)
# uses NIST192p
vk
=
sk
.
get_verifying_key
()
signature
=
sk
.
sign
(
b
"message"
)
assert
vk
.
verify
(
signature
,
b
"message"
)
...
...
@@ -16,12 +19,97 @@ def genratePublicPrivateKey():
pass
def
VRF
(
sk
,
seed
,
rolecount
,
w
,
badaW
,
pk
):
prgoutput
=
pseudoRandomGenerator
(
seed
)
temp
=
bytes
(
str
(
prgoutput
)
.
encode
(
'UTF8'
))
hash
=
sk
.
sign
(
temp
)
proof
=
pk
return
(
hash
,
proof
)
def
verifyVRF
(
pk
,
hash
,
proof
,
seed
):
prgoutput
=
pseudoRandomGenerator
(
seed
)
temp
=
bytes
(
str
(
prgoutput
)
.
encode
(
'UTF8'
))
print
(
proof
.
verify
(
hash
,
temp
))
def
sortition
(
sk
,
seed
,
rolecount
,
w
,
badaW
,
pk
):
w
=
25
badaW
=
100
p
=
w
/
badaW
j
=
0
x
=
0.5
# hash/(2**hashlen)
lastValue
=
0
;
print
(
"probability : "
,
p
)
flag
=
True
while
(
flag
):
lastValue
=
lastValue
+
stats
.
binom
.
pmf
(
j
,
w
,
p
)
nextvalue
=
lastValue
+
stats
.
binom
.
pmf
(
j
+
1
,
w
,
p
)
print
(
lastValue
,
nextvalue
)
if
(((
lastValue
<=
x
)
and
(
nextvalue
>
x
))):
break
if
j
==
w
+
1
:
j
=
0
break
j
=
j
+
1
return
j
def
computeRange
():
badaW
=
10
lastValue
=
0
;
p
=
0.5
x
=
0.0023
j
=
0
if
(
x
<=
stats
.
binom
.
pmf
(
0
,
badaW
,
p
)):
j
=
0
else
:
for
k
in
range
(
0
,
badaW
+
1
):
lastValue
=
lastValue
+
stats
.
binom
.
pmf
(
k
,
badaW
,
p
)
nextvalue
=
lastValue
+
stats
.
binom
.
pmf
(
k
+
1
,
badaW
,
p
)
print
(
lastValue
)
print
(
nextvalue
)
print
(
"------------"
)
j
=
j
+
1
def
bytes_to_int
(
bytes
):
result
=
0
for
b
in
bytes
:
result
=
result
*
256
+
int
(
b
)
return
result
def
testingBinom
():
w
=
1
p
=
0.2
m
=
0
for
k
in
range
(
0
,
2
):
ev
=
stats
.
binom
.
pmf
(
k
,
w
,
p
)
m
=
m
+
ev
print
(
ev
)
print
(
m
)
if
__name__
==
'__main__'
:
# keypair = genratePublicPrivateKey()
# keypair2 = genratePublicPrivateKey()
# print(keypair)
# print(keypair2)
print
(
pseudoRandomGenerator
(
"abc"
))
print
(
pseudoRandomGenerator
(
"abc"
))
keypair
=
genratePublicPrivateKey
()
#
# # # keypair2 = genratePublicPrivateKey()
# # # print(keypair)
# # # print(keypair2)
# # # print(pseudoRandomGenerator("abc"))
# # # print(pseudoRandomGenerator("abc"))
seed
=
(
"abc"
,
1
,
2
)
# # # print(pseudoRandomGenerator(seed))
# x= VRF(keypair[0],seed,3,2,30,keypair[1])
# # verifyVRF(x[1],x[0],x[1],seed)
# # print(stats.binom.pmf(1, 2, .5))
# print(x[0]/(2**(len(bytes_to_int(x[0])))))
# # computeRange()
#
# print(bytes_to_int(x[0]))
print
(
sortition
(
keypair
[
0
],
seed
,
3
,
2
,
30
,
keypair
[
1
]))
# testingBinom()
# computeRange()
\ No newline at end of file
VRF_LIB/README.md
0 → 100644
View file @
1ccfca8e
# Verifiable-Random-Functions
We implemented RSA VRF on Python2 fulfilling the properties of trusted uniqueness, trusted collision resistance, and full pseudorandomness.
USAGE: python RSA_VRF.py [alpha]
This code takes alpha and generates a proof and then proceeds to verify it. You can modify the size of the proof by modifying the variable k.
The slides are in the LaTeX PDF and make sure to install the libraries in the requirements.txt.
VRF_LIB/RSA_VRF.py
0 → 100644
View file @
1ccfca8e
import
hashlib
import
binascii
import
operator
import
math
import
sys
from
sys
import
argv
from
cryptography.hazmat.backends
import
default_backend
from
cryptography.hazmat.primitives.asymmetric
import
rsa
def
integer_byte_size
(
n
):
'''Returns the number of bytes necessary to store the integer n.'''
quanta
,
mod
=
divmod
(
integer_bit_size
(
n
),
8
)
if
mod
or
n
==
0
:
quanta
+=
1
return
quanta
def
integer_bit_size
(
n
):
'''Returns the number of bits necessary to store the integer n.'''
if
n
==
0
:
return
1
s
=
0
while
n
:
s
+=
1
n
>>=
1
return
s
def
integer_ceil
(
a
,
b
):
'''Return the ceil integer of a div b.'''
quanta
,
mod
=
divmod
(
a
,
b
)
if
mod
:
quanta
+=
1
return
quanta
class
RsaPublicKey
(
object
):
__slots__
=
(
'n'
,
'e'
,
'bit_size'
,
'byte_size'
)
def
__init__
(
self
,
n
,
e
):
self
.
n
=
n
self
.
e
=
e
self
.
bit_size
=
integer_bit_size
(
n
)
self
.
byte_size
=
integer_byte_size
(
n
)
def
__repr__
(
self
):
return
'<RsaPublicKey n:
%
d e:
%
d bit_size:
%
d>'
%
(
self
.
n
,
self
.
e
,
self
.
bit_size
)
def
rsavp1
(
self
,
s
):
if
not
(
0
<=
s
<=
self
.
n
-
1
):
raise
Exception
(
"s not within 0 and n - 1"
)
return
self
.
rsaep
(
s
)
def
rsaep
(
self
,
m
):
if
not
(
0
<=
m
<=
self
.
n
-
1
):
raise
Exception
(
"m not within 0 and n - 1"
)
return
pow
(
m
,
self
.
e
,
self
.
n
)
class
RsaPrivateKey
(
object
):
__slots__
=
(
'n'
,
'd'
,
'bit_size'
,
'byte_size'
)
def
__init__
(
self
,
n
,
d
):
self
.
n
=
n
self
.
d
=
d
self
.
bit_size
=
integer_bit_size
(
n
)
self
.
byte_size
=
integer_byte_size
(
n
)
def
__repr__
(
self
):
return
'<RsaPrivateKey n:
%
d d:
%
d bit_size:
%
d>'
%
(
self
.
n
,
self
.
d
,
self
.
bit_size
)
def
rsadp
(
self
,
c
):
if
not
(
0
<=
c
<=
self
.
n
-
1
):
raise
Exception
(
"c not within 0 and n - 1"
)
return
pow
(
c
,
self
.
d
,
self
.
n
)
def
rsasp1
(
self
,
m
):
if
not
(
0
<=
m
<=
self
.
n
-
1
):
raise
Exception
(
"m not within 0 and n - 1"
)
return
self
.
rsadp
(
m
)
def
i2osp
(
x
,
x_len
):
'''
Converts the integer x to its big-endian representation of length
x_len.
'''
# if x > 256**x_len:
# raise ValueError("integer too large")
h
=
hex
(
x
)[
2
:]
if
h
[
-
1
]
==
'L'
:
h
=
h
[:
-
1
]
if
len
(
h
)
&
1
==
1
:
h
=
'0
%
s'
%
h
x
=
binascii
.
unhexlify
(
h
)
return
b
'
\x00
'
*
int
(
x_len
-
len
(
x
))
+
x
def
os2ip
(
x
):
'''
Converts the byte string x representing an integer reprented using the
big-endian convient to an integer.
'''
h
=
binascii
.
hexlify
(
x
)
return
int
(
h
,
16
)
def
mgf1
(
mgf_seed
,
mask_len
,
hash_class
=
hashlib
.
sha1
):
'''
Mask Generation Function v1 from the PKCS#1 v2.0 standard.
mgs_seed - the seed, a byte string
mask_len - the length of the mask to generate
hash_class - the digest algorithm to use, default is SHA1
Return value: a pseudo-random mask, as a byte string
'''
h_len
=
hash_class
()
.
digest_size
if
mask_len
>
0x10000
:
raise
ValueError
(
'mask too long'
)
T
=
b
''
for
i
in
range
(
0
,
integer_ceil
(
mask_len
,
h_len
)):
C
=
i2osp
(
i
,
4
)
T
=
T
+
hash_class
(
mgf_seed
+
C
)
.
digest
()
return
T
[:
mask_len
]
def
VRF_prove
(
private_key
,
alpha
,
k
):
# k is the length of pi
EM
=
mgf1
(
alpha
,
k
-
1
)
m
=
os2ip
(
EM
)
s
=
private_key
.
rsasp1
(
m
)
pi
=
i2osp
(
s
,
k
)
return
pi
def
VRF_proof2hash
(
pi
,
hash
=
hashlib
.
sha1
):
beta
=
hash
(
pi
)
.
digest
()
return
beta
def
VRF_verifying
(
public_key
,
alpha
,
pi
,
k
):
s
=
os2ip
(
pi
)
m
=
public_key
.
rsavp1
(
s
)
EM
=
i2osp
(
m
,
k
-
1
)
EM_
=
mgf1
(
alpha
,
k
-
1
)
if
EM
==
EM_
:
return
"VALID"
else
:
return
"INVALID"
if
__name__
==
"__main__"
:
if
len
(
argv
)
<
2
:
print
(
"USAGE: python RSA_VRF.py [alpha]"
)
exit
(
1
)
private_key
=
rsa
.
generate_private_key
(
public_exponent
=
65537
,
key_size
=
2048
,
backend
=
default_backend
())
private_numbers
=
private_key
.
private_numbers
()
public_key
=
private_key
.
public_key
()
public_numbers
=
public_key
.
public_numbers
()
n
=
public_numbers
.
n
e
=
public_numbers
.
e
d
=
private_numbers
.
d
k
=
20
public_key
=
RsaPublicKey
(
n
,
e
)
private_key
=
RsaPrivateKey
(
n
,
d
)
alpha
=
" "
.
join
(
argv
[
1
:])
#seed
pi
=
VRF_prove
(
private_key
,
bytes
(
alpha
,
'utf-8'
),
k
)
print
(
pi
)
print
(
len
(
pi
))
beta
=
VRF_proof2hash
(
pi
)
print
(
VRF_verifying
(
public_key
,
bytes
(
alpha
,
'utf-8'
),
pi
,
k
))
VRF_LIB/requirements.txt
0 → 100644
View file @
1ccfca8e
cryptography 2.2.2
Write
Preview
Markdown
is supported
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