• Stars
    star
    232
  • Rank 172,847 (Top 4 %)
  • Language
    Go
  • License
    GNU General Publi...
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

zkSNARK library implementation in Go from scratch (compiler, setup, prover, verifier)

Warning

Implementation of the zkSNARK Pinocchio protocol and Groth16 protocol from scratch in Go done in my free time to understand the concepts. Do not use in production.

If you want to generate proofs & verify them from Go, you can try https://github.com/vocdoni/go-snark, which is implemented using the bn256 for the Pairing curve operations for the Groth16 zkSNARK, and it is compatible with circom.

go-snark-study Go Report Card GoDoc

zkSNARK library implementation in Go

Features

Currently allows to do the complete path with Pinocchio protocol and Groth16 protocol :

  1. write circuit
  2. compile circuit
  3. generate trusted setup
  4. calculate witness
  5. generate proofs
  6. verify proofs

Minimal complete flow implementation:

  • Finite Fields (1, 2, 6, 12) operations
  • G1 and G2 curve operations
  • BN128 Pairing
  • circuit flat code compiler
  • circuit to R1CS
  • polynomial operations
  • R1CS to QAP
  • generate trusted setup
  • generate proofs
  • verify proofs with BN128 pairing

WASM usage

Experimentation with go-snark-study compiled to wasm: https://github.com/arnaucube/go-snark-study/tree/master/wasm

Usage

CLI usage

The cli still needs some improvements, such as seting input files, etc.

In this example we will follow the equation example from Vitalik's article: y = x^3 + x + 5, where y==35 and x==3. So we want to prove that we know a secret x such as the result of the equation is 35.

Compile circuit

Having a circuit file test.circuit:

func exp3(private a):
	b = a * a
	c = a * b
	return c

func main(private s0, public s1):
	s3 = exp3(s0)
	s4 = s3 + s0
	s5 = s4 + 5
	equals(s1, s5)
	out = 1 * 1

And a private inputs file privateInputs.json

[
	3
]

And a public inputs file publicInputs.json

[
	35
]

In the command line, execute:

> ./go-snark-cli compile test.circuit

If you want to have the wasm input ready also, add the flag wasm

> ./go-snark-cli compile test.circuit wasm

This will output the compiledcircuit.json file.

Trusted Setup

Having the compiledcircuit.json, now we can generate the TrustedSetup:

> ./go-snark-cli trustedsetup

This will create the file trustedsetup.json with the TrustedSetup data, and also a toxic.json file, with the parameters to delete from the Trusted Setup.

If you want to have the wasm input ready also, add the flag wasm

> ./go-snark-cli trustedsetup wasm

Generate Proofs

Assumming that we have the compiledcircuit.json, trustedsetup.json, privateInputs.json and the publicInputs.json we can now generate the Proofs with the following command:

> ./go-snark-cli genproofs

This will store the file proofs.json, that contains all the SNARK proofs.

Verify Proofs

Having the proofs.json, compiledcircuit.json, trustedsetup.json publicInputs.json files, we can now verify the Pairings of the proofs, in order to verify the proofs.

> ./go-snark-cli verify

This will return a true if the proofs are verified, or a false if the proofs are not verified.

Cli using Groth16

All this process can be done using Groth16 protocol protocol:

> ./go-snark-cli compile test.circuit
> ./go-snark-cli groth16 trustedsetup
> ./go-snark-cli groth16 genproofs
> ./go-snark-cli verify

Library usage

Example:

// compile circuit and get the R1CS
flatCode := `
func exp3(private a):
	b = a * a
	c = a * b
	return c

func main(private s0, public s1):
	s3 = exp3(s0)
	s4 = s3 + s0
	s5 = s4 + 5
	equals(s1, s5)
	out = 1 * 1
`

// parse the code
parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
circuit, err := parser.Parse()
assert.Nil(t, err)
fmt.Println(circuit)


b3 := big.NewInt(int64(3))
privateInputs := []*big.Int{b3}
b35 := big.NewInt(int64(35))
publicSignals := []*big.Int{b35}

// witness
w, err := circuit.CalculateWitness(privateInputs, publicSignals)
assert.Nil(t, err)
fmt.Println("witness", w)

// now we have the witness:
// w = [1 35 3 9 27 30 35 1]

// flat code to R1CS
fmt.Println("generating R1CS from flat code")
a, b, c := circuit.GenerateR1CS()

