diff --git a/src/bqfc.c b/src/bqfc.c index 7ce9425f..2d4e7a22 100644 --- a/src/bqfc.c +++ b/src/bqfc.c @@ -216,9 +216,11 @@ int bqfc_serialize(uint8_t *out_str, mpz_t a, mpz_t b, size_t d_bits) { struct qfb_c f_c; int ret; + int valid_size = bqfc_get_compr_size(d_bits); if (!mpz_cmp_ui(b, 1) && mpz_cmp_ui(a, 2) <= 0) { out_str[0] = !mpz_cmp_ui(a, 2) ? BQFC_IS_GEN : BQFC_IS_1; + memset(&out_str[1], 0, BQFC_FORM_SIZE - 1); return 0; } @@ -228,6 +230,8 @@ int bqfc_serialize(uint8_t *out_str, mpz_t a, mpz_t b, size_t d_bits) goto out; ret = bqfc_serialize_only(out_str, &f_c, d_bits); + if (valid_size != BQFC_FORM_SIZE) + memset(&out_str[valid_size], 0, BQFC_FORM_SIZE - valid_size); out: mpz_clears(f_c.a, f_c.t, f_c.g, f_c.b0, NULL); return ret; @@ -238,7 +242,7 @@ int bqfc_deserialize(mpz_t out_a, mpz_t out_b, const mpz_t D, const uint8_t *str struct qfb_c f_c; int ret; - if (!size) + if (size != BQFC_FORM_SIZE) return -1; /* "Identity" (1, 1) and "generator" (2, 1) forms are serialized with a @@ -249,9 +253,6 @@ int bqfc_deserialize(mpz_t out_a, mpz_t out_b, const mpz_t D, const uint8_t *str return 0; } - if (size != bqfc_get_compr_size(d_bits)) - return -1; - mpz_inits(f_c.a, f_c.t, f_c.g, f_c.b0, NULL); ret = bqfc_deserialize_only(&f_c, str, d_bits); if (ret) diff --git a/src/bqfc.h b/src/bqfc.h index caf742cd..44217c6b 100644 --- a/src/bqfc.h +++ b/src/bqfc.h @@ -15,6 +15,9 @@ struct qfb_c { bool b_sign; }; +#define BQFC_MAX_D_BITS 1024 +/* Force all forms to have the same size (100 bytes). */ +#define BQFC_FORM_SIZE ((BQFC_MAX_D_BITS + 31) / 32 * 3 + 4) int bqfc_compr(struct qfb_c *out_c, mpz_t a, mpz_t b); diff --git a/src/proof_common.h b/src/proof_common.h index 07b7368e..d55bb357 100644 --- a/src/proof_common.h +++ b/src/proof_common.h @@ -60,8 +60,7 @@ integer HashPrime(std::vector seed, int length, vector bitmask) { std::vector SerializeForm(form &y, int d_bits) { y.reduce(); - int form_size = bqfc_get_compr_size(d_bits); - std::vector res(form_size); + std::vector res(BQFC_FORM_SIZE); bqfc_serialize(res.data(), y.a.impl, y.b.impl, d_bits); return res; } diff --git a/src/vdf_client.cpp b/src/vdf_client.cpp index ac951a7c..e79c9980 100644 --- a/src/vdf_client.cpp +++ b/src/vdf_client.cpp @@ -78,7 +78,7 @@ void CreateAndWriteProofTwoWeso(integer& D, form f, uint64_t iters, TwoWesolowsk WriteProof(iters, result, sock); } -char initial_form_s[100]; +char initial_form_s[BQFC_FORM_SIZE]; void InitSession(tcp::socket& sock) { boost::system::error_code error; diff --git a/src/verifier.h b/src/verifier.h index 3b1b0b61..4fd16822 100644 --- a/src/verifier.h +++ b/src/verifier.h @@ -53,7 +53,7 @@ integer ConvertBytesToInt(const uint8_t* bytes, int32_t start_index, int32_t end bool CheckProofOfTimeNWesolowski(integer D, const uint8_t* x_s, const uint8_t* proof_blob, int32_t proof_blob_len, uint64_t iterations, uint64 disc_size_bits, int32_t depth) { - int form_size = bqfc_get_compr_size(D.num_bits()); + int form_size = BQFC_FORM_SIZE; form x = DeserializeForm(D, x_s, form_size); if (proof_blob_len != 2 * form_size + depth * (8 + 2 * form_size)) diff --git a/tests/test_verifier.py b/tests/test_verifier.py index b87cd448..6218e776 100644 --- a/tests/test_verifier.py +++ b/tests/test_verifier.py @@ -7,8 +7,8 @@ def test_prove_and_verify(): discriminant_challenge = secrets.token_bytes(10) discriminant_size = 512 discriminant = create_discriminant(discriminant_challenge, discriminant_size) - form_size = discriminant_size // 32 * 3 + 4 - initial_el = bytes([0x08]) + form_size = 100 + initial_el = b"\x08" + (b"\x00" * 99) iters = 1000000 t1 = time.time()