From 2bcefa92e66cd8d5e6ce266e1a5383282fa30dbd Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Thu, 30 Jun 2022 17:59:28 -0400 Subject: [PATCH 01/14] feat: add ec benchmark --- manta-benchmark/Cargo.toml | 7 ++ manta-benchmark/benches/ec.rs | 130 +++++++++++++++++++++++++++++ manta-benchmark/src/ec.rs | 148 ++++++++++++++++++++++++++++++++++ manta-benchmark/src/lib.rs | 2 + 4 files changed, 287 insertions(+) create mode 100644 manta-benchmark/benches/ec.rs create mode 100644 manta-benchmark/src/ec.rs diff --git a/manta-benchmark/Cargo.toml b/manta-benchmark/Cargo.toml index 2d57c280c..eba9b1259 100644 --- a/manta-benchmark/Cargo.toml +++ b/manta-benchmark/Cargo.toml @@ -27,6 +27,10 @@ maintenance = { status = "actively-developed" } [lib] crate-type = ["cdylib", "lib"] +[[bench]] +name = "ec" +harness = false + [[bench]] name = "mint" harness = false @@ -40,6 +44,9 @@ name = "reclaim" harness = false [dependencies] +ark-bls12-381 = { version = "0.3.0", default-features = false } +ark-ec = { version = "0.3.0", default-features = false } +ark-ff = { version = "0.3.0", default-features = false } getrandom = { version = "0.2.6", features = ["js"] } instant = { version = "0.1.12", features = [ "wasm-bindgen" ] } manta-accounting = { path = "../manta-accounting", default-features = false, features = ["test"] } diff --git a/manta-benchmark/benches/ec.rs b/manta-benchmark/benches/ec.rs new file mode 100644 index 000000000..08fa7f720 --- /dev/null +++ b/manta-benchmark/benches/ec.rs @@ -0,0 +1,130 @@ +// Copyright 2019-2022 Manta Network. +// This file is part of manta-rs. +// +// manta-rs is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// manta-rs is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with manta-rs. If not, see . + +use ark_bls12_381::{g1::Parameters, G1Affine, G1Projective}; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use manta_benchmark::ec; +use manta_crypto::rand::OsRng; + +fn affine_affine_addition(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut lhs = black_box(ec::sample_affine_point::(&mut rng)); + let rhs = black_box(ec::sample_affine_point(&mut rng)); + group.bench_function("affine-affine addition", |b| { + b.iter(|| { + ec::affine_affine_add_assign(&mut lhs, &rhs); + }) + }); +} + +fn projective_affine_addition(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); + let rhs = black_box(ec::sample_affine_point(&mut rng)); + group.bench_function("projective-affine addition", |b| { + b.iter(|| { + ec::projective_affine_add_assign(&mut lhs, &rhs); + }) + }); +} + +fn projective_projective_addition(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); + let rhs = black_box(ec::sample_projective_point::(&mut rng)); + group.bench_function("projective-projective addition", |b| { + b.iter(|| { + ec::projective_projective_add_assign(&mut lhs, &rhs); + }) + }); +} + +fn affine_scalar_multiplication(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut point = black_box(ec::sample_affine_point::(&mut rng)); + let scalar = black_box(ec::sample_scalar::(&mut rng)); + group.bench_function("affine-scalar multiplication", |b| { + b.iter(|| { + let _ = ec::affine_scalar_mul(&mut point, scalar); + }) + }); +} + +fn projective_scalar_multiplication(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut point = black_box(ec::sample_projective_point::(&mut rng)); + let scalar = black_box(ec::sample_scalar::(&mut rng)); + group.bench_function("projective-scalar multiplication", |b| { + b.iter(|| { + ec::projective_scalar_mul_assign(&mut point, scalar); + }) + }); +} + +fn projective_to_affine_normalization(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut point = black_box(ec::sample_projective_point::(&mut rng)); + group.bench_function("projective to affine normalization", |b| { + b.iter(|| { + let _ = ec::projective_to_affine_normalization(&mut point); + }) + }); +} + +fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let point_vec = (0..(1 << 16)) + .map(|_| ec::sample_projective_point::(&mut rng)) + .collect::>(); + group.bench_function("batch vector of projective to affine normalization", |b| { + b.iter(|| { + let _ = ec::batch_vector_projective_to_affine_normalization(point_vec.as_slice()); + }) + }); +} + +fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let point_vec = (0..(1 << 16)) + .map(|_| ec::sample_projective_point::(&mut rng)) + .collect::>(); + group.bench_function("naive vector of projective to affine normalization", |b| { + b.iter(|| { + let _ = ec::naive_vector_projective_to_affine_normalization(point_vec.as_slice()); + }) + }); +} + +criterion_group!( + ec, + affine_affine_addition, + projective_affine_addition, + projective_projective_addition, + affine_scalar_multiplication, + projective_scalar_multiplication, + projective_to_affine_normalization, + batch_vector_projective_to_affine_normalization, + naive_vector_projective_to_affine_normalization, +); +criterion_main!(ec); diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ec.rs new file mode 100644 index 000000000..542773da0 --- /dev/null +++ b/manta-benchmark/src/ec.rs @@ -0,0 +1,148 @@ +// Copyright 2019-2022 Manta Network. +// This file is part of manta-rs. +// +// manta-rs is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// manta-rs is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with manta-rs. If not, see . + +use ark_ec::{ + short_weierstrass_jacobian::{GroupAffine, GroupProjective}, + AffineCurve, ProjectiveCurve, SWModelParameters, +}; +use ark_ff::UniformRand; +use manta_crypto::rand::RngCore; +use std::ops::{AddAssign, MulAssign}; + +pub fn sample_affine_point(rng: &mut R) -> A +where + A: AffineCurve, + R: RngCore + ?Sized, +{ + A::Projective::rand(rng).into_affine() +} + +pub fn sample_projective_point(rng: &mut R) -> P +where + P: ProjectiveCurve, + R: RngCore + ?Sized, +{ + P::rand(rng) +} + +pub fn sample_scalar(rng: &mut R) -> P::ScalarField +where + P: SWModelParameters, + R: RngCore + ?Sized, +{ + P::ScalarField::rand(rng) +} + +pub fn affine_affine_add_assign

(lhs: &mut GroupAffine

, rhs: &GroupAffine

) +where + P: SWModelParameters, +{ + lhs.add_assign(rhs); +} + +pub fn projective_affine_add_assign

(lhs: &mut GroupProjective

, rhs: &GroupAffine

) +where + P: SWModelParameters, +{ + lhs.add_assign_mixed(rhs); +} + +pub fn projective_projective_add_assign

(lhs: &mut GroupProjective

, rhs: &GroupProjective

) +where + P: SWModelParameters, +{ + lhs.add_assign(rhs); +} + +pub fn affine_scalar_mul

(point: &GroupAffine

, scalar: P::ScalarField) -> GroupProjective

+where + P: SWModelParameters, +{ + point.mul(scalar) +} + +pub fn projective_scalar_mul_assign

(point: &mut GroupProjective

, scalar: P::ScalarField) +where + P: SWModelParameters, +{ + point.mul_assign(scalar); +} + +pub fn projective_to_affine_normalization

(point: &P) -> P::Affine +where + P: ProjectiveCurve, +{ + point.into_affine() +} + +pub fn batch_vector_projective_to_affine_normalization

(point_vec: &[P]) -> Vec +where + P: ProjectiveCurve, +{ + P::batch_normalization_into_affine(point_vec) +} + +pub fn naive_vector_projective_to_affine_normalization

