implemented VerifySort and new testcases and codecleanup

parent 4a83e074
......@@ -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,rolecount)
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,rolecount,w,badaW,pk)
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(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()
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