summaryrefslogtreecommitdiffstats
path: root/vendor/web-token/jwt-util-ecc/Math.php
blob: 7d3f5ea73442dd689bfcaa71598bf24741c14797 (plain) (blame)
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
<?php

declare(strict_types=1);

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2018 Spomky-Labs
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

namespace Jose\Component\Core\Util\Ecc;

/**
 * @internal
 */
class Math
{
    public static function cmp(\GMP $first, \GMP $other): int
    {
        return \gmp_cmp($first, $other);
    }

    public static function equals(\GMP $first, \GMP $other): bool
    {
        return 0 === \gmp_cmp($first, $other);
    }

    public static function mod(\GMP $number, \GMP $modulus): \GMP
    {
        return \gmp_mod($number, $modulus);
    }

    public static function add(\GMP $augend, \GMP $addend): \GMP
    {
        return \gmp_add($augend, $addend);
    }

    public static function sub(\GMP $minuend, \GMP $subtrahend): \GMP
    {
        return \gmp_sub($minuend, $subtrahend);
    }

    public static function mul(\GMP $multiplier, \GMP $multiplicand): \GMP
    {
        return \gmp_mul($multiplier, $multiplicand);
    }

    public static function pow(\GMP $base, int $exponent): \GMP
    {
        return \gmp_pow($base, $exponent);
    }

    public static function bitwiseAnd(\GMP $first, \GMP $other): \GMP
    {
        return \gmp_and($first, $other);
    }

    public static function bitwiseXor(\GMP $first, \GMP $other): \GMP
    {
        return \gmp_xor($first, $other);
    }

    public static function toString(\GMP $value): string
    {
        return \gmp_strval($value);
    }

    public static function inverseMod(\GMP $a, \GMP $m): \GMP
    {
        return \gmp_invert($a, $m);
    }

    public static function baseConvert(string $number, int $from, int $to): string
    {
        return \gmp_strval(\gmp_init($number, $from), $to);
    }

    public static function rightShift(\GMP $number, int $positions): \GMP
    {
        return \gmp_div($number, \gmp_pow(\gmp_init(2, 10), $positions));
    }

    public static function stringToInt(string $s): \GMP
    {
        $result = \gmp_init(0, 10);
        $sLen = \mb_strlen($s, '8bit');

        for ($c = 0; $c < $sLen; ++$c) {
            $result = \gmp_add(\gmp_mul(256, $result), \gmp_init(\ord($s[$c]), 10));
        }

        return $result;
    }
}