-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
crypto/subtle: add XORBytes #53021
Comments
cc @golang/security |
Where would it go if it were exported? crypto/subtle? (It's definitely the case that if you are XOR'ing two byte slices together you'd better know what you are doing.) It seems too special-purpose for package bytes. Are there any uses that are not crypto? /cc @golang/security |
This proposal has been added to the active column of the proposals project |
The only other things I can think of are RAID/ECC and some unusual codecs. In practice, every request I've seen in the Go world is for cryptography. |
@racin implemented a function for this for entanglement codes. Arguably our use-case is not a commonly used coding technique, but it's at least one data point that |
If we can figure out where it should go, then it seems OK in principle to add something to the standard library. But we could also recognize the pattern and optimize it in the compiler without any visible API. That's what we do already for:
which turns into a memset. We could also recognize
and turn that into some other library call. I suppose we'd have to figure out what the right pattern is, but maybe that's easier than designing new API. |
But we could also recognize the pattern and optimize it in the compiler without
any visible API.
Perhaps I'm in the minority here, but I prefer a library function to some
magic pattern. I find it easier to call a well-documented library function
than to check the generated assembler in order to make sure that some
magic pattern has matched.
|
I'm OK with adding it to the library but we haven't figured out a good place. If it were to go into a new package that was super-fast operations on byte slices, Having the compiler recognize it doesn't seem so bad compared to all this. |
As @meling mentioned I used However, there seems to be a subtle difference with the In my case, I kept the overflowing bytes of the longest one, whilst in the latter implementation the overflowing input seems to be discarded. |
One advantage to the library function is that the implementation bar is lower. The compiler optimization must ensure that it preserves the semantics precisely, including in the case of panics partway through (exactly what memory writes occur, exactly what panic occurs, and on what line). That could end up making a difference for short byte slices, where overhead matters. On the other hand, the compiler optimization can apply to xor of anything (named uint8s, uint64s, etc.), which seems pretty nice. My 2c would be to start with the compiler optimization, unless there's some reason that this needs to be constant time, in which case it (a) clearly belongs in crypto/subtle and (b) cannot be done just with compiler optimizations. |
Been following this as I use @racin Brings up a good point, which bit me also. If this does get exported/linked we should make that known in the comment/doc for it. While I agree with @josharian, my 2c would be to just add it to |
Given that |
We could also consider it the seed of a future SIMD package. /me ducks |
I think it's pretty much a one-off — I don't think there are many operations that are both that simple and impossible to avoid. Let's just dump it in |
It seems like the choices are: crypto/subtle.XorBytes What people want this for is crypto, so it seems like crypto/subtle is probably the right place to start. It just doesn't seem general enough for bytes, nor does it fit into the rest of the operations in math/bits (none of them are about slices). Does anyone object to crypto/subtle.XorBytes? |
Agreed on |
I spelled it Xor instead of XOR to match math/big. |
Does anyone object to crypto/subtle.XorBytes? (Didn't do plain Xor because maybe some other thing will need subtle xor'ing too.) |
On the other hand, Ian points out that crypto/cipher has XORKeyStream, so maybe XORBytes is better to match other things in crypto. I'm happy to do that too. |
Fixed title to match my comment from last week. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Change https://go.dev/cl/421435 mentions this issue: |
The function
crypto/cipher.xorBytes
performs a well defined, simple operation that is very common in cryptographic algorithms and network protocols. The stdlib contains a number of optimised assembler versions of this function.The Pion libraries, in particular, contain their own version of
xorBytes
, which is far from being as optimised as the Go version. IfxorBytes
were exported, it would yield an immediate performance boost for Pion, and possibly other cryptographic or networking libraries.An alternative would be to put that function under
x/
, so that it doesn't need to remain compatible. However, since the operation that this function performs is simple and well-defined, the cost of exporting it from the stdlib itself seems fairly minor to me.The text was updated successfully, but these errors were encountered: