circomlib/circuits/sha256/xor3.circom
circuits/sha256/xor3.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
20/* Xor3 function for sha256
21
22out = a ^ b ^ c =>
23
24out = a+b+c - 2*a*b - 2*a*c - 2*b*c + 4*a*b*c =>
25
26out = a*( 1 - 2*b - 2*c + 4*b*c ) + b + c - 2*b*c =>
27
28mid = b*c
29out = a*( 1 - 2*b -2*c + 4*mid ) + b + c - 2 * mid
30
31*/
32
33template Xor3(n) {
34 signal input a[n];
35 signal input b[n];
36 signal input c[n];
37 signal output out[n];
38 signal mid[n];
39
40 for (var k=0; k<n; k++) {
41 mid[k] <== b[k]*c[k];
42 out[k] <== a[k] * (1 -2*b[k] -2*c[k] +4*mid[k]) + b[k] + c[k] -2*mid[k];
43 }
44}