Skip to content

Commit

Permalink
zk: Move export_witness_json() to zk module.
Browse files Browse the repository at this point in the history
zkas should be kept dependency-free.
  • Loading branch information
parazyd committed Oct 27, 2023
1 parent 299e70f commit 54ad302
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 54 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ zk = [

zkas = [
"darkfi-serial",

"zk",
]
# -----END LIBRARY FEATURES-----

Expand Down
52 changes: 52 additions & 0 deletions src/zk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,55 @@ where
|mut region| region.assign_advice(|| "load private", column, 0, || value),
)
}

#[cfg(feature = "tinyjson")]
use darkfi_sdk::pasta::pallas;
#[cfg(feature = "tinyjson")]
use std::{collections::HashMap, fs::File, io::Write, path::Path};
#[cfg(feature = "tinyjson")]
use tinyjson::JsonValue::{Array as JsonArray, Object as JsonObj, String as JsonStr};

#[cfg(feature = "tinyjson")]
/// Export witness.json which can be used by zkrunner for debugging circuits
pub fn export_witness_json<P: AsRef<Path>>(
output_path: P,
prover_witnesses: &Vec<Witness>,
public_inputs: &Vec<pallas::Base>,
) {
let mut witnesses = Vec::new();
for witness in prover_witnesses {
let mut value_json = HashMap::new();
match witness {
Witness::Base(value) => {
value.map(|w1| {
value_json.insert("Base".to_string(), JsonStr(format!("{:?}", w1)));
w1
});
}
Witness::Scalar(value) => {
value.map(|w1| {
value_json.insert("Scalar".to_string(), JsonStr(format!("{:?}", w1)));
w1
});
}
_ => unimplemented!(),
}
witnesses.push(JsonObj(value_json));
}

let mut instances = Vec::new();
for instance in public_inputs {
instances.push(JsonStr(format!("{:?}", instance)));
}

let witnesses_json = JsonArray(witnesses);
let instances_json = JsonArray(instances);
let witness_json = JsonObj(HashMap::from([
("witnesses".to_string(), witnesses_json),
("instances".to_string(), instances_json),
]));
// This is a debugging method. We don't care about .expect() crashing.
let json = witness_json.format().expect("cannot create debug json");
let mut output = File::create(output_path).expect("cannot write file");
output.write_all(json.as_bytes()).expect("write failed");
}
3 changes: 0 additions & 3 deletions src/zkas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,3 @@ pub use compiler::Compiler;
/// Decoder module
pub mod decoder;
pub use decoder::ZkBinary;

#[cfg(feature = "tinyjson")]
pub mod util;
49 changes: 0 additions & 49 deletions src/zkas/util.rs

This file was deleted.

0 comments on commit 54ad302

Please sign in to comment.