-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
99 lines (77 loc) · 2.18 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::sync::Mutex;
extern crate libc;
#[macro_use]
extern crate lazy_static;
extern crate rustc_serialize;
lazy_static! {
static ref INIT_LOCK: Mutex<bool> = Mutex::new(false);
}
mod arith;
extern "C" {
fn hackishlibsnarkbindings_init();
fn hackishlibsnarkbindings_gen_keypair();
fn hackishlibsnarkbindings_test() -> bool;
fn hackishlibsnarkbindings_verify(
vk: *const libc::c_uchar,
vk_size: libc::uint32_t,
proof: *const libc::c_uchar,
proof_size: libc::uint32_t,
primary: *const libc::c_uchar,
primary_size: libc::uint32_t
) -> bool;
fn generate_proof(
sk: *const libc::c_uchar,
nf: *const libc::c_uchar,
addr: *const libc::c_uchar,
path: *const libc::c_uchar,
positions: *const bool
) -> [u8; 584];
}
pub fn gen_keypair() {
initialize();
unsafe { hackishlibsnarkbindings_gen_keypair(); }
}
pub fn genproof(sk: &[u8], nf: &[u8], addr: &[u8], path: &Vec<Vec<u8>>, positions: &[bool]) -> [u8; 584] {
initialize();
assert_eq!(path.len(), 4);
let path: Vec<u8> = path.iter().flat_map(|a| a.iter()).map(|a| *a).collect();
assert_eq!(path.len(), 4 * 32);
assert_eq!(sk.len(), 32);
assert_eq!(nf.len(), 32);
assert_eq!(addr.len(), 32);
assert_eq!(positions.len(), 4);
unsafe { generate_proof(&sk[0], &nf[0], &addr[0], &path[0], &positions[0]) }
}
pub fn snark_verify(
vk: &[u8],
proof: &[u8],
primary: &[u8]
) -> bool {
initialize();
unsafe { hackishlibsnarkbindings_verify(&vk[0], vk.len() as u32, &proof[0], proof.len() as u32, &primary[0], primary.len() as u32) }
}
pub fn initialize() {
let mut l = INIT_LOCK.lock().unwrap();
if !*l {
unsafe { hackishlibsnarkbindings_init(); }
*l = true;
}
}
#[test]
fn lol() {
use arith::*;
let b = FieldT::random();
}
#[test]
fn test_from() {
use arith::*;
let a = FieldT::one() + FieldT::one() + FieldT::one() + FieldT::one();
let b = FieldT::from_str("4").unwrap();
assert!(a == b);
assert!(a + FieldT::one() != b);
}
#[test]
fn test_dummy_circuit() {
initialize();
assert!(unsafe { hackishlibsnarkbindings_test() });
}