DebugDecorator::bitwiseAnd()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Mdanter\Ecc\Math;
4
5
use Mdanter\Ecc\Primitives\GeneratorPoint;
6
7
/**
8
 * Debug helper class to trace all calls to math functions along with the provided params and result.
9
 */
10
class DebugDecorator implements GmpMathInterface
11
{
12
    /**
13
     * @var GmpMathInterface
14
     */
15
    private $adapter;
16
17
    /**
18
     * @var callable
19
     */
20
    private $writer;
21
22
    /**
23
     * @param GmpMathInterface     $adapter
24
     * @param callable|null        $callback
25
     */
26
    public function __construct(GmpMathInterface $adapter, callable $callback = null)
27
    {
28
        $this->adapter = $adapter;
29
        $this->writer = $callback ?: function ($message) {
30
            echo $message;
31
        };
32
    }
33
34
    /**
35
     *
36
     * @param string $message
37
     */
38
    private function write($message)
39
    {
40
        call_user_func($this->writer, $message);
41
    }
42
43
    /**
44
     *
45
     * @param  string $func
46
     * @param  array  $args
47
     * @return mixed
48
     */
49
    private function call($func, $args)
50
    {
51
        $strArgs = array_map(
52
            function ($arg) {
53
                if ($arg instanceof \GMP) {
54
                    return var_export($this->adapter->toString($arg), true);
55
                } else {
56
                    return var_export($arg, true);
57
                }
58
            },
59
            $args
60
        );
61
62
        if (strpos($func, '::')) {
63
            list(, $func) = explode('::', $func);
64
        }
65
66
        $this->write($func.'('.implode(', ', $strArgs).')');
67
68
        $res = call_user_func_array([ $this->adapter, $func ], $args);
69
70
        if ($res instanceof \GMP) {
71
            $this->write(' => ' . var_export($this->adapter->toString($res), true) . PHP_EOL);
72
        } else {
73
            $this->write(' => ' . var_export($res, true) . PHP_EOL);
74
        }
75
76
        return $res;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     * @see \Mdanter\Ecc\Math\GmpMathInterface::cmp()
82
     */
83
    public function cmp(\GMP $first, \GMP $other): int
84
    {
85
        $func = __METHOD__;
86
        $args = func_get_args();
87
88
        return call_user_func(
89
            array(
90
            $this,
91
            'call',
92
            ),
93
            $func,
94
            $args
95
        );
96
    }
97
98
99
    /**
100
     * {@inheritDoc}
101
     * @see \Mdanter\Ecc\Math\GmpMathInterface::cmp()
102
     */
103
    public function equals(\GMP $first, \GMP $other): bool
104
    {
105
        $func = __METHOD__;
106
        $args = func_get_args();
107
108
        return call_user_func(
109
            array(
110
                $this,
111
                'call',
112
            ),
113
            $func,
114
            $args
115
        );
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     * @see \Mdanter\Ecc\Math\GmpMathInterface::mod()
121
     */
122
    public function mod(\GMP $number, \GMP $modulus): \GMP
123
    {
124
        $func = __METHOD__;
125
        $args = func_get_args();
126
127
        return call_user_func(
128
            array(
129
            $this,
130
            'call',
131
            ),
132
            $func,
133
            $args
134
        );
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     * @see \Mdanter\Ecc\Math\GmpMathInterface::add()
140
     */
141
    public function add(\GMP $augend, \GMP $addend): \GMP
142
    {
143
        $func = __METHOD__;
144
        $args = func_get_args();
145
146
        return call_user_func(
147
            array(
148
            $this,
149
            'call',
150
            ),
151
            $func,
152
            $args
153
        );
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     * @see \Mdanter\Ecc\Math\GmpMathInterface::sub()
159
     */
160
    public function sub(\GMP $minuend, \GMP $subtrahend): \GMP
161
    {
162
        $func = __METHOD__;
163
        $args = func_get_args();
164
165
        return call_user_func(
166
            array(
167
            $this,
168
            'call',
169
            ),
170
            $func,
171
            $args
172
        );
173
    }
174
175
    /**
176
     * {@inheritDoc}
177
     * @see \Mdanter\Ecc\Math\GmpMathInterface::mul()
178
     */
179
    public function mul(\GMP $multiplier, \GMP $multiplicand): \GMP
180
    {
181
        $func = __METHOD__;
182
        $args = func_get_args();
183
184
        return call_user_func(
185
            array(
186
            $this,
187
            'call',
188
            ),
189
            $func,
190
            $args
191
        );
192
    }
193
194
    /**
195
     * {@inheritDoc}
196
     * @see \Mdanter\Ecc\Math\GmpMathInterface::div()
197
     */
198
    public function div(\GMP $dividend, \GMP $divisor): \GMP
199
    {
200
        $func = __METHOD__;
201
        $args = func_get_args();
202
203
        return call_user_func(
204
            array(
205
            $this,
206
            'call',
207
            ),
208
            $func,
209
            $args
210
        );
211
    }
212
213
    /**
214
     * {@inheritDoc}
215
     * @see \Mdanter\Ecc\Math\GmpMathInterface::pow()
216
     */
217
    public function pow(\GMP $base, int $exponent): \GMP
218
    {
219
        $func = __METHOD__;
220
        $args = func_get_args();
221
222
        return call_user_func(
223
            array(
224
            $this,
225
            'call',
226
            ),
227
            $func,
228
            $args
229
        );
230
    }
231
232
    /**
233
     * {@inheritDoc}
234
     * @see \Mdanter\Ecc\Math\GmpMathInterface::bitwiseAnd()
235
     */
236
    public function bitwiseAnd(\GMP $first, \GMP $other): \GMP
237
    {
238
        $func = __METHOD__;
239
        $args = func_get_args();
240
241
        return call_user_func(
242
            array(
243
            $this,
244
            'call',
245
            ),
246
            $func,
247
            $args
248
        );
249
    }
250
251
    /**
252
     * {@inheritDoc}
253
     * @see \Mdanter\Ecc\MathAdapter::toString()
254
     */
255
    public function toString(\GMP $value): string
256
    {
257
        return $this->adapter->toString($value);
258
    }
259
260
    /**
261
     * {@inheritDoc}
262
     * @see \Mdanter\Ecc\Math\GmpMathInterface::hexDec()
263
     */
264
    public function hexDec(string $hexString): string
265
    {
266
        $func = __METHOD__;
267
        $args = func_get_args();
268
269
        return call_user_func(
270
            array(
271
            $this,
272
            'call',
273
            ),
274
            $func,
275
            $args
276
        );
277
    }
278
279
    /**
280
     * {@inheritDoc}
281
     * @see \Mdanter\Ecc\Math\GmpMathInterface::decHex()
282
     */
283
    public function decHex(string $decString): string
284
    {
285
        $func = __METHOD__;
286
        $args = func_get_args();
287
288
        return call_user_func(
289
            array(
290
            $this,
291
            'call',
292
            ),
293
            $func,
294
            $args
295
        );
296
    }
297
298
    /**
299
     * {@inheritDoc}
300
     * @see \Mdanter\Ecc\Math\GmpMathInterface::powmod()
301
     */
302
    public function powmod(\GMP $base, \GMP $exponent, \GMP $modulus): \GMP
303
    {
304
        $func = __METHOD__;
305
        $args = func_get_args();
306
307
        return call_user_func(
308
            array(
309
            $this,
310
            'call',
311
            ),
312
            $func,
313
            $args
314
        );
315
    }
316
317
    /**
318
     * {@inheritDoc}
319
     * @see \Mdanter\Ecc\Math\GmpMathInterface::isPrime()
320
     */
321
    public function isPrime(\GMP $n): bool
322
    {
323
        $func = __METHOD__;
324
        $args = func_get_args();
325
326
        return call_user_func(
327
            array(
328
            $this,
329
            'call',
330
            ),
331
            $func,
332
            $args
333
        );
334
    }
335
336
    /**
337
     * {@inheritDoc}
338
     * @see \Mdanter\Ecc\Math\GmpMathInterface::nextPrime()
339
     */
340
    public function nextPrime(\GMP $currentPrime): \GMP
341
    {
342
        $func = __METHOD__;
343
        $args = func_get_args();
344
345
        return call_user_func(
346
            array(
347
            $this,
348
            'call',
349
            ),
350
            $func,
351
            $args
352
        );
353
    }
354
355
    /**
356
     * {@inheritDoc}
357
     * @see \Mdanter\Ecc\Math\GmpMathInterface::inverseMod()
358
     */
359
    public function inverseMod(\GMP $a, \GMP $m): \GMP
360
    {
361
        $func = __METHOD__;
362
        $args = func_get_args();
363
364
        return call_user_func(
365
            array(
366
            $this,
367
            'call',
368
            ),
369
            $func,
370
            $args
371
        );
372
    }
373
374
    /**
375
     * {@inheritDoc}
376
     * @see \Mdanter\Ecc\Math\GmpMathInterface::jacobi()
377
     */
378
    public function jacobi(\GMP $a, \GMP $p): int
379
    {
380
        $func = __METHOD__;
381
        $args = func_get_args();
382
383
        return call_user_func(
384
            array(
385
            $this,
386
            'call',
387
            ),
388
            $func,
389
            $args
390
        );
391
    }
392
393
    /**
394
     * {@inheritDoc}
395
     * @see \Mdanter\Ecc\Math\GmpMathInterface::intToString()
396
     */
397
    public function intToFixedSizeString(\GMP $x, int $byteSize): string
398
    {
399
        $func = __METHOD__;
400
        $args = func_get_args();
401
402
        return call_user_func(
403
            array(
404
                $this,
405
                'call',
406
            ),
407
            $func,
408
            $args
409
        );
410
    }
411
412
    /**
413
     * {@inheritDoc}
414
     * @see \Mdanter\Ecc\Math\GmpMathInterface::intToString()
415
     */
416
    public function intToString(\GMP $x): string
417
    {
418
        $func = __METHOD__;
419
        $args = func_get_args();
420
421
        return call_user_func(
422
            array(
423
            $this,
424
            'call',
425
            ),
426
            $func,
427
            $args
428
        );
429
    }
430
431
    /**
432
     * {@inheritDoc}
433
     * @see \Mdanter\Ecc\Math\GmpMathInterface::stringToInt()
434
     */
435
    public function stringToInt(string $s): \GMP
436
    {
437
        $func = __METHOD__;
438
        $args = func_get_args();
439
440
        return call_user_func(
441
            array(
442
            $this,
443
            'call',
444
            ),
445
            $func,
446
            $args
447
        );
448
    }
449
450
    /**
451
     * {@inheritDoc}
452
     * @see \Mdanter\Ecc\Math\GmpMathInterface::digestInteger()
453
     */
454
    public function digestInteger(\GMP $m): \GMP
455
    {
456
        $func = __METHOD__;
457
        $args = func_get_args();
458
459
        return call_user_func(
460
            array(
461
            $this,
462
            'call',
463
            ),
464
            $func,
465
            $args
466
        );
467
    }
468
469
    /**
470
     * {@inheritDoc}
471
     * @see \Mdanter\Ecc\Math\GmpMathInterface::gcd2()
472
     */
473
    public function gcd2(\GMP $a, \GMP $m): \GMP
474
    {
475
        $func = __METHOD__;
476
        $args = func_get_args();
477
478
        return call_user_func(
479
            array(
480
            $this,
481
            'call',
482
            ),
483
            $func,
484
            $args
485
        );
486
    }
487
488
    /**
489
     * {@inheritDoc}
490
     * @see \Mdanter\Ecc\Math\GmpMathInterface::rightShift()
491
     */
492
    public function rightShift(\GMP $number, int $positions): \GMP
493
    {
494
        $func = __METHOD__;
495
        $args = func_get_args();
496
497
        return call_user_func(
498
            array(
499
            $this,
500
            'call',
501
            ),
502
            $func,
503
            $args
504
        );
505
    }
506
507
    /**
508
     * {@inheritDoc}
509
     * @see \Mdanter\Ecc\Math\GmpMathInterface::leftShift()
510
     */
511
    public function leftShift(\GMP $number, int $positions): \GMP
512
    {
513
        $func = __METHOD__;
514
        $args = func_get_args();
515
516
        return call_user_func(
517
            array(
518
            $this,
519
            'call',
520
            ),
521
            $func,
522
            $args
523
        );
524
    }
525
526
    /**
527
     * {@inheritDoc}
528
     * @see \Mdanter\Ecc\Math\GmpMathInterface::bitwiseXor()
529
     */
530
    public function bitwiseXor(\GMP $first, \GMP $other): \GMP
531
    {
532
        $func = __METHOD__;
533
        $args = func_get_args();
534
535
        return call_user_func(
536
            array(
537
            $this,
538
            'call'
539
            ),
540
            $func,
541
            $args
542
        );
543
    }
544
545
    /**
546
     * {@inheritDoc}
547
     * @see \Mdanter\Ecc\Math\GmpMathInterface::baseConvert()
548
     */
549
    public function baseConvert(string $value, int $fromBase, int $toBase): string
550
    {
551
        $func = __METHOD__;
552
        $args = func_get_args();
553
554
        return call_user_func(
555
            array(
556
            $this,
557
            'call'
558
            ),
559
            $func,
560
            $args
561
        );
562
    }
563
564
    /**
565
     * {@inheritDoc}
566
     * @see \Mdanter\Ecc\Math\GmpMathInterface::getEcMath()
567
     */
568
    public function getEcMath(GeneratorPoint $generatorPoint, $input)
569
    {
570
        $func = __METHOD__;
571
        $args = func_get_args();
572
573
        return call_user_func(
574
            array(
575
            $this,
576
            'call'
577
            ),
578
            $func,
579
            $args
580
        );
581
    }
582
583
    /**
584
     * {@inheritDoc}
585
     * @see \Mdanter\Ecc\Math\GmpMathInterface::getModularArithmetic()
586
     */
587
    public function getModularArithmetic(\GMP $modulus): ModularArithmetic
588
    {
589
        $func = __METHOD__;
590
        $args = func_get_args();
591
592
        return call_user_func(
593
            array(
594
                $this,
595
                'call'
596
            ),
597
            $func,
598
            $args
599
        );
600
    }
601
602
    /**
603
     * {@inheritDoc}
604
     * @see \Mdanter\Ecc\Math\GmpMathInterface::getNumberTheory()
605
     */
606
    public function getNumberTheory(): NumberTheory
607
    {
608
        $func = __METHOD__;
609
        $args = func_get_args();
610
611
        return call_user_func(
612
            array(
613
            $this,
614
            'call'
615
            ),
616
            $func,
617
            $args
618
        );
619
    }
620
}
621