implemented sortition

parent 1ccfca8e
from ecdsa import SigningKey,SECP256k1
from ecdsa.keys import BadSignatureError
# import secrets
import struct
import binascii
import random
import scipy.stats as stats
def bytes_to_int(bytes):
result = 0
for b in bytes:
result = result * 256 + int(b)
return result
def pseudoRandomGenerator(seed) :
random.seed(seed)
return random.getrandbits(256)
......@@ -20,6 +25,16 @@ def genratePublicPrivateKey():
pass
def VRF(sk,seed,rolecount,w,badaW,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)
'''
prgoutput = pseudoRandomGenerator(seed)
temp = bytes(str(prgoutput).encode('UTF8'))
hash = sk.sign(temp)
......@@ -29,23 +44,44 @@ def VRF(sk,seed,rolecount,w,badaW,pk):
def verifyVRF(pk,hash,proof,seed):
prgoutput = pseudoRandomGenerator(seed)
temp = bytes(str(prgoutput).encode('UTF8'))
print(proof.verify(hash,temp))
try:
proof.verify(hash, temp)
except BadSignatureError as badSign:
return False
return True
def sortition(sk,seed,rolecount,w,badaW,pk):
w = 25
badaW = 100
'''
: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)
'''
newseed = (seed,rolecount)
hash,proof = VRF(sk, newseed, 3, w, badaW, pk)
p = w/badaW
j=0
x = 0.5 # hash/(2**hashlen)
#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)
# 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)
# print(lastValue,nextvalue)
if (((lastValue<=x) and (nextvalue>x))):
break
if j == w+1:
......@@ -53,8 +89,7 @@ def sortition(sk,seed,rolecount,w,badaW,pk):
break
j = j+1
return j
return (hash,proof,j)
def computeRange():
badaW=10
......@@ -75,11 +110,6 @@ def computeRange():
def bytes_to_int(bytes):
result = 0
for b in bytes:
result = result * 256 + int(b)
return result
def testingBinom():
w = 1
......@@ -92,24 +122,38 @@ def testingBinom():
print(m)
if __name__ == '__main__':
def testHashlen():
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
seed = ("a", 1, 2)
x = VRF(keypair[0], seed, 3, 5, 30, 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
print(temp / (2 ** 256)) #computes something simalar to hash/(2**haslen)
def testSortition():
sk,pk = genratePublicPrivateKey()
seed = ("a",1,2)
rolecount = 1
w = 20
badaW = 100
hash,proof,j = sortition(sk,seed,rolecount,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 testVerifyVRF():
sk,pk = genratePublicPrivateKey()
seed = ("a",1,2)
hash,proof= VRF(sk,seed,3,2,30,pk)
seed2 = ("a", 1, 2)
status = verifyVRF(pk,hash,proof,seed2)
print(status)
if __name__ == '__main__':
testSortition()
# testVerifyVRF()
......@@ -159,5 +159,11 @@ if __name__ == "__main__":
pi = VRF_prove(private_key, bytes(alpha,'utf-8'), k)
print(pi)
print(len(pi))
print(os2ip(pi[0:64]))
m = os2ip(pi[0:33])
print(integer_bit_size(m))
# m = os2ip(pi)/(2**20)
# print(m)
beta = VRF_proof2hash(pi)
print(VRF_verifying(public_key, bytes(alpha,'utf-8'), pi, k))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment