Skip to content

kelby/cryptonight

 
 

Repository files navigation

cryptonight

GoDoc tag CircleCI Codecov Go Report Card License

Pure Go/ASM implementation of CryptoNight hash function and some of its variant, without any CGO binding.

Features

  • Support Monero v7 variant, and also the upcoming variant 2!

  • No CGO hell, making builds easier and faster.

  • Hardware acceleration available for amd64 architecture.

  • Use of an internal sync.Pool to manage caches, since it is memory hard.

Install

$ go get -u ekyu.moe/cryptonight

A simple CLI utility is also available with go get -u ekyu.moe/cryptonight/cmd/cnhash.

Usage of cnhash:
  -in-file string
        Read input from file instead of stdin.
  -in-hex
        Read input in hex instead of binary.
  -include-diff
        Append the difficulty of the result hash to the output. If -out-binary is not given,
the difficulty will be appeneded to the output in decimal with comma separated (CSV friendly),
otherwise it will be appeneded to the hash binary (which is 32 bytes long) directly, in 8 bytes
little endian.
  -out-binary
        Produce output in binary (little endian) instead of hex.
  -out-file string
        Produce output to file instead of stdout.
  -variant int
        Set CryptoNight variant, default 0.

Example

package main

import (
    "fmt"

    "ekyu.moe/cryptonight"
)

func main() {
    original := []byte("Hello, 世界")
    fmt.Printf("%x\n", cryptonight.Sum(original, 0)) // original
    // Output: 0999794e4e20d86e6a81b54495aeb370b6a9ae795fb5af4f778afaf07c0b2e0e

    variant_v1 := []byte("variant 1 requires at least 43 bytes of input.")
    fmt.Printf("%x\n", cryptonight.Sum(variant_v1, 1)) // variant 1
    // Output: 261124c5a6dca5d4aa3667d328a94ead9a819ae714e1f1dc113ceeb14f1ecf99

    variant_v2 := []byte("Monero is cash for a connected world. It’s fast, private, and secure.")
    fmt.Printf("%x\n", cryptonight.Sum(variant_v2, 2)) // variant 2
    // Output: c163d3839d69259d59b7f9b9a09dc043c3b11e34986c2a783091dbdcebed19f9
}

Tested architectures

  • amd64 (w/ AVX, SSE, AES)

  • amd64 (w/o AVX, SSE, AES)

  • 386

  • arm64

Benchmarks

CPU: 4 x Intel® Xeon® CPU E3-1270 v3 @ 3.50GHz

goos: linux
goarch: amd64
pkg: ekyu.moe/cryptonight
BenchmarkSum/v0-naive-4                 100  29107872 ns/op  2106570 B/op   6 allocs/op
BenchmarkSum/v1-naive-4                 100  28548981 ns/op  2106568 B/op   6 allocs/op
BenchmarkSum/v2-naive-4                 100  36806285 ns/op  2106568 B/op   6 allocs/op
BenchmarkSum/v0-cached-4                100  28430188 ns/op    21583 B/op   2 allocs/op
BenchmarkSum/v1-cached-4                100  28107946 ns/op    21583 B/op   2 allocs/op
BenchmarkSum/v2-cached-4                100  36070597 ns/op    21583 B/op   2 allocs/op
BenchmarkSum/v0-parallel-naive-4        100  10572830 ns/op  2106398 B/op   5 allocs/op
BenchmarkSum/v1-parallel-naive-4        100  10322516 ns/op  2106302 B/op   5 allocs/op
BenchmarkSum/v2-parallel-naive-4        100  11734641 ns/op  2106324 B/op   5 allocs/op
BenchmarkSum/v0-parallel-cached-4     10000   9560600 ns/op     1338 B/op   2 allocs/op
BenchmarkSum/v1-parallel-cached-4     10000   9667099 ns/op     1339 B/op   2 allocs/op
BenchmarkSum/v2-parallel-cached-4       100  11809401 ns/op    84837 B/op   2 allocs/op
BenchmarkFinalHash/BLAKE-256-4      1000000      1108 ns/op      208 B/op   3 allocs/op
BenchmarkFinalHash/Grøstl-256-4      200000      9552 ns/op      176 B/op   2 allocs/op
BenchmarkFinalHash/JH-256-4          200000      8281 ns/op      256 B/op   2 allocs/op
BenchmarkFinalHash/Skein-256-4      1000000      1636 ns/op      320 B/op   2 allocs/op
BenchmarkDifficulty-4               5000000       389 ns/op      192 B/op   3 allocs/op
BenchmarkCheckHash-4              100000000      15.8 ns/op       0 B/op    0 allocs/op
PASS

Development

While this repository is already out-of-box, which means what you need to do to use it in your code is just a go get (or dep ensure -add whatsoever), in case you want to hack on this library, some additional steps are required since it uses code generation.

Preprocess

If you modified a source file in this library that uses C-kind macros (the comments tells), in order to expand them and generate the final code, cpp(1) is needed, which a part of GCC toolchain and should be already available if you have installed gcc (or MinGW for Windows) in your machine.

Once some modification is made in a file that used macro, simply use go generate ekyu.moe/cryptonight/... to run the gcc preprocessor on them.

Packages information

ekyu.moe/cryptonight/internal/aes

From Go’s crypto/aes. Since CryptoNight’s use of AES is quite non-standard and not intended for encryption, you must use this package this package with care for project that’s not CryptoNight associated.

ekyu.moe/cryptonight/internal/sha3

From Go’s golang.org/x/crypto/sha3. All CryptoNight specific additional works are made in cn.go only; other files are untouched at all.

ekyu.moe/cryptonight/groestl

Grøstl-256 implementation. It is directly ported from C and not quite optimized.

ekyu.moe/cryptonight/jh

JH-256 implementation. It is directly ported from C and not quite optimized.

Tests, coverage and benchmarks

$ go test -v -race -coverprofile=coverage.txt -covermode=atomic
$ go tool cover -html=coverage.txt
$ go test -v -run=^$ -bench=. -benchmem

TODO

  • ❏ ARM64-specific optimization

  • ✓ Tests on other architectures

  • ✓ Improve performance for variant 2

  • ❏ Improve performance for groestl and jh

Donation

If you find this lib helpful, maybe consider buying me a cup of coffee at

XMR

4777777jHFbZB4gyqrB1JHDtrGFusyj4b3M2nScYDPKEM133ng2QDrK9ycqizXS2XofADw5do5rU19LQmpTGCfeQTerm1Ti

BTC

1Eqqqq9xR78wJyRXXgvR73HEfKdEwq68BT

Much thanks.

License

MIT

About

➿ Pure Go/ASM implementation of CryptoNight hash function with its variants, without any CGO binding.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Go 88.0%
  • Assembly 12.0%