|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script\Interpreter; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
6
|
|
|
use BitWasp\Bitcoin\Flags; |
|
7
|
|
|
use BitWasp\Bitcoin\Math\Math; |
|
8
|
|
|
use BitWasp\Bitcoin\Serializable; |
|
9
|
|
|
use BitWasp\Buffertools\Buffer; |
|
10
|
|
|
|
|
11
|
|
|
class Number extends Serializable |
|
12
|
|
|
{ |
|
13
|
|
|
const MAX_NUM_SIZE = 4; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var int |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $number; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Flags |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $flags; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Number constructor. |
|
27
|
|
|
* @param int $number |
|
28
|
|
|
* @param Math $math |
|
29
|
|
|
*/ |
|
30
|
762 |
|
public function __construct($number, Math $math) |
|
31
|
|
|
{ |
|
32
|
762 |
|
$this->number = $number; |
|
33
|
762 |
|
$this->math = $math; |
|
|
|
|
|
|
34
|
762 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param int $number |
|
38
|
|
|
* @param Math|null $math |
|
39
|
|
|
* @return self |
|
40
|
|
|
*/ |
|
41
|
222 |
|
public static function int($number, Math $math = null) |
|
42
|
|
|
{ |
|
43
|
222 |
|
return new self( |
|
44
|
222 |
|
$number, |
|
45
|
222 |
|
$math ?: Bitcoin::getMath() |
|
46
|
222 |
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Buffer $vch |
|
51
|
|
|
* @param bool $fRequireMinimal |
|
52
|
|
|
* @param int $maxNumSize |
|
53
|
|
|
* @param Math|null $math |
|
54
|
|
|
* @return self |
|
55
|
|
|
*/ |
|
56
|
762 |
|
public static function buffer(Buffer $vch, $fRequireMinimal, $maxNumSize = self::MAX_NUM_SIZE, Math $math = null) |
|
57
|
|
|
{ |
|
58
|
762 |
|
$size = $vch->getSize(); |
|
59
|
762 |
|
if ($size > $maxNumSize) { |
|
60
|
|
|
throw new \RuntimeException('Script number overflow'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
762 |
|
if ($fRequireMinimal && $size > 0) { |
|
64
|
|
|
$binary = $vch->getBinary(); |
|
65
|
|
|
if (ord($binary[$size - 1]) & 0x7f === 0) { |
|
66
|
|
|
if ($size <= 1 || ord($binary[$size - 2]) & 0x80 === 0) { |
|
67
|
|
|
throw new \RuntimeException('Non-minimally encoded script number'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
762 |
|
$math = $math ?: Bitcoin::getMath(); |
|
73
|
762 |
|
$number = new self(0, $math); |
|
74
|
762 |
|
$number->number = $number->parseBuffer($vch); |
|
|
|
|
|
|
75
|
762 |
|
return $number; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Buffer $buffer |
|
80
|
|
|
* @return int|string |
|
81
|
|
|
*/ |
|
82
|
762 |
|
private function parseBuffer(Buffer $buffer) |
|
83
|
|
|
{ |
|
84
|
762 |
|
$size = $buffer->getSize(); |
|
85
|
762 |
|
if ($size === 0) { |
|
86
|
762 |
|
return '0'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$chars = array_map(function ($binary) { |
|
90
|
762 |
|
return ord($binary); |
|
91
|
|
|
|
|
92
|
762 |
|
}, str_split($buffer->flip()->getBinary(), 1)); |
|
93
|
|
|
|
|
94
|
762 |
|
$result = 0; |
|
95
|
762 |
|
for ($i = 0; $i < $size; $i++) { |
|
96
|
762 |
|
$mul = $this->math->mul($i, 8); |
|
97
|
762 |
|
$byte = $this->math->leftShift($chars[$i], $mul); |
|
98
|
762 |
|
$result = $this->math->bitwiseOr($result, $byte); |
|
99
|
762 |
|
} |
|
100
|
|
|
|
|
101
|
762 |
|
if ($chars[0] & 0x80) { |
|
102
|
|
|
$mask = gmp_strval(gmp_com($this->math->leftShift(0x80, (8 * ($size - 1)))), 10); |
|
103
|
|
|
return $this->math->sub(0, $this->math->bitwiseAnd($result, $mask)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
762 |
|
return $result; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return Buffer |
|
111
|
|
|
*/ |
|
112
|
762 |
|
private function serialize() |
|
113
|
|
|
{ |
|
114
|
762 |
|
if ($this->math->cmp($this->number, '0') === 0) { |
|
115
|
762 |
|
return new Buffer('', 4); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Using array of integers instead of bytes |
|
119
|
762 |
|
$result = []; |
|
120
|
762 |
|
$negative = $this->math->cmp($this->number, 0) < 0; |
|
121
|
762 |
|
$abs = $negative ? $this->math->sub(0, $this->number) : $this->number; |
|
122
|
762 |
|
while ($this->math->cmp($abs, 0) > 0) { |
|
123
|
762 |
|
array_unshift($result, (int)$this->math->bitwiseAnd($abs, 0xff)); |
|
124
|
762 |
|
$abs = $this->math->rightShift($abs, 8); |
|
125
|
762 |
|
} |
|
126
|
|
|
|
|
127
|
762 |
|
if ($result[0] & 0x80) { |
|
128
|
|
|
array_unshift($result, $negative ? 0x80 : 0x00); |
|
129
|
762 |
|
} else if ($negative) { |
|
130
|
|
|
array_unshift($result, 0x80); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
762 |
|
return new Buffer(array_reduce($result, function ($agg, $current) { |
|
134
|
762 |
|
return $agg . chr($current); |
|
135
|
762 |
|
}), 4, $this->math); |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return Buffer |
|
141
|
|
|
*/ |
|
142
|
762 |
|
public function getBuffer() |
|
143
|
|
|
{ |
|
144
|
762 |
|
return $this->serialize(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return int |
|
149
|
|
|
*/ |
|
150
|
192 |
|
public function getInt() |
|
151
|
|
|
{ |
|
152
|
192 |
|
return $this->number; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: