From db722fc9baa38d5a0f6e4793866e48219fec4b22 Mon Sep 17 00:00:00 2001 From: Jacob Trombetta Date: Thu, 11 Jul 2024 13:21:13 -0400 Subject: [PATCH] chore: update Pedersen test component to use random elements in bn254 and bls12-381 curve tests (PROOF-891) (#152) * feat: add random affine element generation * chore: update pedersen tests to use random element generator --- cbindings/BUILD | 4 +-- cbindings/pedersen.t.cc | 22 +++++-------- sxt/curve_bng1/random/BUILD | 15 +++++++++ sxt/curve_bng1/random/element_affine.cc | 17 ++++++++++ sxt/curve_bng1/random/element_affine.h | 38 +++++++++++++++++++++++ sxt/curve_bng1/random/element_affine.t.cc | 38 +++++++++++++++++++++++ sxt/curve_g1/random/BUILD | 15 +++++++++ sxt/curve_g1/random/element_affine.cc | 17 ++++++++++ sxt/curve_g1/random/element_affine.h | 38 +++++++++++++++++++++++ sxt/curve_g1/random/element_affine.t.cc | 38 +++++++++++++++++++++++ 10 files changed, 226 insertions(+), 16 deletions(-) create mode 100644 sxt/curve_bng1/random/element_affine.cc create mode 100644 sxt/curve_bng1/random/element_affine.h create mode 100644 sxt/curve_bng1/random/element_affine.t.cc create mode 100644 sxt/curve_g1/random/element_affine.cc create mode 100644 sxt/curve_g1/random/element_affine.h create mode 100644 sxt/curve_g1/random/element_affine.t.cc diff --git a/cbindings/BUILD b/cbindings/BUILD index 92432c9c1..fe6375c73 100644 --- a/cbindings/BUILD +++ b/cbindings/BUILD @@ -125,16 +125,16 @@ sxt_cc_component( "//sxt/base/num:fast_random_number_generator", "//sxt/base/test:unit_test", "//sxt/curve21/type:element_p3", - "//sxt/curve_bng1/constant:generator", "//sxt/curve_bng1/operation:add", "//sxt/curve_bng1/operation:scalar_multiply", + "//sxt/curve_bng1/random:element_affine", "//sxt/curve_bng1/type:conversion_utility", "//sxt/curve_bng1/type:element_affine", "//sxt/curve_bng1/type:element_p2", - "//sxt/curve_g1/constant:generator", "//sxt/curve_g1/operation:add", "//sxt/curve_g1/operation:compression", "//sxt/curve_g1/operation:scalar_multiply", + "//sxt/curve_g1/random:element_affine", "//sxt/curve_g1/type:compressed_element", "//sxt/curve_g1/type:conversion_utility", "//sxt/curve_g1/type:element_affine", diff --git a/cbindings/pedersen.t.cc b/cbindings/pedersen.t.cc index 5c1df7c35..c65b0fddc 100644 --- a/cbindings/pedersen.t.cc +++ b/cbindings/pedersen.t.cc @@ -24,16 +24,16 @@ #include "sxt/base/num/fast_random_number_generator.h" #include "sxt/base/test/unit_test.h" #include "sxt/curve21/type/element_p3.h" -#include "sxt/curve_bng1/constant/generator.h" #include "sxt/curve_bng1/operation/add.h" #include "sxt/curve_bng1/operation/scalar_multiply.h" +#include "sxt/curve_bng1/random/element_affine.h" #include "sxt/curve_bng1/type/conversion_utility.h" #include "sxt/curve_bng1/type/element_affine.h" #include "sxt/curve_bng1/type/element_p2.h" -#include "sxt/curve_g1/constant/generator.h" #include "sxt/curve_g1/operation/add.h" #include "sxt/curve_g1/operation/compression.h" #include "sxt/curve_g1/operation/scalar_multiply.h" +#include "sxt/curve_g1/random/element_affine.h" #include "sxt/curve_g1/type/compressed_element.h" #include "sxt/curve_g1/type/conversion_utility.h" #include "sxt/curve_g1/type/element_affine.h" @@ -78,17 +78,14 @@ static std::vector compute_random_curve25519_generators(uint64 //-------------------------------------------------------------------------------------------------- // get_bls12_381_g1_generators //-------------------------------------------------------------------------------------------------- -/** - * This is a placeholder method. This method will be updated to behave like the - * compute_random_curve25519_generators method after random element generation is implemented inside - * the curve_g1 package group. - */ static std::vector get_bls12_381_g1_generators(uint64_t seq_length, uint64_t offset) { std::vector generators(seq_length); for (uint64_t i = 0; i < seq_length; ++i) { - generators[i] = cg1cn::generator_affine_v; + basn::fast_random_number_generator rng{static_cast(i + 1), + static_cast(i + 2)}; + cg1rn::generate_random_element(generators[i], rng); } return generators; @@ -97,17 +94,14 @@ static std::vector get_bls12_381_g1_generators(uint64_t se //-------------------------------------------------------------------------------------------------- // get_bn254_g1_generators //-------------------------------------------------------------------------------------------------- -/** - * This is a placeholder method. This method will be updated to behave like the - * compute_random_curve25519_generators method after random element generation is implemented inside - * the curve_bng1 package group. - */ static std::vector get_bn254_g1_generators(uint64_t seq_length, uint64_t offset) { std::vector generators(seq_length); for (uint64_t i = 0; i < seq_length; ++i) { - generators[i] = cn1cn::generator_affine_v; + basn::fast_random_number_generator rng{static_cast(i + 1), + static_cast(i + 2)}; + cn1rn::generate_random_element(generators[i], rng); } return generators; diff --git a/sxt/curve_bng1/random/BUILD b/sxt/curve_bng1/random/BUILD index d5e67d849..b32f96f6b 100644 --- a/sxt/curve_bng1/random/BUILD +++ b/sxt/curve_bng1/random/BUILD @@ -3,6 +3,21 @@ load( "sxt_cc_component", ) +sxt_cc_component( + name = "element_affine", + test_deps = [ + "//sxt/base/test:unit_test", + "//sxt/curve_bng1/property:curve", + ], + deps = [ + ":element_p2", + "//sxt/base/macro:cuda_callable", + "//sxt/curve_bng1/type:conversion_utility", + "//sxt/curve_bng1/type:element_affine", + "//sxt/curve_bng1/type:element_p2", + ], +) + sxt_cc_component( name = "element_p2", test_deps = [ diff --git a/sxt/curve_bng1/random/element_affine.cc b/sxt/curve_bng1/random/element_affine.cc new file mode 100644 index 000000000..741a1c8c3 --- /dev/null +++ b/sxt/curve_bng1/random/element_affine.cc @@ -0,0 +1,17 @@ +/** Proofs GPU - Space and Time's cryptographic proof algorithms on the CPU and GPU. + * + * Copyright 2023-present Space and Time Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sxt/curve_bng1/random/element_affine.h" diff --git a/sxt/curve_bng1/random/element_affine.h b/sxt/curve_bng1/random/element_affine.h new file mode 100644 index 000000000..428fbc040 --- /dev/null +++ b/sxt/curve_bng1/random/element_affine.h @@ -0,0 +1,38 @@ +/** Proofs GPU - Space and Time's cryptographic proof algorithms on the CPU and GPU. + * + * Copyright 2023-present Space and Time Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#include "sxt/base/macro/cuda_callable.h" +#include "sxt/curve_bng1/random/element_p2.h" +#include "sxt/curve_bng1/type/conversion_utility.h" +#include "sxt/curve_bng1/type/element_affine.h" +#include "sxt/curve_bng1/type/element_p2.h" + +namespace sxt::cn1rn { +//-------------------------------------------------------------------------------------------------- +// generate_random_element +//-------------------------------------------------------------------------------------------------- +CUDA_CALLABLE +inline void generate_random_element(cn1t::element_affine& a, + basn::fast_random_number_generator& rng) noexcept { + cn1t::element_p2 p; + generate_random_element(p, rng); + cn1t::to_element_affine(a, p); +} +} // namespace sxt::cn1rn diff --git a/sxt/curve_bng1/random/element_affine.t.cc b/sxt/curve_bng1/random/element_affine.t.cc new file mode 100644 index 000000000..c8e2be846 --- /dev/null +++ b/sxt/curve_bng1/random/element_affine.t.cc @@ -0,0 +1,38 @@ +/** Proofs GPU - Space and Time's cryptographic proof algorithms on the CPU and GPU. + * + * Copyright 2023-present Space and Time Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sxt/curve_bng1/random/element_affine.h" + +#include "sxt/base/num/fast_random_number_generator.h" +#include "sxt/base/test/unit_test.h" +#include "sxt/curve_bng1/property/curve.h" +#include "sxt/curve_bng1/type/element_affine.h" + +using namespace sxt; +using namespace sxt::cn1rn; + +TEST_CASE("random element generation") { + basn::fast_random_number_generator rng{1, 2}; + + SECTION("will return different values on the curve if called multiple times") { + cn1t::element_affine e1, e2; + generate_random_element(e1, rng); + generate_random_element(e2, rng); + REQUIRE(e1 != e2); + REQUIRE(cn1p::is_on_curve(e1)); + REQUIRE(cn1p::is_on_curve(e2)); + } +} diff --git a/sxt/curve_g1/random/BUILD b/sxt/curve_g1/random/BUILD index 73e690982..d0201b0ff 100644 --- a/sxt/curve_g1/random/BUILD +++ b/sxt/curve_g1/random/BUILD @@ -3,6 +3,21 @@ load( "sxt_cc_component", ) +sxt_cc_component( + name = "element_affine", + test_deps = [ + "//sxt/base/test:unit_test", + "//sxt/curve_g1/property:curve", + ], + deps = [ + ":element_p2", + "//sxt/base/macro:cuda_callable", + "//sxt/curve_g1/type:conversion_utility", + "//sxt/curve_g1/type:element_affine", + "//sxt/curve_g1/type:element_p2", + ], +) + sxt_cc_component( name = "element_p2", test_deps = [ diff --git a/sxt/curve_g1/random/element_affine.cc b/sxt/curve_g1/random/element_affine.cc new file mode 100644 index 000000000..0097ecb49 --- /dev/null +++ b/sxt/curve_g1/random/element_affine.cc @@ -0,0 +1,17 @@ +/** Proofs GPU - Space and Time's cryptographic proof algorithms on the CPU and GPU. + * + * Copyright 2023-present Space and Time Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sxt/curve_g1/random/element_affine.h" diff --git a/sxt/curve_g1/random/element_affine.h b/sxt/curve_g1/random/element_affine.h new file mode 100644 index 000000000..28864b8db --- /dev/null +++ b/sxt/curve_g1/random/element_affine.h @@ -0,0 +1,38 @@ +/** Proofs GPU - Space and Time's cryptographic proof algorithms on the CPU and GPU. + * + * Copyright 2023-present Space and Time Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#include "sxt/base/macro/cuda_callable.h" +#include "sxt/curve_g1/random/element_p2.h" +#include "sxt/curve_g1/type/conversion_utility.h" +#include "sxt/curve_g1/type/element_affine.h" +#include "sxt/curve_g1/type/element_p2.h" + +namespace sxt::cg1rn { +//-------------------------------------------------------------------------------------------------- +// generate_random_element +//-------------------------------------------------------------------------------------------------- +CUDA_CALLABLE +inline void generate_random_element(cg1t::element_affine& a, + basn::fast_random_number_generator& rng) noexcept { + cg1t::element_p2 p; + generate_random_element(p, rng); + cg1t::to_element_affine(a, p); +} +} // namespace sxt::cg1rn diff --git a/sxt/curve_g1/random/element_affine.t.cc b/sxt/curve_g1/random/element_affine.t.cc new file mode 100644 index 000000000..ad9a806b1 --- /dev/null +++ b/sxt/curve_g1/random/element_affine.t.cc @@ -0,0 +1,38 @@ +/** Proofs GPU - Space and Time's cryptographic proof algorithms on the CPU and GPU. + * + * Copyright 2023-present Space and Time Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sxt/curve_g1/random/element_affine.h" + +#include "sxt/base/num/fast_random_number_generator.h" +#include "sxt/base/test/unit_test.h" +#include "sxt/curve_g1/property/curve.h" +#include "sxt/curve_g1/type/element_affine.h" + +using namespace sxt; +using namespace sxt::cg1rn; + +TEST_CASE("random element generation") { + basn::fast_random_number_generator rng{1, 2}; + + SECTION("will return different values on the curve if called multiple times") { + cg1t::element_affine e1, e2; + generate_random_element(e1, rng); + generate_random_element(e2, rng); + REQUIRE(e1 != e2); + REQUIRE(cg1p::is_on_curve(e1)); + REQUIRE(cg1p::is_on_curve(e2)); + } +}