Skip to content

Instantly share code, notes, and snippets.

@alecnunn
Forked from anonymous/1-intro.md
Created April 29, 2013 05:24
Show Gist options
  • Select an option

  • Save alecnunn/5479847 to your computer and use it in GitHub Desktop.

Select an option

Save alecnunn/5479847 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment