From 9e3072d8c80e1ccf2efe851acd046782cc574dbd Mon Sep 17 00:00:00 2001 From: "Schanzenbach, Martin" Date: Fri, 17 Aug 2018 19:09:44 +0200 Subject: proving key still missing --- src/include/gnunet_zklaim_service.h | 13 ++++ src/zklaim/gnunet-zklaim.c | 131 ++++++++++++++++++++++++++++++++++-- src/zklaim/zklaim_api.c | 56 +++++++++++++++ src/zklaim/zklaim_functions.c | 37 +++++++--- src/zklaim/zklaim_functions.h | 6 ++ 5 files changed, 230 insertions(+), 13 deletions(-) diff --git a/src/include/gnunet_zklaim_service.h b/src/include/gnunet_zklaim_service.h index f4337966b..011f2f9bb 100644 --- a/src/include/gnunet_zklaim_service.h +++ b/src/include/gnunet_zklaim_service.h @@ -183,6 +183,19 @@ GNUNET_ZKLAIM_disconnect (struct GNUNET_ZKLAIM_Handle *h); void GNUNET_ZKLAIM_cancel (struct GNUNET_ZKLAIM_Operation *op); +size_t +GNUNET_ZKLAIM_context_serialize (const struct GNUNET_ZKLAIM_Context *ctx, + char **buf); + +struct GNUNET_ZKLAIM_Context * +GNUNET_ZKLAIM_context_deserialize (char *data, + size_t data_len); + +int +GNUNET_ZKLAIM_context_prove (struct GNUNET_ZKLAIM_Context *ctx, + GNUNET_ZKLAIM_PredicateIterator iter, + void* iter_cls); + #if 0 /* keep Emacsens' auto-indent happy */ { #endif diff --git a/src/zklaim/gnunet-zklaim.c b/src/zklaim/gnunet-zklaim.c index a97bca5d4..fb6695574 100644 --- a/src/zklaim/gnunet-zklaim.c +++ b/src/zklaim/gnunet-zklaim.c @@ -54,6 +54,16 @@ static char* context_name; */ static char* issue_attrs; +/** + * Attribute names for issuer context data + */ +static char* credential; + +/** + * Attribute names for issuer context data + */ +static char* prove_predicate; + /** * Attribute names for issuer context data */ @@ -168,6 +178,10 @@ context_cb (void *cls, const struct GNUNET_ZKLAIM_Context *ctx) { int ret; + size_t len; + char* data; + char *str; + if (NULL == ctx) { fprintf (stderr, @@ -179,8 +193,22 @@ context_cb (void *cls, (struct GNUNET_CRYPTO_EcdsaPrivateKey*)pkey, &issue_iter, NULL); - fprintf (stdout, - "Issued (%d)\n", ret); + if (0 != ret) + { + fprintf (stderr, + "Failed (%d)\n", ret); + } + else + { + len = GNUNET_ZKLAIM_context_serialize (ctx, + &data); + GNUNET_STRINGS_base64_encode (data, + len, + &str); + fprintf (stdout, + "%s\n", str); + GNUNET_free (str); + } } if (NULL == cleanup_task) cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL); @@ -188,9 +216,75 @@ context_cb (void *cls, } +enum zklaim_op +op_str_to_enum (const char* op_str) +{ + + if (0 == strcmp ("<", op_str)) + return zklaim_less; + else if (0 == strcmp ("<=", op_str)) + return zklaim_less_or_eq; + else if (0 == strcmp ("==", op_str)) + return zklaim_eq; + else if (0 == strcmp ("<=", op_str)) + return zklaim_greater_or_eq; + else if (0 == strcmp ("<", op_str)) + return zklaim_greater; + else if (0 == strcmp ("!=", op_str)) + return zklaim_not_eq; + return zklaim_noop; +} + +void +prove_iter (void *cls, + const char* name, + enum zklaim_op *zop, + uint64_t *ref) +{ + char *tmp; + char *attr; + char *val; + char *op; + tmp = GNUNET_strdup (prove_predicate); + fprintf (stderr, + "%s\n", + prove_predicate); + attr = strtok (tmp, " "); + while (NULL != attr) + { + fprintf (stderr, + "Got %s\n", attr); + op = strtok (NULL, " "); + if (NULL == op) + break; + val = strtok (NULL, ";"); + if (NULL == val) + break; + if (0 != strcmp (name, attr)) + { + attr = strtok (NULL, " "); + continue; + } + *zop = op_str_to_enum (op); + if (1 != sscanf (val, "%lu", ref)) + fprintf (stderr, + "Failed parse %s %s %s\n", + attr, op, val); + fprintf (stdout, "Setting %s %s %lu\n", name, op, *ref); + attr = strtok (NULL, " "); + } + GNUNET_free (tmp); + +} + static void handle_arguments () { + struct GNUNET_ZKLAIM_Context *ctx; + size_t len; + char *data; + int ret; + timeout = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &timeout_task, NULL); @@ -206,7 +300,7 @@ handle_arguments () NULL); return; } - if (issue_attrs) + else if (issue_attrs) { zklaim_op = GNUNET_ZKLAIM_lookup_context (zklaim_handle, context_name, @@ -215,6 +309,24 @@ handle_arguments () NULL); return; } + else if (prove_predicate) + { + len = GNUNET_STRINGS_base64_decode (credential, + strlen (credential), + (void**)&data); + + ctx = GNUNET_ZKLAIM_context_deserialize (data, + len); + fprintf (stderr, + "%s\n", + prove_predicate); + + ret = GNUNET_ZKLAIM_context_prove (ctx, + &prove_iter, + NULL); + fprintf (stdout, + "Prove result: %d\n", ret); + } cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL); } @@ -288,7 +400,6 @@ main(int argc, char *const argv[]) NULL, gettext_noop ("Context name"), &context_name), - GNUNET_GETOPT_option_string ('A', "attributes", NULL, @@ -305,9 +416,19 @@ main(int argc, char *const argv[]) &create), GNUNET_GETOPT_option_string ('I', "issue", - gettext_noop ("Issue a credential with the given attributes and given zklaim context"), NULL, + gettext_noop ("Issue a credential with the given attributes and given zklaim context"), &issue_attrs), + GNUNET_GETOPT_option_string ('P', + "predicate", + NULL, + gettext_noop ("Predicate to prove"), + &prove_predicate), + GNUNET_GETOPT_option_string ('R', + "credential", + NULL, + gettext_noop ("A credential"), + &credential), GNUNET_GETOPT_OPTION_END }; if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "ct", diff --git a/src/zklaim/zklaim_api.c b/src/zklaim/zklaim_api.c index 6e9da67f3..6ca94fda5 100644 --- a/src/zklaim/zklaim_api.c +++ b/src/zklaim/zklaim_api.c @@ -536,4 +536,60 @@ GNUNET_ZKLAIM_issue_from_context (struct GNUNET_ZKLAIM_Context *ctx, iter_cls); } +size_t +GNUNET_ZKLAIM_context_serialize (const struct GNUNET_ZKLAIM_Context *ctx, + char **buf) +{ + char *pos; + char *tmp; + size_t len; + size_t len_w; + size_t ret_len = 0; + len = zklaim_ctx_serialize (ctx->ctx, + (unsigned char**) &tmp); + ret_len += strlen (ctx->attrs) + 1 + sizeof (size_t) + len; + *buf = GNUNET_malloc (ret_len); + pos = *buf; + memcpy (pos, ctx->attrs, strlen (ctx->attrs) + 1); + pos += strlen (ctx->attrs) + 1; + len_w = htonl (len); + memcpy (pos, &len_w, sizeof (size_t)); + pos += sizeof (size_t); + memcpy (pos, tmp, len); + GNUNET_free (tmp); + return ret_len; +} + + +struct GNUNET_ZKLAIM_Context * +GNUNET_ZKLAIM_context_deserialize (char *data, + size_t data_len) +{ + struct GNUNET_ZKLAIM_Context *ctx; + char *pos; + size_t len; + + ctx = GNUNET_new (struct GNUNET_ZKLAIM_Context); + ctx->attrs = GNUNET_strdup (data); + pos = data + strlen (ctx->attrs) + 1; + len = ntohl (*((size_t*)pos)); + ctx->ctx = zklaim_context_new (); + pos += sizeof (size_t); + if (0 != zklaim_ctx_deserialize (ctx->ctx, + (unsigned char*) pos, + len)) + return NULL; + return ctx; +} + +int +GNUNET_ZKLAIM_context_prove (struct GNUNET_ZKLAIM_Context *ctx, + GNUNET_ZKLAIM_PredicateIterator iter, + void* iter_cls) +{ + return ZKLAIM_context_prove (ctx, + iter, + iter_cls); +} + /* end of zklaim_api.c */ diff --git a/src/zklaim/zklaim_functions.c b/src/zklaim/zklaim_functions.c index 7f83ecb22..e9e911623 100644 --- a/src/zklaim/zklaim_functions.c +++ b/src/zklaim/zklaim_functions.c @@ -101,7 +101,7 @@ ZKLAIM_context_issue (struct GNUNET_ZKLAIM_Context *ctx, key); } -void +int ZKLAIM_context_prove (struct GNUNET_ZKLAIM_Context *ctx, GNUNET_ZKLAIM_PredicateIterator iter, void *iter_cls) @@ -115,23 +115,44 @@ ZKLAIM_context_prove (struct GNUNET_ZKLAIM_Context *ctx, tmp = GNUNET_strdup (ctx->attrs); attr_name = strtok (tmp, ","); plw = ctx->ctx->pl_ctx_head; - + fprintf (stderr, + "Num payloads: %lu, attrs: %s\n", + ctx->ctx->num_of_payloads, + ctx->attrs); for (i = 0; i < ctx->ctx->num_of_payloads; i++) { for (j = 0; j < ZKLAIM_MAX_PAYLOAD_ATTRIBUTES; j++) { - GNUNET_assert (NULL != attr_name); + plw->pl.data_op[j] = zklaim_noop; + } + plw = plw->next; + } + plw = ctx->ctx->pl_ctx_head; + for (i = 0; i < ctx->ctx->num_of_payloads; i++) + { + fprintf (stderr, + "Payload #%d\n", + i); + + for (j = 0; j < ZKLAIM_MAX_PAYLOAD_ATTRIBUTES; j++) + { + + if (NULL == attr_name) + break; iter (iter_cls, - attr_name, - &plw->pl.data_op[i], - &plw->pl.data_ref[i]); - attr_name = strtok (NULL, ","); + attr_name, + &plw->pl.data_op[j], + &plw->pl.data_ref[j]); + attr_name = strtok (attr_name + strlen (attr_name) + 1, ","); } + if (NULL == attr_name) + break; plw = plw->next; GNUNET_assert (NULL != plw); } GNUNET_free (tmp); - + zklaim_print (ctx->ctx); + return zklaim_proof_generate (ctx->ctx); } int diff --git a/src/zklaim/zklaim_functions.h b/src/zklaim/zklaim_functions.h index 8542023db..6fda611fa 100644 --- a/src/zklaim/zklaim_functions.h +++ b/src/zklaim/zklaim_functions.h @@ -68,4 +68,10 @@ ZKLAIM_context_issue (struct GNUNET_ZKLAIM_Context *ctx, const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, GNUNET_ZKLAIM_PayloadIterator iter, void *iter_cls); + +int +ZKLAIM_context_prove (struct GNUNET_ZKLAIM_Context *ctx, + GNUNET_ZKLAIM_PredicateIterator iter, + void *iter_cls); + #endif -- cgit v1.2.3