Skip to content

privacy-scaling-explorations/circom-ecdsa-p256

Repository files navigation

circom-ecdsa-p256

Implementation of ECDSA operations in circom for the P-256 curve.

Project overview

This repository provides proof-of-concept implementations of ECDSA operations on the P-256 curve in circom. These implementations are for demonstration purposes only. These circuits are not audited, and this is not intended to be used as a library for production-grade applications.

Circuits can be found in circuits. scripts contains various utility scripts (most importantly, scripts for building a few example zkSNARKs using the ECDSA circuit primitives). test contains some unit tests for the circuits, mostly for witness generation.

Information

Due to the nature of the P-256 curved compared to the bn254 circom backend (and Ethereum precompiles), we must used Big Integer representation of the scalars. Please see this blog post from 0xPARC describing BigInt arithmetic for the original circom-ecdsa implementation.

Additionally, we utilize Yi-sun's more update BigInt and ECC arithmetic methods from the circom-pairing library.

Install dependencies

  • Run git submodule update --init --recursive
  • Run yarn at the top level to install npm dependencies (snarkjs and circomlib).
  • You'll also need circom version >= 2.1.5 on your system. Installation instructions here.
  • Run yarn inside of circuits/circom-pairing to install npm dependencies for the circom-pairing library.
  • If you want to build the pubkeygen, eth_addr, and groupsig circuits, you'll need to download a Powers of Tau file with 2^20 constraints and copy it into the circuits subdirectory of the project, with the name pot20_final.ptau. We do not provide such a file in this repo due to its large size. You can download and copy Powers of Tau files from the Hermez trusted setup from this repository.
  • If you want to build the verify circuits, you'll also need a Powers of Tau file that can support at least 2^21 constraints (place it in the same directory as above with the same naming convention).

Building keys and witness generation files

We provide examples of four circuits using the ECDSA primitives implemented here:

  • pubkeygen: Prove knowledge of a private key corresponding to a ECDSA public key.
  • eth_addr: Prove knowledge of a private key corresponding to an Ethereum address.
  • groupsig: Prove knowledge of a private key corresponding to one of three Ethereum addresses, and attest to a specific message.
  • verify: Prove that a ECDSA verification ran properly on a provided signature and message. Note that this circuit does not verify that the public key itself is valid. This must be done separately by the user.

Run yarn build:pubkeygen, yarn build:eth_addr, yarn build:groupsig, yarn build:verify at the top level to compile each respective circuit and keys.

Each of these will create a subdirectory inside a build directory at the top level (which will be created if it doesn't already exist). Inside this directory, the build process will create r1cs and wasm files for witness generation, as well as a zkey file (proving and verifying keys). Note that this process will take several minutes (see full benchmarks below). Building verify requires 56G of RAM.

This process will also generate and verify a proof for a dummy input in the respective scripts/[circuit_name] subdirectory, as a smoke test.

Circuits Description

The following circuits are implemented and can be found in circuits/ecdsa.circom.

  • ECDSAPrivToPub: Given a secp256k1 private key, outputs the corresponding public key by computing (private_key) * G where G is the base point of secp256k1.
  • ECDSAVerifyNoPubkeyCheck: Given a signature (r, s), a message hash, and a secp256k1 public key, it follows ecdsa verification algorithm to extract r' from s, message hash and public key, and then compares r' with r to see if the signaure is correct. The output result is 1 if r' and r are equal, 0 otherwise.

The 256-bits input and output are chunked and represented as k n-bits values where k is 6 and n is 43. Please see above examples for concrete usages.

WARNING: Beware that the input to the above circuits should be properly checked and guarded (Lies on the curve, not equal to zero, etc). The purpose of the above circuits is to serve as building blocks but not as stand alone circuits to deploy.

Benchmarks

All benchmarks were run on an AMD 7700x, 32GB RAM machine

pubkeygen eth_addr groupsig verify
Constraints 114724 247380 250938 1972905
Circuit compilation 15s 47s 48s 44s
Witness generation 6s 11s 12s 78s
Trusted setup phase 2 key generation 46s 94s 98s 81s
Trusted setup phase 2 contribution 6s 20s 19s 82s
Proving key size 102M 132M 134M 1.2G
Proving key verification 48s 81s 80s 45
Proving time 3s 7s 6s 26
Proof verification time <1s <1s 1s 1s

Testing

Run yarn test at the top level to run tests. Note that these tests only test correctness of witness generation. They do not check that circuits are properly constrained, i.e. that only valid witnesses satisfy the constraints. This is a much harder problem that we're currently working on!

Circuit unit tests are written in typescript, in the test directory using chai, mocha, and circom_tester. Running all tests takes about 1 hour on our 3.3GHz, 64G RAM test machine. To run a subset of the tests, use yarn test --grep [test_str] to run all tests whose description matches [test_str].

Groupsig CLI Demo

You can run a CLI demo of a zkSNARK-enabled group signature generator once you've built the groupsig keys. Simply run yarn groupsig-demo at the top level and follow the instructions in your terminal.

Acknowledgments

This project was built during 0xPARC's Applied ZK Learning Group #1.

We use a circom implementation of keccak from Vocdoni. We also use some circom utilities for converting an ECDSA public key to an Ethereum address implemented by lsankar4033, jefflau, and veronicaz41 for another ZK Learning Group project in the same cohort. We use an optimization for big integer multiplication from xJsnark.

About

Big integer arithmetic // secp256k1 & additional P256 ECC operations in circom

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published