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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use crate::poly::Polynomial;
use ff::PrimeField;
use halo2curves::{pairing::Engine, serde::SerdeObject, CurveAffine};
use std::io;
#[derive(Clone, Copy, Debug)]
pub enum SerdeFormat {
Processed,
RawBytes,
RawBytesUnchecked,
}
pub(crate) trait CurveRead: CurveAffine {
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
let mut compressed = Self::Repr::default();
reader.read_exact(compressed.as_mut())?;
Option::from(Self::from_bytes(&compressed))
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Invalid point encoding in proof"))
}
}
impl<C: CurveAffine> CurveRead for C {}
pub trait SerdeCurveAffine: CurveAffine + SerdeObject {
fn read<R: io::Read>(reader: &mut R, format: SerdeFormat) -> Self {
match format {
SerdeFormat::Processed => <Self as CurveRead>::read(reader).unwrap(),
SerdeFormat::RawBytes => <Self as SerdeObject>::read_raw(reader).unwrap(),
SerdeFormat::RawBytesUnchecked => <Self as SerdeObject>::read_raw_unchecked(reader),
}
}
fn write<W: io::Write>(&self, writer: &mut W, format: SerdeFormat) {
match format {
SerdeFormat::Processed => writer.write_all(self.to_bytes().as_ref()).unwrap(),
_ => self.write_raw(writer).unwrap(),
}
}
}
impl<C: CurveAffine + SerdeObject> SerdeCurveAffine for C {}
pub trait SerdePrimeField: PrimeField + SerdeObject {
fn read<R: io::Read>(reader: &mut R, format: SerdeFormat) -> Self {
match format {
SerdeFormat::Processed => {
let mut compressed = Self::Repr::default();
reader.read_exact(compressed.as_mut()).unwrap();
Option::from(Self::from_repr(compressed))
.unwrap_or_else(|| panic!("Invalid prime field point encoding"))
}
SerdeFormat::RawBytes => <Self as SerdeObject>::read_raw(reader).unwrap(),
SerdeFormat::RawBytesUnchecked => <Self as SerdeObject>::read_raw_unchecked(reader),
}
}
fn write<W: io::Write>(&self, writer: &mut W, format: SerdeFormat) {
match format {
SerdeFormat::Processed => writer.write_all(self.to_repr().as_ref()).unwrap(),
_ => self.write_raw(writer).unwrap(),
}
}
}
impl<F: PrimeField + SerdeObject> SerdePrimeField for F {}
pub fn pack(bits: &[bool]) -> u8 {
let mut value = 0u8;
assert!(bits.len() <= 8);
for (bit_index, bit) in bits.iter().enumerate() {
value |= (*bit as u8) << bit_index;
}
value
}
pub fn unpack(byte: u8, bits: &mut [bool]) {
for (bit_index, bit) in bits.iter_mut().enumerate() {
*bit = (byte >> bit_index) & 1 == 1;
}
}
pub(crate) fn read_polynomial_vec<R: io::Read, F: SerdePrimeField, B>(
reader: &mut R,
format: SerdeFormat,
) -> Vec<Polynomial<F, B>> {
let mut len = [0u8; 4];
reader.read_exact(&mut len).unwrap();
let len = u32::from_be_bytes(len);
(0..len)
.map(|_| Polynomial::<F, B>::read(reader, format))
.collect()
}
pub(crate) fn write_polynomial_slice<W: io::Write, F: SerdePrimeField, B>(
slice: &[Polynomial<F, B>],
writer: &mut W,
format: SerdeFormat,
) {
writer
.write_all(&(slice.len() as u32).to_be_bytes())
.unwrap();
for poly in slice.iter() {
poly.write(writer, format);
}
}
pub(crate) fn polynomial_slice_byte_length<F: PrimeField, B>(slice: &[Polynomial<F, B>]) -> usize {
let field_len = F::default().to_repr().as_ref().len();
4 + slice.len() * (4 + field_len * slice.get(0).map(|poly| poly.len()).unwrap_or(0))
}