Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ This work was spawned from the Mina Grant Starter Program and aims to provide a

- AES-128 encryption
- Static and dynamic message sizes
- Counter Mode (CTR)
- Galois Counter Mode (GCM) (coming soon)

## Installation and Quick Start

Expand Down Expand Up @@ -77,6 +75,14 @@ npm run build
node ./build/test/circuitSummary.js
```

## Code Breakdown
The main entrypoint of the code is contained within `src/implementations/IterativeAES128.ts` which has the following implemented.
- `IterativeAes128` is responsible for verifying that a cipher has been encrypted using AES with an arbitrary key and message
- `IterativeAes128MessagePublic` is responsible for verifying that a cipher **and** a message have been encrypted using AES with an arbitrary key.
- `computeIterativeAes128Encryption()` which can be inlined within circuits in order to proof AES encryption.

Additionally, `Byte16` is used to represent 256-bit numbers and is commonly used as inputs to functions and circuits.

## Circuit Breakdown

### AES128 Iterative Summary
Expand All @@ -90,13 +96,6 @@ node ./build/test/circuitSummary.js
| Rot64 | 4800 |
| RangeCheck0 | 4800 |

### Core:
- Implementing block mode: **Counter Mode (CTR)**.

### Optional:

- User authentication block mode: **Galois Counter Mode (GCM)**.

# Contributing
Everyone is welcome to contribute, file an issue or submit a pull request if you think there is something worth mentioning.

Expand Down
123 changes: 0 additions & 123 deletions src/implementations/AES128CTR.ts

This file was deleted.

28 changes: 27 additions & 1 deletion src/implementations/IterativeAES128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ const IterativeAes128 = ZkProgram({
},
});

class IterativeAES128MessagePublicInput extends Struct({
cipher: Byte16,
message: Byte16,
}) {}

/**
* A zkProgram that verifies a proof that a message and cipher are conncted via AES-128 using the given key.
*/
const IterativeAes128MessagePublic = ZkProgram({
name: "aes-verify-iterative-decrypt",
publicInput: IterativeAES128MessagePublicInput,

methods: {
verifyAES128: {
privateInputs: [Byte16],

async method(input: IterativeAES128MessagePublicInput, key: Byte16) {
const message = input.message;
const state = computeIterativeAes128Encryption(message, key);
state.assertEquals(input.cipher);
},
},
},
});

/**
* Generates a proof that the given message was encrypted with AES-128 using the given key.
* The key must be in hex form.
Expand All @@ -77,7 +102,6 @@ const IterativeAes128 = ZkProgram({
* @throws If the message is not 16 characters long or the key is not 32 characters long
* @throws If the proof generation fails
*/
// NO TEST NOW AS IT WILL CHANGE SOON
async function generateIterativeAes128Proof(
message: string,
keyHex: string, // Should we allow non hex strings?
Expand Down Expand Up @@ -107,4 +131,6 @@ export {
generateIterativeAes128Proof,
IterativeAes128,
IterativeAES128PublicInput,
IterativeAes128MessagePublic,
IterativeAES128MessagePublicInput,
};
51 changes: 0 additions & 51 deletions src/implementations/IterativeAES128CTR.ts

This file was deleted.

10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {
IterativeAes128,
IterativeAES128PublicInput,
IterativeAes128MessagePublic,
IterativeAES128MessagePublicInput,
} from "./implementations/IterativeAES128";
import { Byte16 } from "./primitives/Bytes.js";

export { IterativeAes128, IterativeAES128PublicInput, Byte16 };
export {
IterativeAes128,
IterativeAES128PublicInput,
Byte16,
IterativeAes128MessagePublic,
IterativeAES128MessagePublicInput,
};
export { generateIterativeAes128Proof as generateAes128Proof } from "./implementations/IterativeAES128";
23 changes: 23 additions & 0 deletions test/implementations/verifyAES128.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
computeIterativeAes128Encryption,
IterativeAes128,
IterativeAES128PublicInput as AESPublicInput,
IterativeAes128MessagePublic,
IterativeAES128MessagePublicInput,
} from "../../src/implementations/IterativeAES128.js";
import { encryptAES128 } from "../../src/utils/crypto.js";
import { Byte16 } from "../../src/primitives/Bytes.js";
Expand Down Expand Up @@ -53,3 +55,24 @@ describe("Iterative AES128 Encryption", () => {
},
);
});

describe("Iterative AES128 Decryption", () => {
(RUN_ZK_TESTS ? it : it.skip)(
"should verify the proof using the zkProgram",
async () => {
const { verificationKey } = await IterativeAes128MessagePublic.compile();
const { plaintext, key } = testVectorToByte16(testVector1);
const cipher = Byte16.fromHex(getCipherText(testVector1));
const input = new IterativeAES128MessagePublicInput({
cipher,
message: plaintext,
});
const { proof } = await IterativeAes128MessagePublic.verifyAES128(
input,
key,
);
const isValid = await verify(proof, verificationKey);
expect(isValid).toBe(true);
},
);
});
Loading