(point_vec: &[P]) -> Vec +where + P: ProjectiveCurve, +{ + point_vec + .into_iter() + .map(|point| point.into_affine()) + .collect::>() +} + +#[cfg(test)] +mod test { + use super::*; + use ark_bls12_381::{g1::Parameters, G1Affine}; + use manta_crypto::rand::OsRng; + + #[test] + fn addition_is_consistent_for_projective_and_affine_curve() { + let mut rng = OsRng; + let mut lhs_affine = sample_affine_point::(&mut rng); + let mut lhs_projective = lhs_affine.clone().into_projective(); + let mut lhs_projective_clone = lhs_projective.clone(); + let rhs_affine = sample_affine_point::(&mut rng); + let rhs_projective = rhs_affine.clone().into_projective(); + affine_affine_add_assign(&mut lhs_affine, &rhs_affine); + projective_affine_add_assign(&mut lhs_projective, &rhs_affine); + projective_projective_add_assign(&mut lhs_projective_clone, &rhs_projective); + assert!( + lhs_affine == lhs_projective, + "add_assign is not equivalent to add_assign_mixed and into_affine" + ); + assert!( + lhs_affine == lhs_projective_clone, + "add_assign is not equivalent to add_assign_mixed and into_affine" + ); + } + + #[test] + fn multiplication_is_consistent_for_projective_and_affine_curve() { + let mut rng = OsRng; + let lhs_affine = sample_affine_point::(&mut rng); + let mut lhs_projective = lhs_affine.clone().into_projective(); + let scalar = sample_scalar::(&mut rng); + let out_projective = affine_scalar_mul(&lhs_affine, scalar); + projective_scalar_mul_assign(&mut lhs_projective, scalar); + assert!( + out_projective == lhs_projective, + "Multiplication is consistent between projective curve and affine curve." + ); + } +} diff --git a/manta-benchmark/src/lib.rs b/manta-benchmark/src/lib.rs index db17f290c..4304168fc 100644 --- a/manta-benchmark/src/lib.rs +++ b/manta-benchmark/src/lib.rs @@ -25,6 +25,8 @@ use manta_pay::{ }; use wasm_bindgen::prelude::wasm_bindgen; +pub mod ec; + #[wasm_bindgen] #[derive(Clone, Debug)] pub struct Context { From 7a2290c89454cb2b7191a66910e1c3c8a6ce7bdb Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Thu, 30 Jun 2022 18:12:38 -0400 Subject: [PATCH 02/14] feat: update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dac99f161..c25fdc2ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] ### Added +- [\#147](https://github.com/Manta-Network/manta-rs/pull/147) Add benchmarks for Arkworks elliptic curve operations. ### Changed From 137f20ef1e023276bf14dd31633c31c7640d7317 Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Thu, 30 Jun 2022 18:27:43 -0400 Subject: [PATCH 03/14] feat: fix an iter ci issue --- manta-benchmark/src/ec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ec.rs index 542773da0..3958fb947 100644 --- a/manta-benchmark/src/ec.rs +++ b/manta-benchmark/src/ec.rs @@ -100,7 +100,7 @@ where P: ProjectiveCurve, { point_vec - .into_iter() + .iter() .map(|point| point.into_affine()) .collect::>() } From e3ce6f176e95b6181bf175c9919b52d1999d2747 Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Thu, 30 Jun 2022 23:22:30 -0400 Subject: [PATCH 04/14] feat: remove clone() --- manta-benchmark/src/ec.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ec.rs index 3958fb947..afdde5126 100644 --- a/manta-benchmark/src/ec.rs +++ b/manta-benchmark/src/ec.rs @@ -115,10 +115,10 @@ mod test { fn addition_is_consistent_for_projective_and_affine_curve() { let mut rng = OsRng; let mut lhs_affine = sample_affine_point::(&mut rng); - let mut lhs_projective = lhs_affine.clone().into_projective(); - let mut lhs_projective_clone = lhs_projective.clone(); + let mut lhs_projective = lhs_affine.into_projective(); + let mut lhs_projective_clone = lhs_projective; let rhs_affine = sample_affine_point::(&mut rng); - let rhs_projective = rhs_affine.clone().into_projective(); + let rhs_projective = rhs_affine.into_projective(); affine_affine_add_assign(&mut lhs_affine, &rhs_affine); projective_affine_add_assign(&mut lhs_projective, &rhs_affine); projective_projective_add_assign(&mut lhs_projective_clone, &rhs_projective); @@ -136,7 +136,7 @@ mod test { fn multiplication_is_consistent_for_projective_and_affine_curve() { let mut rng = OsRng; let lhs_affine = sample_affine_point::(&mut rng); - let mut lhs_projective = lhs_affine.clone().into_projective(); + let mut lhs_projective = lhs_affine.into_projective(); let scalar = sample_scalar::(&mut rng); let out_projective = affine_scalar_mul(&lhs_affine, scalar); projective_scalar_mul_assign(&mut lhs_projective, scalar); From b906b3623e276b2e28a66ee279c79eadf30fa2e4 Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Fri, 1 Jul 2022 10:56:53 -0400 Subject: [PATCH 05/14] feat: nit --- manta-benchmark/benches/ec.rs | 8 ++++---- manta-benchmark/src/ec.rs | 20 ++++++++++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/manta-benchmark/benches/ec.rs b/manta-benchmark/benches/ec.rs index 08fa7f720..4074f64f6 100644 --- a/manta-benchmark/benches/ec.rs +++ b/manta-benchmark/benches/ec.rs @@ -58,11 +58,11 @@ fn projective_projective_addition(c: &mut Criterion) { fn affine_scalar_multiplication(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let mut point = black_box(ec::sample_affine_point::(&mut rng)); + let point = black_box(ec::sample_affine_point::(&mut rng)); let scalar = black_box(ec::sample_scalar::(&mut rng)); group.bench_function("affine-scalar multiplication", |b| { b.iter(|| { - let _ = ec::affine_scalar_mul(&mut point, scalar); + let _ = ec::affine_scalar_mul(&point, scalar); }) }); } @@ -82,10 +82,10 @@ fn projective_scalar_multiplication(c: &mut Criterion) { fn projective_to_affine_normalization(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let mut point = black_box(ec::sample_projective_point::(&mut rng)); + let point = black_box(ec::sample_projective_point::(&mut rng)); group.bench_function("projective to affine normalization", |b| { b.iter(|| { - let _ = ec::projective_to_affine_normalization(&mut point); + let _ = ec::projective_to_affine_normalization(&point); }) }); } diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ec.rs index afdde5126..a92df4cda 100644 --- a/manta-benchmark/src/ec.rs +++ b/manta-benchmark/src/ec.rs @@ -19,9 +19,10 @@ use ark_ec::{ AffineCurve, ProjectiveCurve, SWModelParameters, }; use ark_ff::UniformRand; +use core::ops::{AddAssign, MulAssign}; use manta_crypto::rand::RngCore; -use std::ops::{AddAssign, MulAssign}; +#[inline] pub fn sample_affine_point(rng: &mut R) -> A where A: AffineCurve, @@ -30,6 +31,7 @@ where A::Projective::rand(rng).into_affine() } +#[inline] pub fn sample_projective_point(rng: &mut R) -> P where P: ProjectiveCurve, @@ -38,6 +40,7 @@ where P::rand(rng) } +#[inline] pub fn sample_scalar(rng: &mut R) -> P::ScalarField where P: SWModelParameters, @@ -46,6 +49,7 @@ where P::ScalarField::rand(rng) } +#[inline] pub fn affine_affine_add_assign

(lhs: &mut GroupAffine

, rhs: &GroupAffine

) where P: SWModelParameters, @@ -53,6 +57,7 @@ where lhs.add_assign(rhs); } +#[inline] pub fn projective_affine_add_assign

(lhs: &mut GroupProjective

, rhs: &GroupAffine

) where P: SWModelParameters, @@ -60,6 +65,7 @@ where lhs.add_assign_mixed(rhs); } +#[inline] pub fn projective_projective_add_assign

(lhs: &mut GroupProjective

, rhs: &GroupProjective

) where P: SWModelParameters, @@ -67,6 +73,7 @@ where lhs.add_assign(rhs); } +#[inline] pub fn affine_scalar_mul

(point: &GroupAffine

, scalar: P::ScalarField) -> GroupProjective

where P: SWModelParameters, @@ -74,6 +81,7 @@ where point.mul(scalar) } +#[inline] pub fn projective_scalar_mul_assign

(point: &mut GroupProjective

, scalar: P::ScalarField) where P: SWModelParameters, @@ -81,6 +89,7 @@ where point.mul_assign(scalar); } +#[inline] pub fn projective_to_affine_normalization

(point: &P) -> P::Affine where P: ProjectiveCurve, @@ -88,6 +97,7 @@ where point.into_affine() } +#[inline] pub fn batch_vector_projective_to_affine_normalization

(point_vec: &[P]) -> Vec where P: ProjectiveCurve, @@ -95,14 +105,12 @@ where P::batch_normalization_into_affine(point_vec) } +#[inline] pub fn naive_vector_projective_to_affine_normalization

