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
d5f53728
Commit
d5f53728
authored
Apr 12, 2019
by
THAKARE AKSHAY HARIBHAU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implemented VerifySort and new testcases and codecleanup
parent
4a83e074
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
15 deletions
+94
-15
Utility.py
Utility.py
+94
-15
No files found.
Utility.py
View file @
d5f53728
...
...
@@ -24,14 +24,10 @@ def genratePublicPrivateKey():
pass
def
VRF
(
sk
,
seed
,
rolecount
,
w
,
badaW
,
pk
):
def
VRF
(
sk
,
seed
,
pk
):
'''
:param sk: secrete key
:param seed:
:param rolecount:
:param w: users stake or weight
:param badaW: total stake in game i.e. sum of wi
:param pk: public key of user
:return: (bytes array of signature may not b of fixed length | public key as a proof)
'''
...
...
@@ -51,20 +47,21 @@ def verifyVRF(pk,hash,proof,seed):
return
True
def
sortition
(
sk
,
seed
,
rolecount
,
w
,
badaW
,
pk
):
def
sortition
(
sk
,
seed
,
rolecount
,
role
,
w
,
badaW
,
pk
):
'''
:param sk: secrete key
:param seed:
:param rolecount:
:param rolecount: tou
:param role:
:param w: users stake or weight
:param badaW: total stake in game i.e. sum of wi
:param pk: public key of user
:return: (bytes array of signature may not b of fixed length | public key as a proof)
'''
newseed
=
(
seed
,
role
count
)
hash
,
proof
=
VRF
(
sk
,
newseed
,
3
,
w
,
badaW
,
pk
)
p
=
w
/
badaW
newseed
=
(
seed
,
role
)
hash
,
proof
=
VRF
(
sk
,
newseed
,
pk
)
p
=
rolecount
/
badaW
j
=
0
#simplifying the computation : hash/(2**hashlen)
...
...
@@ -91,6 +88,51 @@ def sortition(sk,seed,rolecount,w,badaW,pk):
return
(
hash
,
proof
,
j
)
def
verifySort
(
pk
,
hash
,
proof
,
seed
,
rolecount
,
role
,
w
,
badaW
):
'''
:param pk: public key of user
:param hash:
:param proof:
:param seed:
:param rolecount: tou
:param role:
:param w: users stake or weight
:param badaW: total stake in game i.e. sum of wi
:return: number of selected users in commity for the user with pk
'''
newseed
=
(
seed
,
role
)
if
not
verifyVRF
(
pk
,
hash
,
proof
,
newseed
):
return
0
;
p
=
rolecount
/
badaW
j
=
0
#simplifying the computation : hash/(2**hashlen)
tempHash
=
bytes_to_int
(
hash
[:
32
])
# converting first 32 bytes to integer i.e 32*8 bits ie 256 bits
x
=
tempHash
/
(
2
**
256
)
# print(x)
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
;
...
...
@@ -125,7 +167,7 @@ def testingBinom():
def
testHashlen
():
keypair
=
genratePublicPrivateKey
()
seed
=
(
"a"
,
1
,
2
)
x
=
VRF
(
keypair
[
0
],
seed
,
3
,
5
,
30
,
keypair
[
1
])
x
=
VRF
(
keypair
[
0
],
seed
,
keypair
[
1
])
print
(
len
(
binascii
.
hexlify
(
x
[
0
])))
print
(
len
(
x
[
0
]))
temp
=
bytes_to_int
(
x
[
0
][:
32
])
# converting first 32 bytes to integer i.e 32*8 bits ie 256 bits
...
...
@@ -135,25 +177,62 @@ def testHashlen():
def
testSortition
():
sk
,
pk
=
genratePublicPrivateKey
()
seed
=
(
"a"
,
1
,
2
)
rolecount
=
1
roleCount
=
26
role
=
"LEAD"
w
=
20
badaW
=
100
hash
,
proof
,
j
=
sortition
(
sk
,
seed
,
role
count
,
w
,
badaW
,
pk
)
hash
,
proof
,
j
=
sortition
(
sk
,
seed
,
role
Count
,
role
,
w
,
badaW
,
pk
)
# print(hash) #this is a real content of hash it should be used as final thing and not hexlify
print
(
binascii
.
hexlify
(
hash
))
print
(
proof
)
print
(
j
)
def
testVerifySort
():
sk
,
pk
=
genratePublicPrivateKey
()
seed
=
(
"a"
,
1
,
2
)
roleCount
=
26
role
=
"LEAD"
w
=
20
badaW
=
100
hash
,
proof
,
j
=
sortition
(
sk
,
seed
,
roleCount
,
role
,
w
,
badaW
,
pk
)
# print(hash) #this is a real content of hash it should be used as final thing and not hexlify
print
(
"-----sortition output -----------"
)
print
(
binascii
.
hexlify
(
hash
))
print
(
proof
)
print
(
j
)
print
(
"----------------------------------"
)
seed2
=
(
"a"
,
1
,
2
)
roleCount2
=
26
role2
=
"LEAD"
w2
=
20
badaW
=
100
y
=
verifySort
(
pk
,
hash
,
proof
,
seed2
,
roleCount2
,
role2
,
w2
,
badaW
)
print
(
y
)
assert
(
j
==
y
),
"Test VerifySort failed : "
seed3
=
(
"a"
,
1
,
2
)
roleCount3
=
26
role3
=
"Commitee"
w3
=
20
badaW
=
100
y
=
verifySort
(
pk
,
hash
,
proof
,
seed3
,
roleCount3
,
role3
,
w3
,
badaW
)
print
(
y
)
assert
(
y
==
0
),
"Test Verify sort failed : change of seed not detected"
def
testVerifyVRF
():
sk
,
pk
=
genratePublicPrivateKey
()
seed
=
(
"a"
,
1
,
2
)
hash
,
proof
=
VRF
(
sk
,
seed
,
3
,
2
,
30
,
pk
)
hash
,
proof
=
VRF
(
sk
,
seed
,
pk
)
seed2
=
(
"a"
,
1
,
2
)
status
=
verifyVRF
(
pk
,
hash
,
proof
,
seed2
)
print
(
status
)
if
__name__
==
'__main__'
:
testSortition
()
#
testSortition()
# testVerifyVRF()
testVerifySort
()
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