-
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.
See [PoseidonRead](https://github.com/kroma-network/halo2/blob/7d0a36990452c8e7ebd600de258420781a9b7917/halo2_proofs/src/transcript/poseidon.rs#L12-L100), [PoseidonWrite](https://github.com/kroma-network/halo2/blob/7d0a36990452c8e7ebd600de258420781a9b7917/halo2_proofs/src/transcript/poseidon.rs#L102-L174), [Transcript](https://github.com/kroma-network/halo2/blob/7d0a36990452c8e7ebd600de258420781a9b7917/halo2_proofs/src/transcript.rs#L43-L63), [TranscriptRead](https://github.com/kroma-network/halo2/blob/7d0a36990452c8e7ebd600de258420781a9b7917/halo2_proofs/src/transcript.rs#L65-L73) and [TranscriptWirte](https://github.com/kroma-network/halo2/blob/7d0a36990452c8e7ebd600de258420781a9b7917/halo2_proofs/src/transcript.rs#L75-L83).
- Loading branch information
1 parent
bbdeb05
commit 8825d63
Showing
10 changed files
with
437 additions
and
0 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,59 @@ | ||
// 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_CRYPTO_HASHES_SPONGE_POSEIDON_HALO2_POSEIDON_H_ | ||
#define TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON_HALO2_POSEIDON_H_ | ||
|
||
#include <vector> | ||
|
||
#include "third_party/gmp/include/gmpxx.h" | ||
|
||
#include "tachyon/crypto/hashes/sponge/poseidon/poseidon.h" | ||
|
||
namespace tachyon::crypto { | ||
|
||
template <typename PrimeFieldTy> | ||
struct Halo2PoseidonSponge : public PoseidonSponge<PrimeFieldTy> { | ||
using F = PrimeFieldTy; | ||
|
||
Halo2PoseidonSponge() = default; | ||
explicit Halo2PoseidonSponge(const PoseidonConfig<F>& config) | ||
: PoseidonSponge<PrimeFieldTy>(config) { | ||
this->state.elements[0] = F::FromMpzClass(mpz_class(1) << 64); | ||
} | ||
|
||
// FieldBasedCryptographicSponge methods | ||
std::vector<F> SqueezeNativeFieldElements(size_t num_elements) { | ||
std::vector<F> ret = base::CreateVector(num_elements, F::Zero()); | ||
size_t squeeze_index = this->state.mode.next_index; | ||
if (squeeze_index == this->config.rate) { | ||
squeeze_index = 0; | ||
} | ||
this->state[squeeze_index + 1] = F::One(); | ||
switch (this->state.mode.type) { | ||
case DuplexSpongeMode::Type::kAbsorbing: { | ||
this->Permute(); | ||
this->SqueezeInternal(0, &ret); | ||
return ret; | ||
} | ||
case DuplexSpongeMode::Type::kSqueezing: { | ||
size_t squeeze_index = this->state.mode.next_index; | ||
if (squeeze_index == this->config.rate) { | ||
this->Permute(); | ||
squeeze_index = 0; | ||
} | ||
this->SqueezeInternal(squeeze_index, &ret); | ||
return ret; | ||
} | ||
} | ||
NOTREACHED(); | ||
return {}; | ||
} | ||
}; | ||
|
||
} // namespace tachyon::crypto | ||
|
||
#endif // TACHYON_CRYPTO_HASHES_SPONGE_POSEIDON_HALO2_POSEIDON_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
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,30 @@ | ||
load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
tachyon_cc_library( | ||
name = "transcript", | ||
hdrs = ["transcript.h"], | ||
deps = ["//tachyon/math/base:big_int"], | ||
) | ||
|
||
tachyon_cc_library( | ||
name = "poseidon_transcript", | ||
hdrs = ["poseidon_transcript.h"], | ||
deps = [ | ||
":transcript", | ||
"//tachyon/base/buffer:vector_buffer", | ||
"//tachyon/crypto/hashes/sponge/poseidon:halo2_poseidon", | ||
], | ||
) | ||
|
||
tachyon_cc_unittest( | ||
name = "transcript_unittests", | ||
srcs = [ | ||
"poseidon_transcript_unittest.cc", | ||
], | ||
deps = [ | ||
":poseidon_transcript", | ||
"//tachyon/math/elliptic_curves/bn/bn254:g1", | ||
], | ||
) |
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,112 @@ | ||
// 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_TRANSCRIPT_POSEIDON_TRANSCRIPT_H_ | ||
#define TACHYON_ZK_TRANSCRIPT_POSEIDON_TRANSCRIPT_H_ | ||
|
||
#include <array> | ||
#include <utility> | ||
|
||
#include "tachyon/base/buffer/buffer.h" | ||
#include "tachyon/base/buffer/vector_buffer.h" | ||
#include "tachyon/crypto/hashes/sponge/poseidon/halo2_poseidon.h" | ||
#include "tachyon/zk/transcript/transcript.h" | ||
|
||
namespace tachyon::zk { | ||
|
||
template <typename AffinePointTy> | ||
class PoseidonRead : public TranscriptRead<AffinePointTy> { | ||
public: | ||
using ScalarField = typename AffinePointTy::ScalarField; | ||
using CurveConfig = typename AffinePointTy::Curve::Config; | ||
|
||
PoseidonRead() = default; | ||
// Initialize a transcript given an input buffer. | ||
explicit PoseidonRead(base::Buffer read_buf) | ||
: state_( | ||
crypto::PoseidonConfig<ScalarField>::CreateCustom(8, 5, 8, 63, 0)), | ||
buffer_(std::move(read_buf)) {} | ||
|
||
base::Buffer& buffer() { return buffer_; } | ||
const base::Buffer& buffer() const { return buffer_; } | ||
|
||
// Transcript methods | ||
Challenge255<AffinePointTy> SqueezeChallenge() override { | ||
return Challenge255<AffinePointTy>(state_.SqueezeNativeFieldElements(1)[0]); | ||
} | ||
|
||
bool WriteToTranscript(const AffinePointTy& point) override { | ||
std::array<ScalarField, 2> coords = {CurveConfig::BaseToScalar(point.x()), | ||
CurveConfig::BaseToScalar(point.y())}; | ||
return state_.Absorb(coords); | ||
} | ||
|
||
bool WriteToTranscript(const ScalarField& scalar) override { | ||
return state_.Absorb(scalar); | ||
} | ||
|
||
// TranscriptRead methods | ||
bool ReadPoint(AffinePointTy* point) override { | ||
return buffer_.Read(point) && WriteToTranscript(*point); | ||
} | ||
|
||
bool ReadScalar(ScalarField* scalar) override { | ||
return buffer_.Read(scalar) && WriteToTranscript(*scalar); | ||
} | ||
|
||
private: | ||
crypto::Halo2PoseidonSponge<ScalarField> state_; | ||
base::Buffer buffer_; | ||
}; | ||
|
||
template <typename AffinePointTy> | ||
class PoseidonWrite : public TranscriptWrite<AffinePointTy> { | ||
public: | ||
using ScalarField = typename AffinePointTy::ScalarField; | ||
using CurveConfig = typename AffinePointTy::Curve::Config; | ||
|
||
PoseidonWrite() = default; | ||
// Initialize a transcript given an output buffer. | ||
explicit PoseidonWrite(base::VectorBuffer write_buf) | ||
: state_( | ||
crypto::PoseidonConfig<ScalarField>::CreateCustom(8, 5, 8, 63, 0)), | ||
buffer_(std::move(write_buf)) {} | ||
|
||
base::VectorBuffer& buffer() { return buffer_; } | ||
const base::VectorBuffer& buffer() const { return buffer_; } | ||
|
||
// Transcript methods | ||
Challenge255<AffinePointTy> SqueezeChallenge() override { | ||
return Challenge255<AffinePointTy>(state_.SqueezeNativeFieldElements(1)[0]); | ||
} | ||
|
||
bool WriteToTranscript(const AffinePointTy& point) override { | ||
std::array<ScalarField, 2> coords = {CurveConfig::BaseToScalar(point.x()), | ||
CurveConfig::BaseToScalar(point.y())}; | ||
return state_.Absorb(coords); | ||
} | ||
|
||
bool WriteToTranscript(const ScalarField& scalar) override { | ||
return state_.Absorb(scalar); | ||
} | ||
|
||
// TranscriptWrite methods | ||
bool WriteToProof(const AffinePointTy& point) override { | ||
return WriteToTranscript(point) && buffer_.Write(point); | ||
} | ||
|
||
bool WriteToProof(const ScalarField& scalar) override { | ||
return WriteToTranscript(scalar) && buffer_.Write(scalar); | ||
} | ||
|
||
private: | ||
crypto::Halo2PoseidonSponge<ScalarField> state_; | ||
base::VectorBuffer buffer_; | ||
}; | ||
|
||
} // namespace tachyon::zk | ||
|
||
#endif // TACHYON_ZK_TRANSCRIPT_POSEIDON_TRANSCRIPT_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,81 @@ | ||
// 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/transcript/poseidon_transcript.h" | ||
|
||
#include <utility> | ||
#include <vector> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "tachyon/math/elliptic_curves/bn/bn254/g1.h" | ||
|
||
namespace tachyon::zk { | ||
|
||
namespace { | ||
|
||
class PoseidonTranscriptTest : public testing::Test { | ||
public: | ||
static void SetUpTestSuite() { math::bn254::G1Curve::Init(); } | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_F(PoseidonTranscriptTest, WritePoint) { | ||
using AffinePoint = math::bn254::G1AffinePoint; | ||
|
||
base::VectorBuffer write_buf; | ||
PoseidonWrite<AffinePoint> writer(std::move(write_buf)); | ||
AffinePoint expected = AffinePoint::Random(); | ||
ASSERT_TRUE(writer.WriteToProof(expected)); | ||
|
||
base::Buffer read_buf(writer.buffer().buffer(), writer.buffer().buffer_len()); | ||
PoseidonRead<AffinePoint> reader(std::move(read_buf)); | ||
AffinePoint actual; | ||
ASSERT_TRUE(reader.ReadPoint(&actual)); | ||
|
||
EXPECT_EQ(expected, actual); | ||
} | ||
|
||
TEST_F(PoseidonTranscriptTest, WriteScalar) { | ||
using AffinePoint = math::bn254::G1AffinePoint; | ||
using ScalarField = AffinePoint::ScalarField; | ||
|
||
base::VectorBuffer write_buf; | ||
PoseidonWrite<AffinePoint> writer(std::move(write_buf)); | ||
ScalarField expected = ScalarField::Random(); | ||
ASSERT_TRUE(writer.WriteToProof(expected)); | ||
|
||
base::Buffer read_buf(writer.buffer().buffer(), writer.buffer().buffer_len()); | ||
PoseidonRead<AffinePoint> reader(std::move(read_buf)); | ||
ScalarField actual; | ||
ASSERT_TRUE(reader.ReadScalar(&actual)); | ||
|
||
EXPECT_EQ(expected, actual); | ||
} | ||
|
||
TEST_F(PoseidonTranscriptTest, SqueezeChallenge) { | ||
using AffinePoint = math::bn254::G1AffinePoint; | ||
using ScalarField = AffinePoint::ScalarField; | ||
|
||
base::VectorBuffer write_buf; | ||
PoseidonWrite<AffinePoint> writer(std::move(write_buf)); | ||
AffinePoint generator = AffinePoint::Generator(); | ||
ASSERT_TRUE(writer.WriteToProof(generator)); | ||
|
||
std::vector<uint8_t> expected_bytes = {25, 86, 205, 219, 59, 135, 187, 231, | ||
192, 54, 23, 138, 114, 176, 9, 157, | ||
1, 97, 110, 174, 67, 9, 89, 85, | ||
126, 129, 216, 121, 53, 99, 227, 26}; | ||
ScalarField expected = | ||
ScalarField::FromBigInt(math::BigInt<4>::FromBytesLE(expected_bytes)); | ||
|
||
ScalarField actual = writer.SqueezeChallenge().ChallengeAsScalar(); | ||
|
||
EXPECT_EQ(expected, actual); | ||
} | ||
|
||
} // namespace tachyon::zk |
Oops, something went wrong.