Skip to content

Commit

Permalink
Initial refactoring implemented #318
Browse files Browse the repository at this point in the history
  • Loading branch information
nemothenoone committed Apr 22, 2024
1 parent dc5a738 commit 2688eb5
Show file tree
Hide file tree
Showing 26 changed files with 2,142 additions and 1,955 deletions.
6 changes: 3 additions & 3 deletions include/nil/crypto3/zk/snark/proof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {
template<typename ZkScheme>
class proof : public zk::proof<ZkScheme> {
typedef zk::proof<ZkScheme> policy_type;
template<typename ProofSystemType>
class proof : public zk::proof<ProofSystemType> {
typedef zk::proof<ProofSystemType> policy_type;

public:
typedef typename policy_type::scheme_type scheme_type;
Expand Down
4 changes: 2 additions & 2 deletions include/nil/crypto3/zk/snark/proving_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {
template<typename ZkScheme>
template<typename ProofSystemType>
class proving_key {
typedef ZkScheme scheme_type;
typedef ProofSystemType scheme_type;
};
} // namespace snark
} // namespace zk
Expand Down
92 changes: 49 additions & 43 deletions include/nil/crypto3/zk/snark/routing/as_waksman.hpp

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions include/nil/crypto3/zk/snark/systems/plonk/pickles/alphas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace nil {
namespace zk {
namespace snark {
enum argument_type;

template<typename FieldType>
struct Alphas {
/// The next power of alpha to use
Expand All @@ -49,16 +48,16 @@ namespace nil {
/// The powers of alpha: 1, alpha, alpha^2, etc.
/// If set to [Some], you can't register new constraints.
std::vector<typename FieldType::value_type> alphas;
std::unordered_map<argument_type, std::pair<int, int>> mapping;
std::unordered_map<argument_type, std::pair<int, int>> mapping;

Alphas() : next_power(0) {}
// Create alphas from 0 to next_power - 1

void register_(argument_type arg, int power){
if(mapping.find(arg) == mapping.end()){
void register_(argument_type arg, int power) {
if (mapping.find(arg) == mapping.end()) {
mapping[arg] = std::make_pair(next_power, power);
}

next_power += power;
}

Expand All @@ -76,7 +75,7 @@ namespace nil {

// Return num alphas
std::vector<typename FieldType::value_type> get_alphas(argument_type arg, std::size_t num) {
if(mapping.find(arg) == mapping.end()){
if (mapping.find(arg) == mapping.end()) {
assert(false);
}
std::pair<int, int> range = mapping[arg];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {

struct kimchi_constant {
constexpr static const std::size_t CHALLENGE_LENGTH_IN_LIMBS = 2;
constexpr static const std::size_t PERMUTES = 7;
Expand Down
23 changes: 12 additions & 11 deletions include/nil/crypto3/zk/snark/systems/plonk/pickles/constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ namespace nil {
namespace zk {
namespace snark {
template<typename FieldType>
struct ConstraintSystem{
struct ConstraintSystem {
typedef typename FieldType::value_type value_type;
// typedef proof_evaluation_type<value_type> proof_evaluation_type;

constexpr static const std::size_t CONSTRAINTS = 3;
constexpr static const std::size_t ZK_ROWS = 3;
constexpr static const std::size_t GENERIC_REGISTERS = 3;

static value_type perm_scalars(std::vector<proof_evaluation_type<value_type>>& e, value_type beta,
value_type& gamma, std::vector<value_type>& alphas,
value_type& zkp_zeta){
static value_type perm_scalars(std::vector<proof_evaluation_type<value_type>> &e, value_type beta,
value_type &gamma, std::vector<value_type> &alphas,
value_type &zkp_zeta) {
value_type res = e[1].z * beta * alphas.front() * zkp_zeta;
for(int i = 0; i < std::min(e[0].w.size(), e[0].s.size()); ++i){
for (int i = 0; i < std::min(e[0].w.size(), e[0].s.size()); ++i) {
res *= (gamma + (beta * e[0].s[i]) + e[0].w[i]);
}

return -res;
}

static void generic_gate(std::vector<value_type>& res, const value_type& alpha_pow,
const std::size_t register_offset, const value_type& generic_zeta,
const std::array<value_type, kimchi_constant::COLUMNS>& w_zeta){
static void generic_gate(std::vector<value_type> &res, const value_type &alpha_pow,
const std::size_t register_offset, const value_type &generic_zeta,
const std::array<value_type, kimchi_constant::COLUMNS> &w_zeta) {
value_type alpha_generic = alpha_pow * generic_zeta;

// addition
Expand All @@ -44,9 +44,10 @@ namespace nil {
// constant
res.push_back(alpha_generic);
}
static std::vector<value_type> gnrc_scalars(const std::vector<value_type>& alphas,
const std::array<value_type, kimchi_constant::COLUMNS>& w_zeta,
const value_type& generic_zeta){

static std::vector<value_type> gnrc_scalars(const std::vector<value_type> &alphas,
const std::array<value_type, kimchi_constant::COLUMNS> &w_zeta,
const value_type &generic_zeta) {
std::vector<value_type> res;

generic_gate(res, alphas[0], 0, generic_zeta, w_zeta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <nil/crypto3/zk/snark/systems/plonk/pickles/permutation.hpp>
#include <nil/crypto3/math/domains/basic_radix2_domain.hpp>
#include <nil/crypto3/math/polynomial/polynomial.hpp>

#include <map>
#include <array>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#ifndef CRYPTO3_ZK_KIMCHI_FUNCTIONS
#define CRYPTO3_ZK_KIMCHI_FUNCTIONS

namespace nil{
namespace crypto3{
namespace zk{
namespace snark{
template <typename CurveType>
namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {
template<typename CurveType>
struct kimchi_functions {
typedef typename CurveType::scalar_field_type scalar_field_type;
typedef typename CurveType::base_field_type base_field_type;

static typename scalar_field_type::value_type shift_scalar(const typename scalar_field_type::value_type& x){
static typename scalar_field_type::value_type
shift_scalar(const typename scalar_field_type::value_type &x) {
typename scalar_field_type::value_type two = typename scalar_field_type::value_type(2);
typename scalar_field_type::value_type two_pow = two.pow(scalar_field_type::modulus_bits);
if(scalar_field_type::modulus < base_field_type::modulus){
if (scalar_field_type::modulus < base_field_type::modulus) {
return (x - (two_pow + scalar_field_type::value_type::one())) / two;
} else{
} else {
return x - two_pow;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#include <nil/crypto3/zk/snark/systems/plonk/pickles/constants.hpp>

namespace nil{
namespace crypto3{
namespace zk{
namespace snark{
template <typename CurveType>
struct group_map{
namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {
template<typename CurveType>
struct group_map {
typedef typename CurveType::scalar_field_type scalar_field_type;
typedef typename CurveType::base_field_type base_field_type;
typedef typename CurveType::template g1_type<algebra::curves::coordinates::affine> group_type;
Expand All @@ -22,7 +22,7 @@ namespace nil{
value_type sqrt_neg_three_u_squared;
value_type inv_three_u_squared;

static value_type curve_eqn(value_type x){
static value_type curve_eqn(value_type x) {
value_type res = x;
res *= x;
res += a;
Expand All @@ -33,49 +33,50 @@ namespace nil{

group_map() {
u = value_type(1);
while(true){
while (true) {
fu = curve_eqn(u);
if(!fu.is_zero()){
if (!fu.is_zero()) {
break;
}
else{
} else {
++u;
}
}

value_type three_u_squared = value_type(3) * u.squared();
inv_three_u_squared = three_u_squared.inversed();
sqrt_neg_three_u_squared = (-three_u_squared).sqrt();
sqrt_neg_three_u_squared_minus_u_over_2 = (sqrt_neg_three_u_squared - u) * (value_type(2)).inversed();
sqrt_neg_three_u_squared_minus_u_over_2 =
(sqrt_neg_three_u_squared - u) * (value_type(2)).inversed();
}

std::array<value_type, 3> potential_xs_helper(value_type& t2, value_type& alpha){
value_type x1 = sqrt_neg_three_u_squared_minus_u_over_2 - t2.squared() * alpha * sqrt_neg_three_u_squared;
std::array<value_type, 3> potential_xs_helper(value_type &t2, value_type &alpha) {
value_type x1 = sqrt_neg_three_u_squared_minus_u_over_2 -
t2.squared() * alpha * sqrt_neg_three_u_squared;
value_type x2 = -u - x1;
value_type t2_plus_fu = t2 + fu;
value_type x3 = u - t2_plus_fu.squared() * alpha * t2_plus_fu * inv_three_u_squared;
return std::array<value_type, 3>({x1, x2, x3});
}

std::array<value_type, 3> potential_xs(value_type& t){
std::array<value_type, 3> potential_xs(value_type &t) {
value_type t2 = t.squared();
value_type alpha = ((t2 + fu) * t2).inversed();

return potential_xs_helper(t2, alpha);
}

typename group_type::value_type get_xy(value_type& t){
typename group_type::value_type get_xy(value_type &t) {
std::array<value_type, 3> xvec = potential_xs(t);
for(auto &x : xvec){
for (auto &x: xvec) {
value_type y = curve_eqn(x).sqrt();
if(y.squared() == x.pow(3) + a * x + b){
if (y.squared() == x.pow(3) + a * x + b) {
return typename group_type::value_type(x, y);
}
}
return typename group_type::value_type();
}

typename group_type::value_type to_group(value_type t){
typename group_type::value_type to_group(value_type t) {
return get_xy(t);
}
};
Expand Down Expand Up @@ -113,13 +114,14 @@ namespace nil{
return a * endo_coeff + b;
}

typename FieldType::value_type value(){
typename FieldType::value_type value() {
return _val;
}

ScalarChallenge(typename FieldType::value_type _val) : _val(_val) {}

ScalarChallenge() = default;

typename FieldType::value_type _val;
};
}
Expand Down
Loading

0 comments on commit 2688eb5

Please sign in to comment.