Skip to content

Commit 9a89980

Browse files
committed
Extend tutorial fixing the factor example
1 parent 631ae52 commit 9a89980

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

TUTORIAL.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,54 @@ The `===` operator adds a constraint without assigning any value to a signal.
269269

270270
The circuit also has another problem: the operation works in `Z_r`, so we need to guarantee the multiplication does not overflow. This can be done by converting the inputs to binary and checking the ranges, but we will reserve it for future tutorials.
271271

272+
Another problem of the circuit is that circom works with a field of a prime that in general is arround the 255bits. That means that it's very easy to factor in that field.
273+
274+
One possible solution to this, is to limit the inputs to 64 bits. that means that this way it will not be possible to have overflow.
275+
276+
The final circuit would look like:
277+
278+
```
279+
template CheckBits(n) {
280+
signal input in;
281+
signal bits[n];
282+
var lc1=0;
283+
284+
var e2=1;
285+
for (var i = 0; i<n; i++) {
286+
bits[i] <-- (in >> i) & 1;
287+
bits[i] * (bits[i] -1 ) === 0;
288+
lc1 += bits[i] * e2;
289+
e2 = e2+e2;
290+
}
291+
292+
lc1 === in;
293+
}
294+
295+
template Multiplier(n) {
296+
signal private input a;
297+
signal private input b;
298+
signal output c;
299+
signal inva;
300+
signal invb;
301+
302+
component chackA = CheckBits(n);
303+
component chackB = CheckBits(n);
304+
305+
chackA.in <== a;
306+
chackB.in <== b;
307+
308+
inva <-- 1/(a-1);
309+
(a-1)*inva === 1;
310+
311+
invb <-- 1/(b-1);
312+
(b-1)*invb === 1;
313+
314+
c <== a*b;
315+
}
316+
317+
component main = Multiplier(64);
318+
```
319+
272320
## Where to go from here
273321

274322
You may want to read the [README](https://github.com/iden3/circom) to learn more features about `circom`.

0 commit comments

Comments
 (0)