navid/veins / commit

04991c7c989c

Navid M <navidmomtahen@gmail.com> · 2026-11-25

parents: 0c1ad4fc2e5b

Browse files at this commit · Download source

all: fixes
--- /dev/null+++ b/src/build-native.sh@@ -0,0 +1,32 @@+#!/bin/sh+# Build the native hot-path objects and combine them for spectre -link-obj.+set -e++out="${1:-.sxb/linux.x86_64}"+cc="${CC:-cc}"+mkdir -p "$out"++crypto="src/crypto"+b3="$crypto/blake3"++"$cc" -O3 -c "$crypto/blake3_c.c" -o "$out/blake3_c.o"+"$cc" -O3 -c "$b3/blake3.c" -o "$out/blake3_impl.o"+"$cc" -O3 -c "$b3/blake3_dispatch.c" -o "$out/blake3_dispatch.o"+"$cc" -O3 -c "$b3/blake3_portable.c" -o "$out/blake3_portable.o"+"$cc" -O3 -msse2 -c "$b3/blake3_sse2.c" -o "$out/blake3_sse2.o"+"$cc" -O3 -msse4.1 -c "$b3/blake3_sse41.c" -o "$out/blake3_sse41.o"+"$cc" -O3 -mavx2 -c "$b3/blake3_avx2.c" -o "$out/blake3_avx2.o"+"$cc" -O3 -mavx512f -mavx512vl -c "$b3/blake3_avx512.c" -o "$out/blake3_avx512.o"+"$cc" -O3 -c "src/chunking/fastcdc_c.c" -o "$out/fastcdc_c.o"++"$cc" -r \+    "$out/blake3_c.o" \+    "$out/blake3_impl.o" \+    "$out/blake3_dispatch.o" \+    "$out/blake3_portable.o" \+    "$out/blake3_sse2.o" \+    "$out/blake3_sse41.o" \+    "$out/blake3_avx2.o" \+    "$out/blake3_avx512.o" \+    "$out/fastcdc_c.o" \+    -o "$out/vein_native.o"--- a/src/src/chunking/fastcdc.sx+++ b/src/src/chunking/fastcdc.sx@@ -9,6 +9,8 @@  val gear  = use("gear.sx") val lists = use("../core/lists.sx")++extern (C) fn vn_fastcdc_next(data: ref u8, len: usize, gear: ref u8) usize = "vn_fastcdc_next"  pub val (     MIN_SIZE: usize      = 16384@@ -15,23 +17,7 @@     AVG_SIZE: usize      = 65536     MAX_SIZE: usize      = 262144     NORMALIZATION: usize = 2-)--val (-    MASK_S: u64 = 0x3ffff-    MASK_L: u64 = 0x3fff )--fn gear_at(c: u8) u64 = inline trust {-    return @load64(@ptradd(gear.GEAR_RAW, (c as i64) * 8)) as u64-}--fn min_usize(a: usize, b: usize) usize = {-    if a < b {-        return a-    }-    return b-}  /// Return the length of the next chunk at `data`, given that at least /// `len` bytes remain. The result is in `1..=min(len, MAX_SIZE)`.@@ -38,31 +24,8 @@ pub fn next_len(data: ref u8, len: usize) usize = trust {     pre {         progress: len > 0-    }-    if len <= MIN_SIZE {-        return len-    }-    val limit: usize = min_usize(len, MAX_SIZE)-    val normal: usize = min_usize(AVG_SIZE, limit)-    val fp: mut u64 = 0-    val i: mut usize = MIN_SIZE-    for i < normal {-        val c: u8 = @load8(@ptradd(data, i as i64))-        fp = (fp >> 1) + gear_at(c)-        if (fp & MASK_S) == 0 {-            return i + 1-        }-        i++-    }-    for i < limit {-        val c2: u8 = @load8(@ptradd(data, i as i64))-        fp = (fp >> 1) + gear_at(c2)-        if (fp & MASK_L) == 0 {-            return i + 1-        }-        i++     }-    return limit+    return vn_fastcdc_next(data, len, gear.GEAR_RAW) }  /// Split a byte span into chunk lengths.--- /dev/null+++ b/src/src/chunking/fastcdc_c.c@@ -0,0 +1,39 @@+/* FastCDC next-chunk scan used by the format's chunker. */++#include <stddef.h>+#include <stdint.h>+#include <string.h>++#define VN_CDC_MIN 16384+#define VN_CDC_AVG 65536+#define VN_CDC_MAX 262144+#define VN_CDC_MASK_S 0x3ffffULL+#define VN_CDC_MASK_L 0x3fffULL++static uint64_t gear_at(const uint8_t *gear, uint8_t c) {+    uint64_t v;+    memcpy(&v, gear + (size_t)c * 8, sizeof(v));+    return v;+}++size_t vn_fastcdc_next(const uint8_t *data, size_t len, const uint8_t *gear) {+    if (len <= VN_CDC_MIN) {+        return len;+    }+    size_t limit = len < VN_CDC_MAX ? len : VN_CDC_MAX;+    size_t normal = VN_CDC_AVG < limit ? VN_CDC_AVG : limit;+    uint64_t fp = 0;+    for (size_t i = VN_CDC_MIN; i < normal; i++) {+        fp = (fp >> 1) + gear_at(gear, data[i]);+        if ((fp & VN_CDC_MASK_S) == 0) {+            return i + 1;+        }+    }+    for (size_t i = normal; i < limit; i++) {+        fp = (fp >> 1) + gear_at(gear, data[i]);+        if ((fp & VN_CDC_MASK_L) == 0) {+            return i + 1;+        }+    }+    return limit;+}--- a/src/src/cli/selftest_cmd.sx+++ b/src/src/cli/selftest_cmd.sx@@ -265,8 +265,6 @@         blob.root_id(built),         c"34bb8e7c00665b61c882fe1cae0a51a44d6ad1719fd44a1b22c991ae6a3c24bc"     )-    val brp: mut ref void = built.root as ref void-    @free(brp)     blob.free_build(built)     val p0: mut ref void = c0.ptr as ref void     val p1: mut ref void = c1.ptr as ref void--- /dev/null+++ b/src/src/crypto/blake3/LICENSE_A2@@ -0,0 +1,202 @@++                                 Apache License+                           Version 2.0, January 2004+                        http://www.apache.org/licenses/++   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION++   1. Definitions.++      "License" shall mean the terms and conditions for use, reproduction,+      and distribution as defined by Sections 1 through 9 of this document.++      "Licensor" shall mean the copyright owner or entity authorized by+      the copyright owner that is granting the License.++      "Legal Entity" shall mean the union of the acting entity and all+      other entities that control, are controlled by, or are under common+      control with that entity. For the purposes of this definition,+      "control" means (i) the power, direct or indirect, to cause the+      direction or management of such entity, whether by contract or+      otherwise, or (ii) ownership of fifty percent (50%) or more of the+      outstanding shares, or (iii) beneficial ownership of such entity.++      "You" (or "Your") shall mean an individual or Legal Entity+      exercising permissions granted by this License.++      "Source" form shall mean the preferred form for making modifications,+      including but not limited to software source code, documentation+      source, and configuration files.++      "Object" form shall mean any form resulting from mechanical+      transformation or translation of a Source form, including but+      not limited to compiled object code, generated documentation,+      and conversions to other media types.++      "Work" shall mean the work of authorship, whether in Source or+      Object form, made available under the License, as indicated by a+      copyright notice that is included in or attached to the work+      (an example is provided in the Appendix below).++      "Derivative Works" shall mean any work, whether in Source or Object+      form, that is based on (or derived from) the Work and for which the+      editorial revisions, annotations, elaborations, or other modifications+      represent, as a whole, an original work of authorship. For the purposes+      of this License, Derivative Works shall not include works that remain+      separable from, or merely link (or bind by name) to the interfaces of,+      the Work and Derivative Works thereof.++      "Contribution" shall mean any work of authorship, including+      the original version of the Work and any modifications or additions+      to that Work or Derivative Works thereof, that is intentionally+      submitted to Licensor for inclusion in the Work by the copyright owner+      or by an individual or Legal Entity authorized to submit on behalf of+      the copyright owner. For the purposes of this definition, "submitted"+      means any form of electronic, verbal, or written communication sent+      to the Licensor or its representatives, including but not limited to+      communication on electronic mailing lists, source code control systems,+      and issue tracking systems that are managed by, or on behalf of, the+      Licensor for the purpose of discussing and improving the Work, but+      excluding communication that is conspicuously marked or otherwise+      designated in writing by the copyright owner as "Not a Contribution."++      "Contributor" shall mean Licensor and any individual or Legal Entity+      on behalf of whom a Contribution has been received by Licensor and+      subsequently incorporated within the Work.++   2. Grant of Copyright License. Subject to the terms and conditions of+      this License, each Contributor hereby grants to You a perpetual,+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable+      copyright license to reproduce, prepare Derivative Works of,+      publicly display, publicly perform, sublicense, and distribute the+      Work and such Derivative Works in Source or Object form.++   3. Grant of Patent License. Subject to the terms and conditions of+      this License, each Contributor hereby grants to You a perpetual,+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable+      (except as stated in this section) patent license to make, have made,+      use, offer to sell, sell, import, and otherwise transfer the Work,+      where such license applies only to those patent claims licensable+      by such Contributor that are necessarily infringed by their+      Contribution(s) alone or by combination of their Contribution(s)+      with the Work to which such Contribution(s) was submitted. If You+      institute patent litigation against any entity (including a+      cross-claim or counterclaim in a lawsuit) alleging that the Work+      or a Contribution incorporated within the Work constitutes direct+      or contributory patent infringement, then any patent licenses+      granted to You under this License for that Work shall terminate+      as of the date such litigation is filed.++   4. Redistribution. You may reproduce and distribute copies of the+      Work or Derivative Works thereof in any medium, with or without+      modifications, and in Source or Object form, provided that You+      meet the following conditions:++      (a) You must give any other recipients of the Work or+          Derivative Works a copy of this License; and++      (b) You must cause any modified files to carry prominent notices+          stating that You changed the files; and++      (c) You must retain, in the Source form of any Derivative Works+          that You distribute, all copyright, patent, trademark, and+          attribution notices from the Source form of the Work,+          excluding those notices that do not pertain to any part of+          the Derivative Works; and++      (d) If the Work includes a "NOTICE" text file as part of its+          distribution, then any Derivative Works that You distribute must+          include a readable copy of the attribution notices contained+          within such NOTICE file, excluding those notices that do not+          pertain to any part of the Derivative Works, in at least one+          of the following places: within a NOTICE text file distributed+          as part of the Derivative Works; within the Source form or+          documentation, if provided along with the Derivative Works; or,+          within a display generated by the Derivative Works, if and+          wherever such third-party notices normally appear. The contents+          of the NOTICE file are for informational purposes only and+          do not modify the License. You may add Your own attribution+          notices within Derivative Works that You distribute, alongside+          or as an addendum to the NOTICE text from the Work, provided+          that such additional attribution notices cannot be construed+          as modifying the License.++      You may add Your own copyright statement to Your modifications and+      may provide additional or different license terms and conditions+      for use, reproduction, or distribution of Your modifications, or+      for any such Derivative Works as a whole, provided Your use,+      reproduction, and distribution of the Work otherwise complies with+      the conditions stated in this License.++   5. Submission of Contributions. Unless You explicitly state otherwise,+      any Contribution intentionally submitted for inclusion in the Work+      by You to the Licensor shall be under the terms and conditions of+      this License, without any additional terms or conditions.+      Notwithstanding the above, nothing herein shall supersede or modify+      the terms of any separate license agreement you may have executed+      with Licensor regarding such Contributions.++   6. Trademarks. This License does not grant permission to use the trade+      names, trademarks, service marks, or product names of the Licensor,+      except as required for reasonable and customary use in describing the+      origin of the Work and reproducing the content of the NOTICE file.++   7. Disclaimer of Warranty. Unless required by applicable law or+      agreed to in writing, Licensor provides the Work (and each+      Contributor provides its Contributions) on an "AS IS" BASIS,+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or+      implied, including, without limitation, any warranties or conditions+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A+      PARTICULAR PURPOSE. You are solely responsible for determining the+      appropriateness of using or redistributing the Work and assume any+      risks associated with Your exercise of permissions under this License.++   8. Limitation of Liability. In no event and under no legal theory,+      whether in tort (including negligence), contract, or otherwise,+      unless required by applicable law (such as deliberate and grossly+      negligent acts) or agreed to in writing, shall any Contributor be+      liable to You for damages, including any direct, indirect, special,+      incidental, or consequential damages of any character arising as a+      result of this License or out of the use or inability to use the+      Work (including but not limited to damages for loss of goodwill,+      work stoppage, computer failure or malfunction, or any and all+      other commercial damages or losses), even if such Contributor+      has been advised of the possibility of such damages.++   9. Accepting Warranty or Additional Liability. While redistributing+      the Work or Derivative Works thereof, You may choose to offer,+      and charge a fee for, acceptance of support, warranty, indemnity,+      or other liability obligations and/or rights consistent with this+      License. However, in accepting such obligations, You may act only+      on Your own behalf and on Your sole responsibility, not on behalf+      of any other Contributor, and only if You agree to indemnify,+      defend, and hold each Contributor harmless for any liability+      incurred by, or claims asserted against, such Contributor by reason+      of your accepting any such warranty or additional liability.++   END OF TERMS AND CONDITIONS++   APPENDIX: How to apply the Apache License to your work.++      To apply the Apache License to your work, attach the following+      boilerplate notice, with the fields enclosed by brackets "[]"+      replaced with your own identifying information. (Don't include+      the brackets!)  The text should be enclosed in the appropriate+      comment syntax for the file format. We also recommend that a+      file or class name and description of purpose be included on the+      same "printed page" as the copyright notice for easier+      identification within third-party archives.++   Copyright 2019 Jack O'Connor and Samuel Neves++   Licensed under the Apache License, Version 2.0 (the "License");+   you may not use this file except in compliance with the License.+   You may obtain a copy of the License at++       http://www.apache.org/licenses/LICENSE-2.0++   Unless required by applicable law or agreed to in writing, software+   distributed under the License is distributed on an "AS IS" BASIS,+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+   See the License for the specific language governing permissions and+   limitations under the License.--- /dev/null+++ b/src/src/crypto/blake3/LICENSE_A2LLVM@@ -0,0 +1,219 @@+                                 Apache License+                           Version 2.0, January 2004+                        http://www.apache.org/licenses/++    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION++    1. Definitions.++      "License" shall mean the terms and conditions for use, reproduction,+      and distribution as defined by Sections 1 through 9 of this document.++      "Licensor" shall mean the copyright owner or entity authorized by+      the copyright owner that is granting the License.++      "Legal Entity" shall mean the union of the acting entity and all+      other entities that control, are controlled by, or are under common+      control with that entity. For the purposes of this definition,+      "control" means (i) the power, direct or indirect, to cause the+      direction or management of such entity, whether by contract or+      otherwise, or (ii) ownership of fifty percent (50%) or more of the+      outstanding shares, or (iii) beneficial ownership of such entity.++      "You" (or "Your") shall mean an individual or Legal Entity+      exercising permissions granted by this License.++      "Source" form shall mean the preferred form for making modifications,+      including but not limited to software source code, documentation+      source, and configuration files.++      "Object" form shall mean any form resulting from mechanical+      transformation or translation of a Source form, including but+      not limited to compiled object code, generated documentation,+      and conversions to other media types.++      "Work" shall mean the work of authorship, whether in Source or+      Object form, made available under the License, as indicated by a+      copyright notice that is included in or attached to the work+      (an example is provided in the Appendix below).++      "Derivative Works" shall mean any work, whether in Source or Object+      form, that is based on (or derived from) the Work and for which the+      editorial revisions, annotations, elaborations, or other modifications+      represent, as a whole, an original work of authorship. For the purposes+      of this License, Derivative Works shall not include works that remain+      separable from, or merely link (or bind by name) to the interfaces of,+      the Work and Derivative Works thereof.++      "Contribution" shall mean any work of authorship, including+      the original version of the Work and any modifications or additions+      to that Work or Derivative Works thereof, that is intentionally+      submitted to Licensor for inclusion in the Work by the copyright owner+      or by an individual or Legal Entity authorized to submit on behalf of+      the copyright owner. For the purposes of this definition, "submitted"+      means any form of electronic, verbal, or written communication sent+      to the Licensor or its representatives, including but not limited to+      communication on electronic mailing lists, source code control systems,+      and issue tracking systems that are managed by, or on behalf of, the+      Licensor for the purpose of discussing and improving the Work, but+      excluding communication that is conspicuously marked or otherwise+      designated in writing by the copyright owner as "Not a Contribution."++      "Contributor" shall mean Licensor and any individual or Legal Entity+      on behalf of whom a Contribution has been received by Licensor and+      subsequently incorporated within the Work.++    2. Grant of Copyright License. Subject to the terms and conditions of+      this License, each Contributor hereby grants to You a perpetual,+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable+      copyright license to reproduce, prepare Derivative Works of,+      publicly display, publicly perform, sublicense, and distribute the+      Work and such Derivative Works in Source or Object form.++    3. Grant of Patent License. Subject to the terms and conditions of+      this License, each Contributor hereby grants to You a perpetual,+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable+      (except as stated in this section) patent license to make, have made,+      use, offer to sell, sell, import, and otherwise transfer the Work,+      where such license applies only to those patent claims licensable+      by such Contributor that are necessarily infringed by their+      Contribution(s) alone or by combination of their Contribution(s)+      with the Work to which such Contribution(s) was submitted. If You+      institute patent litigation against any entity (including a+      cross-claim or counterclaim in a lawsuit) alleging that the Work+      or a Contribution incorporated within the Work constitutes direct+      or contributory patent infringement, then any patent licenses+      granted to You under this License for that Work shall terminate+      as of the date such litigation is filed.++    4. Redistribution. You may reproduce and distribute copies of the+      Work or Derivative Works thereof in any medium, with or without+      modifications, and in Source or Object form, provided that You+      meet the following conditions:++      (a) You must give any other recipients of the Work or+          Derivative Works a copy of this License; and++      (b) You must cause any modified files to carry prominent notices+          stating that You changed the files; and++      (c) You must retain, in the Source form of any Derivative Works+          that You distribute, all copyright, patent, trademark, and+          attribution notices from the Source form of the Work,+          excluding those notices that do not pertain to any part of+          the Derivative Works; and++      (d) If the Work includes a "NOTICE" text file as part of its+          distribution, then any Derivative Works that You distribute must+          include a readable copy of the attribution notices contained+          within such NOTICE file, excluding those notices that do not+          pertain to any part of the Derivative Works, in at least one+          of the following places: within a NOTICE text file distributed+          as part of the Derivative Works; within the Source form or+          documentation, if provided along with the Derivative Works; or,+          within a display generated by the Derivative Works, if and+          wherever such third-party notices normally appear. The contents+          of the NOTICE file are for informational purposes only and+          do not modify the License. You may add Your own attribution+          notices within Derivative Works that You distribute, alongside+          or as an addendum to the NOTICE text from the Work, provided+          that such additional attribution notices cannot be construed+          as modifying the License.++      You may add Your own copyright statement to Your modifications and+      may provide additional or different license terms and conditions+      for use, reproduction, or distribution of Your modifications, or+      for any such Derivative Works as a whole, provided Your use,+      reproduction, and distribution of the Work otherwise complies with+      the conditions stated in this License.++    5. Submission of Contributions. Unless You explicitly state otherwise,+      any Contribution intentionally submitted for inclusion in the Work+      by You to the Licensor shall be under the terms and conditions of+      this License, without any additional terms or conditions.+      Notwithstanding the above, nothing herein shall supersede or modify+      the terms of any separate license agreement you may have executed+      with Licensor regarding such Contributions.++    6. Trademarks. This License does not grant permission to use the trade+      names, trademarks, service marks, or product names of the Licensor,+      except as required for reasonable and customary use in describing the+      origin of the Work and reproducing the content of the NOTICE file.++    7. Disclaimer of Warranty. Unless required by applicable law or+      agreed to in writing, Licensor provides the Work (and each+      Contributor provides its Contributions) on an "AS IS" BASIS,+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or+      implied, including, without limitation, any warranties or conditions+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A+      PARTICULAR PURPOSE. You are solely responsible for determining the+      appropriateness of using or redistributing the Work and assume any+      risks associated with Your exercise of permissions under this License.++    8. Limitation of Liability. In no event and under no legal theory,+      whether in tort (including negligence), contract, or otherwise,+      unless required by applicable law (such as deliberate and grossly+      negligent acts) or agreed to in writing, shall any Contributor be+      liable to You for damages, including any direct, indirect, special,+      incidental, or consequential damages of any character arising as a+      result of this License or out of the use or inability to use the+      Work (including but not limited to damages for loss of goodwill,+      work stoppage, computer failure or malfunction, or any and all+      other commercial damages or losses), even if such Contributor+      has been advised of the possibility of such damages.++    9. Accepting Warranty or Additional Liability. While redistributing+      the Work or Derivative Works thereof, You may choose to offer,+      and charge a fee for, acceptance of support, warranty, indemnity,+      or other liability obligations and/or rights consistent with this+      License. However, in accepting such obligations, You may act only+      on Your own behalf and on Your sole responsibility, not on behalf+      of any other Contributor, and only if You agree to indemnify,+      defend, and hold each Contributor harmless for any liability+      incurred by, or claims asserted against, such Contributor by reason+      of your accepting any such warranty or additional liability.++    END OF TERMS AND CONDITIONS++    APPENDIX: How to apply the Apache License to your work.++      To apply the Apache License to your work, attach the following+      boilerplate notice, with the fields enclosed by brackets "[]"+      replaced with your own identifying information. (Don't include+      the brackets!)  The text should be enclosed in the appropriate+      comment syntax for the file format. We also recommend that a+      file or class name and description of purpose be included on the+      same "printed page" as the copyright notice for easier+      identification within third-party archives.++    Copyright 2019 Jack O'Connor and Samuel Neves++    Licensed under the Apache License, Version 2.0 (the "License");+    you may not use this file except in compliance with the License.+    You may obtain a copy of the License at++       http://www.apache.org/licenses/LICENSE-2.0++    Unless required by applicable law or agreed to in writing, software+    distributed under the License is distributed on an "AS IS" BASIS,+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+    See the License for the specific language governing permissions and+    limitations under the License.+++---- LLVM Exceptions to the Apache 2.0 License ----++As an exception, if, as a result of your compiling your source code, portions+of this Software are embedded into an Object form of such source code, you+may redistribute such embedded portions in such Object form without complying+with the conditions of Sections 4(a), 4(b) and 4(d) of the License.++In addition, if you combine or link compiled forms of this Software with+software that is licensed under the GPLv2 ("Combined Software") and if a+court of competent jurisdiction determines that the patent provision (Section+3), the indemnity provision (Section 9) or other Section of the License+conflicts with the conditions of the GPLv2, you may retroactively and+prospectively choose to deem waived or otherwise exclude such Section(s) of+the License, but only in their entirety and only with respect to the Combined+Software.+--- /dev/null+++ b/src/src/crypto/blake3/LICENSE_CC0@@ -0,0 +1,121 @@+Creative Commons Legal Code++CC0 1.0 Universal++    CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE+    LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN+    ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS+    INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES+    REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS+    PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM+    THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED+    HEREUNDER.++Statement of Purpose++The laws of most jurisdictions throughout the world automatically confer+exclusive Copyright and Related Rights (defined below) upon the creator+and subsequent owner(s) (each and all, an "owner") of an original work of+authorship and/or a database (each, a "Work").++Certain owners wish to permanently relinquish those rights to a Work for+the purpose of contributing to a commons of creative, cultural and+scientific works ("Commons") that the public can reliably and without fear+of later claims of infringement build upon, modify, incorporate in other+works, reuse and redistribute as freely as possible in any form whatsoever+and for any purposes, including without limitation commercial purposes.+These owners may contribute to the Commons to promote the ideal of a free+culture and the further production of creative, cultural and scientific+works, or to gain reputation or greater distribution for their Work in+part through the use and efforts of others.++For these and/or other purposes and motivations, and without any+expectation of additional consideration or compensation, the person+associating CC0 with a Work (the "Affirmer"), to the extent that he or she+is an owner of Copyright and Related Rights in the Work, voluntarily+elects to apply CC0 to the Work and publicly distribute the Work under its+terms, with knowledge of his or her Copyright and Related Rights in the+Work and the meaning and intended legal effect of CC0 on those rights.++1. Copyright and Related Rights. A Work made available under CC0 may be+protected by copyright and related or neighboring rights ("Copyright and+Related Rights"). Copyright and Related Rights include, but are not+limited to, the following:++  i. the right to reproduce, adapt, distribute, perform, display,+     communicate, and translate a Work;+ ii. moral rights retained by the original author(s) and/or performer(s);+iii. publicity and privacy rights pertaining to a person's image or+     likeness depicted in a Work;+ iv. rights protecting against unfair competition in regards to a Work,+     subject to the limitations in paragraph 4(a), below;+  v. rights protecting the extraction, dissemination, use and reuse of data+     in a Work;+ vi. database rights (such as those arising under Directive 96/9/EC of the+     European Parliament and of the Council of 11 March 1996 on the legal+     protection of databases, and under any national implementation+     thereof, including any amended or successor version of such+     directive); and+vii. other similar, equivalent or corresponding rights throughout the+     world based on applicable law or treaty, and any national+     implementations thereof.++2. Waiver. To the greatest extent permitted by, but not in contravention+of, applicable law, Affirmer hereby overtly, fully, permanently,+irrevocably and unconditionally waives, abandons, and surrenders all of+Affirmer's Copyright and Related Rights and associated claims and causes+of action, whether now known or unknown (including existing as well as+future claims and causes of action), in the Work (i) in all territories+worldwide, (ii) for the maximum duration provided by applicable law or+treaty (including future time extensions), (iii) in any current or future+medium and for any number of copies, and (iv) for any purpose whatsoever,+including without limitation commercial, advertising or promotional+purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each+member of the public at large and to the detriment of Affirmer's heirs and+successors, fully intending that such Waiver shall not be subject to+revocation, rescission, cancellation, termination, or any other legal or+equitable action to disrupt the quiet enjoyment of the Work by the public+as contemplated by Affirmer's express Statement of Purpose.++3. Public License Fallback. Should any part of the Waiver for any reason+be judged legally invalid or ineffective under applicable law, then the+Waiver shall be preserved to the maximum extent permitted taking into+account Affirmer's express Statement of Purpose. In addition, to the+extent the Waiver is so judged Affirmer hereby grants to each affected+person a royalty-free, non transferable, non sublicensable, non exclusive,+irrevocable and unconditional license to exercise Affirmer's Copyright and+Related Rights in the Work (i) in all territories worldwide, (ii) for the+maximum duration provided by applicable law or treaty (including future+time extensions), (iii) in any current or future medium and for any number+of copies, and (iv) for any purpose whatsoever, including without+limitation commercial, advertising or promotional purposes (the+"License"). The License shall be deemed effective as of the date CC0 was+applied by Affirmer to the Work. Should any part of the License for any+reason be judged legally invalid or ineffective under applicable law, such+partial invalidity or ineffectiveness shall not invalidate the remainder+of the License, and in such case Affirmer hereby affirms that he or she+will not (i) exercise any of his or her remaining Copyright and Related+Rights in the Work or (ii) assert any associated claims and causes of+action with respect to the Work, in either case contrary to Affirmer's+express Statement of Purpose.++4. Limitations and Disclaimers.++ a. No trademark or patent rights held by Affirmer are waived, abandoned,+    surrendered, licensed or otherwise affected by this document.+ b. Affirmer offers the Work as-is and makes no representations or+    warranties of any kind concerning the Work, express, implied,+    statutory or otherwise, including without limitation warranties of+    title, merchantability, fitness for a particular purpose, non+    infringement, or the absence of latent or other defects, accuracy, or+    the present or absence of errors, whether or not discoverable, all to+    the greatest extent permissible under applicable law.+ c. Affirmer disclaims responsibility for clearing rights of other persons+    that may apply to the Work or any use thereof, including without+    limitation any person's Copyright and Related Rights in the Work.+    Further, Affirmer disclaims responsibility for obtaining any necessary+    consents, permissions or other rights required for any use of the+    Work.+ d. Affirmer understands and acknowledges that Creative Commons is not a+    party to this document and has no duty or obligation with respect to+    this CC0 or use of the Work.--- /dev/null+++ b/src/src/crypto/blake3/blake3.c@@ -0,0 +1,651 @@+#include <assert.h>+#include <stdbool.h>+#include <string.h>+#include <stdint.h>++#include "blake3.h"+#include "blake3_impl.h"++const char *blake3_version(void) { return BLAKE3_VERSION_STRING; }++INLINE void chunk_state_init(blake3_chunk_state *self, const uint32_t key[8],+                             uint8_t flags) {+  memcpy(self->cv, key, BLAKE3_KEY_LEN);+  self->chunk_counter = 0;+  memset(self->buf, 0, BLAKE3_BLOCK_LEN);+  self->buf_len = 0;+  self->blocks_compressed = 0;+  self->flags = flags;+}++INLINE void chunk_state_reset(blake3_chunk_state *self, const uint32_t key[8],+                              uint64_t chunk_counter) {+  memcpy(self->cv, key, BLAKE3_KEY_LEN);+  self->chunk_counter = chunk_counter;+  self->blocks_compressed = 0;+  memset(self->buf, 0, BLAKE3_BLOCK_LEN);+  self->buf_len = 0;+}++INLINE size_t chunk_state_len(const blake3_chunk_state *self) {+  return (BLAKE3_BLOCK_LEN * (size_t)self->blocks_compressed) ++         ((size_t)self->buf_len);+}++INLINE size_t chunk_state_fill_buf(blake3_chunk_state *self,+                                   const uint8_t *input, size_t input_len) {+  size_t take = BLAKE3_BLOCK_LEN - ((size_t)self->buf_len);+  if (take > input_len) {+    take = input_len;+  }+  uint8_t *dest = self->buf + ((size_t)self->buf_len);+  memcpy(dest, input, take);+  self->buf_len += (uint8_t)take;+  return take;+}++INLINE uint8_t chunk_state_maybe_start_flag(const blake3_chunk_state *self) {+  if (self->blocks_compressed == 0) {+    return CHUNK_START;+  } else {+    return 0;+  }+}++typedef struct {+  uint32_t input_cv[8];+  uint64_t counter;+  uint8_t block[BLAKE3_BLOCK_LEN];+  uint8_t block_len;+  uint8_t flags;+} output_t;++INLINE output_t make_output(const uint32_t input_cv[8],+                            const uint8_t block[BLAKE3_BLOCK_LEN],+                            uint8_t block_len, uint64_t counter,+                            uint8_t flags) {+  output_t ret;+  memcpy(ret.input_cv, input_cv, 32);+  memcpy(ret.block, block, BLAKE3_BLOCK_LEN);+  ret.block_len = block_len;+  ret.counter = counter;+  ret.flags = flags;+  return ret;+}++// Chaining values within a given chunk (specifically the compress_in_place+// interface) are represented as words. This avoids unnecessary bytes<->words+// conversion overhead in the portable implementation. However, the hash_many+// interface handles both user input and parent node blocks, so it accepts+// bytes. For that reason, chaining values in the CV stack are represented as+// bytes.+INLINE void output_chaining_value(const output_t *self, uint8_t cv[32]) {+  uint32_t cv_words[8];+  memcpy(cv_words, self->input_cv, 32);+  blake3_compress_in_place(cv_words, self->block, self->block_len,+                           self->counter, self->flags);+  store_cv_words(cv, cv_words);+}++INLINE void output_root_bytes(const output_t *self, uint64_t seek, uint8_t *out,+                              size_t out_len) {+  if (out_len == 0) {+      return;+  }+  uint64_t output_block_counter = seek / 64;+  size_t offset_within_block = seek % 64;+  uint8_t wide_buf[64];+  if(offset_within_block) {+    blake3_compress_xof(self->input_cv, self->block, self->block_len, output_block_counter, self->flags | ROOT, wide_buf);+    const size_t available_bytes = 64 - offset_within_block;+    const size_t bytes = out_len > available_bytes ? available_bytes : out_len;+    memcpy(out, wide_buf + offset_within_block, bytes);+    out += bytes;+    out_len -= bytes;+    output_block_counter += 1;+  }+  if(out_len / 64) {+    blake3_xof_many(self->input_cv, self->block, self->block_len, output_block_counter, self->flags | ROOT, out, out_len / 64);+  }+  output_block_counter += out_len / 64;+  out += out_len & -64;+  out_len -= out_len & -64;+  if(out_len) {+    blake3_compress_xof(self->input_cv, self->block, self->block_len, output_block_counter, self->flags | ROOT, wide_buf);+    memcpy(out, wide_buf, out_len);+  }+}++INLINE void chunk_state_update(blake3_chunk_state *self, const uint8_t *input,+                               size_t input_len) {+  if (self->buf_len > 0) {+    size_t take = chunk_state_fill_buf(self, input, input_len);+    input += take;+    input_len -= take;+    if (input_len > 0) {+      blake3_compress_in_place(+          self->cv, self->buf, BLAKE3_BLOCK_LEN, self->chunk_counter,+          self->flags | chunk_state_maybe_start_flag(self));+      self->blocks_compressed += 1;+      self->buf_len = 0;+      memset(self->buf, 0, BLAKE3_BLOCK_LEN);+    }+  }++  while (input_len > BLAKE3_BLOCK_LEN) {+    blake3_compress_in_place(self->cv, input, BLAKE3_BLOCK_LEN,+                             self->chunk_counter,+                             self->flags | chunk_state_maybe_start_flag(self));+    self->blocks_compressed += 1;+    input += BLAKE3_BLOCK_LEN;+    input_len -= BLAKE3_BLOCK_LEN;+  }++  chunk_state_fill_buf(self, input, input_len);+}++INLINE output_t chunk_state_output(const blake3_chunk_state *self) {+  uint8_t block_flags =+      self->flags | chunk_state_maybe_start_flag(self) | CHUNK_END;+  return make_output(self->cv, self->buf, self->buf_len, self->chunk_counter,+                     block_flags);+}++INLINE output_t parent_output(const uint8_t block[BLAKE3_BLOCK_LEN],+                              const uint32_t key[8], uint8_t flags) {+  return make_output(key, block, BLAKE3_BLOCK_LEN, 0, flags | PARENT);+}++// Given some input larger than one chunk, return the number of bytes that+// should go in the left subtree. This is the largest power-of-2 number of+// chunks that leaves at least 1 byte for the right subtree.+INLINE size_t left_subtree_len(size_t input_len) {+  // Subtract 1 to reserve at least one byte for the right side. input_len+  // should always be greater than BLAKE3_CHUNK_LEN.+  size_t full_chunks = (input_len - 1) / BLAKE3_CHUNK_LEN;+  return round_down_to_power_of_2(full_chunks) * BLAKE3_CHUNK_LEN;+}++// Use SIMD parallelism to hash up to MAX_SIMD_DEGREE chunks at the same time+// on a single thread. Write out the chunk chaining values and return the+// number of chunks hashed. These chunks are never the root and never empty;+// those cases use a different codepath.+INLINE size_t compress_chunks_parallel(const uint8_t *input, size_t input_len,+                                       const uint32_t key[8],+                                       uint64_t chunk_counter, uint8_t flags,+                                       uint8_t *out) {+#if defined(BLAKE3_TESTING)+  assert(0 < input_len);+  assert(input_len <= MAX_SIMD_DEGREE * BLAKE3_CHUNK_LEN);+#endif++  const uint8_t *chunks_array[MAX_SIMD_DEGREE];+  size_t input_position = 0;+  size_t chunks_array_len = 0;+  while (input_len - input_position >= BLAKE3_CHUNK_LEN) {+    chunks_array[chunks_array_len] = &input[input_position];+    input_position += BLAKE3_CHUNK_LEN;+    chunks_array_len += 1;+  }++  blake3_hash_many(chunks_array, chunks_array_len,+                   BLAKE3_CHUNK_LEN / BLAKE3_BLOCK_LEN, key, chunk_counter,+                   true, flags, CHUNK_START, CHUNK_END, out);++  // Hash the remaining partial chunk, if there is one. Note that the empty+  // chunk (meaning the empty message) is a different codepath.+  if (input_len > input_position) {+    uint64_t counter = chunk_counter + (uint64_t)chunks_array_len;+    blake3_chunk_state chunk_state;+    chunk_state_init(&chunk_state, key, flags);+    chunk_state.chunk_counter = counter;+    chunk_state_update(&chunk_state, &input[input_position],+                       input_len - input_position);+    output_t output = chunk_state_output(&chunk_state);+    output_chaining_value(&output, &out[chunks_array_len * BLAKE3_OUT_LEN]);+    return chunks_array_len + 1;+  } else {+    return chunks_array_len;+  }+}++// Use SIMD parallelism to hash up to MAX_SIMD_DEGREE parents at the same time+// on a single thread. Write out the parent chaining values and return the+// number of parents hashed. (If there's an odd input chaining value left over,+// return it as an additional output.) These parents are never the root and+// never empty; those cases use a different codepath.+INLINE size_t compress_parents_parallel(const uint8_t *child_chaining_values,+                                        size_t num_chaining_values,+                                        const uint32_t key[8], uint8_t flags,+                                        uint8_t *out) {+#if defined(BLAKE3_TESTING)+  assert(2 <= num_chaining_values);+  assert(num_chaining_values <= 2 * MAX_SIMD_DEGREE_OR_2);+#endif++  const uint8_t *parents_array[MAX_SIMD_DEGREE_OR_2];+  size_t parents_array_len = 0;+  while (num_chaining_values - (2 * parents_array_len) >= 2) {+    parents_array[parents_array_len] =+        &child_chaining_values[2 * parents_array_len * BLAKE3_OUT_LEN];+    parents_array_len += 1;+  }++  blake3_hash_many(parents_array, parents_array_len, 1, key,+                   0, // Parents always use counter 0.+                   false, flags | PARENT,+                   0, // Parents have no start flags.+                   0, // Parents have no end flags.+                   out);++  // If there's an odd child left over, it becomes an output.+  if (num_chaining_values > 2 * parents_array_len) {+    memcpy(&out[parents_array_len * BLAKE3_OUT_LEN],+           &child_chaining_values[2 * parents_array_len * BLAKE3_OUT_LEN],+           BLAKE3_OUT_LEN);+    return parents_array_len + 1;+  } else {+    return parents_array_len;+  }+}++// The wide helper function returns (writes out) an array of chaining values+// and returns the length of that array. The number of chaining values returned+// is the dynamically detected SIMD degree, at most MAX_SIMD_DEGREE. Or fewer,+// if the input is shorter than that many chunks. The reason for maintaining a+// wide array of chaining values going back up the tree, is to allow the+// implementation to hash as many parents in parallel as possible.+//+// As a special case when the SIMD degree is 1, this function will still return+// at least 2 outputs. This guarantees that this function doesn't perform the+// root compression. (If it did, it would use the wrong flags, and also we+// wouldn't be able to implement extendable output.) Note that this function is+// not used when the whole input is only 1 chunk long; that's a different+// codepath.+//+// Why not just have the caller split the input on the first update(), instead+// of implementing this special rule? Because we don't want to limit SIMD or+// multi-threading parallelism for that update().+size_t blake3_compress_subtree_wide(const uint8_t *input, size_t input_len,+                                    const uint32_t key[8],+                                    uint64_t chunk_counter, uint8_t flags,+                                    uint8_t *out, bool use_tbb) {+  // Note that the single chunk case does *not* bump the SIMD degree up to 2+  // when it is 1. If this implementation adds multi-threading in the future,+  // this gives us the option of multi-threading even the 2-chunk case, which+  // can help performance on smaller platforms.+  if (input_len <= blake3_simd_degree() * BLAKE3_CHUNK_LEN) {+    return compress_chunks_parallel(input, input_len, key, chunk_counter, flags,+                                    out);+  }++  // With more than simd_degree chunks, we need to recurse. Start by dividing+  // the input into left and right subtrees. (Note that this is only optimal+  // as long as the SIMD degree is a power of 2. If we ever get a SIMD degree+  // of 3 or something, we'll need a more complicated strategy.)+  size_t left_input_len = left_subtree_len(input_len);+  size_t right_input_len = input_len - left_input_len;+  const uint8_t *right_input = &input[left_input_len];+  uint64_t right_chunk_counter =+      chunk_counter + (uint64_t)(left_input_len / BLAKE3_CHUNK_LEN);++  // Make space for the child outputs. Here we use MAX_SIMD_DEGREE_OR_2 to+  // account for the special case of returning 2 outputs when the SIMD degree+  // is 1.+  uint8_t cv_array[2 * MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN];+  size_t degree = blake3_simd_degree();+  if (left_input_len > BLAKE3_CHUNK_LEN && degree == 1) {+    // The special case: We always use a degree of at least two, to make+    // sure there are two outputs. Except, as noted above, at the chunk+    // level, where we allow degree=1. (Note that the 1-chunk-input case is+    // a different codepath.)+    degree = 2;+  }+  uint8_t *right_cvs = &cv_array[degree * BLAKE3_OUT_LEN];++  // Recurse!+  size_t left_n = SIZE_MAX;+  size_t right_n = SIZE_MAX;++#if defined(BLAKE3_USE_TBB)+  blake3_compress_subtree_wide_join_tbb(+      key, flags, use_tbb,+      // left-hand side+      input, left_input_len, chunk_counter, cv_array, &left_n,+      // right-hand side+      right_input, right_input_len, right_chunk_counter, right_cvs, &right_n);+#else+  left_n = blake3_compress_subtree_wide(+      input, left_input_len, key, chunk_counter, flags, cv_array, use_tbb);+  right_n = blake3_compress_subtree_wide(right_input, right_input_len, key,+                                         right_chunk_counter, flags, right_cvs,+                                         use_tbb);+#endif // BLAKE3_USE_TBB++  // The special case again. If simd_degree=1, then we'll have left_n=1 and+  // right_n=1. Rather than compressing them into a single output, return+  // them directly, to make sure we always have at least two outputs.+  if (left_n == 1) {+    memcpy(out, cv_array, 2 * BLAKE3_OUT_LEN);+    return 2;+  }++  // Otherwise, do one layer of parent node compression.+  size_t num_chaining_values = left_n + right_n;+  return compress_parents_parallel(cv_array, num_chaining_values, key, flags,+                                   out);+}++// Hash a subtree with compress_subtree_wide(), and then condense the resulting+// list of chaining values down to a single parent node. Don't compress that+// last parent node, however. Instead, return its message bytes (the+// concatenated chaining values of its children). This is necessary when the+// first call to update() supplies a complete subtree, because the topmost+// parent node of that subtree could end up being the root. It's also necessary+// for extended output in the general case.+//+// As with compress_subtree_wide(), this function is not used on inputs of 1+// chunk or less. That's a different codepath.+INLINE void+compress_subtree_to_parent_node(const uint8_t *input, size_t input_len,+                                const uint32_t key[8], uint64_t chunk_counter,+                                uint8_t flags, uint8_t out[2 * BLAKE3_OUT_LEN],+                                bool use_tbb) {+#if defined(BLAKE3_TESTING)+  assert(input_len > BLAKE3_CHUNK_LEN);+#endif++  uint8_t cv_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN];+  size_t num_cvs = blake3_compress_subtree_wide(input, input_len, key,+                                                chunk_counter, flags, cv_array, use_tbb);+  assert(num_cvs <= MAX_SIMD_DEGREE_OR_2);+  // The following loop never executes when MAX_SIMD_DEGREE_OR_2 is 2, because+  // as we just asserted, num_cvs will always be <=2 in that case. But GCC+  // (particularly GCC 8.5) can't tell that it never executes, and if NDEBUG is+  // set then it emits incorrect warnings here. We tried a few different+  // hacks to silence these, but in the end our hacks just produced different+  // warnings (see https://github.com/BLAKE3-team/BLAKE3/pull/380). Out of+  // desperation, we ifdef out this entire loop when we know it's not needed.+#if MAX_SIMD_DEGREE_OR_2 > 2+  // If MAX_SIMD_DEGREE_OR_2 is greater than 2 and there's enough input,+  // compress_subtree_wide() returns more than 2 chaining values. Condense+  // them into 2 by forming parent nodes repeatedly.+  uint8_t out_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN / 2];+  while (num_cvs > 2) {+    num_cvs =+        compress_parents_parallel(cv_array, num_cvs, key, flags, out_array);+    memcpy(cv_array, out_array, num_cvs * BLAKE3_OUT_LEN);+  }+#endif+  memcpy(out, cv_array, 2 * BLAKE3_OUT_LEN);+}++INLINE void hasher_init_base(blake3_hasher *self, const uint32_t key[8],+                             uint8_t flags) {+  memcpy(self->key, key, BLAKE3_KEY_LEN);+  chunk_state_init(&self->chunk, key, flags);+  self->cv_stack_len = 0;+}++void blake3_hasher_init(blake3_hasher *self) { hasher_init_base(self, IV, 0); }++void blake3_hasher_init_keyed(blake3_hasher *self,+                              const uint8_t key[BLAKE3_KEY_LEN]) {+  uint32_t key_words[8];+  load_key_words(key, key_words);+  hasher_init_base(self, key_words, KEYED_HASH);+}++void blake3_hasher_init_derive_key_raw(blake3_hasher *self, const void *context,+                                       size_t context_len) {+  blake3_hasher context_hasher;+  hasher_init_base(&context_hasher, IV, DERIVE_KEY_CONTEXT);+  blake3_hasher_update(&context_hasher, context, context_len);+  uint8_t context_key[BLAKE3_KEY_LEN];+  blake3_hasher_finalize(&context_hasher, context_key, BLAKE3_KEY_LEN);+  uint32_t context_key_words[8];+  load_key_words(context_key, context_key_words);+  hasher_init_base(self, context_key_words, DERIVE_KEY_MATERIAL);+}++void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context) {+  blake3_hasher_init_derive_key_raw(self, context, strlen(context));+}++// As described in hasher_push_cv() below, we do "lazy merging", delaying+// merges until right before the next CV is about to be added. This is+// different from the reference implementation. Another difference is that we+// aren't always merging 1 chunk at a time. Instead, each CV might represent+// any power-of-two number of chunks, as long as the smaller-above-larger stack+// order is maintained. Instead of the "count the trailing 0-bits" algorithm+// described in the spec, we use a "count the total number of 1-bits" variant+// that doesn't require us to retain the subtree size of the CV on top of the+// stack. The principle is the same: each CV that should remain in the stack is+// represented by a 1-bit in the total number of chunks (or bytes) so far.+INLINE void hasher_merge_cv_stack(blake3_hasher *self, uint64_t total_len) {+  size_t post_merge_stack_len = (size_t)popcnt(total_len);+  while (self->cv_stack_len > post_merge_stack_len) {+    uint8_t *parent_node =+        &self->cv_stack[(self->cv_stack_len - 2) * BLAKE3_OUT_LEN];+    output_t output = parent_output(parent_node, self->key, self->chunk.flags);+    output_chaining_value(&output, parent_node);+    self->cv_stack_len -= 1;+  }+}++// In reference_impl.rs, we merge the new CV with existing CVs from the stack+// before pushing it. We can do that because we know more input is coming, so+// we know none of the merges are root.+//+// This setting is different. We want to feed as much input as possible to+// compress_subtree_wide(), without setting aside anything for the chunk_state.+// If the user gives us 64 KiB, we want to parallelize over all 64 KiB at once+// as a single subtree, if at all possible.+//+// This leads to two problems:+// 1) This 64 KiB input might be the only call that ever gets made to update.+//    In this case, the root node of the 64 KiB subtree would be the root node+//    of the whole tree, and it would need to be ROOT finalized. We can't+//    compress it until we know.+// 2) This 64 KiB input might complete a larger tree, whose root node is+//    similarly going to be the root of the whole tree. For example, maybe+//    we have 196 KiB (that is, 128 + 64) hashed so far. We can't compress the+//    node at the root of the 256 KiB subtree until we know how to finalize it.+//+// The second problem is solved with "lazy merging". That is, when we're about+// to add a CV to the stack, we don't merge it with anything first, as the+// reference impl does. Instead we do merges using the *previous* CV that was+// added, which is sitting on top of the stack, and we put the new CV+// (unmerged) on top of the stack afterwards. This guarantees that we never+// merge the root node until finalize().+//+// Solving the first problem requires an additional tool,+// compress_subtree_to_parent_node(). That function always returns the top+// *two* chaining values of the subtree it's compressing. We then do lazy+// merging with each of them separately, so that the second CV will always+// remain unmerged. (That also helps us support extendable output when we're+// hashing an input all-at-once.)+INLINE void hasher_push_cv(blake3_hasher *self, uint8_t new_cv[BLAKE3_OUT_LEN],+                           uint64_t chunk_counter) {+  hasher_merge_cv_stack(self, chunk_counter);+  memcpy(&self->cv_stack[self->cv_stack_len * BLAKE3_OUT_LEN], new_cv,+         BLAKE3_OUT_LEN);+  self->cv_stack_len += 1;+}++INLINE void blake3_hasher_update_base(blake3_hasher *self, const void *input,+                                      size_t input_len, bool use_tbb) {+  // Explicitly checking for zero avoids causing UB by passing a null pointer+  // to memcpy. This comes up in practice with things like:+  //   std::vector<uint8_t> v;+  //   blake3_hasher_update(&hasher, v.data(), v.size());+  if (input_len == 0) {+    return;+  }++  const uint8_t *input_bytes = (const uint8_t *)input;++  // If we have some partial chunk bytes in the internal chunk_state, we need+  // to finish that chunk first.+  if (chunk_state_len(&self->chunk) > 0) {+    size_t take = BLAKE3_CHUNK_LEN - chunk_state_len(&self->chunk);+    if (take > input_len) {+      take = input_len;+    }+    chunk_state_update(&self->chunk, input_bytes, take);+    input_bytes += take;+    input_len -= take;+    // If we've filled the current chunk and there's more coming, finalize this+    // chunk and proceed. In this case we know it's not the root.+    if (input_len > 0) {+      output_t output = chunk_state_output(&self->chunk);+      uint8_t chunk_cv[32];+      output_chaining_value(&output, chunk_cv);+      hasher_push_cv(self, chunk_cv, self->chunk.chunk_counter);+      chunk_state_reset(&self->chunk, self->key, self->chunk.chunk_counter + 1);+    } else {+      return;+    }+  }++  // Now the chunk_state is clear, and we have more input. If there's more than+  // a single chunk (so, definitely not the root chunk), hash the largest whole+  // subtree we can, with the full benefits of SIMD (and maybe in the future,+  // multi-threading) parallelism. Two restrictions:+  // - The subtree has to be a power-of-2 number of chunks. Only subtrees along+  //   the right edge can be incomplete, and we don't know where the right edge+  //   is going to be until we get to finalize().+  // - The subtree must evenly divide the total number of chunks up until this+  //   point (if total is not 0). If the current incomplete subtree is only+  //   waiting for 1 more chunk, we can't hash a subtree of 4 chunks. We have+  //   to complete the current subtree first.+  // Because we might need to break up the input to form powers of 2, or to+  // evenly divide what we already have, this part runs in a loop.+  while (input_len > BLAKE3_CHUNK_LEN) {+    size_t subtree_len = round_down_to_power_of_2(input_len);+    uint64_t count_so_far = self->chunk.chunk_counter * BLAKE3_CHUNK_LEN;+    // Shrink the subtree_len until it evenly divides the count so far. We know+    // that subtree_len itself is a power of 2, so we can use a bitmasking+    // trick instead of an actual remainder operation. (Note that if the caller+    // consistently passes power-of-2 inputs of the same size, as is hopefully+    // typical, this loop condition will always fail, and subtree_len will+    // always be the full length of the input.)+    //+    // An aside: We don't have to shrink subtree_len quite this much. For+    // example, if count_so_far is 1, we could pass 2 chunks to+    // compress_subtree_to_parent_node. Since we'll get 2 CVs back, we'll still+    // get the right answer in the end, and we might get to use 2-way SIMD+    // parallelism. The problem with this optimization, is that it gets us+    // stuck always hashing 2 chunks. The total number of chunks will remain+    // odd, and we'll never graduate to higher degrees of parallelism. See+    // https://github.com/BLAKE3-team/BLAKE3/issues/69.+    while ((((uint64_t)(subtree_len - 1)) & count_so_far) != 0) {+      subtree_len /= 2;+    }+    // The shrunken subtree_len might now be 1 chunk long. If so, hash that one+    // chunk by itself. Otherwise, compress the subtree into a pair of CVs.+    uint64_t subtree_chunks = subtree_len / BLAKE3_CHUNK_LEN;+    if (subtree_len <= BLAKE3_CHUNK_LEN) {+      blake3_chunk_state chunk_state;+      chunk_state_init(&chunk_state, self->key, self->chunk.flags);+      chunk_state.chunk_counter = self->chunk.chunk_counter;+      chunk_state_update(&chunk_state, input_bytes, subtree_len);+      output_t output = chunk_state_output(&chunk_state);+      uint8_t cv[BLAKE3_OUT_LEN];+      output_chaining_value(&output, cv);+      hasher_push_cv(self, cv, chunk_state.chunk_counter);+    } else {+      // This is the high-performance happy path, though getting here depends+      // on the caller giving us a long enough input.+      uint8_t cv_pair[2 * BLAKE3_OUT_LEN];+      compress_subtree_to_parent_node(input_bytes, subtree_len, self->key,+                                      self->chunk.chunk_counter,+                                      self->chunk.flags, cv_pair, use_tbb);+      hasher_push_cv(self, cv_pair, self->chunk.chunk_counter);+      hasher_push_cv(self, &cv_pair[BLAKE3_OUT_LEN],+                     self->chunk.chunk_counter + (subtree_chunks / 2));+    }+    self->chunk.chunk_counter += subtree_chunks;+    input_bytes += subtree_len;+    input_len -= subtree_len;+  }++  // If there's any remaining input less than a full chunk, add it to the chunk+  // state. In that case, also do a final merge loop to make sure the subtree+  // stack doesn't contain any unmerged pairs. The remaining input means we+  // know these merges are non-root. This merge loop isn't strictly necessary+  // here, because hasher_push_chunk_cv already does its own merge loop, but it+  // simplifies blake3_hasher_finalize below.+  if (input_len > 0) {+    chunk_state_update(&self->chunk, input_bytes, input_len);+    hasher_merge_cv_stack(self, self->chunk.chunk_counter);+  }+}++void blake3_hasher_update(blake3_hasher *self, const void *input,+                          size_t input_len) {+  bool use_tbb = false;+  blake3_hasher_update_base(self, input, input_len, use_tbb);+}++#if defined(BLAKE3_USE_TBB)+void blake3_hasher_update_tbb(blake3_hasher *self, const void *input,+                              size_t input_len) {+  bool use_tbb = true;+  blake3_hasher_update_base(self, input, input_len, use_tbb);+}+#endif // BLAKE3_USE_TBB++void blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out,+                            size_t out_len) {+  blake3_hasher_finalize_seek(self, 0, out, out_len);+}++void blake3_hasher_finalize_seek(const blake3_hasher *self, uint64_t seek,+                                 uint8_t *out, size_t out_len) {+  // Explicitly checking for zero avoids causing UB by passing a null pointer+  // to memcpy. This comes up in practice with things like:+  //   std::vector<uint8_t> v;+  //   blake3_hasher_finalize(&hasher, v.data(), v.size());+  if (out_len == 0) {+    return;+  }++  // If the subtree stack is empty, then the current chunk is the root.+  if (self->cv_stack_len == 0) {+    output_t output = chunk_state_output(&self->chunk);+    output_root_bytes(&output, seek, out, out_len);+    return;+  }+  // If there are any bytes in the chunk state, finalize that chunk and do a+  // roll-up merge between that chunk hash and every subtree in the stack. In+  // this case, the extra merge loop at the end of blake3_hasher_update+  // guarantees that none of the subtrees in the stack need to be merged with+  // each other first. Otherwise, if there are no bytes in the chunk state,+  // then the top of the stack is a chunk hash, and we start the merge from+  // that.+  output_t output;+  size_t cvs_remaining;+  if (chunk_state_len(&self->chunk) > 0) {+    cvs_remaining = self->cv_stack_len;+    output = chunk_state_output(&self->chunk);+  } else {+    // There are always at least 2 CVs in the stack in this case.+    cvs_remaining = self->cv_stack_len - 2;+    output = parent_output(&self->cv_stack[cvs_remaining * 32], self->key,+                           self->chunk.flags);+  }+  while (cvs_remaining > 0) {+    cvs_remaining -= 1;+    uint8_t parent_block[BLAKE3_BLOCK_LEN];+    memcpy(parent_block, &self->cv_stack[cvs_remaining * 32], 32);+    output_chaining_value(&output, &parent_block[32]);+    output = parent_output(parent_block, self->key, self->chunk.flags);+  }+  output_root_bytes(&output, seek, out, out_len);+}++void blake3_hasher_reset(blake3_hasher *self) {+  chunk_state_reset(&self->chunk, self->key, 0);+  self->cv_stack_len = 0;+}--- /dev/null+++ b/src/src/crypto/blake3/blake3.h@@ -0,0 +1,86 @@+#ifndef BLAKE3_H+#define BLAKE3_H++#include <stddef.h>+#include <stdint.h>++#if !defined(BLAKE3_API)+# if defined(_WIN32) || defined(__CYGWIN__)+#   if defined(BLAKE3_DLL)+#     if defined(BLAKE3_DLL_EXPORTS)+#       define BLAKE3_API __declspec(dllexport)+#     else+#       define BLAKE3_API __declspec(dllimport)+#     endif+#     define BLAKE3_PRIVATE+#   else+#     define BLAKE3_API+#     define BLAKE3_PRIVATE+#   endif+# elif __GNUC__ >= 4+#   define BLAKE3_API __attribute__((visibility("default")))+#   define BLAKE3_PRIVATE __attribute__((visibility("hidden")))+# else+#   define BLAKE3_API+#   define BLAKE3_PRIVATE+# endif+#endif++#ifdef __cplusplus+extern "C" {+#endif++#define BLAKE3_VERSION_STRING "1.8.7"+#define BLAKE3_KEY_LEN 32+#define BLAKE3_OUT_LEN 32+#define BLAKE3_BLOCK_LEN 64+#define BLAKE3_CHUNK_LEN 1024+#define BLAKE3_MAX_DEPTH 54++// This struct is a private implementation detail. It has to be here because+// it's part of the blake3_hasher structure defined below.+typedef struct {+  uint32_t cv[8];+  uint64_t chunk_counter;+  uint8_t buf[BLAKE3_BLOCK_LEN];+  uint8_t buf_len;+  uint8_t blocks_compressed;+  uint8_t flags;+} blake3_chunk_state;++typedef struct {+  uint32_t key[8];+  blake3_chunk_state chunk;+  uint8_t cv_stack_len;+  // The stack size is MAX_DEPTH + 1 because we do lazy merging. For example,+  // with 7 chunks, we have 3 entries in the stack. Adding an 8th chunk+  // requires a 4th entry, rather than merging everything down to 1, because we+  // don't know whether more input is coming. This is different from how the+  // reference implementation does things.+  uint8_t cv_stack[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN];+} blake3_hasher;++BLAKE3_API const char *blake3_version(void);+BLAKE3_API void blake3_hasher_init(blake3_hasher *self);+BLAKE3_API void blake3_hasher_init_keyed(blake3_hasher *self,+                                         const uint8_t key[BLAKE3_KEY_LEN]);+BLAKE3_API void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context);+BLAKE3_API void blake3_hasher_init_derive_key_raw(blake3_hasher *self, const void *context,+                                                  size_t context_len);+BLAKE3_API void blake3_hasher_update(blake3_hasher *self, const void *input,+                                     size_t input_len);+#if defined(BLAKE3_USE_TBB)+BLAKE3_API void blake3_hasher_update_tbb(blake3_hasher *self, const void *input,+                                         size_t input_len);+#endif // BLAKE3_USE_TBB+BLAKE3_API void blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out,+                                       size_t out_len);+BLAKE3_API void blake3_hasher_finalize_seek(const blake3_hasher *self, uint64_t seek,+                                            uint8_t *out, size_t out_len);+BLAKE3_API void blake3_hasher_reset(blake3_hasher *self);++#ifdef __cplusplus+}+#endif++#endif /* BLAKE3_H */--- /dev/null+++ b/src/src/crypto/blake3/blake3_avx2.c@@ -0,0 +1,326 @@+#include "blake3_impl.h"++#include <immintrin.h>++#define DEGREE 8++INLINE __m256i loadu(const uint8_t src[32]) {+  return _mm256_loadu_si256((const __m256i *)src);+}++INLINE void storeu(__m256i src, uint8_t dest[16]) {+  _mm256_storeu_si256((__m256i *)dest, src);+}++INLINE __m256i addv(__m256i a, __m256i b) { return _mm256_add_epi32(a, b); }++// Note that clang-format doesn't like the name "xor" for some reason.+INLINE __m256i xorv(__m256i a, __m256i b) { return _mm256_xor_si256(a, b); }++INLINE __m256i set1(uint32_t x) { return _mm256_set1_epi32((int32_t)x); }++INLINE __m256i rot16(__m256i x) {+  return _mm256_shuffle_epi8(+      x, _mm256_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2,+                         13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2));+}++INLINE __m256i rot12(__m256i x) {+  return _mm256_or_si256(_mm256_srli_epi32(x, 12), _mm256_slli_epi32(x, 32 - 12));+}++INLINE __m256i rot8(__m256i x) {+  return _mm256_shuffle_epi8(+      x, _mm256_set_epi8(12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1,+                         12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1));+}++INLINE __m256i rot7(__m256i x) {+  return _mm256_or_si256(_mm256_srli_epi32(x, 7), _mm256_slli_epi32(x, 32 - 7));+}++INLINE void round_fn(__m256i v[16], __m256i m[16], size_t r) {+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);+  v[0] = addv(v[0], v[4]);+  v[1] = addv(v[1], v[5]);+  v[2] = addv(v[2], v[6]);+  v[3] = addv(v[3], v[7]);+  v[12] = xorv(v[12], v[0]);+  v[13] = xorv(v[13], v[1]);+  v[14] = xorv(v[14], v[2]);+  v[15] = xorv(v[15], v[3]);+  v[12] = rot16(v[12]);+  v[13] = rot16(v[13]);+  v[14] = rot16(v[14]);+  v[15] = rot16(v[15]);+  v[8] = addv(v[8], v[12]);+  v[9] = addv(v[9], v[13]);+  v[10] = addv(v[10], v[14]);+  v[11] = addv(v[11], v[15]);+  v[4] = xorv(v[4], v[8]);+  v[5] = xorv(v[5], v[9]);+  v[6] = xorv(v[6], v[10]);+  v[7] = xorv(v[7], v[11]);+  v[4] = rot12(v[4]);+  v[5] = rot12(v[5]);+  v[6] = rot12(v[6]);+  v[7] = rot12(v[7]);+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);+  v[0] = addv(v[0], v[4]);+  v[1] = addv(v[1], v[5]);+  v[2] = addv(v[2], v[6]);+  v[3] = addv(v[3], v[7]);+  v[12] = xorv(v[12], v[0]);+  v[13] = xorv(v[13], v[1]);+  v[14] = xorv(v[14], v[2]);+  v[15] = xorv(v[15], v[3]);+  v[12] = rot8(v[12]);+  v[13] = rot8(v[13]);+  v[14] = rot8(v[14]);+  v[15] = rot8(v[15]);+  v[8] = addv(v[8], v[12]);+  v[9] = addv(v[9], v[13]);+  v[10] = addv(v[10], v[14]);+  v[11] = addv(v[11], v[15]);+  v[4] = xorv(v[4], v[8]);+  v[5] = xorv(v[5], v[9]);+  v[6] = xorv(v[6], v[10]);+  v[7] = xorv(v[7], v[11]);+  v[4] = rot7(v[4]);+  v[5] = rot7(v[5]);+  v[6] = rot7(v[6]);+  v[7] = rot7(v[7]);++  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);+  v[0] = addv(v[0], v[5]);+  v[1] = addv(v[1], v[6]);+  v[2] = addv(v[2], v[7]);+  v[3] = addv(v[3], v[4]);+  v[15] = xorv(v[15], v[0]);+  v[12] = xorv(v[12], v[1]);+  v[13] = xorv(v[13], v[2]);+  v[14] = xorv(v[14], v[3]);+  v[15] = rot16(v[15]);+  v[12] = rot16(v[12]);+  v[13] = rot16(v[13]);+  v[14] = rot16(v[14]);+  v[10] = addv(v[10], v[15]);+  v[11] = addv(v[11], v[12]);+  v[8] = addv(v[8], v[13]);+  v[9] = addv(v[9], v[14]);+  v[5] = xorv(v[5], v[10]);+  v[6] = xorv(v[6], v[11]);+  v[7] = xorv(v[7], v[8]);+  v[4] = xorv(v[4], v[9]);+  v[5] = rot12(v[5]);+  v[6] = rot12(v[6]);+  v[7] = rot12(v[7]);+  v[4] = rot12(v[4]);+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);+  v[0] = addv(v[0], v[5]);+  v[1] = addv(v[1], v[6]);+  v[2] = addv(v[2], v[7]);+  v[3] = addv(v[3], v[4]);+  v[15] = xorv(v[15], v[0]);+  v[12] = xorv(v[12], v[1]);+  v[13] = xorv(v[13], v[2]);+  v[14] = xorv(v[14], v[3]);+  v[15] = rot8(v[15]);+  v[12] = rot8(v[12]);+  v[13] = rot8(v[13]);+  v[14] = rot8(v[14]);+  v[10] = addv(v[10], v[15]);+  v[11] = addv(v[11], v[12]);+  v[8] = addv(v[8], v[13]);+  v[9] = addv(v[9], v[14]);+  v[5] = xorv(v[5], v[10]);+  v[6] = xorv(v[6], v[11]);+  v[7] = xorv(v[7], v[8]);+  v[4] = xorv(v[4], v[9]);+  v[5] = rot7(v[5]);+  v[6] = rot7(v[6]);+  v[7] = rot7(v[7]);+  v[4] = rot7(v[4]);+}++INLINE void transpose_vecs(__m256i vecs[DEGREE]) {+  // Interleave 32-bit lanes. The low unpack is lanes 00/11/44/55, and the high+  // is 22/33/66/77.+  __m256i ab_0145 = _mm256_unpacklo_epi32(vecs[0], vecs[1]);+  __m256i ab_2367 = _mm256_unpackhi_epi32(vecs[0], vecs[1]);+  __m256i cd_0145 = _mm256_unpacklo_epi32(vecs[2], vecs[3]);+  __m256i cd_2367 = _mm256_unpackhi_epi32(vecs[2], vecs[3]);+  __m256i ef_0145 = _mm256_unpacklo_epi32(vecs[4], vecs[5]);+  __m256i ef_2367 = _mm256_unpackhi_epi32(vecs[4], vecs[5]);+  __m256i gh_0145 = _mm256_unpacklo_epi32(vecs[6], vecs[7]);+  __m256i gh_2367 = _mm256_unpackhi_epi32(vecs[6], vecs[7]);++  // Interleave 64-bit lanes. The low unpack is lanes 00/22 and the high is+  // 11/33.+  __m256i abcd_04 = _mm256_unpacklo_epi64(ab_0145, cd_0145);+  __m256i abcd_15 = _mm256_unpackhi_epi64(ab_0145, cd_0145);+  __m256i abcd_26 = _mm256_unpacklo_epi64(ab_2367, cd_2367);+  __m256i abcd_37 = _mm256_unpackhi_epi64(ab_2367, cd_2367);+  __m256i efgh_04 = _mm256_unpacklo_epi64(ef_0145, gh_0145);+  __m256i efgh_15 = _mm256_unpackhi_epi64(ef_0145, gh_0145);+  __m256i efgh_26 = _mm256_unpacklo_epi64(ef_2367, gh_2367);+  __m256i efgh_37 = _mm256_unpackhi_epi64(ef_2367, gh_2367);++  // Interleave 128-bit lanes.+  vecs[0] = _mm256_permute2x128_si256(abcd_04, efgh_04, 0x20);+  vecs[1] = _mm256_permute2x128_si256(abcd_15, efgh_15, 0x20);+  vecs[2] = _mm256_permute2x128_si256(abcd_26, efgh_26, 0x20);+  vecs[3] = _mm256_permute2x128_si256(abcd_37, efgh_37, 0x20);+  vecs[4] = _mm256_permute2x128_si256(abcd_04, efgh_04, 0x31);+  vecs[5] = _mm256_permute2x128_si256(abcd_15, efgh_15, 0x31);+  vecs[6] = _mm256_permute2x128_si256(abcd_26, efgh_26, 0x31);+  vecs[7] = _mm256_permute2x128_si256(abcd_37, efgh_37, 0x31);+}++INLINE void transpose_msg_vecs(const uint8_t *const *inputs,+                               size_t block_offset, __m256i out[16]) {+  out[0] = loadu(&inputs[0][block_offset + 0 * sizeof(__m256i)]);+  out[1] = loadu(&inputs[1][block_offset + 0 * sizeof(__m256i)]);+  out[2] = loadu(&inputs[2][block_offset + 0 * sizeof(__m256i)]);+  out[3] = loadu(&inputs[3][block_offset + 0 * sizeof(__m256i)]);+  out[4] = loadu(&inputs[4][block_offset + 0 * sizeof(__m256i)]);+  out[5] = loadu(&inputs[5][block_offset + 0 * sizeof(__m256i)]);+  out[6] = loadu(&inputs[6][block_offset + 0 * sizeof(__m256i)]);+  out[7] = loadu(&inputs[7][block_offset + 0 * sizeof(__m256i)]);+  out[8] = loadu(&inputs[0][block_offset + 1 * sizeof(__m256i)]);+  out[9] = loadu(&inputs[1][block_offset + 1 * sizeof(__m256i)]);+  out[10] = loadu(&inputs[2][block_offset + 1 * sizeof(__m256i)]);+  out[11] = loadu(&inputs[3][block_offset + 1 * sizeof(__m256i)]);+  out[12] = loadu(&inputs[4][block_offset + 1 * sizeof(__m256i)]);+  out[13] = loadu(&inputs[5][block_offset + 1 * sizeof(__m256i)]);+  out[14] = loadu(&inputs[6][block_offset + 1 * sizeof(__m256i)]);+  out[15] = loadu(&inputs[7][block_offset + 1 * sizeof(__m256i)]);+  for (size_t i = 0; i < 8; ++i) {+    _mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);+  }+  transpose_vecs(&out[0]);+  transpose_vecs(&out[8]);+}++INLINE void load_counters(uint64_t counter, bool increment_counter,+                          __m256i *out_lo, __m256i *out_hi) {+  const __m256i mask = _mm256_set1_epi32(-(int32_t)increment_counter);+  const __m256i add0 = _mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0);+  const __m256i add1 = _mm256_and_si256(mask, add0);+  __m256i l = _mm256_add_epi32(_mm256_set1_epi32((int32_t)counter), add1);+  __m256i carry = _mm256_cmpgt_epi32(_mm256_xor_si256(add1, _mm256_set1_epi32(0x80000000)), +                                     _mm256_xor_si256(   l, _mm256_set1_epi32(0x80000000)));+  __m256i h = _mm256_sub_epi32(_mm256_set1_epi32((int32_t)(counter >> 32)), carry);+  *out_lo = l;+  *out_hi = h;+}++static+void blake3_hash8_avx2(const uint8_t *const *inputs, size_t blocks,+                       const uint32_t key[8], uint64_t counter,+                       bool increment_counter, uint8_t flags,+                       uint8_t flags_start, uint8_t flags_end, uint8_t *out) {+  __m256i h_vecs[8] = {+      set1(key[0]), set1(key[1]), set1(key[2]), set1(key[3]),+      set1(key[4]), set1(key[5]), set1(key[6]), set1(key[7]),+  };+  __m256i counter_low_vec, counter_high_vec;+  load_counters(counter, increment_counter, &counter_low_vec,+                &counter_high_vec);+  uint8_t block_flags = flags | flags_start;++  for (size_t block = 0; block < blocks; block++) {+    if (block + 1 == blocks) {+      block_flags |= flags_end;+    }+    __m256i block_len_vec = set1(BLAKE3_BLOCK_LEN);+    __m256i block_flags_vec = set1(block_flags);+    __m256i msg_vecs[16];+    transpose_msg_vecs(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);++    __m256i v[16] = {+        h_vecs[0],       h_vecs[1],        h_vecs[2],     h_vecs[3],+        h_vecs[4],       h_vecs[5],        h_vecs[6],     h_vecs[7],+        set1(IV[0]),     set1(IV[1]),      set1(IV[2]),   set1(IV[3]),+        counter_low_vec, counter_high_vec, block_len_vec, block_flags_vec,+    };+    round_fn(v, msg_vecs, 0);+    round_fn(v, msg_vecs, 1);+    round_fn(v, msg_vecs, 2);+    round_fn(v, msg_vecs, 3);+    round_fn(v, msg_vecs, 4);+    round_fn(v, msg_vecs, 5);+    round_fn(v, msg_vecs, 6);+    h_vecs[0] = xorv(v[0], v[8]);+    h_vecs[1] = xorv(v[1], v[9]);+    h_vecs[2] = xorv(v[2], v[10]);+    h_vecs[3] = xorv(v[3], v[11]);+    h_vecs[4] = xorv(v[4], v[12]);+    h_vecs[5] = xorv(v[5], v[13]);+    h_vecs[6] = xorv(v[6], v[14]);+    h_vecs[7] = xorv(v[7], v[15]);++    block_flags = flags;+  }++  transpose_vecs(h_vecs);+  storeu(h_vecs[0], &out[0 * sizeof(__m256i)]);+  storeu(h_vecs[1], &out[1 * sizeof(__m256i)]);+  storeu(h_vecs[2], &out[2 * sizeof(__m256i)]);+  storeu(h_vecs[3], &out[3 * sizeof(__m256i)]);+  storeu(h_vecs[4], &out[4 * sizeof(__m256i)]);+  storeu(h_vecs[5], &out[5 * sizeof(__m256i)]);+  storeu(h_vecs[6], &out[6 * sizeof(__m256i)]);+  storeu(h_vecs[7], &out[7 * sizeof(__m256i)]);+}++#if !defined(BLAKE3_NO_SSE41)+void blake3_hash_many_sse41(const uint8_t *const *inputs, size_t num_inputs,+                            size_t blocks, const uint32_t key[8],+                            uint64_t counter, bool increment_counter,+                            uint8_t flags, uint8_t flags_start,+                            uint8_t flags_end, uint8_t *out);+#else+void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs,+                               size_t blocks, const uint32_t key[8],+                               uint64_t counter, bool increment_counter,+                               uint8_t flags, uint8_t flags_start,+                               uint8_t flags_end, uint8_t *out);+#endif++void blake3_hash_many_avx2(const uint8_t *const *inputs, size_t num_inputs,+                           size_t blocks, const uint32_t key[8],+                           uint64_t counter, bool increment_counter,+                           uint8_t flags, uint8_t flags_start,+                           uint8_t flags_end, uint8_t *out) {+  while (num_inputs >= DEGREE) {+    blake3_hash8_avx2(inputs, blocks, key, counter, increment_counter, flags,+                      flags_start, flags_end, out);+    if (increment_counter) {+      counter += DEGREE;+    }+    inputs += DEGREE;+    num_inputs -= DEGREE;+    out = &out[DEGREE * BLAKE3_OUT_LEN];+  }+#if !defined(BLAKE3_NO_SSE41)+  blake3_hash_many_sse41(inputs, num_inputs, blocks, key, counter,+                         increment_counter, flags, flags_start, flags_end, out);+#else+  blake3_hash_many_portable(inputs, num_inputs, blocks, key, counter,+                            increment_counter, flags, flags_start, flags_end,+                            out);+#endif+}--- /dev/null+++ b/src/src/crypto/blake3/blake3_avx512.c@@ -0,0 +1,1388 @@+#include "blake3_impl.h"++#include <immintrin.h>++#define _mm_shuffle_ps2(a, b, c)                                               \+  (_mm_castps_si128(                                                           \+      _mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), (c))))++INLINE __m128i loadu_128(const uint8_t src[16]) {+  return _mm_loadu_si128((void*)src);+}++INLINE __m256i loadu_256(const uint8_t src[32]) {+  return _mm256_loadu_si256((void*)src);+}++INLINE __m512i loadu_512(const uint8_t src[64]) {+  return _mm512_loadu_si512((void*)src);+}++INLINE void storeu_128(__m128i src, uint8_t dest[16]) {+  _mm_storeu_si128((void*)dest, src);+}++INLINE void storeu_256(__m256i src, uint8_t dest[16]) {+  _mm256_storeu_si256((void*)dest, src);+}++INLINE void storeu_512(__m512i src, uint8_t dest[16]) {+  _mm512_storeu_si512((void*)dest, src);+}++INLINE __m128i add_128(__m128i a, __m128i b) { return _mm_add_epi32(a, b); }++INLINE __m256i add_256(__m256i a, __m256i b) { return _mm256_add_epi32(a, b); }++INLINE __m512i add_512(__m512i a, __m512i b) { return _mm512_add_epi32(a, b); }++INLINE __m128i xor_128(__m128i a, __m128i b) { return _mm_xor_si128(a, b); }++INLINE __m256i xor_256(__m256i a, __m256i b) { return _mm256_xor_si256(a, b); }++INLINE __m512i xor_512(__m512i a, __m512i b) { return _mm512_xor_si512(a, b); }++INLINE __m128i set1_128(uint32_t x) { return _mm_set1_epi32((int32_t)x); }++INLINE __m256i set1_256(uint32_t x) { return _mm256_set1_epi32((int32_t)x); }++INLINE __m512i set1_512(uint32_t x) { return _mm512_set1_epi32((int32_t)x); }++INLINE __m128i set4(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {+  return _mm_setr_epi32((int32_t)a, (int32_t)b, (int32_t)c, (int32_t)d);+}++INLINE __m128i rot16_128(__m128i x) { return _mm_ror_epi32(x, 16); }++INLINE __m256i rot16_256(__m256i x) { return _mm256_ror_epi32(x, 16); }++INLINE __m512i rot16_512(__m512i x) { return _mm512_ror_epi32(x, 16); }++INLINE __m128i rot12_128(__m128i x) { return _mm_ror_epi32(x, 12); }++INLINE __m256i rot12_256(__m256i x) { return _mm256_ror_epi32(x, 12); }++INLINE __m512i rot12_512(__m512i x) { return _mm512_ror_epi32(x, 12); }++INLINE __m128i rot8_128(__m128i x) { return _mm_ror_epi32(x, 8); }++INLINE __m256i rot8_256(__m256i x) { return _mm256_ror_epi32(x, 8); }++INLINE __m512i rot8_512(__m512i x) { return _mm512_ror_epi32(x, 8); }++INLINE __m128i rot7_128(__m128i x) { return _mm_ror_epi32(x, 7); }++INLINE __m256i rot7_256(__m256i x) { return _mm256_ror_epi32(x, 7); }++INLINE __m512i rot7_512(__m512i x) { return _mm512_ror_epi32(x, 7); }++/*+ * ----------------------------------------------------------------------------+ * compress_avx512+ * ----------------------------------------------------------------------------+ */++INLINE void g1(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,+               __m128i m) {+  *row0 = add_128(add_128(*row0, m), *row1);+  *row3 = xor_128(*row3, *row0);+  *row3 = rot16_128(*row3);+  *row2 = add_128(*row2, *row3);+  *row1 = xor_128(*row1, *row2);+  *row1 = rot12_128(*row1);+}++INLINE void g2(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,+               __m128i m) {+  *row0 = add_128(add_128(*row0, m), *row1);+  *row3 = xor_128(*row3, *row0);+  *row3 = rot8_128(*row3);+  *row2 = add_128(*row2, *row3);+  *row1 = xor_128(*row1, *row2);+  *row1 = rot7_128(*row1);+}++// Note the optimization here of leaving row1 as the unrotated row, rather than+// row0. All the message loads below are adjusted to compensate for this. See+// discussion at https://github.com/sneves/blake2-avx2/pull/4+INLINE void diagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {+  *row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(2, 1, 0, 3));+  *row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));+  *row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(0, 3, 2, 1));+}++INLINE void undiagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {+  *row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(0, 3, 2, 1));+  *row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));+  *row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(2, 1, 0, 3));+}++INLINE void compress_pre(__m128i rows[4], const uint32_t cv[8],+                         const uint8_t block[BLAKE3_BLOCK_LEN],+                         uint8_t block_len, uint64_t counter, uint8_t flags) {+  rows[0] = loadu_128((uint8_t *)&cv[0]);+  rows[1] = loadu_128((uint8_t *)&cv[4]);+  rows[2] = set4(IV[0], IV[1], IV[2], IV[3]);+  rows[3] = set4(counter_low(counter), counter_high(counter),+                 (uint32_t)block_len, (uint32_t)flags);++  __m128i m0 = loadu_128(&block[sizeof(__m128i) * 0]);+  __m128i m1 = loadu_128(&block[sizeof(__m128i) * 1]);+  __m128i m2 = loadu_128(&block[sizeof(__m128i) * 2]);+  __m128i m3 = loadu_128(&block[sizeof(__m128i) * 3]);++  __m128i t0, t1, t2, t3, tt;++  // Round 1. The first round permutes the message words from the original+  // input order, into the groups that get mixed in parallel.+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(2, 0, 2, 0)); //  6  4  2  0+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 3, 1)); //  7  5  3  1+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(2, 0, 2, 0)); // 14 12 10  8+  t2 = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2, 1, 0, 3));   // 12 10  8 14+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 1, 3, 1)); // 15 13 11  9+  t3 = _mm_shuffle_epi32(t3, _MM_SHUFFLE(2, 1, 0, 3));   // 13 11  9 15+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 2. This round and all following rounds apply a fixed permutation+  // to the message words from the round before.+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 3+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 4+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 5+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 6+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 7+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+}++void blake3_compress_xof_avx512(const uint32_t cv[8],+                                const uint8_t block[BLAKE3_BLOCK_LEN],+                                uint8_t block_len, uint64_t counter,+                                uint8_t flags, uint8_t out[64]) {+  __m128i rows[4];+  compress_pre(rows, cv, block, block_len, counter, flags);+  storeu_128(xor_128(rows[0], rows[2]), &out[0]);+  storeu_128(xor_128(rows[1], rows[3]), &out[16]);+  storeu_128(xor_128(rows[2], loadu_128((uint8_t *)&cv[0])), &out[32]);+  storeu_128(xor_128(rows[3], loadu_128((uint8_t *)&cv[4])), &out[48]);+}++void blake3_compress_in_place_avx512(uint32_t cv[8],+                                     const uint8_t block[BLAKE3_BLOCK_LEN],+                                     uint8_t block_len, uint64_t counter,+                                     uint8_t flags) {+  __m128i rows[4];+  compress_pre(rows, cv, block, block_len, counter, flags);+  storeu_128(xor_128(rows[0], rows[2]), (uint8_t *)&cv[0]);+  storeu_128(xor_128(rows[1], rows[3]), (uint8_t *)&cv[4]);+}++/*+ * ----------------------------------------------------------------------------+ * hash4_avx512+ * ----------------------------------------------------------------------------+ */++INLINE void round_fn4(__m128i v[16], __m128i m[16], size_t r) {+  v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);+  v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);+  v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);+  v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);+  v[0] = add_128(v[0], v[4]);+  v[1] = add_128(v[1], v[5]);+  v[2] = add_128(v[2], v[6]);+  v[3] = add_128(v[3], v[7]);+  v[12] = xor_128(v[12], v[0]);+  v[13] = xor_128(v[13], v[1]);+  v[14] = xor_128(v[14], v[2]);+  v[15] = xor_128(v[15], v[3]);+  v[12] = rot16_128(v[12]);+  v[13] = rot16_128(v[13]);+  v[14] = rot16_128(v[14]);+  v[15] = rot16_128(v[15]);+  v[8] = add_128(v[8], v[12]);+  v[9] = add_128(v[9], v[13]);+  v[10] = add_128(v[10], v[14]);+  v[11] = add_128(v[11], v[15]);+  v[4] = xor_128(v[4], v[8]);+  v[5] = xor_128(v[5], v[9]);+  v[6] = xor_128(v[6], v[10]);+  v[7] = xor_128(v[7], v[11]);+  v[4] = rot12_128(v[4]);+  v[5] = rot12_128(v[5]);+  v[6] = rot12_128(v[6]);+  v[7] = rot12_128(v[7]);+  v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);+  v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);+  v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);+  v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);+  v[0] = add_128(v[0], v[4]);+  v[1] = add_128(v[1], v[5]);+  v[2] = add_128(v[2], v[6]);+  v[3] = add_128(v[3], v[7]);+  v[12] = xor_128(v[12], v[0]);+  v[13] = xor_128(v[13], v[1]);+  v[14] = xor_128(v[14], v[2]);+  v[15] = xor_128(v[15], v[3]);+  v[12] = rot8_128(v[12]);+  v[13] = rot8_128(v[13]);+  v[14] = rot8_128(v[14]);+  v[15] = rot8_128(v[15]);+  v[8] = add_128(v[8], v[12]);+  v[9] = add_128(v[9], v[13]);+  v[10] = add_128(v[10], v[14]);+  v[11] = add_128(v[11], v[15]);+  v[4] = xor_128(v[4], v[8]);+  v[5] = xor_128(v[5], v[9]);+  v[6] = xor_128(v[6], v[10]);+  v[7] = xor_128(v[7], v[11]);+  v[4] = rot7_128(v[4]);+  v[5] = rot7_128(v[5]);+  v[6] = rot7_128(v[6]);+  v[7] = rot7_128(v[7]);++  v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);+  v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);+  v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);+  v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);+  v[0] = add_128(v[0], v[5]);+  v[1] = add_128(v[1], v[6]);+  v[2] = add_128(v[2], v[7]);+  v[3] = add_128(v[3], v[4]);+  v[15] = xor_128(v[15], v[0]);+  v[12] = xor_128(v[12], v[1]);+  v[13] = xor_128(v[13], v[2]);+  v[14] = xor_128(v[14], v[3]);+  v[15] = rot16_128(v[15]);+  v[12] = rot16_128(v[12]);+  v[13] = rot16_128(v[13]);+  v[14] = rot16_128(v[14]);+  v[10] = add_128(v[10], v[15]);+  v[11] = add_128(v[11], v[12]);+  v[8] = add_128(v[8], v[13]);+  v[9] = add_128(v[9], v[14]);+  v[5] = xor_128(v[5], v[10]);+  v[6] = xor_128(v[6], v[11]);+  v[7] = xor_128(v[7], v[8]);+  v[4] = xor_128(v[4], v[9]);+  v[5] = rot12_128(v[5]);+  v[6] = rot12_128(v[6]);+  v[7] = rot12_128(v[7]);+  v[4] = rot12_128(v[4]);+  v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);+  v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);+  v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);+  v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);+  v[0] = add_128(v[0], v[5]);+  v[1] = add_128(v[1], v[6]);+  v[2] = add_128(v[2], v[7]);+  v[3] = add_128(v[3], v[4]);+  v[15] = xor_128(v[15], v[0]);+  v[12] = xor_128(v[12], v[1]);+  v[13] = xor_128(v[13], v[2]);+  v[14] = xor_128(v[14], v[3]);+  v[15] = rot8_128(v[15]);+  v[12] = rot8_128(v[12]);+  v[13] = rot8_128(v[13]);+  v[14] = rot8_128(v[14]);+  v[10] = add_128(v[10], v[15]);+  v[11] = add_128(v[11], v[12]);+  v[8] = add_128(v[8], v[13]);+  v[9] = add_128(v[9], v[14]);+  v[5] = xor_128(v[5], v[10]);+  v[6] = xor_128(v[6], v[11]);+  v[7] = xor_128(v[7], v[8]);+  v[4] = xor_128(v[4], v[9]);+  v[5] = rot7_128(v[5]);+  v[6] = rot7_128(v[6]);+  v[7] = rot7_128(v[7]);+  v[4] = rot7_128(v[4]);+}++INLINE void transpose_vecs_128(__m128i vecs[4]) {+  // Interleave 32-bit lanes. The low unpack is lanes 00/11 and the high is+  // 22/33. Note that this doesn't split the vector into two lanes, as the+  // AVX2 counterparts do.+  __m128i ab_01 = _mm_unpacklo_epi32(vecs[0], vecs[1]);+  __m128i ab_23 = _mm_unpackhi_epi32(vecs[0], vecs[1]);+  __m128i cd_01 = _mm_unpacklo_epi32(vecs[2], vecs[3]);+  __m128i cd_23 = _mm_unpackhi_epi32(vecs[2], vecs[3]);++  // Interleave 64-bit lanes.+  __m128i abcd_0 = _mm_unpacklo_epi64(ab_01, cd_01);+  __m128i abcd_1 = _mm_unpackhi_epi64(ab_01, cd_01);+  __m128i abcd_2 = _mm_unpacklo_epi64(ab_23, cd_23);+  __m128i abcd_3 = _mm_unpackhi_epi64(ab_23, cd_23);++  vecs[0] = abcd_0;+  vecs[1] = abcd_1;+  vecs[2] = abcd_2;+  vecs[3] = abcd_3;+}++INLINE void transpose_msg_vecs4(const uint8_t *const *inputs,+                                size_t block_offset, __m128i out[16]) {+  out[0] = loadu_128(&inputs[0][block_offset + 0 * sizeof(__m128i)]);+  out[1] = loadu_128(&inputs[1][block_offset + 0 * sizeof(__m128i)]);+  out[2] = loadu_128(&inputs[2][block_offset + 0 * sizeof(__m128i)]);+  out[3] = loadu_128(&inputs[3][block_offset + 0 * sizeof(__m128i)]);+  out[4] = loadu_128(&inputs[0][block_offset + 1 * sizeof(__m128i)]);+  out[5] = loadu_128(&inputs[1][block_offset + 1 * sizeof(__m128i)]);+  out[6] = loadu_128(&inputs[2][block_offset + 1 * sizeof(__m128i)]);+  out[7] = loadu_128(&inputs[3][block_offset + 1 * sizeof(__m128i)]);+  out[8] = loadu_128(&inputs[0][block_offset + 2 * sizeof(__m128i)]);+  out[9] = loadu_128(&inputs[1][block_offset + 2 * sizeof(__m128i)]);+  out[10] = loadu_128(&inputs[2][block_offset + 2 * sizeof(__m128i)]);+  out[11] = loadu_128(&inputs[3][block_offset + 2 * sizeof(__m128i)]);+  out[12] = loadu_128(&inputs[0][block_offset + 3 * sizeof(__m128i)]);+  out[13] = loadu_128(&inputs[1][block_offset + 3 * sizeof(__m128i)]);+  out[14] = loadu_128(&inputs[2][block_offset + 3 * sizeof(__m128i)]);+  out[15] = loadu_128(&inputs[3][block_offset + 3 * sizeof(__m128i)]);+  for (size_t i = 0; i < 4; ++i) {+    _mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);+  }+  transpose_vecs_128(&out[0]);+  transpose_vecs_128(&out[4]);+  transpose_vecs_128(&out[8]);+  transpose_vecs_128(&out[12]);+}++INLINE void load_counters4(uint64_t counter, bool increment_counter,+                           __m128i *out_lo, __m128i *out_hi) {+  uint64_t mask = (increment_counter ? ~0 : 0);+  __m256i mask_vec = _mm256_set1_epi64x(mask);+  __m256i deltas = _mm256_setr_epi64x(0, 1, 2, 3);+  deltas = _mm256_and_si256(mask_vec, deltas);+  __m256i counters =+      _mm256_add_epi64(_mm256_set1_epi64x((int64_t)counter), deltas);+  *out_lo = _mm256_cvtepi64_epi32(counters);+  *out_hi = _mm256_cvtepi64_epi32(_mm256_srli_epi64(counters, 32));+}++static+void blake3_hash4_avx512(const uint8_t *const *inputs, size_t blocks,+                         const uint32_t key[8], uint64_t counter,+                         bool increment_counter, uint8_t flags,+                         uint8_t flags_start, uint8_t flags_end, uint8_t *out) {+  __m128i h_vecs[8] = {+      set1_128(key[0]), set1_128(key[1]), set1_128(key[2]), set1_128(key[3]),+      set1_128(key[4]), set1_128(key[5]), set1_128(key[6]), set1_128(key[7]),+  };+  __m128i counter_low_vec, counter_high_vec;+  load_counters4(counter, increment_counter, &counter_low_vec,+                 &counter_high_vec);+  uint8_t block_flags = flags | flags_start;++  for (size_t block = 0; block < blocks; block++) {+    if (block + 1 == blocks) {+      block_flags |= flags_end;+    }+    __m128i block_len_vec = set1_128(BLAKE3_BLOCK_LEN);+    __m128i block_flags_vec = set1_128(block_flags);+    __m128i msg_vecs[16];+    transpose_msg_vecs4(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);++    __m128i v[16] = {+        h_vecs[0],       h_vecs[1],        h_vecs[2],       h_vecs[3],+        h_vecs[4],       h_vecs[5],        h_vecs[6],       h_vecs[7],+        set1_128(IV[0]), set1_128(IV[1]),  set1_128(IV[2]), set1_128(IV[3]),+        counter_low_vec, counter_high_vec, block_len_vec,   block_flags_vec,+    };+    round_fn4(v, msg_vecs, 0);+    round_fn4(v, msg_vecs, 1);+    round_fn4(v, msg_vecs, 2);+    round_fn4(v, msg_vecs, 3);+    round_fn4(v, msg_vecs, 4);+    round_fn4(v, msg_vecs, 5);+    round_fn4(v, msg_vecs, 6);+    h_vecs[0] = xor_128(v[0], v[8]);+    h_vecs[1] = xor_128(v[1], v[9]);+    h_vecs[2] = xor_128(v[2], v[10]);+    h_vecs[3] = xor_128(v[3], v[11]);+    h_vecs[4] = xor_128(v[4], v[12]);+    h_vecs[5] = xor_128(v[5], v[13]);+    h_vecs[6] = xor_128(v[6], v[14]);+    h_vecs[7] = xor_128(v[7], v[15]);++    block_flags = flags;+  }++  transpose_vecs_128(&h_vecs[0]);+  transpose_vecs_128(&h_vecs[4]);+  // The first four vecs now contain the first half of each output, and the+  // second four vecs contain the second half of each output.+  storeu_128(h_vecs[0], &out[0 * sizeof(__m128i)]);+  storeu_128(h_vecs[4], &out[1 * sizeof(__m128i)]);+  storeu_128(h_vecs[1], &out[2 * sizeof(__m128i)]);+  storeu_128(h_vecs[5], &out[3 * sizeof(__m128i)]);+  storeu_128(h_vecs[2], &out[4 * sizeof(__m128i)]);+  storeu_128(h_vecs[6], &out[5 * sizeof(__m128i)]);+  storeu_128(h_vecs[3], &out[6 * sizeof(__m128i)]);+  storeu_128(h_vecs[7], &out[7 * sizeof(__m128i)]);+}++static+void blake3_xof4_avx512(const uint32_t cv[8],+                        const uint8_t block[BLAKE3_BLOCK_LEN],+                        uint8_t block_len, uint64_t counter, uint8_t flags,+                        uint8_t out[4 * 64]) {+  __m128i h_vecs[8] = {+      set1_128(cv[0]), set1_128(cv[1]), set1_128(cv[2]), set1_128(cv[3]),+      set1_128(cv[4]), set1_128(cv[5]), set1_128(cv[6]), set1_128(cv[7]),+  };+  uint32_t block_words[16];+  load_block_words(block, block_words);+  __m128i msg_vecs[16];+  for (size_t i = 0; i < 16; i++) {+      msg_vecs[i] = set1_128(block_words[i]);+  }+  __m128i counter_low_vec, counter_high_vec;+  load_counters4(counter, true, &counter_low_vec, &counter_high_vec);+  __m128i block_len_vec = set1_128(block_len);+  __m128i block_flags_vec = set1_128(flags);+  __m128i v[16] = {+      h_vecs[0],       h_vecs[1],        h_vecs[2],       h_vecs[3],+      h_vecs[4],       h_vecs[5],        h_vecs[6],       h_vecs[7],+      set1_128(IV[0]), set1_128(IV[1]),  set1_128(IV[2]), set1_128(IV[3]),+      counter_low_vec, counter_high_vec, block_len_vec,   block_flags_vec,+  };+  round_fn4(v, msg_vecs, 0);+  round_fn4(v, msg_vecs, 1);+  round_fn4(v, msg_vecs, 2);+  round_fn4(v, msg_vecs, 3);+  round_fn4(v, msg_vecs, 4);+  round_fn4(v, msg_vecs, 5);+  round_fn4(v, msg_vecs, 6);+  for (size_t i = 0; i < 8; i++) {+      v[i] = xor_128(v[i], v[i+8]);+      v[i+8] = xor_128(v[i+8], h_vecs[i]);+  }+  transpose_vecs_128(&v[0]);+  transpose_vecs_128(&v[4]);+  transpose_vecs_128(&v[8]);+  transpose_vecs_128(&v[12]);+  for (size_t i = 0; i < 4; i++) {+      storeu_128(v[i+ 0], &out[(4*i+0) * sizeof(__m128i)]);+      storeu_128(v[i+ 4], &out[(4*i+1) * sizeof(__m128i)]);+      storeu_128(v[i+ 8], &out[(4*i+2) * sizeof(__m128i)]);+      storeu_128(v[i+12], &out[(4*i+3) * sizeof(__m128i)]);+  }+}++/*+ * ----------------------------------------------------------------------------+ * hash8_avx512+ * ----------------------------------------------------------------------------+ */++INLINE void round_fn8(__m256i v[16], __m256i m[16], size_t r) {+  v[0] = add_256(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);+  v[1] = add_256(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);+  v[2] = add_256(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);+  v[3] = add_256(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);+  v[0] = add_256(v[0], v[4]);+  v[1] = add_256(v[1], v[5]);+  v[2] = add_256(v[2], v[6]);+  v[3] = add_256(v[3], v[7]);+  v[12] = xor_256(v[12], v[0]);+  v[13] = xor_256(v[13], v[1]);+  v[14] = xor_256(v[14], v[2]);+  v[15] = xor_256(v[15], v[3]);+  v[12] = rot16_256(v[12]);+  v[13] = rot16_256(v[13]);+  v[14] = rot16_256(v[14]);+  v[15] = rot16_256(v[15]);+  v[8] = add_256(v[8], v[12]);+  v[9] = add_256(v[9], v[13]);+  v[10] = add_256(v[10], v[14]);+  v[11] = add_256(v[11], v[15]);+  v[4] = xor_256(v[4], v[8]);+  v[5] = xor_256(v[5], v[9]);+  v[6] = xor_256(v[6], v[10]);+  v[7] = xor_256(v[7], v[11]);+  v[4] = rot12_256(v[4]);+  v[5] = rot12_256(v[5]);+  v[6] = rot12_256(v[6]);+  v[7] = rot12_256(v[7]);+  v[0] = add_256(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);+  v[1] = add_256(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);+  v[2] = add_256(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);+  v[3] = add_256(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);+  v[0] = add_256(v[0], v[4]);+  v[1] = add_256(v[1], v[5]);+  v[2] = add_256(v[2], v[6]);+  v[3] = add_256(v[3], v[7]);+  v[12] = xor_256(v[12], v[0]);+  v[13] = xor_256(v[13], v[1]);+  v[14] = xor_256(v[14], v[2]);+  v[15] = xor_256(v[15], v[3]);+  v[12] = rot8_256(v[12]);+  v[13] = rot8_256(v[13]);+  v[14] = rot8_256(v[14]);+  v[15] = rot8_256(v[15]);+  v[8] = add_256(v[8], v[12]);+  v[9] = add_256(v[9], v[13]);+  v[10] = add_256(v[10], v[14]);+  v[11] = add_256(v[11], v[15]);+  v[4] = xor_256(v[4], v[8]);+  v[5] = xor_256(v[5], v[9]);+  v[6] = xor_256(v[6], v[10]);+  v[7] = xor_256(v[7], v[11]);+  v[4] = rot7_256(v[4]);+  v[5] = rot7_256(v[5]);+  v[6] = rot7_256(v[6]);+  v[7] = rot7_256(v[7]);++  v[0] = add_256(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);+  v[1] = add_256(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);+  v[2] = add_256(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);+  v[3] = add_256(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);+  v[0] = add_256(v[0], v[5]);+  v[1] = add_256(v[1], v[6]);+  v[2] = add_256(v[2], v[7]);+  v[3] = add_256(v[3], v[4]);+  v[15] = xor_256(v[15], v[0]);+  v[12] = xor_256(v[12], v[1]);+  v[13] = xor_256(v[13], v[2]);+  v[14] = xor_256(v[14], v[3]);+  v[15] = rot16_256(v[15]);+  v[12] = rot16_256(v[12]);+  v[13] = rot16_256(v[13]);+  v[14] = rot16_256(v[14]);+  v[10] = add_256(v[10], v[15]);+  v[11] = add_256(v[11], v[12]);+  v[8] = add_256(v[8], v[13]);+  v[9] = add_256(v[9], v[14]);+  v[5] = xor_256(v[5], v[10]);+  v[6] = xor_256(v[6], v[11]);+  v[7] = xor_256(v[7], v[8]);+  v[4] = xor_256(v[4], v[9]);+  v[5] = rot12_256(v[5]);+  v[6] = rot12_256(v[6]);+  v[7] = rot12_256(v[7]);+  v[4] = rot12_256(v[4]);+  v[0] = add_256(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);+  v[1] = add_256(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);+  v[2] = add_256(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);+  v[3] = add_256(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);+  v[0] = add_256(v[0], v[5]);+  v[1] = add_256(v[1], v[6]);+  v[2] = add_256(v[2], v[7]);+  v[3] = add_256(v[3], v[4]);+  v[15] = xor_256(v[15], v[0]);+  v[12] = xor_256(v[12], v[1]);+  v[13] = xor_256(v[13], v[2]);+  v[14] = xor_256(v[14], v[3]);+  v[15] = rot8_256(v[15]);+  v[12] = rot8_256(v[12]);+  v[13] = rot8_256(v[13]);+  v[14] = rot8_256(v[14]);+  v[10] = add_256(v[10], v[15]);+  v[11] = add_256(v[11], v[12]);+  v[8] = add_256(v[8], v[13]);+  v[9] = add_256(v[9], v[14]);+  v[5] = xor_256(v[5], v[10]);+  v[6] = xor_256(v[6], v[11]);+  v[7] = xor_256(v[7], v[8]);+  v[4] = xor_256(v[4], v[9]);+  v[5] = rot7_256(v[5]);+  v[6] = rot7_256(v[6]);+  v[7] = rot7_256(v[7]);+  v[4] = rot7_256(v[4]);+}++INLINE void transpose_vecs_256(__m256i vecs[8]) {+  // Interleave 32-bit lanes. The low unpack is lanes 00/11/44/55, and the high+  // is 22/33/66/77.+  __m256i ab_0145 = _mm256_unpacklo_epi32(vecs[0], vecs[1]);+  __m256i ab_2367 = _mm256_unpackhi_epi32(vecs[0], vecs[1]);+  __m256i cd_0145 = _mm256_unpacklo_epi32(vecs[2], vecs[3]);+  __m256i cd_2367 = _mm256_unpackhi_epi32(vecs[2], vecs[3]);+  __m256i ef_0145 = _mm256_unpacklo_epi32(vecs[4], vecs[5]);+  __m256i ef_2367 = _mm256_unpackhi_epi32(vecs[4], vecs[5]);+  __m256i gh_0145 = _mm256_unpacklo_epi32(vecs[6], vecs[7]);+  __m256i gh_2367 = _mm256_unpackhi_epi32(vecs[6], vecs[7]);++  // Interleave 64-bit lanes. The low unpack is lanes 00/22 and the high is+  // 11/33.+  __m256i abcd_04 = _mm256_unpacklo_epi64(ab_0145, cd_0145);+  __m256i abcd_15 = _mm256_unpackhi_epi64(ab_0145, cd_0145);+  __m256i abcd_26 = _mm256_unpacklo_epi64(ab_2367, cd_2367);+  __m256i abcd_37 = _mm256_unpackhi_epi64(ab_2367, cd_2367);+  __m256i efgh_04 = _mm256_unpacklo_epi64(ef_0145, gh_0145);+  __m256i efgh_15 = _mm256_unpackhi_epi64(ef_0145, gh_0145);+  __m256i efgh_26 = _mm256_unpacklo_epi64(ef_2367, gh_2367);+  __m256i efgh_37 = _mm256_unpackhi_epi64(ef_2367, gh_2367);++  // Interleave 128-bit lanes.+  vecs[0] = _mm256_permute2x128_si256(abcd_04, efgh_04, 0x20);+  vecs[1] = _mm256_permute2x128_si256(abcd_15, efgh_15, 0x20);+  vecs[2] = _mm256_permute2x128_si256(abcd_26, efgh_26, 0x20);+  vecs[3] = _mm256_permute2x128_si256(abcd_37, efgh_37, 0x20);+  vecs[4] = _mm256_permute2x128_si256(abcd_04, efgh_04, 0x31);+  vecs[5] = _mm256_permute2x128_si256(abcd_15, efgh_15, 0x31);+  vecs[6] = _mm256_permute2x128_si256(abcd_26, efgh_26, 0x31);+  vecs[7] = _mm256_permute2x128_si256(abcd_37, efgh_37, 0x31);+}++INLINE void transpose_msg_vecs8(const uint8_t *const *inputs,+                                size_t block_offset, __m256i out[16]) {+  out[0] = loadu_256(&inputs[0][block_offset + 0 * sizeof(__m256i)]);+  out[1] = loadu_256(&inputs[1][block_offset + 0 * sizeof(__m256i)]);+  out[2] = loadu_256(&inputs[2][block_offset + 0 * sizeof(__m256i)]);+  out[3] = loadu_256(&inputs[3][block_offset + 0 * sizeof(__m256i)]);+  out[4] = loadu_256(&inputs[4][block_offset + 0 * sizeof(__m256i)]);+  out[5] = loadu_256(&inputs[5][block_offset + 0 * sizeof(__m256i)]);+  out[6] = loadu_256(&inputs[6][block_offset + 0 * sizeof(__m256i)]);+  out[7] = loadu_256(&inputs[7][block_offset + 0 * sizeof(__m256i)]);+  out[8] = loadu_256(&inputs[0][block_offset + 1 * sizeof(__m256i)]);+  out[9] = loadu_256(&inputs[1][block_offset + 1 * sizeof(__m256i)]);+  out[10] = loadu_256(&inputs[2][block_offset + 1 * sizeof(__m256i)]);+  out[11] = loadu_256(&inputs[3][block_offset + 1 * sizeof(__m256i)]);+  out[12] = loadu_256(&inputs[4][block_offset + 1 * sizeof(__m256i)]);+  out[13] = loadu_256(&inputs[5][block_offset + 1 * sizeof(__m256i)]);+  out[14] = loadu_256(&inputs[6][block_offset + 1 * sizeof(__m256i)]);+  out[15] = loadu_256(&inputs[7][block_offset + 1 * sizeof(__m256i)]);+  for (size_t i = 0; i < 8; ++i) {+    _mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);+  }+  transpose_vecs_256(&out[0]);+  transpose_vecs_256(&out[8]);+}++INLINE void load_counters8(uint64_t counter, bool increment_counter,+                           __m256i *out_lo, __m256i *out_hi) {+  uint64_t mask = (increment_counter ? ~0 : 0);+  __m512i mask_vec = _mm512_set1_epi64(mask);+  __m512i deltas = _mm512_setr_epi64(0, 1, 2, 3, 4, 5, 6, 7);+  deltas = _mm512_and_si512(mask_vec, deltas);+  __m512i counters =+      _mm512_add_epi64(_mm512_set1_epi64((int64_t)counter), deltas);+  *out_lo = _mm512_cvtepi64_epi32(counters);+  *out_hi = _mm512_cvtepi64_epi32(_mm512_srli_epi64(counters, 32));+}++static+void blake3_hash8_avx512(const uint8_t *const *inputs, size_t blocks,+                         const uint32_t key[8], uint64_t counter,+                         bool increment_counter, uint8_t flags,+                         uint8_t flags_start, uint8_t flags_end, uint8_t *out) {+  __m256i h_vecs[8] = {+      set1_256(key[0]), set1_256(key[1]), set1_256(key[2]), set1_256(key[3]),+      set1_256(key[4]), set1_256(key[5]), set1_256(key[6]), set1_256(key[7]),+  };+  __m256i counter_low_vec, counter_high_vec;+  load_counters8(counter, increment_counter, &counter_low_vec,+                 &counter_high_vec);+  uint8_t block_flags = flags | flags_start;++  for (size_t block = 0; block < blocks; block++) {+    if (block + 1 == blocks) {+      block_flags |= flags_end;+    }+    __m256i block_len_vec = set1_256(BLAKE3_BLOCK_LEN);+    __m256i block_flags_vec = set1_256(block_flags);+    __m256i msg_vecs[16];+    transpose_msg_vecs8(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);++    __m256i v[16] = {+        h_vecs[0],       h_vecs[1],        h_vecs[2],       h_vecs[3],+        h_vecs[4],       h_vecs[5],        h_vecs[6],       h_vecs[7],+        set1_256(IV[0]), set1_256(IV[1]),  set1_256(IV[2]), set1_256(IV[3]),+        counter_low_vec, counter_high_vec, block_len_vec,   block_flags_vec,+    };+    round_fn8(v, msg_vecs, 0);+    round_fn8(v, msg_vecs, 1);+    round_fn8(v, msg_vecs, 2);+    round_fn8(v, msg_vecs, 3);+    round_fn8(v, msg_vecs, 4);+    round_fn8(v, msg_vecs, 5);+    round_fn8(v, msg_vecs, 6);+    h_vecs[0] = xor_256(v[0], v[8]);+    h_vecs[1] = xor_256(v[1], v[9]);+    h_vecs[2] = xor_256(v[2], v[10]);+    h_vecs[3] = xor_256(v[3], v[11]);+    h_vecs[4] = xor_256(v[4], v[12]);+    h_vecs[5] = xor_256(v[5], v[13]);+    h_vecs[6] = xor_256(v[6], v[14]);+    h_vecs[7] = xor_256(v[7], v[15]);++    block_flags = flags;+  }++  transpose_vecs_256(h_vecs);+  storeu_256(h_vecs[0], &out[0 * sizeof(__m256i)]);+  storeu_256(h_vecs[1], &out[1 * sizeof(__m256i)]);+  storeu_256(h_vecs[2], &out[2 * sizeof(__m256i)]);+  storeu_256(h_vecs[3], &out[3 * sizeof(__m256i)]);+  storeu_256(h_vecs[4], &out[4 * sizeof(__m256i)]);+  storeu_256(h_vecs[5], &out[5 * sizeof(__m256i)]);+  storeu_256(h_vecs[6], &out[6 * sizeof(__m256i)]);+  storeu_256(h_vecs[7], &out[7 * sizeof(__m256i)]);+}++static+void blake3_xof8_avx512(const uint32_t cv[8],+                        const uint8_t block[BLAKE3_BLOCK_LEN],+                        uint8_t block_len, uint64_t counter, uint8_t flags,+                        uint8_t out[8 * 64]) {+  __m256i h_vecs[8] = {+      set1_256(cv[0]), set1_256(cv[1]), set1_256(cv[2]), set1_256(cv[3]),+      set1_256(cv[4]), set1_256(cv[5]), set1_256(cv[6]), set1_256(cv[7]),+  };+  uint32_t block_words[16];+  load_block_words(block, block_words);+  __m256i msg_vecs[16];+  for (size_t i = 0; i < 16; i++) {+      msg_vecs[i] = set1_256(block_words[i]);+  }+  __m256i counter_low_vec, counter_high_vec;+  load_counters8(counter, true, &counter_low_vec, &counter_high_vec);+  __m256i block_len_vec = set1_256(block_len);+  __m256i block_flags_vec = set1_256(flags);+  __m256i v[16] = {+      h_vecs[0],       h_vecs[1],        h_vecs[2],       h_vecs[3],+      h_vecs[4],       h_vecs[5],        h_vecs[6],       h_vecs[7],+      set1_256(IV[0]), set1_256(IV[1]),  set1_256(IV[2]), set1_256(IV[3]),+      counter_low_vec, counter_high_vec, block_len_vec,   block_flags_vec,+  };+  round_fn8(v, msg_vecs, 0);+  round_fn8(v, msg_vecs, 1);+  round_fn8(v, msg_vecs, 2);+  round_fn8(v, msg_vecs, 3);+  round_fn8(v, msg_vecs, 4);+  round_fn8(v, msg_vecs, 5);+  round_fn8(v, msg_vecs, 6);+  for (size_t i = 0; i < 8; i++) {+      v[i] = xor_256(v[i], v[i+8]);+      v[i+8] = xor_256(v[i+8], h_vecs[i]);+  }+  transpose_vecs_256(&v[0]);+  transpose_vecs_256(&v[8]);+  for (size_t i = 0; i < 8; i++) {+      storeu_256(v[i+0], &out[(2*i+0) * sizeof(__m256i)]);+      storeu_256(v[i+8], &out[(2*i+1) * sizeof(__m256i)]);+  }+}++/*+ * ----------------------------------------------------------------------------+ * hash16_avx512+ * ----------------------------------------------------------------------------+ */++INLINE void round_fn16(__m512i v[16], __m512i m[16], size_t r) {+  v[0] = add_512(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);+  v[1] = add_512(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);+  v[2] = add_512(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);+  v[3] = add_512(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);+  v[0] = add_512(v[0], v[4]);+  v[1] = add_512(v[1], v[5]);+  v[2] = add_512(v[2], v[6]);+  v[3] = add_512(v[3], v[7]);+  v[12] = xor_512(v[12], v[0]);+  v[13] = xor_512(v[13], v[1]);+  v[14] = xor_512(v[14], v[2]);+  v[15] = xor_512(v[15], v[3]);+  v[12] = rot16_512(v[12]);+  v[13] = rot16_512(v[13]);+  v[14] = rot16_512(v[14]);+  v[15] = rot16_512(v[15]);+  v[8] = add_512(v[8], v[12]);+  v[9] = add_512(v[9], v[13]);+  v[10] = add_512(v[10], v[14]);+  v[11] = add_512(v[11], v[15]);+  v[4] = xor_512(v[4], v[8]);+  v[5] = xor_512(v[5], v[9]);+  v[6] = xor_512(v[6], v[10]);+  v[7] = xor_512(v[7], v[11]);+  v[4] = rot12_512(v[4]);+  v[5] = rot12_512(v[5]);+  v[6] = rot12_512(v[6]);+  v[7] = rot12_512(v[7]);+  v[0] = add_512(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);+  v[1] = add_512(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);+  v[2] = add_512(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);+  v[3] = add_512(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);+  v[0] = add_512(v[0], v[4]);+  v[1] = add_512(v[1], v[5]);+  v[2] = add_512(v[2], v[6]);+  v[3] = add_512(v[3], v[7]);+  v[12] = xor_512(v[12], v[0]);+  v[13] = xor_512(v[13], v[1]);+  v[14] = xor_512(v[14], v[2]);+  v[15] = xor_512(v[15], v[3]);+  v[12] = rot8_512(v[12]);+  v[13] = rot8_512(v[13]);+  v[14] = rot8_512(v[14]);+  v[15] = rot8_512(v[15]);+  v[8] = add_512(v[8], v[12]);+  v[9] = add_512(v[9], v[13]);+  v[10] = add_512(v[10], v[14]);+  v[11] = add_512(v[11], v[15]);+  v[4] = xor_512(v[4], v[8]);+  v[5] = xor_512(v[5], v[9]);+  v[6] = xor_512(v[6], v[10]);+  v[7] = xor_512(v[7], v[11]);+  v[4] = rot7_512(v[4]);+  v[5] = rot7_512(v[5]);+  v[6] = rot7_512(v[6]);+  v[7] = rot7_512(v[7]);++  v[0] = add_512(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);+  v[1] = add_512(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);+  v[2] = add_512(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);+  v[3] = add_512(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);+  v[0] = add_512(v[0], v[5]);+  v[1] = add_512(v[1], v[6]);+  v[2] = add_512(v[2], v[7]);+  v[3] = add_512(v[3], v[4]);+  v[15] = xor_512(v[15], v[0]);+  v[12] = xor_512(v[12], v[1]);+  v[13] = xor_512(v[13], v[2]);+  v[14] = xor_512(v[14], v[3]);+  v[15] = rot16_512(v[15]);+  v[12] = rot16_512(v[12]);+  v[13] = rot16_512(v[13]);+  v[14] = rot16_512(v[14]);+  v[10] = add_512(v[10], v[15]);+  v[11] = add_512(v[11], v[12]);+  v[8] = add_512(v[8], v[13]);+  v[9] = add_512(v[9], v[14]);+  v[5] = xor_512(v[5], v[10]);+  v[6] = xor_512(v[6], v[11]);+  v[7] = xor_512(v[7], v[8]);+  v[4] = xor_512(v[4], v[9]);+  v[5] = rot12_512(v[5]);+  v[6] = rot12_512(v[6]);+  v[7] = rot12_512(v[7]);+  v[4] = rot12_512(v[4]);+  v[0] = add_512(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);+  v[1] = add_512(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);+  v[2] = add_512(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);+  v[3] = add_512(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);+  v[0] = add_512(v[0], v[5]);+  v[1] = add_512(v[1], v[6]);+  v[2] = add_512(v[2], v[7]);+  v[3] = add_512(v[3], v[4]);+  v[15] = xor_512(v[15], v[0]);+  v[12] = xor_512(v[12], v[1]);+  v[13] = xor_512(v[13], v[2]);+  v[14] = xor_512(v[14], v[3]);+  v[15] = rot8_512(v[15]);+  v[12] = rot8_512(v[12]);+  v[13] = rot8_512(v[13]);+  v[14] = rot8_512(v[14]);+  v[10] = add_512(v[10], v[15]);+  v[11] = add_512(v[11], v[12]);+  v[8] = add_512(v[8], v[13]);+  v[9] = add_512(v[9], v[14]);+  v[5] = xor_512(v[5], v[10]);+  v[6] = xor_512(v[6], v[11]);+  v[7] = xor_512(v[7], v[8]);+  v[4] = xor_512(v[4], v[9]);+  v[5] = rot7_512(v[5]);+  v[6] = rot7_512(v[6]);+  v[7] = rot7_512(v[7]);+  v[4] = rot7_512(v[4]);+}++// 0b10001000, or lanes a0/a2/b0/b2 in little-endian order+#define LO_IMM8 0x88++INLINE __m512i unpack_lo_128(__m512i a, __m512i b) {+  return _mm512_shuffle_i32x4(a, b, LO_IMM8);+}++// 0b11011101, or lanes a1/a3/b1/b3 in little-endian order+#define HI_IMM8 0xdd++INLINE __m512i unpack_hi_128(__m512i a, __m512i b) {+  return _mm512_shuffle_i32x4(a, b, HI_IMM8);+}++INLINE void transpose_vecs_512(__m512i vecs[16]) {+  // Interleave 32-bit lanes. The _0 unpack is lanes+  // 0/0/1/1/4/4/5/5/8/8/9/9/12/12/13/13, and the _2 unpack is lanes+  // 2/2/3/3/6/6/7/7/10/10/11/11/14/14/15/15.+  __m512i ab_0 = _mm512_unpacklo_epi32(vecs[0], vecs[1]);+  __m512i ab_2 = _mm512_unpackhi_epi32(vecs[0], vecs[1]);+  __m512i cd_0 = _mm512_unpacklo_epi32(vecs[2], vecs[3]);+  __m512i cd_2 = _mm512_unpackhi_epi32(vecs[2], vecs[3]);+  __m512i ef_0 = _mm512_unpacklo_epi32(vecs[4], vecs[5]);+  __m512i ef_2 = _mm512_unpackhi_epi32(vecs[4], vecs[5]);+  __m512i gh_0 = _mm512_unpacklo_epi32(vecs[6], vecs[7]);+  __m512i gh_2 = _mm512_unpackhi_epi32(vecs[6], vecs[7]);+  __m512i ij_0 = _mm512_unpacklo_epi32(vecs[8], vecs[9]);+  __m512i ij_2 = _mm512_unpackhi_epi32(vecs[8], vecs[9]);+  __m512i kl_0 = _mm512_unpacklo_epi32(vecs[10], vecs[11]);+  __m512i kl_2 = _mm512_unpackhi_epi32(vecs[10], vecs[11]);+  __m512i mn_0 = _mm512_unpacklo_epi32(vecs[12], vecs[13]);+  __m512i mn_2 = _mm512_unpackhi_epi32(vecs[12], vecs[13]);+  __m512i op_0 = _mm512_unpacklo_epi32(vecs[14], vecs[15]);+  __m512i op_2 = _mm512_unpackhi_epi32(vecs[14], vecs[15]);++  // Interleave 64-bit lanes. The _0 unpack is lanes+  // 0/0/0/0/4/4/4/4/8/8/8/8/12/12/12/12, the _1 unpack is lanes+  // 1/1/1/1/5/5/5/5/9/9/9/9/13/13/13/13, the _2 unpack is lanes+  // 2/2/2/2/6/6/6/6/10/10/10/10/14/14/14/14, and the _3 unpack is lanes+  // 3/3/3/3/7/7/7/7/11/11/11/11/15/15/15/15.+  __m512i abcd_0 = _mm512_unpacklo_epi64(ab_0, cd_0);+  __m512i abcd_1 = _mm512_unpackhi_epi64(ab_0, cd_0);+  __m512i abcd_2 = _mm512_unpacklo_epi64(ab_2, cd_2);+  __m512i abcd_3 = _mm512_unpackhi_epi64(ab_2, cd_2);+  __m512i efgh_0 = _mm512_unpacklo_epi64(ef_0, gh_0);+  __m512i efgh_1 = _mm512_unpackhi_epi64(ef_0, gh_0);+  __m512i efgh_2 = _mm512_unpacklo_epi64(ef_2, gh_2);+  __m512i efgh_3 = _mm512_unpackhi_epi64(ef_2, gh_2);+  __m512i ijkl_0 = _mm512_unpacklo_epi64(ij_0, kl_0);+  __m512i ijkl_1 = _mm512_unpackhi_epi64(ij_0, kl_0);+  __m512i ijkl_2 = _mm512_unpacklo_epi64(ij_2, kl_2);+  __m512i ijkl_3 = _mm512_unpackhi_epi64(ij_2, kl_2);+  __m512i mnop_0 = _mm512_unpacklo_epi64(mn_0, op_0);+  __m512i mnop_1 = _mm512_unpackhi_epi64(mn_0, op_0);+  __m512i mnop_2 = _mm512_unpacklo_epi64(mn_2, op_2);+  __m512i mnop_3 = _mm512_unpackhi_epi64(mn_2, op_2);++  // Interleave 128-bit lanes. The _0 unpack is+  // 0/0/0/0/8/8/8/8/0/0/0/0/8/8/8/8, the _1 unpack is+  // 1/1/1/1/9/9/9/9/1/1/1/1/9/9/9/9, and so on.+  __m512i abcdefgh_0 = unpack_lo_128(abcd_0, efgh_0);+  __m512i abcdefgh_1 = unpack_lo_128(abcd_1, efgh_1);+  __m512i abcdefgh_2 = unpack_lo_128(abcd_2, efgh_2);+  __m512i abcdefgh_3 = unpack_lo_128(abcd_3, efgh_3);+  __m512i abcdefgh_4 = unpack_hi_128(abcd_0, efgh_0);+  __m512i abcdefgh_5 = unpack_hi_128(abcd_1, efgh_1);+  __m512i abcdefgh_6 = unpack_hi_128(abcd_2, efgh_2);+  __m512i abcdefgh_7 = unpack_hi_128(abcd_3, efgh_3);+  __m512i ijklmnop_0 = unpack_lo_128(ijkl_0, mnop_0);+  __m512i ijklmnop_1 = unpack_lo_128(ijkl_1, mnop_1);+  __m512i ijklmnop_2 = unpack_lo_128(ijkl_2, mnop_2);+  __m512i ijklmnop_3 = unpack_lo_128(ijkl_3, mnop_3);+  __m512i ijklmnop_4 = unpack_hi_128(ijkl_0, mnop_0);+  __m512i ijklmnop_5 = unpack_hi_128(ijkl_1, mnop_1);+  __m512i ijklmnop_6 = unpack_hi_128(ijkl_2, mnop_2);+  __m512i ijklmnop_7 = unpack_hi_128(ijkl_3, mnop_3);++  // Interleave 128-bit lanes again for the final outputs.+  vecs[0] = unpack_lo_128(abcdefgh_0, ijklmnop_0);+  vecs[1] = unpack_lo_128(abcdefgh_1, ijklmnop_1);+  vecs[2] = unpack_lo_128(abcdefgh_2, ijklmnop_2);+  vecs[3] = unpack_lo_128(abcdefgh_3, ijklmnop_3);+  vecs[4] = unpack_lo_128(abcdefgh_4, ijklmnop_4);+  vecs[5] = unpack_lo_128(abcdefgh_5, ijklmnop_5);+  vecs[6] = unpack_lo_128(abcdefgh_6, ijklmnop_6);+  vecs[7] = unpack_lo_128(abcdefgh_7, ijklmnop_7);+  vecs[8] = unpack_hi_128(abcdefgh_0, ijklmnop_0);+  vecs[9] = unpack_hi_128(abcdefgh_1, ijklmnop_1);+  vecs[10] = unpack_hi_128(abcdefgh_2, ijklmnop_2);+  vecs[11] = unpack_hi_128(abcdefgh_3, ijklmnop_3);+  vecs[12] = unpack_hi_128(abcdefgh_4, ijklmnop_4);+  vecs[13] = unpack_hi_128(abcdefgh_5, ijklmnop_5);+  vecs[14] = unpack_hi_128(abcdefgh_6, ijklmnop_6);+  vecs[15] = unpack_hi_128(abcdefgh_7, ijklmnop_7);+}++INLINE void transpose_msg_vecs16(const uint8_t *const *inputs,+                                 size_t block_offset, __m512i out[16]) {+  out[0] = loadu_512(&inputs[0][block_offset]);+  out[1] = loadu_512(&inputs[1][block_offset]);+  out[2] = loadu_512(&inputs[2][block_offset]);+  out[3] = loadu_512(&inputs[3][block_offset]);+  out[4] = loadu_512(&inputs[4][block_offset]);+  out[5] = loadu_512(&inputs[5][block_offset]);+  out[6] = loadu_512(&inputs[6][block_offset]);+  out[7] = loadu_512(&inputs[7][block_offset]);+  out[8] = loadu_512(&inputs[8][block_offset]);+  out[9] = loadu_512(&inputs[9][block_offset]);+  out[10] = loadu_512(&inputs[10][block_offset]);+  out[11] = loadu_512(&inputs[11][block_offset]);+  out[12] = loadu_512(&inputs[12][block_offset]);+  out[13] = loadu_512(&inputs[13][block_offset]);+  out[14] = loadu_512(&inputs[14][block_offset]);+  out[15] = loadu_512(&inputs[15][block_offset]);+  for (size_t i = 0; i < 16; ++i) {+    _mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);+  }+  transpose_vecs_512(out);+}++INLINE void load_counters16(uint64_t counter, bool increment_counter,+                            __m512i *out_lo, __m512i *out_hi) {+  const __m512i mask = _mm512_set1_epi32(-(int32_t)increment_counter);+  const __m512i deltas = _mm512_set_epi32(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);+  const __m512i masked_deltas = _mm512_and_si512(deltas, mask);+  const __m512i low_words = _mm512_add_epi32(+    _mm512_set1_epi32((int32_t)counter),+    masked_deltas);+  // The carry bit is 1 if the high bit of the word was 1 before addition and is+  // 0 after.+  // NOTE: It would be a bit more natural to use _mm512_cmp_epu32_mask to+  // compute the carry bits here, and originally we did, but that intrinsic is+  // broken under GCC 5.4. See https://github.com/BLAKE3-team/BLAKE3/issues/271.+  const __m512i carries = _mm512_srli_epi32(+    _mm512_andnot_si512(+        low_words, // 0 after (gets inverted by andnot)+        _mm512_set1_epi32((int32_t)counter)), // and 1 before+    31);+  const __m512i high_words = _mm512_add_epi32(+    _mm512_set1_epi32((int32_t)(counter >> 32)),+    carries);+  *out_lo = low_words;+  *out_hi = high_words;+}++static+void blake3_hash16_avx512(const uint8_t *const *inputs, size_t blocks,+                          const uint32_t key[8], uint64_t counter,+                          bool increment_counter, uint8_t flags,+                          uint8_t flags_start, uint8_t flags_end,+                          uint8_t *out) {+  __m512i h_vecs[8] = {+      set1_512(key[0]), set1_512(key[1]), set1_512(key[2]), set1_512(key[3]),+      set1_512(key[4]), set1_512(key[5]), set1_512(key[6]), set1_512(key[7]),+  };+  __m512i counter_low_vec, counter_high_vec;+  load_counters16(counter, increment_counter, &counter_low_vec,+                  &counter_high_vec);+  uint8_t block_flags = flags | flags_start;++  for (size_t block = 0; block < blocks; block++) {+    if (block + 1 == blocks) {+      block_flags |= flags_end;+    }+    __m512i block_len_vec = set1_512(BLAKE3_BLOCK_LEN);+    __m512i block_flags_vec = set1_512(block_flags);+    __m512i msg_vecs[16];+    transpose_msg_vecs16(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);++    __m512i v[16] = {+        h_vecs[0],       h_vecs[1],        h_vecs[2],       h_vecs[3],+        h_vecs[4],       h_vecs[5],        h_vecs[6],       h_vecs[7],+        set1_512(IV[0]), set1_512(IV[1]),  set1_512(IV[2]), set1_512(IV[3]),+        counter_low_vec, counter_high_vec, block_len_vec,   block_flags_vec,+    };+    round_fn16(v, msg_vecs, 0);+    round_fn16(v, msg_vecs, 1);+    round_fn16(v, msg_vecs, 2);+    round_fn16(v, msg_vecs, 3);+    round_fn16(v, msg_vecs, 4);+    round_fn16(v, msg_vecs, 5);+    round_fn16(v, msg_vecs, 6);+    h_vecs[0] = xor_512(v[0], v[8]);+    h_vecs[1] = xor_512(v[1], v[9]);+    h_vecs[2] = xor_512(v[2], v[10]);+    h_vecs[3] = xor_512(v[3], v[11]);+    h_vecs[4] = xor_512(v[4], v[12]);+    h_vecs[5] = xor_512(v[5], v[13]);+    h_vecs[6] = xor_512(v[6], v[14]);+    h_vecs[7] = xor_512(v[7], v[15]);++    block_flags = flags;+  }++  // transpose_vecs_512 operates on a 16x16 matrix of words, but we only have 8+  // state vectors. Pad the matrix with zeros. After transposition, store the+  // lower half of each vector.+  __m512i padded[16] = {+      h_vecs[0],   h_vecs[1],   h_vecs[2],   h_vecs[3],+      h_vecs[4],   h_vecs[5],   h_vecs[6],   h_vecs[7],+      set1_512(0), set1_512(0), set1_512(0), set1_512(0),+      set1_512(0), set1_512(0), set1_512(0), set1_512(0),+  };+  transpose_vecs_512(padded);+  _mm256_mask_storeu_epi32(&out[0 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[0]));+  _mm256_mask_storeu_epi32(&out[1 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[1]));+  _mm256_mask_storeu_epi32(&out[2 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[2]));+  _mm256_mask_storeu_epi32(&out[3 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[3]));+  _mm256_mask_storeu_epi32(&out[4 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[4]));+  _mm256_mask_storeu_epi32(&out[5 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[5]));+  _mm256_mask_storeu_epi32(&out[6 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[6]));+  _mm256_mask_storeu_epi32(&out[7 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[7]));+  _mm256_mask_storeu_epi32(&out[8 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[8]));+  _mm256_mask_storeu_epi32(&out[9 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[9]));+  _mm256_mask_storeu_epi32(&out[10 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[10]));+  _mm256_mask_storeu_epi32(&out[11 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[11]));+  _mm256_mask_storeu_epi32(&out[12 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[12]));+  _mm256_mask_storeu_epi32(&out[13 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[13]));+  _mm256_mask_storeu_epi32(&out[14 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[14]));+  _mm256_mask_storeu_epi32(&out[15 * sizeof(__m256i)], (__mmask8)-1, _mm512_castsi512_si256(padded[15]));+}++static+void blake3_xof16_avx512(const uint32_t cv[8],+                        const uint8_t block[BLAKE3_BLOCK_LEN],+                        uint8_t block_len, uint64_t counter, uint8_t flags,+                        uint8_t out[16 * 64]) {+  __m512i h_vecs[8] = {+      set1_512(cv[0]), set1_512(cv[1]), set1_512(cv[2]), set1_512(cv[3]),+      set1_512(cv[4]), set1_512(cv[5]), set1_512(cv[6]), set1_512(cv[7]),+  };+  uint32_t block_words[16];+  load_block_words(block, block_words);+  __m512i msg_vecs[16];+  for (size_t i = 0; i < 16; i++) {+      msg_vecs[i] = set1_512(block_words[i]);+  }+  __m512i counter_low_vec, counter_high_vec;+  load_counters16(counter, true, &counter_low_vec, &counter_high_vec);+  __m512i block_len_vec = set1_512(block_len);+  __m512i block_flags_vec = set1_512(flags);+  __m512i v[16] = {+      h_vecs[0],       h_vecs[1],        h_vecs[2],       h_vecs[3],+      h_vecs[4],       h_vecs[5],        h_vecs[6],       h_vecs[7],+      set1_512(IV[0]), set1_512(IV[1]),  set1_512(IV[2]), set1_512(IV[3]),+      counter_low_vec, counter_high_vec, block_len_vec,   block_flags_vec,+  };+  round_fn16(v, msg_vecs, 0);+  round_fn16(v, msg_vecs, 1);+  round_fn16(v, msg_vecs, 2);+  round_fn16(v, msg_vecs, 3);+  round_fn16(v, msg_vecs, 4);+  round_fn16(v, msg_vecs, 5);+  round_fn16(v, msg_vecs, 6);+  for (size_t i = 0; i < 8; i++) {+      v[i] = xor_512(v[i], v[i+8]);+      v[i+8] = xor_512(v[i+8], h_vecs[i]);+  }+  transpose_vecs_512(&v[0]);+  for (size_t i = 0; i < 16; i++) {+      storeu_512(v[i], &out[i * sizeof(__m512i)]);+  }+}++/*+ * ----------------------------------------------------------------------------+ * hash_many_avx512+ * ----------------------------------------------------------------------------+ */++INLINE void hash_one_avx512(const uint8_t *input, size_t blocks,+                            const uint32_t key[8], uint64_t counter,+                            uint8_t flags, uint8_t flags_start,+                            uint8_t flags_end, uint8_t out[BLAKE3_OUT_LEN]) {+  uint32_t cv[8];+  memcpy(cv, key, BLAKE3_KEY_LEN);+  uint8_t block_flags = flags | flags_start;+  while (blocks > 0) {+    if (blocks == 1) {+      block_flags |= flags_end;+    }+    blake3_compress_in_place_avx512(cv, input, BLAKE3_BLOCK_LEN, counter,+                                    block_flags);+    input = &input[BLAKE3_BLOCK_LEN];+    blocks -= 1;+    block_flags = flags;+  }+  memcpy(out, cv, BLAKE3_OUT_LEN);+}++void blake3_hash_many_avx512(const uint8_t *const *inputs, size_t num_inputs,+                             size_t blocks, const uint32_t key[8],+                             uint64_t counter, bool increment_counter,+                             uint8_t flags, uint8_t flags_start,+                             uint8_t flags_end, uint8_t *out) {+  while (num_inputs >= 16) {+    blake3_hash16_avx512(inputs, blocks, key, counter, increment_counter, flags,+                         flags_start, flags_end, out);+    if (increment_counter) {+      counter += 16;+    }+    inputs += 16;+    num_inputs -= 16;+    out = &out[16 * BLAKE3_OUT_LEN];+  }+  while (num_inputs >= 8) {+    blake3_hash8_avx512(inputs, blocks, key, counter, increment_counter, flags,+                        flags_start, flags_end, out);+    if (increment_counter) {+      counter += 8;+    }+    inputs += 8;+    num_inputs -= 8;+    out = &out[8 * BLAKE3_OUT_LEN];+  }+  while (num_inputs >= 4) {+    blake3_hash4_avx512(inputs, blocks, key, counter, increment_counter, flags,+                        flags_start, flags_end, out);+    if (increment_counter) {+      counter += 4;+    }+    inputs += 4;+    num_inputs -= 4;+    out = &out[4 * BLAKE3_OUT_LEN];+  }+  while (num_inputs > 0) {+    hash_one_avx512(inputs[0], blocks, key, counter, flags, flags_start,+                    flags_end, out);+    if (increment_counter) {+      counter += 1;+    }+    inputs += 1;+    num_inputs -= 1;+    out = &out[BLAKE3_OUT_LEN];+  }+}++void blake3_xof_many_avx512(const uint32_t cv[8],+                            const uint8_t block[BLAKE3_BLOCK_LEN],+                            uint8_t block_len, uint64_t counter, uint8_t flags,+                            uint8_t* out, size_t outblocks) {+  while (outblocks >= 16) {+    blake3_xof16_avx512(cv, block, block_len, counter, flags, out);+    counter += 16;+    outblocks -= 16;+    out += 16 * BLAKE3_BLOCK_LEN;+  }+  while (outblocks >= 8) {+    blake3_xof8_avx512(cv, block, block_len, counter, flags, out);+    counter += 8;+    outblocks -= 8;+    out += 8 * BLAKE3_BLOCK_LEN;+  }+  while (outblocks >= 4) {+    blake3_xof4_avx512(cv, block, block_len, counter, flags, out);+    counter += 4;+    outblocks -= 4;+    out += 4 * BLAKE3_BLOCK_LEN;+  }+  while (outblocks > 0) {+    blake3_compress_xof_avx512(cv, block, block_len, counter, flags, out);+    counter += 1;+    outblocks -= 1;+    out += BLAKE3_BLOCK_LEN;+  }+}--- /dev/null+++ b/src/src/crypto/blake3/blake3_dispatch.c@@ -0,0 +1,332 @@+#include <stdbool.h>+#include <stddef.h>+#include <stdint.h>++#include "blake3_impl.h"++#if defined(_MSC_VER)+#include <Windows.h>+#endif++#if defined(IS_X86)+#if defined(_MSC_VER)+#include <intrin.h>+#elif defined(__GNUC__)+#include <immintrin.h>+#else+#undef IS_X86 /* Unimplemented! */+#endif+#endif++#if !defined(BLAKE3_ATOMICS)+#if defined(__has_include)+#if __has_include(<stdatomic.h>) && !defined(_MSC_VER)+#define BLAKE3_ATOMICS 1+#else+#define BLAKE3_ATOMICS 0+#endif /* __has_include(<stdatomic.h>) && !defined(_MSC_VER) */+#else+#define BLAKE3_ATOMICS 0+#endif /* defined(__has_include) */+#endif /* BLAKE3_ATOMICS */++#if BLAKE3_ATOMICS+#define ATOMIC_INT _Atomic int+#define ATOMIC_LOAD(x) x+#define ATOMIC_STORE(x, y) x = y+#elif defined(_MSC_VER)+#define ATOMIC_INT LONG+#define ATOMIC_LOAD(x) InterlockedOr(&x, 0)+#define ATOMIC_STORE(x, y) InterlockedExchange(&x, y)+#else+#define ATOMIC_INT int+#define ATOMIC_LOAD(x) x+#define ATOMIC_STORE(x, y) x = y+#endif++#define MAYBE_UNUSED(x) (void)((x))++#if defined(IS_X86)+static uint64_t xgetbv(void) {+#if defined(_MSC_VER)+  return _xgetbv(0);+#else+  uint32_t eax = 0, edx = 0;+  __asm__ __volatile__("xgetbv\n" : "=a"(eax), "=d"(edx) : "c"(0));+  return ((uint64_t)edx << 32) | eax;+#endif+}++static void cpuid(uint32_t out[4], uint32_t id) {+#if defined(_MSC_VER)+  __cpuid((int *)out, id);+#elif defined(__i386__) || defined(_M_IX86)+  __asm__ __volatile__("movl %%ebx, %1\n"+                       "cpuid\n"+                       "xchgl %1, %%ebx\n"+                       : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3])+                       : "a"(id));+#else+  __asm__ __volatile__("cpuid\n"+                       : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3])+                       : "a"(id));+#endif+}++static void cpuidex(uint32_t out[4], uint32_t id, uint32_t sid) {+#if defined(_MSC_VER)+  __cpuidex((int *)out, id, sid);+#elif defined(__i386__) || defined(_M_IX86)+  __asm__ __volatile__("movl %%ebx, %1\n"+                       "cpuid\n"+                       "xchgl %1, %%ebx\n"+                       : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3])+                       : "a"(id), "c"(sid));+#else+  __asm__ __volatile__("cpuid\n"+                       : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3])+                       : "a"(id), "c"(sid));+#endif+}+++enum cpu_feature {+  SSE2 = 1 << 0,+  SSSE3 = 1 << 1,+  SSE41 = 1 << 2,+  AVX = 1 << 3,+  AVX2 = 1 << 4,+  AVX512F = 1 << 5,+  AVX512VL = 1 << 6,+  /* ... */+  UNDEFINED = 1 << 30+};++#if !defined(BLAKE3_TESTING)+static /* Allow the variable to be controlled manually for testing */+#endif+    ATOMIC_INT g_cpu_features = UNDEFINED;++#if !defined(BLAKE3_TESTING)+static+#endif+    enum cpu_feature+    get_cpu_features(void) {++  /* If TSAN detects a data race here, try compiling with -DBLAKE3_ATOMICS=1 */+  enum cpu_feature features = ATOMIC_LOAD(g_cpu_features);+  if (features != UNDEFINED) {+    return features;+  } else {+#if defined(IS_X86)+    uint32_t regs[4] = {0};+    uint32_t *eax = &regs[0], *ebx = &regs[1], *ecx = &regs[2], *edx = &regs[3];+    (void)edx;+    features = 0;+    cpuid(regs, 0);+    const int max_id = *eax;+    cpuid(regs, 1);+#if defined(__amd64__) || defined(_M_X64)+    features |= SSE2;+#else+    if (*edx & (1UL << 26))+      features |= SSE2;+#endif+    if (*ecx & (1UL << 9))+      features |= SSSE3;+    if (*ecx & (1UL << 19))+      features |= SSE41;++    if (*ecx & (1UL << 27)) { // OSXSAVE+      const uint64_t mask = xgetbv();+      if ((mask & 6) == 6) { // SSE and AVX states+        if (*ecx & (1UL << 28))+          features |= AVX;+        if (max_id >= 7) {+          cpuidex(regs, 7, 0);+          if (*ebx & (1UL << 5))+            features |= AVX2;+          if ((mask & 224) == 224) { // Opmask, ZMM_Hi256, Hi16_Zmm+            if (*ebx & (1UL << 31))+              features |= AVX512VL;+            if (*ebx & (1UL << 16))+              features |= AVX512F;+          }+        }+      }+    }+    ATOMIC_STORE(g_cpu_features, features);+    return features;+#else+    /* How to detect NEON? */+    return 0;+#endif+  }+}+#endif++void blake3_compress_in_place(uint32_t cv[8],+                              const uint8_t block[BLAKE3_BLOCK_LEN],+                              uint8_t block_len, uint64_t counter,+                              uint8_t flags) {+#if defined(IS_X86)+  const enum cpu_feature features = get_cpu_features();+  MAYBE_UNUSED(features);+#if !defined(BLAKE3_NO_AVX512)+  if (features & AVX512VL) {+    blake3_compress_in_place_avx512(cv, block, block_len, counter, flags);+    return;+  }+#endif+#if !defined(BLAKE3_NO_SSE41)+  if (features & SSE41) {+    blake3_compress_in_place_sse41(cv, block, block_len, counter, flags);+    return;+  }+#endif+#if !defined(BLAKE3_NO_SSE2)+  if (features & SSE2) {+    blake3_compress_in_place_sse2(cv, block, block_len, counter, flags);+    return;+  }+#endif+#endif+  blake3_compress_in_place_portable(cv, block, block_len, counter, flags);+}++void blake3_compress_xof(const uint32_t cv[8],+                         const uint8_t block[BLAKE3_BLOCK_LEN],+                         uint8_t block_len, uint64_t counter, uint8_t flags,+                         uint8_t out[64]) {+#if defined(IS_X86)+  const enum cpu_feature features = get_cpu_features();+  MAYBE_UNUSED(features);+#if !defined(BLAKE3_NO_AVX512)+  if (features & AVX512VL) {+    blake3_compress_xof_avx512(cv, block, block_len, counter, flags, out);+    return;+  }+#endif+#if !defined(BLAKE3_NO_SSE41)+  if (features & SSE41) {+    blake3_compress_xof_sse41(cv, block, block_len, counter, flags, out);+    return;+  }+#endif+#if !defined(BLAKE3_NO_SSE2)+  if (features & SSE2) {+    blake3_compress_xof_sse2(cv, block, block_len, counter, flags, out);+    return;+  }+#endif+#endif+  blake3_compress_xof_portable(cv, block, block_len, counter, flags, out);+}+++void blake3_xof_many(const uint32_t cv[8],+                     const uint8_t block[BLAKE3_BLOCK_LEN],+                     uint8_t block_len, uint64_t counter, uint8_t flags,+                     uint8_t out[64], size_t outblocks) {+  if (outblocks == 0) {+    // The current assembly implementation always outputs at least 1 block.+    return;+  }+#if defined(IS_X86)+  const enum cpu_feature features = get_cpu_features();+  MAYBE_UNUSED(features);+#if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(BLAKE3_NO_AVX512)+  if (features & AVX512VL) {+    blake3_xof_many_avx512(cv, block, block_len, counter, flags, out, outblocks);+    return;+  }+#endif+#endif+  for(size_t i = 0; i < outblocks; ++i) {+    blake3_compress_xof(cv, block, block_len, counter + i, flags, out + 64*i);+  }+}++void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs,+                      size_t blocks, const uint32_t key[8], uint64_t counter,+                      bool increment_counter, uint8_t flags,+                      uint8_t flags_start, uint8_t flags_end, uint8_t *out) {+#if defined(IS_X86)+  const enum cpu_feature features = get_cpu_features();+  MAYBE_UNUSED(features);+#if !defined(BLAKE3_NO_AVX512)+  if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) {+    blake3_hash_many_avx512(inputs, num_inputs, blocks, key, counter,+                            increment_counter, flags, flags_start, flags_end,+                            out);+    return;+  }+#endif+#if !defined(BLAKE3_NO_AVX2)+  if (features & AVX2) {+    blake3_hash_many_avx2(inputs, num_inputs, blocks, key, counter,+                          increment_counter, flags, flags_start, flags_end,+                          out);+    return;+  }+#endif+#if !defined(BLAKE3_NO_SSE41)+  if (features & SSE41) {+    blake3_hash_many_sse41(inputs, num_inputs, blocks, key, counter,+                           increment_counter, flags, flags_start, flags_end,+                           out);+    return;+  }+#endif+#if !defined(BLAKE3_NO_SSE2)+  if (features & SSE2) {+    blake3_hash_many_sse2(inputs, num_inputs, blocks, key, counter,+                          increment_counter, flags, flags_start, flags_end,+                          out);+    return;+  }+#endif+#endif++#if BLAKE3_USE_NEON == 1+  blake3_hash_many_neon(inputs, num_inputs, blocks, key, counter,+                        increment_counter, flags, flags_start, flags_end, out);+  return;+#endif++  blake3_hash_many_portable(inputs, num_inputs, blocks, key, counter,+                            increment_counter, flags, flags_start, flags_end,+                            out);+}++// The dynamically detected SIMD degree of the current platform.+size_t blake3_simd_degree(void) {+#if defined(IS_X86)+  const enum cpu_feature features = get_cpu_features();+  MAYBE_UNUSED(features);+#if !defined(BLAKE3_NO_AVX512)+  if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) {+    return 16;+  }+#endif+#if !defined(BLAKE3_NO_AVX2)+  if (features & AVX2) {+    return 8;+  }+#endif+#if !defined(BLAKE3_NO_SSE41)+  if (features & SSE41) {+    return 4;+  }+#endif+#if !defined(BLAKE3_NO_SSE2)+  if (features & SSE2) {+    return 4;+  }+#endif+#endif+#if BLAKE3_USE_NEON == 1+  return 4;+#endif+  return 1;+}--- /dev/null+++ b/src/src/crypto/blake3/blake3_impl.h@@ -0,0 +1,333 @@+#ifndef BLAKE3_IMPL_H+#define BLAKE3_IMPL_H++#include <assert.h>+#include <stdbool.h>+#include <stddef.h>+#include <stdint.h>+#include <string.h>++#include "blake3.h"++#ifdef __cplusplus+extern "C" {+#endif++// internal flags+enum blake3_flags {+  CHUNK_START         = 1 << 0,+  CHUNK_END           = 1 << 1,+  PARENT              = 1 << 2,+  ROOT                = 1 << 3,+  KEYED_HASH          = 1 << 4,+  DERIVE_KEY_CONTEXT  = 1 << 5,+  DERIVE_KEY_MATERIAL = 1 << 6,+};++// This C implementation tries to support recent versions of GCC, Clang, and+// MSVC.+#if defined(_MSC_VER)+#define INLINE static __forceinline+#else+#define INLINE static inline __attribute__((always_inline))+#endif++#ifdef __cplusplus+#define NOEXCEPT noexcept+#else+#define NOEXCEPT+#endif++#if (defined(__x86_64__) || defined(_M_X64)) && !defined(_M_ARM64EC)+#define IS_X86+#define IS_X86_64+#endif++#if defined(__i386__) || defined(_M_IX86)+#define IS_X86+#define IS_X86_32+#endif++#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)+#define IS_AARCH64+#endif++#if defined(IS_X86)+#if defined(_MSC_VER)+#include <intrin.h>+#endif+#endif++#if !defined(BLAKE3_USE_NEON) +  // If BLAKE3_USE_NEON not manually set, autodetect based on AArch64ness+  #if defined(IS_AARCH64)+    #if defined(__ARM_BIG_ENDIAN)+      #define BLAKE3_USE_NEON 0+    #else+      #define BLAKE3_USE_NEON 1+    #endif+  #else+    #define BLAKE3_USE_NEON 0+  #endif+#endif++#if defined(IS_X86)+#define MAX_SIMD_DEGREE 16+#elif BLAKE3_USE_NEON == 1+#define MAX_SIMD_DEGREE 4+#else+#define MAX_SIMD_DEGREE 1+#endif++// There are some places where we want a static size that's equal to the+// MAX_SIMD_DEGREE, but also at least 2.+#define MAX_SIMD_DEGREE_OR_2 (MAX_SIMD_DEGREE > 2 ? MAX_SIMD_DEGREE : 2)++static const uint32_t IV[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL,+                               0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL,+                               0x1F83D9ABUL, 0x5BE0CD19UL};++static const uint8_t MSG_SCHEDULE[7][16] = {+    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},+    {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8},+    {3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1},+    {10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6},+    {12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4},+    {9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7},+    {11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13},+};++/* Find index of the highest set bit */+/* x is assumed to be nonzero.       */+static unsigned int highest_one(uint64_t x) {+#if defined(__GNUC__) || defined(__clang__)+  return 63 ^ (unsigned int)__builtin_clzll(x);+#elif defined(_MSC_VER) && defined(IS_X86_64)+  unsigned long index;+  _BitScanReverse64(&index, x);+  return index;+#elif defined(_MSC_VER) && defined(IS_X86_32)+  if(x >> 32) {+    unsigned long index;+    _BitScanReverse(&index, (unsigned long)(x >> 32));+    return 32 + index;+  } else {+    unsigned long index;+    _BitScanReverse(&index, (unsigned long)x);+    return index;+  }+#else+  unsigned int c = 0;+  if(x & 0xffffffff00000000ULL) { x >>= 32; c += 32; }+  if(x & 0x00000000ffff0000ULL) { x >>= 16; c += 16; }+  if(x & 0x000000000000ff00ULL) { x >>=  8; c +=  8; }+  if(x & 0x00000000000000f0ULL) { x >>=  4; c +=  4; }+  if(x & 0x000000000000000cULL) { x >>=  2; c +=  2; }+  if(x & 0x0000000000000002ULL) {           c +=  1; }+  return c;+#endif+}++// Count the number of 1 bits.+INLINE unsigned int popcnt(uint64_t x) {+#if defined(__GNUC__) || defined(__clang__)+  return (unsigned int)__builtin_popcountll(x);+#else+  unsigned int count = 0;+  while (x != 0) {+    count += 1;+    x &= x - 1;+  }+  return count;+#endif+}++// Largest power of two less than or equal to x. As a special case, returns 1+// when x is 0. +INLINE uint64_t round_down_to_power_of_2(uint64_t x) {+  return 1ULL << highest_one(x | 1);+}++INLINE uint32_t counter_low(uint64_t counter) { return (uint32_t)counter; }++INLINE uint32_t counter_high(uint64_t counter) {+  return (uint32_t)(counter >> 32);+}++INLINE uint32_t load32(const void *src) {+  const uint8_t *p = (const uint8_t *)src;+  return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |+         ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);+}++INLINE void load_key_words(const uint8_t key[BLAKE3_KEY_LEN],+                           uint32_t key_words[8]) {+  key_words[0] = load32(&key[0 * 4]);+  key_words[1] = load32(&key[1 * 4]);+  key_words[2] = load32(&key[2 * 4]);+  key_words[3] = load32(&key[3 * 4]);+  key_words[4] = load32(&key[4 * 4]);+  key_words[5] = load32(&key[5 * 4]);+  key_words[6] = load32(&key[6 * 4]);+  key_words[7] = load32(&key[7 * 4]);+}++INLINE void load_block_words(const uint8_t block[BLAKE3_BLOCK_LEN],+                             uint32_t block_words[16]) {+  for (size_t i = 0; i < 16; i++) {+      block_words[i] = load32(&block[i * 4]);+  }+}++INLINE void store32(void *dst, uint32_t w) {+  uint8_t *p = (uint8_t *)dst;+  p[0] = (uint8_t)(w >> 0);+  p[1] = (uint8_t)(w >> 8);+  p[2] = (uint8_t)(w >> 16);+  p[3] = (uint8_t)(w >> 24);+}++INLINE void store_cv_words(uint8_t bytes_out[32], uint32_t cv_words[8]) {+  store32(&bytes_out[0 * 4], cv_words[0]);+  store32(&bytes_out[1 * 4], cv_words[1]);+  store32(&bytes_out[2 * 4], cv_words[2]);+  store32(&bytes_out[3 * 4], cv_words[3]);+  store32(&bytes_out[4 * 4], cv_words[4]);+  store32(&bytes_out[5 * 4], cv_words[5]);+  store32(&bytes_out[6 * 4], cv_words[6]);+  store32(&bytes_out[7 * 4], cv_words[7]);+}++void blake3_compress_in_place(uint32_t cv[8],+                              const uint8_t block[BLAKE3_BLOCK_LEN],+                              uint8_t block_len, uint64_t counter,+                              uint8_t flags);++void blake3_compress_xof(const uint32_t cv[8],+                         const uint8_t block[BLAKE3_BLOCK_LEN],+                         uint8_t block_len, uint64_t counter, uint8_t flags,+                         uint8_t out[64]);++void blake3_xof_many(const uint32_t cv[8],+                     const uint8_t block[BLAKE3_BLOCK_LEN],+                     uint8_t block_len, uint64_t counter, uint8_t flags,+                     uint8_t out[64], size_t outblocks);++void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs,+                      size_t blocks, const uint32_t key[8], uint64_t counter,+                      bool increment_counter, uint8_t flags,+                      uint8_t flags_start, uint8_t flags_end, uint8_t *out);++size_t blake3_simd_degree(void);++BLAKE3_PRIVATE size_t blake3_compress_subtree_wide(const uint8_t *input, size_t input_len,+                                                   const uint32_t key[8],+                                                   uint64_t chunk_counter, uint8_t flags,+                                                   uint8_t *out, bool use_tbb);++#if defined(BLAKE3_USE_TBB)+BLAKE3_PRIVATE void blake3_compress_subtree_wide_join_tbb(+    // shared params+    const uint32_t key[8], uint8_t flags, bool use_tbb,+    // left-hand side params+    const uint8_t *l_input, size_t l_input_len, uint64_t l_chunk_counter,+    uint8_t *l_cvs, size_t *l_n,+    // right-hand side params+    const uint8_t *r_input, size_t r_input_len, uint64_t r_chunk_counter,+    uint8_t *r_cvs, size_t *r_n) NOEXCEPT;+#endif++// Declarations for implementation-specific functions.+void blake3_compress_in_place_portable(uint32_t cv[8],+                                       const uint8_t block[BLAKE3_BLOCK_LEN],+                                       uint8_t block_len, uint64_t counter,+                                       uint8_t flags);++void blake3_compress_xof_portable(const uint32_t cv[8],+                                  const uint8_t block[BLAKE3_BLOCK_LEN],+                                  uint8_t block_len, uint64_t counter,+                                  uint8_t flags, uint8_t out[64]);++void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs,+                               size_t blocks, const uint32_t key[8],+                               uint64_t counter, bool increment_counter,+                               uint8_t flags, uint8_t flags_start,+                               uint8_t flags_end, uint8_t *out);++#if defined(IS_X86)+#if !defined(BLAKE3_NO_SSE2)+void blake3_compress_in_place_sse2(uint32_t cv[8],+                                   const uint8_t block[BLAKE3_BLOCK_LEN],+                                   uint8_t block_len, uint64_t counter,+                                   uint8_t flags);+void blake3_compress_xof_sse2(const uint32_t cv[8],+                              const uint8_t block[BLAKE3_BLOCK_LEN],+                              uint8_t block_len, uint64_t counter,+                              uint8_t flags, uint8_t out[64]);+void blake3_hash_many_sse2(const uint8_t *const *inputs, size_t num_inputs,+                           size_t blocks, const uint32_t key[8],+                           uint64_t counter, bool increment_counter,+                           uint8_t flags, uint8_t flags_start,+                           uint8_t flags_end, uint8_t *out);+#endif+#if !defined(BLAKE3_NO_SSE41)+void blake3_compress_in_place_sse41(uint32_t cv[8],+                                    const uint8_t block[BLAKE3_BLOCK_LEN],+                                    uint8_t block_len, uint64_t counter,+                                    uint8_t flags);+void blake3_compress_xof_sse41(const uint32_t cv[8],+                               const uint8_t block[BLAKE3_BLOCK_LEN],+                               uint8_t block_len, uint64_t counter,+                               uint8_t flags, uint8_t out[64]);+void blake3_hash_many_sse41(const uint8_t *const *inputs, size_t num_inputs,+                            size_t blocks, const uint32_t key[8],+                            uint64_t counter, bool increment_counter,+                            uint8_t flags, uint8_t flags_start,+                            uint8_t flags_end, uint8_t *out);+#endif+#if !defined(BLAKE3_NO_AVX2)+void blake3_hash_many_avx2(const uint8_t *const *inputs, size_t num_inputs,+                           size_t blocks, const uint32_t key[8],+                           uint64_t counter, bool increment_counter,+                           uint8_t flags, uint8_t flags_start,+                           uint8_t flags_end, uint8_t *out);+#endif+#if !defined(BLAKE3_NO_AVX512)+void blake3_compress_in_place_avx512(uint32_t cv[8],+                                     const uint8_t block[BLAKE3_BLOCK_LEN],+                                     uint8_t block_len, uint64_t counter,+                                     uint8_t flags);++void blake3_compress_xof_avx512(const uint32_t cv[8],+                                const uint8_t block[BLAKE3_BLOCK_LEN],+                                uint8_t block_len, uint64_t counter,+                                uint8_t flags, uint8_t out[64]);++void blake3_hash_many_avx512(const uint8_t *const *inputs, size_t num_inputs,+                             size_t blocks, const uint32_t key[8],+                             uint64_t counter, bool increment_counter,+                             uint8_t flags, uint8_t flags_start,+                             uint8_t flags_end, uint8_t *out);++#if !defined(_WIN32) && !defined(__CYGWIN__)+void blake3_xof_many_avx512(const uint32_t cv[8],+                            const uint8_t block[BLAKE3_BLOCK_LEN],+                            uint8_t block_len, uint64_t counter, uint8_t flags,+                            uint8_t* out, size_t outblocks);+#endif+#endif+#endif++#if BLAKE3_USE_NEON == 1+void blake3_hash_many_neon(const uint8_t *const *inputs, size_t num_inputs,+                           size_t blocks, const uint32_t key[8],+                           uint64_t counter, bool increment_counter,+                           uint8_t flags, uint8_t flags_start,+                           uint8_t flags_end, uint8_t *out);+#endif++#ifdef __cplusplus+}+#endif++#endif /* BLAKE3_IMPL_H */--- /dev/null+++ b/src/src/crypto/blake3/blake3_portable.c@@ -0,0 +1,160 @@+#include "blake3_impl.h"+#include <string.h>++INLINE uint32_t rotr32(uint32_t w, uint32_t c) {+  return (w >> c) | (w << (32 - c));+}++INLINE void g(uint32_t *state, size_t a, size_t b, size_t c, size_t d,+              uint32_t x, uint32_t y) {+  state[a] = state[a] + state[b] + x;+  state[d] = rotr32(state[d] ^ state[a], 16);+  state[c] = state[c] + state[d];+  state[b] = rotr32(state[b] ^ state[c], 12);+  state[a] = state[a] + state[b] + y;+  state[d] = rotr32(state[d] ^ state[a], 8);+  state[c] = state[c] + state[d];+  state[b] = rotr32(state[b] ^ state[c], 7);+}++INLINE void round_fn(uint32_t state[16], const uint32_t *msg, size_t round) {+  // Select the message schedule based on the round.+  const uint8_t *schedule = MSG_SCHEDULE[round];++  // Mix the columns.+  g(state, 0, 4, 8, 12, msg[schedule[0]], msg[schedule[1]]);+  g(state, 1, 5, 9, 13, msg[schedule[2]], msg[schedule[3]]);+  g(state, 2, 6, 10, 14, msg[schedule[4]], msg[schedule[5]]);+  g(state, 3, 7, 11, 15, msg[schedule[6]], msg[schedule[7]]);++  // Mix the rows.+  g(state, 0, 5, 10, 15, msg[schedule[8]], msg[schedule[9]]);+  g(state, 1, 6, 11, 12, msg[schedule[10]], msg[schedule[11]]);+  g(state, 2, 7, 8, 13, msg[schedule[12]], msg[schedule[13]]);+  g(state, 3, 4, 9, 14, msg[schedule[14]], msg[schedule[15]]);+}++INLINE void compress_pre(uint32_t state[16], const uint32_t cv[8],+                         const uint8_t block[BLAKE3_BLOCK_LEN],+                         uint8_t block_len, uint64_t counter, uint8_t flags) {+  uint32_t block_words[16];+  block_words[0] = load32(block + 4 * 0);+  block_words[1] = load32(block + 4 * 1);+  block_words[2] = load32(block + 4 * 2);+  block_words[3] = load32(block + 4 * 3);+  block_words[4] = load32(block + 4 * 4);+  block_words[5] = load32(block + 4 * 5);+  block_words[6] = load32(block + 4 * 6);+  block_words[7] = load32(block + 4 * 7);+  block_words[8] = load32(block + 4 * 8);+  block_words[9] = load32(block + 4 * 9);+  block_words[10] = load32(block + 4 * 10);+  block_words[11] = load32(block + 4 * 11);+  block_words[12] = load32(block + 4 * 12);+  block_words[13] = load32(block + 4 * 13);+  block_words[14] = load32(block + 4 * 14);+  block_words[15] = load32(block + 4 * 15);++  state[0] = cv[0];+  state[1] = cv[1];+  state[2] = cv[2];+  state[3] = cv[3];+  state[4] = cv[4];+  state[5] = cv[5];+  state[6] = cv[6];+  state[7] = cv[7];+  state[8] = IV[0];+  state[9] = IV[1];+  state[10] = IV[2];+  state[11] = IV[3];+  state[12] = counter_low(counter);+  state[13] = counter_high(counter);+  state[14] = (uint32_t)block_len;+  state[15] = (uint32_t)flags;++  round_fn(state, &block_words[0], 0);+  round_fn(state, &block_words[0], 1);+  round_fn(state, &block_words[0], 2);+  round_fn(state, &block_words[0], 3);+  round_fn(state, &block_words[0], 4);+  round_fn(state, &block_words[0], 5);+  round_fn(state, &block_words[0], 6);+}++void blake3_compress_in_place_portable(uint32_t cv[8],+                                       const uint8_t block[BLAKE3_BLOCK_LEN],+                                       uint8_t block_len, uint64_t counter,+                                       uint8_t flags) {+  uint32_t state[16];+  compress_pre(state, cv, block, block_len, counter, flags);+  cv[0] = state[0] ^ state[8];+  cv[1] = state[1] ^ state[9];+  cv[2] = state[2] ^ state[10];+  cv[3] = state[3] ^ state[11];+  cv[4] = state[4] ^ state[12];+  cv[5] = state[5] ^ state[13];+  cv[6] = state[6] ^ state[14];+  cv[7] = state[7] ^ state[15];+}++void blake3_compress_xof_portable(const uint32_t cv[8],+                                  const uint8_t block[BLAKE3_BLOCK_LEN],+                                  uint8_t block_len, uint64_t counter,+                                  uint8_t flags, uint8_t out[64]) {+  uint32_t state[16];+  compress_pre(state, cv, block, block_len, counter, flags);++  store32(&out[0 * 4], state[0] ^ state[8]);+  store32(&out[1 * 4], state[1] ^ state[9]);+  store32(&out[2 * 4], state[2] ^ state[10]);+  store32(&out[3 * 4], state[3] ^ state[11]);+  store32(&out[4 * 4], state[4] ^ state[12]);+  store32(&out[5 * 4], state[5] ^ state[13]);+  store32(&out[6 * 4], state[6] ^ state[14]);+  store32(&out[7 * 4], state[7] ^ state[15]);+  store32(&out[8 * 4], state[8] ^ cv[0]);+  store32(&out[9 * 4], state[9] ^ cv[1]);+  store32(&out[10 * 4], state[10] ^ cv[2]);+  store32(&out[11 * 4], state[11] ^ cv[3]);+  store32(&out[12 * 4], state[12] ^ cv[4]);+  store32(&out[13 * 4], state[13] ^ cv[5]);+  store32(&out[14 * 4], state[14] ^ cv[6]);+  store32(&out[15 * 4], state[15] ^ cv[7]);+}++INLINE void hash_one_portable(const uint8_t *input, size_t blocks,+                              const uint32_t key[8], uint64_t counter,+                              uint8_t flags, uint8_t flags_start,+                              uint8_t flags_end, uint8_t out[BLAKE3_OUT_LEN]) {+  uint32_t cv[8];+  memcpy(cv, key, BLAKE3_KEY_LEN);+  uint8_t block_flags = flags | flags_start;+  while (blocks > 0) {+    if (blocks == 1) {+      block_flags |= flags_end;+    }+    blake3_compress_in_place_portable(cv, input, BLAKE3_BLOCK_LEN, counter,+                                      block_flags);+    input = &input[BLAKE3_BLOCK_LEN];+    blocks -= 1;+    block_flags = flags;+  }+  store_cv_words(out, cv);+}++void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs,+                               size_t blocks, const uint32_t key[8],+                               uint64_t counter, bool increment_counter,+                               uint8_t flags, uint8_t flags_start,+                               uint8_t flags_end, uint8_t *out) {+  while (num_inputs > 0) {+    hash_one_portable(inputs[0], blocks, key, counter, flags, flags_start,+                      flags_end, out);+    if (increment_counter) {+      counter += 1;+    }+    inputs += 1;+    num_inputs -= 1;+    out = &out[BLAKE3_OUT_LEN];+  }+}--- /dev/null+++ b/src/src/crypto/blake3/blake3_sse2.c@@ -0,0 +1,566 @@+#include "blake3_impl.h"++#include <immintrin.h>++#define DEGREE 4++#define _mm_shuffle_ps2(a, b, c)                                               \+  (_mm_castps_si128(                                                           \+      _mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), (c))))++INLINE __m128i loadu(const uint8_t src[16]) {+  return _mm_loadu_si128((const __m128i *)src);+}++INLINE void storeu(__m128i src, uint8_t dest[16]) {+  _mm_storeu_si128((__m128i *)dest, src);+}++INLINE __m128i addv(__m128i a, __m128i b) { return _mm_add_epi32(a, b); }++// Note that clang-format doesn't like the name "xor" for some reason.+INLINE __m128i xorv(__m128i a, __m128i b) { return _mm_xor_si128(a, b); }++INLINE __m128i set1(uint32_t x) { return _mm_set1_epi32((int32_t)x); }++INLINE __m128i set4(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {+  return _mm_setr_epi32((int32_t)a, (int32_t)b, (int32_t)c, (int32_t)d);+}++INLINE __m128i rot16(__m128i x) {+  return _mm_shufflehi_epi16(_mm_shufflelo_epi16(x, 0xB1), 0xB1);+}++INLINE __m128i rot12(__m128i x) {+  return xorv(_mm_srli_epi32(x, 12), _mm_slli_epi32(x, 32 - 12));+}++INLINE __m128i rot8(__m128i x) {+  return xorv(_mm_srli_epi32(x, 8), _mm_slli_epi32(x, 32 - 8));+}++INLINE __m128i rot7(__m128i x) {+  return xorv(_mm_srli_epi32(x, 7), _mm_slli_epi32(x, 32 - 7));+}++INLINE void g1(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,+               __m128i m) {+  *row0 = addv(addv(*row0, m), *row1);+  *row3 = xorv(*row3, *row0);+  *row3 = rot16(*row3);+  *row2 = addv(*row2, *row3);+  *row1 = xorv(*row1, *row2);+  *row1 = rot12(*row1);+}++INLINE void g2(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,+               __m128i m) {+  *row0 = addv(addv(*row0, m), *row1);+  *row3 = xorv(*row3, *row0);+  *row3 = rot8(*row3);+  *row2 = addv(*row2, *row3);+  *row1 = xorv(*row1, *row2);+  *row1 = rot7(*row1);+}++// Note the optimization here of leaving row1 as the unrotated row, rather than+// row0. All the message loads below are adjusted to compensate for this. See+// discussion at https://github.com/sneves/blake2-avx2/pull/4+INLINE void diagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {+  *row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(2, 1, 0, 3));+  *row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));+  *row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(0, 3, 2, 1));+}++INLINE void undiagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {+  *row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(0, 3, 2, 1));+  *row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));+  *row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(2, 1, 0, 3));+}++INLINE __m128i blend_epi16(__m128i a, __m128i b, const int16_t imm8) {+  const __m128i bits = _mm_set_epi16(0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01);+  __m128i mask = _mm_set1_epi16(imm8);+  mask = _mm_and_si128(mask, bits);+  mask = _mm_cmpeq_epi16(mask, bits);+  return _mm_or_si128(_mm_and_si128(mask, b), _mm_andnot_si128(mask, a));+}++INLINE void compress_pre(__m128i rows[4], const uint32_t cv[8],+                         const uint8_t block[BLAKE3_BLOCK_LEN],+                         uint8_t block_len, uint64_t counter, uint8_t flags) {+  rows[0] = loadu((uint8_t *)&cv[0]);+  rows[1] = loadu((uint8_t *)&cv[4]);+  rows[2] = set4(IV[0], IV[1], IV[2], IV[3]);+  rows[3] = set4(counter_low(counter), counter_high(counter),+                 (uint32_t)block_len, (uint32_t)flags);++  __m128i m0 = loadu(&block[sizeof(__m128i) * 0]);+  __m128i m1 = loadu(&block[sizeof(__m128i) * 1]);+  __m128i m2 = loadu(&block[sizeof(__m128i) * 2]);+  __m128i m3 = loadu(&block[sizeof(__m128i) * 3]);++  __m128i t0, t1, t2, t3, tt;++  // Round 1. The first round permutes the message words from the original+  // input order, into the groups that get mixed in parallel.+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(2, 0, 2, 0)); //  6  4  2  0+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 3, 1)); //  7  5  3  1+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(2, 0, 2, 0)); // 14 12 10  8+  t2 = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2, 1, 0, 3));   // 12 10  8 14+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 1, 3, 1)); // 15 13 11  9+  t3 = _mm_shuffle_epi32(t3, _MM_SHUFFLE(2, 1, 0, 3));   // 13 11  9 15+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 2. This round and all following rounds apply a fixed permutation+  // to the message words from the round before.+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 3+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 4+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 5+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 6+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 7+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+}++void blake3_compress_in_place_sse2(uint32_t cv[8],+                                   const uint8_t block[BLAKE3_BLOCK_LEN],+                                   uint8_t block_len, uint64_t counter,+                                   uint8_t flags) {+  __m128i rows[4];+  compress_pre(rows, cv, block, block_len, counter, flags);+  storeu(xorv(rows[0], rows[2]), (uint8_t *)&cv[0]);+  storeu(xorv(rows[1], rows[3]), (uint8_t *)&cv[4]);+}++void blake3_compress_xof_sse2(const uint32_t cv[8],+                              const uint8_t block[BLAKE3_BLOCK_LEN],+                              uint8_t block_len, uint64_t counter,+                              uint8_t flags, uint8_t out[64]) {+  __m128i rows[4];+  compress_pre(rows, cv, block, block_len, counter, flags);+  storeu(xorv(rows[0], rows[2]), &out[0]);+  storeu(xorv(rows[1], rows[3]), &out[16]);+  storeu(xorv(rows[2], loadu((uint8_t *)&cv[0])), &out[32]);+  storeu(xorv(rows[3], loadu((uint8_t *)&cv[4])), &out[48]);+}++INLINE void round_fn(__m128i v[16], __m128i m[16], size_t r) {+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);+  v[0] = addv(v[0], v[4]);+  v[1] = addv(v[1], v[5]);+  v[2] = addv(v[2], v[6]);+  v[3] = addv(v[3], v[7]);+  v[12] = xorv(v[12], v[0]);+  v[13] = xorv(v[13], v[1]);+  v[14] = xorv(v[14], v[2]);+  v[15] = xorv(v[15], v[3]);+  v[12] = rot16(v[12]);+  v[13] = rot16(v[13]);+  v[14] = rot16(v[14]);+  v[15] = rot16(v[15]);+  v[8] = addv(v[8], v[12]);+  v[9] = addv(v[9], v[13]);+  v[10] = addv(v[10], v[14]);+  v[11] = addv(v[11], v[15]);+  v[4] = xorv(v[4], v[8]);+  v[5] = xorv(v[5], v[9]);+  v[6] = xorv(v[6], v[10]);+  v[7] = xorv(v[7], v[11]);+  v[4] = rot12(v[4]);+  v[5] = rot12(v[5]);+  v[6] = rot12(v[6]);+  v[7] = rot12(v[7]);+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);+  v[0] = addv(v[0], v[4]);+  v[1] = addv(v[1], v[5]);+  v[2] = addv(v[2], v[6]);+  v[3] = addv(v[3], v[7]);+  v[12] = xorv(v[12], v[0]);+  v[13] = xorv(v[13], v[1]);+  v[14] = xorv(v[14], v[2]);+  v[15] = xorv(v[15], v[3]);+  v[12] = rot8(v[12]);+  v[13] = rot8(v[13]);+  v[14] = rot8(v[14]);+  v[15] = rot8(v[15]);+  v[8] = addv(v[8], v[12]);+  v[9] = addv(v[9], v[13]);+  v[10] = addv(v[10], v[14]);+  v[11] = addv(v[11], v[15]);+  v[4] = xorv(v[4], v[8]);+  v[5] = xorv(v[5], v[9]);+  v[6] = xorv(v[6], v[10]);+  v[7] = xorv(v[7], v[11]);+  v[4] = rot7(v[4]);+  v[5] = rot7(v[5]);+  v[6] = rot7(v[6]);+  v[7] = rot7(v[7]);++  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);+  v[0] = addv(v[0], v[5]);+  v[1] = addv(v[1], v[6]);+  v[2] = addv(v[2], v[7]);+  v[3] = addv(v[3], v[4]);+  v[15] = xorv(v[15], v[0]);+  v[12] = xorv(v[12], v[1]);+  v[13] = xorv(v[13], v[2]);+  v[14] = xorv(v[14], v[3]);+  v[15] = rot16(v[15]);+  v[12] = rot16(v[12]);+  v[13] = rot16(v[13]);+  v[14] = rot16(v[14]);+  v[10] = addv(v[10], v[15]);+  v[11] = addv(v[11], v[12]);+  v[8] = addv(v[8], v[13]);+  v[9] = addv(v[9], v[14]);+  v[5] = xorv(v[5], v[10]);+  v[6] = xorv(v[6], v[11]);+  v[7] = xorv(v[7], v[8]);+  v[4] = xorv(v[4], v[9]);+  v[5] = rot12(v[5]);+  v[6] = rot12(v[6]);+  v[7] = rot12(v[7]);+  v[4] = rot12(v[4]);+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);+  v[0] = addv(v[0], v[5]);+  v[1] = addv(v[1], v[6]);+  v[2] = addv(v[2], v[7]);+  v[3] = addv(v[3], v[4]);+  v[15] = xorv(v[15], v[0]);+  v[12] = xorv(v[12], v[1]);+  v[13] = xorv(v[13], v[2]);+  v[14] = xorv(v[14], v[3]);+  v[15] = rot8(v[15]);+  v[12] = rot8(v[12]);+  v[13] = rot8(v[13]);+  v[14] = rot8(v[14]);+  v[10] = addv(v[10], v[15]);+  v[11] = addv(v[11], v[12]);+  v[8] = addv(v[8], v[13]);+  v[9] = addv(v[9], v[14]);+  v[5] = xorv(v[5], v[10]);+  v[6] = xorv(v[6], v[11]);+  v[7] = xorv(v[7], v[8]);+  v[4] = xorv(v[4], v[9]);+  v[5] = rot7(v[5]);+  v[6] = rot7(v[6]);+  v[7] = rot7(v[7]);+  v[4] = rot7(v[4]);+}++INLINE void transpose_vecs(__m128i vecs[DEGREE]) {+  // Interleave 32-bit lanes. The low unpack is lanes 00/11 and the high is+  // 22/33. Note that this doesn't split the vector into two lanes, as the+  // AVX2 counterparts do.+  __m128i ab_01 = _mm_unpacklo_epi32(vecs[0], vecs[1]);+  __m128i ab_23 = _mm_unpackhi_epi32(vecs[0], vecs[1]);+  __m128i cd_01 = _mm_unpacklo_epi32(vecs[2], vecs[3]);+  __m128i cd_23 = _mm_unpackhi_epi32(vecs[2], vecs[3]);++  // Interleave 64-bit lanes.+  __m128i abcd_0 = _mm_unpacklo_epi64(ab_01, cd_01);+  __m128i abcd_1 = _mm_unpackhi_epi64(ab_01, cd_01);+  __m128i abcd_2 = _mm_unpacklo_epi64(ab_23, cd_23);+  __m128i abcd_3 = _mm_unpackhi_epi64(ab_23, cd_23);++  vecs[0] = abcd_0;+  vecs[1] = abcd_1;+  vecs[2] = abcd_2;+  vecs[3] = abcd_3;+}++INLINE void transpose_msg_vecs(const uint8_t *const *inputs,+                               size_t block_offset, __m128i out[16]) {+  out[0] = loadu(&inputs[0][block_offset + 0 * sizeof(__m128i)]);+  out[1] = loadu(&inputs[1][block_offset + 0 * sizeof(__m128i)]);+  out[2] = loadu(&inputs[2][block_offset + 0 * sizeof(__m128i)]);+  out[3] = loadu(&inputs[3][block_offset + 0 * sizeof(__m128i)]);+  out[4] = loadu(&inputs[0][block_offset + 1 * sizeof(__m128i)]);+  out[5] = loadu(&inputs[1][block_offset + 1 * sizeof(__m128i)]);+  out[6] = loadu(&inputs[2][block_offset + 1 * sizeof(__m128i)]);+  out[7] = loadu(&inputs[3][block_offset + 1 * sizeof(__m128i)]);+  out[8] = loadu(&inputs[0][block_offset + 2 * sizeof(__m128i)]);+  out[9] = loadu(&inputs[1][block_offset + 2 * sizeof(__m128i)]);+  out[10] = loadu(&inputs[2][block_offset + 2 * sizeof(__m128i)]);+  out[11] = loadu(&inputs[3][block_offset + 2 * sizeof(__m128i)]);+  out[12] = loadu(&inputs[0][block_offset + 3 * sizeof(__m128i)]);+  out[13] = loadu(&inputs[1][block_offset + 3 * sizeof(__m128i)]);+  out[14] = loadu(&inputs[2][block_offset + 3 * sizeof(__m128i)]);+  out[15] = loadu(&inputs[3][block_offset + 3 * sizeof(__m128i)]);+  for (size_t i = 0; i < 4; ++i) {+    _mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);+  }+  transpose_vecs(&out[0]);+  transpose_vecs(&out[4]);+  transpose_vecs(&out[8]);+  transpose_vecs(&out[12]);+}++INLINE void load_counters(uint64_t counter, bool increment_counter,+                          __m128i *out_lo, __m128i *out_hi) {+  const __m128i mask = _mm_set1_epi32(-(int32_t)increment_counter);+  const __m128i add0 = _mm_set_epi32(3, 2, 1, 0);+  const __m128i add1 = _mm_and_si128(mask, add0);+  __m128i l = _mm_add_epi32(_mm_set1_epi32((int32_t)counter), add1);+  __m128i carry = _mm_cmpgt_epi32(_mm_xor_si128(add1, _mm_set1_epi32(0x80000000)), +                                  _mm_xor_si128(   l, _mm_set1_epi32(0x80000000)));+  __m128i h = _mm_sub_epi32(_mm_set1_epi32((int32_t)(counter >> 32)), carry);+  *out_lo = l;+  *out_hi = h;+}++static+void blake3_hash4_sse2(const uint8_t *const *inputs, size_t blocks,+                       const uint32_t key[8], uint64_t counter,+                       bool increment_counter, uint8_t flags,+                       uint8_t flags_start, uint8_t flags_end, uint8_t *out) {+  __m128i h_vecs[8] = {+      set1(key[0]), set1(key[1]), set1(key[2]), set1(key[3]),+      set1(key[4]), set1(key[5]), set1(key[6]), set1(key[7]),+  };+  __m128i counter_low_vec, counter_high_vec;+  load_counters(counter, increment_counter, &counter_low_vec,+                &counter_high_vec);+  uint8_t block_flags = flags | flags_start;++  for (size_t block = 0; block < blocks; block++) {+    if (block + 1 == blocks) {+      block_flags |= flags_end;+    }+    __m128i block_len_vec = set1(BLAKE3_BLOCK_LEN);+    __m128i block_flags_vec = set1(block_flags);+    __m128i msg_vecs[16];+    transpose_msg_vecs(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);++    __m128i v[16] = {+        h_vecs[0],       h_vecs[1],        h_vecs[2],     h_vecs[3],+        h_vecs[4],       h_vecs[5],        h_vecs[6],     h_vecs[7],+        set1(IV[0]),     set1(IV[1]),      set1(IV[2]),   set1(IV[3]),+        counter_low_vec, counter_high_vec, block_len_vec, block_flags_vec,+    };+    round_fn(v, msg_vecs, 0);+    round_fn(v, msg_vecs, 1);+    round_fn(v, msg_vecs, 2);+    round_fn(v, msg_vecs, 3);+    round_fn(v, msg_vecs, 4);+    round_fn(v, msg_vecs, 5);+    round_fn(v, msg_vecs, 6);+    h_vecs[0] = xorv(v[0], v[8]);+    h_vecs[1] = xorv(v[1], v[9]);+    h_vecs[2] = xorv(v[2], v[10]);+    h_vecs[3] = xorv(v[3], v[11]);+    h_vecs[4] = xorv(v[4], v[12]);+    h_vecs[5] = xorv(v[5], v[13]);+    h_vecs[6] = xorv(v[6], v[14]);+    h_vecs[7] = xorv(v[7], v[15]);++    block_flags = flags;+  }++  transpose_vecs(&h_vecs[0]);+  transpose_vecs(&h_vecs[4]);+  // The first four vecs now contain the first half of each output, and the+  // second four vecs contain the second half of each output.+  storeu(h_vecs[0], &out[0 * sizeof(__m128i)]);+  storeu(h_vecs[4], &out[1 * sizeof(__m128i)]);+  storeu(h_vecs[1], &out[2 * sizeof(__m128i)]);+  storeu(h_vecs[5], &out[3 * sizeof(__m128i)]);+  storeu(h_vecs[2], &out[4 * sizeof(__m128i)]);+  storeu(h_vecs[6], &out[5 * sizeof(__m128i)]);+  storeu(h_vecs[3], &out[6 * sizeof(__m128i)]);+  storeu(h_vecs[7], &out[7 * sizeof(__m128i)]);+}++INLINE void hash_one_sse2(const uint8_t *input, size_t blocks,+                          const uint32_t key[8], uint64_t counter,+                          uint8_t flags, uint8_t flags_start,+                          uint8_t flags_end, uint8_t out[BLAKE3_OUT_LEN]) {+  uint32_t cv[8];+  memcpy(cv, key, BLAKE3_KEY_LEN);+  uint8_t block_flags = flags | flags_start;+  while (blocks > 0) {+    if (blocks == 1) {+      block_flags |= flags_end;+    }+    blake3_compress_in_place_sse2(cv, input, BLAKE3_BLOCK_LEN, counter,+                                  block_flags);+    input = &input[BLAKE3_BLOCK_LEN];+    blocks -= 1;+    block_flags = flags;+  }+  memcpy(out, cv, BLAKE3_OUT_LEN);+}++void blake3_hash_many_sse2(const uint8_t *const *inputs, size_t num_inputs,+                           size_t blocks, const uint32_t key[8],+                           uint64_t counter, bool increment_counter,+                           uint8_t flags, uint8_t flags_start,+                           uint8_t flags_end, uint8_t *out) {+  while (num_inputs >= DEGREE) {+    blake3_hash4_sse2(inputs, blocks, key, counter, increment_counter, flags,+                      flags_start, flags_end, out);+    if (increment_counter) {+      counter += DEGREE;+    }+    inputs += DEGREE;+    num_inputs -= DEGREE;+    out = &out[DEGREE * BLAKE3_OUT_LEN];+  }+  while (num_inputs > 0) {+    hash_one_sse2(inputs[0], blocks, key, counter, flags, flags_start,+                  flags_end, out);+    if (increment_counter) {+      counter += 1;+    }+    inputs += 1;+    num_inputs -= 1;+    out = &out[BLAKE3_OUT_LEN];+  }+}--- /dev/null+++ b/src/src/crypto/blake3/blake3_sse41.c@@ -0,0 +1,560 @@+#include "blake3_impl.h"++#include <immintrin.h>++#define DEGREE 4++#define _mm_shuffle_ps2(a, b, c)                                               \+  (_mm_castps_si128(                                                           \+      _mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), (c))))++INLINE __m128i loadu(const uint8_t src[16]) {+  return _mm_loadu_si128((const __m128i *)src);+}++INLINE void storeu(__m128i src, uint8_t dest[16]) {+  _mm_storeu_si128((__m128i *)dest, src);+}++INLINE __m128i addv(__m128i a, __m128i b) { return _mm_add_epi32(a, b); }++// Note that clang-format doesn't like the name "xor" for some reason.+INLINE __m128i xorv(__m128i a, __m128i b) { return _mm_xor_si128(a, b); }++INLINE __m128i set1(uint32_t x) { return _mm_set1_epi32((int32_t)x); }++INLINE __m128i set4(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {+  return _mm_setr_epi32((int32_t)a, (int32_t)b, (int32_t)c, (int32_t)d);+}++INLINE __m128i rot16(__m128i x) {+  return _mm_shuffle_epi8(+      x, _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2));+}++INLINE __m128i rot12(__m128i x) {+  return xorv(_mm_srli_epi32(x, 12), _mm_slli_epi32(x, 32 - 12));+}++INLINE __m128i rot8(__m128i x) {+  return _mm_shuffle_epi8(+      x, _mm_set_epi8(12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1));+}++INLINE __m128i rot7(__m128i x) {+  return xorv(_mm_srli_epi32(x, 7), _mm_slli_epi32(x, 32 - 7));+}++INLINE void g1(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,+               __m128i m) {+  *row0 = addv(addv(*row0, m), *row1);+  *row3 = xorv(*row3, *row0);+  *row3 = rot16(*row3);+  *row2 = addv(*row2, *row3);+  *row1 = xorv(*row1, *row2);+  *row1 = rot12(*row1);+}++INLINE void g2(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,+               __m128i m) {+  *row0 = addv(addv(*row0, m), *row1);+  *row3 = xorv(*row3, *row0);+  *row3 = rot8(*row3);+  *row2 = addv(*row2, *row3);+  *row1 = xorv(*row1, *row2);+  *row1 = rot7(*row1);+}++// Note the optimization here of leaving row1 as the unrotated row, rather than+// row0. All the message loads below are adjusted to compensate for this. See+// discussion at https://github.com/sneves/blake2-avx2/pull/4+INLINE void diagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {+  *row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(2, 1, 0, 3));+  *row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));+  *row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(0, 3, 2, 1));+}++INLINE void undiagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {+  *row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(0, 3, 2, 1));+  *row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));+  *row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(2, 1, 0, 3));+}++INLINE void compress_pre(__m128i rows[4], const uint32_t cv[8],+                         const uint8_t block[BLAKE3_BLOCK_LEN],+                         uint8_t block_len, uint64_t counter, uint8_t flags) {+  rows[0] = loadu((uint8_t *)&cv[0]);+  rows[1] = loadu((uint8_t *)&cv[4]);+  rows[2] = set4(IV[0], IV[1], IV[2], IV[3]);+  rows[3] = set4(counter_low(counter), counter_high(counter),+                 (uint32_t)block_len, (uint32_t)flags);++  __m128i m0 = loadu(&block[sizeof(__m128i) * 0]);+  __m128i m1 = loadu(&block[sizeof(__m128i) * 1]);+  __m128i m2 = loadu(&block[sizeof(__m128i) * 2]);+  __m128i m3 = loadu(&block[sizeof(__m128i) * 3]);++  __m128i t0, t1, t2, t3, tt;++  // Round 1. The first round permutes the message words from the original+  // input order, into the groups that get mixed in parallel.+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(2, 0, 2, 0)); //  6  4  2  0+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 3, 1)); //  7  5  3  1+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(2, 0, 2, 0)); // 14 12 10  8+  t2 = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2, 1, 0, 3));   // 12 10  8 14+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 1, 3, 1)); // 15 13 11  9+  t3 = _mm_shuffle_epi32(t3, _MM_SHUFFLE(2, 1, 0, 3));   // 13 11  9 15+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 2. This round and all following rounds apply a fixed permutation+  // to the message words from the round before.+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 3+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 4+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 5+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 6+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+  m0 = t0;+  m1 = t1;+  m2 = t2;+  m3 = t3;++  // Round 7+  t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));+  t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);+  t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));+  tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));+  t1 = _mm_blend_epi16(tt, t1, 0xCC);+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);+  diagonalize(&rows[0], &rows[2], &rows[3]);+  t2 = _mm_unpacklo_epi64(m3, m1);+  tt = _mm_blend_epi16(t2, m2, 0xC0);+  t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));+  g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);+  t3 = _mm_unpackhi_epi32(m1, m3);+  tt = _mm_unpacklo_epi32(m2, t3);+  t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));+  g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);+  undiagonalize(&rows[0], &rows[2], &rows[3]);+}++void blake3_compress_in_place_sse41(uint32_t cv[8],+                                    const uint8_t block[BLAKE3_BLOCK_LEN],+                                    uint8_t block_len, uint64_t counter,+                                    uint8_t flags) {+  __m128i rows[4];+  compress_pre(rows, cv, block, block_len, counter, flags);+  storeu(xorv(rows[0], rows[2]), (uint8_t *)&cv[0]);+  storeu(xorv(rows[1], rows[3]), (uint8_t *)&cv[4]);+}++void blake3_compress_xof_sse41(const uint32_t cv[8],+                               const uint8_t block[BLAKE3_BLOCK_LEN],+                               uint8_t block_len, uint64_t counter,+                               uint8_t flags, uint8_t out[64]) {+  __m128i rows[4];+  compress_pre(rows, cv, block, block_len, counter, flags);+  storeu(xorv(rows[0], rows[2]), &out[0]);+  storeu(xorv(rows[1], rows[3]), &out[16]);+  storeu(xorv(rows[2], loadu((uint8_t *)&cv[0])), &out[32]);+  storeu(xorv(rows[3], loadu((uint8_t *)&cv[4])), &out[48]);+}++INLINE void round_fn(__m128i v[16], __m128i m[16], size_t r) {+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);+  v[0] = addv(v[0], v[4]);+  v[1] = addv(v[1], v[5]);+  v[2] = addv(v[2], v[6]);+  v[3] = addv(v[3], v[7]);+  v[12] = xorv(v[12], v[0]);+  v[13] = xorv(v[13], v[1]);+  v[14] = xorv(v[14], v[2]);+  v[15] = xorv(v[15], v[3]);+  v[12] = rot16(v[12]);+  v[13] = rot16(v[13]);+  v[14] = rot16(v[14]);+  v[15] = rot16(v[15]);+  v[8] = addv(v[8], v[12]);+  v[9] = addv(v[9], v[13]);+  v[10] = addv(v[10], v[14]);+  v[11] = addv(v[11], v[15]);+  v[4] = xorv(v[4], v[8]);+  v[5] = xorv(v[5], v[9]);+  v[6] = xorv(v[6], v[10]);+  v[7] = xorv(v[7], v[11]);+  v[4] = rot12(v[4]);+  v[5] = rot12(v[5]);+  v[6] = rot12(v[6]);+  v[7] = rot12(v[7]);+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);+  v[0] = addv(v[0], v[4]);+  v[1] = addv(v[1], v[5]);+  v[2] = addv(v[2], v[6]);+  v[3] = addv(v[3], v[7]);+  v[12] = xorv(v[12], v[0]);+  v[13] = xorv(v[13], v[1]);+  v[14] = xorv(v[14], v[2]);+  v[15] = xorv(v[15], v[3]);+  v[12] = rot8(v[12]);+  v[13] = rot8(v[13]);+  v[14] = rot8(v[14]);+  v[15] = rot8(v[15]);+  v[8] = addv(v[8], v[12]);+  v[9] = addv(v[9], v[13]);+  v[10] = addv(v[10], v[14]);+  v[11] = addv(v[11], v[15]);+  v[4] = xorv(v[4], v[8]);+  v[5] = xorv(v[5], v[9]);+  v[6] = xorv(v[6], v[10]);+  v[7] = xorv(v[7], v[11]);+  v[4] = rot7(v[4]);+  v[5] = rot7(v[5]);+  v[6] = rot7(v[6]);+  v[7] = rot7(v[7]);++  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);+  v[0] = addv(v[0], v[5]);+  v[1] = addv(v[1], v[6]);+  v[2] = addv(v[2], v[7]);+  v[3] = addv(v[3], v[4]);+  v[15] = xorv(v[15], v[0]);+  v[12] = xorv(v[12], v[1]);+  v[13] = xorv(v[13], v[2]);+  v[14] = xorv(v[14], v[3]);+  v[15] = rot16(v[15]);+  v[12] = rot16(v[12]);+  v[13] = rot16(v[13]);+  v[14] = rot16(v[14]);+  v[10] = addv(v[10], v[15]);+  v[11] = addv(v[11], v[12]);+  v[8] = addv(v[8], v[13]);+  v[9] = addv(v[9], v[14]);+  v[5] = xorv(v[5], v[10]);+  v[6] = xorv(v[6], v[11]);+  v[7] = xorv(v[7], v[8]);+  v[4] = xorv(v[4], v[9]);+  v[5] = rot12(v[5]);+  v[6] = rot12(v[6]);+  v[7] = rot12(v[7]);+  v[4] = rot12(v[4]);+  v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);+  v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);+  v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);+  v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);+  v[0] = addv(v[0], v[5]);+  v[1] = addv(v[1], v[6]);+  v[2] = addv(v[2], v[7]);+  v[3] = addv(v[3], v[4]);+  v[15] = xorv(v[15], v[0]);+  v[12] = xorv(v[12], v[1]);+  v[13] = xorv(v[13], v[2]);+  v[14] = xorv(v[14], v[3]);+  v[15] = rot8(v[15]);+  v[12] = rot8(v[12]);+  v[13] = rot8(v[13]);+  v[14] = rot8(v[14]);+  v[10] = addv(v[10], v[15]);+  v[11] = addv(v[11], v[12]);+  v[8] = addv(v[8], v[13]);+  v[9] = addv(v[9], v[14]);+  v[5] = xorv(v[5], v[10]);+  v[6] = xorv(v[6], v[11]);+  v[7] = xorv(v[7], v[8]);+  v[4] = xorv(v[4], v[9]);+  v[5] = rot7(v[5]);+  v[6] = rot7(v[6]);+  v[7] = rot7(v[7]);+  v[4] = rot7(v[4]);+}++INLINE void transpose_vecs(__m128i vecs[DEGREE]) {+  // Interleave 32-bit lanes. The low unpack is lanes 00/11 and the high is+  // 22/33. Note that this doesn't split the vector into two lanes, as the+  // AVX2 counterparts do.+  __m128i ab_01 = _mm_unpacklo_epi32(vecs[0], vecs[1]);+  __m128i ab_23 = _mm_unpackhi_epi32(vecs[0], vecs[1]);+  __m128i cd_01 = _mm_unpacklo_epi32(vecs[2], vecs[3]);+  __m128i cd_23 = _mm_unpackhi_epi32(vecs[2], vecs[3]);++  // Interleave 64-bit lanes.+  __m128i abcd_0 = _mm_unpacklo_epi64(ab_01, cd_01);+  __m128i abcd_1 = _mm_unpackhi_epi64(ab_01, cd_01);+  __m128i abcd_2 = _mm_unpacklo_epi64(ab_23, cd_23);+  __m128i abcd_3 = _mm_unpackhi_epi64(ab_23, cd_23);++  vecs[0] = abcd_0;+  vecs[1] = abcd_1;+  vecs[2] = abcd_2;+  vecs[3] = abcd_3;+}++INLINE void transpose_msg_vecs(const uint8_t *const *inputs,+                               size_t block_offset, __m128i out[16]) {+  out[0] = loadu(&inputs[0][block_offset + 0 * sizeof(__m128i)]);+  out[1] = loadu(&inputs[1][block_offset + 0 * sizeof(__m128i)]);+  out[2] = loadu(&inputs[2][block_offset + 0 * sizeof(__m128i)]);+  out[3] = loadu(&inputs[3][block_offset + 0 * sizeof(__m128i)]);+  out[4] = loadu(&inputs[0][block_offset + 1 * sizeof(__m128i)]);+  out[5] = loadu(&inputs[1][block_offset + 1 * sizeof(__m128i)]);+  out[6] = loadu(&inputs[2][block_offset + 1 * sizeof(__m128i)]);+  out[7] = loadu(&inputs[3][block_offset + 1 * sizeof(__m128i)]);+  out[8] = loadu(&inputs[0][block_offset + 2 * sizeof(__m128i)]);+  out[9] = loadu(&inputs[1][block_offset + 2 * sizeof(__m128i)]);+  out[10] = loadu(&inputs[2][block_offset + 2 * sizeof(__m128i)]);+  out[11] = loadu(&inputs[3][block_offset + 2 * sizeof(__m128i)]);+  out[12] = loadu(&inputs[0][block_offset + 3 * sizeof(__m128i)]);+  out[13] = loadu(&inputs[1][block_offset + 3 * sizeof(__m128i)]);+  out[14] = loadu(&inputs[2][block_offset + 3 * sizeof(__m128i)]);+  out[15] = loadu(&inputs[3][block_offset + 3 * sizeof(__m128i)]);+  for (size_t i = 0; i < 4; ++i) {+    _mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);+  }+  transpose_vecs(&out[0]);+  transpose_vecs(&out[4]);+  transpose_vecs(&out[8]);+  transpose_vecs(&out[12]);+}++INLINE void load_counters(uint64_t counter, bool increment_counter,+                          __m128i *out_lo, __m128i *out_hi) {+  const __m128i mask = _mm_set1_epi32(-(int32_t)increment_counter);+  const __m128i add0 = _mm_set_epi32(3, 2, 1, 0);+  const __m128i add1 = _mm_and_si128(mask, add0);+  __m128i l = _mm_add_epi32(_mm_set1_epi32((int32_t)counter), add1);+  __m128i carry = _mm_cmpgt_epi32(_mm_xor_si128(add1, _mm_set1_epi32(0x80000000)), +                                  _mm_xor_si128(   l, _mm_set1_epi32(0x80000000)));+  __m128i h = _mm_sub_epi32(_mm_set1_epi32((int32_t)(counter >> 32)), carry);+  *out_lo = l;+  *out_hi = h;+}++static+void blake3_hash4_sse41(const uint8_t *const *inputs, size_t blocks,+                        const uint32_t key[8], uint64_t counter,+                        bool increment_counter, uint8_t flags,+                        uint8_t flags_start, uint8_t flags_end, uint8_t *out) {+  __m128i h_vecs[8] = {+      set1(key[0]), set1(key[1]), set1(key[2]), set1(key[3]),+      set1(key[4]), set1(key[5]), set1(key[6]), set1(key[7]),+  };+  __m128i counter_low_vec, counter_high_vec;+  load_counters(counter, increment_counter, &counter_low_vec,+                &counter_high_vec);+  uint8_t block_flags = flags | flags_start;++  for (size_t block = 0; block < blocks; block++) {+    if (block + 1 == blocks) {+      block_flags |= flags_end;+    }+    __m128i block_len_vec = set1(BLAKE3_BLOCK_LEN);+    __m128i block_flags_vec = set1(block_flags);+    __m128i msg_vecs[16];+    transpose_msg_vecs(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);++    __m128i v[16] = {+        h_vecs[0],       h_vecs[1],        h_vecs[2],     h_vecs[3],+        h_vecs[4],       h_vecs[5],        h_vecs[6],     h_vecs[7],+        set1(IV[0]),     set1(IV[1]),      set1(IV[2]),   set1(IV[3]),+        counter_low_vec, counter_high_vec, block_len_vec, block_flags_vec,+    };+    round_fn(v, msg_vecs, 0);+    round_fn(v, msg_vecs, 1);+    round_fn(v, msg_vecs, 2);+    round_fn(v, msg_vecs, 3);+    round_fn(v, msg_vecs, 4);+    round_fn(v, msg_vecs, 5);+    round_fn(v, msg_vecs, 6);+    h_vecs[0] = xorv(v[0], v[8]);+    h_vecs[1] = xorv(v[1], v[9]);+    h_vecs[2] = xorv(v[2], v[10]);+    h_vecs[3] = xorv(v[3], v[11]);+    h_vecs[4] = xorv(v[4], v[12]);+    h_vecs[5] = xorv(v[5], v[13]);+    h_vecs[6] = xorv(v[6], v[14]);+    h_vecs[7] = xorv(v[7], v[15]);++    block_flags = flags;+  }++  transpose_vecs(&h_vecs[0]);+  transpose_vecs(&h_vecs[4]);+  // The first four vecs now contain the first half of each output, and the+  // second four vecs contain the second half of each output.+  storeu(h_vecs[0], &out[0 * sizeof(__m128i)]);+  storeu(h_vecs[4], &out[1 * sizeof(__m128i)]);+  storeu(h_vecs[1], &out[2 * sizeof(__m128i)]);+  storeu(h_vecs[5], &out[3 * sizeof(__m128i)]);+  storeu(h_vecs[2], &out[4 * sizeof(__m128i)]);+  storeu(h_vecs[6], &out[5 * sizeof(__m128i)]);+  storeu(h_vecs[3], &out[6 * sizeof(__m128i)]);+  storeu(h_vecs[7], &out[7 * sizeof(__m128i)]);+}++INLINE void hash_one_sse41(const uint8_t *input, size_t blocks,+                           const uint32_t key[8], uint64_t counter,+                           uint8_t flags, uint8_t flags_start,+                           uint8_t flags_end, uint8_t out[BLAKE3_OUT_LEN]) {+  uint32_t cv[8];+  memcpy(cv, key, BLAKE3_KEY_LEN);+  uint8_t block_flags = flags | flags_start;+  while (blocks > 0) {+    if (blocks == 1) {+      block_flags |= flags_end;+    }+    blake3_compress_in_place_sse41(cv, input, BLAKE3_BLOCK_LEN, counter,+                                   block_flags);+    input = &input[BLAKE3_BLOCK_LEN];+    blocks -= 1;+    block_flags = flags;+  }+  memcpy(out, cv, BLAKE3_OUT_LEN);+}++void blake3_hash_many_sse41(const uint8_t *const *inputs, size_t num_inputs,+                            size_t blocks, const uint32_t key[8],+                            uint64_t counter, bool increment_counter,+                            uint8_t flags, uint8_t flags_start,+                            uint8_t flags_end, uint8_t *out) {+  while (num_inputs >= DEGREE) {+    blake3_hash4_sse41(inputs, blocks, key, counter, increment_counter, flags,+                       flags_start, flags_end, out);+    if (increment_counter) {+      counter += DEGREE;+    }+    inputs += DEGREE;+    num_inputs -= DEGREE;+    out = &out[DEGREE * BLAKE3_OUT_LEN];+  }+  while (num_inputs > 0) {+    hash_one_sse41(inputs[0], blocks, key, counter, flags, flags_start,+                   flags_end, out);+    if (increment_counter) {+      counter += 1;+    }+    inputs += 1;+    num_inputs -= 1;+    out = &out[BLAKE3_OUT_LEN];+  }+}--- a/src/src/crypto/blake3_c.c+++ b/src/src/crypto/blake3_c.c@@ -1,292 +1,29 @@-/*- * Minimal scalar BLAKE3 (unkeyed, 256-bit output).- *- * This is a direct C transcription of the format's reference implementation.- * It is linked into vn to keep object-ID hashing practical during large- * imports. The pure Spectre implementation remains in blake3.sx for tests and- * for environments where the C object is not linked.- */+/* Thin adapter from the vein runtime to the vendored BLAKE3 C library. */++#include "blake3/blake3.h"  #include <stddef.h> #include <stdint.h>-#include <string.h>--static const uint32_t IV[8] = {-    0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au,-    0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u,-};--static const uint8_t MSG[16 * 7] = {-    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,-    2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8,-    3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1,-    10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6,-    12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4,-    9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7,-    11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13,-};--enum {-    CHUNK_START = 1,-    CHUNK_END = 2,-    PARENT = 4,-    ROOT = 8,-};--static inline uint32_t rotr32(uint32_t x, unsigned n) {-    return (x >> n) | (x << (32 - n));-}--static inline void g(uint32_t s[16], int a, int b, int c, int d, uint32_t mx, uint32_t my) {-    uint32_t x0 = s[a] + s[b] + mx;-    uint32_t x3 = rotr32(s[d] ^ x0, 16);-    uint32_t x2 = s[c] + x3;-    uint32_t x1 = rotr32(s[b] ^ x2, 12);-    x0 = x0 + x1 + my;-    x3 = rotr32(x3 ^ x0, 8);-    x2 = x2 + x3;-    x1 = rotr32(x1 ^ x2, 7);-    s[a] = x0;-    s[b] = x1;-    s[c] = x2;-    s[d] = x3;-}--static void compress_into(-    const uint32_t cv[8],-    const uint32_t block[16],-    uint64_t counter,-    uint32_t block_len,-    uint32_t flags,-    uint32_t out[16]-) {-    for (int i = 0; i < 8; i++) out[i] = cv[i];-    out[8] = IV[0];-    out[9] = IV[1];-    out[10] = IV[2];-    out[11] = IV[3];-    out[12] = (uint32_t)counter;-    out[13] = (uint32_t)(counter >> 32);-    out[14] = block_len;-    out[15] = flags;--    for (int r = 0; r < 7; r++) {-        const uint8_t *m = MSG + r * 16;-        g(out, 0, 4, 8, 12, block[m[0]], block[m[1]]);-        g(out, 1, 5, 9, 13, block[m[2]], block[m[3]]);-        g(out, 2, 6, 10, 14, block[m[4]], block[m[5]]);-        g(out, 3, 7, 11, 15, block[m[6]], block[m[7]]);-        g(out, 0, 5, 10, 15, block[m[8]], block[m[9]]);-        g(out, 1, 6, 11, 12, block[m[10]], block[m[11]]);-        g(out, 2, 7, 8, 13, block[m[12]], block[m[13]]);-        g(out, 3, 4, 9, 14, block[m[14]], block[m[15]]);-    }--    for (int i = 0; i < 8; i++) {-        uint32_t lo = out[i];-        uint32_t hi = out[i + 8];-        out[i] = lo ^ hi;-        out[i + 8] = hi ^ cv[i];-    }-}--static void load_block(const uint8_t *src, size_t len, uint32_t block[16]) {-    uint8_t tmp[64];-    memset(tmp, 0, sizeof(tmp));-    if (len > 0) memcpy(tmp, src, len);-    for (int i = 0; i < 16; i++) {-        block[i] = (uint32_t)tmp[i * 4]-            | ((uint32_t)tmp[i * 4 + 1] << 8)-            | ((uint32_t)tmp[i * 4 + 2] << 16)-            | ((uint32_t)tmp[i * 4 + 3] << 24);-    }-}--static void chunk_cv(-    const uint8_t *data,-    size_t len,-    uint64_t chunk_index,-    uint32_t cv[8],-    uint32_t final_block[16],-    uint32_t *final_flags,-    uint32_t *final_len,-    uint64_t *final_counter-) {-    size_t blocks = len == 0 ? 1 : (len + 63) / 64;-    for (int i = 0; i < 8; i++) cv[i] = IV[i];--    uint32_t state[16];-    for (size_t i = 0; i + 1 < blocks; i++) {-        uint32_t block[16];-        load_block(data + i * 64, 64, block);-        uint32_t flags = (i == 0) ? CHUNK_START : 0;-        compress_into(cv, block, chunk_index, 64, flags, state);-        for (int j = 0; j < 8; j++) cv[j] = state[j];-    }--    size_t last = blocks - 1;-    size_t last_len = len - last * 64;-    load_block(data + last * 64, last_len, final_block);-    *final_flags = CHUNK_END | ((last == 0) ? CHUNK_START : 0);-    *final_len = (uint32_t)last_len;-    *final_counter = chunk_index;-}--static void output_cv(-    const uint32_t cv[8],-    const uint32_t block[16],-    uint64_t counter,-    uint32_t block_len,-    uint32_t flags,-    uint32_t out_cv[8]-) {-    uint32_t state[16];-    compress_into(cv, block, counter, block_len, flags, state);-    for (int i = 0; i < 8; i++) out_cv[i] = state[i];-}--static void parent_cv(const uint32_t left[8], const uint32_t right[8], uint32_t out_cv[8]) {-    uint32_t block[16];-    for (int i = 0; i < 8; i++) block[i] = left[i];-    for (int i = 0; i < 8; i++) block[i + 8] = right[i];-    output_cv(IV, block, 0, 64, PARENT, out_cv);-}  void vein_blake3(const uint8_t *data, size_t len, uint8_t out[32]) {-    uint32_t stack[54][8];-    int depth = 0;-    uint64_t chunks = len == 0 ? 1 : (uint64_t)((len + 1023) / 1024);--    for (uint64_t i = 0; i + 1 < chunks; i++) {-        uint32_t cv[8], block[16], flags, block_len;-        uint64_t counter;-        chunk_cv(data + (size_t)i * 1024, 1024, i, cv, block, &flags, &block_len, &counter);-        uint32_t new_cv[8];-        output_cv(cv, block, counter, block_len, flags, new_cv);-        uint64_t total = i + 1;-        while ((total & 1) == 0) {-            depth--;-            parent_cv(stack[depth], new_cv, new_cv);-            total >>= 1;-        }-        memcpy(stack[depth], new_cv, sizeof(new_cv));-        depth++;-    }--    uint64_t last_index = chunks - 1;-    size_t last_off = (size_t)last_index * 1024;-    size_t last_len = len - last_off;-    uint32_t cv[8], block[16], flags, block_len;-    uint64_t counter;-    chunk_cv(data + last_off, last_len, last_index, cv, block, &flags, &block_len, &counter);--    uint32_t cur_cv[8];-    while (depth > 0) {-        output_cv(cv, block, counter, block_len, flags, cur_cv);-        depth--;-        for (int i = 0; i < 8; i++) {-            block[i] = stack[depth][i];-            block[i + 8] = cur_cv[i];-            cv[i] = IV[i];-        }-        flags = PARENT;-        block_len = 64;-        counter = 0;-    }--    uint32_t state[16];-    compress_into(cv, block, counter, block_len, flags | ROOT, state);-    for (int i = 0; i < 8; i++) {-        out[i * 4] = (uint8_t)(state[i] & 255);-        out[i * 4 + 1] = (uint8_t)((state[i] >> 8) & 255);-        out[i * 4 + 2] = (uint8_t)((state[i] >> 16) & 255);-        out[i * 4 + 3] = (uint8_t)((state[i] >> 24) & 255);-    }+    blake3_hasher hasher;+    blake3_hasher_init(&hasher);+    blake3_hasher_update(&hasher, data, len);+    blake3_hasher_finalize(&hasher, out, 32); }---typedef struct {-    uint32_t stack[54][8];-    int depth;-    uint8_t buf[1024];-    size_t buf_len;-    uint64_t chunk_counter;-} vein_b3_state;  size_t vein_blake3_state_size(void) {-    return sizeof(vein_b3_state);-}--static void state_merge(vein_b3_state *st, uint32_t cv[8]) {-    uint64_t total = st->chunk_counter + 1;-    while ((total & 1) == 0) {-        st->depth--;-        parent_cv(st->stack[st->depth], cv, cv);-        total >>= 1;-    }-    memcpy(st->stack[st->depth], cv, sizeof(uint32_t) * 8);-    st->depth++;-    st->chunk_counter++;-}--static void state_process_full(vein_b3_state *st, const uint8_t *data) {-    uint32_t cv[8], block[16], flags, block_len;-    uint64_t counter;-    chunk_cv(data, 1024, st->chunk_counter, cv, block, &flags, &block_len, &counter);-    uint32_t out_cv[8];-    output_cv(cv, block, counter, block_len, flags, out_cv);-    state_merge(st, out_cv);+    return sizeof(blake3_hasher); } -void vein_blake3_init(void *opaque) {-    vein_b3_state *st = (vein_b3_state *)opaque;-    st->depth = 0;-    st->buf_len = 0;-    st->chunk_counter = 0;+void vein_blake3_init(void *state) {+    blake3_hasher_init((blake3_hasher *)state); } -void vein_blake3_update(void *opaque, const uint8_t *data, size_t len) {-    vein_b3_state *st = (vein_b3_state *)opaque;-    while (len > 0) {-        if (st->buf_len == 1024) {-            state_process_full(st, st->buf);-            st->buf_len = 0;-        }-        size_t room = 1024 - st->buf_len;-        size_t take = len < room ? len : room;-        memcpy(st->buf + st->buf_len, data, take);-        st->buf_len += take;-        data += take;-        len -= take;-    }+void vein_blake3_update(void *state, const uint8_t *data, size_t len) {+    blake3_hasher_update((blake3_hasher *)state, data, len); }--void vein_blake3_final(void *opaque, uint8_t out[32]) {-    vein_b3_state *st = (vein_b3_state *)opaque;-    uint32_t cv[8], block[16], flags, block_len;-    uint64_t counter;-    chunk_cv(st->buf, st->buf_len, st->chunk_counter, cv, block, &flags, &block_len, &counter);--    uint32_t cur_cv[8];-    while (st->depth > 0) {-        output_cv(cv, block, counter, block_len, flags, cur_cv);-        st->depth--;-        for (int i = 0; i < 8; i++) {-            block[i] = st->stack[st->depth][i];-            block[i + 8] = cur_cv[i];-            cv[i] = IV[i];-        }-        flags = PARENT;-        block_len = 64;-        counter = 0;-    } -    uint32_t state[16];-    compress_into(cv, block, counter, block_len, flags | ROOT, state);-    for (int i = 0; i < 8; i++) {-        out[i * 4] = (uint8_t)(state[i] & 255);-        out[i * 4 + 1] = (uint8_t)((state[i] >> 8) & 255);-        out[i * 4 + 2] = (uint8_t)((state[i] >> 16) & 255);-        out[i * 4 + 3] = (uint8_t)((state[i] >> 24) & 255);-    }+void vein_blake3_final(void *state, uint8_t out[32]) {+    blake3_hasher_finalize((const blake3_hasher *)state, out, 32); }--- a/src/src/git/import.sx+++ b/src/src/git/import.sx@@ -62,6 +62,9 @@     new_trees: mut usize     new_blobs: mut usize     writer: mut ref writer.Writer+    time_read: mut i64+    time_chunk: mut i64+    time_write: mut i64 }  fn put_usize(map: mut oidmap.OidMap, oid: bytes.Bytes, value: usize) void = trust {@@ -122,7 +125,7 @@ fn peel_to_commit(git: object.GitRepo, start: bytes.Bytes) option[bytes.Bytes] = trust {     val current: mut bytes.Bytes = bytes.Bytes.dup(start)     for {-        val obj_opt = object.read(git, current)+        val obj_opt = object.read_fast(git, current)         val obj: mut object.Object = object.Object{             kind: 0,             body: bytes.empty(),@@ -182,7 +185,10 @@     if existing is some id {         return some dup_id(id)     }-    val obj_opt = object.read(ctx.git, oid)+    val blob_t0: i64 = time.now_ns()+    val obj_opt = object.read_fast(ctx.git, oid)+    val blob_t1: i64 = time.now_ns()+    ctx.time_read = ctx.time_read + (blob_t1 - blob_t0)     val obj: mut object.Object = object.Object{kind: 0, body: bytes.empty(), alloc: bytes.empty()}     match obj_opt {         ok o => {@@ -197,18 +203,31 @@         return none     }     val built: ref blob.BlobBuild = blob.build(obj.body.ptr, obj.body.len)-    val root: objid.Id = blob.root_id(built)+    val blob_t2: i64 = time.now_ns()+    ctx.time_chunk = ctx.time_chunk + (blob_t2 - blob_t1)+    val root: objid.Id = dup_id(blob.root_id(built))     for ro in built.objects {-        val _ = writer.add(ctx.writer, ro.type_byte, ro.body)+        if ro.id != none {+            val rid: objid.Id = objid.id(ro.id)+            val _ = writer.add_known(ctx.writer, ro.type_byte, rid, ro.body)+            val rip: mut ref void = ro.id as ref void+            @free(rip)+        } else {+            val rid2: objid.Id = writer.add(ctx.writer, ro.type_byte, ro.body)+            val rp2: mut ref void = rid2.ptr as ref void+            @free(rp2)+        }         val rp: mut ref void = ro as ref void         @free(rp)     }     @listfree(built.objects)+    val blob_t3: i64 = time.now_ns()+    ctx.time_write = ctx.time_write + (blob_t3 - blob_t2)     put_id(ctx.blobs_map, oid, root)     ctx.new_blobs++     val out: objid.Id = dup_id(root)-    val root_ptr: mut ref void = built.root as ref void-    @free(root_ptr)+    val rp3: mut ref void = root.ptr as ref void+    @free(rp3)     @free(built as ref void)     object.free_object(obj)     return some out@@ -219,7 +238,7 @@     if existing is some id {         return some dup_id(id)     }-    val obj_opt = object.read(ctx.git, oid)+    val obj_opt = object.read_fast(ctx.git, oid)     val obj: mut object.Object = object.Object{kind: 0, body: bytes.empty(), alloc: bytes.empty()}     match obj_opt {         ok o => {@@ -632,6 +651,7 @@     }      val refset: refs.RefSet = refs.read(git.git_dir)+    val timing_start: i64 = time.now_ns()     val writer_opt = writer.open(repo)     val wref: mut ref writer.Writer = none     match writer_opt {@@ -657,7 +677,10 @@         new_commits: 0,         new_trees: 0,         new_blobs: 0,-        writer: wref+        writer: wref,+        time_read: 0,+        time_chunk: 0,+        time_write: 0     }      val branches: mut list[ref ImportNamedTarget] = lists.new[ref ImportNamedTarget]()@@ -703,7 +726,7 @@             continue         }         oidmap.put(seen, oid, bytes.empty())-        val obj_opt = object.read(ctx.git, oid)+        val obj_opt = object.read_fast(ctx.git, oid)         val obj: mut object.Object = object.Object{             kind: 0,             body: bytes.empty(),@@ -754,6 +777,8 @@     }     @listfree(stack)     oidmap.free(seen)+    val timing_collect: i64 = time.now_ns()+    stdio.eprint("timing: collect {d} ms\n", {(timing_collect - timing_start) / 1000000})      val n: usize = @len(records)     val indegree: ref u32 = @alloc(n * 4 + 1) as ref u32@@ -863,11 +888,19 @@         return 1     } +    val timing_topo: i64 = time.now_ns()+    stdio.eprint("timing: topo {d} ms\n", {(timing_topo - timing_collect) / 1000000})     val build_failed: mut bool = false-    // Import all trees and blobs reachable from the new commits.+    // Import trees and blobs in topological order so git delta bases are read+    // before the objects that depend on them.     val ci: mut usize = 0-    for ci < @len(records) {-        val rec_opt4 = @get(records, ci)+    for ci < @len(order) {+        val idx_opt = @get(order, ci)+        val tree_idx: mut usize = 0+        if idx_opt is some tv {+            tree_idx = tv+        }+        val rec_opt4 = @get(records, tree_idx)         if rec_opt4 is some rec4 {             val tr = import_tree(ctx, rec4.parsed.tree)             if tr is none {@@ -881,6 +914,8 @@         }         ci++     }+    val timing_trees: i64 = time.now_ns()+    stdio.eprint("timing: trees {d} ms\n", {(timing_trees - timing_topo) / 1000000})      val oi: mut usize = 0     for oi < @len(order) {@@ -963,6 +998,8 @@         }         oi++     }+    val timing_commits: i64 = time.now_ns()+    stdio.eprint("timing: commits {d} ms\n", {(timing_commits - timing_trees) / 1000000})     @listfree(order)     for cl4 in children {         @listfree(cl4)@@ -1143,7 +1180,10 @@         @free(up)     } +    stdio.eprint("timing: blob read {d} ms, chunk {d} ms, write {d} ms\n", {ctx.time_read / 1000000, ctx.time_chunk / 1000000, ctx.time_write / 1000000})+    val timing_before_finish: i64 = time.now_ns()     val wrote: bool = writer.finish(ctx.writer)+    stdio.eprint("timing: state {d} ms, finish {d} ms\n", {(timing_before_finish - timing_commits) / 1000000, (time.now_ns() - timing_before_finish) / 1000000})     if !wrote {         stdio.eprint("vn: failed to write object pack\n", {})         if old_encoded.ptr != none {--- a/src/src/git/object.sx+++ b/src/src/git/object.sx@@ -63,6 +63,12 @@         val p: mut ref void = o.alloc.ptr as ref void         @free(p)     }+}++type GitCacheEntry = {+    offset: mut u64+    kind: mut u8+    body: mut bytes.Bytes }  type GitPack = {@@ -71,7 +77,17 @@     count: mut usize     oids: mut ref u8     offsets: mut ref u8+    cache: mut list[ref GitCacheEntry]+    cache_map: mut ref u32+    cache_map_cap: mut usize+    cache_bytes: mut usize+    cache_count: mut usize }++val CACHE_MIN: usize = 0+val CACHE_MAX_BYTES: usize = 8589934592+val CACHE_MAX_ENTRIES: usize = 131072+val CACHE_MAP_CAP: usize = 262144  pub type GitRepo = {     git_dir: mut str.StringView@@ -203,7 +219,6 @@ fn inflate_exact(input: bytes.Bytes, expected: usize) option[bytes.Bytes] = trust {     val cap: usize = expected + 1     val dst: ref u8 = @alloc(cap) as ref u8-    @memset(dst as ref void, 0, cap)     val dest_len: ref u64 = @alloc(8) as ref u64     @store64(dest_len as ref void, expected as u64)     val rc: i32 = uncompress(@@ -474,7 +489,224 @@     return @load64(@ptradd(pack.offsets, (i * 8) as i64)) as u64 } -fn read_at(repo: GitRepo, pack: ref GitPack, start: u64) result[Object, GitError] = trust {+fn cache_hash(offset: u64) usize = {+    val h: u64 = offset * 11400714819323198485+    return (h >> 32) as usize+}++fn cache_slot(pack: ref GitPack, offset: u64) i64 = trust {+    if pack.cache_map_cap == 0 {+        return -1+    }+    val mask: usize = pack.cache_map_cap - 1+    val start: usize = cache_hash(offset) & mask+    val idx: mut usize = start+    for {+        val slot: ref u32 = @ptradd(pack.cache_map as ref void, (idx * 4) as i64) as ref u32+        val v: u32 = @load32(slot) as u32+        if v == 0 {+            return -1+        }+        if v != 4294967295 {+            val ei: usize = (v - 1) as usize+            val o = @get(pack.cache, ei)+            if o is some e {+                if e.body.ptr != none && e.offset == offset {+                    return ei as i64+                }+            }+        }+        idx = (idx + 1) & mask+        if idx == start {+            return -1+        }+    }+    return -1+}++fn cache_map_put(pack: ref GitPack, offset: u64, entry: usize) void = trust {+    if pack.cache_map_cap == 0 {+        return+    }+    val mask: usize = pack.cache_map_cap - 1+    val start: usize = cache_hash(offset) & mask+    val idx: mut usize = start+    val tomb: mut i64 = -1+    for {+        val slot: ref u32 = @ptradd(pack.cache_map as ref void, (idx * 4) as i64) as ref u32+        val v: u32 = @load32(slot) as u32+        if v == 0 {+            val at: mut usize = idx+            if tomb >= 0 {+                at = tomb as usize+            }+            val at_slot: ref u32 = @ptradd(pack.cache_map as ref void, (at * 4) as i64) as ref u32+            @store32(at_slot as ref void, (entry + 1) as i32)+            return+        }+        if v == 4294967295 && tomb < 0 {+            tomb = idx as i64+        }+        idx = (idx + 1) & mask+        if idx == start {+            if tomb >= 0 {+                val tslot: ref u32 = @ptradd(pack.cache_map as ref void, ((tomb as usize) * 4) as i64) as ref u32+                @store32(tslot as ref void, (entry + 1) as i32)+            }+            return+        }+    }+}++fn cache_map_remove(pack: ref GitPack, offset: u64) void = trust {+    if pack.cache_map_cap == 0 {+        return+    }+    val mask: usize = pack.cache_map_cap - 1+    val start: usize = cache_hash(offset) & mask+    val idx: mut usize = start+    for {+        val slot: ref u32 = @ptradd(pack.cache_map as ref void, (idx * 4) as i64) as ref u32+        val v: u32 = @load32(slot) as u32+        if v == 0 {+            return+        }+        if v != 4294967295 {+            val ei: usize = (v - 1) as usize+            val o = @get(pack.cache, ei)+            if o is some e {+                if e.offset == offset {+                    @store32(slot as ref void, -1)+                    return+                }+            }+        }+        idx = (idx + 1) & mask+        if idx == start {+            return+        }+    }+}++fn cache_lookup(pack: ref GitPack, offset: u64) option[Object] = trust {+    val slot: i64 = cache_slot(pack, offset)+    if slot < 0 {+        return none+    }+    val o = @get(pack.cache, slot as usize)+    if o is some e {+        return some Object{kind: e.kind, body: e.body, alloc: bytes.empty()}+    }+    return none+}++fn cache_evict_one(pack: mut ref GitPack) void = trust {+    val victim: mut i64 = -1+    val oldest_off: mut u64 = 18446744073709551615+    val i: mut usize = 0+    for i < @len(pack.cache) {+        val o = @get(pack.cache, i)+        if o is some e {+            if e.body.ptr != none && e.offset < oldest_off {+                oldest_off = e.offset+                victim = i as i64+            }+        }+        i+++    }+    if victim < 0 {+        return+    }+    val vo = @get(pack.cache, victim as usize)+    if vo is some e2 {+        if e2.body.ptr != none {+            pack.cache_bytes = pack.cache_bytes - e2.body.len+            pack.cache_count = pack.cache_count - 1+            cache_map_remove(pack, e2.offset)+            val bp: mut ref void = e2.body.ptr as ref void+            @free(bp)+            e2.body = bytes.empty()+        }+    }+}++fn cache_store(pack: mut ref GitPack, offset: u64, kind: u8, body: bytes.Bytes) bool = trust {+    if body.len < CACHE_MIN || body.len > CACHE_MAX_BYTES {+        return false+    }+    if cache_slot(pack, offset) >= 0 {+        return false+    }+    for pack.cache_count > 0 {+        if pack.cache_bytes + body.len <= CACHE_MAX_BYTES+        && pack.cache_count < CACHE_MAX_ENTRIES {+            break+        }+        cache_evict_one(pack)+    }+    if pack.cache_bytes + body.len > CACHE_MAX_BYTES {+        return false+    }+    val e: mut ref GitCacheEntry = @alloc(@sizeof(GitCacheEntry)) as ref GitCacheEntry+    e.offset = offset+    e.kind = kind+    e.body = body+    val slot: mut i64 = -1+    val i: mut usize = 0+    for i < @len(pack.cache) {+        val o = @get(pack.cache, i)+        if o is some x {+            if x.body.ptr == none {+                slot = i as i64+                break+            }+        }+        i+++    }+    if slot >= 0 {+        @set(pack.cache, slot as usize, e)+    } else {+        @append(pack.cache, e)+    }+    pack.cache_bytes = pack.cache_bytes + body.len+    pack.cache_count = pack.cache_count + 1+    val map_slot: usize = @len(pack.cache) - 1+    if slot >= 0 {+        cache_map_put(pack, offset, slot as usize)+    } else {+        cache_map_put(pack, offset, map_slot)+    }+    return true+}++fn cache_free(pack: ref GitPack) void = trust {+    if pack.cache_map != none {+        @free(pack.cache_map as ref void)+    }+    for e in pack.cache {+        if e.body.ptr != none {+            val bp: mut ref void = e.body.ptr as ref void+            @free(bp)+        }+        val ep: mut ref void = e as ref void+        @free(ep)+    }+    @listfree(pack.cache)+}++fn read_at(+    repo: GitRepo,+    pack: mut ref GitPack,+    start: u64,+    verify: bool,+    use_cache: bool+) result[Object, GitError] = trust {+    if use_cache {+        val cached = cache_lookup(pack, start)+        if cached is some c {+            return ok c+        }+    }     val data: bytes.Bytes = pack.data     val pos: mut usize = start as usize     if pos >= data.len {@@ -540,6 +772,9 @@     }      if kind >= KIND_COMMIT && kind <= KIND_TAG {+        if use_cache && cache_store(pack, start, kind, inflated) {+            return ok Object{kind: kind, body: inflated, alloc: bytes.empty()}+        }         return ok Object{kind: kind, body: inflated, alloc: inflated}     }     if kind != KIND_OFS_DELTA && kind != KIND_REF_DELTA {@@ -553,7 +788,7 @@             @free(inflated.ptr as ref void)             return err GitError.Corrupt         }-        val br = read_at(repo, pack, base_off)+        val br = read_at(repo, pack, base_off, verify, use_cache)         match br {             ok b => {                 base = b@@ -564,7 +799,7 @@             }         }     } else {-        val br2 = read(repo, base_oid)+        val br2 = read_impl(repo, base_oid, verify, use_cache)         match br2 {             ok b => {                 base = b@@ -591,10 +826,13 @@     val base_kind: u8 = base.kind     free_object(base)     @free(inflated.ptr as ref void)+    if use_cache && cache_store(pack, start, base_kind, merged) {+        return ok Object{kind: base_kind, body: merged, alloc: bytes.empty()}+    }     return ok Object{kind: base_kind, body: merged, alloc: merged} } -fn read_loose(repo: GitRepo, oid: bytes.Bytes) option[Object] = trust {+fn read_loose(repo: GitRepo, oid: bytes.Bytes, verify: bool) option[Object] = trust {     val hex: ref u8 = oid_hex(oid)     val objroot: str.StringView = files.join(repo.git_dir, str.from_cstr(c"objects"))     val dirname: str.StringView = str.view(hex, 2)@@ -666,7 +904,7 @@         return none     }     val o = Object{kind: kind, body: body, alloc: inflated}-    if !verify_hash(oid, kind, body) {+    if verify && !verify_hash(oid, kind, body) {         free_object(o)         return none     }@@ -673,23 +911,24 @@     return some o } -pub fn read(repo: GitRepo, oid: bytes.Bytes) result[Object, GitError] = trust {+fn read_impl(+    repo: GitRepo,+    oid: bytes.Bytes,+    verify: bool,+    use_cache: bool+) result[Object, GitError] = trust {     pre {         oid_len: oid.len == 20-    }-    val loose = read_loose(repo, oid)-    if loose is some o {-        return ok o     }     val i: mut usize = 0     for i < @len(repo.packs) {-        val p = pack_at(repo.packs, i)+        val p: mut ref GitPack = pack_at(repo.packs, i)         val found = find_oid(p, oid)         if found is some idx {-            val r = read_at(repo, p, offset_at(p, idx))+            val r = read_at(repo, p, offset_at(p, idx), verify, use_cache)             match r {                 ok o => {-                    if !verify_hash(oid, o.kind, o.body) {+                    if verify && !verify_hash(oid, o.kind, o.body) {                         free_object(o)                         return err GitError.HashMismatch                     }@@ -701,8 +940,22 @@             }         }         i+++    }+    val loose = read_loose(repo, oid, verify)+    if loose is some o2 {+        return ok o2     }     return err GitError.NotFound+}++pub fn read(repo: GitRepo, oid: bytes.Bytes) result[Object, GitError] = trust {+    return read_impl(repo, oid, true, false)+}++/// Read an object without recomputing its hash. The pack trailer and index are+/// already verified when the pack is opened, so this is safe for local import.+pub fn read_fast(repo: GitRepo, oid: bytes.Bytes) result[Object, GitError] = trust {+    return read_impl(repo, oid, false, true) }  fn pack_at(packs: list[ref GitPack], i: usize) ref GitPack = {@@ -833,6 +1086,12 @@     p.count = n     p.oids = oids     p.offsets = offsets+    p.cache = lists.new[ref GitCacheEntry]()+    p.cache_map_cap = CACHE_MAP_CAP+    p.cache_map = @alloc(CACHE_MAP_CAP * 4) as ref u32+    @memset(p.cache_map as ref void, 0, CACHE_MAP_CAP * 4)+    p.cache_bytes = 0+    p.cache_count = 0     return ok p } @@ -954,6 +1213,7 @@  pub fn free_pack(p: ref GitPack) void = trust {     files.free_buffer(p.data)+    cache_free(p)     if p.oids != none {         @free(p.oids as ref void)     }--- a/src/src/io/files.sx+++ b/src/src/io/files.sx@@ -122,7 +122,7 @@             return none         }     }-    if size < 0 || size > 1073741824 {+    if size < 0 || size > 8589934592 {         return none     }     if size == 0 {--- a/src/src/object/blob.sx+++ b/src/src/object/blob.sx@@ -240,7 +240,9 @@         val body: bytes.Bytes = buf.Buf.view(b)         val root: objid.Id = objid.object_id(types.BLOB, body)         val owned: bytes.Bytes = bytes.Bytes.dup(body)-        @append(objects, raw.raw(types.BLOB, owned))+        val ro: mut ref raw.RawObject = raw.raw(types.BLOB, owned)+        ro.id = root.ptr+        @append(objects, ro)         buf.Buf.free(b)         return new_build(root.ptr, owned, objects)     }@@ -251,7 +253,9 @@         val n: usize = fastcdc.next_len(@ptradd(data, off as i64), len - off)         val chunk: bytes.Bytes = bytes.Bytes.slice(bytes.bytes(data, len), off, off + n)         val cid: objid.Id = objid.object_id(types.CHUNK, chunk)-        @append(objects, raw.raw(types.CHUNK, bytes.Bytes.dup(chunk)))+        val ro: mut ref raw.RawObject = raw.raw(types.CHUNK, bytes.Bytes.dup(chunk))+        ro.id = cid.ptr+        @append(objects, ro)         @append(level, new_node(cid, n as u64))         off = off + n     }@@ -272,7 +276,9 @@             val body: bytes.Bytes = buf.Buf.view(b)             val nid: objid.Id = objid.object_id(types.BLOB, body)             val owned: bytes.Bytes = bytes.Bytes.dup(body)-            @append(objects, raw.raw(types.BLOB, owned))+            val ro: mut ref raw.RawObject = raw.raw(types.BLOB, owned)+            ro.id = nid.ptr+            @append(objects, ro)             @append(next, new_node(nid, sum_group(level, g)))             last_body = owned             buf.Buf.free(b)--- a/src/src/object/raw.sx+++ b/src/src/object/raw.sx@@ -6,6 +6,7 @@ pub type RawObject = {     type_byte: mut u8     body: mut bytes.Bytes+    id: mut ref u8 }  pub fn raw(type_byte: u8, body: bytes.Bytes) ref RawObject = {@@ -12,6 +13,7 @@     val o: mut ref RawObject = @alloc(@sizeof(RawObject)) as ref RawObject     o.type_byte = type_byte     o.body = body+    o.id = none     return o } @@ -20,6 +22,10 @@         if o.body.ptr != none {             val body_ptr: mut ref void = o.body.ptr as ref void             trust @free(body_ptr)+        }+        if o.id != none {+            val id_ptr: mut ref void = o.id as ref void+            trust @free(id_ptr)         }         val obj_ptr: mut ref void = o as ref void         trust @free(obj_ptr)--- a/src/src/store/writer.sx+++ b/src/src/store/writer.sx@@ -5,7 +5,10 @@ /// the file, and the index is written from the collected record metadata.  val str     = use("std/string")+val stdio   = use("std/stdio") val time    = use("std/time")+val allocators = use("std/allocators")+val thread  = use("std/thread") val bytes   = use("../core/bytes.sx") val buf     = use("../core/buf.sx") val lists   = use("../core/lists.sx")@@ -18,6 +21,22 @@ val files   = use("../io/files.sx") val native  = use("../io/native.sx") val b3      = use("../crypto/blake3.sx")++extern (C) fn sysconf(name: i32) i64 = "sysconf"++type PendingRecord = {+    id: mut objid.Id+    kind: mut u8+    body: mut bytes.Bytes+    stored: mut bytes.Bytes+    comp: mut u8+}++type CompressJob = {+    items: mut list[ref PendingRecord]+    start: mut usize+    step: mut usize+}  pub type Writer = {     tmp_path: mut str.StringView@@ -27,8 +46,120 @@     count: mut usize     offset: mut u64     metas: mut list[ref records.RecordMeta]+    items: mut list[ref PendingRecord]+    hasher: mut ref void     failed: mut bool     seen: mut idmap.IdSet+}++fn worker_count() usize = trust {+    when linux {+        val n: i64 = sysconf(84)+        if n > 0 {+            return n as usize+        }+    }+    when macos {+        val n2: i64 = sysconf(58)+        if n2 > 0 {+            return n2 as usize+        }+    }+    return 4+}++fn free_items(items: list[ref PendingRecord]) void = trust {+    for r in items {+        if r.body.ptr != none {+            val bp: mut ref void = r.body.ptr as ref void+            @free(bp)+        }+        if r.comp == 1 && r.stored.ptr != none {+            val sp: mut ref void = r.stored.ptr as ref void+            @free(sp)+        }+        if r.id.ptr != none {+            val ip: mut ref void = r.id.ptr as ref void+            @free(ip)+        }+        val rp: mut ref void = r as ref void+        @free(rp)+    }+    @listfree(items)+}++fn compress_worker(arg: ref void) ref void = trust {+    val job: ref CompressJob = arg as ref CompressJob+    val i: mut usize = job.start+    for i < @len(job.items) {+        val item = @get(job.items, i)+        if item is some r {+            val compressed: bytes.Bytes = zstd.compress(r.body, zstd.DEFAULT_LEVEL)+            if compressed.len > 0 {+                r.comp = 1+                r.stored = compressed+            } else {+                r.comp = 0+                r.stored = r.body+            }+        }+        i = i + job.step+    }+    return none+}++fn compress_items(items: list[ref PendingRecord]) void = trust {+    val n: usize = @len(items)+    if n == 0 {+        return+    }+    val workers: mut usize = worker_count()+    if workers < 1 {+        workers = 1+    }+    if workers > n {+        workers = n+    }+    val ac: allocators.HeapAllocator = @zeroed+    val thread_refs: mut list[ref void] = lists.new[ref void]()+    val jobs: mut list[ref CompressJob] = lists.new[ref CompressJob]()+    val w: mut usize = 0+    for w < workers {+        val job: mut ref CompressJob = @alloc(@sizeof(CompressJob)) as ref CompressJob+        job.items = items+        job.start = w+        job.step = workers+        @append(jobs, job)+        val tr = thread.Thread.new[allocators.HeapAllocator](ac, compress_worker, job as ref void)+        match tr {+            ok t => {+                @append(thread_refs, t)+            }+            err _ => {+                compress_worker(job as ref void)+            }+        }+        w+++    }+    val j: mut usize = 0+    for j < @len(thread_refs) {+        val tr = @get(thread_refs, j)+        if tr is some t {+            val joined = thread.Thread.join[allocators.HeapAllocator](ac, t)+            match joined {+                ok _ => {+                }+                err _ => {+                }+            }+        }+        j+++    }+    for job in jobs {+        val _ = job+    }+    @listfree(jobs)+    @listfree(thread_refs) }  fn copy_id(id: objid.Id) objid.Id = trust {@@ -68,8 +199,8 @@     buf.Buf.put_u32_le(header, 1)     buf.Buf.put_u64_le(header, 0)     val wrote: bool = native.write_fd_all(fd, buf.Buf.view(header))-    buf.Buf.free(header)     if !wrote {+        buf.Buf.free(header)         native.close_fd(fd)         val _ = native.delete_file(tmp)         files.free_path(tmpdir)@@ -85,14 +216,18 @@     w.count = 0     w.offset = 16     w.metas = lists.new[ref records.RecordMeta]()+    w.items = lists.new[ref PendingRecord]()+    w.hasher = @alloc(b3.hasher_state_size())+    b3.hasher_init(w.hasher)+    b3.hasher_update(w.hasher, buf.Buf.view(header).ptr, buf.Buf.view(header).len)+    buf.Buf.free(header)     w.failed = false     w.seen = idmap.new(1024)     return some w } -/// Append one object. Takes ownership of `body`.-pub fn add(w: mut Writer, kind: u8, body: bytes.Bytes) objid.Id = trust {-    val id: objid.Id = objid.object_id(kind, body)+/// Append one object with a precomputed ID. Takes ownership of `body`.+pub fn add_known(w: mut Writer, kind: u8, id: objid.Id, body: bytes.Bytes) objid.Id = trust {     if w.failed {         if body.ptr != none {             val bp: mut ref void = body.ptr as ref void@@ -109,42 +244,20 @@         return id     }     val _ = idmap.put(w.seen, id_view)-    val compressed: bytes.Bytes = zstd.compress(body, zstd.DEFAULT_LEVEL)-    val comp: mut u8 = 0-    val stored: mut bytes.Bytes = body-    if compressed.len > 0 {-        comp = 1-        stored = compressed-    }-    val header: mut buf.Buf = buf.new(64)-    buf.Buf.put_id(header, id.ptr)-    buf.Buf.put_u8(header, kind)-    buf.Buf.put_u8(header, comp)-    buf.Buf.put_varint(header, body.len as u64)-    buf.Buf.put_varint(header, stored.len as u64)-    val start: u64 = w.offset-    val wrote_header: bool = native.write_fd_all(w.fd, buf.Buf.view(header))-    val wrote_body: mut bool = false-    if wrote_header {-        wrote_body = native.write_fd_all(w.fd, stored)-    }-    if wrote_header && wrote_body {-        @append(w.metas, records.record_meta(copy_id(id), start, kind, body.len as u64))-        w.count++-        w.offset = start + header.len as u64 + stored.len as u64-    } else {-        w.failed = true-    }-    buf.Buf.free(header)-    if compressed.len > 0 {-        val cp: mut ref void = compressed.ptr as ref void-        @free(cp)-    }-    if body.ptr != none {-        val bp: mut ref void = body.ptr as ref void-        @free(bp)-    }+    val r: mut ref PendingRecord = @alloc(@sizeof(PendingRecord)) as ref PendingRecord+    r.id = copy_id(id)+    r.kind = kind+    r.body = body+    r.stored = bytes.empty()+    r.comp = 0+    @append(w.items, r)     return id+}++/// Append one object. Takes ownership of `body`.+pub fn add(w: mut Writer, kind: u8, body: bytes.Bytes) objid.Id = trust {+    val id: objid.Id = objid.object_id(kind, body)+    return add_known(w, kind, id, body) }  fn free_writer(w: mut ref Writer) void = trust {@@ -151,8 +264,17 @@     if w.fd >= 0 {         native.close_fd(w.fd)         w.fd = -1+    }+    if w.hasher != none {+        @free(w.hasher as ref void)+        w.hasher = none     }     free_meta_list(w.metas)+    if @len(w.items) > 0 {+        free_items(w.items)+    } else {+        @listfree(w.items)+    }     idmap.free(w.seen)     if w.tmp_path.ptr != none {         files.free_path(w.tmp_path)@@ -180,48 +302,75 @@  /// Finish the pack and write its index. `w` is consumed. pub fn finish(w: mut ref Writer) bool = trust {+    if w.failed {+        abort(w)+        return false+    }+    val tf0: i64 = time.now_ns()+    compress_items(w.items)+    stdio.eprint("writer: compress {d} ms\n", {(time.now_ns() - tf0) / 1000000})+    val i: mut usize = 0+    val out: mut buf.Buf = buf.new(8388608)+    val header: mut buf.Buf = buf.new(64)+    for i < @len(w.items) {+        val item = @get(w.items, i)+        if item is some r {+            buf.Buf.reset(header)+            buf.Buf.put_id(header, r.id.ptr)+            buf.Buf.put_u8(header, r.kind)+            buf.Buf.put_u8(header, r.comp)+            buf.Buf.put_varint(header, r.body.len as u64)+            buf.Buf.put_varint(header, r.stored.len as u64)+            val start: u64 = w.offset+            buf.Buf.put_bytes(out, buf.Buf.view(header))+            buf.Buf.put_bytes(out, r.stored)+            @append(w.metas, records.record_meta(copy_id(r.id), start, r.kind, r.body.len as u64))+            w.count+++            w.offset = start + header.len as u64 + r.stored.len as u64+            if out.len >= 8388608 {+                b3.hasher_update(w.hasher, buf.Buf.view(out).ptr, out.len)+                if !native.write_fd_all(w.fd, buf.Buf.view(out)) {+                    w.failed = true+                }+                buf.Buf.reset(out)+            }+        }+        i+++    }+    if out.len > 0 {+        b3.hasher_update(w.hasher, buf.Buf.view(out).ptr, out.len)+        if !native.write_fd_all(w.fd, buf.Buf.view(out)) {+            w.failed = true+        }+        buf.Buf.reset(out)+    }+    buf.Buf.free(out)+    buf.Buf.free(header)+    stdio.eprint("writer: write {d} ms\n", {(time.now_ns() - tf0) / 1000000})     if w.failed {         abort(w)         return false+    }+    if @len(w.items) > 0 {+        free_items(w.items)+    } else {+        @listfree(w.items)     }+    w.items = lists.new[ref PendingRecord]()++    val tf1: i64 = time.now_ns()     val count_buf: mut buf.Buf = buf.new(8)     buf.Buf.put_u64_le(count_buf, w.count as u64)     val patched: bool = native.pwrite_fd_all(w.fd, 8, buf.Buf.view(count_buf))     buf.Buf.free(count_buf)     if !patched {-        abort(w)-        return false-    }-    if !native.seek_set(w.fd, 0) {         abort(w)         return false-    }--    val state_size: usize = b3.hasher_state_size()-    val state: ref void = @alloc(state_size) as ref void-    b3.hasher_init(state)-    val chunk: ref u8 = @alloc(1048576) as ref u8-    val hashed: mut bool = true-    for {-        val n: i64 = native.read_fd(w.fd, chunk, 1048576)-        if n < 0 {-            hashed = false-            break-        }-        if n == 0 {-            break-        }-        b3.hasher_update(state, chunk, n as usize)     }     val digest: ref u8 = @alloc(32) as ref u8-    b3.hasher_final(state, digest)-    @free(state)-    @free(chunk as ref void)-    if !hashed {-        @free(digest as ref void)-        abort(w)-        return false-    }+    b3.hasher_final(w.hasher, digest)+    stdio.eprint("writer: hash {d} ms\n", {(time.now_ns() - tf1) / 1000000})+    val tf2: i64 = time.now_ns()     val trailer: bytes.Bytes = bytes.bytes(digest, 32)     val wrote_trailer: bool = native.pwrite_fd_all(w.fd, w.offset as i64, trailer)     val synced: bool = native.fsync_fd(w.fd)@@ -267,6 +416,7 @@         w.packs_dir,         index_bytes     )+    stdio.eprint("writer: index {d} ms\n", {(time.now_ns() - tf2) / 1000000})     val idx_buf: mut ref void = index_bytes.ptr as ref void     @free(idx_buf)     files.free_path(idx_tmp_name)--- a/src/src/store/zstd.sx+++ b/src/src/store/zstd.sx@@ -24,7 +24,7 @@ ) usize = "ZSTD_decompress" extern (C) fn ZSTD_isError(code: usize) u32 = "ZSTD_isError" -pub val DEFAULT_LEVEL: i32 = 3+pub val DEFAULT_LEVEL: i32 = 1  /// Compress `src`. Returns an empty span when compression fails or does not /// reduce the size.--- a/src/sx.mod+++ b/src/sx.mod@@ -3,7 +3,7 @@ spectre_version "v0.0.18"  build release {-    action "mkdir -p $OUT && cc -O3 -c ./src/crypto/blake3_c.c -o $OUT/blake3_c.o && spectre ./src/main.sx -release -link-obj:$OUT/blake3_c.o -o ./vn"+    action "mkdir -p $OUT && ./build-native.sh $OUT && spectre ./src/main.sx -release -link-obj:$OUT/vein_native.o -o ./vn"     flags "-release"     output "./vn"     windows {@@ -12,7 +12,7 @@ }  build dev {-    action "mkdir -p $OUT && cc -O3 -c ./src/crypto/blake3_c.c -o $OUT/blake3_c.o && spectre ./src/main.sx -link-obj:$OUT/blake3_c.o -o ./vn-dev"+    action "mkdir -p $OUT && ./build-native.sh $OUT && spectre ./src/main.sx -link-obj:$OUT/vein_native.o -o ./vn-dev"     output "./vn-dev"     windows {         output "vn-dev.exe"