Skip to content

Commit 8a81924

Browse files
author
Janis Erdmanis
committed
adding group type for proposition deserialization
1 parent 01dfa96 commit 8a81924

File tree

6 files changed

+81
-22
lines changed

6 files changed

+81
-22
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SigmaProofs"
22
uuid = "f8559b4c-f045-44a2-8db2-503e40bb7416"
33
authors = ["Janis Erdmanis <[email protected]>"]
4-
version = "0.1.1"
4+
version = "0.1.2"
55

66
[deps]
77
CryptoGroups = "bc997328-bedd-407e-bcd3-5758e064a52d"

src/DecryptionProofs.jl

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module DecryptionProofs
22

33
using ..Serializer: Serializer, Path, load
4-
using ..SigmaProofs.Parser: Parser, Tree
4+
using ..SigmaProofs.Parser: Parser, Tree, width_elgamal_vec
55
using ..SigmaProofs: Proposition, Verifier, Simulator
66
using ..ElGamal: ElGamalRow
77
using ..LogProofs: ChaumPedersenProof, LogEquality
@@ -162,27 +162,37 @@ end
162162

163163
Serializer.save(proof::ChaumPedersenProof, ::Type{<:Decryption}, path::Path) = Serializer.save(proof, path; prefix="Decryption")
164164

165-
function Serializer.load(::Type{Decryption}, basedir::Path)
165+
function Serializer.load(::Type{T}, basedir::Path) where T <: Decryption
166+
167+
_extract_group(::Type{Decryption{G}}) where G <: Group = G
168+
_extract_group(::Type{Decryption}) = nothing
169+
170+
G = _extract_group(T)
166171

167172
publickey_tree = Parser.decode(read(joinpath(basedir, "publicKey.bt")))
168173
pk, g = Parser.unmarshal_publickey(publickey_tree; relative=true)
169174

170-
G = typeof(g)
175+
if isnothing(G)
176+
G = typeof(g)
177+
else
178+
pk = convert(G, pk)
179+
g = convert(G, g)
180+
end
171181

172182
ciphertexts_tree = Parser.decode(read(joinpath(basedir, "Ciphertexts.bt")))
173183
plaintexts_tree = Parser.decode(read(joinpath(basedir, "Decryption.bt")))
174-
175-
N = 1 # ToDo: extract that from the tree
184+
185+
N = width_elgamal_vec(G, ciphertexts_tree)
176186

177187
ciphertexts = convert(Vector{ElGamalRow{G, N}}, ciphertexts_tree)
178188
plaintexts = convert(Vector{NTuple{N, G}}, plaintexts_tree)
179189

180190
return Decryption(g, pk, ciphertexts, plaintexts)
181191
end
182192

183-
Serializer.load(::Type{P}, ::Type{Decryption}, path::Path) where P <: ChaumPedersenProof = Serializer.load(P, path; prefix="Decryption")
193+
Serializer.load(::Type{Decryption{G}}, basedir::Path) where G <: Group = load(Decryption, basedir; G)
184194

185-
# ToDO
195+
Serializer.load(::Type{P}, ::Type{Decryption}, path::Path) where P <: ChaumPedersenProof = Serializer.load(P, path; prefix="Decryption")
186196

187197
function Serializer.save(proposition::DecryptionInv, dir::Path)
188198

@@ -199,17 +209,28 @@ end
199209

200210
Serializer.save(proof::ChaumPedersenProof, ::Type{<:DecryptionInv}, path::Path) = Serializer.save(proof, path; prefix="DecryptionInv")
201211

202-
function Serializer.load(::Type{DecryptionInv}, basedir::Path)
203-
212+
213+
function Serializer.load(::Type{T}, basedir::Path) where T <: DecryptionInv
214+
215+
_extract_group(::Type{DecryptionInv{G}}) where G <: Group = G
216+
_extract_group(::Type{DecryptionInv}) = nothing
217+
218+
G = _extract_group(T)
219+
204220
publickey_tree = Parser.decode(read(joinpath(basedir, "publicKey.bt")))
205221
pk, g = Parser.unmarshal_publickey(publickey_tree; relative=true)
206222

207-
G = typeof(g)
223+
if isnothing(G)
224+
G = typeof(g)
225+
else
226+
pk = convert(G, pk)
227+
g = convert(G, g)
228+
end
208229

209230
ciphertexts_tree = Parser.decode(read(joinpath(basedir, "Ciphertexts.bt")))
210231
plaintexts_tree = Parser.decode(read(joinpath(basedir, "DecryptionInv.bt")))
211232

212-
N = 1 # ToDo: extract that from the tree
233+
N = width_elgamal_vec(G, ciphertexts_tree)
213234

214235
ciphertexts = convert(Vector{ElGamalRow{G, N}}, ciphertexts_tree)
215236
plaintexts = convert(Vector{NTuple{N, G}}, plaintexts_tree)

src/Parser.jl

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,29 @@ function marshal(x::PGroup)
272272
return tree
273273
end
274274

