Skip to content

Commit

Permalink
Switch to MCL
Browse files Browse the repository at this point in the history
  • Loading branch information
xevisalle committed Nov 13, 2023
1 parent e9431b8 commit 0dede6b
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 199 deletions.
34 changes: 18 additions & 16 deletions circuits/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ void add(element uOut, element vOut, element u1, element v1, element u2, element

mul_constants(&factor, &one_int, &factor1, &d, &factor2);

mpz_t invFactor;
mpz_init(invFactor);
mclBnFr invFactor;

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);
mclBnFr f_check;
mclBnFr_add(&f_check, &uw[one.index], &uw[factor.index]);
mclBnFr_inv(&invFactor, &f_check);
}

char buff[2048];
mpz_get_str(buff, 10, invFactor);
mclBnFr_getStr(buff, sizeof(buff), &invFactor, 10);
input(&factor4, buff);

addmul(&one, &factor, &one, &factor4); // verify x * 1/x = 1
Expand All @@ -47,13 +45,12 @@ void add(element uOut, element vOut, element u1, element v1, element u2, element

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);
mclBnFr f_check;
mclBnFr_sub(&f_check, &uw[one.index], &uw[factor.index]);
mclBnFr_inv(&invFactor, &f_check);
}

mpz_get_str(buff, 10, invFactor);
mclBnFr_getStr(buff, sizeof(buff), &invFactor, 10);
input(&factor7, buff);

addmul_constants(&one, &one_int, &one, &one_neg, &factor, &one_int, &factor7); // verify x * 1/x = 1
Expand Down Expand Up @@ -146,7 +143,10 @@ void to_bits(element *bits, element val, int size)
{
if(!setParams)
{
mpz_tdiv_q_2exp(t1, uw[val.index], i);
mpz_t factor;
mpz_init(factor);
fr_to_mpz(&factor, &uw[val.index]);
mpz_tdiv_q_2exp(t1, factor, i);
mpz_and(t3, t1, t2);
}

Expand All @@ -156,11 +156,13 @@ void to_bits(element *bits, element val, int size)

mpz_ui_pow_ui(total, 2, i);

mpz_t one_mpz;
mpz_init_set_ui(one_mpz, 1);
mclBnFr one_mpz;
mclBnFr_setInt(&one_mpz, 1);

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

element fa;
Expand Down
33 changes: 2 additions & 31 deletions src/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ void init_setup(void *circuit)
init_circuit(circuit);
setParams = 0;

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

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

Expand Down Expand Up @@ -240,35 +240,6 @@ void sort_list(mpz_t *exp[], int heapsize)
elapsedSort += (end.tv_nsec - begin.tv_nsec) / 1000000000.0;
}

void bos_coster(mpz_t *exp[], int heapsize, int baseNum, proving_key *pk)
{
sort_list(exp, heapsize);
while (mpz_cmp_ui(*exp[2], 0) != 0)
{
struct timespec begin, end;
clock_gettime(CLOCK_MONOTONIC, &begin);
mpz_sub(*exp[0], *exp[0], *exp[2]);

if (baseNum) mclBnG1_add(&pk->xt1[exp[2]-wM], &pk->xt1[exp[0]-wM], &pk->xt1[exp[2]-wM]);
else
{
mclBnG1_add(&pk->A1[exp[2]-uw], &pk->A1[exp[0]-uw], &pk->A1[exp[2]-uw]);
mclBnG1_add(&pk->B1[exp[2]-uw], &pk->B1[exp[0]-uw], &pk->B1[exp[2]-uw]);
mclBnG2_add(&pk->B2[exp[2]-uw], &pk->B2[exp[0]-uw], &pk->B2[exp[2]-uw]);
mclBnG1_add(&pk->pk1[exp[2]-uw], &pk->pk1[exp[0]-uw], &pk->pk1[exp[2]-uw]);
}

clock_gettime(CLOCK_MONOTONIC, &end);
elapsedBosCoster += (end.tv_sec - begin.tv_sec);
elapsedBosCoster += (end.tv_nsec - begin.tv_nsec) / 1000000000.0;
clock_gettime(CLOCK_MONOTONIC, &begin);
binarymaxheap(exp, 0, heapsize);
clock_gettime(CLOCK_MONOTONIC, &end);
elapsedSort += (end.tv_sec - begin.tv_sec);
elapsedSort += (end.tv_nsec - begin.tv_nsec) / 1000000000.0;
}
}

