Skip to content

Commit

Permalink
Changes related to changes in multiprecision.
Browse files Browse the repository at this point in the history
  • Loading branch information
martun committed May 10, 2024
1 parent 1297637 commit 358be60
Show file tree
Hide file tree
Showing 30 changed files with 4,100 additions and 4,085 deletions.
1 change: 0 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
crypto3_zk_systems_plonk_placeholder_placeholder_test
crypto3_zk_commitment_powers_of_tau_test
crypto3_zk_commitment_proof_of_knowledge_test
crypto3_zk_commitment_r1cs_gg_ppzksnark_mpc_test
crypto3_zk_math_expression_test
crypto3_zk_systems_plonk_plonk_constraint_test
crypto3_zk_commitment_proof_of_knowledge_test
Expand Down
4 changes: 2 additions & 2 deletions include/nil/crypto3/zk/commitments/batched_commitment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ namespace nil {
math::polynomial<typename field_type::value_type> get_V(
const std::vector<typename field_type::value_type> &points) const {

math::polynomial<typename field_type::value_type> V = {1};
math::polynomial<typename field_type::value_type> V = {{field_type::value_type::one()}};
for( std::size_t xi_index = 0; xi_index < points.size(); xi_index++ ){
V *= math::polynomial<typename field_type::value_type>({-points[xi_index], 1});
V *= math::polynomial<typename field_type::value_type>({-points[xi_index], field_type::value_type::one()});
}
return V;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ namespace nil {
}

//Calculate combinedQ values
typename FRI::field_type::value_type theta_acc(1);
typename FRI::field_type::value_type theta_acc = FRI::field_type::value_type::one();
typename FRI::polynomial_values_type y;
typename FRI::polynomial_values_type combined_eval_values;
y.resize(coset_size / FRI::m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <nil/crypto3/algebra/type_traits.hpp>

#include <nil/crypto3/multiprecision/number.hpp>
#include <boost/multiprecision/number.hpp>

namespace nil {
namespace crypto3 {
Expand Down Expand Up @@ -173,19 +173,19 @@ namespace nil {
template<typename Type1,
typename Type2,
typename Backend,
multiprecision::expression_template_option ExpressionTemplates>
element_kc<Type1, Type2> operator*(const multiprecision::number <Backend, ExpressionTemplates> &lhs,
boost::multiprecision::expression_template_option ExpressionTemplates>
element_kc<Type1, Type2> operator*(const boost::multiprecision::number <Backend, ExpressionTemplates> &lhs,
const element_kc<Type1, Type2> &rhs) {
return element_kc<Type1, Type2>(lhs * rhs.g, lhs * rhs.h);
}

template<typename Type1,
typename Type2,
typename Backend,
multiprecision::expression_template_option ExpressionTemplates>
boost::multiprecision::expression_template_option ExpressionTemplates>
element_kc<Type1, Type2>
operator*(const element_kc<Type1, Type2> &lhs,
const multiprecision::number <Backend, ExpressionTemplates> &rhs) {
const boost::multiprecision::number <Backend, ExpressionTemplates> &rhs) {
return element_kc<Type1, Type2>(rhs * lhs.g, rhs * lhs.h);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace nil {

std::size_t d = f.degree();
if (d % 2 == 0) {
f.push_back(0);
f.push_back(FieldType::value_type::zero());
d++;
}
math::polynomial<typename FieldType::value_type> f_folded(d / 2 + 1);
Expand All @@ -78,14 +78,14 @@ namespace nil {
math::polynomial_dfs<typename FieldType::value_type> f_folded(
domain->size() / 2 - 1, domain->size() / 2, FieldType::value_type::zero());

typename FieldType::value_type two_inversed = 2;
typename FieldType::value_type two_inversed = 2u;
two_inversed = two_inversed.inversed();
typename FieldType::value_type omega_inversed = domain->get_domain_element(domain->size() - 1);

typename FieldType::value_type acc = alpha;

for (std::size_t i = 0; i <= f_folded.degree(); i++) {
f_folded[i] = two_inversed * ((1 + acc) * f[i] + (1 - acc) * f[domain->size() / 2 + i]);
f_folded[i] = two_inversed * ((1u + acc) * f[i] + (1u - acc) * f[domain->size() / 2 + i]);
acc *= omega_inversed;
}

Expand Down
7 changes: 4 additions & 3 deletions include/nil/crypto3/zk/commitments/polynomial/fri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ namespace nil {
typename FRI::basic_fri::transcript_type &transcript = typename FRI::basic_fri::transcript_type()
) {
std::map<std::size_t, typename FRI::basic_fri::commitment_type> t_roots; t_roots[0] = {t_root};
std::vector<std::vector<std::tuple<std::size_t, std::size_t>>> evals_map(1); evals_map[0] = {{0,0}};
std::vector<std::vector<std::tuple<std::size_t, std::size_t>>> evals_map(1);
evals_map[0] = {{0u,0u}};

std::vector<typename FRI::field_type::value_type> combined_U = {0};
std::vector<math::polynomial<typename FRI::field_type::value_type>> combined_V = {{1}};
std::vector<typename FRI::field_type::value_type> combined_U = {{FRI::field_type::value_type::zero()}};
std::vector<math::polynomial<typename FRI::field_type::value_type>> combined_V = {{FRI::field_type::value_type::one()}};

return verify_eval<typename FRI::basic_fri>(
proof, fri_params, t_roots,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ namespace nil {
namespace zk {
namespace commitments {
template<typename T1, typename T2, typename Backend,
multiprecision::expression_template_option ExpressionTemplates>
boost::multiprecision::expression_template_option ExpressionTemplates>
typename knowledge_commitment<T1, T2>::value_type
opt_window_wnaf_exp(const typename knowledge_commitment<T1, T2>::value_type &base,
const multiprecision::number<Backend, ExpressionTemplates> &scalar,
const boost::multiprecision::number<Backend, ExpressionTemplates> &scalar,
const std::size_t scalar_bits) {
return typename knowledge_commitment<T1, T2>::value_type(
opt_window_wnaf_exp(base.g, scalar, scalar_bits),
Expand Down
34 changes: 20 additions & 14 deletions include/nil/crypto3/zk/commitments/polynomial/kzg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,14 @@ namespace nil {
const typename math::polynomial<typename KZG::scalar_value_type> &f,
typename KZG::scalar_value_type z) {

const typename math::polynomial<typename KZG::scalar_value_type> denominator_polynom = {-z, 1};
// We need two scopes on the next line to force it to use the initializer list version,
// not another constructor with 2 params.
const typename math::polynomial<typename KZG::scalar_value_type> denominator_polynom = {{-z, KZG::scalar_value_type::one()}};

typename math::polynomial<typename KZG::scalar_value_type> q = f;
typename math::polynomial<typename KZG::scalar_value_type> q(f);
q[0] -= f.evaluate(z);
auto r = q % denominator_polynom;
if (r != typename KZG::scalar_value_type(0)) {
if (!r.is_zero()) {
throw std::runtime_error("incorrect eval or point z");
}
q /= denominator_polynom;
Expand Down Expand Up @@ -516,9 +518,9 @@ namespace nil {
static typename math::polynomial<typename KZG::scalar_value_type>
create_polynom_by_zeros(const std::vector<typename KZG::scalar_value_type> S) {
assert(S.size() > 0);
typename math::polynomial<typename KZG::scalar_value_type> Z = {-S[0], 1};
typename math::polynomial<typename KZG::scalar_value_type> Z = {{-S[0], KZG::scalar_value_type::one()}};
for (std::size_t i = 1; i < S.size(); ++i) {
Z *= typename math::polynomial<typename KZG::scalar_value_type>({-S[i], 1});
Z *= typename math::polynomial<typename KZG::scalar_value_type>({-S[i], KZG::scalar_value_type::one()});
}
return Z;
}
Expand All @@ -538,7 +540,8 @@ namespace nil {
std::vector<typename KZG::scalar_value_type> result;
std::set_difference(T.begin(), T.end(), S.begin(), S.end(), std::back_inserter(result));
if (result.size() == 0) {
return typename math::polynomial<typename KZG::scalar_value_type>({{1}});
return typename math::polynomial<typename KZG::scalar_value_type>(
{{KZG::scalar_value_type::one()}});
}
return create_polynom_by_zeros<KZG>(result);
}
Expand Down Expand Up @@ -566,10 +569,11 @@ namespace nil {
auto spare_poly = polys[i] - public_key.r[i];
auto denom = create_polynom_by_zeros<KZG>(public_key.S[i]);
for (auto s : public_key.S[i]) {
assert(spare_poly.evaluate(s) == 0);
assert(denom.evaluate(s) == 0);
assert(spare_poly.evaluate(s).is_zero());
assert(denom.evaluate(s).is_zero());
}
assert(spare_poly % denom == typename math::polynomial<typename KZG::scalar_value_type>({{0}}));
assert(spare_poly % denom == typename math::polynomial<typename KZG::scalar_value_type>(
{{KZG::scalar_value_type::zero()}}));
spare_poly /= denom;
accum += spare_poly * factor;
factor *= gamma;
Expand Down Expand Up @@ -681,7 +685,8 @@ namespace nil {
std::vector<typename KZGScheme::scalar_value_type> result;
std::set_difference(merged_points.begin(), merged_points.end(), points.begin(), points.end(), std::back_inserter(result));
if (result.size() == 0) {
return typename math::polynomial<typename KZGScheme::scalar_value_type>({{1}});
return typename math::polynomial<typename KZGScheme::scalar_value_type>(
{{KZGScheme::scalar_value_type::one()}});
}
BOOST_ASSERT(this->get_V(result) * this->get_V(points) == this->get_V(merged_points));
return this->get_V(result);
Expand All @@ -700,8 +705,8 @@ namespace nil {
);

// Push evaluation points to transcript
for( std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++){
for( std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++ ) {
for(std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++) {
for(std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++) {
nil::marshalling::status_type status;
std::vector<uint8_t> byteblob =
nil::marshalling::pack<endianness>(this->_z.get(batch_ind, i, j), status);
Expand Down Expand Up @@ -744,7 +749,7 @@ namespace nil {
this->_ind_commitments[index] = {};
this->state_commited(index);

std::vector<std::uint8_t> result = {};
std::vector<std::uint8_t> result;
for (std::size_t i = 0; i < this->_polys[index].size(); ++i) {
BOOST_ASSERT(this->_polys[index][i].degree() <= _params.commitment_key.size());
auto single_commitment = nil::crypto3::zk::algorithms::commit_one<KZGScheme>(_params, this->_polys[index][i]);
Expand Down Expand Up @@ -779,7 +784,8 @@ namespace nil {

auto gamma = transcript.template challenge<typename KZGScheme::curve_type::scalar_field_type>();
auto factor = KZGScheme::scalar_value_type::one();
typename math::polynomial<typename KZGScheme::scalar_value_type> accum = {0};
typename math::polynomial<typename KZGScheme::scalar_value_type> accum =
{{KZGScheme::scalar_value_type::zero()}};

for( auto const &it: this->_polys ){
auto k = it.first;
Expand Down
5 changes: 3 additions & 2 deletions include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ namespace nil {
std::vector<typename KZGScheme::scalar_value_type> result;
std::set_difference(merged_points.begin(), merged_points.end(), points.begin(), points.end(), std::back_inserter(result));
if (result.size() == 0) {
return typename math::polynomial<typename KZGScheme::scalar_value_type>({{1}});
return typename math::polynomial<typename KZGScheme::scalar_value_type>(
{{KZGScheme::scalar_value_type::one()}});
}
BOOST_ASSERT(this->get_V(result) * this->get_V(points) == this->get_V(merged_points));
return this->get_V(result);
Expand Down Expand Up @@ -272,7 +273,7 @@ namespace nil {
);

auto theta_2 = transcript.template challenge<typename curve_type::scalar_field_type>();
math::polynomial<typename KZGScheme::scalar_value_type> theta_2_vanish = { -theta_2, 1 };
math::polynomial<typename KZGScheme::scalar_value_type> theta_2_vanish = {{ -theta_2, KZGScheme::scalar_value_type::one() }};

theta_i = KZGScheme::scalar_value_type::one();

Expand Down
14 changes: 7 additions & 7 deletions include/nil/crypto3/zk/commitments/polynomial/lpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace nil {

public:
lpc_commitment_scheme(const typename fri_type::params_type &fri_params)
: _fri_params(fri_params), _etha(0) {
: _fri_params(fri_params), _etha(0u) {
}

preprocessed_data_type preprocess(transcript_type& transcript) const{
Expand Down Expand Up @@ -123,15 +123,15 @@ namespace nil {

// Prepare z-s and combined_Q;
auto theta = transcript.template challenge<field_type>();
typename field_type::value_type theta_acc(1);
typename field_type::value_type theta_acc = field_type::value_type::one();
poly_type combined_Q;
math::polynomial<value_type> V;

auto points = this->get_unique_points();
math::polynomial<value_type> combined_Q_normal;

for (auto const &point: points){
V = {-point, 1};
V = {-point, 1u};
math::polynomial<value_type> Q_normal;
for(std::size_t i: this->_z.get_batches()){
for(std::size_t j = 0; j < this->_z.get_batch_size(i); j++){
Expand All @@ -157,7 +157,7 @@ namespace nil {
if( !_batch_fixed[i] )continue;
math::polynomial<value_type> Q_normal;
auto point = _etha;
V = {-point, 1};
V = {-point, 1u};
for(std::size_t j = 0; j < this->_z.get_batch_size(i); j++){
math::polynomial<value_type> g_normal;
if constexpr(std::is_same<math::polynomial_dfs<value_type>, PolynomialType>::value ) {
Expand Down Expand Up @@ -222,11 +222,11 @@ namespace nil {
typename std::vector<std::vector<std::tuple<std::size_t, std::size_t>>> poly_map(total_points);

value_type theta = transcript.template challenge<field_type>();
value_type theta_acc(1);
value_type theta_acc = value_type::one();

for (std::size_t p = 0; p < points.size(); p++){
auto &point = points[p];
V[p] = {-point, 1};
V[p] = {-point, 1u};
for(std::size_t i:this->_z.get_batches()){
for(std::size_t j = 0; j < this->_z.get_batch_size(i); j++){
auto it = std::find(this->_points[i][j].begin(), this->_points[i][j].end(), point);
Expand All @@ -240,7 +240,7 @@ namespace nil {

if( total_points > points.size()){
std::size_t p = points.size();
V[p] = {-_etha, 1};
V[p] = {-_etha, 1u};
for(std::size_t i:this->_z.get_batches()){
if( !_batch_fixed[i] )continue;
for(std::size_t j = 0; j < this->_z.get_batch_size(i); j++){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ namespace nil {
typename FieldType::value_type theta_acc = FieldType::value_type::one();

for (const auto& gate: gates) {
typename FieldType::value_type gate_result = {0};
typename FieldType::value_type gate_result = FieldType::value_type::zero();

for (const auto& constraint : gate.constraints) {
gate_result += constraint.evaluate(evaluations) * theta_acc;
Expand Down
Loading

0 comments on commit 358be60

Please sign in to comment.