(point_vec: &[P]) -> Vec where P: ProjectiveCurve, { - point_vec - .iter() - .map(|point| point.into_affine()) - .collect::>() + point_vec.iter().map(P::into_affine).collect() } #[cfg(test)] @@ -142,7 +150,7 @@ mod test { projective_scalar_mul_assign(&mut lhs_projective, scalar); assert!( out_projective == lhs_projective, - "Multiplication is consistent between projective curve and affine curve." + "Multiplication is not consistent between projective curve and affine curve." ); } } From f7526a8fb4549b68b5e54708758e27579bf8cf68 Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Fri, 1 Jul 2022 17:42:56 -0400 Subject: [PATCH 06/14] wip: update traits for Parameters --- manta-benchmark/Cargo.toml | 6 +- manta-benchmark/benches/ec.rs | 240 +++++++++++++++++----------------- manta-benchmark/src/ec.rs | 42 +++--- 3 files changed, 144 insertions(+), 144 deletions(-) diff --git a/manta-benchmark/Cargo.toml b/manta-benchmark/Cargo.toml index eba9b1259..240b40ef7 100644 --- a/manta-benchmark/Cargo.toml +++ b/manta-benchmark/Cargo.toml @@ -27,9 +27,9 @@ maintenance = { status = "actively-developed" } [lib] crate-type = ["cdylib", "lib"] -[[bench]] -name = "ec" -harness = false +# [[bench]] +# name = "ec" +# harness = false [[bench]] name = "mint" diff --git a/manta-benchmark/benches/ec.rs b/manta-benchmark/benches/ec.rs index 4074f64f6..cc65e5b89 100644 --- a/manta-benchmark/benches/ec.rs +++ b/manta-benchmark/benches/ec.rs @@ -1,130 +1,130 @@ -// Copyright 2019-2022 Manta Network. -// This file is part of manta-rs. -// -// manta-rs is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// manta-rs is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with manta-rs. If not, see . +// // Copyright 2019-2022 Manta Network. +// // This file is part of manta-rs. +// // +// // manta-rs is free software: you can redistribute it and/or modify +// // it under the terms of the GNU General Public License as published by +// // the Free Software Foundation, either version 3 of the License, or +// // (at your option) any later version. +// // +// // manta-rs is distributed in the hope that it will be useful, +// // but WITHOUT ANY WARRANTY; without even the implied warranty of +// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// // GNU General Public License for more details. +// // +// // You should have received a copy of the GNU General Public License +// // along with manta-rs. If not, see . -use ark_bls12_381::{g1::Parameters, G1Affine, G1Projective}; -use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use manta_benchmark::ec; -use manta_crypto::rand::OsRng; +// use ark_bls12_381::{g1::Parameters, G1Affine, G1Projective}; +// use criterion::{black_box, criterion_group, criterion_main, Criterion}; +// use manta_benchmark::ec; +// use manta_crypto::rand::OsRng; -fn affine_affine_addition(c: &mut Criterion) { - let mut group = c.benchmark_group("bench"); - let mut rng = OsRng; - let mut lhs = black_box(ec::sample_affine_point::(&mut rng)); - let rhs = black_box(ec::sample_affine_point(&mut rng)); - group.bench_function("affine-affine addition", |b| { - b.iter(|| { - ec::affine_affine_add_assign(&mut lhs, &rhs); - }) - }); -} +// fn affine_affine_addition(c: &mut Criterion) { +// let mut group = c.benchmark_group("bench"); +// let mut rng = OsRng; +// let mut lhs = black_box(ec::sample_affine_point::(&mut rng)); +// let rhs = black_box(ec::sample_affine_point(&mut rng)); +// group.bench_function("affine-affine addition", |b| { +// b.iter(|| { +// ec::affine_affine_add_assign(&mut lhs, &rhs); +// }) +// }); +// } -fn projective_affine_addition(c: &mut Criterion) { - let mut group = c.benchmark_group("bench"); - let mut rng = OsRng; - let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); - let rhs = black_box(ec::sample_affine_point(&mut rng)); - group.bench_function("projective-affine addition", |b| { - b.iter(|| { - ec::projective_affine_add_assign(&mut lhs, &rhs); - }) - }); -} +// fn projective_affine_addition(c: &mut Criterion) { +// let mut group = c.benchmark_group("bench"); +// let mut rng = OsRng; +// let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); +// let rhs = black_box(ec::sample_affine_point(&mut rng)); +// group.bench_function("projective-affine addition", |b| { +// b.iter(|| { +// ec::projective_affine_add_assign(&mut lhs, &rhs); +// }) +// }); +// } -fn projective_projective_addition(c: &mut Criterion) { - let mut group = c.benchmark_group("bench"); - let mut rng = OsRng; - let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); - let rhs = black_box(ec::sample_projective_point::(&mut rng)); - group.bench_function("projective-projective addition", |b| { - b.iter(|| { - ec::projective_projective_add_assign(&mut lhs, &rhs); - }) - }); -} +// fn projective_projective_addition(c: &mut Criterion) { +// let mut group = c.benchmark_group("bench"); +// let mut rng = OsRng; +// let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); +// let rhs = black_box(ec::sample_projective_point::(&mut rng)); +// group.bench_function("projective-projective addition", |b| { +// b.iter(|| { +// ec::projective_projective_add_assign(&mut lhs, &rhs); +// }) +// }); +// } -fn affine_scalar_multiplication(c: &mut Criterion) { - let mut group = c.benchmark_group("bench"); - let mut rng = OsRng; - let point = black_box(ec::sample_affine_point::(&mut rng)); - let scalar = black_box(ec::sample_scalar::(&mut rng)); - group.bench_function("affine-scalar multiplication", |b| { - b.iter(|| { - let _ = ec::affine_scalar_mul(&point, scalar); - }) - }); -} +// fn affine_scalar_multiplication(c: &mut Criterion) { +// let mut group = c.benchmark_group("bench"); +// let mut rng = OsRng; +// let point = black_box(ec::sample_affine_point::(&mut rng)); +// let scalar = black_box(ec::sample_scalar::(&mut rng)); +// group.bench_function("affine-scalar multiplication", |b| { +// b.iter(|| { +// let _ = ec::affine_scalar_mul(&point, scalar); +// }) +// }); +// } -fn projective_scalar_multiplication(c: &mut Criterion) { - let mut group = c.benchmark_group("bench"); - let mut rng = OsRng; - let mut point = black_box(ec::sample_projective_point::(&mut rng)); - let scalar = black_box(ec::sample_scalar::(&mut rng)); - group.bench_function("projective-scalar multiplication", |b| { - b.iter(|| { - ec::projective_scalar_mul_assign(&mut point, scalar); - }) - }); -} +// fn projective_scalar_multiplication(c: &mut Criterion) { +// let mut group = c.benchmark_group("bench"); +// let mut rng = OsRng; +// let mut point = black_box(ec::sample_projective_point::(&mut rng)); +// let scalar = black_box(ec::sample_scalar::(&mut rng)); +// group.bench_function("projective-scalar multiplication", |b| { +// b.iter(|| { +// ec::projective_scalar_mul_assign(&mut point, scalar); +// }) +// }); +// } -fn projective_to_affine_normalization(c: &mut Criterion) { - let mut group = c.benchmark_group("bench"); - let mut rng = OsRng; - let point = black_box(ec::sample_projective_point::(&mut rng)); - group.bench_function("projective to affine normalization", |b| { - b.iter(|| { - let _ = ec::projective_to_affine_normalization(&point); - }) - }); -} +// fn projective_to_affine_normalization(c: &mut Criterion) { +// let mut group = c.benchmark_group("bench"); +// let mut rng = OsRng; +// let point = black_box(ec::sample_projective_point::(&mut rng)); +// group.bench_function("projective to affine normalization", |b| { +// b.iter(|| { +// let _ = ec::projective_to_affine_normalization(&point); +// }) +// }); +// } -fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { - let mut group = c.benchmark_group("bench"); - let mut rng = OsRng; - let point_vec = (0..(1 << 16)) - .map(|_| ec::sample_projective_point::(&mut rng)) - .collect::>(); - group.bench_function("batch vector of projective to affine normalization", |b| { - b.iter(|| { - let _ = ec::batch_vector_projective_to_affine_normalization(point_vec.as_slice()); - }) - }); -} +// fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { +// let mut group = c.benchmark_group("bench"); +// let mut rng = OsRng; +// let point_vec = (0..(1 << 16)) +// .map(|_| ec::sample_projective_point::(&mut rng)) +// .collect::>(); +// group.bench_function("batch vector of projective to affine normalization", |b| { +// b.iter(|| { +// let _ = ec::batch_vector_projective_to_affine_normalization(point_vec.as_slice()); +// }) +// }); +// } -fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) { - let mut group = c.benchmark_group("bench"); - let mut rng = OsRng; - let point_vec = (0..(1 << 16)) - .map(|_| ec::sample_projective_point::(&mut rng)) - .collect::>(); - group.bench_function("naive vector of projective to affine normalization", |b| { - b.iter(|| { - let _ = ec::naive_vector_projective_to_affine_normalization(point_vec.as_slice()); - }) - }); -} +// fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) { +// let mut group = c.benchmark_group("bench"); +// let mut rng = OsRng; +// let point_vec = (0..(1 << 16)) +// .map(|_| ec::sample_projective_point::(&mut rng)) +// .collect::>(); +// group.bench_function("naive vector of projective to affine normalization", |b| { +// b.iter(|| { +// let _ = ec::naive_vector_projective_to_affine_normalization(point_vec.as_slice()); +// }) +// }); +// } -criterion_group!( - ec, - affine_affine_addition, - projective_affine_addition, - projective_projective_addition, - affine_scalar_multiplication, - projective_scalar_multiplication, - projective_to_affine_normalization, - batch_vector_projective_to_affine_normalization, - naive_vector_projective_to_affine_normalization, -); -criterion_main!(ec); +// criterion_group!( +// ec, +// affine_affine_addition, +// projective_affine_addition, +// projective_projective_addition, +// affine_scalar_multiplication, +// projective_scalar_multiplication, +// projective_to_affine_normalization, +// batch_vector_projective_to_affine_normalization, +// naive_vector_projective_to_affine_normalization, +// ); +// criterion_main!(ec); diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ec.rs index a92df4cda..5ff67d023 100644 --- a/manta-benchmark/src/ec.rs +++ b/manta-benchmark/src/ec.rs @@ -50,33 +50,33 @@ where } #[inline] -pub fn affine_affine_add_assign

