github.com/consensys/gnark@v0.11.0/constraint/level_builder_test.go (about)

     1  package constraint_test
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/consensys/gnark/constraint/solver"
     9  	"github.com/consensys/gnark/frontend"
    10  	"github.com/consensys/gnark/test"
    11  )
    12  
    13  func idHint(_ *big.Int, in []*big.Int, out []*big.Int) error {
    14  	if len(in) != len(out) {
    15  		return fmt.Errorf("in/out length mismatch %d≠%d", len(in), len(out))
    16  	}
    17  	for i := range in {
    18  		out[i].Set(in[i])
    19  	}
    20  	return nil
    21  }
    22  
    23  type idHintCircuit struct {
    24  	X frontend.Variable
    25  }
    26  
    27  func (c *idHintCircuit) Define(api frontend.API) error {
    28  	x, err := api.Compiler().NewHint(idHint, 1, api.Mul(c.X, c.X))
    29  	if err != nil {
    30  		return err
    31  	}
    32  	api.AssertIsEqual(x[0], api.Mul(c.X, c.X))
    33  	return nil
    34  }
    35  
    36  func TestIdHint(t *testing.T) {
    37  	solver.RegisterHint(idHint)
    38  	assignment := idHintCircuit{0}
    39  
    40  	test.NewAssert(t).CheckCircuit(&idHintCircuit{}, test.WithValidAssignment(&assignment))
    41  }