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