github.com/consensys/gnark@v0.11.0/README.md (about)

     1  # `gnark` zk-SNARK library
     2  
     3  [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/gnark_team.svg?style=social&label=Follow%20%40gnark_team)](https://twitter.com/gnark_team) [![License](https://img.shields.io/badge/license-Apache%202-blue)](LICENSE)
     4  [![Go Report Card](https://goreportcard.com/badge/github.com/ConsenSys/gnark)](https://goreportcard.com/badge/github.com/ConsenSys/gnark)
     5  [![PkgGoDev](https://pkg.go.dev/badge/mod/github.com/consensys/gnark)](https://pkg.go.dev/mod/github.com/consensys/gnark)
     6  [![Documentation Status](https://readthedocs.com/projects/pegasys-gnark/badge/)][`gnark` User Documentation] [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.5819104.svg)](https://doi.org/10.5281/zenodo.5819104)
     7  
     8  `gnark` is a fast zk-SNARK library that offers a high-level API to design circuits. The library is open source and developed under the Apache 2.0 license.
     9  
    10  `gnark` uses [`gnark-crypto`] for the finite-field arithmetic and out-circuit implementation of cryptographic algorithms.
    11  
    12  `gnark` powers [`Linea zk-rollup`](https://linea.build). Include your project in the [known users](docs/KNOWN_USERS.md) section by opening a PR.
    13  
    14  ## Useful Links
    15  
    16  * [`gnark` User Documentation]
    17  * [`gnark` Playground]
    18  * [`gnark` Issues]
    19  * [`gnark` Benchmarks](https://docs.gnark.consensys.net/overview#gnark-is-fast) 🏁
    20  * [`gnark-announce`] - Announcement list for new releases and security patches
    21  
    22  
    23  ## `gnark` Users
    24  
    25  To get started with `gnark` and write your first circuit, follow [these instructions][`gnark` User Documentation].
    26  
    27  Checkout the [online playground][`gnark` Playground] to compile circuits and visualize constraint systems.
    28  
    29  
    30  ## Security
    31  
    32  **`gnark` and [`gnark-crypto`] have been [extensively audited](#audits), but are provided as-is, we make no guarantees or warranties to its safety and reliability. In particular, `gnark` makes no security guarantees such as constant time implementation or side-channel attack resistance.**
    33  
    34  **To report a security bug, please refer to [`gnark` Security Policy](SECURITY.md).**
    35  
    36  Refer to [known security advisories](https://github.com/Consensys/gnark/security/advisories?state=published) for a list of known security issues.
    37  
    38  ## Testing
    39  
    40  `gnark` employs the following testing procedures:
    41  * unit testing - we test the primitives in unit tests
    42  * circuit testing - we test the circuit implementation against several targets:
    43    - test engine - instead of running the full prover and verifier stack, we run the computations only to ensure the completeness of the circuits
    44    - proof engines - we compile the circuits, run the setup, prove and verify using native implementation
    45    - Solidity verifier - in addition to the previous, we verify the proofs in Solidity verifier. See [`gnark-solidity-checker`]
    46  * regression testing - we have implemented [tests for reported issues](internal/regression_tests) to avoid regressions
    47  * constraint count testing - we have implemented [circuit size tests](internal/stats) to avoid regressions
    48  * serialization testing - we check that [serialization round-trip is complete](io/roundtrip.go)
    49  * side-effect testing - we check that circuit [compilation is deterministic](test/assert.go)
    50  * fuzz testing:
    51    - circuit input fuzzing - we provide random inputs to the circuit to cause solver error
    52    - native input fuzzing - we provide random inputs to various native methods to cause errors. We have also stored initial fuzzing corpus for regression tests.
    53    - circuit definition fuzzing - we cooperate with Consensys Diligence to fuzz the circuit definitions to find bugs in the `gnark` circuit compiler.
    54  
    55  The tests are automatically run during every PR and merge commit. We run full test suite only for the Linux on `amd64` target, but run short tests both for Windows target (`amd64`) and macOS target (`arm64`).
    56  
    57  ## Performance
    58  
    59  `gnark` and `gnark-crypto` packages are optimized for 64bits architectures (x86 `amd64`) using assembly operations. We have generic implementation of the same arithmetic algorithms for ARM backends (`arm64`). We do not implement vector operations.
    60  
    61  ## Backwards compatibility
    62  
    63  `gnark` tries to be backwards compatible when possible, however we do not guarantee that serialized object formats are static over different versions of `gnark`. Particularly - we do not have versioning implemented in the serialized formats, so using files between different versions of gnark may lead to undefined behaviour or even crash the program.
    64  
    65  ## Issues
    66  
    67  `gnark` issues are tracked [in the GitHub issues tab][`gnark` Issues].
    68  
    69  **To report a security bug, please refer to [`gnark` Security Policy](SECURITY.md).**
    70  
    71  If you have any questions, queries or comments, [GitHub discussions] is the place to find us.
    72  
    73  You can also get in touch directly: gnark@consensys.net
    74  
    75  ## Release Notes
    76  
    77  [Release Notes](CHANGELOG.md)
    78  
    79  ## Audits
    80  
    81  * [Kudelski Security - October 2022 - gnark-crypto (contracted by Algorand Foundation)](audits/2022-10%20-%20Kudelski%20-%20gnark-crypto.pdf)
    82  * [Sigma Prime - May 2023 - gnark-crypto KZG (contracted by Ethereum Foundation)](audits/2024-05%20-%20Sigma%20Prime%20-%20kzg.pdf)
    83  * [Consensys Diligence - June 2023 - gnark PLONK Solidity verifier](https://consensys.io/diligence/audits/2023/06/linea-plonk-verifier/)
    84  * [LeastAuthority - August 2023 - gnark Groth16 Solidity verifier template (contracted by Worldcoin)](https://leastauthority.com/wp-content/uploads/2023/08/Worldcoin_Groth16_Verifier_in_EVM_Smart_Contract_Final_Audit_Report.pdf)
    85  * [OpenZeppelin - November 2023 - gnark PLONK Solidity verifier template](https://blog.openzeppelin.com/linea-verifier-audit-1)
    86  * [ZKSecurity.xyz - May 2024 - gnark standard library](audits/2024-05%20-%20zksecurity%20-%20gnark%20std.pdf)
    87  * [OpenZeppelin - June 2024 - gnark PLONK prover and verifier](https://blog.openzeppelin.com/linea-prover-audit)
    88  * [LeastAuthority - July 2024 - gnark general and GKR (initial report)](audits/2024-07%20-%20Least%20Authority%20-%20arithm%20and%20GKR.pdf)
    89  
    90  ## Proving schemes and curves
    91  
    92  Refer to [Proving schemes and curves] for more details.
    93  
    94  `gnark` support the following zk-SNARKs:
    95  
    96  - [x] [Groth16](https://eprint.iacr.org/2016/260)
    97  - [x] [PlonK](https://eprint.iacr.org/2019/953)
    98  
    99  which can be instantiated with the following curves
   100  
   101  - [x] BN254
   102  - [x] BLS12-381
   103  - [x] BLS12-377
   104  - [x] BW6-761
   105  - [x] BLS24-315
   106  - [x] BW6-633
   107  - [x] BLS24-317
   108  
   109  ### Example
   110  
   111  Refer to the [`gnark` User Documentation]
   112  
   113  Here is what `x**3 + x + 5 = y` looks like
   114  
   115  ```golang
   116  package main
   117  
   118  import (
   119  	"github.com/consensys/gnark-crypto/ecc"
   120  	"github.com/consensys/gnark/backend/groth16"
   121  	"github.com/consensys/gnark/frontend"
   122  	"github.com/consensys/gnark/frontend/cs/r1cs"
   123  )
   124  
   125  // CubicCircuit defines a simple circuit
   126  // x**3 + x + 5 == y
   127  type CubicCircuit struct {
   128  	// struct tags on a variable is optional
   129  	// default uses variable name and secret visibility.
   130  	X frontend.Variable `gnark:"x"`
   131  	Y frontend.Variable `gnark:",public"`
   132  }
   133  
   134  // Define declares the circuit constraints
   135  // x**3 + x + 5 == y
   136  func (circuit *CubicCircuit) Define(api frontend.API) error {
   137  	x3 := api.Mul(circuit.X, circuit.X, circuit.X)
   138  	api.AssertIsEqual(circuit.Y, api.Add(x3, circuit.X, 5))
   139  	return nil
   140  }
   141  
   142  func main() {
   143  	// compiles our circuit into a R1CS
   144  	var circuit CubicCircuit
   145  	ccs, _ := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
   146  
   147  	// groth16 zkSNARK: Setup
   148  	pk, vk, _ := groth16.Setup(ccs)
   149  
   150  	// witness definition
   151  	assignment := CubicCircuit{X: 3, Y: 35}
   152  	witness, _ := frontend.NewWitness(&assignment, ecc.BN254.ScalarField())
   153  	publicWitness, _ := witness.Public()
   154  
   155  	// groth16: Prove & Verify
   156  	proof, _ := groth16.Prove(ccs, pk, witness)
   157  	groth16.Verify(proof, vk, publicWitness)
   158  }
   159  
   160  ```
   161  
   162  ### GPU Support
   163  
   164  #### Icicle Library
   165  
   166  The following schemes and curves support experimental use of Ingonyama's Icicle GPU library for low level zk-SNARK primitives such as MSM, NTT, and polynomial operations:
   167  
   168  - [x] [Groth16](https://eprint.iacr.org/2016/260)
   169  
   170  instantiated with the following curve(s)
   171  
   172  - [x] BN254
   173  
   174  To use GPUs, add the `icicle` buildtag to your build/run commands, e.g. `go run -tags=icicle main.go`.
   175  
   176  You can then toggle on or off icicle acceleration by providing the `WithIcicleAcceleration` backend ProverOption:
   177  
   178  ```go
   179      // toggle on
   180      proofIci, err := groth16.Prove(ccs, pk, secretWitness, backend.WithIcicleAcceleration())
   181      
   182      // toggle off
   183      proof, err := groth16.Prove(ccs, pk, secretWitness)
   184  ```
   185  
   186  For more information about prerequisites see the [Icicle repo](https://github.com/ingonyama-zk/icicle).
   187  
   188  ## Citing
   189  
   190  If you use `gnark` in your research a citation would be appreciated.
   191  Please use the following BibTeX to cite the most recent release.
   192  
   193  ```bib
   194  @software{gnark-v0.11.0,
   195    author       = {Gautam Botrel and
   196                    Thomas Piellard and
   197                    Youssef El Housni and
   198                    Ivo Kubjas and
   199                    Arya Tabaie},
   200    title        = {ConsenSys/gnark: v0.11.0},
   201    month        = sep,
   202    year         = 2024,
   203    publisher    = {Zenodo},
   204    version      = {v0.11.0},
   205    doi          = {10.5281/zenodo.5819104},
   206    url          = {https://doi.org/10.5281/zenodo.5819104}
   207  }
   208  ```
   209  
   210  ## Contributing
   211  
   212  Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our [code of conduct](CODE_OF_CONDUCT.md), and the process for submitting pull requests to us.
   213  
   214  ## Versioning
   215  
   216  We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/consensys/gnark/tags).
   217  
   218  ## License
   219  
   220  This project is licensed under the Apache 2 License - see the [LICENSE](LICENSE) file for details
   221  
   222  [`gnark` Issues]: https://github.com/consensys/gnark/issues
   223  [`gnark` Playground]: https://play.gnark.io
   224  [`gnark` User Documentation]: https://docs.gnark.consensys.net/
   225  [GitHub discussions]: https://github.com/ConsenSys/gnark/discussions
   226  [Proving schemes and curves]: https://docs.gnark.consensys.net/Concepts/schemes_curves
   227  [`gnark-announce`]: https://groups.google.com/g/gnark-announce
   228  [@gnark_team]: https://twitter.com/gnark_team
   229  [`gnark-crypto`]: https://github.com/Consensys/gnark-crypto
   230  [`gnark-solidity-checker`]: https://github.com/Consensys/gnark-solidity-checker