int fr_cmp(mclBnFr *frFactor1, mclBnFr *frFactor2)
{
mpz_t f1, f2;
Expand Down
6 changes: 3 additions & 3 deletions src/gro16/gro16.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ struct Sigma2
typedef struct
{
mpz_t Ne;
mclBnFr *wMFr;
mclBnFr *wM;

int qap_size;
int *LRO;
mpz_t *LRO_constants;
mclBnFr *LRO_constants;

mclBnG1 alpha1;
mclBnG1 beta1;
Expand All @@ -126,7 +126,7 @@ struct mulExpResult

typedef struct
{
mpz_t *constants;
mclBnFr *constants;
mclBnGT alphabetaT;
mclBnG2 gamma2;
mclBnG2 delta2;
Expand Down
127 changes: 60 additions & 67 deletions src/gro16/parser.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@

void element_log(char *text, element *oo)
{
if(!setParams) gmp_printf("%s%Zd\n", text, uw[oo->index]);
//if(!setParams) gmp_printf("%s%Zd\n", text, uw[oo->index]);
}

void addmul(element *oo, element *lo1, element *lo2, element *ro)
{
if (setParams) N++;
else if (prover)
{
mpz_add(uw[oo->index], uw[lo1->index], uw[lo2->index]);
mpz_mul(uw[oo->index], uw[oo->index], uw[ro->index]);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mclBnFr_add(&uw[oo->index], &uw[lo1->index], &uw[lo2->index]);
mclBnFr_mul(&uw[oo->index], &uw[oo->index], &uw[ro->index]);
}
else
{
Expand All @@ -29,9 +28,8 @@ void addmul0(element *oo, element *lo1, element *lo2, element *ro)
if (setParams) N++;
else if (prover)
{
mpz_add(uw[oo->index], uw[lo1->index], uw[lo2->index]);
mpz_mul(uw[oo->index], uw[oo->index], uw[ro->index]);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mclBnFr_add(&uw[oo->index], &uw[lo1->index], &uw[lo2->index]);
mclBnFr_mul(&uw[oo->index], &uw[oo->index], &uw[ro->index]);
}
else
{
Expand All @@ -48,10 +46,9 @@ void add3mul(element *oo, element *lo1, element *lo2, element *lo3, element *ro)
if (setParams) N++;
else if (prover)
{
mpz_add(uw[oo->index], uw[lo1->index], uw[lo2->index]);
mpz_add(uw[oo->index], uw[oo->index], uw[lo3->index]);
mpz_mul(uw[oo->index], uw[oo->index], uw[ro->index]);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mclBnFr_add(&uw[oo->index], &uw[lo1->index], &uw[lo2->index]);
mclBnFr_add(&uw[oo->index], &uw[oo->index], &uw[lo3->index]);
mclBnFr_mul(&uw[oo->index], &uw[oo->index], &uw[ro->index]);
}
else
{
Expand All @@ -72,12 +69,10 @@ void addsmul(element *oo, int *size, element *los, element *ro)
{
for (int i = 0; i < *size; i++)
{
mpz_add(uw[oo->index], uw[oo->index], uw[los[i].index]);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mclBnFr_add(&uw[oo->index], &uw[oo->index], &uw[los[i].index]);
}

mpz_mul(uw[oo->index], uw[oo->index], uw[ro->index]);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mclBnFr_mul(&uw[oo->index], &uw[oo->index], &uw[ro->index]);
}
else
{
Expand All @@ -98,15 +93,12 @@ void add3muladd3(element *oo, element *lo1, element *lo2, element *lo3, element
if (setParams) N++;
else if (prover)
{
mpz_t factor;
mpz_init(factor);
mpz_add(uw[oo->index], uw[lo1->index], uw[lo2->index]);
mpz_add(uw[oo->index], uw[oo->index], uw[lo3->index]);
mpz_add(factor, uw[ro1->index], uw[ro2->index]);
mpz_add(factor, factor, uw[ro3->index]);
mpz_mul(uw[oo->index], uw[oo->index], factor);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mpz_clear(factor);
mclBnFr factor;
mclBnFr_add(&uw[oo->index], &uw[lo1->index], &uw[lo2->index]);
mclBnFr_add(&uw[oo->index], &uw[oo->index], &uw[lo3->index]);
mclBnFr_add(&factor, &uw[ro1->index], &uw[ro2->index]);
mclBnFr_add(&factor, &factor, &uw[ro3->index]);
mclBnFr_mul(&uw[oo->index], &uw[oo->index], &factor);
}
else
{
Expand All @@ -127,13 +119,10 @@ void addmuladd(element *oo, element *lo1, element *lo2, element *ro1, element *r
if (setParams) N++;
else if (prover)
{
mpz_t factor;
mpz_init(factor);
mpz_add(uw[oo->index], uw[lo1->index], uw[lo2->index]);
mpz_add(factor, uw[ro1->index], uw[ro2->index]);
mpz_mul(uw[oo->index], factor, uw[oo->index]);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mpz_clear(factor);
mclBnFr factor;
mclBnFr_add(&uw[oo->index], &uw[lo1->index], &uw[lo2->index]);
mclBnFr_add(&factor, &uw[ro1->index], &uw[ro2->index]);
mclBnFr_mul(&uw[oo->index], &factor, &uw[oo->index]);
}
else
{
Expand All @@ -152,8 +141,7 @@ void mul(element *oo, element *lo, element *ro)
if (setParams) N++;
else if (prover)
{
mpz_mul(uw[oo->index], uw[lo->index], uw[ro->index]);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mclBnFr_mul(&uw[oo->index], &uw[lo->index], &uw[ro->index]);
}
else
{
Expand All @@ -170,15 +158,15 @@ void addmul_constants(element *oo, int *lc1, element *lo1, int *lc2, element *lo
if (setParams) N++;
else if (prover)
{
mpz_t factor;
mpz_init(factor);
mpz_mul_si(factor, uw[lo1->index], *lc1);
mpz_mul_si(uw[oo->index], uw[lo2->index], *lc2);
mpz_add(factor, factor, uw[oo->index]);
mpz_mul_si(uw[oo->index], uw[ro->index], *rc);
mpz_mul(uw[oo->index], uw[oo->index], factor);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mpz_clear(factor);
mclBnFr factor, factor2;
mclBnFr_setInt(&factor, *lc1);
mclBnFr_mul(&factor, &uw[lo1->index], &factor);
mclBnFr_setInt(&factor2, *lc2);
mclBnFr_mul(&uw[oo->index], &uw[lo2->index], &factor2);
mclBnFr_add(&factor, &factor, &uw[oo->index]);
mclBnFr_setInt(&factor2, *rc);
mclBnFr_mul(&uw[oo->index], &uw[ro->index], &factor2);
mclBnFr_mul(&uw[oo->index], &uw[oo->index], &factor);
}
else
{
Expand All @@ -196,13 +184,12 @@ void mul_constants(element *oo, int *lc, element *lo, int *rc, element *ro)
if (setParams) N++;
else if (prover)
{
mpz_t factor;
mpz_init(factor);
mpz_mul_si(factor, uw[lo->index], *lc);
mpz_mul_si(uw[oo->index], uw[ro->index], *rc);
mpz_mul(uw[oo->index], uw[oo->index], factor);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mpz_clear(factor);
mclBnFr factor, factor2;
mclBnFr_setInt(&factor, *lc);
mclBnFr_mul(&factor, &uw[lo->index], &factor);
mclBnFr_setInt(&factor2, *rc);
mclBnFr_mul(&uw[oo->index], &uw[ro->index], &factor2);
mclBnFr_mul(&uw[oo->index], &uw[oo->index], &factor);
}
else
{
Expand All @@ -214,7 +201,7 @@ void mul_constants(element *oo, int *lc, element *lo, int *rc, element *ro)
}
}

void mul_big_constants(element *oo, mpz_t *lc, element *lo, mpz_t *rc, element *ro)
void mul_big_constants(element *oo, mclBnFr *lc, element *lo, mclBnFr *rc, element *ro)
{
if (setParams)
{
Expand All @@ -223,13 +210,10 @@ void mul_big_constants(element *oo, mpz_t *lc, element *lo, mpz_t *rc, element *
}
else if (prover)
{
mpz_t factor;
mpz_init(factor);
mpz_mul(factor, uw[lo->index], *lc);
mpz_mul(uw[oo->index], uw[ro->index], *rc);
mpz_mul(uw[oo->index], uw[oo->index], factor);
mpz_mod(uw[oo->index], uw[oo->index], pPrime);
mpz_clear(factor);
mclBnFr factor;
mclBnFr_mul(&factor, &uw[lo->index], lc);
mclBnFr_mul(&uw[oo->index], &uw[ro->index], rc);
mclBnFr_mul(&uw[oo->index], &uw[oo->index], &factor);
}
else
{
Expand All @@ -238,8 +222,11 @@ void mul_big_constants(element *oo, mpz_t *lc, element *lo, mpz_t *rc, element *
O[cn][oo->index] = 1;

cn++;
mpz_init_set(LRO_constants[lro_constants_n], *lc);
mpz_init_set(LRO_constants[lro_constants_n + 1], *rc);
mpz_t factor;
fr_to_mpz(&factor, lc);
mpz_set(LRO_constants[lro_constants_n], factor);
fr_to_mpz(&factor, rc);
mpz_set(LRO_constants[lro_constants_n + 1], factor);
lro_constants_n += 2;
}
}
Expand All @@ -256,7 +243,7 @@ void assert_equal(element *lo, element *ro)

void input(element *var, char *val)
{
if (!setParams) mpz_set_str(uw[var->index], val, 10);
if (!setParams) mclBnFr_setStr(&uw[var->index], val, strlen(val), 10);
}

void init_constant(element *toAdd, char *val)
Expand All @@ -266,7 +253,7 @@ void init_constant(element *toAdd, char *val)
{
toAdd->index = constant_n;
constant_n++;
mpz_set_str(uw[toAdd->index], val, 10);
mclBnFr_setStr(&uw[toAdd->index], val, strlen(val), 10);
}
if (setParams) nConst++;
}
Expand Down Expand Up @@ -343,7 +330,7 @@ void test_full_api()

void test_constraint_system(void)
{
uw = (mpz_t*) malloc((99) * sizeof(mpz_t));
uw = (mclBnFr*) malloc((99) * sizeof(mclBnFr));
wn = nPublic + nConst;
un = nConst;
constant_n = 0;
Expand All @@ -352,15 +339,21 @@ void test_constraint_system(void)

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

prover = 1;
init_circuit(&test_full_api);
prover = 0;

CU_ASSERT(mpz_cmp_ui(uw[nConst], 50) == 0);
CU_ASSERT(mpz_cmp_ui(uw[1+nConst], 150) == 0);
CU_ASSERT(mpz_cmp_ui(uw[2+nConst], 150) == 0);
CU_ASSERT(mpz_cmp_ui(uw[3+nConst], 200) == 0);
mclBnFr equal;
mclBnFr_setInt(&equal, 50);
CU_ASSERT(mclBnFr_isEqual(&uw[nConst], &equal));

mclBnFr_setInt(&equal, 150);
CU_ASSERT(mclBnFr_isEqual(&uw[1+nConst], &equal));
CU_ASSERT(mclBnFr_isEqual(&uw[2+nConst], &equal));

mclBnFr_setInt(&equal, 200);
CU_ASSERT(mclBnFr_isEqual(&uw[3+nConst], &equal));
}
Loading

0 comments on commit 0dede6b

Please sign in to comment.