Skip to content

Commit ac209d7

Browse files
Autoparallel0xJepsendevloperlonerapier
authored
fix: witnesscalc build (#91)
* `aes-gcm` compiles * removing consts * stash pop * checkpoint * uncommented in main template Now the issues seem to only be in GCTR and KeyExpansion * unravelled a new error * this is working still, fold is not * FIXED KEY EXPANSION * VERY CLOSE * this still works * this still works * it works now YOU CANNOT HAVE A LOOP THAT EVER HAS A 0 SIZE OR ELSE THIS THING POOPS * if logic to fix STUPID witcalc * few notes * update aes-gcm-fold.circom * Update ff.circom * MAKE `FieldInv` fast af boi * improve FieldInv test * Fixup the tests * fix tests * rm unnecessary diffs --------- Signed-off-by: Thor Kamphefner <thor@pluto.xyz> Signed-off-by: Waylon Jepsen <waylonjepsen1@gmail.com> Co-authored-by: Waylon Jepsen <waylonjepsen1@gmail.com> Co-authored-by: devloper <devloper@users.noreply.github.com> Co-authored-by: Sambhav Dusad <lonerapier@proton.me>
1 parent f0b1dcc commit ac209d7

13 files changed

+247
-309
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ client/static/build
1111
circuits/test/**/*.circom
1212
circuits/test/*.circom
1313
circuits/main/*
14+
ir_log/*
15+
log_input_signals.txt
16+
*.bin

circuits/aes-gcm/aes-gcm-fold.circom

+16-24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pragma circom 2.1.9;
22

33
include "./aes-gcm-foldable.circom";
44

5+
// Compute AES-GCM
56
template AESGCMFOLD(bytesPerFold, totalBytes) {
67
// cannot fold outside chunk boundaries.
78
assert(bytesPerFold % 16 == 0);
@@ -14,47 +15,38 @@ template AESGCMFOLD(bytesPerFold, totalBytes) {
1415

1516
// Output from the last encryption step
1617
// Always use last bytes for inputs which are not same size.
17-
// step_in[0] => lastCounter
18-
// step_in[1] => lastTag
19-
// step_in[2] => foldedBlocks
20-
signal input step_in[48];
18+
// step_in[0..4] => lastCounter
19+
// step_in[4..20] => lastTag
20+
// step_in[20] => foldedBlocks
21+
signal input step_in[21];
2122

2223
// For now, attempt to support variable fold size. Potential fix at 16 in the future.
2324
component aes = AESGCMFOLDABLE(bytesPerFold, totalBytes\16);
24-
aes.key <== key;
25-
aes.iv <== iv;
26-
aes.aad <== aad;
25+
aes.key <== key;
26+
aes.iv <== iv;
27+
aes.aad <== aad;
2728
aes.plainText <== plainText;
2829

2930
// Fold inputs
30-
var inputIndex = bytesPerFold-4;
3131
for(var i = 0; i < 4; i++) {
32-
aes.lastCounter[i] <== step_in[inputIndex];
33-
inputIndex+=1;
32+
aes.lastCounter[i] <== step_in[i];
3433
}
35-
3634
for(var i = 0; i < 16; i++) {
37-
aes.lastTag[i] <== step_in[inputIndex];
38-
inputIndex+=1;
35+
aes.lastTag[i] <== step_in[4 + i];
3936
}
4037
// TODO: range check, assertions, stuff.
41-
inputIndex+=15;
42-
aes.foldedBlocks <== step_in[inputIndex];
38+
aes.foldedBlocks <== step_in[20];
4339

4440
// Fold Outputs
45-
signal output step_out[48];
46-
var outputIndex = bytesPerFold-4;
41+
signal output step_out[21];
4742
for(var i = 0; i < 4; i++) {
48-
step_out[outputIndex] <== aes.counter[i];
49-
outputIndex+=1;
43+
step_out[i] <== aes.counter[i];
5044
}
5145
for(var i = 0; i < 16; i++) {
52-
step_out[outputIndex] <== aes.authTag[i];
53-
outputIndex+=1;
46+
step_out[4 + i] <== aes.authTag[i];
5447
}
55-
outputIndex+=15;
56-
step_out[outputIndex] <== step_in[inputIndex] + bytesPerFold \ 16;
48+
step_out[20] <== step_in[20] + bytesPerFold \ 16;
5749

5850
signal output authTag[16] <== aes.authTag;
5951
signal output cipherText[bytesPerFold] <== aes.cipherText;
60-
}
52+
}

circuits/aes-gcm/aes-gcm-foldable.circom

+34-27
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ template AESGCMFOLDABLE(l, TOTAL_BLOCKS) {
4949
}
5050

5151
// Step 1: Let H = CIPHK(0128)
52-
component cipherH = Cipher(4); // 128-bit key -> 4 32-bit words -> 10 rounds
52+
component cipherH = Cipher(); // 128-bit key -> 4 32-bit words -> 10 rounds
5353
cipherH.key <== key;
5454
cipherH.block <== zeroBlock.blocks[0];
5555

@@ -75,7 +75,7 @@ template AESGCMFOLDABLE(l, TOTAL_BLOCKS) {
7575
J0[3] <== J0WordIncrementer.out;
7676

7777
// Step 3: Let C = GCTRK(inc32(J0), P)
78-
component gctr = GCTR(l, 4);
78+
component gctr = GCTR(l);
7979
gctr.key <== key;
8080
gctr.initialCounterBlock <== J0;
8181
gctr.plainText <== plainText;
@@ -89,15 +89,16 @@ template AESGCMFOLDABLE(l, TOTAL_BLOCKS) {
8989
// len(A) => u64
9090
// len(b) => u64 (together, 1 block)
9191
//
92-
var blockCount = l\16 + (l%16 > 0 ? 1 : 0); // blocksize is 16 bytes
92+
var blockCount = l\16 + (l%16 > 0 ? 1 : 0); // blocksize is 16 bytes
9393
var ghashBlocks = 1 + blockCount + 1;
9494

95-
component targetMode = SelectGhashMode(TOTAL_BLOCKS, blockCount, ghashBlocks);
95+
component targetMode = SelectGhashMode(TOTAL_BLOCKS, blockCount, ghashBlocks);
9696
targetMode.foldedBlocks <== foldedBlocks;
9797

98+
// TODO(CR 2024-10-18): THIS BLOCK IS PROBLEM CHILD SO FAR
9899
// S = GHASHH (A || 0^v || C || 0^u || [len(A)] || [len(C)]).
99100
component selectedBlocks = SelectGhashBlocks(l, ghashBlocks, TOTAL_BLOCKS);
100-
selectedBlocks.aad <== aad;
101+
selectedBlocks.aad <== aad;
101102
selectedBlocks.cipherText <== gctr.cipherText;
102103
selectedBlocks.targetMode <== targetMode.mode;
103104

@@ -136,7 +137,7 @@ template AESGCMFOLDABLE(l, TOTAL_BLOCKS) {
136137
}
137138

138139
// Step 6: Encrypt the tag. Let T = MSBt(GCTRK(J0, S))
139-
component gctrT = GCTR(16, 4);
140+
component gctrT = GCTR(16);
140141
gctrT.key <== key;
141142
gctrT.initialCounterBlock <== StartJ0.blocks[0];
142143
gctrT.plainText <== selectTag.tag;
@@ -171,30 +172,30 @@ template SelectGhashBlocks(l, ghashBlocks, totalBlocks) {
171172
signal targetBlocks[3][ghashBlocks*4*4];
172173
signal modeToBlocks[4] <== [0, 0, 1, 2];
173174

174-
component start = GhashStartMode(l, totalBlocks, ghashBlocks);
175-
start.aad <== aad;
175+
component start = GhashStartMode(l, totalBlocks, ghashBlocks);
176+
start.aad <== aad;
176177
start.cipherText <== cipherText;
177-
targetBlocks[0] <== start.blocks;
178+
targetBlocks[0] <== start.blocks;
178179

179-
component stream = GhashStreamMode(l, ghashBlocks);
180+
component stream = GhashStreamMode(l, ghashBlocks);
180181
stream.cipherText <== cipherText;
181-
targetBlocks[1] <== stream.blocks;
182+
targetBlocks[1] <== stream.blocks;
182183

183-
component end = GhashEndMode(l, totalBlocks, ghashBlocks);
184-
end.cipherText <== cipherText;
184+
component end = GhashEndMode(l, totalBlocks, ghashBlocks);
185+
end.cipherText <== cipherText;
185186
targetBlocks[2] <== end.blocks;
186187

187188
component mapModeToArray = Selector(4);
188-
mapModeToArray.in <== modeToBlocks;
189-
mapModeToArray.index <== targetMode;
189+
mapModeToArray.in <== modeToBlocks;
190+
mapModeToArray.index <== targetMode;
190191

191192
component chooseBlocks = ArraySelector(3, ghashBlocks*4*4);
192-
chooseBlocks.in <== targetBlocks;
193-
chooseBlocks.index <== mapModeToArray.out;
193+
chooseBlocks.in <== targetBlocks;
194+
chooseBlocks.index <== mapModeToArray.out;
194195

195196
component toBlocks = ToBlocks(ghashBlocks*4*4);
196-
toBlocks.stream <== chooseBlocks.out;
197-
blocks <== toBlocks.blocks;
197+
toBlocks.stream <== chooseBlocks.out;
198+
blocks <== toBlocks.blocks;
198199
}
199200

200201
template SelectGhashTag(ghashBlocks) {
@@ -230,10 +231,10 @@ template SelectGhashMode(totalBlocks, blocksPerFold, ghashBlocks) {
230231
// May need to compute these differently due to foldedBlocks.
231232
// i.e. using GT operator, Equal operator, etc.
232233
signal isFinish <-- (blocksPerFold >= totalBlocks-foldedBlocks) ? 1 : 0;
233-
signal isStart <-- (foldedBlocks == 0) ? 1: 0;
234+
signal isStart <-- (foldedBlocks == 0) ? 1: 0;
234235

235236
isFinish * (isFinish - 1) === 0;
236-
isStart * (isStart - 1) === 0;
237+
isStart * (isStart - 1) === 0;
237238

238239
// case isStart && isFinish: START_END_MODE
239240
// case isStart && !isFinish: START_MODE
@@ -247,9 +248,9 @@ template SelectGhashMode(totalBlocks, blocksPerFold, ghashBlocks) {
247248
choice.s <== [isStart, isFinish];
248249

249250
signal isStartEndMode <== IsEqual()([choice.out, m.START_END_MODE]);
250-
signal isStartMode <== IsEqual()([choice.out, m.START_MODE]);
251-
signal isStreamMode <== IsEqual()([choice.out, m.STREAM_MODE]);
252-
signal isEndMode <== IsEqual()([choice.out, m.END_MODE]);
251+
signal isStartMode <== IsEqual()([choice.out, m.START_MODE]);
252+
signal isStreamMode <== IsEqual()([choice.out, m.STREAM_MODE]);
253+
signal isEndMode <== IsEqual()([choice.out, m.END_MODE]);
253254

254255
isStartEndMode + isStartMode + isStreamMode + isEndMode === 1;
255256

@@ -294,7 +295,8 @@ template GhashStartMode(l, totalBlocks, ghashBlocks) {
294295
// Insert in reversed (big endian) order.
295296
blocks[blockIndex+7-i] <== byteValue;
296297
}
297-
blockIndex+=8;
298+
// 16 + l + 8 + 8
299+
blockIndex+=8; // TODO(CR 2024-10-18): I don't think this does anything
298300
}
299301

300302
// TODO: Mildly more efficient if we add this, maybe it's needed?
@@ -347,5 +349,10 @@ template GhashEndMode(l, totalBlocks, ghashBlocks) {
347349
// Insert in reversed (big endian) order.
348350
blocks[blockIndex+7-i] <== byte_value;
349351
}
350-
blockIndex+=8;
351-
}
352+
blockIndex+=8;
353+
// NOTE: Added this so all of blocks is written
354+
for (var i = 0; i<16; i++) {
355+
blocks[blockIndex] <== 0;
356+
blockIndex += 1;
357+
}
358+
}

circuits/aes-gcm/aes-gcm.circom

+5-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ template AESGCM(l) {
3939
}
4040

4141
// Step 1: Let H = CIPHK(0128)
42-
component cipherH = Cipher(4); // 128-bit key -> 4 32-bit words -> 10 rounds
42+
component cipherH = Cipher(); // 128-bit key -> 4 32-bit words -> 10 rounds
4343
cipherH.key <== key;
4444
cipherH.block <== zeroBlock.blocks[0];
4545

@@ -65,11 +65,12 @@ template AESGCM(l) {
6565
J0[3] <== J0WordIncrementer2.out;
6666

6767
// Step 3: Let C = GCTRK(inc32(J0), P)
68-
component gctr = GCTR(l, 4);
68+
component gctr = GCTR(l);
6969
gctr.key <== key;
7070
gctr.initialCounterBlock <== J0;
7171
gctr.plainText <== plainText;
7272

73+
7374
// Step 4: Let u and v
7475
var u = 128 * (l \ 128) - l;
7576
// when we handle dynamic aad lengths, we'll need to change this
@@ -156,11 +157,11 @@ template AESGCM(l) {
156157
// log("end ghash bytes");
157158

158159
// Step 6: Let T = MSBt(GCTRK(J0, S))
159-
component gctrT = GCTR(16, 4);
160+
component gctrT = GCTR(16);
160161
gctrT.key <== key;
161162
gctrT.initialCounterBlock <== J0;
162163
gctrT.plainText <== bytes;
163164

164165
authTag <== gctrT.cipherText;
165166
cipherText <== gctr.cipherText;
166-
}
167+
}

circuits/aes-gcm/aes/cipher.circom

+19-19
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ include "mix_columns.circom";
3030
//
3131
// Ciphertext
3232

33+
3334
// @param nk: number of keys which can be 4, 6, 8
3435
// @inputs block: 4x4 matrix representing the input block
3536
// @inputs key: array of nk*4 bytes representing the key
3637
// @outputs cipher: 4x4 matrix representing the output block
37-
template Cipher(nk){
38-
assert(nk == 4 || nk == 6 || nk == 8 );
38+
template Cipher(){
3939
signal input block[4][4];
40-
signal input key[nk * 4];
40+
signal input key[16];
4141
signal output cipher[4][4];
4242

43-
var nr = Rounds(nk);
43+
// var nr = Rounds(nk);
4444

45-
component keyExpansion = KeyExpansion(nk,nr);
45+
component keyExpansion = KeyExpansion();
4646
keyExpansion.key <== key;
4747

48-
component addRoundKey[nr+1];
49-
component subBytes[nr];
50-
component shiftRows[nr];
51-
component mixColumns[nr-1];
48+
component addRoundKey[11];
49+
component subBytes[10];
50+
component shiftRows[10];
51+
component mixColumns[9];
5252

53-
signal interBlock[nr][4][4];
53+
signal interBlock[10][4][4];
5454

5555
addRoundKey[0] = AddRoundKey();
5656
addRoundKey[0].state <== block;
@@ -59,7 +59,7 @@ template Cipher(nk){
5959
}
6060

6161
interBlock[0] <== addRoundKey[0].newState;
62-
for (var i = 1; i < nr; i++) {
62+
for (var i = 1; i < 10; i++) {
6363
subBytes[i-1] = SubBlock();
6464
subBytes[i-1].state <== interBlock[i-1];
6565

@@ -78,19 +78,19 @@ template Cipher(nk){
7878
interBlock[i] <== addRoundKey[i].newState;
7979
}
8080

81-
subBytes[nr-1] = SubBlock();
82-
subBytes[nr-1].state <== interBlock[nr-1];
81+
subBytes[9] = SubBlock();
82+
subBytes[9].state <== interBlock[9];
8383

84-
shiftRows[nr-1] = ShiftRows();
85-
shiftRows[nr-1].state <== subBytes[nr-1].newState;
84+
shiftRows[9] = ShiftRows();
85+
shiftRows[9].state <== subBytes[9].newState;
8686

87-
addRoundKey[nr] = AddRoundKey();
88-
addRoundKey[nr].state <== shiftRows[nr-1].newState;
87+
addRoundKey[10] = AddRoundKey();
88+
addRoundKey[10].state <== shiftRows[9].newState;
8989
for (var i = 0; i < 4; i++) {
90-
addRoundKey[nr].roundKey[i] <== keyExpansion.keyExpanded[i + (nr * 4)];
90+
addRoundKey[10].roundKey[i] <== keyExpansion.keyExpanded[i + (40)];
9191
}
9292

93-
cipher <== addRoundKey[nr].newState;
93+
cipher <== addRoundKey[10].newState;
9494
}
9595

9696
// @param nk: number of keys which can be 4, 6, 8

0 commit comments

Comments
 (0)