finish removing libsnark
This commit is contained in:
parent
f8fa3efccf
commit
9ad0040dec
4 changed files with 3 additions and 137 deletions
|
@ -12,8 +12,6 @@ pub use self::flat_parameter::FlatParameter;
|
|||
pub use self::flat_variable::FlatVariable;
|
||||
|
||||
use helpers::{DirectiveStatement, Executable};
|
||||
#[cfg(feature = "libsnark")]
|
||||
use standard;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::fmt;
|
||||
use types::Signature;
|
||||
|
@ -62,15 +60,6 @@ impl<T: Field> fmt::Debug for FlatProg<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "libsnark")]
|
||||
impl<T: Field> From<standard::DirectiveR1CS> for FlatProg<T> {
|
||||
fn from(dr1cs: standard::DirectiveR1CS) -> Self {
|
||||
FlatProg {
|
||||
functions: vec![dr1cs.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct FlatFunction<T: Field> {
|
||||
/// Name of the program
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
use helpers::{Executable, Signed};
|
||||
use libsnark::get_sha256round_witness;
|
||||
use serde_json;
|
||||
use standard;
|
||||
use std::fmt;
|
||||
use zokrates_field::field::Field;
|
||||
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub enum LibsnarkGadgetHelper {
|
||||
Sha256Round,
|
||||
}
|
||||
|
||||
impl fmt::Display for LibsnarkGadgetHelper {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
LibsnarkGadgetHelper::Sha256Round => write!(f, "Sha256Round"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field> Executable<T> for LibsnarkGadgetHelper {
|
||||
fn execute(&self, inputs: &Vec<T>) -> Result<Vec<T>, String> {
|
||||
let witness_result: Result<standard::Witness, serde_json::Error> = match self {
|
||||
LibsnarkGadgetHelper::Sha256Round => {
|
||||
serde_json::from_str(&get_sha256round_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::Sha256Round => (768, 25817),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +1,7 @@
|
|||
#[cfg(feature = "libsnark")]
|
||||
mod libsnark_gadget;
|
||||
mod rust;
|
||||
#[cfg(feature = "wasm")]
|
||||
mod wasm;
|
||||
|
||||
#[cfg(feature = "libsnark")]
|
||||
pub use self::libsnark_gadget::LibsnarkGadgetHelper;
|
||||
pub use self::rust::RustHelper;
|
||||
#[cfg(feature = "wasm")]
|
||||
pub use self::wasm::WasmHelper;
|
||||
|
@ -59,8 +55,6 @@ impl<T: Field> fmt::Display for DirectiveStatement<T> {
|
|||
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub enum Helper {
|
||||
#[cfg(feature = "libsnark")]
|
||||
LibsnarkGadget(LibsnarkGadgetHelper),
|
||||
Rust(RustHelper),
|
||||
#[cfg(feature = "wasm")]
|
||||
Wasm(WasmHelper),
|
||||
|
@ -91,8 +85,6 @@ impl 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),
|
||||
#[cfg(feature = "wasm")]
|
||||
Helper::Wasm(ref h) => write!(f, "Wasm::{}", h),
|
||||
|
@ -114,8 +106,6 @@ impl<T: Field> Executable<T> for Helper {
|
|||
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),
|
||||
#[cfg(feature = "wasm")]
|
||||
Helper::Wasm(helper) => helper.execute(inputs),
|
||||
|
@ -136,8 +126,6 @@ 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(),
|
||||
#[cfg(feature = "wasm")]
|
||||
Helper::Wasm(helper) => helper.get_signature(),
|
||||
|
@ -150,69 +138,6 @@ mod tests {
|
|||
use super::*;
|
||||
use zokrates_field::field::FieldPrime;
|
||||
|
||||
#[cfg(feature = "libsnark")]
|
||||
mod sha256libsnark {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn execute() {
|
||||
let sha = LibsnarkGadgetHelper::Sha256Round;
|
||||
// second vector here https://homes.esat.kuleuven.be/~nsmart/MPC/sha-256-test.txt
|
||||
let inputs = vec![
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
|
||||
0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0,
|
||||
0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
|
||||
1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1,
|
||||
0, 0, 0, 0, 1, 1, 1, 0, 0, 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1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
|
||||
1, 0, 0, 1,
|
||||
];
|
||||
let r = sha
|
||||
.execute(&inputs.iter().map(|&i| FieldPrime::from(i)).collect())
|
||||
.unwrap();
|
||||
let r1 = &r[769..1025]; // index of the result
|
||||
let res: Vec<FieldPrime> = vec![
|
||||
1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1,
|
||||
1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0,
|
||||
0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1,
|
||||
0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1,
|
||||
1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1,
|
||||
0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1,
|
||||
0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
|
||||
0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1,
|
||||
0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0,
|
||||
0, 1, 1, 1,
|
||||
]
|
||||
.iter()
|
||||
.map(|&i| FieldPrime::from(i))
|
||||
.collect();
|
||||
assert_eq!(r1, &res[..]);
|
||||
}
|
||||
}
|
||||
|
||||
mod eq_condition {
|
||||
|
||||
// Wanted: (Y = (X != 0) ? 1 : 0)
|
||||
|
|
|
@ -118,7 +118,7 @@ pub fn sha_round<T: Field>() -> FlatFunction<T> {
|
|||
outputs: vec![Type::FieldElement; outputs.len()],
|
||||
};
|
||||
|
||||
// insert a directive to set the witness based on the libsnark gadget and inputs
|
||||
// insert a directive to set the witness based on the bellman gadget and inputs
|
||||
let directive_statement = FlatStatement::Directive(DirectiveStatement {
|
||||
outputs: variables,
|
||||
inputs: inputs,
|
||||
|
@ -198,7 +198,7 @@ mod tests {
|
|||
FlatVariable::new(directive.outputs.len() + 1)
|
||||
);
|
||||
|
||||
// libsnark variable #0: index 0 should equal 1
|
||||
// bellman variable #0: index 0 should equal 1
|
||||
assert_eq!(
|
||||
compiled.statements[1],
|
||||
FlatStatement::Condition(
|
||||
|
@ -207,7 +207,7 @@ mod tests {
|
|||
)
|
||||
);
|
||||
|
||||
// libsnark input #0: index 1 should equal zokrates input #0: index v_count
|
||||
// bellman input #0: index 1 should equal zokrates input #0: index v_count
|
||||
assert_eq!(
|
||||
compiled.statements[2],
|
||||
FlatStatement::Condition(FlatVariable::new(1).into(), FlatVariable::new(26936).into())
|
||||
|
|
Loading…
Reference in a new issue