1
1
// from: https://github.com/crema-labs/aes-circom/tree/main/circuits
2
2
pragma circom 2.1 .9 ;
3
3
4
- include " utils.circom" ;
4
+ include " ../utils.circom" ;
5
+ include " sbox128.circom" ;
6
+
5
7
6
8
// Key Expansion Process
7
9
//
@@ -107,23 +109,72 @@ template NextRound(){
107
109
];
108
110
rcon.index <== round- 1 ;
109
111
component xorWord[4 + 1 ];
110
- xorWord[0 ] = XorWord( );
111
- xorWord[0 ].bytes1 <== substituteWord[0 ].substituted;
112
- xorWord[0 ].bytes2 <== rcon.out;
112
+ xorWord[0 ] = XORBLOCK( 4 );
113
+ xorWord[0 ].a <== substituteWord[0 ].substituted;
114
+ xorWord[0 ].b <== rcon.out;
113
115
114
116
for (var i = 0 ; i < 4 ; i++ ) {
115
- xorWord[i+ 1 ] = XorWord( );
117
+ xorWord[i+ 1 ] = XORBLOCK( 4 );
116
118
if (i == 0 ) {
117
- xorWord[i+ 1 ].bytes1 <== xorWord[0 ].out;
119
+ xorWord[i+ 1 ].a <== xorWord[0 ].out;
118
120
} else {
119
- xorWord[i+ 1 ].bytes1 <== nextKey[i- 1 ];
121
+ xorWord[i+ 1 ].a <== nextKey[i- 1 ];
120
122
}
121
- xorWord[i+ 1 ].bytes2 <== key[i];
123
+ xorWord[i+ 1 ].b <== key[i];
122
124
123
125
for (var j = 0 ; j < 4 ; j++ ) {
124
126
nextKey[i][j] <== xorWord[i+ 1 ].out[j];
125
127
}
126
128
}
127
129
}
128
130
131
+ // Outputs a round constant for a given round number
132
+ template RCon (round ) {
133
+ signal output out[4 ];
134
+
135
+ assert(round > 0 && round <= 10 );
129
136
137
+ var rcon[10 ][4 ] = [
138
+ [0x01 , 0x00 , 0x00 , 0x00 ],
139
+ [0x02 , 0x00 , 0x00 , 0x00 ],
140
+ [0x04 , 0x00 , 0x00 , 0x00 ],
141
+ [0x08 , 0x00 , 0x00 , 0x00 ],
142
+ [0x10 , 0x00 , 0x00 , 0x00 ],
143
+ [0x20 , 0x00 , 0x00 , 0x00 ],
144
+ [0x40 , 0x00 , 0x00 , 0x00 ],
145
+ [0x80 , 0x00 , 0x00 , 0x00 ],
146
+ [0x1b , 0x00 , 0x00 , 0x00 ],
147
+ [0x36 , 0x00 , 0x00 , 0x00 ]
148
+ ];
149
+
150
+ out <== rcon[round- 1 ];
151
+ }
152
+
153
+ // Rotates an array of bytes to the left by a specified rotation
154
+ template Rotate (rotation, length ) {
155
+ assert(rotation < length);
156
+ signal input bytes[length];
157
+ signal output rotated[length];
158
+
159
+ for (var i = 0 ; i < length - rotation; i++ ) {
160
+ rotated[i] <== bytes[i + rotation];
161
+ }
162
+
163
+ for (var i = length - rotation; i < length; i++ ) {
164
+ rotated[i] <== bytes[i - length + rotation];
165
+ }
166
+ }
167
+
168
+ // Substitutes each byte in a word using the S-Box
169
+ template SubstituteWord () {
170
+ signal input bytes[4 ];
171
+ signal output substituted[4 ];
172
+
173
+ component sbox[4 ];
174
+
175
+ for (var i = 0 ; i < 4 ; i++ ) {
176
+ sbox[i] = SBox128();
177
+ sbox[i].in <== bytes[i];
178
+ substituted[i] <== sbox[i].out;
179
+ }
180
+ }
0 commit comments