Skip to content

Commit

Permalink
Require size of serialized compressed forms to be 100 bytes
Browse files Browse the repository at this point in the history
The size is the same regardless of discriminant bits or a form being
'special' (identity or generator). This allows using a fixed-size object
(bytes100) for quadratic forms in chia-blockchain.
  • Loading branch information
rostislav authored and hoffmang9 committed Feb 10, 2021
1 parent 15ffad4 commit 5825ec0
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/bqfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/bqfc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions src/proof_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ integer HashPrime(std::vector<uint8_t> seed, int length, vector<int> bitmask) {
std::vector<unsigned char> SerializeForm(form &y, int d_bits)
{
y.reduce();
int form_size = bqfc_get_compr_size(d_bits);
std::vector<unsigned char> res(form_size);
std::vector<unsigned char> res(BQFC_FORM_SIZE);
bqfc_serialize(res.data(), y.a.impl, y.b.impl, d_bits);
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/vdf_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/verifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5825ec0

Please sign in to comment.