Coder Social home page Coder Social logo

sbnair / zksnark-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from republicprotocol/zksnark-rs

0.0 0.0 0.0 486 KB

Zero knowledge succinct arguments of knowledge, written in Rust

License: GNU General Public License v3.0

Rust 99.13% Nix 0.87%

zksnark-rs's Introduction

Zero Knowledge SNARKs

This library is not ready for production.

This crate provides functionality for creating and using zero knowledge proofs. The implementation is based on groth16.

Usage

The main functions of the alrotihm are the setup, prove and verify functions in the groth16 module. Intermediate representations can be generated from .zk files, which are written in a DSL that represents an arithmetic circuit.

Language

The language for representing arithmetic circuits is quite basic and is written in a lisp-esque style that uses parenthesised prefix notation. The following is an example program for a circuit that computes a quadratic polynomial y = ax^2 + bx + c:

(in x a b c)
(out y)
(verify x y)

(program
    (= t1
        (* x a))
    (= t2
        (* x (+ t1 b)))
    (= y
        (* 1 (+ t2 c))))

The order must always follow in, out, verify and then program. Currently parentheses are 'sticky' in that there must not be any whitespace between them and their interior tokens. The keywords are as follows:

  • in precedes the list of input wires to the circuit, excluding the constant unity wire.
  • out precedes the list of output wires from the circuit.
  • verify precedes the list of wires that the verifier will check by providing them as input in the verification process.
  • program precedes the list of multiplication subcircuits that constitute the entire arithmetic circuit. The multiplication subcircuits model a single multiplication gate that has fan in two, where the two inputs can be a linear combination of any number of circuit inputs and previous internal wires. They use the following keywords.
  • = is the assignment operator, which takes two arguments. The first is the variable that is being assigned to, and represents the output wire of the multiplication gate. The second is the expression being assigned, and represents the linear combination of input wires.
  • * is the multiplication operator, which is used both for the multiplication gate and also to represent the constant scaling in the linear combination inputs to the multiplication gate. It takes only two arguments; when used for a multiplication gate the order does not matter, but for constant scaling the constant must be the first argument.
  • + is the addition operator, and as stated before can have an arbitrary number of arguments. Each argument can either be a variable, or a scaled variable (i.e. it can either look like x, or, for example, like (* 5 x)).

Examples

As an example, consider the simple arithmetic expression x = 4ab + c + 6. We want to verify the wires x and b. The program file can look like the following:

(in a b c)
(out x)
(verify b x)

(program
    (= temp
        (* a b))
    (= x
        (* 1 (+ (* 4 temp) c 6))))

Suppose that the prove wants to prove that they know values a and c for which the circuit is satisfied when the verifier inputs b = 2 and x = 34. The following code is an example of the setup, prove and verify process.

extern crate zksnark;

use zksnark::groth16;
use zksnark::groth16::{Proof, SigmaG1, SigmaG2, QAP};
use zksnark::groth16::circuit::{ASTParser, TryParse};
use zksnark::groth16::fr::FrLocal;
use zksnark::groth16::coefficient_poly::CoefficientPoly;

// x = 4ab + c + 6
let code = &*::std::fs::read_to_string("test_programs/simple.zk").unwrap();
let qap: QAP<CoefficientPoly<FrLocal>> =
    ASTParser::try_parse(code)
        .unwrap()
        .into();

// The assignments are the inputs to the circuit in the order they
// appear in the file
let assignments = &[
    3.into(), // a
    2.into(), // b
    4.into(), // c
];
let weights = groth16::weights(code, assignments).unwrap();

let (sigmag1, sigmag2) = groth16::setup(&qap);

let proof = groth16::prove(&qap, (&sigmag1, &sigmag2), &weights);

assert!(groth16::verify(
    &qap,
    (sigmag1, sigmag2),
    &vec![FrLocal::from(2), FrLocal::from(34)],
    proof
));

zksnark-rs's People

Contributors

ross-pure avatar skyfold avatar loongy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.