-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
math/big: add Int.FillBytes #35833
Comments
Would it make sense to provide the byte slice to avoid a copy? func (x *Int) AsBytes(buf []byte) |
Yes, I like @griesemer's design. Panic is OK if documented. A nil argument could allocate, perhaps? |
Oh, I like that! The nil argument behavior would make sense if we didn't have Bytes, but that's there to stay. I am not a fan of the fact that one can't really tell which is which based on the names (Bytes vs. AsBytes) but I don't have better suggestions. Maybe |
The usual name for this would be Read, not Write 😉 |
My original thought was also Maybe |
I like
|
|
I'm a little unsettled by the absolute value thing, but that's what Bytes does. |
Same, but on the other hand every way of encoding negative numbers I know of went terribly wrong, so I see where that choice is coming from. |
Based on the discussion, this sounds like a likely accept. Leaving open for a week for final comments. |
No final comments, so accepted. |
Change https://golang.org/cl/230397 mentions this issue: |
Implemented this in CL 230397. Note that I made FillBytes return the buffer, because it was nicer to use in all call sites I updated. Let me know if there are objections to that. |
Replaced almost every use of Bytes with FillBytes. Note that the approved proposal was for func (*Int) FillBytes(buf []byte) while this implements func (*Int) FillBytes(buf []byte) []byte because the latter was far nicer to use in all callsites. Fixes golang#35833 Change-Id: Ia912df123e5d79b763845312ea3d9a8051343c0a Reviewed-on: https://go-review.googlesource.com/c/go/+/230397 Reviewed-by: Robert Griesemer <gri@golang.org>
Fixed by c9d5f60, which for some reason didn't close this. |
Replaced almost every use of Bytes with FillBytes. Note that the approved proposal was for func (*Int) FillBytes(buf []byte) while this implements func (*Int) FillBytes(buf []byte) []byte because the latter was far nicer to use in all callsites. Fixes golang#35833 Change-Id: Ia912df123e5d79b763845312ea3d9a8051343c0a Reviewed-on: https://go-review.googlesource.com/c/go/+/230397 Reviewed-by: Robert Griesemer <gri@golang.org>
Sorry for not commenting earlier, but can I ask why an func (*Int) FillBytes(buf []byte) []byte I would assume that it behaved like I don't think we can change the behavior now, unfortunately, but maybe an |
@lukechampine What this API was specifically addressing, more than the performance advantage of saving an allocation, was the need to fill a fixed-size buffer with a possibly shorter big.Int, because big.Ints don't have an innate size. There is no way to do that with an append-like API. |
Ah, because of the big-endianness, right. Thanks for the clarification. |
When implementing cryptography, what we need is almost always a fixed size buffer representing a value in big endian. What
math/big.Int.Bytes
provides is a variable size buffer, so all over the place there are snippets of code that domake
,Bytes
andcopy
with alen
dependent index. I just implemented one the other day for https://golang.org/cl/208484, and just saw one in x/crypto/acme.I'd be willing to bet that every
Bytes
invocation in a crypto package is doing something similar. I also learned that random bugs that occur approximately every 256 executions are probably due to missing this step, and I have found such a bug in the wild at least twice.I propose we solve this at the math/big API level, and add
(*math/big.Int).BytesWithSize
.I don't have a strong opinion on the
len(b) > size
behavior. Where we use it in crypto packages we always know an upper bound, and if we cross it we might as well panic because something went catastrophically wrong. (And the current code would either panic or silently truncate, which is worse.)/cc @griesemer @katiehockman
The text was updated successfully, but these errors were encountered: