Skip to content

Commit b093943

Browse files
authored
Merge pull request #143 from costa-group/only_adding_tags
Sync from costa-group/circomlib
2 parents 8cffc6b + 5982124 commit b093943

13 files changed

Lines changed: 457 additions & 204 deletions

circuits/binsum.circom

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,6 @@
2222

2323
// The templates and functions in this file are general and work for any prime field
2424

25-
/*
26-
*** nbits(x): function that returns the number of bits that we need to represent the value x
27-
- Inputs: x -> field value
28-
- Output: number of bits needed to represent x
29-
30-
Example: nbits(7) = 3, nbits(10) = 4
31-
32-
*/
33-
34-
function nbits(a) {
35-
var n = 1;
36-
var r = 0;
37-
while (n-1<a) {
38-
r++;
39-
n *= 2;
40-
}
41-
return r;
42-
}
43-
4425

4526
/*
4627

circuits/bitify.circom

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ include "comparators.circom";
2222
include "aliascheck.circom";
2323

2424

25+
/*
26+
*** nbits(x): function that returns the number of bits that we need to represent the value x
27+
- Inputs: x -> field value
28+
- Output: number of bits needed to represent x
29+
30+
Example: nbits(7) = 3, nbits(10) = 4
31+
32+
*/
33+
34+
function nbits(a) {
35+
var n = 1;
36+
var r = 0;
37+
while (n-1<a) {
38+
r++;
39+
n *= 2;
40+
}
41+
return r;
42+
}
43+
44+
2545
/*
2646
*** Num2Bits(n): template that transforms an input into its binary representation using n bits
2747
- Inputs: in -> field value

circuits/comparators.circom

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -291,58 +291,3 @@ template CompConstant(ct) {
291291
out <== num2bits.out[127];
292292
}
293293

294-
/*
295-
*** MaxValueCheck(ct): template that receives an input, checks its value is smaller than or equal to the constant value ct given as a parameter, and returns the same input but with the tag maxvalue with value ct
296-
- Inputs: in -> field number
297-
- Outputs: out -> field number
298-
satisfies tag maxvalue with value ct
299-
*/
300-
301-
template MaxValueCheck(ct){
302-
signal input in;
303-
signal output {maxvalue} out;
304-
305-
signal res <== CompConstant(ct)(Num2Bits(254)(in));
306-
res === 0;
307-
out.maxvalue = ct;
308-
out <== in;
309-
}
310-
311-
/*
312-
*** MinValueCheck(ct): template that receives an input, checks its value is greater than or equal to the constant value ct given as a parameter, and returns the same input but with the tag minvalue with value ct
313-
- Inputs: in -> field number
314-
- Outputs: out -> field number
315-
satisfies tag minvalue with value ct
316-
*/
317-
318-
template MinValueCheck(ct){
319-
signal input in;
320-
signal output {minvalue} out;
321-
322-
signal res <== CompConstant(ct-1)(Num2Bits(254)(in));
323-
res === 1;
324-
out.minvalue = ct;
325-
out <== in;
326-
}
327-
328-
/*
329-
*** MinMaxValueCheck(ct): template that receives an input, checks its value is greater than or equal to the constant value ct1 given as a first parameter and smaller than or equal to the constant value ct2 given as a second parameter, and returns the same input but with the tag minvalue with value ct1 and the tag maxvalue with value ct2
330-
- Inputs: in -> field number
331-
- Outputs: out -> field number
332-
satisfies tag minvalue with value ct1
333-
satisfies tag maxvalue with value ct2
334-
*/
335-
336-
template MinMaxValueCheck(ct1,ct2){
337-
signal input in;
338-
signal output {minvalue,maxvalue} out;
339-
340-
signal inb[254] <== Num2Bits(254)(in);
341-
signal res1 <== CompConstant(ct1-1)(inb);
342-
res1 === 1;
343-
out.minvalue = ct1;
344-
signal res2 <== CompConstant(ct2)(inb);
345-
res2 === 0;
346-
out.maxvalue = ct2;
347-
out <== in;
348-
}

circuits/sha256/ch.circom

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,33 @@
1717
along with circom. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
/* Ch
2120

22-
000 0
23-
001 1
24-
010 0
25-
011 1
26-
100 0
27-
101 0
28-
110 1
29-
111 1
21+
pragma circom 2.1.5;
3022

31-
out = a&b ^ (!a)&c =>
32-
33-
out = a*(b-c) + c
23+
/*
3424
35-
*/
36-
pragma circom 2.0.0;
25+
*** Ch_t(n): template that receives three inputs of n bits and returns for each i = 0..n-1
26+
out[i] = (a[i] & b[i]) or (!a[i] & c[i]) (that is, (a[i] => b[i]) & (!a[i] => c[i]))
27+
28+
- Inputs: a[n] -> array of n bits
29+
requires tag binary
30+
b[n] -> array of n bits
31+
requires tag binary
32+
c[n] -> array of n bits
33+
requires tag binary
34+
- Output: out[n] -> array of n bits, it takes the value out[i] = (a[i] & b[i]) or (!a[i] & c[i])
35+
satisfies tag binary
36+
37+
Example: a b c out
38+
0 0 0 0
39+
0 0 1 1
40+
0 1 0 0
41+
0 1 1 1
42+
1 0 0 0
43+
1 0 1 0
44+
1 1 0 1
45+
1 1 1 1
46+
*/
3747

