1
1
#include " kzg.hpp"
2
- #include " sha256.hpp"
3
2
#include < blst.h>
4
3
#include < algorithm>
5
4
#include < optional>
@@ -60,11 +59,16 @@ std::optional<blst_p1_affine> validate_G1(std::span<const std::byte, 48> b) noex
60
59
blst_p1_affine r;
61
60
if (blst_p1_uncompress (&r, reinterpret_cast <const uint8_t *>(b.data ())) != BLST_SUCCESS)
62
61
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))
64
66
return std::nullopt;
65
67
return r;
66
68
}
67
69
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.
68
72
blst_p1_affine add_or_double (const blst_p1_affine& p, const blst_p1& q) noexcept
69
73
{
70
74
blst_p1 r;
@@ -77,14 +81,16 @@ blst_p1_affine add_or_double(const blst_p1_affine& p, const blst_p1& q) noexcept
77
81
blst_p1 mult (const blst_p1& p, const blst_scalar& v) noexcept
78
82
{
79
83
blst_p1 r;
80
- blst_p1_mult (&r, &p, v.b , 255 );
84
+ blst_p1_mult (&r, &p, v.b , BLS_MODULUS_BITS );
81
85
return r;
82
86
}
83
87
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
85
91
{
86
92
blst_p2 r;
87
- blst_p2_add_affine (&r, &q, &p);
93
+ blst_p2_add_or_double_affine (&r, &q, &p);
88
94
blst_p2_affine ra;
89
95
blst_p2_to_affine (&ra, &r);
90
96
return ra;
@@ -93,7 +99,7 @@ blst_p2_affine add(const blst_p2_affine& p, const blst_p2& q) noexcept
93
99
blst_p2 mult (const blst_p2& p, const blst_scalar& v) noexcept
94
100
{
95
101
blst_p2 r;
96
- blst_p2_mult (&r, &p, v.b , 255 );
102
+ blst_p2_mult (&r, &p, v.b , BLS_MODULUS_BITS );
97
103
return r;
98
104
}
99
105
@@ -108,12 +114,12 @@ bool pairings_verify(
108
114
}
109
115
} // namespace
110
116
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 ],
112
118
const std::byte y[32 ], const std::byte commitment[48 ], const std::byte proof[48 ]) noexcept
113
119
{
114
120
std::byte computed_versioned_hash[32 ];
115
121
sha256 (computed_versioned_hash, commitment, 48 );
116
- computed_versioned_hash[0 ] = std::byte{ 0x01 } ;
122
+ computed_versioned_hash[0 ] = VERSIONED_HASH_VERSION_KZG ;
117
123
if (!std::ranges::equal (std::span{versioned_hash, 32 }, computed_versioned_hash))
118
124
return false ;
119
125
@@ -147,7 +153,7 @@ bool kzg_verify_proof(const std::byte versioned_hash[32], const std::byte z[32],
147
153
const auto neg_Z = mult (G2_GENERATOR_NEGATIVE, *zz);
148
154
149
155
// 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);
151
157
152
158
// e(C - [y]₁, [1]₂) =? e(Pi, [s - z]₂)
153
159
return pairings_verify (C_sub_Y, *Pi, X_sub_Z);
0 commit comments