snarkvm_console_types_field/
lib.rs1#![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 field: E::Field,
42}
43
44impl<E: Environment> FieldTrait for Field<E> {}
45
46impl<E: Environment> Field<E> {
47 pub const SIZE_IN_BITS: usize = E::Field::SIZE_IN_BITS;
49 pub const SIZE_IN_BYTES: usize = (E::Field::SIZE_IN_BITS + 7) / 8;
51 pub const SIZE_IN_DATA_BITS: usize = E::Field::SIZE_IN_DATA_BITS;
53
54 pub const fn new(field: E::Field) -> Self {
56 Self { field }
57 }
58
59 pub fn new_domain_separator(domain: &str) -> Self {
61 Self::new(E::Field::from_bytes_le_mod_order(domain.as_bytes()))
62 }
63
64 pub fn from_u8(value: u8) -> Self {
66 Self { field: E::Field::from(value as u128) }
67 }
68
69 pub fn from_u16(value: u16) -> Self {
71 Self { field: E::Field::from(value as u128) }
72 }
73
74 pub fn from_u32(value: u32) -> Self {
76 Self { field: E::Field::from(value as u128) }
77 }
78
79 pub fn from_u64(value: u64) -> Self {
81 Self { field: E::Field::from(value as u128) }
82 }
83
84 pub fn from_u128(value: u128) -> Self {
86 Self { field: E::Field::from(value) }
87 }
88
89 pub fn half() -> Self {
91 Self { field: E::Field::half() }
92 }
93}
94
95impl<E: Environment> Default for Field<E> {
96 fn default() -> Self {
98 Self::zero()
99 }
100}
101
102impl<E: Environment> TypeName for Field<E> {
103 #[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}