snarkvm_fields/
to_field_vec.rs

1// Copyright 2024 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 crate::{ConstraintFieldError, Field, Fp2, Fp2Parameters, PrimeField, ToConstraintField};
17use snarkvm_utilities::FromBits;
18
19impl<F: Field> ToConstraintField<F> for () {
20    #[inline]
21    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
22        Ok(Vec::new())
23    }
24}
25
26impl<F: Field> ToConstraintField<F> for bool {
27    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
28        if *self { Ok(vec![F::one()]) } else { Ok(vec![F::zero()]) }
29    }
30}
31
32impl<F: PrimeField> ToConstraintField<F> for F {
33    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
34        Ok(vec![*self])
35    }
36}
37
38impl<F: Field> ToConstraintField<F> for [F] {
39    #[inline]
40    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
41        Ok(self.to_vec())
42    }
43}
44
45impl<F: Field> ToConstraintField<F> for Vec<F> {
46    #[inline]
47    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
48        Ok(self.to_vec())
49    }
50}
51
52impl<P: Fp2Parameters> ToConstraintField<P::Fp> for Fp2<P> {
53    #[inline]
54    fn to_field_elements(&self) -> Result<Vec<P::Fp>, ConstraintFieldError> {
55        let mut c0 = self.c0.to_field_elements()?;
56        let c1 = self.c1.to_field_elements()?;
57        c0.extend_from_slice(&c1);
58        Ok(c0)
59    }
60}
61
62impl<F: PrimeField> ToConstraintField<F> for [bool] {
63    #[inline]
64    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
65        self.chunks(F::size_in_data_bits())
66            .map(|chunk| {
67                F::from_bigint(F::BigInteger::from_bits_le(chunk)?)
68                    .ok_or(ConstraintFieldError::Message("Invalid data bits for constraint field"))
69            })
70            .collect::<Result<Vec<F>, _>>()
71    }
72}
73
74impl<F: PrimeField, const NUM_BITS: usize> ToConstraintField<F> for [bool; NUM_BITS] {
75    #[inline]
76    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
77        self.as_ref().to_field_elements()
78    }
79}
80
81impl<F: PrimeField> ToConstraintField<F> for [u8] {
82    #[inline]
83    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
84        // Derive the field size in bytes, floored to be conservative.
85        let floored_field_size_in_bytes = F::size_in_data_bits() / 8;
86        let next_power_of_two = floored_field_size_in_bytes
87            .checked_next_power_of_two()
88            .ok_or(ConstraintFieldError::Message("Field size is too large"))?;
89
90        // Pack the bytes into field elements.
91        Ok(self
92            .chunks(floored_field_size_in_bytes)
93            .map(|chunk| {
94                // Before packing, pad the chunk to the next power of two.
95                let mut chunk_vec = vec![0u8; next_power_of_two];
96                chunk_vec[..chunk.len()].copy_from_slice(chunk);
97                F::read_le(&*chunk_vec)
98            })
99            .collect::<Result<Vec<_>, _>>()?)
100    }
101}
102
103impl<F: PrimeField, const NUM_BYTES: usize> ToConstraintField<F> for [u8; NUM_BYTES] {
104    #[inline]
105    fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
106        self.as_ref().to_field_elements()
107    }
108}