|
6 | 6 | import copy
|
7 | 7 | from functools import cached_property
|
8 | 8 | from typing import TYPE_CHECKING
|
| 9 | +import numpy as np |
| 10 | +import math |
9 | 11 |
|
10 | 12 | if TYPE_CHECKING:
|
11 | 13 | from typing import Any, Optional
|
12 | 14 | from . import Attacker
|
13 |
| - from ..language import LanguageGraphAttackStep, Detector |
| 15 | + from ..language import LanguageGraphAttackStep |
14 | 16 | from ..model import ModelAsset
|
15 | 17 |
|
16 | 18 | class AttackGraphNode:
|
@@ -181,6 +183,101 @@ def is_available_defense(self) -> bool:
|
181 | 183 | 'suppress' not in self.tags and \
|
182 | 184 | self.defense_status != 1.0
|
183 | 185 |
|
| 186 | + def ttc_sample(self) -> float: |
| 187 | + """Sample a value from ttc distribution for a node |
| 188 | +
|
| 189 | + TTC Distributions in MAL: |
| 190 | + https://mal-lang.org/mal-langspec/apidocs/org.mal_lang.langspec/org/mal_lang/langspec/ttc/TtcDistribution.html |
| 191 | + """ |
| 192 | + |
| 193 | + def sample(exponential: float, bernoulli=1.0): |
| 194 | + """ |
| 195 | + Generate a random sample for the given distributions. |
| 196 | + If bernoulli distribution is not given, the sample will |
| 197 | + simply be from an exponential. |
| 198 | +
|
| 199 | + If the Bernoulli trial fails (0), return infinity (impossible). |
| 200 | + If the Bernoulli trial succeeds (1), return sample from |
| 201 | + exponential distribution. |
| 202 | + """ |
| 203 | + |
| 204 | + # If bernoulli is set to 1, the sample is just exponential |
| 205 | + if np.random.choice([0, 1], p=[1 - bernoulli, bernoulli]): |
| 206 | + return np.random.exponential(scale=1 / exponential) |
| 207 | + return math.inf |
| 208 | + |
| 209 | + if self.type == "defense": |
| 210 | + # Defenses have no ttc |
| 211 | + return 0 |
| 212 | + |
| 213 | + distribution = self.ttc.get('name') |
| 214 | + s = math.nan |
| 215 | + if distribution == "EasyAndCertain": |
| 216 | + s = sample(exponential=1) |
| 217 | + elif distribution == "EasyAndUncertain": |
| 218 | + s = sample(exponential=1, bernoulli=0.5) |
| 219 | + elif distribution == "HardAndCertain": |
| 220 | + s = sample(exponential=0.1) |
| 221 | + elif distribution == "HardAndUncertain": |
| 222 | + s = sample(exponential=0.1, bernoulli=0.5) |
| 223 | + elif distribution == "VeryHardAndCertain": |
| 224 | + s = sample(exponential=0.01) |
| 225 | + elif distribution == "VeryHardAndUncertain": |
| 226 | + s = sample(exponential=0.01, bernoulli=0.5) |
| 227 | + elif distribution == "Exponential": |
| 228 | + scale = float(self.ttc['arguments'][0]) |
| 229 | + s = sample(exponential=scale) |
| 230 | + else: |
| 231 | + raise ValueError(f"Unknown TTC distribution: {distribution}") |
| 232 | + |
| 233 | + return s |
| 234 | + |
| 235 | + def ttc_expected_value(self) -> float: |
| 236 | + """Returns the expected value of the ttc distribution for a node. |
| 237 | +
|
| 238 | + TTC Distributions in MAL: |
| 239 | + https://mal-lang.org/mal-langspec/apidocs/org.mal_lang.langspec/org/mal_lang/langspec/ttc/TtcDistribution.html |
| 240 | + """ |
| 241 | + |
| 242 | + def expected_value(exponential: float, bernoulli=1.0): |
| 243 | + """Compute expected value for given distributions. |
| 244 | +
|
| 245 | + If bernoulli distribution is 0, the expected value is infinite. |
| 246 | + Otherwise, the expected value is the expected value of the |
| 247 | + exponential distribution divided by the probability of the |
| 248 | + bernoulli distribution. |
| 249 | + """ |
| 250 | + if bernoulli == 0: |
| 251 | + # If Bernoulli always blocks, expectation is infinite |
| 252 | + return math.inf |
| 253 | + |
| 254 | + # Conditional expectation |
| 255 | + return (1 / exponential) / bernoulli |
| 256 | + |
| 257 | + if self.type == "defense": |
| 258 | + # Defenses have no ttc |
| 259 | + return 0 |
| 260 | + |
| 261 | + distribution = self.ttc["name"] |
| 262 | + e = math.nan |
| 263 | + if distribution == "EasyAndCertain": |
| 264 | + e = expected_value(exponential=1.0) |
| 265 | + elif distribution == "EasyAndUncertain": |
| 266 | + e = expected_value(exponential=1.0, bernoulli=0.5) |
| 267 | + elif distribution == "HardAndCertain": |
| 268 | + e = expected_value(exponential=0.1) |
| 269 | + elif distribution == "HardAndUncertain": |
| 270 | + e = expected_value(exponential=0.1, bernoulli=0.5) |
| 271 | + elif distribution == "VeryHardAndCertain": |
| 272 | + e = expected_value(exponential=0.01) |
| 273 | + elif distribution == "VeryHardAndUncertain": |
| 274 | + e = expected_value(exponential=0.01, bernoulli=0.5) |
| 275 | + elif distribution == "Exponential": |
| 276 | + scale = float(self.ttc["arguments"][0]) |
| 277 | + e = expected_value(exponential=scale) |
| 278 | + else: |
| 279 | + raise ValueError(f"Unknown TTC distribution: {distribution}") |
| 280 | + return e |
184 | 281 |
|
185 | 282 | @property
|
186 | 283 | def full_name(self) -> str:
|
|
0 commit comments