diff --git a/goldilocks/base.go b/goldilocks/base.go index 8c9e1d6..57fec90 100644 --- a/goldilocks/base.go +++ b/goldilocks/base.go @@ -18,6 +18,7 @@ import ( "fmt" "math" "math/big" + "os" "github.com/consensys/gnark-crypto/field/goldilocks" "github.com/consensys/gnark/constraint/solver" @@ -86,7 +87,16 @@ type Chip struct { // Creates a new Goldilocks Chip. func New(api frontend.API) *Chip { - rangeChecker := rangecheck.New(api) + use_bit_decomp := os.Getenv("USE_BIT_DECOMPOSITION_RANGE_CHECK") + + var rangeChecker frontend.Rangechecker + + // If USE_BIT_DECOMPOSITION_RANGE_CHECK is not set, then use the std.rangecheck New function + if use_bit_decomp == "" { + rangeChecker = rangecheck.New(api) + } else { + rangeChecker = bitDecompChecker{api: api} + } return &Chip{api: api, rangeChecker: rangeChecker} } diff --git a/goldilocks/utils.go b/goldilocks/utils.go index 654230f..3440c7f 100644 --- a/goldilocks/utils.go +++ b/goldilocks/utils.go @@ -4,6 +4,7 @@ import ( "math/big" "github.com/consensys/gnark/frontend" + "github.com/consensys/gnark/std/math/bits" ) func StrArrayToBigIntArray(input []string) []big.Int { @@ -43,3 +44,11 @@ func Uint64ArrayToQuadraticExtensionArray(input [][]uint64) []QuadraticExtension } return output } + +type bitDecompChecker struct { + api frontend.API +} + +func (pl bitDecompChecker) Check(v frontend.Variable, nbBits int) { + bits.ToBinary(pl.api, v, bits.WithNbDigits(nbBits)) +}