File tree Expand file tree Collapse file tree 2 files changed +36
-5
lines changed
Expand file tree Collapse file tree 2 files changed +36
-5
lines changed Original file line number Diff line number Diff line change @@ -178,14 +178,14 @@ static public function parse(string $data): self
178178 */
179179 public function serialize (): string
180180 {
181- $ x = Utils::gmpToBin ($ this ->x );
181+ $ x = Utils::gmpToBin ($ this ->x , 32 );
182182
183183 if ($ this ->isCompressed ()) {
184184 $ prefix = $ this ->wasOdd ? static ::PREFIX_COMPRESSED_ODD : static ::PREFIX_COMPRESSED_EVEN ;
185185 $ y = '' ;
186186 } else {
187187 $ prefix = static ::PREFIX_UNCOMPRESSED ;
188- $ y = Utils::gmpToBin ($ this ->y );
188+ $ y = Utils::gmpToBin ($ this ->y , 32 );
189189 }
190190
191191 return $ prefix . $ x . $ y ;
Original file line number Diff line number Diff line change @@ -70,13 +70,44 @@ static public function hexToHash(string $hex): string
7070 * @param \GMP $gmp
7171 * @return string
7272 */
73- static public function gmpToBin (\GMP $ gmp ): string
73+ static public function gmpToHex (\GMP $ gmp ): string
7474 {
7575 $ hex = gmp_strval ($ gmp , 16 );
76+
7677 if (strlen ($ hex ) % 2 != 0 ) {
7778 $ hex = '0 ' . $ hex ;
7879 }
79- return hex2bin ($ hex );
80+
81+ return $ hex ;
82+ }
83+
84+ /**
85+ * @param string $hex
86+ * @return string
87+ */
88+ static public function hexToGmp (string $ hex ): \GMP
89+ {
90+ return gmp_init ($ hex , 16 );
91+ }
92+
93+ /**
94+ * @param \GMP $gmp
95+ * @return string
96+ */
97+ static public function gmpToBin (\GMP $ gmp , int $ bytes = null ): string
98+ {
99+ $ bin = hex2bin (static ::gmpToHex ($ gmp ));
100+
101+ if ($ bytes !== null ) {
102+ $ size = strlen ($ bin );
103+ if ($ size < $ bytes ) {
104+ $ bin = str_pad ($ bin , $ bytes , chr (0 ), STR_PAD_LEFT );
105+ } elseif ($ size > $ bytes ) {
106+ $ bin = substr ($ bin , 0 , $ bytes );
107+ }
108+ }
109+
110+ return $ bin ;
80111 }
81112
82113 /**
@@ -85,6 +116,6 @@ static public function gmpToBin(\GMP $gmp): string
85116 */
86117 static public function binToGmp (string $ data ): \GMP
87118 {
88- return gmp_init (bin2hex ($ data ), 16 );
119+ return static :: hexToGmp (bin2hex ($ data ));
89120 }
90121}
You can’t perform that action at this time.
0 commit comments