Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring(circuits): refactor the circuit package to improve its efficiency #832

Merged
merged 10 commits into from
Dec 13, 2023

Conversation

ctrlc03
Copy link
Collaborator

@ctrlc03 ctrlc03 commented Nov 21, 2023

Refactor the package:

  • clean up unused dependencies
  • update dependencies
  • docs improvements
  • monolith file to multiple files
  • circuits comments
  • usage instructions
  • build scripts cleanup
  • speed up tests

@ctrlc03 ctrlc03 self-assigned this Nov 21, 2023
@ctrlc03 ctrlc03 linked an issue Nov 21, 2023 that may be closed by this pull request
7 tasks
@ctrlc03 ctrlc03 changed the title refactoring(circuits) - cleanup circuits package, comment code and split into utilities vs exported functions refactoring(circuits) - refactor the circuit package to improve its efficiency Nov 21, 2023
@ctrlc03 ctrlc03 force-pushed the refactoring/circuits branch from 53e7607 to f451d18 Compare November 24, 2023 22:45
@ctrlc03 ctrlc03 linked an issue Nov 25, 2023 that may be closed by this pull request
@ctrlc03 ctrlc03 force-pushed the refactoring/circuits branch from 19365ec to 761a630 Compare November 27, 2023 16:51
@ctrlc03 ctrlc03 marked this pull request as ready for review November 27, 2023 16:51
@ctrlc03 ctrlc03 requested review from samajammin and 0xmad November 27, 2023 16:51
@0xmad 0xmad changed the title refactoring(circuits) - refactor the circuit package to improve its efficiency refactoring(circuits): refactor the circuit package to improve its efficiency Dec 5, 2023
@0xmad
Copy link
Collaborator

0xmad commented Dec 5, 2023

@ctrlc03 could you please fix conflicts?

@ctrlc03 ctrlc03 force-pushed the refactoring/circuits branch from 761a630 to c80ebd3 Compare December 5, 2023 21:36
@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 5, 2023

@ctrlc03 could you please fix conflicts?

It's up to date now :)

@ctrlc03 ctrlc03 force-pushed the refactoring/circuits branch 4 times, most recently from 5d92fcb to a527139 Compare December 6, 2023 09:54
Copy link
Collaborator

@0xmad 0xmad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctrlc03 thanks, just some comments related to types and doc styles.

circuits/package.json Outdated Show resolved Hide resolved
circuits/package.json Outdated Show resolved Hide resolved
circuits/scripts/build_arm.sh Outdated Show resolved Hide resolved
circuits/ts/__tests__/CalculateTotal.test.ts Outdated Show resolved Hide resolved
circuits/ts/__tests__/MessageToCommand.test.ts Outdated Show resolved Hide resolved
circuits/ts/proofs.ts Outdated Show resolved Hide resolved
circuits/ts/proofs.ts Outdated Show resolved Hide resolved
circuits/ts/utils.ts Outdated Show resolved Hide resolved
core/ts/Poll.ts Outdated Show resolved Hide resolved
@ctrlc03 ctrlc03 force-pushed the refactoring/circuits branch from a527139 to 074b97c Compare December 7, 2023 09:59
Copy link

netlify bot commented Dec 7, 2023

Deploy Preview for maci-typedoc ready!

Name Link
🔨 Latest commit a849486
🔍 Latest deploy log https://app.netlify.com/sites/maci-typedoc/deploys/65798c9a0f82840008ebf4e4
😎 Deploy Preview https://deploy-preview-832--maci-typedoc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@ctrlc03 ctrlc03 force-pushed the refactoring/circuits branch 3 times, most recently from 558bfc5 to f4584de Compare December 7, 2023 16:37
Copy link
Collaborator

@0xmad 0xmad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctrlc03 thanks, just one comment about try/catch. The rest is good to me.

circuits/ts/proofs.ts Outdated Show resolved Hide resolved
@ctrlc03 ctrlc03 force-pushed the refactoring/circuits branch 2 times, most recently from 1521b00 to 66b7c91 Compare December 7, 2023 17:05

The circuits are used by the coordinator (the prover) to prove that they have correctly processed a batch of messages and tallied the votes correctly. This happens after a Poll has completed, and the coordinator has merged the state and message trees. The coordinator then generates a proof for each batch of messages, and submits them to the contract. The contract then verifies the proofs and updates the commitments on chain.

> Please note that in order to function, the circuit in charge of processing the messages requires the coordinator's private key, hence a proof for this circuit can only be generated by the coordinator. The private key is needed in order to generate the ECDH shared key used to decrypt the messages.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> Please note that in order to function, the circuit in charge of processing the messages requires the coordinator's private key, hence a proof for this circuit can only be generated by the coordinator. The private key is needed in order to generate the ECDH shared key used to decrypt the messages.
Please note that in order to function, the circuit in charge of processing the messages requires the coordinator's private key, hence a proof for this circuit can only be generated by the coordinator. The private key is needed in order to generate the ECDH shared key used to decrypt the messages.

