Skip to main content

airbender_crypto/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(test), no_std)]
3#![allow(static_mut_refs)]
4#![allow(clippy::uninit_assumed_init)]
5#![allow(clippy::new_without_default)]
6#![feature(allocator_api)]
7#[allow(clippy::all)]
8#[allow(unused_imports, dead_code)]
9#[cfg(any(
10    all(target_arch = "riscv32", feature = "bigint_ops"),
11    feature = "proving",
12    feature = "testing",
13    test
14))]
15pub mod ark_ff_delegation;
16#[allow(clippy::all)]
17#[allow(unused_imports, dead_code)]
18#[cfg(any(
19    all(target_arch = "riscv32", feature = "bigint_ops"),
20    feature = "proving",
21    feature = "testing",
22    test
23))]
24mod bigint_delegation;
25#[allow(unexpected_cfgs)]
26pub mod blake2s;
27#[allow(clippy::all)]
28pub mod bls12_381;
29#[allow(clippy::all)]
30pub mod bn254;
31mod glv_decomposition;
32pub mod k256;
33pub mod p256;
34pub mod ripemd160;
35pub mod secp256k1;
36pub mod secp256r1;
37pub mod sha256;
38pub mod sha3;
39
40pub use k256 as rust_k256;
41
42#[cfg(any(
43    all(target_arch = "riscv32", feature = "bigint_ops"),
44    feature = "proving",
45    test
46))]
47pub use self::ark_ff_delegation::{BigInt, BigInteger};
48
49#[cfg(not(any(
50    all(target_arch = "riscv32", feature = "bigint_ops"),
51    feature = "proving",
52    test
53)))]
54pub use self::ark_ff::{BigInt, BigInteger};
55
56#[cfg(any(
57    all(target_arch = "riscv32", feature = "bigint_ops"),
58    feature = "proving",
59    test
60))]
61pub use crate::ark_ff_delegation::Fp;
62
63#[cfg(not(any(
64    all(target_arch = "riscv32", feature = "bigint_ops"),
65    feature = "proving",
66    test
67)))]
68pub use ark_ff::Fp;
69
70#[cfg(any(
71    all(target_arch = "riscv32", feature = "bigint_ops"),
72    feature = "proving",
73    feature = "testing",
74    test
75))]
76mod raw_delegation_interface;
77
78pub use blake2 as blake2_ext;
79
80pub use ark_ec;
81pub use ark_ff;
82pub use ark_serialize;
83
84#[cfg(any(
85    all(target_arch = "riscv32", feature = "bigint_ops"),
86    feature = "proving",
87    feature = "testing",
88    test
89))]
90pub use self::raw_delegation_interface::{
91    bigint_op_delegation_raw, bigint_op_delegation_with_carry_bit_raw,
92};
93
94// TODO: Keep this compatibility shim while external call sites migrate away from
95// explicit initialization requirements.
96pub fn init_lib() {}
97
98pub enum BigIntOps {
99    Add = 0,
100    Sub = 1,
101    SubAndNegate = 2,
102    MulLow = 3,
103    MulHigh = 4,
104    Eq = 5,
105    MemCpy = 7,
106}
107
108pub trait MiniDigest: Sized {
109    type HashOutput;
110
111    fn new() -> Self;
112    fn digest(input: impl AsRef<[u8]>) -> Self::HashOutput;
113    fn update(&mut self, input: impl AsRef<[u8]>);
114    fn finalize(self) -> Self::HashOutput;
115    fn finalize_reset(&mut self) -> Self::HashOutput;
116}
117
118///
119/// Parse the byte array as a BE 32-byte BigInt.
120/// If length is less than 32 bytes, it will be left-padded (most significant bytes) with zeroes.
121///
122pub fn parse_u256_be<const N: usize>(input: &[u8; N]) -> BigInt<4> {
123    assert!(N <= 32);
124    // Arkworks has strange format for integer serialization, so we do manually
125    let mut repr = [0u64; 4];
126    let mut repr_index = 0usize;
127    let mut offset = input.len();
128    while offset >= 8 {
129        offset -= 8;
130        repr[repr_index] = u64::from_be_bytes(input[offset..offset + 8].try_into().unwrap());
131        repr_index += 1;
132    }
133    if offset != 0 {
134        let mut buff = [0u8; 8];
135        buff[8 - offset..].copy_from_slice(&input[..offset]);
136        repr[repr_index] = u64::from_be_bytes(buff);
137    }
138    BigInt::new(repr)
139}