snarkvm_console_types_address/
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 bitwise;
20mod bytes;
21mod from_bits;
22mod from_field;
23mod from_fields;
24mod parse;
25mod random;
26mod serialize;
27mod size_in_bits;
28mod size_in_bytes;
29mod to_bits;
30mod to_field;
31mod to_fields;
32mod to_group;
33
34pub use snarkvm_console_network_environment::prelude::*;
35pub use snarkvm_console_types_boolean::Boolean;
36pub use snarkvm_console_types_field::Field;
37pub use snarkvm_console_types_group::Group;
38
39#[derive(Copy, Clone, PartialEq, Eq, Hash)]
40pub struct Address<E: Environment> {
41    /// The underlying address.
42    address: Group<E>,
43}
44
45impl<E: Environment> AddressTrait for Address<E> {}
46
47impl<E: Environment> Visibility for Address<E> {
48    type Boolean = Boolean<E>;
49
50    /// Returns the number of field elements to encode `self`.
51    fn size_in_fields(&self) -> Result<u16> {
52        Ok(1)
53    }
54}
55
56impl<E: Environment> Address<E> {
57    /// Initializes an address from a group element.
58    pub const fn new(group: Group<E>) -> Self {
59        Self { address: group }
60    }
61
62    /// Initializes a `zero` address.
63    pub fn zero() -> Self {
64        Self::new(Group::zero())
65    }
66}
67
68impl<E: Environment> TypeName for Address<E> {
69    /// Returns the type name as a string.
70    #[inline]
71    fn type_name() -> &'static str {
72        "address"
73    }
74}
75
76impl<E: Environment> Deref for Address<E> {
77    type Target = Group<E>;
78
79    /// Returns the address as a group element.
80    #[inline]
81    fn deref(&self) -> &Self::Target {
82        &self.address
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use snarkvm_console_network_environment::Console;
90
91    type CurrentEnvironment = Console;
92
93    const ITERATIONS: u64 = 1000;
94
95    #[test]
96    fn test_deref() -> Result<()> {
97        let mut rng = TestRng::default();
98
99        for _ in 0..ITERATIONS {
100            // Sample a new address.
101            let expected = Address::<CurrentEnvironment>::rand(&mut rng);
102
103            // Check the group representation.
104            let candidate = *expected;
105            assert_eq!(expected, Address::new(candidate));
106        }
107        Ok(())
108    }
109}