snarkvm_console_types_scalar/
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 one;
27mod parse;
28mod random;
29mod serialize;
30mod size_in_bits;
31mod size_in_bytes;
32mod to_bits;
33mod to_field;
34mod zero;
35
36pub use snarkvm_console_network_environment::prelude::*;
37pub use snarkvm_console_types_boolean::Boolean;
38pub use snarkvm_console_types_field::Field;
39
40use zeroize::Zeroize;
41
42#[derive(Copy, Clone, PartialEq, Eq, Hash, Zeroize)]
43pub struct Scalar<E: Environment> {
44    /// The underlying scalar element.
45    scalar: E::Scalar,
46}
47
48impl<E: Environment> ScalarTrait for Scalar<E> {}
49
50impl<E: Environment> Scalar<E> {
51    /// The scalar size in bits.
52    pub const SIZE_IN_BITS: usize = E::Scalar::SIZE_IN_BITS;
53    /// The scalar size in bytes.
54    pub const SIZE_IN_BYTES: usize = (E::Scalar::SIZE_IN_BITS + 7) / 8;
55    /// The scalar capacity for data bits.
56    pub const SIZE_IN_DATA_BITS: usize = E::Scalar::SIZE_IN_DATA_BITS;
57
58    /// Initializes a new scalar.
59    pub const fn new(scalar: E::Scalar) -> Self {
60        Self { scalar }
61    }
62}
63
64impl<E: Environment> TypeName for Scalar<E> {
65    /// Returns the type name as a string.
66    #[inline]
67    fn type_name() -> &'static str {
68        "scalar"
69    }
70}
71
72impl<E: Environment> Deref for Scalar<E> {
73    type Target = E::Scalar;
74
75    #[inline]
76    fn deref(&self) -> &Self::Target {
77        &self.scalar
78    }
79}