SHA-256's pointer forms hash whole blocks where they lie, without a bounds check per byte: each block loader fills the context's message schedule and one Sha256Rounds hashes it, so phones building at -Oz hash a package as fast as Daochi Client's own SHA-256
This commit is contained in:
1 parent
d11e915b5e
commit
944826f707
1 file changed
+49
-15
+49
-15
@@ -7,6 +7,8 @@ Sha256Context :: struct {
|
||||
bit_length: u64
|
||||
block: [64]u8
|
||||
block_length: s32
|
||||
// The message schedule of the block being hashed.
|
||||
words: [64]u32
|
||||
}
|
||||
|
||||
Sha256RoundConstants: [64]u32 = .[
|
||||
@@ -27,32 +29,66 @@ Sha256RotateRight :: (value: u32, bits: u32) -> u32 {
|
||||
return (value >> bits) | (value << (cast(u32)32 - bits))
|
||||
}
|
||||
|
||||
// The buffered block. Each loader reads the block's 16 big-endian words where
|
||||
// it lies; Sha256Rounds hashes them.
|
||||
Sha256Transform :: (context: *Sha256Context) {
|
||||
Sha256TransformBlock(context, context.block[:])
|
||||
}
|
||||
|
||||
// One 64-byte block. It takes a slice, so portable bundles can hash too.
|
||||
Sha256TransformBlock :: (context: *Sha256Context, block: []u8) {
|
||||
words: [64]u32
|
||||
index: s32 = 0
|
||||
while index < 16 {
|
||||
byte_index := index * 4
|
||||
words[index] =
|
||||
context.words[index] =
|
||||
(cast(u32)context.block[byte_index] << cast(u32)24) |
|
||||
(cast(u32)context.block[byte_index + 1] << cast(u32)16) |
|
||||
(cast(u32)context.block[byte_index + 2] << cast(u32)8) |
|
||||
cast(u32)context.block[byte_index + 3]
|
||||
index += 1
|
||||
}
|
||||
Sha256Rounds(context)
|
||||
}
|
||||
|
||||
// One 64-byte block from a slice, so portable bundles can hash too.
|
||||
Sha256TransformBlock :: (context: *Sha256Context, block: []u8) {
|
||||
index: s32 = 0
|
||||
while index < 16 {
|
||||
byte_index := index * 4
|
||||
context.words[index] =
|
||||
(cast(u32)block[byte_index] << cast(u32)24) |
|
||||
(cast(u32)block[byte_index + 1] << cast(u32)16) |
|
||||
(cast(u32)block[byte_index + 2] << cast(u32)8) |
|
||||
cast(u32)block[byte_index + 3]
|
||||
index += 1
|
||||
}
|
||||
Sha256Rounds(context)
|
||||
}
|
||||
|
||||
// One 64-byte block read in place at BLOCK, for the pointer forms. Without a
|
||||
// bounds check per byte, a package hashes as fast on phones, which build at
|
||||
// -Oz, as it did with Daochi Client's own SHA-256.
|
||||
Sha256TransformAt :: (context: *Sha256Context, block: *u8) {
|
||||
index: s32 = 0
|
||||
while index < 16 {
|
||||
byte_index := index * 4
|
||||
context.words[index] =
|
||||
(cast(u32)block[byte_index] << cast(u32)24) |
|
||||
(cast(u32)block[byte_index + 1] << cast(u32)16) |
|
||||
(cast(u32)block[byte_index + 2] << cast(u32)8) |
|
||||
cast(u32)block[byte_index + 3]
|
||||
index += 1
|
||||
}
|
||||
Sha256Rounds(context)
|
||||
}
|
||||
|
||||
// The message schedule from the block's first 16 words, then the 64 rounds.
|
||||
Sha256Rounds :: (context: *Sha256Context) {
|
||||
index: s32 = 16
|
||||
while index < 64 {
|
||||
prior_15 := words[index - 15]
|
||||
prior_2 := words[index - 2]
|
||||
prior_15 := context.words[index - 15]
|
||||
prior_2 := context.words[index - 2]
|
||||
sigma_0 := (((prior_15 >> cast(u32)7) | (prior_15 << cast(u32)25)) ^
|
||||
((prior_15 >> cast(u32)18) | (prior_15 << cast(u32)14)) ^ (prior_15 >> cast(u32)3))
|
||||
sigma_1 := (((prior_2 >> cast(u32)17) | (prior_2 << cast(u32)15)) ^
|
||||
((prior_2 >> cast(u32)19) | (prior_2 << cast(u32)13)) ^ (prior_2 >> cast(u32)10))
|
||||
words[index] = (words[index - 16] + sigma_0 +
|
||||
words[index - 7] + sigma_1)
|
||||
context.words[index] = (context.words[index - 16] + sigma_0 +
|
||||
context.words[index - 7] + sigma_1)
|
||||
index += 1
|
||||
}
|
||||
|
||||
@@ -69,7 +105,7 @@ Sha256TransformBlock :: (context: *Sha256Context, block: []u8) {
|
||||
sigma_1 := (((e >> cast(u32)6) | (e << cast(u32)26)) ^
|
||||
((e >> cast(u32)11) | (e << cast(u32)21)) ^ ((e >> cast(u32)25) | (e << cast(u32)7)))
|
||||
choose := (e & f) ^ ((~e) & g)
|
||||
temp_1 := (h + sigma_1 + choose + Sha256RoundConstants[index] + words[index])
|
||||
temp_1 := (h + sigma_1 + choose + Sha256RoundConstants[index] + context.words[index])
|
||||
sigma_0 := (((a >> cast(u32)2) | (a << cast(u32)30)) ^
|
||||
((a >> cast(u32)13) | (a << cast(u32)19)) ^ ((a >> cast(u32)22) | (a << cast(u32)10)))
|
||||
majority := (a & b) ^ (a & c) ^ (b & c)
|
||||
@@ -112,9 +148,7 @@ Sha256Update :: (context: *Sha256Context, data: *u8, length: s64) {
|
||||
while index < length {
|
||||
// Whole blocks are hashed where they lie instead of byte by byte.
|
||||
if context.block_length == 0 && length - index >= 64 {
|
||||
whole: [64]u8
|
||||
for at: 0..63 { whole[at] = data[index + at] }
|
||||
Sha256TransformBlock(context, whole[:])
|
||||
Sha256TransformAt(context, *data[index])
|
||||
context.bit_length += 512
|
||||
index += 64
|
||||
continue
|
||||
|
||||
Reference in new issue
Block a user