1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

fix tests

This commit is contained in:
sdeml 2019-01-13 16:45:05 +01:00
parent 1d3f4268e7
commit e84e0efd97
5 changed files with 59 additions and 8 deletions

View file

@ -1,6 +1,6 @@
import "LIBSNARK/sha256round" as sha256
def main(field[256] a, field[256] b, field[256] IV) -> (field[256]):
def main(field unused) -> (field[256]):
field[256] a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
field[256] b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1]

View file

@ -123,7 +123,7 @@ mod tests {
#[test]
fn execute() {
let sha = LibsnarkGadgetHelper::Sha256Compress;
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,
@ -145,11 +145,22 @@ mod tests {
0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1,
1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1,
// append SHA256 IV vector (https://en.wikipedia.org/wiki/SHA-2)
0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0,
1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1,
0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1,
0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 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[513..769]; // index of the result
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,

View file

@ -63,22 +63,22 @@ mod tests {
#[test]
fn can_get_sha256_constraints() {
let _a = get_sha256_constraints();
let _a = get_sha256round_constraints();
}
#[test]
fn can_generate_sha_256_witness_null() {
let inputs = vec![FieldPrime::from(0); 512];
let _b = get_sha256_witness(&inputs);
let inputs = vec![FieldPrime::from(0); 768];
let _b = get_sha256round_witness(&inputs);
}
#[test]
fn can_generate_flattened_code() {
let constraints = get_sha256_constraints();
let constraints = get_sha256round_constraints();
let r1cs: standard::R1CS = serde_json::from_str(&constraints).unwrap();
let _prog: FlatProg<FieldPrime> = FlatProg::from(standard::DirectiveR1CS {
r1cs,
directive: helpers::LibsnarkGadgetHelper::Sha256Compress,
directive: helpers::LibsnarkGadgetHelper::Sha256Round,
});
}
}

View file

@ -212,4 +212,36 @@ mod tests {
let c: Constraint = serde_json::from_str(constraint).unwrap();
let _statement: FlatStatement<FieldPrime> = c.into();
}
#[test]
fn generate_sha256_constraints() {
use flat_absy::FlatProg;
use libsnark::get_sha256round_constraints;
let r1cs: R1CS = serde_json::from_str(&get_sha256round_constraints()).unwrap();
let v_count = r1cs.variable_count;
let dr1cs: DirectiveR1CS = DirectiveR1CS {
r1cs,
directive: LibsnarkGadgetHelper::Sha256Round,
};
let compiled: FlatProg<FieldPrime> = FlatProg::from(dr1cs);
// libsnark variable #0: index 0 should equal 1
assert_eq!(
compiled.functions[0].statements[1],
FlatStatement::Condition(
FlatVariable::new(0).into(),
FlatExpression::Number(FieldPrime::from(1))
)
);
// libsnark input #0: index 1 should equal zokrates input #0: index v_count
assert_eq!(
compiled.functions[0].statements[2],
FlatStatement::Condition(
FlatVariable::new(1).into(),
FlatVariable::new(v_count).into()
)
);
}
}

View file

@ -0,0 +1,8 @@
#[macro_use]
extern crate lazy_static;
extern crate num;
extern crate num_bigint;
extern crate serde;
extern crate bincode;
pub mod field;