-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUnsignedInteger.php
More file actions
152 lines (136 loc) · 4.16 KB
/
UnsignedInteger.php
File metadata and controls
152 lines (136 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
namespace ArkEcosystem\Crypto\ByteBuffer\Concerns\Writes;
use InvalidArgumentException;
/**
* This is the unsigned integer writer trait.
*/
trait UnsignedInteger
{
/**
* Writes an 8bit unsigned integer.
*
* @param int $value
* @param int $offset
*
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeUInt8(int $value, int $offset = 0): self
{
return $this->pack('C', $value, $offset);
}
/**
* Writes an icbit unsigned integer.
*
* @param int $value
* @param int $offset
*
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeUInt16(int $value, int $offset = 0): self
{
return $this->pack(['n', 'v', 'S'][$this->order], $value, $offset);
}
/**
* Writes an 32bit unsigned integer.
*
* @param int $value
* @param int $offset
*
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeUInt32(int $value, int $offset = 0): self
{
return $this->pack(['N', 'V', 'L'][$this->order], $value, $offset);
}
/**
* Writes an 64bit unsigned integer.
*
* @param int $value
* @param int $offset
*
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeUInt64(int $value, int $offset = 0): self
{
return $this->pack(['J', 'P', 'Q'][$this->order], $value, $offset);
}
/**
* Writes a 256-bit unsigned integer (uint256).
*
* @param string|int|\GMP $value The value to write as uint256.
* @param int $offset The offset at which to write the value.
*
* @throws InvalidArgumentException If the value does not fit into 256 bits.
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeUInt256($value, int $offset = 0): self
{
// Convert the value to a GMP object for handling large numbers
if (is_numeric($value)) {
$gmpValue = gmp_init($value);
} elseif ($value instanceof \GMP) {
$gmpValue = $value;
} else {
throw new InvalidArgumentException('The value must be a numeric string, integer, or GMP object.');
}
// Export the GMP number to a binary string (big-endian byte order)
$binary = gmp_export($gmpValue);
// Check if the binary length exceeds 32 bytes (256 bits)
if (strlen($binary) > 32) {
throw new InvalidArgumentException('The value must fit into 256 bits.');
}
// Pad the binary string to 32 bytes with zeros on the left
$binary = str_pad($binary, 32, "\0", STR_PAD_LEFT);
// Write the bytes into the buffer at the specified offset
return $this->writeBytes($binary, $offset);
}
/**
* Writes an 8bit unsigned integer. This is an alias of writeUInt8.
*
* @param int $value
* @param int $offset
*
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeUByte(int $value, int $offset = 0): self
{
return $this->writeUInt8($value, $offset);
}
/**
* Writes an 16bit unsigned integer. This is an alias of writeUInt16.
*
* @param int $value
* @param int $offset
*
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeUShort(int $value, int $offset = 0): self
{
return $this->writeUInt16($value, $offset);
}
/**
* Writes an 32bit unsigned integer. This is an alias of writeUInt32.
*
* @param int $value
* @param int $offset
*
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeUInt(int $value, int $offset = 0): self
{
return $this->writeUInt32($value, $offset);
}
/**
* Writes an 64bit unsigned integer. This is an alias of writeUInt64.
*
* @param int $value
* @param int $offset
*
* @return \ArkEcosystem\Crypto\ByteBuffer\ByteBuffer
*/
public function writeULong(int $value, int $offset = 0): self
{
return $this->writeUInt64($value, $offset);
}
}