jewbacca
Joined 16 years ago
Joined 16 years ago
Instead of each operation taking a variable number of numbers and then either returning a value or directly changing the state of the stack (which was also a bug in my previous gist, in pushing the result of C and AC): homogenize the types of the operations by making all of them take a stack and return a stack.
You could also wrap all of the basic arithmetic functions with a higher-level function so that, eg, `add` would still not actually have to be aware of the stack:
import math, operator
def stackFunction(arity, function):
return lambda stack: stack[:-arity] + [function(*stack[-arity:])]
class rpn_engine:
def __init__(self):
self.stack = []
self.catalog = {"+": stackFunction(2, operator.add),
"-": stackFunction(2, operator.sub),
"*": stackFunction(2, operator.mul),
"/": stackFunction(2, operator.truediv),
"^2": stackFunction(1, lambda x: x * x),
"SQRT": stackFunction(1, math.sqrt),
"C": lambda stack: stack[:-1],
"AC": lambda stack: []}
def compute(self, operation):
self.stack = self.catalog[operation](self.stack)
def push(self, number):
self.stack.append(number)That can be taken further and generalized out.
It's been a while since I did much Python metaprogramming-navel-gazing, but if you can't do something to automatically detect the arity of the operations' lambdas/functions, you can at least explicitly annotate the operations data with their arity -- which is already implicitly being done (as code instead of data) in `compute`'s 3 if-statements + the 3 "compute_with_N_operands" functions.
From there, you'll only have 1 case once, instead of 3 cases with each one manifesting in 2 different places. And just about half of the code disappears.
----
edit: since adjkant already covered the "automatically detect the arity of the operations' lambdas/functions" approach to removing all the cases, (https://news.ycombinator.com/item?id=17329772), here is the gist of what I meant by "explicitly annotate the operations data with their arity":
import math, operator
class rpn_engine:
def __init__(self):
self.stack = []
self.catalog = {"+": (2, operator.add),
"-": (2, operator.sub),
"*": (2, operator.mul),
"/": (2, operator.truediv),
"^2": (1, lambda x: x * x),
"SQRT": (1, math.sqrt),
"C": (0, self.stack.pop),
"AC": (0, self.stack.clear)}
def compute(self, operation):
(arity, op) = self.catalog[operation]
operands = reversed([self.stack.pop() for _ in range(arity)])
return self.stack.push(op(*operands))But you can still control access to the content, distributing the password by other channels.
A plausible use case for this (... like basically any crypto thing) would be clandestine organizing. Significantly less sophistication required, and much less of a trail left, to put a static site online.
Shit, you wouldn't even need to host it as a site: you could drop it somewhere as text, eg on a pastebin or in a forum comment, with "save as .html and open" instructions. And you could distribute the encryptor itself the same way.
The really great thing about this is that you could do practical human crypto, without Alice or Bob needing any special knowledge or equipment besides a web browser, using arbitrary uncontrolled public infrastructure.
... though if someone knows the password + controls the infrastructure you've used, they could substitute their own content.
... also short password + direct access to ciphertext -> easy brute force.
... also everything here: https://news.ycombinator.com/item?id=14554187
Wouldn't use it for anything state-level or life-or death. There are mitigations, but that would increase the difficulty and necessary sophistication for using it. Actually, this probably falls into the uncanny valley where it seems just accessibly cyberpunk enough to be extremely dangerous to anyone relying on it. But still, really cool IMO.
Thank god I haven't made over 1000 comments or posts with any one account.
The data's all still in the database, but due to their caching setup, only the last 1000 of anything is publicly indexed. While everything's technically reachable, it's all deep web. To recover something private like upvoted or saved posts, we're talking heat-death-of-the-universe, through a full-table-scan squeezed through brute-forcing a search box, while authenticated, with rate limits.
I recently discovered that, on Reddit, anything beyond your more recent 1000 posts/comments/upvotes is totally irrecoverable to you, even via scraping.
I'm not sure this is a bad idea...
----
"The non linear effects of leaks on unjust systems of governance
[...]
The more secretive or unjust an organization is, the more leaks induce fear and paranoia in its leadership and planning coterie. This must result in minimization of efficient internal communications mechanisms (an increase in cognitive "secrecy tax") and consequent system-wide cognitive decline resulting in decreased ability to hold onto power as the environment demands adaption."
https://web.archive.org/web/20071020051936id_/http://iq.org:...
----
"The non linear effects of leaks on unjust systems of governance
[...]
The more secretive or unjust an organization is, the more leaks induce fear and paranoia in its leadership and planning coterie. This must result in minimization of efficient internal communications mechanisms (an increase in cognitive "secrecy tax") and consequent system-wide cognitive decline resulting in decreased ability to hold onto power as the environment demands adaption."
https://web.archive.org/web/20071020051936id_/http://iq.org:...
https://cryptome.org/0002/ja-conspiracies.pdf
----
I still believe that this will bear out in the long-run, and has already begun to bear out in smaller-scale cases, but it is becoming apparent that in many cases this is like a "the market can stay irrational longer than you can stay solvent" situation, and has really barely begun.
As for Assange personally, I still kinda believe in him, though I recognize that is on shakier ground than it may once have been. After 8 uninterrupted years of however an honest and empathetic person might describe his conditions, I would not be surprised, nor would I particularly fault him for losing it a bit, and letting his personal situation (... being fucking personally targeted for destruction by an unfathomably powerful global-scale system of secretive injustice) compromise his personal pretence towards the objective neutrality on which his thesis's large-scale fruition is predicated.