diff --git a/Makefile b/Makefile index 733e33a..3655131 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ else ifeq ($(ARCH), arm) $(CARM) $(COMMON) $(LIBCROSS) -D $(CURVE) $(MULTI_SET) else ifeq ($(shell uname), Darwin) - $(CC) $(COMMON) $(LIBMAC)-D $(CURVE) $(MULTI_SET) + $(CC) $(COMMON) $(LIBMAC) -D $(CURVE) $(MULTI_SET) -D IS_MAC_OS else $(CC) $(COMMON) $(LIB) -D $(CURVE) $(MULTI_SET) diff --git a/src/bench.c b/src/bench.c index 948ff30..b18756a 100644 --- a/src/bench.c +++ b/src/bench.c @@ -24,7 +24,7 @@ int main(int argc, char *argv[]) bench = 1; if (argc < 3) { - printf("******************* ZPiE v0.4 *******************\n"); + printf("******************* ZPiE v0.5 *******************\n"); printf("USAGE: ./zpie [ACTIONS] [OPTIONS]\n\n"); printf("[ACTIONS]:\n"); printf("-s : Perform setup of 'c' constraints.\n"); @@ -38,7 +38,7 @@ int main(int argc, char *argv[]) if ((argc == 4) && (strcmp(argv[3], "-l") == 0)) logs = 1; - printf("******************* ZPiE v0.4 *******************\n"); + printf("******************* ZPiE v0.5 *******************\n"); if ((strcmp(argv[1], "-s") == 0) || (strcmp(argv[1], "-p") == 0) || (strcmp(argv[1], "-v") == 0)) { @@ -47,8 +47,8 @@ int main(int argc, char *argv[]) printf("--- Starting ZPiE - Groth'16...\n"); printf(" |--- # of constraints: %d\n", N); - printf(" |--- # of variables: %d\n", M); - printf(" |--- # of public outputs: %d\n", nPublic); + printf(" |--- # of elements: %d\n", M); + printf(" |--- # of public elements: %d\n", nPublic); } else { @@ -72,13 +72,13 @@ int main(int argc, char *argv[]) if (strcmp(argv[1], "-s") == 0) { setup_keys keys = perform_setup(&bench_circuit); - store_setup(keys); + store_setup(&keys); } else if (strcmp(argv[1], "-p") == 0) { setup_keys keys = read_setup(&bench_circuit); proof p = generate_proof(&bench_circuit, keys.pk); - store_proof(p); + store_proof(&p); } else if (strcmp(argv[1], "-v") == 0) { diff --git a/src/common/utils.c b/src/common/utils.c index 6119f09..3a2e97b 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -68,11 +68,9 @@ void init_prover(void *circuit, proving_key pk) BsFr = (mclBnFr*) malloc((n) * sizeof(mclBnFr)); CsFr = (mclBnFr*) malloc((n) * sizeof(mclBnFr)); - if (bench) printf(" |--- Mode: Prove\n"); - mpz_init(pPrime); mpz_set_str(pPrime, PRIMESTR, 10); - if (bench) printf(" |--- FFT constraints size : %d\n", n); + if (bench) printf(" |--- FFT domain size : %d\n", n); rsigma = (mpz_t*) malloc((n) * sizeof(mpz_t)); rsigmaInv = (mpz_t*) malloc((n) * sizeof(mpz_t)); diff --git a/src/gro16/prover.c b/src/gro16/prover.c index c77e31a..edde8a8 100644 --- a/src/gro16/prover.c +++ b/src/gro16/prover.c @@ -111,7 +111,11 @@ void mul_exp(struct mulExpResult *result, mpz_t *uwProof, proving_key pk) if(i >= (nPublic + nConst)) mpz_to_fr(&uwFactorPublic[i-(nPublic + nConst)], &uw[i]); } - int num_threads = get_nprocs(); + #ifdef IS_MAC_OS + int num_threads = 8; + #else + int num_threads = get_nprocs(); + #endif mclBnG1_mulVecMT(&result->uwA1, pk.A1, uwFactor, M, num_threads); mclBnG1_mulVecMT(&result->uwB1, pk.B1, uwFactor, M, num_threads); diff --git a/src/tests.c b/src/tests.c index 8342a1c..a280ee2 100644 --- a/src/tests.c +++ b/src/tests.c @@ -52,32 +52,6 @@ void test_mimc_hash() mimc7(&h, &x_in, &k); } -void test_setup(void) -{ - test_no_rand = 1; - setup_keys keys = perform_setup(&test_single_constraint); - - char* pk_bytes = serialize_pk(&keys.pk); - char* vk_bytes = serialize_vk(&keys.vk); - - BYTE hash_bytes[SHA256_BLOCK_SIZE]; - SHA256_CTX ctx; - - sha256_init(&ctx); - sha256_update(&ctx, pk_bytes, strlen(pk_bytes)); - sha256_final(&ctx, hash_bytes); - - CU_ASSERT(!strcmp(to_hex(hash_bytes, sizeof hash_bytes), "26047d607444ba18b641499f11483896560195b1f16b0a12c734ccf0f6552cf4")); - - sha256_init(&ctx); - sha256_update(&ctx, vk_bytes, strlen(vk_bytes)); - sha256_final(&ctx, hash_bytes); - - CU_ASSERT(!strcmp(to_hex(hash_bytes, sizeof hash_bytes), "1c91757242555e6705802233a5b7ca934fd33278c2461f21df343321c8ffb5d0")); - - test_no_rand = 0; -} - void test_prover(void) { test_no_rand = 1; @@ -156,12 +130,6 @@ int main() return CU_get_error(); } - if ((NULL == suite) || (NULL == CU_add_test(suite, "\n\nSetup Testing\n\n", test_setup))) - { - CU_cleanup_registry(); - return CU_get_error(); - } - CU_basic_run_tests(); if(CU_get_number_of_failures()) abort(); diff --git a/src/zpie.c b/src/zpie.c index 4eeeb1a..c894ac2 100644 --- a/src/zpie.c +++ b/src/zpie.c @@ -158,158 +158,109 @@ setup_keys perform_setup(void *circuit) return keys; } -char* serialize_pk(proving_key *pk) +void serialize_pk(proving_key *pk) { + FILE *fpk; + fpk = fopen("data/provingkey.params", "w"); + int n = mpz_get_ui(pk->Ne); - char *pk_bytes; - pk_bytes = (char *) malloc(1024 * n * sizeof(char)); + int buff_pk_size = SIZE_FR * n + SIZE_G2 * (2 + M) + SIZE_G1 * (M - (nPublic + nConst) + 3 + n + 2 * M); + char buff_pk[buff_pk_size]; - for (int i = 0; i < 1024 * n * sizeof(char); i++) - { - pk_bytes[i] = 0; - } + mpz_out_raw(fpk, pk->Ne); - char buff[2048]; - mpz_get_str(buff, 16, pk->Ne); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); + mpz_t factor; + mpz_init(factor); + mpz_set_si(factor, pk->qap_size); + mpz_out_raw(fpk, factor); - for (int i = 0; i < n; i++) + for (int i = 0; i < pk->qap_size; i++) { - mclBnFr_getStr(buff, sizeof(buff), &pk->wMFr[i], 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); + mpz_set_si(factor, pk->LRO[i]); + mpz_out_raw(fpk, factor); } - sprintf(buff, "%d", pk->qap_size); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); - - for (int i = 0; i < pk->qap_size; i++) + for (int i = 0; i < lro_const_total; i++) { - sprintf(buff, "%d", pk->LRO[i]); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); + mpz_out_raw(fpk, pk->LRO_constants[i]); } - for (int i = 0; i < lro_const_total; i++) + int size = 0; + + for (int i = 0; i < n; i++) { - mpz_get_str(buff, 16, pk->LRO_constants[i]); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); + size += mclBnFr_serialize(buff_pk + size, SIZE_FR, &pk->wMFr[i]); } - mclBnG1_getStr(buff, sizeof(buff), &pk->alpha1, 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); - mclBnG1_getStr(buff, sizeof(buff), &pk->beta1, 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); - mclBnG2_getStr(buff, sizeof(buff), &pk->beta2, 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); - mclBnG1_getStr(buff, sizeof(buff), &pk->delta1, 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); - mclBnG2_getStr(buff, sizeof(buff), &pk->delta2, 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); + size += mclBnG1_serialize(buff_pk + size, SIZE_G1, &pk->alpha1); + size += mclBnG1_serialize(buff_pk + size, SIZE_G1, &pk->beta1); + size += mclBnG2_serialize(buff_pk + size, SIZE_G2, &pk->beta2); + size += mclBnG1_serialize(buff_pk + size, SIZE_G1, &pk->delta1); + size += mclBnG2_serialize(buff_pk + size, SIZE_G2, &pk->delta2); for (int i = 0; i < M; i++) { - mclBnG1_getStr(buff, sizeof(buff), &pk->A1[i], 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); - mclBnG1_getStr(buff, sizeof(buff), &pk->B1[i], 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); - mclBnG2_getStr(buff, sizeof(buff), &pk->B2[i], 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); + size += mclBnG1_serialize(buff_pk + size, SIZE_G1, &pk->A1[i]); + size += mclBnG1_serialize(buff_pk + size, SIZE_G1, &pk->B1[i]); + size += mclBnG2_serialize(buff_pk + size, SIZE_G2, &pk->B2[i]); } for (int i = 0; i < M-(nPublic + nConst); i++) { - mclBnG1_getStr(buff, sizeof(buff), &pk->pk1[i], 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); + size += mclBnG1_serialize(buff_pk + size, SIZE_G1, &pk->pk1[i]); } for (int i = 0; i < n; i++) { - mclBnG1_getStr(buff, sizeof(buff), &pk->xt1[i], 16); - strcat(pk_bytes, buff); - strcat(pk_bytes, "\n"); + size += mclBnG1_serialize(buff_pk + size, SIZE_G1, &pk->xt1[i]); } - return pk_bytes; + fwrite(buff_pk, 1, size, fpk); + fclose(fpk); } -char* serialize_vk(verifying_key *vk) +void serialize_vk(verifying_key *vk) { - char *vk_bytes; - vk_bytes = (char *) malloc(1024 * (nPublic + nConst) * sizeof(char)); + FILE *fvk; + fvk = fopen("data/verifyingkey.params", "w"); - for (int i = 0; i < 1024 * (nPublic + nConst) * sizeof(char); i++) - { - vk_bytes[i] = 0; - } + int buff_vk_size = SIZE_GT + SIZE_G2 * 2 + SIZE_G1 * (nPublic + nConst); + char buff_vk[buff_vk_size]; - char buff[2048]; for (int i = 0; i < nConst; i++) { - mpz_get_str(buff, 10, vk->constants[i]); - strcat(vk_bytes, buff); - strcat(vk_bytes, "\n"); + mpz_out_raw(fvk, vk->constants[i]); } - mclBnGT_getStr(buff, sizeof(buff), &vk->alphabetaT, 10); - strcat(vk_bytes, buff); - strcat(vk_bytes, "\n"); - - mclBnG2_getStr(buff, sizeof(buff), &vk->gamma2, 10); - strcat(vk_bytes, buff); - strcat(vk_bytes, "\n"); + int size = 0; - mclBnG2_getStr(buff, sizeof(buff), &vk->delta2, 10); - strcat(vk_bytes, buff); - strcat(vk_bytes, "\n"); + size += mclBnGT_serialize(buff_vk, SIZE_GT, &vk->alphabetaT); + size += mclBnG2_serialize(buff_vk + size, SIZE_G2, &vk->gamma2); + size += mclBnG2_serialize(buff_vk + size, SIZE_G2, &vk->delta2); for (int i = 0; i < (nPublic + nConst); i++) { - mclBnG1_getStr(buff, sizeof(buff), &vk->vk1[i], 10); - strcat(vk_bytes, buff); - strcat(vk_bytes, "\n"); + size += mclBnG1_serialize(buff_vk + size, SIZE_G1, &vk->vk1[i]); } - return vk_bytes; + fwrite(buff_vk, 1, size, fvk); + fclose(fvk); } -void store_setup(setup_keys keys) +void store_setup(setup_keys *keys) { struct stat st = {0}; if (stat("data", &st) == -1) mkdir("data", 0700); - FILE *fpk, *fvk; - fpk = fopen("data/provingkey.params", "w"); - fvk = fopen("data/verifyingkey.params", "w"); - - char *pk_bytes = serialize_pk(&keys.pk); - char *vk_bytes = serialize_vk(&keys.vk); - - fprintf(fpk, "%s", pk_bytes); - fprintf(fvk, "%s", vk_bytes); - - fclose(fpk); - fclose(fvk); + serialize_pk(&keys->pk); + serialize_vk(&keys->vk); } setup_keys read_setup(void *circuit) { init_setup(circuit); - char buff[2048]; FILE *fpk, *fvk; fpk = fopen("data/provingkey.params", "r"); @@ -317,10 +268,13 @@ setup_keys read_setup(void *circuit) setup_keys keys; - fgets(buff, sizeof buff, fpk); - mpz_init_set_str(keys.pk.Ne, buff, 16); + mpz_init(keys.pk.Ne); + mpz_inp_raw(keys.pk.Ne, fpk); int n = mpz_get_ui(keys.pk.Ne); + + int buff_pk_size = SIZE_FR * n + SIZE_G2 * (2 + M) + SIZE_G1 * (M - (nPublic + nConst) + 3 + n + 2 * M); + char buff_pk[buff_pk_size]; keys.pk.wMFr = (mclBnFr*) malloc((n) * sizeof(mclBnFr)); keys.vk.vk1 = (mclBnG1*) malloc(((nPublic + nConst)) * sizeof(mclBnG1)); @@ -334,83 +288,75 @@ setup_keys read_setup(void *circuit) keys.pk.B2 = (mclBnG2*) malloc((M) * sizeof(mclBnG2)); keys.pk.LRO_constants = (mpz_t*) malloc((lro_const_total) * sizeof(mpz_t)); - for (int i = 0; i < n; i++) - { - fgets(buff, sizeof buff, fpk); - mclBnFr_setStr(&keys.pk.wMFr[i], buff, strlen(buff), 16); - } - - fgets(buff, sizeof buff, fpk); - keys.pk.qap_size = atoi(buff); + mpz_t factor; + mpz_init(factor); + mpz_inp_raw(factor, fpk); + keys.pk.qap_size = mpz_get_si(factor); keys.pk.LRO = (int*) malloc((keys.pk.qap_size) * sizeof(int)); for (int i = 0; i < keys.pk.qap_size; i++) { - fgets(buff, sizeof buff, fpk); - keys.pk.LRO[i] = atoi(buff); + mpz_inp_raw(factor, fpk); + keys.pk.LRO[i] = mpz_get_si(factor); } for (int i = 0; i < lro_const_total; i++) { - fgets(buff, sizeof buff, fpk); - mpz_init_set_str(keys.pk.LRO_constants[i], buff, 16); + mpz_init(keys.pk.LRO_constants[i]); + mpz_inp_raw(keys.pk.LRO_constants[i], fpk); } - fgets(buff, sizeof buff, fpk); - mclBnG1_setStr(&keys.pk.alpha1, buff, strlen(buff), 16); - fgets(buff,sizeof buff, fpk); - mclBnG1_setStr(&keys.pk.beta1, buff, strlen(buff), 16); - fgets(buff,sizeof buff, fpk); - mclBnG2_setStr(&keys.pk.beta2, buff, strlen(buff), 16); - fgets(buff,sizeof buff, fpk); - mclBnG1_setStr(&keys.pk.delta1, buff, strlen(buff), 16); - fgets(buff,sizeof buff, fpk); - mclBnG2_setStr(&keys.pk.delta2, buff, strlen(buff), 16); + int size = 0; + fread(buff_pk, 1, buff_pk_size, fpk); + + for (int i = 0; i < n; i++) + { + size += mclBnFr_deserialize(&keys.pk.wMFr[i], buff_pk + size, SIZE_FR); + } + + size += mclBnG1_deserialize(&keys.pk.alpha1, buff_pk + size, SIZE_G1); + size += mclBnG1_deserialize(&keys.pk.beta1, buff_pk + size, SIZE_G1); + size += mclBnG2_deserialize(&keys.pk.beta2, buff_pk + size, SIZE_G2); + size += mclBnG1_deserialize(&keys.pk.delta1, buff_pk + size, SIZE_G1); + size += mclBnG2_deserialize(&keys.pk.delta2, buff_pk + size, SIZE_G2); for (int i = 0; i < M; i++) { - fgets(buff,sizeof buff, fpk); - mclBnG1_setStr(&keys.pk.A1[i], buff, strlen(buff), 16); - fgets(buff,sizeof buff, fpk); - mclBnG1_setStr(&keys.pk.B1[i], buff, strlen(buff), 16); - fgets(buff,sizeof buff, fpk); - mclBnG2_setStr(&keys.pk.B2[i], buff, strlen(buff), 16); + size += mclBnG1_deserialize(&keys.pk.A1[i], buff_pk + size, SIZE_G1); + size += mclBnG1_deserialize(&keys.pk.B1[i], buff_pk + size, SIZE_G1); + size += mclBnG2_deserialize(&keys.pk.B2[i], buff_pk + size, SIZE_G2); } for (int i = 0; i < M-(nPublic + nConst); i++) { - fgets(buff,sizeof buff, fpk); - mclBnG1_setStr(&keys.pk.pk1[i], buff, strlen(buff), 16); + size += mclBnG1_deserialize(&keys.pk.pk1[i], buff_pk + size, SIZE_G1); } for (int i = 0; i < n; i++) { - fgets(buff,sizeof buff, fpk); - mclBnG1_setStr(&keys.pk.xt1[i], buff, strlen(buff), 16); + size += mclBnG1_deserialize(&keys.pk.xt1[i], buff_pk + size, SIZE_G1); } for (int i = 0; i < nConst; i++) { - fgets(buff,sizeof buff, fvk); - mpz_init_set_str(keys.vk.constants[i], buff, 10); + mpz_init(keys.vk.constants[i]); + mpz_inp_raw(keys.vk.constants[i], fvk); } - fgets(buff, sizeof buff, fvk); - mclBnGT_setStr(&keys.vk.alphabetaT, buff, strlen(buff), 10); - - fgets(buff, sizeof buff, fvk); - mclBnG2_setStr(&keys.vk.gamma2, buff, strlen(buff), 10); + char buff_vk[SIZE_GT + SIZE_G2 * 2 + SIZE_G1 * (nPublic + nConst)]; + size = 0; - fgets(buff, sizeof buff, fvk); - mclBnG2_setStr(&keys.vk.delta2, buff, strlen(buff), 10); + fread(buff_vk, 1, SIZE_GT + SIZE_G2 * 2 + SIZE_G1 * (nPublic + nConst), fvk); + size += mclBnGT_deserialize(&keys.vk.alphabetaT, buff_vk, SIZE_GT); + size += mclBnG2_deserialize(&keys.vk.gamma2, buff_vk + size, SIZE_G2); + size += mclBnG2_deserialize(&keys.vk.delta2, buff_vk + size, SIZE_G2); keys.vk.vk1 = (mclBnG1*) malloc(((nPublic + nConst)) * sizeof(mclBnG1)); for (int i = 0; i < (nPublic + nConst); i++) { - fgets(buff, sizeof buff, fvk); - mclBnG1_setStr(&keys.vk.vk1[i], buff, strlen(buff), 10); + size += mclBnG1_deserialize(&keys.vk.vk1[i], buff_vk + size, SIZE_G1); } fclose(fpk); @@ -477,7 +423,7 @@ proof generate_proof(void *circuit, proving_key pk) return p; } -void store_proof(proof p) +void store_proof(proof *p) { char buff[2048]; FILE *fproof; @@ -485,19 +431,16 @@ void store_proof(proof p) for (int i = 0; i < (nPublic); i++) { - mpz_out_str(fproof, 10, p.uwProof[i]); - fprintf(fproof, "\n"); + mpz_out_raw(fproof, p->uwProof[i]); } - mclBnG1_getStr(buff, sizeof(buff), &p.piA, 10); - fprintf(fproof, "%s\n", buff); + int size = 0; - mclBnG2_getStr(buff, sizeof(buff), &p.piB2, 10); - fprintf(fproof, "%s\n", buff); - - mclBnG1_getStr(buff, sizeof(buff), &p.piC, 10); - fprintf(fproof, "%s\n", buff); + size += mclBnG1_serialize(buff, SIZE_G1, &p->piA); + size += mclBnG2_serialize(buff + size, SIZE_G2, &p->piB2); + size += mclBnG1_serialize(buff + size, SIZE_G1, &p->piC); + fwrite(buff, 1, size, fproof); fclose(fproof); } @@ -513,19 +456,16 @@ proof read_proof() for (int i = 0; i < (nPublic); i++) { - fgets(buff, sizeof buff, fproof); mpz_init(p.uwProof[i]); - mpz_set_str(p.uwProof[i], buff, 10); + mpz_inp_raw(p.uwProof[i], fproof); } - fgets(buff, sizeof buff, fproof); - mclBnG1_setStr(&p.piA, buff, strlen(buff), 10); - - fgets(buff, sizeof buff, fproof); - mclBnG2_setStr(&p.piB2, buff, strlen(buff), 10); + int size = 0; - fgets(buff, sizeof buff, fproof); - mclBnG1_setStr(&p.piC, buff, strlen(buff), 10); + fread(buff, 1, SIZE_G1 + SIZE_G2 + SIZE_G1, fproof); + size += mclBnG1_deserialize(&p.piA, buff, SIZE_G1); + size += mclBnG2_deserialize(&p.piB2, buff + size, SIZE_G2); + size += mclBnG1_deserialize(&p.piC, buff + size, SIZE_G1); fclose(fproof); diff --git a/src/zpie.h b/src/zpie.h index 9735422..b3fd45a 100644 --- a/src/zpie.h +++ b/src/zpie.h @@ -3,6 +3,10 @@ #ifdef BN128 #define USEDCURVE MCL_BN_SNARK1 #define BITS 254 + #define SIZE_FR 32 + #define SIZE_G1 32 + #define SIZE_G2 64 + #define SIZE_GT 384 #define PRIMESTR "21888242871839275222246405745257275088548364400416034343698204186575808495617" #define GROUPGEN 5 #define GGEN "1 1 2" @@ -13,6 +17,10 @@ #elif BLS12_381 #define USEDCURVE MCL_BLS12_381 #define BITS 255 + #define SIZE_FR 32 + #define SIZE_G1 48 + #define SIZE_G2 96 + #define SIZE_GT 576 #define PRIMESTR "52435875175126190479447740508185965837690552500527637822603658699938581184513" #define GROUPGEN 7 #define GGEN "1 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569" @@ -32,6 +40,10 @@ #include "common/sha256.c" #include "CUnit/Basic.h" +#ifndef IS_MAC_OS + #include +#endif + int init_suite(void) { return 0; } int clean_suite(void) { return 0; }