-
Notifications
You must be signed in to change notification settings - Fork 178
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
Type mismatches in exports and headers #29
Comments
How? Well, given that you compile blst C source separately, as you're expected to (see below). Either way, it should be noted that formally speaking C standard is incomplete. Most notably it doesn't prescribe anything about calling convention. In other words one can't consider it in total isolation, but complement the context with something else, most notably platform-specific ABI specifications. Now, with this in mind, the assertion is that as long as the Moving on. But there is something else that C standard is silent about... About how booleans are to be ... "instantiated" if you wish. More specifically it doesn't say that code evaluating Now, you can say "but why doesn't blst.h return Now, you can say "but why doesn't blst.h return say Bottom line. It's not an oversight, and there is rationale behind it. And compile separately. |
Well, formally speaking there is a 3rd option, implement the constant-time cast from non-boolean to boolean in assembly... It feels like an overdo, but if we come to the conclusion that separate compilation is not sufficient guarantee after all, it would be the way to go... |
Oh! I notice that you ought to come from nim, which means that you rather not compile it separately... Well, |
Actually, not so much nim, as LTO :) We're considering disabling it for libraries like BLST though that are already well-optimized as it might interfere with, like you mention, constant time. I guess the difference that can happen is that some compiler optimize more aggressively around bool, assuming that it takes a value of 0 or 1 exclusively which might affect code paths down the road. Aliasing should be fine I guess since it's a return value that doesn't have an address - though in theory a function pointer could get messed up. |
And as already implied, it would be 100% correct assumption. Because all the subroutines that are declared As for LTO. I customarily argue against it for any cryptographic |
On a tangential note. More specifically on
where
I've omitted some instructions from the above transcript, but left the most important ones, two loads. This is what we want, right? I mean for constant-time-ness we want two loads, some calculations, and store. So it looks proper. But what about the final machine code? Well, it didn't have a conditional branch, not in this case, but the compiler transformed the IR to a selection between pointers instead of values and ended up using one load, thus making code non-constant-time:-( So a non-bool type appears more suitable for the purpose, but does it actually guarantee the constant-time-ness? As mentioned earlier, even though blst returns a non-bool type, it's Boolean by value, and formally speaking, if given the opportunity, compiler could figure it out and apply same optimizations as to bool type. Or is there some provision in the standard that would prevent the compiler? Just in case for reference, the loop in |
I'd think often ends up being more of a practical barrier than a standard issue due to how c files typically are compiled in separate translation units - as long as compilation, optimization and translation to machine instructions is done in isolation, optimization information such as possible value constraints are not passed between the TU's - with LTO however, that changes in two ways: value constraints that stem from flow analysis (and not type information) can now be applied in interprocedural analysis and ABI constraints can be relaxed as symbols no longer need to conform to an ABI description which incidentally also allows constant specialization and more aggressive inlining to take place. Conversely, when returning bool, the compiler adds instructions to clamp the value, if along a code path it cannot deterministically say that the value will be 1 or 0, it will add instructions accordingly to clamp it. FWIW, we've switched off LTO for now for our crypto dependencies, including blst, but it's a bit of a blunt way to address the problem - what I'd ideally like to do is to extend our compiler to annotate the llvm IR it generates with specific optimization constraints. |
Couple of closing comments. As it turns out the above mentioned Secondly, as time progresses, the initial assertion about supported ABIs is no longer rock-solid. Because wasm doesn't have as well-defined ABI as more traditional platforms. As result, it's |
The header file that describes the function uses a different type than the actual implementation - this may lead to miscompiles / optimization errors as there may be subtle differences between
bool
andlimb_t
.blst/bindings/blst.h
Line 142 in a8398ed
blst/src/exports.c
Line 244 in a8398ed
The text was updated successfully, but these errors were encountered: