snarkvm_circuit_program/data/identifier/
from_field.rs

1// Copyright (c) 2019-2025 Provable Inc.
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
16use super::*;
17
18impl<A: Aleo> FromField for Identifier<A> {
19    type Field = Field<A>;
20
21    /// Initializes a new identifier from a field element.
22    fn from_field(field: Self::Field) -> Self {
23        // Convert the field to a list of little-endian bits.
24        Self::from_bits_le(&field.to_bits_le())
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use crate::{Circuit, data::identifier::tests::sample_console_identifier};
32
33    use anyhow::Result;
34
35    const ITERATIONS: u64 = 100;
36
37    fn check_from_field(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) -> Result<()> {
38        for _ in 0..ITERATIONS {
39            // Initialize the console identifier.
40            let console_identifier = sample_console_identifier::<Circuit>()?;
41            // Initialize the circuit list of bits.
42            let circuit_field = Field::constant(console::ToField::to_field(&console_identifier)?);
43
44            Circuit::scope("Identifier FromField", || {
45                let candidate = Identifier::<Circuit>::from_field(circuit_field);
46                assert_eq!(Mode::Constant, candidate.eject_mode());
47                assert_eq!(console_identifier, candidate.eject_value());
48                assert_scope!(num_constants, num_public, num_private, num_constraints);
49            });
50            Circuit::reset();
51        }
52        Ok(())
53    }
54
55    #[test]
56    fn test_from_field() -> Result<()> {
57        check_from_field(253, 0, 0, 0)
58    }
59}