275+
# A bit verbose; I could instead read all curve specifications and retrieve the NIST name
276+
function normalize_ecgroup_name(x::String)
277+
name = replace(x, "_"=>"-")
278+
279+
lcase = lowercase(name)
280+
281+
if lcase == "prime192v1"
282+
return "P-192"
283+
elseif lcase == "secp224r1"
284+
return "P-224"
285+
elseif lcase == "prime256v1"
286+
return "P-256"
287+
elseif lcase == "secp384r1"
288+
return "P-384"
289+
elseif lcase == "secp521r1"
290+
return "P-521"
291+
else
292+
return name
293+
end
275294

276-
normalize_ecgroup_name(x::String) = replace(x, "_"=>"-")
277-
normalize_ecgroup_name(x::Symbol) = normalize_ecgroup_name(String(x))
295+
end
278296

297+
normalize_ecgroup_name(x::Symbol) = normalize_ecgroup_name(String(x))
279298

280299
function marshal(g::ECGroup)
281300

@@ -293,7 +312,6 @@ function marshal(g::ECGroup)
293312
return tree
294313
end
295314

296-
297315
function unmarshal(tree::Tree)
298316

299317
group_type = convert(String, tree.x[1])
@@ -307,7 +325,6 @@ function unmarshal(tree::Tree)
307325
end
308326
end
309327

310-
311328
function _unmarshal_pgroup(x::Node)
312329

313330
(p, q, g, e) = convert(Tuple{BigInt, BigInt, BigInt, UInt32}, x)
@@ -486,6 +503,22 @@ function unmarshal_privatekey(tree::Tree)
486503
return (s, g) ### The group can often be omited when not needed.
487504
end
488505

506+
# Returns the depth of the tree
507+
function depth(tree::Tree)
508+
509+
if tree isa Leaf
510+
return 0
511+
else
512+
return 1 + depth(tree[1])
513+
end
514+
515+
end
516+
517+
width_elgamal_row(::Type{<:PGroup}, tree::Tree) = depth(tree) == 1 ? 1 : length(tree[1])
518+
width_elgamal_row(::Type{<:ECGroup}, tree::Tree) = depth(tree) == 2 ? 1 : length(tree[1])
519+
520+
width_elgamal_vec(::Type{<:PGroup}, tree::Tree) = depth(tree) == 2 ? 1 : length(tree[1])
521+
width_elgamal_vec(::Type{<:ECGroup}, tree::Tree) = depth(tree) == 3 ? 1 : length(tree[1])
489522

490523
Tree(x::Vector{BigInt}; L = bitlength(maximum(x))) = Node([Leaf(i, L) for i in x])
491524

src/SigmaProofs.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ struct Simulator{T<:Proposition}
2323
# Alternative is to do conversion here if necessary
2424
@assert isvalid(typeof(proof), proposition) "Inconsistent simulator"
2525

26-
#@assert typeof(proof) <: proof_type(proposition)
27-
2826
return new{typeof(proposition)}(proposition, proof, verifier)
2927
end
3028
end

src/Verificatum/GeneratorBasis.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ using CryptoGroups.Specs: MODP, ECP
66
using CryptoPRG.Verificatum: PRG, RO
77
using CryptoUtils: is_quadratic_residue, sqrt_mod_prime
88

9-
# import ...SigmaProofs: generator_basis
10-
119
function modp_generator_basis(prg::PRG, p::Integer, q::Integer, N::Integer; nr::Integer = 0)
1210

1311
np = bitlength(p)

src/Verificatum/Verificatum.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,14 @@ function Serializer.save(spec::ProtocolSpec, path::Path; name="undefined")
250250
end
251251

252252

253-
function Serializer.load(::Type{ProtocolSpec}, path::Path; auxsid = "default")
254253

254+
function Serializer.load(::Type{T}, path::Path; auxsid = "default") where T <: ProtocolSpec
255+
256+
_extract_group(::Type{ProtocolSpec{G}}) where G <: Group = G
257+
_extract_group(::Type{ProtocolSpec}) = nothing
258+
259+
G = _extract_group(T)
260+
255261
xml = read(path) |> String
256262

257263
rohash = HashSpec(match(r"<rohash>(.*?)</rohash>", xml)[1] |> map_hash_name)
@@ -263,11 +269,14 @@ function Serializer.load(::Type{ProtocolSpec}, path::Path; auxsid = "default")
263269
ne = parse(Int32, match(r"<ebitlenro>(.*?)</ebitlenro>", xml)[1])
264270

265271
g = unmarshal(decode(split(s_Gq, "::")[2]))
272+
273+
if !isnothing(G)
274+
g = convert(G, g)
275+
end
266276

267277
version = match(r"<version>(.*?)</version>", xml)[1] |> String
268278
sid = match(r"<sid>(.*?)</sid>", xml)[1] |> String
269279

270-
271280
return ProtocolSpec(; g, nr, nv, ne, prghash, rohash, version, sid, auxsid)
272281
end
273282

0 commit comments

Comments
 (0)