diff --git a/curl/.examples/curl_examples_test.go b/curl/.examples/curl_examples_test.go index 4e889c48a..d205f5465 100644 --- a/curl/.examples/curl_examples_test.go +++ b/curl/.examples/curl_examples_test.go @@ -2,6 +2,7 @@ package curl_examples_test import ( "fmt" + "github.com/iotaledger/iota.go/curl" "github.com/iotaledger/iota.go/trinary" ) @@ -43,12 +44,13 @@ func ExampleTransform() {} func ExampleReset() {} // i req: trits, The Trits of which to compute the hash of. +// i: CurlRounds, The optional number of rounds to use. // o: Trits, The Trits representation of the hash. // o: error, Returned for internal errors. func ExampleHashTrits() { trytes := "PDFIDVWRXONZSPJJQVZVVMLGSVB" trits := trinary.MustTrytesToTrits(trytes) - tritsHash, err := curl.HashTrits(trits) + tritsHash, err := curl.HashTrits(trits, curl.CurlP81) if err != nil { // handle error return @@ -57,11 +59,12 @@ func ExampleHashTrits() { } // i req: t, The Trytes of which to compute the hash of. +// i: CurlRounds, The optional number of rounds to use. // o: Trytes, The Trytes representation of the hash. // o: error, Returned for internal errors. func ExampleHashTrytes() { trytes := "PDFIDVWRXONZSPJJQVZVVMLGSVB" - hash, err := curl.HashTrytes(trytes) + hash, err := curl.HashTrytes(trytes, curl.CurlP81) if err != nil { // handle error return diff --git a/curl/curl.go b/curl/curl.go index 54ad804e7..6156c5ca3 100644 --- a/curl/curl.go +++ b/curl/curl.go @@ -6,16 +6,26 @@ import ( . "github.com/iotaledger/iota.go/trinary" ) +// CurlRounds is the default number of rounds used in transform. +type CurlRounds int + const ( // StateSize is the size of the Curl hash function. StateSize = HashTrinarySize * 3 + + // CurlP27 is used for hashing with 27 rounds + CurlP27 CurlRounds = 27 + + // CurlP81 is used for hashing with 81 rounds + CurlP81 CurlRounds = 81 + // NumberOfRounds is the default number of rounds in transform. - NumberOfRounds = 81 + NumberOfRounds = CurlP81 ) var ( // optional transform function in C. - transformC func(Trits) + transformC func(Trits, int) // TruthTable of the Curl hash function. TruthTable = [11]int8{1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0} // Indices of the Curl hash function. @@ -37,13 +47,21 @@ func init() { // Curl is a sponge function with an internal State of size StateSize. // b = r + c, b = StateSize, r = HashSize, c = StateSize - HashSize type Curl struct { - State Trits + State Trits + Rounds CurlRounds } // NewCurl initializes a new instance with an empty State. -func NewCurl() *Curl { +func NewCurl(rounds ...CurlRounds) *Curl { + curlRounds := NumberOfRounds + + if len(rounds) > 0 { + curlRounds = rounds[0] + } + c := &Curl{ - State: make(Trits, StateSize), + State: make(Trits, StateSize), + Rounds: curlRounds, } return c } @@ -132,13 +150,13 @@ func (c *Curl) MustAbsorbTrytes(inn Trytes) { // Transform does Transform in sponge func. func (c *Curl) Transform() { if transformC != nil { - transformC(c.State) + transformC(c.State, int(c.Rounds)) return } var cpy [StateSize]int8 - for r := NumberOfRounds; r > 0; r-- { + for r := c.Rounds; r > 0; r-- { copy(cpy[:], c.State) for i := 0; i < StateSize; i++ { t1 := Indices[i] @@ -156,15 +174,15 @@ func (c *Curl) Reset() { } // HashTrits returns the hash of the given trits. -func HashTrits(trits Trits) (Trits, error) { - c := NewCurl() +func HashTrits(trits Trits, rounds ...CurlRounds) (Trits, error) { + c := NewCurl(rounds...) c.Absorb(trits) return c.Squeeze(HashTrinarySize) } // HashTrytes returns the hash of the given trytes. -func HashTrytes(t Trytes) (Trytes, error) { - c := NewCurl() +func HashTrytes(t Trytes, rounds ...CurlRounds) (Trytes, error) { + c := NewCurl(rounds...) err := c.AbsorbTrytes(t) if err != nil { return "", err @@ -174,8 +192,8 @@ func HashTrytes(t Trytes) (Trytes, error) { // MustHashTrytes returns the hash of the given trytes. // It panics if the given trytes are not valid. -func MustHashTrytes(t Trytes) Trytes { - trytes, err := HashTrytes(t) +func MustHashTrytes(t Trytes, rounds ...CurlRounds) Trytes { + trytes, err := HashTrytes(t, rounds...) if err != nil { panic(err) } diff --git a/curl/curl_c_transform.go b/curl/curl_c_transform.go index 2b8f1f6fd..bc828fcd5 100644 --- a/curl/curl_c_transform.go +++ b/curl/curl_c_transform.go @@ -15,12 +15,12 @@ const char truthTable[] = {1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0}; const int indices__[] = { 0, 364, 728, 363, 727, 362, 726, 361, 725, 360, 724, 359, 723, 358, 722, 357, 721, 356, 720, 355, 719, 354, 718, 353, 717, 352, 716, 351, 715, 350, 714, 349, 713, 348, 712, 347, 711, 346, 710, 345, 709, 344, 708, 343, 707, 342, 706, 341, 705, 340, 704, 339, 703, 338, 702, 337, 701, 336, 700, 335, 699, 334, 698, 333, 697, 332, 696, 331, 695, 330, 694, 329, 693, 328, 692, 327, 691, 326, 690, 325, 689, 324, 688, 323, 687, 322, 686, 321, 685, 320, 684, 319, 683, 318, 682, 317, 681, 316, 680, 315, 679, 314, 678, 313, 677, 312, 676, 311, 675, 310, 674, 309, 673, 308, 672, 307, 671, 306, 670, 305, 669, 304, 668, 303, 667, 302, 666, 301, 665, 300, 664, 299, 663, 298, 662, 297, 661, 296, 660, 295, 659, 294, 658, 293, 657, 292, 656, 291, 655, 290, 654, 289, 653, 288, 652, 287, 651, 286, 650, 285, 649, 284, 648, 283, 647, 282, 646, 281, 645, 280, 644, 279, 643, 278, 642, 277, 641, 276, 640, 275, 639, 274, 638, 273, 637, 272, 636, 271, 635, 270, 634, 269, 633, 268, 632, 267, 631, 266, 630, 265, 629, 264, 628, 263, 627, 262, 626, 261, 625, 260, 624, 259, 623, 258, 622, 257, 621, 256, 620, 255, 619, 254, 618, 253, 617, 252, 616, 251, 615, 250, 614, 249, 613, 248, 612, 247, 611, 246, 610, 245, 609, 244, 608, 243, 607, 242, 606, 241, 605, 240, 604, 239, 603, 238, 602, 237, 601, 236, 600, 235, 599, 234, 598, 233, 597, 232, 596, 231, 595, 230, 594, 229, 593, 228, 592, 227, 591, 226, 590, 225, 589, 224, 588, 223, 587, 222, 586, 221, 585, 220, 584, 219, 583, 218, 582, 217, 581, 216, 580, 215, 579, 214, 578, 213, 577, 212, 576, 211, 575, 210, 574, 209, 573, 208, 572, 207, 571, 206, 570, 205, 569, 204, 568, 203, 567, 202, 566, 201, 565, 200, 564, 199, 563, 198, 562, 197, 561, 196, 560, 195, 559, 194, 558, 193, 557, 192, 556, 191, 555, 190, 554, 189, 553, 188, 552, 187, 551, 186, 550, 185, 549, 184, 548, 183, 547, 182, 546, 181, 545, 180, 544, 179, 543, 178, 542, 177, 541, 176, 540, 175, 539, 174, 538, 173, 537, 172, 536, 171, 535, 170, 534, 169, 533, 168, 532, 167, 531, 166, 530, 165, 529, 164, 528, 163, 527, 162, 526, 161, 525, 160, 524, 159, 523, 158, 522, 157, 521, 156, 520, 155, 519, 154, 518, 153, 517, 152, 516, 151, 515, 150, 514, 149, 513, 148, 512, 147, 511, 146, 510, 145, 509, 144, 508, 143, 507, 142, 506, 141, 505, 140, 504, 139, 503, 138, 502, 137, 501, 136, 500, 135, 499, 134, 498, 133, 497, 132, 496, 131, 495, 130, 494, 129, 493, 128, 492, 127, 491, 126, 490, 125, 489, 124, 488, 123, 487, 122, 486, 121, 485, 120, 484, 119, 483, 118, 482, 117, 481, 116, 480, 115, 479, 114, 478, 113, 477, 112, 476, 111, 475, 110, 474, 109, 473, 108, 472, 107, 471, 106, 470, 105, 469, 104, 468, 103, 467, 102, 466, 101, 465, 100, 464, 99, 463, 98, 462, 97, 461, 96, 460, 95, 459, 94, 458, 93, 457, 92, 456, 91, 455, 90, 454, 89, 453, 88, 452, 87, 451, 86, 450, 85, 449, 84, 448, 83, 447, 82, 446, 81, 445, 80, 444, 79, 443, 78, 442, 77, 441, 76, 440, 75, 439, 74, 438, 73, 437, 72, 436, 71, 435, 70, 434, 69, 433, 68, 432, 67, 431, 66, 430, 65, 429, 64, 428, 63, 427, 62, 426, 61, 425, 60, 424, 59, 423, 58, 422, 57, 421, 56, 420, 55, 419, 54, 418, 53, 417, 52, 416, 51, 415, 50, 414, 49, 413, 48, 412, 47, 411, 46, 410, 45, 409, 44, 408, 43, 407, 42, 406, 41, 405, 40, 404, 39, 403, 38, 402, 37, 401, 36, 400, 35, 399, 34, 398, 33, 397, 32, 396, 31, 395, 30, 394, 29, 393, 28, 392, 27, 391, 26, 390, 25, 389, 24, 388, 23, 387, 22, 386, 21, 385, 20, 384, 19, 383, 18, 382, 17, 381, 16, 380, 15, 379, 14, 378, 13, 377, 12, 376, 11, 375, 10, 374, 9, 373, 8, 372, 7, 371, 6, 370, 5, 369, 4, 368, 3, 367, 2, 366, 1, 365, 0}; -void transform(signed char state[]) +void transform(signed char state[], int rounds) { int r = 0, i = 0; signed char copy[STATE_LENGTH]={0}; signed char *from=state,*to=copy; - for (r = 0; r < 81; r++) + for (r = 0; r < rounds; r++) { for (i = 0; i < STATE_LENGTH; i++) { @@ -44,6 +44,6 @@ func init() { transformC = transformInC } -func transformInC(state trinary.Trits) { - C.transform((*C.schar)(&state[0])) +func transformInC(state trinary.Trits, rounds int) { + C.transform((*C.schar)(&state[0]), C.int(rounds)) } diff --git a/curl/curl_test.go b/curl/curl_test.go index ebf6e3ae0..f62c9cf97 100644 --- a/curl/curl_test.go +++ b/curl/curl_test.go @@ -12,13 +12,14 @@ import ( var _ = Describe("Curl", func() { DescribeTable("hash computation", - func(in Trytes, expected Trytes) { - Expect(MustHashTrytes(in)).To(Equal(expected)) + func(in Trytes, expected Trytes, rounds ...CurlRounds) { + Expect(MustHashTrytes(in, rounds...)).To(Equal(expected)) }, - Entry("normal trytes", "A", "TJVKPMTAMIZVBVHIVQUPTKEMPROEKV9SB9COEDQYRHYPTYSKQIAN9PQKMZHCPO9TS9BHCORFKW9CQXZEE"), + Entry("normal trytes", "A", "TJVKPMTAMIZVBVHIVQUPTKEMPROEKV9SB9COEDQYRHYPTYSKQIAN9PQKMZHCPO9TS9BHCORFKW9CQXZEE", CurlP81), Entry("normal trytes #2", "B", "QFZXTJUJNLAOSZKXXMMGJJLFACVLRQMRBKOJLMTZXPLPVDSWWWXLBX9CDZWHMDMSDMDQKXQGEWPC9BJHN"), - Entry("normal trytes #3", "ABCDEFGHIJ", "JKSGOZW9WFTALAYESGNJYRGCKIMZSVBMFIIHYBFCUCSLWDI9EEPTZBLGWNPJOMW9HZWNOFGBR9RNHKCYI"), - Entry("empty trytes", "", "999999999999999999999999999999999999999999999999999999999999999999999999999999999"), + Entry("normal trytes #3", "ABCDEFGHIJ", "JKSGOZW9WFTALAYESGNJYRGCKIMZSVBMFIIHYBFCUCSLWDI9EEPTZBLGWNPJOMW9HZWNOFGBR9RNHKCYI", CurlP81), + Entry("empty trytes", "", "999999999999999999999999999999999999999999999999999999999999999999999999999999999", CurlP81), + Entry("empty trytes", "TWENTYSEVEN", "RQPYXJPRXEEPLYLAHWTTFRXXUZTV9SZPEVOQ9FZATCXJOZLZ9A9BFXTUBSHGXN9OOA9GWIPGAAWEDVNPN", CurlP27), ) }) diff --git a/pow/pow_go.go b/pow/pow_go.go index 8efdcf18b..6d0e48645 100644 --- a/pow/pow_go.go +++ b/pow/pow_go.go @@ -217,7 +217,7 @@ func transform64(lmid *[curl.StateSize]uint64, hmid *[curl.StateSize]uint64) { lto := <mp hto := &htmp - for r := 0; r < curl.NumberOfRounds-1; r++ { + for r := 0; r < int(curl.NumberOfRounds)-1; r++ { for j := 0; j < curl.StateSize; j++ { t1 := curl.Indices[j] t2 := curl.Indices[j+1]