Skip to main content

risc0_zkp_hal/
lib.rs

1// Copyright 2022 Risc0, 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
15pub mod cpu;
16
17use std::rc::Rc;
18
19use downcast_rs::{impl_downcast, Downcast};
20use risc0_zkp_core::{fp::Fp, fp4::Fp4, sha::Digest};
21
22pub type Buffer<T> = Rc<dyn BufferTrait<T>>;
23
24pub trait BufferTrait<T>: Downcast {
25    fn size(&self) -> usize;
26
27    fn slice(&self, offset: usize, size: usize) -> Buffer<T>;
28
29    fn view(&self, f: &mut dyn FnMut(&[T]));
30
31    fn view_mut(&self, f: &mut dyn FnMut(&mut [T]));
32}
33
34impl_downcast!(BufferTrait<T>);
35
36pub trait Hal {
37    fn alloc<T: 'static + Default + Clone>(&self, size: usize) -> Buffer<T>;
38
39    fn copy_from<T>(&self, slice: &[T]) -> Buffer<T>;
40
41    fn batch_expand(&self, output: &Buffer<Fp>, input: &Buffer<Fp>, count: usize);
42
43    fn batch_evaluate_ntt(&self, io: &Buffer<Fp>, count: usize, expand_bits: usize);
44
45    fn batch_interpolate_ntt(&self, io: &Buffer<Fp>, count: usize);
46
47    fn batch_bit_reverse(&self, io: &Buffer<Fp>, count: usize);
48
49    fn batch_evaluate_any(
50        &self,
51        coeffs: &Buffer<Fp>,
52        poly_count: usize,
53        which: &Buffer<u32>,
54        xs: &Buffer<Fp4>,
55        out: &Buffer<Fp4>,
56    );
57
58    fn zk_shift(&self, io: &Buffer<Fp>, count: usize);
59
60    fn mix_poly_coeffs(
61        &self,
62        out: &Buffer<Fp4>,
63        mix_start: &Buffer<Fp4>,
64        mix: &Buffer<Fp4>,
65        input: &Buffer<Fp>,
66        combos: &Buffer<u32>,
67        input_size: usize,
68        count: usize,
69    );
70
71    fn eltwise_add_fp(&self, output: &Buffer<Fp>, input1: &Buffer<Fp>, input2: &Buffer<Fp>);
72
73    fn eltwise_sum_fp4(&self, output: &Buffer<Fp>, input: &Buffer<Fp4>);
74
75    fn eltwise_copy_fp(&self, output: &Buffer<Fp>, input: &Buffer<Fp>);
76
77    fn eltwise_copy_digest(&self, output: &Buffer<Digest>, input: &Buffer<Digest>);
78
79    fn fri_fold(&self, output: &Buffer<Fp>, input: &Buffer<Fp>, mix: &Buffer<Fp4>);
80
81    fn sha_rows(&self, output: &Buffer<Digest>, matrix: &Buffer<Fp>);
82
83    fn sha_fold(&self, output: &Buffer<Digest>, input: &Buffer<Digest>);
84}