-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support halo2 (de)serialization of point to/from proof
Halo2 encodes point with a compression making use of a spare bit of base field. But our copyable was a just memory copy including y and infinity flag. To match with an outcome of Halo2 and furthermore support various encoding scheme, `Transcript` class delegates encoding to a child class.
- Loading branch information
Showing
7 changed files
with
276 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2020-2022 The Electric Coin Company | ||
// Copyright 2022 The Halo2 developers | ||
// Use of this source code is governed by a MIT/Apache-2.0 style license that | ||
// can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 | ||
// file. | ||
|
||
#ifndef TACHYON_ZK_PLONK_HALO2_PROOF_SERIALIZER_H_ | ||
#define TACHYON_ZK_PLONK_HALO2_PROOF_SERIALIZER_H_ | ||
|
||
#include <type_traits> | ||
#include <utility> | ||
|
||
#include "tachyon/base/buffer/buffer.h" | ||
#include "tachyon/math/elliptic_curves/affine_point.h" | ||
#include "tachyon/math/finite_fields/prime_field_base.h" | ||
|
||
namespace tachyon::zk::halo2 { | ||
|
||
template <typename F, typename SFINAE = void> | ||
class ProofSerializer; | ||
|
||
template <typename F> | ||
class ProofSerializer< | ||
F, std::enable_if_t<std::is_base_of_v<math::PrimeFieldBase<F>, F>>> { | ||
public: | ||
[[nodiscard]] static bool ReadFromProof(const base::Buffer& buffer, | ||
F* value) { | ||
return buffer.Read(value); | ||
} | ||
|
||
[[nodiscard]] static bool WriteToProof(const F& value, base::Buffer& buffer) { | ||
return buffer.Write(value); | ||
} | ||
}; | ||
|
||
template <typename Curve> | ||
class ProofSerializer<math::AffinePoint<Curve>> { | ||
public: | ||
using BaseField = typename math::AffinePoint<Curve>::BaseField; | ||
using BigIntTy = typename BaseField::BigIntTy; | ||
|
||
static_assert(BaseField::kModulusBits % 8 != 0, | ||
"Halo2 needs 1 spare bit to put sign bit"); | ||
|
||
constexpr static size_t kByteSize = BaseField::kLimbNums * sizeof(uint64_t); | ||
|
||
[[nodiscard]] static bool ReadFromProof(const base::Buffer& buffer, | ||
math::AffinePoint<Curve>* point_out) { | ||
uint8_t bytes[kByteSize]; | ||
if (!buffer.Read(bytes)) return false; | ||
uint8_t is_odd = bytes[kByteSize - 1] >> 7; | ||
bytes[kByteSize - 1] &= 0b01111111; | ||
BaseField x = BaseField::FromBigInt(BigIntTy::FromBytesLE(bytes)); | ||
if (x.IsZero()) { | ||
*point_out = math::AffinePoint<Curve>::Zero(); | ||
return true; | ||
} else { | ||
std::optional<math::AffinePoint<Curve>> point = | ||
math::AffinePoint<Curve>::CreateFromX(x, is_odd); | ||
if (!point.has_value()) return false; | ||
*point_out = std::move(point).value(); | ||
return true; | ||
} | ||
} | ||
|
||
[[nodiscard]] static bool WriteToProof(const math::AffinePoint<Curve>& point, | ||
base::Buffer& buffer) { | ||
if (point.infinity()) { | ||
constexpr uint8_t kZeroBytes[kByteSize] = { | ||
0, | ||
}; | ||
return buffer.Write(kZeroBytes); | ||
} else { | ||
uint8_t is_odd = uint8_t{point.y().ToBigInt().IsOdd()} << 7; | ||
std::array<uint8_t, kByteSize> x = point.x().ToBigInt().ToBytesLE(); | ||
if (!buffer.Write(x)) return false; | ||
return buffer.WriteAt(buffer.buffer_offset() - 1, | ||
static_cast<uint8_t>(x[kByteSize - 1] | is_odd)); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace tachyon::zk::halo2 | ||
|
||
#endif // TACHYON_ZK_PLONK_HALO2_PROOF_SERIALIZER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2020-2022 The Electric Coin Company | ||
// Copyright 2022 The Halo2 developers | ||
// Use of this source code is governed by a MIT/Apache-2.0 style license that | ||
// can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 | ||
// file. | ||
|
||
#include "tachyon/zk/plonk/halo2/proof_serializer.h" | ||
|
||
#include <vector> | ||
|
||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
|
||
#include "tachyon/base/buffer/vector_buffer.h" | ||
#include "tachyon/math/elliptic_curves/bn/bn254/g1.h" | ||
|
||
namespace tachyon::zk::halo2 { | ||
|
||
namespace { | ||
|
||
using namespace math::bn254; | ||
|
||
class ProofSerializerTest : public testing::Test { | ||
public: | ||
static void SetUpTestSuite() { G1Curve::Init(); } | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_F(ProofSerializerTest, SerializeScalar) { | ||
struct { | ||
std::string_view hex; | ||
std::vector<uint8_t> proof; | ||
} tests[] = { | ||
{"0x2482c9ce1f365ed93c2afe1df9c673b0ba65278badd4d150f3b848cdd3d0cec8", | ||
{200, 206, 208, 211, 205, 72, 184, 243, 80, 209, 212, | ||
173, 139, 39, 101, 186, 176, 115, 198, 249, 29, 254, | ||
42, 60, 217, 94, 54, 31, 206, 201, 130, 36}}, | ||
}; | ||
|
||
for (const auto& test : tests) { | ||
std::vector<uint8_t> buffer; | ||
buffer.resize(test.proof.size()); | ||
base::Buffer write_buf(buffer.data(), buffer.size()); | ||
Fr expected = Fr::FromHexString(test.hex); | ||
ASSERT_TRUE(ProofSerializer<Fr>::WriteToProof(expected, write_buf)); | ||
EXPECT_THAT(buffer, testing::ElementsAreArray(test.proof)); | ||
|
||
write_buf.set_buffer_offset(0); | ||
Fr actual; | ||
ASSERT_TRUE(ProofSerializer<Fr>::ReadFromProof(write_buf, &actual)); | ||
EXPECT_EQ(actual, expected); | ||
} | ||
} | ||
|
||
TEST_F(ProofSerializerTest, SerializeProof) { | ||
struct { | ||
std::array<std::string_view, 2> hex; | ||
std::vector<uint8_t> proof; | ||
} tests[] = { | ||
// even point | ||
{{ | ||
"0x233bd4dc42ffd123f6d041dca2117acea5f6a201b4612a81e7081cad001df470", | ||
"0x14ecc49a7d74ee9059862ca5237c72f22dc6c39b64ec3e7c4ec314187577ee56", | ||
}, | ||
{112, 244, 29, 0, 173, 28, 8, 231, 129, 42, 97, | ||
180, 1, 162, 246, 165, 206, 122, 17, 162, 220, 65, | ||
208, 246, 35, 209, 255, 66, 220, 212, 59, 35}}, | ||
// odd point | ||
{{ | ||
"0x1ec72fa9df2846c267ad6bc77e438c0d8c0c9bba978be3095cc48b0334299dbb", | ||
"0x2c1b5dfdca4dfc40a864355fead42fb3656a8a3304ad11b1dee1a4b924ac5a03", | ||
}, | ||
{187, 157, 41, 52, 3, 139, 196, 92, 9, 227, 139, | ||
151, 186, 155, 12, 140, 13, 140, 67, 126, 199, 107, | ||
173, 103, 194, 70, 40, 223, 169, 47, 199, 158}}, | ||
}; | ||
|
||
for (const auto& test : tests) { | ||
std::vector<uint8_t> buffer; | ||
buffer.resize(test.proof.size()); | ||
base::Buffer write_buf(buffer.data(), buffer.size()); | ||
Fq x = Fq::FromHexString(test.hex[0]); | ||
Fq y = Fq::FromHexString(test.hex[1]); | ||
G1AffinePoint expected(x, y); | ||
ASSERT_TRUE( | ||
ProofSerializer<G1AffinePoint>::WriteToProof(expected, write_buf)); | ||
EXPECT_THAT(buffer, testing::ElementsAreArray(test.proof)); | ||
|
||
write_buf.set_buffer_offset(0); | ||
G1AffinePoint actual; | ||
ASSERT_TRUE( | ||
ProofSerializer<G1AffinePoint>::ReadFromProof(write_buf, &actual)); | ||
EXPECT_EQ(actual, expected); | ||
} | ||
} | ||
|
||
} // namespace tachyon::zk::halo2 |
Oops, something went wrong.