(Context for this question is speeding up crypto/subtle)
When Go allocates strings/byte arrays, are there any implicit guarantees about alignment? The
Spec has fairly weak assertions about the alignment of arrays, but I was more probing here to see if the implementation does alignment under the hood anyways.
The reason I was interested in this was the possibility of speeding up
subtle.ConstantTimeCompare, which compares the arrays byte by byte. I am fully aware that there may be dragons here, but the performance difference between byte-by-byte and int32-by-int32 checking is too good to not investigate.
An additional reason for bringing up alignment is how array placement interacts with pages. Accessing the individual bytes of an array is not constant time if they span across page boundaries, which would leak timing information about array placement (though, I think it should still take the same amount regardless of contents). The question is whether being able to ignore page faults because they don't leak relevant timing info implies that doing other tricks is acceptable. I have a hacky, broken (but passing) attempt at what I am exploring here:
https://play.golang.org/p/3TZnmzUays
Before:
BenchmarkConstantTimeCompare-12 500000 2307 ns/op
After:
BenchmarkConstantTimeCompare-12 2000000 938 ns/op
I am not a cryptographer, I have only heard about such things from tales of others.