risc0_zkp_core/
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
15#![no_std]
16#![deny(missing_docs)]
17#![doc = include_str!("../README.md")]
18
19extern crate alloc;
20
21use rand::Rng;
22
23pub mod fp;
24pub mod fp4;
25pub mod ntt;
26pub mod poly;
27pub mod rou;
28pub mod sha;
29pub mod sha_cpu;
30pub mod sha_rng;
31
32/// For x = (1 << po2), given x, find po2.
33pub fn to_po2(x: usize) -> usize {
34    (31 - (x as u32).leading_zeros()) as usize
35}
36
37/// Compute `ceil(log_2(value))`
38///
39/// Find the smallest value `result` such that `2^result >= value`.
40#[inline]
41pub const fn log2_ceil(value: usize) -> usize {
42    let mut result = 0;
43    while (1 << result) < value {
44        result += 1;
45    }
46    result
47}
48
49/// Generic trait for generating random values.
50pub trait Random {
51    /// Generate a uniform random value.
52    fn random<R: Rng>(rng: &mut R) -> Self;
53}
54
55impl Random for u32 {
56    fn random<R: Rng>(rng: &mut R) -> Self {
57        rng.next_u32()
58    }
59}