put helpers in folder, add libsnark flags
This commit is contained in:
parent
101bbb17f4
commit
f2e727cc03
5 changed files with 100 additions and 88 deletions
|
@ -14,6 +14,7 @@ use std::fmt;
|
|||
use std::collections::{BTreeMap};
|
||||
use field::Field;
|
||||
use substitution::Substitution;
|
||||
#[cfg(feature = "libsnark")]
|
||||
use standard;
|
||||
use helpers::{DirectiveStatement, Executable};
|
||||
|
||||
|
@ -61,6 +62,7 @@ impl<T: Field> fmt::Debug for FlatProg<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "libsnark")]
|
||||
impl<T: Field> From<standard::R1CS> for FlatProg<T> {
|
||||
fn from(r1cs: standard::R1CS) -> Self {
|
||||
FlatProg {
|
||||
|
|
43
zokrates_core/src/helpers/libsnark_gadget.rs
Normal file
43
zokrates_core/src/helpers/libsnark_gadget.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
use libsnark::get_sha256_witness;
|
||||
use serde_json;
|
||||
use standard;
|
||||
use std::fmt;
|
||||
use field::{Field};
|
||||
use helpers::{Signed, Executable};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub enum LibsnarkGadgetHelper {
|
||||
Sha256Compress,
|
||||
}
|
||||
|
||||
impl fmt::Display for LibsnarkGadgetHelper {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
LibsnarkGadgetHelper::Sha256Compress => write!(f, "Sha256Compress"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field> Executable<T> for LibsnarkGadgetHelper {
|
||||
fn execute(&self, inputs: &Vec<T>) -> Result<Vec<T>, String> {
|
||||
match self {
|
||||
LibsnarkGadgetHelper::Sha256Compress => {
|
||||
let witness_result: Result<standard::Witness, serde_json::Error> = serde_json::from_str(&get_sha256_witness(inputs));
|
||||
|
||||
if let Err(e) = witness_result {
|
||||
return Err(format!("{}", e));
|
||||
}
|
||||
|
||||
Ok(witness_result.unwrap().variables.iter().map(|&i| T::from(i)).collect())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Signed for LibsnarkGadgetHelper {
|
||||
fn get_signature(&self) -> (usize, usize) {
|
||||
match self {
|
||||
LibsnarkGadgetHelper::Sha256Compress => (512, 25561),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,13 @@
|
|||
#[cfg(feature = "libsnark")]
|
||||
mod libsnark_gadget;
|
||||
mod rust;
|
||||
|
||||
#[cfg(feature = "libsnark")]
|
||||
pub use self::libsnark_gadget::LibsnarkGadgetHelper;
|
||||
pub use self::rust::RustHelper;
|
||||
use std::fmt;
|
||||
use field::{Field};
|
||||
use serde_json;
|
||||
use standard;
|
||||
|
||||
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub struct DirectiveStatement {
|
||||
|
@ -18,6 +24,7 @@ impl fmt::Display for DirectiveStatement {
|
|||
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub enum Helper {
|
||||
#[cfg(feature = "libsnark")]
|
||||
LibsnarkGadget(LibsnarkGadgetHelper),
|
||||
Rust(RustHelper)
|
||||
}
|
||||
|
@ -25,40 +32,13 @@ pub enum Helper {
|
|||
impl fmt::Display for Helper {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
#[cfg(feature = "libsnark")]
|
||||
Helper::LibsnarkGadget(ref h) => write!(f, "LibsnarkGadget::{}", h),
|
||||
Helper::Rust(ref h) => write!(f, "Rust::{}", h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub enum LibsnarkGadgetHelper {
|
||||
Sha256Compress,
|
||||
}
|
||||
|
||||
impl fmt::Display for LibsnarkGadgetHelper {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
LibsnarkGadgetHelper::Sha256Compress => write!(f, "Sha256Compress"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub enum RustHelper {
|
||||
Identity,
|
||||
ConditionEq,
|
||||
}
|
||||
|
||||
impl fmt::Display for RustHelper {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
RustHelper::Identity => write!(f, "Identity"),
|
||||
RustHelper::ConditionEq => write!(f, "ConditionEq"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Executable<T: Field>
|
||||
: Signed {
|
||||
fn execute(&self, inputs: &Vec<T>) -> Result<Vec<T>, String>;
|
||||
|
@ -68,69 +48,13 @@ pub trait Signed {
|
|||
fn get_signature(&self) -> (usize, usize);
|
||||
}
|
||||
|
||||
impl<T: Field> Executable<T> for LibsnarkGadgetHelper {
|
||||
fn execute(&self, inputs: &Vec<T>) -> Result<Vec<T>, String> {
|
||||
match self {
|
||||
LibsnarkGadgetHelper::Sha256Compress => {
|
||||
#[cfg(not(feature = "libsnark"))]
|
||||
{
|
||||
Err(format!("Libsnark is not available"))
|
||||
}
|
||||
|
||||
#[cfg(feature = "libsnark")]
|
||||
{
|
||||
use libsnark::*;
|
||||
|
||||
let witness_result: Result<standard::Witness, serde_json::Error> = serde_json::from_str(&get_sha256_witness(inputs));
|
||||
|
||||
if let Err(e) = witness_result {
|
||||
return Err(format!("{}", e));
|
||||
}
|
||||
|
||||
Ok(witness_result.unwrap().variables.iter().map(|&i| T::from(i)).collect())
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Signed for LibsnarkGadgetHelper {
|
||||
fn get_signature(&self) -> (usize, usize) {
|
||||
match self {
|
||||
LibsnarkGadgetHelper::Sha256Compress => (512, 25561),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field> Executable<T> for RustHelper {
|
||||
fn execute(&self, inputs: &Vec<T>) -> Result<Vec<T>, String> {
|
||||
match self {
|
||||
RustHelper::Identity => Ok(inputs.clone()),
|
||||
RustHelper::ConditionEq => {
|
||||
match inputs[0].is_zero() {
|
||||
true => Ok(vec![T::zero(), T::one()]),
|
||||
false => Ok(vec![T::one(), T::one() / inputs[0].clone()])
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Signed for RustHelper {
|
||||
fn get_signature(&self) -> (usize, usize) {
|
||||
match self {
|
||||
RustHelper::Identity => (1, 1),
|
||||
RustHelper::ConditionEq => (1, 2),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field> Executable<T> for Helper {
|
||||
fn execute(&self, inputs: &Vec<T>) -> Result<Vec<T>, String> {
|
||||
let (expected_input_count, expected_output_count) = self.get_signature();
|
||||
assert!(inputs.len() == expected_input_count);
|
||||
|
||||
let result = match self {
|
||||
#[cfg(feature = "libsnark")]
|
||||
Helper::LibsnarkGadget(helper) => helper.execute(inputs),
|
||||
Helper::Rust(helper) => helper.execute(inputs)
|
||||
};
|
||||
|
@ -145,6 +69,7 @@ impl<T: Field> Executable<T> for Helper {
|
|||
impl Signed for Helper {
|
||||
fn get_signature(&self) -> (usize, usize) {
|
||||
match self {
|
||||
#[cfg(feature = "libsnark")]
|
||||
Helper::LibsnarkGadget(helper) => helper.get_signature(),
|
||||
Helper::Rust(helper) => helper.get_signature()
|
||||
}
|
||||
|
@ -161,7 +86,6 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "nolibsnark"))]
|
||||
fn execute() {
|
||||
let sha = LibsnarkGadgetHelper::Sha256Compress;
|
||||
// second vector here https://homes.esat.kuleuven.be/~nsmart/MPC/sha-256-test.txt
|
42
zokrates_core/src/helpers/rust.rs
Normal file
42
zokrates_core/src/helpers/rust.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use std::fmt;
|
||||
use helpers::{Signed, Executable};
|
||||
use field::{Field};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub enum RustHelper {
|
||||
Identity,
|
||||
ConditionEq,
|
||||
}
|
||||
|
||||
impl fmt::Display for RustHelper {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
RustHelper::Identity => write!(f, "Identity"),
|
||||
RustHelper::ConditionEq => write!(f, "ConditionEq"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Signed for RustHelper {
|
||||
fn get_signature(&self) -> (usize, usize) {
|
||||
match self {
|
||||
RustHelper::Identity => (1, 1),
|
||||
RustHelper::ConditionEq => (1, 2),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T: Field> Executable<T> for RustHelper {
|
||||
fn execute(&self, inputs: &Vec<T>) -> Result<Vec<T>, String> {
|
||||
match self {
|
||||
RustHelper::Identity => Ok(inputs.clone()),
|
||||
RustHelper::ConditionEq => {
|
||||
match inputs[0].is_zero() {
|
||||
true => Ok(vec![T::zero(), T::one()]),
|
||||
false => Ok(vec![T::one(), T::one() / inputs[0].clone()])
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ mod semantics;
|
|||
mod substitution;
|
||||
mod flatten;
|
||||
mod optimizer;
|
||||
#[cfg(feature = "libsnark")]
|
||||
mod standard;
|
||||
mod helpers;
|
||||
mod types;
|
||||
|
|
Loading…
Reference in a new issue