-
Notifications
You must be signed in to change notification settings - Fork 152
Refactor entropy.so.1 as C extension #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
EdwardLarson
merged 5 commits into
redballoonsecurity:master
from
EdwardLarson:refactor/entropy_c_extension
Dec 28, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b4d2527
refactor entropy.so.1 as a C extension that can be built with setupto…
Edward-Larson 211de6b
remove unneeded entropy makefile
Edward-Larson 29836c3
remove unneeded data_len arg, smarter warning log
Edward-Larson 6e19fe0
fix some typing stuff
Edward-Larson 355bcd9
apply changes suggested in review
Edward-Larson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import logging | ||
| import math | ||
| from typing import Callable, List, Optional | ||
|
|
||
|
|
||
| def entropy_py( | ||
| data: bytes, window_size: int, log_percent: Optional[Callable[[int], None]] = None | ||
| ) -> bytes: | ||
| """ | ||
| Return a list of entropy values where each value represents the Shannon entropy of the byte | ||
| value distribution over a fixed-size, sliding window. | ||
| """ | ||
| if log_percent is None: | ||
| log_percent = lambda x: None | ||
| else: | ||
| # Sort of hacky way to know we are being called from the tests and don't need to log this | ||
| logging.warning( | ||
| f"Using the Python implementation of the Shannon entropy calculation! This is potentially " | ||
| f"very slow, and is only used when the C extension cannot be built/found." | ||
| ) | ||
|
|
||
| # Create a histogram, and populate it with initial values | ||
| histogram = [0] * 256 | ||
| for b in data[:window_size]: | ||
| histogram[b] += 1 | ||
|
|
||
| # Calculate the entropy using a sliding window | ||
| entropy = [0] * (len(data) - window_size) | ||
| last_percent_logged = 0 | ||
| for i in range(len(entropy)): | ||
| entropy[i] = math.floor(255 * _shannon_entropy(histogram, window_size)) | ||
| histogram[data[i]] -= 1 | ||
| histogram[data[i + window_size]] += 1 | ||
| percent = int((i * 100) / len(data)) | ||
| if percent > last_percent_logged and percent % 10 == 0: | ||
| log_percent(percent) | ||
| last_percent_logged = percent | ||
| return bytes(entropy) | ||
|
|
||
|
|
||
| def _shannon_entropy(distribution: List[int], window_size: int) -> float: | ||
| """ | ||
| Return the Shannon entropy of the input probability distribution (represented as a histogram | ||
| counting byte occurrences over a window of known size). | ||
|
|
||
| Shannon entropy represents how uniform a probability distribution is. Since more uniform | ||
| implies less predictable (because the probability of any outcome is equally likely in a | ||
| uniform distribution), a sample with higher entropy is "more random" than one with lower | ||
| entropy. More here: <https://en.wikipedia.org/wiki/Entropy_(information_theory)>. | ||
| """ | ||
|
|
||
| result = 0.0 | ||
| for num_occurrences in distribution: | ||
| probability = num_occurrences / window_size | ||
| # Note that the zero check is required because the domain of log2 is the positive reals | ||
| result += probability * math.log2(probability) if probability != 0.0 else 0.0 | ||
| return -result / math.log2(window_size) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.