Skip to content

Conversation

@czurnieden
Copy link
Contributor

First sketch.

@minad minad changed the title Addition of buffer size and written bytes mp_export: Addition of buffer size and written bytes Sep 30, 2019
@minad
Copy link
Member

minad commented Sep 30, 2019

See my comment about written. Furthermore if we really want to improve this function, we should add enums for order and endian. How can we handle the deprecation?

@czurnieden
Copy link
Contributor Author

we should add enums for order and endian.

Yepp, would look better.

How can we handle the deprecation?

Good question. The way bn_deprecated.c works it would need a different function-name, so: rename?

@sjaeckel
Copy link
Member

sjaeckel commented Oct 1, 2019

How can we handle the deprecation?

Good question. The way bn_deprecated.c works it would need a different function-name, so: rename?

I first thought about renaming it to mp_{ex,im}port_gmp(), but that'd be lying as the current implementation is exactly that (besides the parts where it isn't...) ... this would have been a good name in the first place for those functions... probably mp_{ex,im}port_safe() ?
Or maybe mp_{ex,im}port_gmp_style() or mp_{ex,im}port_gmp_compat()?

I also already thought about having one of those replacements for 1.2 and after 1.2 is out change the API of mp_{ex,im}port()? Would that be acceptable?

@minad
Copy link
Member

minad commented Oct 1, 2019

Hmm, I would rather introduce new names. For example mp_pack/mp_unpack?

@minad
Copy link
Member

minad commented Oct 1, 2019

mp_to_packed/mp_from_packed would be more consistent with the from_to_bin functions?

@sjaeckel
Copy link
Member

sjaeckel commented Oct 1, 2019

For example mp_pack/mp_unpack?

it seems you're also one of the goto guys if one has problems with the second of the two hard problems of CS ;-)

mp_to_packed/mp_from_packed would be more consistent with the from_to_bin functions?

I like the first better... they sound natural, whereas those sound pushed to be consistent...

@czurnieden czurnieden force-pushed the refactor_import_export branch from 26a1dcb to 98eb1eb Compare October 1, 2019 14:33
@czurnieden
Copy link
Contributor Author

mp_pack/mp_unpack

Yepp, sounds good.
Do it?

@minad minad added this to the v1.2.0 milestone Oct 1, 2019
@minad
Copy link
Member

minad commented Oct 2, 2019

What's the status here? Ready?

@czurnieden
Copy link
Contributor Author

What's the status here? Ready?

If you are OK with it, drop me a note and I will squash&finish.

@czurnieden czurnieden force-pushed the refactor_import_export branch from aec531d to ede14c7 Compare October 2, 2019 21:24
demo/test.c Outdated
goto LTM_ERR;
}

free(buf);
Copy link
Member

Choose a reason for hiding this comment

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

Free or mp_free? Is this consistent in the test suite

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this consistent in the test suite
(I'll take it as a question) The rest of the tests is done with static buffers didn't want to do it here, although it is possible.

@minad
Copy link
Member

minad commented Oct 3, 2019

There is an endian==0 comparison

@minad
Copy link
Member

minad commented Oct 3, 2019

Countp should probably be set to the written count? Maybe add a function mp_pack_size which does this mp_count_bits?

@minad
Copy link
Member

minad commented Oct 3, 2019

An internal helper function for the native endian test woul be nice. Are there more functions which check endianness?

@czurnieden
Copy link
Contributor Author

Countp should probably be set to the written count? Maybe add a function mp_pack_size which does this mp_count_bits?

We have written to mean the size of amount of bytes throughout for now, countp is different in that regard.

But I do support the proposal to put size = (size_t)(((mp_count_bits(&a) + 1) + 7) / 8); into an extra function and I don't know why I haven't done it already.

Copy link
Member

@minad minad left a comment

Choose a reason for hiding this comment

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

Sorry for me being unclear and complaining too much. I think MP_GET_ENDIAN should be a macro/inline function in tommath_private since it will get optimized away completely.

@minad
Copy link
Member

minad commented Oct 3, 2019

Regarding countp - since we added the maxsize we introduced an additional behavior. In that case we want countp to return the actually written units. This mirrors the behavior of the other functions with written argument.

@czurnieden
Copy link
Contributor Author

I think MP_GET_ENDIAN should be a macro/inline function in tommath_private since it will get optimized away completely

That's what I started with but than I read that you wanted a function.
*grr*!
;-)

@czurnieden czurnieden force-pushed the refactor_import_export branch from f716d33 to cdc0ecd Compare October 4, 2019 15:45
Copy link
Member

@minad minad left a comment

Choose a reason for hiding this comment

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

There is still unknown_endian in the docs. I would also like for countp to mirror the behavior of written. But besides that it looks good. Also squash and rebase.

@czurnieden czurnieden force-pushed the refactor_import_export branch from cdc0ecd to 8ef79ca Compare October 5, 2019 16:41
bn_mp_pack.c Outdated
nail_bytes = nails / 8u;

bits = (size_t)mp_count_bits(&t);
count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u);
Copy link
Member

Choose a reason for hiding this comment

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

Use mp_pack_size/count here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use mp_pack_size/count here?

Yes, of course.

