func (difficulty Difficulty) MarshalText() ([]byte, error) {
	bits := make([]byte, 8)
	binary.LittleEndian.PutUint64(bits, difficulty.bits)
	size := hex.EncodedLen(len(bits))
	buffer := make([]byte, size)
	hex.Encode(buffer, bits)
	return buffer, nil
}
MarshalText - convert a difficulty to little endian hex for JSON
package difficulty
import (
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"math"
	"math/big"
	"sync"
	"github.com/bitmark-inc/logger"
)
const (
	OneUint64                     uint64  = 0x00ffffffffffffff
	minimumReciprocal             float64 = 1.0
	ExpectedBlockSpacingInSecond          = 2 * 60
	AdjustTimespanInBlocks                = 200
	adjustTimespanInSecond                = ExpectedBlockSpacingInSecond * AdjustTimespanInBlocks
	nextDifficultyRatioUpperbound         = 4
	nextDifficultyRaioLowerbound          = 0.25
	firstBlock                            = 2
	minMutiplyOfTimespanPeriod            = 2
	defaultEmptyBits                      = 8
)
type Difficulty struct {
	m          *sync.RWMutex  
	big        big.Int        
	reciprocal float64        
	bits       uint64         
}
var Current = New()
var constOne = []byte{
	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
var one big.Int         
var floatOne big.Float  
func init() {
	one.SetBytes(constOne)
	floatOne.SetInt(&one)
	Current.SetBits(OneUint64)
}
func New() *Difficulty {
	d := new(Difficulty)
	return d.internalReset()
}
func (difficulty *Difficulty) Value() float64 {
	difficulty.m.RLock()
	defer difficulty.m.RUnlock()
	return difficulty.reciprocal
}
func (difficulty *Difficulty) Bits() uint64 {
	difficulty.m.RLock()
	defer difficulty.m.RUnlock()
	return difficulty.bits
}
func (difficulty *Difficulty) String() string {
	difficulty.m.RLock()
	defer difficulty.m.RUnlock()
	return fmt.Sprintf("%016x", difficulty.bits)
}
func (difficulty *Difficulty) GoString() string {
	return fmt.Sprintf("%064x", difficulty.BigInt())
}
func (difficulty *Difficulty) BigInt() *big.Int {
	difficulty.m.RLock()
	defer difficulty.m.RUnlock()
	d := new(big.Int)
	return d.Set(&difficulty.big)
}
func (difficulty *Difficulty) internalReset() *Difficulty {
	if nil == difficulty.m {
		difficulty.m = new(sync.RWMutex)
	}
	difficulty.big.Set(&one)
	difficulty.reciprocal = minimumReciprocal
	difficulty.bits = OneUint64
	return difficulty
}
func (difficulty *Difficulty) SetBits(u uint64) *Difficulty {
	if OneUint64 == u {
		difficulty.m.Lock()
		defer difficulty.m.Unlock()
		return difficulty.internalReset()
	}
	exponent := uint(u>>56) & 0xff
	mantissa := u&0x00ffffffffffffff | 0x0100000000000000  
	if exponent >= 0xc0 {
		logger.Criticalf("difficulty.SetBits(0x%16x) invalid value", u)
		logger.Panic("difficulty.SetBits: failed")
	}
	d := big.NewInt(0)
	d.SetUint64(mantissa)
	d.Lsh(d, 256-65-exponent)  
	denominator := new(big.Float)
	denominator.SetInt(d)
	q := new(big.Float)
	result, _ := q.Quo(&floatOne, denominator).Float64()
	difficulty.m.Lock()
	defer difficulty.m.Unlock()
	difficulty.big.Set(d)
	difficulty.reciprocal = result
	difficulty.bits = u
	return difficulty
}
func (difficulty *Difficulty) Set(f float64) {
	difficulty.m.Lock()
	defer difficulty.m.Unlock()
	difficulty.convertDifficultyIntoReciprocal(f)
}
func (difficulty *Difficulty) convertDifficultyIntoReciprocal(f float64) float64 {
	if f < minimumReciprocal {
		difficulty.internalReset()
		return difficulty.reciprocal
	}
	difficulty.reciprocal = f
	r := new(big.Float)
	r.SetMode(big.ToPositiveInf).SetPrec(256).SetFloat64(f).Quo(&floatOne, r)
	d, _ := r.Int(&difficulty.big)
	buffer := d.Bytes()  
	if len(buffer) > 32 {
		logger.Criticalf("difficulty.convertDifficultyIntoReciprocal(%g) invalid value", f)
		logger.Panic("difficulty.SetBits: failed - needs more than 256 bits")
	}
scan_buffer:
	for i, b := range buffer {
		if 0 != b {
			u := uint64(b) << 56
			if i+1 < len(buffer) {
				u |= uint64(buffer[i+1]) << 48
			}
			if i+2 < len(buffer) {
				u |= uint64(buffer[i+2]) << 40
			}
			if i+3 < len(buffer) {
				u |= uint64(buffer[i+3]) << 32
			}
			if i+4 < len(buffer) {
				u |= uint64(buffer[i+4]) << 24
			}
			if i+5 < len(buffer) {
				u |= uint64(buffer[i+5]) << 16
			}
			if i+6 < len(buffer) {
				u |= uint64(buffer[i+6]) << 8
			}
			if i+7 < len(buffer) {
				u |= uint64(buffer[i+7])
			}
			e := uint64(32-len(buffer)+i)*8 - 1
			rounder := 0
			for 0x0100000000000000 != 0xff00000000000000&u {
				if 1 == u&1 {
					rounder += 1
				}
				u >>= 1
				e -= 1
			}
			if rounder > 4 {
				u += 1
			}
			u = u&0x00ffffffffffffff | e<<56
			difficulty.bits = u
			break scan_buffer
		}
	}
	return difficulty.reciprocal
}
func (difficulty *Difficulty) SetBytes(b []byte) *Difficulty {
	const byteLength = 8
	if len(b) != byteLength {
		logger.Panicf("difficulty.SetBytes: too few bytes expected: %d had: %d", byteLength, len(b))
	}
	u := uint64(b[0]) |
		uint64(b[1])<<8 |
		uint64(b[2])<<16 |
		uint64(b[3])<<24 |
		uint64(b[4])<<32 |
		uint64(b[5])<<40 |
		uint64(b[6])<<48 |
		uint64(b[7])<<56
	return difficulty.SetBits(u)
}