From a415c95f6fd23cfd5a83004484333beae7161a35 Mon Sep 17 00:00:00 2001 From: Kevin Jue Date: Thu, 18 May 2023 15:30:32 -0700 Subject: [PATCH] Refactor gate deserialization (#16) * helper functions for coset_interpolation_gate * coset interpolation gate working * hard coded the coset gate (for now) * refactored gate serialization --- plonky2_verifier/arithmetic_extension_gate.go | 19 ++ plonky2_verifier/arithmetic_gate.go | 20 ++ plonky2_verifier/base_sum_gate.go | 34 ++- plonky2_verifier/constant_gate.go | 19 ++ plonky2_verifier/coset_interpolation_gate.go | 39 +++ plonky2_verifier/exponentiation_gate.go | 19 ++ plonky2_verifier/gate.go | 250 ++---------------- .../multiplication_extension_gate.go | 19 ++ plonky2_verifier/noop_gate.go | 8 + plonky2_verifier/poseidon_gate.go | 8 + plonky2_verifier/public_input_gate.go | 8 + plonky2_verifier/random_access_gate.go | 32 +++ plonky2_verifier/reducing_extension_gate.go | 19 ++ plonky2_verifier/reducing_gate.go | 19 ++ 14 files changed, 286 insertions(+), 227 deletions(-) diff --git a/plonky2_verifier/arithmetic_extension_gate.go b/plonky2_verifier/arithmetic_extension_gate.go index 727f53a..ee2c59f 100644 --- a/plonky2_verifier/arithmetic_extension_gate.go +++ b/plonky2_verifier/arithmetic_extension_gate.go @@ -3,8 +3,27 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var aritheticExtensionGateRegex = regexp.MustCompile("ArithmeticExtensionGate { num_ops: (?P[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 { numOps uint64 } diff --git a/plonky2_verifier/arithmetic_gate.go b/plonky2_verifier/arithmetic_gate.go index 1734556..55ab259 100644 --- a/plonky2_verifier/arithmetic_gate.go +++ b/plonky2_verifier/arithmetic_gate.go @@ -3,8 +3,28 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var aritheticGateRegex = regexp.MustCompile("ArithmeticGate { num_ops: (?P[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 { numOps uint64 } diff --git a/plonky2_verifier/base_sum_gate.go b/plonky2_verifier/base_sum_gate.go index a4310ec..4f4f9a2 100644 --- a/plonky2_verifier/base_sum_gate.go +++ b/plonky2_verifier/base_sum_gate.go @@ -3,11 +3,37 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var baseSumGateRegex = regexp.MustCompile("BaseSumGate { num_limbs: (?P[0-9]+) } \\+ Base: (?P[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 ( - WIRE_SUM = 0 - START_LIMBS = 1 + BASESUM_GATE_WIRE_SUM = 0 + BASESUM_GATE_START_LIMBS = 1 ) type BaseSumGate struct { @@ -29,14 +55,14 @@ func (g *BaseSumGate) Id() string { func (g *BaseSumGate) limbs() []uint64 { limbIndices := make([]uint64, g.numLimbs) for i := uint64(0); i < g.numLimbs; i++ { - limbIndices[i] = uint64(START_LIMBS + i) + limbIndices[i] = uint64(BASESUM_GATE_START_LIMBS + i) } return limbIndices } 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) limbIndices := g.limbs() for i, limbIdx := range limbIndices { diff --git a/plonky2_verifier/constant_gate.go b/plonky2_verifier/constant_gate.go index 2d12609..9d4895c 100644 --- a/plonky2_verifier/constant_gate.go +++ b/plonky2_verifier/constant_gate.go @@ -3,8 +3,27 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var constantGateRegex = regexp.MustCompile("ConstantGate { num_consts: (?P[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 { numConsts uint64 } diff --git a/plonky2_verifier/coset_interpolation_gate.go b/plonky2_verifier/coset_interpolation_gate.go index 5e84d63..2659139 100644 --- a/plonky2_verifier/coset_interpolation_gate.go +++ b/plonky2_verifier/coset_interpolation_gate.go @@ -3,10 +3,49 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" + "strings" "github.com/consensys/gnark-crypto/field/goldilocks" ) +var cosetInterpolationGateRegex = regexp.MustCompile("CosetInterpolationGate { subgroup_bits: (?P[0-9]+), degree: (?P[0-9]+), barycentric_weights: \\[(?P[0-9, ]+)\\], _phantom: PhantomData }") + +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 } + 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 { subgroupBits uint64 degree uint64 diff --git a/plonky2_verifier/exponentiation_gate.go b/plonky2_verifier/exponentiation_gate.go index f141bb7..8bef534 100644 --- a/plonky2_verifier/exponentiation_gate.go +++ b/plonky2_verifier/exponentiation_gate.go @@ -3,8 +3,27 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var exponentiationGateRegex = regexp.MustCompile("ExponentiationGate { num_power_bits: (?P[0-9]+), _phantom: PhantomData }[0-9]+)>") + +func deserializeExponentiationGate(parameters map[string]string) gate { + // Has the format "ExponentiationGate { num_power_bits: 67, _phantom: PhantomData }" + 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 { numPowerBits uint64 } diff --git a/plonky2_verifier/gate.go b/plonky2_verifier/gate.go index 8dcb8dd..b3d9953 100644 --- a/plonky2_verifier/gate.go +++ b/plonky2_verifier/gate.go @@ -4,10 +4,6 @@ import ( "fmt" . "gnark-plonky2-verifier/field" "regexp" - "strconv" - "strings" - - "github.com/consensys/gnark-crypto/field/goldilocks" ) type gate interface { @@ -15,231 +11,39 @@ type gate interface { EvalUnfiltered(p *PlonkChip, vars EvaluationVars) []QuadraticExtension } -func GateInstanceFromId(gateId string) gate { - if strings.HasPrefix(gateId, "ArithmeticGate") { - numOpsRaw := strings.Split(gateId, ":")[1] - numOpsRaw = strings.Split(numOpsRaw, "}")[0] - numOpsRaw = strings.TrimSpace(numOpsRaw) - numOps, err := strconv.Atoi(numOpsRaw) - if err != nil { - panic("Invalid gate ID for ArithmeticGate") - } - return NewArithmeticGate(uint64(numOps)) - } - - if strings.HasPrefix(gateId, "ConstantGate") { - numConstsRaw := strings.Split(gateId, ":")[1] - 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[0-9]+) } \\+ Base: (?P[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 }" - - regEx := "RandomAccessGate { bits: (?P[0-9]+), num_copies: (?P[0-9]+), num_extra_constants: (?P[0-9]+), _phantom: PhantomData }[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[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[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[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[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 }" - - regEx := "ExponentiationGate { num_power_bits: (?P[0-9]+), _phantom: PhantomData }[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 } - 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 } +var gateRegexHandlers = map[*regexp.Regexp]func(parameters map[string]string) gate{ + aritheticGateRegex: deserializeArithmeticGate, + aritheticExtensionGateRegex: deserializeExtensionArithmeticGate, + baseSumGateRegex: deserializeBaseSumGate, + constantGateRegex: deserializeConstantGate, + cosetInterpolationGateRegex: deserializeCosetInterpolationGate, + exponentiationGateRegex: deserializeExponentiationGate, + mulExtensionGateRegex: deserializeMulExtensionGate, + noopGateRegex: deserializeNoopGate, + poseidonGateRegex: deserializePoseidonGate, + publicInputGateRegex: deserializePublicInputGate, + randomAccessGateRegex: deserializeRandomAccessGate, + reducingExtensionGateRegex: deserializeReducingExtensionGate, + reducingGateRegex: deserializeReducingGate, +} - /* - regEx := "CosetInterpolationGate { subgroup_bits: (?P[0-9]+), degree: (?P[0-9]+), barycentric_weights: \\[(?P[0-9, ]+)\\], _phantom: PhantomData }" - r, err := regexp.Compile(regEx) - if err != nil { - panic("Invalid CosetInterpolationGate regular expression") +func GateInstanceFromId(gateId string) gate { + for regex, handler := range gateRegexHandlers { + matches := regex.FindStringSubmatch(gateId) + if matches != nil { + parameters := make(map[string]string) + for i, name := range regex.SubexpNames() { + if i != 0 && name != "" { + parameters[name] = matches[i] + } } - 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 { - matches := r.FindStringSubmatch(gateId) - result := make(map[string]int) - for i, name := range r.SubexpNames() { - if i != 0 && name != "" { - value, err := strconv.Atoi(matches[i]) - if err != nil { - panic("Invalid field value for \"name\": " + err.Error()) + if matches != nil { + return handler(parameters) } - result[name] = value } } - - return result + panic(fmt.Sprintf("Unknown gate ID %s", gateId)) } func (p *PlonkChip) computeFilter( diff --git a/plonky2_verifier/multiplication_extension_gate.go b/plonky2_verifier/multiplication_extension_gate.go index d33914d..de7852b 100644 --- a/plonky2_verifier/multiplication_extension_gate.go +++ b/plonky2_verifier/multiplication_extension_gate.go @@ -3,8 +3,27 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var mulExtensionGateRegex = regexp.MustCompile("MulExtensionGate { num_ops: (?P[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 { numOps uint64 } diff --git a/plonky2_verifier/noop_gate.go b/plonky2_verifier/noop_gate.go index fb48ae5..c007612 100644 --- a/plonky2_verifier/noop_gate.go +++ b/plonky2_verifier/noop_gate.go @@ -2,8 +2,16 @@ package plonky2_verifier import ( . "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 { } diff --git a/plonky2_verifier/poseidon_gate.go b/plonky2_verifier/poseidon_gate.go index a5e4e5a..80ee309 100644 --- a/plonky2_verifier/poseidon_gate.go +++ b/plonky2_verifier/poseidon_gate.go @@ -3,8 +3,16 @@ package plonky2_verifier import ( . "gnark-plonky2-verifier/field" "gnark-plonky2-verifier/poseidon" + "regexp" ) +var poseidonGateRegex = regexp.MustCompile("PoseidonGate.*") + +func deserializePoseidonGate(parameters map[string]string) gate { + // Has the format "PoseidonGate(PhantomData)" + return NewPoseidonGate() +} + type PoseidonGate struct { } diff --git a/plonky2_verifier/public_input_gate.go b/plonky2_verifier/public_input_gate.go index 30fa95c..410acb1 100644 --- a/plonky2_verifier/public_input_gate.go +++ b/plonky2_verifier/public_input_gate.go @@ -2,8 +2,16 @@ package plonky2_verifier import ( . "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 { } diff --git a/plonky2_verifier/random_access_gate.go b/plonky2_verifier/random_access_gate.go index 3ea67e7..385e97b 100644 --- a/plonky2_verifier/random_access_gate.go +++ b/plonky2_verifier/random_access_gate.go @@ -3,8 +3,40 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var randomAccessGateRegex = regexp.MustCompile("RandomAccessGate { bits: (?P[0-9]+), num_copies: (?P[0-9]+), num_extra_constants: (?P[0-9]+), _phantom: PhantomData }[0-9]+)>") + +func deserializeRandomAccessGate(parameters map[string]string) gate { + // Has the format "RandomAccessGate { bits: 2, num_copies: 13, num_extra_constants: 2, _phantom: PhantomData }" + 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 { bits uint64 numCopies uint64 diff --git a/plonky2_verifier/reducing_extension_gate.go b/plonky2_verifier/reducing_extension_gate.go index 7681d81..a665a44 100644 --- a/plonky2_verifier/reducing_extension_gate.go +++ b/plonky2_verifier/reducing_extension_gate.go @@ -3,8 +3,27 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var reducingExtensionGateRegex = regexp.MustCompile("ReducingExtensionGate { num_coeffs: (?P[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 { numCoeffs uint64 } diff --git a/plonky2_verifier/reducing_gate.go b/plonky2_verifier/reducing_gate.go index 24240ae..ebeb71b 100644 --- a/plonky2_verifier/reducing_gate.go +++ b/plonky2_verifier/reducing_gate.go @@ -3,8 +3,27 @@ package plonky2_verifier import ( "fmt" . "gnark-plonky2-verifier/field" + "regexp" + "strconv" ) +var reducingGateRegex = regexp.MustCompile("ReducingGate { num_coeffs: (?P[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 { numCoeffs uint64 }