1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

add private field to params. WIP

This commit is contained in:
Thibaut Schaeffer 2017-11-26 00:37:53 +01:00
parent 4f86e8f321
commit a38b727234
12 changed files with 106 additions and 3 deletions

View file

@ -1,4 +1,4 @@
// a and b are factorization of c
def main(c):
def main(c, private a, private b):
c == a * b
return 1

13
examples/private.code Normal file
View file

@ -0,0 +1,13 @@
// only using sub, no need to flatten
def main(x, private y):
a = 5
b = 7
c = if a == b then 4 else 3 fi
c == 3
d = if a == 5 then 1 else 2 fi
d == 1
e = if a < b then 5 else 6 fi
e == 5
f = if b < a then 7 else 8 fi
f == 8
return x

BIN
out Normal file

Binary file not shown.

3
out.code Normal file
View file

@ -0,0 +1,3 @@
def main(c,a,b):
c == (a * b)
return 1

BIN
proving.key Normal file

Binary file not shown.

View file

@ -195,6 +195,7 @@ impl<T: Field> fmt::Debug for Statement<T> {
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct Parameter {
pub id: String,
pub private: bool,
}
impl fmt::Display for Parameter {

View file

@ -424,6 +424,7 @@ impl Flattener {
match param_expr.apply_substitution(&self.substitution) {
Expression::Identifier(ref x) => params_flattened.push(Parameter {
id: x.clone().to_string(),
private: false
}),
_ => {
let expr_subbed = param_expr.apply_substitution(&self.substitution);
@ -439,6 +440,7 @@ impl Flattener {
.push(Statement::Definition(intermediate_var.clone(), rhs));
params_flattened.push(Parameter {
id: intermediate_var.clone().to_string(),
private: false
});
}
}
@ -626,6 +628,7 @@ impl Flattener {
for arg in funct.arguments {
arguments_flattened.push(Parameter {
id: arg.id.to_string(),
private: arg.private
});
}
// flatten statements in functions and apply substitution

View file

@ -150,6 +150,7 @@ enum Token<T: Field> {
Mult,
Div,
Pow,
Private,
Ide(String),
Num(T),
Unknown(String),
@ -188,6 +189,7 @@ impl<T: Field> fmt::Display for Token<T> {
Token::Mult => write!(f, "*"),
Token::Div => write!(f, "/"),
Token::Pow => write!(f, "**"),
Token::Private => write!(f, "private"),
Token::Ide(ref x) => write!(f, "{}", x),
Token::Num(ref x) => write!(f, "{}", x),
Token::Unknown(ref x) => write!(f, "{}", x),
@ -502,6 +504,14 @@ fn next_token<T: Field>(input: &String, pos: &Position) -> (Token<T>, String, Po
},
)
}
Some(_) if input[offset..].starts_with("private ") => (
Token::Private,
input[offset + 8..].to_string(),
Position {
line: pos.line,
col: pos.col + offset + 8,
},
),
Some(x) => match x {
'0'...'9' => parse_num(
&input[offset..].to_string(),
@ -1132,8 +1142,55 @@ fn parse_function<T: Field>(
let mut p = p3;
loop {
match next_token(&s, &p) {
(Token::Private, s4, p4) => {
match next_token(&s4, &p4) {
(Token::Ide(x), s5, p5) => {
args.push(Parameter { id: x, private: true });
match next_token(&s5, &p5) {
(Token::Comma, s6, p6) => {
s = s6;
p = p6;
}
(Token::Close, s5, p5) => match next_token(&s5, &p5) {
(Token::Colon, s6, p6) => match next_token(&s6, &p6) {
(Token::InlineComment(_), _, _) => break,
(Token::Unknown(ref x6), ..) if x6 == "" => break,
(t6, _, p6) => {
return Err(Error {
expected: vec![Token::Unknown("".to_string())],
got: t6,
pos: p6,
})
}
},
(t6, _, p6) => {
return Err(Error {
expected: vec![Token::Colon],
got: t6,
pos: p6,
})
}
},
(t5, _, p5) => {
return Err(Error {
expected: vec![Token::Comma, Token::Close],
got: t5,
pos: p5,
})
}
}
}
(t5, _, p5) => {
return Err(Error {
expected: vec![Token::Comma, Token::Close],
got: t5,
pos: p5,
})
}
}
}
(Token::Ide(x), s4, p4) => {
args.push(Parameter { id: x });
args.push(Parameter { id: x, private: false });
match next_token(&s4, &p4) {
(Token::Comma, s5, p5) => {
s = s5;

View file

@ -290,7 +290,8 @@ pub fn r1cs_program<T: Field>(
.iter()
.find(|x: &&Function<T>| x.id == "main".to_string())
.unwrap();
variables.extend(main.arguments.iter().map(|x| format!("{}", x)));
variables.extend(main.arguments.iter().filter(|x| x.private == false).map(|x| format!("{}", x)));
// ~out is added after main's arguments as we want variables (columns)
// in the r1cs to be aligned like "public inputs | private inputs"
variables.push("~out".to_string());
@ -298,6 +299,9 @@ pub fn r1cs_program<T: Field>(
// position where private part of witness starts
let private_inputs_offset = variables.len();
variables.extend(main.arguments.iter().filter(|x| x.private == true).map(|x| format!("{}", x)));
for def in &main.statements {
let mut a_row: Vec<(usize, T)> = Vec::new();
let mut b_row: Vec<(usize, T)> = Vec::new();

4
variables.inf Normal file
View file

@ -0,0 +1,4 @@
Private inputs offset:
3
R1CS variable order:
~one c ~out a b

13
verification.key Normal file
View file

@ -0,0 +1,13 @@
vk.A = [0x31794eb3a1691cc21bc9c964ee68db7ae28106b82787eef92fcae4a9c0e483, 0x2852286171b3eb1fff063874ef396d5c87d28b156a46a6ced7679780a681925e], [0x23a5d861d726720339f2861a861ee210604638ee1ead2af2c6f625d56cfda2d1, 0x16e92a3994f5a1d5809dcf62cf954fa9ae908a31b3080da065330850e9dfc543]
vk.B = 0x2b943f88d3bf0f6e2a822c6403c2c65a1d15450140b3f16a88f57e341c803b9f, 0x1c2dfce4287fb58ffe48044b396f9a988af57f58b778bbe395d73b3ebd40ac52
vk.C = [0x17d0522fa7e63bbdbefac9977e8787dbca5493f4d5b8001478944ab0be9474d2, 0x234356e7b44810bda6a4bb5ff7a17f01cd2be0356d1477912a9b97557e428341], [0x14893eb4d934af54b57b983239d5dc3b4074d809a87399f40626d685579d41c, 0x257802281a9aa9825bc886e6d9a0251a82fa50758bcd2677019ef6612e581819]
vk.gamma = [0x2ab2532debf0b5223ec3212b4e7b0afd66a3e10b6bc24282ea9780b40b06427b, 0x68d5b8e053f64c547a4363171733e75ca6cb692eaed3fd062bb810b42731e43], [0x15b3a5a27e97d8850ccb7a8f7be16835fd069c8982852ba0a19ca6699fed1f65, 0x3f7228f46f64d11bcba6540de41036d25dcad2b05b3f7066557c0b675692cb7]
vk.gammaBeta1 = 0x27602b76a313ceecf3afd6417563cb03de4e74799dd16fef8c28171313679571, 0x10ac0cd595cfdaf433efc2d4464271c62f7dc0f4e3c6b137f11ead282f03f76c
vk.gammaBeta2 = [0x12ffa58b224c0656664d1dd439cbb6b339e4f2d28a8efd3592cb418b0bb71e7f, 0x1431c785fb238f1c42f96aed8118b3abb575dc75335435b33f3266eb796ff26c], [0x55e2b2ba927eee3bc8aecbab1a72e1702f692f66b3cfa3c41069dde61ba0649, 0x9fff9db01e86a749b3a7e47767e11fbf10c16dbc8f74379f67af61eeb3c1bba]
vk.Z = [0x11b12fe6a6dcd649d8c634e506a4aa176ca70fde632fdfda06b9b28073732517, 0x2646ea2adcd0c0726548ecaa03dae29070f95c286985c8e07c2e18863be619c1], [0x256579f8d1321a4ca8e6a807e103c2e8d08d9b88b930e9b17d4c3313a7454851, 0x225eca3599e7f392017bb5dde4df4c974422e2377828ed91a1a6b56e2f278184]
vk.IC.len() = 5
vk.IC[0] = 0x1bb9cf704f62808123097642c9fc34dfa360451e8f8dcb6ea438793d45fa082d, 0x11729b4322acb068e76e0db12bf08825c77271879b241908d70ee7597efa4460
vk.IC[1] = 0x2644885e567a696deb2169d46cde807af0344e5d29fd86bd2a2fd94ce07fe4a3, 0xde1853871c61add0694c4f0c86575fde5b53e504c9ae799f54726c096f5496e
vk.IC[2] = 0x1abeee45d3bb30bc7f335e4e118ce7c50252e7f5998efafe1d68b1582fd0bdeb, 0x267ff3cc655bdf4aa2eb413fd64d6782af4d763670ec3c984e36a1c10bad7064
vk.IC[3] = 0x28f8d04e1314fff7541495e1798bd3d9e16e59c6744e42747cd71032979f6fb9, 0x2f696b7f7a693e24461c733af0252bbb55ad38af1341d98bc1e5fe1470acd13b
vk.IC[4] = 0x100dfc176ee1c80cb185857765fe74db10cf2b967ea9cf09e75955a9d3d72475, 0x2d391ecaaefbb947add78ed0ad7f2d9efec162aef33779027016018ba8c799f6

5
witness Normal file
View file

@ -0,0 +1,5 @@
~out 1
a 2
~one 1
c 8
b 4