(lhs: &mut GroupAffine

, rhs: &GroupAffine

) +pub fn affine_affine_add_assign<'a, A>(lhs: &mut A, rhs: &'a A) where - P: SWModelParameters, + A: AffineCurve + AddAssign<&'a A>, { lhs.add_assign(rhs); } #[inline] -pub fn projective_affine_add_assign

(lhs: &mut GroupProjective

, rhs: &GroupAffine

) +pub fn projective_affine_add_assign

(lhs: &mut P, rhs: &P::Affine) where - P: SWModelParameters, + P: ProjectiveCurve, { lhs.add_assign_mixed(rhs); } #[inline] -pub fn projective_projective_add_assign

(lhs: &mut GroupProjective

, rhs: &GroupProjective

) +pub fn projective_projective_add_assign<'a, P>(lhs: &mut P, rhs: &'a P) where - P: SWModelParameters, + P: ProjectiveCurve, // + AddAssign<&'a P>, // TODO { lhs.add_assign(rhs); } #[inline] -pub fn affine_scalar_mul

(point: &GroupAffine

, scalar: P::ScalarField) -> GroupProjective

+pub fn affine_scalar_mul(point: &A, scalar: A::ScalarField) -> A::Projective where - P: SWModelParameters, + A: AffineCurve, { point.mul(scalar) } @@ -140,17 +140,17 @@ mod test { ); } - #[test] - fn multiplication_is_consistent_for_projective_and_affine_curve() { - let mut rng = OsRng; - let lhs_affine = sample_affine_point::(&mut rng); - let mut lhs_projective = lhs_affine.into_projective(); - let scalar = sample_scalar::(&mut rng); - let out_projective = affine_scalar_mul(&lhs_affine, scalar); - projective_scalar_mul_assign(&mut lhs_projective, scalar); - assert!( - out_projective == lhs_projective, - "Multiplication is not consistent between projective curve and affine curve." - ); - } + // #[test] + // fn multiplication_is_consistent_for_projective_and_affine_curve() { + // let mut rng = OsRng; + // let lhs_affine = sample_affine_point::(&mut rng); + // let mut lhs_projective = lhs_affine.into_projective(); + // let scalar = sample_scalar::(&mut rng); + // let out_projective = affine_scalar_mul(&lhs_affine, scalar); + // projective_scalar_mul_assign(&mut lhs_projective, scalar); + // assert!( + // out_projective == lhs_projective, + // "Multiplication is not consistent between projective curve and affine curve." + // ); + // } } From 9304bf16e3844d9b54f78f87ae5ed3c2ecd8fd32 Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Fri, 1 Jul 2022 23:44:23 -0400 Subject: [PATCH 07/14] feat: replace SWParameters with more generic traits --- manta-benchmark/Cargo.toml | 6 +- manta-benchmark/benches/ec.rs | 240 +++++++++++++++++----------------- manta-benchmark/src/ec.rs | 56 ++++---- 3 files changed, 149 insertions(+), 153 deletions(-) diff --git a/manta-benchmark/Cargo.toml b/manta-benchmark/Cargo.toml index 240b40ef7..eba9b1259 100644 --- a/manta-benchmark/Cargo.toml +++ b/manta-benchmark/Cargo.toml @@ -27,9 +27,9 @@ maintenance = { status = "actively-developed" } [lib] crate-type = ["cdylib", "lib"] -# [[bench]] -# name = "ec" -# harness = false +[[bench]] +name = "ec" +harness = false [[bench]] name = "mint" diff --git a/manta-benchmark/benches/ec.rs b/manta-benchmark/benches/ec.rs index cc65e5b89..9418355b4 100644 --- a/manta-benchmark/benches/ec.rs +++ b/manta-benchmark/benches/ec.rs @@ -1,130 +1,130 @@ -// // Copyright 2019-2022 Manta Network. -// // This file is part of manta-rs. -// // -// // manta-rs is free software: you can redistribute it and/or modify -// // it under the terms of the GNU General Public License as published by -// // the Free Software Foundation, either version 3 of the License, or -// // (at your option) any later version. -// // -// // manta-rs is distributed in the hope that it will be useful, -// // but WITHOUT ANY WARRANTY; without even the implied warranty of -// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// // GNU General Public License for more details. -// // -// // You should have received a copy of the GNU General Public License -// // along with manta-rs. If not, see . +// Copyright 2019-2022 Manta Network. +// This file is part of manta-rs. +// +// manta-rs is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// manta-rs is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with manta-rs. If not, see . -// use ark_bls12_381::{g1::Parameters, G1Affine, G1Projective}; -// use criterion::{black_box, criterion_group, criterion_main, Criterion}; -// use manta_benchmark::ec; -// use manta_crypto::rand::OsRng; +use ark_bls12_381::{G1Affine, G1Projective}; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use manta_benchmark::ec; +use manta_crypto::rand::OsRng; -// fn affine_affine_addition(c: &mut Criterion) { -// let mut group = c.benchmark_group("bench"); -// let mut rng = OsRng; -// let mut lhs = black_box(ec::sample_affine_point::(&mut rng)); -// let rhs = black_box(ec::sample_affine_point(&mut rng)); -// group.bench_function("affine-affine addition", |b| { -// b.iter(|| { -// ec::affine_affine_add_assign(&mut lhs, &rhs); -// }) -// }); -// } +fn affine_affine_addition(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut lhs = black_box(ec::sample_affine_point::(&mut rng)); + let rhs = black_box(ec::sample_affine_point(&mut rng)); + group.bench_function("affine-affine addition", |b| { + b.iter(|| { + ec::affine_affine_add_assign(&mut lhs, &rhs); + }) + }); +} -// fn projective_affine_addition(c: &mut Criterion) { -// let mut group = c.benchmark_group("bench"); -// let mut rng = OsRng; -// let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); -// let rhs = black_box(ec::sample_affine_point(&mut rng)); -// group.bench_function("projective-affine addition", |b| { -// b.iter(|| { -// ec::projective_affine_add_assign(&mut lhs, &rhs); -// }) -// }); -// } +fn projective_affine_addition(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); + let rhs = black_box(ec::sample_affine_point(&mut rng)); + group.bench_function("projective-affine addition", |b| { + b.iter(|| { + ec::projective_affine_add_assign(&mut lhs, &rhs); + }) + }); +} -// fn projective_projective_addition(c: &mut Criterion) { -// let mut group = c.benchmark_group("bench"); -// let mut rng = OsRng; -// let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); -// let rhs = black_box(ec::sample_projective_point::(&mut rng)); -// group.bench_function("projective-projective addition", |b| { -// b.iter(|| { -// ec::projective_projective_add_assign(&mut lhs, &rhs); -// }) -// }); -// } +fn projective_projective_addition(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); + let rhs = black_box(ec::sample_projective_point::(&mut rng)); + group.bench_function("projective-projective addition", |b| { + b.iter(|| { + ec::projective_projective_add_assign(&mut lhs, rhs); + }) + }); +} -// fn affine_scalar_multiplication(c: &mut Criterion) { -// let mut group = c.benchmark_group("bench"); -// let mut rng = OsRng; -// let point = black_box(ec::sample_affine_point::(&mut rng)); -// let scalar = black_box(ec::sample_scalar::(&mut rng)); -// group.bench_function("affine-scalar multiplication", |b| { -// b.iter(|| { -// let _ = ec::affine_scalar_mul(&point, scalar); -// }) -// }); -// } +fn affine_scalar_multiplication(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let point = black_box(ec::sample_affine_point::(&mut rng)); + let scalar = black_box(ec::sample_scalar::(&mut rng)); + group.bench_function("affine-scalar multiplication", |b| { + b.iter(|| { + let _ = ec::affine_scalar_mul(&point, scalar); + }) + }); +} -// fn projective_scalar_multiplication(c: &mut Criterion) { -// let mut group = c.benchmark_group("bench"); -// let mut rng = OsRng; -// let mut point = black_box(ec::sample_projective_point::(&mut rng)); -// let scalar = black_box(ec::sample_scalar::(&mut rng)); -// group.bench_function("projective-scalar multiplication", |b| { -// b.iter(|| { -// ec::projective_scalar_mul_assign(&mut point, scalar); -// }) -// }); -// } +fn projective_scalar_multiplication(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let mut point = black_box(ec::sample_projective_point::(&mut rng)); + let scalar = black_box(ec::sample_scalar::(&mut rng)); + group.bench_function("projective-scalar multiplication", |b| { + b.iter(|| { + ec::projective_scalar_mul_assign(&mut point, scalar); + }) + }); +} -// fn projective_to_affine_normalization(c: &mut Criterion) { -// let mut group = c.benchmark_group("bench"); -// let mut rng = OsRng; -// let point = black_box(ec::sample_projective_point::(&mut rng)); -// group.bench_function("projective to affine normalization", |b| { -// b.iter(|| { -// let _ = ec::projective_to_affine_normalization(&point); -// }) -// }); -// } +fn projective_to_affine_normalization(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let point = black_box(ec::sample_projective_point::(&mut rng)); + group.bench_function("projective to affine normalization", |b| { + b.iter(|| { + let _ = ec::projective_to_affine_normalization(&point); + }) + }); +} -// fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { -// let mut group = c.benchmark_group("bench"); -// let mut rng = OsRng; -// let point_vec = (0..(1 << 16)) -// .map(|_| ec::sample_projective_point::(&mut rng)) -// .collect::>(); -// group.bench_function("batch vector of projective to affine normalization", |b| { -// b.iter(|| { -// let _ = ec::batch_vector_projective_to_affine_normalization(point_vec.as_slice()); -// }) -// }); -// } +fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let point_vec = (0..(1 << 16)) + .map(|_| ec::sample_projective_point::(&mut rng)) + .collect::>(); + group.bench_function("batch vector of projective to affine normalization", |b| { + b.iter(|| { + let _ = ec::batch_vector_projective_to_affine_normalization(point_vec.as_slice()); + }) + }); +} -// fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) { -// let mut group = c.benchmark_group("bench"); -// let mut rng = OsRng; -// let point_vec = (0..(1 << 16)) -// .map(|_| ec::sample_projective_point::(&mut rng)) -// .collect::>(); -// group.bench_function("naive vector of projective to affine normalization", |b| { -// b.iter(|| { -// let _ = ec::naive_vector_projective_to_affine_normalization(point_vec.as_slice()); -// }) -// }); -// } +fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) { + let mut group = c.benchmark_group("bench"); + let mut rng = OsRng; + let point_vec = (0..(1 << 16)) + .map(|_| ec::sample_projective_point::(&mut rng)) + .collect::>(); + group.bench_function("naive vector of projective to affine normalization", |b| { + b.iter(|| { + let _ = ec::naive_vector_projective_to_affine_normalization(point_vec.as_slice()); + }) + }); +} -// criterion_group!( -// ec, -// affine_affine_addition, -// projective_affine_addition, -// projective_projective_addition, -// affine_scalar_multiplication, -// projective_scalar_multiplication, -// projective_to_affine_normalization, -// batch_vector_projective_to_affine_normalization, -// naive_vector_projective_to_affine_normalization, -// ); -// criterion_main!(ec); +criterion_group!( + ec, + affine_affine_addition, + projective_affine_addition, + projective_projective_addition, + affine_scalar_multiplication, + projective_scalar_multiplication, + projective_to_affine_normalization, + batch_vector_projective_to_affine_normalization, + naive_vector_projective_to_affine_normalization, +); +criterion_main!(ec); diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ec.rs index 5ff67d023..8bdd95577 100644 --- a/manta-benchmark/src/ec.rs +++ b/manta-benchmark/src/ec.rs @@ -14,12 +14,9 @@ // You should have received a copy of the GNU General Public License // along with manta-rs. If not, see . -use ark_ec::{ - short_weierstrass_jacobian::{GroupAffine, GroupProjective}, - AffineCurve, ProjectiveCurve, SWModelParameters, -}; +use ark_ec::{AffineCurve, ProjectiveCurve}; use ark_ff::UniformRand; -use core::ops::{AddAssign, MulAssign}; +use core::ops::AddAssign; use manta_crypto::rand::RngCore; #[inline] @@ -41,12 +38,12 @@ where } #[inline] -pub fn sample_scalar(rng: &mut R) -> P::ScalarField +pub fn sample_scalar(rng: &mut R) -> A::ScalarField where - P: SWModelParameters, + A: AffineCurve, R: RngCore + ?Sized, { - P::ScalarField::rand(rng) + A::ScalarField::rand(rng) } #[inline] @@ -66,9 +63,9 @@ where } #[inline] -pub fn projective_projective_add_assign<'a, P>(lhs: &mut P, rhs: &'a P) +pub fn projective_projective_add_assign<'a, P>(lhs: &mut P, rhs: P) where - P: ProjectiveCurve, // + AddAssign<&'a P>, // TODO + P: ProjectiveCurve, { lhs.add_assign(rhs); } @@ -82,9 +79,9 @@ where } #[inline] -pub fn projective_scalar_mul_assign

