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