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
20include "bitify.circom";
21include "escalarmulfix.circom";
22
23template BabyAdd() {
24 signal input x1;
25 signal input y1;
26 signal input x2;
27 signal input y2;
28 signal output xout;
29 signal output yout;
30
31 signal beta;
32 signal gamma;
33 signal delta;
34 signal tau;
35
36 var a = 168700;
37 var d = 168696;
38
39 beta <== x1*y2;
40 gamma <== y1*x2;
41 delta <== (-a*x1+y1)*(x2 + y2);
42 tau <== beta * gamma;
43
44 xout <-- (beta + gamma) / (1+ d*tau);
45 (1+ d*tau) * xout === (beta + gamma);
46
47 yout <-- (delta + a*beta - gamma) / (1-d*tau);
48 (1-d*tau)*yout === (delta + a*beta - gamma);
49}
50
51template BabyDbl() {
52 signal input x;
53 signal input y;
54 signal output xout;
55 signal output yout;
56
57 component adder = BabyAdd();
58 adder.x1 <== x;
59 adder.y1 <== y;
60 adder.x2 <== x;
61 adder.y2 <== y;
62
63 adder.xout ==> xout;
64 adder.yout ==> yout;
65}
66
67
68template BabyCheck() {
69 signal input x;
70 signal input y;
71
72 signal x2;
73 signal y2;
74
75 var a = 168700;
76 var d = 168696;
77
78 x2 <== x*x;
79 y2 <== y*y;
80
81 a*x2 + y2 === 1 + d*x2*y2;
82}
83
84// Extracts the public key from private key
85template BabyPbk() {
86 signal private input in;
87 signal output Ax;
88 signal output Ay;
89
90 var BASE8[2] = [
91 5299619240641551281634865583518297030282874472190772894086521144482721001553,
92 16950150798460657717958625567821834550301663161624707787222815936182638968203
93 ];
94
95 component pvkBits = Num2Bits(253);
96 pvkBits.in <== in;
97
98 component mulFix = EscalarMulFix(253, BASE8);
99
100 var i;
101 for (i=0; i<253; i++) {
102 mulFix.e[i] <== pvkBits.out[i];
103 }
104 Ax <== mulFix.out[0];
105 Ay <== mulFix.out[1];
106}