├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "bellman" 3 | version = "0.1.0" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | dependencies = [ 6 | "bit-vec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)", 8 | "crossbeam 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 9 | "futures 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)", 10 | "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", 11 | "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 12 | "pairing 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", 13 | "rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 14 | ] 15 | 16 | [[package]] 17 | name = "bellman-demo" 18 | version = "0.1.0" 19 | dependencies = [ 20 | "bellman 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 21 | "pairing 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", 22 | "rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 23 | ] 24 | 25 | [[package]] 26 | name = "bit-vec" 27 | version = "0.4.4" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | 30 | [[package]] 31 | name = "bitflags" 32 | version = "1.0.4" 33 | source = "registry+https://github.com/rust-lang/crates.io-index" 34 | 35 | [[package]] 36 | name = "byteorder" 37 | version = "1.2.6" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | 40 | [[package]] 41 | name = "crossbeam" 42 | version = "0.3.2" 43 | source = "registry+https://github.com/rust-lang/crates.io-index" 44 | 45 | [[package]] 46 | name = "fuchsia-zircon" 47 | version = "0.3.3" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | dependencies = [ 50 | "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 51 | "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 52 | ] 53 | 54 | [[package]] 55 | name = "fuchsia-zircon-sys" 56 | version = "0.3.3" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | 59 | [[package]] 60 | name = "futures" 61 | version = "0.1.24" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | 64 | [[package]] 65 | name = "futures-cpupool" 66 | version = "0.1.8" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | dependencies = [ 69 | "futures 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)", 70 | "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 71 | ] 72 | 73 | [[package]] 74 | name = "libc" 75 | version = "0.2.43" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | 78 | [[package]] 79 | name = "num_cpus" 80 | version = "1.8.0" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | dependencies = [ 83 | "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", 84 | ] 85 | 86 | [[package]] 87 | name = "pairing" 88 | version = "0.14.2" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | dependencies = [ 91 | "byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)", 92 | "rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", 93 | ] 94 | 95 | [[package]] 96 | name = "rand" 97 | version = "0.4.3" 98 | source = "registry+https://github.com/rust-lang/crates.io-index" 99 | dependencies = [ 100 | "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 101 | "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", 102 | "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 103 | ] 104 | 105 | [[package]] 106 | name = "winapi" 107 | version = "0.3.6" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | dependencies = [ 110 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 111 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 112 | ] 113 | 114 | [[package]] 115 | name = "winapi-i686-pc-windows-gnu" 116 | version = "0.4.0" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | 119 | [[package]] 120 | name = "winapi-x86_64-pc-windows-gnu" 121 | version = "0.4.0" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | 124 | [metadata] 125 | "checksum bellman 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eae372472c7ea8f7c8fc6a62f7d5535db8302de7f1aafda2e13a97c4830d3bcf" 126 | "checksum bit-vec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "02b4ff8b16e6076c3e14220b39fbc1fabb6737522281a388998046859400895f" 127 | "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" 128 | "checksum byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "90492c5858dd7d2e78691cfb89f90d273a2800fc11d98f60786e5d87e2f83781" 129 | "checksum crossbeam 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24ce9782d4d5c53674646a6a4c1863a21a8fc0cb649b3c94dfc16e45071dea19" 130 | "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" 131 | "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" 132 | "checksum futures 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "0c84b40c7e2de99ffd70602db314a7a8c26b2b3d830e6f7f7a142a8860ab3ca4" 133 | "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" 134 | "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" 135 | "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" 136 | "checksum pairing 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ceda21136251c6d5a422d3d798d8ac22515a6e8d3521bb60c59a8349d36d0d57" 137 | "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" 138 | "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" 139 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 140 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 141 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bellman-demo" 3 | version = "0.1.0" 4 | authors = ["sean"] 5 | 6 | [dependencies] 7 | bellman = "0.1" 8 | rand = "0.4" 9 | 10 | [dependencies.pairing] 11 | version = "0.14" 12 | features = ["u128-support"] -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | // https://github.com/ebfull/bellman-demo 2 | 3 | #![allow(unused_imports)] 4 | #![allow(unused_variables)] 5 | extern crate bellman; 6 | extern crate pairing; 7 | extern crate rand; 8 | use bellman::{Circuit, ConstraintSystem, SynthesisError}; 9 | use pairing::{Engine, Field, PrimeField}; 10 | 11 | trait OptionExt { 12 | fn grab(&self) -> Result; 13 | } 14 | 15 | impl OptionExt for Option { 16 | fn grab(&self) -> Result { 17 | self.ok_or(SynthesisError::AssignmentMissing) 18 | } 19 | } 20 | 21 | struct DemoCircuit { 22 | a: Option, 23 | b: Option, 24 | c: Option, 25 | } 26 | 27 | // Implementation of our circuit. 28 | impl Circuit for DemoCircuit { 29 | fn synthesize>(self, cs: &mut CS) -> Result<(), SynthesisError> { 30 | // variables: a, b 31 | // public input: c 32 | // constraint system: 33 | // a * a = a 34 | // b * b = b 35 | // 2a * b = a + b - c 36 | 37 | let a = cs.alloc(|| "a", || self.a.grab())?; 38 | 39 | // a * a = a 40 | cs.enforce(|| "a is a boolean", |lc| lc + a, |lc| lc + a, |lc| lc + a); 41 | 42 | let b = cs.alloc(|| "b", || self.b.grab())?; 43 | 44 | // b * b = b 45 | cs.enforce(|| "b is a boolean", |lc| lc + b, |lc| lc + b, |lc| lc + b); 46 | 47 | // c = a xor b 48 | let c = cs.alloc_input(|| "c", || self.c.grab())?; 49 | 50 | // 2a * b = a + b - c 51 | cs.enforce( 52 | || "xor constraint", 53 | |lc| lc + (E::Fr::from_str("2").unwrap(), a), 54 | |lc| lc + b, 55 | |lc| lc + a + b - c, 56 | ); 57 | 58 | Ok(()) 59 | } 60 | } 61 | 62 | // Create some parameters, create a proof, and verify the proof. 63 | fn main() { 64 | use pairing::bls12_381::{Bls12, Fr}; 65 | use rand::thread_rng; 66 | use std::marker::PhantomData; 67 | 68 | use bellman::groth16::{ 69 | create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof, Proof, 70 | }; 71 | 72 | let rng = &mut thread_rng(); 73 | 74 | let params = { 75 | let c = DemoCircuit:: { 76 | a: None, 77 | b: None, 78 | c: None, 79 | }; 80 | 81 | generate_random_parameters(c, rng).unwrap() 82 | }; 83 | 84 | let pvk = prepare_verifying_key(¶ms.vk); 85 | 86 | let c = DemoCircuit { 87 | a: Some(Fr::one()), 88 | b: Some(Fr::zero()), 89 | c: Some(Fr::one()), 90 | }; 91 | 92 | // Create a groth16 proof with our parameters. 93 | let proof = create_random_proof(c, ¶ms, rng).unwrap(); 94 | 95 | assert!(verify_proof(&pvk, &proof, &[Fr::one()]).unwrap()); 96 | } 97 | --------------------------------------------------------------------------------