circomlib/circuits/pedersen.circom
circuits/pedersen.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 "montgomery.circom";
21include "mux3.circom";
22include "babyjub.circom";
23
24template Window4() {
25 signal input in[4];
26 signal input base[2];
27 signal output out[2];
28 signal output out8[2]; // Returns 8*Base (To be linked)
29
30 component mux = MultiMux3(2);
31
32 mux.s[0] <== in[0];
33 mux.s[1] <== in[1];
34 mux.s[2] <== in[2];
35
36 component dbl2 = MontgomeryDouble();
37 component adr3 = MontgomeryAdd();
38 component adr4 = MontgomeryAdd();
39 component adr5 = MontgomeryAdd();
40 component adr6 = MontgomeryAdd();
41 component adr7 = MontgomeryAdd();
42 component adr8 = MontgomeryAdd();
43
44// in[0] -> 1*BASE
45
46 mux.c[0][0] <== base[0];
47 mux.c[1][0] <== base[1];
48
49// in[1] -> 2*BASE
50 dbl2.in[0] <== base[0];
51 dbl2.in[1] <== base[1];
52 mux.c[0][1] <== dbl2.out[0];
53 mux.c[1][1] <== dbl2.out[1];
54
55// in[2] -> 3*BASE
56 adr3.in1[0] <== base[0];
57 adr3.in1[1] <== base[1];
58 adr3.in2[0] <== dbl2.out[0];
59 adr3.in2[1] <== dbl2.out[1];
60 mux.c[0][2] <== adr3.out[0];
61 mux.c[1][2] <== adr3.out[1];
62
63// in[3] -> 4*BASE
64 adr4.in1[0] <== base[0];
65 adr4.in1[1] <== base[1];
66 adr4.in2[0] <== adr3.out[0];
67 adr4.in2[1] <== adr3.out[1];
68 mux.c[0][3] <== adr4.out[0];
69 mux.c[1][3] <== adr4.out[1];
70
71// in[4] -> 5*BASE
72 adr5.in1[0] <== base[0];
73 adr5.in1[1] <== base[1];
74 adr5.in2[0] <== adr4.out[0];
75 adr5.in2[1] <== adr4.out[1];
76 mux.c[0][4] <== adr5.out[0];
77 mux.c[1][4] <== adr5.out[1];
78
79// in[5] -> 6*BASE
80 adr6.in1[0] <== base[0];
81 adr6.in1[1] <== base[1];
82 adr6.in2[0] <== adr5.out[0];
83 adr6.in2[1] <== adr5.out[1];
84 mux.c[0][5] <== adr6.out[0];
85 mux.c[1][5] <== adr6.out[1];
86
87// in[6] -> 7*BASE
88 adr7.in1[0] <== base[0];
89 adr7.in1[1] <== base[1];
90 adr7.in2[0] <== adr6.out[0];
91 adr7.in2[1] <== adr6.out[1];
92 mux.c[0][6] <== adr7.out[0];
93 mux.c[1][6] <== adr7.out[1];
94
95// in[7] -> 8*BASE
96 adr8.in1[0] <== base[0];
97 adr8.in1[1] <== base[1];
98 adr8.in2[0] <== adr7.out[0];
99 adr8.in2[1] <== adr7.out[1];
100 mux.c[0][7] <== adr8.out[0];
101 mux.c[1][7] <== adr8.out[1];
102
103 out8[0] <== adr8.out[0];
104 out8[1] <== adr8.out[1];
105
106 out[0] <== mux.out[0];
107 out[1] <== - mux.out[1]*2*in[3] + mux.out[1]; // Negate y if in[3] is one
108}
109
110
111template Segment(nWindows) {
112 signal input in[nWindows*4];
113 signal input base[2];
114 signal output out[2];
115
116 var i;
117 var j;
118
119 // Convert the base to montgomery
120
121 component e2m = Edwards2Montgomery();
122 e2m.in[0] <== base[0];
123 e2m.in[1] <== base[1];
124
125 component windows[nWindows];
126 component doublers1[nWindows-1];
127 component doublers2[nWindows-1];
128 component adders[nWindows-1];
129 for (i=0; i<nWindows; i++) {
130 windows[i] = Window4();
131 if (i==0) {
132 windows[i].base[0] <== e2m.out[0];
133 windows[i].base[1] <== e2m.out[1];
134 } else {
135 doublers1[i-1] = MontgomeryDouble();
136 doublers2[i-1] = MontgomeryDouble();
137 doublers1[i-1].in[0] <== windows[i-1].out8[0];
138 doublers1[i-1].in[1] <== windows[i-1].out8[1];
139 doublers2[i-1].in[0] <== doublers1[i-1].out[0];
140 doublers2[i-1].in[1] <== doublers1[i-1].out[1];
141
142 windows[i].base[0] <== doublers2[i-1].out[0];
143 windows[i].base[1] <== doublers2[i-1].out[1];
144
145 adders[i-1] = MontgomeryAdd();
146 if (i==1) {
147 adders[i-1].in1[0] <== windows[0].out[0];
148 adders[i-1].in1[1] <== windows[0].out[1];
149 } else {
150 adders[i-1].in1[0] <== adders[i-2].out[0];
151 adders[i-1].in1[1] <== adders[i-2].out[1];
152 }
153 adders[i-1].in2[0] <== windows[i].out[0];
154 adders[i-1].in2[1] <== windows[i].out[1];
155 }
156 for (j=0; j<4; j++) {
157 windows[i].in[j] <== in[4*i+j];
158 }
159 }
160
161 component m2e = Montgomery2Edwards();
162
163 if (nWindows > 1) {
164 m2e.in[0] <== adders[nWindows-2].out[0];
165 m2e.in[1] <== adders[nWindows-2].out[1];
166 } else {
167 m2e.in[0] <== windows[0].out[0];
168 m2e.in[1] <== windows[0].out[1];
169 }
170
171 out[0] <== m2e.out[0];
172 out[1] <== m2e.out[1];
173}
174
175template Pedersen(n) {
176 signal input in[n];
177 signal output out[2];
178
179 var BASE = [
180 [10457101036533406547632367118273992217979173478358440826365724437999023779287,19824078218392094440610104313265183977899662750282163392862422243483260492317],
181 [2671756056509184035029146175565761955751135805354291559563293617232983272177,2663205510731142763556352975002641716101654201788071096152948830924149045094],
182 [5802099305472655231388284418920769829666717045250560929368476121199858275951,5980429700218124965372158798884772646841287887664001482443826541541529227896],
183 [7107336197374528537877327281242680114152313102022415488494307685842428166594,2857869773864086953506483169737724679646433914307247183624878062391496185654],
184 [20265828622013100949498132415626198973119240347465898028410217039057588424236,1160461593266035632937973507065134938065359936056410650153315956301179689506],
185 [1487999857809287756929114517587739322941449154962237464737694709326309567994,14017256862867289575056460215526364897734808720610101650676790868051368668003],
186 [14618644331049802168996997831720384953259095788558646464435263343433563860015,13115243279999696210147231297848654998887864576952244320558158620692603342236],
187 [6814338563135591367010655964669793483652536871717891893032616415581401894627,13660303521961041205824633772157003587453809761793065294055279768121314853695],
188 [3571615583211663069428808372184817973703476260057504149923239576077102575715,11981351099832644138306422070127357074117642951423551606012551622164230222506],
189 [18597552580465440374022635246985743886550544261632147935254624835147509493269,6753322320275422086923032033899357299485124665258735666995435957890214041481]
190 ]
191
192 var nSegments = ((n-1)\200)+1;
193
194 component segments[nSegments];
195
196 var i;
197 var j;
198 var nBits;
199 var nWindows;
200 for (i=0; i<nSegments; i++) {
201 nBits = (i == (nSegments-1)) ? n - (nSegments-1)*200 : 200;
202 nWindows = ((nBits - 1)\4)+1;
203 segments[i] = Segment(nWindows);
204 segments[i].base[0] <== BASE[i][0];
205 segments[i].base[1] <== BASE[i][1];
206 for (j = 0; j<nBits; j++) {
207 segments[i].in[j] <== in[i*200+j];
208 }
209 // Fill padding bits
210 for (j = nBits; j < nWindows*4; j++) {
211 segments[i].in[j] <== 0;
212 }
213 }
214
215 component adders[nSegments-1];
216
217 for (i=0; i<nSegments-1; i++) {
218 adders[i] = BabyAdd();
219 if (i==0) {
220 adders[i].x1 <== segments[0].out[0];
221 adders[i].y1 <== segments[0].out[1];
222 adders[i].x2 <== segments[1].out[0];
223 adders[i].y2 <== segments[1].out[1];
224 } else {
225 adders[i].x1 <== adders[i-1].xout;
226 adders[i].y1 <== adders[i-1].yout;
227 adders[i].x2 <== segments[i+1].out[0];
228 adders[i].y2 <== segments[i+1].out[1];
229 }
230 }
231
232/*
233 coponent packPoint = PackPoint();
234
235 if (nSegments>1) {
236 packPoint.in[0] <== adders[nSegments-2].xout;
237 packPoint.in[1] <== adders[nSegments-2].yout;
238 } else {
239 packPoint.in[0] <== segments[0].out[0];
240 packPoint.in[1] <== segments[0].out[1];
241 }
242
243 out[0] <== packPoint.out[0];
244 out[1] <== packPoint.out[1];
245*/
246
247 if (nSegments>1) {
248 out[0] <== adders[nSegments-2].xout;
249 out[1] <== adders[nSegments-2].yout;
250 } else {
251 out[0] <== segments[0].out[0];
252 out[1] <== segments[0].out[1];
253 }
254}
255