snarkvm_circuit_program/state_path/helpers/
header_leaf.rs

1// Copyright 2024-2025 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
16use snarkvm_circuit_network::Aleo;
17use snarkvm_circuit_types::{Boolean, Field, U8, environment::prelude::*};
18
19#[derive(Clone)]
20pub struct HeaderLeaf<A: Aleo> {
21    /// The index of the Merkle leaf.
22    index: U8<A>,
23    /// The ID.
24    id: Field<A>,
25}
26
27impl<A: Aleo> HeaderLeaf<A> {
28    /// Returns the index of the Merkle leaf.
29    pub fn index(&self) -> &U8<A> {
30        &self.index
31    }
32
33    /// Returns the ID in the Merkle leaf.
34    pub const fn id(&self) -> &Field<A> {
35        &self.id
36    }
37}
38
39impl<A: Aleo> Inject for HeaderLeaf<A> {
40    type Primitive = console::HeaderLeaf<A::Network>;
41
42    /// Initializes a new header leaf circuit from a primitive.
43    fn new(mode: Mode, leaf: Self::Primitive) -> Self {
44        Self { index: U8::new(mode, console::U8::new(leaf.index())), id: Field::new(mode, leaf.id()) }
45    }
46}
47
48impl<A: Aleo> Eject for HeaderLeaf<A> {
49    type Primitive = console::HeaderLeaf<A::Network>;
50
51    /// Ejects the mode of the header leaf.
52    fn eject_mode(&self) -> Mode {
53        (&self.index, &self.id).eject_mode()
54    }
55
56    /// Ejects the header leaf.
57    fn eject_value(&self) -> Self::Primitive {
58        Self::Primitive::new(*self.index.eject_value(), self.id.eject_value())
59    }
60}
61
62impl<A: Aleo> ToBits for HeaderLeaf<A> {
63    type Boolean = Boolean<A>;
64
65    /// Outputs the little-endian bit representation of `self` *without* trailing zeros.
66    fn write_bits_le(&self, vec: &mut Vec<Self::Boolean>) {
67        self.index.write_bits_le(vec);
68        self.id.write_bits_le(vec);
69    }
70
71    /// Outputs the big-endian bit representation of `self` *without* leading zeros.
72    fn write_bits_be(&self, vec: &mut Vec<Self::Boolean>) {
73        self.index.write_bits_be(vec);
74        self.id.write_bits_be(vec);
75    }
76}