Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(std): add LessThan() and IsEqual() #234

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions std/math/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package math

import "github.com/consensys/gnark/frontend"

type Math struct {
api frontend.API // underlying constraint system
}

func NewMath(api frontend.API) (Math, error) {
return Math{api}, nil
}

// Returns 1 if a < b
func (m *Math) LessThan(a, b frontend.Variable) frontend.Variable {
aBits := m.api.ToBinary(a)
bBits := m.api.ToBinary(b)

var output frontend.Variable
output = 0

// This section performs Compare()
// Output is 1 if a > b
// Output is 0 if a == b
// Output is -1 if a < b

// Backwards due to little endian
for i := len(aBits) - 1; i >= 0; i-- {
m.api.AssertIsBoolean(aBits[i])
m.api.AssertIsBoolean(bBits[i])

aIsOne := aBits[i]
bIsOne := bBits[i]

aIsNotOne := m.api.IsZero(aBits[i])
bIsNotOne := m.api.IsZero(bBits[i])

aBeatsB := m.api.And(aIsOne, bIsNotOne)
bBeatsA := m.api.And(bIsOne, aIsNotOne)

newOutput := m.api.Select(aBeatsB, 1, 0)
newOutput = m.api.Select(bBeatsA, -1, newOutput)

output = m.api.Select(m.api.IsZero(output), newOutput, output)
}

// Now, convert output of Convert() into LessThan()
return m.IsEqual(output, -1)
}

func (m *Math) IsEqual(a, b frontend.Variable) frontend.Variable {
return m.api.IsZero(m.api.Sub(a, b))
}
80 changes: 80 additions & 0 deletions std/math/math_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package math

import (
"testing"

"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/test"
)

type LessThanCircuit struct {
A frontend.Variable `gnark:",public"`
B frontend.Variable `gnark:",public"`
ExpectedOutput frontend.Variable `gnark:",public"`
}

func (c *LessThanCircuit) Define(api frontend.API) error {
math, _ := NewMath(api)
output := math.LessThan(c.A, c.B)

api.AssertIsEqual(output, c.ExpectedOutput)

return nil
}

func TestLessThan(t *testing.T) {
assert := test.NewAssert(t)

var lessThanCircuit LessThanCircuit

assert.ProverSucceeded(&lessThanCircuit, &LessThanCircuit{
A: 9,
B: 10,
ExpectedOutput: 1,
})

assert.ProverSucceeded(&lessThanCircuit, &LessThanCircuit{
A: 10,
B: 10,
ExpectedOutput: 0,
})

assert.ProverSucceeded(&lessThanCircuit, &LessThanCircuit{
A: 11,
B: 10,
ExpectedOutput: 0,
})
}

type IsEqualCircuit struct {
A frontend.Variable `gnark:",public"`
B frontend.Variable `gnark:",public"`
ExpectedOutput frontend.Variable `gnark:",public"`
}

func (c *IsEqualCircuit) Define(api frontend.API) error {
math, _ := NewMath(api)
output := math.IsEqual(c.A, c.B)

api.AssertIsEqual(output, c.ExpectedOutput)

return nil
}

func TestIsEqual(t *testing.T) {
assert := test.NewAssert(t)

var isEqualCircuit IsEqualCircuit

assert.ProverSucceeded(&isEqualCircuit, &IsEqualCircuit{
A: 10,
B: 10,
ExpectedOutput: 1,
})

assert.ProverSucceeded(&isEqualCircuit, &IsEqualCircuit{
A: 9,
B: 10,
ExpectedOutput: 0,
})
}