/*
now we have the R1CS from the circuit:
a: [[0 0 1 0 0 0 0 0] [0 0 1 0 0 0 0 0] [0 0 1 0 1 0 0 0] [5 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0] [0 1 0 0 0 0 0 0] [1 0 0 0 0 0 0 0]]
b: [[0 0 1 0 0 0 0 0] [0 0 0 1 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0]]
c: [[0 0 0 1 0 0 0 0] [0 0 0 0 1 0 0 0] [0 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0] [0 1 0 0 0 0 0 0] [0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 1]]
*/


alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)


ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)

// calculate trusted setup
setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)

hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)

proof, err := GenerateProofs(*circuit, setup, w, px)

b35Verif := big.NewInt(int64(35))
publicSignalsVerif := []*big.Int{b35Verif}
assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
Verify Proof generated from snarkjs

Is possible with go-snark-study to verify proofs generated by snarkjs

Example:

verified, err := VerifyFromCircom("circom-test/verification_key.json", "circom-test/proof.json", "circom-test/public.json")
assert.Nil(t, err)
assert.True(t, verified)

Versions

History of versions & tags of this project:

  • v0.0.1: zkSnark complete flow working with Pinocchio protocol
  • v0.0.2: circuit language improved (allow function calls and file imports)
  • v0.0.3: Groth16 zkSnark protocol added

Test

go test ./... -v

vim/nvim circuit syntax highlighter

For more details and installation instructions see https://github.com/arnaucube/go-snark-study/tree/master/vim-syntax


Thanks to @jbaylina, @bellesmarta, @adriamb for their explanations that helped to understand this a little bit. Also thanks to @vbuterin for all the published articles explaining the zkSNARKs.

More Repositories

1

coffeeMiner

collaborative (mitm) cryptocurrency mining pool in wifi networks
Python
1,057
star
2

darkID-prototype

Blockchain based anonymous distributed ID system using RSA Blind Signatures
Go
107
star
3

cryptofun

Crypto algorithms from scratch in Go. Learning purposes only. ECC, BN128 pairing, Paillier, RSA, Homomorphic computation, ElGamal, Schnorr, ECDSA, BLS, ...
Go
68
star
4

flock-botnet

Twitter botnet with autonomous bots replying tweets with text generated based on probabilities in Markov chains
Go
55
star
5

awesome-circom

A curated list of repos related to Circom
53
star
6

poseidon-rs

Poseidon hash function
Rust
40
star
7

goBlockchainDataAnalysis

blockchain data analysis
Go
36
star
8

provoj

Simple python library to check the endpoints of an RESTful API
Python
35
star
9

protogalaxy-poc

ProtoGalaxy folding scheme PoC implementation https://eprint.iacr.org/2023/1106.pdf
Rust
31
star
10

evm-rs

Ethereum Virtual Machine implementation from scratch in Rust
Rust
24
star
11

babyjubjub-rs

BabyJubJub elliptic curve implementation in Rust
Rust
24
star
12

kzg-commitments-study

Kate-Zaverucha-Goldberg Polynomial Commitments
Go
23
star
13

miksi-core

ethereum zk coin-mixer
Solidity
19
star
14

goDDOS

academical ddos server client written in go
Go
17
star
15

shamirsecretsharing

ShamirSecretSharing Rust & Go implementation + WASM lib
Rust
16
star
16

merkletree-rs

Sparse MerkleTree implementation in Rust
Rust
16
star
17

go-blindsecp256k1

Blind signatures over secp256k1 elliptic curve
Go
12
star
18

go-bellman-verifier

Groth16 zkSNARK bellman proof verifier using cloudflare/bn256 Pairing
Go
11
star
19

goNmapTelegramBot

simple telegram bot, that gets an ip, performs a nmap port scan, and returns the result to the telegram user
Go
9
star
20

eth-kzg-ceremony-alt

Alternative implementation of the contributor-client for the Ethereum KZG Trusted Setup Ceremony
Go
9
star
21

go-dht

Kademlia DHT implementation
Go
8
star
22

mimc-rs

MiMC hash function
Rust
7
star
23

blogo

Static blog generator, template engine from markdown and html templates
Go
7
star
24

argos

Open source twitter entropic toolkit written in Go lang
Go
7
star
25

gogame

