From 82a203537baafdfbeb15c6c18f7bc82e3f443076 Mon Sep 17 00:00:00 2001 From: xevisalle Date: Thu, 9 Nov 2023 01:46:34 +0100 Subject: [PATCH] rsigma to mcl --- src/common/fourier.c | 4 ++-- src/common/utils.c | 57 +++++++++++++++++++------------------------- src/gro16/gro16.h | 6 ++--- src/zpie.c | 4 ++-- 4 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/common/fourier.c b/src/common/fourier.c index 45afa42..dd7b99a 100644 --- a/src/common/fourier.c +++ b/src/common/fourier.c @@ -63,11 +63,11 @@ void ifft_t(size_t arr, mclBnFr domain[], mclBnFr *o) out[i] = o[i]; } - mclBnFr_mul(&o[0], &out[0], &rsigmaFr[0]); + mclBnFr_mul(&o[0], &out[0], &rsigma[0]); for (int i = 1; i < arr; i++) { - mclBnFr_mul(&o[i], &out[arr-i], &rsigmaFr[i]); + mclBnFr_mul(&o[i], &out[arr-i], &rsigma[i]); } fft(arr, domain, o); diff --git a/src/common/utils.c b/src/common/utils.c index 3853d92..69b05f6 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -72,22 +72,16 @@ void init_prover(void *circuit, proving_key pk) mpz_set_str(pPrime, PRIMESTR, 10); if (bench) printf(" |--- FFT domain size : %d\n", n); - rsigma = (mpz_t*) malloc((n) * sizeof(mpz_t)); - rsigmaFr = (mclBnFr*) malloc((n) * sizeof(mclBnFr)); - rsigmaInv = (mpz_t*) malloc((n) * sizeof(mpz_t)); - - mpz_t randNum; - mpz_init(randNum); - mpz_t factor, shift_fft_mpz; - mpz_init_set_ui(factor, n); - mpz_invert(factor, factor, pPrime); + rsigma = (mclBnFr*) malloc((n) * sizeof(mclBnFr)); + rsigmaInv = (mclBnFr*) malloc((n) * sizeof(mclBnFr)); + + mpz_t shift_fft_mpz; mpz_init(shift); mpz_init(shift_fft_mpz); mclBnFr rand; generate_random_scalar(&rand); - fr_to_mpz(&randNum, &rand); - mpz_set(shift, randNum); + fr_to_mpz(&shift, &rand); mpz_powm(shift_fft_mpz, shift, pk.Ne, pPrime); mpz_sub_ui(shift_fft_mpz, shift_fft_mpz, 1); @@ -95,32 +89,29 @@ void init_prover(void *circuit, proving_key pk) mpz_to_fr(&shift_fft, &shift_fft_mpz); - mpz_init2(rsigma[0], BITS); - mpz_init2(rsigmaInv[0], BITS); - mpz_set_ui(rsigma[0], 1); - mpz_invert(rsigmaInv[0], rsigma[0], pPrime); + mclBnFr_setInt(&rsigma[0], 1); + mclBnFr_inv(&rsigmaInv[0], &rsigma[0]); - mclBnFr frFactor; - mpz_to_fr(&frFactor, &rsigmaInv[0]); - mclBnG1_mul(&pk.xt1_rand[0], &pk.xt1[0], &frFactor); - mpz_mul(rsigma[0], rsigma[0], factor); - mpz_mod(rsigma[0], rsigma[0], pPrime); - mpz_to_fr(&rsigmaFr[0], &rsigma[0]); + mclBnG1_mul(&pk.xt1_rand[0], &pk.xt1[0], &rsigmaInv[0]); + + mclBnFr n_inverted; + mclBnFr_setInt(&n_inverted, n); + mclBnFr_inv(&n_inverted, &n_inverted); + + mclBnFr_mul(&rsigma[0], &rsigma[0], &n_inverted); for (int i = 1; i < n; i++) { - mclBnFr frFactorMulti; - mpz_init2(rsigma[i], BITS); - mpz_init2(rsigmaInv[i], BITS); - mpz_powm_ui(rsigma[i], shift, i, pPrime); - mpz_invert(rsigmaInv[i], rsigma[i], pPrime); - - mpz_to_fr(&frFactorMulti, &rsigmaInv[i]); - mclBnG1_mul(&pk.xt1_rand[i], &pk.xt1[i], &frFactorMulti); - - mpz_mul(rsigma[i], rsigma[i], factor); - mpz_mod(rsigma[i], rsigma[i], pPrime); - mpz_to_fr(&rsigmaFr[i], &rsigma[i]); + mpz_t factor; + mpz_init(factor); + mpz_powm_ui(factor, shift, i, pPrime); + + mpz_to_fr(&rsigma[i], &factor); + + mclBnFr_inv(&rsigmaInv[i], &rsigma[i]); + mclBnG1_mul(&pk.xt1_rand[i], &pk.xt1[i], &rsigmaInv[i]); + + mclBnFr_mul(&rsigma[i], &rsigma[i], &n_inverted); } clock_gettime(CLOCK_MONOTONIC, &end); diff --git a/src/gro16/gro16.h b/src/gro16/gro16.h index ef50b55..42c76ce 100644 --- a/src/gro16/gro16.h +++ b/src/gro16/gro16.h @@ -45,9 +45,9 @@ static mclBnFr *AsFr; static mclBnFr *BsFr; static mclBnFr *CsFr; -static mpz_t *rsigma; -static mclBnFr *rsigmaFr; -static mpz_t *rsigmaInv; +static mclBnFr *rsigma; +static mclBnFr *rsigmaInv; + static mpz_t shift; static mclBnFr shift_fft; diff --git a/src/zpie.c b/src/zpie.c index c894ac2..51e24c1 100644 --- a/src/zpie.c +++ b/src/zpie.c @@ -409,8 +409,8 @@ proof generate_proof(void *circuit, proving_key pk) mclBnFr_clear(&BsFr[i]); mclBnFr_clear(&CsFr[i]); - mpz_clear(rsigma[i]); - mpz_clear(rsigmaInv[i]); + mclBnFr_clear(&rsigma[i]); + mclBnFr_clear(&rsigmaInv[i]); } for (int i = 0; i < M; i++)