| 1 |
// https://raw.githubusercontent.com/iden3/circomlib/master/circuits/eddsa.circom |
| 2 |
/* |
| 3 |
Copyright 2018 0KIMS association. |
| 4 |
|
| 5 |
This file is part of circom (Zero Knowledge Circuit Compiler). |
| 6 |
|
| 7 |
circom is a free software: you can redistribute it and/or modify it |
| 8 |
under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation, either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
circom is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with circom. If not, see <https://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
pragma circom 2.0.0; |
| 21 |
|
| 22 |
include "compconstant.circom"; |
| 23 |
include "pointbits.circom"; |
| 24 |
include "pedersen.circom"; |
| 25 |
include "escalarmulany.circom"; |
| 26 |
include "escalarmulfix.circom"; |
| 27 |
|
| 28 |
template EdDSAVerifier(n) { |
| 29 |
signal input msg[n]; |
| 30 |
|
| 31 |
signal input A[256]; |
| 32 |
signal input R8[256]; |
| 33 |
signal input S[256]; |
| 34 |
|
| 35 |
signal Ax; |
| 36 |
signal Ay; |
| 37 |
|
| 38 |
signal R8x; |
| 39 |
signal R8y; |
| 40 |
|
| 41 |
var i; |
| 42 |
|
| 43 |
// Ensure S<Subgroup Order |
| 44 |
|
| 45 |
component compConstant = CompConstant(2736030358979909402780800718157159386076813972158567259200215660948447373040); |
| 46 |
|
| 47 |
for (i=0; i<254; i++) { |
| 48 |
S[i] ==> compConstant.in[i]; |
| 49 |
} |
| 50 |
compConstant.out === 0; |
| 51 |
S[254] === 0; |
| 52 |
S[255] === 0; |
| 53 |
|
| 54 |
// Convert A to Field elements (And verify A) |
| 55 |
|
| 56 |
component bits2pointA = Bits2Point_Strict(); |
| 57 |
|
| 58 |
for (i=0; i<256; i++) { |
| 59 |
bits2pointA.in[i] <== A[i]; |
| 60 |
} |
| 61 |
Ax <== bits2pointA.out[0]; |
| 62 |
Ay <== bits2pointA.out[1]; |
| 63 |
|
| 64 |
// Convert R8 to Field elements (And verify R8) |
| 65 |
|
| 66 |
component bits2pointR8 = Bits2Point_Strict(); |
| 67 |
|
| 68 |
for (i=0; i<256; i++) { |
| 69 |
bits2pointR8.in[i] <== R8[i]; |
| 70 |
} |
| 71 |
R8x <== bits2pointR8.out[0]; |
| 72 |
R8y <== bits2pointR8.out[1]; |
| 73 |
|
| 74 |
// Calculate the h = H(R,A, msg) |
| 75 |
|
| 76 |
component hash = Pedersen(512+n); |
| 77 |
|
| 78 |
for (i=0; i<256; i++) { |
| 79 |
hash.in[i] <== R8[i]; |
| 80 |
hash.in[256+i] <== A[i]; |
| 81 |
} |
| 82 |
for (i=0; i<n; i++) { |
| 83 |
hash.in[512+i] <== msg[i]; |
| 84 |
} |
| 85 |
|
| 86 |
component point2bitsH = Point2Bits_Strict(); |
| 87 |
point2bitsH.in[0] <== hash.out[0]; |
| 88 |
point2bitsH.in[1] <== hash.out[1]; |
| 89 |
|
| 90 |
// Calculate second part of the right side: right2 = h*8*A |
| 91 |
|
| 92 |
// Multiply by 8 by adding it 3 times. This also ensure that the result is in |
| 93 |
// the subgroup. |
| 94 |
component dbl1 = BabyDbl(); |
| 95 |
dbl1.x <== Ax; |
| 96 |
dbl1.y <== Ay; |
| 97 |
component dbl2 = BabyDbl(); |
| 98 |
dbl2.x <== dbl1.xout; |
| 99 |
dbl2.y <== dbl1.yout; |
| 100 |
component dbl3 = BabyDbl(); |
| 101 |
dbl3.x <== dbl2.xout; |
| 102 |
dbl3.y <== dbl2.yout; |
| 103 |
|
| 104 |
// We check that A is not zero. |
| 105 |
component isZero = IsZero(); |
| 106 |
isZero.in <== dbl3.x; |
| 107 |
isZero.out === 0; |
| 108 |
|
| 109 |
component mulAny = EscalarMulAny(256); |
| 110 |
for (i=0; i<256; i++) { |
| 111 |
mulAny.e[i] <== point2bitsH.out[i]; |
| 112 |
} |
| 113 |
mulAny.p[0] <== dbl3.xout; |
| 114 |
mulAny.p[1] <== dbl3.yout; |
| 115 |
|
| 116 |
|
| 117 |
// Compute the right side: right = R8 + right2 |
| 118 |
|
| 119 |
component addRight = BabyAdd(); |
| 120 |
addRight.x1 <== R8x; |
| 121 |
addRight.y1 <== R8y; |
| 122 |
addRight.x2 <== mulAny.out[0]; |
| 123 |
addRight.y2 <== mulAny.out[1]; |
| 124 |
|
| 125 |
// Calculate left side of equation left = S*B8 |
| 126 |
|
| 127 |
var BASE8[2] = [ |
| 128 |
5299619240641551281634865583518297030282874472190772894086521144482721001553, |
| 129 |
16950150798460657717958625567821834550301663161624707787222815936182638968203 |
| 130 |
]; |
| 131 |
component mulFix = EscalarMulFix(256, BASE8); |
| 132 |
for (i=0; i<256; i++) { |
| 133 |
mulFix.e[i] <== S[i]; |
| 134 |
} |
| 135 |
|
| 136 |
// Do the comparation left == right |
| 137 |
|
| 138 |
mulFix.out[0] === addRight.xout; |
| 139 |
mulFix.out[1] === addRight.yout; |
| 140 |
} |