You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My initial plan for this commit was to implement the nostd version
without randomization support, and patch it in later. However, I
realized that even without rerandomization, I still needed
synchronization logic in order to initialize the global context object.
(Upstream provides a static "no precomp" context object, but it has no
precomputation tables and therefore can't be used for verification,
which makes it unusable for our purposes).
In order to implement initialization, with ChatGPT's help I implemented
a simple spinlock. However, there are a number of problems with
spinlocks -- see this article (from Kix in #346) for some of them:
https://matklad.github.io/2020/01/02/spinlocks-considered-harmful.html
To avoid these problems, we tweak the spinlock logic so that we only try
spinning a small finite number of times, then give up. Our "give up"
logic is:
1. When initializing the global context, if we can't get the lock, we
just initialize a new stack-local context and use that. (A parallel
thread must be initializing the context, which is wasteful but
harmless.)
2. Once we unlock the context, we copy it onto the stack and re-lock it
in order to minimize the time holding the lock. (The exception is
during initialization where we hold the lock for the whole
initialization, in the hopes that other threads will block on us
instead of doing their own initialization.) If we rerandomize, we do
this on the stack-local copy and then only re-lock to copy it back.
3. If we fail to get the lock to copy the rerandomized context back, we
just don't copy it. The result is that we wasted some time
rerandomizing without any benefit, which is not the end of the world.
Next steps are:
1. Update the API to use this logic everywhere; on validation functions
we don't need to rerandomize and on signing/keygen functions we
should rerandomize using our secret key material.
2. Remove the existing "no context" API, along with the global-context
and global-context-less-secure features.
3. Improve our entropy story on nostd by scraping system time or CPU
jitter or something and hashing that into our rerandomization. We
don't need to do a great job here -- if we can get even a bit or two
per signature, that will completely BTFO a timing attacker.
0 commit comments