(point: &mut GroupProjective

, scalar: P::ScalarField) +pub fn projective_scalar_mul_assign

(point: &mut P, scalar: P::ScalarField) where - P: SWModelParameters, + P: ProjectiveCurve, { point.mul_assign(scalar); } @@ -116,7 +113,7 @@ where #[cfg(test)] mod test { use super::*; - use ark_bls12_381::{g1::Parameters, G1Affine}; + use ark_bls12_381::G1Affine; use manta_crypto::rand::OsRng; #[test] @@ -126,31 +123,30 @@ mod test { let mut lhs_projective = lhs_affine.into_projective(); let mut lhs_projective_clone = lhs_projective; let rhs_affine = sample_affine_point::(&mut rng); - let rhs_projective = rhs_affine.into_projective(); affine_affine_add_assign(&mut lhs_affine, &rhs_affine); projective_affine_add_assign(&mut lhs_projective, &rhs_affine); - projective_projective_add_assign(&mut lhs_projective_clone, &rhs_projective); + projective_projective_add_assign(&mut lhs_projective_clone, rhs_affine.into_projective()); assert!( lhs_affine == lhs_projective, - "add_assign is not equivalent to add_assign_mixed and into_affine" + "Addition is not consistent for affine curve and projective curve." ); assert!( lhs_affine == lhs_projective_clone, - "add_assign is not equivalent to add_assign_mixed and into_affine" + "Addition is not consistent for affine curve and projective curve." ); } - // #[test] - // fn multiplication_is_consistent_for_projective_and_affine_curve() { - // let mut rng = OsRng; - // let lhs_affine = sample_affine_point::(&mut rng); - // let mut lhs_projective = lhs_affine.into_projective(); - // let scalar = sample_scalar::(&mut rng); - // let out_projective = affine_scalar_mul(&lhs_affine, scalar); - // projective_scalar_mul_assign(&mut lhs_projective, scalar); - // assert!( - // out_projective == lhs_projective, - // "Multiplication is not consistent between projective curve and affine curve." - // ); - // } + #[test] + fn multiplication_is_consistent_for_projective_and_affine_curve() { + let mut rng = OsRng; + let lhs_affine = sample_affine_point::(&mut rng); + let mut lhs_projective = lhs_affine.into_projective(); + let scalar = sample_scalar::(&mut rng); + let out_projective = affine_scalar_mul(&lhs_affine, scalar); + projective_scalar_mul_assign(&mut lhs_projective, scalar); + assert!( + out_projective == lhs_projective, + "Multiplication is not consistent between projective curve and affine curve." + ); + } } From a4316584ab422fb06d4bb4500cacd16da451eb5b Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Sat, 2 Jul 2022 00:00:44 -0400 Subject: [PATCH 08/14] feat: update assert => assert_eq --- manta-benchmark/src/ec.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ec.rs index 8bdd95577..d40457364 100644 --- a/manta-benchmark/src/ec.rs +++ b/manta-benchmark/src/ec.rs @@ -126,12 +126,12 @@ mod test { affine_affine_add_assign(&mut lhs_affine, &rhs_affine); projective_affine_add_assign(&mut lhs_projective, &rhs_affine); projective_projective_add_assign(&mut lhs_projective_clone, rhs_affine.into_projective()); - assert!( - lhs_affine == lhs_projective, + assert_eq!( + lhs_affine, lhs_projective, "Addition is not consistent for affine curve and projective curve." ); - assert!( - lhs_affine == lhs_projective_clone, + assert_eq!( + lhs_affine, lhs_projective_clone, "Addition is not consistent for affine curve and projective curve." ); } @@ -144,8 +144,8 @@ mod test { let scalar = sample_scalar::(&mut rng); let out_projective = affine_scalar_mul(&lhs_affine, scalar); projective_scalar_mul_assign(&mut lhs_projective, scalar); - assert!( - out_projective == lhs_projective, + assert_eq!( + out_projective, lhs_projective, "Multiplication is not consistent between projective curve and affine curve." ); } From a696fd754ee8150679367cffc5c4e379410c1611 Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Wed, 6 Jul 2022 15:50:56 -0400 Subject: [PATCH 09/14] feat: add black_box --- manta-benchmark/benches/ec.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/manta-benchmark/benches/ec.rs b/manta-benchmark/benches/ec.rs index 9418355b4..a28d0075e 100644 --- a/manta-benchmark/benches/ec.rs +++ b/manta-benchmark/benches/ec.rs @@ -62,7 +62,7 @@ fn affine_scalar_multiplication(c: &mut Criterion) { let scalar = black_box(ec::sample_scalar::(&mut rng)); group.bench_function("affine-scalar multiplication", |b| { b.iter(|| { - let _ = ec::affine_scalar_mul(&point, scalar); + let _ = black_box(ec::affine_scalar_mul(&point, scalar)); }) }); } @@ -85,7 +85,7 @@ fn projective_to_affine_normalization(c: &mut Criterion) { let point = black_box(ec::sample_projective_point::(&mut rng)); group.bench_function("projective to affine normalization", |b| { b.iter(|| { - let _ = ec::projective_to_affine_normalization(&point); + let _ = black_box(ec::projective_to_affine_normalization(black_box(&point))); }) }); } @@ -98,7 +98,7 @@ fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { .collect::>(); group.bench_function("batch vector of projective to affine normalization", |b| { b.iter(|| { - let _ = ec::batch_vector_projective_to_affine_normalization(point_vec.as_slice()); + let _ = black_box(ec::batch_vector_projective_to_affine_normalization(black_box(point_vec.as_slice()))); }) }); } @@ -111,7 +111,7 @@ fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) { .collect::>(); group.bench_function("naive vector of projective to affine normalization", |b| { b.iter(|| { - let _ = ec::naive_vector_projective_to_affine_normalization(point_vec.as_slice()); + let _ = black_box(ec::naive_vector_projective_to_affine_normalization(black_box(point_vec.as_slice()))); }) }); } From c8fa31c7603e70d255dc1b7db78e90fdb8fdf31c Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Wed, 6 Jul 2022 15:53:32 -0400 Subject: [PATCH 10/14] chore: fix a lint issue --- manta-benchmark/src/ec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ec.rs index d40457364..d42cea4e5 100644 --- a/manta-benchmark/src/ec.rs +++ b/manta-benchmark/src/ec.rs @@ -63,7 +63,7 @@ where } #[inline] -pub fn projective_projective_add_assign<'a, P>(lhs: &mut P, rhs: P) +pub fn projective_projective_add_assign

