func ValidateNonce(payload []byte) bool { if len(payload) < 2 { return false } var offset int var nonce, trials_test uint64 var hash_test, initial_payload []byte nonce, offset = varint.Decode(payload) initial_payload = payload[offset:] sha := sha512.New() sha.Write(initial_payload) initial_hash := sha.Sum(nil) var target uint64 = MAX_UINT64 / uint64((len(payload)+PAYLOAD_LENGTH_EXTRA_BYTES+8)*AVERAGE_PROOF_OF_WORK_NONCE_TRIALS_PER_BYTE) hash_test = varint.Encode(nonce) hash_test = append(hash_test, initial_hash...) sha1, sha2 := sha512.New(), sha512.New() sha1.Write(hash_test) sha2.Write(sha1.Sum(nil)) trials_test = binary.BigEndian.Uint64(sha2.Sum(nil)[:8]) return trials_test <= target }
func (a *addr) Serialize() ([]byte, error) { var buf bytes.Buffer buf.Write(varint.Encode(uint64(len(a.AddrList)))) for i := range a.AddrList { b, err := a.AddrList[i].Serialize() if err != nil { return nil, err } buf.Write(b) } return buf.Bytes(), nil }
func TestPOW(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } payload, err := ioutil.ReadFile("pow.go") if err != nil { t.Error("Unable to read from file\n") } nonce := Nonce(payload) new_payload := varint.Encode(nonce) new_payload = append(new_payload, payload...) if !ValidateNonce(new_payload) { t.Error("Nonce %d is not valid for payload", nonce) } }
func scan(offset_start, offset_end, target uint64, payload_hash []byte, out chan<- uint64, done chan<- bool, shutdown *bool) { var trials uint64 = MAX_UINT64 var nonce uint64 = offset_start h1, h2 := sha512.New(), sha512.New() for trials > target { nonce++ if nonce > offset_end || *shutdown { done <- true return } b := varint.Encode(nonce) b = append(b, payload_hash...) h1.Write(b) h2.Write(h1.Sum(nil)) trials = binary.BigEndian.Uint64(h2.Sum(nil)[:8]) h1.Reset() h2.Reset() } out <- nonce done <- true }
func New(addressVersion, stream uint64, eighteenByteRipe bool) (*address, error) { var err error addr := new(address) addr.SigningKey, err = bitecdsa.GenerateKey(bitelliptic.S256(), rand.Reader) if err != nil { return nil, errors.New("address.New: Error generating ecdsa signing keys") } var ripe []byte for { addr.EncryptionKey, err = bitecdsa.GenerateKey(bitelliptic.S256(), rand.Reader) if err != nil { return nil, errors.New("address.New: Error generating ecdsa encryption keys") } var keyMerge bytes.Buffer keyMerge.Write(addr.SigningKey.PublicKey.X.Bytes()) keyMerge.Write(addr.SigningKey.PublicKey.Y.Bytes()) keyMerge.Write(addr.EncryptionKey.PublicKey.X.Bytes()) keyMerge.Write(addr.EncryptionKey.PublicKey.Y.Bytes()) sha := sha512.New() sha.Write(keyMerge.Bytes()) ripemd := ripemd160.New() ripemd.Write(sha.Sum(nil)) ripe = ripemd.Sum(nil) if eighteenByteRipe { if ripe[0] == 0x00 && ripe[1] == 0x00 { ripe = ripe[2:] break } } else { if ripe[0] == 0x00 { ripe = ripe[1:] break } } } var bmAddr bytes.Buffer bmAddr.Write(varint.Encode(addressVersion)) bmAddr.Write(varint.Encode(stream)) bmAddr.Write(ripe) sha1, sha2 := sha512.New(), sha512.New() sha1.Write(bmAddr.Bytes()) sha2.Write(sha1.Sum(nil)) checksum := sha2.Sum(nil)[:4] bmAddr.Write(checksum) encoded, err := base58.Encode(bmAddr.Bytes()) if err != nil { return nil, err } addr.Identifier = "BM-" + encoded return addr, nil }