From e081cf4bedfe4bca1ef7580c9e1e9870333a1c2e Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Fri, 20 Oct 2023 08:57:16 +0300 Subject: [PATCH 1/8] Added unchecked to commitment_scheme #38 --- .../templates/commitment_scheme.hpp | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp index 5d8fd54..5472f3a 100644 --- a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp +++ b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp @@ -85,14 +85,17 @@ library modular_commitment_scheme_$TEST_NAME$ { function calculate_2points_interpolation(uint256[] memory xi, uint256[2] memory z, uint256 modulus) internal pure returns(uint256[2] memory U){ // require( xi.length == 2 ); +unchecked { U[0] = addmod(mulmod(z[0], xi[1], modulus),modulus - mulmod(z[1], xi[0], modulus), modulus); U[1] = addmod(z[1], modulus - z[0], modulus); +} } // coeffs for zs on each degree can be precomputed if necessary function calculate_3points_interpolation(uint256[] memory xi, uint256[3] memory z, uint256 modulus) internal pure returns(uint256[3] memory U){ // require( xi.length == 3 ); +unchecked { z[0] = mulmod(z[0], addmod(xi[1], modulus - xi[2], modulus), modulus); z[1] = mulmod(z[1], addmod(xi[2], modulus - xi[0], modulus), modulus); z[2] = mulmod(z[2], addmod(xi[0], modulus - xi[1], modulus), modulus); @@ -106,6 +109,7 @@ library modular_commitment_scheme_$TEST_NAME$ { U[1] = addmod(U[1], modulus - mulmod(z[2], addmod(xi[0], xi[1], modulus), modulus), modulus); U[2] = addmod(z[0], addmod(z[1], z[2], modulus), modulus); +} } function prepare_eval_points(uint256[][unique_points] memory result, uint256 xi) internal view { @@ -113,7 +117,9 @@ library modular_commitment_scheme_$TEST_NAME$ { $POINTS_INITIALIZATION$ } - function prepare_U_V(bytes calldata blob, commitment_state memory state, uint256 xi) internal view returns(bool result){ + function prepare_U_V(bytes calldata blob, commitment_state memory state, uint256 xi) internal view returns(bool result){ + +unchecked { result = true; uint64 ind = 0; prepare_eval_points(state.unique_eval_points, xi); @@ -245,9 +251,12 @@ library modular_commitment_scheme_$TEST_NAME$ { } unchecked{ind++;} } +} } function compute_combined_Q(bytes calldata blob,commitment_state memory state) internal view returns(uint256[2] memory y){ + +unchecked { uint256[2][unique_points] memory values; { uint256 offset = state.initial_data_offset - state.poly_num * 0x40; // Save initial data offset for future use; @@ -287,6 +296,7 @@ library modular_commitment_scheme_$TEST_NAME$ { y[1] = addmod(y[1], tmp[1], modulus); unchecked{p++;} } +} } function initialize( @@ -343,6 +353,7 @@ library modular_commitment_scheme_$TEST_NAME$ { function copy_pairs_and_check(bytes calldata blob, uint256 offset, bytes memory leaf, uint256 size, uint256 proof_offset) internal pure returns(bool b){ +unchecked { uint256 offset2 = 0x20; for(uint256 k = 0; k < size;){ assembly{ @@ -364,10 +375,12 @@ library modular_commitment_scheme_$TEST_NAME$ { } else { return true; } +} } function copy_reverted_pairs_and_check(bytes calldata blob, uint256 offset, bytes memory leaf, uint256 size, uint256 proof_offset) internal pure returns(bool){ +unchecked { uint256 offset2 = 0x20; for(uint256 k = 0; k < size;){ assembly{ @@ -389,14 +402,17 @@ library modular_commitment_scheme_$TEST_NAME$ { } else { return true; } +} } function colinear_check(uint256 x, uint256[2] memory y, uint256 alpha, uint256 colinear_value) internal pure returns(bool){ + +unchecked { uint256 tmp; tmp = addmod(y[0], y[1], modulus); tmp = mulmod(tmp, x, modulus); tmp = addmod( - tmp, + tmp, mulmod( alpha, addmod(y[0], modulus-y[1], modulus), @@ -411,14 +427,17 @@ library modular_commitment_scheme_$TEST_NAME$ { return false; } return true; +} } function verify_eval( bytes calldata blob, - uint256[5] memory commitments, + uint256[5] memory commitments, uint256 challenge, bytes32 transcript_state ) internal view returns (bool){ + +unchecked { types.transcript_data memory tr_state; tr_state.current_challenge = transcript_state; commitment_state memory state; @@ -585,10 +604,11 @@ library modular_commitment_scheme_$TEST_NAME$ { unchecked{i++;} } return true; +} } } )"; } } -#endif //__MODULAR_CONTRACT_TEMPLATE_HPP__ \ No newline at end of file +#endif //__MODULAR_CONTRACT_TEMPLATE_HPP__ From 2e2bc84a138d78e0b979a3bd1ea3421eee307ab0 Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Mon, 23 Oct 2023 07:50:37 +0300 Subject: [PATCH 2/8] Added unchecked on all functions in commitment scheme #38 --- .../templates/commitment_scheme.hpp | 85 +++++++++---------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp index 5472f3a..4757238 100644 --- a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp +++ b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp @@ -188,7 +188,7 @@ unchecked { console.log("UNPROCESSED number of evaluation points"); return false; } - unchecked{ind++;} + ind++; } // Prepare combined U @@ -245,11 +245,11 @@ unchecked { } } offset += state.unique_eval_points[cur_point].length * 0x20; - unchecked{i++;cur++;} + i++;cur++; } - unchecked{k++;} + k++; } - unchecked{ind++;} + ind++; } } } @@ -273,14 +273,14 @@ unchecked { for(uint256 k = 0; k < unique_points; ){ values[k][0] = mulmod(values[k][0], state.theta, modulus); values[k][1] = mulmod(values[k][1], state.theta, modulus); - unchecked{k++;} + k++; } values[cur_point][0] = addmod(values[cur_point][0], basic_marshalling.get_uint256_be(blob, offset), modulus); values[cur_point][1] = addmod(values[cur_point][1], basic_marshalling.get_uint256_be(blob, offset + 0x20), modulus); - unchecked{offset += 0x40;j++; cur++;} + offset += 0x40;j++; cur++; } - unchecked{b++;} + b++; } } for(uint256 p = 0; p < unique_points; ){ @@ -294,7 +294,7 @@ unchecked { tmp[1] = mulmod(tmp[1], field.inverse_static(polynomial.evaluate(state.denominators[p], modulus - s, modulus), modulus), modulus); y[0] = addmod(y[0], tmp[0], modulus); y[1] = addmod(y[1], tmp[1], modulus); - unchecked{p++;} + p++; } } } @@ -366,9 +366,7 @@ unchecked { calldataload(add(blob.offset, add(offset, 0x20))) ) } - unchecked{ - k++; offset2 += 0x40; offset += 0x40; - } + k++; offset2 += 0x40; offset += 0x40; } if( !merkle_verifier.parse_verify_merkle_proof_bytes_be(blob, proof_offset, leaf, offset2 - 0x20 )){ return false; @@ -393,9 +391,7 @@ unchecked { calldataload(add(blob.offset, offset)) ) } - unchecked{ - k++; offset2 += 0x40; offset += 0x40; - } + k++; offset2 += 0x40; offset += 0x40; } if( !merkle_verifier.parse_verify_merkle_proof_bytes_be(blob, proof_offset, leaf, offset2 - 0x20 )){ return false; @@ -448,53 +444,48 @@ unchecked { for(uint8 i = 0; i < batches_num;){ transcript.update_transcript_b32(tr_state, bytes32(commitments[i])); - unchecked{i++;} + i++; } state.theta = transcript.get_field_challenge(tr_state, modulus); state.points_num = basic_marshalling.get_length(blob, 0x0); - unchecked{ - offset = 0x8 + state.points_num*0x20 + 0x8; - } + offset = 0x8 + state.points_num*0x20 + 0x8; for(uint8 i = 0; i < batches_num;){ state.batch_sizes[i] = uint64(uint8(blob[offset + 0x1])); if( state.batch_sizes[i] > state.max_batch ) state.max_batch = state.batch_sizes[i]; state.poly_num += state.batch_sizes[i]; - unchecked { i++; offset +=2;} - } - unchecked{ - offset += 0x8; - offset += state.poly_num; - state.roots_offset = offset + 0x8; - offset += 0x8; + i++; offset +=2; } + + offset += 0x8; + offset += state.poly_num; + state.roots_offset = offset + 0x8; + offset += 0x8; + for( uint8 i = 0; i < r;){ transcript.update_transcript_b32(tr_state, bytes32(basic_marshalling.get_uint256_be(blob, offset + 0x8))); state.alphas[i] = transcript.get_field_challenge(tr_state, modulus); - unchecked{i++; offset +=40; } + i++; offset +=40; } $GRINDING_CHECK$ - unchecked{ - offset += 0x8 + r; - state.initial_data_offset = offset + 0x8; - offset += 0x8 + 0x20*basic_marshalling.get_length(blob, offset); - } + offset += 0x8 + r; + state.initial_data_offset = offset + 0x8; + offset += 0x8 + 0x20*basic_marshalling.get_length(blob, offset); + + state.round_data_offset = offset + 0x8; + offset += 0x8 + 0x20*basic_marshalling.get_length(blob, offset); + offset += 0x8; - unchecked{ - state.round_data_offset = offset + 0x8; - offset += 0x8 + 0x20*basic_marshalling.get_length(blob, offset); - offset += 0x8; - } state.initial_proof_offset = offset; for(uint8 i = 0; i < lambda;){ for(uint j = 0; j < batches_num;){ if(basic_marshalling.get_uint256_be(blob, offset + 0x10) != commitments[j] ) return false; offset = merkle_verifier.skip_merkle_proof_be(blob, offset); - unchecked{j++;} + j++; } - unchecked{i++;} + i++; } offset += 0x8; state.round_proof_offset = offset; @@ -503,16 +494,16 @@ unchecked { for(uint256 j = 0; j < r;){ if(basic_marshalling.get_uint256_be(blob, offset + 0x10) != basic_marshalling.get_uint256_be(blob, state.roots_offset + j * 40 + 0x8) ) return false; offset = merkle_verifier.skip_merkle_proof_be(blob, offset); - unchecked{j++;} + j++; } - unchecked{i++;} + i++; } state.final_polynomial = new uint256[](basic_marshalling.get_length(blob, offset)); - unchecked{offset += 0x8;} + offset += 0x8; for (uint256 i = 0; i < state.final_polynomial.length;) { state.final_polynomial[i] = basic_marshalling.get_uint256_be(blob, offset); - unchecked{ i++; offset+=0x20;} + i++; offset+=0x20; } } if( state.final_polynomial.length > (( 1 << (field.log2(max_degree + 1) - r + 1) ) ) ){ @@ -543,7 +534,7 @@ unchecked { state.leaf_length = state.batch_sizes[j] * 0x40; state.initial_data_offset += state.batch_sizes[j] * 0x40; state.initial_proof_offset = merkle_verifier.skip_merkle_proof_be(blob, state.initial_proof_offset); - unchecked{j++;} + j++; } { state.y = compute_combined_Q(blob, state); @@ -568,7 +559,7 @@ unchecked { for(state.j = 1; state.j < r;){ state.x_index %= state.domain_size; state.x = mulmod(state.x, state.x, modulus); - state.domain_size >>= 1; + state.domain_size >>= 1; if( state.x_index < state.domain_size ){ if(!copy_pairs_and_check(blob, state.round_data_offset, state.leaf_data, 1, state.round_proof_offset)) { console.log("Error in round mekle proof"); @@ -586,7 +577,7 @@ unchecked { console.log("Round colinear check failed"); return false; } - unchecked{state.j++; state.round_data_offset += 0x40;} + state.j++; state.round_data_offset += 0x40; state.round_proof_offset = merkle_verifier.skip_merkle_proof_be(blob, state.round_proof_offset); } @@ -601,12 +592,12 @@ unchecked { } state.round_data_offset += 0x40; - unchecked{i++;} + i++; } return true; } } -} +} )"; } } From 3273dc6f3da369ccb2f5115dda2c472918fadba5 Mon Sep 17 00:00:00 2001 From: "e.tatuzova" Date: Tue, 24 Oct 2023 10:54:19 +0400 Subject: [PATCH 3/8] Multiply gate argument result on mask polynomial value #41 --- .../transpiler/templates/modular_verifier.hpp | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/include/nil/blueprint/transpiler/templates/modular_verifier.hpp b/include/nil/blueprint/transpiler/templates/modular_verifier.hpp index e093e21..ef16102 100644 --- a/include/nil/blueprint/transpiler/templates/modular_verifier.hpp +++ b/include/nil/blueprint/transpiler/templates/modular_verifier.hpp @@ -12,9 +12,9 @@ namespace nil { ILookupArgument lookup_contract = ILookupArgument(_lookup_argument_address); (lookup_argument, tr_state.current_challenge) = lookup_contract.verify( // (lookup_argument, tr_state.current_challenge) = modular_lookup_argument_$TEST_NAME$.verify( - blob[special_selectors_offset: table_offset + quotient_offset], - blob[lookup_offset:lookup_offset + sorted_columns * 0x60], - basic_marshalling.get_uint256_be(blob, 0x81), + blob[special_selectors_offset: table_offset + quotient_offset], + blob[lookup_offset:lookup_offset + sorted_columns * 0x60], + basic_marshalling.get_uint256_be(blob, 0x81), state.l0, tr_state.current_challenge ); @@ -78,7 +78,7 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ function initialize( // address permutation_argument_address, - address lookup_argument_address, + address lookup_argument_address, address gate_argument_address, address commitment_contract_address ) public{ @@ -108,7 +108,7 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ } // Public input columns - function public_input_direct(bytes calldata blob, uint256[] calldata public_input, verifier_state memory state) internal view + function public_input_direct(bytes calldata blob, uint256[] calldata public_input, verifier_state memory state) internal view returns (bool check){ check = true; @@ -125,12 +125,12 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ ), modulus ); - + result = addmod( - result, + result, mulmod( public_input[i], L, modulus - ), + ), modulus ); } @@ -158,8 +158,8 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ state.xi = basic_marshalling.get_uint256_be(blob, $EVAL_PROOF_OFFSET$); state.Z_at_xi = addmod(field.pow_small(state.xi, rows_amount, modulus), modulus-1, modulus); state.l0 = mulmod( - state.Z_at_xi, - field.inverse_static(mulmod(addmod(state.xi, modulus - 1, modulus), rows_amount, modulus), modulus), + state.Z_at_xi, + field.inverse_static(mulmod(addmod(state.xi, modulus - 1, modulus), rows_amount, modulus), modulus), modulus ); @@ -171,7 +171,7 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ } } - //1. Init transcript + //1. Init transcript types.transcript_data memory tr_state; tr_state.current_challenge = transcript_state; @@ -181,8 +181,8 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ //3. Permutation argument uint256[3] memory permutation_argument = modular_permutation_argument_$TEST_NAME$.verify( - blob[$Z_OFFSET$:$TABLE_Z_OFFSET$+$QUOTIENT_OFFSET$], - transcript.get_field_challenge(tr_state, modulus), + blob[$Z_OFFSET$:$TABLE_Z_OFFSET$+$QUOTIENT_OFFSET$], + transcript.get_field_challenge(tr_state, modulus), transcript.get_field_challenge(tr_state, modulus), state.l0 ); @@ -200,6 +200,19 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ //6. Gate argument IGateArgument modular_gate_argument = IGateArgument(_gate_argument_address); state.F[7] = modular_gate_argument.verify(blob[table_offset:table_end_offset], transcript.get_field_challenge(tr_state, modulus)); + state.F[7] = mulmod( + state.F[7], + addmod( + 1, + modulus - addmod( + basic_marshalling.get_uint256_be(blob, special_selectors_offset), + basic_marshalling.get_uint256_be(blob, special_selectors_offset + 0x60), + modulus + ), + modulus + ), + modulus + ); } // No public input gate @@ -238,8 +251,8 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ uint256 factor = 1; for(uint64 i = 0; i < uint64(uint8(blob[z_offset + basic_marshalling.get_length(blob, z_offset - 0x8) *0x20 + 0xf]));){ T_consolidated = addmod( - T_consolidated, - mulmod(basic_marshalling.get_uint256_be(blob, table_offset + quotient_offset + i *0x20), factor, modulus), + T_consolidated, + mulmod(basic_marshalling.get_uint256_be(blob, table_offset + quotient_offset + i *0x20), factor, modulus), modulus ); factor = mulmod(factor, state.Z_at_xi + 1, modulus); @@ -254,7 +267,7 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{ console.log("Gas for verification:", state.gas-gasleft()); } -} +} )"; } } From 28f74ca4145a79d85ddd83e6fd91967937d5da4e Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Tue, 24 Oct 2023 10:29:10 +0300 Subject: [PATCH 4/8] Remove extra parameter #38 --- .../nil/blueprint/transpiler/templates/commitment_scheme.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp index 4757238..e6f2675 100644 --- a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp +++ b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp @@ -92,7 +92,7 @@ unchecked { } // coeffs for zs on each degree can be precomputed if necessary - function calculate_3points_interpolation(uint256[] memory xi, uint256[3] memory z, uint256 modulus) + function calculate_3points_interpolation(uint256[] memory xi, uint256[3] memory z) internal pure returns(uint256[3] memory U){ // require( xi.length == 3 ); unchecked { From 87d2130671477146fe5385f34abfd556e8f4b10d Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Tue, 24 Oct 2023 15:15:13 +0300 Subject: [PATCH 5/8] rm extra arguments #38 --- .../blueprint/transpiler/templates/commitment_scheme.hpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp index e6f2675..59a6210 100644 --- a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp +++ b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp @@ -82,7 +82,7 @@ library modular_commitment_scheme_$TEST_NAME$ { uint256 offset; } - function calculate_2points_interpolation(uint256[] memory xi, uint256[2] memory z, uint256 modulus) + function calculate_2points_interpolation(uint256[] memory xi, uint256[2] memory z) internal pure returns(uint256[2] memory U){ // require( xi.length == 2 ); unchecked { @@ -224,8 +224,7 @@ unchecked { tmp[0] = basic_marshalling.get_uint256_be(blob, offset); tmp[1] = basic_marshalling.get_uint256_be(blob, offset + 0x20); tmp = calculate_2points_interpolation( - point, tmp, modulus - ); + point, tmp); state.combined_U[ind][0] = addmod(state.combined_U[ind][0], tmp[0], modulus); state.combined_U[ind][1] = addmod(state.combined_U[ind][1], tmp[1], modulus); } else @@ -235,8 +234,7 @@ unchecked { tmp[1] = basic_marshalling.get_uint256_be(blob, offset + 0x20); tmp[2] = basic_marshalling.get_uint256_be(blob, offset + 0x40); tmp = calculate_3points_interpolation( - point, tmp, modulus - ); + point, tmp); state.combined_U[ind][0] = addmod(state.combined_U[ind][0], tmp[0], modulus); state.combined_U[ind][1] = addmod(state.combined_U[ind][1], tmp[1], modulus); state.combined_U[ind][2] = addmod(state.combined_U[ind][2], tmp[2], modulus); From 4285a41ddd99e3a56550d8c6a01011f221ef731b Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Wed, 1 Nov 2023 10:57:37 +0300 Subject: [PATCH 6/8] Ensure that at lease one constraint is included in module #38 --- include/nil/blueprint/transpiler/evm_verifier_gen.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nil/blueprint/transpiler/evm_verifier_gen.hpp b/include/nil/blueprint/transpiler/evm_verifier_gen.hpp index b9011d5..b2a748e 100644 --- a/include/nil/blueprint/transpiler/evm_verifier_gen.hpp +++ b/include/nil/blueprint/transpiler/evm_verifier_gen.hpp @@ -517,7 +517,7 @@ namespace nil { bool first_constraint = true; - while ((printed_cost < _gates_contract_size_threshold) && (it != last) ) { + while ((printed_cost <= _gates_contract_size_threshold) && (it != last) ) { if (first_constraint) { result << "// gate === " << it->gate_index << " ===" << std::endl; From ce6ac4d5c8a0197fc5337dac6ab2b59b2fedaaff Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Fri, 3 Nov 2023 13:36:21 +0300 Subject: [PATCH 7/8] whitespace cleanup #38 --- .../blueprint/transpiler/lpc_scheme_gen.hpp | 1 + .../templates/commitment_scheme.hpp | 76 +++++++++---------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp b/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp index 92fd530..f2965e7 100644 --- a/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp +++ b/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp @@ -142,6 +142,7 @@ namespace nil { std::stringstream points_initializer; std::size_t i = 0; for(const auto& point: unique_points){ + std::cout << "unique point: " << point << std::endl; points_initializer << "\t\t result[" << i << "] = new uint256[](" << std::count(point.begin(), point.end(), '&') << ");" << std::endl; std::size_t prev = 0; std::size_t found = point.find("& "); diff --git a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp index 59a6210..04f159f 100644 --- a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp +++ b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp @@ -130,24 +130,24 @@ unchecked { state.factors[ind] = 1; state.denominators[ind][0] = modulus - state.unique_eval_points[ind][0]; state.denominators[ind][1] = 1; - } else + } else if( state.unique_eval_points[ind].length == 2 ){ // xi1 - xi0 - state.factors[ind] = + state.factors[ind] = addmod(state.unique_eval_points[ind][1], modulus - state.unique_eval_points[ind][0], modulus); state.denominators[ind][2] = 1; - state.denominators[ind][1] = + state.denominators[ind][1] = modulus - addmod(state.unique_eval_points[ind][0], state.unique_eval_points[ind][1], modulus); - state.denominators[ind][0] = + state.denominators[ind][0] = mulmod(state.unique_eval_points[ind][0], state.unique_eval_points[ind][1], modulus); state.denominators[ind][0] = mulmod(state.denominators[ind][0], state.factors[ind], modulus); state.denominators[ind][1] = mulmod(state.denominators[ind][1], state.factors[ind], modulus); state.denominators[ind][2] = mulmod(state.denominators[ind][2], state.factors[ind], modulus); - } else + } else if( state.unique_eval_points[ind].length == 3 ){ - state.factors[ind] = modulus - + state.factors[ind] = modulus - mulmod( mulmod( addmod(state.unique_eval_points[ind][0], modulus - state.unique_eval_points[ind][1], modulus), @@ -160,24 +160,24 @@ unchecked { state.denominators[ind][3] = 1; state.denominators[ind][2] = modulus - addmod( - state.unique_eval_points[ind][0], - addmod(state.unique_eval_points[ind][1],state.unique_eval_points[ind][2], modulus), + state.unique_eval_points[ind][0], + addmod(state.unique_eval_points[ind][1],state.unique_eval_points[ind][2], modulus), modulus ); - state.denominators[ind][1] = + state.denominators[ind][1] = addmod( mulmod(state.unique_eval_points[ind][0], state.unique_eval_points[ind][1], modulus), addmod( mulmod(state.unique_eval_points[ind][0], state.unique_eval_points[ind][2], modulus), mulmod(state.unique_eval_points[ind][1], state.unique_eval_points[ind][2], modulus), modulus - ), + ), modulus ); - state.denominators[ind][0] = + state.denominators[ind][0] = modulus - mulmod( - state.unique_eval_points[ind][0], - mulmod(state.unique_eval_points[ind][1],state.unique_eval_points[ind][2], modulus), + state.unique_eval_points[ind][0], + mulmod(state.unique_eval_points[ind][1],state.unique_eval_points[ind][2], modulus), modulus ); state.denominators[ind][0] = mulmod(state.denominators[ind][0], state.factors[ind], modulus); @@ -198,7 +198,7 @@ unchecked { uint64 cur = 0; uint256 offset = 0x8; for( uint256 k = 0; k < batches_num;){ - for( uint256 i = 0; i < state.batch_sizes[k];){ + for( uint256 i = 0; i < state.batch_sizes[k];){ uint256 cur_point = 0; if(cur < points_ids.length ) cur_point = uint8(points_ids[cur]); else if(k == 2) cur_point = permutation_point; @@ -207,18 +207,18 @@ unchecked { else console.log("Wrong index"); polynomial.multiply_poly_on_coeff( - state.combined_U[ind], - state.theta, + state.combined_U[ind], + state.theta, modulus ); if( cur_point == ind ){ if( point.length == 1 ){ state.combined_U[ind][0] = addmod( state.combined_U[ind][0], - basic_marshalling.get_uint256_be(blob, offset), + basic_marshalling.get_uint256_be(blob, offset), modulus ); - } else + } else if( point.length == 2 ){ uint256[2] memory tmp; tmp[0] = basic_marshalling.get_uint256_be(blob, offset); @@ -227,7 +227,7 @@ unchecked { point, tmp); state.combined_U[ind][0] = addmod(state.combined_U[ind][0], tmp[0], modulus); state.combined_U[ind][1] = addmod(state.combined_U[ind][1], tmp[1], modulus); - } else + } else if( point.length == 3){ uint256[3] memory tmp; tmp[0] = basic_marshalling.get_uint256_be(blob, offset); @@ -241,7 +241,7 @@ unchecked { } else { return false; } - } + } offset += state.unique_eval_points[cur_point].length * 0x20; i++;cur++; } @@ -307,17 +307,17 @@ unchecked { tr_state_after = tr_state.current_challenge; } - function copy_memory_pair_and_check(bytes calldata blob, uint256 proof_offset, bytes memory leaf, uint256[2] memory pair) + function copy_memory_pair_and_check(bytes calldata blob, uint256 proof_offset, bytes memory leaf, uint256[2] memory pair) internal pure returns(bool b){ uint256 c = pair[0]; uint256 d = pair[1]; assembly{ mstore( - add(leaf, 0x20), + add(leaf, 0x20), c ) mstore( - add(leaf, 0x40), + add(leaf, 0x40), d ) } @@ -328,17 +328,17 @@ unchecked { } } - function copy_reverted_memory_pair_and_check(bytes calldata blob, uint256 proof_offset, bytes memory leaf, uint256[2] memory pair) + function copy_reverted_memory_pair_and_check(bytes calldata blob, uint256 proof_offset, bytes memory leaf, uint256[2] memory pair) internal pure returns(bool b){ uint256 c = pair[0]; uint256 d = pair[1]; assembly{ mstore( - add(leaf, 0x20), + add(leaf, 0x20), d ) mstore( - add(leaf, 0x40), + add(leaf, 0x40), c ) } @@ -349,18 +349,18 @@ unchecked { } } - function copy_pairs_and_check(bytes calldata blob, uint256 offset, bytes memory leaf, uint256 size, uint256 proof_offset) + function copy_pairs_and_check(bytes calldata blob, uint256 offset, bytes memory leaf, uint256 size, uint256 proof_offset) internal pure returns(bool b){ unchecked { uint256 offset2 = 0x20; for(uint256 k = 0; k < size;){ assembly{ mstore( - add(leaf, offset2), + add(leaf, offset2), calldataload(add(blob.offset, offset)) ) mstore( - add(leaf, add(offset2, 0x20)), + add(leaf, add(offset2, 0x20)), calldataload(add(blob.offset, add(offset, 0x20))) ) } @@ -374,18 +374,18 @@ unchecked { } } - function copy_reverted_pairs_and_check(bytes calldata blob, uint256 offset, bytes memory leaf, uint256 size, uint256 proof_offset) + function copy_reverted_pairs_and_check(bytes calldata blob, uint256 offset, bytes memory leaf, uint256 size, uint256 proof_offset) internal pure returns(bool){ unchecked { uint256 offset2 = 0x20; for(uint256 k = 0; k < size;){ assembly{ mstore( - add(leaf, offset2), + add(leaf, offset2), calldataload(add(blob.offset, add(offset, 0x20))) ) mstore( - add(leaf, add(offset2, 0x20)), + add(leaf, add(offset2, 0x20)), calldataload(add(blob.offset, offset)) ) } @@ -409,7 +409,7 @@ unchecked { tmp, mulmod( alpha, - addmod(y[0], modulus-y[1], modulus), + addmod(y[0], modulus-y[1], modulus), modulus ), modulus @@ -467,7 +467,7 @@ unchecked { } $GRINDING_CHECK$ - + offset += 0x8 + r; state.initial_data_offset = offset + 0x8; offset += 0x8 + 0x20*basic_marshalling.get_length(blob, offset); @@ -476,7 +476,7 @@ unchecked { offset += 0x8 + 0x20*basic_marshalling.get_length(blob, offset); offset += 0x8; - state.initial_proof_offset = offset; + state.initial_proof_offset = offset; for(uint8 i = 0; i < lambda;){ for(uint j = 0; j < batches_num;){ if(basic_marshalling.get_uint256_be(blob, offset + 0x10) != commitments[j] ) return false; @@ -490,7 +490,7 @@ unchecked { for(uint256 i = 0; i < lambda;){ for(uint256 j = 0; j < r;){ - if(basic_marshalling.get_uint256_be(blob, offset + 0x10) != basic_marshalling.get_uint256_be(blob, state.roots_offset + j * 40 + 0x8) ) return false; + if(basic_marshalling.get_uint256_be(blob, offset + 0x10) != basic_marshalling.get_uint256_be(blob, state.roots_offset + j * 40 + 0x8) ) return false; offset = merkle_verifier.skip_merkle_proof_be(blob, offset); j++; } @@ -555,7 +555,7 @@ unchecked { state.round_proof_offset = merkle_verifier.skip_merkle_proof_be(blob, state.round_proof_offset); for(state.j = 1; state.j < r;){ - state.x_index %= state.domain_size; + state.x_index %= state.domain_size; state.x = mulmod(state.x, state.x, modulus); state.domain_size >>= 1; if( state.x_index < state.domain_size ){ @@ -589,7 +589,7 @@ unchecked { return false; } state.round_data_offset += 0x40; - + i++; } return true; From 0b34da22cb159cf52d6c3c2a4e0a8aede72fe7b6 Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Wed, 8 Nov 2023 12:17:41 +0300 Subject: [PATCH 8/8] rm debug print #38 --- include/nil/blueprint/transpiler/lpc_scheme_gen.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp b/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp index f2965e7..92fd530 100644 --- a/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp +++ b/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp @@ -142,7 +142,6 @@ namespace nil { std::stringstream points_initializer; std::size_t i = 0; for(const auto& point: unique_points){ - std::cout << "unique point: " << point << std::endl; points_initializer << "\t\t result[" << i << "] = new uint256[](" << std::count(point.begin(), point.end(), '&') << ");" << std::endl; std::size_t prev = 0; std::size_t found = point.find("& ");