Consensys / gnark

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
https://hackmd.io/@gnark
Apache License 2.0
1.35k stars 347 forks source link

Conditionals statements in circuit definition #81

Open gbotrel opened 3 years ago

gbotrel commented 3 years ago

In an imperative programming language, that would be a if / else.

However, it doesn't translate well in a declarative API to define the circuit, as the output of the frontend.Compile method is an arithmetic representation that must encode the various branches.

gnark offers cs.Select(...) API, which is similar to Prolog-like languages.

// Select if b is true, yields i1 else yields i2
func (cs *ConstraintSystem) Select(b Variable, i1, i2 interface{}) Variable {

At a minimum, gnark should provide some syntactic sugar to wrap several instructions, maybe in a lambda, into a a if-like statement.

SherLzp commented 3 years ago

Recently wants to use it, could you give some ideas about how to add if in circuit?

gbotrel commented 3 years ago

hi @SherLzp --> there is a branch, a bit outdated to explore some ideas: https://github.com/ConsenSys/gnark/tree/feat/conditional

this is going to get some attention quite soon, but first we have some PlonK efficiency work on the plate.

gbotrel commented 3 years ago

in particular: https://github.com/ConsenSys/gnark/blob/feat/conditional/internal/backend/circuits/conditional.go

SherLzp commented 3 years ago

Noted. Thanks

SherLzp commented 2 years ago

When will this issue be implemented? @gbotrel