namnc / circom-2-arithc

Circom interpreter to arithmetic circuit description
MIT License
33 stars 7 forks source link

Implement `circomlib-mpc` #14

Open brech1 opened 4 months ago

brech1 commented 4 months ago

Implement our version of circomlib in order to support basic functions.

This is needed due to circomlib being done to work with R1CS, not working for execution.

namnc commented 1 month ago

circomlib has not been updated for 2 years, we can keep it as a submodule here and work on circomlib-mpc

brech1 commented 1 month ago
voltrevo commented 1 day ago

I'd love to get more into circom and help out with this issue. Can we put a list together of what "basic functions" we'd like to accomplish for this issue?

namnc commented 1 day ago

Sure, here's the thing. Within circomlib we can categorize the templates into 2 types: crypto and non crypto. Crypto = template for cryptographic primitives such as ECDSA or Poseidon. Non-crypto = template for scalar-based primitives such as comparators, sign, switcher.

For crypto type we have to wait for typing as we need to describe the computation in mod p. For non-crypto type we can divide the templates into two sub-categories as well: binary-based and arithmetic-based. Again the binary-based type we have to wait to typing as we need to describe the computation in mod 2. I think what is left for 0.1 is the arithmetic-based templates that are:

  1. I think these need no change at all: mux1.circom ... mux4.circom and switcher.circom.
  2. The following is a bit tricky https://github.com/iden3/circomlib/blob/cff5ab6288b55ef23602221694a6a38a0239dcc0/circuits/comparators.circom

As we support equality check and also comparison in our interpreter, i.e. we just add a gate for the ==, <, >, <=, >= we can just scrap all of the comparators templates. If one write a new circom program for MPC then they can just use the ops directly.

The whole point of this issue is to allow one to run MPC with a circom program that was written for zk using the templates above (with the bit decomposition thingy). This issue presents two challenges:

I don't know (if there is and what is) a clean way to solve this.

@voltrevo ^^^

namnc commented 1 day ago

I think we can push the tricky part to 0.2 (with typing so everything is natural).

namnc commented 1 day ago

This could be related, ZK vs MPC for division. Say we want to do a divided by b (a >= b) In MPC we just write q <== a \ b in ZK we write q <-- a \ b; r <-- a - b q; a === b q + r

My idea would be to wrap this in a template says template divide() with input a, b and provide a ZK and an MPC version and the interpreter should find the correct file to point to: For MPC we can have q and potentially r as an output (so we need also r <== a % b) For ZK we can have q as input and r as input and then do as above.

e.g. ZK version: https://github.com/socathie/circomlib-ml/blob/c82b3072d7946a76487a8c1be463fc407045391c/circuits/Dense.circom#L24 MPC version: https://github.com/namnc/circomlib-ml-patches/blob/master/circuits/Dense.circom.patch