Help us shape the future of Go! Take the 2026 Go Developer Survey

bits

package
v0.16.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 6 Imported by: 90

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertIsTrit

func AssertIsTrit(api frontend.API, v frontend.Variable)

AssertIsTrit constrains digit to be 0, 1 or 2.

func FromBase

func FromBase(api frontend.API, base Base, digits []frontend.Variable, opts ...BaseConversionOption) frontend.Variable

FromBase compute from a set of digits its canonical representation in little-endian order. For example for base 2, it returns Σbi = Σ (2**i * digits[i])

func FromBinary

func FromBinary(api frontend.API, digits []frontend.Variable, opts ...BaseConversionOption) frontend.Variable

FromBinary is an alias of FromBase(api, Binary, digits)

func FromTernary

func FromTernary(api frontend.API, digits []frontend.Variable, opts ...BaseConversionOption) frontend.Variable

FromTernary is an alias of FromBase(api, Ternary, digits)

func GetHints added in v0.9.0

func GetHints() []solver.Hint

func ToBase

func ToBase(api frontend.API, base Base, v frontend.Variable, opts ...BaseConversionOption) []frontend.Variable

ToBase decomposes scalar v into digits in given base using options opts. The decomposition is in little-endian order.

func ToBinary

ToBinary is an alias of ToBase(api, Binary, v, opts)

func ToTernary

func ToTernary(api frontend.API, v frontend.Variable, opts ...BaseConversionOption) []frontend.Variable

ToTernary is an alias of ToBase(api, Ternary, v, opts...)

Types

type Base

type Base uint8

Base defines the base for decomposing the scalar into digits.

const (
	// Binary base decomposes scalar into bits (0-1)
	Binary Base = 2

	// Ternary base decomposes scalar into trits (0-1-2)
	Ternary Base = 3
)

type BaseConversionOption

type BaseConversionOption func(opt *baseConversionConfig) error

BaseConversionOption configures the behaviour of scalar decomposition.

func OmitModulusCheck added in v0.9.0

func OmitModulusCheck() BaseConversionOption

OmitModulusCheck omits the comparison against native field modulus in case the bitlength of the decomposed value (if WithNbDigits not set or set to bitlength of the native modulus) eqals bitlength of the modulus.

The check is otherwise required as there are possibly multiple correct binary decompositions. For example, when decomposing small a the decomposition could return the slices for both a or a+r, where r is the native modulus and the enforced constraints are correct due to implicit modular reduction by r.

This option can be used in case the decomposed output is manually checked to be unique or if uniqueness is not required.

func WithNbDigits

func WithNbDigits(nbDigits int) BaseConversionOption

WithNbDigits sets the resulting number of digits (nbDigits) to be used in the base conversion.

nbDigits must be > 0. If nbDigits is lower than the length of full decomposition and WithUnconstrainedOutputs option is not used, then the conversion functions will generate an unsatisfiable constraint.

If nbDigits is larger than the bitlength of the modulus, then the returned slice has length nbDigits with excess bits being 0.

If WithNbDigits option is not set, then the full decomposition is returned.

func WithUnconstrainedInputs

func WithUnconstrainedInputs() BaseConversionOption

WithUnconstrainedInputs indicates to the FromBase apis to constrain its inputs (digits) to ensure they are valid digits in base b. For example, FromBinary without this option will add 1 constraint per bit to ensure it is either 0 or 1.

func WithUnconstrainedOutputs

func WithUnconstrainedOutputs() BaseConversionOption

WithUnconstrainedOutputs sets the bit conversion API to NOT constrain the output bits. This is UNSAFE but is useful when the outputs are already constrained by other circuit constraints. The sum of the digits will is constrained like so Σbi = Σ (base**i * digits[i]) But the individual digits are not constrained to be valid digits in base b.

func WithUpperBound added in v0.16.4

func WithUpperBound(bound *big.Int) BaseConversionOption

WithUpperBound sets the constant the decomposed value is checked against, instead of the default native field modulus minus one.

The default check exists because a decomposition is only unique below the modulus: for a small value a, both a and a+r decompose correctly, where r is the native modulus, as the constraints are satisfied under the implicit modular reduction. Checking against a smaller constant is a strictly stronger statement, so it keeps that uniqueness and additionally pins the value into the caller's own range.

bound must be positive and must not exceed the native modulus minus one; a larger bound would readmit the multiple-decomposition case the check exists to prevent.

Setting this option always enforces the comparison, including when WithNbDigits is lower than the bitlength of the modulus, since the caller is asking for a bound the digit count alone does not imply. It cannot be combined with OmitModulusCheck, and it only applies to binary conversion.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL