Skip to content

feat: constant value binary decomposition #1510

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

Merged
merged 10 commits into from
Jun 6, 2025
Prev Previous commit
Next Next commit
feat: implement recomposition for constants
  • Loading branch information
ivokub committed Jun 6, 2025
commit c65bb76d6674940e4912995511ef210bffb5393c
26 changes: 26 additions & 0 deletions std/math/bits/conversion_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ func fromBinary(api frontend.API, digits []frontend.Variable, opts ...BaseConver
panic(err)
}
}
// check if the inputs are all constant. In this case, recompose without adding any constraints.
allConst := true
constDigits := make([]*big.Int, len(digits))
for i := range digits {
if constV, ok := api.Compiler().ConstantValue(digits[i]); !ok {
// there is at least one digit which is not a constant. Break out to the general case.
allConst = false
break
} else {
constDigits[len(digits)-i-1] = constV
}
}
if allConst {
res := new(big.Int)
for _, d := range constDigits {
// check that the inputs are binary digits. 1 has 1 bit and 0 has 0 bits.
if d.BitLen() > 1 {
panic(fmt.Sprintf("constant input to FromBinary has more than 1 bit. Has %d bits", d.BitLen()))
}
res.Lsh(res, 1)
res.Add(res, d)
}
res.Mod(res, api.Compiler().Field()) // ensure the result is mod reduced
return res
}
// if we are here, then we have at least one unconstrained input or the inputs are not constant.

// Σbi = Σ (2**i * b[i])
Σbi := frontend.Variable(0)
Expand Down