3848
template Ch_t(n) {
3949
signal input {binary} a[n];

circuits/sha256/constants.circom

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
*/
1919
pragma circom 2.0.0;
2020

21+
22+
/*
23+
24+
*** H(x): template that returns the value of the constant H(x) used in the sha256 protocol represented using 32 bits
25+
26+
- Output: out[32] -> array of 32 bits, returns the constant H(x)
27+
satisfies tag binary
28+
29+
*/
30+
2131
template H(x) {
2232
signal output {binary} out[32];
2333
var c[8] = [0x6a09e667,
@@ -34,6 +44,16 @@ template H(x) {
3444
}
3545
}
3646

47+
/*
48+
49+
*** K(x): template that returns the value of the constant K(x) used in the sha256 protocol represented using 32 bits
50+
51+
- Output: out[32] -> array of 32 bits, returns the constant K(x)
52+
satisfies tag binary
53+
54+
*/
55+
56+
3757
template K(x) {
3858
signal output {binary} out[32];
3959
var c[64] = [

circuits/sha256/maj.circom

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,32 @@
1717
along with circom. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
/* Maj function for sha256
20+
pragma circom 2.1.5;
2121

22-
out = a&b ^ a&c ^ b&c =>
23-
24-
out = a*b + a*c + b*c - 2*a*b*c =>
25-
26-
out = a*( b + c - 2*b*c ) + b*c =>
27-
28-
mid = b*c
29-
out = a*( b + c - 2*mid ) + mid
22+
/*
3023
31-
*/
32-
pragma circom 2.0.0;
24+
*** Maj_t(n): template that receives three inputs of n bits and returns for each i = 0..n out[i] = 1 in case at least two of a the values of a[i], b[i] and c[i] are 1, and 0 otherwise
25+
26+
- Inputs: a[n] -> array of n bits
27+
requires tag binary
28+
b[n] -> array of n bits
29+
requires tag binary
30+
c[n] -> array of n bits
31+
requires tag binary
32+
- Output: out[n] -> array of n bits, it takes the value:
33+
out[i] = a[i] & b[i] \/ a[i] & c[i] \/ b[i] & c[i]
34+
satisfies tag binary
35+
36+
Example: a b c out
37+
0 0 0 0
38+
0 0 1 0
39+
0 1 0 0
40+
0 1 1 1
41+
1 0 0 0
42+
1 0 1 1
43+
1 1 0 1
44+
1 1 1 1
45+
*/
3346

3447
template Maj_t(n) {
3548
signal input {binary} a[n];

circuits/sha256/rotate.circom

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@
1616
You should have received a copy of the GNU General Public License
1717
along with circom. If not, see <https://www.gnu.org/licenses/>.
1818
*/
19-
pragma circom 2.0.0;
19+
pragma circom 2.1.5;
20+
21+
/*
22+
23+
*** RotR(n, r): template that receives an array of n bits and returns the array rotated r positions to the right
24+
25+
- Inputs: in[n] -> array of n bits
26+
requires tag binary
27+
- Output: out[n] -> array of n bits, it takes the value:
28+
out[i] = in[(i + r) % n]
29+
satisfies tag binary
30+
31+
Example: RotR(4, 1)([1, 0, 1, 1]) = [0, 1, 1, 1]
32+
33+
*/
2034

2135
template RotR(n, r) {
2236
signal input {binary} in[n];

circuits/sha256/shift.circom

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,22 @@
1616
You should have received a copy of the GNU General Public License
1717
along with circom. If not, see <https://www.gnu.org/licenses/>.
1818
*/
19-
pragma circom 2.0.0;
19+
pragma circom 2.1.5;
20+
21+
22+
/*
23+
24+
*** ShR(n, r): template that receives an array of n bits and returns the array shifted r positions to the right
25+
26+
- Inputs: in[n] -> array of n bits
27+
requires tag binary
28+
- Output: out[n] -> array of n bits, it takes the value:
29+
out[i] = in[i + r] if i + r < n, out[i] = 0 otherwise
30+
satisfies tag binary
31+
32+
Example: ShR(4, 2)([1, 0, 0, 1]) = [0, 1, 0, 0]
33+
34+
*/
2035

2136
template ShR(n, r) {
2237
signal input {binary} in[n];

circuits/sha256/sigma.circom

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@
1616
You should have received a copy of the GNU General Public License
1717
along with circom. If not, see <https://www.gnu.org/licenses/>.
1818
*/
19-
pragma circom 2.0.0;
19+
pragma circom 2.1.5;
2020

2121
include "xor3.circom";
2222
include "rotate.circom";
2323
include "shift.circom";
2424

25+
26+
/*
27+
28+
*** SmallSigma(ra, rb, rc): template that receives an array in of 32 bits and returns an array out of 32 bits s.t. out[i] = XOR3(rot_a[i], rot_b[i], shift_c[i]) with
29+
* rot_a is the array in rotated ra bits to the right (see rotate.circom)
30+
* rot_b is the array in rotated rb bits to the right (see rotate.circom)
31+
* shift_c is the array in shifted rc bits to the right (see shift.circom)
32+
- Inputs: in[n] -> array of n bits
33+
requires tag binary
34+
- Output: out[n] -> array of n bits, it takes the value described above
35+
satisfies tag binary
36+
37+
*/
38+
2539
template SmallSigma(ra, rb, rc) {
2640
signal input {binary} in[32];
2741
signal output {binary} out[32];
@@ -49,6 +63,20 @@ template SmallSigma(ra, rb, rc) {
4963
}
5064
}
5165

66+
/*
67+
68+
*** BigSigma(ra, rb, rc): template that receives an array in of 32 bits and returns an array out of 32 bits s.t. out[i] = XOR3(rot_a[i], rot_b[i], rot_c[i]) with
69+
* rot_a is the array in rotated ra bits to the right (see rotate.circom)
70+
* rot_b is the array in rotated rb bits to the right (see rotate.circom)
71+
* rot_c is the array in rotated rc bits to the right (see rotate.circom)
72+
- Inputs: in[n] -> array of n bits
73+
requires tag binary
74+
- Output: out[n] -> array of n bits, it takes the value described above
75+
satisfies tag binary
76+
77+
*/
78+
*/
79+
5280
template BigSigma(ra, rb, rc) {
5381
signal input {binary} in[32];
5482
signal output {binary} out[32];

circuits/sha256/xor3.circom

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,32 @@
1717
along with circom. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
/* Xor3 function for sha256
20+
pragma circom 2.1.5;
2121

22-
out = a ^ b ^ c =>
23-
24-
out = a+b+c - 2*a*b - 2*a*c - 2*b*c + 4*a*b*c =>
25-
26-
out = a*( 1 - 2*b - 2*c + 4*b*c ) + b + c - 2*b*c =>
27-
28-
mid = b*c
29-
out = a*( 1 - 2*b -2*c + 4*mid ) + b + c - 2 * mid
22+
/*
3023
31-
*/
32-
pragma circom 2.0.0;
24+
*** Xor3(n): template that receives three inputs of n bits and returns for each i = 0..n-1
25+
out[i] = xor3(a[i], b[i], c[i]), more details in the table below
26+
27+
- Inputs: a[n] -> array of n bits
28+
requires tag binary
29+
b[n] -> array of n bits
30+
requires tag binary
31+
c[n] -> array of n bits
32+
requires tag binary
33+
- Output: out[n] -> array of n bits, it takes the value out[i] = xor3(a[i], b[i], c[i])
34+
satisfies tag binary
35+
36+
Example: a b c out
37+
0 0 0 0
38+
0 0 1 1
39+
0 1 0 1
40+
0 1 1 0
41+
1 0 0 1
42+
1 0 1 0
43+
1 1 0 0
44+
1 1 1 1
45+
*/
3346

3447
template Xor3(n) {
3548
signal input {binary} a[n];

0 commit comments

Comments
 (0)