circomlib/circuits/babyjub.circom
circuits/babyjub.circomRaw
1/*
2 Copyright 2018 0KIMS association.
3
4 This file is part of circom (Zero Knowledge Circuit Compiler).
5
6 circom is a free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 circom is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with circom. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20template BabyAdd() {
21 signal input x1;
22 signal input y1;
23 signal input x2;
24 signal input y2;
25 signal output xout;
26 signal output yout;
27
28 signal beta;
29 signal gamma;
30 signal delta;
31 signal tau;
32
33 var a = 168700;
34 var d = 168696;
35
36 beta <== x1*y2;
37 gamma <== y1*x2;
38 delta <== (-a*x1+y1)*(x2 + y2);
39 tau <== beta * gamma;
40
41 xout <-- (beta + gamma) / (1+ d*tau);
42 (1+ d*tau) * xout === (beta + gamma);
43
44 yout <-- (delta + a*beta - gamma) / (1-d*tau);
45 (1-d*tau)*yout === (delta + a*beta - gamma);
46}
47
48template BabyDbl() {
49 signal input x;
50 signal input y;
51 signal output xout;
52 signal output yout;
53
54 component adder = BabyAdd();
55 adder.x1 <== x;
56 adder.y1 <== y;
57 adder.x2 <== x;
58 adder.y2 <== y;
59
60 adder.xout ==> xout;
61 adder.yout ==> yout;
62}
63
64
65template BabyCheck() {
66 signal input x;
67 signal input y;
68
69 signal x2;
70 signal y2;
71
72 var a = 168700;
73 var d = 168696;
74
75 x2 <== x*x;
76 y2 <== y*y;
77
78 a*x2 + y2 === 1 + d*x2*y2;
79}