Skip to content

Commit 1f691a4

Browse files
committed
WIP
1 parent 1dc4d67 commit 1f691a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+15407
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ members = [
1515
"r1cs-core",
1616
"r1cs-core-new",
1717
"r1cs-std",
18+
"r1cs-std-new",
1819
"algebra-core/algebra-core-derive",
1920
]
2021

r1cs-std-new/Cargo.toml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[package]
2+
name = "r1cs-std-new"
3+
version = "0.1.0"
4+
authors = [
5+
"Sean Bowe",
6+
"Alessandro Chiesa",
7+
"Matthew Green",
8+
"Ian Miers",
9+
"Pratyush Mishra",
10+
"Howard Wu"
11+
]
12+
description = "A standard library for constraint system gadgets"
13+
homepage = "https://libzexe.org"
14+
repository = "https://github.com/scipr/zexe"
15+
documentation = "https://docs.rs/r1cs-std/"
16+
keywords = ["zero knowledge", "cryptography", "zkSNARK", "SNARK"]
17+
categories = ["cryptography"]
18+
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
19+
license = "MIT/Apache-2.0"
20+
edition = "2018"
21+
22+
################################# Dependencies ################################
23+
24+
[dependencies]
25+
algebra = { path = "../algebra", default-features = false }
26+
r1cs-core = { package = "r1cs-core-new", path = "../r1cs-core-new", default-features = false }
27+
derivative = { version = "2", features = ["use_core"] }
28+
29+
[dev-dependencies]
30+
rand = { version = "0.7", default-features = false }
31+
rand_xorshift = { version = "0.2" }
32+
# Currently this means that all downstream users of `r1cs-std` will be using
33+
# `algebra` with the `bls12_381` feature.
34+
algebra = { path = "../algebra", default-features = false, features = [ "bls12_381" ] }
35+
36+
[features]
37+
default = ["std"]
38+
full = [ "bls12_377", "jubjub", "edwards_bls12", "edwards_sw6", "mnt4_298", "mnt4_753", "mnt6_298", "mnt6_753" ]
39+
40+
bls12_377 = [ "algebra/bls12_377" ]
41+
jubjub = [ "algebra/jubjub" ]
42+
edwards_bls12 = [ "algebra/edwards_bls12" ]
43+
edwards_sw6 = [ "algebra/edwards_sw6" ]
44+
mnt4_298 = [ "algebra/mnt4_298" ]
45+
mnt4_753 = [ "algebra/mnt4_753" ]
46+
mnt6_298 = [ "algebra/mnt6_298" ]
47+
mnt6_753 = [ "algebra/mnt6_753" ]
48+
49+
std = [ "algebra/std", "r1cs-core/std" ]
50+
parallel = [ "std", "algebra/parallel" ]

r1cs-std-new/LICENSE-APACHE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE-APACHE

r1cs-std-new/LICENSE-MIT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE-MIT

r1cs-std-new/src/alloc.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use crate::Vec;
2+
use algebra::Field;
3+
use core::borrow::Borrow;
4+
use r1cs_core::{ConstraintSystem, SynthesisError};
5+
6+
pub trait AllocGadget<V, ConstraintF: Field>
7+
where
8+
Self: Sized,
9+
V: ?Sized,
10+
{
11+
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, f: F) -> Result<Self, SynthesisError>
12+
where
13+
F: FnOnce() -> Result<T, SynthesisError>,
14+
T: Borrow<V>;
15+
16+
fn alloc_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
17+
cs: CS,
18+
f: F,
19+
) -> Result<Self, SynthesisError>
20+
where
21+
F: FnOnce() -> Result<T, SynthesisError>,
22+
T: Borrow<V>,
23+
{
24+
Self::alloc(cs, f)
25+
}
26+
27+
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
28+
cs: CS,
29+
f: F,
30+
) -> Result<Self, SynthesisError>
31+
where
32+
F: FnOnce() -> Result<T, SynthesisError>,
33+
T: Borrow<V>;
34+
35+
fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
36+
cs: CS,
37+
f: F,
38+
) -> Result<Self, SynthesisError>
39+
where
40+
F: FnOnce() -> Result<T, SynthesisError>,
41+
T: Borrow<V>,
42+
{
43+
Self::alloc_input(cs, f)
44+
}
45+
}
46+
47+
impl<I, ConstraintF: Field, A: AllocGadget<I, ConstraintF>> AllocGadget<[I], ConstraintF>
48+
for Vec<A>
49+
{
50+
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
51+
mut cs: CS,
52+
f: F,
53+
) -> Result<Self, SynthesisError>
54+
where
55+
F: FnOnce() -> Result<T, SynthesisError>,
56+
T: Borrow<[I]>,
57+
{
58+
let mut vec = Vec::new();
59+
for (i, value) in f()?.borrow().iter().enumerate() {
60+
vec.push(A::alloc(&mut cs.ns(|| format!("value_{}", i)), || {
61+
Ok(value)
62+
})?);
63+
}
64+
Ok(vec)
65+
}
66+
67+
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
68+
mut cs: CS,
69+
f: F,
70+
) -> Result<Self, SynthesisError>
71+
where
72+
F: FnOnce() -> Result<T, SynthesisError>,
73+
T: Borrow<[I]>,
74+
{
75+
let mut vec = Vec::new();
76+
for (i, value) in f()?.borrow().iter().enumerate() {
77+
vec.push(A::alloc_input(
78+
&mut cs.ns(|| format!("value_{}", i)),
79+
|| Ok(value),
80+
)?);
81+
}
82+
Ok(vec)
83+
}
84+
85+
fn alloc_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
86+
mut cs: CS,
87+
f: F,
88+
) -> Result<Self, SynthesisError>
89+
where
90+
F: FnOnce() -> Result<T, SynthesisError>,
91+
T: Borrow<[I]>,
92+
{
93+
let mut vec = Vec::new();
94+
for (i, value) in f()?.borrow().iter().enumerate() {
95+
vec.push(A::alloc_checked(
96+
&mut cs.ns(|| format!("value_{}", i)),
97+
|| Ok(value),
98+
)?);
99+
}
100+
Ok(vec)
101+
}
102+
103+
fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
104+
mut cs: CS,
105+
f: F,
106+
) -> Result<Self, SynthesisError>
107+
where
108+
F: FnOnce() -> Result<T, SynthesisError>,
109+
T: Borrow<[I]>,
110+
{
111+
let mut vec = Vec::new();
112+
for (i, value) in f()?.borrow().iter().enumerate() {
113+
vec.push(A::alloc_input_checked(
114+
&mut cs.ns(|| format!("value_{}", i)),
115+
|| Ok(value),
116+
)?);
117+
}
118+
Ok(vec)
119+
}
120+
}

0 commit comments

Comments
 (0)