Skip to content

Commit

Permalink
Merge pull request #708 from 0x3327/feat/elgamal
Browse files Browse the repository at this point in the history
Feat/elgamal
  • Loading branch information
daodesigner authored Jul 4, 2023
2 parents 15d0971 + 5888b46 commit 294e0e5
Show file tree
Hide file tree
Showing 66 changed files with 9,131 additions and 4,865 deletions.
57 changes: 48 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ For development purposes, you can generate the proving and verifying keys for
the zk-SNARK circuits, along with their Solidity verifier contracts as such.

Navigate to the rapidsnark [repo](https://github.com/iden3/rapidsnark) to install the necessary tooling.
More details can be found in /docs/installation.md, Section Install `rapidsnark`;

Build the zk-SNARKs and generate their proving and verifying keys:
To build the circom circuits, follow the /docs/installation.md, Section Configure circom-helper and zkey-manager. Then run:

```bash
cd circuits
Expand All @@ -88,6 +89,9 @@ npm run compileSol

Avoid using `npx hardhat compile` and instead use the provided command as artifacts are copied into their relevant directories.

To build the zk-SNARKs and generate their proving and verifying keys, follow the instructions from /docs/installation.md,
Section: Generate `.zkey` files.

### Local development

This repository is organised as Lerna submodules. Each submodule contains its
Expand Down Expand Up @@ -121,23 +125,43 @@ For example:

### Testing

It is implied that the previous steps have been completed before running tests.

### Unit tests

The following submodules contain unit tests: `core`, `crypto`, `circuits`,
`contracts`, and `domainobjs`.

Except for the `contracts` submodule, run unit tests as such (the following
Except for the `contracts` and `circuits` submodule, run unit tests as such (the following
example is for `crypto`):

```bash
cd crypto
npm run test
```

For `contracts` and `integrationTests`, run the tests one by one. This prevents
incorrect nonce errors.
For `circuits`, first build the zk-SNARKs as explained above and then run in one terminal:

First, start a Hardhat instance in a separate terminal:
```bash
cd circuits
npm run circom-helper
```
wait for *Launched JSON-RPC server at port 9001* message and then run in another terminal:

```bash
cd circuits
npm run test
```

Note that some tests might fail due to jest timeout. You can fix this by adjusting the timeout period defined at the top of the test file.

For example, in the file *MessageToCommand.test.ts*, increase timeout_in_ms on this line: ```jest.setTimeout(<timeout_in_ms>)```.

For `contracts` and `integrationTests`, run the tests one by one. This prevents incorrect nonce errors.

For `contracts`, first, compile the contracts as explained above.

Then, start a Hardhat instance in a separate terminal:

```bash
cd contracts
Expand Down Expand Up @@ -170,17 +194,32 @@ cd contracts
./scripts/runTestsInCi.sh
```

Or run all integration tests (this also starts its own Hardhat instance):
For `integrationTests`, first make sure to install necessary tooling for rapidsnark as explained above, build the zk-SNARKs and generate their proving and verifying keys.

Run all integration tests (this also starts its own Hardhat instance so make sure to kill any running hardhat instance):

```bash
cd integrationTests
./scripts/runTestsInCi.sh
```

You can ignore the Hardhat errors which this script emits as you should already
have Hardhat running in a separate terminal. Otherwise, you will have to exit
Ganache using the `kill` command.
### CLI tests

Make sure dependencies are installed, circuits are built, zkeys keys generated and contract compiled.
First run hardhat:

```bash
cd contracts
npm run hardhat
```

Then navigate to /cli/tests/vanilla and execute each test like this:

```bash
cd cli/tests/vanilla
bash ./test1.sh
```
You can find more details about running cli tests in /docs/testing.md.

### Docker

Expand Down
1 change: 1 addition & 0 deletions circom
Submodule circom added at ce903c
28 changes: 21 additions & 7 deletions circuits/circom/isDeactivatedKey.circom
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,41 @@ template IsDeactivatedKey(levels) {
signal input key[2];

// Ciphertext of the encrypted key status
signal input c1;
signal input c2;
signal input c1[2];
signal input c2[2];

signal input salt;

signal input path_index[levels];
signal input path_elements[levels][LEAVES_PER_PATH_LEVEL];
signal output isDeactivated;
signal output computedRoot;

// Hash public key x and y coordinates
// Hash public key x and y coordinates with salt: hash(key[0], key[1], salt)
signal keyHash;
component keyHasher = PoseidonHashT5();

// Tree leaf hash: hash(keyHash, c1[0], c1[1], c2[0], c2[1])
signal leafHash;

component keyHasher = PoseidonHashT4();
keyHasher.inputs[0] <== key[0];
keyHasher.inputs[1] <== key[1];
keyHasher.inputs[2] <== c1;
keyHasher.inputs[3] <== c2;
keyHasher.inputs[2] <== salt;

keyHash <== keyHasher.out;

component leafHasher = PoseidonHashT6();
leafHasher.inputs[0] <== keyHash;
leafHasher.inputs[1] <== c1[0];
leafHasher.inputs[2] <== c1[1];
leafHasher.inputs[3] <== c2[0];
leafHasher.inputs[4] <== c2[1];

leafHash <== leafHasher.out;

// Compute root for the given proof params
component incProof = QuinTreeInclusionProof(levels);
incProof.leaf <== keyHash;
incProof.leaf <== leafHash;

for (var i = 0; i < levels; i++) {
incProof.path_index[i] <== path_index[i];
Expand Down
4 changes: 3 additions & 1 deletion circuits/circom/messageValidator.circom
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pragma circom 2.0.0;
include "./verifySignature.circom";
include "./utils.circom";
include "./verifySignature.circom";
include "../node_modules/circomlib/circuits/comparators.circom";
include "../node_modules/circomlib/circuits/comparators.circom";

template MessageValidator() {
// a) Whether the state leaf index is valid
Expand Down
Loading

0 comments on commit 294e0e5

Please sign in to comment.