Nostalgic OGame clone in Go
Go
7
star
26

commonroutesApp

car sharing app - client side [Angularjs + Ionic]
JavaScript
6
star
27

class2context

javascript library, to add context menu functionallity to html page
JavaScript
6
star
28

decentralized-blogging-platform

Decentralized blogging platform, over IPFS
HTML
6
star
29

go-merkletree-old

Optimized MerkleTree implementation in Go.
Go
6
star
30

echo-botnet

A twitter botnet with autonomous bots replying tweets with pre-configured replies
Go
5
star
31

fri-commitment

FRI commitment scheme
Rust
5
star
32

link2epub

Very simple tool to download articles and convert it to .epub/.mobi files.
Go
5
star
33

blindsecp256k1-js

Typescript implementation of blind signatures over secp256k1 compatible with https://github.com/arnaucube/go-blindsecp256k1
TypeScript
4
star
34

fft-rs

Fast Fourier Transform implementation in Rust
Rust
4
star
35

slowlorisdb

Slow, decentralized and cryptographically consistent database
Go
3
star
36

galdric

machine learning server, for image classification applying KNN
Go
3
star
37

huffman-coding

Huffman encoding and decoding
Go
3
star
38

wifiAutoWPS

auto wps pin for wifi code adaption
Shell
3
star
39

commonroutesServer

car sharing app - backend
JavaScript
3
star
40

miksi-app

JavaScript
2
star
41

configs

config files
Shell
2
star
42

gogame-frontend

Frontend for https://github.com/arnaucube/gogame
JavaScript
2
star
43

faircoinmap-webapp

Webapp map showing places accepting FairCoin
JavaScript
2
star
44

go-chip8

CHIP-8 emulator written in Go
Go
2
star
45

bc

Own p2p network and own blockchain libraries written in Go, to develop own decentralized apps.
Go
2
star
46

goCaptcha

captcha server, with own datasets
Go
2
star
47

md-live-server

Server that renders markdown files and live updates the page each time that the file is updated
Go
2
star
48

commonroutesBot

Telegram bot to notify when new travels are published.
JavaScript
2
star
49

pad2ipfs

Simply Go lang library to get the content from a pad (etherpad) and put into IPFS.
Go
2
star
50

math

Notes, code and documents done while reading books and papers.
TeX
2
star
51

goImgServer

Server of images, written in Go lang
Go
2
star
52

padArchiver

Tool to store pads (from url) into local directory and IPFS.
Go
2
star
53

blockchainIDsystem

Blockchain based anonymous distributed ID system
Go
2
star
54

esp32-wallet-viewer

esp32 code to monitor wallets
C
2
star
55

slides

Go
1
star
56

blog

JavaScript
1
star
57

mumble-telegram-bot

Telegram bot that sends messages when users connect, move from rooms, and disconnect from the Mumble (Murmur) server.
JavaScript
1
star
58

ring-signatures-rs

bLSAG implementation in rust
Rust
1
star
59

commonroutesLandingPage

CSS
1
star
60

twitterReader

twitter reader assistant
Go
1
star
61

raspberryGPIOhtmlserver

[nodejs + express + pi-gpio] nodejs server to control raspberrypi GPIO, from an html server
JavaScript
1
star
62

tor-eth-online-shop

HTML
1
star
63

blindsig-client-server-example

Example of usage of https://github.com/arnaucube/go-blindsecp256k1
JavaScript
1
star
64

paraulogic-word-finder

Python
1
star
65

blogoExample

This is an example of the use of blogo static blog engine (https://github.com/arnaucube/blogo).
HTML
1
star
66

kesto

box with pieces of code
Go
1
star
67

ipa-rs

modified Inner Product Argument version from Halo paper
Rust
1
star
68

goRecommender

Content recommendation API based on Machine Learning
Go
1
star
69

nokto

xfce terminal theme
1
star
70

chip8-rs

CHIP-8 emulator written in Rust
Rust
1
star
71

faircoin-calculator

very simple Faircoin calculator
HTML
1
star
72

goKNN

Go library of the K Nearest Neighbors algorithm, calculating the Euclidean distances
Go
1
star
73

kindle-weather

CSS
1
star
74

botnetCrafter

Python
1
star
75

cellMapVisualizer

Data analyisis server & Web map visualization for a dataset of more than 3.5GB
Go
1
star