Skip to content

Instantly share code, notes, and snippets.

Created April 26, 2013 20:53
Show Gist options
  • Select an option

  • Save anonymous/5470372 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/5470372 to your computer and use it in GitHub Desktop.

To run the Java version:

To run the Clojure version:

To run the Ruby and Python versions:

  • Paste it into miner.rb and run time ruby miner.rb
  • Or into miner.py and run time python miner.py.

My results:

  • Java: 11.640 seconds
  • Clojure: 11.692 seconds
  • Python: 32.628 seconds
  • Ruby: 47.892 seconds

-- Dobry Den

# Clojure
(ns tnt.miner
(:use [digest] [criterium.core])
(:require [clojure.string :refer [join]])
(:import [java.security Security MessageDigest]
[org.apache.commons.codec.binary Hex])
(:gen-class))
;(set! *unchecked-math* true)
;(set! *warn-on-reflection* true)
(def difficulty 7)
(def winning-prefix (join (repeat difficulty "0")))
(defn sha512 ^String [^String s]
(-> (MessageDigest/getInstance "SHA-512")
(.digest (.getBytes s))
(Hex/encodeHex)
(String.)))
(defn worker [seed]
(loop [n 0]
(let [try (str seed n)
hash (sha512 try)]
(if (.startsWith hash winning-prefix)
(print try "-")
(recur (inc n))))))
(defn -main [& args]
(time (worker "kNTny")))
// Java
import static org.apache.commons.codec.binary.Hex.encodeHex;
import java.security.MessageDigest;
public class BenchMark {
final static int difficulty = 7;
static String winningPrefix = "0000000";
static void worker(String seed) {
int num = 0;
while (true) {
String attempt = seed + num;
try {
String hash = sha512(attempt);
if (hash.startsWith(winningPrefix)) {
System.out.println(attempt);
System.exit(0);
}
} catch (Exception e) {
}
num++;
}
}
static String sha512(String message) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] digest = md.digest(message.getBytes());
return new String(encodeHex(digest));
}
public static void main(String[] args) throws Exception {
worker("kNTny");
}
}
# Python - just ripped the loop out of Plausibility's miner
import sys
import time
import string
import hashlib
def worker():
difficulty = 7
num = 0
buf = "0" * difficulty
work = "kNTny"
while True:
work_hash = hashlib.sha512(work + str(num)).hexdigest()
if work_hash[0:difficulty] == buf:
print(work + str(num))
print(work_hash)
break
num += 1
worker()
require "digest/sha2"
Worker = Struct.new(:seed, :winning_prefix) do
def work
n = 0
begin
hash = sha512("#{seed}#{n}")
n += 1
end until hash.start_with?(winning_prefix)
hash
end
def sha512(str)
Digest::SHA2.new(512).update(str).hexdigest
end
end
difficulty = 7
winning_prefix = "0" * difficulty
puts Worker.new("kNTny", winning_prefix).work
@sysr-q
Copy link

sysr-q commented Apr 27, 2013

Just to clarify to readers, the magic number we're looking to reach to hash is kNTny7203745, which is hashed into 0000000cea680366347c752dd41bb813e1b0f6e8aee8e4affc173ef[...] (shortened for brevity).

I ran the python script and got this:

kNTny7203745
0000000cea680366347c752dd41bb813e1b0f6e8aee8e4affc173ef88e1ffa71861f74e0f37019c605d3add02df1d8fa13b52c40fe68a037fae4aace1e656dd8
python blc_mining_test.py  9.92s user 0.00s system 99% cpu 9.952 total

Which is significantly faster than your 32 seconds.

For the Ruby script I got a large, but significantly faster result than you, yet again:

0000000cea680366347c752dd41bb813e1b0f6e8aee8e4affc173ef88e1ffa71861f74e0f37019c605d3add02df1d8fa13b52c40fe68a037fae4aace1e656dd8
ruby blc_mining_test.rb  30.59s user 0.00s system 99% cpu 30.680 total

I think we're going to need to average stuff out, since my 9.92s is waaaaay faster than your 32.628s.


Also, I wrote a crappy C++ implementation, but keep in mind I'm not a C++ developer by any margin, I rarely write anything in it, so to an experienced programmer, this will look like ass cakes:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>

const char *hash(char *s, int num)
{
    char s1[strlen(s)];
    sprintf(s1, "%s%d", s, num);
    unsigned char digest[SHA512_DIGEST_LENGTH];  
    SHA512((unsigned char*)s1, strlen(s1), (unsigned char*)&digest);    
    char *mdString = new char[SHA512_DIGEST_LENGTH*2+1];
    for(int i = 0; i < SHA512_DIGEST_LENGTH; i++)
    {
        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
    }
    return mdString;
}

int main()
{
    int difficulty = 7;
    int num = 0;
    char work[] = "kNTny";
    bool break_out = false;
    while (true) {
        const char *work_hash = hash(work, num);
        delete work_hash;
        num++;
        for (int n = 0; n < difficulty; n++) {
            if (work_hash[n] != '0') { // Magic "0"*7 start
                break_out = true;
                break;
            }
        }
        if (break_out) {
            break_out = false;
            continue;
        }
        std::cout << work << num << " " << work_hash << std::endl;
        break;
    }
    return 0;
}

To be fair, that looks like asscakes to me, but you know.
(Compiled with: g++ blc_miner.cpp -o blc_miner -lcrypto)

For that I got the output:

kNTny7203746 0000000cea680366347c752dd41bb813e1b0f6e8aee8e4affc173ef88e1ffa71861f74e0f37019c605d3add02df1d8fa13b52c40fe68a037fae4aace1e656dd8
./blc_miner  38.81s user 0.00s system 99% cpu 38.932 total

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment