aboutsummaryrefslogtreecommitdiff
path: root/c/blake3_c_rust_bindings/src/lib.rs
blob: 41e4938bb0a2e20a649ea015576160c4ee3da9df (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! These are Rust bindings for the C implementation of BLAKE3. As there is a
//! native (and faster) Rust implementation of BLAKE3 provided in this same
//! repo, these bindings are not expected to be used in production. They're
//! intended for testing and benchmarking.

use std::ffi::{c_void, CString};
use std::mem::MaybeUninit;

#[cfg(test)]
mod test;

pub const BLOCK_LEN: usize = 64;
pub const CHUNK_LEN: usize = 1024;
pub const OUT_LEN: usize = 32;

// Feature detection functions for tests and benchmarks. Note that the C code
// does its own feature detection in blake3_dispatch.c.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub fn sse2_detected() -> bool {
    is_x86_feature_detected!("sse2")
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub fn sse41_detected() -> bool {
    is_x86_feature_detected!("sse4.1")
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub fn avx2_detected() -> bool {
    is_x86_feature_detected!("avx2")
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub fn avx512_detected() -> bool {
    is_x86_feature_detected!("avx512f") && is_x86_feature_detected!("avx512vl")
}

#[derive(Clone)]
pub struct Hasher(ffi::blake3_hasher);

impl Hasher {
    pub fn new() -> Self {
        let mut c_state = MaybeUninit::uninit();
        unsafe {
            ffi::blake3_hasher_init(c_state.as_mut_ptr());
            Self(c_state.assume_init())
        }
    }

    pub fn new_keyed(key: &[u8; 32]) -> Self {
        let mut c_state = MaybeUninit::uninit();
        unsafe {
            ffi::blake3_hasher_init_keyed(c_state.as_mut_ptr(), key.as_ptr());
            Self(c_state.assume_init())
        }
    }

    pub fn new_derive_key(context: &str) -> Self {
        let mut c_state = MaybeUninit::uninit();
        let context_c_string = CString::new(context).expect("valid C string, no null bytes");
        unsafe {
            ffi::blake3_hasher_init_derive_key(c_state.as_mut_ptr(), context_c_string.as_ptr());
            Self(c_state.assume_init())
        }
    }

    pub fn new_derive_key_raw(context: &[u8]) -> Self {
        let mut c_state = MaybeUninit::uninit();
        unsafe {
            ffi::blake3_hasher_init_derive_key_raw(
                c_state.as_mut_ptr(),
                context.as_ptr() as *const _,
                context.len(),
            );
            Self(c_state.assume_init())
        }
    }

    pub fn update(&mut self, input: &[u8]) {
        unsafe {
            ffi::blake3_hasher_update(&mut self.0, input.as_ptr() as *const c_void, input.len());
        }
    }

    pub fn finalize(&self, output: &mut [u8]) {
        unsafe {
            ffi::blake3_hasher_finalize(&self.0, output.as_mut_ptr(), output.len());
        }
    }

    pub fn finalize_seek(&self, seek: u64, output: &mut [u8]) {
        unsafe {
            ffi::blake3_hasher_finalize_seek(&self.0, seek, output.as_mut_ptr(), output.len());
        }
    }

    pub fn reset(&mut self) {
        unsafe {
            ffi::blake3_hasher_reset(&mut self.0);
        }
    }
}

pub mod ffi {
    #[repr(C)]
    #[derive(Copy, Clone)]
    pub struct blake3_chunk_state {
        pub cv: [u32; 8usize],
        pub chunk_counter: u64,
        pub buf: [u8; 64usize],
        pub buf_len: u8,
        pub blocks_compressed: u8,
        pub flags: u8,
    }

    #[repr(C)]
    #[derive(Copy, Clone)]
    pub struct blake3_hasher {
        pub key: [u32; 8usize],
        pub chunk: blake3_chunk_state,
        pub cv_stack_len: u8,
        pub cv_stack: [u8; 1728usize],
    }

    extern "C" {
        // public interface
        pub fn blake3_hasher_init(self_: *mut blake3_hasher);
        pub fn blake3_hasher_init_keyed(self_: *mut blake3_hasher, key: *const u8);
        pub fn blake3_hasher_init_derive_key(
            self_: *mut blake3_hasher,
            context: *const ::std::os::raw::c_char,
        );
        pub fn blake3_hasher_init_derive_key_raw(
            self_: *mut blake3_hasher,
            context: *const ::std::os::raw::c_void,
            context_len: usize,
        );
        pub fn blake3_hasher_update(
            self_: *mut blake3_hasher,
            input: *const ::std::os::raw::c_void,
            input_len: usize,
        );
        pub fn blake3_hasher_finalize(self_: *const blake3_hasher, out: *mut u8, out_len: usize);
        pub fn blake3_hasher_finalize_seek(
            self_: *const blake3_hasher,
            seek: u64,
            out: *mut u8,
            out_len: usize,
        );
        pub fn blake3_hasher_reset(self_: *mut blake3_hasher);

        // portable low-level functions
        pub fn blake3_compress_in_place_portable(
            cv: *mut u32,
            block: *const u8,
            block_len: u8,
            counter: u64,
            flags: u8,
        );
        pub fn blake3_compress_xof_portable(
            cv: *const u32,
            block: *const u8,
            block_len: u8,
            counter: u64,
            flags: u8,
            out: *mut u8,
        );
        pub fn blake3_hash_many_portable(
            inputs: *const *const u8,
            num_inputs: usize,
            blocks: usize,
            key: *const u32,
            counter: u64,
            increment_counter: bool,
            flags: u8,
            flags_start: u8,
            flags_end: u8,
            out: *mut u8,
        );
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    pub mod x86 {
        extern "C" {
            // SSE2 low level functions
            pub fn blake3_compress_in_place_sse2(
                cv: *mut u32,
                block: *const u8,
                block_len: u8,
                counter: u64,
                flags: u8,
            );
            pub fn blake3_compress_xof_sse2(
                cv: *const u32,
                block: *const u8,
                block_len: u8,
                counter: u64,
                flags: u8,
                out: *mut u8,
            );
            pub fn blake3_hash_many_sse2(
                inputs: *const *const u8,
                num_inputs: usize,
                blocks: usize,
                key: *const u32,
                counter: u64,
                increment_counter: bool,
                flags: u8,
                flags_start: u8,
                flags_end: u8,
                out: *mut u8,
            );

            // SSE4.1 low level functions
            pub fn blake3_compress_in_place_sse41(
                cv: *mut u32,
                block: *const u8,
                block_len: u8,
                counter: u64,
                flags: u8,
            );
            pub fn blake3_compress_xof_sse41(
                cv: *const u32,
                block: *const u8,
                block_len: u8,
                counter: u64,
                flags: u8,
                out: *mut u8,
            );
            pub fn blake3_hash_many_sse41(
                inputs: *const *const u8,
                num_inputs: usize,
                blocks: usize,
                key: *const u32,
                counter: u64,
                increment_counter: bool,
                flags: u8,
                flags_start: u8,
                flags_end: u8,
                out: *mut u8,
            );

            // AVX2 low level functions
            pub fn blake3_hash_many_avx2(
                inputs: *const *const u8,
                num_inputs: usize,
                blocks: usize,
                key: *const u32,
                counter: u64,
                increment_counter: bool,
                flags: u8,
                flags_start: u8,
                flags_end: u8,
                out: *mut u8,
            );

            // AVX-512 low level functions
            pub fn blake3_compress_xof_avx512(
                cv: *const u32,
                block: *const u8,
                block_len: u8,
                counter: u64,
                flags: u8,
                out: *mut u8,
            );
            pub fn blake3_compress_in_place_avx512(
                cv: *mut u32,
                block: *const u8,
                block_len: u8,
                counter: u64,
                flags: u8,
            );
            pub fn blake3_hash_many_avx512(
                inputs: *const *const u8,
                num_inputs: usize,
                blocks: usize,
                key: *const u32,
                counter: u64,
                increment_counter: bool,
                flags: u8,
                flags_start: u8,
                flags_end: u8,
                out: *mut u8,
            );
        }
    }

    #[cfg(feature = "neon")]
    pub mod neon {
        extern "C" {
            // NEON low level functions
            pub fn blake3_hash_many_neon(
                inputs: *const *const u8,
                num_inputs: usize,
                blocks: usize,
                key: *const u32,
                counter: u64,
                increment_counter: bool,
                flags: u8,
                flags_start: u8,
                flags_end: u8,
                out: *mut u8,
            );
        }
    }
}