Skip to content

introduce mp_set_sint and mp_set_uint functions with precise types #285

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

Merged
merged 5 commits into from
May 24, 2019

Conversation

minad
Copy link
Member

@minad minad commented May 21, 2019

This is a bit of a compromise now.

  • The API is biased toward 32 bit (mp_get_sint, mp_get_uint, ...).
  • 64 bit variants are provided (mp_get_sint64)
  • Unsigned getters return two complement for negative numbers
  • There are an unsigned getters for the magnitudes only (mp_get_mag, mp_get_mag64) - these are just like the current mp_get_long mp_get_long_long
  • There are aliases for long and unsigned long (mp_get_ulong, mp_get_slong, ...) since long is problematic if porting between 32/64 bit platforms. Furthermore since the current API uses long, this would make things easier. But maybe these should also go to not again introduce legacy cruft.
  • mp_set and mp_init_set are NOT deprecated in order to not penalize programs on platforms with int < 32 bit. This is still the most efficient function.

Why are these changes needed?

  • signed setters/getters are a new addition
  • unsigned setters return two complement such that round tripping and casting works seemless with c integers
  • the new setters never fail, the current setters return mp_err
  • the new functions use precise types (the api is robust under platform changes)
/**** NEW ****/
/* get integer, set integer and init with integer (int32_t) */
int32_t mp_get_sint(const mp_int *a) MP_WUR;
void mp_set_sint(mp_int *a, int32_t b);
mp_err mp_init_sint(mp_int *a, int32_t b) MP_WUR;

/**** NEW ****/
/* get integer, set integer and init with integer (int64_t) */
int64_t mp_get_sint64(const mp_int *a) MP_WUR;
void mp_set_sint64(mp_int *a, int64_t b);

/**** CHANGED API: More precise replacements for mp_set_int, mp_set_long, mp_get_int, two complement ****/
/* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint32_t) */
#define mp_get_uint(a) ((uint32_t)mp_get_sint(a))
void mp_set_uint(mp_int *a, uint32_t b);
mp_err mp_init_uint(mp_int *a, uint32_t b) MP_WUR;

/**** CHANGED API: More precise replacements for mp_set_int, mp_set_long, mp_get_int, two complement ****/
/* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint64_t) */
#define mp_get_uint64(a) ((uint64_t)mp_get_sint64(a))
void mp_set_uint64(mp_int *a, uint64_t b);

/***** CHANGED API: More precise replacement for mp_get_long/long_long ****/
/* get magnitude */
uint32_t mp_get_mag(const mp_int *a) MP_WUR;
uint64_t mp_get_mag64(const mp_int *a) MP_WUR;

/**** COMPATIBILITY API, needed? ****/
/* get integer, set integer (long) */
#define mp_get_slong(a)        (sizeof (long) == 8 ? (long)mp_get_sint64(a) : (long)mp_get_sint(a))
#define mp_set_slong(a, b)     (sizeof (long) == 8 ? mp_set_sint64((a), (b)) : mp_set_sint((a), (int32_t)(b)))

/* get integer, set integer (unsigned long) */
#define mp_get_ulong(a)       (sizeof (long) == 8 ? (unsigned long)mp_get_uint64(a) : (unsigned long)mp_get_ulong(a))
#define mp_get_magl(a)     (sizeof (long) == 8 ? (unsigned long)mp_get_mag64(a) : (unsigned long)mp_get_mag(a))
#define mp_set_ulong(a, b)    (sizeof (long) == 8 ? mp_set_uint64((a), (b)) : mp_set_uint((a), (uint32_t)(b)))

/* set to single unsigned digit, only 8 bit guaranteed */
void mp_set(mp_int *a, mp_digit b);
mp_err mp_init_set(mp_int *a, mp_digit b) MP_WUR;

/* get integer, set integer and init with integer (deprecated) */
MP_DEPRECATED(mp_get_mag/mp_get_uint) unsigned long mp_get_int(const mp_int *a) MP_WUR;
MP_DEPRECATED(mp_get_magl/mp_get_ulong) unsigned long mp_get_long(const mp_int *a) MP_WUR;
MP_DEPRECATED(mp_get_mag64/mp_get_uint64) unsigned long long mp_get_long_long(const mp_int *a) MP_WUR;
MP_DEPRECATED(mp_set_uint) mp_err mp_set_int(mp_int *a, unsigned long b);
MP_DEPRECATED(mp_set_ulong) mp_err mp_set_long(mp_int *a, unsigned long b);
MP_DEPRECATED(mp_set_uint64) mp_err mp_set_long_long(mp_int *a, unsigned long long b);
MP_DEPRECATED(mp_init_uint) mp_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR;

@minad minad changed the title [WIP] introduce various mp_set_i and mp_set_u functions with precise … [WIP] introduce mp_set_i and mp_set_u functions with precise types May 21, 2019
Copy link
Contributor

@czurnieden czurnieden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did know that I would insist on documentation, didn't you? ;-)
But otherwise: yepp!

@minad
Copy link
Member Author

minad commented May 22, 2019

Tests are also missing. Maybe I have time later today to fix those things.

EDIT: Btw. I changed the names again to be more in line with what tommath looks now. @czurnieden are you ok with it?

@minad minad changed the title [WIP] introduce mp_set_i and mp_set_u functions with precise types [WIP] introduce mp_set_sint and mp_set_uint functions with precise types May 22, 2019
@minad minad requested a review from czurnieden May 22, 2019 09:28
@minad
Copy link
Member Author

minad commented May 22, 2019

@czurnieden Updated docs + tests

@minad minad changed the title [WIP] introduce mp_set_sint and mp_set_uint functions with precise types introduce mp_set_sint and mp_set_uint functions with precise types May 22, 2019
@minad minad requested a review from sjaeckel May 22, 2019 10:50
@minad minad mentioned this pull request May 22, 2019
@minad
Copy link
Member Author

minad commented May 22, 2019

@czurnieden Ok now?

Copy link
Contributor

@czurnieden czurnieden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok now?

Yepp.
At least I couldn't find more ;-)

@minad
Copy link
Member Author

minad commented May 23, 2019

@sjaeckel There you go

@minad
Copy link
Member Author

minad commented May 23, 2019

@sjaeckel I made another proposal, _l and _ul suffix for the long macros.

Maybe also use i32/u32 then for shorter names?

@sjaeckel sjaeckel merged commit 20ae9fb into develop May 24, 2019
@sjaeckel sjaeckel deleted the set-int3 branch May 24, 2019 09:23
@sjaeckel
Copy link
Member

that looks very good now! I like it

@minad
Copy link
Member Author

minad commented May 24, 2019

Thanks

@minad
Copy link
Member Author

minad commented May 24, 2019

If you have time to merge other stuff, I consider #273, #288, #290 and #291 as ready

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

Successfully merging this pull request may close these issues.

3 participants