Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add field25 type package (PROOF-775) #82

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions sxt/field25/type/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load(
"//bazel:sxt_build_system.bzl",
"sxt_cc_component",
)

sxt_cc_component(
name = "element",
impl_deps = [
"//sxt/field25/base:byte_conversion",
"//sxt/field25/base:reduce",
],
is_cuda = True,
test_deps = [
"//sxt/base/test:unit_test",
"//sxt/field25/base:constants",
],
deps = [
"//sxt/base/macro:cuda_callable",
],
)

sxt_cc_component(
name = "literal",
test_deps = [
"//sxt/base/test:unit_test",
],
deps = [
":element",
"//sxt/base/type:literal",
"//sxt/field25/base:byte_conversion",
],
)
70 changes: 70 additions & 0 deletions sxt/field25/type/element.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/** 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/field25/type/element.h"

#include <array>
#include <iomanip>

#include "sxt/field25/base/byte_conversion.h"
#include "sxt/field25/base/reduce.h"

namespace sxt::f25t {
//--------------------------------------------------------------------------------------------------
// print_impl
//--------------------------------------------------------------------------------------------------
static std::ostream& print_impl(std::ostream& out, const std::array<uint8_t, 32>& bytes,
int start) noexcept {
out << std::hex << static_cast<int>(bytes[start]);
for (int i = start; i-- > 0;) {
out << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]);
}
out << "_f25";
return out;
}

//--------------------------------------------------------------------------------------------------
// operator<<
//--------------------------------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& out, const element& e) noexcept {
std::array<uint8_t, 32> bytes = {};
f25b::to_bytes_le(bytes.data(), e.data());
auto flags = out.flags();
out << "0x";
for (int i = 32; i-- > 0;) {
if (bytes[i] != 0) {
print_impl(out, bytes, i);
out.flags(flags);
return out;
}
}
out << "0_f25";
out.flags(flags);
return out;
}

//--------------------------------------------------------------------------------------------------
// operator==
//--------------------------------------------------------------------------------------------------
CUDA_CALLABLE bool operator==(const element& lhs, const element& rhs) noexcept {
for (size_t i = 0; i < element::num_limbs_v; ++i) {
if (lhs[i] != rhs[i]) {
return false;
}
}
return true;
}
} // namespace sxt::f25t
67 changes: 67 additions & 0 deletions sxt/field25/type/element.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/** 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 <cstdint>
#include <iosfwd>

#include "sxt/base/macro/cuda_callable.h"

namespace sxt::f25t {
//--------------------------------------------------------------------------------------------------
// element
//--------------------------------------------------------------------------------------------------
class element {
public:
static constexpr size_t num_limbs_v = 4;

element() noexcept = default;

CUDA_CALLABLE constexpr element(uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4) noexcept
: data_{x1, x2, x3, x4} {}

CUDA_CALLABLE constexpr element(const uint64_t x[4]) noexcept : data_{x[0], x[1], x[2], x[3]} {}

CUDA_CALLABLE constexpr const uint64_t& operator[](int index) const noexcept {
return data_[index];
}

CUDA_CALLABLE constexpr uint64_t& operator[](int index) noexcept { return data_[index]; }

CUDA_CALLABLE constexpr const uint64_t* data() const noexcept { return data_; }

CUDA_CALLABLE constexpr uint64_t* data() noexcept { return data_; }

private:
uint64_t data_[num_limbs_v];
};

//--------------------------------------------------------------------------------------------------
// operator<<
//--------------------------------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& out, const element& e) noexcept;

//--------------------------------------------------------------------------------------------------
// operator==
//--------------------------------------------------------------------------------------------------
CUDA_CALLABLE bool operator==(const element& lhs, const element& rhs) noexcept;

//--------------------------------------------------------------------------------------------------
// operator!=
//--------------------------------------------------------------------------------------------------
inline bool operator!=(const element& lhs, const element& rhs) noexcept { return !(lhs == rhs); }
} // namespace sxt::f25t
53 changes: 53 additions & 0 deletions sxt/field25/type/element.t.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/** 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/field25/type/element.h"

#include <sstream>

#include "sxt/base/test/unit_test.h"
#include "sxt/field25/base/constants.h"

using namespace sxt;
using namespace sxt::f25t;

TEST_CASE("element conversion") {
std::ostringstream oss;

SECTION("of zero prints as zero") {
element e{0, 0, 0, 0};
oss << e;
REQUIRE(oss.str() == "0x0_f25");
}

SECTION("of one in Montgomery form prints as one") {
element e(f25b::r_v.data());
oss << e;
REQUIRE(oss.str() == "0x1_f25");
}

SECTION("of the modulus prints as zero") {
element e(f25b::p_v.data());
oss << e;
REQUIRE(oss.str() == "0x0_f25");
}

SECTION("of r2_v is r_v") {
element e(f25b::r2_v.data());
oss << e;
REQUIRE(oss.str() == "0xe0a77c19a07df2f666ea36f7879462c0a78eb28f5c70b3dd35d438dc58f0d9d_f25");
}
}
17 changes: 17 additions & 0 deletions sxt/field25/type/literal.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/field25/type/literal.h"
37 changes: 37 additions & 0 deletions sxt/field25/type/literal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** 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 <array>

#include "sxt/base/type/literal.h"
#include "sxt/field25/base/byte_conversion.h"
#include "sxt/field25/type/element.h"

namespace sxt::f25t {
//--------------------------------------------------------------------------------------------------
// _f25
//--------------------------------------------------------------------------------------------------
template <char... Chars> element operator"" _f25() noexcept {
std::array<uint64_t, 4> bytes = {};
bast::parse_literal<4, Chars...>(bytes);
element res;
bool is_below;
f25b::from_bytes_le(is_below, res.data(), reinterpret_cast<const uint8_t*>(bytes.data()));
return res;
}
} // namespace sxt::f25t
62 changes: 62 additions & 0 deletions sxt/field25/type/literal.t.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/** 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/field25/type/literal.h"

#include <sstream>

#include "sxt/base/test/unit_test.h"

using namespace sxt::f25t;

TEST_CASE("literal element printing") {
std::ostringstream oss;

SECTION("of zero prints 0x0_f25") {
oss << 0x0_f25;
REQUIRE(oss.str() == "0x0_f25");
}

SECTION("of one prints 0x1_f25") {
oss << 0x1_f25;
REQUIRE(oss.str() == "0x1_f25");
}

SECTION("of 10 prints 0xa_f25") {
oss << 0xa_f25;
REQUIRE(oss.str() == "0xa_f25");
}

SECTION("of 16 prints 0x10_f25") {
oss << 0x10_f25;
REQUIRE(oss.str() == "0x10_f25");
}

SECTION("of the modulus prints 0x0_f25") {
oss << 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47_f25;
REQUIRE(oss.str() == "0x0_f25");
}

SECTION("of the modulus minus one prints a pre-computed value") {
oss << 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd46_f25;
REQUIRE(oss.str() == "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd46_f25");
}

SECTION("of the modulus plus one prints as one") {
oss << 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd48_f25;
REQUIRE(oss.str() == "0x1_f25");
}
}