├── bitmessage ├── alert.go ├── submitorder.go ├── hash.go ├── checkorder.go ├── reply.go ├── addr.go ├── networkaddress.go ├── inv.go ├── gets.go ├── version.go ├── headers.go ├── block.go ├── bitmessage.go └── tx.go ├── README ├── LICENSE ├── bitecdsa ├── LICENSE ├── bitecdsa.go └── bitecdsa_test.go ├── bitelliptic ├── LICENSE ├── bitelliptic_test.go └── bitelliptic.go └── mymath ├── hashing.go ├── base58.go ├── mymath.go └── bitmath.go /bitmessage/alert.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 ThePiachu. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package bitmessage 6 | 7 | 8 | //TODO: do 9 | //https://en.bitcoin.it/wiki/Protocol_specification#alert 10 | type Alert struct{ 11 | 12 | } -------------------------------------------------------------------------------- /bitmessage/submitorder.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 ThePiachu. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package bitmessage 6 | 7 | //TODO: do 8 | //https://en.bitcoin.it/wiki/Protocol_specification#submitorder 9 | type SubmitOrder struct{ 10 | hash [32]byte 11 | //wallet_entry CWalletTx 12 | } -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is a work-in-progress implementation of Bitcoin in Google Go. 2 | 3 | Package description: 4 | -mymath - package for various math operations and type conversions 5 | -bitelliptic - package for handling Koblitz ECDSA curves 6 | -bitecdsa - package for handling ECDSA algorithms that use the Koblitz curves 7 | -bitmessage - package for handling messages from the Bitcoin protocol 8 | 9 | Code progress: 10 | -Alpha version, some functionality still missing. -------------------------------------------------------------------------------- /bitmessage/hash.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 ThePiachu. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package bitmessage 6 | 7 | import( 8 | "mymath" 9 | "big" 10 | ) 11 | 12 | type Hash [32]byte 13 | 14 | func (h Hash)Len()int{ 15 | return 32 16 | } 17 | 18 | func NewHashFromBig(hash *big.Int) Hash{ 19 | var h Hash 20 | h=mymath.Big2Hex32(hash) 21 | return h 22 | } 23 | 24 | func NewHashFromString(hash string) Hash{ 25 | var h Hash 26 | h=mymath.String2Hex32(hash) 27 | return h 28 | } 29 | 30 | func NewHash(b []byte) Hash{ 31 | var h Hash 32 | if len(b)==32{ 33 | copy(h[:], b) 34 | } 35 | return h 36 | } 37 | -------------------------------------------------------------------------------- /bitmessage/checkorder.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 ThePiachu. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package bitmessage 6 | 7 | //TODO: do 8 | //TODO: figure out? 9 | //https://en.bitcoin.it/wiki/Protocol_specification#checkorder 10 | type CheckOrder struct{ 11 | /* 12 | checkorder 13 | This message is used for IP Transactions, to ask the peer if it accepts such transactions and allow it to look at the content of the order. 14 | It contains a CWalletTx object 15 | Payload: 16 | Field Size Description Data type Comments 17 | Fields from CMerkleTx 18 | ? hashBlock 19 | ? vMerkleBranch 20 | ? nIndex 21 | Fields from CWalletTx 22 | ? vtxPrev 23 | ? mapValue 24 | ? vOrderForm 25 | ? fTimeReceivedIsTxTime 26 | ? nTimeReceived 27 | ? fFromMe 28 | ? fSpent 29 | */ 30 | } -------------------------------------------------------------------------------- /bitmessage/reply.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 ThePiachu. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package bitmessage 6 | 7 | import( 8 | "mymath" 9 | ) 10 | 11 | const ( 12 | SUCCESS uint32 = iota //==0 //The IP Transaction can proceed (checkorder), or has been accepted (submitorder) 13 | WALLET_ERROR uint32 = iota //==1 //AcceptWalletTransaction() failed 14 | DENIED uint32 = iota //==2 //IP Transactions are not accepted by this node 15 | ) 16 | 17 | //TODO: test 18 | type Reply struct{ 19 | Reply [4]byte 20 | } 21 | 22 | func (r *Reply)SetReply(rep uint32){ 23 | answer:=mymath.Uint322HexRev(rep)//TODO: check if it is rev or not 24 | copy(r.Reply[:], answer[:]) 25 | } 26 | 27 | func (r *Reply)Compile()[]byte{ 28 | return r.Reply[:] 29 | } 30 | 31 | func (r *Reply)Len() int{ 32 | return len(r.Reply) 33 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 ThePiachu. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * The name of ThePiachu may not be used to endorse or promote products 14 | derived from this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /bitmessage/addr.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 ThePiachu. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package bitmessage 6 | 7 | 8 | import ( 9 | 10 | ) 11 | 12 | //checked, works as expected 13 | type AddressList struct{ 14 | Count uint8 15 | AddrList []*NetworkAddress 16 | } 17 | 18 | /*type Address struct{ 19 | lastseen uint32 20 | address NetworkAddress 21 | }*/ 22 | 23 | func (al *AddressList)AddAddress(na *NetworkAddress){ 24 | if al.Count<29{ //so payload length would be less than 1000 bytes 25 | al.Count++ 26 | al.AddrList=append(al.AddrList, na) 27 | } 28 | } 29 | 30 | func (al *AddressList)Clear(){ 31 | al.Count=0 32 | al.AddrList=nil 33 | } 34 | 35 | //TODO: double check if 30 is really the answer 36 | func (al *AddressList)Compile()[]byte{ 37 | answer:=make([]byte, 1+30*len(al.AddrList)) 38 | 39 | answer[0]=al.Count 40 | iterator:=1 41 | for i:=0;i 5 { 80 | break 81 | } 82 | } 83 | } 84 | 85 | //TODO: test more curves? 86 | func BenchmarkBaseMult(b *testing.B) { 87 | b.ResetTimer() 88 | s256 := S224() 89 | e := s256BaseMultTests[0]//TODO: check, used to be 25 instead of 0, but it's probably ok 90 | k, _ := new(big.Int).SetString(e.k, 16) 91 | b.StartTimer() 92 | for i := 0; i < b.N; i++ { 93 | s256.ScalarBaseMult(k.Bytes()) 94 | } 95 | } 96 | 97 | //TODO: test more curves? 98 | func TestMarshal(t *testing.T) { 99 | s256 := S256() 100 | _, x, y, err := s256.GenerateKey(rand.Reader) 101 | if err != nil { 102 | t.Error(err) 103 | return 104 | } 105 | serialised := s256.Marshal(x, y) 106 | xx, yy := s256.Unmarshal(serialised) 107 | if xx == nil { 108 | t.Error("failed to unmarshal") 109 | return 110 | } 111 | if xx.Cmp(x) != 0 || yy.Cmp(y) != 0 { 112 | t.Error("unmarshal returned different values") 113 | return 114 | } 115 | } -------------------------------------------------------------------------------- /bitecdsa/bitecdsa.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 The Go Authors. All rights reserved. 2 | // Copyright 2011 ThePiachu. All rights reserved. 3 | // Use of this source code is governed by a BSD-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // Package ecdsa implements the Elliptic Curve Digital Signature Algorithm, as 7 | // defined in FIPS 186-3. 8 | package bitecdsa 9 | 10 | // References: 11 | // [NSA]: Suite B implementor's guide to FIPS 186-3, 12 | // http://www.nsa.gov/ia/_files/ecdsa.pdf 13 | // [SECG]: SECG, SEC1 14 | // http://www.secg.org/download/aid-780/sec1-v2.pdf 15 | 16 | import ( 17 | "big" 18 | "bitelliptic" 19 | "io" 20 | "os" 21 | ) 22 | 23 | // PublicKey represents an ECDSA public key. 24 | type PublicKey struct { 25 | *bitelliptic.BitCurve 26 | X, Y *big.Int 27 | } 28 | 29 | // PrivateKey represents a ECDSA private key. 30 | type PrivateKey struct { 31 | PublicKey 32 | D *big.Int 33 | } 34 | 35 | var one = new(big.Int).SetInt64(1) 36 | 37 | // randFieldElement returns a random element of the field underlying the given 38 | // curve using the procedure given in [NSA] A.2.1. 39 | func randFieldElement(c *bitelliptic.BitCurve, rand io.Reader) (k *big.Int, err os.Error) { 40 | b := make([]byte, c.BitSize/8+8) 41 | _, err = io.ReadFull(rand, b) 42 | if err != nil { 43 | return 44 | } 45 | 46 | k = new(big.Int).SetBytes(b) 47 | n := new(big.Int).Sub(c.N, one) 48 | k.Mod(k, n) 49 | k.Add(k, one) 50 | return 51 | } 52 | 53 | // GenerateKey generates a public&private key pair. 54 | func GenerateKey(c *bitelliptic.BitCurve, rand io.Reader) (priv *PrivateKey, err os.Error) { 55 | k, err := randFieldElement(c, rand) 56 | if err != nil { 57 | return 58 | } 59 | 60 | priv = new(PrivateKey) 61 | priv.PublicKey.BitCurve = c 62 | priv.D = k 63 | priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes()) 64 | return 65 | } 66 | 67 | // hashToInt converts a hash value to an integer. There is some disagreement 68 | // about how this is done. [NSA] suggests that this is done in the obvious 69 | // manner, but [SECG] truncates the hash to the bit-length of the curve order 70 | // first. We follow [SECG] because that's what OpenSSL does. 71 | func hashToInt(hash []byte, c *bitelliptic.BitCurve) *big.Int { 72 | orderBits := c.N.BitLen() 73 | orderBytes := (orderBits + 7) / 8 74 | if len(hash) > orderBytes { 75 | hash = hash[:orderBytes] 76 | } 77 | 78 | ret := new(big.Int).SetBytes(hash) 79 | excess := orderBytes*8 - orderBits 80 | if excess > 0 { 81 | ret.Rsh(ret, uint(excess)) 82 | } 83 | return ret 84 | } 85 | 86 | // Sign signs an arbitrary length hash (which should be the result of hashing a 87 | // larger message) using the private key, priv. It returns the signature as a 88 | // pair of integers. The security of the private key depends on the entropy of 89 | // rand. 90 | func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err os.Error) { 91 | // See [NSA] 3.4.1 92 | c := priv.PublicKey.BitCurve 93 | 94 | var k, kInv *big.Int 95 | for { 96 | for { 97 | k, err = randFieldElement(c, rand) 98 | if err != nil { 99 | r = nil 100 | return 101 | } 102 | 103 | kInv = new(big.Int).ModInverse(k, c.N) 104 | r, _ = priv.BitCurve.ScalarBaseMult(k.Bytes()) 105 | r.Mod(r, priv.BitCurve.N) 106 | if r.Sign() != 0 { 107 | break 108 | } 109 | } 110 | 111 | e := hashToInt(hash, c) 112 | s = new(big.Int).Mul(priv.D, r) 113 | s.Add(s, e) 114 | s.Mul(s, kInv) 115 | s.Mod(s, priv.PublicKey.BitCurve.N) 116 | if s.Sign() != 0 { 117 | break 118 | } 119 | } 120 | 121 | return 122 | } 123 | 124 | // Verify verifies the signature in r, s of hash using the public key, pub. It 125 | // returns true iff the signature is valid. 126 | func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool { 127 | // See [NSA] 3.4.2 128 | c := pub.BitCurve 129 | 130 | if r.Sign() == 0 || s.Sign() == 0 { 131 | return false 132 | } 133 | if r.Cmp(c.N) >= 0 || s.Cmp(c.N) >= 0 { 134 | return false 135 | } 136 | e := hashToInt(hash, c) 137 | w := new(big.Int).ModInverse(s, c.N) 138 | 139 | u1 := e.Mul(e, w) 140 | u2 := w.Mul(r, w) 141 | 142 | x1, y1 := c.ScalarBaseMult(u1.Bytes()) 143 | x2, y2 := c.ScalarMult(pub.X, pub.Y, u2.Bytes()) 144 | if x1.Cmp(x2) == 0 { 145 | return false 146 | } 147 | x, _ := c.Add(x1, y1, x2, y2) 148 | x.Mod(x, c.N) 149 | return x.Cmp(r) == 0 150 | } 151 | -------------------------------------------------------------------------------- /mymath/hashing.go: -------------------------------------------------------------------------------- 1 | package mymath 2 | //Subpackage used for all variations of hashing algorithms 3 | 4 | 5 | import( 6 | "hash" 7 | "crypto/sha256" 8 | "crypto/sha1" 9 | "crypto/ripemd160" 10 | ) 11 | 12 | //TODO: test and add to tests 13 | //double SHA-256 hashing of a single byte array 14 | func DoubleSHA(b []byte)([]byte){ 15 | var h hash.Hash = sha256.New() 16 | h.Write(b) 17 | var h2 hash.Hash = sha256.New() 18 | h2.Write(h.Sum()) 19 | 20 | return h2.Sum() 21 | } 22 | 23 | //TODO: test and add to tests 24 | //reverse double SHA-256 hashing of a single byte array 25 | func DoubleSHARev(b []byte)([]byte){ 26 | return Rev(DoubleSHA(Rev(b))) 27 | } 28 | 29 | //TODO: test and add to tests 30 | //Single SHA-256 hashing of a single byte array 31 | func SingleSHA(b []byte)([]byte){ 32 | var h hash.Hash = sha256.New() 33 | h.Write(b) 34 | 35 | return h.Sum() 36 | } 37 | 38 | //TODO: test and add to tests 39 | //Reversed single SHA-256 hashing of a single byte array 40 | func SingleSHARev(b []byte)([]byte){ 41 | return Rev(SingleSHA(Rev(b))) 42 | } 43 | 44 | //TODO: test and add to tests 45 | //Single SHA-1 hashing of a single byte array 46 | func SingleSHA1(b []byte)([]byte){ 47 | var h hash.Hash = sha1.New()//TODO: double check 48 | h.Write(b) 49 | 50 | return h.Sum() 51 | } 52 | 53 | //Reversed SHA-1 hashing of a single byte array 54 | //TODO: test and add to tests 55 | func SingleSHA1Rev(b []byte)([]byte){ 56 | return Rev(SingleSHA1(Rev(b)))//TODO: double check 57 | } 58 | 59 | //TODO: test and add to tests 60 | //double hash input bytes, double hash their concatanation 61 | func DoubleDoubleSHA(a []byte, b []byte)([]byte){ 62 | var tmp []byte 63 | 64 | //hash first input 65 | var h1 hash.Hash = sha256.New() 66 | h1.Write(a) 67 | 68 | //hash the first input the second time 69 | tmp=h1.Sum() 70 | h1=sha256.New() 71 | h1.Write(tmp) 72 | 73 | //hash the second input 74 | var h2 hash.Hash = sha256.New() 75 | h2.Write(b) 76 | 77 | //hash the second input the second time 78 | tmp=h2.Sum() 79 | h2=sha256.New() 80 | h2.Write(tmp) 81 | 82 | //hash the concatenation of the double hashes of both inputs 83 | var answer hash.Hash=sha256.New() 84 | answer.Write(append(h1.Sum(), h2.Sum()...)) 85 | 86 | //double hash the concatenation 87 | tmp=answer.Sum() 88 | answer=sha256.New() 89 | answer.Write(tmp) 90 | 91 | return answer.Sum()//return result 92 | } 93 | 94 | //TODO: test and add to tests 95 | //double SHA-256 hash of a concatenation of the two inputs 96 | func DoubleSHAPair(a []byte, b []byte)([]byte){ 97 | 98 | var tmp []byte 99 | 100 | //hash the concatenation of the two inputs 101 | var answer hash.Hash=sha256.New() 102 | answer.Write(append(a, b...)) 103 | 104 | //hash it the second time 105 | tmp=answer.Sum() 106 | answer=sha256.New() 107 | answer.Write(tmp) 108 | 109 | return answer.Sum()//return 110 | } 111 | 112 | //TODO: test and add to tests 113 | //double SHA-256 hash of a concatenation of the reverse of two inputs 114 | func DoubleSHAPairRev(a []byte, b []byte)([]byte){ 115 | var tmp []byte 116 | 117 | //hash the concatenation of the reverse of both inputs 118 | var answer hash.Hash=sha256.New() 119 | //answer.Write(append(a[:]+b[:])) 120 | answer.Write(append(Rev(a), Rev(b)...)) 121 | 122 | //hash it again 123 | tmp=answer.Sum() 124 | answer=sha256.New() 125 | answer.Write(tmp) 126 | 127 | return Rev(answer.Sum())//return the reverse of the output 128 | } 129 | 130 | //TODO: test and add to tests 131 | //SHA-256 RIPEMD-160 operation for bitcoin address hashing 132 | func SHARipemd(b []byte)([]byte){ 133 | 134 | //sha hashing of the input 135 | var h hash.Hash = sha256.New() 136 | h.Write(b) 137 | 138 | //ripemd hashing of the sha hash 139 | var h2 hash.Hash = ripemd160.New() 140 | h2.Write(h.Sum()) 141 | 142 | return h2.Sum()//return 143 | } 144 | 145 | //TODO: test and add to tests 146 | //reverse SHA-256 RIPEMD-160 hash 147 | func SHARipemdRev(b []byte)([]byte){ 148 | return Rev(SHARipemd(Rev(b))) 149 | } 150 | 151 | //TODO: test and add to tests 152 | //RIPEMD-160 operation for bitcoin address hashing 153 | func Ripemd(b []byte)([]byte){ 154 | //ripemd hashing of the sha hash 155 | var h hash.Hash = ripemd160.New() 156 | h.Write(b) 157 | 158 | return h.Sum()//return 159 | } 160 | 161 | //TODO: test and add to tests 162 | //reverse RIPEMD-160 hash 163 | func RipemdRev(b []byte)([]byte){ 164 | return Rev(Ripemd(Rev(b))) 165 | } -------------------------------------------------------------------------------- /bitmessage/headers.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 ThePiachu. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package bitmessage 6 | 7 | import ( 8 | "mymath" 9 | ) 10 | 11 | //TODO:test 12 | type Headers struct{ 13 | Count mymath.VarInt 14 | Headers []*BlockHeader//block_header[] 15 | } 16 | 17 | //TODO:check if there is any limit on Count 18 | func (hs *Headers)AddHeader(bh *BlockHeader){ 19 | hs.Count++ 20 | hs.Headers=append(hs.Headers, bh) 21 | } 22 | 23 | func (hs *Headers)Clear(){ 24 | hs.Count=0 25 | hs.Headers=nil 26 | } 27 | 28 | func (hs *Headers)Compile()[]byte{ 29 | 30 | vi:=mymath.VarInt2HexRev(hs.Count)//TODO: check if Rev or not 31 | 32 | answer:=make([]byte, len(vi)+81*len(hs.Headers))//TODO: double check if it is 81 33 | 34 | iterator:=0 35 | copy(answer[iterator:], vi) 36 | iterator+=len(vi) 37 | for i:=0;i25{//if it is longer than 25, return nothing 91 | return nil 92 | } 93 | answer:=make([]byte, 25)//make 25 byte container 94 | for i:=0;i0{//converts the number into base58 112 | tmp.Mod(valCopy, big.NewInt(58))//takes modulo 58 value 113 | valCopy.Div(valCopy, big.NewInt(58))//divides the rest by 58 114 | tmpStr+=alphabet[tmp.Int64():tmp.Int64()+1]//encodes 115 | } 116 | for i:=(len(tmpStr)-1); i>-1; i--{ 117 | answer+=tmpStr[i:i+1]//reverses the order 118 | } 119 | return Base58(answer)//returns 120 | } 121 | 122 | //encodes int to base58 string 123 | func Int2Base58 (val int) Base58{ 124 | answer :="" 125 | 126 | if val<=0{//if it is less than 0, returns empty string 127 | return Base58("") 128 | } 129 | valCopy:=val 130 | 131 | tmpStr:="" 132 | tmp:=0 133 | for valCopy>0{//converts the number into base58 134 | tmp=valCopy%58//takes modulo 58 value 135 | valCopy/=58//divides the rest by 58 136 | 137 | tmpStr+=alphabet[tmp:tmp+1]//encodes 138 | } 139 | for i:=(len(tmpStr)-1); i>-1; i--{ 140 | answer+=tmpStr[i:i+1]//reverses the order 141 | } 142 | 143 | return Base58(answer)//returns 144 | } 145 | 146 | //encodes hex bytes into base58 147 | func Hex2Base58(val []byte) Base58{ 148 | tmp:= Big2Base58(Hex2Big(val))//encoding of the number without zeroes in front 149 | 150 | //looking for zeros at the beggining 151 | i:=0 152 | for i=0;val[i]==0 && i11 { 88 | return 89 | } 90 | 91 | for i:=0;i=20 ;{ 243 | msgtype:=MessageType(msgs[iterator+4:iterator+16]) 244 | 245 | Payloadbyte:=msgs[iterator+16:iterator+20] 246 | Payload:=int(mymath.HexRev2Big(Payloadbyte).Int64()) 247 | 248 | if msgtype==VERSION || msgtype==VERACK { 249 | vec=append(vec, DecodeMessage(msgs[iterator:iterator+20+Payload])) 250 | iterator=iterator+20+Payload 251 | }else{ 252 | vec=append(vec, DecodeMessage(msgs[iterator:iterator+24+Payload])) 253 | iterator=iterator+24+Payload 254 | } 255 | } 256 | /*answer:=make([]BitMessage, vec.Len()) 257 | 258 | for i:=0;i> 3 229 | priv = make([]byte, byteLen) 230 | 231 | for x == nil { 232 | _, err = io.ReadFull(rand, priv) 233 | if err != nil { 234 | return 235 | } 236 | // We have to mask off any excess bits in the case that the size of the 237 | // underlying field is not a whole number of bytes. 238 | priv[0] &= mask[BitCurve.BitSize%8] 239 | // This is because, in tests, rand will return all zeros and we don't 240 | // want to get the point at infinity and loop forever. 241 | priv[1] ^= 0x42 242 | x, y = BitCurve.ScalarBaseMult(priv) 243 | } 244 | return 245 | } 246 | 247 | // Marshal converts a point into the form specified in section 4.3.6 of ANSI 248 | // X9.62. 249 | func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte { 250 | byteLen := (BitCurve.BitSize + 7) >> 3 251 | 252 | ret := make([]byte, 1+2*byteLen) 253 | ret[0] = 4 // uncompressed point 254 | 255 | xBytes := x.Bytes() 256 | copy(ret[1+byteLen-len(xBytes):], xBytes) 257 | yBytes := y.Bytes() 258 | copy(ret[1+2*byteLen-len(yBytes):], yBytes) 259 | return ret 260 | } 261 | 262 | // Unmarshal converts a point, serialised by Marshal, into an x, y pair. On 263 | // error, x = nil. 264 | func (BitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) { 265 | byteLen := (BitCurve.BitSize + 7) >> 3 266 | if len(data) != 1+2*byteLen { 267 | return 268 | } 269 | if data[0] != 4 { // uncompressed form 270 | return 271 | } 272 | x = new(big.Int).SetBytes(data[1 : 1+byteLen]) 273 | y = new(big.Int).SetBytes(data[1+byteLen:]) 274 | return 275 | } 276 | 277 | //curve parameters taken from: 278 | //http://www.secg.org/collateral/sec2_final.pdf 279 | 280 | var initonce sync.Once 281 | var secp160k1 *BitCurve 282 | var secp192k1 *BitCurve 283 | var secp224k1 *BitCurve 284 | var secp256k1 *BitCurve 285 | 286 | func initAll() { 287 | initS160() 288 | initS192() 289 | initS224() 290 | initS256() 291 | } 292 | 293 | func initS160() { 294 | // See SEC 2 section 2.4.1 295 | secp160k1 = new(BitCurve) 296 | secp160k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", 16) 297 | secp160k1.N, _ = new(big.Int).SetString("0100000000000000000001B8FA16DFAB9ACA16B6B3", 16) 298 | secp160k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000007", 16) 299 | secp160k1.Gx, _ = new(big.Int).SetString("3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", 16) 300 | secp160k1.Gy, _ = new(big.Int).SetString("938CF935318FDCED6BC28286531733C3F03C4FEE", 16) 301 | secp160k1.BitSize = 160 302 | } 303 | 304 | func initS192() { 305 | // See SEC 2 section 2.5.1 306 | secp192k1 = new(BitCurve) 307 | secp192k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", 16) 308 | secp192k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", 16) 309 | secp192k1.B, _ = new(big.Int).SetString("000000000000000000000000000000000000000000000003", 16) 310 | secp192k1.Gx, _ = new(big.Int).SetString("DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", 16) 311 | secp192k1.Gy, _ = new(big.Int).SetString("9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", 16) 312 | secp192k1.BitSize = 192 313 | } 314 | 315 | func initS224() { 316 | // See SEC 2 section 2.6.1 317 | secp224k1 = new(BitCurve) 318 | secp224k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D", 16) 319 | secp224k1.N, _ = new(big.Int).SetString("010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7", 16) 320 | secp224k1.B, _ = new(big.Int).SetString("00000000000000000000000000000000000000000000000000000005", 16) 321 | secp224k1.Gx, _ = new(big.Int).SetString("A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C", 16) 322 | secp224k1.Gy, _ = new(big.Int).SetString("7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5", 16) 323 | secp224k1.BitSize = 224 324 | } 325 | 326 | func initS256() { 327 | // See SEC 2 section 2.7.1 328 | secp256k1 = new(BitCurve) 329 | secp256k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16) 330 | secp256k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) 331 | secp256k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16) 332 | secp256k1.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16) 333 | secp256k1.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) 334 | secp256k1.BitSize = 256 335 | } 336 | 337 | // S160 returns a BitCurve which implements secp160k1 (see SEC 2 section 2.4.1) 338 | func S160() *BitCurve { 339 | initonce.Do(initAll) 340 | return secp160k1 341 | } 342 | 343 | // S192 returns a BitCurve which implements secp192k1 (see SEC 2 section 2.5.1) 344 | func S192() *BitCurve { 345 | initonce.Do(initAll) 346 | return secp192k1 347 | } 348 | 349 | // S224 returns a BitCurve which implements secp224k1 (see SEC 2 section 2.6.1) 350 | func S224() *BitCurve { 351 | initonce.Do(initAll) 352 | return secp224k1 353 | } 354 | 355 | // S256 returns a BitCurve which implements secp256k1 (see SEC 2 section 2.7.1) 356 | func S256() *BitCurve { 357 | initonce.Do(initAll) 358 | return secp256k1 359 | } -------------------------------------------------------------------------------- /mymath/mymath.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 ThePiachu. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package mymath 6 | //Package for handling common math and conversion operations used in the rest of the program. 7 | 8 | 9 | import( 10 | "big" 11 | "encoding/hex" 12 | "crypto/rand" 13 | "bytes" 14 | 15 | "log" 16 | "strings" 17 | "math" 18 | ) 19 | 20 | func Hex2Uint64(b []byte) uint64{ 21 | var answer uint64 22 | answer=0 23 | if len(b)>0{ 24 | maxBytes:=8 25 | if len(b)<8{ 26 | maxBytes=len(b) 27 | } 28 | for i:=0;i0{ 40 | maxBytes:=4 41 | if len(b)<4{ 42 | maxBytes=len(b) 43 | } 44 | for i:=0;i0x7F{ 272 | answer=append([]byte{0x01}, answer[:]...) 273 | }else{ 274 | answer[0]+=0x80 275 | } 276 | } 277 | return answer 278 | } 279 | 280 | func Randuint64() []byte{ 281 | uint64max:=big.NewInt(1) 282 | uint64max.Lsh(uint64max, 64) 283 | 284 | randnum, _:=rand.Int(rand.Reader, uint64max) 285 | 286 | random:=randnum.Bytes() 287 | answer:=make([]byte, 8) 288 | 289 | for i:=0;i0{ 396 | maxBytes:=8 397 | if len(b)<8{ 398 | maxBytes=len(b) 399 | } 400 | for i:=0;i0{ 412 | maxBytes:=4 413 | if len(b)<4{ 414 | maxBytes=len(b) 415 | } 416 | for i:=0;i1;{ 198 | //currentlevel:=level[:int(math.Ceil(float64(len(level)/2.0)))] 199 | //currentlevel:=make([][]byte, int(math.Ceil(float64(1.0*len(level)/2.0)))) 200 | currentlevel:=make([][]byte, len(level)/2+len(level)%2) 201 | log.Printf("len - %d", len(currentlevel)) 202 | for i:=0;i