github.com/consensys/gnark@v0.11.0/internal/regression_tests/issue1246/issue1246_test.go (about)

     1  package issue1246_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/consensys/gnark/frontend"
     7  	"github.com/consensys/gnark/test"
     8  )
     9  
    10  // Circuit definition
    11  // here we aim to catch the case where the API doesn't enforce the condition to be a boolean
    12  type notBoolCond struct {
    13  	Condition, Y1, Y2 frontend.Variable
    14  }
    15  
    16  func (circuit *notBoolCond) Define(api frontend.API) error {
    17  	d := api.Select(circuit.Condition, circuit.Y1, circuit.Y2)
    18  
    19  	// per api definition, d should hold either Y1 or Y2.
    20  	// we have y1 = 2, y2 = 4, condition = 2 (non boolean)
    21  	// r = condition(y1-y2) + y2
    22  	api.AssertIsEqual(d, 0)
    23  
    24  	return nil
    25  }
    26  
    27  func TestSelectConditionNonBool(t *testing.T) {
    28  	assert := test.NewAssert(t)
    29  
    30  	assert.CheckCircuit(&notBoolCond{},
    31  		test.WithInvalidAssignment(&notBoolCond{
    32  			Condition: 2,
    33  			Y1:        2,
    34  			Y2:        4,
    35  		}),
    36  	)
    37  }