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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#![allow(non_snake_case)]
use curve25519_dalek::ristretto::CompressedRistretto;
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::{Identity, IsIdentity};
use errors::R1CSError;
use inner_product_proof::InnerProductProof;
use util;
use serde::de::Visitor;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
const ONE_PHASE_COMMITMENTS: u8 = 0;
const TWO_PHASE_COMMITMENTS: u8 = 1;
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
pub struct R1CSProof {
pub(super) A_I1: CompressedRistretto,
pub(super) A_O1: CompressedRistretto,
pub(super) S1: CompressedRistretto,
pub(super) A_I2: CompressedRistretto,
pub(super) A_O2: CompressedRistretto,
pub(super) S2: CompressedRistretto,
pub(super) T_1: CompressedRistretto,
pub(super) T_3: CompressedRistretto,
pub(super) T_4: CompressedRistretto,
pub(super) T_5: CompressedRistretto,
pub(super) T_6: CompressedRistretto,
pub(super) t_x: Scalar,
pub(super) t_x_blinding: Scalar,
pub(super) e_blinding: Scalar,
pub(super) ipp_proof: InnerProductProof,
}
impl R1CSProof {
pub fn to_bytes(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(self.serialized_size());
if self.missing_phase2_commitments() {
buf.push(ONE_PHASE_COMMITMENTS);
buf.extend_from_slice(self.A_I1.as_bytes());
buf.extend_from_slice(self.A_O1.as_bytes());
buf.extend_from_slice(self.S1.as_bytes());
} else {
buf.push(TWO_PHASE_COMMITMENTS);
buf.extend_from_slice(self.A_I1.as_bytes());
buf.extend_from_slice(self.A_O1.as_bytes());
buf.extend_from_slice(self.S1.as_bytes());
buf.extend_from_slice(self.A_I2.as_bytes());
buf.extend_from_slice(self.A_O2.as_bytes());
buf.extend_from_slice(self.S2.as_bytes());
}
buf.extend_from_slice(self.T_1.as_bytes());
buf.extend_from_slice(self.T_3.as_bytes());
buf.extend_from_slice(self.T_4.as_bytes());
buf.extend_from_slice(self.T_5.as_bytes());
buf.extend_from_slice(self.T_6.as_bytes());
buf.extend_from_slice(self.t_x.as_bytes());
buf.extend_from_slice(self.t_x_blinding.as_bytes());
buf.extend_from_slice(self.e_blinding.as_bytes());
buf.extend(self.ipp_proof.to_bytes_iter());
buf
}
pub fn serialized_size(&self) -> usize {
let elements = if self.missing_phase2_commitments() {
11
} else {
14
};
1 + elements * 32 + self.ipp_proof.serialized_size()
}
fn missing_phase2_commitments(&self) -> bool {
self.A_I2.is_identity() && self.A_O2.is_identity() && self.S2.is_identity()
}
pub fn from_bytes(slice: &[u8]) -> Result<R1CSProof, R1CSError> {
if slice.len() < 1 {
return Err(R1CSError::FormatError);
}
let version = slice[0];
let mut slice = &slice[1..];
if slice.len() % 32 != 0 {
return Err(R1CSError::FormatError);
}
let minlength = match version {
ONE_PHASE_COMMITMENTS => 11 * 32,
TWO_PHASE_COMMITMENTS => 14 * 32,
_ => return Err(R1CSError::FormatError),
};
if slice.len() < minlength {
return Err(R1CSError::FormatError);
}
macro_rules! read32 {
() => {{
let tmp = util::read32(slice);
slice = &slice[32..];
tmp
}};
}
let A_I1 = CompressedRistretto(read32!());
let A_O1 = CompressedRistretto(read32!());
let S1 = CompressedRistretto(read32!());
let (A_I2, A_O2, S2) = if version == ONE_PHASE_COMMITMENTS {
(
CompressedRistretto::identity(),
CompressedRistretto::identity(),
CompressedRistretto::identity(),
)
} else {
(
CompressedRistretto(read32!()),
CompressedRistretto(read32!()),
CompressedRistretto(read32!()),
)
};
let T_1 = CompressedRistretto(read32!());
let T_3 = CompressedRistretto(read32!());
let T_4 = CompressedRistretto(read32!());
let T_5 = CompressedRistretto(read32!());
let T_6 = CompressedRistretto(read32!());
let t_x = Scalar::from_canonical_bytes(read32!()).ok_or(R1CSError::FormatError)?;
let t_x_blinding = Scalar::from_canonical_bytes(read32!()).ok_or(R1CSError::FormatError)?;
let e_blinding = Scalar::from_canonical_bytes(read32!()).ok_or(R1CSError::FormatError)?;
let ipp_proof = InnerProductProof::from_bytes(slice).map_err(|_| R1CSError::FormatError)?;
Ok(R1CSProof {
A_I1,
A_O1,
S1,
A_I2,
A_O2,
S2,
T_1,
T_3,
T_4,
T_5,
T_6,
t_x,
t_x_blinding,
e_blinding,
ipp_proof,
})
}
}
impl Serialize for R1CSProof {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(&self.to_bytes()[..])
}
}
impl<'de> Deserialize<'de> for R1CSProof {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct R1CSProofVisitor;
impl<'de> Visitor<'de> for R1CSProofVisitor {
type Value = R1CSProof;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
formatter.write_str("a valid R1CSProof")
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<R1CSProof, E>
where
E: serde::de::Error,
{
R1CSProof::from_bytes(v).map_err(serde::de::Error::custom)
}
}
deserializer.deserialize_bytes(R1CSProofVisitor)
}
}