blob: 20c8cfd73ca525ff75e935877de48ef08045fab2 [file] [edit]
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Generates the fixed-schedule, unrolled scalar BLAKE3 compression kernel.
library;
import 'dart:io';
void main() {
const permutation = [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8];
const groups = [
(0, 4, 8, 12),
(1, 5, 9, 13),
(2, 6, 10, 14),
(3, 7, 11, 15),
(0, 5, 10, 15),
(1, 6, 11, 12),
(2, 7, 8, 13),
(3, 4, 9, 14),
];
var out = StringBuffer('''
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Generated by pkg/analyzer/tool/blake3/generate.dart. Do not edit.
part of 'blake3.dart';
''');
out.write('''
/// Compresses one 64-byte block with four independent scalar chains.
final class _ScalarBlake3 extends _Blake3Base implements ScalarBlake3 {
@override
void _compress(Uint32List cv, Uint32List words, int offset,
int counter, int blockLength, int flags) {
''');
for (var i = 0; i < 8; i++) {
out.writeln('var v$i = cv[$i];');
}
for (var i = 8; i < 12; i++) {
out.writeln('var v$i = _blake3Iv[${i - 8}];');
}
out.write('''
var v12 = counter & 0xffffffff;
var v13 = counter >>> 32;
var v14 = blockLength;
var v15 = flags;
''');
for (var i = 0; i < 16; i++) {
out.writeln('var m$i = words[offset + $i];');
}
var schedule = List.generate(16, (i) => i);
for (var round = 0; round < 7; round++) {
out.writeln();
out.writeln('// Round ${round + 1}.');
for (var (j, (a, b, c, d)) in groups.indexed) {
if (j > 0) {
out.writeln();
}
out.writeln('// g($a, $b, $c, $d), permutation #$round');
for (var (word, r1, r2) in [
(schedule[2 * j], 16, 12),
(schedule[2 * j + 1], 8, 7),
]) {
out.write('''
v$a = (v$a + v$b + m$word) & 0xffffffff;
v$d ^= v$a;
v$d = ((v$d >>> $r1) | (v$d << ${32 - r1})) & 0xffffffff;
v$c = (v$c + v$d) & 0xffffffff;
v$b ^= v$c;
v$b = ((v$b >>> $r2) | (v$b << ${32 - r2})) & 0xffffffff;
''');
}
}
schedule = [for (var i in permutation) schedule[i]];
}
for (var i = 0; i < 8; i++) {
out.writeln('cv[$i] = v$i ^ v${i + 8};');
}
out.write('''
}
}
''');
var output = File.fromUri(
Platform.script.resolve(
'../../lib/src/utilities/hash/blake3_scalar_compression.dart',
),
);
output.writeAsStringSync(out.toString());
}