Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: zhong
Views: 56
""" This SageMath worksheet contains code for the Equality of Discrete Logarithms Zero-Knowledge Proof System. The parameters p,q,g1 and hash function should be used for the Assignment """
import hashlib p = 19557005198709002903944860293031210393112763219086296641657077101075696217230957575932867920574816565856404011416908876733063974554042055901449481509845537165833627083609136426386625960849573203936690977282752217830467255076903813033768202255463477317279507253420233052258146393806024179130344501759742626253484354878829682461413002143954786668386100202257237258007726886368228024987052808444032800050116408995869561195208576647050961019393809940446490709061546503439488751331252440431940049716566765873615456614911458658341732120931319694200689362617296630728389003180670326151653001322354906363854478585334821235883 q = Integer((p-1)/2) #m=1; # g=2; # m=0; # x=ZZ.random_element(q); # PK=power_mod(g,x,p); # r=ZZ.random_element(q); # R=power_mod(g,r,p); # S=Integer(mod(power_mod(PK,r,p)*g**(m),p),p); #g1 = 4; g2 = 3; #x = ZZ.random_element(q); # y1 = power_mod(g1,x,p); # y2 = power_mod(g2,x,p);
def eqdlprove(p,q,g1,g2,y1,y2,x): r = ZZ.random_element(q); com1 = power_mod(g1,r,p); com2 = power_mod(g2,r,p); hash_value = hashlib.sha256(str(y1) + str(y2) + str(com1) + str(com2)).hexdigest(); ch = Integer(int(hash_value,16),q); s = Integer(Mod(r + x * ch,q)); return [ch,s,hash_value]
def myeqdlprove(g,c1,h,c2,x): g1=g; g2=h; r = ZZ.random_element(q); com1 = power_mod(g1,r,p); com2 = power_mod(g2,r,p); hash_value = hashlib.sha256(str(c1) + str(c2) + str(com1) + str(com2)).hexdigest(); ch = Integer(int(hash_value,16),q); s = Integer(Mod(r + x * ch,q)); return [ch,s]
def disjprove(p,q,S,PK,g,R,r): #r=ZZ.random_element(q); #PK=power_mod(g,x,p); #R=power_mod(g,r,p); #S=Integer(mod(power_mod(PK,r,p)*g**(m),p),p); #S=Integer(mod(power_mod(PK,r,p)*g**(m),p)); #S=Integer(mod(power_mod(PK,r,p)*power_mod(g,m,p),p)); ch0 = ZZ.random_element(q); rep0=ZZ.random_element(q); U0=mod(power_mod(g,rep0,p)/power_mod(R,ch0,p),p); V0=mod(power_mod(PK,rep0,p)/power_mod(S,ch0,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)); return [ch0,ch1,rep0,rep1]
def disjprove2(p,q,S,PK,g,R,r): #r=ZZ.random_element(q); #PK=power_mod(g,x,p); #R=power_mod(g,r,p); #S=Integer(mod(power_mod(PK,r,p)*g**(m),p),p); #S=Integer(mod(power_mod(PK,r,p)*g**(m),p)); #S=Integer(mod(power_mod(PK,r,p)*power_mod(g,m,p),p)); ch1 = ZZ.random_element(q); rep1=ZZ.random_element(q); U1=mod(power_mod(g,rep1,p)/power_mod(R,ch1,p),p); V1=mod(power_mod(PK,rep1,p)/power_mod(Integer(S*inverse_mod(g,p)),ch1,p),p); u0=ZZ.random_element(q); U0=power_mod(g,u0,p); V0=power_mod(PK,u0,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,ch1,rep0,rep1]
2046
[ch0,ch1,rep0,rep1]=disjprove2(p,q,S,PK,g,R,r)
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 995, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> NameError: name 'S' is not defined
471777347440367626918212757109010736981968888843601640976481725797355712944716646751332705419472153716560114622030660913358270423626682693405517995843828173577023353153090736852134794554442052363913373322025696200895393408913502915169992289177960579971337149457081988333689989766164257787288182173190925951772680319490407914023299815872180404209168873789699440709239900003811395375387631307708242872052425743192998393950906694411435123423414484523046812561634542036937539670416119870947657600348200679182706799584841683361353888707573193203405079459120989409464646428032591616175871690634566661815135457651033623657 17333048191029285402656599149750941933567927997187611870139847625029826454555738839558396807532916927518988847759387330802584937018925689524146297088750054201161086390827911044019140074087516900493279893510552138594768435862916249986068757909765715691150545937089124440010653831932711108053017173622664836017817983855768560715169628853764713430291498898152726204281359304608823994043001349519258356133670075974717992000192282612691180140654682679866229519705611954462762220973353430156474960821565482452367993225920841591743203737053399667954463926297776377760833440012903343232328841851897710685575742181299749556001 12599553145256429926520271162743653005564246354704734031159918290965218671553249727597951517206318753994276435815250895643234974375394519350697360112628788060297172521207352949866327315582151122029766118278437788597037985511115310708601736874564062131998445482913168626236389644382229978364165640524146318890166903013743217332577558006560566615043325023878139582313114378332273538417700437616728792487042302805478894632543568250737724809644648037741702496827141685184274726087152463278364653571504398375947509118077301360761198537632278233057366999981102772297225538461050302451712554133322997620116693204612210294996
def disjverify(g,PK,R,S,ch0,ch1,rep0,rep1): u0=mod(power_mod(g,rep0,p)/power_mod(R,ch0,p),p); v0=mod(power_mod(PK,rep0,p)/power_mod(S,ch0,p),p); u1=mod(power_mod(g,rep1,p)/power_mod(R,ch1,p),p); v1=mod(power_mod(PK,rep1,p)/power_mod(Integer(S*inverse_mod(g,p)),ch1,p),p); hash_value2 = hashlib.sha256(str(R) + str(S) + str(u0) + str(v0)+str(u1)+str(v1)).hexdigest(); ch2 = Integer(int(hash_value2,16),q); ch=mod(ch0+ch1,q); return ch==ch2
disjverify(g,PK,R,S,ch0,ch1,rep0,rep1)
True
clear
<function clear at 0x7fd5203a75f0>
[ch,s,hash_value] = eqdlprove(p,q,g1,g2,y1,y2,x); print("challenge = " + str(ch) + "\n") print("solution = " + str(s)+ "\n") print("hash_value" + str(hash_value))
challenge = 83155204311621940942984014210400273456747855202255373325138368703134416484655 solution = 3698248624868163379947345302858925915624106351186017645684648561651578630261522680981743122247020496664305166473612776983855464463579372685077221832041458310232632285251887866461887718032115411981831168131983095402897000297455431084965792003138761001958412974009297604156353911325468058532448438760935898988421818611199650604925086193748930096640023804983864823202283746461603762580964842672824581358963875525685604745209299920526100336290367640411262653994986280219928256914950768327633074738109361379006307552502029498754114097084532574841167506007431906574142991262823987762894968308178227661867078302199036034580 hash_valueb7d82d80f180e22c6d2638116371b455bd2af71ef13df2a327f8766b247fb92f
def eqdlverify(p,q,g1,g2,y1,y2,ch,s): com1 = Mod(power_mod(g1,s,p) * inverse_mod(power_mod(y1,ch,p),p),p); com2 = Mod(power_mod(g2,s,p) * inverse_mod(power_mod(y2,ch,p),p),p); hash_value = hashlib.sha256(str(y1) + str(y2) + str(com1) + str(com2)).hexdigest(); return ch == Integer(int(hash_value,16),q)
eqdlverify(p,q,g1,g2,y1,y2,ch,s)
False
def setup(g): x=ZZ.random_element(q); PK=power_mod(g,x,p); key=[x,PK,g] return key
key=setup(3) key
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "", line 2, in setup NameError: global name 'q' is not defined
def vote(v): g=key[2]; PK=key[1]; r=ZZ.random_element(q); R=power_mod(g,r,p); S=Integer(mod(power_mod(PK,r,p)*g**(v),p)); C=[R,S] if v==1: [ch0,ch1,rep0,rep1]=disjprove(p,q,S,PK,g,R,r); else: [ch0,ch1,rep0,rep1]=disjprove2(p,q,S,PK,g,R,r); N=[ch0,ch1,rep0,rep1]; b=[C,N] return b
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> IndexError: list index out of range
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> IndexError: list index out of range
# def vote(v,g,x,PK): # r=ZZ.random_element(q); # R=power_mod(g,r,p); # S=Integer(mod(power_mod(PK,r,p)*g**(v),p)); # C=[R,S] # if v==1: # [ch0,ch1,rep0,rep1]=disjprove(p,q,S,PK,g,R,r); # else: # [ch0,ch1,rep0,rep1]=disjprove2(p,q,S,PK,g,R,r); # N=[ch0,ch1,rep0,rep1]; # return [R,S,N,x,g,PK]
def validate(b): g=key[2]; PK=key[1]; C=b[0]; N=b[1]; R=C[0]; S=C[1]; ch0=N[0]; ch1=N[1]; rep0=N[2]; rep1=N[3]; u0=mod(power_mod(g,rep0,p)/power_mod(R,ch0,p),p); v0=mod(power_mod(PK,rep0,p)/power_mod(S,ch0,p),p); u1=mod(power_mod(g,rep1,p)/power_mod(R,ch1,p),p); v1=mod(power_mod(PK,rep1,p)/power_mod(Integer(S*inverse_mod(g,p)),ch1,p),p); hash_value2 = hashlib.sha256(str(R) + str(S) + str(u0) + str(v0)+str(u1)+str(v1)).hexdigest(); ch2 = Integer(int(hash_value2,16),q); ch=mod(ch0+ch1,q); if ch == ch2: return "accept" else: return "reject"
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> NameError: name 'b' is not defined
def tally(Bb,sk): R_r=1; S_r=1; for b in Bb: if validate(b) == 'reject': result="invalid"; pi_e="null" break; else: result="accept" R_r=Integer(mod(b[0][0]*R_r,p)); S_r=Integer(mod(b[0][1]*S_r,p)); if result != "invalid": g_r=Integer(mod(S_r*inverse_mod(power_mod(R_r,sk,p),p),p)); c1=R_r; c2=Integer(mod(S_r*Integer(inverse_mod(g_r,p)),p)); [ch,s]=myeqdlprove(g,c1,h,c2,sk) pi_e=[ch,s] for i in range(100): if power_mod(g,i,p) == g_r: result=i; return [result,pi_e]
myhas=hashlib.sha256('Anonymity_Privacy_and_Cybercrime_Zhong_').hexdigest(); myhas
'20351442a61b827a45107600114f448b40e55c1f185d5ad20cadbfa613f84065'
def myvote(): Bb=[0]*15; for i in range(15): if i < 5: Bb[i]=vote(1); else: Bb[i]=vote(0); return Bb
Bb=myvote(); h=key[1]; g=key[2]; sk=key[0]; [result,pi_e] =tally(Bb,sk) result
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "", line 5, in myvote File "", line 2, in vote NameError: global name 'key' is not defined