Skip to content

Commit

Permalink
chore: repo cleaning, remove unused code paths (#84)
Browse files Browse the repository at this point in the history
* explicitly include circomlib. cloned and installed circomlib in circuits directory. Fixes circom-lsp path issues.

* silence logs in tests

* fix or rm deprecated or broken tests

* fix or rm deprecated or broken tests 2

* comment out slow tests

* rm circomlib

* checkpoint

* fix paths

* checkoutpoint

* aes tests

* common tests

* GHASH test cleanup

* hash test cleanup

* crema labs attribution

* contributor broken link

* remove client directory

---------

Co-authored-by: Waylon Jepsen <[email protected]>
  • Loading branch information
thor314 and 0xJepsen authored Sep 23, 2024
1 parent 7ad6b0c commit 06fdebf
Show file tree
Hide file tree
Showing 63 changed files with 1,112 additions and 14,325 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ target
/.vscode


circomlib
node_modules/*

build/*
ptau/*
client/node_modules
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</h1>

<div align="center">
<a href="https://github.com/pluto/aes-gcm-circom/graphs/contributors">
<a href="https://github.com/pluto/aes-proof/graphs/contributors">
<img src="https://img.shields.io/github/contributors/pluto/aes-gcm-circom?style=flat-square&logo=github&logoColor=8b949e&labelColor=282f3b&color=32c955" alt="Contributors" />
</a>
<a href="https://github.com/pluto/aes-gcm-circom/actions/workflows/test.yaml">
Expand Down
12 changes: 0 additions & 12 deletions circuits.json

This file was deleted.

144 changes: 0 additions & 144 deletions circuits/aes-ctr/ctr.circom

This file was deleted.

1 change: 0 additions & 1 deletion circuits/aes-gcm/README.md

This file was deleted.

13 changes: 7 additions & 6 deletions circuits/aes-gcm/aes-gcm.circom
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
pragma circom 2.1.9;

include "../aes-ctr/ctr.circom";
include "ghash.circom";
include "../aes-ctr/cipher.circom";
include "aes/cipher.circom";
include "circomlib/circuits/bitify.circom";
include "utils.circom";
include "gctr.circom";
include "helper_functions.circom";


/// AES-GCM with 128 bit key authenticated encryption according to: https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
Expand Down Expand Up @@ -119,15 +117,18 @@ template AESGCM(l) {
byte_value += (len >> i*8+j) & 1;
}
ghashMessage[ghashblocks-1][i\4+2][i%4] <== byte_value;
// TODO: Probably need to check exact value.
}

// Step 5: Define a block, S
// needs to take in the number of blocks
component ghash = GHASH(ghashblocks);
ghash.HashKey <== cipherH.cipher;
component hashKeyToStream = ToStream(16, 16);
hashKeyToStream.bl <== cipherH.cipher;
ghash.HashKey <== hashKeyToStream.out;
// S = GHASHH (A || 0^v || C || 0^u || [len(A)] || [len(C)]).
ghash.msg <== ghashMessage; // TODO(WJ 2024-09-16): this is wrong
component msgToStream = ToStream(ghashblocks, 16);
msgToStream.blocks <== ghashMessage;
ghash.msg <== msgToStream.stream;
// In Steps 4 and 5, the AAD and the ciphertext are each appended with the minimum number of
// ‘0’ bits, possibly none, so that the bit lengths of the resulting strings are multiples of the block
// size. The concatenation of these strings is appended with the 64-bit representations of the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// from: https://github.com/crema-labs/aes-circom/tree/main/circuits
pragma circom 2.1.9;

include "key_expansion.circom";
Expand Down Expand Up @@ -103,4 +104,6 @@ function Rounds (nk) {
} else {
return 14;
}
}
}


Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// from: https://github.com/crema-labs/aes-circom/tree/main/circuits
pragma circom 2.1.9;

include "sbox128.circom";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// from: https://github.com/crema-labs/aes-circom/tree/main/circuits
pragma circom 2.1.9;

include "transformations.circom";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// from: https://github.com/crema-labs/aes-circom/tree/main/circuits
pragma circom 2.1.9;

include "circomlib/circuits/comparators.circom";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// from: https://github.com/crema-labs/aes-circom/tree/main/circuits
pragma circom 2.1.9;

include "transformations.circom";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// from: https://github.com/crema-labs/aes-circom/tree/main/circuits
pragma circom 2.1.9;

include "circomlib/circuits/comparators.circom";
Expand Down
118 changes: 91 additions & 27 deletions circuits/aes-ctr/utils.circom → circuits/aes-gcm/aes/utils.circom
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// from: https://github.com/crema-labs/aes-circom/tree/main/circuits
pragma circom 2.1.9;

include "sbox128.circom";
Expand Down Expand Up @@ -123,39 +124,102 @@ template MulByte(){
}
}

// XORs two bytes
template XorByte(){
signal input a;
signal input b;
signal output out;

component abits = Num2Bits(8);
abits.in <== a;
//convert stream of plain text to blocks of 16 bytes
template ToBlocks(l){
signal input stream[l];

component bbits = Num2Bits(8);
bbits.in <== b;

component XorBits = XorBits();
XorBits.a <== abits.out;
XorBits.b <== bbits.out;
var n = l\16;
if(l%16 > 0){
n = n + 1;
}
signal output blocks[n][4][4];

var i, j, k;

for (var idx = 0; idx < l; idx++) {
blocks[i][k][j] <== stream[idx];
k = k + 1;
if (k == 4){
k = 0;
j = j + 1;
if (j == 4){
j = 0;
i = i + 1;
}
}
}

component num = Bits2Num(8);
num.in <== XorBits.out;
if (l%16 > 0){
blocks[i][k][j] <== 1;
k = k + 1;
}
}

out <== num.out;
// convert blocks of 16 bytes to stream of bytes
template ToStream(n,l){
signal input blocks[n][4][4];

signal output stream[l];

var i, j, k;

while(i*16 + j*4 + k < l){
stream[i*16 + j*4 + k] <== blocks[i][k][j];
k = k + 1;
if (k == 4){
k = 0;
j = j + 1;
if (j == 4){
j = 0;
i = i + 1;
}
}
}
}

// XORs two arrays of bits
template XorBits(){
signal input a[8];
signal input b[8];
signal output out[8];
template AddCipher(){
signal input state[4][4];
signal input cipher[4][4];
signal output newState[4][4];

component xor[8];
for (var i = 0; i < 8; i++) {
xor[i] = XOR();
xor[i].a <== a[i];
xor[i].b <== b[i];
out[i] <== xor[i].out;
component xorbyte[4][4];

for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
xorbyte[i][j] = XorByte();
xorbyte[i][j].a <== state[i][j];
xorbyte[i][j].b <== cipher[i][j];
newState[i][j] <== xorbyte[i][j].out;
}
}
}

// converts iv to counter blocks
// iv is 16 bytes
template GenerateCounterBlocks(n){
assert(n < 0xffffffff);
signal input iv[16];
signal output counterBlocks[n][4][4];

var ivr[16] = iv;

component toBlocks[n];

for (var i = 0; i < n; i++) {
toBlocks[i] = ToBlocks(16);
toBlocks[i].stream <-- ivr;
counterBlocks[i] <== toBlocks[i].blocks[0];
ivr[15] = (ivr[15] + 1)%256;
if (ivr[15] == 0){
ivr[14] = (ivr[14] + 1)%256;
if (ivr[14] == 0){
ivr[13] = (ivr[13] + 1)%256;
if (ivr[13] == 0){
ivr[12] = (ivr[12] + 1)%256;
}
}
}

}
}
Loading

0 comments on commit 06fdebf

Please sign in to comment.