Skip to content

Commit e7c4ec9

Browse files
authored
setup chacha circuit for NIVC folding (#63)
* chacha: add step_in as public input * fix: makefile build * format chacha circuit * update package json
1 parent 194d5d1 commit e7c4ec9

6 files changed

Lines changed: 114 additions & 114 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $(shell mkdir -p $(addsuffix /artifacts,$(TARGET_DIRS)))
1212

1313
# Default target
1414
.PHONY: all clean
15-
all: buildmak
15+
all: build params
1616

1717
# Build target
1818
.PHONY: build

builds/target_1024b/chacha20_nivc_1024.circom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ pragma circom 2.1.9;
22

33
include "../../circuits/chacha20/nivc/chacha20_nivc.circom";
44

5-
component main = ChaCha20_NIVC(256);
5+
component main { public [step_in] } = ChaCha20_NIVC(256);

builds/target_512b/chacha20_nivc_512b.circom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ pragma circom 2.1.9;
22

33
include "../../circuits/chacha20/nivc/chacha20_nivc.circom";
44

5-
component main = ChaCha20_NIVC(128);
5+
component main { public [step_in] } = ChaCha20_NIVC(128);

circuits/chacha20/nivc/chacha20_nivc.circom

Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -24,116 +24,116 @@ include "../../utils/array.circom";
2424
// +---+---+---+---+
2525
// paramaterized by n which is the number of 32-bit words to encrypt
2626
template ChaCha20_NIVC(N) {
27-
// key => 8 32-bit words = 32 bytes
28-
signal input key[8][32];
29-
// nonce => 3 32-bit words = 12 bytes
30-
signal input nonce[3][32];
31-
// counter => 32-bit word to apply w nonce
32-
signal input counter[32];
27+
// key => 8 32-bit words = 32 bytes
28+
signal input key[8][32];
29+
// nonce => 3 32-bit words = 12 bytes
30+
signal input nonce[3][32];
31+
// counter => 32-bit word to apply w nonce
32+
signal input counter[32];
3333

34-
// the below can be both ciphertext or plaintext depending on the direction
35-
// in => N 32-bit words => N 4 byte words
36-
signal input plainText[N][32];
37-
// out => N 32-bit words => N 4 byte words
38-
signal input cipherText[N][32];
34+
// the below can be both ciphertext or plaintext depending on the direction
35+
// in => N 32-bit words => N 4 byte words
36+
signal input plainText[N][32];
37+
// out => N 32-bit words => N 4 byte words
38+
signal input cipherText[N][32];
3939

40-
signal input step_in[1];
41-
signal output step_out[1];
40+
signal input step_in[1];
41+
signal output step_out[1];
4242

43-
var tmp[16][32] = [
44-
[
45-
// constant 0x61707865
46-
0, 1, 1, 0, 0, 0, 0, 1, 0,
47-
1, 1, 1, 0, 0, 0, 0, 0, 1,
48-
1, 1, 1, 0, 0, 0, 0, 1, 1,
49-
0, 0, 1, 0, 1
50-
],
51-
[
52-
// constant 0x3320646e
53-
0, 0, 1, 1, 0, 0, 1, 1, 0,
54-
0, 1, 0, 0, 0, 0, 0, 0, 1,
55-
1, 0, 0, 1, 0, 0, 0, 1, 1,
56-
0, 1, 1, 1, 0
57-
],
58-
[
59-
// constant 0x79622d32
60-
0, 1, 1, 1, 1, 0, 0, 1, 0,
61-
1, 1, 0, 0, 0, 1, 0, 0, 0,
62-
1, 0, 1, 1, 0, 1, 0, 0, 1,
63-
1, 0, 0, 1, 0
64-
],
65-
[
66-
// constant 0x6b206574
67-
0, 1, 1, 0, 1, 0, 1, 1, 0,
68-
0, 1, 0, 0, 0, 0, 0, 0, 1,
69-
1, 0, 0, 1, 0, 1, 0, 1, 1,
70-
1, 0, 1, 0, 0
71-
],
72-
key[0], key[1], key[2], key[3],
73-
key[4], key[5], key[6], key[7],
74-
counter, nonce[0], nonce[1], nonce[2]
75-
];
43+
var tmp[16][32] = [
44+
[
45+
// constant 0x61707865
46+
0, 1, 1, 0, 0, 0, 0, 1, 0,
47+
1, 1, 1, 0, 0, 0, 0, 0, 1,
48+
1, 1, 1, 0, 0, 0, 0, 1, 1,
49+
0, 0, 1, 0, 1
50+
],
51+
[
52+
// constant 0x3320646e
53+
0, 0, 1, 1, 0, 0, 1, 1, 0,
54+
0, 1, 0, 0, 0, 0, 0, 0, 1,
55+
1, 0, 0, 1, 0, 0, 0, 1, 1,
56+
0, 1, 1, 1, 0
57+
],
58+
[
59+
// constant 0x79622d32
60+
0, 1, 1, 1, 1, 0, 0, 1, 0,
61+
1, 1, 0, 0, 0, 1, 0, 0, 0,
62+
1, 0, 1, 1, 0, 1, 0, 0, 1,
63+
1, 0, 0, 1, 0
64+
],
65+
[
66+
// constant 0x6b206574
67+
0, 1, 1, 0, 1, 0, 1, 1, 0,
68+
0, 1, 0, 0, 0, 0, 0, 0, 1,
69+
1, 0, 0, 1, 0, 1, 0, 1, 1,
70+
1, 0, 1, 0, 0
71+
],
72+
key[0], key[1], key[2], key[3],
73+
key[4], key[5], key[6], key[7],
74+
counter, nonce[0], nonce[1], nonce[2]
75+
];
7676

77-
// 1 in 32-bit words
78-
signal one[32];
79-
one <== [
80-
0, 0, 0, 0, 0, 0, 0, 0,
81-
0, 0, 0, 0, 0, 0, 0, 0,
82-
0, 0, 0, 0, 0, 0, 0, 0,
83-
0, 0, 0, 0, 0, 0, 0, 1
84-
];
77+
// 1 in 32-bit words
78+
signal one[32];
79+
one <== [
80+
0, 0, 0, 0, 0, 0, 0, 0,
81+
0, 0, 0, 0, 0, 0, 0, 0,
82+
0, 0, 0, 0, 0, 0, 0, 0,
83+
0, 0, 0, 0, 0, 0, 0, 1
84+
];
8585

86-
var i = 0;
87-
var j = 0;
86+
var i = 0;
87+
var j = 0;
8888

89-
// do the ChaCha20 rounds
90-
// rounds opperates on 4 words at a time
91-
component rounds[N/16];
92-
component xors[N];
93-
component counter_adder[N/16 - 1];
89+
// do the ChaCha20 rounds
90+
// rounds opperates on 4 words at a time
91+
component rounds[N/16];
92+
component xors[N];
93+
component counter_adder[N/16 - 1];
9494

95-
signal computedCipherText[N][32];
95+
signal computedCipherText[N][32];
9696

97-
for(i = 0; i < N/16; i++) {
98-
rounds[i] = Round();
99-
rounds[i].in <== tmp;
100-
// XOR block with input
101-
for(j = 0; j < 16; j++) {
102-
xors[i*16 + j] = XorBits(32);
103-
xors[i*16 + j].a <== plainText[i*16 + j];
104-
xors[i*16 + j].b <== rounds[i].out[j];
105-
computedCipherText[i*16 + j] <== xors[i*16 + j].out;
106-
}
97+
for(i = 0; i < N/16; i++) {
98+
rounds[i] = Round();
99+
rounds[i].in <== tmp;
100+
// XOR block with input
101+
for(j = 0; j < 16; j++) {
102+
xors[i*16 + j] = XorBits(32);
103+
xors[i*16 + j].a <== plainText[i*16 + j];
104+
xors[i*16 + j].b <== rounds[i].out[j];
105+
computedCipherText[i*16 + j] <== xors[i*16 + j].out;
106+
}
107107

108-
if(i < N/16 - 1) {
109-
counter_adder[i] = AddBits(32);
110-
counter_adder[i].a <== tmp[12];
111-
counter_adder[i].b <== one;
108+
if(i < N/16 - 1) {
109+
counter_adder[i] = AddBits(32);
110+
counter_adder[i].a <== tmp[12];
111+
counter_adder[i].b <== one;
112112

113-
// increment the counter
114-
tmp[12] = counter_adder[i].out;
115-
}
116-
}
113+
// increment the counter
114+
tmp[12] = counter_adder[i].out;
115+
}
116+
}
117117

118-
signal ciphertext_equal_check[N][32];
119-
for(var i = 0 ; i < N; i++) {
120-
for(var j = 0 ; j < 32 ; j++) {
121-
ciphertext_equal_check[i][j] <== IsEqual()([computedCipherText[i][j], cipherText[i][j]]);
122-
ciphertext_equal_check[i][j] === 1;
123-
}
118+
signal ciphertext_equal_check[N][32];
119+
for(var i = 0 ; i < N; i++) {
120+
for(var j = 0 ; j < 32 ; j++) {
121+
ciphertext_equal_check[i][j] <== IsEqual()([computedCipherText[i][j], cipherText[i][j]]);
122+
ciphertext_equal_check[i][j] === 1;
124123
}
124+
}
125125

126-
component toBytes[N];
127-
signal bigEndianPlaintext[N*4];
128-
for(var i = 0 ; i < N; i++) {
129-
toBytes[i] = fromLittleEndianToWords32();
130-
for(var j = 0 ; j < 32 ; j++) {
131-
toBytes[i].data[j] <== plainText[i][j];
132-
}
133-
for(var j = 0; j < 4; j++) {
134-
bigEndianPlaintext[i*4 + j] <== toBytes[i].words[j];
135-
}
126+
component toBytes[N];
127+
signal bigEndianPlaintext[N*4];
128+
for(var i = 0 ; i < N; i++) {
129+
toBytes[i] = fromLittleEndianToWords32();
130+
for(var j = 0 ; j < 32 ; j++) {
131+
toBytes[i].data[j] <== plainText[i][j];
132+
}
133+
for(var j = 0; j < 4; j++) {
134+
bigEndianPlaintext[i*4 + j] <== toBytes[i].words[j];
136135
}
137-
signal data_hash <== DataHasher(N*4)(bigEndianPlaintext);
138-
step_out[0] <== data_hash;
136+
}
137+
signal data_hash <== DataHasher(N*4)(bigEndianPlaintext);
138+
step_out[0] <== data_hash;
139139
}

circuits/test/full/full.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ const http_response_plaintext = [
4141
10, 32, 32, 32, 125, 13, 10, 125];
4242

4343
const chacha20_http_response_ciphertext = [
44-
2,125,219,141,140,93,49,129,95,178,135,109,48,36,194,46,239,155,160,70,208,147,37,212,17,195,149,
45-
190,38,215,23,241,84,204,167,184,179,172,187,145,38,75,123,96,81,6,149,36,135,227,226,254,177,90,
46-
241,159,0,230,183,163,210,88,133,176,9,122,225,83,171,157,185,85,122,4,110,52,2,90,36,189,145,63,
47-
122,75,94,21,163,24,77,85,110,90,228,157,103,41,59,128,233,149,57,175,121,163,185,144,162,100,17,
48-
34,9,252,162,223,59,221,106,127,104,11,121,129,154,49,66,220,65,130,171,165,43,8,21,248,12,214,33,
49-
6,109,3,144,52,124,225,206,223,213,86,186,93,170,146,141,145,140,57,152,226,218,57,30,4,131,161,0,
50-
248,172,49,206,181,47,231,87,72,96,139,145,117,45,77,134,249,71,87,178,239,30,244,156,70,118,180,
51-
176,90,92,80,221,177,86,120,222,223,244,109,150,226,142,97,171,210,38,117,143,163,204,25,223,238,
52-
209,58,59,100,1,86,241,103,152,228,37,187,79,36,136,133,171,41,184,145,146,45,192,173,219,146,133,
53-
12,246,190,5,54,99,155,8,198,156,174,99,12,210,95,5,128,166,118,50,66,26,20,3,129,232,1,192,104,
54-
23,152,212,94,97,138,162,90,185,108,221,211,247,184,253,15,16,24,32,240,240,3,148,89,30,54,161,
55-
131,230,161,217,29,229,251,33,220,230,102,131,245,27,141,220,67,16,26
44+
2, 125, 219, 141, 140, 93, 49, 129, 95, 178, 135, 109, 48, 36, 194, 46, 239, 155, 160, 70, 208, 147, 37, 212, 17, 195, 149,
45+
190, 38, 215, 23, 241, 84, 204, 167, 184, 179, 172, 187, 145, 38, 75, 123, 96, 81, 6, 149, 36, 135, 227, 226, 254, 177, 90,
46+
241, 159, 0, 230, 183, 163, 210, 88, 133, 176, 9, 122, 225, 83, 171, 157, 185, 85, 122, 4, 110, 52, 2, 90, 36, 189, 145, 63,
47+
122, 75, 94, 21, 163, 24, 77, 85, 110, 90, 228, 157, 103, 41, 59, 128, 233, 149, 57, 175, 121, 163, 185, 144, 162, 100, 17,
48+
34, 9, 252, 162, 223, 59, 221, 106, 127, 104, 11, 121, 129, 154, 49, 66, 220, 65, 130, 171, 165, 43, 8, 21, 248, 12, 214, 33,
49+
6, 109, 3, 144, 52, 124, 225, 206, 223, 213, 86, 186, 93, 170, 146, 141, 145, 140, 57, 152, 226, 218, 57, 30, 4, 131, 161, 0,
50+
248, 172, 49, 206, 181, 47, 231, 87, 72, 96, 139, 145, 117, 45, 77, 134, 249, 71, 87, 178, 239, 30, 244, 156, 70, 118, 180,
51+
176, 90, 92, 80, 221, 177, 86, 120, 222, 223, 244, 109, 150, 226, 142, 97, 171, 210, 38, 117, 143, 163, 204, 25, 223, 238,
52+
209, 58, 59, 100, 1, 86, 241, 103, 152, 228, 37, 187, 79, 36, 136, 133, 171, 41, 184, 145, 146, 45, 192, 173, 219, 146, 133,
53+
12, 246, 190, 5, 54, 99, 155, 8, 198, 156, 174, 99, 12, 210, 95, 5, 128, 166, 118, 50, 66, 26, 20, 3, 129, 232, 1, 192, 104,
54+
23, 152, 212, 94, 97, 138, 162, 90, 185, 108, 221, 211, 247, 184, 253, 15, 16, 24, 32, 240, 240, 3, 148, 89, 30, 54, 161,
55+
131, 230, 161, 217, 29, 229, 251, 33, 220, 230, 102, 131, 245, 27, 141, 220, 67, 16, 26
5656
];
5757

5858
const aes_http_response_ciphertext = [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "web-prover-circuits",
33
"description": "ZK Circuits for WebProofs",
4-
"version": "0.5.8",
4+
"version": "0.5.9",
55
"license": "Apache-2.0",
66
"repository": {
77
"type": "git",

0 commit comments

Comments
 (0)