Skip to content

Commit

Permalink
fix: minimum 1 bit for constant binary decomposition (Consensys#1229)
Browse files Browse the repository at this point in the history
* fix: minimum 1 bit for constant binary decomposition

* fix: remove incorrect optim path for constant

* test: apply ivokub suggested edit
  • Loading branch information
gbotrel authored Jul 31, 2024
1 parent aa6efa4 commit d8ccab5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
6 changes: 0 additions & 6 deletions frontend/cs/r1cs/api_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ func (builder *builder) mustBeLessOrEqVar(a, bound frontend.Variable) {
// but a can be either constant or a wire.

nbBits := builder.cs.FieldBitLen()
if ca, aConst := builder.constantValue(a); aConst {
// if a is a constant, we only need the number of bits of a;
// the binary decomposition of the bound will fail if nbBits(bound) > nbBits(a)
ba := builder.cs.ToBigInt(ca)
nbBits = ba.BitLen()
}

aBits := bits.ToBinary(builder, a, bits.WithNbDigits(nbBits), bits.WithUnconstrainedOutputs(), bits.OmitModulusCheck())
boundBits := bits.ToBinary(builder, bound, bits.WithNbDigits(nbBits))
Expand Down
11 changes: 8 additions & 3 deletions internal/regression_tests/issue1227/issue_1227_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import (
)

type Circuit struct {
X frontend.Variable
constVal int
X frontend.Variable
}

func (circuit *Circuit) Define(api frontend.API) error {
api.AssertIsLessOrEqual(1, circuit.X)
api.AssertIsLessOrEqual(circuit.constVal, circuit.X)
return nil
}

func TestConstantPath(t *testing.T) {
assert := test.NewAssert(t)
assert.CheckCircuit(&Circuit{},
assert.CheckCircuit(&Circuit{constVal: 1},
test.WithValidAssignment(&Circuit{X: 1}), // 1 <= 1 --> true
test.WithInvalidAssignment(&Circuit{X: 0})) // 1 <= 0 --> false
// test edge case where constant is 0
assert.CheckCircuit(&Circuit{constVal: 0},
test.WithValidAssignment(&Circuit{X: 1}), // 0 <= 1 --> true
test.WithValidAssignment(&Circuit{X: 0})) // 0 <= 0 --> true
}

0 comments on commit d8ccab5

Please sign in to comment.