Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve challenge #1

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
//

const (
aliceBalance = 500
aliceBalance = 100000
bobBalance = 0
)

Expand All @@ -35,7 +35,7 @@ func init() {
vk = groth16.NewVerifyingKey(ecc.BN254)
vk.ReadFrom(bytes.NewReader(vkBytes))

// deserialize pk from disk
//deserialize pk from disk
pkBytes, err := os.ReadFile("./pk.bin")
if err != nil {
panic(err)
Expand All @@ -62,6 +62,7 @@ type Circuit struct {
}

func (circuit *Circuit) Define(api frontend.API) error {
fmt.Println("Define")
// init
gkrBalance := NewBalanceGKR(api, 1)

Expand Down Expand Up @@ -139,7 +140,7 @@ func VerifyProof(newBobBalanceStr string, proofHex string) error {

err = groth16.Verify(proof, vk, publicWitness)
if err != nil {
return err
return fmt.Errorf("Verify error: %v\n", err)
}

return nil
Expand Down
69 changes: 66 additions & 3 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package zksec_gkr
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"

"github.com/consensys/gnark-crypto/ecc"
Expand All @@ -12,6 +15,7 @@ import (
"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
//"github.com/stretchr/testify/assert"
)

func TestProveAndVerify(t *testing.T) {
Expand All @@ -21,18 +25,29 @@ func TestProveAndVerify(t *testing.T) {
// fixed
BobBalance: bobBalance,
// given by the user
NewBobBalance: 500,
NewBobBalance: 100000,
// given by the user
NewAliceBalance: 0,
// private
Transfer: 500,
Transfer: 100000,
}

// // compile the circuit
// ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &Circuit{})
// if err != nil {
// t.Fatalf("failed to compile circuit: %v", err)
// }

witness, err := frontend.NewWitness(&circuit, ecc.BN254.ScalarField())
if err != nil {
t.Fatal(err)
}

// pk, _, err := groth16.Setup(ccs)
// if err != nil {
// t.Fatalf("failed to setup circuit: %v", err)
// }

oR1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
if err != nil {
fmt.Println("error occured ", err)
Expand All @@ -53,8 +68,56 @@ func TestProveAndVerify(t *testing.T) {
t.Fatal(err)
}
proofHex := hex.EncodeToString(buf.Bytes())
err = VerifyProof("500", proofHex)
fmt.Printf("proofHex: %v\n", proofHex)
err = VerifyProof("100000", proofHex)
if err != nil {
t.Fatal(err)
}

//uploadProof(proofHex, "100000")
//assert.Equal(t, 1,2)
}

func uploadProof(proof, new_bob_balance string) {
url := "http://147.182.233.80:8080/"

// Create a map with the data to be sent in JSON format
data := map[string]string{
"new_bob_balance": new_bob_balance,
"proof_hex": proof,
}

// Encode the map into JSON
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}


req, err := http.NewRequest("GET", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating request:", err)
return
}

req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}

fmt.Println("Response Status:", resp.Status)
fmt.Println("Response Body:", string(body))
}