Skip to content

Commit

Permalink
chore: update Pedersen test component to use random elements in bn254…
Browse files Browse the repository at this point in the history
… and bls12-381 curve tests (PROOF-891) (#152)

* feat: add random affine element generation

* chore: update pedersen tests to use random element generator
  • Loading branch information
jacobtrombetta authored Jul 11, 2024
1 parent 4f590f5 commit db722fc
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cbindings/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 8 additions & 14 deletions cbindings/pedersen.t.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -78,17 +78,14 @@ static std::vector<c21t::element_p3> 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<cg1t::element_affine> get_bls12_381_g1_generators(uint64_t seq_length,
uint64_t offset) {
std::vector<cg1t::element_affine> 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<uint64_t>(i + 1),
static_cast<uint64_t>(i + 2)};
cg1rn::generate_random_element(generators[i], rng);
}

return generators;
Expand All @@ -97,17 +94,14 @@ static std::vector<cg1t::element_affine> 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<cn1t::element_affine> get_bn254_g1_generators(uint64_t seq_length,
uint64_t offset) {
std::vector<cn1t::element_affine> 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<uint64_t>(i + 1),
static_cast<uint64_t>(i + 2)};
cn1rn::generate_random_element(generators[i], rng);
}

return generators;
Expand Down
15 changes: 15 additions & 0 deletions sxt/curve_bng1/random/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
17 changes: 17 additions & 0 deletions sxt/curve_bng1/random/element_affine.cc
Original file line number Diff line number Diff line change
@@ -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"
38 changes: 38 additions & 0 deletions sxt/curve_bng1/random/element_affine.h
Original file line number Diff line number Diff line change
@@ -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 <cstring>

#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
38 changes: 38 additions & 0 deletions sxt/curve_bng1/random/element_affine.t.cc
Original file line number Diff line number Diff line change
@@ -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));
}
}
15 changes: 15 additions & 0 deletions sxt/curve_g1/random/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
17 changes: 17 additions & 0 deletions sxt/curve_g1/random/element_affine.cc
Original file line number Diff line number Diff line change
@@ -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"
38 changes: 38 additions & 0 deletions sxt/curve_g1/random/element_affine.h
Original file line number Diff line number Diff line change
@@ -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 <cstring>

#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
38 changes: 38 additions & 0 deletions sxt/curve_g1/random/element_affine.t.cc
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit db722fc

Please sign in to comment.