aboutsummaryrefslogtreecommitdiff
path: root/src/zklaim/zklaim.c
diff options
context:
space:
mode:
authorSchanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de>2018-08-16 17:04:43 +0200
committerSchanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de>2018-08-16 17:08:02 +0200
commit5ef8d3c6cbe7970bc9c9e54b4ab23c8727200b15 (patch)
tree920767b6ba29e3c9a4cffab54feb26c8cb633a72 /src/zklaim/zklaim.c
parent91e525e96c354a0b0382329e5cc05dd073f621e7 (diff)
downloadgnunet-5ef8d3c6cbe7970bc9c9e54b4ab23c8727200b15.tar.gz
gnunet-5ef8d3c6cbe7970bc9c9e54b4ab23c8727200b15.zip
simplify
Diffstat (limited to 'src/zklaim/zklaim.c')
-rw-r--r--src/zklaim/zklaim.c309
1 files changed, 243 insertions, 66 deletions
diff --git a/src/zklaim/zklaim.c b/src/zklaim/zklaim.c
index a53c193d1..82eb38d2d 100644
--- a/src/zklaim/zklaim.c
+++ b/src/zklaim/zklaim.c
@@ -1,79 +1,256 @@
1/* 1/*
2 This file is part of GNUnet. Copyright (C) 2001-2018 Christian Grothoff 2 * This file is part of zklaim.
3 (and other contributing authors) 3 * zklaim is free software: you can redistribute it and/or modify
4 4 * it under the terms of the GNU General Public License as published by
5 GNUnet is free software: you can redistribute it and/or modify it 5 * the Free Software Foundation, either version 3 of the License, or
6 under the terms of the GNU Affero General Public License as published 6 * (at your option) any later version.
7 by the Free Software Foundation, either version 3 of the License, 7 *
8 or (at your option) any later version. 8 * zklaim is distributed in the hope that it will be useful,
9 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 GNUnet is distributed in the hope that it will be useful, but 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 * GNU General Public License for more details.
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 *
13 Affero General Public License for more details. 13 * You should have received a copy of the GNU General Public License
14 14 * along with zklaim. If not, see https://www.gnu.org/licenses/.
15 You should have received a copy of the GNU Affero General Public License 15 */
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 16
18*/
19 17
20/** 18/**
21 * @file abe/abe.c 19 * Test File for zklaim c implementation
22 * @brief functions for Attribute-Based Encryption
23 * @author Martin Schanzenbach
24 */ 20 */
25 21
26
27#include "platform.h"
28#include <zklaim/zklaim.h> 22#include <zklaim/zklaim.h>
29#include "gnunet_crypto_lib.h" 23#include <zklaim/zklaim_ecc.h>
30 24#include <zklaim/zklaim_hash.h>
31struct GNUNET_ZKLAIM_Context 25#include <stdlib.h>
32{ 26#include <stdio.h>
33 zklaim_ctx* ctx; 27#include <string.h>
34 gcry_sexp_t priv; 28#include <gcrypt.h>
35 gcry_sexp_t pub; 29
36}; 30// to boost up valgrind
37 31int worker() {
38struct GNUNET_ZKLAIM_Payload 32 gcry_sexp_t priv, pub;
39{ 33 zklaim_gen_pk(&priv);
40 zklaim_payload pl; 34 zklaim_get_pub(priv, &pub);
41}; 35 unsigned char *pubbuf;
42 36 size_t publen;
43struct GNUNET_ZKLAIM_Context* 37 zklaim_pub2buf(pub, &pubbuf, &publen);
44GNUNET_ZKLAIM_new () 38
45{ 39 printf("===========================================================================\n");
46 struct GNUNET_ZKLAIM_Context *ctx; 40 printf("===========================================================================\n");
47 unsigned char *pubbuf; 41 printf("============================== ISSUER =====================================\n");
48 size_t publen; 42 printf("===========================================================================\n");
49 43 printf("===========================================================================\n");
50 ctx = GNUNET_new (struct GNUNET_ZKLAIM_Context); 44 //zklaim_ctx* ctx = zklaim_context_init();
51 ctx->ctx = zklaim_context_new(); 45 //zklaim_proving_key* pk = zklaim_proving_key_get(ctx);
52 zklaim_pub2buf(ctx->pub, &pubbuf, &publen); 46 /*
53 zklaim_gen_pk(&ctx->priv); 47 * - 1 -
54 zklaim_get_pub(ctx->priv, &ctx->pub); 48 * initialize data structures
55 if (sizeof(ctx->ctx->pub_key) != publen) { 49 */
50 printf("[ISSUER] initializing context\n");
51 zklaim_ctx *ctx = zklaim_context_new();
52
53 if (sizeof(ctx->pub_key) != publen) {
56 printf("size mismatch!"); 54 printf("size mismatch!");
57 return NULL; 55 return 1;
58 } 56 }
59 57
60 memcpy(ctx->ctx->pub_key, pubbuf, sizeof(ctx->ctx->pub_key)); 58 // TODO: there should be a zklaim method for this
61 free(pubbuf); 59 memcpy(ctx->pub_key, pubbuf, sizeof(ctx->pub_key));
62 return ctx; 60 free(pubbuf);
63} 61
62 //print_sexp(pub);
63
64 /*
65 * - 2 -
66 * setup the first payload
67 */
68 printf("[ISSUER] Setting up payloads..\n");
69 zklaim_payload pl, pl2;
70 memset(&pl, 0, sizeof(zklaim_payload));
71 pl.data0_ref = 18;
72 pl.data0_op = (enum zklaim_op) (zklaim_greater | zklaim_eq);
73 pl.data1_ref = 1;
74 pl.data1_op = zklaim_eq;
75 pl.data2_ref = 2;
76 pl.data2_op = zklaim_eq;
77 pl.data3_ref = 3;
78 pl.data3_op = zklaim_eq;
79 pl.data4_ref = 600;
80 pl.data4_op = zklaim_less;
81 pl.priv = 0;
82
83 memset(&pl2, 0, sizeof(zklaim_payload));
84 pl2.data0_ref = 0;
85 pl2.data0_op = zklaim_noop;
86 pl2.data1_ref = 0;
87 pl2.data1_op = zklaim_noop;
88 pl2.data2_ref = 0;
89 pl2.data2_op = zklaim_noop;
90 pl2.data3_ref = 0;
91 pl2.data3_op = zklaim_noop;
92 pl2.data4_ref = 9223372036854775807;
93 pl2.data4_op = zklaim_less_or_eq;
94 pl2.priv = 0;
95
96 // fill in the values
97 zklaim_set_attr(&pl, 23, 0);
98 zklaim_set_attr(&pl, 1, 1);
99 zklaim_set_attr(&pl, 2, 2);
100 zklaim_set_attr(&pl, 3, 3);
101 zklaim_set_attr(&pl, 599, 4);
102
103 zklaim_set_attr(&pl2, 0, 0);
104 zklaim_set_attr(&pl2, 0, 1);
105 zklaim_set_attr(&pl2, 0, 2);
106 zklaim_set_attr(&pl2, 0, 3);
107 zklaim_set_attr(&pl2, 9223372036854775807, 4);
108
109 /*
110 * - 3 -
111 * add payload to context
112 */
113 printf("[ISSUER] adding payloads to context\n");
114 zklaim_add_pl(ctx, pl);
115 /* zklaim_add_pl(ctx, pl); */
116 //zklaim_add_pl(ctx, pl2);
117 zklaim_hash_ctx(ctx);
118
119 printf("[ISSUER] performing trusted setup in order to generate keys\n");
120 printf("-> trusted setup result: %s\n", (zklaim_trusted_setup(ctx) ? "failed" : "passed"));
121
122 //unsigned char* buf;
123 //size_t vksize = zklaim_verification_key_serialize(ctx, &buf);
124 //printf("serialized vk size: %zuB\n", vksize);
125
126 // write key to disk
127 //FILE* f = fopen("/tmp/vk1", "w");
128 //fwrite(buf, vksize, 1, f);
129 //fclose(f);
130
131 //zklaim_verification_key_deserialize(ctx2, buf, vksize);
132
133 //free(buf);
134
135 //size_t pksize = zklaim_proving_key_serialize(ctx, &buf);
136 //printf("serialized pk size: %.2fMB\n", ((float) pksize)/1000/1000);
137
138 //zklaim_proving_key_deserialize(ctx, buf, pksize);
139
140 //zklaim_print(ctx);
141
142 //zklaim_ctx_sign(ctx, priv);
143
144
145 printf("-> signing context: %s\n", zklaim_ctx_sign(ctx, priv) ? "failed" : "passed");
146
147 unsigned char* ctx_issuer;
148
149 printf("\n[ISSUER] serializing context\n");
150 size_t len = zklaim_ctx_serialize(ctx, &ctx_issuer);
151
152 /*
153 * - 4 -
154 * generate proof
155 */
156
157 //printf("\n-> signing context: %s\n", zklaim_ctx_sign(ctx, priv) ? "failed" : "passed");
158
159 //unsigned char* proof;
160 //size_t proof_size = zklaim_proof_serialize(ctx, &proof);
161 //printf("serialized proof size: %zuB\n", proof_size);
162
163 //zklaim_proof* zkproof_imported;
164
165 //zklaim_proof_deserialize(ctx, proof, proof_size);
166
167 printf("===========================================================================\n");
168 printf("===========================================================================\n");
169 printf("============================== PROVER =====================================\n");
170 printf("===========================================================================\n");
171 printf("===========================================================================\n");
172
173
174 zklaim_ctx* ctx_prover = zklaim_context_new();
175 printf("\n[PROVER] deserializing context\n");
176 printf("-> deserialisation status: %s\n\n", zklaim_ctx_deserialize(ctx_prover, ctx_issuer, len) ? "failed" : "passed");
177
178 zklaim_print(ctx_prover);
179
180 ctx_prover->pk = ctx->pk;
181
182 int res = zklaim_ctx_verify(ctx_prover);
183 printf("[PROVER] verification result: %d %s\n", res, (res ? "failed" : "passed"));
184 printf(" ^--- failure with code 3 (ZKLAIM_INVALID_PROOF) is ok for the prover, as signature passes, but (non-existing) proof fails\n\n");
185
186 printf("[PROVER] resetting operations and reference values to create taylored proof\n");
187 printf("[PROVER] generating proof\n");
188
189 ctx_prover->pk = (unsigned char*) calloc(1, ctx->pk_size);
190 ctx_prover->pk_size = ctx->pk_size;
191 memcpy(ctx_prover->pk, ctx->pk, ctx_prover->pk_size);
192
193 // set custom prover reference values here:
194 ctx_prover->pl_ctx_head->pl.data0_ref = 20;
195 /* ctx_prover->pl_ctx_head->next->pl.data0_ref = 30; */
196 /* ctx_prover->pl_ctx_head->next->pl.data0_op = zklaim_less_or_eq; */
197 //ctx_prover->pl_ctx_head->pl.data0_op = zklaim_less;
198 ctx_prover->pl_ctx_head->pl.data4_ref = 0;
199 ctx_prover->pl_ctx_head->pl.data4_op = zklaim_noop;
200
201 ctx_prover->pl_ctx_head->pl.data1_ref = 0;
202 ctx_prover->pl_ctx_head->pl.data1_op = zklaim_noop;
203
204 ctx_prover->pl_ctx_head->pl.data2_ref = 0;
205 ctx_prover->pl_ctx_head->pl.data2_op = zklaim_noop;
206
207 ctx_prover->pl_ctx_head->pl.data3_ref = 0;
208 ctx_prover->pl_ctx_head->pl.data3_op = zklaim_noop;
209
210 zklaim_print(ctx_prover);
211
212 printf("-> proof generation status: %s\n\n", (zklaim_proof_generate(ctx_prover) ? "failed" : "passed"));
213
214
215 printf("[PROVER] blinding attributes\n");
216 zklaim_clear_pres(ctx_prover);
217
218
219 printf("[PROVER] serializing context\n\n");
220
221 unsigned char *ctx_prover_buf;
222 len = zklaim_ctx_serialize(ctx_prover, &ctx_prover_buf);
223
224 /*
225 * - 5 -
226 * verify proof
227 */
228 printf("===========================================================================\n");
229 printf("===========================================================================\n");
230 printf("============================= VERIFIER ====================================\n");
231 printf("===========================================================================\n");
232 printf("===========================================================================\n");
233
234 zklaim_ctx* ctx_verifier = zklaim_context_new();
235 printf("\n[VERIFIER] deserializing context\n");
236 printf("-> deserialisation status: %s\n\n", zklaim_ctx_deserialize(ctx_verifier, ctx_prover_buf, len) ? "failed" : "passed");
237 printf("[VERIFIER] verifying proof and context\n");
238 res = zklaim_ctx_verify(ctx_verifier);
239 printf("verification result: %d %s\n\n", res, (res ? "failed" : "passed"));
240
241 zklaim_print(ctx_verifier);
242
243 free(ctx_prover_buf);
244 free(ctx_issuer);
245 zklaim_ctx_free(ctx);
246 gcry_sexp_release(priv);
247 gcry_sexp_release(pub);
248 zklaim_ctx_free(ctx_prover);
249 zklaim_ctx_free(ctx_verifier);
64 250
65int 251 return ZKLAIM_OK;
66GNUNET_ZKLAIM_add_payload (struct GNUNET_ZKLAIM_Context *ctx,
67 struct GNUNET_ZKLAIM_Payload *pl)
68{
69 zklaim_add_pl (ctx->ctx, pl->pl);
70 return GNUNET_OK;
71} 252}
72 253
73int 254int main() {
74GNUNET_ZKLAIM_finalize (struct GNUNET_ZKLAIM_Context *ctx) 255 return worker();
75{
76 zklaim_hash_ctx (ctx->ctx);
77 zklaim_ctx_sign (ctx->ctx, ctx->priv);
78 return 1;
79} 256}