snarkvm_console_types_integers/
lib.rs

1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#![cfg_attr(test, allow(clippy::assertions_on_result_states))]
17#![warn(clippy::cast_possible_truncation)]
18
19mod arithmetic;
20mod bitwise;
21mod bytes;
22mod compare;
23mod from_bits;
24mod from_field;
25mod from_field_lossy;
26mod from_fields;
27mod one;
28mod parse;
29mod random;
30mod serialize;
31mod size_in_bits;
32mod size_in_bytes;
33mod to_bits;
34mod to_field;
35mod to_fields;
36mod to_scalar;
37mod zero;
38
39pub use snarkvm_console_network_environment::prelude::*;
40pub use snarkvm_console_types_boolean::Boolean;
41pub use snarkvm_console_types_field::Field;
42pub use snarkvm_console_types_scalar::Scalar;
43
44use core::marker::PhantomData;
45
46pub type I8<E> = Integer<E, i8>;
47pub type I16<E> = Integer<E, i16>;
48pub type I32<E> = Integer<E, i32>;
49pub type I64<E> = Integer<E, i64>;
50pub type I128<E> = Integer<E, i128>;
51
52pub type U8<E> = Integer<E, u8>;
53pub type U16<E> = Integer<E, u16>;
54pub type U32<E> = Integer<E, u32>;
55pub type U64<E> = Integer<E, u64>;
56pub type U128<E> = Integer<E, u128>;
57
58#[derive(Copy, Clone, PartialEq, Eq, Hash)]
59pub struct Integer<E: Environment, I: IntegerType> {
60    /// The underlying integer value.
61    integer: I,
62    /// PhantomData.
63    _phantom: PhantomData<E>,
64}
65
66impl<E: Environment, I: IntegerType> IntegerTrait<I, U8<E>, U16<E>, U32<E>> for Integer<E, I> {}
67
68impl<E: Environment, I: IntegerType> IntegerCore<I> for Integer<E, I> {}
69
70impl<E: Environment, I: IntegerType> Visibility for Integer<E, I> {
71    type Boolean = Boolean<E>;
72
73    /// Returns the number of field elements to encode `self`.
74    fn size_in_fields(&self) -> Result<u16> {
75        Ok(1)
76    }
77}
78
79impl<E: Environment, I: IntegerType> Integer<E, I> {
80    pub const MAX: Self = Self::new(I::MAX);
81    pub const MIN: Self = Self::new(I::MIN);
82
83    /// Initializes a new integer.
84    pub const fn new(integer: I) -> Self {
85        Self { integer, _phantom: PhantomData }
86    }
87}
88
89impl<E: Environment, I: IntegerType> TypeName for Integer<E, I> {
90    /// Returns the type name as a string.
91    #[inline]
92    fn type_name() -> &'static str {
93        I::type_name()
94    }
95}
96
97impl<E: Environment, I: IntegerType> Deref for Integer<E, I> {
98    type Target = I;
99
100    #[inline]
101    fn deref(&self) -> &Self::Target {
102        &self.integer
103    }
104}