Skip to content

Instantly share code, notes, and snippets.

@thanhlmm
Created January 7, 2023 11:06
Show Gist options
  • Save thanhlmm/c1ca64a7241b14a39a36620b108f2a61 to your computer and use it in GitHub Desktop.
Save thanhlmm/c1ca64a7241b14a39a36620b108f2a61 to your computer and use it in GitHub Desktop.
Sum uint256
-- SELECT from_big_endian_64(FROM_HEX('00000000000000000000000000000000000000000000000000005af3107a4000')) AS integer_value
WITH input as (
SELECT ARRAY [ from_big_endian_32(FROM_HEX(SUBSTR(string_column, 1, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 9, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 17, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 25, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 33, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 41, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 49, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 57, 8))) ] as input1,
ARRAY [ from_big_endian_32(FROM_HEX(SUBSTR(string_column, 1, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 9, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 17, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 25, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 33, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 41, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 49, 8))),
from_big_endian_32(FROM_HEX(SUBSTR(string_column, 57, 8))) ] as input2
FROM (
SELECT '00000000000000000000000000000000000000000000000000005af3107a4000' as string_column
)
),
SUM_PARTS as (
select reduce(
ARRAY [ input1,
input2 ],
ARRAY [ 0,
0,
0,
0,
0,
0,
0,
0 ],
(s, x)->ARRAY [ element_at(s, 1) + element_at(x, 1),
element_at(s, 2) + element_at(x, 2),
element_at(s, 3) + element_at(x, 3),
element_at(s, 4) + element_at(x, 4),
element_at(s, 5) + element_at(x, 5),
element_at(s, 6) + element_at(x, 6),
element_at(s, 7) + element_at(x, 7),
element_at(s, 8) + element_at(x, 8) ],
s->s
) as sum_parts_output
from input
),
part8 as (
SELECT element_at(sum_parts_output, 8) as output FROM SUM_PARTS
),
part7 as (
SELECT element_at(sum_parts_output, 7) + part8.output / 4294967295 as output FROM SUM_PARTS, part8
),
part6 as (
SELECT element_at(sum_parts_output, 6) + part7.output / 4294967295 as output FROM SUM_PARTS, part7
),
part5 as (
SELECT element_at(sum_parts_output, 5) + part6.output / 4294967295 as output FROM SUM_PARTS, part6
),
part4 as (
SELECT element_at(sum_parts_output, 4) + part5.output / 4294967295 as output FROM SUM_PARTS, part5
),
part3 as (
SELECT element_at(sum_parts_output, 3) + part4.output / 4294967295 as output FROM SUM_PARTS, part4
),
part2 as (
SELECT element_at(sum_parts_output, 2) + part3.output / 4294967295 as output FROM SUM_PARTS, part3
),
part1 as (
SELECT element_at(sum_parts_output, 1) + part2.output / 4294967295 as output FROM SUM_PARTS, part2
)
SELECT
array_join(transform(ARRAY [
to_hex(to_big_endian_32(CAST(part1.output % 4294967295 as INTEGER))),
to_hex(to_big_endian_32(CAST(part2.output % 4294967295 as INTEGER))),
to_hex(to_big_endian_32(CAST(part3.output % 4294967295 as INTEGER))),
to_hex(to_big_endian_32(CAST(part4.output % 4294967295 as INTEGER))),
to_hex(to_big_endian_32(CAST(part5.output % 4294967295 as INTEGER))),
to_hex(to_big_endian_32(CAST(part6.output % 4294967295 as INTEGER))),
to_hex(to_big_endian_32(CAST(part7.output % 4294967295 as INTEGER))),
to_hex(to_big_endian_32(CAST(part8.output % 4294967295 as INTEGER)))
], x -> CAST(x AS VARCHAR)), '') as output
from part8, part7, part6, part5, part4, part3, part2, part1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment