Skip to content

Commit

Permalink
Remove MPZ usage from fourier
Browse files Browse the repository at this point in the history
  • Loading branch information
xevisalle committed Nov 9, 2023
1 parent 32f959c commit 2e6f7dc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
23 changes: 6 additions & 17 deletions src/common/fourier.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ void fft(size_t arr, mclBnFr domain[], mclBnFr *o)
}
}

void ifft(size_t arr, mclBnFr domain[], mclBnFr *o, mpz_t *Ne)
void ifft(size_t arr, mclBnFr domain[], mclBnFr *o)
{
fft(arr, domain, o);

mpz_t factor, factor2;
mclBnFr out[arr];
mclBnFr frFactor;

Expand All @@ -41,17 +40,10 @@ void ifft(size_t arr, mclBnFr domain[], mclBnFr *o, mpz_t *Ne)
out[i] = o[i];
}

mpz_init(factor2);
mpz_powm(factor2, shift, *Ne, pPrime);
mpz_sub_ui(factor2, factor2, 1);
mpz_invert(factor2, factor2, pPrime);
mclBnFr_setInt(&frFactor, arr);
mclBnFr_inv(&frFactor, &frFactor);
mclBnFr_mul(&frFactor, &frFactor, &shift_fft);

mpz_init_set_ui(factor, arr);
mpz_invert(factor, factor, pPrime);
mpz_mul(factor, factor, factor2);
mpz_mod(factor, factor, pPrime);

mpz_to_fr(&frFactor, &factor);
mclBnFr_mul(&o[0], &out[0], &frFactor);

for (int i = 1; i < arr; i++)
Expand All @@ -71,14 +63,11 @@ void ifft_t(size_t arr, mclBnFr domain[], mclBnFr *o)
out[i] = o[i];
}

mclBnFr frFactor;
mpz_to_fr(&frFactor, &rsigma[0]);
mclBnFr_mul(&o[0], &out[0], &frFactor);
mclBnFr_mul(&o[0], &out[0], &rsigmaFr[0]);

for (int i = 1; i < arr; i++)
{
mpz_to_fr(&frFactor, &rsigma[i]);
mclBnFr_mul(&o[i], &out[arr-i], &frFactor);
mclBnFr_mul(&o[i], &out[arr-i], &rsigmaFr[i]);
}

fft(arr, domain, o);
Expand Down
12 changes: 11 additions & 1 deletion src/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,28 @@ void init_prover(void *circuit, proving_key pk)
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;
mpz_t factor, shift_fft_mpz;
mpz_init_set_ui(factor, n);
mpz_invert(factor, factor, pPrime);
mpz_init(shift);
mpz_init(shift_fft_mpz);

mclBnFr rand;
generate_random_scalar(&rand);
fr_to_mpz(&randNum, &rand);
mpz_set(shift, randNum);

mpz_powm(shift_fft_mpz, shift, pk.Ne, pPrime);
mpz_sub_ui(shift_fft_mpz, shift_fft_mpz, 1);
mpz_invert(shift_fft_mpz, shift_fft_mpz, pPrime);

mpz_to_fr(&shift_fft, &shift_fft_mpz);

mpz_init2(rsigma[0], BITS);
mpz_init2(rsigmaInv[0], BITS);
mpz_set_ui(rsigma[0], 1);
Expand All @@ -97,6 +105,7 @@ void init_prover(void *circuit, proving_key pk)
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]);

for (int i = 1; i < n; i++)
{
Expand All @@ -111,6 +120,7 @@ void init_prover(void *circuit, proving_key pk)

mpz_mul(rsigma[i], rsigma[i], factor);
mpz_mod(rsigma[i], rsigma[i], pPrime);
mpz_to_fr(&rsigmaFr[i], &rsigma[i]);
}

clock_gettime(CLOCK_MONOTONIC, &end);
Expand Down
2 changes: 2 additions & 0 deletions src/gro16/gro16.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ static mclBnFr *BsFr;
static mclBnFr *CsFr;

static mpz_t *rsigma;
static mclBnFr *rsigmaFr;
static mpz_t *rsigmaInv;
static mpz_t shift;
static mclBnFr shift_fft;

static mpz_t *wM;

Expand Down
2 changes: 1 addition & 1 deletion src/gro16/prover.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void h_coefficients(proving_key pk)
mclBnFr_sub(&AsFr[i], &AsFr[i], &CsFr[i]);
}

ifft(n, pk.wMFr, AsFr, &pk.Ne);
ifft(n, pk.wMFr, AsFr);
}

void mul_exp(struct mulExpResult *result, mpz_t *uwProof, proving_key pk)
Expand Down

0 comments on commit 2e6f7dc

Please sign in to comment.