diff --git a/curl.go b/curl.go index 296eee043..821ccea13 100644 --- a/curl.go +++ b/curl.go @@ -107,10 +107,3 @@ func (c *Curl) Reset() { c.state[i] = 0 } } - -//Hash returns hash of t. -func (t Trytes) Hash() Trytes { - c := NewCurl() - c.Absorb(t) - return c.Squeeze() -} diff --git a/curl_test.go b/curl_test.go index 85c0a9494..6240021bf 100644 --- a/curl_test.go +++ b/curl_test.go @@ -29,7 +29,7 @@ import "testing" func TestCurl(t *testing.T) { trytes := Trytes("QQ9VLGQTJICJH9UZNSBXRSKBWVXCTRNGLJPEYFSZBISDCGBHGAV9TEHIMJS9LMQNHFNWVAXALKGESPWZMUMZPQCKCUFSEJZVHWZHZOEZBPTERXPHUQLQLZMENKOKAWMJ9LCFSIBSBEYCJQVQQMTMRJMDEKRXLCIRZFWQBRJYLPSK9XLWFYFTU9FBJATWPQRJBHWYJRUEXSXMBJLVWNYRTZJTHLEKDTWCGJ9OXDEZNWTKLTXXTKVFXDMRJUDAMDACRHJKZIJFJBZRVDLSTIMOWPTLLIVHCFUBMSQIPVPCSQAPZGHHNNQLWEHDTIQZQTAXJMTTROYOTIZSZKKQFXBHXFKSNAGZWHWEGXCKHSJQGBJGC9IKUSSJEAOHTPGPMIYPEXJXJKRLX9IOCUDKK9ONEUONYVGHRHXSAUZJXGVQNWSDZIYXXZMMJKRTOXSWJHBIMXPEMJTKIBSQKPICTKAQLJVOLZSGNVVBIOFJJKKJ9UBYKJWCBRPQLGBNEKEFRYCHB9PORAKEKTJZYZBFGZLONUJCPNINGUTYWTMDKTEPDASCHNVHXTUIOZ9PDALXKQYLILMUJEJWTYYAGEQXNNPLOMFGHSTFCNRMDFUSQREHFASDXZZOYNWNVKCRFZIDF9X9YKME9O9NJH9LFVNNSOXHSQOMBULFHBBPSGRCCGKWENQOZHRSIIHKKXAQTFISNVGIVMBDKSJYDW9VTZBAVYWWJAIYSCHGBIXKMKHBRTHWQMIWQVPWFUJQVUDZRDX9MMCXOQTQZGKECGIHATHUC9TD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XDTIPKOJDLYOKRCEKCBMKVYIAKVA9WTGDWHIRUAWOVRKOSYTNIZAZNTJRFJDMNLGHTDKPKZDLBPQXRIRIVREMOBCPHMBBAUKNXHU9XIZNG9GD9LDIBBFPSI9PJNRCHXHNWAZXIACE9LUBNUWOK9LGJ9MKZQRI9CBCJUNALQKKVGGSPRJFAGCXNFO99YMLMKI9NVUZCZ9BCUEBSGMAVNKGWYWWQPZISMKAROXLQWEHOJIJOIIYRUDBNHRD9DEDQWQONAXKKSYMYCFTITZFKIXKZCGAVAFQIYEMESOIMWUUDSXJRR9RVWTAAHCOA9SCQBF9LAGPPYDXPEBKLHZ9KHKTXFP9XOVMVWIXEWMOISJHMQEXMYMZCUGEQNKGUNVRPUDPRX9IR9LBASIARWNFXXESPITSLYAQMLCLVTLHW9999999999999999999999999999999999999999999999999999FBIEUWD99A99999999C99999999DEXRPLKGBROUQMKCLMRPG9HFKCACDZ9AB9HOJQWERTYWERJNOYLW9PKLOGDUPC9DLGSUH9UHSKJOASJRU9MMRRSLICRITOROFC9FBVWLFEDNN9KJKYHUMRCJEUDGCYCWTBP9HHBEEJRFAU9FALRJWTU99NZK999999UE9VSBDVSRNTBZWPXYZPGAUTSWFLARLPXMHYBSTEUWIDOFJQJMVIACGUPTOMBWQO9AEADCFCMFJ999999WQKHJEXIHMOKQETOUTEO9JUPCDNAJQYZVXQRCXGYGEBOTMHE9HSJXVYVQUS9FPDLQWWKSYVDPCXX9LLAT") hh := Trytes("MRYSIXABICSX9XQSLPAPQHGAPCMBDQZXH9EOHPLL9LFQNUDTETNQFUJO9DPHTNPJI9BTQH9RM9I999999") - h := trytes.Hash() + h := trytes.CurlHash() if hh != h { t.Error("hash is illegal.") } diff --git a/pow_c_test.go b/pow_c_test.go index 0d7247c71..ef35e02bc 100644 --- a/pow_c_test.go +++ b/pow_c_test.go @@ -36,7 +36,7 @@ func testPowC(t *testing.T) { t.Fatal(err) } tx = tx[:len(tx)-NonceTrinarySize/3] + nonce - h := tx.Hash() + h := tx.CurlHash() if h[len(h)-5:] != "99999" { t.Error("pow is illegal", h) } diff --git a/pow_go_test.go b/pow_go_test.go index e62c736d8..2e99b4509 100644 --- a/pow_go_test.go +++ b/pow_go_test.go @@ -36,7 +36,7 @@ func testPowGo(t *testing.T) { t.Fatal(err) } tx = tx[:len(tx)-NonceTrinarySize/3] + nonce - h := tx.Hash() + h := tx.CurlHash() if h[len(h)-5:] != "99999" { t.Error("pow is illegal", h) } diff --git a/sign.go b/sign.go index a8c2f455b..6d68fdbdd 100644 --- a/sign.go +++ b/sign.go @@ -28,6 +28,7 @@ package giota import ( "crypto/rand" "errors" + "math" "math/big" ) @@ -81,14 +82,14 @@ func NewKey(seed Trytes, index, securityLevel int) Trytes { seed = seedTrits.Trytes() hash := seed.Hash() - c := NewCurl() - c.Absorb(hash) + k := NewKerl() + k.Absorb(hash.Trits()) key := make([]byte, (HashSize*27*securityLevel)/3) for l := 0; l < securityLevel; l++ { for i := 0; i < 27; i++ { - b := c.Squeeze() - copy(key[(l*27+i)*HashSize/3:], b) + b, _ := k.Squeeze(HashSize) + copy(key[(l*27+i)*HashSize/3:], []byte(b.Trytes())) } } @@ -152,26 +153,39 @@ func seri27(l *[stateSize]uint64, h *[stateSize]uint64) Trytes { return keyFragment.Trytes() } -//Digests calculates hash x 26 for each segments in keyTrits. func Digests(key Trytes) (Trytes, error) { - if len(key) < HashSize*27/3 { - return "", ErrKeyTritsLength - } + //var digests Trits + var buffer Trits + k := key.Trits() + digests := make(Trits, int(math.Floor(float64(len(k))/float64(SignatureSize)))*HashSize) + for i := 0; i < int(math.Floor(float64(len(k))/float64(SignatureSize))); i++ { + keyFragment := k[i*SignatureSize : (i+1)*SignatureSize] + for j := 0; j < 27; j++ { + buffer = keyFragment[j*HashSize : (j+1)*HashSize] + for k := 0; k < MaxTryteValue-MinTryteValue; k++ { + kKerl := NewKerl() + kKerl.Reset() + kKerl.Absorb(buffer) + buffer, _ = kKerl.Squeeze(HashSize) + } + for k := 0; k < HashSize; k++ { + t := keyFragment + t[j*HashSize+k] = buffer[k] + keyFragment = t + } + } - // Integer division, becaue we don't care about impartial keys. - numKeys := len(key) / (HashSize * 9) - digests := make([]byte, HashSize*numKeys/3) - for i := 0; i < numKeys; i++ { - lmid, hmid := para27(key[i*HashSize*9:]) - for k := 0; k < 26; k++ { - transform64(lmid, hmid) - clearState(lmid, hmid) + var kerl = NewKerl() + kerl.Reset() + kerl.Absorb(keyFragment) + buffer, _ = kerl.Squeeze(HashSize) + for j := 0; j < HashSize; j++ { + t := digests + t[i*HashSize+j] = buffer[j] + digests = t } - keyFragment := seri27(lmid, hmid) - s := keyFragment.Hash() - copy(digests[i*HashSize/3:], s) } - return Trytes(digests), nil + return digests.Trytes(), nil } //digest calculates hash x normalizedBundleFragment[i] for each segments in keyTrits. @@ -293,7 +307,18 @@ func (a Address) Checksum() Trytes { if len(a) != 81 { panic("len(address) must be 81") } - return Trytes(a).Hash()[:9] + + addressTrits := Trytes(a).Trits() + + kerl := NewKerl() + kerl.Reset() + kerl.Absorb(addressTrits) + checksumTrits, _ := kerl.Squeeze(HashSize) + + checksumTrytes := checksumTrits.Trytes() + checksum := checksumTrytes[len(checksumTrytes)-9:] + + return checksum } //WithChecksum returns Address+checksum. diff --git a/sign_test.go b/sign_test.go index 0e7c0f8ed..9e066847b 100644 --- a/sign_test.go +++ b/sign_test.go @@ -58,7 +58,7 @@ var addressTCs = []addressTestCase{ addressTestCase{ addr: "RGVOWCDJAGSO9TNLBBPUVYE9KHBOAZNVFRVKVYYCHRKQRKRNKGGWBF9WCRJVROKLVKWZUMBABVJGAALWU", validAddr: true, - checksum: "QNXFPRSPG", + checksum: "NPJ9QIHFW", validChecksum: true, }, addressTestCase{ @@ -68,9 +68,9 @@ var addressTCs = []addressTestCase{ validChecksum: true, }, addressTestCase{ - addr: "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", + addr: "999999999999999999999999999999999999999999999999999999999999999999999999999999999", validAddr: true, - checksum: "999999999", + checksum: "A9BEONKZW", validChecksum: true, }, addressTestCase{ @@ -109,8 +109,8 @@ func TestNewAddressFromTrytes(t *testing.T) { func TestAddress(t *testing.T) { var seed Trytes = "WQNZOHUT99PWKEBFSKQSYNC9XHT9GEBMOSJAQDQAXPEZPJNDIUB9TSNWVMHKWICW9WVZXSMDFGISOD9FZ" - var adr0 Trytes = "KTXFP9XOVMVWIXEWMOISJHMQEXMYMZCUGEQNKGUNVRPUDPRX9IR9LBASIARWNFXXESPITSLYAQMLCLVTL9QTIWOWTY" - var adr1 Trytes = "PQTDJXXKSNYZGRJDXEHHMNCLUVOIRZC9VXYLSITYMVCQDQERAHAUZJKRNBQEUHOLEAXRUSQBNYVJWESYRPLAKVDWST" + var adr0 Trytes = "AYYNHWWNZQOFYXNQSLVULU9ARZCSXNWWAFYEWEL9LIXYDFS9KDSRZF9ZID9AQWSLAEUAJSTQKGPGXNWCDDSSQMTJOA" + var adr1 Trytes = "9CTFIAYOFLOKXVNDFKNERQQEFR9FCIXQQHNRDKHIVVGFZQKTBWPCOIHCCQIU9ASJQECGPHDBAREDXIRCXOZXPACEEW" adr, err := NewAddress(seed, 0, 2) if err != nil { t.Fatal(err) diff --git a/transaction.go b/transaction.go index 5eea3db28..c8fe91a9e 100644 --- a/transaction.go +++ b/transaction.go @@ -160,7 +160,7 @@ func (t *Transaction) HasValidNonce(mwm int64) bool { //Hash returns the hash of the transaction. func (t *Transaction) Hash() Trytes { - return t.Trytes().Hash() + return t.Trytes().CurlHash() } //UnmarshalJSON makes transaction struct from json. diff --git a/trinary.go b/trinary.go index 0dee3d6d0..999748ac0 100644 --- a/trinary.go +++ b/trinary.go @@ -372,3 +372,18 @@ func (t Trytes) IsValid() error { } return nil } + +//Hash returns hash of t. +func (t Trytes) Hash() Trytes { + c := NewKerl() + c.Absorb(t.Trits()) + tt, _ := c.Squeeze(HashSize) + return tt.Trytes() +} + +//Hash returns hash of t. +func (t Trytes) CurlHash() Trytes { + c := NewCurl() + c.Absorb(t) + return c.Squeeze() +}