1extern crate libc;
2use libc::c_char;
3
4include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
5
6
7pub fn hash(input: &[u8; 80]) -> Result<[u8; 32], &'static str> {
8 let mut output = [0u8; 32];
9
10 let result = unsafe {
11 yespower_hash(input.as_ptr() as *const c_char, output.as_mut_ptr() as *mut c_char)
12 };
13
14 if result == 0 {
15 Ok(output)
16 } else {
17 Err("Failed to compute yespower hash")
18 }
19}
20
21#[cfg(test)]
22mod tests {
23 use super::*;
24 use hex;
25
26 #[test]
27 fn test_hash() {
28 let input_hex = "0000002009f42768de3cfb4e58fc56368c1477f87f60e248d7130df3fb8acd7f6208b83a72f90dd3ad8fe06c7f70d73f256f1e07185dcc217a58b9517c699226ac0297d2ad60ba61b62a021d9b7700f0";
29 let expected_output_hex = "9d90c21b5a0bb9566d2999c5d703d7327ee3ac97c020d387aa2dfd0700000000";
30
31 let input_bytes: [u8; 80] = hex::decode(input_hex).expect("Decoding failed").try_into().expect("Incorrect input length");
32 let expected_output_bytes: [u8; 32] = hex::decode(expected_output_hex).expect("Decoding failed").try_into().expect("Incorrect output length");
33
34 match hash(&input_bytes) {
35 Ok(output) => assert_eq!(output, expected_output_bytes),
36 Err(e) => panic!("Hashing failed: {}", e),
37 }
38
39 }
40}