-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPOC.lua
221 lines (201 loc) · 6.17 KB
/
POC.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
-- Copyright (C) 2024 Dyne.org foundation designed, written and
-- maintained by Denis Roio <jaromil@dyne.org>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public
-- License along with this program. If not, see
-- <https://www.gnu.org/licenses/>.
CONF.output.encoding = { fun = get_encoding_function'url64',
name = 'url64' }
-- Simple BLS signature formula
-- δ = r.O
-- γ = δ.G2
-- σ = δ * ( H(m)*G1 )
-- assume: ε(δ*G2, H(m)) == ε(G2, δ*H(m))
-- check: ε(γ, H(m)) == ε(G2, σ)
-- SD-BLS adds revocation key signature formula
-- ρ = β * ( H(m)*G1 )
-- σ = σ + ρ
-- assume:
-- v(σ - ρ, δ*G2)
-- where:
-- G1, G2 = generator points for ECP1, ECP2
G1 = ECP.generator()
G2 = ECP2.generator()
-- H = hash to point function to ECP1
HG1 = ECP.hashtopoint
Miller = PAIR.ate
function keygen()
-- δ = r.O
-- γ = δ.G2
local sk = INT.random()
return { sk = sk,
pk = G2 * sk }
end
function sign(sk, msg)
-- σ = δ * ( H(m)*G1 )
return HG1(msg) * sk
end
function verify(pk, msg, sig)
-- e(γ,H(m)) == e(G2,σ)
return(
Miller(pk, HG1(msg))
==
Miller(G2, sig)
)
end
-- Issuer's keyring hardcoded 0x0 seed
A = {sk = BIG.new(sha256(OCTET.zero(32)))}
A.pk = G2*A.sk
-- Holder sends claims to issuer and proves them
CLAIMS = {
name = "Pasqualino",
surname = "Frafuso",
nickname = "Settebellezze",
born_in = "Napoli",
gender = "male",
above_18 = 'true',
nationality = "italian"
}
-- Issuer signs claims
SIGNED_CLAIMS = { }
REVOCATIONS = { }
for k,v in pairs(CLAIMS) do
local rev = BIG.random()
local id = k..'='..v
local sig = sign(A.sk, id) + sign(rev, id)
SIGNED_CLAIMS[id] = { sig, G1 * rev, G2 * rev }
REVOCATIONS['HolderID/'..id] = rev
end
--I.warn({ Revocations = A_REVOKES })
-- SignedClaims = S_CLAIMS })
-- holder discloses 3 credentials
disclose = { 'name', 'gender', 'above_18' }
CREDENTIAL_PROOF = { }
sha256 = HASH.new('sha256')
local tri
for m,v in pairs(SIGNED_CLAIMS) do
local sig = v[1] -- naked issuer's sig
local revG1 = v[2]
local revG2 = v[3]
local claim = strtok(m, '=')
local er = BIG.random()
local tri = BIG.new(sha256:process(
(Miller(A.pk, revG1) ^ er):octet()
))
-- assert(tri == BIG.new(sha256:process(
-- (PAIR.ate(A.pk, G1*er)^rev):octet()
-- )))
if array_contains(disclose, claim[1]) then
table.insert(CREDENTIAL_PROOF, {
id = m,
s = sig + sign(tri, m),
p = (revG2 + G2*tri):to_zcash(),
r = (A.pk*er):to_zcash() -- protect
})
end
end
function revocation_contains(revocations, claim)
local res = false -- store here result for constant time operations
local rev = revocations[claim.id]
if rev then
local tri =
BIG.new(
sha256:process(
(Miller(ECP2.from_zcash(claim.r), G1)^rev)
:octet()
))
if -- addendum of claim.p is equal to revG2
ECP2.from_zcash(claim.p) - (G2*tri) == G2*rev
and -- verify unblinded issuer signature
verify(A.pk, claim.id,
claim.s
- sign(tri, claim.id)
- sign(rev, claim.id))
then
res = true
end
end
return res
end
-- disclose = { 'name', 'gender', 'above_18' }
local torevoke = {
'HolderID/born_in=Napoli',
'HolderID/gender=male',
'HolderID/nationality=italian'}
local revocations = {}
for _,v in pairs(torevoke) do
local k = strtok(v,'/')[2]
revocations[k] = REVOCATIONS[v]
end
-- show encoded claims example
print(JSON.encode({
credential_proof = CREDENTIAL_PROOF,
verifier = 'IssuerID',
revocations = revocations
}))
-- relying party verifies credentials
-- downloads PK of IssuerID from DID
for _,proof in pairs(CREDENTIAL_PROOF) do
local sig = proof.s
local pk = ECP2.from_zcash(proof.p)
if proof.id == 'gender=male' then
assert(revocation_contains(revocations, proof), "Not revoked: "..proof.id)
else
assert(not revocation_contains(revocations, proof), "Revoked: "..proof.id)
end
assert( verify(pk + A.pk, proof.id, sig) )
end
local function FUZZ(o)
t = type(o)
assert(iszen(t), "cannot fuzz non zenroom type: "..t)
local tmp = o:octet():fuzz_byte()
if t == 'zenroom.ecp' then return ECP.new(tmp) end
if t == 'zenroom.ecp2' then return ECP2.new(tmp) end
if t == 'zenroom.big' then return BIG.new(tmp) end
if t == 'zenroom.octet' then return tmp end
error("cannot fuzz zenroom type: "..t)
end
warn('random proof.s')
for _,proof in pairs(CREDENTIAL_PROOF) do
local sig = ECP.random() -- FUZZ
local pk = ECP2.from_zcash(proof.p)
if proof.id == 'gender=male' then
assert(revocation_contains(revocations, proof), "Not revoked: "..proof.id)
else
assert(not revocation_contains(revocations, proof), "Revoked: "..proof.id)
end
assert( not verify(pk + A.pk, proof.id, sig) )
end
warn('random proof.p')
for _,proof in pairs(CREDENTIAL_PROOF) do
local sig = proof.s
local pk = ECP2.random() -- FUZZ
if proof.id == 'gender=male' then
assert(revocation_contains(revocations, proof), "Not revoked: "..proof.id)
else
assert(not revocation_contains(revocations, proof), "Revoked: "..proof.id)
end
assert( not verify(pk + A.pk, proof.id, sig) )
end
warn('random proof.r')
for _,proof in pairs(CREDENTIAL_PROOF) do
local sig = proof.s
local pk = ECP2.from_zcash(proof.p)
proof.r = (A.pk*BIG.random()):to_zcash() -- FUZZ(proof.r)
if proof.id == 'gender=male' then
assert(revocation_contains(revocations, proof), "Not revoked: "..proof.id)
else
assert(not revocation_contains(revocations, proof), "Revoked: "..proof.id)
end
assert( verify(pk + A.pk, proof.id, sig) )
end