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: addition of Cmp in the API #233

Merged
merged 1 commit into from
Jan 20, 2022
Merged
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
9 changes: 6 additions & 3 deletions frontend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ type API interface {
// Add returns res = i1+i2+...in
Add(i1, i2 Variable, in ...Variable) Variable

// Sub returns res = i1 - i2 - ...in
Sub(i1, i2 Variable, in ...Variable) Variable

// Neg returns -i
Neg(i1 Variable) Variable

// Sub returns res = i1 - i2 - ...in
Sub(i1, i2 Variable, in ...Variable) Variable

// Mul returns res = i1 * i2 * ... in
Mul(i1, i2 Variable, in ...Variable) Variable

Expand Down Expand Up @@ -89,6 +89,9 @@ type API interface {
// IsZero returns 1 if a is zero, 0 otherwise
IsZero(i1 Variable) Variable

// Cmp returns 1 if i1>i2, 0 if i1=i2, -1 if i1<i2
Cmp(i1, i2 Variable) Variable

// ---------------------------------------------------------------------------------------------
// Assertions

Expand Down
26 changes: 26 additions & 0 deletions frontend/cs/plonk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,32 @@ func (system *sparseR1CS) IsZero(i1 frontend.Variable) frontend.Variable {
return m
}

// Cmp returns 1 if i1>i2, 0 if i1=i2, -1 if i1<i2
func (system *sparseR1CS) Cmp(i1, i2 frontend.Variable) frontend.Variable {

bi1 := system.ToBinary(i1, system.BitLen())
bi2 := system.ToBinary(i2, system.BitLen())

var res frontend.Variable
res = 0

for i := system.BitLen() - 1; i >= 0; i-- {

iszeroi1 := system.IsZero(bi1[i])
iszeroi2 := system.IsZero(bi2[i])

i1i2 := system.And(bi1[i], iszeroi2)
i2i1 := system.And(bi2[i], iszeroi1)

n := system.Select(i2i1, -1, 0)
m := system.Select(i1i2, 1, n)

res = system.Select(system.IsZero(res), m, res)

}
return res
}

// Println behaves like fmt.Println but accepts Variable as parameter
// whose value will be resolved at runtime when computed by the solver
// Println enables circuit debugging and behaves almost like fmt.Println()
Expand Down
Loading