Sunday 29 December 2013

Gardyloo!

Not much this time, but I wish to have this video here (on science and philosophy).


...also let's put some messages into big numbers...

n=java.math.BigInteger(vec2mat(dec2bin(double('Abibliophobia'),7),1)',2)

char(bin2dec(vec2mat(char(n.toString(2)),7))')


n =

1272217067541950449850037473

ans =

Abibliophobia

--

...decode code in octave (as opposed to matlab)...


r1 = javaObject('java.math.BigInteger', '1272217067541950449850037473')
r2 = javaMethod('toString', r1, 2)
r3 = char(r2)
r4 = vec2mat(r3, 7)
r5 = bin2dec(r4)
r6 = transpose(r5)
r7 = char(r6)

r7 = Abibliophobia


--

a = javaObject('java.math.BigInteger', '1272217067541950449850037473')
b = javaMethod('toString', a, 2)
char(bin2dec(reshape(b, 7, length(b)/7)')')

--

http://www.parentherald.com/articles/3478/20131230/alcohol-leaves-its-mark-on-youngsters-dna.htm

Sunday 22 December 2013

Though you are not sentient and do not comprehend, I nonetheless consider you a true and valued friend.

...some quick notes, mostly for myself. I've been calculating massive amounts of cross correlation at work recently and the CPU-time needed for that is significant so I was looking into using the GPU instead. I found that GeForce GTX 670 does what I need about 5 times faster than i7-2600K (though utilization of the cores was poor in this code, but nice finger exercise none the less).

Small update: I managed to get a speedup of more than 8-10 using GPU with large number of repetitions.
More update: Using single precision floating point operations gives 2x 240 MS/s with the GPU.


% This program compares the difference in speed between calculating
% a cross correlation of 2x8 MS vectors using CPU and GPU
% on i7-2600K @ 3.4 GHz with 8 GB of RAM vs. GeForce GTX 670 
% the speedup is around 4.9, cross correlation can be efficiently 
% calculated through the use of fast fourier transform.
% If only cross-spectrum is of interest then inverse fft is
% unnecessary and some time can be saved.

clear all;
a = randn(2^23, 1);
b = randn(2^23, 1);

tic;
A = fft(a);
B = fft(b);
C = ifft(times(conj(A), B), 'symmetric'); % xcorr of CH1 and CH2
aa = dot(a, a); % zero delay autocorr of CH1
bb = dot(b, b); % zero delay autocorr of CH2
timecpu = toc

tic;
a = gpuArray(a);
b = gpuArray(b);
A = fft(a);
B = fft(b);
C = ifft(times(conj(A), B), 'symmetric');
aa = dot(a, a);
bb = dot(b, b);
C = gather(C);
aa = gather(aa);
bb = gather(bb);
timegpu = toc

speed = timecpu/timegpu

---------------


N=21;
n=100;
n1 = randn(2^N, 1);
n2 = randn(2^N, 1);
tic;
for z=1:n
    a = gpuArray(n1);
    b = gpuArray(n2);
    A = fft(a);
    B = fft(b);
    C = ifft(times(conj(A), B), 'symmetric');
    C = gather(C); % if you don't gather -> 96 MSPS (x2)
end
timegpu = toc
speed = n*2^N/timegpu/1e6


timegpu =

    2.8006


speed =

   74.8825



2x 75 MS/s, if you don't gather (for example just average on the GPU) 2x 96 MS/s, 2x240 MS/s with single precision floating point instructions ... randn(2^N, 1, 'single').

http://en.wikipedia.org/wiki/Cross_correlation

--------------------


Intel Core i7-2600K (4x 3.4 GHz)
GeForce GTX 670 (1344 CUDA-cores, 915 MHz, 6 GHz memory)

Averaging performance including data transfer overheads :

CPU single precision dot-product (1-point xcorr) 2x 600 MS/s
CPU double precision dot-product (1-point xcorr) 2x 300 MS/s
CPU single precision cross-correlation 2x  23 MS/s
CPU double precision cross-correlation 2x  13 MS/s

GPU single precision dot-product (1-point xcorr) 2x 433 MS/s
GPU double precision dot-product (1-point xcorr) 2x 227 MS/s
GPU single precision cross-correlation 2x 225 MS/s
GPU double precision cross-correlation 2x  90 MS/s


--------------------

In the spirit of the Hobbit...
Some fiddling with matlab again. I wasn't aware that java could be integrated this way into matlab, but nice to know...

% Simple RSA cryptosystem testing in matlab

one = java.math.BigInteger.ONE;
p = one.probablePrime(256, java.util.Random);
q = one.probablePrime(256, java.util.Random);


n = p.multiply(q); % first part of the public key (n, e)
tmp0 = p.subtract(one);
tmp1 = q.subtract(one);
totient = tmp0.multiply(tmp1);
tmp0 = java.math.BigInteger('2');
tmp1 = totient.divide(tmp0);
e = tmp1.nextProbablePrime(); % second part of the public key (n, e)
d = e.modInverse(totient); % the private key

m = java.math.BigInteger('123321243425345'); % message

c = m.modPow(e, n) % encryption
m = c.modPow(d, n) % decryption

http://en.wikipedia.org/wiki/RSA_(algorithm)

--------------------


% Fermat primality test

one = java.math.BigInteger.ONE;
p = java.math.BigInteger('221');
n = p.subtract(java.math.BigInteger.ONE);

a = one.nextProbablePrime();
a.modPow(n, p) % a^n mod p

a = a.nextProbablePrime();
a.modPow(n, p) % this needs to be always one if p is prime
% ...

http://en.wikipedia.org/wiki/Fermat_primality_test
http://en.wikipedia.org/wiki/AKS_primality_test

http://en.memory-alpha.org/wiki/Ode_to_Spot

-----------------

Efficient modpow algorithm for large numbers...

a = java.math.BigInteger('2988348162058574136915891421498819466320163312926952423791023078876139');
b = java.math.BigInteger('2351399303373464486466122544523690094744975233415544072992656881240319');
m = java.math.BigInteger('87814412832655818143772433337418883492663173730772486450699007318453048545183');

ZERO = java.math.BigInteger.ZERO;
ONE = java.math.BigInteger.ONE;
s = ONE;
t = a;
u = b;

while u.equals(ZERO) == 0
    if u.and(ONE).equals(ONE) == 1
        s = s.multiply(t).mod(m);
    end
    u = u.shiftRight(1)
    t = t.multiply(t).mod(m);
end

a.modPow(b, m)
s

Monday 16 December 2013

When a man cannot choose, he ceases to be a man.

So, the hidden messages... basically single lines of code in matlab...

The first one you might recognize by the artifacts at the four corners of the image, they suggest that something funny is going on in the frequency space. In other words the classic way to find the hidden message is to perform a 2D fourier transform on the image. Perhaps the simplest way to reveal the message is to use a gimp plugin (for example G'MIC). You can also make these images using G'MIC and simply painting your hidden messages and then inverse transforming them. However, I did mine in matlab. In this case I transform the red color component.

pic1 = imread('d:\babel\20131215\me.jpg');
imagesc(log10(abs(fft2(pic1(:, :, 1)))), [0 5]);
truesize;





The image suffers a little bit from jpg-compression, but no worries. The logarithmic scaling is unimportant, you see it just fine even with gimps default settings. You could use the same idea to encode messages into mp3s for example and they would be revealed in their spectrogram (4:10 is interesting in this video).


Perhaps the most famous was the face used by Aphex Twin at the end of one of their tracks (at 5:30).


Here's how I did my picture:

pic = imread('d:\babel\20131209\me.png');
MSG = imread('d:\babel\20131209\msg.png');
MSG = imresize(MSG, [406 640]);
R = fft2(pic(:, :, 1));
R = R + 15*double(MSG);
r = ifft2(R, 'symmetric');
pic(:, :, 1) = abs(r);
imwrite(pic, 'd:\babel\20131209\me.jpg', 'JPG');

You could do something similar even for a video.

The second image is a bit more interesting because no visible signs can be seen and it would be very difficult to recognize anything but noise even if you could compare it to the original unaltered image. Yet the method is fairly simple. The least significant bit of every pixel is part of 8-bit ASCII encoded message (notice how the image is encoded using a bit perfect PNG-compression unlike the first image).

pic2 = imread('d:\babel\20131215\cat.png');
sprintf(char(bin2dec(vec2mat(sprintf('%d', double(mod(pic2(1:1702*8), 2))), 8))))


ans =

You ask me...

Don't pay too much attention to the trickery in the code. There are numerous ways to do it all equally well. You could even encode the message into a lesser number of bits like 7 as ASCII is often encoded in or you could even make your own character sets or include further tricks, however I figured that doing so would make it unnecessarily hard. 

Through my living room window one early morning.
And here's the code to make the image with the message:

message = double('You ask me for a hamburger. My attempt to reciprocate is cut brutally short as my body experiences a sudden lack of electrons. Across a variety of hidden dimensions you are dismayed. John Lennon hands me an apple, but it slips through my fingers. I am reborn as an ocelot. You disapprove. A crack echoes through the universe in defiance of conventional physics as cosmological background noise shifts from randomness to a perfect A Flat. Children everywhere stop what they are doing and hum along in perfect pitch with the background radiation. Birds fall from the sky as the sun engulfs the earth. You hesitate momentarily before allowing yourself to assume the locus of all knowledge. Entropy crumbles as you peruse the information contained within the universe. A small library in Phoenix ceases to exist. You stumble under the weight of everythingness, Your mouth opens up to cry out, and collapses around your body before blinking you out of the spatial plane. You exist only within the fourth dimension. The fountainhead of all knowledge rolls along the ground and collides with a small dog. My head tastes sideways as spacetime is reestablished, you blink back into the corporeal world disoriented, only for me to hand you a hamburger as my body collapses under the strain of reconstitution. The universe has reasserted itself. A particular small dog is fed steak for the rest of its natural life. You die in a freak accident moments later, and you soul works at the returns desk for the Phoenix library. You disapprove. Your disapproval sends ripples through the inter-dimensional void between life and death. A small child begins to cry as he walks toward the stairway where his father stands.');

message_in_bin = dec2bin(message, 8);
A = imread('d:\babel\20131209\org_cat.png');
ss = size(message_in_bin);
message = message_in_bin';
for x = 1:ss(1)*ss(2)
    value = bin2dec(message(x));
    if mod(A(x), 2) ~= value
        if A(x) > 100
            A(x) = A(x) - 1;
        else
            A(x) = A(x) + 1;
        end
    end
end
imwrite(A, 'd:\babel\20131209\cat.png', 'PNG');

http://en.wikipedia.org/wiki/Steganography

Monday 9 December 2013

Nothing is easy, nothing is pure. Yet we must go on.

“You will be required to do wrong no matter where you go. It is the basic condition of life, to be required to violate your own identity. At some time, every creature which lives must do so. It is the ultimate shadow, the defeat of creation; this is the curse at work, the curse that feeds on all life. Everywhere in the universe.”
...Do Androids Dream of Electric Sheep?




The two pictures above have hidden messages. Can you find them? It's not hard at all, but without a bit of background it might be tricky. In fact, I believe many of my readers have the necessary background in principle, but will anyone do it?-) And for some, even the time would be right. I'll even give you useless hints, Disney has something to do with the first one and the second one is a story. Matlab helps.

Functions are just names for algorithms which paint a map and sometimes (if not always) two different functions paint maps of the same object, only with a slight twist.


Fucking magnets, How do they work? ...quite simple in fact.


Perhaps I want nothing. Perhaps I just wanted to know what you thought about it.


At least 5e120 calculations have been performed by the universe since the big bang. About 100 billion humans have walked on this planet since the beginning of time. The number of multiverses has been estimated to be around 10^(1e16).