Sunday, October 30, 2022

A Bit of Algebra

In this blog post we're going to look at finding the real positive x such that: \[x^{x^3} = \frac{1}{2^{1/6}} \hspace{10 mm} (Equation 1)\]
By all means, do have a go at solving it, before reading our solution below.


The first, slightly non-obvious step is to raise both sides of the equation to the power of 3: \[ \left( x^{x^3} \right)^3 = \left( \frac{1}{2^{1/6}} \right)^3 \] Rearranging that a bit, we have: \[ x^{3x^3} = \frac{1}{2^{3/6}} \] And so: \[ \left( x^3 \right) ^{x^3} = \left( \frac{1}{2} \right) ^ {1/2} \hspace{10 mm} (Equation 2) \] If \[ a^a = b^b \] then we have a solution: \[a=b\]
So, returning to Equation 2, we have: \[x^3=1/2 \] And hence: \[x=\frac{1}{2^{1/3}} \] So, we have a solution to equation 1. But are there any others?
In fact, it is possible have \[ a \neq b \] when \[ a^a = b^b \] For example: \[ \left( \frac{1}{4} \right)^{1/4} = \left( \frac{1}{2} \right)^{1/2} \] So, returning to equation 2, we can see that another solution for x is when: \[ x^3 = \frac{1}{4} \] And hence: \[ x = \frac{1}{2^{2/3}} \] So, our solutions for x are: \[ \frac{1}{2^{1/3}} \hspace{4 mm} and \hspace{4 mm} \frac{1}{2^{2/3}} \] But are there other solutions?
If we let \[ z=x^3 \] then, returning to equation 2 we have: \[ z^z= \frac{1} {\sqrt{2}} \hspace{10 mm} \] We can plot \(z^z \) for positive z: We can see that \(z^z\) decreases in the range 0 to 1/e, after that it increases and keeps on increasing. So for positive z, we will have a maximum of two solutions to \( z^z = k\), where k is some constant. We have two solutions already, so we now know there aren't any more,
unless of course we want to investigate negative or complex solutions...

Monday, October 3, 2022

Catching Raindrops

Suppose we know the area of a country and the average annual rainfall. And then suppose that we also know the population and the daily water consumption, then we can work out what proportion of drops of rain that fall need to be captured for use by people.
We convert the daily consuption to annual and then compare the volume of water that is consumed with the volume of water that falls as rain.
Inputs
Population people
Average Water Consumption litres per person per day
Land Area square kilometers
Rainfall meters per year
Result
Proportion of raindrops to catch %
(If you change one of the inputs above and hit enter, then the result will be updated.)

Singapore has approximately a population of 5.9 million people, they use 141 litres of water per person per day. The land area is small: 729 sq km, their annual rainfall is about 2.2 meters. So if they captured 19% of the raindrops then they'd meet their water needs.
On the other hand, Ireland has a slightly smaller population but much larger land area and so they only need capture a much lower proportion of their raindrops.

Sunday, August 21, 2022

Pumped Storage Power Calculations

Suppose a country had some good sources of intermittent renewable energy, perhaps tidal which is nice and predicatable or wind which is much less foreseable. Either way, one approach to achieve a consistant supply would be to store the energy produced and then release it on demand. A pumped storage power station may be appropriate. It consists of pairs of lakes which are relatively close by eachother, though there is a significant altitude difference between the lakes. One or both of the lakes may be man-made. When there is excess supply of electricity, water is pumped from the low lake to the high lake. On the other hand, when there is excess demand, water flows from the high lake to the low lake, passing through a generator and so it produces electricity.



Now, let's look into how much energy could be stored.
The potential energy stored is Mgh:
where g is the acceleration due to gravity.
h is the height of the high lake above the low lake.
M is the mass of water, which is evaluated by multiplying the volume of one lake times the density of fresh water (1,000 kg per cubic meter).
We'll also introduce an efficiency factor because the system won't be perfectly efficient.
Inputs
Population people
Average Power Per Person Watts
Lake Radius (r) meters
Lake Depth (d) meters
Altitude Difference (h) meters
Efficiency %
Number of Lake Pairs
Results
Annual Energy Consumption TW hours / year
Energy Stored TW hours
Days of Energy Stored days.
(If you change one of the inputs above and hit enter, then the results will be updated.)

