From 16b382f1cd9226d4efadbca7e637683b79a1881e Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 29 Feb 2024 16:54:32 +0900 Subject: [PATCH] uint256 to words --- main.go | 18 ++++++++++++++ word/word.go | 26 ++++++++++++++++++++ word/word_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 main.go create mode 100644 word/word.go create mode 100644 word/word_test.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..f9d3c92 --- /dev/null +++ b/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + + "github.com/gnoswap-labs/int256/word" + "github.com/holiman/uint256" +) + +func main() { + num, _ := uint256.FromHex("0x1234567890abcdef1234567890abcdef1234567890abcdef1234544440abcdef") + + w := word.ToWords(num) + + for i := 0; i < 8; i++ { + fmt.Printf("%d: %x\n", i, w.Words[i]) + } +} diff --git a/word/word.go b/word/word.go new file mode 100644 index 0000000..a299498 --- /dev/null +++ b/word/word.go @@ -0,0 +1,26 @@ +package word + +import ( + "encoding/binary" + + "github.com/holiman/uint256" +) + +type Uint32Words struct { + Words [8]uint32 +} + +func ToWords(num *uint256.Int) *Uint32Words { + // convert uint256 to bytes + bytes := num.Bytes() + + // convert byte array to 32bit size words + words := new(Uint32Words) + for i := 0; i < 8; i++ { + // Extract each 32-bit word. + // The binary.BigEndian.Uint32 function reads 4 bytes from the byte slice and converts it to uint32. + words.Words[i] = binary.BigEndian.Uint32(bytes[i*4 : (i+1)*4]) + } + + return words +} \ No newline at end of file diff --git a/word/word_test.go b/word/word_test.go new file mode 100644 index 0000000..d980904 --- /dev/null +++ b/word/word_test.go @@ -0,0 +1,62 @@ +package word + +import ( + "testing" + + "github.com/holiman/uint256" +) + +func TestToWords(t *testing.T) { + tests := []struct { + name string + hex string // Hexadecimal representation of the uint256 value + expect [8]uint32 + }{ + { + name: "HalfMaxUint256", + hex: "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + expect: [8]uint32{ + 0x7fffffff, 0xffffffff, 0xffffffff, 0xffffffff, + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, + }, + }, + { + name: "RandomNumber", + hex: "0x1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f809", + expect: [8]uint32{ + 0x1a2b3c4d, 0x5e6f7081, 0x92a3b4c5, 0xd6e7f809, + 0x1a2b3c4d, 0x5e6f7081, 0x92a3b4c5, 0xd6e7f809, + }, + }, + { + name: "MaxUint256", + hex: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + expect: [8]uint32{ + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, + }, + }, + { + name: "ArbitraryNumber", + hex: "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", + expect: [8]uint32{ + 0x12345678, 0x90abcdef, 0x12345678, 0x90abcdef, + 0x12345678, 0x90abcdef, 0x12345678, 0x90abcdef, + }, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + num, _ := uint256.FromHex(tc.hex) + + result := ToWords(num) + + for i, word := range result.Words { + if word != tc.expect[i] { + t.Errorf("Test %s failed: word %d is 0x%x, want 0x%x", tc.name, i, word, tc.expect[i]) + } + } + }) + } +}