hacspec_lib/lib.rs
1//! # The hacspec standard library
2//!
3//! ## Data types
4//! The standard library provides two main data types.
5//!
6//! ### Sequences
7//! Sequences [`Seq`](`seq::Seq`) arrays with a fixed length set at runtime.
8//! They replace Rust vectors, which are not allowed in hacspec.
9//!
10//! See the [seq](`mod@seq`) module documentation for more details.
11//!
12//! ```
13//! use hacspec_lib::*;
14//! let x = Seq::<U128>::from_public_slice(&[5, 2, 7, 8, 9]);
15//! let x = Seq::<u128>::from_native_slice(&[5, 2, 7, 8, 9]);
16//! let y = ByteSeq::from_hex("0388dace60b6a392f328c2b971b2fe78");
17//! ```
18//!
19//! ### Arrays
20//! Arrays have a fixed length that is known at compile time.
21//! They replace the Rust arrays, which are not allowed in hacspec.
22//!
23//! See the [arrays](`mod@array`) module documentation for more details.
24//!
25//! To define a new array type with name `State`, holding `16` `u32` run
26//!
27//! ```
28//! use hacspec_lib::*;
29//! array!(State, 16, u32, type_for_indexes: StateIdx);
30//! ```
31//!
32//! The `type_for_indexes` defines the index type for this array as `StateIdx`.
33//! Such an array can now be used similarly to regular Rust arrays.
34//!
35//! ```
36//! use hacspec_lib::*;
37//! array!(State, 16, u32, type_for_indexes: StateIdx);
38//! fn modify_state(mut state: State) -> State {
39//! state[1] = state[1] + state[2];
40//! state
41//! }
42//! ```
43//!
44//! ## Numeric Types
45//! The standard library provides two main numeric types.
46//!
47//! ### Math Integers
48//! Integers with a fixed upper bound on the byte length.
49//! See the [math integer](`mod@math_integers`) module documentation for more details.
50//!
51//! The following example defines and uses the type `LargeSecretInteger` that can hold unsigned integers up to 2^233-1.
52//!
53//! ```
54//! use hacspec_lib::*;
55//! unsigned_integer!(LargeSecretInteger, 233);
56//! let a = LargeSecretInteger::from_literal(1);
57//! let b = LargeSecretInteger::from_literal(2);
58//! let c = a + b;
59//! let result = std::panic::catch_unwind(|| {
60//! // This panics because comparing secret math integers is currently not support.
61//! assert!(c.equal(LargeSecretInteger::from_literal(3)));
62//! });
63//! assert!(result.is_err());
64//! let _max = LargeSecretInteger::from_hex("1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
65//! ```
66//!
67//! ## Secret Integers
68//! All numeric types can be public or secret.
69//! By default they are secret types.
70//! Public types are prefixed with `public_`.
71//!
72//! ### Secret Machine Integers
73//! The regular machine integers Rust provides are considered public integers.
74//! This standard library defines secret variants for all public machine integers defined as follows.
75//!
76//! Unsigned secret integers: `U8, U16, U32, U64, U128`
77//!
78//! Signed secret integers: `I8, I16, I32, I64, I128`
79//!
80//! See the [secret integers](`secret_integers`) for details.
81
82#![no_std]
83
84#[cfg(all(feature = "alloc", not(feature = "std")))]
85extern crate alloc;
86#[cfg(feature = "std")]
87extern crate std as alloc;
88
89pub mod array;
90mod bigint_integers;
91mod machine_integers;
92pub mod math_integers;
93mod math_util;
94pub mod prelude;
95pub mod seq;
96mod traits;
97mod transmute;
98mod util;
99mod vec_integers;
100mod vec_integers_public;
101mod vec_integers_secret;
102mod vec_util;
103pub mod buf;
104
105pub use crate::prelude::*;