(lhs: &mut P, rhs: P) where P: ProjectiveCurve, { From e9aec35de44249df5547a938e3fb546853dfa3ab Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Wed, 6 Jul 2022 15:58:06 -0400 Subject: [PATCH 11/14] chore: fix a lint issue --- manta-benchmark/benches/ec.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/manta-benchmark/benches/ec.rs b/manta-benchmark/benches/ec.rs index a28d0075e..ea85a6229 100644 --- a/manta-benchmark/benches/ec.rs +++ b/manta-benchmark/benches/ec.rs @@ -98,7 +98,9 @@ fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { .collect::>(); group.bench_function("batch vector of projective to affine normalization", |b| { b.iter(|| { - let _ = black_box(ec::batch_vector_projective_to_affine_normalization(black_box(point_vec.as_slice()))); + let _ = black_box(ec::batch_vector_projective_to_affine_normalization( + black_box(point_vec.as_slice()), + )); }) }); } @@ -111,7 +113,9 @@ fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) { .collect::>(); group.bench_function("naive vector of projective to affine normalization", |b| { b.iter(|| { - let _ = black_box(ec::naive_vector_projective_to_affine_normalization(black_box(point_vec.as_slice()))); + let _ = black_box(ec::naive_vector_projective_to_affine_normalization( + black_box(point_vec.as_slice()), + )); }) }); } From 112dbdff1a289c201d0567bcb1e4627564651d02 Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Wed, 6 Jul 2022 17:06:55 -0400 Subject: [PATCH 12/14] fix: resolve inconsistencies in benchmarking suite - Rename `ec` to `ecc` - Add missing documentation lints - Use consistent `black_box` and let-binding drop rules for all `ecc` benchmarks - Fix `Cargo.toml` issues Signed-off-by: Brandon H. Gomes --- Cargo.toml | 10 +--- manta-accounting/Cargo.toml | 2 +- manta-benchmark/Cargo.toml | 8 +-- manta-benchmark/benches/{ec.rs => ecc.rs} | 61 +++++++++++---------- manta-benchmark/benches/mint.rs | 2 + manta-benchmark/benches/private_transfer.rs | 2 + manta-benchmark/benches/reclaim.rs | 2 + manta-benchmark/src/{ec.rs => ecc.rs} | 4 ++ manta-benchmark/src/lib.rs | 10 +++- manta-crypto/Cargo.toml | 2 +- manta-pay/Cargo.toml | 2 +- manta-util/Cargo.toml | 2 +- 12 files changed, 60 insertions(+), 47 deletions(-) rename manta-benchmark/benches/{ec.rs => ecc.rs} (61%) rename manta-benchmark/src/{ec.rs => ecc.rs} (98%) diff --git a/Cargo.toml b/Cargo.toml index 845348c88..87511e2a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,3 @@ [workspace] resolver = "2" -members = [ - "manta-accounting", - "manta-benchmark", - "manta-crypto", - "manta-parameters", - "manta-pay", - "manta-util", - "manta-workspace-hack", -] +members = ["manta-*"] diff --git a/manta-accounting/Cargo.toml b/manta-accounting/Cargo.toml index aa5105198..67b00210d 100644 --- a/manta-accounting/Cargo.toml +++ b/manta-accounting/Cargo.toml @@ -61,10 +61,10 @@ futures = { version = "0.3.21", optional = true, default-features = false, featu indexmap = { version = "1.8.0", optional = true, default-features = false } manta-crypto = { path = "../manta-crypto", default-features = false } manta-util = { path = "../manta-util", default-features = false, features = ["alloc"] } +manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } parking_lot = { version = "0.12.0", optional = true, default-features = false } rand_chacha = { version = "0.3.1", optional = true, default-features = false } statrs = { version = "0.15.0", optional = true, default-features = false } -manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } [dev-dependencies] manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom"] } diff --git a/manta-benchmark/Cargo.toml b/manta-benchmark/Cargo.toml index 9124444e7..40a970211 100644 --- a/manta-benchmark/Cargo.toml +++ b/manta-benchmark/Cargo.toml @@ -28,7 +28,7 @@ maintenance = { status = "actively-developed" } crate-type = ["cdylib", "lib"] [[bench]] -name = "ec" +name = "ecc" harness = false [[bench]] @@ -47,15 +47,15 @@ harness = false ark-bls12-381 = { version = "0.3.0", default-features = false } ark-ec = { version = "0.3.0", default-features = false } ark-ff = { version = "0.3.0", default-features = false } -getrandom = { version = "0.2.6", features = ["js"] } -instant = { version = "0.1.12", features = [ "wasm-bindgen" ] } +getrandom = { version = "0.2.6", default-features = false, features = ["js"] } +instant = { version = "0.1.12", default-features = false, features = [ "wasm-bindgen" ] } manta-accounting = { path = "../manta-accounting", default-features = false, features = ["test"] } manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom", "test"] } manta-pay = { path = "../manta-pay", default-features = false, features = ["groth16", "test"] } +manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } wasm-bindgen = { version = "0.2.81", default-features = false } wasm-bindgen-test = { version = "0.3.30", default-features = false } web-sys = { version = "0.3.58", default-features = false, features = ["console"] } -manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } [dev-dependencies] criterion = { version = "0.3.4", default-features = false } diff --git a/manta-benchmark/benches/ec.rs b/manta-benchmark/benches/ecc.rs similarity index 61% rename from manta-benchmark/benches/ec.rs rename to manta-benchmark/benches/ecc.rs index ea85a6229..ae309e145 100644 --- a/manta-benchmark/benches/ec.rs +++ b/manta-benchmark/benches/ecc.rs @@ -14,19 +14,22 @@ // You should have received a copy of the GNU General Public License // along with manta-rs. If not, see . +//! Elliptic Curve Cryptography Benchmarks + use ark_bls12_381::{G1Affine, G1Projective}; +use core::iter::repeat_with; use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use manta_benchmark::ec; +use manta_benchmark::ecc; use manta_crypto::rand::OsRng; fn affine_affine_addition(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let mut lhs = black_box(ec::sample_affine_point::(&mut rng)); - let rhs = black_box(ec::sample_affine_point(&mut rng)); + let mut lhs = black_box(ecc::sample_affine_point::(&mut rng)); + let rhs = black_box(ecc::sample_affine_point(&mut rng)); group.bench_function("affine-affine addition", |b| { b.iter(|| { - ec::affine_affine_add_assign(&mut lhs, &rhs); + let _ = black_box(ecc::affine_affine_add_assign(&mut lhs, &rhs)); }) }); } @@ -34,11 +37,11 @@ fn affine_affine_addition(c: &mut Criterion) { fn projective_affine_addition(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); - let rhs = black_box(ec::sample_affine_point(&mut rng)); + let mut lhs = black_box(ecc::sample_projective_point::(&mut rng)); + let rhs = black_box(ecc::sample_affine_point(&mut rng)); group.bench_function("projective-affine addition", |b| { b.iter(|| { - ec::projective_affine_add_assign(&mut lhs, &rhs); + let _ = black_box(ecc::projective_affine_add_assign(&mut lhs, &rhs)); }) }); } @@ -46,11 +49,11 @@ fn projective_affine_addition(c: &mut Criterion) { fn projective_projective_addition(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let mut lhs = black_box(ec::sample_projective_point::(&mut rng)); - let rhs = black_box(ec::sample_projective_point::(&mut rng)); + let mut lhs = black_box(ecc::sample_projective_point::(&mut rng)); + let rhs = black_box(ecc::sample_projective_point::(&mut rng)); group.bench_function("projective-projective addition", |b| { b.iter(|| { - ec::projective_projective_add_assign(&mut lhs, rhs); + let _ = black_box(ecc::projective_projective_add_assign(&mut lhs, rhs)); }) }); } @@ -58,11 +61,11 @@ fn projective_projective_addition(c: &mut Criterion) { fn affine_scalar_multiplication(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let point = black_box(ec::sample_affine_point::(&mut rng)); - let scalar = black_box(ec::sample_scalar::(&mut rng)); + let point = black_box(ecc::sample_affine_point::(&mut rng)); + let scalar = black_box(ecc::sample_scalar::(&mut rng)); group.bench_function("affine-scalar multiplication", |b| { b.iter(|| { - let _ = black_box(ec::affine_scalar_mul(&point, scalar)); + let _ = black_box(ecc::affine_scalar_mul(&point, scalar)); }) }); } @@ -70,11 +73,11 @@ fn affine_scalar_multiplication(c: &mut Criterion) { fn projective_scalar_multiplication(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let mut point = black_box(ec::sample_projective_point::(&mut rng)); - let scalar = black_box(ec::sample_scalar::(&mut rng)); + let mut point = black_box(ecc::sample_projective_point::(&mut rng)); + let scalar = black_box(ecc::sample_scalar::(&mut rng)); group.bench_function("projective-scalar multiplication", |b| { b.iter(|| { - ec::projective_scalar_mul_assign(&mut point, scalar); + let _ = black_box(ecc::projective_scalar_mul_assign(&mut point, scalar)); }) }); } @@ -82,10 +85,10 @@ fn projective_scalar_multiplication(c: &mut Criterion) { fn projective_to_affine_normalization(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let point = black_box(ec::sample_projective_point::(&mut rng)); + let point = black_box(ecc::sample_projective_point::(&mut rng)); group.bench_function("projective to affine normalization", |b| { b.iter(|| { - let _ = black_box(ec::projective_to_affine_normalization(black_box(&point))); + let _ = black_box(ecc::projective_to_affine_normalization(&point)); }) }); } @@ -93,13 +96,14 @@ fn projective_to_affine_normalization(c: &mut Criterion) { fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let point_vec = (0..(1 << 16)) - .map(|_| ec::sample_projective_point::(&mut rng)) + let points = repeat_with(|| ecc::sample_projective_point::(&mut rng)) + .take(1 << 16) .collect::>(); + let points_slice = black_box(points.as_slice()); group.bench_function("batch vector of projective to affine normalization", |b| { b.iter(|| { - let _ = black_box(ec::batch_vector_projective_to_affine_normalization( - black_box(point_vec.as_slice()), + let _ = black_box(ecc::batch_vector_projective_to_affine_normalization( + points_slice, )); }) }); @@ -108,20 +112,21 @@ fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) { fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) { let mut group = c.benchmark_group("bench"); let mut rng = OsRng; - let point_vec = (0..(1 << 16)) - .map(|_| ec::sample_projective_point::(&mut rng)) + let points = repeat_with(|| ecc::sample_projective_point::(&mut rng)) + .take(1 << 16) .collect::>(); + let points_slice = black_box(points.as_slice()); group.bench_function("naive vector of projective to affine normalization", |b| { b.iter(|| { - let _ = black_box(ec::naive_vector_projective_to_affine_normalization( - black_box(point_vec.as_slice()), + let _ = black_box(ecc::naive_vector_projective_to_affine_normalization( + points_slice, )); }) }); } criterion_group!( - ec, + ecc, affine_affine_addition, projective_affine_addition, projective_projective_addition, @@ -131,4 +136,4 @@ criterion_group!( batch_vector_projective_to_affine_normalization, naive_vector_projective_to_affine_normalization, ); -criterion_main!(ec); +criterion_main!(ecc); diff --git a/manta-benchmark/benches/mint.rs b/manta-benchmark/benches/mint.rs index 0b1637cd4..bb62b27e5 100644 --- a/manta-benchmark/benches/mint.rs +++ b/manta-benchmark/benches/mint.rs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with manta-rs. If not, see . +//! Mint Benchmarks + use criterion::{black_box, criterion_group, criterion_main, Criterion}; use manta_accounting::transfer::test::assert_valid_proof; use manta_crypto::rand::{OsRng, Rand}; diff --git a/manta-benchmark/benches/private_transfer.rs b/manta-benchmark/benches/private_transfer.rs index 2491f516e..15e875395 100644 --- a/manta-benchmark/benches/private_transfer.rs +++ b/manta-benchmark/benches/private_transfer.rs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with manta-rs. If not, see . +//! Private Transfer Benchmarks + use criterion::{black_box, criterion_group, criterion_main, Criterion}; use manta_accounting::transfer::test::assert_valid_proof; use manta_crypto::rand::OsRng; diff --git a/manta-benchmark/benches/reclaim.rs b/manta-benchmark/benches/reclaim.rs index 5c187bf39..9277238be 100644 --- a/manta-benchmark/benches/reclaim.rs +++ b/manta-benchmark/benches/reclaim.rs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with manta-rs. If not, see . +//! Reclaim Benchmarks + use criterion::{black_box, criterion_group, criterion_main, Criterion}; use manta_accounting::transfer::test::assert_valid_proof; use manta_crypto::rand::OsRng; diff --git a/manta-benchmark/src/ec.rs b/manta-benchmark/src/ecc.rs similarity index 98% rename from manta-benchmark/src/ec.rs rename to manta-benchmark/src/ecc.rs index d42cea4e5..aaf37730d 100644 --- a/manta-benchmark/src/ec.rs +++ b/manta-benchmark/src/ecc.rs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with manta-rs. If not, see . +//! Elliptic Curve Cryptography Utilities + use ark_ec::{AffineCurve, ProjectiveCurve}; use ark_ff::UniformRand; use core::ops::AddAssign; @@ -116,6 +118,7 @@ mod test { use ark_bls12_381::G1Affine; use manta_crypto::rand::OsRng; + /// #[test] fn addition_is_consistent_for_projective_and_affine_curve() { let mut rng = OsRng; @@ -136,6 +139,7 @@ mod test { ); } + /// #[test] fn multiplication_is_consistent_for_projective_and_affine_curve() { let mut rng = OsRng; diff --git a/manta-benchmark/src/lib.rs b/manta-benchmark/src/lib.rs index 4304168fc..629c5f6eb 100644 --- a/manta-benchmark/src/lib.rs +++ b/manta-benchmark/src/lib.rs @@ -14,6 +14,12 @@ // You should have received a copy of the GNU General Public License // along with manta-rs. If not, see . +//! Benchmarking Suite + +#![cfg_attr(doc_cfg, feature(doc_cfg))] +#![forbid(rustdoc::broken_intra_doc_links)] +#![forbid(missing_docs)] + use manta_accounting::transfer::test::assert_valid_proof; use manta_crypto::rand::{OsRng, Rand}; use manta_pay::{ @@ -25,7 +31,7 @@ use manta_pay::{ }; use wasm_bindgen::prelude::wasm_bindgen; -pub mod ec; +pub mod ecc; #[wasm_bindgen] #[derive(Clone, Debug)] @@ -41,7 +47,7 @@ impl Context { #[wasm_bindgen(constructor)] pub fn new() -> Self { let (proving_context, verifying_context, parameters, utxo_accumulator_model) = - parameters::generate().unwrap(); + parameters::generate().expect("Unable to generate default parameters."); Self { proving_context, verifying_context, diff --git a/manta-crypto/Cargo.toml b/manta-crypto/Cargo.toml index 0e173d15d..d63635ed3 100644 --- a/manta-crypto/Cargo.toml +++ b/manta-crypto/Cargo.toml @@ -40,9 +40,9 @@ test = [] [dependencies] derivative = { version = "2.2.0", default-features = false, features = ["use_core"] } manta-util = { path = "../manta-util", default-features = false, features = ["alloc"] } +manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } rand = { version = "0.8.4", optional = true, default-features = false, features = ["alloc"] } rand_core = { version = "0.6.3", default-features = false } -manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } [dev-dependencies] rand = "0.8.4" diff --git a/manta-pay/Cargo.toml b/manta-pay/Cargo.toml index 76efff574..77bbedd77 100644 --- a/manta-pay/Cargo.toml +++ b/manta-pay/Cargo.toml @@ -125,6 +125,7 @@ manta-accounting = { path = "../manta-accounting", default-features = false } manta-crypto = { path = "../manta-crypto", default-features = false } manta-parameters = { path = "../manta-parameters", optional = true, default-features = false } manta-util = { path = "../manta-util", default-features = false } +manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } parking_lot = { version = "0.12.1", optional = true, default-features = false } rand_chacha = { version = "0.3.1", default-features = false } rayon = { version = "1.5.1", optional = true, default-features = false } @@ -137,7 +138,6 @@ tide = { version = "0.16.0", optional = true, default-features = false, features tokio = { version = "1.18.2", optional = true, default-features = false } tokio-tungstenite = { version = "0.17.1", optional = true, default-features = false, features = ["native-tls"] } ws_stream_wasm = { version = "0.7.3", optional = true, default-features = false } -manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } [dev-dependencies] manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom"] } diff --git a/manta-util/Cargo.toml b/manta-util/Cargo.toml index efe14e169..6e38136dc 100644 --- a/manta-util/Cargo.toml +++ b/manta-util/Cargo.toml @@ -39,7 +39,7 @@ std = ["alloc"] [dependencies] crossbeam-channel = { version = "0.5.4", optional = true, default-features = false } +manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } rayon = { version = "1.5.3", optional = true, default-features = false } serde = { version = "1.0.138", optional = true, default-features = false, features = ["derive"] } serde_with = { version = "1.14.0", optional = true, default-features = false, features = ["macros"] } -manta-workspace-hack = { version = "0.1.0", path = "../manta-workspace-hack" } From 8d5ffda8d375ca1b98a6371dec3c0604c463af7c Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Wed, 6 Jul 2022 17:43:33 -0400 Subject: [PATCH 13/14] feat: add docs --- manta-benchmark/src/ecc.rs | 17 +++++++++++++++-- manta-benchmark/src/lib.rs | 9 +++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/manta-benchmark/src/ecc.rs b/manta-benchmark/src/ecc.rs index aaf37730d..145e0eeb5 100644 --- a/manta-benchmark/src/ecc.rs +++ b/manta-benchmark/src/ecc.rs @@ -21,6 +21,7 @@ use ark_ff::UniformRand; use core::ops::AddAssign; use manta_crypto::rand::RngCore; +/// Samples an affine point. #[inline] pub fn sample_affine_point(rng: &mut R) -> A where @@ -30,6 +31,7 @@ where A::Projective::rand(rng).into_affine() } +/// Samples a projective point. #[inline] pub fn sample_projective_point(rng: &mut R) -> P where @@ -39,6 +41,7 @@ where P::rand(rng) } +/// Samples a scalar from scalar field. #[inline] pub fn sample_scalar(rng: &mut R) -> A::ScalarField where @@ -48,6 +51,7 @@ where A::ScalarField::rand(rng) } +/// Adds two affine points. #[inline] pub fn affine_affine_add_assign<'a, A>(lhs: &mut A, rhs: &'a A) where @@ -56,6 +60,7 @@ where lhs.add_assign(rhs); } +/// Adds a projective point with an affine point. #[inline] pub fn projective_affine_add_assign

