Skip to content

Commit dc87c94

Browse files
committed
Add push-button ZKP Log demo
1 parent eeda5b1 commit dc87c94

File tree

1 file changed

+110
-1
lines changed

1 file changed

+110
-1
lines changed

lib/mix/tasks/demo/zkp_log.ex

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,117 @@ defmodule Mix.Tasks.Demo.ZkpLog do
33
@requirements ["loadpaths", "app.config", "app.start"]
44

55
use Mix.Task
6+
require Logger
7+
8+
defmodule NonInteractiveProverGood do
9+
use Zkp.ZkpChor.Chorex, :prover
10+
11+
@impl true
12+
def get_ident() do
13+
"alice"
14+
end
15+
16+
@impl true
17+
def get_secret(username) do
18+
passwd = "hello world"
19+
:crypto.hash(:sha256, passwd <> username)
20+
end
21+
22+
@impl true
23+
def gen_verification_token(username, password, g, p) do
24+
:crypto.mod_pow(g, :crypto.hash(:sha256, password <> username), p)
25+
end
26+
27+
@impl true
28+
def notify_progress(n) do
29+
IO.puts("[Prover] verified; #{n} rounds of verification remain")
30+
end
31+
end
32+
33+
defmodule NonInteractiveProverBad do
34+
use Zkp.ZkpChor.Chorex, :prover
35+
36+
@impl true
37+
def get_ident() do
38+
"alice"
39+
end
40+
41+
@impl true
42+
def get_secret(username) do
43+
passwd = "NOT MY PASSWORD"
44+
:crypto.hash(:sha256, passwd <> username)
45+
end
46+
47+
@impl true
48+
def gen_verification_token(username, password, g, p) do
49+
:crypto.mod_pow(g, :crypto.hash(:sha256, password <> username), p)
50+
end
51+
52+
@impl true
53+
def notify_progress(n) do
54+
IO.puts("[Prover] verified; #{n} rounds of verification remain")
55+
end
56+
end
657

758
def run(_args) do
8-
IO.puts("Hey look running the ZKP_LOG demo!")
59+
Logger.info("Starting non-interactive ZKP logarithm login demo")
60+
Logger.info("Registration: using username 'alice', password 'hello world'")
61+
62+
Logger.info(
63+
"You should see 'User alice registered."
64+
)
65+
66+
Chorex.start(
67+
Zkp.ZkpChor.Chorex,
68+
%{ Prover => NonInteractiveProverGood, Verifier => Zkp.LogVerifier },
69+
["alice", "hello world", :register]
70+
)
71+
72+
receive do
73+
{:chorex_return, Verifier, {:ok, userid}} -> IO.puts("User #{userid} registered.")
74+
{:chorex_return, Verifier, :failed} -> IO.puts("User not registered.")
75+
end
76+
receive do
77+
{:chorex_return, Prover, _} -> IO.puts("Prover finished.")
78+
end
79+
80+
Logger.info("Registration done. Attempting login with good credentials.")
81+
82+
Logger.info(
83+
"You should see that login is successful after 5 challenge rounds."
84+
)
85+
86+
Chorex.start(
87+
Zkp.ZkpChor.Chorex,
88+
%{ Prover => NonInteractiveProverGood,
89+
Verifier => Zkp.LogVerifier },
90+
[5])
91+
92+
receive do
93+
{:chorex_return, Verifier, resp} -> IO.puts("Verifier responds #{inspect resp}")
94+
end
95+
96+
receive do
97+
{:chorex_return, Prover, resp} -> IO.puts("Prover responds #{inspect resp}")
98+
end
99+
100+
Logger.info("Now trying to login with bad credentials.")
101+
Logger.info("You should see a failure.")
102+
103+
Chorex.start(
104+
Zkp.ZkpChor.Chorex,
105+
%{ Prover => NonInteractiveProverBad,
106+
Verifier => Zkp.LogVerifier },
107+
[5])
108+
109+
receive do
110+
{:chorex_return, Verifier, resp} -> IO.puts("Verifier responds #{inspect resp}")
111+
end
112+
113+
receive do
114+
{:chorex_return, Prover, resp} -> IO.puts("Prover responds #{inspect resp}")
115+
end
116+
117+
Logger.info("ZKP Logarithm demo complete")
9118
end
10119
end

0 commit comments

Comments
 (0)