Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Induce data availability into contract #41

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules

package-lock.json
#Hardhat files
cache
artifacts
Expand All @@ -13,4 +13,6 @@ contract-deployed.env
ganache/

# secrets
/secrets.json
/secrets.json
accounts.json
deployed.json
156 changes: 156 additions & 0 deletions contracts/Bytes.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

pragma solidity ^0.8.0;

library Bytes {
function shiftAndReverseBits8(uint8 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 248 ? 8 : 256 - offset;
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>= 1;
}
}

function shiftAndReverseBits16(uint16 val, uint8 offset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we combine shiftAndReverseBits16, shiftAndReverseBits32, shiftAndReverseBits64, shiftAndReverseBits128?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried but failed. If we defined both foo(uint16) and foo(uint32), the calling of foo seems to be considered as ambiguous even you have specified the type of argument explicitly.

internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 240 ? 16 : 256 - offset;
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>= 1;
}
}

function shiftAndReverseBits32(uint32 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 224 ? 32 : 256 - offset;
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>= 1;
}
}

function shiftAndReverseBits64(uint64 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 192 ? 64 : 256 - offset;
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>= 1;
}
}

function shiftAndReverseBits128(uint128 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 128 ? 128 : 256 - offset;
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>= 1;
}
}

function shiftAndReverseBits(uint256 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
for (uint16 i = 0; i < 256 - offset; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>= 1;
}
}

//see https://ethereum.stackexchange.com/questions/83626/how-to-reverse-byte-order-in-uint256-or-bytes32
function swapBytes(uint256 input) internal pure returns (uint256 v) {
v = input;

// swap bytes
v =
((v &
0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >>
8) |
((v &
0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) <<
8);

// swap 2-byte long pairs
v =
((v &
0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >>
16) |
((v &
0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) <<
16);

// swap 4-byte long pairs
v =
((v &
0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >>
32) |
((v &
0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) <<
32);

// swap 8-byte long pairs
v =
((v &
0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >>
64) |
((v &
0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) <<
64);

// swap 16-byte long pairs
v = (v >> 128) | (v << 128);
}

// See comment at the top of this file for explanation of how this function works.
// NOTE: theoretically possible overflow of (_start + 0x2)
function bytesToUInt16(bytes memory _bytes, uint256 _start)
internal
pure
returns (uint16 r)
{
uint256 offset = _start + 0x2;
require(_bytes.length >= offset, "T");
assembly {
r := mload(add(_bytes, offset))
}
}

// NOTE: theoretically possible overflow of (_offset + 2)
function readUInt16(bytes memory _data, uint256 _offset)
internal
pure
returns (uint256 newOffset, uint16 r)
{
newOffset = _offset + 2;
r = bytesToUInt16(_data, _offset);
}
}
Loading