free earlier when both subsequent branches will free. Throw appropriately on verify_kzg_proof failure

This commit is contained in:
dancoffman 2022-11-04 14:12:11 -07:00
parent d007bf79d0
commit fa8eebbc0b
No known key found for this signature in database
GPG Key ID: 47B1F53E36A9B3CC
1 changed files with 7 additions and 10 deletions

View File

@ -170,16 +170,14 @@ Napi::Value ComputeAggregateKzgProof(const Napi::CallbackInfo& info) {
blobs_count, blobs_count,
kzg_settings kzg_settings
); );
free(blobs);
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {
Napi::Error::New(env, "Failed to compute proof") Napi::Error::New(env, "Failed to compute proof")
.ThrowAsJavaScriptException(); .ThrowAsJavaScriptException();
free(blobs);
return env.Undefined(); return env.Undefined();
}; };
free(blobs);
uint8_t proof_bytes[BYTES_PER_PROOF]; uint8_t proof_bytes[BYTES_PER_PROOF];
bytes_from_g1(proof_bytes, &proof); bytes_from_g1(proof_bytes, &proof);
return napi_typed_array_from_bytes(proof_bytes, BYTES_PER_PROOF, env); return napi_typed_array_from_bytes(proof_bytes, BYTES_PER_PROOF, env);
@ -256,10 +254,11 @@ Napi::Value VerifyAggregateKzgProof(const Napi::CallbackInfo& info) {
&proof, &proof,
kzg_settings kzg_settings
); );
if (ret != C_KZG_OK) {
free(commitments); free(commitments);
free(blobs); free(blobs);
if (ret != C_KZG_OK) {
Napi::Error::New( Napi::Error::New(
env, env,
"verify_aggregate_kzg_proof failed with error code: " + std::to_string(ret) "verify_aggregate_kzg_proof failed with error code: " + std::to_string(ret)
@ -267,9 +266,6 @@ Napi::Value VerifyAggregateKzgProof(const Napi::CallbackInfo& info) {
return env.Null(); return env.Null();
} }
free(commitments);
free(blobs);
return Napi::Boolean::New(env, verification_result); return Napi::Boolean::New(env, verification_result);
} }
@ -331,7 +327,8 @@ Napi::Value VerifyKzgProof(const Napi::CallbackInfo& info) {
bool out; bool out;
if (verify_kzg_proof(&out, &commitment, &fz, &fy, &proof, kzg_settings) != C_KZG_OK) { if (verify_kzg_proof(&out, &commitment, &fz, &fy, &proof, kzg_settings) != C_KZG_OK) {
return Napi::Boolean::New(env, false); Napi::TypeError::New(env, "Failed to verify KZG proof").ThrowAsJavaScriptException();
return env.Null();
} }
return Napi::Boolean::New(env, out); return Napi::Boolean::New(env, out);