Skip to content

Commit

Permalink
clean up test json generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeytimoshin committed Mar 28, 2024
1 parent 5c25797 commit c01c4c6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 189 deletions.
136 changes: 98 additions & 38 deletions gnark-prover/merkle-tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,98 +8,158 @@ import (
)

func TestInclusionParameters_TestTree(t *testing.T) {
var tree = BuildTestTree(3, 1, true)
var json = tree.ToJSON()
fmt.Println(json)

file, err := os.OpenFile("../test-data/inclusion_tmp.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
defer func(file *os.File) {
err := file.Close()
if err != nil {
t.Errorf("Error closing file: %v", err)
}
}(file)

// generate trees with depth 1..26 and numberOfUtxos 1..10 and store the serialized results in a file
for i := 1; i <= 8; i++ {
for j := 1; j <= 4; j++ {
tree := BuildTestTree(i, j, true)
json := tree.ToJSON()

_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 1, json))

var json, err = tree.MarshalJSON()
if err != nil {
fmt.Println(err)
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 1, json)
if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}

invalidValue := big.NewInt(999)
tree.Root[0] = *invalidValue
json = tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 0, json))
json, err = tree.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 0, json)
if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}

tree.Leaf[0] = *invalidValue
json = tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 0, json))
json, err = tree.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 0, json)
if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}

tree.InPathIndices[0] = 999
json = tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 0, json))
json, err = tree.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 0, json)
if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}

tree.InPathElements[0][0] = *invalidValue
json = tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 0, json))
json, err = tree.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 0, json)
if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}

if err != nil {
fmt.Println(err)
return
}

}
}
}
func TestNonInclusionParameters_TestTree(t *testing.T) {
var tree = BuildTestNonInclusionTree(2, 1, true, true)
var json = tree.ToJSON()
fmt.Println(json)

func TestNonInclusionParameters_TestTree(t *testing.T) {
file, err := os.OpenFile("../test-data/non-inclusion_tmp.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
t.Errorf("Error opening file: %v", err)
return
}
defer file.Close()
defer func(file *os.File) {
err := file.Close()
if err != nil {
t.Errorf("Error closing file: %v", err)
}
}(file)

// generate trees with depth 1..26 and numberOfUtxos 1..10 and store the serialized results in a file
for i := 1; i <= 8; i++ {
for j := 1; j <= 4; j++ {
tree := BuildTestNonInclusionTree(i, j, true, true)
json := tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 1, json))
var json, err = tree.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 1, json)

if err != nil {
fmt.Println(err)
t.Errorf("Error writing to file: %v", err)
return
}

invalidValue := big.NewInt(9999)
tree.Root[0] = *invalidValue
json = tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 0, json))
json, err = tree.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 0, json)

if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}

tree.Value[0] = *invalidValue
json = tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 0, json))
json, err = tree.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 0, json)

tree.InPathIndices[0] = 9999
json = tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 0, json))
json, err = tree.MarshalJSON()
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 0, json)

tree.InPathElements[0][0] = *invalidValue
json = tree.ToJSON()
_, err = file.WriteString(fmt.Sprintf("%d;%s\n", 0, json))

json, err = tree.MarshalJSON()
if err != nil {
fmt.Println(err)
t.Errorf("Error marshalling JSON: %v", err)
return
}
_, err = fmt.Fprintf(file, "%d;%s\n", 0, json)
if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}

Expand Down
54 changes: 0 additions & 54 deletions gnark-prover/prover/inclusion_proving_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,57 +136,3 @@ func (ps *ProvingSystem) VerifyInclusion(root []big.Int, leaf []big.Int, proof *
}
return groth16.Verify(proof.Proof, ps.VerifyingKey, witness)
}

func (p *InclusionParameters) ToJSON() string {
// convert params to string of hex values like: {"root":["0x0"],"inPathIndices":[0],"inPathElements":[["0x0"]],"leaf":["0x0"]}
// create json string variable
var jsonStr string
jsonStr += fmt.Sprintf("{\"root\": [")
// convert root to string of hex values
for i, v := range p.Root {
jsonStr += fmt.Sprintf("\"0x%s\"", v.Text(16))
if i < len(p.Root)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"inPathIndices\": [")
// convert inPathIndices to string of uint32 values
for i, v := range p.InPathIndices {
jsonStr += fmt.Sprintf("%d", v)
if i < len(p.InPathIndices)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"inPathElements\": [")
// convert inPathElements to string of array of hex values
for i, v := range p.InPathElements {
jsonStr += "["
for j, w := range v {
jsonStr += fmt.Sprintf("\"0x%s\"", w.Text(16))
if j < len(v)-1 {
jsonStr += ","
}
}
jsonStr += "]"
if i < len(p.InPathElements)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"leaf\": [")
// convert leaf to string of hex values
for i, v := range p.Leaf {
jsonStr += fmt.Sprintf("\"0x%s\"", v.Text(16))
if i < len(p.Leaf)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("]}")

return jsonStr
}
97 changes: 0 additions & 97 deletions gnark-prover/prover/non_inclusion_proving_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,100 +139,3 @@ func (ps *ProvingSystem) ProveNonInclusion(params *NonInclusionParameters) (*Pro

return &Proof{proof}, nil
}

func (ps *ProvingSystem) VerifyNonInclusion(root []big.Int, value []big.Int, proof *Proof) error {
rootArray := make([]frontend.Variable, ps.NumberOfUtxos)
for i, v := range root {
rootArray[i] = v
}

valueArray := make([]frontend.Variable, ps.NumberOfUtxos)
for i, v := range value {
valueArray[i] = v
}

publicAssignment := NonInclusionCircuit{
Root: rootArray,
Value: valueArray,
}
witness, err := frontend.NewWitness(&publicAssignment, ecc.BN254.ScalarField(), frontend.PublicOnly())
if err != nil {
return err
}
return groth16.Verify(proof.Proof, ps.VerifyingKey, witness)
}

func (p *NonInclusionParameters) ToJSON() string {
var jsonStr string
jsonStr += fmt.Sprintf("{\"root\": [")
for i, v := range p.Root {
jsonStr += fmt.Sprintf("\"0x%s\"", v.Text(16))
if i < len(p.Root)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"inPathIndices\": [")
for i, v := range p.InPathIndices {
jsonStr += fmt.Sprintf("%d", v)
if i < len(p.InPathIndices)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"inPathElements\": [")
for i, v := range p.InPathElements {
jsonStr += "["
for j, w := range v {
jsonStr += fmt.Sprintf("\"0x%s\"", w.Text(16))
if j < len(v)-1 {
jsonStr += ","
}
}
jsonStr += "]"
if i < len(p.InPathElements)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"value\": [")
for i, v := range p.Value {
jsonStr += fmt.Sprintf("\"0x%s\"", v.Text(16))
if i < len(p.Value)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"leafLowerRangeValue\": [")
for i, v := range p.LeafLowerRangeValue {
jsonStr += fmt.Sprintf("\"0x%s\"", v.Text(16))
if i < len(p.LeafLowerRangeValue)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"leafHigherRangeValue\": [")
for i, v := range p.LeafHigherRangeValue {
jsonStr += fmt.Sprintf("\"0x%s\"", v.Text(16))
if i < len(p.LeafHigherRangeValue)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("],")

jsonStr += fmt.Sprintf("\"leafIndex\": [")
for i, v := range p.LeafIndex {
jsonStr += fmt.Sprintf("%d", v)
if i < len(p.InPathIndices)-1 {
jsonStr += ","
}
}
jsonStr += fmt.Sprintf("]}")

return jsonStr
}

0 comments on commit c01c4c6

Please sign in to comment.