Skip to content

Commit c1eb809

Browse files
authored
v0.0.3
2 parents 40da64b + 49d2422 commit c1eb809

File tree

6 files changed

+108
-72
lines changed

6 files changed

+108
-72
lines changed

contracts/AssertBytes.sol

+33-31
Original file line numberDiff line numberDiff line change
@@ -105,40 +105,42 @@ library AssertBytes {
105105
// slength can contain both the length and contents of the array
106106
// if length < 32 bytes so let's prepare for that
107107
// v. http://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage
108-
switch lt(slength, 32)
109-
case 1 {
110-
// blank the last byte which is the length
111-
fslot := mul(div(fslot, 0x100), 0x100)
108+
if iszero(iszero(slength)) {
109+
switch lt(slength, 32)
110+
case 1 {
111+
// blank the last byte which is the length
112+
fslot := mul(div(fslot, 0x100), 0x100)
112113

113-
if iszero(eq(fslot, mload(add(_b, 0x20)))) {
114-
// unsuccess:
115-
returnBool := 0
116-
}
117-
}
118-
default {
119-
// cb is a circuit breaker in the for loop since there's
120-
// no said feature for inline assembly loops
121-
// cb = 1 - don't breaker
122-
// cb = 0 - break
123-
let cb := 1
124-
125-
// get the keccak hash to get the contents of the array
126-
mstore(0x0, _a_slot)
127-
let sc := keccak256(0x0, 0x20)
128-
129-
let mc := add(_b, 0x20)
130-
let end := add(mc, mlength)
131-
132-
// the next line is the loop condition:
133-
// while(uint(mc < end) + cb == 2)
134-
for {} eq(add(lt(mc, end), cb), 2) {
135-
sc := add(sc, 1)
136-
mc := add(mc, 0x20)
137-
} {
138-
if iszero(eq(sload(sc), mload(mc))) {
114+
if iszero(eq(fslot, mload(add(_b, 0x20)))) {
139115
// unsuccess:
140116
returnBool := 0
141-
cb := 0
117+
}
118+
}
119+
default {
120+
// cb is a circuit breaker in the for loop since there's
121+
// no said feature for inline assembly loops
122+
// cb = 1 - don't breaker
123+
// cb = 0 - break
124+
let cb := 1
125+
126+
// get the keccak hash to get the contents of the array
127+
mstore(0x0, _a_slot)
128+
let sc := keccak256(0x0, 0x20)
129+
130+
let mc := add(_b, 0x20)
131+
let end := add(mc, mlength)
132+
133+
// the next line is the loop condition:
134+
// while(uint(mc < end) + cb == 2)
135+
for {} eq(add(lt(mc, end), cb), 2) {
136+
sc := add(sc, 1)
137+
mc := add(mc, 0x20)
138+
} {
139+
if iszero(eq(sload(sc), mload(mc))) {
140+
// unsuccess:
141+
returnBool := 0
142+
cb := 0
143+
}
142144
}
143145
}
144146
}

contracts/BytesLib.sol

+33-31
Original file line numberDiff line numberDiff line change
@@ -271,40 +271,42 @@ library BytesLib {
271271
// slength can contain both the length and contents of the array
272272
// if length < 32 bytes so let's prepare for that
273273
// v. http://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage
274-
switch lt(slength, 32)
275-
case 1 {
276-
// blank the last byte which is the length
277-
fslot := mul(div(fslot, 0x100), 0x100)
274+
if iszero(iszero(slength)) {
275+
switch lt(slength, 32)
276+
case 1 {
277+
// blank the last byte which is the length
278+
fslot := mul(div(fslot, 0x100), 0x100)
278279

279-
if iszero(eq(fslot, mload(add(_postBytes, 0x20)))) {
280-
// unsuccess:
281-
success := 0
282-
}
283-
}
284-
default {
285-
// cb is a circuit breaker in the for loop since there's
286-
// no said feature for inline assembly loops
287-
// cb = 1 - don't breaker
288-
// cb = 0 - break
289-
let cb := 1
290-
291-
// get the keccak hash to get the contents of the array
292-
mstore(0x0, _preBytes_slot)
293-
let sc := keccak256(0x0, 0x20)
294-
295-
let mc := add(_postBytes, 0x20)
296-
let end := add(mc, mlength)
297-
298-
// the next line is the loop condition:
299-
// while(uint(mc < end) + cb == 2)
300-
for {} eq(add(lt(mc, end), cb), 2) {
301-
sc := add(sc, 1)
302-
mc := add(mc, 0x20)
303-
} {
304-
if iszero(eq(sload(sc), mload(mc))) {
280+
if iszero(eq(fslot, mload(add(_postBytes, 0x20)))) {
305281
// unsuccess:
306282
success := 0
307-
cb := 0
283+
}
284+
}
285+
default {
286+
// cb is a circuit breaker in the for loop since there's
287+
// no said feature for inline assembly loops
288+
// cb = 1 - don't breaker
289+
// cb = 0 - break
290+
let cb := 1
291+
292+
// get the keccak hash to get the contents of the array
293+
mstore(0x0, _preBytes_slot)
294+
let sc := keccak256(0x0, 0x20)
295+
296+
let mc := add(_postBytes, 0x20)
297+
let end := add(mc, mlength)
298+
299+
// the next line is the loop condition:
300+
// while(uint(mc < end) + cb == 2)
301+
for {} eq(add(lt(mc, end), cb), 2) {
302+
sc := add(sc, 1)
303+
mc := add(mc, 0x20)
304+
} {
305+
if iszero(eq(sload(sc), mload(mc))) {
306+
// unsuccess:
307+
success := 0
308+
cb := 0
309+
}
308310
}
309311
}
310312
}

ethpm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"package_name": "bytes",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Solidity bytes tightly packed arrays utility library.",
55
"authors": [
66
"Gonçalo Sá <[email protected]>"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "solidity-bytes-utils",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Solidity bytes tightly packed arrays utility library.",
55
"main": "truffle.js",
66
"repository": {

test/TestBytesLib1.sol

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ contract TestBytesLib1 {
1010
using BytesLib for bytes;
1111

1212
bytes storageCheckBytes = hex"aabbccddeeff";
13+
bytes storageCheckBytesZeroLength = hex"";
1314

1415
bytes storageBytes4 = hex"f00dfeed";
1516
bytes storageBytes31 = hex"f00d000000000000000000000000000000000000000000000000000000feed";
@@ -28,16 +29,31 @@ contract TestBytesLib1 {
2829

2930
function testSanityCheck() public {
3031
// Assert library sanity checks
32+
//
33+
// Please don't change the ordering of the var definitions
34+
// the order is purposeful for testing zero-length arrays
3135
bytes memory checkBytes = hex"aabbccddeeff";
36+
bytes memory checkBytesZeroLength = hex"";
3237

3338
bytes memory checkBytesRight = hex"aabbccddeeff";
34-
bytes memory checkBytesWrong = hex"000000";
39+
bytes memory checkBytesZeroLengthRight = hex"";
40+
bytes memory checkBytesWrongLength = hex"aa0000";
41+
bytes memory checkBytesWrongContent = hex"aabbccddee00";
3542

3643
AssertBytes.equal(checkBytes, checkBytesRight, "Sanity check should be checking equal bytes arrays out.");
37-
AssertBytes.notEqual(checkBytes, checkBytesWrong, "Sanity check should be checking different bytes arrays out.");
44+
AssertBytes.notEqual(checkBytes, checkBytesWrongLength, "Sanity check should be checking different length bytes arrays out.");
45+
AssertBytes.notEqual(checkBytes, checkBytesWrongContent, "Sanity check should be checking different content bytes arrays out.");
3846

39-
AssertBytes.equalStorage(storageCheckBytes, checkBytesRight, "Sanity check should be checking equal bytes arrays out.");
40-
AssertBytes.notEqualStorage(storageCheckBytes, checkBytesWrong, "Sanity check should be checking different bytes arrays out.");
47+
AssertBytes.equalStorage(storageCheckBytes, checkBytesRight, "Sanity check should be checking equal bytes arrays out. (Storage)");
48+
AssertBytes.notEqualStorage(storageCheckBytes, checkBytesWrongLength, "Sanity check should be checking different length bytes arrays out. (Storage)");
49+
AssertBytes.notEqualStorage(storageCheckBytes, checkBytesWrongContent, "Sanity check should be checking different content bytes arrays out. (Storage)");
50+
51+
// Zero-length checks
52+
AssertBytes.equal(checkBytesZeroLength, checkBytesZeroLengthRight, "Sanity check should be checking equal zero-length bytes arrays out.");
53+
AssertBytes.notEqual(checkBytesZeroLength, checkBytes, "Sanity check should be checking different length bytes arrays out.");
54+
55+
AssertBytes.equalStorage(storageCheckBytesZeroLength, checkBytesZeroLengthRight, "Sanity check should be checking equal zero-length bytes arrays out. (Storage)");
56+
AssertBytes.notEqualStorage(storageCheckBytesZeroLength, checkBytes, "Sanity check should be checking different length bytes arrays out. (Storage)");
4157
}
4258

4359
/**

test/TestBytesLib2.sol

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,39 @@ contract TestBytesLib2 {
1010
using BytesLib for bytes;
1111

1212
bytes storageCheckBytes = hex"aabbccddeeff";
13+
bytes storageCheckBytesZeroLength = hex"";
1314

1415
/**
1516
* Sanity Checks
1617
*/
1718

1819
function testSanityCheck() public {
1920
// Assert library sanity checks
21+
//
22+
// Please don't change the ordering of the var definitions
23+
// the order is purposeful for testing zero-length arrays
2024
bytes memory checkBytes = hex"aabbccddeeff";
25+
bytes memory checkBytesZeroLength = hex"";
2126

2227
bytes memory checkBytesRight = hex"aabbccddeeff";
23-
bytes memory checkBytesWrong = hex"000000";
28+
bytes memory checkBytesZeroLengthRight = hex"";
29+
bytes memory checkBytesWrongLength = hex"aa0000";
30+
bytes memory checkBytesWrongContent = hex"aabbccddee00";
2431

2532
AssertBytes.equal(checkBytes, checkBytesRight, "Sanity check should be checking equal bytes arrays out.");
26-
AssertBytes.notEqual(checkBytes, checkBytesWrong, "Sanity check should be checking different bytes arrays out.");
33+
AssertBytes.notEqual(checkBytes, checkBytesWrongLength, "Sanity check should be checking different length bytes arrays out.");
34+
AssertBytes.notEqual(checkBytes, checkBytesWrongContent, "Sanity check should be checking different content bytes arrays out.");
2735

28-
AssertBytes.equalStorage(storageCheckBytes, checkBytesRight, "Sanity check should be checking equal bytes arrays out.");
29-
AssertBytes.notEqualStorage(storageCheckBytes, checkBytesWrong, "Sanity check should be checking different bytes arrays out.");
36+
AssertBytes.equalStorage(storageCheckBytes, checkBytesRight, "Sanity check should be checking equal bytes arrays out. (Storage)");
37+
AssertBytes.notEqualStorage(storageCheckBytes, checkBytesWrongLength, "Sanity check should be checking different length bytes arrays out. (Storage)");
38+
AssertBytes.notEqualStorage(storageCheckBytes, checkBytesWrongContent, "Sanity check should be checking different content bytes arrays out. (Storage)");
39+
40+
// Zero-length checks
41+
AssertBytes.equal(checkBytesZeroLength, checkBytesZeroLengthRight, "Sanity check should be checking equal zero-length bytes arrays out.");
42+
AssertBytes.notEqual(checkBytesZeroLength, checkBytes, "Sanity check should be checking different length bytes arrays out.");
43+
44+
AssertBytes.equalStorage(storageCheckBytesZeroLength, checkBytesZeroLengthRight, "Sanity check should be checking equal zero-length bytes arrays out. (Storage)");
45+
AssertBytes.notEqualStorage(storageCheckBytesZeroLength, checkBytes, "Sanity check should be checking different length bytes arrays out. (Storage)");
3046
}
3147

3248
/**

0 commit comments

Comments
 (0)