(lhs: &mut P, rhs: &P::Affine) where @@ -64,6 +69,7 @@ where lhs.add_assign_mixed(rhs); } +/// Adds two projective points. #[inline] pub fn projective_projective_add_assign

(lhs: &mut P, rhs: P) where @@ -72,6 +78,7 @@ where lhs.add_assign(rhs); } +/// Multiplies an affine point `point` with a scalar field element `scalar`. #[inline] pub fn affine_scalar_mul(point: &A, scalar: A::ScalarField) -> A::Projective where @@ -80,6 +87,7 @@ where point.mul(scalar) } +/// Multiplies a projective point `point` with a scalar field element `scalar`. #[inline] pub fn projective_scalar_mul_assign

(point: &mut P, scalar: P::ScalarField) where @@ -88,6 +96,7 @@ where point.mul_assign(scalar); } +/// Normalizes a projective point to an affine point. #[inline] pub fn projective_to_affine_normalization

(point: &P) -> P::Affine where @@ -96,6 +105,7 @@ where point.into_affine() } +/// Normalizes each projective point in `point_vec` to an affine point in a batch style. #[inline] pub fn batch_vector_projective_to_affine_normalization

(point_vec: &[P]) -> Vec where @@ -104,6 +114,7 @@ where P::batch_normalization_into_affine(point_vec) } +/// Naively normalizes each projective point in `point_vec` to an affine point without batching. #[inline] pub fn naive_vector_projective_to_affine_normalization

