Skip to content

Commit

Permalink
merkle depth 1 works
Browse files Browse the repository at this point in the history
  • Loading branch information
mstraka100 committed Nov 1, 2018
1 parent 593dd9d commit 50cfb02
Show file tree
Hide file tree
Showing 2 changed files with 872 additions and 16 deletions.
40 changes: 24 additions & 16 deletions circuitify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ regex var_re = regex("[A-Za-z_][0-9a-zA-Z_]*");
regex secret_re = regex("#(-?[0-9]+)");
regex num_re = regex("[0-9]+");

mpz_class modinv(mpz_class x) {
mpz_t ret;
mpz_init(ret);
mpz_class n = mod - 2;
mpz_powm(ret, x.get_mpz_t(), n.get_mpz_t(), mod.get_mpz_t());
return mpz_class(ret);
}

class Vars {

private:
Expand Down Expand Up @@ -229,6 +237,7 @@ class Linear {
real = (real - other.real + mod) % mod;
constant = (constant - other.constant + mod) % mod;
vars.sub(other.vars);
cout << "SUBBING" << endl;
}

void mul(mpz_class v) {
Expand All @@ -238,9 +247,14 @@ class Linear {
}

void div(mpz_class v) {
real = (real / v) % mod;
constant = (constant / v) % mod;
vars.div(v);
cout << "DIVIDING" << endl;
v = modinv(v);
cout << "div value: " << v << endl << "real value to divide: " << real << endl;
mul(v);
//real = (real * v) % mod;
//real = (real / v) % mod;
//constant = (constant / v) % mod;
// vars.div(v);
}

void index_temp_vars(map<int, vector<int>>& index, int pos) {
Expand Down Expand Up @@ -275,14 +289,6 @@ struct multiplication {
Linear o;
};

mpz_class modinv(mpz_class x) {
mpz_t ret;
mpz_init(ret);
mpz_class n = mod - 2;
mpz_powm(ret, x.get_mpz_t(), n.get_mpz_t(), mod.get_mpz_t());
return mpz_class(ret);
}

void new_mul(mpz_class l, mpz_class r, Linear& nl, Linear& nr, Linear& no)
{
mpz_class o = (l*r) % mod;
Expand Down Expand Up @@ -344,13 +350,17 @@ Linear new_multiplication(Linear& l, Linear& r, bool addeqs = true) {
// mutates l and/or r
Linear new_division(Linear& l, Linear& r) {
if (r.is_const()) {
cout << "dividing by constant" << endl;
l.div(r.constant);
return l;
}
Linear lv = Linear();
Linear rv = Linear();
Linear ret = Linear();
new_mul(l.real * modinv(r.real), r.real, lv, rv, ret);
new_mul((l.real * modinv(r.real)) % mod, r.real, ret, rv, lv);
/*cout << "l: " << l.real << " lv: " << lv.real << " r: " << r.real << " rv: " << rv.real << endl;
cout << "inverse: " << l.real * modinv(r.real) << endl;
cout << "ret: " << ret.real << endl;*/
l.sub(lv);
r.sub(rv);
eqs.push_back(l);
Expand Down Expand Up @@ -444,12 +454,14 @@ Linear parse_expression(string s) {
delim = {"*", "/"};
split = split_expr_binary(s, delim, sp);
if (split) {
cout << "multiplying or dividing" << endl;
Linear left = parse_expression(sp.l);
Linear right = parse_expression(sp.r);
if (sp.op == "*") {
Linear ret = new_multiplication(left, right);
return ret;
} else {
cout << "new division" << endl;
Linear ret = new_division(left, right);
return ret;
}
Expand Down Expand Up @@ -508,9 +520,6 @@ vector<Linear> parse_expressions(string s) {
return ret;
}


//void split_by_delimiter(constconst string delim, )

bool all_const(vector<Linear> v) {
for (int i = 0; i < v.size(); i++) {
if (!v[i].is_const())
Expand Down Expand Up @@ -567,7 +576,6 @@ void parse_statement(string& s) {
}
} else {
for (int i = 0; i < bits.size(); i++) {
cout << "i: " << i << endl;
Linear l;
mpz_class v = (val.real >> i) & 1;
new_temp(v,l);
Expand Down
Loading

0 comments on commit 50cfb02

Please sign in to comment.