Path: blob/master/sha3/mod_blakecoin.c
1299 views
/* $Id: blake.c 252 2011-06-07 17:55:14Z tp $ */1/*2* BLAKECOIN implementation. (Stripped to 256 bits only)3*4* ==========================(LICENSE BEGIN)============================5*6* Copyright (c) 2007-2010 Projet RNRT SAPHIR7*8* Permission is hereby granted, free of charge, to any person obtaining9* a copy of this software and associated documentation files (the10* "Software"), to deal in the Software without restriction, including11* without limitation the rights to use, copy, modify, merge, publish,12* distribute, sublicense, and/or sell copies of the Software, and to13* permit persons to whom the Software is furnished to do so, subject to14* the following conditions:15*16* The above copyright notice and this permission notice shall be17* included in all copies or substantial portions of the Software.18*19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,20* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.22* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY23* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,24* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE25* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.26*27* ===========================(LICENSE END)=============================28*29* @author Thomas Pornin <[email protected]>30* @author Tanguy Pruvot (cpuminer implementation)31*/3233#include <stddef.h>34#include <string.h>35#include <limits.h>3637#include "sph_blake.h"3839#ifdef __cplusplus40extern "C"{41#endif4243#ifdef _MSC_VER44#pragma warning (disable: 4146)45#endif4647static const sph_u32 IV256[8] = {48SPH_C32(0x6A09E667), SPH_C32(0xBB67AE85),49SPH_C32(0x3C6EF372), SPH_C32(0xA54FF53A),50SPH_C32(0x510E527F), SPH_C32(0x9B05688C),51SPH_C32(0x1F83D9AB), SPH_C32(0x5BE0CD19)52};5354#define Z00 055#define Z01 156#define Z02 257#define Z03 358#define Z04 459#define Z05 560#define Z06 661#define Z07 762#define Z08 863#define Z09 964#define Z0A A65#define Z0B B66#define Z0C C67#define Z0D D68#define Z0E E69#define Z0F F7071#define Z10 E72#define Z11 A73#define Z12 474#define Z13 875#define Z14 976#define Z15 F77#define Z16 D78#define Z17 679#define Z18 180#define Z19 C81#define Z1A 082#define Z1B 283#define Z1C B84#define Z1D 785#define Z1E 586#define Z1F 38788#define Z20 B89#define Z21 890#define Z22 C91#define Z23 092#define Z24 593#define Z25 294#define Z26 F95#define Z27 D96#define Z28 A97#define Z29 E98#define Z2A 399#define Z2B 6100#define Z2C 7101#define Z2D 1102#define Z2E 9103#define Z2F 4104105#define Z30 7106#define Z31 9107#define Z32 3108#define Z33 1109#define Z34 D110#define Z35 C111#define Z36 B112#define Z37 E113#define Z38 2114#define Z39 6115#define Z3A 5116#define Z3B A117#define Z3C 4118#define Z3D 0119#define Z3E F120#define Z3F 8121122#define Z40 9123#define Z41 0124#define Z42 5125#define Z43 7126#define Z44 2127#define Z45 4128#define Z46 A129#define Z47 F130#define Z48 E131#define Z49 1132#define Z4A B133#define Z4B C134#define Z4C 6135#define Z4D 8136#define Z4E 3137#define Z4F D138139#define Z50 2140#define Z51 C141#define Z52 6142#define Z53 A143#define Z54 0144#define Z55 B145#define Z56 8146#define Z57 3147#define Z58 4148#define Z59 D149#define Z5A 7150#define Z5B 5151#define Z5C F152#define Z5D E153#define Z5E 1154#define Z5F 9155156#define Z60 C157#define Z61 5158#define Z62 1159#define Z63 F160#define Z64 E161#define Z65 D162#define Z66 4163#define Z67 A164#define Z68 0165#define Z69 7166#define Z6A 6167#define Z6B 3168#define Z6C 9169#define Z6D 2170#define Z6E 8171#define Z6F B172173#define Z70 D174#define Z71 B175#define Z72 7176#define Z73 E177#define Z74 C178#define Z75 1179#define Z76 3180#define Z77 9181#define Z78 5182#define Z79 0183#define Z7A F184#define Z7B 4185#define Z7C 8186#define Z7D 6187#define Z7E 2188#define Z7F A189190#define Z80 6191#define Z81 F192#define Z82 E193#define Z83 9194#define Z84 B195#define Z85 3196#define Z86 0197#define Z87 8198#define Z88 C199#define Z89 2200#define Z8A D201#define Z8B 7202#define Z8C 1203#define Z8D 4204#define Z8E A205#define Z8F 5206207#define Z90 A208#define Z91 2209#define Z92 8210#define Z93 4211#define Z94 7212#define Z95 6213#define Z96 1214#define Z97 5215#define Z98 F216#define Z99 B217#define Z9A 9218#define Z9B E219#define Z9C 3220#define Z9D C221#define Z9E D222#define Z9F 0223224#define Mx(r, i) Mx_(Z ## r ## i)225#define Mx_(n) Mx__(n)226#define Mx__(n) M ## n227228#define CSx(r, i) CSx_(Z ## r ## i)229#define CSx_(n) CSx__(n)230#define CSx__(n) CS ## n231232#define CS0 SPH_C32(0x243F6A88)233#define CS1 SPH_C32(0x85A308D3)234#define CS2 SPH_C32(0x13198A2E)235#define CS3 SPH_C32(0x03707344)236#define CS4 SPH_C32(0xA4093822)237#define CS5 SPH_C32(0x299F31D0)238#define CS6 SPH_C32(0x082EFA98)239#define CS7 SPH_C32(0xEC4E6C89)240#define CS8 SPH_C32(0x452821E6)241#define CS9 SPH_C32(0x38D01377)242#define CSA SPH_C32(0xBE5466CF)243#define CSB SPH_C32(0x34E90C6C)244#define CSC SPH_C32(0xC0AC29B7)245#define CSD SPH_C32(0xC97C50DD)246#define CSE SPH_C32(0x3F84D5B5)247#define CSF SPH_C32(0xB5470917)248249#if SPH_64250251#define CBx(r, i) CBx_(Z ## r ## i)252#define CBx_(n) CBx__(n)253#define CBx__(n) CB ## n254255#define CB0 SPH_C64(0x243F6A8885A308D3)256#define CB1 SPH_C64(0x13198A2E03707344)257#define CB2 SPH_C64(0xA4093822299F31D0)258#define CB3 SPH_C64(0x082EFA98EC4E6C89)259#define CB4 SPH_C64(0x452821E638D01377)260#define CB5 SPH_C64(0xBE5466CF34E90C6C)261#define CB6 SPH_C64(0xC0AC29B7C97C50DD)262#define CB7 SPH_C64(0x3F84D5B5B5470917)263#define CB8 SPH_C64(0x9216D5D98979FB1B)264#define CB9 SPH_C64(0xD1310BA698DFB5AC)265#define CBA SPH_C64(0x2FFD72DBD01ADFB7)266#define CBB SPH_C64(0xB8E1AFED6A267E96)267#define CBC SPH_C64(0xBA7C9045F12C7F99)268#define CBD SPH_C64(0x24A19947B3916CF7)269#define CBE SPH_C64(0x0801F2E2858EFC16)270#define CBF SPH_C64(0x636920D871574E69)271272#endif273274#define GS(m0, m1, c0, c1, a, b, c, d) do { \275a = SPH_T32(a + b + (m0 ^ c1)); \276d = SPH_ROTR32(d ^ a, 16); \277c = SPH_T32(c + d); \278b = SPH_ROTR32(b ^ c, 12); \279a = SPH_T32(a + b + (m1 ^ c0)); \280d = SPH_ROTR32(d ^ a, 8); \281c = SPH_T32(c + d); \282b = SPH_ROTR32(b ^ c, 7); \283} while (0)284285#define ROUND_S(r) do { \286GS(Mx(r, 0), Mx(r, 1), CSx(r, 0), CSx(r, 1), V0, V4, V8, VC); \287GS(Mx(r, 2), Mx(r, 3), CSx(r, 2), CSx(r, 3), V1, V5, V9, VD); \288GS(Mx(r, 4), Mx(r, 5), CSx(r, 4), CSx(r, 5), V2, V6, VA, VE); \289GS(Mx(r, 6), Mx(r, 7), CSx(r, 6), CSx(r, 7), V3, V7, VB, VF); \290GS(Mx(r, 8), Mx(r, 9), CSx(r, 8), CSx(r, 9), V0, V5, VA, VF); \291GS(Mx(r, A), Mx(r, B), CSx(r, A), CSx(r, B), V1, V6, VB, VC); \292GS(Mx(r, C), Mx(r, D), CSx(r, C), CSx(r, D), V2, V7, V8, VD); \293GS(Mx(r, E), Mx(r, F), CSx(r, E), CSx(r, F), V3, V4, V9, VE); \294} while (0)295296#define DECL_STATE32 \297sph_u32 H0, H1, H2, H3, H4, H5, H6, H7; \298sph_u32 S0, S1, S2, S3, T0, T1;299300#define READ_STATE32(state) do { \301H0 = (state)->H[0]; \302H1 = (state)->H[1]; \303H2 = (state)->H[2]; \304H3 = (state)->H[3]; \305H4 = (state)->H[4]; \306H5 = (state)->H[5]; \307H6 = (state)->H[6]; \308H7 = (state)->H[7]; \309S0 = (state)->S[0]; \310S1 = (state)->S[1]; \311S2 = (state)->S[2]; \312S3 = (state)->S[3]; \313T0 = (state)->T0; \314T1 = (state)->T1; \315} while (0)316317#define WRITE_STATE32(state) do { \318(state)->H[0] = H0; \319(state)->H[1] = H1; \320(state)->H[2] = H2; \321(state)->H[3] = H3; \322(state)->H[4] = H4; \323(state)->H[5] = H5; \324(state)->H[6] = H6; \325(state)->H[7] = H7; \326(state)->S[0] = S0; \327(state)->S[1] = S1; \328(state)->S[2] = S2; \329(state)->S[3] = S3; \330(state)->T0 = T0; \331(state)->T1 = T1; \332} while (0)333334#define BLAKE32_ROUNDS 8335336#define COMPRESS32 do { \337sph_u32 M0, M1, M2, M3, M4, M5, M6, M7; \338sph_u32 M8, M9, MA, MB, MC, MD, ME, MF; \339sph_u32 V0, V1, V2, V3, V4, V5, V6, V7; \340sph_u32 V8, V9, VA, VB, VC, VD, VE, VF; \341V0 = H0; \342V1 = H1; \343V2 = H2; \344V3 = H3; \345V4 = H4; \346V5 = H5; \347V6 = H6; \348V7 = H7; \349V8 = S0 ^ CS0; \350V9 = S1 ^ CS1; \351VA = S2 ^ CS2; \352VB = S3 ^ CS3; \353VC = T0 ^ CS4; \354VD = T0 ^ CS5; \355VE = T1 ^ CS6; \356VF = T1 ^ CS7; \357M0 = sph_dec32be_aligned(buf + 0); \358M1 = sph_dec32be_aligned(buf + 4); \359M2 = sph_dec32be_aligned(buf + 8); \360M3 = sph_dec32be_aligned(buf + 12); \361M4 = sph_dec32be_aligned(buf + 16); \362M5 = sph_dec32be_aligned(buf + 20); \363M6 = sph_dec32be_aligned(buf + 24); \364M7 = sph_dec32be_aligned(buf + 28); \365M8 = sph_dec32be_aligned(buf + 32); \366M9 = sph_dec32be_aligned(buf + 36); \367MA = sph_dec32be_aligned(buf + 40); \368MB = sph_dec32be_aligned(buf + 44); \369MC = sph_dec32be_aligned(buf + 48); \370MD = sph_dec32be_aligned(buf + 52); \371ME = sph_dec32be_aligned(buf + 56); \372MF = sph_dec32be_aligned(buf + 60); \373ROUND_S(0); \374ROUND_S(1); \375ROUND_S(2); \376ROUND_S(3); \377ROUND_S(4); \378ROUND_S(5); \379ROUND_S(6); \380ROUND_S(7); \381if (BLAKE32_ROUNDS == 14) { \382ROUND_S(8); \383ROUND_S(9); \384ROUND_S(0); \385ROUND_S(1); \386ROUND_S(2); \387ROUND_S(3); \388} \389H0 ^= S0 ^ V0 ^ V8; \390H1 ^= S1 ^ V1 ^ V9; \391H2 ^= S2 ^ V2 ^ VA; \392H3 ^= S3 ^ V3 ^ VB; \393H4 ^= S0 ^ V4 ^ VC; \394H5 ^= S1 ^ V5 ^ VD; \395H6 ^= S2 ^ V6 ^ VE; \396H7 ^= S3 ^ V7 ^ VF; \397} while (0)398399400static const sph_u32 salt_zero_small[4] = { 0, 0, 0, 0 };401402static void403blake32_init(sph_blake_small_context *sc,404const sph_u32 *iv, const sph_u32 *salt)405{406memcpy(sc->H, iv, 8 * sizeof(sph_u32));407memcpy(sc->S, salt, 4 * sizeof(sph_u32));408sc->T0 = sc->T1 = 0;409sc->ptr = 0;410}411412static void413blake32(sph_blake_small_context *sc, const void *data, size_t len)414{415unsigned char *buf;416size_t ptr;417DECL_STATE32418419buf = sc->buf;420ptr = sc->ptr;421if (len < (sizeof sc->buf) - ptr) {422memcpy(buf + ptr, data, len);423ptr += len;424sc->ptr = ptr;425return;426}427428READ_STATE32(sc);429while (len > 0) {430size_t clen;431432clen = (sizeof sc->buf) - ptr;433if (clen > len)434clen = len;435memcpy(buf + ptr, data, clen);436ptr += clen;437data = (const unsigned char *)data + clen;438len -= clen;439if (ptr == sizeof sc->buf) {440if ((T0 = SPH_T32(T0 + 512)) < 512)441T1 = SPH_T32(T1 + 1);442COMPRESS32;443ptr = 0;444}445}446WRITE_STATE32(sc);447sc->ptr = ptr;448}449450static void451blake32_close(sph_blake_small_context *sc,452unsigned ub, unsigned n, void *dst, size_t out_size_w32)453{454union {455unsigned char buf[64];456sph_u32 dummy;457} u;458size_t ptr, k;459unsigned bit_len;460unsigned z;461sph_u32 th, tl;462unsigned char *out;463464ptr = sc->ptr;465bit_len = ((unsigned)ptr << 3) + n;466z = 0x80 >> n;467u.buf[ptr] = ((ub & -z) | z) & 0xFF;468tl = sc->T0 + bit_len;469th = sc->T1;470if (ptr == 0 && n == 0) {471sc->T0 = SPH_C32(0xFFFFFE00);472sc->T1 = SPH_C32(0xFFFFFFFF);473} else if (sc->T0 == 0) {474sc->T0 = SPH_C32(0xFFFFFE00) + bit_len;475sc->T1 = SPH_T32(sc->T1 - 1);476} else {477sc->T0 -= 512 - bit_len;478}479if (bit_len <= 446) {480memset(u.buf + ptr + 1, 0, 55 - ptr);481if (out_size_w32 == 8)482u.buf[55] |= 1;483sph_enc32be_aligned(u.buf + 56, th);484sph_enc32be_aligned(u.buf + 60, tl);485blake32(sc, u.buf + ptr, 64 - ptr);486} else {487memset(u.buf + ptr + 1, 0, 63 - ptr);488blake32(sc, u.buf + ptr, 64 - ptr);489sc->T0 = SPH_C32(0xFFFFFE00);490sc->T1 = SPH_C32(0xFFFFFFFF);491memset(u.buf, 0, 56);492if (out_size_w32 == 8)493u.buf[55] = 1;494sph_enc32be_aligned(u.buf + 56, th);495sph_enc32be_aligned(u.buf + 60, tl);496blake32(sc, u.buf, 64);497}498out = dst;499for (k = 0; k < out_size_w32; k ++)500sph_enc32be(out + (k << 2), sc->H[k]);501}502503void504blakecoin_init(void *cc)505{506blake32_init(cc, IV256, salt_zero_small);507}508509void510blakecoin(void *cc, const void *data, size_t len)511{512blake32(cc, data, len);513}514515static void516blakecoin_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)517{518blake32_close(cc, ub, n, dst, 8);519blakecoin_init(cc);520}521522void523blakecoin_close(void *cc, void *dst)524{525blakecoin_addbits_and_close(cc, 0, 0, dst);526}527528#ifdef __cplusplus529}530#endif531532533