snarkvm_console_types_field/
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 one;
25mod parse;
26mod random;
27mod serialize;
28mod size_in_bits;
29mod size_in_bytes;
30mod to_bits;
31mod zero;
32
33pub use snarkvm_console_network_environment::prelude::*;
34pub use snarkvm_console_types_boolean::Boolean;
35
36use zeroize::Zeroize;
37
38#[derive(Copy, Clone, PartialEq, Eq, Hash, Zeroize)]
39pub struct Field<E: Environment> {
40    /// The underlying field element.
41    field: E::Field,
42}
43
44impl<E: Environment> FieldTrait for Field<E> {}
45
46impl<E: Environment> Field<E> {
47    /// The field size in bits.
48    pub const SIZE_IN_BITS: usize = E::Field::SIZE_IN_BITS;
49    /// The field size in bytes.
50    pub const SIZE_IN_BYTES: usize = (E::Field::SIZE_IN_BITS + 7) / 8;
51    /// The field capacity for data bits.
52    pub const SIZE_IN_DATA_BITS: usize = E::Field::SIZE_IN_DATA_BITS;
53
54    /// Initializes a new field.
55    pub const fn new(field: E::Field) -> Self {
56        Self { field }
57    }
58
59    /// Initializes a new field as a domain separator.
60    pub fn new_domain_separator(domain: &str) -> Self {
61        Self::new(E::Field::from_bytes_le_mod_order(domain.as_bytes()))
62    }
63
64    /// Initializes a new field from a `u8`.
65    pub fn from_u8(value: u8) -> Self {
66        Self { field: E::Field::from(value as u128) }
67    }
68
69    /// Initializes a new field from a `u16`.
70    pub fn from_u16(value: u16) -> Self {
71        Self { field: E::Field::from(value as u128) }
72    }
73
74    /// Initializes a new field from a `u32`.
75    pub fn from_u32(value: u32) -> Self {
76        Self { field: E::Field::from(value as u128) }
77    }
78
79    /// Initializes a new field from a `u64`.
80    pub fn from_u64(value: u64) -> Self {
81        Self { field: E::Field::from(value as u128) }
82    }
83
84    /// Initializes a new field from a `u128`.
85    pub fn from_u128(value: u128) -> Self {
86        Self { field: E::Field::from(value) }
87    }
88
89    /// Returns `1 * 2^{-1}`.
90    pub fn half() -> Self {
91        Self { field: E::Field::half() }
92    }
93}
94
95impl<E: Environment> Default for Field<E> {
96    /// Returns the default field element.
97    fn default() -> Self {
98        Self::zero()
99    }
100}
101
102impl<E: Environment> TypeName for Field<E> {
103    /// Returns the type name as a string.
104    #[inline]
105    fn type_name() -> &'static str {
106        "field"
107    }
108}
109
110impl<E: Environment> Deref for Field<E> {
111    type Target = E::Field;
112
113    #[inline]
114    fn deref(&self) -> &Self::Target {
115        &self.field
116    }
117}
118
119impl<E: Environment> DerefMut for Field<E> {
120    #[inline]
121    fn deref_mut(&mut self) -> &mut Self::Target {
122        &mut self.field
123    }
124}