Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
liyue201 committed Aug 31, 2023
1 parent 3d9fbff commit 84bdfce
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
21 changes: 11 additions & 10 deletions std/comparison/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"math/big"
)

// LogCeil returns the bit count of n
func LogCeil(n int) int {
// BitCount returns the number of bits of n
func BitCount(n int) int {
m := 0
for ; n > 0; n >>= 1 {
m++
Expand All @@ -25,32 +25,33 @@ func BoolNeg(api frontend.API, a frontend.Variable) frontend.Variable {
return api.Sub(1, a)
}

// IsEqual returns 1 if a = b, otherwise return 0.
func IsEqual(api frontend.API, a frontend.Variable, b frontend.Variable) frontend.Variable {
return api.IsZero(api.Sub(a, b))
}

// IsLess returns 1 if a < b
// a and b must satisfy less than maxBits bits
// IsLess returns 1 if a < b, otherwise return 0.
// a and b must satisfy no more than maxBits bits
func IsLess(api frontend.API, maxBits int, a frontend.Variable, b frontend.Variable) frontend.Variable {
negB := api.Neg(b)
bi1 := api.ToBinary(api.Add(a, OneLsh(uint(maxBits)), negB), maxBits+1)
return api.Sub(frontend.Variable(1), bi1[maxBits])
}

// IsGreater returns 1 if a > b
// a and b must satisfy less than maxBits bits
// IsGreater returns 1 if a > b, otherwise return 0.
// a and b must satisfy no more than maxBits bits
func IsGreater(api frontend.API, maxBits int, a frontend.Variable, b frontend.Variable) frontend.Variable {
return IsLess(api, maxBits, b, a)
}

// IsLessOrEqual returns 1 if a <= b
// a and b must satisfy less than maxBits bits
// IsLessOrEqual returns 1 if a <= b, otherwise return 0
// a and b must satisfy no more than maxBits bits
func IsLessOrEqual(api frontend.API, maxBits int, a frontend.Variable, b frontend.Variable) frontend.Variable {
return BoolNeg(api, IsGreater(api, maxBits, a, b))
}

// IsGreaterOrEqual returns 1 if a >= b
// a and b must satisfy less than maxBits bits
// IsGreaterOrEqual returns 1 if a >= b, otherwise return 0
// a and b must satisfy no more than maxBits bits
func IsGreaterOrEqual(api frontend.API, maxBits int, a frontend.Variable, b frontend.Variable) frontend.Variable {
return BoolNeg(api, IsLess(api, maxBits, a, b))
}
50 changes: 50 additions & 0 deletions std/comparison/comparison_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package comparison

import (
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/test"
"testing"
)

type comparisonCircuit struct {
In [4]frontend.Variable
}

func (c *comparisonCircuit) Define(api frontend.API) error {

api.AssertIsEqual(IsEqual(api, c.In[0], c.In[1]), 1)
api.AssertIsEqual(IsEqual(api, c.In[1], c.In[2]), 0)

for i := 0; i < len(c.In)-1; i++ {
api.AssertIsEqual(IsLessOrEqual(api, 8, c.In[i], c.In[i+1]), 1)
api.AssertIsEqual(IsGreater(api, 8, c.In[i], c.In[i+1]), 0)

api.AssertIsEqual(IsGreaterOrEqual(api, 8, c.In[i+1], c.In[i]), 1)
api.AssertIsEqual(IsLess(api, 8, c.In[i+1], c.In[i]), 0)
}

for i := 1; i < len(c.In)-1; i++ {
api.AssertIsEqual(IsLess(api, 8, c.In[i], c.In[i+1]), 1)
api.AssertIsEqual(IsGreater(api, 8, c.In[i+1], c.In[i]), 1)
}

return nil
}

func TestComparison(t *testing.T) {

witness := comparisonCircuit{}

witness.In[0] = 10
witness.In[1] = 10
witness.In[2] = 67
witness.In[3] = 255

assert := test.NewAssert(t)
assert.ProverSucceeded(&comparisonCircuit{}, &witness,
test.WithCurves(ecc.BN254),
test.WithBackends(backend.GROTH16, backend.PLONK),
test.NoFuzzing())
}
2 changes: 1 addition & 1 deletion std/hash/sha2/sha2.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (d *digest) FixedLengthSum(length frontend.Variable) []uints.U8 {
data := make([]uints.U8, len(d.in))
copy(data, d.in)

maxBits := comparison.LogCeil(len(data)+64*2) + 1
maxBits := comparison.BitCount(len(data)+64*2) + 1

for i := 0; i < 64*2; i++ {
data = append(data, uints.NewU8(0))
Expand Down

0 comments on commit 84bdfce

Please sign in to comment.