github.com/ledgerwatch/erigon-lib@v1.0.0/gointerfaces/type_utils.go (about) 1 /* 2 Copyright 2021 Erigon contributors 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 */ 13 14 package gointerfaces 15 16 import ( 17 "encoding/binary" 18 19 "github.com/holiman/uint256" 20 "github.com/ledgerwatch/erigon-lib/gointerfaces/types" 21 ) 22 23 func ConvertH2048ToBloom(h2048 *types.H2048) [256]byte { 24 var bloom [256]byte 25 copy(bloom[:], ConvertH512ToBytes(h2048.Hi.Hi)) 26 copy(bloom[64:], ConvertH512ToBytes(h2048.Hi.Lo)) 27 copy(bloom[128:], ConvertH512ToBytes(h2048.Lo.Hi)) 28 copy(bloom[192:], ConvertH512ToBytes(h2048.Lo.Lo)) 29 return bloom 30 } 31 32 func ConvertBytesToH2048(data []byte) *types.H2048 { 33 return &types.H2048{ 34 Hi: &types.H1024{ 35 Hi: ConvertBytesToH512(data), 36 Lo: ConvertBytesToH512(data[64:]), 37 }, 38 Lo: &types.H1024{ 39 Hi: ConvertBytesToH512(data[128:]), 40 Lo: ConvertBytesToH512(data[192:]), 41 }, 42 } 43 } 44 45 func ConvertH256ToHash(h256 *types.H256) [32]byte { 46 var hash [32]byte 47 binary.BigEndian.PutUint64(hash[0:], h256.Hi.Hi) 48 binary.BigEndian.PutUint64(hash[8:], h256.Hi.Lo) 49 binary.BigEndian.PutUint64(hash[16:], h256.Lo.Hi) 50 binary.BigEndian.PutUint64(hash[24:], h256.Lo.Lo) 51 return hash 52 } 53 54 func ConvertH512ToHash(h512 *types.H512) [64]byte { 55 var b [64]byte 56 binary.BigEndian.PutUint64(b[0:], h512.Hi.Hi.Hi) 57 binary.BigEndian.PutUint64(b[8:], h512.Hi.Hi.Lo) 58 binary.BigEndian.PutUint64(b[16:], h512.Hi.Lo.Hi) 59 binary.BigEndian.PutUint64(b[24:], h512.Hi.Lo.Lo) 60 binary.BigEndian.PutUint64(b[32:], h512.Lo.Hi.Hi) 61 binary.BigEndian.PutUint64(b[40:], h512.Lo.Hi.Lo) 62 binary.BigEndian.PutUint64(b[48:], h512.Lo.Lo.Hi) 63 binary.BigEndian.PutUint64(b[56:], h512.Lo.Lo.Lo) 64 return b 65 } 66 67 func ConvertHashesToH256(hashes [][32]byte) []*types.H256 { 68 res := make([]*types.H256, len(hashes)) 69 for i := range hashes { 70 res[i] = ConvertHashToH256(hashes[i]) 71 } 72 return res 73 } 74 75 func ConvertHashToH256(hash [32]byte) *types.H256 { 76 return &types.H256{ 77 Lo: &types.H128{Lo: binary.BigEndian.Uint64(hash[24:]), Hi: binary.BigEndian.Uint64(hash[16:])}, 78 Hi: &types.H128{Lo: binary.BigEndian.Uint64(hash[8:]), Hi: binary.BigEndian.Uint64(hash[0:])}, 79 } 80 } 81 82 func ConvertHashToH512(hash [64]byte) *types.H512 { 83 return ConvertBytesToH512(hash[:]) 84 } 85 86 func ConvertH160toAddress(h160 *types.H160) [20]byte { 87 var addr [20]byte 88 binary.BigEndian.PutUint64(addr[0:], h160.Hi.Hi) 89 binary.BigEndian.PutUint64(addr[8:], h160.Hi.Lo) 90 binary.BigEndian.PutUint32(addr[16:], h160.Lo) 91 return addr 92 } 93 94 func ConvertAddressToH160(addr [20]byte) *types.H160 { 95 return &types.H160{ 96 Lo: binary.BigEndian.Uint32(addr[16:]), 97 Hi: &types.H128{Lo: binary.BigEndian.Uint64(addr[8:]), Hi: binary.BigEndian.Uint64(addr[0:])}, 98 } 99 } 100 101 func ConvertH256ToUint256Int(h256 *types.H256) *uint256.Int { 102 // Note: uint256.Int is an array of 4 uint64 in little-endian order, i.e. most significant word is [3] 103 var i uint256.Int 104 i[3] = h256.Hi.Hi 105 i[2] = h256.Hi.Lo 106 i[1] = h256.Lo.Hi 107 i[0] = h256.Lo.Lo 108 return &i 109 } 110 111 func ConvertUint256IntToH256(i *uint256.Int) *types.H256 { 112 // Note: uint256.Int is an array of 4 uint64 in little-endian order, i.e. most significant word is [3] 113 return &types.H256{ 114 Lo: &types.H128{Lo: i[0], Hi: i[1]}, 115 Hi: &types.H128{Lo: i[2], Hi: i[3]}, 116 } 117 } 118 119 func ConvertH512ToBytes(h512 *types.H512) []byte { 120 b := ConvertH512ToHash(h512) 121 return b[:] 122 } 123 124 func ConvertBytesToH512(b []byte) *types.H512 { 125 if len(b) < 64 { 126 var b1 [64]byte 127 copy(b1[:], b) 128 b = b1[:] 129 } 130 return &types.H512{ 131 Lo: &types.H256{ 132 Lo: &types.H128{Lo: binary.BigEndian.Uint64(b[56:]), Hi: binary.BigEndian.Uint64(b[48:])}, 133 Hi: &types.H128{Lo: binary.BigEndian.Uint64(b[40:]), Hi: binary.BigEndian.Uint64(b[32:])}, 134 }, 135 Hi: &types.H256{ 136 Lo: &types.H128{Lo: binary.BigEndian.Uint64(b[24:]), Hi: binary.BigEndian.Uint64(b[16:])}, 137 Hi: &types.H128{Lo: binary.BigEndian.Uint64(b[8:]), Hi: binary.BigEndian.Uint64(b[0:])}, 138 }, 139 } 140 }