Circom operates over the prime field of p = 21888242871839275222246405745257275088548364400416034343698204186575808495617
which is the Field Modulus of BN254
curve. This might be because of the existing support for BN254
curves in the Ethereum ecosystem like precompiles (add, mul, pairing), powers of tau parameters & so on.
Circom supports these curves as of now : bn128
, bls12381
, goldilocks
, grumpkin
, pallas
, vesta
. Refer here
To add your choice of curve & tweak the prime field of circom, follow the given steps :
- Fork the repository -
https://github.com/iden3/circom
- In
circom/src/input_user.rs
, add your curve in theprime_value
field - In
code_producers/src/c_elements/c_code_generator.rs
, include your curve to generate the cpp, hpp, asm files - In
code_producers/src/wasm_elements/wasm_code_generator.rs
, include your curve to generate the wat files - In
program_structure/src/utils/constants.rs
, add your curve's prime field as a constant - Clone this repository -
https://github.com/iden3/ffwasm
& install the dependencies by runningnpm i
- Run
node tools/build.js -q <your_prime> -n Fr
inside theffwasm
folder to generatefr.wasm
&fr.wat
files - From the
fr.wat
file, carefully parse thetype
,data
&code
intofr-types.wat
,fr-data.wat
&fr-code.wat
undercode_producers/src/wasm_elements/<your_curve>
. Refer to this commit - In the
fr-code.wat
, search for$Fr_F1m_isZero
, if it exists, replace it with$Fr_int_isZero
. Refer to this commit. Not doing this change will lead to this error when you compile your circuits :
error[W01]: Error translating the circuit from wat to wasm.
Exception encountered when encoding WASM: failed to find func named `$Fr_F1m_isZero` at byte offset 164466
previous errors were found
- This is enough for you to compile your circuits with your choice of curve. Further more, to produce C code for your circuits, you might have to generate
cpp
,hpp
,asm
files using the steps mentioned inhttps://github.com/iden3/ffiasm
- This might not work well with
snarkjs
A few implementations to refer to :
- circom-secq256r1 - Adds prime for secq256r1
- circom-secq - Adds prime for secq256k1
- PR - Pallas, Vesta, Grumpkin - Adds prime for Pasta curves & Grumpkin