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