-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.lua
199 lines (182 loc) · 5 KB
/
common.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
-- SD-BLS common functions for the proof of concept in Zenroom
--
-- 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' }
G1 = ECP.generator()
G2 = ECP2.generator()
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
-- POC high level API
function issuer_sign_kv(sk, claims)
local signed = { }
local revs= { }
for k,v in pairs(claims) do
local rSK = BIG.random()
local m = k..'='..v
local o = { }
o.r = (G2 * rSK):to_zcash()
o.H = sha256(m..o.r)
o.s = sign(A.sk, o.H..o.r) + sign(rSK, o.H..o.r)
signed[m] = o
revs['HolderID/'..m] = rSK
end
return signed, revs
end
function issuer_sign(sk, claims)
local signed = { }
local revs= { }
for k,v in pairs(claims) do
local rSK = BIG.random()
local m = k..'='..v
local o = { }
o.r = (G2 * rSK):to_zcash()
o.H = sha256(m..o.r)
o.s = sign(A.sk, o.H..o.r) + sign(rSK, o.H..o.r)
signed[m] = o
revs[o.H] = rSK
end
return signed, revs
end
function issuer_revoke(revocations, torevoke)
local revs = { }
for _,v in pairs(torevoke) do
-- revokers will keep a database of HolderIDs with revocations; the
-- privacy of such databases can be enhanced by not holding values
-- in such a database.
local m = strtok(v,'/')[2]
local r = (G2*revocations[v]):to_zcash()
local h = sha256(m..r)
-- I.warn(r)
revs[h] = revocations[v]
end
return revs
end
function holder_prove(signed_claims, disclosures)
local res = { }
for m,v in pairs(signed_claims) do
if array_contains(disclosures, m) then
local obj = {
m = m,
H = v.H,
r = v.r
}
local tSK = BIG.random()
obj.s = v.s + sign(tSK, obj.H..obj.r)
obj.t = os.date()
obj.p = (G2*tSK):to_zcash()
obj.c = sign(tSK, obj.H .. obj.r .. obj.s:octet() .. obj.t)
table.insert(res, obj)
end
end
return res
end
-- used in POC with k=v credentials
function holder_prove_kv(signed_claims, disclosures)
local res = { }
for m,v in pairs(signed_claims) do
local claim = strtok(m, '=')
if array_contains(disclosures, claim[1]) then
local obj = {
m = m,
H = v.H,
r = v.r
}
local tSK = BIG.random()
obj.s = v.s + sign(tSK, obj.H..obj.r)
obj.t = os.date()
obj.p = (G2*tSK):to_zcash()
obj.c = sign(tSK, obj.H .. obj.r .. obj.s:octet() .. obj.t)
table.insert(res, obj)
end
end
return res
end
function verify_proof(APK, proof)
local res = true and
verify(ECP2.from_zcash(proof.p),
(proof.H .. proof.r .. proof.s:octet() .. proof.t),
proof.c)
return res and verify(ECP2.from_zcash(proof.r) +
ECP2.from_zcash(proof.p) +
APK, proof.H..proof.r, proof.s)
end
function anon_revocation_contains(rev, proof)
local res = false -- store here result for constant time operations
local r = proof.r
-- assert(revocations[h])
-- TODO: for some reason revocations[proof.H] doesn't works
for _,v in ipairs(rev) do
res = r == (G2*v):to_zcash()
end
return res
end
function revocation_contains(rev, proof)
local res = false -- store here result for constant time operations
local h = proof.H
local r = proof.r
-- assert(revocations[h])
-- TODO: for some reason revocations[proof.H] doesn't works
local f = rev[h]
if f then
res = r == (G2*f):to_zcash()
end
return res
end
-- Benchmark functions
function generate_fake_claims(num)
local cls = { }
for i=1,num do
cls[OCTET.random(8)] = OCTET.random(8)
end
return cls
end
function test_many_revocs(num, revocs, proofs)
local start = os.clock()
local c = 0
local found = 0
for k,v in ipairs(proofs) do
local pk = ECP2.from_zcash(v.r)
if verify(pk + A.pk, v.id, v.s) then
found = revocation_contains(revocs, v)
end
c = c + 1
if c == num then break end
end
assert(found == 1)
return os.clock() - start
end