(point_vec: &[P]) -> Vec where @@ -118,7 +129,8 @@ mod test { use ark_bls12_381::G1Affine; use manta_crypto::rand::OsRng; - /// + /// Tests if affine-affine addition, affine-projective addition, and projective-projective + /// addition give the same result. #[test] fn addition_is_consistent_for_projective_and_affine_curve() { let mut rng = OsRng; @@ -139,7 +151,8 @@ mod test { ); } - /// + /// Tests if affine-scalar multiplication and projective-scalar multiplication give + /// the same result. #[test] fn multiplication_is_consistent_for_projective_and_affine_curve() { let mut rng = OsRng; diff --git a/manta-benchmark/src/lib.rs b/manta-benchmark/src/lib.rs index 629c5f6eb..d3d104599 100644 --- a/manta-benchmark/src/lib.rs +++ b/manta-benchmark/src/lib.rs @@ -33,6 +33,7 @@ use wasm_bindgen::prelude::wasm_bindgen; pub mod ecc; +/// Context Type #[wasm_bindgen] #[derive(Clone, Debug)] pub struct Context { @@ -44,6 +45,7 @@ pub struct Context { #[wasm_bindgen] impl Context { + /// Constructs a new [`Context`] which can be used for proving and verifying [`Proof`]. #[wasm_bindgen(constructor)] pub fn new() -> Self { let (proving_context, verifying_context, parameters, utxo_accumulator_model) = @@ -63,9 +65,11 @@ impl Default for Context { } } +/// Proof Type #[wasm_bindgen] pub struct Proof(TransferPost); +/// Proves a mint [`Proof`] given the [`Context`]. #[wasm_bindgen] pub fn prove_mint(context: &Context) -> Proof { let mut rng = OsRng; @@ -78,6 +82,7 @@ pub fn prove_mint(context: &Context) -> Proof { )) } +/// Proves a private transfer [`Proof`] given the [`Context`]. #[wasm_bindgen] pub fn prove_private_transfer(context: &Context) -> Proof { let mut rng = OsRng; @@ -89,6 +94,7 @@ pub fn prove_private_transfer(context: &Context) -> Proof { )) } +/// Proves a reclaim [`Proof`] given the [`Context`]. #[wasm_bindgen] pub fn prove_reclaim(context: &Context) -> Proof { let mut rng = OsRng; @@ -100,16 +106,19 @@ pub fn prove_reclaim(context: &Context) -> Proof { )) } +/// Verifies a mint [`Proof`] given the [`Context`]. #[wasm_bindgen] pub fn verify_mint(context: &Context, proof: &Proof) { assert_valid_proof(&context.verifying_context.mint, &proof.0); } +/// Verifies a private transfer [`Proof`] given the [`Context`]. #[wasm_bindgen] pub fn verify_private_transfer(context: &Context, proof: &Proof) { assert_valid_proof(&context.verifying_context.private_transfer, &proof.0); } +/// Verifies a reclaim [`Proof`] given the [`Context`]. #[wasm_bindgen] pub fn verify_reclaim(context: &Context, proof: &Proof) { assert_valid_proof(&context.verifying_context.reclaim, &proof.0); From 82cf5bdca8c4e584bdc1ce49974f34d2b67729c1 Mon Sep 17 00:00:00 2001 From: Boyuan Feng Date: Wed, 6 Jul 2022 17:51:27 -0400 Subject: [PATCH 14/14] feat: update docs --- manta-benchmark/src/ecc.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/manta-benchmark/src/ecc.rs b/manta-benchmark/src/ecc.rs index 145e0eeb5..d4d7df6b1 100644 --- a/manta-benchmark/src/ecc.rs +++ b/manta-benchmark/src/ecc.rs @@ -41,7 +41,7 @@ where P::rand(rng) } -/// Samples a scalar from scalar field. +/// Samples a scalar field element. #[inline] pub fn sample_scalar(rng: &mut R) -> A::ScalarField where @@ -96,7 +96,7 @@ where point.mul_assign(scalar); } -/// Normalizes a projective point to an affine point. +/// Normalizes a projective point into an affine point. #[inline] pub fn projective_to_affine_normalization

(point: &P) -> P::Affine where @@ -105,7 +105,7 @@ where point.into_affine() } -/// Normalizes each projective point in `point_vec` to an affine point in a batch style. +/// Normalizes each projective point of `point_vec` into an affine point with the batching optimization. #[inline] pub fn batch_vector_projective_to_affine_normalization

(point_vec: &[P]) -> Vec where @@ -114,7 +114,7 @@ where P::batch_normalization_into_affine(point_vec) } -/// Naively normalizes each projective point in `point_vec` to an affine point without batching. +/// Naively normalizes each projective point of `point_vec` into an affine point without the batching optimization. #[inline] pub fn naive_vector_projective_to_affine_normalization

(point_vec: &[P]) -> Vec where @@ -130,7 +130,7 @@ mod test { use manta_crypto::rand::OsRng; /// Tests if affine-affine addition, affine-projective addition, and projective-projective - /// addition give the same result. + /// addition give same results. #[test] fn addition_is_consistent_for_projective_and_affine_curve() { let mut rng = OsRng; @@ -152,7 +152,7 @@ mod test { } /// Tests if affine-scalar multiplication and projective-scalar multiplication give - /// the same result. + /// same results. #[test] fn multiplication_is_consistent_for_projective_and_affine_curve() { let mut rng = OsRng;