-
Notifications
You must be signed in to change notification settings - Fork 206
make mp_to_radix return the count of characters of the converted number #348
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
make mp_to_radix return the count of characters of the converted number #348
Conversation
First problem is, as you can see, are the macros but that was expected and is, I think, solvable. |
I find it a bit awkward that way (there are alternatives which I find equally or even more odd, but YMMV, so drop a note, please). A cleaner way, I think, would be to add another parameter, maybe mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *used_length, int radix) or something in that line. |
what do you find awkward? What do you think about replacing
|
Removing radix_size is a good idea. We could still branch to an O(1) size computation function internally if buf==NULL. |
The function in #343 could be used internally. |
@sjaeckel what shall we do about the to_signed_bin functions? They also need a size argument. These functions should probably be treated differently since size computation is cheaper. But I don't like the difference in API now. |
I'd say either we drop the original one or we keep them both through a compile-time setting if someone doesn't want to spend the space for the tables
very good point! IMO we should deprecate the ones w/o length |
Just introduce functions with new names mp_to_ubin and mp_to_sbin? |
Tables has about 14k (depending a bit on MP_xBIT but not much), original about 3k , and the one with
My social interactions.
That would be quite radical aaaaaand I like it! ;-) |
Hmm, I worked a bit on mp_to_radix and I am not sure if deprecating mp_radix_size is the right way. We have the following scenarios:
In particular the overloading in the case of buffer overflow is not nice. We could always return the needed length, but then the division would have to continue even if no chars are written anymore. My recommendation would be to not even merge this PR and just keep the length argument as value. The user can compute the number of written characters themselves. They know their buffer size and they know mp_radix_size. This doesn't sound userfriendly either, but I don't think this is a common use case. The standard use case is 1. calling mp_radix_size 2. allocating a buffer 2. calling mp_to_radix. The second use case is just calling mp_to_radix with a static buffer and hoping that the buffer is large enough (maybe falling back to allocating if not). |
The exact details are debatable, of course, but in general it is a good idea to get rid of it. Since we got No
I understood it in the way that that case is meant to replace mp_err mp_to_radix(const mp_int *a, char *str, size_t *length_written, size_t size, int radix) with
would be better.
Could you be a bit more specific, please? |
@czurnieden I think a function which allocates would also be acceptable, similar to what gmp offers.
As of now mp_to_unsigned_bit, mp_unsigned_size, mp_to_radix, mp_radix_size etc all have a similar API. We should decide if we want to have the functions consistent. Should they all take buffers and allocate if no buffer is given? What shall they do if the buffer is not large enough? |
EDIT: See updated proposal below
As @czurnieden proposed above, the |
5e7f7c5
to
fcaa30f
Compare
A quick try at the propositions. To be debated. There is a |
Took the liberty to add "work in progress" because that's what it is, isn't it? |
I'm skeptic about the pattern-change from "alloc outside" to "alloc inside", but I somehow like it. I'd say if we do it like that we won't need the size_t mp_ubin_size(const mp_int *a) MP_WUR;
mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
mp_err mp_to_ubin(const mp_int *a, unsigned char **buf, size_t *len) MP_WUR;
size_t mp_sbin_size(const mp_int *a) MP_WUR;
mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
mp_err mp_to_sbin(const mp_int *a, unsigned char **buf, size_t *len) MP_WUR;
mp_err mp_radix_size(const mp_int *a, size_t *size, unsigned radix) MP_WUR;
mp_err mp_from_radix(mp_int *a, const char *str, unsigned radix) MP_WUR;
mp_err mp_to_radix(const mp_int *a, char **str, size_t *len, unsigned radix) MP_WUR; where
|
No realloc! This is a bad idea! |
then no allocation inside IMO |
we can also do a |
Fixed. It was my fault: a typo that I wasn't able to recognize for some reason, despite it—the typo, that is—screaming at me the whole time. Yes, I'm an idiot, sue me ;-) |
…ecated mp_toradix_n
569b1ee
to
bd46b60
Compare
Status? Could you please squash? |
We are only waiting for your OK, so if you are OK with it, drop me a note and I will squash&finish. |
bd46b60
to
ececff7
Compare
Looks good |
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.
LGTM
bn_s_mp_reverse.c
Outdated
@@ -6,7 +6,7 @@ | |||
/* reverse an array, used for radix code */ | |||
void s_mp_reverse(unsigned char *s, int len) |
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.
oh I just realized that it isn't done here... can you please change len
to be size_t
b3fc059
to
0902fd2
Compare
Squash, rebase and fix CI? But besides that I approve. |
CI? |
0902fd2
to
8ba6283
Compare
Oh, you mean Travis (continuous integration)? Because I just cannot resist: it got a bit down here with the new owner, didn't it? >;-> |
8ba6283
to
1ceea2c
Compare
Ah, no that was over at #348 |
1ceea2c
to
e11b9c8
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.
Looks good
e11b9c8
to
71d1b7b
Compare
@czurnieden thanks :) @sjaeckel from my side this looks ready. |
As suggested by @sjaeckel in #343
Mmh…