try make blake2bip work

This commit is contained in:
John Tromp 2016-10-26 21:52:46 -04:00
parent 1788096b83
commit 6473d85b1f
10 changed files with 804 additions and 4 deletions

View File

@ -28,7 +28,7 @@ hash1: equi.h dev_miner.h dev_miner.cpp blake2b/asm/zcblake2_avx2.o Makefile
equidev: equi.h equi_dev_miner.h equi_dev_miner.cpp Makefile
$(GPP) -DATOMIC equi_dev_miner.cpp blake/blake2b.cpp blake2-avx2/blake2bip.c -o equidev
equidev1: equi.h equi_dev_miner.h equi_dev_miner.cpp Makefile
equidev1: equi.h equi_dev_miner.h equi_dev_miner.cpp blake2-avx2/blake2bip.c Makefile
$(GPP) equi_dev_miner.cpp blake/blake2b.cpp blake2-avx2/blake2bip.c -o equidev1
eqcuda: equi_miner.cu equi.h blake2b.cu Makefile

121
blake2-avx2/LICENSE Normal file
View File

@ -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.

48
blake2-avx2/Makefile Normal file
View File

@ -0,0 +1,48 @@
CFLAGS=-std=c89 -Ofast -march=haswell -Wall -Wextra -pedantic -Wfatal-errors -Wno-long-long
all: check
bench: check
$(SHELL) bench.sh
check: blake2b-check-1.exe blake2b-check-2.exe blake2b-check-3.exe blake2bp-check-1.exe blake2bp-check-2.exe blake2bp-check-3.exe blake2sp-check-1.exe blake2sp-check-2.exe blake2sp-check-3.exe
blake2b-check-1.exe: blake2b.c
$(CC) $(CFLAGS) -DBLAKE2B_SELFTEST -DPERMUTE_WITH_NOTHING -o $@ $<
./$@
blake2b-check-2.exe: blake2b.c
$(CC) $(CFLAGS) -DBLAKE2B_SELFTEST -DPERMUTE_WITH_SHUFFLES -o $@ $<
./$@
blake2b-check-3.exe: blake2b.c
$(CC) $(CFLAGS) -DBLAKE2B_SELFTEST -DPERMUTE_WITH_GATHER -o $@ $<
./$@
blake2bp-check-1.exe: blake2bp.c
$(CC) $(CFLAGS) -DBLAKE2BP_SELFTEST -DPERMUTE_WITH_NOTHING -o $@ $<
./$@
blake2bp-check-2.exe: blake2bp.c
$(CC) $(CFLAGS) -DBLAKE2BP_SELFTEST -DPERMUTE_WITH_SHUFFLES -o $@ $<
./$@
blake2bp-check-3.exe: blake2bp.c
$(CC) $(CFLAGS) -DBLAKE2BP_SELFTEST -DPERMUTE_WITH_GATHER -o $@ $<
./$@
blake2sp-check-1.exe: blake2sp.c
$(CC) $(CFLAGS) -DBLAKE2SP_SELFTEST -DPERMUTE_WITH_NOTHING -o $@ $<
./$@
blake2sp-check-2.exe: blake2sp.c
$(CC) $(CFLAGS) -DBLAKE2SP_SELFTEST -DPERMUTE_WITH_SHUFFLES -o $@ $<
./$@
blake2sp-check-3.exe: blake2sp.c
$(CC) $(CFLAGS) -DBLAKE2SP_SELFTEST -DPERMUTE_WITH_GATHER -o $@ $<
./$@
clean:
rm -f *.exe

9
blake2-avx2/README.md Normal file
View File

