Skip to content

Commit

Permalink
zkWF2: Inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bazsalanszky committed Nov 3, 2024
0 parents commit 3b88aed
Show file tree
Hide file tree
Showing 98 changed files with 20,257 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.r1cs
contract.sol
*.wtns
*.ptau
*.log
*.bin
bin/*
build/*
117 changes: 117 additions & 0 deletions cmds/handshake-test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/rand"
"encoding/hex"
"fmt"
"log"
"math/big"
"sync"

"github.com/consensys/gnark-crypto/ecc/bn254/twistededwards/eddsa"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ftsrg/zkWF/pkg/crypto/keys"
"github.com/ftsrg/zkWF/pkg/web3"
)

const (
participants = 3
)

func main() {
for i := 0; i < participants; i++ {
err := keys.GenerateKeyPair(fmt.Sprintf("key%d.json", i))
if err != nil {
panic(err)
}
}

fmt.Println("Key pairs generated successfully!")

// Load the key pair
privateKeys := make([]*eddsa.PrivateKey, participants)
for i := 0; i < participants; i++ {
keyPair, err := keys.LoadKeyPair(fmt.Sprintf("key%d.json", i))
if err != nil {
panic(err)
}

privateKey, err := eddsa.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}

privateKey.SetBytes(keyPair.Bytes())
privateKeys[i] = privateKey
}
pubKeys := make([]*eddsa.PublicKey, participants)
for i := 0; i < 3; i++ {
pubKeys[i] = &privateKeys[i].PublicKey
}

pubKeyStrs := make([]string, participants)
for i := 0; i < participants; i++ {
pubKeyStrs[i] = hex.EncodeToString(privateKeys[i].PublicKey.Bytes())
}

contractAddress, err := web3.DeployContract(pubKeyStrs)
if err != nil {
panic(err)
}

fmt.Println("Contract deployed successfully at address:", contractAddress)

client, err := ethclient.Dial("http://localhost:8545")
if err != nil {
panic(err)
}

ethPrivKeys := []string{"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", "59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d", "47c99abed3324a2707c28affff1267e45918ec8c3f20b8aa892e8b065d2942dd"}

var wg sync.WaitGroup
wg.Add(participants)
for i, privKey := range ethPrivKeys {
go func(i int, privKey string) {
defer wg.Done()
privateKey, err := crypto.HexToECDSA(privKey)
if err != nil {
log.Fatal(err)
}

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}

fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}

gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}

auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(3000000) // in units
auth.GasPrice = gasPrice

secret, err := web3.PerformHandshake(client, contractAddress, auth, privateKeys[i], pubKeys)
if err != nil {
panic(err)
}
fmt.Printf("Secret %d: %s\n", i, hex.EncodeToString(secret))
}(i, privKey)
}

wg.Wait()
}
30 changes: 30 additions & 0 deletions cmds/zkwf/compile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"log"

"github.com/ftsrg/zkWF/pkg/zkp"
"github.com/spf13/cobra"
)

var compileCommand = &cobra.Command{
Use: "compile <model>",
Short: "Compile a BPMN file into a zero-knowledge circuit",
Args: cobra.ExactArgs(1),
Run: compileCMDExecute,
}

func compileCMDExecute(cmd *cobra.Command, args []string) {
modelPath := args[0]
outputFlag, _ := cmd.Flags().GetString("output")

zkwf, err := zkp.NewZkWFProgram(modelPath)
if err != nil {
log.Fatalln("Failed to create zkWF program:", err)
}

err = zkwf.Compile(outputFlag)
if err != nil {
log.Fatalln("Failed to compile zkWF program:", err)
}
}
35 changes: 35 additions & 0 deletions cmds/zkwf/deploy_ecdh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"log"

"github.com/ftsrg/zkWF/pkg/web3"
"github.com/spf13/cobra"
)

var deployEcdhCommand = &cobra.Command{
Use: "deploy-ecdh",
Short: "Deploy the ECDH contract with predefined public keys",
RunE: deployEcdhCommandFunc,
}

func init() {
deployEcdhCommand.Flags().StringSliceP("public-keys", "p", []string{}, "Public keys to be deployed")
rootCMD.AddCommand(deployEcdhCommand)
}

func deployEcdhCommandFunc(cmd *cobra.Command, args []string) error {
publicKeys, _ := cmd.Flags().GetStringSlice("public-keys")

if len(publicKeys) == 0 {
log.Fatalln("No public keys provided")
}

address, err := web3.DeployContract(publicKeys)
if err != nil {
return err
}

cmd.Printf("Contract deployed! Address: %s\n", address)
return nil
}
35 changes: 35 additions & 0 deletions cmds/zkwf/fill_inputs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"log"

"github.com/ftsrg/zkWF/pkg/zkp"
"github.com/spf13/cobra"
)

var fillInputsCommand = &cobra.Command{
Use: "fill-inputs <input-file> <keys-file>",
Short: "Fill inputs for the zkWF circuit",
Long: `Fill inputs for the zkWF circuit. It reads the input and keys from the given files and fills the input file with the appropriate values as follows:
- Generates a randomness for both states
- Fills in the Hash fields
- Signs the hash with the given keys
- Fills in the encryption fields
`,
Args: cobra.ExactArgs(2),
Run: fillInputsCmdFunc,
}

func init() {
rootCMD.AddCommand(fillInputsCommand)
}

func fillInputsCmdFunc(cmd *cobra.Command, args []string) {
inputFile := args[0]
keysFile := args[1]

err := zkp.FillInputs(inputFile, keysFile)
if err != nil {
log.Fatalln("Failed to fill inputs:", err)
}
}
32 changes: 32 additions & 0 deletions cmds/zkwf/generate_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"log"

"github.com/ftsrg/zkWF/pkg/crypto/keys"
"github.com/spf13/cobra"
)

var generateKeyCommand = &cobra.Command{
Use: "generate-key",
Short: "Generate a new eddsa key pair",
RunE: generateKeyCommandFunc,
}

func init() {
generateKeyCommand.PersistentFlags().StringP("output", "o", "keys.json", "Output file path")
rootCMD.AddCommand(generateKeyCommand)
}

func generateKeyCommandFunc(cmd *cobra.Command, args []string) error {
outputFile, _ := cmd.Flags().GetString("output")

err := keys.GenerateKeyPair(outputFile)
if err != nil {
return fmt.Errorf("failed to generate key: %w", err)
}

log.Println("Key pair generated and saved to", outputFile)
return nil
}
Loading

0 comments on commit 3b88aed

Please sign in to comment.