| 1 |
|
| 2 |
procedure g(state: pcuint32; a, b, c, d: csize_t; x, y: cuint32); inline; |
| 3 |
begin |
| 4 |
state[a] := state[a] + state[b] + x; |
| 5 |
state[d] := RorDWord(state[d] xor state[a], 16); |
| 6 |
state[c] := state[c] + state[d]; |
| 7 |
state[b] := RorDWord(state[b] xor state[c], 12); |
| 8 |
state[a] := state[a] + state[b] + y; |
| 9 |
state[d] := RorDWord(state[d] xor state[a], 8); |
| 10 |
state[c] := state[c] + state[d]; |
| 11 |
state[b] := RorDWord(state[b] xor state[c], 7); |
| 12 |
end; |
| 13 |
|
| 14 |
procedure round_fn(state: pcuint32; const msg: pcuint32; round: csize_t); inline; |
| 15 |
var |
| 16 |
schedule: pcuint8; |
| 17 |
begin |
| 18 |
// Select the message schedule based on the round. |
| 19 |
schedule := MSG_SCHEDULE[round]; |
| 20 |
|
| 21 |
// Mix the columns. |
| 22 |
g(state, 0, 4, 8, 12, msg[schedule[0]], msg[schedule[1]]); |
| 23 |
g(state, 1, 5, 9, 13, msg[schedule[2]], msg[schedule[3]]); |
| 24 |
g(state, 2, 6, 10, 14, msg[schedule[4]], msg[schedule[5]]); |
| 25 |
g(state, 3, 7, 11, 15, msg[schedule[6]], msg[schedule[7]]); |
| 26 |
|
| 27 |
// Mix the rows. |
| 28 |
g(state, 0, 5, 10, 15, msg[schedule[8]], msg[schedule[9]]); |
| 29 |
g(state, 1, 6, 11, 12, msg[schedule[10]], msg[schedule[11]]); |
| 30 |
g(state, 2, 7, 8, 13, msg[schedule[12]], msg[schedule[13]]); |
| 31 |
g(state, 3, 4, 9, 14, msg[schedule[14]], msg[schedule[15]]); |
| 32 |
end; |
| 33 |
|
| 34 |
procedure compress_pre(state: pcuint32; const cv: pcuint32; |
| 35 |
const block: pcuint8; block_len: cuint8; |
| 36 |
counter: cuint64; flags: cuint8); inline; |
| 37 |
var |
| 38 |
block_words: array[0..15] of cuint32; |
| 39 |
begin |
| 40 |
block_words[0] := load32(block + 4 * 0); |
| 41 |
block_words[1] := load32(block + 4 * 1); |
| 42 |
block_words[2] := load32(block + 4 * 2); |
| 43 |
block_words[3] := load32(block + 4 * 3); |
| 44 |
block_words[4] := load32(block + 4 * 4); |
| 45 |
block_words[5] := load32(block + 4 * 5); |
| 46 |
block_words[6] := load32(block + 4 * 6); |
| 47 |
block_words[7] := load32(block + 4 * 7); |
| 48 |
block_words[8] := load32(block + 4 * 8); |
| 49 |
block_words[9] := load32(block + 4 * 9); |
| 50 |
block_words[10] := load32(block + 4 * 10); |
| 51 |
block_words[11] := load32(block + 4 * 11); |
| 52 |
block_words[12] := load32(block + 4 * 12); |
| 53 |
block_words[13] := load32(block + 4 * 13); |
| 54 |
block_words[14] := load32(block + 4 * 14); |
| 55 |
block_words[15] := load32(block + 4 * 15); |
| 56 |
|
| 57 |
state[0] := cv[0]; |
| 58 |
state[1] := cv[1]; |
| 59 |
state[2] := cv[2]; |
| 60 |
state[3] := cv[3]; |
| 61 |
state[4] := cv[4]; |
| 62 |
state[5] := cv[5]; |
| 63 |
state[6] := cv[6]; |
| 64 |
state[7] := cv[7]; |
| 65 |
state[8] := BLAKE3_IV[0]; |
| 66 |
state[9] := BLAKE3_IV[1]; |
| 67 |
state[10] := BLAKE3_IV[2]; |
| 68 |
state[11] := BLAKE3_IV[3]; |
| 69 |
state[12] := Int64Rec(counter).Lo; |
| 70 |
state[13] := Int64Rec(counter).Hi; |
| 71 |
state[14] := cuint32(block_len); |
| 72 |
state[15] := cuint32(flags); |
| 73 |
|
| 74 |
round_fn(state, @block_words[0], 0); |
| 75 |
round_fn(state, @block_words[0], 1); |
| 76 |
round_fn(state, @block_words[0], 2); |
| 77 |
round_fn(state, @block_words[0], 3); |
| 78 |
round_fn(state, @block_words[0], 4); |
| 79 |
round_fn(state, @block_words[0], 5); |
| 80 |
round_fn(state, @block_words[0], 6); |
| 81 |
end; |
| 82 |
|
| 83 |
procedure blake3_compress_in_place_portable(cv: pcuint32; |
| 84 |
const block: pcuint8; |
| 85 |
block_len: cuint8; counter: cuint64; |
| 86 |
flags: cuint8); |
| 87 |
var |
| 88 |
state: array[0..15] of cuint32; |
| 89 |
begin |
| 90 |
compress_pre(state, cv, block, block_len, counter, flags); |
| 91 |
cv[0] := state[0] xor state[8]; |
| 92 |
cv[1] := state[1] xor state[9]; |
| 93 |
cv[2] := state[2] xor state[10]; |
| 94 |
cv[3] := state[3] xor state[11]; |
| 95 |
cv[4] := state[4] xor state[12]; |
| 96 |
cv[5] := state[5] xor state[13]; |
| 97 |
cv[6] := state[6] xor state[14]; |
| 98 |
cv[7] := state[7] xor state[15]; |
| 99 |
end; |
| 100 |
|
| 101 |
procedure blake3_compress_xof_portable(const cv: pcuint32; |
| 102 |
const block: pcuint8; |
| 103 |
block_len: cuint8; counter: cuint64; |
| 104 |
flags: cuint8; out_: pcuint8); |
| 105 |
var |
| 106 |
state: array[0..15] of cuint32; |
| 107 |
begin |
| 108 |
compress_pre(state, cv, block, block_len, counter, flags); |
| 109 |
|
| 110 |
store32(@out_[0 * 4], state[0] xor state[8]); |
| 111 |
store32(@out_[1 * 4], state[1] xor state[9]); |
| 112 |
store32(@out_[2 * 4], state[2] xor state[10]); |
| 113 |
store32(@out_[3 * 4], state[3] xor state[11]); |
| 114 |
store32(@out_[4 * 4], state[4] xor state[12]); |
| 115 |
store32(@out_[5 * 4], state[5] xor state[13]); |
| 116 |
store32(@out_[6 * 4], state[6] xor state[14]); |
| 117 |
store32(@out_[7 * 4], state[7] xor state[15]); |
| 118 |
store32(@out_[8 * 4], state[8] xor cv[0]); |
| 119 |
store32(@out_[9 * 4], state[9] xor cv[1]); |
| 120 |
store32(@out_[10 * 4], state[10] xor cv[2]); |
| 121 |
store32(@out_[11 * 4], state[11] xor cv[3]); |
| 122 |
store32(@out_[12 * 4], state[12] xor cv[4]); |
| 123 |
store32(@out_[13 * 4], state[13] xor cv[5]); |
| 124 |
store32(@out_[14 * 4], state[14] xor cv[6]); |
| 125 |
store32(@out_[15 * 4], state[15] xor cv[7]); |
| 126 |
end; |
| 127 |
|
| 128 |
procedure hash_one_portable(input: pcuint8; blocks: csize_t; |
| 129 |
const key: pcuint32; counter: cuint64; |
| 130 |
flags: uint8; flags_start: cuint8; |
| 131 |
flags_end: cuint8; out_: pcuint8); inline; |
| 132 |
var |
| 133 |
block_flags: cuint8; |
| 134 |
cv: array[0..7] of cuint32; |
| 135 |
begin |
| 136 |
Move(key^, cv[0], BLAKE3_KEY_LEN); |
| 137 |
block_flags := flags or flags_start; |
| 138 |
while (blocks > 0) do |
| 139 |
begin |
| 140 |
if (blocks = 1) then begin |
| 141 |
block_flags := block_flags or flags_end; |
| 142 |
end; |
| 143 |
blake3_compress_in_place_portable(cv, input, BLAKE3_BLOCK_LEN, counter, |
| 144 |
block_flags); |
| 145 |
input := @input[BLAKE3_BLOCK_LEN]; |
| 146 |
blocks -= 1; |
| 147 |
block_flags := flags; |
| 148 |
end; |
| 149 |
store_cv_words(out_, cv); |
| 150 |
end; |
| 151 |
|
| 152 |
procedure blake3_hash_many_portable(inputs: ppcuint8; num_inputs: csize_t; |
| 153 |
blocks: csize_t; const key: pcuint32; |
| 154 |
counter: cuint64; increment_counter: boolean32; |
| 155 |
flags: cuint8; flags_start: cuint8; |
| 156 |
flags_end: cuint8; out_: pcuint8); |
| 157 |
begin |
| 158 |
while (num_inputs > 0) do |
| 159 |
begin |
| 160 |
hash_one_portable(inputs[0], blocks, key, counter, flags, flags_start, |
| 161 |
flags_end, out_); |
| 162 |
if (increment_counter) then begin |
| 163 |
counter += 1; |
| 164 |
end; |
| 165 |
inputs += 1; |
| 166 |
num_inputs -= 1; |
| 167 |
out_ := @out_[BLAKE3_OUT_LEN]; |
| 168 |
end; |
| 169 |
end; |
| 170 |
|