Skip to content
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

For ease of use there should be a function telling the size of string for bignum_to_string() #30

Open
yurivict opened this issue Sep 24, 2021 · 1 comment

Comments

@yurivict
Copy link

yurivict commented Sep 24, 2021

Otherwise the user of bignum_to_string() should guess how many characters might be needed?

@kokke
Copy link
Owner

kokke commented Dec 22, 2021

Hi @yurivict and thanks for your interest in the project :)

You raise a good question!

Otherwise the user of bignum_to_string() should guess how many characters might be needed?

No guessing is needed, but I completely understand if it's not immediately obvious.

You use two bytes to represent each byte of the bignum (always -> because of hex-string representation).

If you are using e.g. 1024-bit bignums, you need 2048 bits of space + a null-termination byte.
The bignum_to_string function also null-terminates the string for you, so if you pass it "too much memory" it will still work.

From the README (emphasis mine)

No dynamic memory management is utilized, and stdio.h is only used for testing functions parsing to and from hex-strings.

So I consider bignum_to_string and bignum_from_string to be used only for testing and I am not sure I agree that a new function needs to be added.

A quick idea instead would be to make a macro like #define BN_STR_FMT_BUF_SIZE ((2 * BN_ARRAY_SIZE) + 1) or something along that way.

I think that should do the trick, but I haven't tested it - it's just off the top of my head :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants