Skip to content

Commit

Permalink
Improve pointers usage
Browse files Browse the repository at this point in the history
  • Loading branch information
xevisalle committed Nov 9, 2023
1 parent 82a2035 commit 8656ae7
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ int main(int argc, char *argv[])
else if (strcmp(argv[1], "-p") == 0)
{
setup_keys keys = read_setup(&bench_circuit);
proof p = generate_proof(&bench_circuit, keys.pk);
proof p = generate_proof(&bench_circuit, &keys.pk);
store_proof(&p);
}
else if (strcmp(argv[1], "-v") == 0)
{
setup_keys keys = read_setup(&bench_circuit);
proof p = read_proof();
verify_proof(&bench_circuit, p, keys.vk);
verify_proof(&bench_circuit, &p, &keys.vk);
}

return 0;
Expand Down
29 changes: 14 additions & 15 deletions src/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ void init_setup(void *circuit)
}
}

void init_prover(void *circuit, proving_key pk)
void init_prover(void *circuit, proving_key *pk)
{
init_setup(circuit);

struct timespec begin, end;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &begin);

int n = mpz_get_ui(pk.Ne);
int n = mpz_get_ui(pk->Ne);

AsFr = (mclBnFr*) malloc((n) * sizeof(mclBnFr));
BsFr = (mclBnFr*) malloc((n) * sizeof(mclBnFr));
Expand All @@ -75,41 +75,40 @@ void init_prover(void *circuit, proving_key pk)
rsigma = (mclBnFr*) malloc((n) * sizeof(mclBnFr));
rsigmaInv = (mclBnFr*) malloc((n) * sizeof(mclBnFr));

mpz_t shift_fft_mpz;
static mpz_t shift;
mpz_init(shift);
mpz_init(shift_fft_mpz);

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

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_powm(shift, shift, pk->Ne, pPrime);
mpz_sub_ui(shift, shift, 1);
mpz_invert(shift, shift, pPrime);

mpz_to_fr(&shift_fft, &shift_fft_mpz);
mpz_to_fr(&shift_fft, &shift);

mclBnFr_setInt(&rsigma[0], 1);
mclBnFr_inv(&rsigmaInv[0], &rsigma[0]);

