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

IPA over Grumpkin #51

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
324 changes: 324 additions & 0 deletions src/blocks/IpaPcs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.16;

import "src/blocks/grumpkin/Grumpkin.sol";
import "src/blocks/KeccakTranscript.sol";

library InnerProductArgument {
struct IpaInputGrumpkin {
Grumpkin.GrumpkinAffinePoint[] ck_v;
Grumpkin.GrumpkinAffinePoint[] ck_s;
uint256[] point;
Grumpkin.GrumpkinAffinePoint[] L_vec;
Grumpkin.GrumpkinAffinePoint[] R_vec;
Grumpkin.GrumpkinAffinePoint commitment;
uint256 eval;
uint256 a_hat;
}

struct InstanceGrumpkin {
Grumpkin.GrumpkinAffinePoint comm_a_vec;
uint256[] b_vec;
uint256 c;
}

struct R {
uint256[] r_vec;
uint256[] r_vec_squared;
uint256[] r_vec_inversed;
uint256[] r_vec_inversed_squared;
}

struct P_hat_right_input {
uint256 n;
R r_vectors;
Grumpkin.GrumpkinAffinePoint[] ck1;
uint256[] b_vec;
uint256 a_hat;
Grumpkin.GrumpkinAffinePoint ck_c;
}

function batchInvert(uint256[] memory r_vec, uint256 modulus) private view returns (uint256[] memory) {
uint256[] memory products = new uint256[](r_vec.length);
uint256 acc = 1;
uint256 index;
for (index = 0; index < r_vec.length; index++) {
products[index] = acc;
acc = mulmod(acc, r_vec[index], modulus);
}

acc = Field.invert(acc, modulus);

uint256[] memory inversed = new uint256[](r_vec.length);

uint256 tmp;
for (index = 0; index < r_vec.length; index++) {
tmp = mulmod(acc, r_vec[r_vec.length - index - 1], modulus);
inversed[r_vec.length - index - 1] = mulmod(products[r_vec.length - index - 1], acc, modulus);
acc = tmp;
}

return inversed;
}

function compute_r_based_values(uint256[] memory r_vec, uint256 modulus) private view returns (R memory) {
uint256[] memory r_vec_squared = new uint256[](r_vec.length);
uint256 index;
for (index = 0; index < r_vec.length; index++) {
r_vec_squared[index] = mulmod(r_vec[index], r_vec[index], modulus);
}

uint256[] memory r_vec_inversed = batchInvert(r_vec, modulus);

uint256[] memory r_vec_inversed_squared = new uint256[](r_vec.length);
for (index = 0; index < r_vec.length; index++) {
r_vec_inversed_squared[index] = mulmod(r_vec_inversed[index], r_vec_inversed[index], modulus);
}
return R(r_vec, r_vec_squared, r_vec_inversed, r_vec_inversed_squared);
}

function split_at(Grumpkin.GrumpkinAffinePoint[] memory ck, uint256 n)
private
pure
returns (Grumpkin.GrumpkinAffinePoint[] memory, Grumpkin.GrumpkinAffinePoint[] memory)
{
require(n <= ck.length, "[split_at] unexpected n");

Grumpkin.GrumpkinAffinePoint[] memory ck1 = new Grumpkin.GrumpkinAffinePoint[](n);
Grumpkin.GrumpkinAffinePoint[] memory ck2 = new Grumpkin.GrumpkinAffinePoint[](n);
uint256 ck_index = 0;
for (uint256 i = 0; i < n; i++) {
ck1[i] = ck[ck_index];
ck_index++;
}
for (uint256 i = n; i < ck.length; i++) {
ck2[i] = ck[ck_index];
ck_index++;
}

return (ck1, ck2);
}

function scale(Grumpkin.GrumpkinAffinePoint[] memory ck_c, uint256 r)
private
view
returns (Grumpkin.GrumpkinAffinePoint memory)
{
require(ck_c.length == 1, "[scale] unexpected ck_c");
return Grumpkin.scalarMul(ck_c[0], r);
}

function inner_product_inner(uint256[] memory c) private pure returns (uint256[] memory) {
if (c.length == 1) {
return c;
}
uint256[] memory c_inner = new uint256[](c.length / 2);
for (uint256 index = 0; index < c_inner.length; index++) {
c_inner[index] = addmod(c[2 * index], c[2 * index + 1], Grumpkin.P_MOD);
}
return inner_product_inner(c_inner);
}

function inner_product(uint256[] memory a, uint256[] memory b) private pure returns (uint256) {
require(a.length == b.length);
uint256[] memory c = new uint256[](a.length);
uint256 index;
for (index = 0; index < a.length; index++) {
c[index] = mulmod(a[index], b[index], Grumpkin.P_MOD);
}

c = inner_product_inner(c);
return c[0];
}

function get_pos_value(uint256 i) private pure returns (uint256) {
require(i >= 1, "[get_pos_value], i < 1");
require(i <= 16, "[get_pos_value], i > 16");
uint256[] memory result = new uint256[](16);
result[0] = 0;
result[1] = 1;
result[2] = 1;
result[3] = 2;
result[4] = 2;
result[5] = 2;
result[6] = 2;
result[7] = 3;
result[8] = 3;
result[9] = 3;
result[10] = 3;
result[11] = 3;
result[12] = 3;
result[13] = 3;
result[14] = 3;
result[15] = 4;
return result[i - 1];
}

function compute_P_hat_right(P_hat_right_input memory input)
private
returns (Grumpkin.GrumpkinAffinePoint memory)
{
uint256[] memory s = new uint256[](input.n);

uint256 v = 1;
uint256 index;
for (index = 0; index < input.r_vectors.r_vec_inversed.length; index++) {
v = mulmod(v, input.r_vectors.r_vec_inversed[index], Grumpkin.P_MOD);
}
s[0] = v;

uint256 pos_in_r;
uint256 r_square_length = input.r_vectors.r_vec_squared.length;
for (index = 1; index < input.n; index++) {
pos_in_r = get_pos_value(index);
s[index] = mulmod(
s[index - (1 << pos_in_r)],
input.r_vectors.r_vec_squared[r_square_length - 1 - pos_in_r],
Grumpkin.P_MOD
);
}

uint256 b_hat = inner_product(input.b_vec, s);
Grumpkin.GrumpkinAffinePoint memory ck_hat = Grumpkin.multiScalarMul(input.ck1, s);

Grumpkin.GrumpkinAffinePoint[] memory bases = new Grumpkin.GrumpkinAffinePoint[](2);
bases[0] = ck_hat;
bases[1] = input.ck_c;

uint256[] memory scalars = new uint256[](2);
scalars[0] = input.a_hat;
scalars[1] = mulmod(input.a_hat, b_hat, Grumpkin.P_MOD);

return Grumpkin.multiScalarMul(bases, scalars);
}

function compute_P_hat_left(IpaInputGrumpkin memory input, R memory r_vec, Grumpkin.GrumpkinAffinePoint memory ck_c)
private
returns (Grumpkin.GrumpkinAffinePoint memory)
{
Grumpkin.GrumpkinAffinePoint memory P = Grumpkin.add(input.commitment, Grumpkin.scalarMul(ck_c, input.eval));

uint256 msm_len = input.L_vec.length + input.R_vec.length + 1;

uint256 msm_index = 0;
uint256[] memory scalars = new uint256[](msm_len);
for (uint256 index = 0; index < r_vec.r_vec_squared.length; index++) {
scalars[msm_index] = r_vec.r_vec_squared[index];
msm_index++;
}
for (uint256 index = 0; index < r_vec.r_vec_inversed_squared.length; index++) {
scalars[msm_index] = r_vec.r_vec_inversed_squared[index];
msm_index++;
}
scalars[msm_index] = 0x01;

msm_index = 0;
Grumpkin.GrumpkinAffinePoint[] memory bases = new Grumpkin.GrumpkinAffinePoint[](msm_len);
for (uint256 index = 0; index < input.L_vec.length; index++) {
bases[msm_index] = input.L_vec[index];
msm_index++;
}
for (uint256 index = 0; index < input.R_vec.length; index++) {
bases[msm_index] = input.R_vec[index];
msm_index++;
}
bases[msm_index] = P;

return Grumpkin.multiScalarMul(bases, scalars);
}

function compute_P_hat_right(
uint256 b_hat,
uint256 a_hat,
Grumpkin.GrumpkinAffinePoint memory ck_hat,
Grumpkin.GrumpkinAffinePoint memory ck_c
) private returns (Grumpkin.GrumpkinAffinePoint memory) {
Grumpkin.GrumpkinAffinePoint[] memory bases = new Grumpkin.GrumpkinAffinePoint[](2);
bases[0] = ck_hat;
bases[1] = ck_c;

uint256[] memory scalars = new uint256[](2);
scalars[0] = a_hat;
scalars[1] = mulmod(a_hat, b_hat, Grumpkin.P_MOD);

return Grumpkin.multiScalarMul(bases, scalars);
}

function verifyGrumpkin(IpaInputGrumpkin memory input, KeccakTranscriptLib.KeccakTranscript memory transcript)
public
returns (bool)
{
uint256 n = 2 ** input.point.length;

uint256[] memory b_vec = EqPolynomialLib.evals(input.point, Grumpkin.P_MOD, Grumpkin.negateBase);
(Grumpkin.GrumpkinAffinePoint[] memory ck1,) = split_at(input.ck_v, b_vec.length);

// b"IPA" in Rust
uint8[] memory label = new uint8[](3);
label[0] = 0x49;
label[1] = 0x50;
label[2] = 0x41;

transcript = KeccakTranscriptLib.dom_sep(transcript, label);

if (b_vec.length != n) {
revert("NovaError::InvalidInputLength");
}
if (n != 1 << input.L_vec.length) {
revert("NovaError::InvalidInputLength");
}
if (input.L_vec.length != input.R_vec.length) {
revert("NovaError::InvalidInputLength");
}
if (input.L_vec.length >= 32) {
revert("NovaError::InvalidInputLength");
}

// b"U" in Rust
label = new uint8[](1);
label[0] = 0x55;

transcript =
KeccakTranscriptLib.absorb(transcript, label, InstanceGrumpkin(input.commitment, b_vec, input.eval));

// b"r" in Rust
label = new uint8[](1);
label[0] = 0x72;
uint256 r;
(transcript, r) = KeccakTranscriptLib.squeeze(transcript, ScalarFromUniformLib.curveGrumpkin(), label);

uint256[] memory r_vec = new uint256[](input.L_vec.length);
for (uint256 index = 0; index < r_vec.length; index++) {
// b"L" in Rust
label[0] = 0x4c;
transcript = KeccakTranscriptLib.absorb(transcript, label, input.L_vec[index].x);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in arecibo, I could find the implementation of Keccak256Transcript::absorb and the use of to_transcript_bytes but I could not find the implementation for the latter for CompressedCommitment.

Why is it that we only need to absorb the x of the curve point and not x+y+infinity_byte as we are in a lot of our implementations in arecibo?

Copy link
Member Author

@storojs72 storojs72 Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of to_transcript_bytes for CompressedCommitment is indeed hidden in following macro. As far as I know, all the necessary information about y coordinate is already stored in x.

Compression / uncompression of EC points is one of the boring thing in our codebase (@huitseeker is aware of it). from one side we want to keep proof size as smaller as possible (hence compression is our friend). On the other hand some cryptographic operations can be performed only with points in uncompressed form

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of to_transcript_bytes for CompressedCommitment is indeed hidden in following macro.

Thanks! I understand now that it trace all the way to the defined Compressed type which in our case is the G1Compressed structure defined through the usage of the new_curve_impl macro in the halo2 crate.

As far as I know, all the necessary information about y coordinate is already stored in x.

So from what I get of your sentence and a few research, the compressed point is the data of x along with a sign bit that is derived from the y value of an affine point. And it seems that this is what we absorb in the Rust implementation. Aren't we missing the sign bit then? Or do we not need it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sign is also encoded in the information behind x (consider from_bytes function in halo2curves). The field element for Bn256 (and Grumpkin) have 254 bits in size (link), while x is uint256, which is 256 bits - so we have two bits for the metadata and by absorbing whole uint256 we absorb sign as well.

For the uncompressed commitment (which is essentially a Grumpkin affine point with two coordinates) we have different absorb where we put x, y and an extra-byte representing whether point is infinity (usually this is not the case, so this is 0x00) to the transcript's memory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the authoritative format for this conversion is in SECG1, section 2.3.3. read with compression active.


// b"R" in Rust
label[0] = 0x52;
transcript = KeccakTranscriptLib.absorb(transcript, label, input.R_vec[index].x);

// b"r" in Rust
label[0] = 0x72;
(transcript, r_vec[index]) =
KeccakTranscriptLib.squeeze(transcript, ScalarFromUniformLib.curveGrumpkin(), label);
}

R memory r_vectors = compute_r_based_values(r_vec, Grumpkin.P_MOD);

Grumpkin.GrumpkinAffinePoint memory ck_c = scale(input.ck_s, r);

Grumpkin.GrumpkinAffinePoint memory P_hat_right =
compute_P_hat_right(P_hat_right_input(n, r_vectors, ck1, b_vec, input.a_hat, ck_c));

Grumpkin.GrumpkinAffinePoint memory P_hat_left = compute_P_hat_left(input, r_vectors, ck_c);

if (P_hat_right.x != P_hat_left.x) {
return false;
}
if (P_hat_right.y != P_hat_left.y) {
return false;
}

return true;
}
}
Loading
Loading