-
Notifications
You must be signed in to change notification settings - Fork 215
mp_export: Addition of buffer size and written bytes #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
See my comment about written. Furthermore if we really want to improve this function, we should add enums for order and endian. How can we handle the deprecation? |
Yepp, would look better.
Good question. The way |
I first thought about renaming it to I also already thought about having one of those replacements for 1.2 and after 1.2 is out change the API of |
|
Hmm, I would rather introduce new names. For example mp_pack/mp_unpack? |
|
mp_to_packed/mp_from_packed would be more consistent with the from_to_bin functions? |
it seems you're also one of the goto guys if one has problems with the second of the two hard problems of CS ;-)
I like the first better... they sound natural, whereas those sound pushed to be consistent... |
26a1dcb to
98eb1eb
Compare
Yepp, sounds good. |
|
What's the status here? Ready? |
If you are OK with it, drop me a note and I will squash&finish. |
aec531d to
ede14c7
Compare
demo/test.c
Outdated
| goto LTM_ERR; | ||
| } | ||
|
|
||
| free(buf); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Free or mp_free? Is this consistent in the test suite
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this consistent in the test suite
(I'll take it as a question) The rest of the tests is done with static buffers didn't want to do it here, although it is possible.
|
There is an endian==0 comparison |
|
Countp should probably be set to the written count? Maybe add a function mp_pack_size which does this mp_count_bits? |
|
An internal helper function for the native endian test woul be nice. Are there more functions which check endianness? |
We have But I do support the proposal to put |
minad
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for me being unclear and complaining too much. I think MP_GET_ENDIAN should be a macro/inline function in tommath_private since it will get optimized away completely.
|
Regarding countp - since we added the maxsize we introduced an additional behavior. In that case we want countp to return the actually written units. This mirrors the behavior of the other functions with written argument. |
That's what I started with but than I read that you wanted a function. |
f716d33 to
cdc0ecd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is still unknown_endian in the docs. I would also like for countp to mirror the behavior of written. But besides that it looks good. Also squash and rebase.
cdc0ecd to
8ef79ca
Compare
bn_mp_pack.c
Outdated
| nail_bytes = nails / 8u; | ||
|
|
||
| bits = (size_t)mp_count_bits(&t); | ||
| count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use mp_pack_size/count here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use mp_pack_size/count here?
Yes, of course.
So you are OK with what I did?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but we still have to decide bytes or units.
Right now countp in units, maxsize in bytes, pack_size in bytes? The same applies to mp_unpack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shuffled it a bit around, probably more to what you wanted in the first place.
I hope ;-)
Argh, documentation not yet updated!
Yes, but we still have to decide bytes or units.
Do you want to name them (enum)?
Otherwise the types can stay as they are now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I don't like it like this. We have count for unpack which is used to pass in the buffer size and for pack we have both maxsize and countp. Either we also change countp/count to bytes or we change maxsize to units. Mixing the units is not good as we have it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I hope I'm up-to-date now with the rebasing)
Hmm I don't like it like this.
Yeah, it's a bit of a mess, admitted.
Either we also change countp/count to bytes or we change maxsize to units.
maxsize (I think it's maxlen elsewhere but I always mix them up) is in all other cases meant to measure the given buffer-size in bytes, a size_t type.
Let me restrict myself to mp_pack for now:
countp does, as not only the name suggests, some (ac)counting. The size in bytes behind countp depends on a second variable (it formerly returned count which were units computed from bitsize and nails) and can be different from the amount of actually used up bytes. so I would say that the expectation is until now that countp returns units.
You won't have a port to get the amount of written bytes if you use countp for units.
On the other side: we have an external function to compute countp so the information thatcountp was once needed for is now available externally and in advance, countp is now free for other uses, e.g.: returning the number bytes written.
It changes the API but it is not much work to write a little macro as a wrapper if so desired.
So for mp_pack I'd propose:
countp(maybe change the name) are the bytes writtenmaxsizeis the size in bytes of the buffer givencountas computed bymp_pack_countreturns the number of units you get with the combination of bit-size and nails given tomp_pack_count. That means that there are no accounts of units send in or out ofmp_pack.
And for mp_unpack
countis not the number of bytes but the number of units, the actual byte-size of the input is only of internal interest but can be computed fromcountandnailsif so desired although I would just measure the input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pack/unpack should also be symmetric. This means count should be bytes/unit for both. However we should also consider that changing from units/bytes will introduce errors for user who do not expect that change when they move to the new function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suggestion would therefore be to call it mp_pack_count, count counts unit, maxcount etc. This also makes sense. The user is not operating on byte arrays but on arrays of unit size (like calloc for example). The only downside is that its different from the sbin/ubin functions which operate on bytes.
8fda366 to
08fc22b
Compare
08fc22b to
06a62aa
Compare
|
@sjaeckel what is your opinion on the bytes vs size-units discussion? |
I would say we keep the semantics as it was. i.e. the proposed API should change like that:
IMO this makes sense as: a call to |
|
I agree with @sjaeckel and I would like to add that mp_pack_count should also return a count. Not a size in bytes. |
|
Maybe also rearrange the args. |
that's what it does already.
there's no real style how ltm treats the order of args, so I'm somehow against reordering only here, but it's true... somehow it'd also make sense to order them like that |
I just thought about it a bit more and yes, please re-order |
|
superseded by #361 |
First sketch.