-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmimcfstl_2br_snark.cpp
92 lines (64 loc) · 2.01 KB
/
mimcfstl_2br_snark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef MIMC_FSTL_2BR_SNARK_CPP
#define MIMC_FSTL_2BR_SNARK_CPP
/*
This function computes all the rank-1 constraints for the MiMC
2 branch FN
+--------------------------------------------------------------------------+
*/
template<typename field_t>
void mimcfstl_2br_snark<field_t>::generate_r1_constraint()
{
index_t lindex = 1;
index_t rindex = lindex + 1;
index_t var_index = rindex;
var_index++;
for(int i = 0;i < numofRound;i++) {
linear_term< field_t > xc(0, roundConst[i]);
linear_term< field_t > x1(lindex, (field_t)(1));
linear_term< field_t > x2(rindex, (field_t)(1));
linear_term< field_t > x3(var_index, (field_t)(1));
var_index++;
linear_combination< field_t > A(xc + x1);
linear_combination < field_t > B(A);
linear_combination < field_t > C(x3);
constraint < field_t > constr(A, B, C);
mimcfstl2br_constr_wit.add_constraint(constr);
A.reset(C);
x3.set(var_index, (field_t) 1);
x2.set(rindex, (field_t) (-1) );
rindex = lindex;
lindex = var_index;
var_index++;
C.clear();
C.add_term(x3);
C.add_term(x2);
constr.reset_constraint(A, B, C);
mimcfstl2br_constr_wit.add_constraint(constr);
}
//mimcfstl2br_constr_wit.print_constraints();
}
template<typename field_t>
void mimcfstl_2br_snark<field_t>::generate_witness(field_t leftinput,
field_t rightinput)
{
index_t var_index = 0;
mimcfstl2br_constr_wit.add_witness({leftinput, rightinput});
var_index++;
field_t lval = leftinput;
field_t rval = rightinput;
for(int i = 0;i < numofRound;i++) {
field_t temp = (leftinput + roundConst[i]);
field_t temp1 = temp*temp;
addCount += 1;
multCount++;
//field_t temp1 = (leftinput + roundConst[i])*temp + rightinput;
temp = temp1*temp + rightinput;
//temp = temp + rightinput;
addCount += 1;
multCount++;
mimcfstl2br_constr_wit.add_witness({temp, temp1});
rightinput = leftinput;
leftinput = temp1;
}
}
#endif