So you are OK with what I did?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but we still have to decide bytes or units.

Right now countp in units, maxsize in bytes, pack_size in bytes? The same applies to mp_unpack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I shuffled it a bit around, probably more to what you wanted in the first place.
I hope ;-)

Argh, documentation not yet updated!

Yes, but we still have to decide bytes or units.

Do you want to name them (enum)?
Otherwise the types can stay as they are now.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm I don't like it like this. We have count for unpack which is used to pass in the buffer size and for pack we have both maxsize and countp. Either we also change countp/count to bytes or we change maxsize to units. Mixing the units is not good as we have it now.

Copy link
Contributor Author

@czurnieden czurnieden Oct 6, 2019

Choose a reason for hiding this comment

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

(I hope I'm up-to-date now with the rebasing)

Hmm I don't like it like this.

Yeah, it's a bit of a mess, admitted.

Either we also change countp/count to bytes or we change maxsize to units.

maxsize (I think it's maxlen elsewhere but I always mix them up) is in all other cases meant to measure the given buffer-size in bytes, a size_t type.

Let me restrict myself to mp_pack for now:
countp does, as not only the name suggests, some (ac)counting. The size in bytes behind countp depends on a second variable (it formerly returned count which were units computed from bitsize and nails) and can be different from the amount of actually used up bytes. so I would say that the expectation is until now that countp returns units.
You won't have a port to get the amount of written bytes if you use countp for units.

On the other side: we have an external function to compute countp so the information thatcountp was once needed for is now available externally and in advance, countp is now free for other uses, e.g.: returning the number bytes written.
It changes the API but it is not much work to write a little macro as a wrapper if so desired.

So for mp_pack I'd propose:

  • countp (maybe change the name) are the bytes written
  • maxsize is the size in bytes of the buffer given
  • count as computed by mp_pack_count returns the number of units you get with the combination of bit-size and nails given to mp_pack_count. That means that there are no accounts of units send in or out of mp_pack.

And for mp_unpack

  • count is not the number of bytes but the number of units, the actual byte-size of the input is only of internal interest but can be computed from count and nails if so desired although I would just measure the input.

Copy link
Member

Choose a reason for hiding this comment

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

pack/unpack should also be symmetric. This means count should be bytes/unit for both. However we should also consider that changing from units/bytes will introduce errors for user who do not expect that change when they move to the new function.

Copy link
Member

Choose a reason for hiding this comment

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

My suggestion would therefore be to call it mp_pack_count, count counts unit, maxcount etc. This also makes sense. The user is not operating on byte arrays but on arrays of unit size (like calloc for example). The only downside is that its different from the sbin/ubin functions which operate on bytes.

@czurnieden czurnieden force-pushed the refactor_import_export branch 2 times, most recently from 8fda366 to 08fc22b Compare October 6, 2019 19:21
@czurnieden czurnieden force-pushed the refactor_import_export branch from 08fc22b to 06a62aa Compare October 6, 2019 19:24
@minad
Copy link
Member

minad commented Oct 7, 2019

@sjaeckel what is your opinion on the bytes vs size-units discussion?

@sjaeckel
Copy link
Member

sjaeckel commented Oct 7, 2019

@sjaeckel what is your opinion on the bytes vs size-units discussion?

I would say we keep the semantics as it was.

i.e. the proposed API should change like that:

diff --git a/tommath.h b/tommath.h
index 2a37709..6b627ff 100644
--- a/tommath.h
+++ b/tommath.h
@@ -384,3 +384,3 @@ size_t mp_pack_count(mp_int *a, size_t nails, size_t size) MP_WUR;
 mp_err mp_pack(void *rop, size_t *countp, mp_order order, size_t size, mp_endian endian,
-               size_t nails, const mp_int *op, size_t maxsize) MP_WUR;
+               size_t nails, const mp_int *op, size_t maxcount) MP_WUR;

mp_pack related API's work with count's of elements, buffers have to be count * size

IMO this makes sense as: a call to mp_pack() with MP_MSB_FIRST, MP_BIG_ENDIAN, nails=0 and size=1 is equal to a call of mp_to_ubin(), in the result which is written out and in the number of written elements ;-)

@minad
Copy link
Member

minad commented Oct 7, 2019

I agree with @sjaeckel and I would like to add that mp_pack_count should also return a count. Not a size in bytes.

@minad
Copy link
Member

minad commented Oct 7, 2019

Maybe also rearrange the args. void*buf,size_t countmax,size_t*countp

@sjaeckel
Copy link
Member

sjaeckel commented Oct 7, 2019

I would like to add that mp_pack_count should also return a count

that's what it does already.

Maybe also rearrange the args.

there's no real style how ltm treats the order of args, so I'm somehow against reordering only here, but it's true... somehow it'd also make sense to order them like that mp_err mp_pack(void *rop, size_t maxsize, size_t *countp, ...)

@sjaeckel
Copy link
Member

sjaeckel commented Oct 7, 2019

mp_err mp_pack(void *rop, size_t maxsize, size_t *countp, ...)

I just thought about it a bit more and yes, please re-order

@minad
Copy link
Member

minad commented Oct 8, 2019

superseded by #361

@minad minad closed this Oct 8, 2019
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