@ -0,0 +1,9 @@
# BLAKE2 AVX2 implementations
This is **experimental** code implementing [BLAKE2](https://blake2.net/) using the AVX2 instruction set present in the Intel [Haswell](https://en.wikipedia.org/wiki/Haswell_%28microarchitecture%29) and later microarchitectures.
It currently implements BLAKE2b, BLAKE2bp, and BLAKE2sp using 3 similar but slightly different approaches: one lets the compiler choose how to permute the message, another one does it manually, and the final one uses the gather instructions introduced with AVX2. Current recorded speeds for long messages are:
- 3.19 cycles per byte on Haswell for BLAKE2b;
- 1.45 cycles per byte on Haswell for BLAKE2bp;
- 1.56 cycles per byte on Haswell for BLAKE2sp.

156
blake2-avx2/blake2.h Normal file
View File

@ -0,0 +1,156 @@
/*
BLAKE2 reference source code package - optimized C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#pragma once
#ifndef __BLAKE2_H__
#define __BLAKE2_H__
#include <stddef.h>
#include <stdint.h>
#if defined(_MSC_VER)
#define ALIGN(x) __declspec(align(x))
#else
#define ALIGN(x) __attribute__ ((__aligned__(x)))
#endif
#if defined(__cplusplus)
extern "C" {
#endif
enum blake2s_constant
{
BLAKE2S_BLOCKBYTES = 64,
BLAKE2S_OUTBYTES = 32,
BLAKE2S_KEYBYTES = 32,
BLAKE2S_SALTBYTES = 8,
BLAKE2S_PERSONALBYTES = 8
};
enum blake2b_constant
{
BLAKE2B_BLOCKBYTES = 128,
BLAKE2B_OUTBYTES = 64,
BLAKE2B_KEYBYTES = 64,
BLAKE2B_SALTBYTES = 16,
BLAKE2B_PERSONALBYTES = 16
};
#pragma pack(push, 1)
typedef struct __blake2s_param
{
uint8_t digest_length; // 1
uint8_t key_length; // 2
uint8_t fanout; // 3
uint8_t depth; // 4
uint32_t leaf_length; // 8
uint8_t node_offset[6];// 14
uint8_t node_depth; // 15
uint8_t inner_length; // 16
// uint8_t reserved[0];
uint8_t salt[BLAKE2S_SALTBYTES]; // 24
uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
} blake2s_param;
ALIGN( 64 ) typedef struct __blake2s_state
{
uint32_t h[8];
uint32_t t[2];
uint32_t f[2];
uint8_t buf[2 * BLAKE2S_BLOCKBYTES];
size_t buflen;
uint8_t last_node;
} blake2s_state;
typedef struct __blake2b_param
{
uint8_t digest_length; // 1
uint8_t key_length; // 2
uint8_t fanout; // 3
uint8_t depth; // 4
uint32_t leaf_length; // 8
uint64_t node_offset; // 16
uint8_t node_depth; // 17
uint8_t inner_length; // 18
uint8_t reserved[14]; // 32
uint8_t salt[BLAKE2B_SALTBYTES]; // 48
uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64
} blake2b_param;
ALIGN( 64 ) typedef struct __blake2b_state
{
uint64_t h[8];
uint8_t buf[BLAKE2B_BLOCKBYTES];
uint16_t counter;
uint8_t buflen;
uint8_t lastblock;
} blake2b_state;
ALIGN( 64 ) typedef struct __blake2sp_state
{
blake2s_state S[8][1];
blake2s_state R[1];
uint8_t buf[8 * BLAKE2S_BLOCKBYTES];
size_t buflen;
} blake2sp_state;
ALIGN( 64 ) typedef struct __blake2bp_state
{
blake2b_state S[4][1];
blake2b_state R[1];
uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
size_t buflen;
} blake2bp_state;
#pragma pack(pop)
// Streaming API
int blake2s_init( blake2s_state *S, const uint8_t outlen );
int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen );
int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen );
int blake2b_init( blake2b_state *S, const uint8_t outlen );
int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen );
int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen );
int blake2sp_init( blake2sp_state *S, const uint8_t outlen );
int blake2sp_init_key( blake2sp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
int blake2sp_update( blake2sp_state *S, const uint8_t *in, uint64_t inlen );
int blake2sp_final( blake2sp_state *S, uint8_t *out, uint8_t outlen );
int blake2bp_init( blake2bp_state *S, const uint8_t outlen );
int blake2bp_init_key( blake2bp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
int blake2bp_update( blake2bp_state *S, const uint8_t *in, uint64_t inlen );
int blake2bp_final( blake2bp_state *S, uint8_t *out, uint8_t outlen );
// Simple API
int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
int blake2b_long(uint8_t *out, const void *in, const uint32_t outlen, const uint64_t inlen);
int blake2sp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
int blake2bp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
static inline int blake2( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen )
{
return blake2b( out, in, key, outlen, inlen, keylen );
}
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -0,0 +1,60 @@
#ifndef BLAKE2_AVX2_BLAKE2B_COMMON_H
#define BLAKE2_AVX2_BLAKE2B_COMMON_H
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <immintrin.h>
#include "blake2.h"
#define LOAD128(p) _mm_load_si128( (__m128i *)(p) )
#define STORE128(p,r) _mm_store_si128((__m128i *)(p), r)
#define LOADU128(p) _mm_loadu_si128( (__m128i *)(p) )
#define STOREU128(p,r) _mm_storeu_si128((__m128i *)(p), r)
#define LOAD(p) _mm256_load_si256( (__m256i *)(p) )
#define STORE(p,r) _mm256_store_si256((__m256i *)(p), r)
#define LOADU(p) _mm256_loadu_si256( (__m256i *)(p) )
#define STOREU(p,r) _mm256_storeu_si256((__m256i *)(p), r)
#if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
#if defined(_MSC_VER)
#define INLINE __inline
#elif defined(__GNUC__)
#define INLINE __inline__
#else
#define INLINE
#endif
#else
#define INLINE inline
#endif
static INLINE uint64_t LOADU64(void const * p) {
uint64_t v;
memcpy(&v, p, sizeof v);
return v;
}
#define ROTATE16 _mm256_setr_epi8( 2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9, \
2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9 )
#define ROTATE24 _mm256_setr_epi8( 3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10, \
3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10 )
#define ADD(a, b) _mm256_add_epi64(a, b)
#define SUB(a, b) _mm256_sub_epi64(a, b)
#define XOR(a, b) _mm256_xor_si256(a, b)
#define AND(a, b) _mm256_and_si256(a, b)
#define OR(a, b) _mm256_or_si256(a, b)
#define ROT32(x) _mm256_shuffle_epi32((x), _MM_SHUFFLE(2, 3, 0, 1))
#define ROT24(x) _mm256_shuffle_epi8((x), ROTATE24)
#define ROT16(x) _mm256_shuffle_epi8((x), ROTATE16)
#define ROT63(x) _mm256_or_si256(_mm256_srli_epi64((x), 63), ADD((x), (x)))
#endif

View File

@ -0,0 +1,54 @@
#ifndef BLAKE2_AVX2_BLAKE2B_LOAD_AVX2_SIMPLE_H
#define BLAKE2_AVX2_BLAKE2B_LOAD_AVX2_SIMPLE_H
#define BLAKE2B_LOAD_MSG_0_1(b0) b0 = _mm256_set_epi64x(m6, m4, m2, m0);
#define BLAKE2B_LOAD_MSG_0_2(b0) b0 = _mm256_set_epi64x(m7, m5, m3, m1);
#define BLAKE2B_LOAD_MSG_0_3(b0) b0 = _mm256_set_epi64x(m14, m12, m10, m8);
#define BLAKE2B_LOAD_MSG_0_4(b0) b0 = _mm256_set_epi64x(m15, m13, m11, m9);
#define BLAKE2B_LOAD_MSG_1_1(b0) b0 = _mm256_set_epi64x(m13, m9, m4, m14);
#define BLAKE2B_LOAD_MSG_1_2(b0) b0 = _mm256_set_epi64x(m6, m15, m8, m10);
#define BLAKE2B_LOAD_MSG_1_3(b0) b0 = _mm256_set_epi64x(m5, m11, m0, m1);
#define BLAKE2B_LOAD_MSG_1_4(b0) b0 = _mm256_set_epi64x(m3, m7, m2, m12);
#define BLAKE2B_LOAD_MSG_2_1(b0) b0 = _mm256_set_epi64x(m15, m5, m12, m11);
#define BLAKE2B_LOAD_MSG_2_2(b0) b0 = _mm256_set_epi64x(m13, m2, m0, m8);
#define BLAKE2B_LOAD_MSG_2_3(b0) b0 = _mm256_set_epi64x(m9, m7, m3, m10);
#define BLAKE2B_LOAD_MSG_2_4(b0) b0 = _mm256_set_epi64x(m4, m1, m6, m14);
#define BLAKE2B_LOAD_MSG_3_1(b0) b0 = _mm256_set_epi64x(m11, m13, m3, m7);
#define BLAKE2B_LOAD_MSG_3_2(b0) b0 = _mm256_set_epi64x(m14, m12, m1, m9);
#define BLAKE2B_LOAD_MSG_3_3(b0) b0 = _mm256_set_epi64x(m15, m4, m5, m2);
#define BLAKE2B_LOAD_MSG_3_4(b0) b0 = _mm256_set_epi64x(m8, m0, m10, m6);
#define BLAKE2B_LOAD_MSG_4_1(b0) b0 = _mm256_set_epi64x(m10, m2, m5, m9);
#define BLAKE2B_LOAD_MSG_4_2(b0) b0 = _mm256_set_epi64x(m15, m4, m7, m0);
#define BLAKE2B_LOAD_MSG_4_3(b0) b0 = _mm256_set_epi64x(m3, m6, m11, m14);
#define BLAKE2B_LOAD_MSG_4_4(b0) b0 = _mm256_set_epi64x(m13, m8, m12, m1);
#define BLAKE2B_LOAD_MSG_5_1(b0) b0 = _mm256_set_epi64x(m8, m0, m6, m2);
#define BLAKE2B_LOAD_MSG_5_2(b0) b0 = _mm256_set_epi64x(m3, m11, m10, m12);
#define BLAKE2B_LOAD_MSG_5_3(b0) b0 = _mm256_set_epi64x(m1, m15, m7, m4);
#define BLAKE2B_LOAD_MSG_5_4(b0) b0 = _mm256_set_epi64x(m9, m14, m5, m13);
#define BLAKE2B_LOAD_MSG_6_1(b0) b0 = _mm256_set_epi64x(m4, m14, m1, m12);
#define BLAKE2B_LOAD_MSG_6_2(b0) b0 = _mm256_set_epi64x(m10, m13, m15, m5);
#define BLAKE2B_LOAD_MSG_6_3(b0) b0 = _mm256_set_epi64x(m8, m9, m6, m0);
#define BLAKE2B_LOAD_MSG_6_4(b0) b0 = _mm256_set_epi64x(m11, m2, m3, m7);
#define BLAKE2B_LOAD_MSG_7_1(b0) b0 = _mm256_set_epi64x(m3, m12, m7, m13);
#define BLAKE2B_LOAD_MSG_7_2(b0) b0 = _mm256_set_epi64x(m9, m1, m14, m11);
#define BLAKE2B_LOAD_MSG_7_3(b0) b0 = _mm256_set_epi64x(m2, m8, m15, m5);
#define BLAKE2B_LOAD_MSG_7_4(b0) b0 = _mm256_set_epi64x(m10, m6, m4, m0);
#define BLAKE2B_LOAD_MSG_8_1(b0) b0 = _mm256_set_epi64x(m0, m11, m14, m6);
#define BLAKE2B_LOAD_MSG_8_2(b0) b0 = _mm256_set_epi64x(m8, m3, m9, m15);
#define BLAKE2B_LOAD_MSG_8_3(b0) b0 = _mm256_set_epi64x(m10, m1, m13, m12);
#define BLAKE2B_LOAD_MSG_8_4(b0) b0 = _mm256_set_epi64x(m5, m4, m7, m2);
#define BLAKE2B_LOAD_MSG_9_1(b0) b0 = _mm256_set_epi64x(m1, m7, m8, m10);
#define BLAKE2B_LOAD_MSG_9_2(b0) b0 = _mm256_set_epi64x(m5, m6, m4, m2);
#define BLAKE2B_LOAD_MSG_9_3(b0) b0 = _mm256_set_epi64x(m13, m3, m9, m15);
#define BLAKE2B_LOAD_MSG_9_4(b0) b0 = _mm256_set_epi64x(m0, m12, m14, m11);
#define BLAKE2B_LOAD_MSG_10_1(b0) b0 = _mm256_set_epi64x(m6, m4, m2, m0);
#define BLAKE2B_LOAD_MSG_10_2(b0) b0 = _mm256_set_epi64x(m7, m5, m3, m1);
#define BLAKE2B_LOAD_MSG_10_3(b0) b0 = _mm256_set_epi64x(m14, m12, m10, m8);
#define BLAKE2B_LOAD_MSG_10_4(b0) b0 = _mm256_set_epi64x(m15, m13, m11, m9);
#define BLAKE2B_LOAD_MSG_11_1(b0) b0 = _mm256_set_epi64x(m13, m9, m4, m14);
#define BLAKE2B_LOAD_MSG_11_2(b0) b0 = _mm256_set_epi64x(m6, m15, m8, m10);
#define BLAKE2B_LOAD_MSG_11_3(b0) b0 = _mm256_set_epi64x(m5, m11, m0, m1);
#define BLAKE2B_LOAD_MSG_11_4(b0) b0 = _mm256_set_epi64x(m3, m7, m2, m12);
#endif

344
blake2-avx2/blake2bip.c Normal file
View File

@ -0,0 +1,344 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "blake2.h"
#include "blake2b-common.h"
#include "blake2bip.h"
ALIGN(64) static const uint64_t blake2b_IV[8] = {
UINT64_C(0x6A09E667F3BCC908), UINT64_C(0xBB67AE8584CAA73B),
UINT64_C(0x3C6EF372FE94F82B), UINT64_C(0xA54FF53A5F1D36F1),
UINT64_C(0x510E527FADE682D1), UINT64_C(0x9B05688C2B3E6C1F),
UINT64_C(0x1F83D9ABFB41BD6B), UINT64_C(0x5BE0CD19137E2179),
};
ALIGN(64) static const uint32_t blake2b_sigma[12][16] = {
{ 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480},
{448, 320, 128, 256, 288, 480, 416, 192, 32, 384, 0, 64, 352, 224, 160, 96},
{352, 256, 384, 0, 160, 64, 480, 416, 320, 448, 96, 192, 224, 32, 288, 128},
{224, 288, 96, 32, 416, 384, 352, 448, 64, 192, 160, 320, 128, 0, 480, 256},
{288, 0, 160, 224, 64, 128, 320, 480, 448, 32, 352, 384, 192, 256, 96, 416},
{ 64, 384, 192, 320, 0, 352, 256, 96, 128, 416, 224, 160, 480, 448, 32, 288},
{384, 160, 32, 480, 448, 416, 128, 320, 0, 224, 192, 96, 288, 64, 256, 352},
{416, 352, 224, 448, 384, 32, 96, 288, 160, 0, 480, 128, 256, 192, 64, 320},
{192, 480, 448, 288, 352, 96, 0, 256, 384, 64, 416, 224, 32, 128, 320, 160},
{320, 64, 256, 128, 224, 192, 32, 160, 480, 352, 288, 448, 96, 384, 416, 0},
{ 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480},
{448, 320, 128, 256, 288, 480, 416, 192, 32, 384, 0, 64, 352, 224, 160, 96},
};
#define BLAKE2B_G1_V1(a, b, c, d, m) do { \
a = ADD(a, m); \
a = ADD(a, b); d = XOR(d, a); d = ROT32(d); \
c = ADD(c, d); b = XOR(b, c); b = ROT24(b); \
} while(0)
#define BLAKE2B_G2_V1(a, b, c, d, m) do { \
a = ADD(a, m); \
a = ADD(a, b); d = XOR(d, a); d = ROT16(d); \
c = ADD(c, d); b = XOR(b, c); b = ROT63(b); \
} while(0)
#define BLAKE2B_DIAG_V1(a, b, c, d) do { \
d = _mm256_permute4x64_epi64(d, _MM_SHUFFLE(2,1,0,3)); \
c = _mm256_permute4x64_epi64(c, _MM_SHUFFLE(1,0,3,2)); \
b = _mm256_permute4x64_epi64(b, _MM_SHUFFLE(0,3,2,1)); \
} while(0)
#define BLAKE2B_UNDIAG_V1(a, b, c, d) do { \
d = _mm256_permute4x64_epi64(d, _MM_SHUFFLE(0,3,2,1)); \
c = _mm256_permute4x64_epi64(c, _MM_SHUFFLE(1,0,3,2)); \
b = _mm256_permute4x64_epi64(b, _MM_SHUFFLE(2,1,0,3)); \
} while(0)
#if defined(PERMUTE_WITH_SHUFFLES)
#include "blake2b-load-avx2.h"
#elif defined(PERMUTE_WITH_GATHER)
#else
#include "blake2b-load-avx2-simple.h"
#endif
#if defined(PERMUTE_WITH_GATHER)
ALIGN(64) static const uint32_t indices[12][16] = {
{ 0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15},
{14, 4, 9, 13,10, 8, 15, 6, 1, 0, 11, 5,12, 2, 7, 3},
{11, 12, 5, 15, 8, 0, 2, 13,10, 3, 7, 9,14, 6, 1, 4},
{ 7, 3, 13, 11, 9, 1, 12, 14, 2, 5, 4, 15, 6, 10, 0, 8},
{ 9, 5, 2, 10, 0, 7, 4, 15,14, 11, 6, 3, 1, 12, 8, 13},
{ 2, 6, 0, 8,12, 10, 11, 3, 4, 7, 15, 1,13, 5, 14, 9},
{12, 1, 14, 4, 5, 15, 13, 10, 0, 6, 9, 8, 7, 3, 2, 11},
{13, 7, 12, 3,11, 14, 1, 9, 5, 15, 8, 2, 0, 4, 6, 10},
{ 6, 14, 11, 0,15, 9, 3, 8,12, 13, 1, 10, 2, 7, 4, 5},
{10, 8, 7, 1, 2, 4, 6, 5,15, 9, 3, 13,11, 14, 12, 0},
{ 0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15},
{14, 4, 9, 13,10, 8, 15, 6, 1, 0, 11, 5,12, 2, 7, 3},
};
#define BLAKE2B_ROUND_V1(a, b, c, d, r, m) do { \
__m256i b0; \
b0 = _mm256_i32gather_epi64((void *)(m), LOAD128(&indices[r][ 0]), 8); \
BLAKE2B_G1_V1(a, b, c, d, b0); \
b0 = _mm256_i32gather_epi64((void *)(m), LOAD128(&indices[r][ 4]), 8); \
BLAKE2B_G2_V1(a, b, c, d, b0); \
BLAKE2B_DIAG_V1(a, b, c, d); \
b0 = _mm256_i32gather_epi64((void *)(m), LOAD128(&indices[r][ 8]), 8); \
BLAKE2B_G1_V1(a, b, c, d, b0); \
b0 = _mm256_i32gather_epi64((void *)(m), LOAD128(&indices[r][12]), 8); \
BLAKE2B_G2_V1(a, b, c, d, b0); \
BLAKE2B_UNDIAG_V1(a, b, c, d); \
} while(0)
#define BLAKE2B_ROUNDS_V1(a, b, c, d, m) do { \
int i; \
for(i = 0; i < 12; ++i) { \
BLAKE2B_ROUND_V1(a, b, c, d, i, m); \
} \
} while(0)
#else /* !PERMUTE_WITH_GATHER */
#define BLAKE2B_ROUND_V1(a, b, c, d, r, m) do { \
__m256i b0; \
BLAKE2B_LOAD_MSG_ ##r ##_1(b0); \
BLAKE2B_G1_V1(a, b, c, d, b0); \
BLAKE2B_LOAD_MSG_ ##r ##_2(b0); \
BLAKE2B_G2_V1(a, b, c, d, b0); \
BLAKE2B_DIAG_V1(a, b, c, d); \
BLAKE2B_LOAD_MSG_ ##r ##_3(b0); \
BLAKE2B_G1_V1(a, b, c, d, b0); \
BLAKE2B_LOAD_MSG_ ##r ##_4(b0); \
BLAKE2B_G2_V1(a, b, c, d, b0); \
BLAKE2B_UNDIAG_V1(a, b, c, d); \
} while(0)
#define BLAKE2B_ROUNDS_V1(a, b, c, d, m) do { \
BLAKE2B_ROUND_V1(a, b, c, d, 0, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 1, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 2, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 3, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 4, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 5, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 6, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 7, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 8, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 9, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 10, (m)); \
BLAKE2B_ROUND_V1(a, b, c, d, 11, (m)); \
} while(0)
#endif
#if defined(PERMUTE_WITH_GATHER)
#define DECLARE_MESSAGE_WORDS(m)
#elif defined(PERMUTE_WITH_SHUFFLES)
#define DECLARE_MESSAGE_WORDS(m) \
const __m256i m0 = _mm256_broadcastsi128_si256(LOADU128((m) + 0)); \
const __m256i m1 = _mm256_broadcastsi128_si256(LOADU128((m) + 16)); \
const __m256i m2 = _mm256_broadcastsi128_si256(LOADU128((m) + 32)); \
const __m256i m3 = _mm256_broadcastsi128_si256(LOADU128((m) + 48)); \
const __m256i m4 = _mm256_broadcastsi128_si256(LOADU128((m) + 64)); \
const __m256i m5 = _mm256_broadcastsi128_si256(LOADU128((m) + 80)); \
const __m256i m6 = _mm256_broadcastsi128_si256(LOADU128((m) + 96)); \
const __m256i m7 = _mm256_broadcastsi128_si256(LOADU128((m) + 112)); \
__m256i t0, t1;
#else
#define DECLARE_MESSAGE_WORDS(m) \
const uint64_t m0 = LOADU64((m) + 0); \
const uint64_t m1 = LOADU64((m) + 8); \
const uint64_t m2 = LOADU64((m) + 16); \
const uint64_t m3 = LOADU64((m) + 24); \
const uint64_t m4 = LOADU64((m) + 32); \
const uint64_t m5 = LOADU64((m) + 40); \
const uint64_t m6 = LOADU64((m) + 48); \
const uint64_t m7 = LOADU64((m) + 56); \
const uint64_t m8 = LOADU64((m) + 64); \
const uint64_t m9 = LOADU64((m) + 72); \
const uint64_t m10 = LOADU64((m) + 80); \
const uint64_t m11 = LOADU64((m) + 88); \
const uint64_t m12 = LOADU64((m) + 96); \
const uint64_t m13 = LOADU64((m) + 104); \
const uint64_t m14 = LOADU64((m) + 112); \
const uint64_t m15 = LOADU64((m) + 120);
#endif
#define BLAKE2B_COMPRESS_V1(a, b, m, t0, t1, f0, f1) do { \
DECLARE_MESSAGE_WORDS(m) \
const __m256i iv0 = a; \
const __m256i iv1 = b; \
__m256i c = LOAD(&blake2b_IV[0]); \
__m256i d = XOR( \
LOAD(&blake2b_IV[4]), \
_mm256_set_epi64x(f1, f0, t1, t0) \
); \
BLAKE2B_ROUNDS_V1(a, b, c, d, m); \
a = XOR(a, c); \
b = XOR(b, d); \
a = XOR(a, iv0); \
b = XOR(b, iv1); \
} while(0)
#define BLAKE2B_G_V4(m, r, i, a, b, c, d) do { \
a = ADD(a, LOAD((uint8_t const *)(m) + blake2b_sigma[r][2*i+0])); \
a = ADD(a, b); d = XOR(d, a); d = ROT32(d); \
c = ADD(c, d); b = XOR(b, c); b = ROT24(b); \
a = ADD(a, LOAD((uint8_t const *)(m) + blake2b_sigma[r][2*i+1])); \
a = ADD(a, b); d = XOR(d, a); d = ROT16(d); \
c = ADD(c, d); b = XOR(b, c); b = ROT63(b); \
} while(0)
#define BLAKE2B_ROUND_V4(v, m, r) do { \
BLAKE2B_G_V4(m, r, 0, v[ 0], v[ 4], v[ 8], v[12]); \
BLAKE2B_G_V4(m, r, 1, v[ 1], v[ 5], v[ 9], v[13]); \
BLAKE2B_G_V4(m, r, 2, v[ 2], v[ 6], v[10], v[14]); \
BLAKE2B_G_V4(m, r, 3, v[ 3], v[ 7], v[11], v[15]); \
BLAKE2B_G_V4(m, r, 4, v[ 0], v[ 5], v[10], v[15]); \
BLAKE2B_G_V4(m, r, 5, v[ 1], v[ 6], v[11], v[12]); \
BLAKE2B_G_V4(m, r, 6, v[ 2], v[ 7], v[ 8], v[13]); \
BLAKE2B_G_V4(m, r, 7, v[ 3], v[ 4], v[ 9], v[14]); \
} while(0)
#if defined(PERMUTE_WITH_GATHER)
#define BLAKE2B_LOADMSG_V4(w, m) do { \
int i; \
for(i = 0; i < 16; ++i) { \
w[i] = _mm256_i32gather_epi64( \
(const void *)((m) + i * sizeof(uint64_t)), \
_mm_set_epi32(48, 32, 16, 0), \
sizeof(uint64_t) \
); \
} \
} while(0)
#else
#define BLAKE2B_PACK_MSG_V4(w, m) do { \
__m256i t0, t1, t2, t3; \
t0 = _mm256_unpacklo_epi64(m[ 0], m[ 4]); \
t1 = _mm256_unpackhi_epi64(m[ 0], m[ 4]); \
t2 = _mm256_unpacklo_epi64(m[ 8], m[12]); \
t3 = _mm256_unpackhi_epi64(m[ 8], m[12]); \
w[ 0] = _mm256_permute2x128_si256(t0, t2, 0x20); \
w[ 2] = _mm256_permute2x128_si256(t0, t2, 0x31); \
w[ 1] = _mm256_permute2x128_si256(t1, t3, 0x20); \
w[ 3] = _mm256_permute2x128_si256(t1, t3, 0x31); \
t0 = _mm256_unpacklo_epi64(m[ 1], m[ 5]); \
t1 = _mm256_unpackhi_epi64(m[ 1], m[ 5]); \
t2 = _mm256_unpacklo_epi64(m[ 9], m[13]); \
t3 = _mm256_unpackhi_epi64(m[ 9], m[13]); \
w[ 4] = _mm256_permute2x128_si256(t0, t2, 0x20); \
w[ 6] = _mm256_permute2x128_si256(t0, t2, 0x31); \
w[ 5] = _mm256_permute2x128_si256(t1, t3, 0x20); \
w[ 7] = _mm256_permute2x128_si256(t1, t3, 0x31); \
t0 = _mm256_unpacklo_epi64(m[ 2], m[ 6]); \
t1 = _mm256_unpackhi_epi64(m[ 2], m[ 6]); \
t2 = _mm256_unpacklo_epi64(m[10], m[14]); \
t3 = _mm256_unpackhi_epi64(m[10], m[14]); \
w[ 8] = _mm256_permute2x128_si256(t0, t2, 0x20); \
w[10] = _mm256_permute2x128_si256(t0, t2, 0x31); \
w[ 9] = _mm256_permute2x128_si256(t1, t3, 0x20); \
w[11] = _mm256_permute2x128_si256(t1, t3, 0x31); \
t0 = _mm256_unpacklo_epi64(m[ 3], m[ 7]); \
t1 = _mm256_unpackhi_epi64(m[ 3], m[ 7]); \
t2 = _mm256_unpacklo_epi64(m[11], m[15]); \
t3 = _mm256_unpackhi_epi64(m[11], m[15]); \
w[12] = _mm256_permute2x128_si256(t0, t2, 0x20); \
w[14] = _mm256_permute2x128_si256(t0, t2, 0x31); \
w[13] = _mm256_permute2x128_si256(t1, t3, 0x20); \
w[15] = _mm256_permute2x128_si256(t1, t3, 0x31); \
} while(0)
#define BLAKE2B_LOADMSG_V4(w, m) do { \
__m256i t[16]; \
int i; \
for(i = 0; i < 16; ++i) { \
t[i] = LOADU((m) + i * 32); \
} \
BLAKE2B_PACK_MSG_V4(w, t); \
} while(0)
#endif
#define BLAKE2B_UNPACK_STATE_V4(u, v) do { \
__m256i t0, t1, t2, t3; \
t0 = _mm256_unpacklo_epi64(v[0], v[1]); \
t1 = _mm256_unpackhi_epi64(v[0], v[1]); \
t2 = _mm256_unpacklo_epi64(v[2], v[3]); \
t3 = _mm256_unpackhi_epi64(v[2], v[3]); \
u[0] = _mm256_permute2x128_si256(t0, t2, 0x20); \
u[2] = _mm256_permute2x128_si256(t1, t3, 0x20); \
u[4] = _mm256_permute2x128_si256(t0, t2, 0x31); \
u[6] = _mm256_permute2x128_si256(t1, t3, 0x31); \
t0 = _mm256_unpacklo_epi64(v[4], v[5]); \
t1 = _mm256_unpackhi_epi64(v[4], v[5]); \
t2 = _mm256_unpacklo_epi64(v[6], v[7]); \
t3 = _mm256_unpackhi_epi64(v[6], v[7]); \
u[1] = _mm256_permute2x128_si256(t0, t2, 0x20); \
u[3] = _mm256_permute2x128_si256(t1, t3, 0x20); \
u[5] = _mm256_permute2x128_si256(t0, t2, 0x31); \
u[7] = _mm256_permute2x128_si256(t1, t3, 0x31); \
} while(0)
#define BLAKE2B_COMPRESS_V4(v, m, counter, flag) do { \
__m256i iv[8], w[16]; \
int i, r; \
for(i = 0; i < 8; ++i) { \
iv[i] = v[i]; \
} \
v[ 8] = _mm256_set1_epi64x(blake2b_IV[0]); \
v[ 9] = _mm256_set1_epi64x(blake2b_IV[1]); \
v[10] = _mm256_set1_epi64x(blake2b_IV[2]); \
v[11] = _mm256_set1_epi64x(blake2b_IV[3]); \
v[12] = XOR(_mm256_set1_epi64x(blake2b_IV[4]), counter); \
v[13] = _mm256_set1_epi64x(blake2b_IV[5]); \
v[14] = XOR(_mm256_set1_epi64x(blake2b_IV[6]), flag); \
v[15] = XOR(_mm256_set1_epi64x(blake2b_IV[7]), flag); \
BLAKE2B_LOADMSG_V4(w, m); \
for(r = 0; r < 12; ++r) { \
BLAKE2B_ROUND_V4(v, w, r); \
} \
for(i = 0; i < 8; ++i) { \
v[i] = XOR(XOR(v[i], v[i+8]), iv[i]); \
} \
} while(0)
void blake2bip_final(const blake2b_state *S, uchar *out, u32 blockidx) {
ALIGN(64) uint8_t buffer[4 * BLAKE2B_BLOCKBYTES]; // 4 * 128
memset(buffer, 0, 4 * BLAKE2B_BLOCKBYTES); // zero whole buffer
memcpy(buffer, S->buf, S->buflen);
uint32_t b = htole32(4 * blockidx);
memcpy(buffer + S->buflen, &b, 4);
size_t inlen = S->buflen + 4;
buffer[0*128+inlen-4] += 0;
memcpy(buffer + 128, buffer, inlen);
buffer[1*128+inlen-4] += 1;
memcpy(buffer + 256, buffer, inlen);
buffer[2*128+inlen-4] += 2;
memcpy(buffer + 384, buffer, inlen);
buffer[3*128+inlen-4] += 3;
__m256i v[16], s[8]; // 16 * 32, 8 * 32
union {
__m256i v;
uint64_t w[4];
} counter;
int i;
for(i = 0; i < 8; ++i) {
v[i] = _mm256_set1_epi64x(S->h[i]); // initialize all 256/64 = 4 lanes
}
#if 0 // these look blake2bp specific
v[0] = XOR(v[0], _mm256_set1_epi64x(0x02040040UL));
v[1] = XOR(v[1], _mm256_set_epi64x(3, 2, 1, 0));
v[2] = XOR(v[2], _mm256_set1_epi64x(0x00004000UL));
#endif
__m256i x4 = _mm256_set_epi64x( 0, 0, 0,inlen);
__m256i f0 = _mm256_set_epi64x(~0, ~0, ~0, ~0);
counter.v = _mm256_add_epi64(counter.v, x4);
BLAKE2B_COMPRESS_V4(v, buffer, counter.v, f0);
BLAKE2B_UNPACK_STATE_V4(s, v);
// big loop obsoleted as it would if(!inlen) break
memcpy(out, s, 256); // instead of return blake2b_root(out, (void *)s);
}

11
blake2-avx2/blake2bip.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef BLAKE2_AVX2_BLAKE2BIP_H
#define BLAKE2_AVX2_BLAKE2BIP_H
#include <stddef.h>
typedef uint32_t u32;
typedef unsigned char uchar;
void blake2bip_final(const blake2b_state *midstate, uchar *hashout, u32 blockidx);
#endif

View File

@ -523,9 +523,6 @@ struct equi {
#endif
uchar hashes[BLAKESINPARALLEL * 64];
blake2b_state state = blake_ctx;
state->counter += state->buflen;
state->lastblock = 1;
memset(state->buf+state->buflen, 0, BLAKE2B_BLOCKBYTES-state->buflen); // Padding
for (u32 block = id; block < NBLOCKS; block += nthreads) {
blake2bip_final(&state, hashes, block);
#ifndef HASHONLY