MPI: Add stack-local limb storage to MPI objects. #306
No reviewers
Labels
No labels
api-spec
bug
CLA requested
CLA valid
compliance
DO NOT MERGE
duplicate
enhancement
feature branch
good first issue
HEADER FORMATTING ISSUE
help wanted
Information
invalid
Jira status: AWAITING REVIEW
Jira status: CLOSED
Jira status: DONE
Jira status: IN PROGRESS
Jira status: IN REVIEW
Jira status: OPEN
Jira status: REOPENED
Jira status: RESOLVED
mirrored
mirror internally
needs: backports
needs: ci
needs: design review
needs: preceding PR
needs: review
needs: work
notabug
open for community contribution
Product Backlog
question
ready for merge
security
stale
type: bug
type: enhancement
type: question
web pages
wontfix
writing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-ARMmbed/mbed-crypto!306
Loading…
Reference in a new issue
No description provided.
Delete branch "ar/mpi_limbstorage"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This allows for a small chunk of memory to be added to each mbedtls_mpi
structure instantiation, the amount of which is adjustable via a new
compile-time parameter MBEDTLS_MPI_LOCAL_LIMB_SIZE. If a given MPI can
fit wholly within this chunk of memory (which will typically reside on
the stack), no heap allocation calls for that MPI will be made. Should,
however, an MPI require more space or grow beyond the extents provided
by that chunk, then an allocation call will be made as usual and limb
storage will be diverted appropriately. The default size of stack-local
storage to be reserved is 32 bytes (i.e., 256 bits).
This is particularly useful for elliptic curve operations: a 10%
reduction to execution time was observed for operations against the
secp256{r,k}1 curves with a minor (temporary) increase in the
required amount of stack space. To maintain the current behavior of
always using heap allocation regardless of MPI size, the library can
be compiled with MBEDTLS_MPI_LOCAL_LIMB_SIZE defined to 0.
I have tested this change with the existing test suite. I believe existing
tests adequately cover this change and that no new tests would be
required.
I've made this change for a performance-sensitive project that I'm
currently working on, and am submitting this pull request in hopes the
change would be useful for mbedtls/mbed-crypto upstream. My mbed
username is also 'areading', and I accept the Contributor License Agreement.
If this commit is in line with mbedTLS project goals and is a worthwhile
candidate for inclusion into the upstream development, I would be happy to
backport this into versions 2.16 or 2.7 as well, if deemed appropriate.
Updated query_config.c per CI failure in original commit.
Thank you for your contribution! We've indeed identified the heavy malloc use in bignum as a source of inefficiency in asymmetric cryptography, so improvements there are welcome.
Unfortunately, we cannot accept this code as is, and I'm not sure about the general approach.
Blockers:
bignum.hmust not change under the default configuration. The representation ofmbedtls_mpiis in a public header and is not explicitly marked as something that may change in a future version. For example an application is entitled to writembedtls_mpi X = {1, 0, 0}or evenmbedtls_mpi X = {1, 3, malloc(3*sizeof(mbedtls_mpi_uint))}. ThereforeMBEDTLS_MPI_LOCAL_LIMB_SIZEmust default to 0, and all new fields inmbedtls_mpimust be disabled whenMBEDTLS_MPI_LOCAL_LIMB_SIZEis 0.MBEDTLS_MPI_LOCAL_LIMB_SIZEequal to 0 and withMBEDTLS_MPI_LOCAL_LIMB_SIZEnonzero. This means at a minimum adding a component toall.shwith a nonzero value forMBEDTLS_MPI_LOCAL_LIMB_SIZE.Regarding the design, turning on
MBEDTLS_MPI_LOCAL_LIMB_SIZEmeans an increase in stack size. This increase is minor by the standards of a typical hosted platform, but we also target microcontrollers where a kilobyte of stack is huge. For example,mbedtls_mpi_exp_modhas 7 to 131mbedtls_mpistructures on its stack (depending on the value of the memory/performance tuning parameterMBEDTLS_MPI_WINDOW_SIZE, so adding 32 bytes there means an increase of 224B to ~4kB in stack usage. So even if we were willing to break backward compatibility, we'd still want to turn the feature off on constrained platforms.In the long term, we'd like to completely rewrite the bignum module to never use malloc. This means rewriting the cryptography code as well, so it's a huge task, and I don't know when we'll be able to do that if ever.
In the meantime, we are definitely interested in incremental improvements like yours. But we have to balance those with the backward compatibility requirements and the testing burden, so I can't promise that we'll add this feature even if it defaults off. Please give us some time to evaluate the design and discuss it internally.
Hi @areading! We've recently merged back mbed-crypto to the mbedtls repo, as announced here. As a result, we are no longer accepting PRs to the mbed-crypto repo (except a short grace period for recently active PR that can be finalized quickly).
Since this PR hasn't been active very recently and isn't ready to be merged as it is, I'm closing it and encouraging you to re-submit your contribution as a new PR on the mbedtls repository: https://github.com/ARMmbed/mbedtls/pulls
If you do so, you might want to take this occasion to review our new contribution licensing rules (no more CLA, but we now require a Signed-of-by tag in each commit) and update your commit messages accordingly.
Thanks again for your contribution, and I hope to see you soon with a new PR in the mbedtls repo!
Pull request closed