Skip to content

Commit c863dbc

Browse files
committed
address review comments
1 parent 4299018 commit c863dbc

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed

lib/evmone_precompiles/kzg.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "kzg.hpp"
2-
#include "sha256.hpp"
32
#include <blst.h>
43
#include <algorithm>
54
#include <optional>
@@ -60,11 +59,16 @@ std::optional<blst_p1_affine> validate_G1(std::span<const std::byte, 48> b) noex
6059
blst_p1_affine r;
6160
if (blst_p1_uncompress(&r, reinterpret_cast<const uint8_t*>(b.data())) != BLST_SUCCESS)
6261
return std::nullopt;
63-
if (!blst_p1_affine_in_g1(&r)) // Subgroup check is required by the spec but not testable.
62+
63+
// Subgroup check is required by the spec but there are no test vectors
64+
// with points outside G1 which would satisfy the final pairings check.
65+
if (!blst_p1_affine_in_g1(&r))
6466
return std::nullopt;
6567
return r;
6668
}
6769

70+
/// Add two points from E1 and convert the result to affine form.
71+
/// The conversion to affine is very costly so use only if the affine of the result is needed.
6872
blst_p1_affine add_or_double(const blst_p1_affine& p, const blst_p1& q) noexcept
6973
{
7074
blst_p1 r;
@@ -77,14 +81,16 @@ blst_p1_affine add_or_double(const blst_p1_affine& p, const blst_p1& q) noexcept
7781
blst_p1 mult(const blst_p1& p, const blst_scalar& v) noexcept
7882
{
7983
blst_p1 r;
80-
blst_p1_mult(&r, &p, v.b, 255);
84+
blst_p1_mult(&r, &p, v.b, BLS_MODULUS_BITS);
8185
return r;
8286
}
8387

84-
blst_p2_affine add(const blst_p2_affine& p, const blst_p2& q) noexcept
88+
/// Add two points from E2 and convert the result to affine form.
89+
/// The conversion to affine is very costly so use only if the affine of the result is needed.
90+
blst_p2_affine add_or_double(const blst_p2_affine& p, const blst_p2& q) noexcept
8591
{
8692
blst_p2 r;
87-
blst_p2_add_affine(&r, &q, &p);
93+
blst_p2_add_or_double_affine(&r, &q, &p);
8894
blst_p2_affine ra;
8995
blst_p2_to_affine(&ra, &r);
9096
return ra;
@@ -93,7 +99,7 @@ blst_p2_affine add(const blst_p2_affine& p, const blst_p2& q) noexcept
9399
blst_p2 mult(const blst_p2& p, const blst_scalar& v) noexcept
94100
{
95101
blst_p2 r;
96-
blst_p2_mult(&r, &p, v.b, 255);
102+
blst_p2_mult(&r, &p, v.b, BLS_MODULUS_BITS);
97103
return r;
98104
}
99105

@@ -108,12 +114,12 @@ bool pairings_verify(
108114
}
109115
} // namespace
110116

111-
bool kzg_verify_proof(const std::byte versioned_hash[32], const std::byte z[32],
117+
bool kzg_verify_proof(const std::byte versioned_hash[VERSIONED_HASH_SIZE], const std::byte z[32],
112118
const std::byte y[32], const std::byte commitment[48], const std::byte proof[48]) noexcept
113119
{
114120
std::byte computed_versioned_hash[32];
115121
sha256(computed_versioned_hash, commitment, 48);
116-
computed_versioned_hash[0] = std::byte{0x01};
122+
computed_versioned_hash[0] = VERSIONED_HASH_VERSION_KZG;
117123
if (!std::ranges::equal(std::span{versioned_hash, 32}, computed_versioned_hash))
118124
return false;
119125

@@ -147,7 +153,7 @@ bool kzg_verify_proof(const std::byte versioned_hash[32], const std::byte z[32],
147153
const auto neg_Z = mult(G2_GENERATOR_NEGATIVE, *zz);
148154

149155
// Compute X - Z which is [s - z]₂.
150-
const auto X_sub_Z = add(KZG_SETUP_G2_1, neg_Z);
156+
const auto X_sub_Z = add_or_double(KZG_SETUP_G2_1, neg_Z);
151157

152158
// e(C - [y]₁, [1]₂) =? e(Pi, [s - z]₂)
153159
return pairings_verify(C_sub_Y, *Pi, X_sub_Z);

lib/evmone_precompiles/kzg.hpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
#pragma once
2-
#include <cstddef>
2+
#include "sha256.hpp"
3+
#include <intx/intx.hpp>
34

45
namespace evmone::crypto
56
{
6-
bool kzg_verify_proof(const std::byte versioned_hash[32], const std::byte z[32],
7+
using intx::operator""_u256;
8+
9+
/// Length (in bytes) of the versioned hash (based on SHA256).
10+
constexpr auto VERSIONED_HASH_SIZE = SHA256_HASH_SIZE;
11+
12+
/// The KZG version number of the versioned hash.
13+
constexpr std::byte VERSIONED_HASH_VERSION_KZG{0x01};
14+
15+
/// An EIP-4844 parameter.
16+
constexpr auto FIELD_ELEMENTS_PER_BLOB = 4096_u256;
17+
18+
/// Scalar field modulus of BLS12-381.
19+
constexpr auto BLS_MODULUS =
20+
52435875175126190479447740508185965837690552500527637822603658699938581184513_u256;
21+
22+
/// Number of significant bits of the BLS_MODULUS.
23+
constexpr size_t BLS_MODULUS_BITS = 255;
24+
static_assert(BLS_MODULUS >> BLS_MODULUS_BITS == 0);
25+
26+
bool kzg_verify_proof(const std::byte versioned_hash[VERSIONED_HASH_SIZE], const std::byte z[32],
727
const std::byte y[32], const std::byte commitment[48], const std::byte proof[48]) noexcept;
8-
}
28+
} // namespace evmone::crypto

test/precompiles_bench/precompiles_bench.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const inline std::array inputs<PrecompileId::ecpairing>{
124124

125125
template <>
126126
const inline std::array inputs<PrecompileId::point_evaluation>{
127-
// Inputs taken randomely from Mainnet
127+
// Inputs taken randomly from Mainnet
128128
// https://etherscan.io/address/0x000000000000000000000000000000000000000a/advanced#internaltx
129129
"012b08a0504a63aac18383db69fe6b52fc833e3d060b87c2726c4140c909d91807dddd3c80995c2bb3012943e2036e77490b1f6ddc58ca39a4fb4f3225ae56ab11dc2c4d89f777f0f5c2a51f45b73ff1538761f9cf23ed74c74472fea625ad8bace1db77e25ceb316d914182e05dd810f112352e1d6ed9e47af28e2f64e22b94c411794359c2273bc10bc0390963fb1a97bb642307bfa4424c66bd90ecc0ecffd5045e492b40304df20346693db7450457e2c72588a6a2b1a16909e2ab1e6284"_hex,
130130
"019cd755316533108b9eade41e35a16442ae76acd5b7d4e8903ecb9d9f48348a00000000000000000000000000000000dd372dcb4e5565861fc29cfb12f4373861e6e2dfca75084191a505f7988db8e82a4a4a09734b6fd7677d590a1cb512768c381fc4957f406ef89996d9dfa1d39b5c8d1368569e56fd61036c537400a3f4515eeb0c4d183142daa2c30423e0c3fa84667445c1669d3a3e3fce8a1144811e4452841399318c21cca9d20c91fb162929c4e96d391b70158bcd4c69b682b272"_hex,

test/state/precompiles.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,6 @@ ExecutionResult blake2bf_execute(const uint8_t* input, [[maybe_unused]] size_t i
293293
return {EVMC_SUCCESS, sizeof(h)};
294294
}
295295

296-
namespace
297-
{
298-
using intx::operator""_u256;
299-
constexpr auto FIELD_ELEMENTS_PER_BLOB = 4096_u256;
300-
constexpr auto BLS_MODULUS =
301-
52435875175126190479447740508185965837690552500527637822603658699938581184513_u256;
302-
} // namespace
303-
304296
ExecutionResult point_evaluation_execute(const uint8_t* input, size_t input_size, uint8_t* output,
305297
[[maybe_unused]] size_t output_size) noexcept
306298
{
@@ -317,8 +309,10 @@ ExecutionResult point_evaluation_execute(const uint8_t* input, size_t input_size
317309
if (!r)
318310
return {EVMC_PRECOMPILE_FAILURE, 0};
319311

320-
intx::be::unsafe::store(output, FIELD_ELEMENTS_PER_BLOB);
321-
intx::be::unsafe::store(output + 32, BLS_MODULUS);
312+
// Return FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS as padded 32 byte big endian values
313+
// as required by the EIP-4844.
314+
intx::be::unsafe::store(output, crypto::FIELD_ELEMENTS_PER_BLOB);
315+
intx::be::unsafe::store(output + 32, crypto::BLS_MODULUS);
322316
return {EVMC_SUCCESS, 64};
323317
}
324318

test/unittests/precompiles_kzg_test.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
#include <evmc/evmc.hpp>
6-
#include <evmc/hex.hpp>
76
#include <evmone_precompiles/kzg.hpp>
87
#include <evmone_precompiles/sha256.hpp>
98
#include <gtest/gtest.h>
@@ -30,7 +29,7 @@ auto versioned_hash(std::span<const std::byte> input) noexcept
3029
{
3130
std::array<std::byte, 32> hash{};
3231
sha256(hash.data(), input.data(), input.size());
33-
hash[0] = std::byte{1};
32+
hash[0] = VERSIONED_HASH_VERSION_KZG;
3433
return hash;
3534
}
3635
} // namespace

0 commit comments

Comments
 (0)