mclBnG1_mul(&pk.xt1_rand[0], &pk.xt1[0], &rsigmaInv[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);

mclBnFr one;
mclBnFr_setInt(&one, 1);
mclBnFr_mul(&rsigma[1], &rand, &one);

for (int i = 1; i < n; i++)
{
mpz_t factor;
mpz_init(factor);
mpz_powm_ui(factor, shift, i, pPrime);

mpz_to_fr(&rsigma[i], &factor);
if (i < n - 1) mclBnFr_mul(&rsigma[i + 1], &rsigma[i], &rand);

mclBnFr_inv(&rsigmaInv[i], &rsigma[i]);
mclBnG1_mul(&pk.xt1_rand[i], &pk.xt1[i], &rsigmaInv[i]);
mclBnG1_mul(&pk->xt1_rand[i], &pk->xt1[i], &rsigmaInv[i]);

mclBnFr_mul(&rsigma[i], &rsigma[i], &n_inverted);
}
Expand Down
2 changes: 0 additions & 2 deletions src/gro16/gro16.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ static mclBnFr *CsFr;

static mclBnFr *rsigma;
static mclBnFr *rsigmaInv;

static mpz_t shift;
static mclBnFr shift_fft;

static mpz_t *wM;
Expand Down
82 changes: 41 additions & 41 deletions src/gro16/prover.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

void h_coefficients(proving_key pk)
void h_coefficients(proving_key *pk)
{
int n = mpz_get_ui(pk.Ne);
int n = mpz_get_ui(pk->Ne);
mclBnFr uwFr[M];

#pragma omp parallel for
Expand All @@ -21,46 +21,46 @@ void h_coefficients(proving_key pk)
int l_it = 0;
int r_it = 1;

for (int j = 0; j < pk.qap_size; j+=3)
for (int j = 0; j < pk->qap_size; j+=3)
{
switch (pk.LRO[j])
switch (pk->LRO[j])
{
case 1: mclBnFr_add(&AsFr[pk.LRO[j+1]], &AsFr[pk.LRO[j+1]], &uwFr[pk.LRO[j+2]]); break;
case 2: mclBnFr_add(&BsFr[pk.LRO[j+1]], &BsFr[pk.LRO[j+1]], &uwFr[pk.LRO[j+2]]); break;
case 3: mclBnFr_add(&CsFr[pk.LRO[j+1]], &CsFr[pk.LRO[j+1]], &uwFr[pk.LRO[j+2]]); break;
case 1: mclBnFr_add(&AsFr[pk->LRO[j+1]], &AsFr[pk->LRO[j+1]], &uwFr[pk->LRO[j+2]]); break;
case 2: mclBnFr_add(&BsFr[pk->LRO[j+1]], &BsFr[pk->LRO[j+1]], &uwFr[pk->LRO[j+2]]); break;
case 3: mclBnFr_add(&CsFr[pk->LRO[j+1]], &CsFr[pk->LRO[j+1]], &uwFr[pk->LRO[j+2]]); break;
case 10:
{
mclBnFr factorFr;
if (pk.LRO[j+3] != INT_MAX)
if (pk->LRO[j+3] != INT_MAX)
{
mclBnFr_setInt(&factorFr, pk.LRO[j+3]);
mclBnFr_mul(&factorFr, &uwFr[pk.LRO[j+2]], &factorFr);
mclBnFr_setInt(&factorFr, pk->LRO[j+3]);
mclBnFr_mul(&factorFr, &uwFr[pk->LRO[j+2]], &factorFr);
}
else
{
mpz_to_fr(&factorFr, &pk.LRO_constants[l_it]);
mclBnFr_mul(&factorFr, &uwFr[pk.LRO[j+2]], &factorFr);
mpz_to_fr(&factorFr, &pk->LRO_constants[l_it]);
mclBnFr_mul(&factorFr, &uwFr[pk->LRO[j+2]], &factorFr);
l_it+=2;
}
mclBnFr_add(&AsFr[pk.LRO[j+1]], &AsFr[pk.LRO[j+1]], &factorFr);
mclBnFr_add(&AsFr[pk->LRO[j+1]], &AsFr[pk->LRO[j+1]], &factorFr);
j+=1;
break;
}
case 20:
{
mclBnFr factorFr;
if (pk.LRO[j+3] != INT_MAX)
if (pk->LRO[j+3] != INT_MAX)
{
mclBnFr_setInt(&factorFr, pk.LRO[j+3]);
mclBnFr_mul(&factorFr, &uwFr[pk.LRO[j+2]], &factorFr);
mclBnFr_setInt(&factorFr, pk->LRO[j+3]);
mclBnFr_mul(&factorFr, &uwFr[pk->LRO[j+2]], &factorFr);
}
else
{
mpz_to_fr(&factorFr, &pk.LRO_constants[r_it]);
mclBnFr_mul(&factorFr, &uwFr[pk.LRO[j+2]], &factorFr);
mpz_to_fr(&factorFr, &pk->LRO_constants[r_it]);
mclBnFr_mul(&factorFr, &uwFr[pk->LRO[j+2]], &factorFr);
r_it+=2;
}
mclBnFr_add(&BsFr[pk.LRO[j+1]], &BsFr[pk.LRO[j+1]], &factorFr);
mclBnFr_add(&BsFr[pk->LRO[j+1]], &BsFr[pk->LRO[j+1]], &factorFr);
j+=1;
break;
}
Expand All @@ -71,13 +71,13 @@ void h_coefficients(proving_key pk)
{
switch (get_thread())
{
case 0: ifft_t(n, pk.wMFr, AsFr); break;
case 1: ifft_t(n, pk.wMFr, BsFr); break;
case 2: ifft_t(n, pk.wMFr, CsFr); break;
case 0: ifft_t(n, pk->wMFr, AsFr); break;
case 1: ifft_t(n, pk->wMFr, BsFr); break;
case 2: ifft_t(n, pk->wMFr, CsFr); break;
case 99:
ifft_t(n, pk.wMFr, AsFr);
ifft_t(n, pk.wMFr, BsFr);
ifft_t(n, pk.wMFr, CsFr);
ifft_t(n, pk->wMFr, AsFr);
ifft_t(n, pk->wMFr, BsFr);
ifft_t(n, pk->wMFr, CsFr);
break;
}
}
Expand All @@ -89,12 +89,12 @@ void h_coefficients(proving_key pk)
mclBnFr_sub(&AsFr[i], &AsFr[i], &CsFr[i]);
}

ifft(n, pk.wMFr, AsFr);
ifft(n, pk->wMFr, AsFr);
}

void mul_exp(struct mulExpResult *result, mpz_t *uwProof, proving_key pk)
void mul_exp(struct mulExpResult *result, mpz_t *uwProof, proving_key *pk)
{
int n = mpz_get_ui(pk.Ne);
int n = mpz_get_ui(pk->Ne);

mclBnFr uwFactor[M];
mclBnFr uwFactorPublic[M-(nPublic + nConst)];
Expand All @@ -117,14 +117,14 @@ void mul_exp(struct mulExpResult *result, mpz_t *uwProof, proving_key pk)
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);
mclBnG2_mulVecMT(&result->uwB2, pk.B2, uwFactor, M, num_threads);
mclBnG1_mulVecMT(&result->uwC1, pk.pk1, uwFactorPublic, M-(nPublic + nConst), num_threads);
mclBnG1_mulVecMT(&result->htdelta, pk.xt1_rand, AsFr, n, num_threads);
mclBnG1_mulVecMT(&result->uwA1, pk->A1, uwFactor, M, num_threads);
mclBnG1_mulVecMT(&result->uwB1, pk->B1, uwFactor, M, num_threads);
mclBnG2_mulVecMT(&result->uwB2, pk->B2, uwFactor, M, num_threads);
mclBnG1_mulVecMT(&result->uwC1, pk->pk1, uwFactorPublic, M-(nPublic + nConst), num_threads);
mclBnG1_mulVecMT(&result->htdelta, pk->xt1_rand, AsFr, n, num_threads);
}

void prove(int *circuit, mclBnG1 *piA, mclBnG2 *piB2, mclBnG1 *piC, mpz_t *uwProof, proving_key pk)
void prove(int *circuit, mclBnG1 *piA, mclBnG2 *piB2, mclBnG1 *piC, mpz_t *uwProof, proving_key *pk)
{
prover = 1;

Expand Down Expand Up @@ -169,22 +169,22 @@ void prove(int *circuit, mclBnG1 *piA, mclBnG2 *piB2, mclBnG1 *piC, mpz_t *uwPro
generate_random_scalar(&s);

// piA = s1.alpha + Auw + r * s1.delta;
mclBnG1_mul(piA, &pk.delta1, &r);
mclBnG1_mul(piA, &pk->delta1, &r);
mclBnG1_add(piA, piA, &result.uwA1);
mclBnG1_add(piA, piA, &pk.alpha1);
mclBnG1_add(piA, piA, &pk->alpha1);
// piB1 = s1.beta + B1uw + s * s1.delta;
mclBnG1_mul(&piB1, &pk.delta1, &s);
mclBnG1_mul(&piB1, &pk->delta1, &s);
mclBnG1_add(&piB1, &piB1, &result.uwB1);
mclBnG1_add(&piB1, &piB1, &pk.beta1);
mclBnG1_add(&piB1, &piB1, &pk->beta1);
// piB2 = s2.beta + B2uw + s * s2.delta;
mclBnG2_mul(piB2, &pk.delta2, &s);
mclBnG2_mul(piB2, &pk->delta2, &s);
mclBnG2_add(piB2, piB2, &result.uwB2);
mclBnG2_add(piB2, piB2, &pk.beta2);
mclBnG2_add(piB2, piB2, &pk->beta2);

mclBnG1 factorG1;

// piC = Cw + htdelta + piA*s + piB*r - r*s*s1.delta
mclBnG1_mul(&factorG1, &pk.delta1, &r);
mclBnG1_mul(&factorG1, &pk->delta1, &r);
mclBnG1_mul(&factorG1, &factorG1, &s);
mclBnG1_mul(piC, &piB1, &r);
mclBnG1_sub(&factorG1, piC, &factorG1);
Expand Down
24 changes: 12 additions & 12 deletions src/gro16/verifier.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

int verify(mclBnG1 *piA, mclBnG2 *piB2, mclBnG1 *piC, mpz_t u[(nPublic + nConst)], verifying_key vk)
int verify(proof *p, verifying_key *vk)
{
mclBnG1 factorG1;
mclBnFr frFactor;
Expand All @@ -10,16 +10,16 @@ int verify(mclBnG1 *piA, mclBnG2 *piB2, mclBnG1 *piC, mpz_t u[(nPublic + nConst)
for (int i = (nPublic); i--;)
{
// Vu = Vu + u[i] * s1.vk[i]
mpz_to_fr(&frFactor, &u[i]);
mclBnG1_mul(&factorG1, &vk.vk1[i+nConst], &frFactor);
mpz_to_fr(&frFactor, &p->uwProof[i]);
mclBnG1_mul(&factorG1, &vk->vk1[i+nConst], &frFactor);
mclBnG1_add(&Vu, &Vu, &factorG1);
}

for (int i = (nConst); i--;)
{
// Vu = Vu + u[i] * s1.vk[i]
mpz_to_fr(&frFactor, &vk.constants[i]);
mclBnG1_mul(&factorG1, &vk.vk1[i], &frFactor);
mpz_to_fr(&frFactor, &vk->constants[i]);
mclBnG1_mul(&factorG1, &vk->vk1[i], &frFactor);
mclBnG1_add(&Vu, &Vu, &factorG1);
}

Expand All @@ -29,13 +29,13 @@ int verify(mclBnG1 *piA, mclBnG2 *piB2, mclBnG1 *piC, mpz_t u[(nPublic + nConst)
{
switch (get_thread())
{
case 0: mclBn_pairing(&pairing1, piA, piB2); break;
case 1: mclBn_pairing(&pairing2, &Vu, &vk.gamma2); break;
case 2: mclBn_pairing(&pairing3, piC, &vk.delta2); break;
case 0: mclBn_pairing(&pairing1, &p->piA, &p->piB2); break;
case 1: mclBn_pairing(&pairing2, &Vu, &vk->gamma2); break;
case 2: mclBn_pairing(&pairing3, &p->piC, &vk->delta2); break;
case 99:
mclBn_pairing(&pairing1, piA, piB2);
mclBn_pairing(&pairing2, &Vu, &vk.gamma2);
mclBn_pairing(&pairing3, piC, &vk.delta2);
mclBn_pairing(&pairing1, &p->piA, &p->piB2);
mclBn_pairing(&pairing2, &Vu, &vk->gamma2);
mclBn_pairing(&pairing3, &p->piC, &vk->delta2);
break;
}
}
Expand All @@ -53,7 +53,7 @@ int verify(mclBnG1 *piA, mclBnG2 *piB2, mclBnG1 *piC, mpz_t u[(nPublic + nConst)
}

log_message("Computing e(alpha, beta) * e(Vu, gamma) * e(piC, delta)...");
mclBnGT_mul(&factorGT, &vk.alphabetaT, &pairing2);
mclBnGT_mul(&factorGT, &vk->alphabetaT, &pairing2);
mclBnGT_mul(&factorGT, &factorGT, &pairing3);
log_state(1);
if (logs)
Expand Down
14 changes: 7 additions & 7 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void test_prover(void)
{
test_no_rand = 1;
setup_keys keys = perform_setup(&test_single_constraint);
proof p = generate_proof(&test_single_constraint, keys.pk);
proof p = generate_proof(&test_single_constraint, &keys.pk);

const char *piAstr = "1 13398732126763033363928255770670403609664455533535809960659793057603927642327 14567332642717250669329472598965177550050834309459245026995104363234319745805";
const char *piB2str = "1 9513526328373247288214002967710658327692956864193416721895179753121227228903 17320346092699268035923233491595138958007151833266586455159840335219170425243 8079768110185479532548096263199181437927983909022782182442306192699700743609 19381997603489315175356927627025590277145986935796790438444340629346184509934";
Expand All @@ -82,13 +82,13 @@ void test_full_circuits(void)
setup_keys keys_mh = perform_setup(&test_mimc_hash);
setup_keys keys_ev = perform_setup(&test_eddsa_verification);

proof p_sc = generate_proof(&test_single_constraint, keys_sc.pk);
proof p_mh = generate_proof(&test_mimc_hash, keys_mh.pk);
proof p_ev = generate_proof(&test_eddsa_verification, keys_ev.pk);
proof p_sc = generate_proof(&test_single_constraint, &keys_sc.pk);
proof p_mh = generate_proof(&test_mimc_hash, &keys_mh.pk);
proof p_ev = generate_proof(&test_eddsa_verification, &keys_ev.pk);

CU_ASSERT(verify_proof(&test_single_constraint, p_sc, keys_sc.vk));
CU_ASSERT(verify_proof(&test_mimc_hash, p_mh, keys_mh.vk));
CU_ASSERT(verify_proof(&test_eddsa_verification, p_ev, keys_ev.vk));
CU_ASSERT(verify_proof(&test_single_constraint, &p_sc, &keys_sc.vk));
CU_ASSERT(verify_proof(&test_mimc_hash, &p_mh, &keys_mh.vk));
CU_ASSERT(verify_proof(&test_eddsa_verification, &p_ev, &keys_ev.vk));
}

//TODO: fix this
Expand Down
10 changes: 4 additions & 6 deletions src/zpie.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ setup_keys read_setup(void *circuit)
return keys;
}

proof generate_proof(void *circuit, proving_key pk)
proof generate_proof(void *circuit, proving_key *pk)
{
init_prover(circuit, pk);

Expand All @@ -377,7 +377,7 @@ proof generate_proof(void *circuit, proving_key pk)
mpz_init(uw[i]);
}

int n = mpz_get_ui(pk.Ne);
int n = mpz_get_ui(pk->Ne);
wM = (mpz_t*) malloc((n) * sizeof(mpz_t));

proof p;
Expand Down Expand Up @@ -418,8 +418,6 @@ proof generate_proof(void *circuit, proving_key pk)
mpz_clear(uw[i]);
}

mpz_clear(shift);

return p;
}

Expand Down Expand Up @@ -472,15 +470,15 @@ proof read_proof()
return p;
}

int verify_proof(void *circuit, proof p, verifying_key vk)
int verify_proof(void *circuit, proof *p, verifying_key *vk)
{
init_setup(circuit);

struct timespec begin, end;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &begin);

int verified = verify(&p.piA, &p.piB2, &p.piC, p.uwProof, vk);
int verified = verify(p, vk);

if (verified)
{
Expand Down
Loading

0 comments on commit 8656ae7

Please sign in to comment.