github.com/deso-protocol/core@v1.2.9/desohash/algorithm_test.go (about) 1 package desohash 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "fmt" 7 "math/rand" 8 "strconv" 9 "testing" 10 "time" 11 ) 12 13 type output struct { 14 V0 [32]byte 15 V1 [32]byte 16 } 17 18 type testVector struct { 19 input []byte 20 expected output 21 } 22 23 var ( 24 empty = testVector{ 25 input: []byte{}, 26 expected: output{ 27 V0: [32]byte{114, 23, 202, 186, 96, 139, 52, 36, 242, 19, 30, 176, 125, 131, 78, 220, 163, 169, 29, 234, 101, 225, 173, 227, 218, 14, 111, 145, 145, 42, 12, 224}, 28 V1: [32]byte{206, 229, 20, 193, 88, 210, 180, 26, 86, 122, 78, 46, 187, 40, 195, 65, 42, 27, 24, 130, 105, 217, 202, 144, 145, 53, 166, 51, 23, 42, 68, 67}, 29 }, 30 } 31 32 zeroHeaderV0 = testVector{ 33 input: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 34 expected: output{ 35 V0: [32]byte{163, 26, 132, 217, 121, 192, 98, 38, 25, 124, 55, 0, 118, 142, 73, 208, 127, 207, 123, 226, 205, 66, 183, 103, 54, 32, 115, 162, 37, 41, 100, 168}, 36 V1: [32]byte{220, 154, 17, 120, 104, 154, 164, 84, 48, 65, 141, 33, 70, 75, 247, 102, 147, 110, 103, 251, 75, 96, 75, 52, 186, 204, 52, 205, 95, 25, 91, 71}, 37 }, 38 } 39 40 maxHeaderV0 = testVector{ 41 input: []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 42 expected: output{ 43 V0: [32]byte{224, 40, 180, 163, 199, 90, 133, 139, 57, 187, 38, 22, 99, 131, 161, 190, 129, 114, 237, 219, 32, 136, 135, 104, 223, 246, 90, 51, 15, 207, 210, 240}, 44 V1: [32]byte{208, 117, 164, 127, 17, 189, 225, 172, 116, 108, 103, 77, 3, 164, 73, 168, 147, 187, 128, 62, 123, 86, 36, 156, 112, 70, 92, 130, 39, 253, 90, 109}, 45 }, 46 } 47 48 genesisHeaderV0 = testVector{ 49 input: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 113, 209, 3, 221, 111, 255, 27, 214, 17, 11, 200, 237, 10, 47, 49, 24, 187, 226, 154, 103, 228, 92, 108, 125, 151, 84, 106, 209, 38, 144, 111, 192, 31, 5, 96, 0, 0, 0, 0, 0, 0, 0, 0}, 50 expected: output{ 51 V0: [32]byte{85, 103, 196, 91, 123, 131, 182, 4, 249, 255, 92, 181, 232, 141, 252, 154, 215, 213, 161, 221, 88, 24, 221, 25, 230, 208, 36, 102, 244, 124, 189, 98}, 52 V1: [32]byte{91, 127, 218, 187, 252, 235, 213, 244, 72, 16, 177, 30, 58, 33, 141, 12, 52, 228, 126, 215, 95, 102, 156, 107, 170, 24, 68, 75, 67, 56, 206, 188}, 53 }, 54 } 55 56 zeroHeaderV1 = testVector{ 57 input: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 58 expected: output{ 59 V0: [32]byte{208, 249, 212, 35, 44, 185, 9, 71, 200, 202, 67, 203, 45, 160, 126, 226, 237, 32, 73, 189, 236, 28, 23, 150, 116, 171, 215, 253, 178, 65, 62, 219}, 60 V1: [32]byte{246, 215, 207, 120, 242, 33, 199, 221, 121, 141, 54, 155, 96, 107, 190, 93, 59, 171, 22, 121, 187, 92, 0, 128, 78, 135, 219, 51, 38, 205, 92, 91}, 61 }, 62 } 63 64 maxHeaderV1 = testVector{ 65 input: []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 66 expected: output{ 67 V0: [32]byte{2, 7, 246, 17, 87, 211, 208, 147, 234, 79, 179, 55, 226, 109, 21, 2, 190, 20, 71, 52, 240, 136, 41, 100, 65, 159, 37, 242, 20, 47, 157, 143}, 68 V1: [32]byte{45, 146, 175, 74, 163, 141, 154, 116, 242, 153, 16, 47, 67, 23, 225, 207, 25, 229, 2, 133, 182, 130, 179, 128, 74, 159, 25, 219, 79, 6, 252, 75}, 69 }, 70 } 71 ) 72 73 var testVectors = []testVector{empty, zeroHeaderV0, maxHeaderV0, genesisHeaderV0, zeroHeaderV1, maxHeaderV1} 74 75 func TestDeSoHashV1Distribution(t *testing.T) { 76 const iterations = 1e5 77 78 r := rand.New(rand.NewSource(time.Now().Unix())) 79 80 bytes := [32]uint64{} 81 82 for i := uint64(0); i < iterations; i++ { 83 hash := DeSoHashV1([]byte(fmt.Sprintf("%b", r.Uint64()))) 84 85 for j, b := range hash { 86 bytes[j] += uint64(b) 87 } 88 } 89 90 for i, b := range bytes { 91 bytes[i] = b / iterations 92 } 93 94 for _, b := range bytes { 95 spread := int(b) - 127 96 if spread > 1 || spread < -1 { 97 t.Fatalf("TestDeSoHashV1Distribution: Non-random distribution! - %v", bytes) 98 } 99 } 100 } 101 102 func TestDeSoHashV1(t *testing.T) { 103 for _, vec := range testVectors { 104 hash := DeSoHashV1(vec.input) 105 106 if bytes.Compare(vec.expected.V1[:], hash[:]) != 0 { 107 t.Errorf("TestDeSoHashV1: Mismatched hash value! Input: %v, Hash: %v, Expected: %v", hex.EncodeToString(vec.input), hex.EncodeToString(hash[:]), hex.EncodeToString(vec.expected.V1[:])) 108 t.Errorf("TestDeSoHashV1: Mismatched hash value! Input: %v, Hash: %v, Expected: %v", hex.EncodeToString(vec.input), (hash[:]), (vec.expected.V1[:])) 109 } 110 } 111 } 112 113 func TestDeSoHashV0(t *testing.T) { 114 for _, vec := range testVectors { 115 hash := DeSoHashV0(vec.input) 116 117 if bytes.Compare(vec.expected.V0[:], hash[:]) != 0 { 118 t.Errorf("TestDeSoHashV0: Mismatched hash value! Input: %v, Hash: %v, Expected: %v", hex.EncodeToString(vec.input), hex.EncodeToString(hash[:]), hex.EncodeToString(vec.expected.V0[:])) 119 t.Errorf("TestDeSoHashV0: Mismatched hash value! Input: %v, Hash: %v, Expected: %v", hex.EncodeToString(vec.input), (hash[:]), (vec.expected.V0[:])) 120 } 121 } 122 } 123 124 func BenchmarkDeSoHashV0(b *testing.B) { 125 for i := 0; i < b.N; i++ { 126 _ = DeSoHashV0([]byte(strconv.FormatInt(int64(i), 10))) 127 } 128 } 129 130 func BenchmarkDeSoHashV1(b *testing.B) { 131 for i := 0; i < b.N; i++ { 132 _ = DeSoHashV1([]byte(strconv.FormatInt(int64(i), 10))) 133 } 134 }