-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit.circom
148 lines (111 loc) · 2.38 KB
/
circuit.circom
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
pragma circom 2.1.4;
include "circomlib/comparators.circom";
include "circomlib/sign.circom";
include "circomlib/bitify.circom";
template floor(v){
var t;
signal output outf;
if(v<0){
t=v +(-1);
outf <== t;
}
else outf <== v;
}
template lagrangeBasis(data, i) {
var numerator = 1;
var denominator = 1;
signal output outa;
signal output outb;
for (var j=0; j<3; j++) {
if (i != j) {
numerator = numerator * (-data[j]);
denominator = denominator * (data[j] - data[i]);
}
}
outa <== numerator;
outb <== denominator;
}
template IsNegative(){
signal input in;
signal output out;
component n2b = Num2Bits(254);
component sign = Sign();
in ==> n2b.in;
for (var i = 0; i<254; i++) {
n2b.out[i] ==> sign.in[i];
}
sign.sign ==> out;
}
template divmod(num,den,p){
var t = 0;
var nt = 1;
var r = num;
var nr = den % p;
var tmp;
component res;
component isz = IsZero();
// component gt = GreaterThan();
component neg = IsNegative();
var quot;
component outd;
while (!nr.isz) {
quot === floor(r / nr);
tmp = nt;
nt = t - (quot * (nt));
t = tmp;
tmp = nr;
nr = r - (quot * (nr));
r = tmp;
}
if (r > 1 )
{
outd <== 0;
}
if (t.neg)
{
t += p;
}
res = num * t % p;
outd <== res;
}
// Lagrange interpolation
template lagrangeInterpolate(data,p){
var S = 0;
signal input share[3];
signal output oute;
component basis;
component divI;
component datA;
for (var i=0; i<3; i++) {
basis = lagrangeBasis(data, 1);
var num = basis.outa;
var den = basis.outb;
divI = divmod(num,den,p);
datA = (data[i].y * divI);
S = S + datA ;
}
var rest = S % (p);
oute <== rest;
}
template combine () {
signal input share[3][2];
var sharesX;
var sharesY;
signal output secret[3][2];
var shares[3][2];
for(var i = 0;i<3;i++)
{
sharesX = share[i][0];
sharesY = share[i][1];
shares[i][0] = sharesX;
shares[i][1] = sharesY;
}
secret <== shares;
}
// component main = combine();
/* INPUT = {
"share":[["0","1"],["8","1"],["1","4"]]
} */
// secret <== lagrangeInterpolate(shares, prime);
// }
component main{public[share,prime]} = combine();