diff options
author | Schanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de> | 2018-05-15 19:15:19 +0200 |
---|---|---|
committer | Schanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de> | 2018-08-16 17:08:01 +0200 |
commit | 82a2868eacfc8c11946183ee373301832f39c01b (patch) | |
tree | 979d23c860df03355d5acebbca8da88c17c10875 /src/zklaim | |
parent | 5bb2565bab0b8fa8cac263b9fb70edf3ae2278d9 (diff) | |
download | gnunet-82a2868eacfc8c11946183ee373301832f39c01b.tar.gz gnunet-82a2868eacfc8c11946183ee373301832f39c01b.zip |
start zklaim
Diffstat (limited to 'src/zklaim')
-rw-r--r-- | src/zklaim/Makefile.am | 31 | ||||
-rw-r--r-- | src/zklaim/main.c | 237 |
2 files changed, 268 insertions, 0 deletions
diff --git a/src/zklaim/Makefile.am b/src/zklaim/Makefile.am new file mode 100644 index 000000000..44f016f2b --- /dev/null +++ b/src/zklaim/Makefile.am | |||
@@ -0,0 +1,31 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
3 | |||
4 | if MINGW | ||
5 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols | ||
6 | endif | ||
7 | |||
8 | if USE_COVERAGE | ||
9 | AM_CFLAGS = --coverage -O0 | ||
10 | XLIB = -lgcov | ||
11 | endif | ||
12 | |||
13 | bin_PROGRAMS = \ | ||
14 | gnunet-zklaim | ||
15 | |||
16 | gnunet_zklaim_SOURCES = \ | ||
17 | main.c | ||
18 | gnunet_zklaim_LDFLAGS = -rdynamic | ||
19 | gnunet_zklaim_LDADD = \ | ||
20 | -lzklaim \ | ||
21 | -lgcrypt \ | ||
22 | -lsnark \ | ||
23 | -lff \ | ||
24 | -lgmp \ | ||
25 | -lgmpxx \ | ||
26 | -lcrypto \ | ||
27 | -lprocps \ | ||
28 | -lzm \ | ||
29 | -lstdc++ \ | ||
30 | -lm | ||
31 | |||
diff --git a/src/zklaim/main.c b/src/zklaim/main.c new file mode 100644 index 000000000..3fe823ba1 --- /dev/null +++ b/src/zklaim/main.c | |||
@@ -0,0 +1,237 @@ | |||
1 | /** | ||
2 | * Test File for zklaim c implementation | ||
3 | */ | ||
4 | |||
5 | #include "zklaim.h" | ||
6 | #include "zklaim_ecc.h" | ||
7 | #include "zklaim_hash.h" | ||
8 | #include <stdlib.h> | ||
9 | #include <stdio.h> | ||
10 | #include <string.h> | ||
11 | #include <gcrypt.h> | ||
12 | |||
13 | // to boost up valgrind | ||
14 | int worker() { | ||
15 | gcry_sexp_t priv, pub; | ||
16 | zklaim_gen_pk(&priv); | ||
17 | zklaim_get_pub(priv, &pub); | ||
18 | unsigned char *pubbuf; | ||
19 | size_t publen; | ||
20 | zklaim_pub2buf(pub, &pubbuf, &publen); | ||
21 | |||
22 | printf("===========================================================================\n"); | ||
23 | printf("===========================================================================\n"); | ||
24 | printf("============================== ISSUER =====================================\n"); | ||
25 | printf("===========================================================================\n"); | ||
26 | printf("===========================================================================\n"); | ||
27 | //zklaim_ctx* ctx = zklaim_context_init(); | ||
28 | //zklaim_proving_key* pk = zklaim_proving_key_get(ctx); | ||
29 | /* | ||
30 | * - 1 - | ||
31 | * initialize data structures | ||
32 | */ | ||
33 | printf("[ISSUER] initializing context\n"); | ||
34 | zklaim_ctx *ctx = zklaim_context_new(); | ||
35 | |||
36 | if (sizeof(ctx->pub_key) != publen) { | ||
37 | printf("size mismatch!"); | ||
38 | return 1; | ||
39 | } | ||
40 | |||
41 | // TODO: there should be a zklaim method for this | ||
42 | memcpy(ctx->pub_key, pubbuf, sizeof(ctx->pub_key)); | ||
43 | free(pubbuf); | ||
44 | |||
45 | //print_sexp(pub); | ||
46 | |||
47 | /* | ||
48 | * - 2 - | ||
49 | * setup the first payload | ||
50 | */ | ||
51 | printf("[ISSUER] Setting up payloads..\n"); | ||
52 | zklaim_payload pl, pl2; | ||
53 | memset(&pl, 0, sizeof(zklaim_payload)); | ||
54 | pl.data0_ref = 18; | ||
55 | pl.data0_op = (enum zklaim_op) (zklaim_greater | zklaim_eq); | ||
56 | pl.data1_ref = 1; | ||
57 | pl.data1_op = zklaim_eq; | ||
58 | pl.data2_ref = 2; | ||
59 | pl.data2_op = zklaim_eq; | ||
60 | pl.data3_ref = 3; | ||
61 | pl.data3_op = zklaim_eq; | ||
62 | pl.data4_ref = 600; | ||
63 | pl.data4_op = zklaim_less; | ||
64 | pl.priv = 0; | ||
65 | |||
66 | memset(&pl2, 0, sizeof(zklaim_payload)); | ||
67 | pl2.data0_ref = 0; | ||
68 | pl2.data0_op = zklaim_noop; | ||
69 | pl2.data1_ref = 0; | ||
70 | pl2.data1_op = zklaim_noop; | ||
71 | pl2.data2_ref = 0; | ||
72 | pl2.data2_op = zklaim_noop; | ||
73 | pl2.data3_ref = 0; | ||
74 | pl2.data3_op = zklaim_noop; | ||
75 | pl2.data4_ref = 9223372036854775807; | ||
76 | pl2.data4_op = zklaim_less_or_eq; | ||
77 | pl2.priv = 0; | ||
78 | |||
79 | // fill in the values | ||
80 | zklaim_set_attr(&pl, 23, 0); | ||
81 | zklaim_set_attr(&pl, 1, 1); | ||
82 | zklaim_set_attr(&pl, 2, 2); | ||
83 | zklaim_set_attr(&pl, 3, 3); | ||
84 | zklaim_set_attr(&pl, 599, 4); | ||
85 | |||
86 | zklaim_set_attr(&pl2, 0, 0); | ||
87 | zklaim_set_attr(&pl2, 0, 1); | ||
88 | zklaim_set_attr(&pl2, 0, 2); | ||
89 | zklaim_set_attr(&pl2, 0, 3); | ||
90 | zklaim_set_attr(&pl2, 9223372036854775807, 4); | ||
91 | |||
92 | /* | ||
93 | * - 3 - | ||
94 | * add payload to context | ||
95 | */ | ||
96 | printf("[ISSUER] adding payloads to context\n"); | ||
97 | zklaim_add_pl(ctx, pl); | ||
98 | //zklaim_add_pl(ctx, pl2); | ||
99 | //zklaim_add_pl(ctx, pl2); | ||
100 | zklaim_hash_ctx(ctx); | ||
101 | |||
102 | printf("[ISSUER] performing trusted setup in order to generate keys\n"); | ||
103 | printf("-> trusted setup result: %s\n", (zklaim_trusted_setup(ctx) ? "failed" : "passed")); | ||
104 | |||
105 | //unsigned char* buf; | ||
106 | //size_t vksize = zklaim_verification_key_serialize(ctx, &buf); | ||
107 | //printf("serialized vk size: %zuB\n", vksize); | ||
108 | |||
109 | // write key to disk | ||
110 | //FILE* f = fopen("/tmp/vk1", "w"); | ||
111 | //fwrite(buf, vksize, 1, f); | ||
112 | //fclose(f); | ||
113 | |||
114 | //zklaim_verification_key_deserialize(ctx2, buf, vksize); | ||
115 | |||
116 | //free(buf); | ||
117 | |||
118 | //size_t pksize = zklaim_proving_key_serialize(ctx, &buf); | ||
119 | //printf("serialized pk size: %.2fMB\n", ((float) pksize)/1000/1000); | ||
120 | |||
121 | //zklaim_proving_key_deserialize(ctx, buf, pksize); | ||
122 | |||
123 | //zklaim_print(ctx); | ||
124 | |||
125 | //zklaim_ctx_sign(ctx, priv); | ||
126 | |||
127 | |||
128 | printf("-> signing context: %s\n", zklaim_ctx_sign(ctx, priv) ? "failed" : "passed"); | ||
129 | |||
130 | unsigned char* ctx_issuer; | ||
131 | |||
132 | printf("\n[ISSUER] serializing context\n"); | ||
133 | size_t len = zklaim_ctx_serialize(ctx, &ctx_issuer); | ||
134 | |||
135 | /* | ||
136 | * - 4 - | ||
137 | * generate proof | ||
138 | */ | ||
139 | |||
140 | //printf("\n-> signing context: %s\n", zklaim_ctx_sign(ctx, priv) ? "failed" : "passed"); | ||
141 | |||
142 | //unsigned char* proof; | ||
143 | //size_t proof_size = zklaim_proof_serialize(ctx, &proof); | ||
144 | //printf("serialized proof size: %zuB\n", proof_size); | ||
145 | |||
146 | //zklaim_proof* zkproof_imported; | ||
147 | |||
148 | //zklaim_proof_deserialize(ctx, proof, proof_size); | ||
149 | |||
150 | printf("===========================================================================\n"); | ||
151 | printf("===========================================================================\n"); | ||
152 | printf("============================== PROVER =====================================\n"); | ||
153 | printf("===========================================================================\n"); | ||
154 | printf("===========================================================================\n"); | ||
155 | |||
156 | |||
157 | zklaim_ctx* ctx_prover = zklaim_context_new(); | ||
158 | printf("\n[PROVER] deserializing context\n"); | ||
159 | printf("-> deserialisation status: %s\n\n", zklaim_ctx_deserialize(ctx_prover, ctx_issuer, len) ? "failed" : "passed"); | ||
160 | |||
161 | zklaim_print(ctx_prover); | ||
162 | |||
163 | ctx_prover->pk = ctx->pk; | ||
164 | |||
165 | int res = zklaim_ctx_verify(ctx_prover); | ||
166 | printf("[PROVER] verification result: %d %s\n", res, (res ? "failed" : "passed")); | ||
167 | printf(" ^--- failure with code 3 (ZKLAIM_INVALID_PROOF) is ok for the prover, as signature passes, but (non-existing) proof fails\n\n"); | ||
168 | |||
169 | printf("[PROVER] resetting operations and reference values to create taylored proof\n"); | ||
170 | printf("[PROVER] generating proof\n"); | ||
171 | |||
172 | ctx_prover->pk = (unsigned char*) calloc(1, ctx->pk_size); | ||
173 | ctx_prover->pk_size = ctx->pk_size; | ||
174 | memcpy(ctx_prover->pk, ctx->pk, ctx_prover->pk_size); | ||
175 | |||
176 | // set custom prover reference values here: | ||
177 | ctx_prover->pl_ctx_head->pl.data0_ref = 20; | ||
178 | //ctx_prover->pl_ctx_head->pl.data0_op = zklaim_less; | ||
179 | ctx_prover->pl_ctx_head->pl.data4_ref = 0; | ||
180 | ctx_prover->pl_ctx_head->pl.data4_op = zklaim_noop; | ||
181 | |||
182 | ctx_prover->pl_ctx_head->pl.data1_ref = 0; | ||
183 | ctx_prover->pl_ctx_head->pl.data1_op = zklaim_noop; | ||
184 | |||
185 | ctx_prover->pl_ctx_head->pl.data2_ref = 0; | ||
186 | ctx_prover->pl_ctx_head->pl.data2_op = zklaim_noop; | ||
187 | |||
188 | ctx_prover->pl_ctx_head->pl.data3_ref = 0; | ||
189 | ctx_prover->pl_ctx_head->pl.data3_op = zklaim_noop; | ||
190 | |||
191 | zklaim_print(ctx_prover); | ||
192 | |||
193 | printf("-> proof generation status: %s\n\n", (zklaim_proof_generate(ctx_prover) ? "failed" : "passed")); | ||
194 | |||
195 | |||
196 | printf("[PROVER] blinding attributes\n"); | ||
197 | zklaim_clear_pres(ctx_prover); | ||
198 | |||
199 | |||
200 | printf("[PROVER] serializing context\n\n"); | ||
201 | |||
202 | unsigned char *ctx_prover_buf; | ||
203 | len = zklaim_ctx_serialize(ctx_prover, &ctx_prover_buf); | ||
204 | |||
205 | /* | ||
206 | * - 5 - | ||
207 | * verify proof | ||
208 | */ | ||
209 | printf("===========================================================================\n"); | ||
210 | printf("===========================================================================\n"); | ||
211 | printf("============================= VERIFIER ====================================\n"); | ||
212 | printf("===========================================================================\n"); | ||
213 | printf("===========================================================================\n"); | ||
214 | |||
215 | zklaim_ctx* ctx_verifier = zklaim_context_new(); | ||
216 | printf("\n[VERIFIER] deserializing context\n"); | ||
217 | printf("-> deserialisation status: %s\n\n", zklaim_ctx_deserialize(ctx_verifier, ctx_prover_buf, len) ? "failed" : "passed"); | ||
218 | printf("[VERIFIER] verifying proof and context\n"); | ||
219 | res = zklaim_ctx_verify(ctx_verifier); | ||
220 | printf("verification result: %d %s\n\n", res, (res ? "failed" : "passed")); | ||
221 | |||
222 | zklaim_print(ctx_verifier); | ||
223 | |||
224 | free(ctx_prover_buf); | ||
225 | free(ctx_issuer); | ||
226 | zklaim_ctx_free(ctx); | ||
227 | gcry_sexp_release(priv); | ||
228 | gcry_sexp_release(pub); | ||
229 | zklaim_ctx_free(ctx_prover); | ||
230 | zklaim_ctx_free(ctx_verifier); | ||
231 | |||
232 | return ZKLAIM_OK; | ||
233 | } | ||
234 | |||
235 | int main() { | ||
236 | return worker(); | ||
237 | } | ||