Skip to content

Instantly share code, notes, and snippets.

@beevelop
Forked from phiresky/.gitignore
Created May 22, 2017 17:26
Show Gist options
  • Save beevelop/bd12c6722b64bf7a939ce9fa42cc143d to your computer and use it in GitHub Desktop.
Save beevelop/bd12c6722b64bf7a939ce9fa42cc143d to your computer and use it in GitHub Desktop.

Revisions

  1. @phiresky phiresky revised this gist Jun 1, 2016. 2 changed files with 14 additions and 12 deletions.
    2 changes: 1 addition & 1 deletion findlatest.sh
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,6 @@ do
    echo $(date): new block found >&2
    echo $latestbits $latestparent
    fi
    sleep 3
    sleep 1
    done

    24 changes: 13 additions & 11 deletions mine.sh
    Original file line number Diff line number Diff line change
    @@ -13,17 +13,19 @@ do
    echo restarting mining, bits="$bits", parent="$parent" >&2
    (
    killoldminers
    if [ $bits -le 32 ]; then
    for processor in $(seq 1 $(nproc))
    do
    echo starting ./sha256test $bits "$parent $username" >&2
    ./sha256test $bits "$parent $username" &
    done | while read hash parent username seed
    do
    echo curl "http://hash.h10a.de/?Z=$parent&P=$username&R=$seed" >&2
    curl "http://hash.h10a.de/?Z=$parent&P=$username&R=$seed"
    done
    else echo too difficult. ignoring
    if [ $bits -le 32 ]
    then
    for processor in $(seq 1 $(nproc))
    do
    echo starting ./sha256test $bits "$parent $username" >&2
    ./sha256test $bits "$parent $username" &
    done | while read hash parent username seed
    do
    echo curl "http://hash.h10a.de/?Z=$parent&P=$username&R=$seed" >&2
    curl "http://hash.h10a.de/?Z=$parent&P=$username&R=$seed"
    done
    else
    echo too difficult. ignoring
    fi
    )&
    done
  2. @phiresky phiresky revised this gist Jun 1, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions mine.sh
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ do
    echo restarting mining, bits="$bits", parent="$parent" >&2
    (
    killoldminers
    if [ $bits -le 32 ]; then
    for processor in $(seq 1 $(nproc))
    do
    echo starting ./sha256test $bits "$parent $username" >&2
    @@ -22,6 +23,8 @@ do
    echo curl "http://hash.h10a.de/?Z=$parent&P=$username&R=$seed" >&2
    curl "http://hash.h10a.de/?Z=$parent&P=$username&R=$seed"
    done
    else echo too difficult. ignoring
    fi
    )&
    done

  3. @phiresky phiresky revised this gist Jun 1, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion mine.sh
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,16 @@ set -euf

    username=$1

    function killoldminers {
    killall --quiet --exact sha256test || echo no mining processes running
    }
    trap killoldminers EXIT

    ./findlatest.sh | while read bits parent
    do
    echo restarting mining, bits="$bits", parent="$parent" >&2
    killall --quiet --exact sha256test || echo starting
    (
    killoldminers
    for processor in $(seq 1 $(nproc))
    do
    echo starting ./sha256test $bits "$parent $username" >&2
  4. @phiresky phiresky revised this gist May 31, 2016. No changes.
  5. @phiresky phiresky revised this gist May 31, 2016. 3 changed files with 3 additions and 0 deletions.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    latestinfo
    sha256test
    callgrind*
    sha256test.gcda
    2 changes: 2 additions & 0 deletions build.sh
    Original file line number Diff line number Diff line change
    @@ -1 +1,3 @@
    gcc -Wall -march=native -mtune=native -fprofile-generate -O3 sha256test.c -o sha256test -lcrypto
    ./sha256test 25 "00000000c7e6e62a5d21dcb25bb2a937b19b2d32c8bd2339a22f18a4d3926862 robin"
    gcc -Wall -march=native -mtune=native -fprofile-use -O3 sha256test.c -o sha256test -lcrypto
    Binary file removed sha256test.gcda
    Binary file not shown.
  6. @phiresky phiresky revised this gist May 31, 2016. 7 changed files with 60 additions and 751 deletions.
    3 changes: 3 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    latestinfo
    sha256test
    callgrind*
    2 changes: 1 addition & 1 deletion build.sh
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    gcc -static -march=native -O3 sha256test.c sha256-64.S -o sha256test
    gcc -Wall -march=native -mtune=native -fprofile-use -O3 sha256test.c -o sha256test -lcrypto
    231 changes: 0 additions & 231 deletions sha256-64.S
    Original file line number Diff line number Diff line change
    @@ -1,231 +0,0 @@
    /*
    * SHA-256 hash in x86-64 assembly
    *
    * Copyright (c) 2015 Project Nayuki
    * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
    *
    * (MIT License)
    * 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.
    */


    /* void sha256_compress(uint32_t state[8], const uint8_t block[64]) */
    .globl sha256_compress
    sha256_compress:
    /*
    * Storage usage:
    * Bytes Location Description
    * 4 eax Temporary for calculation per round
    * 4 ebx Temporary for calculation per round
    * 4 ecx Temporary for calculation per round
    * 4 edx Temporary for calculation per round
    * 8 rsi Base address of block array argument (read-only)
    * 8 rdi Base address of state array argument (read-only)
    * 8 rsp x86-64 stack pointer
    * 4 r8d SHA-256 state variable A
    * 4 r9d SHA-256 state variable B
    * 4 r10d SHA-256 state variable C
    * 4 r11d SHA-256 state variable D
    * 4 r12d SHA-256 state variable E
    * 4 r13d SHA-256 state variable F
    * 4 r14d SHA-256 state variable G
    * 4 r15d SHA-256 state variable H
    * 64 [rsp+0] Circular buffer of most recent 16 key schedule items, 4 bytes each
    * 16 xmm0 Caller's value of r10 (only low 64 bits are used)
    * 16 xmm1 Caller's value of r11 (only low 64 bits are used)
    * 16 xmm2 Caller's value of r12 (only low 64 bits are used)
    * 16 xmm3 Caller's value of r13 (only low 64 bits are used)
    * 16 xmm4 Caller's value of r14 (only low 64 bits are used)
    * 16 xmm5 Caller's value of r15 (only low 64 bits are used)
    * 16 xmm6 Caller's value of rbx (only low 64 bits are used)
    */

    #define SCHED(i) (((i)&0xF)*4)(%rsp)

    #define ROUNDa(i, a, b, c, d, e, f, g, h, k) \
    movl (i*4)(%rsi), %ebx; \
    bswapl %ebx; \
    movl %ebx, SCHED(i); \
    ROUNDTAIL(a, b, c, d, e, f, g, h, k)

    #define ROUNDb(i, a, b, c, d, e, f, g, h, k) \
    movl SCHED(i-15), %eax; \
    movl SCHED(i-16), %ebx; \
    addl SCHED(i- 7), %ebx; \
    movl %eax, %ecx; \
    movl %eax, %edx; \
    rorl $18, %ecx; \
    shrl $3, %edx; \
    rorl $7, %eax; \
    xorl %edx, %ecx; \
    xorl %ecx, %eax; \
    addl %eax, %ebx; \
    movl SCHED(i- 2), %eax; \
    movl %eax, %ecx; \
    movl %eax, %edx; \
    rorl $19, %ecx; \
    shrl $10, %edx; \
    rorl $17, %eax; \
    xorl %edx, %ecx; \
    xorl %ecx, %eax; \
    addl %eax, %ebx; \
    movl %ebx, SCHED(i); \
    ROUNDTAIL(a, b, c, d, e, f, g, h, k)

    #define ROUNDTAIL(a, b, c, d, e, f, g, h, k) \
    /* Part 0 */ \
    movl %e, %ecx; \
    movl %e, %edx; \
    movl %e, %eax; \
    rorl $11, %ecx; \
    rorl $25, %edx; \
    rorl $6, %eax; \
    xorl %edx, %ecx; \
    xorl %ecx, %eax; \
    addl %ebx, %h; \
    movl %g, %ecx; \
    xorl %f, %ecx; \
    andl %e, %ecx; \
    xorl %g, %ecx; \
    leal k(%rax,%rcx), %eax; \
    addl %eax, %h; \
    /* Part 1 */ \
    addl %h, %d; \
    /* Part 2 */ \
    movl %a, %ecx; \
    movl %a, %edx; \
    movl %a, %eax; \
    rorl $13, %ecx; \
    rorl $22, %edx; \
    rorl $2, %eax; \
    xorl %edx, %ecx; \
    xorl %ecx, %eax; \
    movl %c, %ecx; \
    addl %eax, %h; \
    movl %c, %eax; \
    orl %b, %eax; \
    andl %b, %ecx; \
    andl %a, %eax; \
    orl %ecx, %eax; \
    addl %eax, %h;

    /* Save registers, allocate scratch space */
    movq %r10, %xmm0
    movq %r11, %xmm1
    movq %r12, %xmm2
    movq %r13, %xmm3
    movq %r14, %xmm4
    movq %r15, %xmm5
    movq %rbx, %xmm6
    subq $64, %rsp

    /* Load state */
    movl 0(%rdi), %r8d /* a */
    movl 4(%rdi), %r9d /* b */
    movl 8(%rdi), %r10d /* c */
    movl 12(%rdi), %r11d /* d */
    movl 16(%rdi), %r12d /* e */
    movl 20(%rdi), %r13d /* f */
    movl 24(%rdi), %r14d /* g */
    movl 28(%rdi), %r15d /* h */

    /* Do 64 rounds of hashing */
    ROUNDa( 0, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, 0x428A2F98)
    ROUNDa( 1, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x71374491)
    ROUNDa( 2, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, -0x4A3F0431)
    ROUNDa( 3, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, -0x164A245B)
    ROUNDa( 4, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x3956C25B)
    ROUNDa( 5, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, 0x59F111F1)
    ROUNDa( 6, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x6DC07D5C)
    ROUNDa( 7, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , -0x54E3A12B)
    ROUNDa( 8, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, -0x27F85568)
    ROUNDa( 9, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x12835B01)
    ROUNDa(10, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, 0x243185BE)
    ROUNDa(11, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, 0x550C7DC3)
    ROUNDa(12, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x72BE5D74)
    ROUNDa(13, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, -0x7F214E02)
    ROUNDa(14, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x6423F959)
    ROUNDa(15, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , -0x3E640E8C)
    ROUNDb(16, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, -0x1B64963F)
    ROUNDb(17, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, -0x1041B87A)
    ROUNDb(18, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, 0x0FC19DC6)
    ROUNDb(19, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, 0x240CA1CC)
    ROUNDb(20, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x2DE92C6F)
    ROUNDb(21, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, 0x4A7484AA)
    ROUNDb(22, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , 0x5CB0A9DC)
    ROUNDb(23, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , 0x76F988DA)
    ROUNDb(24, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, -0x67C1AEAE)
    ROUNDb(25, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, -0x57CE3993)
    ROUNDb(26, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, -0x4FFCD838)
    ROUNDb(27, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, -0x40A68039)
    ROUNDb(28, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, -0x391FF40D)
    ROUNDb(29, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, -0x2A586EB9)
    ROUNDb(30, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , 0x06CA6351)
    ROUNDb(31, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , 0x14292967)
    ROUNDb(32, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, 0x27B70A85)
    ROUNDb(33, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x2E1B2138)
    ROUNDb(34, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, 0x4D2C6DFC)
    ROUNDb(35, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, 0x53380D13)
    ROUNDb(36, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x650A7354)
    ROUNDb(37, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, 0x766A0ABB)
    ROUNDb(38, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x7E3D36D2)
    ROUNDb(39, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , -0x6D8DD37B)
    ROUNDb(40, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, -0x5D40175F)
    ROUNDb(41, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, -0x57E599B5)
    ROUNDb(42, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, -0x3DB47490)
    ROUNDb(43, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, -0x3893AE5D)
    ROUNDb(44, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, -0x2E6D17E7)
    ROUNDb(45, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, -0x2966F9DC)
    ROUNDb(46, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x0BF1CA7B)
    ROUNDb(47, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , 0x106AA070)
    ROUNDb(48, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, 0x19A4C116)
    ROUNDb(49, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x1E376C08)
    ROUNDb(50, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, 0x2748774C)
    ROUNDb(51, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, 0x34B0BCB5)
    ROUNDb(52, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x391C0CB3)
    ROUNDb(53, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, 0x4ED8AA4A)
    ROUNDb(54, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , 0x5B9CCA4F)
    ROUNDb(55, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , 0x682E6FF3)
    ROUNDb(56, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, 0x748F82EE)
    ROUNDb(57, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x78A5636F)
    ROUNDb(58, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, -0x7B3787EC)
    ROUNDb(59, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, -0x7338FDF8)
    ROUNDb(60, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, -0x6F410006)
    ROUNDb(61, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, -0x5BAF9315)
    ROUNDb(62, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x41065C09)
    ROUNDb(63, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , -0x398E870E)

    /* Add to state */
    addl %r8d , 0(%rdi)
    addl %r9d , 4(%rdi)
    addl %r10d, 8(%rdi)
    addl %r11d, 12(%rdi)
    addl %r12d, 16(%rdi)
    addl %r13d, 20(%rdi)
    addl %r14d, 24(%rdi)
    addl %r15d, 28(%rdi)

    /* Restore registers */
    movq %xmm0, %r10
    movq %xmm1, %r11
    movq %xmm2, %r12
    movq %xmm3, %r13
    movq %xmm4, %r14
    movq %xmm5, %r15
    movq %xmm6, %rbx
    addq $64, %rsp
    retq
    228 changes: 0 additions & 228 deletions sha256.S
    Original file line number Diff line number Diff line change
    @@ -1,228 +0,0 @@
    /*
    * SHA-256 hash in x86 assembly
    *
    * Copyright (c) 2014 Project Nayuki
    * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
    *
    * (MIT License)
    * 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.
    */


    /* void sha256_compress(uint32_t state[8], const uint8_t block[64]) */
    .globl sha256_compress
    sha256_compress:
    /*
    * Storage usage:
    * Bytes Location Description
    * 4 eax Temporary for calculation per round
    * 4 ebx Temporary for calculation per round
    * 4 ecx Temporary for calculation per round
    * 4 edx Temporary for calculation per round
    * 4 ebp Temporary for calculation per round
    * 4 esi (During state loading and update) base address of state array argument
    * (During hash rounds) temporary for calculation per round
    * 4 edi Base address of block array argument (during key schedule loading rounds only)
    * 4 esp x86 stack pointer
    * 32 [esp+ 0] SHA-256 state variables A,B,C,D,E,F,G,H (4 bytes each)
    * 64 [esp+ 32] Key schedule of 16 * 4 bytes
    * 4 [esp+ 96] Caller's value of ebx
    * 4 [esp+100] Caller's value of esi
    * 4 [esp+104] Caller's value of edi
    * 4 [esp+108] Caller's value of ebp
    */

    #define SCHED(i) ((((i)&0xF)+8)*4)(%esp)

    #define ROUNDa(i, a, b, c, d, e, f, g, h, k) \
    movl (i*4)(%edi), %ebp; \
    bswapl %ebp; \
    movl %ebp, SCHED(i); \
    ROUNDTAIL(i, a, b, c, d, e, f, g, h, k)

    #define ROUNDb(i, a, b, c, d, e, f, g, h, k) \
    movl SCHED(i-15), %eax; \
    movl SCHED(i-16), %ebp; \
    movl %eax, %ebx; \
    addl SCHED(i- 7), %ebp; \
    movl %eax, %ecx; \
    rorl $18, %ebx; \
    shrl $3, %ecx; \
    rorl $7, %eax; \
    xorl %ecx, %ebx; \
    xorl %ebx, %eax; \
    addl %eax, %ebp; \
    movl SCHED(i- 2), %eax; \
    movl %eax, %ebx; \
    movl %eax, %ecx; \
    rorl $19, %ebx; \
    shrl $10, %ecx; \
    rorl $17, %eax; \
    xorl %ecx, %ebx; \
    xorl %ebx, %eax; \
    addl %eax, %ebp; \
    movl %ebp, SCHED(i); \
    ROUNDTAIL(i, a, b, c, d, e, f, g, h, k)

    #define STATE(i) (i*4)(%esp)

    #define ROUNDTAIL(i, a, b, c, d, e, f, g, h, k) \
    /* Part 0 */ \
    movl STATE(e), %eax; \
    movl %eax, %ebx; \
    movl %eax, %ecx; \
    movl %eax, %edx; \
    rorl $11, %eax; \
    rorl $25, %ebx; \
    rorl $6, %ecx; \
    movl STATE(h), %esi; \
    xorl %ebx, %eax; \
    xorl %eax, %ecx; \
    addl %ebp, %esi; \
    movl STATE(g), %ebx; \
    movl STATE(f), %eax; \
    xorl %ebx, %eax; \
    andl %edx, %eax; \
    xorl %ebx, %eax; \
    leal k(%ecx,%eax), %ecx; \
    addl %ecx, %esi; \
    /* Part 1 */ \
    addl %esi, STATE(d); \
    /* Part 2 */ \
    movl STATE(a), %eax; \
    movl %eax, %ebx; \
    movl %eax, %ecx; \
    movl %eax, %edx; \
    rorl $13, %eax; \
    rorl $22, %ebx; \
    rorl $2, %ecx; \
    xorl %ebx, %eax; \
    xorl %eax, %ecx; \
    movl STATE(c), %eax; \
    addl %ecx, %esi; \
    movl %eax, %ecx; \
    movl STATE(b), %ebx; \
    orl %ebx, %ecx; \
    andl %ebx, %eax; \
    andl %edx, %ecx; \
    orl %eax, %ecx; \
    addl %ecx, %esi; \
    movl %esi, STATE(h);

    /* Allocate scratch space, save registers */
    subl $112, %esp
    movl %ebx, 96(%esp)
    movl %esi, 100(%esp)
    movl %edi, 104(%esp)
    movl %ebp, 108(%esp)

    /* Copy state */
    movl 116(%esp), %esi /* Argument: state */
    movl 0(%esi), %eax; movl %eax, 0(%esp)
    movl 4(%esi), %eax; movl %eax, 4(%esp)
    movl 8(%esi), %eax; movl %eax, 8(%esp)
    movl 12(%esi), %eax; movl %eax, 12(%esp)
    movl 16(%esi), %eax; movl %eax, 16(%esp)
    movl 20(%esi), %eax; movl %eax, 20(%esp)
    movl 24(%esi), %eax; movl %eax, 24(%esp)
    movl 28(%esi), %eax; movl %eax, 28(%esp)

    /* Do 64 rounds of hashing */
    movl 120(%esp), %edi /* Argument: block */
    ROUNDa( 0, 0, 1, 2, 3, 4, 5, 6, 7, 0x428A2F98)
    ROUNDa( 1, 7, 0, 1, 2, 3, 4, 5, 6, 0x71374491)
    ROUNDa( 2, 6, 7, 0, 1, 2, 3, 4, 5, 0xB5C0FBCF)
    ROUNDa( 3, 5, 6, 7, 0, 1, 2, 3, 4, 0xE9B5DBA5)
    ROUNDa( 4, 4, 5, 6, 7, 0, 1, 2, 3, 0x3956C25B)
    ROUNDa( 5, 3, 4, 5, 6, 7, 0, 1, 2, 0x59F111F1)
    ROUNDa( 6, 2, 3, 4, 5, 6, 7, 0, 1, 0x923F82A4)
    ROUNDa( 7, 1, 2, 3, 4, 5, 6, 7, 0, 0xAB1C5ED5)
    ROUNDa( 8, 0, 1, 2, 3, 4, 5, 6, 7, 0xD807AA98)
    ROUNDa( 9, 7, 0, 1, 2, 3, 4, 5, 6, 0x12835B01)
    ROUNDa(10, 6, 7, 0, 1, 2, 3, 4, 5, 0x243185BE)
    ROUNDa(11, 5, 6, 7, 0, 1, 2, 3, 4, 0x550C7DC3)
    ROUNDa(12, 4, 5, 6, 7, 0, 1, 2, 3, 0x72BE5D74)
    ROUNDa(13, 3, 4, 5, 6, 7, 0, 1, 2, 0x80DEB1FE)
    ROUNDa(14, 2, 3, 4, 5, 6, 7, 0, 1, 0x9BDC06A7)
    ROUNDa(15, 1, 2, 3, 4, 5, 6, 7, 0, 0xC19BF174)
    ROUNDb(16, 0, 1, 2, 3, 4, 5, 6, 7, 0xE49B69C1)
    ROUNDb(17, 7, 0, 1, 2, 3, 4, 5, 6, 0xEFBE4786)
    ROUNDb(18, 6, 7, 0, 1, 2, 3, 4, 5, 0x0FC19DC6)
    ROUNDb(19, 5, 6, 7, 0, 1, 2, 3, 4, 0x240CA1CC)
    ROUNDb(20, 4, 5, 6, 7, 0, 1, 2, 3, 0x2DE92C6F)
    ROUNDb(21, 3, 4, 5, 6, 7, 0, 1, 2, 0x4A7484AA)
    ROUNDb(22, 2, 3, 4, 5, 6, 7, 0, 1, 0x5CB0A9DC)
    ROUNDb(23, 1, 2, 3, 4, 5, 6, 7, 0, 0x76F988DA)
    ROUNDb(24, 0, 1, 2, 3, 4, 5, 6, 7, 0x983E5152)
    ROUNDb(25, 7, 0, 1, 2, 3, 4, 5, 6, 0xA831C66D)
    ROUNDb(26, 6, 7, 0, 1, 2, 3, 4, 5, 0xB00327C8)
    ROUNDb(27, 5, 6, 7, 0, 1, 2, 3, 4, 0xBF597FC7)
    ROUNDb(28, 4, 5, 6, 7, 0, 1, 2, 3, 0xC6E00BF3)
    ROUNDb(29, 3, 4, 5, 6, 7, 0, 1, 2, 0xD5A79147)
    ROUNDb(30, 2, 3, 4, 5, 6, 7, 0, 1, 0x06CA6351)
    ROUNDb(31, 1, 2, 3, 4, 5, 6, 7, 0, 0x14292967)
    ROUNDb(32, 0, 1, 2, 3, 4, 5, 6, 7, 0x27B70A85)
    ROUNDb(33, 7, 0, 1, 2, 3, 4, 5, 6, 0x2E1B2138)
    ROUNDb(34, 6, 7, 0, 1, 2, 3, 4, 5, 0x4D2C6DFC)
    ROUNDb(35, 5, 6, 7, 0, 1, 2, 3, 4, 0x53380D13)
    ROUNDb(36, 4, 5, 6, 7, 0, 1, 2, 3, 0x650A7354)
    ROUNDb(37, 3, 4, 5, 6, 7, 0, 1, 2, 0x766A0ABB)
    ROUNDb(38, 2, 3, 4, 5, 6, 7, 0, 1, 0x81C2C92E)
    ROUNDb(39, 1, 2, 3, 4, 5, 6, 7, 0, 0x92722C85)
    ROUNDb(40, 0, 1, 2, 3, 4, 5, 6, 7, 0xA2BFE8A1)
    ROUNDb(41, 7, 0, 1, 2, 3, 4, 5, 6, 0xA81A664B)
    ROUNDb(42, 6, 7, 0, 1, 2, 3, 4, 5, 0xC24B8B70)
    ROUNDb(43, 5, 6, 7, 0, 1, 2, 3, 4, 0xC76C51A3)
    ROUNDb(44, 4, 5, 6, 7, 0, 1, 2, 3, 0xD192E819)
    ROUNDb(45, 3, 4, 5, 6, 7, 0, 1, 2, 0xD6990624)
    ROUNDb(46, 2, 3, 4, 5, 6, 7, 0, 1, 0xF40E3585)
    ROUNDb(47, 1, 2, 3, 4, 5, 6, 7, 0, 0x106AA070)
    ROUNDb(48, 0, 1, 2, 3, 4, 5, 6, 7, 0x19A4C116)
    ROUNDb(49, 7, 0, 1, 2, 3, 4, 5, 6, 0x1E376C08)
    ROUNDb(50, 6, 7, 0, 1, 2, 3, 4, 5, 0x2748774C)
    ROUNDb(51, 5, 6, 7, 0, 1, 2, 3, 4, 0x34B0BCB5)
    ROUNDb(52, 4, 5, 6, 7, 0, 1, 2, 3, 0x391C0CB3)
    ROUNDb(53, 3, 4, 5, 6, 7, 0, 1, 2, 0x4ED8AA4A)
    ROUNDb(54, 2, 3, 4, 5, 6, 7, 0, 1, 0x5B9CCA4F)
    ROUNDb(55, 1, 2, 3, 4, 5, 6, 7, 0, 0x682E6FF3)
    ROUNDb(56, 0, 1, 2, 3, 4, 5, 6, 7, 0x748F82EE)
    ROUNDb(57, 7, 0, 1, 2, 3, 4, 5, 6, 0x78A5636F)
    ROUNDb(58, 6, 7, 0, 1, 2, 3, 4, 5, 0x84C87814)
    ROUNDb(59, 5, 6, 7, 0, 1, 2, 3, 4, 0x8CC70208)
    ROUNDb(60, 4, 5, 6, 7, 0, 1, 2, 3, 0x90BEFFFA)
    ROUNDb(61, 3, 4, 5, 6, 7, 0, 1, 2, 0xA4506CEB)
    ROUNDb(62, 2, 3, 4, 5, 6, 7, 0, 1, 0xBEF9A3F7)
    ROUNDb(63, 1, 2, 3, 4, 5, 6, 7, 0, 0xC67178F2)

    /* Add to state */
    movl 116(%esp), %esi /* Argument: state */
    movl 0(%esp), %eax; addl %eax, 0(%esi)
    movl 4(%esp), %eax; addl %eax, 4(%esi)
    movl 8(%esp), %eax; addl %eax, 8(%esi)
    movl 12(%esp), %eax; addl %eax, 12(%esi)
    movl 16(%esp), %eax; addl %eax, 16(%esi)
    movl 20(%esp), %eax; addl %eax, 20(%esi)
    movl 24(%esp), %eax; addl %eax, 24(%esi)
    movl 28(%esp), %eax; addl %eax, 28(%esi)

    /* Restore registers */
    movl 96(%esp), %ebx
    movl 100(%esp), %esi
    movl 104(%esp), %edi
    movl 108(%esp), %ebp
    addl $112, %esp
    retl
    196 changes: 0 additions & 196 deletions sha256.c
    Original file line number Diff line number Diff line change
    @@ -1,196 +0,0 @@
    /*
    * SHA-256 hash in C
    *
    * Copyright (c) 2014 Project Nayuki
    * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
    *
    * (MIT License)
    * 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.
    */

    #include <stdint.h>


    void sha256_compress(uint32_t state[8], const uint8_t block[64]) {
    // 32-bit right rotation
    #define ROR(x, i) \
    (((x) << (32 - (i))) | ((x) >> (i)))

    #define LOADSCHEDULE(i) \
    schedule[i] = \
    (uint32_t)block[i * 4 + 0] << 24 \
    | (uint32_t)block[i * 4 + 1] << 16 \
    | (uint32_t)block[i * 4 + 2] << 8 \
    | (uint32_t)block[i * 4 + 3];

    #define SCHEDULE(i) \
    schedule[i] = schedule[i - 16] + schedule[i - 7] \
    + (ROR(schedule[i - 15], 7) ^ ROR(schedule[i - 15], 18) ^ (schedule[i - 15] >> 3)) \
    + (ROR(schedule[i - 2], 17) ^ ROR(schedule[i - 2], 19) ^ (schedule[i - 2] >> 10));

    #define ROUND(a, b, c, d, e, f, g, h, i, k) \
    h += (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + (g ^ (e & (f ^ g))) + UINT32_C(k) + schedule[i]; \
    d += h; \
    h += (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & (b | c)) | (b & c));

    uint32_t schedule[64];
    LOADSCHEDULE( 0)
    LOADSCHEDULE( 1)
    LOADSCHEDULE( 2)
    LOADSCHEDULE( 3)
    LOADSCHEDULE( 4)
    LOADSCHEDULE( 5)
    LOADSCHEDULE( 6)
    LOADSCHEDULE( 7)
    LOADSCHEDULE( 8)
    LOADSCHEDULE( 9)
    LOADSCHEDULE(10)
    LOADSCHEDULE(11)
    LOADSCHEDULE(12)
    LOADSCHEDULE(13)
    LOADSCHEDULE(14)
    LOADSCHEDULE(15)
    SCHEDULE(16)
    SCHEDULE(17)
    SCHEDULE(18)
    SCHEDULE(19)
    SCHEDULE(20)
    SCHEDULE(21)
    SCHEDULE(22)
    SCHEDULE(23)
    SCHEDULE(24)
    SCHEDULE(25)
    SCHEDULE(26)
    SCHEDULE(27)
    SCHEDULE(28)
    SCHEDULE(29)
    SCHEDULE(30)
    SCHEDULE(31)
    SCHEDULE(32)
    SCHEDULE(33)
    SCHEDULE(34)
    SCHEDULE(35)
    SCHEDULE(36)
    SCHEDULE(37)
    SCHEDULE(38)
    SCHEDULE(39)
    SCHEDULE(40)
    SCHEDULE(41)
    SCHEDULE(42)
    SCHEDULE(43)
    SCHEDULE(44)
    SCHEDULE(45)
    SCHEDULE(46)
    SCHEDULE(47)
    SCHEDULE(48)
    SCHEDULE(49)
    SCHEDULE(50)
    SCHEDULE(51)
    SCHEDULE(52)
    SCHEDULE(53)
    SCHEDULE(54)
    SCHEDULE(55)
    SCHEDULE(56)
    SCHEDULE(57)
    SCHEDULE(58)
    SCHEDULE(59)
    SCHEDULE(60)
    SCHEDULE(61)
    SCHEDULE(62)
    SCHEDULE(63)

    uint32_t a = state[0];
    uint32_t b = state[1];
    uint32_t c = state[2];
    uint32_t d = state[3];
    uint32_t e = state[4];
    uint32_t f = state[5];
    uint32_t g = state[6];
    uint32_t h = state[7];
    ROUND(a, b, c, d, e, f, g, h, 0, 0x428A2F98)
    ROUND(h, a, b, c, d, e, f, g, 1, 0x71374491)
    ROUND(g, h, a, b, c, d, e, f, 2, 0xB5C0FBCF)
    ROUND(f, g, h, a, b, c, d, e, 3, 0xE9B5DBA5)
    ROUND(e, f, g, h, a, b, c, d, 4, 0x3956C25B)
    ROUND(d, e, f, g, h, a, b, c, 5, 0x59F111F1)
    ROUND(c, d, e, f, g, h, a, b, 6, 0x923F82A4)
    ROUND(b, c, d, e, f, g, h, a, 7, 0xAB1C5ED5)
    ROUND(a, b, c, d, e, f, g, h, 8, 0xD807AA98)
    ROUND(h, a, b, c, d, e, f, g, 9, 0x12835B01)
    ROUND(g, h, a, b, c, d, e, f, 10, 0x243185BE)
    ROUND(f, g, h, a, b, c, d, e, 11, 0x550C7DC3)
    ROUND(e, f, g, h, a, b, c, d, 12, 0x72BE5D74)
    ROUND(d, e, f, g, h, a, b, c, 13, 0x80DEB1FE)
    ROUND(c, d, e, f, g, h, a, b, 14, 0x9BDC06A7)
    ROUND(b, c, d, e, f, g, h, a, 15, 0xC19BF174)
    ROUND(a, b, c, d, e, f, g, h, 16, 0xE49B69C1)
    ROUND(h, a, b, c, d, e, f, g, 17, 0xEFBE4786)
    ROUND(g, h, a, b, c, d, e, f, 18, 0x0FC19DC6)
    ROUND(f, g, h, a, b, c, d, e, 19, 0x240CA1CC)
    ROUND(e, f, g, h, a, b, c, d, 20, 0x2DE92C6F)
    ROUND(d, e, f, g, h, a, b, c, 21, 0x4A7484AA)
    ROUND(c, d, e, f, g, h, a, b, 22, 0x5CB0A9DC)
    ROUND(b, c, d, e, f, g, h, a, 23, 0x76F988DA)
    ROUND(a, b, c, d, e, f, g, h, 24, 0x983E5152)
    ROUND(h, a, b, c, d, e, f, g, 25, 0xA831C66D)
    ROUND(g, h, a, b, c, d, e, f, 26, 0xB00327C8)
    ROUND(f, g, h, a, b, c, d, e, 27, 0xBF597FC7)
    ROUND(e, f, g, h, a, b, c, d, 28, 0xC6E00BF3)
    ROUND(d, e, f, g, h, a, b, c, 29, 0xD5A79147)
    ROUND(c, d, e, f, g, h, a, b, 30, 0x06CA6351)
    ROUND(b, c, d, e, f, g, h, a, 31, 0x14292967)
    ROUND(a, b, c, d, e, f, g, h, 32, 0x27B70A85)
    ROUND(h, a, b, c, d, e, f, g, 33, 0x2E1B2138)
    ROUND(g, h, a, b, c, d, e, f, 34, 0x4D2C6DFC)
    ROUND(f, g, h, a, b, c, d, e, 35, 0x53380D13)
    ROUND(e, f, g, h, a, b, c, d, 36, 0x650A7354)
    ROUND(d, e, f, g, h, a, b, c, 37, 0x766A0ABB)
    ROUND(c, d, e, f, g, h, a, b, 38, 0x81C2C92E)
    ROUND(b, c, d, e, f, g, h, a, 39, 0x92722C85)
    ROUND(a, b, c, d, e, f, g, h, 40, 0xA2BFE8A1)
    ROUND(h, a, b, c, d, e, f, g, 41, 0xA81A664B)
    ROUND(g, h, a, b, c, d, e, f, 42, 0xC24B8B70)
    ROUND(f, g, h, a, b, c, d, e, 43, 0xC76C51A3)
    ROUND(e, f, g, h, a, b, c, d, 44, 0xD192E819)
    ROUND(d, e, f, g, h, a, b, c, 45, 0xD6990624)
    ROUND(c, d, e, f, g, h, a, b, 46, 0xF40E3585)
    ROUND(b, c, d, e, f, g, h, a, 47, 0x106AA070)
    ROUND(a, b, c, d, e, f, g, h, 48, 0x19A4C116)
    ROUND(h, a, b, c, d, e, f, g, 49, 0x1E376C08)
    ROUND(g, h, a, b, c, d, e, f, 50, 0x2748774C)
    ROUND(f, g, h, a, b, c, d, e, 51, 0x34B0BCB5)
    ROUND(e, f, g, h, a, b, c, d, 52, 0x391C0CB3)
    ROUND(d, e, f, g, h, a, b, c, 53, 0x4ED8AA4A)
    ROUND(c, d, e, f, g, h, a, b, 54, 0x5B9CCA4F)
    ROUND(b, c, d, e, f, g, h, a, 55, 0x682E6FF3)
    ROUND(a, b, c, d, e, f, g, h, 56, 0x748F82EE)
    ROUND(h, a, b, c, d, e, f, g, 57, 0x78A5636F)
    ROUND(g, h, a, b, c, d, e, f, 58, 0x84C87814)
    ROUND(f, g, h, a, b, c, d, e, 59, 0x8CC70208)
    ROUND(e, f, g, h, a, b, c, d, 60, 0x90BEFFFA)
    ROUND(d, e, f, g, h, a, b, c, 61, 0xA4506CEB)
    ROUND(c, d, e, f, g, h, a, b, 62, 0xBEF9A3F7)
    ROUND(b, c, d, e, f, g, h, a, 63, 0xC67178F2)
    state[0] += a;
    state[1] += b;
    state[2] += c;
    state[3] += d;
    state[4] += e;
    state[5] += f;
    state[6] += g;
    state[7] += h;
    }
    151 changes: 56 additions & 95 deletions sha256test.c
    Original file line number Diff line number Diff line change
    @@ -1,116 +1,77 @@
    /*
    * SHA-256 hash in C and x86 assembly
    *
    * Copyright (c) 2014 Project Nayuki
    * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
    *
    * (MIT License)
    * 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.
    */

    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <openssl/sha.h>


    void sha256_hash(const uint8_t *message, uint32_t len, uint32_t hash[8]);

    // Link this program with an external C or x86 compression function
    extern void sha256_compress(uint32_t state[8], const uint8_t block[64]);


    int count_leading_zeroes(uint32_t hash[8]) {
    // todo maybe use clz cpu instruction
    int count_leading_zeroes(unsigned char* hash, int len) {
    int count = 0;
    for(int i = 0; i < 8; i++) {
    if(hash[i] != 0) return count + __builtin_clz(hash[i]);
    else count += 32;
    for(int i = 0; i < len; i++) {
    if(hash[i] != 0) {
    if(hash[i] & 128) return count;
    else if(hash[i] & 64) return count + 1;
    else if(hash[i] & 32) return count + 2;
    else if(hash[i] & 16) return count + 3;
    else if(hash[i] & 8) return count + 4;
    else if(hash[i] & 4) return count + 5;
    else if(hash[i] & 2) return count + 6;
    else if(hash[i] & 1) return count + 7;
    } else count += 8;
    }
    return count;
    }

    int mainshit(int argc, char**argv) {
    uint32_t bla[] = {0x000000000,0, 0x0fffffff, 0,0,0,0,0,0};
    printf("%d", count_leading_zeroes(bla));
    int HASH_LENGTH = 256 / 8;

    double timediff(struct timespec *tstart, struct timespec *tend) {
    return ((double)tend->tv_sec + 1.0e-9*tend->tv_nsec) -
    ((double)tstart->tv_sec + 1.0e-9*tstart->tv_nsec);
    }

    /* Main program */
    int print_every = (int)1e8;

    char* ToString(char* bufferEnd, uint64_t i) {
    // http://stackoverflow.com/a/18859192/2639190
    char *p = bufferEnd;
    *(--p) = '\0';
    lldiv_t qr;
    qr.quot = i;
    do {
    qr = lldiv(qr.quot, 10);
    *(--p) = llabs(qr.rem) + '0';
    } while (qr.quot);
    return p;
    }

    int main(int argc, char **argv) {
    int minbits = atoi(argv[1]);
    struct timespec res;
    clock_gettime(CLOCK_MONOTONIC, &res);
    srand(res.tv_nsec);
    char message_template[200];
    sprintf(message_template, "%s %08x%%016llx", argv[2], rand());
    uint8_t message[200];
    uint32_t hash[8];
    int best_count = 0;
    time_t last = time(NULL);
    for(uint64_t i = 0; i < 1e15; i++) {
    sprintf(message, message_template, i);
    if(i%(int)1e8 == 0) {
    fprintf(stderr, "%.3f MHash/s\n", 1e8/1e6/((time(NULL)-last)));
    last = time(NULL);
    struct timespec begin;
    clock_gettime(CLOCK_MONOTONIC, &begin);
    char message_constant[200];
    int message_constant_len = sprintf(message_constant, "%s %09ld.", argv[2], begin.tv_nsec);
    unsigned char hash[HASH_LENGTH];
    SHA256_CTX constant_ctx;
    SHA256_Init(&constant_ctx);
    SHA256_Update(&constant_ctx, message_constant, message_constant_len);
    SHA256_CTX full_ctx;
    char message_tail_buf[200];
    for(uint64_t i = 0; i < 1e16; i++) {
    full_ctx = constant_ctx;
    char* message_tail = ToString(message_tail_buf + 200, i);
    SHA256_Update(&full_ctx, message_tail, (message_tail_buf + 200) - message_tail - 1);
    SHA256_Final(hash, &full_ctx);
    if(i!=0 && i%print_every == 0) {
    struct timespec end; clock_gettime(CLOCK_MONOTONIC, &end);
    fprintf(stderr, "%.3f MHash/s\n", print_every/1e6/(timediff(&begin, &end)));
    begin = end;
    }
    sha256_hash(message, strlen(message), hash);
    if(count_leading_zeroes(hash) >= minbits) {
    printf("%08x%08x%08x%08x%08x%08x%08x%08x %s\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], message);
    if(count_leading_zeroes(hash, HASH_LENGTH) >= minbits) {
    for(int i = 0; i < HASH_LENGTH; i++) printf("%02x", hash[i]);
    printf(" %s%s\n", message_constant, message_tail);
    return 0;
    }
    }
    return 0;
    }




    /* Full message hasher */

    void sha256_hash(const uint8_t *message, uint32_t len, uint32_t hash[8]) {
    hash[0] = UINT32_C(0x6A09E667);
    hash[1] = UINT32_C(0xBB67AE85);
    hash[2] = UINT32_C(0x3C6EF372);
    hash[3] = UINT32_C(0xA54FF53A);
    hash[4] = UINT32_C(0x510E527F);
    hash[5] = UINT32_C(0x9B05688C);
    hash[6] = UINT32_C(0x1F83D9AB);
    hash[7] = UINT32_C(0x5BE0CD19);

    uint32_t i;
    for (i = 0; len - i >= 64; i += 64)
    sha256_compress(hash, message + i);

    uint8_t block[64];
    uint32_t rem = len - i;
    memcpy(block, message + i, rem);

    block[rem] = 0x80;
    rem++;
    if (64 - rem >= 8)
    memset(block + rem, 0, 56 - rem);
    else {
    memset(block + rem, 0, 64 - rem);
    sha256_compress(hash, block);
    memset(block, 0, 56);
    }

    uint64_t longLen = ((uint64_t)len) << 3;
    for (i = 0; i < 8; i++)
    block[64 - 1 - i] = (uint8_t)(longLen >> (i * 8));
    sha256_compress(hash, block);
    }
    Binary file added sha256test.gcda
    Binary file not shown.
  7. @phiresky phiresky revised this gist May 31, 2016. 3 changed files with 6 additions and 3 deletions.
    1 change: 1 addition & 0 deletions build.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    gcc -static -march=native -O3 sha256test.c sha256-64.S -o sha256test
    Binary file removed sha256test
    Binary file not shown.
    8 changes: 5 additions & 3 deletions sha256test.c
    Original file line number Diff line number Diff line change
    @@ -29,9 +29,6 @@
    #include <time.h>


    /* Function prototypes */

    static int self_check(void);
    void sha256_hash(const uint8_t *message, uint32_t len, uint32_t hash[8]);

    // Link this program with an external C or x86 compression function
    @@ -63,8 +60,13 @@ int main(int argc, char **argv) {
    uint8_t message[200];
    uint32_t hash[8];
    int best_count = 0;
    time_t last = time(NULL);
    for(uint64_t i = 0; i < 1e15; i++) {
    sprintf(message, message_template, i);
    if(i%(int)1e8 == 0) {
    fprintf(stderr, "%.3f MHash/s\n", 1e8/1e6/((time(NULL)-last)));
    last = time(NULL);
    }
    sha256_hash(message, strlen(message), hash);
    if(count_leading_zeroes(hash) >= minbits) {
    printf("%08x%08x%08x%08x%08x%08x%08x%08x %s\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], message);
  8. @phiresky phiresky revised this gist May 31, 2016. 2 changed files with 2 additions and 1 deletion.
    Binary file modified sha256test
    Binary file not shown.
    3 changes: 2 additions & 1 deletion sha256test.c
    Original file line number Diff line number Diff line change
    @@ -63,11 +63,12 @@ int main(int argc, char **argv) {
    uint8_t message[200];
    uint32_t hash[8];
    int best_count = 0;
    for(uint64_t i = 0; i < 1e10; i++) {
    for(uint64_t i = 0; i < 1e15; i++) {
    sprintf(message, message_template, i);
    sha256_hash(message, strlen(message), hash);
    if(count_leading_zeroes(hash) >= minbits) {
    printf("%08x%08x%08x%08x%08x%08x%08x%08x %s\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], message);
    return 0;
    }
    }
    return 0;
  9. @phiresky phiresky created this gist May 31, 2016.
    20 changes: 20 additions & 0 deletions findlatest.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/bash
    set -euf

    latestparent=unknown
    latestbits=0
    while true
    do
    echo $(date): checking for new blocks... >&2
    curl -s http://hash.h10a.de/?raw > latestinfo
    bits=$(head -1 latestinfo)
    parent=$(tail -n +2 latestinfo | sort -k2 -n|tail -1|cut -f1)
    if [ $parent != $latestparent ] || [ $bits != $latestbits ]; then
    latestparent=$parent
    latestbits=$bits
    echo $(date): new block found >&2
    echo $latestbits $latestparent
    fi
    sleep 3
    done

    22 changes: 22 additions & 0 deletions mine.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!/bin/bash
    set -euf

    username=$1

    ./findlatest.sh | while read bits parent
    do
    echo restarting mining, bits="$bits", parent="$parent" >&2
    killall --quiet --exact sha256test || echo starting
    (
    for processor in $(seq 1 $(nproc))
    do
    echo starting ./sha256test $bits "$parent $username" >&2
    ./sha256test $bits "$parent $username" &
    done | while read hash parent username seed
    do
    echo curl "http://hash.h10a.de/?Z=$parent&P=$username&R=$seed" >&2
    curl "http://hash.h10a.de/?Z=$parent&P=$username&R=$seed"
    done
    )&
    done

    231 changes: 231 additions & 0 deletions sha256-64.S
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,231 @@
    /*
    * SHA-256 hash in x86-64 assembly
    *
    * Copyright (c) 2015 Project Nayuki
    * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
    *
    * (MIT License)
    * 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.
    */


    /* void sha256_compress(uint32_t state[8], const uint8_t block[64]) */
    .globl sha256_compress
    sha256_compress:
    /*
    * Storage usage:
    * Bytes Location Description
    * 4 eax Temporary for calculation per round
    * 4 ebx Temporary for calculation per round
    * 4 ecx Temporary for calculation per round
    * 4 edx Temporary for calculation per round
    * 8 rsi Base address of block array argument (read-only)
    * 8 rdi Base address of state array argument (read-only)
    * 8 rsp x86-64 stack pointer
    * 4 r8d SHA-256 state variable A
    * 4 r9d SHA-256 state variable B
    * 4 r10d SHA-256 state variable C
    * 4 r11d SHA-256 state variable D
    * 4 r12d SHA-256 state variable E
    * 4 r13d SHA-256 state variable F
    * 4 r14d SHA-256 state variable G
    * 4 r15d SHA-256 state variable H
    * 64 [rsp+0] Circular buffer of most recent 16 key schedule items, 4 bytes each
    * 16 xmm0 Caller's value of r10 (only low 64 bits are used)
    * 16 xmm1 Caller's value of r11 (only low 64 bits are used)
    * 16 xmm2 Caller's value of r12 (only low 64 bits are used)
    * 16 xmm3 Caller's value of r13 (only low 64 bits are used)
    * 16 xmm4 Caller's value of r14 (only low 64 bits are used)
    * 16 xmm5 Caller's value of r15 (only low 64 bits are used)
    * 16 xmm6 Caller's value of rbx (only low 64 bits are used)
    */

    #define SCHED(i) (((i)&0xF)*4)(%rsp)

    #define ROUNDa(i, a, b, c, d, e, f, g, h, k) \
    movl (i*4)(%rsi), %ebx; \
    bswapl %ebx; \
    movl %ebx, SCHED(i); \
    ROUNDTAIL(a, b, c, d, e, f, g, h, k)

    #define ROUNDb(i, a, b, c, d, e, f, g, h, k) \
    movl SCHED(i-15), %eax; \
    movl SCHED(i-16), %ebx; \
    addl SCHED(i- 7), %ebx; \
    movl %eax, %ecx; \
    movl %eax, %edx; \
    rorl $18, %ecx; \
    shrl $3, %edx; \
    rorl $7, %eax; \
    xorl %edx, %ecx; \
    xorl %ecx, %eax; \
    addl %eax, %ebx; \
    movl SCHED(i- 2), %eax; \
    movl %eax, %ecx; \
    movl %eax, %edx; \
    rorl $19, %ecx; \
    shrl $10, %edx; \
    rorl $17, %eax; \
    xorl %edx, %ecx; \
    xorl %ecx, %eax; \
    addl %eax, %ebx; \
    movl %ebx, SCHED(i); \
    ROUNDTAIL(a, b, c, d, e, f, g, h, k)

    #define ROUNDTAIL(a, b, c, d, e, f, g, h, k) \
    /* Part 0 */ \
    movl %e, %ecx; \
    movl %e, %edx; \
    movl %e, %eax; \
    rorl $11, %ecx; \
    rorl $25, %edx; \
    rorl $6, %eax; \
    xorl %edx, %ecx; \
    xorl %ecx, %eax; \
    addl %ebx, %h; \
    movl %g, %ecx; \
    xorl %f, %ecx; \
    andl %e, %ecx; \
    xorl %g, %ecx; \
    leal k(%rax,%rcx), %eax; \
    addl %eax, %h; \
    /* Part 1 */ \
    addl %h, %d; \
    /* Part 2 */ \
    movl %a, %ecx; \
    movl %a, %edx; \
    movl %a, %eax; \
    rorl $13, %ecx; \
    rorl $22, %edx; \
    rorl $2, %eax; \
    xorl %edx, %ecx; \
    xorl %ecx, %eax; \
    movl %c, %ecx; \
    addl %eax, %h; \
    movl %c, %eax; \
    orl %b, %eax; \
    andl %b, %ecx; \
    andl %a, %eax; \
    orl %ecx, %eax; \
    addl %eax, %h;

    /* Save registers, allocate scratch space */
    movq %r10, %xmm0
    movq %r11, %xmm1
    movq %r12, %xmm2
    movq %r13, %xmm3
    movq %r14, %xmm4
    movq %r15, %xmm5
    movq %rbx, %xmm6
    subq $64, %rsp

    /* Load state */
    movl 0(%rdi), %r8d /* a */
    movl 4(%rdi), %r9d /* b */
    movl 8(%rdi), %r10d /* c */
    movl 12(%rdi), %r11d /* d */
    movl 16(%rdi), %r12d /* e */
    movl 20(%rdi), %r13d /* f */
    movl 24(%rdi), %r14d /* g */
    movl 28(%rdi), %r15d /* h */

    /* Do 64 rounds of hashing */
    ROUNDa( 0, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, 0x428A2F98)
    ROUNDa( 1, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x71374491)
    ROUNDa( 2, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, -0x4A3F0431)
    ROUNDa( 3, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, -0x164A245B)
    ROUNDa( 4, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x3956C25B)
    ROUNDa( 5, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, 0x59F111F1)
    ROUNDa( 6, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x6DC07D5C)
    ROUNDa( 7, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , -0x54E3A12B)
    ROUNDa( 8, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, -0x27F85568)
    ROUNDa( 9, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x12835B01)
    ROUNDa(10, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, 0x243185BE)
    ROUNDa(11, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, 0x550C7DC3)
    ROUNDa(12, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x72BE5D74)
    ROUNDa(13, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, -0x7F214E02)
    ROUNDa(14, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x6423F959)
    ROUNDa(15, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , -0x3E640E8C)
    ROUNDb(16, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, -0x1B64963F)
    ROUNDb(17, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, -0x1041B87A)
    ROUNDb(18, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, 0x0FC19DC6)
    ROUNDb(19, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, 0x240CA1CC)
    ROUNDb(20, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x2DE92C6F)
    ROUNDb(21, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, 0x4A7484AA)
    ROUNDb(22, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , 0x5CB0A9DC)
    ROUNDb(23, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , 0x76F988DA)
    ROUNDb(24, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, -0x67C1AEAE)
    ROUNDb(25, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, -0x57CE3993)
    ROUNDb(26, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, -0x4FFCD838)
    ROUNDb(27, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, -0x40A68039)
    ROUNDb(28, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, -0x391FF40D)
    ROUNDb(29, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, -0x2A586EB9)
    ROUNDb(30, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , 0x06CA6351)
    ROUNDb(31, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , 0x14292967)
    ROUNDb(32, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, 0x27B70A85)
    ROUNDb(33, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x2E1B2138)
    ROUNDb(34, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, 0x4D2C6DFC)
    ROUNDb(35, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, 0x53380D13)
    ROUNDb(36, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x650A7354)
    ROUNDb(37, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, 0x766A0ABB)
    ROUNDb(38, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x7E3D36D2)
    ROUNDb(39, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , -0x6D8DD37B)
    ROUNDb(40, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, -0x5D40175F)
    ROUNDb(41, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, -0x57E599B5)
    ROUNDb(42, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, -0x3DB47490)
    ROUNDb(43, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, -0x3893AE5D)
    ROUNDb(44, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, -0x2E6D17E7)
    ROUNDb(45, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, -0x2966F9DC)
    ROUNDb(46, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x0BF1CA7B)
    ROUNDb(47, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , 0x106AA070)
    ROUNDb(48, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, 0x19A4C116)
    ROUNDb(49, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x1E376C08)
    ROUNDb(50, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, 0x2748774C)
    ROUNDb(51, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, 0x34B0BCB5)
    ROUNDb(52, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, 0x391C0CB3)
    ROUNDb(53, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, 0x4ED8AA4A)
    ROUNDb(54, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , 0x5B9CCA4F)
    ROUNDb(55, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , 0x682E6FF3)
    ROUNDb(56, r8d , r9d , r10d, r11d, r12d, r13d, r14d, r15d, 0x748F82EE)
    ROUNDb(57, r15d, r8d , r9d , r10d, r11d, r12d, r13d, r14d, 0x78A5636F)
    ROUNDb(58, r14d, r15d, r8d , r9d , r10d, r11d, r12d, r13d, -0x7B3787EC)
    ROUNDb(59, r13d, r14d, r15d, r8d , r9d , r10d, r11d, r12d, -0x7338FDF8)
    ROUNDb(60, r12d, r13d, r14d, r15d, r8d , r9d , r10d, r11d, -0x6F410006)
    ROUNDb(61, r11d, r12d, r13d, r14d, r15d, r8d , r9d , r10d, -0x5BAF9315)
    ROUNDb(62, r10d, r11d, r12d, r13d, r14d, r15d, r8d , r9d , -0x41065C09)
    ROUNDb(63, r9d , r10d, r11d, r12d, r13d, r14d, r15d, r8d , -0x398E870E)

    /* Add to state */
    addl %r8d , 0(%rdi)
    addl %r9d , 4(%rdi)
    addl %r10d, 8(%rdi)
    addl %r11d, 12(%rdi)
    addl %r12d, 16(%rdi)
    addl %r13d, 20(%rdi)
    addl %r14d, 24(%rdi)
    addl %r15d, 28(%rdi)

    /* Restore registers */
    movq %xmm0, %r10
    movq %xmm1, %r11
    movq %xmm2, %r12
    movq %xmm3, %r13
    movq %xmm4, %r14
    movq %xmm5, %r15
    movq %xmm6, %rbx
    addq $64, %rsp
    retq
    228 changes: 228 additions & 0 deletions sha256.S
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,228 @@
    /*
    * SHA-256 hash in x86 assembly
    *
    * Copyright (c) 2014 Project Nayuki
    * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
    *
    * (MIT License)
    * 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.
    */


    /* void sha256_compress(uint32_t state[8], const uint8_t block[64]) */
    .globl sha256_compress
    sha256_compress:
    /*
    * Storage usage:
    * Bytes Location Description
    * 4 eax Temporary for calculation per round
    * 4 ebx Temporary for calculation per round
    * 4 ecx Temporary for calculation per round
    * 4 edx Temporary for calculation per round
    * 4 ebp Temporary for calculation per round
    * 4 esi (During state loading and update) base address of state array argument
    * (During hash rounds) temporary for calculation per round
    * 4 edi Base address of block array argument (during key schedule loading rounds only)
    * 4 esp x86 stack pointer
    * 32 [esp+ 0] SHA-256 state variables A,B,C,D,E,F,G,H (4 bytes each)
    * 64 [esp+ 32] Key schedule of 16 * 4 bytes
    * 4 [esp+ 96] Caller's value of ebx
    * 4 [esp+100] Caller's value of esi
    * 4 [esp+104] Caller's value of edi
    * 4 [esp+108] Caller's value of ebp
    */

    #define SCHED(i) ((((i)&0xF)+8)*4)(%esp)

    #define ROUNDa(i, a, b, c, d, e, f, g, h, k) \
    movl (i*4)(%edi), %ebp; \
    bswapl %ebp; \
    movl %ebp, SCHED(i); \
    ROUNDTAIL(i, a, b, c, d, e, f, g, h, k)

    #define ROUNDb(i, a, b, c, d, e, f, g, h, k) \
    movl SCHED(i-15), %eax; \
    movl SCHED(i-16), %ebp; \
    movl %eax, %ebx; \
    addl SCHED(i- 7), %ebp; \
    movl %eax, %ecx; \
    rorl $18, %ebx; \
    shrl $3, %ecx; \
    rorl $7, %eax; \
    xorl %ecx, %ebx; \
    xorl %ebx, %eax; \
    addl %eax, %ebp; \
    movl SCHED(i- 2), %eax; \
    movl %eax, %ebx; \
    movl %eax, %ecx; \
    rorl $19, %ebx; \
    shrl $10, %ecx; \
    rorl $17, %eax; \
    xorl %ecx, %ebx; \
    xorl %ebx, %eax; \
    addl %eax, %ebp; \
    movl %ebp, SCHED(i); \
    ROUNDTAIL(i, a, b, c, d, e, f, g, h, k)

    #define STATE(i) (i*4)(%esp)

    #define ROUNDTAIL(i, a, b, c, d, e, f, g, h, k) \
    /* Part 0 */ \
    movl STATE(e), %eax; \
    movl %eax, %ebx; \
    movl %eax, %ecx; \
    movl %eax, %edx; \
    rorl $11, %eax; \
    rorl $25, %ebx; \
    rorl $6, %ecx; \
    movl STATE(h), %esi; \
    xorl %ebx, %eax; \
    xorl %eax, %ecx; \
    addl %ebp, %esi; \
    movl STATE(g), %ebx; \
    movl STATE(f), %eax; \
    xorl %ebx, %eax; \
    andl %edx, %eax; \
    xorl %ebx, %eax; \
    leal k(%ecx,%eax), %ecx; \
    addl %ecx, %esi; \
    /* Part 1 */ \
    addl %esi, STATE(d); \
    /* Part 2 */ \
    movl STATE(a), %eax; \
    movl %eax, %ebx; \
    movl %eax, %ecx; \
    movl %eax, %edx; \
    rorl $13, %eax; \
    rorl $22, %ebx; \
    rorl $2, %ecx; \
    xorl %ebx, %eax; \
    xorl %eax, %ecx; \
    movl STATE(c), %eax; \
    addl %ecx, %esi; \
    movl %eax, %ecx; \
    movl STATE(b), %ebx; \
    orl %ebx, %ecx; \
    andl %ebx, %eax; \
    andl %edx, %ecx; \
    orl %eax, %ecx; \
    addl %ecx, %esi; \
    movl %esi, STATE(h);

    /* Allocate scratch space, save registers */
    subl $112, %esp
    movl %ebx, 96(%esp)
    movl %esi, 100(%esp)
    movl %edi, 104(%esp)
    movl %ebp, 108(%esp)

    /* Copy state */
    movl 116(%esp), %esi /* Argument: state */
    movl 0(%esi), %eax; movl %eax, 0(%esp)
    movl 4(%esi), %eax; movl %eax, 4(%esp)
    movl 8(%esi), %eax; movl %eax, 8(%esp)
    movl 12(%esi), %eax; movl %eax, 12(%esp)
    movl 16(%esi), %eax; movl %eax, 16(%esp)
    movl 20(%esi), %eax; movl %eax, 20(%esp)
    movl 24(%esi), %eax; movl %eax, 24(%esp)
    movl 28(%esi), %eax; movl %eax, 28(%esp)

    /* Do 64 rounds of hashing */
    movl 120(%esp), %edi /* Argument: block */
    ROUNDa( 0, 0, 1, 2, 3, 4, 5, 6, 7, 0x428A2F98)
    ROUNDa( 1, 7, 0, 1, 2, 3, 4, 5, 6, 0x71374491)
    ROUNDa( 2, 6, 7, 0, 1, 2, 3, 4, 5, 0xB5C0FBCF)
    ROUNDa( 3, 5, 6, 7, 0, 1, 2, 3, 4, 0xE9B5DBA5)
    ROUNDa( 4, 4, 5, 6, 7, 0, 1, 2, 3, 0x3956C25B)
    ROUNDa( 5, 3, 4, 5, 6, 7, 0, 1, 2, 0x59F111F1)
    ROUNDa( 6, 2, 3, 4, 5, 6, 7, 0, 1, 0x923F82A4)
    ROUNDa( 7, 1, 2, 3, 4, 5, 6, 7, 0, 0xAB1C5ED5)
    ROUNDa( 8, 0, 1, 2, 3, 4, 5, 6, 7, 0xD807AA98)
    ROUNDa( 9, 7, 0, 1, 2, 3, 4, 5, 6, 0x12835B01)
    ROUNDa(10, 6, 7, 0, 1, 2, 3, 4, 5, 0x243185BE)
    ROUNDa(11, 5, 6, 7, 0, 1, 2, 3, 4, 0x550C7DC3)
    ROUNDa(12, 4, 5, 6, 7, 0, 1, 2, 3, 0x72BE5D74)
    ROUNDa(13, 3, 4, 5, 6, 7, 0, 1, 2, 0x80DEB1FE)
    ROUNDa(14, 2, 3, 4, 5, 6, 7, 0, 1, 0x9BDC06A7)
    ROUNDa(15, 1, 2, 3, 4, 5, 6, 7, 0, 0xC19BF174)
    ROUNDb(16, 0, 1, 2, 3, 4, 5, 6, 7, 0xE49B69C1)
    ROUNDb(17, 7, 0, 1, 2, 3, 4, 5, 6, 0xEFBE4786)
    ROUNDb(18, 6, 7, 0, 1, 2, 3, 4, 5, 0x0FC19DC6)
    ROUNDb(19, 5, 6, 7, 0, 1, 2, 3, 4, 0x240CA1CC)
    ROUNDb(20, 4, 5, 6, 7, 0, 1, 2, 3, 0x2DE92C6F)
    ROUNDb(21, 3, 4, 5, 6, 7, 0, 1, 2, 0x4A7484AA)
    ROUNDb(22, 2, 3, 4, 5, 6, 7, 0, 1, 0x5CB0A9DC)
    ROUNDb(23, 1, 2, 3, 4, 5, 6, 7, 0, 0x76F988DA)
    ROUNDb(24, 0, 1, 2, 3, 4, 5, 6, 7, 0x983E5152)
    ROUNDb(25, 7, 0, 1, 2, 3, 4, 5, 6, 0xA831C66D)
    ROUNDb(26, 6, 7, 0, 1, 2, 3, 4, 5, 0xB00327C8)
    ROUNDb(27, 5, 6, 7, 0, 1, 2, 3, 4, 0xBF597FC7)
    ROUNDb(28, 4, 5, 6, 7, 0, 1, 2, 3, 0xC6E00BF3)
    ROUNDb(29, 3, 4, 5, 6, 7, 0, 1, 2, 0xD5A79147)
    ROUNDb(30, 2, 3, 4, 5, 6, 7, 0, 1, 0x06CA6351)
    ROUNDb(31, 1, 2, 3, 4, 5, 6, 7, 0, 0x14292967)
    ROUNDb(32, 0, 1, 2, 3, 4, 5, 6, 7, 0x27B70A85)
    ROUNDb(33, 7, 0, 1, 2, 3, 4, 5, 6, 0x2E1B2138)
    ROUNDb(34, 6, 7, 0, 1, 2, 3, 4, 5, 0x4D2C6DFC)
    ROUNDb(35, 5, 6, 7, 0, 1, 2, 3, 4, 0x53380D13)
    ROUNDb(36, 4, 5, 6, 7, 0, 1, 2, 3, 0x650A7354)
    ROUNDb(37, 3, 4, 5, 6, 7, 0, 1, 2, 0x766A0ABB)
    ROUNDb(38, 2, 3, 4, 5, 6, 7, 0, 1, 0x81C2C92E)
    ROUNDb(39, 1, 2, 3, 4, 5, 6, 7, 0, 0x92722C85)
    ROUNDb(40, 0, 1, 2, 3, 4, 5, 6, 7, 0xA2BFE8A1)
    ROUNDb(41, 7, 0, 1, 2, 3, 4, 5, 6, 0xA81A664B)
    ROUNDb(42, 6, 7, 0, 1, 2, 3, 4, 5, 0xC24B8B70)
    ROUNDb(43, 5, 6, 7, 0, 1, 2, 3, 4, 0xC76C51A3)
    ROUNDb(44, 4, 5, 6, 7, 0, 1, 2, 3, 0xD192E819)
    ROUNDb(45, 3, 4, 5, 6, 7, 0, 1, 2, 0xD6990624)
    ROUNDb(46, 2, 3, 4, 5, 6, 7, 0, 1, 0xF40E3585)
    ROUNDb(47, 1, 2, 3, 4, 5, 6, 7, 0, 0x106AA070)
    ROUNDb(48, 0, 1, 2, 3, 4, 5, 6, 7, 0x19A4C116)
    ROUNDb(49, 7, 0, 1, 2, 3, 4, 5, 6, 0x1E376C08)
    ROUNDb(50, 6, 7, 0, 1, 2, 3, 4, 5, 0x2748774C)
    ROUNDb(51, 5, 6, 7, 0, 1, 2, 3, 4, 0x34B0BCB5)
    ROUNDb(52, 4, 5, 6, 7, 0, 1, 2, 3, 0x391C0CB3)
    ROUNDb(53, 3, 4, 5, 6, 7, 0, 1, 2, 0x4ED8AA4A)
    ROUNDb(54, 2, 3, 4, 5, 6, 7, 0, 1, 0x5B9CCA4F)
    ROUNDb(55, 1, 2, 3, 4, 5, 6, 7, 0, 0x682E6FF3)
    ROUNDb(56, 0, 1, 2, 3, 4, 5, 6, 7, 0x748F82EE)
    ROUNDb(57, 7, 0, 1, 2, 3, 4, 5, 6, 0x78A5636F)
    ROUNDb(58, 6, 7, 0, 1, 2, 3, 4, 5, 0x84C87814)
    ROUNDb(59, 5, 6, 7, 0, 1, 2, 3, 4, 0x8CC70208)
    ROUNDb(60, 4, 5, 6, 7, 0, 1, 2, 3, 0x90BEFFFA)
    ROUNDb(61, 3, 4, 5, 6, 7, 0, 1, 2, 0xA4506CEB)
    ROUNDb(62, 2, 3, 4, 5, 6, 7, 0, 1, 0xBEF9A3F7)
    ROUNDb(63, 1, 2, 3, 4, 5, 6, 7, 0, 0xC67178F2)

    /* Add to state */
    movl 116(%esp), %esi /* Argument: state */
    movl 0(%esp), %eax; addl %eax, 0(%esi)
    movl 4(%esp), %eax; addl %eax, 4(%esi)
    movl 8(%esp), %eax; addl %eax, 8(%esi)
    movl 12(%esp), %eax; addl %eax, 12(%esi)
    movl 16(%esp), %eax; addl %eax, 16(%esi)
    movl 20(%esp), %eax; addl %eax, 20(%esi)
    movl 24(%esp), %eax; addl %eax, 24(%esi)
    movl 28(%esp), %eax; addl %eax, 28(%esi)

    /* Restore registers */
    movl 96(%esp), %ebx
    movl 100(%esp), %esi
    movl 104(%esp), %edi
    movl 108(%esp), %ebp
    addl $112, %esp
    retl
    196 changes: 196 additions & 0 deletions sha256.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,196 @@
    /*
    * SHA-256 hash in C
    *
    * Copyright (c) 2014 Project Nayuki
    * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
    *
    * (MIT License)
    * 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.
    */

    #include <stdint.h>


    void sha256_compress(uint32_t state[8], const uint8_t block[64]) {
    // 32-bit right rotation
    #define ROR(x, i) \
    (((x) << (32 - (i))) | ((x) >> (i)))

    #define LOADSCHEDULE(i) \
    schedule[i] = \
    (uint32_t)block[i * 4 + 0] << 24 \
    | (uint32_t)block[i * 4 + 1] << 16 \
    | (uint32_t)block[i * 4 + 2] << 8 \
    | (uint32_t)block[i * 4 + 3];

    #define SCHEDULE(i) \
    schedule[i] = schedule[i - 16] + schedule[i - 7] \
    + (ROR(schedule[i - 15], 7) ^ ROR(schedule[i - 15], 18) ^ (schedule[i - 15] >> 3)) \
    + (ROR(schedule[i - 2], 17) ^ ROR(schedule[i - 2], 19) ^ (schedule[i - 2] >> 10));

    #define ROUND(a, b, c, d, e, f, g, h, i, k) \
    h += (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + (g ^ (e & (f ^ g))) + UINT32_C(k) + schedule[i]; \
    d += h; \
    h += (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & (b | c)) | (b & c));

    uint32_t schedule[64];
    LOADSCHEDULE( 0)
    LOADSCHEDULE( 1)
    LOADSCHEDULE( 2)
    LOADSCHEDULE( 3)
    LOADSCHEDULE( 4)
    LOADSCHEDULE( 5)
    LOADSCHEDULE( 6)
    LOADSCHEDULE( 7)
    LOADSCHEDULE( 8)
    LOADSCHEDULE( 9)
    LOADSCHEDULE(10)
    LOADSCHEDULE(11)
    LOADSCHEDULE(12)
    LOADSCHEDULE(13)
    LOADSCHEDULE(14)
    LOADSCHEDULE(15)
    SCHEDULE(16)
    SCHEDULE(17)
    SCHEDULE(18)
    SCHEDULE(19)
    SCHEDULE(20)
    SCHEDULE(21)
    SCHEDULE(22)
    SCHEDULE(23)
    SCHEDULE(24)
    SCHEDULE(25)
    SCHEDULE(26)
    SCHEDULE(27)
    SCHEDULE(28)
    SCHEDULE(29)
    SCHEDULE(30)
    SCHEDULE(31)
    SCHEDULE(32)
    SCHEDULE(33)
    SCHEDULE(34)
    SCHEDULE(35)
    SCHEDULE(36)
    SCHEDULE(37)
    SCHEDULE(38)
    SCHEDULE(39)
    SCHEDULE(40)
    SCHEDULE(41)
    SCHEDULE(42)
    SCHEDULE(43)
    SCHEDULE(44)
    SCHEDULE(45)
    SCHEDULE(46)
    SCHEDULE(47)
    SCHEDULE(48)
    SCHEDULE(49)
    SCHEDULE(50)
    SCHEDULE(51)
    SCHEDULE(52)
    SCHEDULE(53)
    SCHEDULE(54)
    SCHEDULE(55)
    SCHEDULE(56)
    SCHEDULE(57)
    SCHEDULE(58)
    SCHEDULE(59)
    SCHEDULE(60)
    SCHEDULE(61)
    SCHEDULE(62)
    SCHEDULE(63)

    uint32_t a = state[0];
    uint32_t b = state[1];
    uint32_t c = state[2];
    uint32_t d = state[3];
    uint32_t e = state[4];
    uint32_t f = state[5];
    uint32_t g = state[6];
    uint32_t h = state[7];
    ROUND(a, b, c, d, e, f, g, h, 0, 0x428A2F98)
    ROUND(h, a, b, c, d, e, f, g, 1, 0x71374491)
    ROUND(g, h, a, b, c, d, e, f, 2, 0xB5C0FBCF)
    ROUND(f, g, h, a, b, c, d, e, 3, 0xE9B5DBA5)
    ROUND(e, f, g, h, a, b, c, d, 4, 0x3956C25B)
    ROUND(d, e, f, g, h, a, b, c, 5, 0x59F111F1)
    ROUND(c, d, e, f, g, h, a, b, 6, 0x923F82A4)
    ROUND(b, c, d, e, f, g, h, a, 7, 0xAB1C5ED5)
    ROUND(a, b, c, d, e, f, g, h, 8, 0xD807AA98)
    ROUND(h, a, b, c, d, e, f, g, 9, 0x12835B01)
    ROUND(g, h, a, b, c, d, e, f, 10, 0x243185BE)
    ROUND(f, g, h, a, b, c, d, e, 11, 0x550C7DC3)
    ROUND(e, f, g, h, a, b, c, d, 12, 0x72BE5D74)
    ROUND(d, e, f, g, h, a, b, c, 13, 0x80DEB1FE)
    ROUND(c, d, e, f, g, h, a, b, 14, 0x9BDC06A7)
    ROUND(b, c, d, e, f, g, h, a, 15, 0xC19BF174)
    ROUND(a, b, c, d, e, f, g, h, 16, 0xE49B69C1)
    ROUND(h, a, b, c, d, e, f, g, 17, 0xEFBE4786)
    ROUND(g, h, a, b, c, d, e, f, 18, 0x0FC19DC6)
    ROUND(f, g, h, a, b, c, d, e, 19, 0x240CA1CC)
    ROUND(e, f, g, h, a, b, c, d, 20, 0x2DE92C6F)
    ROUND(d, e, f, g, h, a, b, c, 21, 0x4A7484AA)
    ROUND(c, d, e, f, g, h, a, b, 22, 0x5CB0A9DC)
    ROUND(b, c, d, e, f, g, h, a, 23, 0x76F988DA)
    ROUND(a, b, c, d, e, f, g, h, 24, 0x983E5152)
    ROUND(h, a, b, c, d, e, f, g, 25, 0xA831C66D)
    ROUND(g, h, a, b, c, d, e, f, 26, 0xB00327C8)
    ROUND(f, g, h, a, b, c, d, e, 27, 0xBF597FC7)
    ROUND(e, f, g, h, a, b, c, d, 28, 0xC6E00BF3)
    ROUND(d, e, f, g, h, a, b, c, 29, 0xD5A79147)
    ROUND(c, d, e, f, g, h, a, b, 30, 0x06CA6351)
    ROUND(b, c, d, e, f, g, h, a, 31, 0x14292967)
    ROUND(a, b, c, d, e, f, g, h, 32, 0x27B70A85)
    ROUND(h, a, b, c, d, e, f, g, 33, 0x2E1B2138)
    ROUND(g, h, a, b, c, d, e, f, 34, 0x4D2C6DFC)
    ROUND(f, g, h, a, b, c, d, e, 35, 0x53380D13)
    ROUND(e, f, g, h, a, b, c, d, 36, 0x650A7354)
    ROUND(d, e, f, g, h, a, b, c, 37, 0x766A0ABB)
    ROUND(c, d, e, f, g, h, a, b, 38, 0x81C2C92E)
    ROUND(b, c, d, e, f, g, h, a, 39, 0x92722C85)
    ROUND(a, b, c, d, e, f, g, h, 40, 0xA2BFE8A1)
    ROUND(h, a, b, c, d, e, f, g, 41, 0xA81A664B)
    ROUND(g, h, a, b, c, d, e, f, 42, 0xC24B8B70)
    ROUND(f, g, h, a, b, c, d, e, 43, 0xC76C51A3)
    ROUND(e, f, g, h, a, b, c, d, 44, 0xD192E819)
    ROUND(d, e, f, g, h, a, b, c, 45, 0xD6990624)
    ROUND(c, d, e, f, g, h, a, b, 46, 0xF40E3585)
    ROUND(b, c, d, e, f, g, h, a, 47, 0x106AA070)
    ROUND(a, b, c, d, e, f, g, h, 48, 0x19A4C116)
    ROUND(h, a, b, c, d, e, f, g, 49, 0x1E376C08)
    ROUND(g, h, a, b, c, d, e, f, 50, 0x2748774C)
    ROUND(f, g, h, a, b, c, d, e, 51, 0x34B0BCB5)
    ROUND(e, f, g, h, a, b, c, d, 52, 0x391C0CB3)
    ROUND(d, e, f, g, h, a, b, c, 53, 0x4ED8AA4A)
    ROUND(c, d, e, f, g, h, a, b, 54, 0x5B9CCA4F)
    ROUND(b, c, d, e, f, g, h, a, 55, 0x682E6FF3)
    ROUND(a, b, c, d, e, f, g, h, 56, 0x748F82EE)
    ROUND(h, a, b, c, d, e, f, g, 57, 0x78A5636F)
    ROUND(g, h, a, b, c, d, e, f, 58, 0x84C87814)
    ROUND(f, g, h, a, b, c, d, e, 59, 0x8CC70208)
    ROUND(e, f, g, h, a, b, c, d, 60, 0x90BEFFFA)
    ROUND(d, e, f, g, h, a, b, c, 61, 0xA4506CEB)
    ROUND(c, d, e, f, g, h, a, b, 62, 0xBEF9A3F7)
    ROUND(b, c, d, e, f, g, h, a, 63, 0xC67178F2)
    state[0] += a;
    state[1] += b;
    state[2] += c;
    state[3] += d;
    state[4] += e;
    state[5] += f;
    state[6] += g;
    state[7] += h;
    }
    Binary file added sha256test
    Binary file not shown.
    113 changes: 113 additions & 0 deletions sha256test.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    /*
    * SHA-256 hash in C and x86 assembly
    *
    * Copyright (c) 2014 Project Nayuki
    * https://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
    *
    * (MIT License)
    * 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.
    */

    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>


    /* Function prototypes */

    static int self_check(void);
    void sha256_hash(const uint8_t *message, uint32_t len, uint32_t hash[8]);

    // Link this program with an external C or x86 compression function
    extern void sha256_compress(uint32_t state[8], const uint8_t block[64]);


    int count_leading_zeroes(uint32_t hash[8]) {
    int count = 0;
    for(int i = 0; i < 8; i++) {
    if(hash[i] != 0) return count + __builtin_clz(hash[i]);
    else count += 32;
    }
    }

    int mainshit(int argc, char**argv) {
    uint32_t bla[] = {0x000000000,0, 0x0fffffff, 0,0,0,0,0,0};
    printf("%d", count_leading_zeroes(bla));
    }

    /* Main program */

    int main(int argc, char **argv) {
    int minbits = atoi(argv[1]);
    struct timespec res;
    clock_gettime(CLOCK_MONOTONIC, &res);
    srand(res.tv_nsec);
    char message_template[200];
    sprintf(message_template, "%s %08x%%016llx", argv[2], rand());
    uint8_t message[200];
    uint32_t hash[8];
    int best_count = 0;
    for(uint64_t i = 0; i < 1e10; i++) {
    sprintf(message, message_template, i);
    sha256_hash(message, strlen(message), hash);
    if(count_leading_zeroes(hash) >= minbits) {
    printf("%08x%08x%08x%08x%08x%08x%08x%08x %s\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], message);
    }
    }
    return 0;
    }




    /* Full message hasher */

    void sha256_hash(const uint8_t *message, uint32_t len, uint32_t hash[8]) {
    hash[0] = UINT32_C(0x6A09E667);
    hash[1] = UINT32_C(0xBB67AE85);
    hash[2] = UINT32_C(0x3C6EF372);
    hash[3] = UINT32_C(0xA54FF53A);
    hash[4] = UINT32_C(0x510E527F);
    hash[5] = UINT32_C(0x9B05688C);
    hash[6] = UINT32_C(0x1F83D9AB);
    hash[7] = UINT32_C(0x5BE0CD19);

    uint32_t i;
    for (i = 0; len - i >= 64; i += 64)
    sha256_compress(hash, message + i);

    uint8_t block[64];
    uint32_t rem = len - i;
    memcpy(block, message + i, rem);

    block[rem] = 0x80;
    rem++;
    if (64 - rem >= 8)
    memset(block + rem, 0, 56 - rem);
    else {
    memset(block + rem, 0, 64 - rem);
    sha256_compress(hash, block);
    memset(block, 0, 56);
    }

    uint64_t longLen = ((uint64_t)len) << 3;
    for (i = 0; i < 8; i++)
    block[64 - 1 - i] = (uint8_t)(longLen >> (i * 8));
    sha256_compress(hash, block);
    }