diff --git a/internal/stats/latest.stats b/internal/stats/latest.stats index ca6fe17b4f..f4d2c47616 100644 Binary files a/internal/stats/latest.stats and b/internal/stats/latest.stats differ diff --git a/internal/stats/snippet.go b/internal/stats/snippet.go index 839d9475e6..f6716a3bce 100644 --- a/internal/stats/snippet.go +++ b/internal/stats/snippet.go @@ -70,12 +70,6 @@ func initSnippets() { registerSnippet("math/bits.ToTernary/unconstrained", func(api frontend.API, newVariable func() frontend.Variable) { _ = bits.ToTernary(api, newVariable(), bits.WithUnconstrainedOutputs()) }) - registerSnippet("math/bits.ToNAF", func(api frontend.API, newVariable func() frontend.Variable) { - _ = bits.ToNAF(api, newVariable()) - }) - registerSnippet("math/bits.ToNAF/unconstrained", func(api frontend.API, newVariable func() frontend.Variable) { - _ = bits.ToNAF(api, newVariable(), bits.WithUnconstrainedOutputs()) - }) registerSnippet("hash/mimc", func(api frontend.API, newVariable func() frontend.Variable) { mimc, _ := mimc.NewMiMC(api) diff --git a/std/math/bits/hints.go b/std/math/bits/hints.go index bb3da6d13c..2266b4bb3a 100644 --- a/std/math/bits/hints.go +++ b/std/math/bits/hints.go @@ -1,7 +1,6 @@ package bits import ( - "errors" "math/big" "github.com/consensys/gnark/constraint/solver" @@ -12,7 +11,6 @@ func GetHints() []solver.Hint { ithBit, nBits, nTrits, - nNaf, } } @@ -61,54 +59,3 @@ func nTrits(_ *big.Int, inputs []*big.Int, results []*big.Int) error { return nil } - -// NNAF returns the NAF decomposition of the input. The number of digits is -// defined by the number of elements in the results slice. -func nNaf(_ *big.Int, inputs []*big.Int, results []*big.Int) error { - n := inputs[0] - return nafDecomposition(n, results) -} - -// nafDecomposition gets the naf decomposition of a big number -func nafDecomposition(a *big.Int, results []*big.Int) error { - if a == nil || a.Sign() == -1 { - return errors.New("invalid input to naf decomposition; negative (or nil) big.Int not supported") - } - - var zero, one, three big.Int - - one.SetUint64(1) - three.SetUint64(3) - - n := 0 - - // some buffers - var buf, aCopy big.Int - aCopy.Set(a) - - for aCopy.Cmp(&zero) != 0 && n < len(results) { - - // if aCopy % 2 == 0 - buf.And(&aCopy, &one) - - // aCopy even - if buf.Cmp(&zero) == 0 { - results[n].SetUint64(0) - } else { // aCopy odd - buf.And(&aCopy, &three) - if buf.IsUint64() && buf.Uint64() == 3 { - results[n].SetInt64(-1) - aCopy.Add(&aCopy, &one) - } else { - results[n].SetUint64(1) - } - } - aCopy.Rsh(&aCopy, 1) - n++ - } - for ; n < len(results); n++ { - results[n].SetUint64(0) - } - - return nil -} diff --git a/std/math/bits/naf.go b/std/math/bits/naf.go deleted file mode 100644 index 56b4b9e468..0000000000 --- a/std/math/bits/naf.go +++ /dev/null @@ -1,50 +0,0 @@ -package bits - -import ( - "math/big" - - "github.com/consensys/gnark/frontend" -) - -// ToNAF returns the NAF decomposition of given input. -// The non-adjacent form (NAF) of a number is a unique signed-digit representation, -// in which non-zero values cannot be adjacent. For example, NAF(13) = [1, 0, -1, 0, 1]. -func ToNAF(api frontend.API, v frontend.Variable, opts ...BaseConversionOption) []frontend.Variable { - // parse options - cfg := baseConversionConfig{ - NbDigits: api.Compiler().FieldBitLen(), - UnconstrainedOutputs: false, - } - - for _, o := range opts { - if err := o(&cfg); err != nil { - panic(err) - } - } - - c := big.NewInt(1) - - bits, err := api.Compiler().NewHint(nNaf, cfg.NbDigits, v) - if err != nil { - panic(err) - } - - var Σbi frontend.Variable - Σbi = 0 - for i := 0; i < cfg.NbDigits; i++ { - Σbi = api.Add(Σbi, api.Mul(bits[i], c)) - c.Lsh(c, 1) - if !cfg.UnconstrainedOutputs { - // b * (1 - b) * (1 + b) == 0 - // TODO this adds 3 constraint, not 2. Need api.Compiler().AddConstraint(...) - b := bits[i] - y := api.Mul(api.Sub(1, b), api.Add(1, b)) - api.AssertIsEqual(api.Mul(b, y), 0) - } - } - - // record the constraint Σ (2**i * b[i]) == v - api.AssertIsEqual(Σbi, v) - - return bits -} diff --git a/std/math/bits/naf_test.go b/std/math/bits/naf_test.go deleted file mode 100644 index 2ead74a682..0000000000 --- a/std/math/bits/naf_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package bits_test - -import ( - "testing" - - "github.com/consensys/gnark/frontend" - "github.com/consensys/gnark/std/math/bits" - "github.com/consensys/gnark/test" -) - -type toNAFCircuit struct { - A frontend.Variable - B0, B1, B2, B3, B4 frontend.Variable -} - -func (c *toNAFCircuit) Define(api frontend.API) error { - // to binary - b := bits.ToNAF(api, c.A, bits.WithNbDigits(6)) - api.AssertIsEqual(b[0], c.B0) - api.AssertIsEqual(b[1], c.B1) - api.AssertIsEqual(b[2], c.B2) - api.AssertIsEqual(b[3], c.B3) - api.AssertIsEqual(b[4], c.B4) - api.AssertIsEqual(b[5], 0) - - return nil -} - -func TestToNAF(t *testing.T) { - assert := test.NewAssert(t) - assert.ProverSucceeded(&toNAFCircuit{}, &toNAFCircuit{A: 13, B0: 1, B1: 0, B2: -1, B3: 0, B4: 1}) -}