-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathffi.rs
More file actions
189 lines (169 loc) · 6.06 KB
/
ffi.rs
File metadata and controls
189 lines (169 loc) · 6.06 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// SPDX-License-Identifier: CC0-1.0
//! FFI Bindings
//!
//! This module contains bindings to the C library types and functions
//! that are required to execute jets.
//! It is split into several modules, each one corresponding to a `.h` file
//! in the C library.
//!
//! All types are converted to CamelCase and prefixed with the letter C;
//! function names are unchanged.
#![allow(non_camel_case_types)]
pub use core::ffi::c_void;
pub type c_uchar = u8;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_size_t = usize;
pub type c_uint_fast8_t = u8;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub type c_uint_fast16_t = u16;
#[cfg(any(target_os = "windows", target_os = "android", target_arch = "wasm32"))]
pub type c_uint_fast16_t = u32;
#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "windows",
target_os = "android",
target_arch = "wasm32"
)))]
pub type c_uint_fast16_t = usize;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "windows",
target_os = "android",
target_arch = "wasm32"
))]
pub type c_uint_fast32_t = u32;
#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "windows",
target_os = "android",
target_arch = "wasm32"
)))]
pub type c_uint_fast32_t = usize;
#[cfg(target_arch = "wasm32")]
pub type c_uint_fast64_t = u64;
#[cfg(not(target_arch = "wasm32"))]
pub type c_uint_fast64_t = usize;
pub type c_uint_least32_t = u32;
extern "C" {
pub static c_sizeof_uchar: c_size_t;
pub static c_alignof_uchar: c_size_t;
pub static c_sizeof_int: c_size_t;
pub static c_alignof_int: c_size_t;
pub static c_sizeof_uint: c_size_t;
pub static c_alignof_uint: c_size_t;
pub static c_sizeof_size_t: c_size_t;
pub static c_alignof_size_t: c_size_t;
pub static c_sizeof_uint_fast8_t: c_size_t;
pub static c_alignof_uint_fast8_t: c_size_t;
pub static c_sizeof_uint_fast16_t: c_size_t;
pub static c_alignof_uint_fast16_t: c_size_t;
pub static c_sizeof_uint_fast32_t: c_size_t;
pub static c_alignof_uint_fast32_t: c_size_t;
pub static c_sizeof_uint_fast64_t: c_size_t;
pub static c_alignof_uint_fast64_t: c_size_t;
pub static c_sizeof_uint_least32_t: c_size_t;
pub static c_alignof_uint_least32_t: c_size_t;
pub static c_sizeof_long_double: c_size_t;
pub static c_alignof_long_double: c_size_t;
}
pub type ubounded = c_uint_least32_t;
/// Used with `evalTCOProgram` to enforce consensus limits.
pub const BUDGET_MAX: ubounded = 4000050;
/// The max value of UBOUNDED_MAX
pub const UBOUNDED_MAX: ubounded = ubounded::MAX;
extern "C" {
pub static c_sizeof_ubounded: c_size_t;
pub static c_alignof_ubounded: c_size_t;
}
pub mod bounded {
use super::ubounded;
extern "C" {
pub static c_overhead: ubounded;
}
/// constant Overhead of each jet
pub fn cost_overhead() -> ubounded {
unsafe { c_overhead }
}
}
pub type UWORD = c_uint_fast16_t;
extern "C" {
pub static c_sizeof_UWORD: c_size_t;
pub static c_alignof_UWORD: c_size_t;
}
pub mod sha256 {
use super::*;
use hashes::sha256::Midstate;
/// The 256-bit array of a SHA-256 hash or midstate.
#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
pub struct CSha256Midstate {
pub s: [u32; 8],
}
impl From<CSha256Midstate> for Midstate {
fn from(c_midstate: CSha256Midstate) -> Midstate {
let mut inner = [0; 32];
for (idx, chunk) in c_midstate.s.iter().enumerate() {
inner[idx * 4..(idx + 1) * 4].copy_from_slice(&chunk.to_be_bytes());
}
Midstate(inner)
}
}
impl From<Midstate> for CSha256Midstate {
fn from(midstate: Midstate) -> CSha256Midstate {
let mut s = [0; 8];
for (idx, chunk) in midstate.0.chunks(4).enumerate() {
s[idx] = u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
}
CSha256Midstate { s }
}
}
extern "C" {
pub static c_sizeof_sha256_midstate: c_size_t;
pub static c_alignof_sha256_midstate: c_size_t;
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem::{align_of, size_of};
#[test]
#[rustfmt::skip]
fn test_sizes() {
unsafe {
assert_eq!(size_of::<c_uchar>(), c_sizeof_uchar);
assert_eq!(size_of::<c_int>(), c_sizeof_int);
assert_eq!(size_of::<c_uint>(), c_sizeof_uint);
assert_eq!(size_of::<c_size_t>(), c_sizeof_size_t);
assert_eq!(size_of::<c_uint_fast8_t>(), c_sizeof_uint_fast8_t);
assert_eq!(size_of::<c_uint_fast16_t>(), c_sizeof_uint_fast16_t);
assert_eq!(size_of::<c_uint_fast32_t>(), c_sizeof_uint_fast32_t);
assert_eq!(size_of::<c_uint_fast64_t>(), c_sizeof_uint_fast64_t);
assert_eq!(size_of::<c_uint_least32_t>(), c_sizeof_uint_least32_t);
assert_eq!(size_of::<ubounded>(), c_sizeof_ubounded);
assert_eq!(size_of::<UWORD>(), c_sizeof_UWORD);
assert_eq!(size_of::<sha256::CSha256Midstate>(), sha256::c_sizeof_sha256_midstate);
}
}
#[test]
#[rustfmt::skip]
fn test_aligns() {
unsafe {
assert_eq!(align_of::<c_uchar>(), c_alignof_uchar);
assert_eq!(align_of::<c_int>(), c_alignof_int);
assert_eq!(align_of::<c_uint>(), c_alignof_uint);
assert_eq!(align_of::<c_size_t>(), c_alignof_size_t);
assert_eq!(align_of::<c_uint_fast8_t>(), c_alignof_uint_fast8_t);
assert_eq!(align_of::<c_uint_fast16_t>(), c_alignof_uint_fast16_t);
assert_eq!(align_of::<c_uint_fast32_t>(), c_alignof_uint_fast32_t);
assert_eq!(align_of::<c_uint_fast64_t>(), c_alignof_uint_fast64_t);
assert_eq!(align_of::<c_uint_least32_t>(), c_alignof_uint_least32_t);
assert_eq!(align_of::<ubounded>(), c_alignof_ubounded);
assert_eq!(align_of::<UWORD>(), c_alignof_UWORD);
assert_eq!(align_of::<sha256::CSha256Midstate>(), sha256::c_alignof_sha256_midstate);
}
}
}