|
|
@ -50,28 +50,28 @@ func init() { |
|
|
|
} |
|
|
|
|
|
|
|
// A type alias used to represent Goldilocks field elements.
|
|
|
|
type GoldilocksVariable struct { |
|
|
|
type Variable struct { |
|
|
|
Limb frontend.Variable |
|
|
|
} |
|
|
|
|
|
|
|
// Creates a new Goldilocks field element from an existing variable. Assumes that the element is
|
|
|
|
// already reduced.
|
|
|
|
func NewVariable(x frontend.Variable) GoldilocksVariable { |
|
|
|
return GoldilocksVariable{Limb: x} |
|
|
|
func NewVariable(x frontend.Variable) Variable { |
|
|
|
return Variable{Limb: x} |
|
|
|
} |
|
|
|
|
|
|
|
// The zero element in the Golidlocks field.
|
|
|
|
func Zero() GoldilocksVariable { |
|
|
|
func Zero() Variable { |
|
|
|
return NewVariable(0) |
|
|
|
} |
|
|
|
|
|
|
|
// The one element in the Goldilocks field.
|
|
|
|
func One() GoldilocksVariable { |
|
|
|
func One() Variable { |
|
|
|
return NewVariable(1) |
|
|
|
} |
|
|
|
|
|
|
|
// The negative one element in the Goldilocks field.
|
|
|
|
func NegOne() GoldilocksVariable { |
|
|
|
func NegOne() Variable { |
|
|
|
return NewVariable(MODULUS.Uint64() - 1) |
|
|
|
} |
|
|
|
|
|
|
@ -88,38 +88,38 @@ func NewGoldilocksApi(api frontend.API) *GoldilocksApi { |
|
|
|
} |
|
|
|
|
|
|
|
// Adds two field elements such that x + y = z within the Golidlocks field.
|
|
|
|
func (p *GoldilocksApi) Add(a GoldilocksVariable, b GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) Add(a Variable, b Variable) Variable { |
|
|
|
return p.MulAdd(a, NewVariable(1), b) |
|
|
|
} |
|
|
|
|
|
|
|
// Adds two field elements such that x + y = z within the Golidlocks field without reducing.
|
|
|
|
func (p *GoldilocksApi) AddNoReduce(a GoldilocksVariable, b GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) AddNoReduce(a Variable, b Variable) Variable { |
|
|
|
return NewVariable(p.api.Add(a.Limb, b.Limb)) |
|
|
|
} |
|
|
|
|
|
|
|
// Subtracts two field elements such that x + y = z within the Golidlocks field.
|
|
|
|
func (p *GoldilocksApi) Sub(a GoldilocksVariable, b GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) Sub(a Variable, b Variable) Variable { |
|
|
|
return p.MulAdd(b, NewVariable(MODULUS.Uint64()-1), a) |
|
|
|
} |
|
|
|
|
|
|
|
// Subtracts two field elements such that x + y = z within the Golidlocks field without reducing.
|
|
|
|
func (p *GoldilocksApi) SubNoReduce(a GoldilocksVariable, b GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) SubNoReduce(a Variable, b Variable) Variable { |
|
|
|
return NewVariable(p.api.Add(a.Limb, p.api.Mul(b.Limb, MODULUS.Uint64()-1))) |
|
|
|
} |
|
|
|
|
|
|
|
// Multiplies two field elements such that x * y = z within the Golidlocks field.
|
|
|
|
func (p *GoldilocksApi) Mul(a GoldilocksVariable, b GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) Mul(a Variable, b Variable) Variable { |
|
|
|
return p.MulAdd(a, b, Zero()) |
|
|
|
} |
|
|
|
|
|
|
|
// Multiplies two field elements such that x * y = z within the Golidlocks field without reducing.
|
|
|
|
func (p *GoldilocksApi) MulNoReduce(a GoldilocksVariable, b GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) MulNoReduce(a Variable, b Variable) Variable { |
|
|
|
return NewVariable(p.api.Mul(a.Limb, b.Limb)) |
|
|
|
} |
|
|
|
|
|
|
|
// Multiplies two field elements and adds a field element such that x * y + z = c within the
|
|
|
|
// Golidlocks field.
|
|
|
|
func (p *GoldilocksApi) MulAdd(a GoldilocksVariable, b GoldilocksVariable, c GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) MulAdd(a Variable, b Variable, c Variable) Variable { |
|
|
|
result, err := p.api.Compiler().NewHint(MulAddHint, 2, a.Limb, b.Limb, c.Limb) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
@ -140,7 +140,7 @@ func (p *GoldilocksApi) MulAdd(a GoldilocksVariable, b GoldilocksVariable, c Gol |
|
|
|
|
|
|
|
// Multiplies two field elements and adds a field element such that x * y + z = c within the
|
|
|
|
// Golidlocks field without reducing.
|
|
|
|
func (p *GoldilocksApi) MulAddNoReduce(a GoldilocksVariable, b GoldilocksVariable, c GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) MulAddNoReduce(a Variable, b Variable, c Variable) Variable { |
|
|
|
return p.AddNoReduce(p.MulNoReduce(a, b), c) |
|
|
|
} |
|
|
|
|
|
|
@ -168,7 +168,7 @@ func MulAddHint(_ *big.Int, inputs []*big.Int, results []*big.Int) error { |
|
|
|
} |
|
|
|
|
|
|
|
// Reduces a field element x such that x % MODULUS = y.
|
|
|
|
func (p *GoldilocksApi) Reduce(x GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) Reduce(x Variable) Variable { |
|
|
|
// Witness a `quotient` and `remainder` such that:
|
|
|
|
//
|
|
|
|
// MODULUS * quotient + remainder = x
|
|
|
@ -192,7 +192,7 @@ func (p *GoldilocksApi) Reduce(x GoldilocksVariable) GoldilocksVariable { |
|
|
|
} |
|
|
|
|
|
|
|
// Reduces a field element x such that x % MODULUS = y.
|
|
|
|
func (p *GoldilocksApi) ReduceWithMaxBits(x GoldilocksVariable, maxNbBits uint64) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) ReduceWithMaxBits(x Variable, maxNbBits uint64) Variable { |
|
|
|
// Witness a `quotient` and `remainder` such that:
|
|
|
|
//
|
|
|
|
// MODULUS * quotient + remainder = x
|
|
|
@ -227,7 +227,7 @@ func ReduceHint(_ *big.Int, inputs []*big.Int, results []*big.Int) error { |
|
|
|
} |
|
|
|
|
|
|
|
// Computes the inverse of a field element x such that x * x^-1 = 1.
|
|
|
|
func (p *GoldilocksApi) Inverse(x GoldilocksVariable) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) Inverse(x Variable) Variable { |
|
|
|
result, err := p.api.Compiler().NewHint(InverseHint, 1, x.Limb) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
@ -261,7 +261,7 @@ func InverseHint(_ *big.Int, inputs []*big.Int, results []*big.Int) error { |
|
|
|
} |
|
|
|
|
|
|
|
// Computes a field element raised to some power.
|
|
|
|
func (p *GoldilocksApi) Exp(x GoldilocksVariable, k *big.Int) GoldilocksVariable { |
|
|
|
func (p *GoldilocksApi) Exp(x Variable, k *big.Int) Variable { |
|
|
|
if k.IsUint64() && k.Uint64() == 0 { |
|
|
|
return One() |
|
|
|
} |
|
|
@ -306,7 +306,7 @@ func SplitLimbsHint(_ *big.Int, inputs []*big.Int, results []*big.Int) error { |
|
|
|
} |
|
|
|
|
|
|
|
// Range checks a field element x to be less than the Golidlocks modulus 2 ^ 64 - 2 ^ 32 + 1.
|
|
|
|
func (p *GoldilocksApi) RangeCheck(x GoldilocksVariable) { |
|
|
|
func (p *GoldilocksApi) RangeCheck(x Variable) { |
|
|
|
// The Goldilocks' modulus is 2^64 - 2^32 + 1, which is:
|
|
|
|
//
|
|
|
|
// 1111111111111111111111111111111100000000000000000000000000000001
|
|
|
@ -350,7 +350,7 @@ func (p *GoldilocksApi) RangeCheck(x GoldilocksVariable) { |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
func (p *GoldilocksApi) AssertIsEqual(x, y GoldilocksVariable) { |
|
|
|
func (p *GoldilocksApi) AssertIsEqual(x, y Variable) { |
|
|
|
p.api.AssertIsEqual(x.Limb, y.Limb) |
|
|
|
} |
|
|
|
|
|
|
|