Skip to content

Commit 8a7ffdb

Browse files
authored
fix: improve AssertIsCrumb readability and add constant check
Add constant value check at the start of AssertIsCrumb to avoid unnecessary constraint generation for compile-time constants. Use separate variable x instead of overwriting input parameter for better code clarity. Add mathematical comment explaining the polynomial constraint derivation, matching the SCS implementation style.
1 parent 4c62e1a commit 8a7ffdb

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

frontend/cs/r1cs/api_assertions.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,20 @@ func (builder *builder[E]) AssertIsBoolean(i1 frontend.Variable) {
7878
}
7979

8080
func (builder *builder[E]) AssertIsCrumb(i1 frontend.Variable) {
81-
i1 = builder.MulAcc(builder.Mul(-3, i1), i1, i1)
82-
i1 = builder.MulAcc(builder.Mul(2, i1), i1, i1)
83-
builder.AssertIsEqual(i1, 0)
81+
const errorMsg = "AssertIsCrumb: input is not a crumb"
82+
if c, ok := builder.constantValue(i1); ok {
83+
cv := builder.cs.ToBigInt(c)
84+
if cv.IsUint64() && cv.Uint64() < 4 {
85+
return
86+
}
87+
panic(errorMsg)
88+
}
89+
90+
// i1 (i1-1) (i1-2) (i1-3) = (i1² - 3i1) (i1² - 3i1 + 2)
91+
// take X := i1² - 3i1 and we get X (X+2) = 0
92+
x := builder.MulAcc(builder.Mul(-3, i1), i1, i1)
93+
x = builder.MulAcc(builder.Mul(2, x), x, x)
94+
builder.AssertIsEqual(x, 0)
8495
}
8596

8697
// AssertIsLessOrEqual adds assertion in constraint builder (v ⩽ bound)

0 commit comments

Comments
 (0)