Skip to content

Commit

Permalink
Merge pull request #4 - ZPiE v0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
xevisalle authored Nov 4, 2023
2 parents 55efe23 + 91c1657 commit 52a89b8
Show file tree
Hide file tree
Showing 15 changed files with 495 additions and 435 deletions.
13 changes: 6 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ LIBMAC = /opt/homebrew/lib/libgmp.a /opt/homebrew/opt/libomp/lib/libomp.a /opt/h
LIBCROSS = $(MCLPATH)/lib/libmclbn384_256.a $(MCLPATH)/lib/libmcl.a $(GMPPATH)/lib/libgmp.a -I $(MCLPATH)/include -I $(GMPPATH)/include -lstdc++
SRC = $(shell pwd)/src/*.c $(shell pwd)/circuits/*.c $(shell pwd)/src/*.h

MULEXP = MCL_MULEXP
CURVE = BN128
ARCH = None
MULTI = off
Expand All @@ -22,22 +21,22 @@ endif

zpie: $(SRC)
ifeq ($(ARCH), x86)
$(CC) -m32 $(COMMON) $(LIBCROSS) -D $(MULEXP) -D $(CURVE) $(MULTI_SET)
$(CC) -m32 $(COMMON) $(LIBCROSS) -D $(CURVE) $(MULTI_SET)

else ifeq ($(ARCH), x86_64)
$(CC) -m64 $(COMMON) $(LIBCROSS) -D $(MULEXP) -D $(CURVE) $(MULTI_SET)
$(CC) -m64 $(COMMON) $(LIBCROSS) -D $(CURVE) $(MULTI_SET)

else ifeq ($(ARCH), aarch64)
$(CAARCH64) $(COMMON) $(LIBCROSS) -D $(MULEXP) -D $(CURVE) $(MULTI_SET)
$(CAARCH64) $(COMMON) $(LIBCROSS) -D $(CURVE) $(MULTI_SET)

else ifeq ($(ARCH), arm)
$(CARM) $(COMMON) $(LIBCROSS) -D $(MULEXP) -D $(CURVE) $(MULTI_SET)
$(CARM) $(COMMON) $(LIBCROSS) -D $(CURVE) $(MULTI_SET)

else ifeq ($(shell uname), Darwin)
$(CC) $(COMMON) $(LIBMAC) -D $(MULEXP) -D $(CURVE) $(MULTI_SET)
$(CC) $(COMMON) $(LIBMAC)-D $(CURVE) $(MULTI_SET)

else
$(CC) $(COMMON) $(LIB) -D $(MULEXP) -D $(CURVE) $(MULTI_SET)
$(CC) $(COMMON) $(LIB) -D $(CURVE) $(MULTI_SET)

endif
test:
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ ZPiE needs [GMP](https://gmplib.org/) and [MCL](https://github.com/herumi/mcl).
sudo apt install libgmp-dev libcunit1-dev
git clone https://github.com/herumi/mcl
cd mcl
make -j4
make -j8
```

If willing to use the multi-thread execution, compile MCL using `make -j8 MCL_USE_OMP=1`.

## Test
ZPiE can be tested as follows:

Expand Down Expand Up @@ -57,7 +59,7 @@ BN128 (default)
BLS12_381
```

We can specify to run the code in multi-thread mode:
We can specify to run the code in multi-thread mode (if MCL was compiled accordingly):

```
make bench MULTI=on
Expand Down Expand Up @@ -94,7 +96,7 @@ int main()
// we perform the setup
setup_keys keys = perform_setup(&circuit);

// we generate a proof (../data/proof.params)
// we generate a proof
proof p = generate_proof(&circuit, keys.pk);

// we verify the proof
Expand Down
25 changes: 4 additions & 21 deletions circuits/mimc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,23 @@

void mimc7(element *h, element *x_in, element *k)
{
char buff[2048];
FILE *cnst;
cnst = fopen("circuits/constants.txt", "r");

element c[91];
init_array(c, 91);

for (int i = 0; i < 91; i++)
{
fgets(buff, sizeof buff, cnst);
input(&c[i], buff);
}

fclose(cnst);

element t[NROUNDS];
element r[NROUNDS];
element f[NROUNDS*3];

init_array(t, NROUNDS);
init_array(r, NROUNDS);
init_array(f, NROUNDS*3);

int it = 0;

for (int i = 0; i < NROUNDS; i++)
{
if (i == 0) addmul(&t[i], k, x_in, &one);
else add3mul(&t[i], k, &r[i-1], &c[i], &one);
if (i == 0) addmuladd(&f[it], k, x_in, k, x_in);
else add3muladd3(&f[it], k, &r[i-1], &c_mimc[i], k, &r[i-1], &c_mimc[i]);

mul(&f[it], &t[i], &t[i]);
mul(&f[it+1], &f[it], &f[it]);
mul(&f[it+2], &f[it+1], &f[it]);
mul(&r[i], &f[it+2], &t[i]);
if (i == 0) addmul(&r[i], k, x_in, &f[it+2]);
else add3mul(&r[i], k, &r[i-1], &c_mimc[i], &f[it+2]);

it = it + 3;
}
Expand Down
117 changes: 60 additions & 57 deletions circuits/utils.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
void add(element uOut, element vOut, element u1, element v1, element u2, element v2)
{
element a, d, dNeg;
init(&a);
init(&d);
init(&dNeg);
input(&a, "-168700");
input(&d, "168696");
input(&dNeg, "-168696");

element factor, factor1, factor2, factor3, factor4, factor5, factor6, factor7, factor8, factor9, factor10;
element factor, factor1, factor2, factor3, factor4, factor5, factor6, factor7;
init(&factor);
init(&factor1);
init(&factor2);
Expand All @@ -17,44 +9,55 @@ void add(element uOut, element vOut, element u1, element v1, element u2, element
init(&factor5);
init(&factor6);
init(&factor7);
init(&factor8);
init(&factor9);
init(&factor10);

// uOut = (u1*v2 + v1*u2) / (1 + d*u1*u2*v1*v2)
mul(&factor1, &u1, &v2);

mul(&factor2, &v1, &u2);

mul(&factor3, &factor1, &factor2);
mul(&factor, &factor3, &d);
int d = 168696;
int one_int = 1;

addmul(&factor4, &factor, &one, &one);
mul_constants(&factor, &one_int, &factor1, &d, &factor2);

mpz_t invFactor;
mpz_init(invFactor);
if(!setParams) mpz_invert(invFactor, uw[factor4.index], pPrime);

if(!setParams)
{
mpz_t f_check;
mpz_init(f_check);
mpz_add(f_check, uw[one.index], uw[factor.index]);
mpz_invert(invFactor, f_check, pPrime);
}

char buff[2048];
mpz_get_str(buff, 10, invFactor);
input(&factor5, buff);
addmul(&uOut, &factor1, &factor2, &factor5);
input(&factor4, buff);

addmul(&one, &factor, &one, &factor4); // verify x * 1/x = 1
addmul(&uOut, &factor1, &factor2, &factor4);

// vOut = (v1*v2 - a*u1*u2) / (1 - d*u1*u2*v1*v2)
mul(&factor6, &v1, &v2);
mul(&factor7, &u1, &u2);
mul(&factor8, &factor7, &a);
mul(&factor5, &v1, &v2);

int a = -168700;
int one_neg = -1;

element factorNeg;
init(&factorNeg);
mul_constants(&factor6, &a, &u1, &one_int, &u2);

mul(&factorNeg, &factor3, &dNeg);
addmul(&factor9, &one, &factorNeg, &one);
if(!setParams)
{
mpz_t f_check;
mpz_init(f_check);
mpz_sub(f_check, uw[one.index], uw[factor.index]);
mpz_invert(invFactor, f_check, pPrime);
}

if(!setParams) mpz_invert(invFactor, uw[factor9.index], pPrime);
mpz_get_str(buff, 10, invFactor);
input(&factor10, buff);
addmul(&vOut, &factor6, &factor8, &factor10);
input(&factor7, buff);

addmul_constants(&one, &one_int, &one, &one_neg, &factor, &one_int, &factor7); // verify x * 1/x = 1
addmul(&vOut, &factor5, &factor6, &factor7);
}

void mul_scalar(element mulOut1, element mulOut2, element A1, element A2, element *bits, int size)
Expand Down Expand Up @@ -99,27 +102,32 @@ void mul_scalar(element mulOut1, element mulOut2, element A1, element A2, elemen
add(doubledP1[i], doubledP2[i], doubledP1[i-1], doubledP2[i-1], doubledP1[i-1], doubledP2[i-1]);
}

element f1, f2, f3, f4, f5;
element f1, f2, f4, f5;
init(&f1);
init(&f2);
init(&f3);
init(&f4);
init(&f5);

mul(&f1, &accumulatedP1[i+1], &bits[i]);
mul(&f2, &accumulatedP2[i+1], &bits[i]);

addmul(&f3, &oneNeg, &bits[i], &oneNeg);
int one_alone = 1;
int one_neg = -1;

mul(&f4, &step1[i], &f3);
mul(&f5, &step2[i], &f3);
mul_constants(&f4, &one_neg, &bits[i], &one_alone, &step1[i]);
mul_constants(&f5, &one_neg, &bits[i], &one_alone, &step2[i]);

addmul(&step1[i+1], &f1, &f4, &one);
addmul(&step2[i+1], &f2, &f5, &one);
if(i+1 != size)
{
add3mul(&step1[i+1], &f1, &f4, &step1[i], &one);
add3mul(&step2[i+1], &f2, &f5, &step2[i], &one);
}
else
{
add3mul(&mulOut1, &f1, &f4, &step1[i], &one);
add3mul(&mulOut2, &f2, &f5, &step2[i], &one);
}
}

mul(&mulOut1, &step1[size], &one);
mul(&mulOut2, &step2[size], &one);
}

void to_bits(element *bits, element val, int size)
Expand All @@ -130,11 +138,10 @@ void to_bits(element *bits, element val, int size)
mpz_init(t3);
mpz_init(total);

element oneNeg;
init(&oneNeg);
input(&oneNeg, "-1");
mpz_set_str(t2, "1", 10);

element b[size];

for (int i = 0; i < size; i++)
{
if(!setParams)
Expand All @@ -143,28 +150,24 @@ void to_bits(element *bits, element val, int size)
mpz_and(t3, t1, t2);
}

element b;
init(&b);
char buff[2048];
mpz_get_str(buff, 10, t3);
input(&bits[i], buff);

addmul(&b, &bits[i], &oneNeg, &bits[i]);
mpz_t pow;
mpz_init(pow);
mpz_ui_pow_ui(pow, 2, i);
mpz_mul(t3, t3, pow);
mpz_add(total, total, t3);
mpz_ui_pow_ui(total, 2, i);

mpz_t one_mpz;
mpz_init_set_ui(one_mpz, 1);

init(&b[i]);
mul_big_constants(&b[i], &total, &bits[i], &one_mpz, &one);
}

element check, checkCnst;
init(&check);
init(&checkCnst);
char buff[2048];
mpz_get_str(buff, 10, total);
input(&check, buff);
element fa;
init(&fa);

mul(&checkCnst, &check, &one);
addsmul(&fa, &size, b, &one);
assert_equal(&fa, &val);
}

typedef struct
Expand Down
4 changes: 2 additions & 2 deletions src/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(int argc, char *argv[])
bench = 1;
if (argc < 3)
{
printf("******************* ZPiE v0.3 *******************\n");
printf("******************* ZPiE v0.4 *******************\n");
printf("USAGE: ./zpie [ACTIONS] [OPTIONS]\n\n");
printf("[ACTIONS]:\n");
printf("-s <c>: Perform setup of 'c' constraints.\n");
Expand All @@ -38,7 +38,7 @@ int main(int argc, char *argv[])

if ((argc == 4) && (strcmp(argv[3], "-l") == 0)) logs = 1;

printf("******************* ZPiE v0.3 *******************\n");
printf("******************* ZPiE v0.4 *******************\n");

if ((strcmp(argv[1], "-s") == 0) || (strcmp(argv[1], "-p") == 0) || (strcmp(argv[1], "-v") == 0))
{
Expand Down
12 changes: 5 additions & 7 deletions src/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void init_setup(void *circuit)
M = 0;
N = 0;
nPublic = 0;
nConst = 0;
lro_const_total = 0;

mclBn_init(USEDCURVE, MCLBN_COMPILED_TIME_VAR);

Expand All @@ -44,10 +46,11 @@ void init_setup(void *circuit)
setParams = 0;

uw = (mpz_t*) malloc((M) * sizeof(mpz_t));
LRO_constants = (mpz_t*) malloc((lro_const_total) * sizeof(mpz_t));

for (int i = 0; i < M; i++)
{
mpz_init2(uw[i], BITS);
mpz_init2(uw[i], BITS);
}
}

Expand Down Expand Up @@ -149,12 +152,7 @@ void bos_coster_bp(mclBnG1 *chunk, mclBnG1 *points, mclBnFr *scalars, int heapsi

static inline void mult_exp(mclBnG1 *chunk, mclBnG1 *points, mclBnFr *scalars, int heapsize)
{
#ifdef BOSCOSTER_MULEXP
if ((heapsize > 32) && ((heapsize != 0) && ((heapsize & (heapsize - 1)) == 0))) bos_coster_bp(chunk, points, scalars, heapsize);
else mclBnG1_mulVec(chunk, points, scalars, heapsize);
#elif MCL_MULEXP
mclBnG1_mulVec(chunk, points, scalars, heapsize);
#endif
mclBnG1_mulVec(chunk, points, scalars, heapsize);
}

char *to_hex(const unsigned char *array, size_t length)
Expand Down
Loading

0 comments on commit 52a89b8

Please sign in to comment.