Why the special quote formatting here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like an important note or something


MACI uses [GROTH16](https://eprint.iacr.org/2016/260.pdf) as its proving system. GROTH16 is a zk-SNARK proving system that allows for the generation of proofs that are small and fast to verify.

## How are the circuits used
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## How are the circuits used
## How are the circuits used?


![flow](/img/circuits/processingAfterPollEnds.svg)

## How the circuits work
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## How the circuits work
## How do the circuits work?

Keeping the consistency of question format


## How do the Circuits fit in a voting round

![flow](/img/circuits/processingAfterPollEnds.svg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These flow charts are awesome! 🤩

I have some improvement suggestions but I think that's best saved for a future PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to edit the file yourself! included the source in static/img/circuits which you can load into excalidraw

state and ballot trees according to the contents of said messages.
2. `TallyVotes.circom`, which counts votes from users' ballots, batch by batch.
3. `Subsidy.circom`, which implements [pairwise subsidy](https://hackmd.io/@chaosma/H1_9xmT2K)
1. [`ProcessMessages.circom`](https://github.com/privacy-scaling-explorations/maci/blob/dev/circuits/circom/processMessages.circom), which takes a batch of encrypted messages, decrypts them, and generates a proof that the coordinator's local processing was performed correctly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick but I think this should be ProcessMessages (circuit name, PascalCase) or processMessages.circom (file name, camelCase)

Naming convention question: should Circom files be camelCase?

Copy link
Member

@samajammin samajammin Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might just be simpler to update the filenames to be PascalCase to match the circuit names? (not in this PR)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about conventions, here is how circomlib names them (all lower it seems)


### Message processing (processMessages)

This circuit allows the coordinator to prove that they have correctly processed each message in reverse order, in a consecutive batch of 5 ^ msgBatchDepth messages to the respective state leaf within the state tree. Coordinators would use this circuit to prove correct execution at the end of each Poll. The pre-requisites for this are:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think worth explaining why this happens in batches (because of gas limit, correct?)

Copy link
Collaborator Author

@ctrlc03 ctrlc03 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well AFAIU we do it in batches because of circuits' constraints. If we were to process all messages in one go, the constraints would be so many that it would take forever and the circuit's artifacts (zkeys, r1cs, etc) would be MASSIVE (tens of GB)

### Vote tallying
#### Inputs

| Input signal | Description |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This table is great, thanks for doing this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no can't take props for that, it's a copy pasta from here: https://maci.pse.dev/docs/spec 🙂

7. That each message in `msgs` exists in the message tree
8. That after decrypting and applying each message, in reverse order, to the corresponding state and ballot leaves, the new state root, new ballot root, and `newSbSalt` are the preimage to `newSbCommitment`

### Tally Votes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanation would be nice here (but as I'm reviewing I'm thinking a lot of these documentation changes could be done in future PRs)


### Subsisdy (Optional)

The subsidy circuit is used to implement pairwise subsidy. This is a technique that can be used to detect voters collusion. It currently is not optimized for production and the team will work on a more efficient implementation in the future.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The subsidy circuit is used to implement pairwise subsidy. This is a technique that can be used to detect voters collusion. It currently is not optimized for production and the team will work on a more efficient implementation in the future.
This circuit is an optional feature - it's not required for MACI to function. The subsidy circuit is used to implement pairwise subsidy. This is a technique that can be used to detect voters collusion. It currently is not optimized for production and the team will work on a more efficient implementation in the future.

Copy link
Member

@samajammin samajammin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 👏

I left a bunch of suggestions, I think entirely related to the documentation.

Given the conversation today around prioritizing the package refactor work (vs. documentation), I'm ok with addressing documentation improvements in future PRs (which I'd be happy to help with).


Install dependencies for and `zkey-manager`:

```bash
sudo apt-get install libgmp-dev nlohmann-json3-dev nasm g++
```

> Note that on a arm macbook you won't need the above. However, you will not be able to compile the c++ witness generator.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> Note that on a arm macbook you won't need the above. However, you will not be able to compile the c++ witness generator.
> Note that on an ARM MacBook you won't need the above. However, you will not be able to compile the C++ witness generator.

@ctrlc03 ctrlc03 force-pushed the refactoring/circuits branch from 386e488 to a849486 Compare December 13, 2023 10:51
@ctrlc03 ctrlc03 merged commit 8cc155f into dev Dec 13, 2023
11 of 12 checks passed
@ctrlc03 ctrlc03 deleted the refactoring/circuits branch December 13, 2023 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Refactoring of Circuits package Update docs for system requirements
3 participants