Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bn_mp_grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int mp_grow(mp_int *a, int size)
/* if the alloc size is smaller alloc more ram */
if (a->alloc < size) {
/* ensure there are always at least MP_PREC digits extra on top */
size += (MP_PREC * 2) - (size % MP_PREC);
size += ((MP_PREC * 2) - 1) - ((size + (MP_PREC - 1)) % MP_PREC);

/* reallocate the array a->dp
*
Expand Down
4 changes: 2 additions & 2 deletions bn_mp_init_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
/* init an mp_init for a given size */
int mp_init_size(mp_int *a, int size)
{
/* pad size so there are always extra digits */
size += (MP_PREC * 2) - (size % MP_PREC);
/* ensure there are always at least MP_PREC digits extra on top */
size += ((MP_PREC * 2) - 1) - ((size + (MP_PREC - 1)) % MP_PREC);

/* alloc mem */
a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit));
Expand Down
13 changes: 7 additions & 6 deletions bn_mp_shrink.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
int mp_shrink(mp_int *a)
{
mp_digit *tmp;
int used = 1;
int alloc = MP_PREC;
Copy link
Member

Choose a reason for hiding this comment

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

this still shrink's only down to MP_PREC and not the required storage for a long long

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the requirement of MP_PREC should be that it can store at least a long long. That's what the change in tommath.h was meant for! mp_init() also starts with allocating MP_PREC digits.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So, for MP_8BIT, the minimum MP_PREC=16, for MP_16BIT, the minimum MP_PREC=8, for MP_32BIT the minimum MP_PREC=4, for MP_64BIT it is MP_PREC=2. That should be documented somewhere.

Copy link
Member

Choose a reason for hiding this comment

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

@czurnieden what do you say?

Copy link
Member

@minad minad Apr 12, 2019

Choose a reason for hiding this comment

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

What about adding a MP_MIN_PREC setting to the minimums given by @nijtmans and changing MP_PREC such that it is always larger than MP_MIN_PREC?
Furthermore I think MP_PREC is usually too large. But this depends heavly on the use case. Via static_assert it could be checked that the condition always holds if MP_PREC is set by the user.
Maybe just set MP_MIN_PREC to 16 / sizeof (mp_digit)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How about an assert somewhere in the code checking that MP_PREC * sizeof(mp_digit) >= sizeof(long long) ???

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hm... I meant MP_PREC * DIGIT_BIT >= sizeof(long long). That means that MP_PREC for MP_8BIT could even be as low as 10, although that's not a power of 2.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hm .... MP_PREC * DIGIT_BIT >= CHAR_BIT * sizeof(long long)

Copy link
Member

Choose a reason for hiding this comment

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

How about an assert somewhere in the code checking that MP_PREC * sizeof(mp_digit) >= sizeof(long long) ???

no we need to make it sure at compile time

I'd make it #define MP_MIN_PREC ((CHAR_BIT * sizeof(long long))/DIGIT_BIT) and hope that nobody uses this macro in a #if preprocessor statement :)

and please keep the patch to to tommath.h regarding MP_8BIT

Copy link
Contributor

Choose a reason for hiding this comment

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

czurnieden what do you say?

A MP_PREC that is always of the same bitsize no matter what the MP_xBITis would add nicely to the "rounding edges" part of the LTM update, yes.


if (a->used > 0) {
used = a->used;
if (a->used > MP_PREC) {
alloc = a->used;
alloc += (MP_PREC - 1) - ((alloc + (MP_PREC - 1)) % MP_PREC);
}

if (a->alloc != used) {
if (a->alloc != alloc) {
if ((tmp = (mp_digit *) MP_REALLOC(a->dp,
(size_t)a->alloc * sizeof(mp_digit),
(size_t)used * sizeof(mp_digit))) == NULL) {
(size_t)alloc * sizeof(mp_digit))) == NULL) {
return MP_MEM;
}
a->dp = tmp;
a->alloc = used;
a->alloc = alloc;
}
return MP_OKAY;
}
Expand Down
2 changes: 2 additions & 0 deletions tommath.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ extern int KARATSUBA_MUL_CUTOFF,
#ifndef MP_PREC
# ifndef MP_LOW_MEM
# define MP_PREC 32 /* default digits of precision */
# elif defined(MP_8BIT)
# define MP_PREC 16 /* default digits of precision */
# else
# define MP_PREC 8 /* default digits of precision */
# endif
Expand Down
18 changes: 7 additions & 11 deletions tommath_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,14 @@ extern const size_t mp_s_rmap_reverse_sz;
int func_name (mp_int * a, type b) \
{ \
int x = 0; \
int new_size = (((CHAR_BIT * sizeof(type)) + DIGIT_BIT) - 1) / DIGIT_BIT; \
int res = mp_grow(a, new_size); \
if (res == MP_OKAY) { \
mp_zero(a); \
while (b != 0u) { \
a->dp[x++] = ((mp_digit)b & MP_MASK); \
if ((CHAR_BIT * sizeof (b)) <= DIGIT_BIT) { break; } \
b >>= (((CHAR_BIT * sizeof (b)) <= DIGIT_BIT) ? 0 : DIGIT_BIT); \
} \
a->used = x; \
mp_zero(a); \
while (b != 0u) { \
a->dp[x++] = ((mp_digit)b & MP_MASK); \
if ((CHAR_BIT * sizeof (b)) <= DIGIT_BIT) { break; } \
b >>= (((CHAR_BIT * sizeof (b)) <= DIGIT_BIT) ? 0 : DIGIT_BIT); \
} \
return res; \
a->used = x; \
return MP_OKAY; \
}

/* deprecated functions */
Expand Down