api.Cmp usage coupled with api.Select #808
Unanswered
NikitaMasych
asked this question in
Q&A
Replies: 2 comments 4 replies
-
Not sure without seing the full snippet; but Can you print the error of the |
Beta Was this translation helpful? Give feedback.
4 replies
-
Regarding api.Println, for this version: package main
import (
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/pkg/errors"
)
type circuit struct {
A, B, C, D frontend.Variable
}
func (c *circuit) Define(api frontend.API) error {
cmp := api.Cmp(c.A, c.B) // -1
api.AssertIsBoolean(cmp) // assert does not fail, why?
res := api.Select(cmp, c.C, c.D) // res should be D?
api.Println(res)
return nil
}
func main() {
ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, new(circuit))
if err != nil {
panic(errors.Wrap(err, "failed to compile circuit"))
}
pk, vk, err := groth16.Setup(ccs)
if err != nil {
panic(errors.Wrap(err, "failed to setup pk and vk"))
}
assignment := &circuit{
A: 1,
B: 2,
C: 3,
D: 4,
}
wit, err := frontend.NewWitness(assignment, ecc.BN254.ScalarField())
if err != nil {
panic(errors.Wrap(err, "failed to instantiate new witness"))
}
proof, err := groth16.Prove(ccs, pk, wit)
if err != nil {
panic(errors.Wrap(err, "failed to prove"))
}
publicWitness, err := wit.Public()
if err != nil {
panic(errors.Wrap(err, "failed to extract public witness"))
}
if err := groth16.Verify(proof, vk, publicWitness); err != nil {
panic(errors.Wrap(err, "failed to verify proof"))
}
} Output is:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys, assume we have the following logic
c := api.Select(api.Cmp(a, b), d, e)
, where a, b, c, d, e are of typefrontend.Variable
.Select
method according to docs will return second one (d) if the first argument is true, otherwise the third argument (e). api.Cmp returns -1, 0 or 1. But, in practice, usingapi.Println(c)
I get<unsolved>
being outputted. Even more, usingapi.AssertIsBoolean(api.Cmp(a,b))
I receive no errors. What am I missing and how can one achieve such a logic?Beta Was this translation helpful? Give feedback.
All reactions