diff --git a/.gitignore b/.gitignore index ce23abd8..1046279d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,4 @@ -cgminer -cgminer.exe -minerd -minerd.exe +*.a *.o *.lo *.bin @@ -45,3 +42,21 @@ lib/warn-on-use.h mkinstalldirs *.swp + +\.idea/ +cmake-build-*/ +**/\.dirstamp + +sgminer +libtool +ltmain\.sh + +m4/libtool\.m4 + +m4/lt~obsolete\.m4 + +m4/ltoptions\.m4 + +m4/ltsugar\.m4 + +m4/ltversion\.m4 diff --git a/Makefile.am b/Makefile.am index 69922b38..82ba7bd4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -85,6 +85,7 @@ sgminer_SOURCES += algorithm/ethash.c algorithm/ethgencache.c algorithm/ethash.h sgminer_SOURCES += algorithm/cryptonight.c algorithm/cryptonight.h algorithm/cn-aes-tbls.h sgminer_SOURCES += algorithm/equihash.c algorithm/equihash.h sgminer_SOURCES += gbt-util.c gbt-util.h +sgminer_SOURCES += algorithm/lbry.c algorithm/lbry.h bin_SCRIPTS = $(top_srcdir)/kernel/*.cl diff --git a/README.md b/README.md index ea13cd49..ff32cf4a 100644 --- a/README.md +++ b/README.md @@ -1,472 +1,18 @@ -# sgminer - - -## Introduction - -This is a multi-threaded multi-pool GPU miner with ATI GPU monitoring, -(over)clocking and fanspeed support for scrypt-based cryptocurrency. It is -based on cgminer by Con Kolivas (ckolivas), which is in turn based on -cpuminer by Jeff Garzik (jgarzik). - -**releases**: https://github.com/sgminer-dev/sgminer/releases - -**git tree**: https://github.com/sgminer-dev/sgminer - -**bugtracker**: https://github.com/sgminer-dev/sgminer/issues - -**irc**: `#sgminer` and `#sgminer-dev` on freenode - -**mailing lists**: https://sourceforge.net/p/sgminer/mailman/ - -License: GPLv3. See `COPYING` for details. - - -## Documentation - -Documentation is available in directory `doc`. It is organised by topics: - -* `API` for the RPC API specification; -* `configuration.md` for (largely incomplete) detailed information on all - configuration options; -* `FAQ.md` for frequently asked questions; -* `GPU` for semi-obsolete information on GPU configuration options and mining - SHA256d-based coins; -* `kernel.md` for OpenCL kernel-related information, including development - procedure; -* `MINING.md` for how to find the right balance in GPU configuration to mine - Scrypt-based coins efficiently; -* `windows-build.txt` for information on how to build on Windows. - -Note that **most of the documentation is outdated or incomplete**. If -you want to contribute, fork this repository, update as needed, and -submit a pull request. - - -## Building - -### Dependencies - -Mandatory: - -* [curl dev library](http://curl.haxx.se/libcurl/) - `libcurl4-openssl-dev` on Debian -* [pkg-config](http://www.freedesktop.org/wiki/Software/pkg-config) -* [libtool](http://www.gnu.org/software/libtool/) -* [AMD APP SDK](http://developer.amd.com/tools-and-sdks/heterogeneous-computing/amd-accelerated-parallel-processing-app-sdk/downloads/) - available under various names as a package on different GNU/Linux distributions - -Optional: - -* curses dev library - `libncurses5-dev` on Debian or `libpdcurses` on WIN32, for text user interface -* [AMD ADL SDK](http://developer.amd.com/display-library-adl-sdk/) - version 6, required for ATI GPU monitoring & clocking - -If building from git: - -* autoconf -* automake - -sgminer-specific configuration options: - - --disable-adl Override detection and disable building with adl - --disable-adl-checks - --without-curses Do not compile support for curses TUI - -#### Debian Example - - apt-get install libcurl4-openssl-dev pkg-config libtool libncurses5-dev -AMD APP SDK and AMD ADL SDK must be downloaded from the amd websites. - -### *nix build instructions - -If needed, place include headers (`*.h` files) from `ADL_SDK_**.zip` in `sgminer/ADL_SDK`. - -Then: - - git submodule init - git submodule update - autoreconf -i - CFLAGS="-O2 -Wall -march=native -std=gnu99" ./configure - make - -To compile a version that can be used accross machines, remove -`-march=native`. - -To compile a debug version, replace `-O2` with `-ggdb`. - -Depending on your environment, replace `-std=gnu99` with `-std=c99`. - -Systemwide installation is optional. You may run `sgminer` from the build -directory directly, or `make install` if you wish to install -`sgminer` to a system location or a location you specified with `--prefix`. - -### Windows build instructions - -See `doc/windows-build.txt` for MinGW compilation and cross-compiation, -`doc/cygwin-build.txt` for building using Cygwin, or use the provided -`winbuild` Microsoft Visual Studio project (tested on MSVS2010), with -instructions in `winbuild/README.txt`. - - -## Basic Usage - -**WARNING**: documentation below this point has not been updated since the -fork. - -After saving configuration from the menu, you do not need to give sgminer -any arguments and it will load your configuration. - -Any configuration file may also contain a single - - "include" : "filename" - -to recursively include another configuration file. - -Writing the configuration will save all settings from all files in the -output. - -Single pool: - -sgminer -o http://pool:port -u username -p password - -Multiple pools: - -sgminer -o http://pool1:port -u pool1username -p pool1password -o http://pool2:port -u pool2usernmae -p pool2password - -Single pool with a standard http proxy, regular desktop: - -sgminer -o "http:proxy:port|http://pool:port" -u username -p password - -Single pool with a socks5 proxy, regular desktop: - -sgminer -o "socks5:proxy:port|http://pool:port" -u username -p password - -Single pool with stratum protocol support: - -sgminer -o stratum+tcp://pool:port -u username -p password - -The list of proxy types are: - http: standard http 1.1 proxy - http0: http 1.0 proxy - socks4: socks4 proxy - socks5: socks5 proxy - socks4a: socks4a proxy - socks5h: socks5 proxy using a hostname - -If you compile sgminer with a version of CURL before 7.19.4 then some of -the above will not be available. All are available since CURL version -7.19.4. - -If you specify the --socks-proxy option to sgminer, it will only be -applied to all pools that don't specify their own proxy setting like -above. - -For more advanced usage , run `sgminer --help`. - -See `doc/GPU` for more information regarding GPU mining and -`doc/SCRYPT` for more information regarding Scrypt mining. - - -## Runtime usage - -The following options are available while running with a single keypress: - -[P]ool management [G]PU management [S]ettings [D]isplay options [Q]uit - -P gives you: - -Current pool management strategy: Failover -[F]ailover only disabled -[A]dd pool [R]emove pool [D]isable pool [E]nable pool -[C]hange management strategy [S]witch pool [I]nformation - - -S gives you: - -[Q]ueue: 1 -[S]cantime: 60 -[E]xpiry: 120 -[W]rite config file -[C]gminer restart - - -D gives you: - -[N]ormal [C]lear [S]ilent mode (disable all output) -[D]ebug:off -[P]er-device:off -[Q]uiet:off -[V]erbose:off -[R]PC debug:off -[W]orkTime details:off -co[M]pact: off -[L]og interval:5 - - -Q quits the application. - - -G gives you something like: - -GPU 0: [124.2 / 191.3 Mh/s] [A:77 R:33 HW:0 U:1.73/m WU 1.73/m] -Temp: 67.0 C -Fan Speed: 35% (2500 RPM) -Engine Clock: 960 MHz -Memory Clock: 480 Mhz -Vddc: 1.200 V -Activity: 93% -Powertune: 0% -Last initialised: [2011-09-06 12:03:56] -Thread 0: 62.4 Mh/s Enabled ALIVE -Thread 1: 60.2 Mh/s Enabled ALIVE - -[E]nable [D]isable [R]estart GPU [C]hange settings -Or press any other key to continue - - -The running log shows output like this: - - [2012-10-12 18:02:20] Accepted f0c05469 Diff 1/1 GPU 0 pool 1 - [2012-10-12 18:02:22] Accepted 218ac982 Diff 7/1 GPU 1 pool 1 - [2012-10-12 18:02:23] Accepted d8300795 Diff 1/1 GPU 3 pool 1 - [2012-10-12 18:02:24] Accepted 122c1ff1 Diff 14/1 GPU 1 pool 1 - -The 8 byte hex value are the 2nd 8 bytes of the share being submitted to the -pool. The 2 diff values are the actual difficulty target that share reached -followed by the difficulty target the pool is currently asking for. - -The output line shows the following: -(5s):1713.6 (avg):1707.8 Mh/s | A:729 R:8 HW:0 WU:22.53/m - -Each column is as follows: -5s: A 5 second exponentially decaying average hash rate -avg: An all time average hash rate -A: The total difficulty of Accepted shares -R: The total difficulty of Rejected shares -HW: The number of HardWare errors -WU: The Work Utility defined as the number of diff1 shares work / minute - (accepted or rejected). - - GPU 1: 73.5C 2551RPM | 427.3/443.0Mh/s | A:8 R:0 HW:0 WU:4.39/m - -Each column is as follows: -Temperature (if supported) -Fanspeed (if supported) -A 5 second exponentially decaying average hash rate -An all time average hash rate -The total difficulty of accepted shares -The total difficulty of rejected shares -The number of hardware erorrs -The work utility defined as the number of diff1 shares work / minute - -The sgminer status line shows: - ST: 1 SS: 0 NB: 1 LW: 8 GF: 1 RF: 1 - -ST is STaged work items (ready to use). -SS is Stale Shares discarded (detected and not submitted so don't count as rejects) -NB is New Blocks detected on the network -LW is Locally generated Work items -GF is Getwork Fail Occasions (server slow to provide work) -RF is Remote Fail occasions (server slow to accept work) - -The block display shows: -Block: 0074c5e482e34a506d2a051a... Started: [17:17:22] Best share: 2.71K - -This shows a short stretch of the current block, when the new block started, -and the all time best difficulty share you've found since starting sgminer -this time. - - -## Multipool - -### Failover strategies - -A number of different strategies for dealing with multipool setups are -available. Each has their advantages and disadvantages so multiple strategies -are available by user choice, as per the following list: - -#### Failover - -The default strategy is failover. This means that if you input a number of -pools, it will try to use them as a priority list, moving away from the 1st -to the 2nd, 2nd to 3rd and so on. If any of the earlier pools recover, it will -move back to the higher priority ones. - -#### Round robin - -This strategy only moves from one pool to the next when the current one falls -idle and makes no attempt to move otherwise. - -#### Rotate - -This strategy moves at user-defined intervals from one active pool to the next, -skipping pools that are idle. - -#### Load balance - -This strategy sends work to all the pools on a quota basis. By default, all -pools are allocated equal quotas unless specified with --quota. This -apportioning of work is based on work handed out, not shares returned so is -independent of difficulty targets or rejected shares. While a pool is disabled -or dead, its quota is dropped until it is re-enabled. Quotas are forward -looking, so if the quota is changed on the fly, it only affects future work. -If all pools are set to zero quota or all pools with quota are dead, it will -fall back to a failover mode. See quota below for more information. - -The failover-only flag has special meaning in combination with load-balance -mode and it will distribute quota back to priority pool 0 from any pools that -are unable to provide work for any reason so as to maintain quota ratios -between the rest of the pools. - -#### Balance - -This strategy monitors the amount of difficulty 1 shares solved for each pool -and uses it to try to end up doing the same amount of work for all pools. - - -### Quotas - -The load-balance multipool strategy works off a quota based scheduler. The -quotas handed out by default are equal, but the user is allowed to specify any -arbitrary ratio of quotas. For example, if all the quota values add up to 100, -each quota value will be a percentage, but if 2 pools are specified and pool0 -is given a quota of 1 and pool1 is given a quota of 9, pool0 will get 10% of -the work and pool1 will get 90%. Quotas can be changed on the fly by the API, -and do not act retrospectively. Setting a quota to zero will effectively -disable that pool unless all other pools are disabled or dead. In that -scenario, load-balance falls back to regular failover priority-based strategy. -While a pool is dead, it loses its quota and no attempt is made to catch up -when it comes back to life. - -To specify quotas on the command line, pools should be specified with a -semicolon separated --quota(or -U) entry instead of --url. Pools specified with ---url are given a nominal quota value of 1 and entries can be mixed. - -For example: ---url poola:porta -u usernamea -p passa --quota "2;poolb:portb" -u usernameb -p passb -Will give poola 1/3 of the work and poolb 2/3 of the work. - -Writing configuration files with quotas is likewise supported. To use -the above quotas in a configuration file they would be specified thus: - - "pools" : [ - { - "url" : "poola:porta", - "user" : "usernamea", - "pass" : "passa" - }, - { - "quota" : "2;poolb:portb", - "user" : "usernameb", - "pass" : "passb" - } - ] - - -### Extra File Configuration - -If you want to store a number of pools in your configuration file, but -don't always want them automatically enabled at start up (or restart), -then the "state" option with a value of "disabled" can be used: - - "pools" : [ - { - "url" : "poola:porta", - "user" : "usernamea", - "pass" : "passa" - }, - { - "quota" : "2;poolb:portb", - "user" : "usernameb", - "pass" : "passb", - "state" : "disabled" - } - ] - -It is then trivial to change the "state" setting to "enabled" in the -configuration file at anytime and then restart the miner (see below). -You can enable the pool whilst the miner is still running ('p' followed -by 'e' followed by pool number) - but the pool will still be disabled on -restart if the config file is not changed. - -"state" can also be set to "hidden". This allows the json file to -contain a large number of pools, of which some could be automatically -culled at start up. This makes it easy to swap pools in and out of the -runtime selection, without having a large list of pools cluttering up -the display. - - "pools" : [ - { - "poolname" : "Main Pool", - "url" : "poola:porta", - "user" : "usernamea", - "pass" : "passa", - "state" : "disabled" - }, - { - "poolname" : "Joe's Weekend Pool", - "quota" : "2;poolb:portb", - "user" : "usernameb", - "pass" : "passb", - "state" : "hidden" - } - ] - -These options are considered experimental and therefore will NOT be -created when the 'Write config file' option is used ('s' followed by -'w'). - -A restart of the miner ('s' followed by 'c') will reload the config -file and any changes that may have been made. - - -## Logging - -sgminer will log to stderr if it detects stderr is being redirected to a -file. To enable logging simply append `2>logfile.txt` to your command line -and `logfile.txt` will contain all debug output unless you set `debug-log` -to `false`, in which case it will only contain output at the log level you -specified (notice by default). - -There is also the -m option on Linux which will spawn a command of your choice -and pipe the output directly to that command. - -The WorkTime details 'debug' option adds details on the end of each line -displayed for Accepted or Rejected work done. An example would be: - - <-00000059.ed4834a3 M:X D:1.0 G:17:02:38:0.405 C:1.855 (2.995) W:3.440 (0.000) S:0.461 R:17:02:47 - -The first 2 hex codes are the previous block hash, the rest are reported in -seconds unless stated otherwise: -The previous hash is followed by the getwork mode used M:X where X is one of -P:Pool, T:Test Pool, L:LP or B:Benchmark, -then D:d.ddd is the difficulty required to get a share from the work, -then G:hh:mm:ss:n.nnn, which is when the getwork or LP was sent to the pool and -the n.nnn is how long it took to reply, -followed by 'O' on it's own if it is an original getwork, or 'C:n.nnn' if it was -a clone with n.nnn stating how long after the work was recieved that it was cloned, -(m.mmm) is how long from when the original work was received until work started, -W:n.nnn is how long the work took to process until it was ready to submit, -(m.mmm) is how long from ready to submit to actually doing the submit, this is -usually 0.000 unless there was a problem with submitting the work, -S:n.nnn is how long it took to submit the completed work and await the reply, -R:hh:mm:ss is the actual time the work submit reply was received - -If you start sgminer with the --sharelog option, you can get detailed -information for each share found. The argument to the option may be "-" for -standard output (not advisable with the ncurses UI), any valid positive number -for that file descriptor, or a filename. - -To log share data to a file named "share.log", you can use either: -./sgminer --sharelog 50 -o xxx -u yyy -p zzz 50>share.log -./sgminer --sharelog share.log -o xxx -u yyy -p zzz - -For every share found, data will be logged in a CSV (Comma Separated Value) -format: - timestamp,disposition,target,pool,dev,thr,sharehash,sharedata -For example (this is wrapped, but it's all on one line for real): - 1335313090,reject, - ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000, - http://localhost:8337,GPU0,0, - 6f983c918f3299b58febf95ec4d0c7094ed634bc13754553ec34fc3800000000, - 00000001a0980aff4ce4a96d53f4b89a2d5f0e765c978640fe24372a000001c5 - 000000004a4366808f81d44f26df3d69d7dc4b3473385930462d9ab707b50498 - f681634a4f1f63d01a0cd43fb338000000000080000000000000000000000000 - 0000000000000000000000000000000000000000000000000000000080020000 +This fork of sgminer-gm supports mining LBRY in both Stratum pools and directly to a local LBRYcrd instance. + +#### Example: +``` +# compile the thing: +git clone https://github.com/lbryio/sgminer-gm +cd sgminer-gm +git submodule init +git submodule update +./autogen.sh # has to run after submodule stuff +./configure CFLAGS="-O3 -march=native -I/usr/lib/cuda-10.1/targets/x86_64-linux/include -L/usr/lib/cuda-10.1/targets/x86_64-linux/lib" +make -j10 + +# start mining on testnet: +./sgminer -k lbry -O ruser:rpswd -o "http://127.0.0.1:19245" +``` + +Substitute the cuda paths for your local OpenCL installation location. Please refer to the documentation found upstream for further build instructions, etc. \ No newline at end of file diff --git a/algorithm.c b/algorithm.c index 96c9679e..1f37b742 100644 --- a/algorithm.c +++ b/algorithm.c @@ -43,6 +43,7 @@ #include "algorithm/ethash.h" #include "algorithm/cryptonight.h" #include "algorithm/equihash.h" +#include "algorithm/lbry.h" #include "compat.h" @@ -77,7 +78,8 @@ const char *algorithm_type_str[] = { "Vanilla", "Ethash", "Cryptonight", - "Equihash" + "Equihash", + "LBRY" }; void sha256(const unsigned char *message, unsigned int len, unsigned char *digest) @@ -1218,6 +1220,30 @@ static cl_int queue_equihash_kernel(_clState *clState, dev_blk_ctx *blk, __maybe } #undef WORKSIZE +static cl_int queue_lbry_kernel(struct __clState *clState, struct _dev_blk_ctx *blk, __maybe_unused cl_uint threads) +{ + cl_kernel *kernel = &clState->kernel; + unsigned int num = 0; + cl_ulong le_target; + cl_int status = 0; + + le_target = *(cl_ulong *)(blk->work->target + 24); + flip112(clState->cldata, blk->work->data); + status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 112, clState->cldata, 0, NULL, NULL); + + CL_SET_ARG(clState->CLbuffer0); + CL_SET_ARG(clState->padbuffer8); + num = 0; + kernel = clState->extra_kernels; + CL_SET_ARG_0(clState->padbuffer8); + num = 0; + + CL_NEXTKERNEL_SET_ARG(clState->padbuffer8); + CL_SET_ARG(clState->outputBuffer); + CL_SET_ARG(le_target); + + return status; +} static algorithm_settings_t algos[] = { // kernels starting from this will have difficulty calculated by using litecoin algorithm @@ -1318,7 +1344,8 @@ static algorithm_settings_t algos[] = { { "cryptonight", ALGO_CRYPTONIGHT, "", 1, 0x100010001LLU, 0x100010001LLU, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 6, 0, 0, cryptonight_regenhash, NULL, queue_cryptonight_kernel, gen_hash, NULL }, { "equihash", ALGO_EQUIHASH, "", 1, (1ULL << 28), (1ULL << 28), 0, 0, 0x20000, 0xFFFF000000000000ULL, 0x00000000UL, 0, 128, 0, equihash_regenhash, NULL, queue_equihash_kernel, gen_hash, append_equihash_compiler_options }, - + { "lbry", ALGO_LBRY, "", 1, 256, 256, 0, 0, 0xFF, 0xFFFFULL, 0x0000ffffUL, 2, 4 * 8 * 4194304, 0, lbry_regenhash, NULL, queue_lbry_kernel, gen_hash, NULL }, + // Terminator (do not remove) { NULL, ALGO_UNK, "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL } }; diff --git a/algorithm.h b/algorithm.h index 54e2308e..cc73896e 100644 --- a/algorithm.h +++ b/algorithm.h @@ -39,7 +39,8 @@ typedef enum { ALGO_VANILLA, ALGO_ETHASH, ALGO_CRYPTONIGHT, - ALGO_EQUIHASH + ALGO_EQUIHASH, + ALGO_LBRY } algorithm_type_t; extern const char *algorithm_type_str[]; diff --git a/algorithm/lbry.c b/algorithm/lbry.c new file mode 100644 index 00000000..79ac78c4 --- /dev/null +++ b/algorithm/lbry.c @@ -0,0 +1,72 @@ +#include "config.h" +#include "miner.h" + +#include +#include +#include + +#include "sph/sph_sha2.h" +#include "sph/sph_ripemd.h" + +typedef struct { + sph_sha256_context sha256; + sph_sha512_context sha512; + sph_ripemd160_context ripemd; +} lbryhash_context_holder; + +void lbryhash(void* output, const void* input) +{ + uint32_t hashA[16], hashB[16], hashC[16]; + lbryhash_context_holder ctx; + + sph_sha256_init(&ctx.sha256); + sph_sha512_init(&ctx.sha512); + sph_ripemd160_init(&ctx.ripemd); + + memset(hashA, 0, 16 * sizeof(uint32_t)); + memset(hashB, 0, 16 * sizeof(uint32_t)); + memset(hashC, 0, 16 * sizeof(uint32_t)); + + sph_sha256 (&ctx.sha256, input, 112); + sph_sha256_close(&ctx.sha256, hashA); + + sph_sha256 (&ctx.sha256, hashA, 32); + sph_sha256_close(&ctx.sha256, hashA); + + sph_sha512 (&ctx.sha512, hashA, 32); + sph_sha512_close(&ctx.sha512, hashA); + + sph_ripemd160 (&ctx.ripemd, hashA, 32); + sph_ripemd160_close(&ctx.ripemd, hashB); + + sph_ripemd160 (&ctx.ripemd, hashA+8, 32); + sph_ripemd160_close(&ctx.ripemd, hashC); + + sph_sha256 (&ctx.sha256, hashB, 20); + sph_sha256 (&ctx.sha256, hashC, 20); + sph_sha256_close(&ctx.sha256, hashA); + + sph_sha256 (&ctx.sha256, hashA, 32); + sph_sha256_close(&ctx.sha256, hashA); + + memcpy(output, hashA, 32); +} + +void lbry_regenhash(struct work *work) +{ + uint32_t data[28]; + uint32_t *nonce = (uint32_t *)(work->data + 108); + uint32_t *ohash = (uint32_t *)(work->hash); + + //be32enc_vect(data, (const uint32_t *)work->data, 27); + //data[27] = htobe32(*nonce); + flip112(data, work->data); + + lbryhash(ohash, data); + + char *dbgstr = bin2hex(ohash, 32); + applog(LOG_DEBUG, "Regenhash result: %s\nNonce = 0x%08X", dbgstr, *nonce); + free(dbgstr); + + //exit(0); +} diff --git a/algorithm/lbry.h b/algorithm/lbry.h new file mode 100644 index 00000000..eecc060e --- /dev/null +++ b/algorithm/lbry.h @@ -0,0 +1,8 @@ +#ifndef LBRY_H +#define LBRY_H + +#include "miner.h" + +void lbry_regenhash(struct work *work); + +#endif diff --git a/kernel/lbry.cl b/kernel/lbry.cl new file mode 100644 index 00000000..b936ccf5 --- /dev/null +++ b/kernel/lbry.cl @@ -0,0 +1,135 @@ +#include "sha256.cl" +#include "wolf-sha512.cl" +#include "ripemd160.cl" + +__attribute__((reqd_work_group_size(WORKSIZE, 1, 1))) +__kernel void search(__global const uint *input, __global uint8 *ctx) +{ + // SHA256 takes 16 uints of input per block - we have 112 bytes to process + // 8 * 16 == 64, meaning two block transforms. + + uint SHA256Buf[16]; + uint gid = get_global_id(0); + + // Remember the last four is the nonce - so 108 bytes / 4 bytes per dword + #pragma unroll + for(int i = 0; i < 16; ++i) SHA256Buf[i] = SWAP32(input[i]); + + + + // SHA256 initialization constants + uint8 outbuf = (uint8)(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19); + + #pragma unroll + for(int i = 0; i < 3; ++i) + { + if(i == 1) + { + #pragma unroll + for(int i = 0; i < 11; ++i) SHA256Buf[i] = SWAP32(input[i + 16]); + SHA256Buf[11] = SWAP32(gid); + SHA256Buf[12] = 0x80000000; + SHA256Buf[13] = 0x00000000; + SHA256Buf[14] = 0x00000000; + SHA256Buf[15] = 0x00000380; + } + if(i == 2) + { + ((uint8 *)SHA256Buf)[0] = outbuf; + SHA256Buf[8] = 0x80000000; + #pragma unroll + for(int i = 9; i < 15; ++i) SHA256Buf[i] = 0x00000000; + SHA256Buf[15] = 0x00000100; + outbuf = (uint8)(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19); + } + outbuf = sha256_round(((uint16 *)SHA256Buf)[0], outbuf); + } + + outbuf.s0 = SWAP32(outbuf.s0); + outbuf.s1 = SWAP32(outbuf.s1); + outbuf.s2 = SWAP32(outbuf.s2); + outbuf.s3 = SWAP32(outbuf.s3); + outbuf.s4 = SWAP32(outbuf.s4); + outbuf.s5 = SWAP32(outbuf.s5); + outbuf.s6 = SWAP32(outbuf.s6); + outbuf.s7 = SWAP32(outbuf.s7); + + ctx[get_global_id(0) - get_global_offset(0)] = outbuf; +} + +__attribute__((reqd_work_group_size(WORKSIZE, 1, 1))) +__kernel void search1(__global uint8 *ctx) +{ + ulong W[16] = { 0UL }, SHA512Out[8]; + uint SHA256Buf[16]; + uint8 outbuf = ctx[get_global_id(0) - get_global_offset(0)]; + + ((uint8 *)W)[0] = outbuf; + + for(int i = 0; i < 4; ++i) W[i] = SWAP64(W[i]); + + W[4] = 0x8000000000000000UL; + W[15] = 0x0000000000000100UL; + + for(int i = 0; i < 8; ++i) SHA512Out[i] = SHA512_INIT[i]; + + SHA512Block(W, SHA512Out); + + for(int i = 0; i < 8; ++i) SHA512Out[i] = SWAP64(SHA512Out[i]); + + uint RMD160_0[16] = { 0U }; + uint RMD160_1[16] = { 0U }; + uint RMD160_0_Out[5], RMD160_1_Out[5]; + + for(int i = 0; i < 4; ++i) + { + ((ulong *)RMD160_0)[i] = SHA512Out[i]; + ((ulong *)RMD160_1)[i] = SHA512Out[i + 4]; + } + + RMD160_0[8] = RMD160_1[8] = 0x00000080; + RMD160_0[14] = RMD160_1[14] = 0x00000100; + + for(int i = 0; i < 5; ++i) + { + RMD160_0_Out[i] = RMD160_IV[i]; + RMD160_1_Out[i] = RMD160_IV[i]; + } + + RIPEMD160_ROUND_BODY(RMD160_0, RMD160_0_Out); + RIPEMD160_ROUND_BODY(RMD160_1, RMD160_1_Out); + + for(int i = 0; i < 5; ++i) SHA256Buf[i] = SWAP32(RMD160_0_Out[i]); + for(int i = 5; i < 10; ++i) SHA256Buf[i] = SWAP32(RMD160_1_Out[i - 5]); + SHA256Buf[10] = 0x80000000; + + for(int i = 11; i < 15; ++i) SHA256Buf[i] = 0x00000000U; + + SHA256Buf[15] = 0x00000140; + + outbuf = (uint8)(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19); + outbuf = sha256_round(((uint16 *)SHA256Buf)[0], outbuf); + + ctx[get_global_id(0) - get_global_offset(0)] = outbuf; +} + +__attribute__((reqd_work_group_size(WORKSIZE, 1, 1))) +__kernel void search2(__global uint8 *ctx, __global uint *output, ulong target) +{ + uint SHA256Buf[16] = { 0U }; + uint gid = get_global_id(0); + uint8 outbuf = ctx[get_global_id(0) - get_global_offset(0)]; + + ((uint8 *)SHA256Buf)[0] = outbuf; + SHA256Buf[8] = 0x80000000; + for(int i = 9; i < 15; ++i) SHA256Buf[i] = 0x00000000; + SHA256Buf[15] = 0x00000100; + + outbuf = (uint8)(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19); + outbuf = sha256_round(((uint16 *)SHA256Buf)[0], outbuf); + + outbuf.s6 = SWAP32(outbuf.s6); + outbuf.s7 = SWAP32(outbuf.s7); + + if(as_ulong(outbuf.s67) <= target) output[atomic_inc(output+0xFF)] = SWAP32(gid); +} diff --git a/kernel/ripemd160.cl b/kernel/ripemd160.cl new file mode 100644 index 00000000..1fe38a96 --- /dev/null +++ b/kernel/ripemd160.cl @@ -0,0 +1,225 @@ +#define RIPEMD160_IN(x) W[x] + +#define RF1(x, y, z) ((x) ^ (y) ^ (z)) +#define RF2(x, y, z) ((((y) ^ (z)) & (x)) ^ (z)) +#define RF3(x, y, z) (((x) | ~(y)) ^ (z)) +#define RF4(x, y, z) ((((x) ^ (y)) & (z)) ^ (y)) +#define RF5(x, y, z) ((x) ^ ((y) | ~(z))) + +#define K11 0x00000000 +#define K12 0x5A827999 +#define K13 0x6ED9EBA1 +#define K14 0x8F1BBCDC +#define K15 0xA953FD4E + +#define K21 0x50A28BE6 +#define K22 0x5C4DD124 +#define K23 0x6D703EF3 +#define K24 0x7A6D76E9 +#define K25 0x00000000 + +const __constant uint RMD160_IV[5] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 }; + +#define inner_align(src0, src1, src2) ((uint)(((((ulong)src0) << 32) | (ulong)src1) >> (src2 & 0x1F))) + +#define RR(a, b, c, d, e, f, s, r, k) do { \ + const uint rrtmp = a + f(b, c, d) + r + k; \ + a = inner_align(rrtmp, rrtmp, 32U - (uint)s) + e; \ + c = inner_align(c, c, 32U - 10U); \ + } while (0) + +#define ROUND1(a, b, c, d, e, f, s, r, k) \ + RR(a ## 1, b ## 1, c ## 1, d ## 1, e ## 1, f, s, r, K1 ## k) + +#define ROUND2(a, b, c, d, e, f, s, r, k) \ + RR(a ## 2, b ## 2, c ## 2, d ## 2, e ## 2, f, s, r, K2 ## k) + +void RIPEMD160_ROUND_BODY(uint *in, uint *h) +{ + uint A1, B1, C1, D1, E1; + uint A2, B2, C2, D2, E2; + uint tmp; + + A1 = A2 = (h)[0]; + B1 = B2 = (h)[1]; + C1 = C2 = (h)[2]; + D1 = D2 = (h)[3]; + E1 = E2 = (h)[4]; + + ROUND1(A, B, C, D, E, RF1, 11, (in)[ 0], 1); + ROUND1(E, A, B, C, D, RF1, 14, (in)[ 1], 1); + ROUND1(D, E, A, B, C, RF1, 15, (in)[ 2], 1); + ROUND1(C, D, E, A, B, RF1, 12, (in)[ 3], 1); + ROUND1(B, C, D, E, A, RF1, 5, (in)[ 4], 1); + ROUND1(A, B, C, D, E, RF1, 8, (in)[ 5], 1); + ROUND1(E, A, B, C, D, RF1, 7, (in)[ 6], 1); + ROUND1(D, E, A, B, C, RF1, 9, (in)[ 7], 1); + ROUND1(C, D, E, A, B, RF1, 11, (in)[ 8], 1); + ROUND1(B, C, D, E, A, RF1, 13, (in)[ 9], 1); + ROUND1(A, B, C, D, E, RF1, 14, (in)[10], 1); + ROUND1(E, A, B, C, D, RF1, 15, (in)[11], 1); + ROUND1(D, E, A, B, C, RF1, 6, (in)[12], 1); + ROUND1(C, D, E, A, B, RF1, 7, (in)[13], 1); + ROUND1(B, C, D, E, A, RF1, 9, (in)[14], 1); + ROUND1(A, B, C, D, E, RF1, 8, (in)[15], 1); + + ROUND1(E, A, B, C, D, RF2, 7, (in)[ 7], 2); + ROUND1(D, E, A, B, C, RF2, 6, (in)[ 4], 2); + ROUND1(C, D, E, A, B, RF2, 8, (in)[13], 2); + ROUND1(B, C, D, E, A, RF2, 13, (in)[ 1], 2); + ROUND1(A, B, C, D, E, RF2, 11, (in)[10], 2); + ROUND1(E, A, B, C, D, RF2, 9, (in)[ 6], 2); + ROUND1(D, E, A, B, C, RF2, 7, (in)[15], 2); + ROUND1(C, D, E, A, B, RF2, 15, (in)[ 3], 2); + ROUND1(B, C, D, E, A, RF2, 7, (in)[12], 2); + ROUND1(A, B, C, D, E, RF2, 12, (in)[ 0], 2); + ROUND1(E, A, B, C, D, RF2, 15, (in)[ 9], 2); + ROUND1(D, E, A, B, C, RF2, 9, (in)[ 5], 2); + ROUND1(C, D, E, A, B, RF2, 11, (in)[ 2], 2); + ROUND1(B, C, D, E, A, RF2, 7, (in)[14], 2); + ROUND1(A, B, C, D, E, RF2, 13, (in)[11], 2); + ROUND1(E, A, B, C, D, RF2, 12, (in)[ 8], 2); + + ROUND1(D, E, A, B, C, RF3, 11, (in)[ 3], 3); + ROUND1(C, D, E, A, B, RF3, 13, (in)[10], 3); + ROUND1(B, C, D, E, A, RF3, 6, (in)[14], 3); + ROUND1(A, B, C, D, E, RF3, 7, (in)[ 4], 3); + ROUND1(E, A, B, C, D, RF3, 14, (in)[ 9], 3); + ROUND1(D, E, A, B, C, RF3, 9, (in)[15], 3); + ROUND1(C, D, E, A, B, RF3, 13, (in)[ 8], 3); + ROUND1(B, C, D, E, A, RF3, 15, (in)[ 1], 3); + ROUND1(A, B, C, D, E, RF3, 14, (in)[ 2], 3); + ROUND1(E, A, B, C, D, RF3, 8, (in)[ 7], 3); + ROUND1(D, E, A, B, C, RF3, 13, (in)[ 0], 3); + ROUND1(C, D, E, A, B, RF3, 6, (in)[ 6], 3); + ROUND1(B, C, D, E, A, RF3, 5, (in)[13], 3); + ROUND1(A, B, C, D, E, RF3, 12, (in)[11], 3); + ROUND1(E, A, B, C, D, RF3, 7, (in)[ 5], 3); + ROUND1(D, E, A, B, C, RF3, 5, (in)[12], 3); + + ROUND1(C, D, E, A, B, RF4, 11, (in)[ 1], 4); + ROUND1(B, C, D, E, A, RF4, 12, (in)[ 9], 4); + ROUND1(A, B, C, D, E, RF4, 14, (in)[11], 4); + ROUND1(E, A, B, C, D, RF4, 15, (in)[10], 4); + ROUND1(D, E, A, B, C, RF4, 14, (in)[ 0], 4); + ROUND1(C, D, E, A, B, RF4, 15, (in)[ 8], 4); + ROUND1(B, C, D, E, A, RF4, 9, (in)[12], 4); + ROUND1(A, B, C, D, E, RF4, 8, (in)[ 4], 4); + ROUND1(E, A, B, C, D, RF4, 9, (in)[13], 4); + ROUND1(D, E, A, B, C, RF4, 14, (in)[ 3], 4); + ROUND1(C, D, E, A, B, RF4, 5, (in)[ 7], 4); + ROUND1(B, C, D, E, A, RF4, 6, (in)[15], 4); + ROUND1(A, B, C, D, E, RF4, 8, (in)[14], 4); + ROUND1(E, A, B, C, D, RF4, 6, (in)[ 5], 4); + ROUND1(D, E, A, B, C, RF4, 5, (in)[ 6], 4); + ROUND1(C, D, E, A, B, RF4, 12, (in)[ 2], 4); + + ROUND1(B, C, D, E, A, RF5, 9, (in)[ 4], 5); + ROUND1(A, B, C, D, E, RF5, 15, (in)[ 0], 5); + ROUND1(E, A, B, C, D, RF5, 5, (in)[ 5], 5); + ROUND1(D, E, A, B, C, RF5, 11, (in)[ 9], 5); + ROUND1(C, D, E, A, B, RF5, 6, (in)[ 7], 5); + ROUND1(B, C, D, E, A, RF5, 8, (in)[12], 5); + ROUND1(A, B, C, D, E, RF5, 13, (in)[ 2], 5); + ROUND1(E, A, B, C, D, RF5, 12, (in)[10], 5); + ROUND1(D, E, A, B, C, RF5, 5, (in)[14], 5); + ROUND1(C, D, E, A, B, RF5, 12, (in)[ 1], 5); + ROUND1(B, C, D, E, A, RF5, 13, (in)[ 3], 5); + ROUND1(A, B, C, D, E, RF5, 14, (in)[ 8], 5); + ROUND1(E, A, B, C, D, RF5, 11, (in)[11], 5); + ROUND1(D, E, A, B, C, RF5, 8, (in)[ 6], 5); + ROUND1(C, D, E, A, B, RF5, 5, (in)[15], 5); + ROUND1(B, C, D, E, A, RF5, 6, (in)[13], 5); + + ROUND2(A, B, C, D, E, RF5, 8, (in)[ 5], 1); + ROUND2(E, A, B, C, D, RF5, 9, (in)[14], 1); + ROUND2(D, E, A, B, C, RF5, 9, (in)[ 7], 1); + ROUND2(C, D, E, A, B, RF5, 11, (in)[ 0], 1); + ROUND2(B, C, D, E, A, RF5, 13, (in)[ 9], 1); + ROUND2(A, B, C, D, E, RF5, 15, (in)[ 2], 1); + ROUND2(E, A, B, C, D, RF5, 15, (in)[11], 1); + ROUND2(D, E, A, B, C, RF5, 5, (in)[ 4], 1); + ROUND2(C, D, E, A, B, RF5, 7, (in)[13], 1); + ROUND2(B, C, D, E, A, RF5, 7, (in)[ 6], 1); + ROUND2(A, B, C, D, E, RF5, 8, (in)[15], 1); + ROUND2(E, A, B, C, D, RF5, 11, (in)[ 8], 1); + ROUND2(D, E, A, B, C, RF5, 14, (in)[ 1], 1); + ROUND2(C, D, E, A, B, RF5, 14, (in)[10], 1); + ROUND2(B, C, D, E, A, RF5, 12, (in)[ 3], 1); + ROUND2(A, B, C, D, E, RF5, 6, (in)[12], 1); + + ROUND2(E, A, B, C, D, RF4, 9, (in)[ 6], 2); + ROUND2(D, E, A, B, C, RF4, 13, (in)[11], 2); + ROUND2(C, D, E, A, B, RF4, 15, (in)[ 3], 2); + ROUND2(B, C, D, E, A, RF4, 7, (in)[ 7], 2); + ROUND2(A, B, C, D, E, RF4, 12, (in)[ 0], 2); + ROUND2(E, A, B, C, D, RF4, 8, (in)[13], 2); + ROUND2(D, E, A, B, C, RF4, 9, (in)[ 5], 2); + ROUND2(C, D, E, A, B, RF4, 11, (in)[10], 2); + ROUND2(B, C, D, E, A, RF4, 7, (in)[14], 2); + ROUND2(A, B, C, D, E, RF4, 7, (in)[15], 2); + ROUND2(E, A, B, C, D, RF4, 12, (in)[ 8], 2); + ROUND2(D, E, A, B, C, RF4, 7, (in)[12], 2); + ROUND2(C, D, E, A, B, RF4, 6, (in)[ 4], 2); + ROUND2(B, C, D, E, A, RF4, 15, (in)[ 9], 2); + ROUND2(A, B, C, D, E, RF4, 13, (in)[ 1], 2); + ROUND2(E, A, B, C, D, RF4, 11, (in)[ 2], 2); + + ROUND2(D, E, A, B, C, RF3, 9, (in)[15], 3); + ROUND2(C, D, E, A, B, RF3, 7, (in)[ 5], 3); + ROUND2(B, C, D, E, A, RF3, 15, (in)[ 1], 3); + ROUND2(A, B, C, D, E, RF3, 11, (in)[ 3], 3); + ROUND2(E, A, B, C, D, RF3, 8, (in)[ 7], 3); + ROUND2(D, E, A, B, C, RF3, 6, (in)[14], 3); + ROUND2(C, D, E, A, B, RF3, 6, (in)[ 6], 3); + ROUND2(B, C, D, E, A, RF3, 14, (in)[ 9], 3); + ROUND2(A, B, C, D, E, RF3, 12, (in)[11], 3); + ROUND2(E, A, B, C, D, RF3, 13, (in)[ 8], 3); + ROUND2(D, E, A, B, C, RF3, 5, (in)[12], 3); + ROUND2(C, D, E, A, B, RF3, 14, (in)[ 2], 3); + ROUND2(B, C, D, E, A, RF3, 13, (in)[10], 3); + ROUND2(A, B, C, D, E, RF3, 13, (in)[ 0], 3); + ROUND2(E, A, B, C, D, RF3, 7, (in)[ 4], 3); + ROUND2(D, E, A, B, C, RF3, 5, (in)[13], 3); + + ROUND2(C, D, E, A, B, RF2, 15, (in)[ 8], 4); + ROUND2(B, C, D, E, A, RF2, 5, (in)[ 6], 4); + ROUND2(A, B, C, D, E, RF2, 8, (in)[ 4], 4); + ROUND2(E, A, B, C, D, RF2, 11, (in)[ 1], 4); + ROUND2(D, E, A, B, C, RF2, 14, (in)[ 3], 4); + ROUND2(C, D, E, A, B, RF2, 14, (in)[11], 4); + ROUND2(B, C, D, E, A, RF2, 6, (in)[15], 4); + ROUND2(A, B, C, D, E, RF2, 14, (in)[ 0], 4); + ROUND2(E, A, B, C, D, RF2, 6, (in)[ 5], 4); + ROUND2(D, E, A, B, C, RF2, 9, (in)[12], 4); + ROUND2(C, D, E, A, B, RF2, 12, (in)[ 2], 4); + ROUND2(B, C, D, E, A, RF2, 9, (in)[13], 4); + ROUND2(A, B, C, D, E, RF2, 12, (in)[ 9], 4); + ROUND2(E, A, B, C, D, RF2, 5, (in)[ 7], 4); + ROUND2(D, E, A, B, C, RF2, 15, (in)[10], 4); + ROUND2(C, D, E, A, B, RF2, 8, (in)[14], 4); + + ROUND2(B, C, D, E, A, RF1, 8, (in)[12], 5); + ROUND2(A, B, C, D, E, RF1, 5, (in)[15], 5); + ROUND2(E, A, B, C, D, RF1, 12, (in)[10], 5); + ROUND2(D, E, A, B, C, RF1, 9, (in)[ 4], 5); + ROUND2(C, D, E, A, B, RF1, 12, (in)[ 1], 5); + ROUND2(B, C, D, E, A, RF1, 5, (in)[ 5], 5); + ROUND2(A, B, C, D, E, RF1, 14, (in)[ 8], 5); + ROUND2(E, A, B, C, D, RF1, 6, (in)[ 7], 5); + ROUND2(D, E, A, B, C, RF1, 8, (in)[ 6], 5); + ROUND2(C, D, E, A, B, RF1, 13, (in)[ 2], 5); + ROUND2(B, C, D, E, A, RF1, 6, (in)[13], 5); + ROUND2(A, B, C, D, E, RF1, 5, (in)[14], 5); + ROUND2(E, A, B, C, D, RF1, 15, (in)[ 0], 5); + ROUND2(D, E, A, B, C, RF1, 13, (in)[ 3], 5); + ROUND2(C, D, E, A, B, RF1, 11, (in)[ 9], 5); + ROUND2(B, C, D, E, A, RF1, 11, (in)[11], 5); + + tmp = (h)[1] + C1 + D2; + (h)[1] = (h)[2] + D1 + E2; + (h)[2] = (h)[3] + E1 + A2; + (h)[3] = (h)[4] + A1 + B2; + (h)[4] = (h)[0] + B1 + C2; + (h)[0] = tmp; +} diff --git a/kernel/sha256.cl b/kernel/sha256.cl new file mode 100644 index 00000000..2eab642b --- /dev/null +++ b/kernel/sha256.cl @@ -0,0 +1,150 @@ +#define ROL32(x, y) rotate(x, y ## U) +#define SHR(x, y) (x >> y) +#define SWAP32(x) as_uint(as_uchar4(x).s3210) +#define SWAP64(x) as_ulong(as_uchar8(x).s76543210) + +#define S0(x) (ROL32(x, 25) ^ ROL32(x, 14) ^ SHR(x, 3)) +#define S1(x) (ROL32(x, 15) ^ ROL32(x, 13) ^ SHR(x, 10)) + +#define S2(x) (ROL32(x, 30) ^ ROL32(x, 19) ^ ROL32(x, 10)) +#define S3(x) (ROL32(x, 26) ^ ROL32(x, 21) ^ ROL32(x, 7)) + +#define P(a,b,c,d,e,f,g,h,x,K) \ +{ \ + temp1 = h + S3(e) + F1(e,f,g) + (K + x); \ + d += temp1; h = temp1 + S2(a) + F0(a,b,c); \ +} + +#define F0(y, x, z) bitselect(z, y, z ^ x) +#define F1(x, y, z) bitselect(z, y, x) + +#define R0 (W0 = S1(W14) + W9 + S0(W1) + W0) +#define R1 (W1 = S1(W15) + W10 + S0(W2) + W1) +#define R2 (W2 = S1(W0) + W11 + S0(W3) + W2) +#define R3 (W3 = S1(W1) + W12 + S0(W4) + W3) +#define R4 (W4 = S1(W2) + W13 + S0(W5) + W4) +#define R5 (W5 = S1(W3) + W14 + S0(W6) + W5) +#define R6 (W6 = S1(W4) + W15 + S0(W7) + W6) +#define R7 (W7 = S1(W5) + W0 + S0(W8) + W7) +#define R8 (W8 = S1(W6) + W1 + S0(W9) + W8) +#define R9 (W9 = S1(W7) + W2 + S0(W10) + W9) +#define R10 (W10 = S1(W8) + W3 + S0(W11) + W10) +#define R11 (W11 = S1(W9) + W4 + S0(W12) + W11) +#define R12 (W12 = S1(W10) + W5 + S0(W13) + W12) +#define R13 (W13 = S1(W11) + W6 + S0(W14) + W13) +#define R14 (W14 = S1(W12) + W7 + S0(W15) + W14) +#define R15 (W15 = S1(W13) + W8 + S0(W0) + W15) + +#define RD14 (S1(W12) + W7 + S0(W15) + W14) +#define RD15 (S1(W13) + W8 + S0(W0) + W15) + + +inline uint8 sha256_round(uint16 data, uint8 buf) +{ + uint temp1; + uint8 res; + uint W0 = (data.s0); + uint W1 = (data.s1); + uint W2 = (data.s2); + uint W3 = (data.s3); + uint W4 = (data.s4); + uint W5 = (data.s5); + uint W6 = (data.s6); + uint W7 = (data.s7); + uint W8 = (data.s8); + uint W9 = (data.s9); + uint W10 = (data.sA); + uint W11 = (data.sB); + uint W12 = (data.sC); + uint W13 = (data.sD); + uint W14 = (data.sE); + uint W15 = (data.sF); + + uint v0 = buf.s0; + uint v1 = buf.s1; + uint v2 = buf.s2; + uint v3 = buf.s3; + uint v4 = buf.s4; + uint v5 = buf.s5; + uint v6 = buf.s6; + uint v7 = buf.s7; + + P(v0, v1, v2, v3, v4, v5, v6, v7, W0, 0x428A2F98); + P(v7, v0, v1, v2, v3, v4, v5, v6, W1, 0x71374491); + P(v6, v7, v0, v1, v2, v3, v4, v5, W2, 0xB5C0FBCF); + P(v5, v6, v7, v0, v1, v2, v3, v4, W3, 0xE9B5DBA5); + P(v4, v5, v6, v7, v0, v1, v2, v3, W4, 0x3956C25B); + P(v3, v4, v5, v6, v7, v0, v1, v2, W5, 0x59F111F1); + P(v2, v3, v4, v5, v6, v7, v0, v1, W6, 0x923F82A4); + P(v1, v2, v3, v4, v5, v6, v7, v0, W7, 0xAB1C5ED5); + P(v0, v1, v2, v3, v4, v5, v6, v7, W8, 0xD807AA98); + P(v7, v0, v1, v2, v3, v4, v5, v6, W9, 0x12835B01); + P(v6, v7, v0, v1, v2, v3, v4, v5, W10, 0x243185BE); + P(v5, v6, v7, v0, v1, v2, v3, v4, W11, 0x550C7DC3); + P(v4, v5, v6, v7, v0, v1, v2, v3, W12, 0x72BE5D74); + P(v3, v4, v5, v6, v7, v0, v1, v2, W13, 0x80DEB1FE); + P(v2, v3, v4, v5, v6, v7, v0, v1, W14, 0x9BDC06A7); + P(v1, v2, v3, v4, v5, v6, v7, v0, W15, 0xC19BF174); + + P(v0, v1, v2, v3, v4, v5, v6, v7, R0, 0xE49B69C1); + P(v7, v0, v1, v2, v3, v4, v5, v6, R1, 0xEFBE4786); + P(v6, v7, v0, v1, v2, v3, v4, v5, R2, 0x0FC19DC6); + P(v5, v6, v7, v0, v1, v2, v3, v4, R3, 0x240CA1CC); + P(v4, v5, v6, v7, v0, v1, v2, v3, R4, 0x2DE92C6F); + P(v3, v4, v5, v6, v7, v0, v1, v2, R5, 0x4A7484AA); + P(v2, v3, v4, v5, v6, v7, v0, v1, R6, 0x5CB0A9DC); + P(v1, v2, v3, v4, v5, v6, v7, v0, R7, 0x76F988DA); + P(v0, v1, v2, v3, v4, v5, v6, v7, R8, 0x983E5152); + P(v7, v0, v1, v2, v3, v4, v5, v6, R9, 0xA831C66D); + P(v6, v7, v0, v1, v2, v3, v4, v5, R10, 0xB00327C8); + P(v5, v6, v7, v0, v1, v2, v3, v4, R11, 0xBF597FC7); + P(v4, v5, v6, v7, v0, v1, v2, v3, R12, 0xC6E00BF3); + P(v3, v4, v5, v6, v7, v0, v1, v2, R13, 0xD5A79147); + P(v2, v3, v4, v5, v6, v7, v0, v1, R14, 0x06CA6351); + P(v1, v2, v3, v4, v5, v6, v7, v0, R15, 0x14292967); + + P(v0, v1, v2, v3, v4, v5, v6, v7, R0, 0x27B70A85); + P(v7, v0, v1, v2, v3, v4, v5, v6, R1, 0x2E1B2138); + P(v6, v7, v0, v1, v2, v3, v4, v5, R2, 0x4D2C6DFC); + P(v5, v6, v7, v0, v1, v2, v3, v4, R3, 0x53380D13); + P(v4, v5, v6, v7, v0, v1, v2, v3, R4, 0x650A7354); + P(v3, v4, v5, v6, v7, v0, v1, v2, R5, 0x766A0ABB); + P(v2, v3, v4, v5, v6, v7, v0, v1, R6, 0x81C2C92E); + P(v1, v2, v3, v4, v5, v6, v7, v0, R7, 0x92722C85); + P(v0, v1, v2, v3, v4, v5, v6, v7, R8, 0xA2BFE8A1); + P(v7, v0, v1, v2, v3, v4, v5, v6, R9, 0xA81A664B); + P(v6, v7, v0, v1, v2, v3, v4, v5, R10, 0xC24B8B70); + P(v5, v6, v7, v0, v1, v2, v3, v4, R11, 0xC76C51A3); + P(v4, v5, v6, v7, v0, v1, v2, v3, R12, 0xD192E819); + P(v3, v4, v5, v6, v7, v0, v1, v2, R13, 0xD6990624); + P(v2, v3, v4, v5, v6, v7, v0, v1, R14, 0xF40E3585); + P(v1, v2, v3, v4, v5, v6, v7, v0, R15, 0x106AA070); + + P(v0, v1, v2, v3, v4, v5, v6, v7, R0, 0x19A4C116); + P(v7, v0, v1, v2, v3, v4, v5, v6, R1, 0x1E376C08); + P(v6, v7, v0, v1, v2, v3, v4, v5, R2, 0x2748774C); + P(v5, v6, v7, v0, v1, v2, v3, v4, R3, 0x34B0BCB5); + P(v4, v5, v6, v7, v0, v1, v2, v3, R4, 0x391C0CB3); + P(v3, v4, v5, v6, v7, v0, v1, v2, R5, 0x4ED8AA4A); + P(v2, v3, v4, v5, v6, v7, v0, v1, R6, 0x5B9CCA4F); + P(v1, v2, v3, v4, v5, v6, v7, v0, R7, 0x682E6FF3); + P(v0, v1, v2, v3, v4, v5, v6, v7, R8, 0x748F82EE); + P(v7, v0, v1, v2, v3, v4, v5, v6, R9, 0x78A5636F); + P(v6, v7, v0, v1, v2, v3, v4, v5, R10, 0x84C87814); + P(v5, v6, v7, v0, v1, v2, v3, v4, R11, 0x8CC70208); + P(v4, v5, v6, v7, v0, v1, v2, v3, R12, 0x90BEFFFA); + P(v3, v4, v5, v6, v7, v0, v1, v2, R13, 0xA4506CEB); + P(v2, v3, v4, v5, v6, v7, v0, v1, RD14, 0xBEF9A3F7); + P(v1, v2, v3, v4, v5, v6, v7, v0, RD15, 0xC67178F2); + + res.s0 = (v0 + buf.s0); + res.s1 = (v1 + buf.s1); + res.s2 = (v2 + buf.s2); + res.s3 = (v3 + buf.s3); + res.s4 = (v4 + buf.s4); + res.s5 = (v5 + buf.s5); + res.s6 = (v6 + buf.s6); + res.s7 = (v7 + buf.s7); + return (res); +} + diff --git a/kernel/wolf-sha512.cl b/kernel/wolf-sha512.cl new file mode 100644 index 00000000..72454310 --- /dev/null +++ b/kernel/wolf-sha512.cl @@ -0,0 +1,108 @@ +static const __constant ulong K512[80] = +{ + 0x428A2F98D728AE22UL, 0x7137449123EF65CDUL, + 0xB5C0FBCFEC4D3B2FUL, 0xE9B5DBA58189DBBCUL, + 0x3956C25BF348B538UL, 0x59F111F1B605D019UL, + 0x923F82A4AF194F9BUL, 0xAB1C5ED5DA6D8118UL, + 0xD807AA98A3030242UL, 0x12835B0145706FBEUL, + 0x243185BE4EE4B28CUL, 0x550C7DC3D5FFB4E2UL, + 0x72BE5D74F27B896FUL, 0x80DEB1FE3B1696B1UL, + 0x9BDC06A725C71235UL, 0xC19BF174CF692694UL, + 0xE49B69C19EF14AD2UL, 0xEFBE4786384F25E3UL, + 0x0FC19DC68B8CD5B5UL, 0x240CA1CC77AC9C65UL, + 0x2DE92C6F592B0275UL, 0x4A7484AA6EA6E483UL, + 0x5CB0A9DCBD41FBD4UL, 0x76F988DA831153B5UL, + 0x983E5152EE66DFABUL, 0xA831C66D2DB43210UL, + 0xB00327C898FB213FUL, 0xBF597FC7BEEF0EE4UL, + 0xC6E00BF33DA88FC2UL, 0xD5A79147930AA725UL, + 0x06CA6351E003826FUL, 0x142929670A0E6E70UL, + 0x27B70A8546D22FFCUL, 0x2E1B21385C26C926UL, + 0x4D2C6DFC5AC42AEDUL, 0x53380D139D95B3DFUL, + 0x650A73548BAF63DEUL, 0x766A0ABB3C77B2A8UL, + 0x81C2C92E47EDAEE6UL, 0x92722C851482353BUL, + 0xA2BFE8A14CF10364UL, 0xA81A664BBC423001UL, + 0xC24B8B70D0F89791UL, 0xC76C51A30654BE30UL, + 0xD192E819D6EF5218UL, 0xD69906245565A910UL, + 0xF40E35855771202AUL, 0x106AA07032BBD1B8UL, + 0x19A4C116B8D2D0C8UL, 0x1E376C085141AB53UL, + 0x2748774CDF8EEB99UL, 0x34B0BCB5E19B48A8UL, + 0x391C0CB3C5C95A63UL, 0x4ED8AA4AE3418ACBUL, + 0x5B9CCA4F7763E373UL, 0x682E6FF3D6B2B8A3UL, + 0x748F82EE5DEFB2FCUL, 0x78A5636F43172F60UL, + 0x84C87814A1F0AB72UL, 0x8CC702081A6439ECUL, + 0x90BEFFFA23631E28UL, 0xA4506CEBDE82BDE9UL, + 0xBEF9A3F7B2C67915UL, 0xC67178F2E372532BUL, + 0xCA273ECEEA26619CUL, 0xD186B8C721C0C207UL, + 0xEADA7DD6CDE0EB1EUL, 0xF57D4F7FEE6ED178UL, + 0x06F067AA72176FBAUL, 0x0A637DC5A2C898A6UL, + 0x113F9804BEF90DAEUL, 0x1B710B35131C471BUL, + 0x28DB77F523047D84UL, 0x32CAAB7B40C72493UL, + 0x3C9EBE0A15C9BEBCUL, 0x431D67C49C100D4CUL, + 0x4CC5D4BECB3E42B6UL, 0x597F299CFC657E2AUL, + 0x5FCB6FAB3AD6FAECUL, 0x6C44198C4A475817UL +}; + +static const __constant ulong SHA512_INIT[8] = +{ + 0x6A09E667F3BCC908UL, 0xBB67AE8584CAA73BUL, + 0x3C6EF372FE94F82BUL, 0xA54FF53A5F1D36F1UL, + 0x510E527FADE682D1UL, 0x9B05688C2B3E6C1FUL, + 0x1F83D9ABFB41BD6BUL, 0x5BE0CD19137E2179UL +}; + +#define ROTR64(x, y) rotate((x), 64UL - (y)) + +#ifndef AMD + #define BSG5_0(x) (ROTR64(x,28) ^ ROTR64(x,34) ^ ROTR64(x,39)) + #define BSG5_1(x) (ROTR64(x,14) ^ ROTR64(x,18) ^ ROTR64(x,41)) + #define SSG5_0(x) (ROTR64(x, 1) ^ ROTR64(x ,8) ^ ((x) >> 7)) + #define SSG5_1(x) (ROTR64(x,19) ^ ROTR64(x,61) ^ ((x) >> 6)) +#else + ulong FAST_ROTR64_LO(const uint2 x, const uint y) { return(as_ulong(amd_bitalign(x.s10, x, y))); } + ulong FAST_ROTR64_HI(const uint2 x, const uint y) { return(as_ulong(amd_bitalign(x, x.s10, (y - 32)))); } + + #define BSG5_0(x) (FAST_ROTR64_LO(as_uint2(x), 28) ^ FAST_ROTR64_HI(as_uint2(x), 34) ^ FAST_ROTR64_HI(as_uint2(x), 39)) + #define BSG5_1(x) (FAST_ROTR64_LO(as_uint2(x), 14) ^ FAST_ROTR64_LO(as_uint2(x), 18) ^ FAST_ROTR64_HI(as_uint2(x), 41)) + #define SSG5_0(x) (FAST_ROTR64_LO(as_uint2(x), 1) ^ FAST_ROTR64_LO(as_uint2(x), 8) ^ ((x) >> 7)) + #define SSG5_1(x) (FAST_ROTR64_LO(as_uint2(x), 19) ^ FAST_ROTR64_HI(as_uint2(x), 61) ^ ((x) >> 6)) +#endif + +#define CH(X, Y, Z) bitselect(Z, Y, X) +#define SHA_MAJ(X, Y, Z) CH((X ^ Z), Y, Z) + +void SHA2_512_STEP2(const ulong *W, uint ord, ulong *r, int i) +{ + ulong T1; + int x = 8 - ord; + + ulong a = r[x & 7], b = r[(x + 1) & 7], c = r[(x + 2) & 7], d = r[(x + 3) & 7]; + ulong e = r[(x + 4) & 7], f = r[(x + 5) & 7], g = r[(x + 6) & 7], h = r[(x + 7) & 7]; + + T1 = h + BSG5_1(e) + CH(e, f, g) + W[i] + K512[i]; + r[(3 + x) & 7] = d + T1; + r[(7 + x) & 7] = T1 + BSG5_0(a) + SHA_MAJ(a, b, c); +} + +void SHA512Block(ulong *data, ulong *buf) +{ + ulong W[80], r[8]; + + for(int i = 0; i < 8; ++i) r[i] = buf[i]; + + for(int i = 0; i < 16; ++i) W[i] = data[i]; + + #pragma unroll 4 + for(int i = 16; i < 80; ++i) W[i] = SSG5_1(W[i - 2]) + W[i - 7] + SSG5_0(W[i - 15]) + W[i - 16]; + + #pragma unroll 1 + for(int i = 0; i < 80; i += 8) + { + #pragma unroll + for(int j = 0; j < 8; ++j) + { + SHA2_512_STEP2(W, j, r, i + j); + } + } + + for(int i = 0; i < 8; ++i) buf[i] += r[i]; +} \ No newline at end of file diff --git a/miner.h b/miner.h index 4a8ee989..4826e6a0 100644 --- a/miner.h +++ b/miner.h @@ -769,6 +769,16 @@ static inline void flip80(void *dest_p, const void *src_p) dest[i] = swab32(src[i]); } +static inline void flip112(void *dest_p, const void *src_p) +{ + uint32_t *dest = (uint32_t *)dest_p; + const uint32_t *src = (uint32_t *)src_p; + int i; + + for (i = 0; i < 28; i++) + dest[i] = swab32(src[i]); +} + static inline void flip128(void *dest_p, const void *src_p) { uint32_t *dest = (uint32_t *)dest_p; @@ -1333,6 +1343,8 @@ struct stratum_work { char *bbversion; char *nbit; char *ntime; + uint8_t trie[32]; + bool has_trie; bool clean; size_t cb_len; @@ -1486,7 +1498,6 @@ struct pool { char *coinbasetxn; char *longpollid; char *gbt_workid; - int gbt_expires; uint32_t gbt_version; uint32_t curtime; uint32_t gbt_bits; @@ -1494,7 +1505,7 @@ struct pool { size_t gbt_txns; size_t coinbase_len; - /* equihash GBT */ + /* equihash GBT or LBRY claim hash */ unsigned char reserved[32]; /* Shared by both stratum & GBT */ diff --git a/ocl.c b/ocl.c index f75c8d57..ce063247 100644 --- a/ocl.c +++ b/ocl.c @@ -935,6 +935,9 @@ out: case ALGO_ETHASH: readbufsize = 32; break; + case ALGO_LBRY: + readbufsize = 112; + break; default: readbufsize = 128; } diff --git a/sgminer.c b/sgminer.c index 45993769..22a3b291 100644 --- a/sgminer.c +++ b/sgminer.c @@ -2234,6 +2234,18 @@ static void gen_gbt_work(struct pool *pool, struct work *work) //set solutionsize - supposed to always be 1344 add_var_int(work->data + offsetNonce + 32, 1344); } + else if (pool->algorithm.type == ALGO_LBRY) { + headerLen = 112; + offsetTime = 100; + offsetBits = 104; + offsetNonce = 108; + nonceLen = 4; + + offsetPadding = headerLen; + lenPadding = 168 - offsetPadding; + + memcpy(work->data + offsetMerkleRoot + 32, pool->reserved, 32); + } else { headerLen = 128; offsetTime = offsetMerkleRoot + 32; @@ -2314,7 +2326,6 @@ static bool gbt_decode(struct pool *pool, json_t *res_val) const char *longpollid; const char *reserved; unsigned char hash_swap[32]; - int expires; int version; int curtime; bool submitold; @@ -2329,11 +2340,10 @@ static bool gbt_decode(struct pool *pool, json_t *res_val) const char *coinbasefrscript; previousblockhash = json_string_value(json_object_get(res_val, "previousblockhash")); - reserved = json_string_value(json_object_get(res_val, "reserved")); + reserved = json_string_value(json_object_get(res_val, pool->algorithm.type == ALGO_LBRY ? "claimtrie" : "reserved")); target = json_string_value(json_object_get(res_val, "target")); coinbasetxn = json_string_value(json_object_get(json_object_get(res_val, "coinbasetxn"), "data")); longpollid = json_string_value(json_object_get(res_val, "longpollid")); - expires = json_integer_value(json_object_get(res_val, "expires")); version = json_integer_value(json_object_get(res_val, "version")); curtime = json_integer_value(json_object_get(res_val, "curtime")); submitold = json_is_true(json_object_get(res_val, "submitold")); @@ -2342,6 +2352,7 @@ static bool gbt_decode(struct pool *pool, json_t *res_val) bool invalid = false; if (pool->algorithm.type == ALGO_EQUIHASH) { + applog(LOG_DEBUG, "USING EQUIHASH"); height = json_integer_value(json_object_get(res_val, "height")); coinbasevalue = json_integer_value(json_object_get(res_val, "coinbasevalue")); coinbasefrvalue = json_integer_value(json_object_get(res_val, "coinbasefrvalue")); @@ -2349,7 +2360,26 @@ static bool gbt_decode(struct pool *pool, json_t *res_val) invalid = (!previousblockhash || !target || !coinbasevalue || !height || !longpollid || !version || !curtime || !bits); } else { - invalid = (!previousblockhash || !target || !coinbasetxn || !longpollid || !expires || !version || !curtime || !bits); + invalid = (!previousblockhash || !target || !coinbasetxn || !longpollid || !version || !curtime || !bits); + } + + if (previousblockhash) + applog(LOG_DEBUG, "previousblockhash: %s", previousblockhash); + if (reserved) + applog(LOG_DEBUG, "reserved: %s", reserved); + if (target) + applog(LOG_DEBUG, "target: %s", target); + if (coinbasetxn) + applog(LOG_DEBUG, "coinbasetxn: %s", coinbasetxn); + if (longpollid) + applog(LOG_DEBUG, "longpollid: %s", longpollid); + applog(LOG_DEBUG, "version: %d", version); + applog(LOG_DEBUG, "curtime: %d", curtime); + applog(LOG_DEBUG, "submitold: %s", submitold ? "true" : "false"); + if (bits) + applog(LOG_DEBUG, "bits: %s", bits); + if (workid) { + applog(LOG_DEBUG, "workid: %s, %s", workid, !workid ? "true" : "false"); } if (invalid) { @@ -2357,24 +2387,6 @@ static bool gbt_decode(struct pool *pool, json_t *res_val) return false; } - applog(LOG_DEBUG, "previousblockhash: %s", previousblockhash); - if (reserved) { - applog(LOG_DEBUG, "reserved: %s", reserved); - } - applog(LOG_DEBUG, "target: %s", target); - applog(LOG_DEBUG, "coinbasetxn: %s", coinbasetxn); - applog(LOG_DEBUG, "longpollid: %s", longpollid); - if (expires) { - applog(LOG_DEBUG, "expires: %d", expires); - } - applog(LOG_DEBUG, "version: %d", version); - applog(LOG_DEBUG, "curtime: %d", curtime); - applog(LOG_DEBUG, "submitold: %s", submitold ? "true" : "false"); - applog(LOG_DEBUG, "bits: %s", bits); - if (workid) { - applog(LOG_DEBUG, "workid: %s", workid); - } - cg_wlock(&pool->gbt_lock); if (pool->algorithm.type == ALGO_EQUIHASH) { @@ -2429,7 +2441,6 @@ static bool gbt_decode(struct pool *pool, json_t *res_val) applog(LOG_DEBUG, "swab256_target: %s", bin2hex(pool->gbt_target,32)); - pool->gbt_expires = expires; pool->gbt_version = htobe32(version); pool->curtime = htobe32(curtime); pool->submit_old = submitold; @@ -3346,37 +3357,46 @@ static bool submit_upstream_work(struct work *work, CURL *curl, char *curl_err_s endian_flip128(work->data, work->data); /* build hex string - Make sure to restrict to 80 bytes for Neoscrypt */ - int datasize = 128; + int datasize = 80; if (work->pool->algorithm.type == ALGO_NEOSCRYPT) datasize = 80; else if (work->pool->algorithm.type == ALGO_CRE) datasize = 168; - hexstr = bin2hex(work->data, datasize); + else if (work->pool->algorithm.type == ALGO_LBRY) + datasize = 112; /* build JSON-RPC request */ if (work->gbt) { char *gbt_block, *varint; - unsigned char data[80]; - flip80(data, work->data); - gbt_block = bin2hex(data, 80); + if (datasize == 80) + flip80(work->data, work->data); + else if (datasize == 112) + flip112(work->data, work->data); + else if (datasize == 128) + flip128(work->data, work->data); + else if (datasize == 168) + flip168(work->data, work->data); + else + applog_and_exit("Unimplemented flip handler for %d", datasize); + gbt_block = bin2hex(work->data, datasize); if (work->gbt_txns < 0xfd) { - uint8_t val = work->gbt_txns; + uint8_t valx = work->gbt_txns; - varint = bin2hex((const unsigned char *)&val, 1); + varint = bin2hex((const unsigned char *)&valx, 1); } else if (work->gbt_txns <= 0xffff) { - uint16_t val = htole16(work->gbt_txns); + uint16_t valx = htole16(work->gbt_txns); gbt_block = (char *)realloc_strcat(gbt_block, "fd"); - varint = bin2hex((const unsigned char *)&val, 2); + varint = bin2hex((const unsigned char *)&valx, 2); } else { - uint32_t val = htole32(work->gbt_txns); + uint32_t valx = htole32(work->gbt_txns); gbt_block = (char *)realloc_strcat(gbt_block, "fe"); - varint = bin2hex((const unsigned char *)&val, 4); + varint = bin2hex((const unsigned char *)&valx, 4); } gbt_block = (char *)realloc_strcat(gbt_block, varint); free(varint); @@ -3395,7 +3415,9 @@ static bool submit_upstream_work(struct work *work, CURL *curl, char *curl_err_s } else { s = strdup("{\"method\": \"getwork\", \"params\": [ \""); + hexstr = bin2hex(work->data, MAX(datasize, 128)); s = (char *)realloc_strcat(s, hexstr); + free(hexstr); s = (char *)realloc_strcat(s, "\" ], \"id\":1}"); } } @@ -3409,7 +3431,7 @@ static bool submit_upstream_work(struct work *work, CURL *curl, char *curl_err_s free(s); if (unlikely(!val)) { - applog(LOG_INFO, "submit_upstream_work json_rpc_call failed"); + applog(LOG_INFO, "submit_upstream_work json_rpc_call failed. Error: %s", curl_err_str); if (!pool_tset(pool, &pool->submit_fail)) { total_ro++; pool->remotefail_occasions++; @@ -3502,7 +3524,6 @@ static bool submit_upstream_work(struct work *work, CURL *curl, char *curl_err_s rc = true; out: - free(hexstr); return rc; } @@ -6026,6 +6047,9 @@ static void *stratum_sthread(void *userdata) free(nonce); free(solution); } + else if (pool->algorithm.type == ALGO_LBRY) { + nonce = *((uint32_t *)(work->data + 108)); + } else { if (unlikely(work->nonce2_len > 8)) { applog(LOG_ERR, "%s asking for inappropriately long nonce2 length %d", get_pool_name(pool), (int)work->nonce2_len); @@ -7494,7 +7518,8 @@ static void rebuild_nonce(struct work *work, uint32_t nonce) nonce_pos = 140; else if (work->pool->algorithm.type == ALGO_CRYPTONIGHT) nonce_pos = 39; - + else if(work->pool->algorithm.type == ALGO_LBRY) + nonce_pos = 108; if (work->pool->algorithm.type == ALGO_ETHASH) { work->Nonce += nonce; } diff --git a/sph/Makefile.am b/sph/Makefile.am index bc2f4b23..2c2a6950 100644 --- a/sph/Makefile.am +++ b/sph/Makefile.am @@ -1,3 +1,3 @@ noinst_LIBRARIES = libsph.a -libsph_a_SOURCES = bmw.c echo.c jh.c luffa.c simd.c blake.c cubehash.c groestl.c keccak.c shavite.c skein.c sha2.c sha2big.c fugue.c hamsi.c panama.c shabal.c whirlpool.c sha256_Y.c +libsph_a_SOURCES = bmw.c echo.c jh.c luffa.c simd.c blake.c cubehash.c groestl.c keccak.c shavite.c skein.c sha2.c sha2big.c fugue.c hamsi.c panama.c shabal.c whirlpool.c sha256_Y.c ripemd.c diff --git a/sph/ripemd.c b/sph/ripemd.c new file mode 100644 index 00000000..1ce5c315 --- /dev/null +++ b/sph/ripemd.c @@ -0,0 +1,834 @@ +/* $Id: ripemd.c 216 2010-06-08 09:46:57Z tp $ */ +/* + * RIPEMD-160 implementation. + * + * ==========================(LICENSE BEGIN)============================ + * + * Copyright (c) 2007-2010 Projet RNRT SAPHIR + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * ===========================(LICENSE END)============================= + * + * @author Thomas Pornin + */ + +#include +#include + +#include "sph_ripemd.h" + +/* + * Round functions for RIPEMD (original). + */ +#define F(x, y, z) ((((y) ^ (z)) & (x)) ^ (z)) +#define G(x, y, z) (((x) & (y)) | (((x) | (y)) & (z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) + +static const sph_u32 oIV[5] = { + SPH_C32(0x67452301), SPH_C32(0xEFCDAB89), + SPH_C32(0x98BADCFE), SPH_C32(0x10325476) +}; + +/* + * Round functions for RIPEMD-128 and RIPEMD-160. + */ +#define F1(x, y, z) ((x) ^ (y) ^ (z)) +#define F2(x, y, z) ((((y) ^ (z)) & (x)) ^ (z)) +#define F3(x, y, z) (((x) | ~(y)) ^ (z)) +#define F4(x, y, z) ((((x) ^ (y)) & (z)) ^ (y)) +#define F5(x, y, z) ((x) ^ ((y) | ~(z))) + +static const sph_u32 IV[5] = { + SPH_C32(0x67452301), SPH_C32(0xEFCDAB89), SPH_C32(0x98BADCFE), + SPH_C32(0x10325476), SPH_C32(0xC3D2E1F0) +}; + +#define ROTL SPH_ROTL32 + +/* ===================================================================== */ +/* + * RIPEMD (original hash, deprecated). + */ + +#define FF1(A, B, C, D, X, s) do { \ + sph_u32 tmp = SPH_T32((A) + F(B, C, D) + (X)); \ + (A) = ROTL(tmp, (s)); \ + } while (0) + +#define GG1(A, B, C, D, X, s) do { \ + sph_u32 tmp = SPH_T32((A) + G(B, C, D) \ + + (X) + SPH_C32(0x5A827999)); \ + (A) = ROTL(tmp, (s)); \ + } while (0) + +#define HH1(A, B, C, D, X, s) do { \ + sph_u32 tmp = SPH_T32((A) + H(B, C, D) \ + + (X) + SPH_C32(0x6ED9EBA1)); \ + (A) = ROTL(tmp, (s)); \ + } while (0) + +#define FF2(A, B, C, D, X, s) do { \ + sph_u32 tmp = SPH_T32((A) + F(B, C, D) \ + + (X) + SPH_C32(0x50A28BE6)); \ + (A) = ROTL(tmp, (s)); \ + } while (0) + +#define GG2(A, B, C, D, X, s) do { \ + sph_u32 tmp = SPH_T32((A) + G(B, C, D) + (X)); \ + (A) = ROTL(tmp, (s)); \ + } while (0) + +#define HH2(A, B, C, D, X, s) do { \ + sph_u32 tmp = SPH_T32((A) + H(B, C, D) \ + + (X) + SPH_C32(0x5C4DD124)); \ + (A) = ROTL(tmp, (s)); \ + } while (0) + +#define RIPEMD_ROUND_BODY(in, h) do { \ + sph_u32 A1, B1, C1, D1; \ + sph_u32 A2, B2, C2, D2; \ + sph_u32 tmp; \ + \ + A1 = A2 = (h)[0]; \ + B1 = B2 = (h)[1]; \ + C1 = C2 = (h)[2]; \ + D1 = D2 = (h)[3]; \ + \ + FF1(A1, B1, C1, D1, in( 0), 11); \ + FF1(D1, A1, B1, C1, in( 1), 14); \ + FF1(C1, D1, A1, B1, in( 2), 15); \ + FF1(B1, C1, D1, A1, in( 3), 12); \ + FF1(A1, B1, C1, D1, in( 4), 5); \ + FF1(D1, A1, B1, C1, in( 5), 8); \ + FF1(C1, D1, A1, B1, in( 6), 7); \ + FF1(B1, C1, D1, A1, in( 7), 9); \ + FF1(A1, B1, C1, D1, in( 8), 11); \ + FF1(D1, A1, B1, C1, in( 9), 13); \ + FF1(C1, D1, A1, B1, in(10), 14); \ + FF1(B1, C1, D1, A1, in(11), 15); \ + FF1(A1, B1, C1, D1, in(12), 6); \ + FF1(D1, A1, B1, C1, in(13), 7); \ + FF1(C1, D1, A1, B1, in(14), 9); \ + FF1(B1, C1, D1, A1, in(15), 8); \ + \ + GG1(A1, B1, C1, D1, in( 7), 7); \ + GG1(D1, A1, B1, C1, in( 4), 6); \ + GG1(C1, D1, A1, B1, in(13), 8); \ + GG1(B1, C1, D1, A1, in( 1), 13); \ + GG1(A1, B1, C1, D1, in(10), 11); \ + GG1(D1, A1, B1, C1, in( 6), 9); \ + GG1(C1, D1, A1, B1, in(15), 7); \ + GG1(B1, C1, D1, A1, in( 3), 15); \ + GG1(A1, B1, C1, D1, in(12), 7); \ + GG1(D1, A1, B1, C1, in( 0), 12); \ + GG1(C1, D1, A1, B1, in( 9), 15); \ + GG1(B1, C1, D1, A1, in( 5), 9); \ + GG1(A1, B1, C1, D1, in(14), 7); \ + GG1(D1, A1, B1, C1, in( 2), 11); \ + GG1(C1, D1, A1, B1, in(11), 13); \ + GG1(B1, C1, D1, A1, in( 8), 12); \ + \ + HH1(A1, B1, C1, D1, in( 3), 11); \ + HH1(D1, A1, B1, C1, in(10), 13); \ + HH1(C1, D1, A1, B1, in( 2), 14); \ + HH1(B1, C1, D1, A1, in( 4), 7); \ + HH1(A1, B1, C1, D1, in( 9), 14); \ + HH1(D1, A1, B1, C1, in(15), 9); \ + HH1(C1, D1, A1, B1, in( 8), 13); \ + HH1(B1, C1, D1, A1, in( 1), 15); \ + HH1(A1, B1, C1, D1, in(14), 6); \ + HH1(D1, A1, B1, C1, in( 7), 8); \ + HH1(C1, D1, A1, B1, in( 0), 13); \ + HH1(B1, C1, D1, A1, in( 6), 6); \ + HH1(A1, B1, C1, D1, in(11), 12); \ + HH1(D1, A1, B1, C1, in(13), 5); \ + HH1(C1, D1, A1, B1, in( 5), 7); \ + HH1(B1, C1, D1, A1, in(12), 5); \ + \ + FF2(A2, B2, C2, D2, in( 0), 11); \ + FF2(D2, A2, B2, C2, in( 1), 14); \ + FF2(C2, D2, A2, B2, in( 2), 15); \ + FF2(B2, C2, D2, A2, in( 3), 12); \ + FF2(A2, B2, C2, D2, in( 4), 5); \ + FF2(D2, A2, B2, C2, in( 5), 8); \ + FF2(C2, D2, A2, B2, in( 6), 7); \ + FF2(B2, C2, D2, A2, in( 7), 9); \ + FF2(A2, B2, C2, D2, in( 8), 11); \ + FF2(D2, A2, B2, C2, in( 9), 13); \ + FF2(C2, D2, A2, B2, in(10), 14); \ + FF2(B2, C2, D2, A2, in(11), 15); \ + FF2(A2, B2, C2, D2, in(12), 6); \ + FF2(D2, A2, B2, C2, in(13), 7); \ + FF2(C2, D2, A2, B2, in(14), 9); \ + FF2(B2, C2, D2, A2, in(15), 8); \ + \ + GG2(A2, B2, C2, D2, in( 7), 7); \ + GG2(D2, A2, B2, C2, in( 4), 6); \ + GG2(C2, D2, A2, B2, in(13), 8); \ + GG2(B2, C2, D2, A2, in( 1), 13); \ + GG2(A2, B2, C2, D2, in(10), 11); \ + GG2(D2, A2, B2, C2, in( 6), 9); \ + GG2(C2, D2, A2, B2, in(15), 7); \ + GG2(B2, C2, D2, A2, in( 3), 15); \ + GG2(A2, B2, C2, D2, in(12), 7); \ + GG2(D2, A2, B2, C2, in( 0), 12); \ + GG2(C2, D2, A2, B2, in( 9), 15); \ + GG2(B2, C2, D2, A2, in( 5), 9); \ + GG2(A2, B2, C2, D2, in(14), 7); \ + GG2(D2, A2, B2, C2, in( 2), 11); \ + GG2(C2, D2, A2, B2, in(11), 13); \ + GG2(B2, C2, D2, A2, in( 8), 12); \ + \ + HH2(A2, B2, C2, D2, in( 3), 11); \ + HH2(D2, A2, B2, C2, in(10), 13); \ + HH2(C2, D2, A2, B2, in( 2), 14); \ + HH2(B2, C2, D2, A2, in( 4), 7); \ + HH2(A2, B2, C2, D2, in( 9), 14); \ + HH2(D2, A2, B2, C2, in(15), 9); \ + HH2(C2, D2, A2, B2, in( 8), 13); \ + HH2(B2, C2, D2, A2, in( 1), 15); \ + HH2(A2, B2, C2, D2, in(14), 6); \ + HH2(D2, A2, B2, C2, in( 7), 8); \ + HH2(C2, D2, A2, B2, in( 0), 13); \ + HH2(B2, C2, D2, A2, in( 6), 6); \ + HH2(A2, B2, C2, D2, in(11), 12); \ + HH2(D2, A2, B2, C2, in(13), 5); \ + HH2(C2, D2, A2, B2, in( 5), 7); \ + HH2(B2, C2, D2, A2, in(12), 5); \ + \ + tmp = SPH_T32((h)[1] + C1 + D2); \ + (h)[1] = SPH_T32((h)[2] + D1 + A2); \ + (h)[2] = SPH_T32((h)[3] + A1 + B2); \ + (h)[3] = SPH_T32((h)[0] + B1 + C2); \ + (h)[0] = tmp; \ + } while (0) + +/* + * One round of RIPEMD. The data must be aligned for 32-bit access. + */ +static void +ripemd_round(const unsigned char *data, sph_u32 r[5]) +{ +#if SPH_LITTLE_FAST + +#define RIPEMD_IN(x) sph_dec32le_aligned(data + (4 * (x))) + +#else + + sph_u32 X_var[16]; + int i; + + for (i = 0; i < 16; i ++) + X_var[i] = sph_dec32le_aligned(data + 4 * i); +#define RIPEMD_IN(x) X_var[x] + +#endif + RIPEMD_ROUND_BODY(RIPEMD_IN, r); +#undef RIPEMD_IN +} + +/* see sph_ripemd.h */ +void +sph_ripemd_init(void *cc) +{ + sph_ripemd_context *sc; + + sc = cc; + memcpy(sc->val, oIV, sizeof sc->val); +#if SPH_64 + sc->count = 0; +#else + sc->count_high = sc->count_low = 0; +#endif +} + +#define RFUN ripemd_round +#define HASH ripemd +#define LE32 1 +#include "md_helper.c" +#undef RFUN +#undef HASH +#undef LE32 + +/* see sph_ripemd.h */ +void +sph_ripemd_close(void *cc, void *dst) +{ + ripemd_close(cc, dst, 4); + sph_ripemd_init(cc); +} + +/* see sph_ripemd.h */ +void +sph_ripemd_comp(const sph_u32 msg[16], sph_u32 val[4]) +{ +#define RIPEMD_IN(x) msg[x] + RIPEMD_ROUND_BODY(RIPEMD_IN, val); +#undef RIPEMD_IN +} + +/* ===================================================================== */ +/* + * RIPEMD-128. + */ + +/* + * Round constants for RIPEMD-128. + */ +#define sK11 SPH_C32(0x00000000) +#define sK12 SPH_C32(0x5A827999) +#define sK13 SPH_C32(0x6ED9EBA1) +#define sK14 SPH_C32(0x8F1BBCDC) + +#define sK21 SPH_C32(0x50A28BE6) +#define sK22 SPH_C32(0x5C4DD124) +#define sK23 SPH_C32(0x6D703EF3) +#define sK24 SPH_C32(0x00000000) + +#define sRR(a, b, c, d, f, s, r, k) do { \ + a = ROTL(SPH_T32(a + f(b, c, d) + r + k), s); \ + } while (0) + +#define sROUND1(a, b, c, d, f, s, r, k) \ + sRR(a ## 1, b ## 1, c ## 1, d ## 1, f, s, r, sK1 ## k) + +#define sROUND2(a, b, c, d, f, s, r, k) \ + sRR(a ## 2, b ## 2, c ## 2, d ## 2, f, s, r, sK2 ## k) + +/* + * This macro defines the body for a RIPEMD-128 compression function + * implementation. The "in" parameter should evaluate, when applied to a + * numerical input parameter from 0 to 15, to an expression which yields + * the corresponding input block. The "h" parameter should evaluate to + * an array or pointer expression designating the array of 4 words which + * contains the input and output of the compression function. + */ + +#define RIPEMD128_ROUND_BODY(in, h) do { \ + sph_u32 A1, B1, C1, D1; \ + sph_u32 A2, B2, C2, D2; \ + sph_u32 tmp; \ + \ + A1 = A2 = (h)[0]; \ + B1 = B2 = (h)[1]; \ + C1 = C2 = (h)[2]; \ + D1 = D2 = (h)[3]; \ + \ + sROUND1(A, B, C, D, F1, 11, in( 0), 1); \ + sROUND1(D, A, B, C, F1, 14, in( 1), 1); \ + sROUND1(C, D, A, B, F1, 15, in( 2), 1); \ + sROUND1(B, C, D, A, F1, 12, in( 3), 1); \ + sROUND1(A, B, C, D, F1, 5, in( 4), 1); \ + sROUND1(D, A, B, C, F1, 8, in( 5), 1); \ + sROUND1(C, D, A, B, F1, 7, in( 6), 1); \ + sROUND1(B, C, D, A, F1, 9, in( 7), 1); \ + sROUND1(A, B, C, D, F1, 11, in( 8), 1); \ + sROUND1(D, A, B, C, F1, 13, in( 9), 1); \ + sROUND1(C, D, A, B, F1, 14, in(10), 1); \ + sROUND1(B, C, D, A, F1, 15, in(11), 1); \ + sROUND1(A, B, C, D, F1, 6, in(12), 1); \ + sROUND1(D, A, B, C, F1, 7, in(13), 1); \ + sROUND1(C, D, A, B, F1, 9, in(14), 1); \ + sROUND1(B, C, D, A, F1, 8, in(15), 1); \ + \ + sROUND1(A, B, C, D, F2, 7, in( 7), 2); \ + sROUND1(D, A, B, C, F2, 6, in( 4), 2); \ + sROUND1(C, D, A, B, F2, 8, in(13), 2); \ + sROUND1(B, C, D, A, F2, 13, in( 1), 2); \ + sROUND1(A, B, C, D, F2, 11, in(10), 2); \ + sROUND1(D, A, B, C, F2, 9, in( 6), 2); \ + sROUND1(C, D, A, B, F2, 7, in(15), 2); \ + sROUND1(B, C, D, A, F2, 15, in( 3), 2); \ + sROUND1(A, B, C, D, F2, 7, in(12), 2); \ + sROUND1(D, A, B, C, F2, 12, in( 0), 2); \ + sROUND1(C, D, A, B, F2, 15, in( 9), 2); \ + sROUND1(B, C, D, A, F2, 9, in( 5), 2); \ + sROUND1(A, B, C, D, F2, 11, in( 2), 2); \ + sROUND1(D, A, B, C, F2, 7, in(14), 2); \ + sROUND1(C, D, A, B, F2, 13, in(11), 2); \ + sROUND1(B, C, D, A, F2, 12, in( 8), 2); \ + \ + sROUND1(A, B, C, D, F3, 11, in( 3), 3); \ + sROUND1(D, A, B, C, F3, 13, in(10), 3); \ + sROUND1(C, D, A, B, F3, 6, in(14), 3); \ + sROUND1(B, C, D, A, F3, 7, in( 4), 3); \ + sROUND1(A, B, C, D, F3, 14, in( 9), 3); \ + sROUND1(D, A, B, C, F3, 9, in(15), 3); \ + sROUND1(C, D, A, B, F3, 13, in( 8), 3); \ + sROUND1(B, C, D, A, F3, 15, in( 1), 3); \ + sROUND1(A, B, C, D, F3, 14, in( 2), 3); \ + sROUND1(D, A, B, C, F3, 8, in( 7), 3); \ + sROUND1(C, D, A, B, F3, 13, in( 0), 3); \ + sROUND1(B, C, D, A, F3, 6, in( 6), 3); \ + sROUND1(A, B, C, D, F3, 5, in(13), 3); \ + sROUND1(D, A, B, C, F3, 12, in(11), 3); \ + sROUND1(C, D, A, B, F3, 7, in( 5), 3); \ + sROUND1(B, C, D, A, F3, 5, in(12), 3); \ + \ + sROUND1(A, B, C, D, F4, 11, in( 1), 4); \ + sROUND1(D, A, B, C, F4, 12, in( 9), 4); \ + sROUND1(C, D, A, B, F4, 14, in(11), 4); \ + sROUND1(B, C, D, A, F4, 15, in(10), 4); \ + sROUND1(A, B, C, D, F4, 14, in( 0), 4); \ + sROUND1(D, A, B, C, F4, 15, in( 8), 4); \ + sROUND1(C, D, A, B, F4, 9, in(12), 4); \ + sROUND1(B, C, D, A, F4, 8, in( 4), 4); \ + sROUND1(A, B, C, D, F4, 9, in(13), 4); \ + sROUND1(D, A, B, C, F4, 14, in( 3), 4); \ + sROUND1(C, D, A, B, F4, 5, in( 7), 4); \ + sROUND1(B, C, D, A, F4, 6, in(15), 4); \ + sROUND1(A, B, C, D, F4, 8, in(14), 4); \ + sROUND1(D, A, B, C, F4, 6, in( 5), 4); \ + sROUND1(C, D, A, B, F4, 5, in( 6), 4); \ + sROUND1(B, C, D, A, F4, 12, in( 2), 4); \ + \ + sROUND2(A, B, C, D, F4, 8, in( 5), 1); \ + sROUND2(D, A, B, C, F4, 9, in(14), 1); \ + sROUND2(C, D, A, B, F4, 9, in( 7), 1); \ + sROUND2(B, C, D, A, F4, 11, in( 0), 1); \ + sROUND2(A, B, C, D, F4, 13, in( 9), 1); \ + sROUND2(D, A, B, C, F4, 15, in( 2), 1); \ + sROUND2(C, D, A, B, F4, 15, in(11), 1); \ + sROUND2(B, C, D, A, F4, 5, in( 4), 1); \ + sROUND2(A, B, C, D, F4, 7, in(13), 1); \ + sROUND2(D, A, B, C, F4, 7, in( 6), 1); \ + sROUND2(C, D, A, B, F4, 8, in(15), 1); \ + sROUND2(B, C, D, A, F4, 11, in( 8), 1); \ + sROUND2(A, B, C, D, F4, 14, in( 1), 1); \ + sROUND2(D, A, B, C, F4, 14, in(10), 1); \ + sROUND2(C, D, A, B, F4, 12, in( 3), 1); \ + sROUND2(B, C, D, A, F4, 6, in(12), 1); \ + \ + sROUND2(A, B, C, D, F3, 9, in( 6), 2); \ + sROUND2(D, A, B, C, F3, 13, in(11), 2); \ + sROUND2(C, D, A, B, F3, 15, in( 3), 2); \ + sROUND2(B, C, D, A, F3, 7, in( 7), 2); \ + sROUND2(A, B, C, D, F3, 12, in( 0), 2); \ + sROUND2(D, A, B, C, F3, 8, in(13), 2); \ + sROUND2(C, D, A, B, F3, 9, in( 5), 2); \ + sROUND2(B, C, D, A, F3, 11, in(10), 2); \ + sROUND2(A, B, C, D, F3, 7, in(14), 2); \ + sROUND2(D, A, B, C, F3, 7, in(15), 2); \ + sROUND2(C, D, A, B, F3, 12, in( 8), 2); \ + sROUND2(B, C, D, A, F3, 7, in(12), 2); \ + sROUND2(A, B, C, D, F3, 6, in( 4), 2); \ + sROUND2(D, A, B, C, F3, 15, in( 9), 2); \ + sROUND2(C, D, A, B, F3, 13, in( 1), 2); \ + sROUND2(B, C, D, A, F3, 11, in( 2), 2); \ + \ + sROUND2(A, B, C, D, F2, 9, in(15), 3); \ + sROUND2(D, A, B, C, F2, 7, in( 5), 3); \ + sROUND2(C, D, A, B, F2, 15, in( 1), 3); \ + sROUND2(B, C, D, A, F2, 11, in( 3), 3); \ + sROUND2(A, B, C, D, F2, 8, in( 7), 3); \ + sROUND2(D, A, B, C, F2, 6, in(14), 3); \ + sROUND2(C, D, A, B, F2, 6, in( 6), 3); \ + sROUND2(B, C, D, A, F2, 14, in( 9), 3); \ + sROUND2(A, B, C, D, F2, 12, in(11), 3); \ + sROUND2(D, A, B, C, F2, 13, in( 8), 3); \ + sROUND2(C, D, A, B, F2, 5, in(12), 3); \ + sROUND2(B, C, D, A, F2, 14, in( 2), 3); \ + sROUND2(A, B, C, D, F2, 13, in(10), 3); \ + sROUND2(D, A, B, C, F2, 13, in( 0), 3); \ + sROUND2(C, D, A, B, F2, 7, in( 4), 3); \ + sROUND2(B, C, D, A, F2, 5, in(13), 3); \ + \ + sROUND2(A, B, C, D, F1, 15, in( 8), 4); \ + sROUND2(D, A, B, C, F1, 5, in( 6), 4); \ + sROUND2(C, D, A, B, F1, 8, in( 4), 4); \ + sROUND2(B, C, D, A, F1, 11, in( 1), 4); \ + sROUND2(A, B, C, D, F1, 14, in( 3), 4); \ + sROUND2(D, A, B, C, F1, 14, in(11), 4); \ + sROUND2(C, D, A, B, F1, 6, in(15), 4); \ + sROUND2(B, C, D, A, F1, 14, in( 0), 4); \ + sROUND2(A, B, C, D, F1, 6, in( 5), 4); \ + sROUND2(D, A, B, C, F1, 9, in(12), 4); \ + sROUND2(C, D, A, B, F1, 12, in( 2), 4); \ + sROUND2(B, C, D, A, F1, 9, in(13), 4); \ + sROUND2(A, B, C, D, F1, 12, in( 9), 4); \ + sROUND2(D, A, B, C, F1, 5, in( 7), 4); \ + sROUND2(C, D, A, B, F1, 15, in(10), 4); \ + sROUND2(B, C, D, A, F1, 8, in(14), 4); \ + \ + tmp = SPH_T32((h)[1] + C1 + D2); \ + (h)[1] = SPH_T32((h)[2] + D1 + A2); \ + (h)[2] = SPH_T32((h)[3] + A1 + B2); \ + (h)[3] = SPH_T32((h)[0] + B1 + C2); \ + (h)[0] = tmp; \ + } while (0) + +/* + * One round of RIPEMD-128. The data must be aligned for 32-bit access. + */ +static void +ripemd128_round(const unsigned char *data, sph_u32 r[5]) +{ +#if SPH_LITTLE_FAST + +#define RIPEMD128_IN(x) sph_dec32le_aligned(data + (4 * (x))) + +#else + + sph_u32 X_var[16]; + int i; + + for (i = 0; i < 16; i ++) + X_var[i] = sph_dec32le_aligned(data + 4 * i); +#define RIPEMD128_IN(x) X_var[x] + +#endif + RIPEMD128_ROUND_BODY(RIPEMD128_IN, r); +#undef RIPEMD128_IN +} + +/* see sph_ripemd.h */ +void +sph_ripemd128_init(void *cc) +{ + sph_ripemd128_context *sc; + + sc = cc; + memcpy(sc->val, IV, sizeof sc->val); +#if SPH_64 + sc->count = 0; +#else + sc->count_high = sc->count_low = 0; +#endif +} + +#define RFUN ripemd128_round +#define HASH ripemd128 +#define LE32 1 +#include "md_helper.c" +#undef RFUN +#undef HASH +#undef LE32 + +/* see sph_ripemd.h */ +void +sph_ripemd128_close(void *cc, void *dst) +{ + ripemd128_close(cc, dst, 4); + sph_ripemd128_init(cc); +} + +/* see sph_ripemd.h */ +void +sph_ripemd128_comp(const sph_u32 msg[16], sph_u32 val[4]) +{ +#define RIPEMD128_IN(x) msg[x] + RIPEMD128_ROUND_BODY(RIPEMD128_IN, val); +#undef RIPEMD128_IN +} + +/* ===================================================================== */ +/* + * RIPEMD-160. + */ + +/* + * Round constants for RIPEMD-160. + */ +#define K11 SPH_C32(0x00000000) +#define K12 SPH_C32(0x5A827999) +#define K13 SPH_C32(0x6ED9EBA1) +#define K14 SPH_C32(0x8F1BBCDC) +#define K15 SPH_C32(0xA953FD4E) + +#define K21 SPH_C32(0x50A28BE6) +#define K22 SPH_C32(0x5C4DD124) +#define K23 SPH_C32(0x6D703EF3) +#define K24 SPH_C32(0x7A6D76E9) +#define K25 SPH_C32(0x00000000) + +#define RR(a, b, c, d, e, f, s, r, k) do { \ + a = SPH_T32(ROTL(SPH_T32(a + f(b, c, d) + r + k), s) + e); \ + c = ROTL(c, 10); \ + } while (0) + +#define ROUND1(a, b, c, d, e, f, s, r, k) \ + RR(a ## 1, b ## 1, c ## 1, d ## 1, e ## 1, f, s, r, K1 ## k) + +#define ROUND2(a, b, c, d, e, f, s, r, k) \ + RR(a ## 2, b ## 2, c ## 2, d ## 2, e ## 2, f, s, r, K2 ## k) + +/* + * This macro defines the body for a RIPEMD-160 compression function + * implementation. The "in" parameter should evaluate, when applied to a + * numerical input parameter from 0 to 15, to an expression which yields + * the corresponding input block. The "h" parameter should evaluate to + * an array or pointer expression designating the array of 5 words which + * contains the input and output of the compression function. + */ + +#define RIPEMD160_ROUND_BODY(in, h) do { \ + sph_u32 A1, B1, C1, D1, E1; \ + sph_u32 A2, B2, C2, D2, E2; \ + sph_u32 tmp; \ + \ + A1 = A2 = (h)[0]; \ + B1 = B2 = (h)[1]; \ + C1 = C2 = (h)[2]; \ + D1 = D2 = (h)[3]; \ + E1 = E2 = (h)[4]; \ + \ + ROUND1(A, B, C, D, E, F1, 11, in( 0), 1); \ + ROUND1(E, A, B, C, D, F1, 14, in( 1), 1); \ + ROUND1(D, E, A, B, C, F1, 15, in( 2), 1); \ + ROUND1(C, D, E, A, B, F1, 12, in( 3), 1); \ + ROUND1(B, C, D, E, A, F1, 5, in( 4), 1); \ + ROUND1(A, B, C, D, E, F1, 8, in( 5), 1); \ + ROUND1(E, A, B, C, D, F1, 7, in( 6), 1); \ + ROUND1(D, E, A, B, C, F1, 9, in( 7), 1); \ + ROUND1(C, D, E, A, B, F1, 11, in( 8), 1); \ + ROUND1(B, C, D, E, A, F1, 13, in( 9), 1); \ + ROUND1(A, B, C, D, E, F1, 14, in(10), 1); \ + ROUND1(E, A, B, C, D, F1, 15, in(11), 1); \ + ROUND1(D, E, A, B, C, F1, 6, in(12), 1); \ + ROUND1(C, D, E, A, B, F1, 7, in(13), 1); \ + ROUND1(B, C, D, E, A, F1, 9, in(14), 1); \ + ROUND1(A, B, C, D, E, F1, 8, in(15), 1); \ + \ + ROUND1(E, A, B, C, D, F2, 7, in( 7), 2); \ + ROUND1(D, E, A, B, C, F2, 6, in( 4), 2); \ + ROUND1(C, D, E, A, B, F2, 8, in(13), 2); \ + ROUND1(B, C, D, E, A, F2, 13, in( 1), 2); \ + ROUND1(A, B, C, D, E, F2, 11, in(10), 2); \ + ROUND1(E, A, B, C, D, F2, 9, in( 6), 2); \ + ROUND1(D, E, A, B, C, F2, 7, in(15), 2); \ + ROUND1(C, D, E, A, B, F2, 15, in( 3), 2); \ + ROUND1(B, C, D, E, A, F2, 7, in(12), 2); \ + ROUND1(A, B, C, D, E, F2, 12, in( 0), 2); \ + ROUND1(E, A, B, C, D, F2, 15, in( 9), 2); \ + ROUND1(D, E, A, B, C, F2, 9, in( 5), 2); \ + ROUND1(C, D, E, A, B, F2, 11, in( 2), 2); \ + ROUND1(B, C, D, E, A, F2, 7, in(14), 2); \ + ROUND1(A, B, C, D, E, F2, 13, in(11), 2); \ + ROUND1(E, A, B, C, D, F2, 12, in( 8), 2); \ + \ + ROUND1(D, E, A, B, C, F3, 11, in( 3), 3); \ + ROUND1(C, D, E, A, B, F3, 13, in(10), 3); \ + ROUND1(B, C, D, E, A, F3, 6, in(14), 3); \ + ROUND1(A, B, C, D, E, F3, 7, in( 4), 3); \ + ROUND1(E, A, B, C, D, F3, 14, in( 9), 3); \ + ROUND1(D, E, A, B, C, F3, 9, in(15), 3); \ + ROUND1(C, D, E, A, B, F3, 13, in( 8), 3); \ + ROUND1(B, C, D, E, A, F3, 15, in( 1), 3); \ + ROUND1(A, B, C, D, E, F3, 14, in( 2), 3); \ + ROUND1(E, A, B, C, D, F3, 8, in( 7), 3); \ + ROUND1(D, E, A, B, C, F3, 13, in( 0), 3); \ + ROUND1(C, D, E, A, B, F3, 6, in( 6), 3); \ + ROUND1(B, C, D, E, A, F3, 5, in(13), 3); \ + ROUND1(A, B, C, D, E, F3, 12, in(11), 3); \ + ROUND1(E, A, B, C, D, F3, 7, in( 5), 3); \ + ROUND1(D, E, A, B, C, F3, 5, in(12), 3); \ + \ + ROUND1(C, D, E, A, B, F4, 11, in( 1), 4); \ + ROUND1(B, C, D, E, A, F4, 12, in( 9), 4); \ + ROUND1(A, B, C, D, E, F4, 14, in(11), 4); \ + ROUND1(E, A, B, C, D, F4, 15, in(10), 4); \ + ROUND1(D, E, A, B, C, F4, 14, in( 0), 4); \ + ROUND1(C, D, E, A, B, F4, 15, in( 8), 4); \ + ROUND1(B, C, D, E, A, F4, 9, in(12), 4); \ + ROUND1(A, B, C, D, E, F4, 8, in( 4), 4); \ + ROUND1(E, A, B, C, D, F4, 9, in(13), 4); \ + ROUND1(D, E, A, B, C, F4, 14, in( 3), 4); \ + ROUND1(C, D, E, A, B, F4, 5, in( 7), 4); \ + ROUND1(B, C, D, E, A, F4, 6, in(15), 4); \ + ROUND1(A, B, C, D, E, F4, 8, in(14), 4); \ + ROUND1(E, A, B, C, D, F4, 6, in( 5), 4); \ + ROUND1(D, E, A, B, C, F4, 5, in( 6), 4); \ + ROUND1(C, D, E, A, B, F4, 12, in( 2), 4); \ + \ + ROUND1(B, C, D, E, A, F5, 9, in( 4), 5); \ + ROUND1(A, B, C, D, E, F5, 15, in( 0), 5); \ + ROUND1(E, A, B, C, D, F5, 5, in( 5), 5); \ + ROUND1(D, E, A, B, C, F5, 11, in( 9), 5); \ + ROUND1(C, D, E, A, B, F5, 6, in( 7), 5); \ + ROUND1(B, C, D, E, A, F5, 8, in(12), 5); \ + ROUND1(A, B, C, D, E, F5, 13, in( 2), 5); \ + ROUND1(E, A, B, C, D, F5, 12, in(10), 5); \ + ROUND1(D, E, A, B, C, F5, 5, in(14), 5); \ + ROUND1(C, D, E, A, B, F5, 12, in( 1), 5); \ + ROUND1(B, C, D, E, A, F5, 13, in( 3), 5); \ + ROUND1(A, B, C, D, E, F5, 14, in( 8), 5); \ + ROUND1(E, A, B, C, D, F5, 11, in(11), 5); \ + ROUND1(D, E, A, B, C, F5, 8, in( 6), 5); \ + ROUND1(C, D, E, A, B, F5, 5, in(15), 5); \ + ROUND1(B, C, D, E, A, F5, 6, in(13), 5); \ + \ + ROUND2(A, B, C, D, E, F5, 8, in( 5), 1); \ + ROUND2(E, A, B, C, D, F5, 9, in(14), 1); \ + ROUND2(D, E, A, B, C, F5, 9, in( 7), 1); \ + ROUND2(C, D, E, A, B, F5, 11, in( 0), 1); \ + ROUND2(B, C, D, E, A, F5, 13, in( 9), 1); \ + ROUND2(A, B, C, D, E, F5, 15, in( 2), 1); \ + ROUND2(E, A, B, C, D, F5, 15, in(11), 1); \ + ROUND2(D, E, A, B, C, F5, 5, in( 4), 1); \ + ROUND2(C, D, E, A, B, F5, 7, in(13), 1); \ + ROUND2(B, C, D, E, A, F5, 7, in( 6), 1); \ + ROUND2(A, B, C, D, E, F5, 8, in(15), 1); \ + ROUND2(E, A, B, C, D, F5, 11, in( 8), 1); \ + ROUND2(D, E, A, B, C, F5, 14, in( 1), 1); \ + ROUND2(C, D, E, A, B, F5, 14, in(10), 1); \ + ROUND2(B, C, D, E, A, F5, 12, in( 3), 1); \ + ROUND2(A, B, C, D, E, F5, 6, in(12), 1); \ + \ + ROUND2(E, A, B, C, D, F4, 9, in( 6), 2); \ + ROUND2(D, E, A, B, C, F4, 13, in(11), 2); \ + ROUND2(C, D, E, A, B, F4, 15, in( 3), 2); \ + ROUND2(B, C, D, E, A, F4, 7, in( 7), 2); \ + ROUND2(A, B, C, D, E, F4, 12, in( 0), 2); \ + ROUND2(E, A, B, C, D, F4, 8, in(13), 2); \ + ROUND2(D, E, A, B, C, F4, 9, in( 5), 2); \ + ROUND2(C, D, E, A, B, F4, 11, in(10), 2); \ + ROUND2(B, C, D, E, A, F4, 7, in(14), 2); \ + ROUND2(A, B, C, D, E, F4, 7, in(15), 2); \ + ROUND2(E, A, B, C, D, F4, 12, in( 8), 2); \ + ROUND2(D, E, A, B, C, F4, 7, in(12), 2); \ + ROUND2(C, D, E, A, B, F4, 6, in( 4), 2); \ + ROUND2(B, C, D, E, A, F4, 15, in( 9), 2); \ + ROUND2(A, B, C, D, E, F4, 13, in( 1), 2); \ + ROUND2(E, A, B, C, D, F4, 11, in( 2), 2); \ + \ + ROUND2(D, E, A, B, C, F3, 9, in(15), 3); \ + ROUND2(C, D, E, A, B, F3, 7, in( 5), 3); \ + ROUND2(B, C, D, E, A, F3, 15, in( 1), 3); \ + ROUND2(A, B, C, D, E, F3, 11, in( 3), 3); \ + ROUND2(E, A, B, C, D, F3, 8, in( 7), 3); \ + ROUND2(D, E, A, B, C, F3, 6, in(14), 3); \ + ROUND2(C, D, E, A, B, F3, 6, in( 6), 3); \ + ROUND2(B, C, D, E, A, F3, 14, in( 9), 3); \ + ROUND2(A, B, C, D, E, F3, 12, in(11), 3); \ + ROUND2(E, A, B, C, D, F3, 13, in( 8), 3); \ + ROUND2(D, E, A, B, C, F3, 5, in(12), 3); \ + ROUND2(C, D, E, A, B, F3, 14, in( 2), 3); \ + ROUND2(B, C, D, E, A, F3, 13, in(10), 3); \ + ROUND2(A, B, C, D, E, F3, 13, in( 0), 3); \ + ROUND2(E, A, B, C, D, F3, 7, in( 4), 3); \ + ROUND2(D, E, A, B, C, F3, 5, in(13), 3); \ + \ + ROUND2(C, D, E, A, B, F2, 15, in( 8), 4); \ + ROUND2(B, C, D, E, A, F2, 5, in( 6), 4); \ + ROUND2(A, B, C, D, E, F2, 8, in( 4), 4); \ + ROUND2(E, A, B, C, D, F2, 11, in( 1), 4); \ + ROUND2(D, E, A, B, C, F2, 14, in( 3), 4); \ + ROUND2(C, D, E, A, B, F2, 14, in(11), 4); \ + ROUND2(B, C, D, E, A, F2, 6, in(15), 4); \ + ROUND2(A, B, C, D, E, F2, 14, in( 0), 4); \ + ROUND2(E, A, B, C, D, F2, 6, in( 5), 4); \ + ROUND2(D, E, A, B, C, F2, 9, in(12), 4); \ + ROUND2(C, D, E, A, B, F2, 12, in( 2), 4); \ + ROUND2(B, C, D, E, A, F2, 9, in(13), 4); \ + ROUND2(A, B, C, D, E, F2, 12, in( 9), 4); \ + ROUND2(E, A, B, C, D, F2, 5, in( 7), 4); \ + ROUND2(D, E, A, B, C, F2, 15, in(10), 4); \ + ROUND2(C, D, E, A, B, F2, 8, in(14), 4); \ + \ + ROUND2(B, C, D, E, A, F1, 8, in(12), 5); \ + ROUND2(A, B, C, D, E, F1, 5, in(15), 5); \ + ROUND2(E, A, B, C, D, F1, 12, in(10), 5); \ + ROUND2(D, E, A, B, C, F1, 9, in( 4), 5); \ + ROUND2(C, D, E, A, B, F1, 12, in( 1), 5); \ + ROUND2(B, C, D, E, A, F1, 5, in( 5), 5); \ + ROUND2(A, B, C, D, E, F1, 14, in( 8), 5); \ + ROUND2(E, A, B, C, D, F1, 6, in( 7), 5); \ + ROUND2(D, E, A, B, C, F1, 8, in( 6), 5); \ + ROUND2(C, D, E, A, B, F1, 13, in( 2), 5); \ + ROUND2(B, C, D, E, A, F1, 6, in(13), 5); \ + ROUND2(A, B, C, D, E, F1, 5, in(14), 5); \ + ROUND2(E, A, B, C, D, F1, 15, in( 0), 5); \ + ROUND2(D, E, A, B, C, F1, 13, in( 3), 5); \ + ROUND2(C, D, E, A, B, F1, 11, in( 9), 5); \ + ROUND2(B, C, D, E, A, F1, 11, in(11), 5); \ + \ + tmp = SPH_T32((h)[1] + C1 + D2); \ + (h)[1] = SPH_T32((h)[2] + D1 + E2); \ + (h)[2] = SPH_T32((h)[3] + E1 + A2); \ + (h)[3] = SPH_T32((h)[4] + A1 + B2); \ + (h)[4] = SPH_T32((h)[0] + B1 + C2); \ + (h)[0] = tmp; \ + } while (0) + +/* + * One round of RIPEMD-160. The data must be aligned for 32-bit access. + */ +static void +ripemd160_round(const unsigned char *data, sph_u32 r[5]) +{ +#if SPH_LITTLE_FAST + +#define RIPEMD160_IN(x) sph_dec32le_aligned(data + (4 * (x))) + +#else + + sph_u32 X_var[16]; + int i; + + for (i = 0; i < 16; i ++) + X_var[i] = sph_dec32le_aligned(data + 4 * i); + +#define RIPEMD160_IN(x) X_var[x] + +#endif + RIPEMD160_ROUND_BODY(RIPEMD160_IN, r); +#undef RIPEMD160_IN +} + +/* see sph_ripemd.h */ +void +sph_ripemd160_init(void *cc) +{ + sph_ripemd160_context *sc; + + sc = cc; + memcpy(sc->val, IV, sizeof sc->val); +#if SPH_64 + sc->count = 0; +#else + sc->count_high = sc->count_low = 0; +#endif +} + +#define RFUN ripemd160_round +#define HASH ripemd160 +#define LE32 1 +#include "md_helper.c" +#undef RFUN +#undef HASH +#undef LE32 + +/* see sph_ripemd.h */ +void +sph_ripemd160_close(void *cc, void *dst) +{ + ripemd160_close(cc, dst, 5); + sph_ripemd160_init(cc); +} + +/* see sph_ripemd.h */ +void +sph_ripemd160_comp(const sph_u32 msg[16], sph_u32 val[5]) +{ +#define RIPEMD160_IN(x) msg[x] + RIPEMD160_ROUND_BODY(RIPEMD160_IN, val); +#undef RIPEMD160_IN +} diff --git a/sph/sph_ripemd.h b/sph/sph_ripemd.h new file mode 100644 index 00000000..25677683 --- /dev/null +++ b/sph/sph_ripemd.h @@ -0,0 +1,273 @@ +/* $Id: sph_ripemd.h 216 2010-06-08 09:46:57Z tp $ */ +/** + * RIPEMD, RIPEMD-128 and RIPEMD-160 interface. + * + * RIPEMD was first described in: Research and Development in Advanced + * Communication Technologies in Europe, "RIPE Integrity Primitives: + * Final Report of RACE Integrity Primitives Evaluation (R1040)", RACE, + * June 1992. + * + * A new, strengthened version, dubbed RIPEMD-160, was published in: H. + * Dobbertin, A. Bosselaers, and B. Preneel, "RIPEMD-160, a strengthened + * version of RIPEMD", Fast Software Encryption - FSE'96, LNCS 1039, + * Springer (1996), pp. 71--82. + * + * This article describes both RIPEMD-160, with a 160-bit output, and a + * reduced version called RIPEMD-128, which has a 128-bit output. RIPEMD-128 + * was meant as a "drop-in" replacement for any hash function with 128-bit + * output, especially the original RIPEMD. + * + * @warning Collisions, and an efficient method to build other collisions, + * have been published for the original RIPEMD, which is thus considered as + * cryptographically broken. It is also very rarely encountered, and there + * seems to exist no free description or implementation of RIPEMD (except + * the sphlib code, of course). As of january 2007, RIPEMD-128 and RIPEMD-160 + * seem as secure as their output length allows. + * + * ==========================(LICENSE BEGIN)============================ + * + * Copyright (c) 2007-2010 Projet RNRT SAPHIR + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * ===========================(LICENSE END)============================= + * + * @file sph_ripemd.h + * @author Thomas Pornin + */ + +#ifndef SPH_RIPEMD_H__ +#define SPH_RIPEMD_H__ + +#include +#include "sph_types.h" + +/** + * Output size (in bits) for RIPEMD. + */ +#define SPH_SIZE_ripemd 128 + +/** + * Output size (in bits) for RIPEMD-128. + */ +#define SPH_SIZE_ripemd128 128 + +/** + * Output size (in bits) for RIPEMD-160. + */ +#define SPH_SIZE_ripemd160 160 + +/** + * This structure is a context for RIPEMD computations: it contains the + * intermediate values and some data from the last entered block. Once + * a RIPEMD computation has been performed, the context can be reused for + * another computation. + * + * The contents of this structure are private. A running RIPEMD computation + * can be cloned by copying the context (e.g. with a simple + * memcpy()). + */ +typedef struct { +#ifndef DOXYGEN_IGNORE + unsigned char buf[64]; /* first field, for alignment */ + sph_u32 val[4]; +#if SPH_64 + sph_u64 count; +#else + sph_u32 count_high, count_low; +#endif +#endif +} sph_ripemd_context; + +/** + * Initialize a RIPEMD context. This process performs no memory allocation. + * + * @param cc the RIPEMD context (pointer to + * a sph_ripemd_context) + */ +void sph_ripemd_init(void *cc); + +/** + * Process some data bytes. It is acceptable that len is zero + * (in which case this function does nothing). + * + * @param cc the RIPEMD context + * @param data the input data + * @param len the input data length (in bytes) + */ +void sph_ripemd(void *cc, const void *data, size_t len); + +/** + * Terminate the current RIPEMD computation and output the result into the + * provided buffer. The destination buffer must be wide enough to + * accomodate the result (16 bytes). The context is automatically + * reinitialized. + * + * @param cc the RIPEMD context + * @param dst the destination buffer + */ +void sph_ripemd_close(void *cc, void *dst); + +/** + * Apply the RIPEMD compression function on the provided data. The + * msg parameter contains the 16 32-bit input blocks, + * as numerical values (hence after the little-endian decoding). The + * val parameter contains the 5 32-bit input blocks for + * the compression function; the output is written in place in this + * array. + * + * @param msg the message block (16 values) + * @param val the function 128-bit input and output + */ +void sph_ripemd_comp(const sph_u32 msg[16], sph_u32 val[4]); + +/* ===================================================================== */ + +/** + * This structure is a context for RIPEMD-128 computations: it contains the + * intermediate values and some data from the last entered block. Once + * a RIPEMD-128 computation has been performed, the context can be reused for + * another computation. + * + * The contents of this structure are private. A running RIPEMD-128 computation + * can be cloned by copying the context (e.g. with a simple + * memcpy()). + */ +typedef struct { +#ifndef DOXYGEN_IGNORE + unsigned char buf[64]; /* first field, for alignment */ + sph_u32 val[4]; +#if SPH_64 + sph_u64 count; +#else + sph_u32 count_high, count_low; +#endif +#endif +} sph_ripemd128_context; + +/** + * Initialize a RIPEMD-128 context. This process performs no memory allocation. + * + * @param cc the RIPEMD-128 context (pointer to + * a sph_ripemd128_context) + */ +void sph_ripemd128_init(void *cc); + +/** + * Process some data bytes. It is acceptable that len is zero + * (in which case this function does nothing). + * + * @param cc the RIPEMD-128 context + * @param data the input data + * @param len the input data length (in bytes) + */ +void sph_ripemd128(void *cc, const void *data, size_t len); + +/** + * Terminate the current RIPEMD-128 computation and output the result into the + * provided buffer. The destination buffer must be wide enough to + * accomodate the result (16 bytes). The context is automatically + * reinitialized. + * + * @param cc the RIPEMD-128 context + * @param dst the destination buffer + */ +void sph_ripemd128_close(void *cc, void *dst); + +/** + * Apply the RIPEMD-128 compression function on the provided data. The + * msg parameter contains the 16 32-bit input blocks, + * as numerical values (hence after the little-endian decoding). The + * val parameter contains the 5 32-bit input blocks for + * the compression function; the output is written in place in this + * array. + * + * @param msg the message block (16 values) + * @param val the function 128-bit input and output + */ +void sph_ripemd128_comp(const sph_u32 msg[16], sph_u32 val[4]); + +/* ===================================================================== */ + +/** + * This structure is a context for RIPEMD-160 computations: it contains the + * intermediate values and some data from the last entered block. Once + * a RIPEMD-160 computation has been performed, the context can be reused for + * another computation. + * + * The contents of this structure are private. A running RIPEMD-160 computation + * can be cloned by copying the context (e.g. with a simple + * memcpy()). + */ +typedef struct { +#ifndef DOXYGEN_IGNORE + unsigned char buf[64]; /* first field, for alignment */ + sph_u32 val[5]; +#if SPH_64 + sph_u64 count; +#else + sph_u32 count_high, count_low; +#endif +#endif +} sph_ripemd160_context; + +/** + * Initialize a RIPEMD-160 context. This process performs no memory allocation. + * + * @param cc the RIPEMD-160 context (pointer to + * a sph_ripemd160_context) + */ +void sph_ripemd160_init(void *cc); + +/** + * Process some data bytes. It is acceptable that len is zero + * (in which case this function does nothing). + * + * @param cc the RIPEMD-160 context + * @param data the input data + * @param len the input data length (in bytes) + */ +void sph_ripemd160(void *cc, const void *data, size_t len); + +/** + * Terminate the current RIPEMD-160 computation and output the result into the + * provided buffer. The destination buffer must be wide enough to + * accomodate the result (20 bytes). The context is automatically + * reinitialized. + * + * @param cc the RIPEMD-160 context + * @param dst the destination buffer + */ +void sph_ripemd160_close(void *cc, void *dst); + +/** + * Apply the RIPEMD-160 compression function on the provided data. The + * msg parameter contains the 16 32-bit input blocks, + * as numerical values (hence after the little-endian decoding). The + * val parameter contains the 5 32-bit input blocks for + * the compression function; the output is written in place in this + * array. + * + * @param msg the message block (16 values) + * @param val the function 160-bit input and output + */ +void sph_ripemd160_comp(const sph_u32 msg[16], sph_u32 val[5]); + +#endif