@theqrl/qrl-contracts 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/interfaces/ISQRCTN1.hyp +6 -0
- package/interfaces/IZRC165.hyp +6 -0
- package/interfaces/IZRC4906.hyp +20 -0
- package/interfaces/draft-IZRC6093.hyp +161 -0
- package/package.json +26 -0
- package/token/SQRCTB1/ISQRCTB1.hyp +123 -0
- package/token/SQRCTB1/ISQRCTB1Receiver.hyp +59 -0
- package/token/SQRCTB1/SQRCTB1.hyp +389 -0
- package/token/SQRCTB1/extensions/ISQRCTB1MetadataURI.hyp +20 -0
- package/token/SQRCTB1/utils/SQRCTB1Utils.hyp +88 -0
- package/token/SQRCTF1/ISQRCTF1.hyp +79 -0
- package/token/SQRCTF1/SQRCTF1.hyp +305 -0
- package/token/SQRCTF1/extensions/ISQRCTF1Metadata.hyp +26 -0
- package/token/SQRCTN1/ISQRCTN1.hyp +135 -0
- package/token/SQRCTN1/ISQRCTN1Receiver.hyp +28 -0
- package/token/SQRCTN1/SQRCTN1.hyp +430 -0
- package/token/SQRCTN1/extensions/ISQRCTN1Metadata.hyp +27 -0
- package/token/SQRCTN1/extensions/SQRCTN1URIStorage.hyp +58 -0
- package/token/SQRCTN1/utils/SQRCTN1Utils.hyp +50 -0
- package/utils/Arrays.hyp +552 -0
- package/utils/Comparators.hyp +19 -0
- package/utils/Context.hyp +28 -0
- package/utils/Counters.hyp +43 -0
- package/utils/Panic.hyp +57 -0
- package/utils/SlotDerivation.hyp +155 -0
- package/utils/StorageSlot.hyp +143 -0
- package/utils/Strings.hyp +490 -0
- package/utils/introspection/IZRC165.hyp +25 -0
- package/utils/introspection/ZRC165.hyp +25 -0
- package/utils/math/Math.hyp +749 -0
- package/utils/math/SafeCast.hyp +1162 -0
- package/utils/math/SignedMath.hyp +68 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// QRL Contracts (last updated v0.1.0) (utils/math/SignedMath.hyp)
|
|
3
|
+
|
|
4
|
+
pragma hyperion >=0.0;
|
|
5
|
+
|
|
6
|
+
import {SafeCast} from "./SafeCast.hyp";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @dev Standard signed math utilities missing in the Hyperion language.
|
|
10
|
+
*/
|
|
11
|
+
library SignedMath {
|
|
12
|
+
/**
|
|
13
|
+
* @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
|
|
14
|
+
*
|
|
15
|
+
* IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
|
|
16
|
+
* However, the compiler may optimize Hyperion ternary operations (i.e. `a ? b : c`) to only compute
|
|
17
|
+
* one branch when needed, making this function more expensive.
|
|
18
|
+
*/
|
|
19
|
+
function ternary(bool condition, int256 a, int256 b) internal pure returns (int256) {
|
|
20
|
+
unchecked {
|
|
21
|
+
// branchless ternary works because:
|
|
22
|
+
// b ^ (a ^ b) == a
|
|
23
|
+
// b ^ 0 == b
|
|
24
|
+
return b ^ ((a ^ b) * int256(SafeCast.toUint(condition)));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @dev Returns the largest of two signed numbers.
|
|
30
|
+
*/
|
|
31
|
+
function max(int256 a, int256 b) internal pure returns (int256) {
|
|
32
|
+
return ternary(a > b, a, b);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @dev Returns the smallest of two signed numbers.
|
|
37
|
+
*/
|
|
38
|
+
function min(int256 a, int256 b) internal pure returns (int256) {
|
|
39
|
+
return ternary(a < b, a, b);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @dev Returns the average of two signed numbers without overflow.
|
|
44
|
+
* The result is rounded towards zero.
|
|
45
|
+
*/
|
|
46
|
+
function average(int256 a, int256 b) internal pure returns (int256) {
|
|
47
|
+
// Formula from the book "Hacker's Delight"
|
|
48
|
+
int256 x = (a & b) + ((a ^ b) >> 1);
|
|
49
|
+
return x + (int256(uint256(x) >> 255) & (a ^ b));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @dev Returns the absolute unsigned value of a signed value.
|
|
54
|
+
*/
|
|
55
|
+
function abs(int256 n) internal pure returns (uint256) {
|
|
56
|
+
unchecked {
|
|
57
|
+
// Formula from the "Bit Twiddling Hacks" by Sean Eron Anderson.
|
|
58
|
+
// Since `n` is a signed integer, the generated bytecode will use the SAR opcode to perform the right shift,
|
|
59
|
+
// taking advantage of the most significant (or "sign" bit) in two's complement representation.
|
|
60
|
+
// This opcode adds new most significant bits set to the value of the previous most significant bit. As a result,
|
|
61
|
+
// the mask will either be `bytes32(0)` (if n is positive) or `~bytes32(0)` (if n is negative).
|
|
62
|
+
int256 mask = n >> 255;
|
|
63
|
+
|
|
64
|
+
// A `bytes32(0)` mask leaves the input unchanged, while a `~bytes32(0)` mask complements it.
|
|
65
|
+
return uint256((n + mask) ^ mask);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|