From 27b6a6359b2493c4c9e17b137e8a5f3dd4ed4f60 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Fri, 12 Jan 2024 17:20:21 -0300 Subject: [PATCH] Update example --- .../go_exporter_example/example.go | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/examples/prove-gnark-plonk/go_exporter_example/example.go b/examples/prove-gnark-plonk/go_exporter_example/example.go index 1f0dbcadf..c22387ab3 100644 --- a/examples/prove-gnark-plonk/go_exporter_example/example.go +++ b/examples/prove-gnark-plonk/go_exporter_example/example.go @@ -20,6 +20,7 @@ import ( "log" "os" "reflect" + "strconv" "unsafe" "encoding/json" @@ -52,6 +53,38 @@ type SerializedCircuit struct { Permutation []int64 } +func witness_to_json_array(fullWitness witness.Witness) string { + + witnessVec := fullWitness.Vector() + witnessVecString := fmt.Sprint(witnessVec) + inputJSON := fmt.Sprint("{\"witness\": ", witnessVecString, "}\n") + + fmt.Println("Witness json") + fmt.Println(inputJSON) + + // Unmarshal JSON into a map + var data map[string]interface{} + json.Unmarshal([]byte(inputJSON), &data) + + // Convert array values to strings + witnessArray, _ := data["witness"].([]interface{}) + + var witnessStrings []string + for _, value := range witnessArray { + // Convert the value to a string + strValue := strconv.Itoa(int(value.(float64))) + witnessStrings = append(witnessStrings, strValue) + } + + // Update the map with the converted array + data["witness"] = witnessStrings + + // Marshal the updated map back to JSON + outputJSON, _ := json.Marshal(data) + + return string(outputJSON) +} + func ToJSON(_r1cs *cs.SparseR1CS, pk *plonk_bls12381.ProvingKey, fullWitness witness.Witness, witnessPublic fr_bls12381.Vector) { // n nbConstraints := _r1cs.GetNbConstraints() nbPublic := len(_r1cs.Public) @@ -176,14 +209,12 @@ func ToJSON(_r1cs *cs.SparseR1CS, pk *plonk_bls12381.ProvingKey, fullWitness wit witnessVec := fullWitness.Vector() println() - witnessVecString := fmt.Sprint(witnessVec) fmt.Println(witnessVecString) - jsonWitness := fmt.Sprint("{ \"witness\" : ", witnessVecString, " }\n") f, _ := os.Create("witness.json") - fmt.Fprintf(f, jsonWitness) + fmt.Fprintf(f, witness_to_json_array(fullWitness)) // witnessFormatted := fmt.Sprintln(witnessVec)