Skip to content

Commit

Permalink
Merge pull request #131 from Schaeff/string-free-flat-absy
Browse files Browse the repository at this point in the history
Remove Strings from the flat absy
  • Loading branch information
JacobEberhardt committed Oct 5, 2018
2 parents 8521b62 + 45988a8 commit 5f68001
Show file tree
Hide file tree
Showing 18 changed files with 967 additions and 589 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion zokrates_cli/src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ fn main() {

let witness_map = main_flattened.get_witness(arguments).unwrap();

println!("Witness: {:?}", witness_map);
println!("\nWitness: \n\n{}", witness_map
.iter()
.filter_map(|(variable, value)| match variable {
variable if variable.is_output() => Some(format!("{} {}", variable, value)),
_ => None
}).collect::<Vec<String>>().join("\n"));

// write witness to file
let output_path = Path::new(sub_matches.value_of("output").unwrap());
Expand Down
3 changes: 1 addition & 2 deletions zokrates_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ num = {version = "0.1.36", default-features = false}
num-bigint = {version = "0.1.36", default-features = false}
lazy_static = "0.1.*"
reduce = "0.1.1"
# cli
clap = "2.26.2"
# serialization and deserialization
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
bincode = "0.8.0"
regex = "0.2"
bimap = "0.1"

[dev-dependencies]
glob = "0.2.11"
Expand Down
24 changes: 21 additions & 3 deletions zokrates_core/src/flat_absy/flat_parameter.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
use flat_absy::flat_variable::FlatVariable;
use std::fmt;
use substitution::Substitution;

#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct FlatParameter {
pub id: String,
pub id: FlatVariable,
pub private: bool,
}

impl FlatParameter {
fn new(id: FlatVariable, private: bool) -> Self {
FlatParameter {
id,
private
}
}

pub fn public(v: FlatVariable) -> Self {
Self::new(v, false)
}

pub fn private(v: FlatVariable) -> Self {
Self::new(v, true)
}
}

impl fmt::Display for FlatParameter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let visibility = if self.private { "private " } else { "" };
Expand All @@ -21,9 +39,9 @@ impl fmt::Debug for FlatParameter {
}

impl FlatParameter {
pub fn apply_substitution(self, substitution: &Substitution) -> FlatParameter {
pub fn apply_direct_substitution(self, substitution: &Substitution) -> FlatParameter {
FlatParameter {
id: substitution.get(&self.id).unwrap().to_string(),
id: substitution.get(&self.id).unwrap(),
private: self.private
}
}
Expand Down
88 changes: 88 additions & 0 deletions zokrates_core/src/flat_absy/flat_variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::fmt;
use substitution::Substitution;


// A variable in a constraint system
// id > 0 for intermediate variables
// id == 0 for ~one
// id < 0 for public outputs
#[derive(Serialize, Deserialize, Clone, PartialEq, Hash, Eq, Ord, PartialOrd, Copy)]
pub struct FlatVariable {
id: isize,
}

impl FlatVariable {
pub fn new(id: usize) -> Self {
FlatVariable {
id: 1 + id as isize,
}
}

pub fn one() -> Self {
FlatVariable {
id: 0,
}
}

pub fn public(id: usize) -> Self {
FlatVariable {
id: - (id as isize) - 1,
}
}

pub fn id(&self) -> usize {
assert!(self.id > 0);
(self.id as usize) - 1
}
}

impl fmt::Display for FlatVariable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.id {
0 => write!(f, "~one"),
i if i > 0 => write!(f, "_{}", i + 1),
i => write!(f, "~out_{}", - (i + 1))
}
}
}

impl fmt::Debug for FlatVariable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "FlatVariable(id: {})", self.id)
}
}

impl FlatVariable {
pub fn apply_substitution(self, substitution: &Substitution, should_fallback: bool) -> Self {
match should_fallback {
true => substitution.get(&self).unwrap_or(self),
false => substitution.get(&self).unwrap()
}
}

pub fn is_output(&self) -> bool {
self.id < 0
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn one() {
assert_eq!(FlatVariable::one().id, 0);
}

#[test]
fn public() {
assert_eq!(FlatVariable::public(0).id, - 1);
assert_eq!(FlatVariable::public(42).id, - 43);
}

#[test]
fn private() {
assert_eq!(FlatVariable::new(0).id, 1);
assert_eq!(FlatVariable::new(42).id, 43);
}
}
Loading

0 comments on commit 5f68001

Please sign in to comment.