MCPcopy Create free account
hub / github.com/PolyhedraZK/ExpanderCompilerCollection / Cmp

Method Cmp

ecgo/builder/api.go:358–383  ·  view source on GitHub ↗

Cmp compares i1 and i2 and returns 1 if i1>i2, 0 if i1=i2, -1 if i1<i2.

(i1, i2 frontend.Variable)

Source from the content-addressed store, hash-verified

356
357// Cmp compares i1 and i2 and returns 1 if i1>i2, 0 if i1=i2, -1 if i1<i2.
358func (builder *builder) Cmp(i1, i2 frontend.Variable) frontend.Variable {
359 nbBits := builder.field.FieldBitLen()
360 // in AssertIsLessOrEq we omitted comparison against modulus for the left
361 // side as if `a+r<b` implies `a<b`, then here we compute the inequality
362 // directly.
363 bi1 := bits.ToBinary(builder, i1, bits.WithNbDigits(nbBits))
364 bi2 := bits.ToBinary(builder, i2, bits.WithNbDigits(nbBits))
365
366 res := newVariable(builder.toVariableId(0))
367
368 for i := builder.field.FieldBitLen() - 1; i >= 0; i-- {
369
370 iszeroi1 := builder.IsZero(bi1[i])
371 iszeroi2 := builder.IsZero(bi2[i])
372
373 i1i2 := builder.And(bi1[i], iszeroi2)
374 i2i1 := builder.And(bi2[i], iszeroi1)
375
376 n := builder.Select(i2i1, -1, 0)
377 m := builder.Select(i1i2, 1, n)
378
379 res = newVariable(builder.toVariableId(builder.Select(builder.IsZero(res), m, res)))
380
381 }
382 return res
383}
384
385// Println is not implemented and will panic if called.
386func (builder *builder) Println(a ...frontend.Variable) {

Callers 5

CheckCircuitFunction · 0.80
CheckCircuitMultiWitnessFunction · 0.80
callHintFunction · 0.80
GetFieldFromOrderFunction · 0.80
GetFieldIdFunction · 0.80

Calls 7

toVariableIdMethod · 0.95
IsZeroMethod · 0.95
AndMethod · 0.95
SelectMethod · 0.95
newVariableFunction · 0.85
ToBinaryMethod · 0.80
FieldBitLenMethod · 0.65

Tested by

no test coverage detected