Skip to main content

risc0_circuit_keccak_sys/
lib.rs

1// Copyright 2024 RISC Zero, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[cfg(feature = "cuda")]
16use cust::memory::DevicePointer;
17
18use risc0_core::field::baby_bear::{BabyBearElem, BabyBearExtElem};
19
20#[derive(Debug)]
21#[repr(C)]
22pub struct ScatterInfo {
23    pub offset: u32,
24    pub row: u32,
25    pub col: u16,
26    pub count: u16,
27    pub bits: u32,
28}
29
30#[repr(C)]
31pub struct RawPreflightTrace {
32    pub preimages: *const [u64; 25],
33    pub preimages_count: u32,
34    pub preimage_idxs: *const u32,
35    pub run_order: *const u32,
36}
37
38#[repr(C)]
39pub struct RawExecBuffers {
40    pub global: RawBuffer,
41    pub data: RawBuffer,
42}
43
44#[repr(C)]
45pub struct RawBuffer {
46    pub buf: *const BabyBearElem,
47    pub rows: usize,
48    pub cols: usize,
49    pub checked_reads: bool,
50}
51
52extern "C" {
53    pub fn risc0_circuit_keccak_cpu_witgen(
54        mode: u32,
55        buffers: *const RawExecBuffers,
56        preflight: *const RawPreflightTrace,
57        cycles: u32,
58    ) -> *const std::os::raw::c_char;
59
60    pub fn risc0_circuit_keccak_cpu_poly_fp(
61        cycle: usize,
62        steps: usize,
63        poly_mixs: *const BabyBearExtElem,
64        args_ptr: *const *const BabyBearElem,
65        result: *mut BabyBearExtElem,
66    ) -> *const std::os::raw::c_char;
67}
68
69#[cfg(feature = "cuda")]
70extern "C" {
71    pub fn risc0_circuit_keccak_cuda_scatter(
72        into: DevicePointer<u8>,
73        infos: *const ScatterInfo,
74        from: DevicePointer<u8>,
75        rows: u32,
76        count: u32,
77    ) -> *const std::os::raw::c_char;
78
79    pub fn risc0_circuit_keccak_cuda_witgen(
80        mode: u32,
81        buffers: *const RawExecBuffers,
82        preflight: *const RawPreflightTrace,
83        cycles: u32,
84    ) -> *const std::os::raw::c_char;
85
86    pub fn risc0_circuit_keccak_cuda_eval_check(
87        check: DevicePointer<u8>,
88        ctrl: DevicePointer<u8>,
89        data: DevicePointer<u8>,
90        accum: DevicePointer<u8>,
91        mix: DevicePointer<u8>,
92        out: DevicePointer<u8>,
93        rou: *const BabyBearElem,
94        po2: u32,
95        domain: u32,
96        poly_mix_pows: *const u32,
97    ) -> *const std::os::raw::c_char;
98
99    pub fn risc0_circuit_keccak_cuda_reset() -> *const std::os::raw::c_char;
100}