Xornet-Euphoria / zero-knowledge_proof

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

zero-knowledge_proof

Prerequisite

  • pwntools
  • PyCryptodome

Abstract

ゼロ知識証明の勉強のために自分で実装してみた際のコードです。離散対数問題の解を秘密として、それを所持していることを証明するAliceとそれを検証するBobでの通信を行っています。
また、攻撃者MalloryがAliceになりすましたとしても証明に失敗する例も用意しています。

how to use

  1. edit protocol.py (if you need)

  2. python alice.py or python mallory.py

  3. python bob.py

example

Valid Prover(Alice)

Alice(Prover)

$ python alice.py
[*] parameters are set.
{'p': 76561679471715412728912744660712718367045959006674734607735040049799702935863, 'g': 5, 'h': 3785009918282855528211561954943880141914973771496585893017675070654652107758}
[*] secret -> 70662950738187723213755684117953245089002682460179799994135410121540961232592
[+] Trying to bind to 0.0.0.0 on port 43121: Done
[+] Waiting for connections on 0.0.0.0:43121: Got connection from 127.0.0.1 on port 51944
[*] I got c: 1
[*] I got c: 0
[*] I got c: 1
[*] I got c: 0
[*] I got c: 0
[*] I got c: 1
[*] I got c: 1
[*] I got c: 1
[*] I got c: 0
[*] I got c: 1
[*] Closed connection to 127.0.0.1 port 51944

Bob(Verifier)

$ python bob.py
[+] Opening connection to localhost on port 43121: Done
Hi bob. This is our parameters.

{'p': 76561679471715412728912744660712718367045959006674734607735040049799702935863, 'g': 5, 'h': 3785009918282855528211561954943880141914973771496585893017675070654652107758}
[*] ~~~~~ round 1 ~~~~~
[*]   I got x: 52725007870267950891911152150133001784372249978507709207183949795985428370155
[*]   I send c: 1
[*]   I got y: 73290106583291367666370832070379760057462277862740383814002801610025449771305
[*]   Accepted
[*] ~~~~~ round 2 ~~~~~
[*]   I got x: 66636544622951683327526362460423754462398749258380556161336883847155491093284
[*]   I send c: 0
[*]   I got y: 9541295973318536971608601308157715758385916781856532551044314008876212918050
[*]   Accepted
[*] ~~~~~ round 3 ~~~~~
[*]   I got x: 75161675829883095970842469916242687011218757911011813805806760943229928939672
[*]   I send c: 1
[*]   I got y: 99002444007562290123120933704870999587896307660468838573353787151792719100793
[*]   Accepted
[*] ~~~~~ round 4 ~~~~~
[*]   I got x: 58153266848820089169045076412422056408962124505251427254753371602216870806869
[*]   I send c: 0
[*]   I got y: 23801152765142127340361812392233335393815957101655221265743038364880343408286
[*]   Accepted
[*] ~~~~~ round 5 ~~~~~
[*]   I got x: 37934953945459937005165440838056506890048682031272604744122016059377560241319
[*]   I send c: 0
[*]   I got y: 68937861341669821971375929586849722448243111017983883956340335245256984569641
[*]   Accepted
[*] ~~~~~ round 6 ~~~~~
[*]   I got x: 30654532133778639583789776593297090340429037629653144135566276814309554543917
[*]   I send c: 1
[*]   I got y: 119257058598275936021719097233478453953049301609879636843922001837289782526125
[*]   Accepted
[*] ~~~~~ round 7 ~~~~~
[*]   I got x: 12884793206860350446160213402231850000324575171296842468421098405987362277693
[*]   I send c: 1
[*]   I got y: 91995678060344440736245801944276447838282212485451162895517989750621322646353
[*]   Accepted
[*] ~~~~~ round 8 ~~~~~
[*]   I got x: 22236108096231187001344958678363261243487044012922807570071409339219551569385
[*]   I send c: 1
[*]   I got y: 74194039196183929925284662886501328580947220955018295484938942210336115722124
[*]   Accepted
[*] ~~~~~ round 9 ~~~~~
[*]   I got x: 49403089253561867162076161227297844849347070405073646594299071904824310590726
[*]   I send c: 0
[*]   I got y: 16414488728338641714233014206618478385030935242346294992876148806163062315365
[*]   Accepted
[*] ~~~~~ round 10 ~~~~~
[*]   I got x: 20631580977829787357285161628260431101277406072476485957840094212420425841745
[*]   I send c: 1
[*]   I got y: 79474713641524692115932855021857487721024741724236732809210209153450641913691
[*]   Accepted
[*] Closed connection to localhost port 43121
[*] Alice is a valid prover

