Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Gang Chen (SID 1642993)

48 views
############################################################################################################################################################# #This code is implemented by student Gang Chen #student id number is: 1642993 #This code is to be submitted to the department of Computer Science #This code is to be submitted to the module 06-28215 Anonymity, Privacy and Cybercrime #<--------------------------------------------------------------------> # This is question 2 #<--------------------------------------------------------------------> import hashlib p=19557005198709002903944860293031210393112763219086296641657077101075696217230957575932867920574816565856404011416908876733063974554042055901449481509845537165833627083609136426386625960849573203936690977282752217830467255076903813033768202255463477317279507253420233052258146393806024179130344501759742626253484354878829682461413002143954786668386100202257237258007726886368228024987052808444032800050116408995869561195208576647050961019393809940446490709061546503439488751331252440431940049716566765873615456614911458658341732120931319694200689362617296630728389003180670326151653001322354906363854478585334821235883; #Set up all the constant that we are going to use during the process q = Integer((p-1)/2); #g can be any suitable generator, here I used 3 g = 3; #Please set the m manully, since this program is not AlphaGo m = 0; r = ZZ.random_element(q); x = ZZ.random_element(q); pk = power_mod(g,x,p); R = power_mod(g,r,p); S = power_mod(pk,r,p) * power_mod(g,m,p); def DisjProve(g,pk,R,S,r,p,q,m): #if m equal to 1 then perform the first part of if-statement #else if m equal to 0 then perform else-statement #the value of m needs to manullay changed in order to see how it performs if m == 1: ch0 = ZZ.random_element(q); rep0 = ZZ.random_element(q); U0 = Mod(power_mod(g,rep0,p) * inverse_mod(power_mod(R,ch0,p),p),p); V0 = Mod(power_mod(pk,rep0,p) * inverse_mod(power_mod(S,ch0,p),p),p); u1 = ZZ.random_element(q); U1 = power_mod(g,u1,p); V1 = power_mod(pk,u1,p); hash_value = hashlib.sha256(str(R) + str(S) + str(U0) + str(V0) + str(U1) + str(V1)).hexdigest(); ch = Integer(int(hash_value,16),q); ch1 = Integer(Mod((ch-ch0),q)); rep1 = Integer(Mod(u1+(ch1*r),q)); else: u0 = ZZ.random_element(q); U0 = power_mod(g,u0,p); V0 = power_mod(pk,u0,p); ch1 = ZZ.random_element(q); rep1 = ZZ.random_element(q); U1 = Mod(power_mod(g,rep1,p) * inverse_mod(power_mod(R,ch1,p),p),p); V1 = Mod(power_mod(pk,rep1,p) * inverse_mod(power_mod(S * inverse_mod(g,p),ch1,p),p),p); hash_value = hashlib.sha256(str(R) + str(S) + str(U0) + str(V0) + str(U1) + str(V1)).hexdigest(); ch = Integer(int(hash_value,16),q); ch0 = Integer(Mod((ch-ch1),q)); rep0 = Integer(Mod((u0+(ch0*r)),q)); end return [ch0,rep0,ch1,rep1] [ch0,rep0,ch1,rep1] = DisjProve(g,pk,R,S,r,p,q,m); #Verifier def DisVerify(g,pk,R,S,p,q,ch0,rep0,ch1,rep1): a = Mod(power_mod(g,rep0,p) * power_mod(R,-ch0,p),p); b = Mod(power_mod(pk,rep0,p) * inverse_mod(power_mod(S,ch0,p),p),p); c = Mod(power_mod(g,rep1,p) * inverse_mod(power_mod(R,ch1,p),p),p); d = Mod(power_mod(pk,rep1,p) * inverse_mod(power_mod(S * inverse_mod(g,p),ch1,p),p),p); hash_value = hashlib.sha256(str(R) + str(S) + str(a) + str(b) + str(c) + str(d)).hexdigest(); ch = Integer(int(hash_value,16),q); ch_what = Integer(Mod((ch0+ch1),q)); return ch == ch_what; DisVerify(g,pk,R,S,p,q,ch0,rep0,ch1,rep1); ︠1283990a-4b42-49e7-a1de-84a9a0a6b8da︠ ######################################################################################################################################################### #This code is implemented by student Gang Chen #student id number is: 1642993 #This code is to be submitted to the department of Computer Science #This code is to be submitted to the module 06-28215 Anonymity, Privacy and Cybercrime #<--------------------------------------------------------------------> # This is question 3 #<--------------------------------------------------------------------> import hashlib import math p= 19557005198709002903944860293031210393112763219086296641657077101075696217230957575932867920574816565856404011416908876733063974554042055901449481509845537165833627083609136426386625960849573203936690977282752217830467255076903813033768202255463477317279507253420233052258146393806024179130344501759742626253484354878829682461413002143954786668386100202257237258007726886368228024987052808444032800050116408995869561195208576647050961019393809940446490709061546503439488751331252440431940049716566765873615456614911458658341732120931319694200689362617296630728389003180670326151653001322354906363854478585334821235883; #Set up all the constant that we are going to use during the process q = Integer((p-1)/2); g = 4; BB = []; def setup(g): sk = ZZ.random_element(q); pk = power_mod(g,sk,p); return (pk,sk) ##Test (pk,sk) = setup(g); #print("pk = " + str(pk) + "\n"); #print("sk = " + str(sk)); def vote(id_h,v): r = ZZ.random_element(q); R = power_mod(g,r,p); S = power_mod(pk,r,p) * power_mod(g,v,p); C = [R,S]; [ch0,rep0,ch1,rep1] = DisjProve(g,pk,R,S,r,p,q,v); pi = [ch0,rep0,ch1,rep1]; b = [R,S,ch0,rep0,ch1,rep1] return b ##Test #id = 1; #v = 1; #(R,S,ch0,rep0,ch1,rep1) = vote(id,v,pk); def DisjProve(g,pk,R,S,r,p,q,m): #if m equal to 1 then perform the first part of if-statement #else if m equal to 0 then perform else-statement #the value of m needs to manullay changed in order to see how it performs if m == 1: ch0 = ZZ.random_element(q); rep0 = ZZ.random_element(q); U0 = Mod(power_mod(g,rep0,p) * inverse_mod(power_mod(R,ch0,p),p),p); V0 = Mod(power_mod(pk,rep0,p) * inverse_mod(power_mod(S,ch0,p),p),p); u1 = ZZ.random_element(q); U1 = power_mod(g,u1,p); V1 = power_mod(pk,u1,p); hash_value = hashlib.sha256(str(R) + str(S) + str(U0) + str(V0) + str(U1) + str(V1)).hexdigest(); ch = Integer(int(hash_value,16),q); ch1 = Integer(Mod((ch-ch0),q)); rep1 = Integer(Mod(u1+(ch1*r),q)); else: u0 = ZZ.random_element(q); U0 = power_mod(g,u0,p); V0 = power_mod(pk,u0,p); ch1 = ZZ.random_element(q); rep1 = ZZ.random_element(q); U1 = Mod(power_mod(g,rep1,p) * inverse_mod(power_mod(R,ch1,p),p),p); V1 = Mod(power_mod(pk,rep1,p) * inverse_mod(power_mod(S * inverse_mod(g,p),ch1,p),p),p); hash_value = hashlib.sha256(str(R) + str(S) + str(U0) + str(V0) + str(U1) + str(V1)).hexdigest(); ch = Integer(int(hash_value,16),q); ch0 = Integer(Mod((ch-ch1),q)); rep0 = Integer(Mod((u0+(ch0*r)),q)); return [ch0,rep0,ch1,rep1] def Validate(b): return DisVerify(g,pk,b[0],b[1],p,q,b[2],b[3],b[4],b[5]) ##Test(don't use this :) #kk = Validate(R,S,ch0,rep0,ch1,rep1); #kk #Verifier def DisVerify(g,pk,R,S,p,q,ch0,rep0,ch1,rep1): a = Mod(power_mod(g,rep0,p) * power_mod(R,-ch0,p),p); b = Mod(power_mod(pk,rep0,p) * inverse_mod(power_mod(S,ch0,p),p),p); c = Mod(power_mod(g,rep1,p) * inverse_mod(power_mod(R,ch1,p),p),p); d = Mod(power_mod(pk,rep1,p) * inverse_mod(power_mod(S * inverse_mod(g,p),ch1,p),p),p); hash_value = hashlib.sha256(str(R) + str(S) + str(a) + str(b) + str(c) + str(d)).hexdigest(); ch = Integer(int(hash_value,16),q); ch_what = Integer(Mod((ch0+ch1),q)); return ch == ch_what; def Box(BB,b): if Validate(b) != True: return False else: BB = BB.append(b); return def Tally(BB,sk): count = 1; R_sum = 1; S_sum = 1; for j in range(0, len(BB)): if Validate(BB[j]) == False: return False for i in range(0, len(BB)): print "number of valid Vote %d" %count b_temp = BB[i]; R_temp = b_temp[0]; S_temp = b_temp[1]; R_sum *= R_temp; S_sum *= S_temp; count += 1; g_result = Mod(S_sum * inverse_mod(power_mod(R_sum,sk,p),p),p); pi_eq = eqdlprove(R_sum,pk,S_sum,sk); result = math.log(g_result,g); return [result,pi_eq]; def eqdlprove(R,pk,S,sk): r = ZZ.random_element(q); com1 = power_mod(g,r,p); y1 = power_mod(g,sk,p); hash_value = hashlib.sha256(str(y1) + str(com1)).hexdigest(); ch = Integer(int(hash_value,16),q); s = Integer(Mod(r + sk * ch,q)); return [ch,s]; #This is the test case I set up for this part of the assignment Voter_1 = vote(1,1); Voter_2 = vote(2,1); Voter_3 = vote(3,0); Voter_4 = vote(4,0); Voter_5 = vote(5,0); Voter_6 = vote(6,1); Voter_7 = vote(7,1); Voter_8 = vote(8,1); Voter_9 = vote(9,0); Voter_10 = vote(10,1); Box(BB,Voter_1); Box(BB,Voter_2); Box(BB,Voter_3); Box(BB,Voter_4); Box(BB,Voter_5); Box(BB,Voter_6); Box(BB,Voter_7); Box(BB,Voter_8); Box(BB,Voter_9); Box(BB,Voter_10); [voter, return_value]=Tally(BB,sk); print "The total number voted for agree is %d" %voter