If we were to use tidal power, which goes through almost 2 complete cycles per day, then we may need about half a day's worth of demand to be stored, which may be achievable with a series of pumped storage power stations. On the other hand, if we were to try to rely on wind as the sole energy source and we wanted to store enough energy to see us through say six weeks of calm weather, then the number of pumped storage power stations required is likely to be unacceptably large.

Now suppose we choose to harness tidal power. One approach would be to put a sea-wall across the entrance to some bays or inlets. Let the water flow in as the tide rises. The sea water is then blocked as the tide starts to ebb. To extract a maximum amount of energy from this, we would wait until the tide at its lowest point and then instantaneously release all the sea water, passing it through a generator. But it is likely to be much more practical to release the water before low-tide. Also if we want to use much of the electricity as it is being used, then we would want the generation of electricity to continue through much of the tidal cycle.

We can set a target of what proportion of our energy needs we want to come from tidal and then we can work out what area of the sea needs to be put behind sea walls with tidal generators.
In a 27 day lunar cycle we have 2 tides per day due to the spin of the earth, less 2 tides due to orbit of the moon. For now we won't go into the details of why that's true. But our statement here is that we have 2 * ( 27 - 1 ) tides in 27 days, i.e. 1.93 tides per day. So a tidal cycle is (24 / 1.93) hours = 12.44 hours. But we can generate power both as the tide flows in and as the tide flows out. So we'll use the half tidal period which is a little over 6 hours.

The target amount of energy to generate in a half tidal cycle is:
population * powerPerPerson * durationOfHalfTidalCycle * targetTidalPowerProportion.
On the other hand we see how much energy is available from the tidal movement using the equation:
Potential Energy: E= Mgh
In this case M is the mass of the sea water which is the area times the tidal height times the density of sea water (approximately 1030 kg per cubic meter).

So we can evaluate the area of sea that is required:
Inputs
Tidal Height meters
Tidal Power Efficiency %
Proportion of Total Energy from Tidal %
Result
Sea Area Required square KM
(Once again, if you change the inputs and hit enter, then the result will update.)

Tidal power is something that can provide predictable renewable energy. However, a significan area of sea needs to be harnessed.

Tuesday, June 21, 2022

Sha256 Hashing Algorithm as a Directed Graph

The quick and easy part of bitcoin mining is checking the digital signatures of those spending their coins and then confirming that there hasn't been a double spend. Then comes the slow part. Miners guess a value for a number known as a nuance and then take the (Sha256) Hash of the verified transactions and the nuance. If the result is less than the currently specified threshold, then the miner can announce his block to the community. The vast majority of the nuances guessed lead to a hash that is greater than then threshold and so are rejected.
Miners seek a number called a nuance such that
\[Hash( transactions, nuance) < threshold \]
When the nuance is found, it represents proof that the mining community has done a lot of work. This approach is known as a proof of work algorithm.
Well, suppose someone were to invert the Hash function, they would start with say (threshold - 1) and then work out the nuance that would achieve that. So they could mine bitcoin without doing the work. Bitcoin would be unable to continue using the proof of work algorithm and would very likely collapse. For bitcoin to survive it is important that no one known has to invert the hash function.
The Hash function that bitcoin uses is called Sha256. (For details look at the wikipedia article). For the rest of this blog post, I'm going to focus on the part of the algorithm where we start with 512 bits, they are processed through 16 preliminary iterations and then a further 48 standard iterations, at the end of which we have 256 output bits. The algorithm has the following attributes:
  • It is not a secret, anyone who wants can look at the code or indeed write their own code to implement it.
  • It is completely deterministic and repeatable. It gives the same answer every time.
  • There are no 'if' statements.
