Skip to content

Commit 1711f39

Browse files
committed
test: add evaluation unit test
1 parent 2d373e9 commit 1711f39

File tree

3 files changed

+322
-0
lines changed

3 files changed

+322
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ cfg-if = "1.0.0"
4545
once_cell = "1.18.0"
4646
anyhow = "1.0.72"
4747
rand = "0.8.4"
48+
rand_xorshift = "0.3.0"
4849

4950
[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
5051
pasta-msm = { git="https://github.com/lurk-lab/pasta-msm", branch="dev", version = "0.1.4" }

ThirdPartyNotices.txt

+209
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,212 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
5252
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
5353
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
5454
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55+
56+
------------------------------------------------------------
57+
https://github.com/AztecProtocol/aztec-packages/
58+
59+
Licensed under Apache 2.0
60+
61+
Copyright 2022 Aztec
62+
63+
Apache License
64+
Version 2.0, January 2004
65+
http://www.apache.org/licenses/
66+
67+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
68+
69+
1. Definitions.
70+
71+
"License" shall mean the terms and conditions for use, reproduction,
72+
and distribution as defined by Sections 1 through 9 of this document.
73+
74+
"Licensor" shall mean the copyright owner or entity authorized by
75+
the copyright owner that is granting the License.
76+
77+
"Legal Entity" shall mean the union of the acting entity and all
78+
other entities that control, are controlled by, or are under common
79+
control with that entity. For the purposes of this definition,
80+
"control" means (i) the power, direct or indirect, to cause the
81+
direction or management of such entity, whether by contract or
82+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
83+
outstanding shares, or (iii) beneficial ownership of such entity.
84+
85+
"You" (or "Your") shall mean an individual or Legal Entity
86+
exercising permissions granted by this License.
87+
88+
"Source" form shall mean the preferred form for making modifications,
89+
including but not limited to software source code, documentation
90+
source, and configuration files.
91+
92+
"Object" form shall mean any form resulting from mechanical
93+
transformation or translation of a Source form, including but
94+
not limited to compiled object code, generated documentation,
95+
and conversions to other media types.
96+
97+
"Work" shall mean the work of authorship, whether in Source or
98+
Object form, made available under the License, as indicated by a
99+
copyright notice that is included in or attached to the work
100+
(an example is provided in the Appendix below).
101+
102+
"Derivative Works" shall mean any work, whether in Source or Object
103+
form, that is based on (or derived from) the Work and for which the
104+
editorial revisions, annotations, elaborations, or other modifications
105+
represent, as a whole, an original work of authorship. For the purposes
106+
of this License, Derivative Works shall not include works that remain
107+
separable from, or merely link (or bind by name) to the interfaces of,
108+
the Work and Derivative Works thereof.
109+
110+
"Contribution" shall mean any work of authorship, including
111+
the original version of the Work and any modifications or additions
112+
to that Work or Derivative Works thereof, that is intentionally
113+
submitted to Licensor for inclusion in the Work by the copyright owner
114+
or by an individual or Legal Entity authorized to submit on behalf of
115+
the copyright owner. For the purposes of this definition, "submitted"
116+
means any form of electronic, verbal, or written communication sent
117+
to the Licensor or its representatives, including but not limited to
118+
communication on electronic mailing lists, source code control systems,
119+
and issue tracking systems that are managed by, or on behalf of, the
120+
Licensor for the purpose of discussing and improving the Work, but
121+
excluding communication that is conspicuously marked or otherwise
122+
designated in writing by the copyright owner as "Not a Contribution."
123+
124+
"Contributor" shall mean Licensor and any individual or Legal Entity
125+
on behalf of whom a Contribution has been received by Licensor and
126+
subsequently incorporated within the Work.
127+
128+
2. Grant of Copyright License. Subject to the terms and conditions of
129+
this License, each Contributor hereby grants to You a perpetual,
130+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
131+
copyright license to reproduce, prepare Derivative Works of,
132+
publicly display, publicly perform, sublicense, and distribute the
133+
Work and such Derivative Works in Source or Object form.
134+
135+
3. Grant of Patent License. Subject to the terms and conditions of
136+
this License, each Contributor hereby grants to You a perpetual,
137+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
138+
(except as stated in this section) patent license to make, have made,
139+
use, offer to sell, sell, import, and otherwise transfer the Work,
140+
where such license applies only to those patent claims licensable
141+
by such Contributor that are necessarily infringed by their
142+
Contribution(s) alone or by combination of their Contribution(s)
143+
with the Work to which such Contribution(s) was submitted. If You
144+
institute patent litigation against any entity (including a
145+
cross-claim or counterclaim in a lawsuit) alleging that the Work
146+
or a Contribution incorporated within the Work constitutes direct
147+
or contributory patent infringement, then any patent licenses
148+
granted to You under this License for that Work shall terminate
149+
as of the date such litigation is filed.
150+
151+
4. Redistribution. You may reproduce and distribute copies of the
152+
Work or Derivative Works thereof in any medium, with or without
153+
modifications, and in Source or Object form, provided that You
154+
meet the following conditions:
155+
156+
(a) You must give any other recipients of the Work or
157+
Derivative Works a copy of this License; and
158+
159+
(b) You must cause any modified files to carry prominent notices
160+
stating that You changed the files; and
161+
162+
(c) You must retain, in the Source form of any Derivative Works
163+
that You distribute, all copyright, patent, trademark, and
164+
attribution notices from the Source form of the Work,
165+
excluding those notices that do not pertain to any part of
166+
the Derivative Works; and
167+
168+
(d) If the Work includes a "NOTICE" text file as part of its
169+
distribution, then any Derivative Works that You distribute must
170+
include a readable copy of the attribution notices contained
171+
within such NOTICE file, excluding those notices that do not
172+
pertain to any part of the Derivative Works, in at least one
173+
of the following places: within a NOTICE text file distributed
174+
as part of the Derivative Works; within the Source form or
175+
documentation, if provided along with the Derivative Works; or,
176+
within a display generated by the Derivative Works, if and
177+
wherever such third-party notices normally appear. The contents
178+
of the NOTICE file are for informational purposes only and
179+
do not modify the License. You may add Your own attribution
180+
notices within Derivative Works that You distribute, alongside
181+
or as an addendum to the NOTICE text from the Work, provided
182+
that such additional attribution notices cannot be construed
183+
as modifying the License.
184+
185+
You may add Your own copyright statement to Your modifications and
186+
may provide additional or different license terms and conditions
187+
for use, reproduction, or distribution of Your modifications, or
188+
for any such Derivative Works as a whole, provided Your use,
189+
reproduction, and distribution of the Work otherwise complies with
190+
the conditions stated in this License.
191+
192+
5. Submission of Contributions. Unless You explicitly state otherwise,
193+
any Contribution intentionally submitted for inclusion in the Work
194+
by You to the Licensor shall be under the terms and conditions of
195+
this License, without any additional terms or conditions.
196+
Notwithstanding the above, nothing herein shall supersede or modify
197+
the terms of any separate license agreement you may have executed
198+
with Licensor regarding such Contributions.
199+
200+
6. Trademarks. This License does not grant permission to use the trade
201+
names, trademarks, service marks, or product names of the Licensor,
202+
except as required for reasonable and customary use in describing the
203+
origin of the Work and reproducing the content of the NOTICE file.
204+
205+
7. Disclaimer of Warranty. Unless required by applicable law or
206+
agreed to in writing, Licensor provides the Work (and each
207+
Contributor provides its Contributions) on an "AS IS" BASIS,
208+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
209+
implied, including, without limitation, any warranties or conditions
210+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
211+
PARTICULAR PURPOSE. You are solely responsible for determining the
212+
appropriateness of using or redistributing the Work and assume any
213+
risks associated with Your exercise of permissions under this License.
214+
215+
8. Limitation of Liability. In no event and under no legal theory,
216+
whether in tort (including negligence), contract, or otherwise,
217+
unless required by applicable law (such as deliberate and grossly
218+
negligent acts) or agreed to in writing, shall any Contributor be
219+
liable to You for damages, including any direct, indirect, special,
220+
incidental, or consequential damages of any character arising as a
221+
result of this License or out of the use or inability to use the
222+
Work (including but not limited to damages for loss of goodwill,
223+
work stoppage, computer failure or malfunction, or any and all
224+
other commercial damages or losses), even if such Contributor
225+
has been advised of the possibility of such damages.
226+
227+
9. Accepting Warranty or Additional Liability. While redistributing
228+
the Work or Derivative Works thereof, You may choose to offer,
229+
and charge a fee for, acceptance of support, warranty, indemnity,
230+
or other liability obligations and/or rights consistent with this
231+
License. However, in accepting such obligations, You may act only
232+
on Your own behalf and on Your sole responsibility, not on behalf
233+
of any other Contributor, and only if You agree to indemnify,
234+
defend, and hold each Contributor harmless for any liability
235+
incurred by, or claims asserted against, such Contributor by reason
236+
of your accepting any such warranty or additional liability.
237+
238+
END OF TERMS AND CONDITIONS
239+
240+
APPENDIX: How to apply the Apache License to your work.
241+
242+
To apply the Apache License to your work, attach the following
243+
boilerplate notice, with the fields enclosed by brackets "[]"
244+
replaced with your own identifying information. (Don't include
245+
the brackets!) The text should be enclosed in the appropriate
246+
comment syntax for the file format. We also recommend that a
247+
file or class name and description of purpose be included on the
248+
same "printed page" as the copyright notice for easier
249+
identification within third-party archives.
250+
251+
Copyright [yyyy] [name of copyright owner]
252+
253+
Licensed under the Apache License, Version 2.0 (the "License");
254+
you may not use this file except in compliance with the License.
255+
You may obtain a copy of the License at
256+
257+
http://www.apache.org/licenses/LICENSE-2.0
258+
259+
Unless required by applicable law or agreed to in writing, software
260+
distributed under the License is distributed on an "AS IS" BASIS,
261+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
262+
See the License for the specific language governing permissions and
263+
limitations under the License.

src/spartan/polys/multilinear.rs

+112
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ mod tests {
213213

214214
use super::*;
215215
use pasta_curves::Fp;
216+
use rand_core::SeedableRng;
216217

217218
fn make_mlp<F: PrimeField>(len: usize, value: F) -> MultilinearPolynomial<F> {
218219
MultilinearPolynomial {
@@ -335,4 +336,115 @@ mod tests {
335336
test_evaluation_with::<provider::bn256_grumpkin::bn256::Scalar>();
336337
test_evaluation_with::<provider::secp_secq::secp256k1::Scalar>();
337338
}
339+
340+
pub fn partial_eval<F: PrimeField>(
341+
poly: &MultilinearPolynomial<F>,
342+
point: &[F],
343+
) -> MultilinearPolynomial<F> {
344+
// Get size of partial evaluation point u = (u_0,...,u_{m-1})
345+
let m = point.len();
346+
347+
// Assert that the size of the polynomial being evaluated is a power of 2 greater than (1 << m)
348+
assert!(poly.Z.len().is_power_of_two());
349+
assert!(poly.Z.len() >= 1 << m);
350+
let n = poly.Z.len().trailing_zeros() as usize;
351+
352+
// Partial evaluation is done in m rounds l = 0,...,m-1.
353+
let mut n_l = 1 << (n - 1);
354+
355+
// Temporary buffer of half the size of the polynomial
356+
let mut tmp = vec![F::ZERO; n_l];
357+
358+
let prev = &poly.Z;
359+
360+
// Evaluate variable X_{n-1} at u_{m-1}
361+
let u_l = point[m - 1];
362+
for i in 0..n_l {
363+
tmp[i] = prev[i] + u_l * (prev[i + n_l] - prev[i]);
364+
}
365+
366+
// Evaluate m-1 variables X_{n-l-1}, ..., X_{n-2} at m-1 remaining values u_0,...,u_{m-2})
367+
for l in 1..m {
368+
n_l = 1 << (n - l - 1);
369+
let u_l = point[m - l - 1];
370+
for i in 0..n_l {
371+
tmp[i] = tmp[i] + u_l * (tmp[i + n_l] - tmp[i]);
372+
}
373+
}
374+
tmp.truncate(1 << (poly.num_vars - m));
375+
376+
MultilinearPolynomial::new(tmp)
377+
}
378+
379+
fn make_rand_mlp<F: PrimeField, R: RngCore>(
380+
var_count: usize,
381+
mut rng: &mut R,
382+
) -> MultilinearPolynomial<F> {
383+
let eqpoly = EqPolynomial::new(
384+
std::iter::from_fn(|| Some(F::random(&mut rng)))
385+
.take(var_count)
386+
.collect::<Vec<_>>(),
387+
);
388+
MultilinearPolynomial::new(eqpoly.evals())
389+
}
390+
391+
fn partial_evaluate_mle_with<F: PrimeField>() {
392+
// Initialize a random polynomial
393+
let n = 5;
394+
let mut rng = rand_xorshift::XorShiftRng::from_seed([0u8; 16]);
395+
let poly = make_rand_mlp::<F, _>(n, &mut rng);
396+
397+
// Define a random multivariate evaluation point u = (u_0, u_1, u_2, u_3, u_4)
398+
let u_0 = F::random(&mut rng);
399+
let u_1 = F::random(&mut rng);
400+
let u_2 = F::random(&mut rng);
401+
let u_3 = F::random(&mut rng);
402+
let u_4 = F::random(&mut rng);
403+
let u_challenge = vec![u_4, u_3, u_2, u_1, u_0];
404+
405+
// Directly computing v = p(u_0,...,u_4) and comparing it with the result of
406+
// first computing the partial evaluation in the last 3 variables
407+
// g(X_0,X_1) = p(X_0,X_1,u_2,u_3,u_4), then v = g(u_0,u_1)
408+
409+
// Compute v = p(u_0,...,u_4)
410+
let v_expected = poly.evaluate(&u_challenge[..]);
411+
412+
// Compute g(X_0,X_1) = p(X_0,X_1,u_2,u_3,u_4), then v = g(u_0,u_1)
413+
let u_part_1 = vec![u_1, u_0]; // note the endianness difference
414+
let u_part_2 = vec![u_2, u_3, u_4];
415+
let partial_evaluated_poly = partial_eval(&poly, &u_part_2);
416+
let v_result = partial_evaluated_poly.evaluate(&u_part_1);
417+
418+
assert_eq!(v_result, v_expected);
419+
}
420+
421+
#[test]
422+
fn test_partial_evaluate_mle() {
423+
partial_evaluate_mle_with::<Fp>();
424+
partial_evaluate_mle_with::<bn256::Scalar>();
425+
partial_evaluate_mle_with::<secp256k1::Scalar>();
426+
}
427+
428+
fn partial_and_evaluate_with<F: PrimeField>() {
429+
for _i in 0..50 {
430+
// Initialize a random polynomial
431+
let n = 7;
432+
let mut rng = rand_xorshift::XorShiftRng::from_seed([0u8; 16]);
433+
let poly = make_rand_mlp::<F, _>(n, &mut rng);
434+
435+
// draw a random point
436+
let pt: Vec<_> = std::iter::from_fn(|| Some(F::random(&mut rng)))
437+
.take(n)
438+
.collect();
439+
let rev_pt: Vec<_> = pt.iter().cloned().rev().collect();
440+
assert_eq!(poly.evaluate(&pt), partial_eval(&poly, &rev_pt).Z[0])
441+
}
442+
}
443+
444+
#[test]
445+
fn test_partial_and_evaluate() {
446+
partial_and_evaluate_with::<Fp>();
447+
partial_and_evaluate_with::<bn256::Scalar>();
448+
partial_and_evaluate_with::<secp256k1::Scalar>();
449+
}
338450
}

0 commit comments

Comments
 (0)