snarkvm_circuit_environment/traits/to.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 crate::{BooleanTrait, FieldTrait, GroupTrait, ScalarTrait};
17
18/// Unary operator for converting to `k` number of bits.
19pub trait ToLowerBits {
20 type Boolean: BooleanTrait;
21
22 ///
23 /// Outputs the lower `k` bits of an `n`-bit element in little-endian representation.
24 /// Enforces that the upper `n - k` bits are zero.
25 ///
26 fn to_lower_bits_le(&self, k: usize) -> Vec<Self::Boolean>;
27
28 ///
29 /// Outputs the lower `k` bits of an `n`-bit element in big-endian representation.
30 /// Enforces that the upper `n - k` bits are zero.
31 ///
32 fn to_lower_bits_be(&self, k: usize) -> Vec<Self::Boolean>;
33}
34
35/// Unary operator for converting to `k` number of bits.
36pub trait ToUpperBits {
37 type Boolean: BooleanTrait;
38
39 ///
40 /// Outputs the upper `k` bits of an `n`-bit element in little-endian representation.
41 /// Enforces that the lower `n - k` bits are zero.
42 ///
43 fn to_upper_bits_le(&self, k: usize) -> Vec<Self::Boolean>;
44
45 ///
46 /// Outputs the upper `k` bits of an `n`-bit element in big-endian representation.
47 /// Enforces that the lower `n - k` bits are zero.
48 ///
49 fn to_upper_bits_be(&self, k: usize) -> Vec<Self::Boolean>;
50}
51
52/// Unary operator for converting to a base field.
53pub trait ToField {
54 type Field: FieldTrait;
55
56 /// Returns a circuit as a base field element.
57 fn to_field(&self) -> Self::Field;
58}
59
60/// Unary operator for converting to a list of base fields.
61pub trait ToFields {
62 type Field: FieldTrait;
63
64 /// Returns the circuit as a list of base field elements.
65 fn to_fields(&self) -> Vec<Self::Field>;
66}
67
68/// Unary operator for converting to an affine group.
69pub trait ToGroup {
70 type Group: GroupTrait<Self::Scalar>;
71 type Scalar: ScalarTrait;
72
73 /// Returns the circuit as a list of affine group elements.
74 fn to_group(&self) -> Self::Group;
75}