|
@ -9,13 +9,13 @@ import ( |
|
|
const W uint64 = 7 |
|
|
const W uint64 = 7 |
|
|
const DTH_ROOT uint64 = 18446744069414584320 |
|
|
const DTH_ROOT uint64 = 18446744069414584320 |
|
|
|
|
|
|
|
|
type QuadraticExtensionVariable [2]Variable |
|
|
|
|
|
|
|
|
type QuadraticExtensionVariable [2]GoldilocksVariable |
|
|
|
|
|
|
|
|
func NewQuadraticExtensionVariable(x Variable, y Variable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func NewQuadraticExtensionVariable(x GoldilocksVariable, y GoldilocksVariable) QuadraticExtensionVariable { |
|
|
return QuadraticExtensionVariable{x, y} |
|
|
return QuadraticExtensionVariable{x, y} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p Variable) ToQuadraticExtension() QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p GoldilocksVariable) ToQuadraticExtension() QuadraticExtensionVariable { |
|
|
return NewQuadraticExtensionVariable(p, Zero()) |
|
|
return NewQuadraticExtensionVariable(p, Zero()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -28,35 +28,35 @@ func OneExtension() QuadraticExtensionVariable { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Adds two quadratic extension variables in the Goldilocks field.
|
|
|
// Adds two quadratic extension variables in the Goldilocks field.
|
|
|
func (p *Chip) AddExtension(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) AddExtension(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
c0 := p.Add(a[0], b[0]) |
|
|
c0 := p.Add(a[0], b[0]) |
|
|
c1 := p.Add(a[1], b[1]) |
|
|
c1 := p.Add(a[1], b[1]) |
|
|
return NewQuadraticExtensionVariable(c0, c1) |
|
|
return NewQuadraticExtensionVariable(c0, c1) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Adds two quadratic extension variables in the Goldilocks field without reducing.
|
|
|
// Adds two quadratic extension variables in the Goldilocks field without reducing.
|
|
|
func (p *Chip) AddExtensionNoReduce(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) AddExtensionNoReduce(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
c0 := p.AddNoReduce(a[0], b[0]) |
|
|
c0 := p.AddNoReduce(a[0], b[0]) |
|
|
c1 := p.AddNoReduce(a[1], b[1]) |
|
|
c1 := p.AddNoReduce(a[1], b[1]) |
|
|
return NewQuadraticExtensionVariable(c0, c1) |
|
|
return NewQuadraticExtensionVariable(c0, c1) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Subtracts two quadratic extension variables in the Goldilocks field.
|
|
|
// Subtracts two quadratic extension variables in the Goldilocks field.
|
|
|
func (p *Chip) SubExtension(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) SubExtension(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
c0 := p.Sub(a[0], b[0]) |
|
|
c0 := p.Sub(a[0], b[0]) |
|
|
c1 := p.Sub(a[1], b[1]) |
|
|
c1 := p.Sub(a[1], b[1]) |
|
|
return NewQuadraticExtensionVariable(c0, c1) |
|
|
return NewQuadraticExtensionVariable(c0, c1) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Subtracts two quadratic extension variables in the Goldilocks field without reducing.
|
|
|
// Subtracts two quadratic extension variables in the Goldilocks field without reducing.
|
|
|
func (p *Chip) SubExtensionNoReduce(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) SubExtensionNoReduce(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
c0 := p.SubNoReduce(a[0], b[0]) |
|
|
c0 := p.SubNoReduce(a[0], b[0]) |
|
|
c1 := p.SubNoReduce(a[1], b[1]) |
|
|
c1 := p.SubNoReduce(a[1], b[1]) |
|
|
return NewQuadraticExtensionVariable(c0, c1) |
|
|
return NewQuadraticExtensionVariable(c0, c1) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Multiplies quadratic extension variable in the Goldilocks field.
|
|
|
// Multiplies quadratic extension variable in the Goldilocks field.
|
|
|
func (p *Chip) MulExtension(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) MulExtension(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
product := p.MulExtensionNoReduce(a, b) |
|
|
product := p.MulExtensionNoReduce(a, b) |
|
|
product[0] = p.Reduce(product[0]) |
|
|
product[0] = p.Reduce(product[0]) |
|
|
product[1] = p.Reduce(product[1]) |
|
|
product[1] = p.Reduce(product[1]) |
|
@ -64,7 +64,7 @@ func (p *Chip) MulExtension(a, b QuadraticExtensionVariable) QuadraticExtensionV |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Multiplies quadratic extension variable in the Goldilocks field without reducing.
|
|
|
// Multiplies quadratic extension variable in the Goldilocks field without reducing.
|
|
|
func (p *Chip) MulExtensionNoReduce(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) MulExtensionNoReduce(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
c0o0 := p.MulNoReduce(a[0], b[0]) |
|
|
c0o0 := p.MulNoReduce(a[0], b[0]) |
|
|
c0o1 := p.MulNoReduce(p.MulNoReduce(NewVariable(7), a[1]), b[1]) |
|
|
c0o1 := p.MulNoReduce(p.MulNoReduce(NewVariable(7), a[1]), b[1]) |
|
|
c0 := p.AddNoReduce(c0o0, c0o1) |
|
|
c0 := p.AddNoReduce(c0o0, c0o1) |
|
@ -74,7 +74,7 @@ func (p *Chip) MulExtensionNoReduce(a, b QuadraticExtensionVariable) QuadraticEx |
|
|
|
|
|
|
|
|
// Multiplies two operands a and b and adds to c in the Goldilocks extension field. a * b + c must
|
|
|
// Multiplies two operands a and b and adds to c in the Goldilocks extension field. a * b + c must
|
|
|
// be less than RANGE_CHECK_NB_BITS bits.
|
|
|
// be less than RANGE_CHECK_NB_BITS bits.
|
|
|
func (p *Chip) MulAddExtension(a, b, c QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) MulAddExtension(a, b, c QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
product := p.MulExtensionNoReduce(a, b) |
|
|
product := p.MulExtensionNoReduce(a, b) |
|
|
sum := p.AddExtensionNoReduce(product, c) |
|
|
sum := p.AddExtensionNoReduce(product, c) |
|
|
sum[0] = p.Reduce(sum[0]) |
|
|
sum[0] = p.Reduce(sum[0]) |
|
@ -82,7 +82,7 @@ func (p *Chip) MulAddExtension(a, b, c QuadraticExtensionVariable) QuadraticExte |
|
|
return sum |
|
|
return sum |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *Chip) MulAddExtensionNoReduce(a, b, c QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) MulAddExtensionNoReduce(a, b, c QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
product := p.MulExtensionNoReduce(a, b) |
|
|
product := p.MulExtensionNoReduce(a, b) |
|
|
sum := p.AddExtensionNoReduce(product, c) |
|
|
sum := p.AddExtensionNoReduce(product, c) |
|
|
return sum |
|
|
return sum |
|
@ -90,7 +90,7 @@ func (p *Chip) MulAddExtensionNoReduce(a, b, c QuadraticExtensionVariable) Quadr |
|
|
|
|
|
|
|
|
// Multiplies two operands a and b and subtracts to c in the Goldilocks extension field. a * b - c must
|
|
|
// Multiplies two operands a and b and subtracts to c in the Goldilocks extension field. a * b - c must
|
|
|
// be less than RANGE_CHECK_NB_BITS bits.
|
|
|
// be less than RANGE_CHECK_NB_BITS bits.
|
|
|
func (p *Chip) SubMulExtension(a, b, c QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) SubMulExtension(a, b, c QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
difference := p.SubExtensionNoReduce(a, b) |
|
|
difference := p.SubExtensionNoReduce(a, b) |
|
|
product := p.MulExtensionNoReduce(difference, c) |
|
|
product := p.MulExtensionNoReduce(difference, c) |
|
|
product[0] = p.Reduce(product[0]) |
|
|
product[0] = p.Reduce(product[0]) |
|
@ -99,9 +99,9 @@ func (p *Chip) SubMulExtension(a, b, c QuadraticExtensionVariable) QuadraticExte |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Multiplies quadratic extension variable in the Goldilocks field by a scalar.
|
|
|
// Multiplies quadratic extension variable in the Goldilocks field by a scalar.
|
|
|
func (p *Chip) ScalarMulExtension( |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) ScalarMulExtension( |
|
|
a QuadraticExtensionVariable, |
|
|
a QuadraticExtensionVariable, |
|
|
b Variable, |
|
|
|
|
|
|
|
|
b GoldilocksVariable, |
|
|
) QuadraticExtensionVariable { |
|
|
) QuadraticExtensionVariable { |
|
|
return NewQuadraticExtensionVariable( |
|
|
return NewQuadraticExtensionVariable( |
|
|
p.Mul(a[0], b), |
|
|
p.Mul(a[0], b), |
|
@ -110,8 +110,8 @@ func (p *Chip) ScalarMulExtension( |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Computes an inner product over quadratic extension variable vectors in the Goldilocks field.
|
|
|
// Computes an inner product over quadratic extension variable vectors in the Goldilocks field.
|
|
|
func (p *Chip) InnerProductExtension( |
|
|
|
|
|
constant Variable, |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) InnerProductExtension( |
|
|
|
|
|
constant GoldilocksVariable, |
|
|
startingAcc QuadraticExtensionVariable, |
|
|
startingAcc QuadraticExtensionVariable, |
|
|
pairs [][2]QuadraticExtensionVariable, |
|
|
pairs [][2]QuadraticExtensionVariable, |
|
|
) QuadraticExtensionVariable { |
|
|
) QuadraticExtensionVariable { |
|
@ -126,7 +126,7 @@ func (p *Chip) InnerProductExtension( |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Computes the inverse of a quadratic extension variable in the Goldilocks field.
|
|
|
// Computes the inverse of a quadratic extension variable in the Goldilocks field.
|
|
|
func (p *Chip) InverseExtension(a QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) InverseExtension(a QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
a0IsZero := p.api.IsZero(a[0].Limb) |
|
|
a0IsZero := p.api.IsZero(a[0].Limb) |
|
|
a1IsZero := p.api.IsZero(a[1].Limb) |
|
|
a1IsZero := p.api.IsZero(a[1].Limb) |
|
|
p.api.AssertIsEqual(p.api.Mul(a0IsZero, a1IsZero), frontend.Variable(0)) |
|
|
p.api.AssertIsEqual(p.api.Mul(a0IsZero, a1IsZero), frontend.Variable(0)) |
|
@ -139,12 +139,12 @@ func (p *Chip) InverseExtension(a QuadraticExtensionVariable) QuadraticExtension |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Divides two quadratic extension variables in the Goldilocks field.
|
|
|
// Divides two quadratic extension variables in the Goldilocks field.
|
|
|
func (p *Chip) DivExtension(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) DivExtension(a, b QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
return p.MulExtension(a, p.InverseExtension(b)) |
|
|
return p.MulExtension(a, p.InverseExtension(b)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Exponentiates a quadratic extension variable to some exponent in the Golidlocks field.
|
|
|
// Exponentiates a quadratic extension variable to some exponent in the Golidlocks field.
|
|
|
func (p *Chip) ExpExtension( |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) ExpExtension( |
|
|
a QuadraticExtensionVariable, |
|
|
a QuadraticExtensionVariable, |
|
|
exponent uint64, |
|
|
exponent uint64, |
|
|
) QuadraticExtensionVariable { |
|
|
) QuadraticExtensionVariable { |
|
@ -173,12 +173,12 @@ func (p *Chip) ExpExtension( |
|
|
return product |
|
|
return product |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *Chip) ReduceExtension(x QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) ReduceExtension(x QuadraticExtensionVariable) QuadraticExtensionVariable { |
|
|
return NewQuadraticExtensionVariable(p.Reduce(x[0]), p.Reduce(x[1])) |
|
|
return NewQuadraticExtensionVariable(p.Reduce(x[0]), p.Reduce(x[1])) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Reduces a list of extension field terms with a scalar power in the Goldilocks field.
|
|
|
// Reduces a list of extension field terms with a scalar power in the Goldilocks field.
|
|
|
func (p *Chip) ReduceWithPowers( |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) ReduceWithPowers( |
|
|
terms []QuadraticExtensionVariable, |
|
|
terms []QuadraticExtensionVariable, |
|
|
scalar QuadraticExtensionVariable, |
|
|
scalar QuadraticExtensionVariable, |
|
|
) QuadraticExtensionVariable { |
|
|
) QuadraticExtensionVariable { |
|
@ -197,14 +197,14 @@ func (p *Chip) ReduceWithPowers( |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Outputs whether the quadratic extension variable is zero.
|
|
|
// Outputs whether the quadratic extension variable is zero.
|
|
|
func (p *Chip) IsZero(x QuadraticExtensionVariable) frontend.Variable { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) IsZero(x QuadraticExtensionVariable) frontend.Variable { |
|
|
x0IsZero := p.api.IsZero(x[0].Limb) |
|
|
x0IsZero := p.api.IsZero(x[0].Limb) |
|
|
x1IsZero := p.api.IsZero(x[1].Limb) |
|
|
x1IsZero := p.api.IsZero(x[1].Limb) |
|
|
return p.api.Mul(x0IsZero, x1IsZero) |
|
|
return p.api.Mul(x0IsZero, x1IsZero) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Lookup is similar to select, but returns the first variable if the bit is zero and vice-versa.
|
|
|
// Lookup is similar to select, but returns the first variable if the bit is zero and vice-versa.
|
|
|
func (p *Chip) Lookup( |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) Lookup( |
|
|
b frontend.Variable, |
|
|
b frontend.Variable, |
|
|
x, y QuadraticExtensionVariable, |
|
|
x, y QuadraticExtensionVariable, |
|
|
) QuadraticExtensionVariable { |
|
|
) QuadraticExtensionVariable { |
|
@ -214,7 +214,7 @@ func (p *Chip) Lookup( |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Lookup2 is similar to select2, but returns the first variable if the bit is zero and vice-versa.
|
|
|
// Lookup2 is similar to select2, but returns the first variable if the bit is zero and vice-versa.
|
|
|
func (p *Chip) Lookup2( |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) Lookup2( |
|
|
b0 frontend.Variable, |
|
|
b0 frontend.Variable, |
|
|
b1 frontend.Variable, |
|
|
b1 frontend.Variable, |
|
|
qe0, qe1, qe2, qe3 QuadraticExtensionVariable, |
|
|
qe0, qe1, qe2, qe3 QuadraticExtensionVariable, |
|
@ -225,7 +225,7 @@ func (p *Chip) Lookup2( |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Asserts that two quadratic extension variables are equal.
|
|
|
// Asserts that two quadratic extension variables are equal.
|
|
|
func (p *Chip) AssertIsEqualExtension( |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) AssertIsEqualExtension( |
|
|
a QuadraticExtensionVariable, |
|
|
a QuadraticExtensionVariable, |
|
|
b QuadraticExtensionVariable, |
|
|
b QuadraticExtensionVariable, |
|
|
) { |
|
|
) { |
|
@ -233,7 +233,7 @@ func (p *Chip) AssertIsEqualExtension( |
|
|
p.AssertIsEqual(a[1], b[1]) |
|
|
p.AssertIsEqual(a[1], b[1]) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *Chip) RangeCheckQE(a QuadraticExtensionVariable) { |
|
|
|
|
|
|
|
|
func (p *GoldilocksApi) RangeCheckQE(a QuadraticExtensionVariable) { |
|
|
p.RangeCheck(a[0]) |
|
|
p.RangeCheck(a[0]) |
|
|
p.RangeCheck(a[1]) |
|
|
p.RangeCheck(a[1]) |
|
|
} |
|
|
} |