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
use rand::{thread_rng, Rng};
use std::iter;
use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint};
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::{IsIdentity, VartimeMultiscalarMul};
use crate::toolbox::{SchnorrCS, TranscriptProtocol};
use crate::{BatchableProof, CompactProof, ProofError, Transcript};
pub struct Verifier<'a> {
transcript: &'a mut Transcript,
num_scalars: usize,
points: Vec<CompressedRistretto>,
point_labels: Vec<&'static [u8]>,
constraints: Vec<(PointVar, Vec<(ScalarVar, PointVar)>)>,
}
#[derive(Copy, Clone)]
pub struct ScalarVar(usize);
#[derive(Copy, Clone)]
pub struct PointVar(usize);
impl<'a> Verifier<'a> {
pub fn new(proof_label: &'static [u8], transcript: &'a mut Transcript) -> Self {
transcript.domain_sep(proof_label);
Verifier {
transcript,
num_scalars: 0,
points: Vec::default(),
point_labels: Vec::default(),
constraints: Vec::default(),
}
}
pub fn allocate_scalar(&mut self, label: &'static [u8]) -> ScalarVar {
self.transcript.append_scalar_var(label);
self.num_scalars += 1;
ScalarVar(self.num_scalars - 1)
}
pub fn allocate_point(
&mut self,
label: &'static [u8],
assignment: CompressedRistretto,
) -> Result<PointVar, ProofError> {
self.transcript
.validate_and_append_point_var(label, &assignment)?;
self.points.push(assignment);
self.point_labels.push(label);
Ok(PointVar(self.points.len() - 1))
}
pub fn verify_compact(self, proof: &CompactProof) -> Result<(), ProofError> {
if proof.responses.len() != self.num_scalars {
return Err(ProofError::VerificationFailure);
}
let points = self
.points
.iter()
.map(|pt| pt.decompress())
.collect::<Option<Vec<RistrettoPoint>>>()
.ok_or(ProofError::VerificationFailure)?;
let minus_c = -proof.challenge;
for (lhs_var, rhs_lc) in &self.constraints {
let commitment = RistrettoPoint::vartime_multiscalar_mul(
rhs_lc
.iter()
.map(|(sc_var, _pt_var)| proof.responses[sc_var.0])
.chain(iter::once(minus_c)),
rhs_lc
.iter()
.map(|(_sc_var, pt_var)| points[pt_var.0])
.chain(iter::once(points[lhs_var.0])),
);
self.transcript
.append_blinding_commitment(self.point_labels[lhs_var.0], &commitment);
}
let challenge = self.transcript.get_challenge(b"chal");
if challenge == proof.challenge {
Ok(())
} else {
Err(ProofError::VerificationFailure)
}
}
pub fn verify_batchable(self, proof: &BatchableProof) -> Result<(), ProofError> {
if proof.responses.len() != self.num_scalars {
return Err(ProofError::VerificationFailure);
}
if proof.commitments.len() != self.constraints.len() {
return Err(ProofError::VerificationFailure);
}
for (i, commitment) in proof.commitments.iter().enumerate() {
let (ref lhs_var, ref _rhs_lc) = self.constraints[i];
self.transcript.validate_and_append_blinding_commitment(
self.point_labels[lhs_var.0],
&commitment,
)?;
}
let minus_c = -self.transcript.get_challenge(b"chal");
let commitments_offset = self.points.len();
let combined_points = self.points.iter().chain(proof.commitments.iter());
let mut coeffs = vec![Scalar::zero(); self.points.len() + proof.commitments.len()];
for i in 0..self.constraints.len() {
let (ref lhs_var, ref rhs_lc) = self.constraints[i];
let random_factor = Scalar::from(thread_rng().gen::<u128>());
coeffs[commitments_offset + i] += -random_factor;
coeffs[lhs_var.0] += random_factor * minus_c;
for (sc_var, pt_var) in rhs_lc {
coeffs[pt_var.0] += random_factor * proof.responses[sc_var.0];
}
}
let check = RistrettoPoint::optional_multiscalar_mul(
&coeffs,
combined_points.map(|pt| pt.decompress()),
)
.ok_or(ProofError::VerificationFailure)?;
if check.is_identity() {
Ok(())
} else {
Err(ProofError::VerificationFailure)
}
}
}
impl<'a> SchnorrCS for Verifier<'a> {
type ScalarVar = ScalarVar;
type PointVar = PointVar;
fn constrain(&mut self, lhs: PointVar, linear_combination: Vec<(ScalarVar, PointVar)>) {
self.constraints.push((lhs, linear_combination));
}
}