Invalid Prover(Mallory)

Mallory(Invalid Prover)

$ python mallory.py
[*] parameters are set.
{'p': 115339763981940619229218684242493798545353535236578098539298886653342087469529, 'g': 2, 'h': 112644052166310359255936996535527273386152105759172028023452775906566955371751}
[*] I dont know secret
[+] Trying to bind to 0.0.0.0 on port 43121: Done
[+] Waiting for connections on 0.0.0.0:43121: Got connection from 127.0.0.1 on port 54442
[*] [round 1]: I expcted 1 and got c: 1
[*] [round 2]: I expcted 1 and got c: 0
[*] [round 3]: I expcted 0 and got c: 0
[*] [round 4]: I expcted 1 and got c: 0
[*] [round 5]: I expcted 1 and got c: 0
[*] [round 6]: I expcted 0 and got c: 1
[*] [round 7]: I expcted 0 and got c: 1
[*] [round 8]: I expcted 0 and got c: 0
[*] [round 9]: I expcted 0 and got c: 0
[*] [round 10]: I expcted 0 and got c: 1
[*] Closed connection to 127.0.0.1 port 54442

Bob

$ python bob.py
[+] Opening connection to localhost on port 43121: Done
Hi bob. This is our parameters.

{'p': 115339763981940619229218684242493798545353535236578098539298886653342087469529, 'g': 2, 'h': 112644052166310359255936996535527273386152105759172028023452775906566955371751}
[*] ~~~~~ round 1 ~~~~~
[*]   I got x: 26900555544412463823782968446509563256900641942138388989889407326472218596261
[*]   I send c: 1
[*]   I got y: 46537827931773255063303769112255431122177165664750171391664215975133121284040
[!]   wrong responce...
[*] ~~~~~ round 2 ~~~~~
[*]   I got x: 6608219139816559507032228584675707217481358781387187525426439055101682861298
[*]   I send c: 0
[*]   I got y: 87492164282923926096863504168248731889635508197791725605463033783865346137563
[!]   wrong responce...
[*] ~~~~~ round 3 ~~~~~
[*]   I got x: 6617873149112899712950470782429657729925099525572353556494069129407566895996
[*]   I send c: 0
[*]   I got y: 111330555754678467276501161005565659611078993149973526601915817005965100665635
[*]   Accepted
[*] ~~~~~ round 4 ~~~~~
[*]   I got x: 38634719543310746252573057604893878696380744740056832158160075997564679478107
[*]   I send c: 0
[*]   I got y: 31886976147943596761503529197078305611105276540829152981421748454029942307624
[!]   wrong responce...
[*] ~~~~~ round 5 ~~~~~
[*]   I got x: 40532530493250565590801712809848721651984151618697075717550439913748563497609
[*]   I send c: 0
[*]   I got y: 23711894308522342746988797183004707109525387935763136560348157041936737995816
[!]   wrong responce...
[*] ~~~~~ round 6 ~~~~~
[*]   I got x: 65860645126362791061466982603000547010501188276795163042257071480566761063446
[*]   I send c: 1
[*]   I got y: 90824630313351793850690211485384739513777101434641127323662235186223009054520
[!]   wrong responce...
[*] ~~~~~ round 7 ~~~~~
[*]   I got x: 110069160128537454218520077535480499129722244010952883278256082850191689224539
[*]   I send c: 1
[*]   I got y: 50054857088821936005984533664878884327037589196637775894151261725530826011262
[!]   wrong responce...
[*] ~~~~~ round 8 ~~~~~
[*]   I got x: 30529656327413155957112053289626048810064738018142921359751453477855408090981
[*]   I send c: 0
[*]   I got y: 66550941950090020489206885660642958378025673473876029746949221620216186946528
[*]   Accepted
[*] ~~~~~ round 9 ~~~~~
[*]   I got x: 33089212197472170018020173156163149131154351986478442924067960222559300429802
[*]   I send c: 0
[*]   I got y: 42679126788482150106266641600999397496686714374613996907644206956885274868587
[*]   Accepted
[*] ~~~~~ round 10 ~~~~~
[*]   I got x: 42601930420653368816049636592625757139587134552722624658201597057337184624591
[*]   I send c: 1
[*]   I got y: 10329201827508415636643815148804645764605412112523268827222737275646426624612
[!]   wrong responce...
[*] Closed connection to localhost port 43121
[!] Alice is not a valid prover

todo

  • aliceがsecretを知らない場合をコマンドライン引数で指定出来るようにして証明を失敗させる

About


Languages

Language:Python 100.0%