implemented sortition

parent 1ccfca8e
from ecdsa import SigningKey,SECP256k1 from ecdsa import SigningKey,SECP256k1
from ecdsa.keys import BadSignatureError
# import secrets import binascii
import struct
import random import random
import scipy.stats as stats 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) : def pseudoRandomGenerator(seed) :
random.seed(seed) random.seed(seed)
return random.getrandbits(256) return random.getrandbits(256)
...@@ -20,6 +25,16 @@ def genratePublicPrivateKey(): ...@@ -20,6 +25,16 @@ def genratePublicPrivateKey():
pass pass
def VRF(sk,seed,rolecount,w,badaW,pk): 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) prgoutput = pseudoRandomGenerator(seed)
temp = bytes(str(prgoutput).encode('UTF8')) temp = bytes(str(prgoutput).encode('UTF8'))
hash = sk.sign(temp) hash = sk.sign(temp)
...@@ -29,23 +44,44 @@ def VRF(sk,seed,rolecount,w,badaW,pk): ...@@ -29,23 +44,44 @@ def VRF(sk,seed,rolecount,w,badaW,pk):
def verifyVRF(pk,hash,proof,seed): def verifyVRF(pk,hash,proof,seed):
prgoutput = pseudoRandomGenerator(seed) prgoutput = pseudoRandomGenerator(seed)
temp = bytes(str(prgoutput).encode('UTF8')) 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): 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 p = w/badaW
j=0 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; lastValue = 0;
print("probability : ",p) # print("probability : ",p)
flag = True flag = True
while(flag): while(flag):
lastValue = lastValue + stats.binom.pmf(j, w, p) lastValue = lastValue + stats.binom.pmf(j, w, p)
nextvalue = lastValue + stats.binom.pmf(j + 1, w, p) nextvalue = lastValue + stats.binom.pmf(j + 1, w, p)
print(lastValue,nextvalue) # print(lastValue,nextvalue)
if (((lastValue<=x) and (nextvalue>x))): if (((lastValue<=x) and (nextvalue>x))):
break break
if j == w+1: if j == w+1:
...@@ -53,8 +89,7 @@ def sortition(sk,seed,rolecount,w,badaW,pk): ...@@ -53,8 +89,7 @@ def sortition(sk,seed,rolecount,w,badaW,pk):
break break
j = j+1 j = j+1
return j return (hash,proof,j)
def computeRange(): def computeRange():
badaW=10 badaW=10
...@@ -75,11 +110,6 @@ def computeRange(): ...@@ -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(): def testingBinom():
w = 1 w = 1
...@@ -92,24 +122,38 @@ def testingBinom(): ...@@ -92,24 +122,38 @@ def testingBinom():
print(m) print(m)
if __name__ == '__main__': def testHashlen():
keypair = genratePublicPrivateKey() keypair = genratePublicPrivateKey()
# seed = ("a", 1, 2)
# # # keypair2 = genratePublicPrivateKey() x = VRF(keypair[0], seed, 3, 5, 30, keypair[1])
# # # print(keypair) print(len(binascii.hexlify(x[0])))
# # # print(keypair2) print(len(x[0]))
# # # print(pseudoRandomGenerator("abc")) temp = bytes_to_int(x[0][:32]) # converting first 32 bytes to integer i.e 32*8 bits ie 256 bits
# # # print(pseudoRandomGenerator("abc"))
seed = ("abc",1,2) print(temp / (2 ** 256)) #computes something simalar to hash/(2**haslen)
# # # print(pseudoRandomGenerator(seed))
# x= VRF(keypair[0],seed,3,2,30,keypair[1]) def testSortition():
# # verifyVRF(x[1],x[0],x[1],seed) sk,pk = genratePublicPrivateKey()
# # print(stats.binom.pmf(1, 2, .5)) seed = ("a",1,2)
# print(x[0]/(2**(len(bytes_to_int(x[0]))))) rolecount = 1
# # computeRange() w = 20
# badaW = 100
# print(bytes_to_int(x[0])) hash,proof,j = sortition(sk,seed,rolecount,w,badaW,pk)
print(sortition(keypair[0],seed,3,2,30,keypair[1])) # print(hash) #this is a real content of hash it should be used as final thing and not hexlify
print(binascii.hexlify(hash))
# testingBinom() print(proof)
# computeRange() print(j)
\ No newline at end of file
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__": ...@@ -159,5 +159,11 @@ if __name__ == "__main__":
pi = VRF_prove(private_key, bytes(alpha,'utf-8'), k) pi = VRF_prove(private_key, bytes(alpha,'utf-8'), k)
print(pi) print(pi)
print(len(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) beta = VRF_proof2hash(pi)
print(VRF_verifying(public_key, bytes(alpha,'utf-8'), pi, k)) 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