-
Notifications
You must be signed in to change notification settings - Fork 86
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
bignum_from_string: only works with hex-strings w/ multiple of 8 characters #14
Comments
Hi @DEF7 and thanks for your interest in this project :) You're correct that the to/from_string methods only works for a 8-bite chunks, but this is by design. I only use the functions for the test-code, which I hint at in the readme:
I agree. I would most likely accept a PR that improved the current situation :) |
Ok. In the meantime I'll just pad the beginning of my hex strings with
zeroes which seems to work fine for now. Thanks!
…On Mon, Jan 20, 2020 at 8:09 AM kokke ***@***.***> wrote:
Hi @DEF7 <https://github.com/DEF7> and thanks for your interest in this
project :)
You're correct that the to/from_string methods only works for a 8-bite
chunks, but this is by design.
I only use the functions for the test-code, which I hint at in the readme: **stdio.h
is only used for testing functions parsing to and from hex-strings.**.
This is the reason for not implementing better to/from_string methods --
they are only used for the testing.
Custom to/from string routines would be better.
I agree. I would most likely accept a PR that improved the current
situation :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#14?email_source=notifications&email_token=ACYGB5KE4LJCCWBACS4HB73Q6XEDJA5CNFSM4KJGGWJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJND4ZQ#issuecomment-576339558>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACYGB5IC2GGINBAONB6TDOTQ6XEDJANCNFSM4KJGGWJQ>
.
|
Until further notice, I think it's fair to add this piece of assertion-code in require((nbytes % (sizeof(DTYPE) * 2)) == 0, "string length must be a multiple of (sizeof(DTYPE) * 2) characters"); , ... so that you at least get a proper failure/crash with a line number etc., if you mess up: ,
What do you think @DEF7 ? |
The problem lies with this piece of code:
tiny-bignum-c/bn.c
Lines 108 to 120 in e814d2b
which appears as:
`
int i = nbytes - (2 * WORD_SIZE); /* index into string /
int j = 0; / index into array */
/* reading last hex-byte "MSB" from string first -> big endian /
/ MSB ~= most significant byte / block ? :) /
while (i >= 0)
{
tmp = 0;
sscanf(&str[i], SSCANF_FORMAT_STR, &tmp);
n->array[j] = tmp;
i -= (2 * WORD_SIZE); / step WORD_SIZE hex-byte(s) back in the string. /
j += 1; / step one element forward in the array. */
}
`
If 'i' (the length of the inbound string, in characters) is not a multiple of '2 * WORD_SIZE' then it will skip over the MSB hexadecimal characters at the beginning of the string when 'i < 0' and miss the characters at the beginning of the inbound string, incorrectly setting 'n'.
I think the use of sprintf/sscanf is not ideal. Custom to/from string routines would be better.
The text was updated successfully, but these errors were encountered: