1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::{ConstraintFieldError, Field, FieldParameters, Fp2, Fp2Parameters, PrimeField, ToConstraintField};
impl<F: PrimeField> ToConstraintField<F> for F {
fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
Ok(vec![*self])
}
}
impl<F: Field> ToConstraintField<F> for [F] {
#[inline]
fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
Ok(self.to_vec())
}
}
impl<F: Field> ToConstraintField<F> for Vec<F> {
#[inline]
fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
Ok(self.to_vec())
}
}
impl<F: Field> ToConstraintField<F> for () {
#[inline]
fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
Ok(Vec::new())
}
}
impl<P: Fp2Parameters> ToConstraintField<P::Fp> for Fp2<P> {
#[inline]
fn to_field_elements(&self) -> Result<Vec<P::Fp>, ConstraintFieldError> {
let mut c0 = self.c0.to_field_elements()?;
let c1 = self.c1.to_field_elements()?;
c0.extend_from_slice(&c1);
Ok(c0)
}
}
impl<F: PrimeField> ToConstraintField<F> for [u8] {
#[inline]
fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
let max_size = <F as PrimeField>::Parameters::CAPACITY / 8;
let max_size = max_size as usize;
let fes = self
.chunks(max_size)
.map(|chunk| {
let mut chunk = chunk.to_vec();
chunk.resize(max_size + 1, 0u8);
F::read(chunk.as_slice())
})
.collect::<Result<Vec<_>, _>>()?;
Ok(fes)
}
}
impl<F: PrimeField> ToConstraintField<F> for [u8; 32] {
#[inline]
fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
self.as_ref().to_field_elements()
}
}
impl<F: Field> ToConstraintField<F> for bool {
fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
if *self { Ok(vec![F::one()]) } else { Ok(vec![F::zero()]) }
}
}