Skip to content

Commit

Permalink
small comment updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasxia01 committed Dec 1, 2023
1 parent b24c737 commit 1f7d4a9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template <typename FF_> class Poseidon2ExternalRelationImpl {
/**
* @brief Expression for the poseidon2 external round relation, based on E_i in Section 6 of
* https://eprint.iacr.org/2023/323.pdf.
* @details This relation is defined as:
* @details This relation is defined as C(in(X)...) :=
* q_poseidon2_external * ( (v1 - w_1_shift) + \alpha * (v2 - w_2_shift) +
* \alpha^2 * (v3 - w_3_shift) + \alpha^3 * (v4 - w_4_shift) ) = 0 where:
* u1 := (w_1 + q_1)^5
Expand Down Expand Up @@ -80,41 +80,36 @@ template <typename FF_> class Poseidon2ExternalRelationImpl {
u4 *= s4;

// matrix mul v = M_E * u with 14 additions
auto t0 = u1 + u2; // A + B
auto t1 = u3 + u4; // C + D
auto t2 = u2 + u2; // 2B
t2 += t1; // 2B + C + D
auto t3 = u4 + u4; // 2D
t3 += t0; // 2D + A + B
auto t0 = u1 + u2; // u_1 + u_2
auto t1 = u3 + u4; // u_3 + u_4
auto t2 = u2 + u2; // 2u_2
t2 += t1; // 2u_2 + u_3 + u_4
auto t3 = u4 + u4; // 2u_4
t3 += t0; // u_1 + u_2 + 2u_4
auto v4 = t1 + t1;
v4 += v4;
v4 += t3; // A + B + 4C + 6D
v4 += t3; // u_1 + u_2 + 4u_3 + 6u_4
auto v2 = t0 + t0;
v2 += v2;
v2 += t2; // 4A + 6B + C + D
auto v1 = t3 + v2; // 5A + 7B + C + 3D
auto v3 = t2 + v4; // A + 3B + 5C + 7D
v2 += t2; // 4u_1 + 6u_2 + u_3 + u_4
auto v1 = t3 + v2; // 5u_1 + 7u_2 + u_3 + 3u_4
auto v3 = t2 + v4; // u_1 + 3u_2 + 5u_3 + 7u_4

{
auto tmp = q_poseidon2_external * (v1 - w_l_shift);
tmp *= scaling_factor;
std::get<0>(evals) += tmp;
}
{
auto tmp = q_poseidon2_external * (v2 - w_r_shift);
tmp *= scaling_factor;
std::get<1>(evals) += tmp;
}
{
auto tmp = q_poseidon2_external * (v3 - w_o_shift);
tmp *= scaling_factor;
std::get<2>(evals) += tmp;
}
{
auto tmp = q_poseidon2_external * (v4 - w_4_shift);
tmp *= scaling_factor;
std::get<3>(evals) += tmp;
}
auto tmp = q_poseidon2_external * (v1 - w_l_shift);
tmp *= scaling_factor;
std::get<0>(evals) += tmp;

tmp = q_poseidon2_external * (v2 - w_r_shift);
tmp *= scaling_factor;
std::get<1>(evals) += tmp;

tmp = q_poseidon2_external * (v3 - w_o_shift);
tmp *= scaling_factor;
std::get<2>(evals) += tmp;

tmp = q_poseidon2_external * (v4 - w_4_shift);
tmp *= scaling_factor;
std::get<3>(evals) += tmp;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ template <typename FF_> class Poseidon2InternalRelationImpl {
/**
* @brief Expression for the poseidon2 internal round relation, based on I_i in Section 6 of
* https://eprint.iacr.org/2023/323.pdf.
* @details This relation is defined as:
* @details This relation is defined as C(in(X)...) :=
* q_poseidon2_internal * ( (v1 - w_1_shift) + \alpha * (v2 - w_2_shift) +
* \alpha^2 * (v3 - w_3_shift) + \alpha^3 * (v4 - w_4_shift) ) = 0 where:
* u1 := (w_1 + q_1)^5
Expand Down Expand Up @@ -66,34 +66,30 @@ template <typename FF_> class Poseidon2InternalRelationImpl {

// matrix mul with v = M_I * u 4 muls and 7 additions
auto sum = u1 + u2 + u3 + u4;
{
auto v1 = u1 * crypto::Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal[0];
v1 += sum;
auto tmp = q_poseidon2_internal * (v1 - w_l_shift);
tmp *= scaling_factor;
std::get<0>(evals) += tmp;
}
{
auto v2 = u2 * crypto::Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal[1];
v2 += sum;
auto tmp = q_poseidon2_internal * (v2 - w_r_shift);
tmp *= scaling_factor;
std::get<1>(evals) += tmp;
}
{
auto v3 = u3 * crypto::Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal[2];
v3 += sum;
auto tmp = q_poseidon2_internal * (v3 - w_o_shift);
tmp *= scaling_factor;
std::get<2>(evals) += tmp;
}
{
auto v4 = u4 * crypto::Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal[3];
v4 += sum;
auto tmp = q_poseidon2_internal * (v4 - w_4_shift);
tmp *= scaling_factor;
std::get<3>(evals) += tmp;
}

auto v1 = u1 * crypto::Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal[0];
v1 += sum;
auto tmp = q_poseidon2_internal * (v1 - w_l_shift);
tmp *= scaling_factor;
std::get<0>(evals) += tmp;

auto v2 = u2 * crypto::Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal[1];
v2 += sum;
tmp = q_poseidon2_internal * (v2 - w_r_shift);
tmp *= scaling_factor;
std::get<1>(evals) += tmp;

auto v3 = u3 * crypto::Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal[2];
v3 += sum;
tmp = q_poseidon2_internal * (v3 - w_o_shift);
tmp *= scaling_factor;
std::get<2>(evals) += tmp;

auto v4 = u4 * crypto::Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal[3];
v4 += sum;
tmp = q_poseidon2_internal * (v4 - w_4_shift);
tmp *= scaling_factor;
std::get<3>(evals) += tmp;
};
}; // namespace proof_system

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,21 +597,21 @@ TEST_F(UltraRelationConsistency, Poseidon2ExternalRelation)
u4 *= u4;
u4 *= s4;

// multiply with external matrix
auto t0 = u1 + u2; // A + B
auto t1 = u3 + u4; // C + D
auto t2 = u2 + u2; // 2B
t2 += t1; // 2B + C + D
auto t3 = u4 + u4; // 2D
t3 += t0; // 2D + A + B
// matrix mul v = M_E * u with 14 additions
auto t0 = u1 + u2; // u_1 + u_2
auto t1 = u3 + u4; // u_3 + u_4
auto t2 = u2 + u2; // 2u_2
t2 += t1; // 2u_2 + u_3 + u_4
auto t3 = u4 + u4; // 2u_4
t3 += t0; // u_1 + u_2 + 2u_4
auto v4 = t1 + t1;
v4 += v4;
v4 += t3; // A + B + 4C + 6D
v4 += t3; // u_1 + u_2 + 4u_3 + 6u_4
auto v2 = t0 + t0;
v2 += v2;
v2 += t2; // 4A + 6B + C + D
auto v1 = t3 + v2; // 5A + 7B + C + 3D
auto v3 = t2 + v4; // A + 3B + 5C + 7D
v2 += t2; // 4u_1 + 6u_2 + u_3 + u_4
auto v1 = t3 + v2; // 5u_1 + 7u_2 + u_3 + 3u_4
auto v3 = t2 + v4; // u_1 + 3u_2 + 5u_3 + 7u_4

// output is { v1, v2, v3, v4 }

Expand Down

0 comments on commit 1f7d4a9

Please sign in to comment.