1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait implementation of the total algorithmic cost via complex tuning per one computation for digestion algorithms. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\MessageDigestion; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Interfaces\MessageDigestion\ComplexAlgorithmicCostInterface as AlgorithmicCostSpecification; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait ComplexAlgorithmicCostTrait - Reusable implementation of `ComplexAlgorithmicCostInterface`. |
13
|
|
|
* |
14
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\ComplexAlgorithmicCostInterface The abstract specification. |
15
|
|
|
* |
16
|
|
|
* @package CryptoManana\Core\Traits\MessageDigestion |
17
|
|
|
* |
18
|
|
|
* @property int $memoryCost The digestion internal computational memory cost property storage. |
19
|
|
|
* @property int $timeCost The digestion internal computational time cost property storage. |
20
|
|
|
* @property int $threadsCost The digestion internal computational thread cost property storage. |
21
|
|
|
* |
22
|
|
|
* @mixin AlgorithmicCostSpecification |
23
|
|
|
* |
24
|
|
|
* @codeCoverageIgnore |
25
|
|
|
*/ |
26
|
|
|
trait ComplexAlgorithmicCostTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Setter for the memory cost property. |
30
|
|
|
* |
31
|
|
|
* @param int $cost The algorithmic memory cost. |
32
|
|
|
* |
33
|
|
|
* @return $this The hash algorithm object. |
34
|
|
|
* @throws \Exception Validation errors. |
35
|
|
|
*/ |
36
|
|
|
public function setMemoryCost($cost) |
37
|
|
|
{ |
38
|
|
|
$cost = filter_var( |
39
|
|
|
$cost, |
40
|
|
|
FILTER_VALIDATE_INT, |
41
|
|
|
[ |
42
|
|
|
"options" => [ |
43
|
|
|
"min_range" => static::MINIMUM_MEMORY_COST, |
|
|
|
|
44
|
|
|
"max_range" => static::MAXIMUM_MEMORY_COST, |
|
|
|
|
45
|
|
|
], |
46
|
|
|
] |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
if ($cost === false) { |
50
|
|
|
throw new \InvalidArgumentException( |
51
|
|
|
'The number of internal iterations must be a valid integer bigger than 0.' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->memoryCost = $cost; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Getter for the memory cost property. |
62
|
|
|
* |
63
|
|
|
* @return int The algorithmic memory cost. |
64
|
|
|
*/ |
65
|
|
|
public function getMemoryCost() |
66
|
|
|
{ |
67
|
|
|
return $this->memoryCost; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Setter for the time cost property. |
72
|
|
|
* |
73
|
|
|
* @param int $cost The algorithmic time cost. |
74
|
|
|
* |
75
|
|
|
* @return $this The hash algorithm object. |
76
|
|
|
* @throws \Exception Validation errors. |
77
|
|
|
*/ |
78
|
|
|
public function setTimeCost($cost) |
79
|
|
|
{ |
80
|
|
|
$cost = filter_var( |
81
|
|
|
$cost, |
82
|
|
|
FILTER_VALIDATE_INT, |
83
|
|
|
[ |
84
|
|
|
"options" => [ |
85
|
|
|
"min_range" => static::MINIMUM_TIME_COST, |
|
|
|
|
86
|
|
|
"max_range" => static::MAXIMUM_TIME_COST, |
|
|
|
|
87
|
|
|
], |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
if ($cost === false) { |
92
|
|
|
throw new \InvalidArgumentException( |
93
|
|
|
'The number of internal iterations must be a valid integer bigger than 0.' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->timeCost = $cost; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Getter for the time cost property. |
104
|
|
|
* |
105
|
|
|
* @return int The algorithmic time cost. |
106
|
|
|
*/ |
107
|
|
|
public function getTimeCost() |
108
|
|
|
{ |
109
|
|
|
return $this->timeCost; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Setter for the threads cost property. |
114
|
|
|
* |
115
|
|
|
* @param int $cost The algorithmic threads cost. |
116
|
|
|
* |
117
|
|
|
* @return $this The hash algorithm object. |
118
|
|
|
* @throws \Exception Validation errors. |
119
|
|
|
*/ |
120
|
|
|
public function setThreadsCost($cost) |
121
|
|
|
{ |
122
|
|
|
$cost = filter_var( |
123
|
|
|
$cost, |
124
|
|
|
FILTER_VALIDATE_INT, |
125
|
|
|
[ |
126
|
|
|
"options" => [ |
127
|
|
|
"min_range" => static::MINIMUM_THREADS_COST, |
|
|
|
|
128
|
|
|
"max_range" => static::MAXIMUM_THREADS_COST, |
|
|
|
|
129
|
|
|
], |
130
|
|
|
] |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
if ($cost === false) { |
134
|
|
|
throw new \InvalidArgumentException( |
135
|
|
|
'The number of internal iterations must be a valid integer bigger than 0.' |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->threadsCost = $cost; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Getter for the threads cost property. |
146
|
|
|
* |
147
|
|
|
* @return int The algorithmic threads cost. |
148
|
|
|
*/ |
149
|
|
|
public function getThreadsCost() |
150
|
|
|
{ |
151
|
|
|
return $this->threadsCost; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|