mirror of
https://github.com/arnaucube/gnark-plonky2-verifier.git
synced 2026-01-12 00:51:33 +01:00
Refactor gate deserialization (#16)
* helper functions for coset_interpolation_gate * coset interpolation gate working * hard coded the coset gate (for now) * refactored gate serialization
This commit is contained in:
@@ -3,8 +3,27 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var aritheticExtensionGateRegex = regexp.MustCompile("ArithmeticExtensionGate { num_ops: (?P<numOps>[0-9]+) }")
|
||||||
|
|
||||||
|
func deserializeExtensionArithmeticGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "ArithmeticExtensionGate { num_ops: 10 }"
|
||||||
|
numOps, hasNumOps := parameters["numOps"]
|
||||||
|
if !hasNumOps {
|
||||||
|
panic("Missing field num_ops in ArithmeticExtensionGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numOpsInt, err := strconv.Atoi(numOps)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid num_ops field in ArithmeticExtensionGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewArithmeticExtensionGate(uint64(numOpsInt))
|
||||||
|
}
|
||||||
|
|
||||||
type ArithmeticExtensionGate struct {
|
type ArithmeticExtensionGate struct {
|
||||||
numOps uint64
|
numOps uint64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,28 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var aritheticGateRegex = regexp.MustCompile("ArithmeticGate { num_ops: (?P<numOps>[0-9]+) }")
|
||||||
|
|
||||||
|
func deserializeArithmeticGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "ArithmeticGate { num_ops: 10 }"
|
||||||
|
|
||||||
|
numOps, hasNumOps := parameters["numOps"]
|
||||||
|
if !hasNumOps {
|
||||||
|
panic("no num_ops field in ArithmeticGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numOpsInt, err := strconv.Atoi(numOps)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid num_ops field in ArithmeticGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewArithmeticGate(uint64(numOpsInt))
|
||||||
|
}
|
||||||
|
|
||||||
type ArithmeticGate struct {
|
type ArithmeticGate struct {
|
||||||
numOps uint64
|
numOps uint64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,37 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var baseSumGateRegex = regexp.MustCompile("BaseSumGate { num_limbs: (?P<numLimbs>[0-9]+) } \\+ Base: (?P<base>[0-9]+)")
|
||||||
|
|
||||||
|
func deserializeBaseSumGate(parameters map[string]string) gate {
|
||||||
|
|
||||||
|
// Has the format "BaseSumGate { num_limbs: 32 } + Base: 2"
|
||||||
|
numLimbs, hasNumLimbs := parameters["numLimbs"]
|
||||||
|
base, hasBase := parameters["base"]
|
||||||
|
if !hasNumLimbs || !hasBase {
|
||||||
|
panic("Missing field num_limbs or base in BaseSumGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numLimbsInt, err := strconv.Atoi(numLimbs)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid num_limbs field in BaseSumGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
baseInt, err := strconv.Atoi(base)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid base field in BaseSumGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewBaseSumGate(uint64(numLimbsInt), uint64(baseInt))
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
WIRE_SUM = 0
|
BASESUM_GATE_WIRE_SUM = 0
|
||||||
START_LIMBS = 1
|
BASESUM_GATE_START_LIMBS = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseSumGate struct {
|
type BaseSumGate struct {
|
||||||
@@ -29,14 +55,14 @@ func (g *BaseSumGate) Id() string {
|
|||||||
func (g *BaseSumGate) limbs() []uint64 {
|
func (g *BaseSumGate) limbs() []uint64 {
|
||||||
limbIndices := make([]uint64, g.numLimbs)
|
limbIndices := make([]uint64, g.numLimbs)
|
||||||
for i := uint64(0); i < g.numLimbs; i++ {
|
for i := uint64(0); i < g.numLimbs; i++ {
|
||||||
limbIndices[i] = uint64(START_LIMBS + i)
|
limbIndices[i] = uint64(BASESUM_GATE_START_LIMBS + i)
|
||||||
}
|
}
|
||||||
|
|
||||||
return limbIndices
|
return limbIndices
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *BaseSumGate) EvalUnfiltered(p *PlonkChip, vars EvaluationVars) []QuadraticExtension {
|
func (g *BaseSumGate) EvalUnfiltered(p *PlonkChip, vars EvaluationVars) []QuadraticExtension {
|
||||||
sum := vars.localWires[WIRE_SUM]
|
sum := vars.localWires[BASESUM_GATE_WIRE_SUM]
|
||||||
limbs := make([]QuadraticExtension, g.numLimbs)
|
limbs := make([]QuadraticExtension, g.numLimbs)
|
||||||
limbIndices := g.limbs()
|
limbIndices := g.limbs()
|
||||||
for i, limbIdx := range limbIndices {
|
for i, limbIdx := range limbIndices {
|
||||||
|
|||||||
@@ -3,8 +3,27 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var constantGateRegex = regexp.MustCompile("ConstantGate { num_consts: (?P<numConsts>[0-9]+) }")
|
||||||
|
|
||||||
|
func deserializeConstantGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "ConstantGate { num_consts: 2 }"
|
||||||
|
numConsts, hasNumConsts := parameters["numConsts"]
|
||||||
|
if !hasNumConsts {
|
||||||
|
panic("Missing field num_consts in ConstantGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numConstsInt, err := strconv.Atoi(numConsts)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid num_consts field in ConstantGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewConstantGate(uint64(numConstsInt))
|
||||||
|
}
|
||||||
|
|
||||||
type ConstantGate struct {
|
type ConstantGate struct {
|
||||||
numConsts uint64
|
numConsts uint64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,49 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/consensys/gnark-crypto/field/goldilocks"
|
"github.com/consensys/gnark-crypto/field/goldilocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var cosetInterpolationGateRegex = regexp.MustCompile("CosetInterpolationGate { subgroup_bits: (?P<subgroupBits>[0-9]+), degree: (?P<degree>[0-9]+), barycentric_weights: \\[(?P<barycentricWeights>[0-9, ]+)\\], _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>")
|
||||||
|
|
||||||
|
func deserializeCosetInterpolationGate(parameters map[string]string) gate {
|
||||||
|
// Has the format CosetInterpolationGate { subgroup_bits: 4, degree: 6, barycentric_weights: [17293822565076172801, 18374686475376656385, 18446744069413535745, 281474976645120, 17592186044416, 18446744069414584577, 18446744000695107601, 18446744065119617025, 1152921504338411520, 72057594037927936, 18446744069415632897, 18446462594437939201, 18446726477228539905, 18446744069414584065, 68719476720, 4294967296], _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>
|
||||||
|
subgroupBits, hasSubgroupBits := parameters["subgroupBits"]
|
||||||
|
degree, hasDegree := parameters["degree"]
|
||||||
|
barycentricWeights, hasBarycentricWeights := parameters["barycentricWeights"]
|
||||||
|
|
||||||
|
if !hasSubgroupBits || !hasDegree || !hasBarycentricWeights {
|
||||||
|
panic("missing subgroupBits, degree or barycentricWeights in CosetInterpolationGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
subgroupBitsInt, err := strconv.ParseUint(subgroupBits, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic("invalid subgroupBits in CosetInterpolationGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
degreeInt, err := strconv.ParseUint(degree, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic("invalid degree in CosetInterpolationGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
barycentricWeightsStr := strings.Split(barycentricWeights, ",")
|
||||||
|
barycentricWeightsInt := make([]goldilocks.Element, len(barycentricWeightsStr))
|
||||||
|
for i, barycentricWeightStr := range barycentricWeightsStr {
|
||||||
|
barycentricWeightStr = strings.TrimSpace(barycentricWeightStr)
|
||||||
|
barycentricWeightInt, err := strconv.ParseUint(barycentricWeightStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic("invalid barycentricWeights in CosetInterpolationGate")
|
||||||
|
}
|
||||||
|
barycentricWeightsInt[i].SetUint64(barycentricWeightInt)
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewCosetInterpolationGate(subgroupBitsInt, degreeInt, barycentricWeightsInt)
|
||||||
|
}
|
||||||
|
|
||||||
type CosetInterpolationGate struct {
|
type CosetInterpolationGate struct {
|
||||||
subgroupBits uint64
|
subgroupBits uint64
|
||||||
degree uint64
|
degree uint64
|
||||||
|
|||||||
@@ -3,8 +3,27 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var exponentiationGateRegex = regexp.MustCompile("ExponentiationGate { num_power_bits: (?P<numPowerBits>[0-9]+), _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=(?P<base>[0-9]+)>")
|
||||||
|
|
||||||
|
func deserializeExponentiationGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "ExponentiationGate { num_power_bits: 67, _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>"
|
||||||
|
numPowerBits, hasNumPowerBits := parameters["numPowerBits"]
|
||||||
|
if !hasNumPowerBits {
|
||||||
|
panic("Missing field num_power_bits in ExponentiationGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numPowerBitsInt, err := strconv.Atoi(numPowerBits)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid num_power_bits field in ExponentiationGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewExponentiationGate(uint64(numPowerBitsInt))
|
||||||
|
}
|
||||||
|
|
||||||
type ExponentiationGate struct {
|
type ExponentiationGate struct {
|
||||||
numPowerBits uint64
|
numPowerBits uint64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/consensys/gnark-crypto/field/goldilocks"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type gate interface {
|
type gate interface {
|
||||||
@@ -15,231 +11,39 @@ type gate interface {
|
|||||||
EvalUnfiltered(p *PlonkChip, vars EvaluationVars) []QuadraticExtension
|
EvalUnfiltered(p *PlonkChip, vars EvaluationVars) []QuadraticExtension
|
||||||
}
|
}
|
||||||
|
|
||||||
func GateInstanceFromId(gateId string) gate {
|
var gateRegexHandlers = map[*regexp.Regexp]func(parameters map[string]string) gate{
|
||||||
if strings.HasPrefix(gateId, "ArithmeticGate") {
|
aritheticGateRegex: deserializeArithmeticGate,
|
||||||
numOpsRaw := strings.Split(gateId, ":")[1]
|
aritheticExtensionGateRegex: deserializeExtensionArithmeticGate,
|
||||||
numOpsRaw = strings.Split(numOpsRaw, "}")[0]
|
baseSumGateRegex: deserializeBaseSumGate,
|
||||||
numOpsRaw = strings.TrimSpace(numOpsRaw)
|
constantGateRegex: deserializeConstantGate,
|
||||||
numOps, err := strconv.Atoi(numOpsRaw)
|
cosetInterpolationGateRegex: deserializeCosetInterpolationGate,
|
||||||
if err != nil {
|
exponentiationGateRegex: deserializeExponentiationGate,
|
||||||
panic("Invalid gate ID for ArithmeticGate")
|
mulExtensionGateRegex: deserializeMulExtensionGate,
|
||||||
}
|
noopGateRegex: deserializeNoopGate,
|
||||||
return NewArithmeticGate(uint64(numOps))
|
poseidonGateRegex: deserializePoseidonGate,
|
||||||
}
|
publicInputGateRegex: deserializePublicInputGate,
|
||||||
|
randomAccessGateRegex: deserializeRandomAccessGate,
|
||||||
if strings.HasPrefix(gateId, "ConstantGate") {
|
reducingExtensionGateRegex: deserializeReducingExtensionGate,
|
||||||
numConstsRaw := strings.Split(gateId, ":")[1]
|
reducingGateRegex: deserializeReducingGate,
|
||||||
numConstsRaw = strings.Split(numConstsRaw, "}")[0]
|
|
||||||
numConstsRaw = strings.TrimSpace(numConstsRaw)
|
|
||||||
numConsts, err := strconv.Atoi(numConstsRaw)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid gate ID")
|
|
||||||
}
|
|
||||||
return NewConstantGate(uint64(numConsts))
|
|
||||||
}
|
|
||||||
|
|
||||||
if gateId == "NoopGate" {
|
|
||||||
return NewNoopGate()
|
|
||||||
}
|
|
||||||
|
|
||||||
if gateId == "PublicInputGate" {
|
|
||||||
return NewPublicInputGate()
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(gateId, "PoseidonGate") {
|
|
||||||
return NewPoseidonGate()
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(gateId, "BaseSumGate") {
|
|
||||||
// Has the format "BaseSumGate { num_limbs: 32 } + Base: 2"
|
|
||||||
|
|
||||||
regEx := "BaseSumGate { num_limbs: (?P<numLimbs>[0-9]+) } \\+ Base: (?P<base>[0-9]+)"
|
|
||||||
r, err := regexp.Compile(regEx)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid BaseSumGate regular expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := getRegExMatches(r, gateId)
|
|
||||||
numLimbs, hasNumLimbs := matches["numLimbs"]
|
|
||||||
base, hasBase := matches["base"]
|
|
||||||
if !hasNumLimbs || !hasBase {
|
|
||||||
panic("Invalid BaseSumGate ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewBaseSumGate(uint64(numLimbs), uint64(base))
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(gateId, "RandomAccessGate") {
|
|
||||||
// Has the format "RandomAccessGate { bits: 2, num_copies: 13, num_extra_constants: 2, _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>"
|
|
||||||
|
|
||||||
regEx := "RandomAccessGate { bits: (?P<bits>[0-9]+), num_copies: (?P<numCopies>[0-9]+), num_extra_constants: (?P<numExtraConstants>[0-9]+), _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=(?P<base>[0-9]+)>"
|
|
||||||
r, err := regexp.Compile(regEx)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid RandomAccessGate regular expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := getRegExMatches(r, gateId)
|
|
||||||
bits, hasBits := matches["bits"]
|
|
||||||
numCopies, hasNumCopies := matches["numCopies"]
|
|
||||||
numExtraConstants, hasNumExtraConstants := matches["numExtraConstants"]
|
|
||||||
if !hasBits || !hasNumCopies || !hasNumExtraConstants {
|
|
||||||
panic("Invalid RandomAccessGate ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewRandomAccessGate(uint64(bits), uint64(numCopies), uint64(numExtraConstants))
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(gateId, "ArithmeticExtension") {
|
|
||||||
// Has the format "ArithmeticExtensionGate { num_ops: 10 }"
|
|
||||||
|
|
||||||
regEx := "ArithmeticExtensionGate { num_ops: (?P<numOps>[0-9]+) }"
|
|
||||||
r, err := regexp.Compile(regEx)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid ArithmeticExtensionGate regular expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := getRegExMatches(r, gateId)
|
|
||||||
numOps, hasNumOps := matches["numOps"]
|
|
||||||
if !hasNumOps {
|
|
||||||
panic("Invalid ArithmeticExtensionGate ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewArithmeticExtensionGate(uint64(numOps))
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(gateId, "MulExtensionGate") {
|
|
||||||
// Has the format "MulExtensionGate { num_ops: 13 }"
|
|
||||||
|
|
||||||
regEx := "MulExtensionGate { num_ops: (?P<numOps>[0-9]+) }"
|
|
||||||
r, err := regexp.Compile(regEx)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid MulExtensionGate regular expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := getRegExMatches(r, gateId)
|
|
||||||
numOps, hasNumOps := matches["numOps"]
|
|
||||||
if !hasNumOps {
|
|
||||||
panic("Invalid MulExtensionGate ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewMultiplicationExtensionGate(uint64(numOps))
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(gateId, "ReducingExtensionGate") {
|
|
||||||
// Has the format "ReducingExtensionGate { num_coeffs: 33 }"
|
|
||||||
|
|
||||||
regEx := "ReducingExtensionGate { num_coeffs: (?P<numCoeffs>[0-9]+) }"
|
|
||||||
r, err := regexp.Compile(regEx)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid ReducingExtensionGate regular expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := getRegExMatches(r, gateId)
|
|
||||||
numCoeffs, hasNumCoeffs := matches["numCoeffs"]
|
|
||||||
if !hasNumCoeffs {
|
|
||||||
panic("Invalid ReducingExtensionGate ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewReducingExtensionGate(uint64(numCoeffs))
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(gateId, "ReducingGate") {
|
|
||||||
// Has the format "ReducingGate { num_coeffs: 33 }"
|
|
||||||
|
|
||||||
regEx := "ReducingGate { num_coeffs: (?P<numCoeffs>[0-9]+) }"
|
|
||||||
r, err := regexp.Compile(regEx)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid ReducingGate regular expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := getRegExMatches(r, gateId)
|
|
||||||
numCoeffs, hasNumCoeffs := matches["numCoeffs"]
|
|
||||||
if !hasNumCoeffs {
|
|
||||||
panic("Invalid ReducingGate ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewReducingGate(uint64(numCoeffs))
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(gateId, "ExponentiationGate") {
|
|
||||||
// Has the format "ExponentiationGate { num_power_bits: 67, _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>"
|
|
||||||
|
|
||||||
regEx := "ExponentiationGate { num_power_bits: (?P<numPowerBits>[0-9]+), _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=(?P<base>[0-9]+)>"
|
|
||||||
r, err := regexp.Compile(regEx)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid ExponentiationGate regular expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := getRegExMatches(r, gateId)
|
|
||||||
numPowerBits, hasNumPowerBits := matches["numPowerBits"]
|
|
||||||
if !hasNumPowerBits {
|
|
||||||
panic("Invalid ExponentiationGate ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewExponentiationGate(uint64(numPowerBits))
|
|
||||||
}
|
|
||||||
|
|
||||||
// CosetInterpolationGate { subgroup_bits: 4, degree: 6, barycentric_weights: [17293822565076172801, 18374686475376656385, 18446744069413535745, 281474976645120, 17592186044416, 18446744069414584577, 18446744000695107601, 18446744065119617025, 1152921504338411520, 72057594037927936, 18446744069415632897, 18446462594437939201, 18446726477228539905, 18446744069414584065, 68719476720, 4294967296], _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>
|
|
||||||
if strings.HasPrefix(gateId, "CosetInterpolationGate") {
|
|
||||||
// Has the format CosetInterpolationGate { subgroup_bits: 4, degree: 6, barycentric_weights: [17293822565076172801, 18374686475376656385, 18446744069413535745, 281474976645120, 17592186044416, 18446744069414584577, 18446744000695107601, 18446744065119617025, 1152921504338411520, 72057594037927936, 18446744069415632897, 18446462594437939201, 18446726477228539905, 18446744069414584065, 68719476720, 4294967296], _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>
|
|
||||||
|
|
||||||
/*
|
|
||||||
regEx := "CosetInterpolationGate { subgroup_bits: (?P<subgroupBits>[0-9]+), degree: (?P<degree>[0-9]+), barycentric_weights: \\[(?P<barycentricWeights>[0-9, ]+)\\], _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>"
|
|
||||||
r, err := regexp.Compile(regEx)
|
|
||||||
if err != nil {
|
|
||||||
panic("Invalid CosetInterpolationGate regular expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := getRegExMatches(r, gateId)
|
|
||||||
subgroupBits, hasSubgroupBits := matches["subgroupBits"]
|
|
||||||
degree, hasDegree := matches["degree"]
|
|
||||||
barycentricWeights, hasBarycentricWeights := matches["barycentricWeights"]
|
|
||||||
if !hasSubgroupBits || !hasDegree || !hasBarycentricWeights {
|
|
||||||
panic("Invalid CosetInterpolationGate ID")
|
|
||||||
}*/
|
|
||||||
|
|
||||||
return NewCosetInterpolationGate(
|
|
||||||
4,
|
|
||||||
6,
|
|
||||||
[]goldilocks.Element{
|
|
||||||
goldilocks.NewElement(17293822565076172801),
|
|
||||||
goldilocks.NewElement(18374686475376656385),
|
|
||||||
goldilocks.NewElement(18446744069413535745),
|
|
||||||
goldilocks.NewElement(281474976645120),
|
|
||||||
goldilocks.NewElement(17592186044416),
|
|
||||||
goldilocks.NewElement(18446744069414584577),
|
|
||||||
goldilocks.NewElement(18446744000695107601),
|
|
||||||
goldilocks.NewElement(18446744065119617025),
|
|
||||||
goldilocks.NewElement(1152921504338411520),
|
|
||||||
goldilocks.NewElement(72057594037927936),
|
|
||||||
goldilocks.NewElement(18446744069415632897),
|
|
||||||
goldilocks.NewElement(18446462594437939201),
|
|
||||||
goldilocks.NewElement(18446726477228539905),
|
|
||||||
goldilocks.NewElement(18446744069414584065),
|
|
||||||
goldilocks.NewElement(68719476720),
|
|
||||||
goldilocks.NewElement(4294967296),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
panic(fmt.Sprintf("Unknown gate ID %s", gateId))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRegExMatches(r *regexp.Regexp, gateId string) map[string]int {
|
func GateInstanceFromId(gateId string) gate {
|
||||||
matches := r.FindStringSubmatch(gateId)
|
for regex, handler := range gateRegexHandlers {
|
||||||
result := make(map[string]int)
|
matches := regex.FindStringSubmatch(gateId)
|
||||||
for i, name := range r.SubexpNames() {
|
if matches != nil {
|
||||||
if i != 0 && name != "" {
|
parameters := make(map[string]string)
|
||||||
value, err := strconv.Atoi(matches[i])
|
for i, name := range regex.SubexpNames() {
|
||||||
if err != nil {
|
if i != 0 && name != "" {
|
||||||
panic("Invalid field value for \"name\": " + err.Error())
|
parameters[name] = matches[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != nil {
|
||||||
|
return handler(parameters)
|
||||||
}
|
}
|
||||||
result[name] = value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
panic(fmt.Sprintf("Unknown gate ID %s", gateId))
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PlonkChip) computeFilter(
|
func (p *PlonkChip) computeFilter(
|
||||||
|
|||||||
@@ -3,8 +3,27 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var mulExtensionGateRegex = regexp.MustCompile("MulExtensionGate { num_ops: (?P<numOps>[0-9]+) }")
|
||||||
|
|
||||||
|
func deserializeMulExtensionGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "MulExtensionGate { num_ops: 13 }"
|
||||||
|
numOps, hasNumOps := parameters["numOps"]
|
||||||
|
if !hasNumOps {
|
||||||
|
panic("Missing field num_ops in MulExtensionGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numOpsInt, err := strconv.Atoi(numOps)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid num_ops field in MulExtensionGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewMultiplicationExtensionGate(uint64(numOpsInt))
|
||||||
|
}
|
||||||
|
|
||||||
type MultiplicationExtensionGate struct {
|
type MultiplicationExtensionGate struct {
|
||||||
numOps uint64
|
numOps uint64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,16 @@ package plonky2_verifier
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var noopGateRegex = regexp.MustCompile("NoopGate")
|
||||||
|
|
||||||
|
func deserializeNoopGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "NoopGate"
|
||||||
|
return NewNoopGate()
|
||||||
|
}
|
||||||
|
|
||||||
type NoopGate struct {
|
type NoopGate struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,16 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
"gnark-plonky2-verifier/poseidon"
|
"gnark-plonky2-verifier/poseidon"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var poseidonGateRegex = regexp.MustCompile("PoseidonGate.*")
|
||||||
|
|
||||||
|
func deserializePoseidonGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "PoseidonGate(PhantomData<plonky2_field::goldilocks_field::GoldilocksField>)<WIDTH=12>"
|
||||||
|
return NewPoseidonGate()
|
||||||
|
}
|
||||||
|
|
||||||
type PoseidonGate struct {
|
type PoseidonGate struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,16 @@ package plonky2_verifier
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var publicInputGateRegex = regexp.MustCompile("PublicInputGate")
|
||||||
|
|
||||||
|
func deserializePublicInputGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "PublicInputGate"
|
||||||
|
return NewPublicInputGate()
|
||||||
|
}
|
||||||
|
|
||||||
type PublicInputGate struct {
|
type PublicInputGate struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,40 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var randomAccessGateRegex = regexp.MustCompile("RandomAccessGate { bits: (?P<bits>[0-9]+), num_copies: (?P<numCopies>[0-9]+), num_extra_constants: (?P<numExtraConstants>[0-9]+), _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=(?P<base>[0-9]+)>")
|
||||||
|
|
||||||
|
func deserializeRandomAccessGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "RandomAccessGate { bits: 2, num_copies: 13, num_extra_constants: 2, _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>"
|
||||||
|
bits, hasBits := parameters["bits"]
|
||||||
|
numCopies, hasNumCopies := parameters["numCopies"]
|
||||||
|
numExtraConstants, hasNumExtraConstants := parameters["numExtraConstants"]
|
||||||
|
|
||||||
|
if !hasBits || !hasNumCopies || !hasNumExtraConstants {
|
||||||
|
panic("missing bits, numCopies, numExtraConstants or base in RandomAccessGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
bitsInt, err := strconv.ParseUint(bits, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic("invalid bits in RandomAccessGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numCopiesInt, err := strconv.ParseUint(numCopies, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic("invalid numCopies in RandomAccessGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numExtraConstantsInt, err := strconv.ParseUint(numExtraConstants, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic("invalid numExtraConstants in RandomAccessGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewRandomAccessGate(bitsInt, numCopiesInt, numExtraConstantsInt)
|
||||||
|
}
|
||||||
|
|
||||||
type RandomAccessGate struct {
|
type RandomAccessGate struct {
|
||||||
bits uint64
|
bits uint64
|
||||||
numCopies uint64
|
numCopies uint64
|
||||||
|
|||||||
@@ -3,8 +3,27 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var reducingExtensionGateRegex = regexp.MustCompile("ReducingExtensionGate { num_coeffs: (?P<numCoeffs>[0-9]+) }")
|
||||||
|
|
||||||
|
func deserializeReducingExtensionGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "ReducingGate { num_coeffs: 33 }"
|
||||||
|
numCoeffs, hasNumCoeffs := parameters["numCoeffs"]
|
||||||
|
if !hasNumCoeffs {
|
||||||
|
panic("Missing field num_coeffs in ReducingExtensionGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numCoeffsInt, err := strconv.Atoi(numCoeffs)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid num_coeffs field in ReducingExtensionGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewReducingExtensionGate(uint64(numCoeffsInt))
|
||||||
|
}
|
||||||
|
|
||||||
type ReducingExtensionGate struct {
|
type ReducingExtensionGate struct {
|
||||||
numCoeffs uint64
|
numCoeffs uint64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,27 @@ package plonky2_verifier
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
. "gnark-plonky2-verifier/field"
|
. "gnark-plonky2-verifier/field"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var reducingGateRegex = regexp.MustCompile("ReducingGate { num_coeffs: (?P<numCoeffs>[0-9]+) }")
|
||||||
|
|
||||||
|
func deserializeReducingGate(parameters map[string]string) gate {
|
||||||
|
// Has the format "ReducingGate { num_coeffs: 33 }"
|
||||||
|
numCoeffs, hasNumCoeffs := parameters["numCoeffs"]
|
||||||
|
if !hasNumCoeffs {
|
||||||
|
panic("Missing field num_coeffs in ReducingGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
numCoeffsInt, err := strconv.Atoi(numCoeffs)
|
||||||
|
if err != nil {
|
||||||
|
panic("Invalid num_coeffs field in ReducingGate")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewReducingGate(uint64(numCoeffsInt))
|
||||||
|
}
|
||||||
|
|
||||||
type ReducingGate struct {
|
type ReducingGate struct {
|
||||||
numCoeffs uint64
|
numCoeffs uint64
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user