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