github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/merkletree/split_test.go (about) 1 package merkletree 2 3 import ( 4 "fmt" 5 "math/big" 6 "testing" 7 8 "github.com/0xPolygon/supernets2-node/hex" 9 "github.com/0xPolygon/supernets2-node/test/testutils" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestSplit(t *testing.T) { 14 v1str := "115792089237316195423570985008687907853269984665640564039457584007913129639935" 15 16 v1, ok := new(big.Int).SetString(v1str, 10) 17 require.True(t, ok) 18 19 v := scalar2fea(v1) 20 21 v2 := fea2scalar(v) 22 require.Equal(t, v1, v2) 23 24 vv := scalar2fea(v2) 25 26 require.Equal(t, v, vv) 27 } 28 29 func Test_h4ToScalar(t *testing.T) { 30 tcs := []struct { 31 input []uint64 32 expected string 33 }{ 34 { 35 input: []uint64{0, 0, 0, 0}, 36 expected: "0", 37 }, 38 { 39 input: []uint64{0, 1, 2, 3}, 40 expected: "18831305206160042292187933003464876175252262292329349513216", 41 }, 42 } 43 44 for i, tc := range tcs { 45 tc := tc 46 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 47 actual := h4ToScalar(tc.input) 48 expected, ok := new(big.Int).SetString(tc.expected, 10) 49 require.True(t, ok) 50 require.Equal(t, expected, actual) 51 }) 52 } 53 } 54 55 func Test_scalarToh4(t *testing.T) { 56 tcs := []struct { 57 input string 58 expected []uint64 59 }{ 60 { 61 input: "0", 62 expected: []uint64{0, 0, 0, 0}, 63 }, 64 { 65 input: "18831305206160042292187933003464876175252262292329349513216", 66 expected: []uint64{0, 1, 2, 3}, 67 }, 68 } 69 70 for i, tc := range tcs { 71 tc := tc 72 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 73 bi, ok := new(big.Int).SetString(tc.input, 10) 74 require.True(t, ok) 75 76 actual := scalarToh4(bi) 77 require.Equal(t, tc.expected, actual) 78 }) 79 } 80 } 81 82 func Test_h4ToString(t *testing.T) { 83 tcs := []struct { 84 input []uint64 85 expected string 86 }{ 87 { 88 input: []uint64{0, 0, 0, 0}, 89 expected: "0x0000000000000000000000000000000000000000000000000000000000000000", 90 }, 91 { 92 input: []uint64{0, 1, 2, 3}, 93 expected: "0x0000000000000003000000000000000200000000000000010000000000000000", 94 }, 95 } 96 97 for i, tc := range tcs { 98 tc := tc 99 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 100 actual := H4ToString(tc.input) 101 require.Equal(t, tc.expected, actual) 102 }) 103 } 104 } 105 106 func Test_Conversions(t *testing.T) { 107 tcs := []struct { 108 input []uint64 109 }{ 110 { 111 input: []uint64{0, 0, 0, 0}, 112 }, 113 { 114 input: []uint64{0, 1, 2, 3}, 115 }, 116 } 117 118 for i, tc := range tcs { 119 tc := tc 120 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 121 resScalar := h4ToScalar(tc.input) 122 init := scalarToh4(resScalar) 123 require.Equal(t, tc.input, init) 124 }) 125 } 126 } 127 128 func Test_scalar2fea(t *testing.T) { 129 tcs := []struct { 130 input string 131 }{ 132 { 133 input: "0", 134 }, 135 { 136 input: "100", 137 }, 138 { 139 input: "115792089237316195423570985008687907853269984665640564039457584007913129639935", 140 }, 141 } 142 143 for i, tc := range tcs { 144 tc := tc 145 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 146 scalar, ok := new(big.Int).SetString(tc.input, 10) 147 require.True(t, ok) 148 149 res := scalar2fea(scalar) 150 151 actual := fea2scalar(res) 152 require.Equal(t, tc.input, actual.String()) 153 }) 154 } 155 } 156 157 func Test_fea2scalar(t *testing.T) { 158 tcs := []struct { 159 input []uint64 160 }{ 161 { 162 input: []uint64{0, 0, 0, 0, 0, 0, 0, 0}, 163 }, 164 { 165 input: []uint64{1, 1, 1, 1, 1, 1, 1, 1}, 166 }, 167 { 168 input: []uint64{1, 0, 0, 0, 3693650181, 4001443757, 599269951, 1255793162}, 169 }, 170 } 171 172 for i, tc := range tcs { 173 tc := tc 174 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 175 res := fea2scalar(tc.input) 176 177 actual := scalar2fea(res) 178 179 require.Equal(t, tc.input, actual) 180 }) 181 } 182 } 183 184 func Test_stringToh4(t *testing.T) { 185 tcs := []struct { 186 description string 187 input string 188 expected []uint64 189 expectedErr bool 190 expectedErrMsg string 191 }{ 192 { 193 description: "happy path", 194 input: "cafe", 195 expected: []uint64{51966, 0, 0, 0}, 196 }, 197 { 198 description: "0x prefix is allowed", 199 input: "0xcafe", 200 expected: []uint64{51966, 0, 0, 0}, 201 }, 202 203 { 204 description: "non hex input causes error", 205 input: "yu74", 206 expectedErr: true, 207 expectedErrMsg: "Could not convert", 208 }, 209 { 210 description: "empty input causes error", 211 input: "", 212 expectedErr: true, 213 expectedErrMsg: "Could not convert", 214 }, 215 } 216 217 for _, tc := range tcs { 218 tc := tc 219 t.Run(tc.description, func(t *testing.T) { 220 actual, err := StringToh4(tc.input) 221 require.NoError(t, testutils.CheckError(err, tc.expectedErr, tc.expectedErrMsg)) 222 223 require.Equal(t, tc.expected, actual) 224 }) 225 } 226 } 227 228 func Test_ScalarToFilledByteSlice(t *testing.T) { 229 tcs := []struct { 230 input string 231 expected string 232 }{ 233 { 234 input: "0", 235 expected: "0x0000000000000000000000000000000000000000000000000000000000000000", 236 }, 237 { 238 input: "256", 239 expected: "0x0000000000000000000000000000000000000000000000000000000000000100", 240 }, 241 { 242 input: "235938498573495379548793890390932048239042839490238", 243 expected: "0x0000000000000000000000a16f882ee8972432c0a71c5e309ad5f7215690aebe", 244 }, 245 { 246 input: "4309593458485959083095843905390485089430985490434080439904305093450934509490", 247 expected: "0x098724b9a1bc97eee674cf5b6b56b8fafd83ac49c3da1f2c87c822548bbfdfb2", 248 }, 249 { 250 input: "98999023430240239049320492430858334093493024832984092384902398409234090932489", 251 expected: "0xdadf762a31e865f150a1456d7db7963c91361b771c8381a3fb879cf5bf91b909", 252 }, 253 } 254 255 for i, tc := range tcs { 256 tc := tc 257 t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) { 258 input, ok := big.NewInt(0).SetString(tc.input, 10) 259 require.True(t, ok) 260 261 actualSlice := ScalarToFilledByteSlice(input) 262 263 actual := hex.EncodeToHex(actualSlice) 264 265 require.Equal(t, tc.expected, actual) 266 }) 267 } 268 } 269 270 func Test_h4ToFilledByteSlice(t *testing.T) { 271 tcs := []struct { 272 input []uint64 273 expected string 274 }{ 275 { 276 input: []uint64{0, 0, 0, 0}, 277 expected: "0x0000000000000000000000000000000000000000000000000000000000000000", 278 }, 279 { 280 input: []uint64{0, 1, 2, 3}, 281 expected: "0x0000000000000003000000000000000200000000000000010000000000000000", 282 }, 283 { 284 input: []uint64{55345354959, 991992992929, 2, 3}, 285 expected: "0x00000000000000030000000000000002000000e6f763d4a10000000ce2d718cf", 286 }, 287 { 288 input: []uint64{8398349845894398543, 3485942349435495945, 734034022234249459, 5490434584389534589}, 289 expected: "0x4c31f12a390ec37d0a2fd00ddc52d8f330608e18f597e609748ceeb03ffe024f", 290 }, 291 } 292 293 for i, tc := range tcs { 294 tc := tc 295 t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) { 296 actualSlice := h4ToFilledByteSlice(tc.input) 297 298 actual := hex.EncodeToHex(actualSlice) 299 300 require.Equal(t, tc.expected, actual) 301 }) 302 } 303 } 304 305 func Test_string2fea(t *testing.T) { 306 tcs := []struct { 307 input string 308 expectedOutput []uint64 309 expectedError bool 310 expectedErrorMsg string 311 }{ 312 { 313 input: "0", 314 expectedOutput: []uint64{0, 0, 0, 0, 0, 0, 0, 0}, 315 }, 316 { 317 input: "10", 318 expectedOutput: []uint64{16, 0, 0, 0, 0, 0, 0, 0}, 319 }, 320 { 321 input: "256", 322 expectedOutput: []uint64{598, 0, 0, 0, 0, 0, 0, 0}, 323 }, 324 { 325 input: "6195423570985008687907853269984665640564039457584007913129639935", 326 expectedOutput: []uint64{694393141, 1074237745, 60053336, 1701053796, 845781062, 1752762245, 1889030152, 1637171765}, 327 }, 328 { 329 input: "deadbeef", 330 expectedOutput: []uint64{3735928559, 0, 0, 0, 0, 0, 0, 0}, 331 }, 332 { 333 input: "deadbeefs", 334 expectedError: true, 335 expectedErrorMsg: `Could not convert "deadbeefs" into big int`, 336 }, 337 } 338 for i, tc := range tcs { 339 tc := tc 340 t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) { 341 actualOutput, err := string2fea(tc.input) 342 require.NoError(t, testutils.CheckError(err, tc.expectedError, tc.expectedErrorMsg)) 343 344 require.Equal(t, tc.expectedOutput, actualOutput) 345 }) 346 } 347 } 348 349 func Test_fea2string(t *testing.T) { 350 tcs := []struct { 351 input []uint64 352 expectedOutput string 353 expectedError bool 354 expectedErrorMsg string 355 }{ 356 { 357 input: []uint64{0, 0, 0, 0, 0, 0, 0, 0}, 358 expectedOutput: "0x0000000000000000000000000000000000000000000000000000000000000000", 359 }, 360 { 361 input: []uint64{16, 0, 0, 0, 0, 0, 0, 0}, 362 expectedOutput: "0x0000000000000000000000000000000000000000000000000000000000000010", 363 }, 364 { 365 input: []uint64{598, 0, 0, 0, 0, 0, 0, 0}, 366 expectedOutput: "0x0000000000000000000000000000000000000000000000000000000000000256", 367 }, 368 { 369 input: []uint64{694393141, 1074237745, 60053336, 1701053796, 845781062, 1752762245, 1889030152, 1637171765}, 370 expectedOutput: "0x6195423570985008687907853269984665640564039457584007913129639935", 371 }, 372 } 373 for i, tc := range tcs { 374 tc := tc 375 t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) { 376 actualOutput := fea2string(tc.input) 377 require.Equal(t, tc.expectedOutput, actualOutput) 378 }) 379 } 380 }