One of the operations carried out is integer addition. The inputs are put into groups of 32 bits and then the 32 bits are treated as an integer. Pairs of integers are added. If we go down a level from the integer to the bits that make it up, we can implement the integer addition using the AND operator and an XOR operator on pairs of bits.
The rest of the algorithm can be implemented using:
NOT operator, which takes one bit as an input and has one output bit.
XOR operator, which takes two bits as input and has one output bit.
AND operator, which takes two bits as input and has one output bit.
So the entire algorithm can be represented as a directed graph going from the 512 input nodes to the 256 output nodes. Every node represents one bit. The inputs bits are set and then after that every other node is assigned a value of either 0 or 1. A node is assigned a value just once.
If a NOT operator was used to assign the value, then it has one precedent.
On the other hand if an AND or XOR operator is used then it will have 2 precedents.
However nodes can have any number of dependents. The graph is strictly directed. There are no circular loops.
So now we might ask the question: how might someone invert this algorithm. Suppose the output was fixed or indeed constrained, how could we find appropriate inputs?
Well, lets start with the NOT operator.
Input A NOT A
1 0
0 1
This is very straight forward to invert. Indeed if you apply the NOT operator to the output, then you'll recover the input.

Now lets move on to the XOR operator. It is also known as the 'exclusive or'. A XOR B is A or B but not both.
Input A Input B A XOR B
1 1 0
1 0 1
0 1 1
0 0 0
Suppose: A XOR B = 0
that means that A and B are the same. They could both be 1 or both be 0. We just know that they are not different. So if we are traversing our graph from the outputs to the intputs, (i.e. from right to left in the image above) then A XOR B = 0 imposes the simple relationship between A and B that A=B.
On the other hand: A XOR B = 1
tells us that A and B are different. So if we go from right to left across our graph then that condition needs to be maintained.
So the XOR output will tell us if the inputs are the same or different.

The final operator we need to deal with is the AND operator.
Input A Input B A AND B
1 1 1
1 0 0
0 1 0
0 0 0
When A AND B = 1,
that implies that both A and B are 1 and so if we have that condition, then we can take a step back through the graph and set the two inputs to 1.
A more difficult case to deal with is: A AND B = 0
In that case either A or B could be 1, but not at the same time.
So that relationship between A and B is (a little bit) complicated.
The Sha256 algorithm consists of 64 iterations ( 16 preliminary, followed by 48 standard). However on each iteration the bits pass through numerous NOT, XOR and AND operators. In my implemenetation in Java (which is available in github) I had:
NOT operator called 117,248 times.
XOR operator called 58,880 times.
AND operator called 106,240 times.
There are a number of ways those numbers could be reduced. For example we could use an OR operator. But if someone was trying to come up with an algorithm to invert the Sha256, then introducing new operators may make it more complicated. Indeed we could replace the XOR with 5 calls to an AND so then we just have lots of AND's and NOT's to invert.
To successfully invert the Sha256, the run time should not grow exponentially with the number of operators N. Something linear in N would be great. Something quadratic in N may be doable.

One method that might be tried would be a probabilistic approach, assigning each node a value between 0 and 1 (inclusive).
For example if we had: A AND B = 0
then we might set A =1/3 and B=1/3.
Reflecting the fact that they are both more likely to be zero than 1.
We could attempt to traverse the graph from right to left and back again a number of times until we had a stable solution. Perhaps a simulated anealing method could be tried. But the problem is that the values in the graph are not stable. One flipped input bit has a huge cascade effect through the graph. It would be very difficult to get a probabalistic method to work because of that instability.

A very different way of trying to reverse it would be to bring the constraints on the nodes through the graph from the outputs back to the inputs. For example if an output needs to be 0 and it is assigned using an AND operator then we know that the two inputs cannot both be 1. We could then pass that constraint back to their precedents.
However in this case if we are not clever then the constraints would grow exponentially as we go back through the graph. If a node has some contraints, then they are passed on to its two precedents, in many cases each of which will have 2 precedents, which in turn may have 2 precedents. Typically to pass from the outputs to the inputs, bits need to pass through 200 AND operators. \(2^{200}\) is a very big number. Someone would need to be very smart in how they compress the constraints to prevent the exponential explosion.

If we observe the Sha256 calculator as it goes from the input to the outputs we find we just need 512 currently live nodes (bits). After a while the inputs are no longer needed. Later, after the intermediate nodes are used, they too are no longer needed. 512 is a comparatively small number. Similarly, someone going back through the graph (from the outputs to the inputs) just needs to keep track of 512 bits at any given point in time. That's not a lot of memory. How difficult could it be to invert the Sha256?
Well, so far no-one has been able to work out how to do it...