Skip to content

Commit

Permalink
Transcript and Fiat-Shamir heuristic indexed challenges support added…
Browse files Browse the repository at this point in the history
…. PLONKs Fiat-Shamir manifest inited. #24 #20
  • Loading branch information
nkaskov committed Oct 20, 2021
1 parent 355e4ea commit 7fa02b3
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 60 deletions.
62 changes: 62 additions & 0 deletions include/nil/crypto3/zk/snark/schemes/plonk/transcript.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2021 Mikhail Komarov <nemo@nil.foundation>
// Copyright (c) 2021 Nikita Kaskov <nbering@nil.foundation>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_PLONK_KATE_COMMITMENT_FIAT_SHAMIR_MANIFEST_HPP
#define CRYPTO3_ZK_PLONK_KATE_COMMITMENT_FIAT_SHAMIR_MANIFEST_HPP

namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {

struct plonk_kate_fiat_shamir_heuristic_manifest {
enum challenges_ids{
beta,
gamma,
alpha,
zeta,
nu,
u
};
};

template <std::size_t AlphasAmount>
struct plonk_fri_fiat_shamir_heuristic_manifest {
enum challenges_ids{
beta,
gamma,
alpha,
zeta = alpha + AlphasAmount,
nu,
u
};
};

} // namespace snark
} // namespace zk
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_PLONK_KATE_COMMITMENT_FIAT_SHAMIR_MANIFEST_HPP
87 changes: 55 additions & 32 deletions include/nil/crypto3/zk/snark/transcript/fiat_shamir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,48 @@ namespace nil {
namespace zk {
namespace snark {

template<typename ...>
struct fiat_shamir_heuristic_manifest {
enum challenges_ids{

}

typedef ... preprocessor;

typedef std::tuple<...> processors;

struct transcript_manifest {
using challenges_ids = typename Manifest::challenges_ids;

using processors = typename Manifest::processors;

using challenges = std::array<, std::tuple_size<processors>>;

using result = typename Hash::digest_type;
}
}

/*!
* @brief Fiat–Shamir heuristic.
* @tparam Hash Hash function, which serves as a non-interactive random oracle.
* @tparam TManifest Fiat-Shamir Heuristic Manifest in the following form:
*
* template<typename ...>
* struct fiat_shamir_heuristic_manifest {
*
* typedef ... preprocessor;
*
* struct transcript_manifest {
* std::size_t gammas_amount = 5;
* public:
* enum challenges_ids{
* alpha,
* beta,
* gamma = 10,
* delta = gamma + gammas_amount,
* epsilon
* }
*
* typedef std::tuple<...> processors;
*
* using challenges = std::array<, std::tuple_size<processors>>;
*
* }
* };
*/
template<fiat_shamir_heuristic_manifest Manifest, typename Hash>
class fiat_shamir_heuristic : public transcript<typename Manifest::transcript_manifest>{
class fiat_shamir_heuristic : public transcript<typename TManifest::transcript_manifest>{

typedef transcript<typename Manifest::transcript_manifest> transcript_type;
typedef transcript<typename TManifest::transcript_manifest> transcript_type;

template <typename Manifest::challenges_ids challenge_id>
bool set_challenge(std::tuple_element<challenge_id, typename Manifest::transcript_manifest::challenges> value){
return transcript_type::set_challenge(value);
template <typename TManifest::challenges_ids challenge_id>
bool set_challenge(std::tuple_element<challenge_id, typename TManifest::transcript_manifest::challenges> value){
return transcript_type::set_challenge<challenge_id>(value);
}

template <typename TManifest::challenges_ids challenge_id, std::size_t Index>
bool set_challenge(std::tuple_element<Index,
std::tuple_element<ChallengeId, typename TManifest::transcript_manifest::challenges>> value){
return transcript_type::set_challenge<challenge_id, Index>(value);
}

preprocessor::input_type input_value
Expand All @@ -77,25 +85,40 @@ namespace nil {
random_value = algebra::random_element<...>();
}

template <typename Manifest::challenges_ids challenge_id>
std::tuple_element<challenge_id, typename Manifest::transcript_manifest::challenges> get_challenge() const{
template <typename TManifest::challenges_ids challenge_id>
std::tuple_element<challenge_id, typename TManifest::transcript_manifest::challenges> get_challenge() const{
transcript_type::get_challenge<challenge_id>();
}

template <typename Manifest::challenges_ids challenge_id>
std::tuple_element<challenge_id, typename Manifest::processors>::result_type get_challenge_result(){
template <typename TManifest::challenges_ids ChallengeId, std::size_t Index>
std::tuple_element<Index,
std::tuple_element<ChallengeId, typename TManifest::transcript_manifest::challenges>> get_challenge() const{
transcript_type::get_challenge<challenge_id, Index>();
}

template <typename TManifest::challenges_ids challenge_id>
std::tuple_element<challenge_id, typename TManifest::processors>::result_type get_challenge_result(){

set_challenge<challenge_id>(hash<Hash>(get_challenge_result<challenge_id - 1>()));

transcript_type::get_challenge_result();
}

template <typename TManifest::challenges_ids challenge_id, std::size_t Index>
std::tuple_element<Index,
std::tuple_element<ChallengeId, typename TManifest::processors>>::result_type get_challenge_result(){

set_challenge<challenge_id, Index>(hash<Hash>(previous_result));

transcript_type::get_challenge_result<challenge_id, Index>();
}

template <>
std::tuple_element<challenge_id, typename Manifest::processors>::result_type get_challenge_result<0>(){
std::tuple_element<challenge_id, typename TManifest::processors>::result_type get_challenge_result<0>(){
return preprocessor(input_value, random_value);
}

typename Manifest::result export(){
typename TManifest::result export(){

}
};
Expand Down
117 changes: 89 additions & 28 deletions include/nil/crypto3/zk/snark/transcript/transcript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,114 @@ namespace nil {
namespace zk {
namespace snark {

struct transcript_manifest {
enum challenges_ids{

}

typedef std::tuple<...> challenges;

typedef std::tuple<...> processors;

typedef ... result;
}

/*!
* @brief Transcript policy. Assumed to be inherited by particular algorithms.
* @tparam TChallenges Enum of challenge IDs.
* @tparam TManifest Transcript Manifest in the following form:
*
*
* class transcript_manifest {
*
* std::size_t gammas_amount = 5;
*
* public:
* enum challenges_ids{
* alpha,
* beta,
* gamma = 10,
* delta = gamma + gammas_amount,
* epsilon
* }
*
* typedef std::tuple<...> challenges;
* typedef std::tuple<...> processors;
* };
*
* In the case above we have following list of challenges: (\alpha, \beta,
* \gamma_0, \gamma_1, \gamma_2, \gamma_3, \gamma_4, \delta, \varepsilon)
*
*/
template<transcript_manifest Manifest>
template<typename TManifest>
class transcript {

typename Manifest::challenges challenges;
typename TManifest::challenges challenges;

std::size_t next_challenge_id = 0;
public:

transcript (){}

transcript (std::tuple<> in_challenges): challenges(in_challenges){
transcript (std::tuple<> in_challenges): challenges(in_challenges){}

/*!
* @brief For ordinary challenges. \alpha
* @tparam ChallengeId Ordinary challenge ID. In the example above it's \alpha.
*
*/
template <typename TManifest::challenges_ids ChallengeId>
bool set_challenge(std::tuple_element<ChallengeId, typename TManifest::challenges> value){
std::get<ChallengeId>(challenges) = value;

return (ChallengeId == next_challenge_id++);
}

/*!
* @brief For indexed challenges. (\alpha_0, ..., \alpha_n)
* @tparam ChallengeId Indexed challenge ID. In the example above it's \alpha.
* @tparam Index Index of the particular challenge. In the example above it's \alpha_i.
*
*/
template <typename TManifest::challenges_ids ChallengeId, std::size_t Index>
bool set_challenge(std::tuple_element<Index,
std::tuple_element<ChallengeId, typename TManifest::challenges>> value){
std::get<Index>(std::get<ChallengeId>(challenges)) = value;

return (ChallengeId+Index == next_challenge_id++);
}

template <typename Manifest::challenges_ids challenge_id>
bool set_challenge(std::tuple_element<challenge_id, typename Manifest::challenges> value){
std::get<challenge_id>(challenges) = value;
/*!
* @brief For ordinary challenges. \alpha
* @tparam ChallengeId Ordinary challenge ID. In the example above it's \alpha.
*
*/
template <typename TManifest::challenges_ids ChallengeId>
std::tuple_element<ChallengeId, typename TManifest::challenges> get_challenge() const{
return std::get<ChallengeId>(challenges);
}

return (challenge_id == next_challenge_id++);
}
/*!
* @brief For indexed challenges. (\alpha_0, ..., \alpha_n)
* @tparam ChallengeId Indexed challenge ID. In the example above it's \alpha.
* @tparam Index Index of the particular challenge. In the example above it's \alpha_i.
*
*/
template <typename TManifest::challenges_ids ChallengeId, std::size_t Index>
std::tuple_element<Index,
std::tuple_element<ChallengeId, typename TManifest::challenges>> get_challenge() const{
return std::get<Index>(std::get<ChallengeId>(challenges));
}

template <typename Manifest::challenges_ids challenge_id>
std::tuple_element<challenge_id, typename Manifest::challenges> get_challenge() const{
return std::get<challenge_id>(challenges);
/*!
* @brief For ordinary challenges. \alpha
* @tparam ChallengeId Ordinary challenge ID. In the example above it's \alpha.
*
*/
template <typename TManifest::challenges_ids ChallengeId>
std::tuple_element<ChallengeId, typename TManifest::processors>::result_type get_challenge_result(){
return std::tuple_element<ChallengeId, typename TManifest::processors>(
get_challenge<ChallengeId>());
}

template <typename Manifest::challenges_ids challenge_id>
std::tuple_element<challenge_id, typename Manifest::processors>::result_type get_challenge_result(){
return std::tuple_element<challenge_id, typename Manifest::processors>(
std::get<challenge_id>(challenges));
/*!
* @brief For indexed challenges. (\alpha_0, ..., \alpha_n)
* @tparam ChallengeId Indexed challenge ID. In the example above it's \alpha.
* @tparam Index Index of the particular challenge. In the example above it's \alpha_i.
*
*/
template <typename TManifest::challenges_ids ChallengeId, std::size_t Index>
std::tuple_element<Index,
std::tuple_element<ChallengeId, typename TManifest::processors>>::result_type get_challenge_result(){
return std::tuple_element<Index, std::tuple_element<ChallengeId, typename TManifest::processors>>(
get_challenge<ChallengeId, Index>());
}
};
} // namespace snark
Expand Down

0 comments on commit 7fa02b3

Please sign in to comment.