-
Notifications
You must be signed in to change notification settings - Fork 400
WIP: Streaming SHA256 ops. #817
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -716,6 +716,60 @@ void CSHA256::Midstate(unsigned char hash[OUTPUT_SIZE], uint64_t* len, unsigned | |
| } | ||
| } | ||
|
|
||
| std::vector<unsigned char> CSHA256::Save() const { | ||
| size_t buf_size = bytes % 64; | ||
| std::vector<unsigned char> result(40 + buf_size); | ||
|
|
||
| WriteBE32(&result[ 0], s[0]); | ||
| WriteBE32(&result[ 4], s[1]); | ||
| WriteBE32(&result[ 8], s[2]); | ||
| WriteBE32(&result[12], s[3]); | ||
| WriteBE32(&result[16], s[4]); | ||
| WriteBE32(&result[20], s[5]); | ||
| WriteBE32(&result[24], s[6]); | ||
| WriteBE32(&result[28], s[7]); | ||
|
|
||
| WriteLE64(&result[32], bytes << 3); | ||
|
|
||
| memcpy(&result[40], buf, buf_size); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| bool CSHA256::Load(const std::vector<unsigned char>& vch) { | ||
| if (vch.size() < 40) return false; | ||
|
|
||
| uint64_t bits = ReadLE64(&vch[32]); | ||
| size_t buf_size = (bits >> 3) % 64; | ||
|
|
||
| if ((bits & 0x07) != 0 || vch.size() != 40 + buf_size) return false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a comment to state that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit more than that: the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean you could just just do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had hoped it goes without saying that one shouldn't mangle an invalidly serialized data blob into a valid one.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. validity of serialized data depends on the definition of the validity, but it is not explicitly specified (the serialization code in |
||
|
|
||
| // We want to leave the internal state of the object unchanged if false is returned. | ||
| // So no member variables can be modified until now. | ||
|
|
||
| s[0] = ReadBE32(&vch[ 0]); | ||
| s[1] = ReadBE32(&vch[ 4]); | ||
| s[2] = ReadBE32(&vch[ 8]); | ||
| s[3] = ReadBE32(&vch[12]); | ||
| s[4] = ReadBE32(&vch[16]); | ||
| s[5] = ReadBE32(&vch[20]); | ||
| s[6] = ReadBE32(&vch[24]); | ||
| s[7] = ReadBE32(&vch[28]); | ||
|
|
||
| bytes = bits >> 3; | ||
roconnor-blockstream marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| memcpy(buf, &vch[40], buf_size); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool CSHA256::SafeWrite(const unsigned char* data, size_t len) { | ||
| const uint64_t SHA256_MAX = 0x1FFFFFFFFFFFFFFF; // SHA256's maximum allowed message length in bytes. | ||
| if (SHA256_MAX < bytes || SHA256_MAX - bytes < len) return false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it because you want to allow for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You cold make
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose I could write Is this really all worthwhile just to avoid one
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose we also get rid of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all three
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just rename
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I said, my opinion on this is not strong. Speaking of returning the number of bytes still available to process from |
||
| Write(data, len); | ||
| return true; | ||
| } | ||
|
|
||
| CSHA256& CSHA256::Reset() | ||
| { | ||
| bytes = 0; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.