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

[BASE] Anemoi hash r1cs #65

Open
wants to merge 120 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
922ba99
Updated libff to point to last commit
Oct 24, 2022
2ba1b0d
added TAGS to .gitignore
Jun 24, 2022
43a9884
added BLS12-381 curve to CMakeLists.txt
Jun 24, 2022
3d34c06
added test for plonk in CMakeLists.txt
Jun 24, 2022
2d86e31
anemoi: added initial directory and files for the implementation of t…
Jul 27, 2022
819304c
anemoi: added initial test for Anemoi hash
Jul 27, 2022
cfec176
anmeoi: implemented r1cs gadget for the component y = alpha * x^3 + beta
Jul 28, 2022
d011427
anemoi: implemented r1cs gadget for the component y = alpha * x^2 + beta
Jul 29, 2022
076cee8
anemoi: major reimplementation of r1cs gadgets for anemoi power 2 and…
Aug 31, 2022
f7aa383
anemoi: constructors definition moved from .hpp to .tcc file
Sep 5, 2022
96ba7bb
anemoi: replaced constants names alpha, beta resp. by const_a, const_…
Sep 5, 2022
87fb680
anemoi: simplified the addition of r1cs constraints in the power 2 an…
Sep 5, 2022
b9a0e1b
anemoi: set constant private members const_a, const_b explicitly as c…
Sep 5, 2022
eebe993
anemoi: renamed anemoi_power_* gadgets to flystel_power_* which is te…
Sep 8, 2022
165a7e2
anemoi: added flystel power 5 gadget for opertion y=x^5
Sep 8, 2022
a92d59d
anemoi: added gadgets for the Qi and Qf components of the Flystel s-b…
Sep 8, 2022
0492bff
anemoi: added gadget for the flystel component for prime fields; chan…
Sep 14, 2022
d038da1
anemoi: started implementation of anemoi permutation gadget for prime…
Sep 15, 2022
9206d9b
anemoi: started specialization of the components (Q_gamma, Q_delta, F…
Sep 16, 2022
289d873
anemoi: improved specialization of anemoi components; updated anemoi …
Sep 19, 2022
456831c
anemoi: updated anemoi permutation class; added function to return th…
Sep 21, 2022
2c1048f
anemoi: added header file containing all round constants (initial)
Sep 21, 2022
7508a5b
anemoi: replaced inputs to Q_gamma, Q_delta, E from pb_variable to pb…
Sep 21, 2022
75a8cc5
anemoi: updated E_power_five gadget
Sep 22, 2022
18e08dc
anemoi: added a cnstructor of pb_linear_combination initializing an o…
Sep 23, 2022
4daa5ac
anemoi: updated gadget for flystel sbox using the new constructor of …
Sep 23, 2022
7cc1c11
WIP: draft chages to anemoi circuit
dtebbs Sep 27, 2022
4ef1d96
anemoi: implemented Flystel E five root transformation
Sep 29, 2022
64a2c69
anemoi: implemented new functionality to Flystel gadget for prime fie…
Sep 29, 2022
e1fe2c3
anemoi: debgging info for flystel gadget
Oct 4, 2022
429b178
anemoi: added temporary debug info in protoboard and pb_variable for …
Oct 4, 2022
9d0e3ba
anemoi: added more debug code for Flystel gadget
Oct 5, 2022
e32c518
anemoi: updated unit tests for the Flystel gadget
Oct 6, 2022
5073dba
anemoi: merged bug fix plus more debug tests
Oct 6, 2022
d8151e3
anemoi: replaced inputs to all gadgets from pb_linear_combination to …
Oct 7, 2022
88b4024
anemoi: added gadget for the flystel sbox plus unit test
Oct 7, 2022
b7dcb35
anemoi: removed debug comments from protoboard.tcc
Oct 10, 2022
377ea06
anemoi: completed unit test for flystel sbox; code cleanup
Oct 10, 2022
f247bfe
anemoi: transposed matrix M4 fixing a bug in the paper
Oct 17, 2022
58ff44a
anemoi: removed test for plonk in CMakeLists.txt
Oct 21, 2022
e3af3c2
anemoi: added latest revision of depends/libff
Oct 26, 2022
20340d2
anemoi: removed default initialization of input var annotation_prefix…
Oct 26, 2022
a43ace3
anemoi: removed printing of columns value in std::logic_error to fix …
Oct 26, 2022
4ab01e1
anemoi: made the declaration of the anemoi_permutation_round_prime_fi…
Oct 26, 2022
82032ee
anemoi: removed unused parameter pb in anemoi_permutation_round_prime…
Oct 26, 2022
e75dc3e
anemoi: removed unused parameter annotation_prefix from anemoi_permut…
Oct 26, 2022
cf73f03
anemoi: removed default value "" for annotation_prefix (cf. PR #65 ht…
Oct 31, 2022
3124b60
anemoi: set member variables representing intermediate values to be o…
Oct 31, 2022
c18735b
anemoi: updated copyright notice in all files
Oct 31, 2022
579c1a3
anemoi: added formal reference to the eprint version of the Anemoi pa…
Oct 31, 2022
e18fcc3
anemoi: removed redundant comment (https://github.com/clearmatics/lib…
Oct 31, 2022
f249b73
anemoi: added editorial changes in comments
Oct 31, 2022
99bfaa3
anemoi: changed comments in allocate() to match variable names (https…
Oct 31, 2022
853afc2
anemoi: uncommented test_pb_verify_circuit which was temporarily comm…
Oct 31, 2022
ff23014
anemoi: removed redundant asserts in pb_variable added earlier for de…
Oct 31, 2022
0a609c9
anemoi: removed redundant "== true" in if-statement (https://github.c…
Oct 31, 2022
e719051
anemoi: removed all printf-s (https://github.com/clearmatics/libsnark…
Nov 2, 2022
ab8832a
anemoi: clarified comment (https://github.com/clearmatics/libsnark/pu…
Nov 2, 2022
61b6f28
anemoi: added missing calls to test_pb_verify_circuit
Nov 2, 2022
e970f79
anemoi: added missing const identifier (https://github.com/clearmatic…
Nov 2, 2022
28d960e
anemoi: removed redundant comments
Nov 2, 2022
42f79e8
anemoi: removed redundant include (https://github.com/clearmatics/lib…
Nov 2, 2022
39f1eef
anemoi: switched to gtest framework (https://github.com/clearmatics/l…
Nov 2, 2022
e567f8a
anemoi: added script for generating Anemoi parameters for different c…
Oct 12, 2022
fc915a5
anemoi: added directory __pycache__ from anemoi sage scripts to .giti…
Oct 13, 2022
047f39d
anemoi: updated sage scripts with latest fixes from the official anem…
Oct 17, 2022
6269627
anemoi: added initial implementation of a parametrization class and i…
Oct 17, 2022
2c25f01
anemoi: created class with all anemoi parameters specialised by a giv…
Oct 19, 2022
567dfb6
anemoi: removed x,y from all comments since the input and output/resu…
Nov 4, 2022
777b44a
anemoi: removed redundant comment
Nov 4, 2022
503c47f
anemoi: introduced BignumT type in anemoi_parameters class; removed u…
Nov 4, 2022
079bcf6
anemoi: moved the initialization of the members of the anemoi_paramet…
Nov 4, 2022
04e08d8
anemoi: removed specialization of debug_parameters class (https://git…
Nov 4, 2022
912d156
anemoi: replaced types of members of class anemoi_parameters that hav…
Nov 4, 2022
7df5c34
anemoi: changed types of debug_parameters_bls12_381 parameters class …
Nov 8, 2022
1b6ca9a
anemoi: added static_assert in flystel_prime_field_gadget to make sur…
Nov 8, 2022
fe54373
anemoi: added ppT to be used as a template parameter everywhere inste…
Nov 8, 2022
785f2e0
anemoi: added definition of ppT in the paremeters and debug_parameter…
Nov 8, 2022
05faa47
anemoi: updated license text (https://github.com/clearmatics/libsnark…
Nov 8, 2022
645cfc2
anemoi: removed the copy of the official Anemoi SAGE implementation f…
Nov 8, 2022
8dedaa2
Merge pull request #76 from clearmatics/anemoi-hash-r1cs-parametrization
dtebbs Nov 9, 2022
cac4bb1
anemoi: removed duplicate gadgets for the flystel Q-functions for pri…
Nov 3, 2022
6e68d79
Merge pull request #85 from clearmatics/anemoi-hash-r1cs-duplicate-ga…
dtebbs Nov 10, 2022
8a6e635
anemoi: added static_assert on the allowed dimensions of the mds matr…
Nov 4, 2022
a008b47
Merge pull request #80 from clearmatics/anemoi-hash-r1cs-mds-matrix
dtebbs Nov 10, 2022
d0f143f
anemoi: added r1cs gadget for the round transformation of the anemoi …
Nov 8, 2022
fe1fb91
anemoi: removed redundant debug code
Dec 15, 2022
502ef71
anemoi: added proper fomatting of annotation prefix string (https://g…
Jan 9, 2023
7584a1a
anemoi: replaced method push_back with emplace_back (https://github.c…
Jan 9, 2023
c9fab73
anemoi: removed unnecessary guards from cpp file (https://github.com/…
Jan 9, 2023
759cfea
anemoi: updated sage script for generating anemoi parameters to handl…
Jan 11, 2023
fab27a7
anemoi: added file with all parameters for different instances of ane…
Jan 11, 2023
cd8616d
anemoi: updated parameters script
Jan 11, 2023
2bc1e60
anemoi: removed parameters.txt file; its content is generated by the …
Jan 11, 2023
928ba7b
anemoi: added the original round constants instead of just adding zer…
Jan 11, 2023
b8c82f6
anemoi: fixed the use of emplace_back to directly call the constructo…
Jan 12, 2023
f744943
anemoi: fixed improper use of types in anemoi_parameters class (https…
Jan 13, 2023
ceb06f4
anemoi: replaced libff::bigint<libff::Fr<libff::bls12_381_pp>::num_li…
Jan 16, 2023
8a455ea
Merge pull request #101 from clearmatics/anemoi-hash-r1cs-permutation…
dtebbs Jan 16, 2023
5ac46aa
anemoi: removed duplicate gadgets for the flystel Q-functions for pri…
Jan 12, 2023
3592255
anemoi: implemented function for generating the mds matrix for differ…
Jan 16, 2023
c84efa4
anemoi: added a TODO note regarding the removal of the input g parame…
Jan 17, 2023
85bb953
anemoi: defined the types of the mds matrices (https://github.com/cle…
Jan 17, 2023
2c62e4d
anemoi: fixed compilation errors related to method anemoi_permutation…
Jan 17, 2023
eefd98d
Merge pull request #102 from clearmatics/anemoi-hash-r1cs-numstatecol…
dtebbs Jan 23, 2023
0fdd0c3
Merge branch 'develop' into anemoi-hash-r1cs
Jan 25, 2023
8a77138
anemoi: added support for generating anemoi parameters for all curves…
Jan 19, 2023
5bb5dec
anemoi: added description of the C,D round constants in comments
Jan 19, 2023
3e3fb66
WIP: anemoi test tweaks
dtebbs Jan 25, 2023
bb7fee5
anemoi: added tests for gadget instatiations for all curves (https://…
Jan 26, 2023
bbafbdd
anemoi: removed test_curve_parameters as obsolete now that we have te…
Feb 3, 2023
9e56d98
Merge pull request #106 from clearmatics/82-anemoi-parameters-all-curves
dtebbs Feb 3, 2023
644dde8
anemoi: added the number of rounds for each instance and for each cur…
Jan 27, 2023
f2e9f87
anemoi: implemented gadget for the full anemoi permutation (https://g…
Jan 27, 2023
5ebc5b3
anemoi: renamed NumStateColumns_L to NumStateColumns
Feb 8, 2023
2099655
anemoi: renamed test_anemoi_permutation_round_prime_field_gadget to t…
Feb 8, 2023
e7630b2
anemoi: added the round index as a member of the anemoi_round_prime_f…
Feb 8, 2023
bb2eff3
anemoi: added output test values for anemoi with 128-bit security; ou…
Feb 22, 2023
af1afbb
anemoi: added minor variable renaming Round -> Rounds (https://github…
Mar 6, 2023
42aeac3
Merge pull request #110 from clearmatics/104-anemoi-permutation-all-r…
dtebbs Mar 6, 2023
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ libsnark/zk_proof_systems/zksnark/ram_zksnark/tests/test_ram_zksnark

build
*~
TAGS
.dir-locals.el
scripts/anemoi-hash/__pycache__
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ set(
"BN128"
CACHE
STRING
"Default curve: one of ALT_BN128, BN128, EDWARDS, MNT4, MNT6"
"Default curve: one of ALT_BN128, BN128, EDWARDS, MNT4, MNT6, BLS12_381"
)

option(
Expand Down
2 changes: 1 addition & 1 deletion depends/libff
Submodule libff updated 40 files
+1 −0 .gitignore
+1 −1 CMakeLists.txt
+2 −0 libff/algebra/curves/alt_bn128/alt_bn128_pp.cpp
+2 −0 libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp
+2 −0 libff/algebra/curves/bls12_377/bls12_377_pp.cpp
+2 −0 libff/algebra/curves/bls12_377/bls12_377_pp.hpp
+10 −0 libff/algebra/curves/bls12_381/README.md
+68 −0 libff/algebra/curves/bls12_381/bls12_381.sage
+464 −0 libff/algebra/curves/bls12_381/bls12_381_g1.cpp
+113 −0 libff/algebra/curves/bls12_381/bls12_381_g1.hpp
+486 −0 libff/algebra/curves/bls12_381/bls12_381_g2.cpp
+120 −0 libff/algebra/curves/bls12_381/bls12_381_g2.hpp
+628 −0 libff/algebra/curves/bls12_381/bls12_381_init.cpp
+63 −0 libff/algebra/curves/bls12_381/bls12_381_init.hpp
+532 −0 libff/algebra/curves/bls12_381/bls12_381_pairing.cpp
+117 −0 libff/algebra/curves/bls12_381/bls12_381_pairing.hpp
+52 −0 libff/algebra/curves/bls12_381/bls12_381_pp.cpp
+54 −0 libff/algebra/curves/bls12_381/bls12_381_pp.hpp
+2 −0 libff/algebra/curves/bn128/bn128_pp.cpp
+2 −0 libff/algebra/curves/bn128/bn128_pp.hpp
+2 −0 libff/algebra/curves/bw6_761/bw6_761_pp.cpp
+2 −0 libff/algebra/curves/bw6_761/bw6_761_pp.hpp
+17 −1 libff/algebra/curves/curve_serialization.tcc
+2 −0 libff/algebra/curves/curve_utils.tcc
+2 −0 libff/algebra/curves/edwards/edwards_pp.cpp
+2 −0 libff/algebra/curves/edwards/edwards_pp.hpp
+2 −0 libff/algebra/curves/mnt/mnt4/mnt4_pp.cpp
+2 −0 libff/algebra/curves/mnt/mnt4/mnt4_pp.hpp
+2 −0 libff/algebra/curves/mnt/mnt6/mnt6_pp.cpp
+2 −0 libff/algebra/curves/mnt/mnt6/mnt6_pp.hpp
+3 −1 libff/algebra/curves/public_params.hpp
+8 −1 libff/algebra/curves/tests/test_bilinearity.cpp
+47 −1 libff/algebra/curves/tests/test_groups.cpp
+3 −0 libff/algebra/fields/field_utils.hpp
+111 −14 libff/algebra/fields/field_utils.tcc
+34 −0 libff/algebra/fields/fp12_2over3over2.hpp
+35 −0 libff/algebra/fields/fp12_2over3over2.tcc
+31 −0 libff/algebra/fields/fp6_3over2.hpp
+19 −6 libff/algebra/fields/tests/test_fields.cpp
+36 −28 libff/algebra/scalar_multiplication/tests/test_multiexp.cpp
1 change: 1 addition & 0 deletions libsnark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ if ("${IS_LIBSNARK_PARENT}")
libsnark_test(test_r1cs_ppzksnark_verifier_gadget gadgetlib1/tests/test_r1cs_ppzksnark_verifier_gadget.cpp)
libsnark_test(test_r1cs_gg_ppzksnark_verifier_gadget gadgetlib1/tests/test_r1cs_gg_ppzksnark_verifier_gadget.cpp)
libsnark_test(test_kzg10_verifier_gadget gadgetlib1/tests/test_kzg10_verifier_gadget.cpp)
libsnark_test(test_anemoi_gadget gadgetlib1/gadgets/hashes/anemoi/tests/anemoi_outputs.cpp gadgetlib1/gadgets/hashes/anemoi/tests/test_anemoi_gadget.cpp)

# TODO (howardwu): Resolve runtime on targets:
# libsnark_test(zk_proof_systems_uscs_ppzksnark_test zk_proof_systems/ppzksnark/uscs_ppzksnark/tests/test_uscs_ppzksnark.cpp)
Expand Down
307 changes: 307 additions & 0 deletions libsnark/gadgetlib1/gadgets/hashes/anemoi/anemoi_components.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
/** @file
*****************************************************************************
* @author This file is part of libsnark, developed by Clearmatics Ltd
* (originally developed by SCIPR Lab) and contributors
* (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#ifndef LIBSNARK_GADGETLIB1_GADGETS_HASHES_ANEMOI_COMPONENTS_HPP_
#define LIBSNARK_GADGETLIB1_GADGETS_HASHES_ANEMOI_COMPONENTS_HPP_

#include "libsnark/gadgetlib1/gadgets/hashes/anemoi/anemoi_parameters.hpp"

#include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>

/// Implementation of the Anenoi arithmetization-oriented hash function
///
/// Reference:
/// - \[BBCPSVW22]:
/// Title: "New Design Techniques for Efficient
/// Arithmetization-Oriented Hash Functions: Anemoi Permutations and
/// Jive Compression Mode", Clemence Bouvier, Pierre Briaud, Pyrros
/// Chaidos, Leo Perrin, Robin Salen, Vesselin Velichkov, Danny
/// Willems, Cryptology ePrint Archive, Report 2022/840, 2019,
/// <https://eprint.iacr.org/2022/840>

namespace libsnark
{

/// Combined gadget for the Flystel Q-functions for prime fields Q(x) = A x^2 +
/// B:
/// Q_gamma(x) = beta x^2 + gamma: A = beta, B = gamma
/// Q_delta(x) = beta x^2 + delta: A = beta, B = delta
template<typename ppT>
class flystel_Q_prime_field_gadget : public gadget<libff::Fr<ppT>>
{
using FieldT = libff::Fr<ppT>;

private:
const FieldT A;
const FieldT B;

public:
const linear_combination<FieldT> input;
const pb_variable<FieldT> output;

flystel_Q_prime_field_gadget(
protoboard<FieldT> &pb,
const FieldT A,
const FieldT B,
const linear_combination<FieldT> &input,
const pb_variable<FieldT> &output,
const std::string &annotation_prefix);

void generate_r1cs_constraints();
void generate_r1cs_witness();
};

/// Combined gadget for the Flystel Q-functions for binary fields Q(x) = A x^3 +
/// B:
/// Q_gamma(x) = beta x^3 + gamma: A = beta, B = gamma
/// Q_delta(x) = beta x^3 + delta: A = beta, B = delta
template<typename ppT>
class flystel_Q_binary_field_gadget : public gadget<libff::Fr<ppT>>
{
using FieldT = libff::Fr<ppT>;

private:
const pb_variable<FieldT> internal;
const FieldT A;
const FieldT B;

public:
const linear_combination<FieldT> input;
const pb_variable<FieldT> output;

flystel_Q_binary_field_gadget(
protoboard<FieldT> &pb,
const FieldT A,
const FieldT B,
const linear_combination<FieldT> &input,
const pb_variable<FieldT> &output,
const std::string &annotation_prefix);

void generate_r1cs_constraints();
void generate_r1cs_witness();
};

/// Compute y = x^5
template<typename ppT>
class flystel_E_power_five_gadget : public gadget<libff::Fr<ppT>>
{
using FieldT = libff::Fr<ppT>;

private:
// internal (i.e. intermediate) variables
const pb_variable<FieldT> a0;
const pb_variable<FieldT> a1;

public:
const linear_combination<FieldT> input;
const pb_variable<FieldT> output;

flystel_E_power_five_gadget(
protoboard<FieldT> &pb,
const linear_combination<FieldT> &input,
const pb_variable<FieldT> &output,
const std::string &annotation_prefix);

void generate_r1cs_constraints();
void generate_r1cs_witness();
};

/// Compute y = x^1/5, x=input, y=output/result
template<typename ppT, class parameters = anemoi_parameters<libff::Fr<ppT>>>
class flystel_E_root_five_gadget : public gadget<libff::Fr<ppT>>
{
using FieldT = libff::Fr<ppT>;

private:
// internal (i.e. intermediate) variables
const pb_variable<FieldT> a0;
const pb_variable<FieldT> a1;

public:
const linear_combination<FieldT> input;
const pb_variable<FieldT> output;

flystel_E_root_five_gadget(
protoboard<FieldT> &pb,
const linear_combination<FieldT> &input,
const pb_variable<FieldT> &output,
const std::string &annotation_prefix);

void generate_r1cs_constraints();
void generate_r1cs_witness();
};

/// Anemoi closed Flystel component for fields of prime characteristic
///
/// x0,x1: input (x,y in [BBCPSVW22])
/// y0,y1: output (u,v in [BBCPSVW22])
///
/// The component performs the following computation:
///
/// a0 = (beta x1^2 + gamma) == Q_gamma(x1)
/// a1 = (x0 - a0)^{1/alpha} == E_root_five(x0-a0)
/// a2 = beta (x1-a1)^2 + delta == Q_delta(x1-a1)
/// y0 = x0 - a0 + a2
/// y1 = x1 - a1
///
/// \note: in [BBCPSVW22] (x0,x1)->(y0,y1) is denoted with (x,y)->(u,v)
template<typename ppT, class parameters = anemoi_parameters<libff::Fr<ppT>>>
class flystel_prime_field_gadget : public gadget<libff::Fr<ppT>>
{
using FieldT = libff::Fr<ppT>;

private:
// internal (i.e. intermediate) variables
const pb_variable<FieldT> a0;
const pb_variable<FieldT> a1;
const pb_variable<FieldT> a2;

public:
const linear_combination<FieldT> input_x0;
const linear_combination<FieldT> input_x1;
const pb_variable<FieldT> output_y0;
const pb_variable<FieldT> output_y1;

flystel_Q_prime_field_gadget<ppT> Q_gamma;
flystel_Q_prime_field_gadget<ppT> Q_delta;
flystel_E_root_five_gadget<ppT, parameters> E_root_five;

flystel_prime_field_gadget(
protoboard<FieldT> &pb,
const linear_combination<FieldT> &x0,
const linear_combination<FieldT> &x1,
const pb_variable<FieldT> &y0,
const pb_variable<FieldT> &y1,
const std::string &annotation_prefix);

void generate_r1cs_constraints();
void generate_r1cs_witness();
};

/// One round of the Anemoi permutation mapping (Fr)^{2L} -> (Fr)^{2L}
///
/// NumStateColumns : L parameter - number of columns in the
/// state. can be 1,2,3,4. Each column is composed
/// of 2 elements in Fr. One Flystel Sbox accepts
/// 1 column as input. There are L Flystel-s in 1
/// round of the Anemoi permutation applied in
/// parallel.
template<
typename ppT,
size_t NumStateColumns,
class parameters = anemoi_parameters<libff::Fr<ppT>>>
class anemoi_round_prime_field_gadget : public gadget<libff::Fr<ppT>>
{
using FieldT = libff::Fr<ppT>;

private:
// The index of the round within the full Anemoi permutation
// (composed of multiple rounds iterated in a sequence). It is
// used to derive the round constants C,D.
size_t round_index;
// matrix M
std::vector<std::vector<FieldT>> M_matrix;
// vector of Flystel S-boxes
std::vector<flystel_prime_field_gadget<ppT, parameters>> Flystel;

public:
const pb_linear_combination_array<FieldT> X_left_input;
const pb_linear_combination_array<FieldT> X_right_input;
const pb_variable_array<FieldT> Y_left_output;
const pb_variable_array<FieldT> Y_right_output;

anemoi_round_prime_field_gadget(
protoboard<FieldT> &pb,
const size_t &round_index,
const pb_linear_combination_array<FieldT> &X_left_input,
const pb_linear_combination_array<FieldT> &X_right_input,
const pb_variable_array<FieldT> &Y_left_output,
const pb_variable_array<FieldT> &Y_right_output,
const std::string &annotation_prefix);

void anemoi_get_round_constants(
const size_t &iround, std::vector<FieldT> &C, std::vector<FieldT> &D);

void generate_r1cs_constraints();
void generate_r1cs_witness();
};

// MDS matrix for each allowed dimension: 2,3 or 4
template<typename ppT, size_t NumStateColumns> class anemoi_permutation_mds;

template<typename ppT> class anemoi_permutation_mds<ppT, 2>
{
using anemoi_mds_matrix_t = std::array<std::array<libff::Fr<ppT>, 2>, 2>;

public:
static anemoi_mds_matrix_t permutation_mds(const libff::Fr<ppT> g);
};

template<typename ppT> class anemoi_permutation_mds<ppT, 3>
{
using anemoi_mds_matrix_t = std::array<std::array<libff::Fr<ppT>, 3>, 3>;

public:
static anemoi_mds_matrix_t permutation_mds(const libff::Fr<ppT> g);
};

template<typename ppT> class anemoi_permutation_mds<ppT, 4>
{
using anemoi_mds_matrix_t = std::array<std::array<libff::Fr<ppT>, 4>, 4>;

public:
static anemoi_mds_matrix_t permutation_mds(const libff::Fr<ppT> g);
};

/// Full Anemoi permutation mapping (Fr)^{2L} -> (Fr)^{2L}
/// see anemoi_round_prime_field_gadget
template<
typename ppT,
size_t NumStateColumns,
bool b_sec128,
class parameters = anemoi_parameters<libff::Fr<ppT>>>
class anemoi_permutation_prime_field_gadget : public gadget<libff::Fr<ppT>>
{
using FieldT = libff::Fr<ppT>;

private:
// C round constants for all rounds
std::vector<std::vector<FieldT>> C_const_vec;
// D round constants for all rounds
std::vector<std::vector<FieldT>> D_const_vec;
// vector of round gadgets
std::vector<
anemoi_round_prime_field_gadget<ppT, NumStateColumns, parameters>>
Rounds;

public:
const pb_linear_combination_array<FieldT> X_left_input;
const pb_linear_combination_array<FieldT> X_right_input;
const pb_variable_array<FieldT> Y_left_output;
const pb_variable_array<FieldT> Y_right_output;

anemoi_permutation_prime_field_gadget(
protoboard<FieldT> &pb,
// TODO: remove constants
const std::vector<std::vector<FieldT>> &C_const,
const std::vector<std::vector<FieldT>> &D_const,
const pb_linear_combination_array<FieldT> &X_left_input,
const pb_linear_combination_array<FieldT> &X_right_input,
const pb_variable_array<FieldT> &Y_left_output,
const pb_variable_array<FieldT> &Y_right_output,
const std::string &annotation_prefix);

void generate_r1cs_constraints();
void generate_r1cs_witness();
};

} // namespace libsnark

#include "libsnark/gadgetlib1/gadgets/hashes/anemoi/anemoi_components.tcc"

#endif // LIBSNARK_GADGETLIB1_GADGETS_HASHES_ANEMOI_COMPONENTS_HPP_
Loading