bug: ...The usage of func : api.AssertIsLessOrEqual() #1221
Closed
George551556
started this conversation in
General
Replies: 2 comments 5 replies
-
I test the api.api.AssertIsLessOrEqual(v1, v2) in the following case. it works as expected. package less_or_equal
import (
"fmt"
"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/consensys/gnark/test"
"math/rand"
"testing"
)
// Circuit defines a simple circuit
type Circuit struct {
// struct tags on a variable is optional
// default uses variable name and secret visibility.
X frontend.Variable `gnark:"x"`
Y frontend.Variable `gnark:"y"`
}
// Define declares the circuit constraints
func (circuit *Circuit) Define(api frontend.API) error {
api.AssertIsLessOrEqual(circuit.X, circuit.Y)
return nil
}
func TestCubic_LessOrEqual_BN254(t *testing.T) {
field := ecc.BN254.ScalarField()
assert := test.NewAssert(t)
var circuit Circuit
_r1cs, err := frontend.Compile(field, r1cs.NewBuilder, &circuit)
assert.NoError(err)
pk, vk, err := groth16.Setup(_r1cs)
assert.NoError(err)
//smaller success
smaller := &Circuit{
X: 3,
Y: 4,
}
wit, err := frontend.NewWitness(smaller, ecc.BN254.ScalarField())
assert.NoError(err)
proof, err := groth16.Prove(_r1cs, pk, wit)
assert.NoError(err)
pubWit, err := wit.Public()
assert.NoError(err)
err = groth16.Verify(proof, vk, pubWit)
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
fmt.Printf("verification pass\n")
}
//equal success
equal := &Circuit{
X: 3,
Y: 3,
}
wit, err = frontend.NewWitness(equal, ecc.BN254.ScalarField())
assert.NoError(err)
proof, err = groth16.Prove(_r1cs, pk, wit)
assert.NoError(err)
pubWit, err = wit.Public()
assert.NoError(err)
err = groth16.Verify(proof, vk, pubWit)
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
fmt.Printf("verification pass\n")
}
pubWit, err = wit.Public()
assert.NoError(err)
for i := 0; i < 100; i++ {
y := rand.Int()
x := rand.Intn(y + 1)
fmt.Printf("i:%v, x: %v, y: %v\n", i, x, y)
assignment := &Circuit{
X: x,
Y: y,
}
wit, err = frontend.NewWitness(assignment, ecc.BN254.ScalarField())
assert.NoError(err)
proof, err = groth16.Prove(_r1cs, pk, wit)
assert.NoError(err)
pubWit, err = wit.Public()
assert.NoError(err)
err = groth16.Verify(proof, vk, pubWit)
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
fmt.Printf("verification pass\n")
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yup, it would be better if you posted your non-working example. PS! Converted to discussion as seems to be question about gnark usage, not an issue. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Expected Behavior
Actual Behavior
Possible Fix
Steps to Reproduce
Context
Your Environment
HEAD@develop
):Beta Was this translation helpful? Give feedback.
All reactions