<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jpcarzolio &#187; LCG</title>
	<atom:link href="http://jpcarzolio.com/tag/lcg/feed/" rel="self" type="application/rss+xml" />
	<link>http://jpcarzolio.com</link>
	<description>Juan&#039;s personal website and blog</description>
	<lastBuildDate>Wed, 25 Mar 2020 18:57:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Image &#8220;encryption&#8221;</title>
		<link>http://jpcarzolio.com/2015/image-encryption/</link>
		<comments>http://jpcarzolio.com/2015/image-encryption/#comments</comments>
		<pubDate>Fri, 14 Aug 2015 16:18:43 +0000</pubDate>
		<dc:creator><![CDATA[Juan]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[LCG]]></category>

		<guid isPermaLink="false">https://jpcarzolio.com/?p=158</guid>
		<description><![CDATA[Can images be encrypted? Well, of course &#8212; what kind of question is that? Any file &#8211;any bitstream&#8212; can be encrypted. But encrypting a file (image or otherwise) turns it into random-looking garbage. Therefore, in a sense, an encrypted image file would cease to be an image file at all (e.g. you wouldn&#8217;t be able...]]></description>
				<content:encoded><![CDATA[<p>Can images be encrypted? Well, of course &#8212; what kind of question is that? <em>Any</em> file &#8211;any <em>bitstream</em>&#8212; can be encrypted. But encrypting a file (image or otherwise) turns it into random-looking garbage. Therefore, in a sense, an encrypted image file would cease to be an image file at all (e.g. you wouldn&#8217;t be able to open it in Photoshop or Gimp, transcode it to a different format, or display it directly in a web browser). There may be image format extensions that allow encryption, but I never heard of them, and many applications wouldn&#8217;t support them.</p>
<p>But what about encrypting the image data itself, the pixels, rather than the file as a whole? That can certainly be done, but using a standard encryption algorithm would only make sense with an uncompressed format that stores raw data (like BMP), because otherwise the compression algorithm would likely perform poorly on the encrypted data, and in the case of lossy compression, it would mess it up. Also, depending on the cipher, the encrypted data (ciphertext) might be longer than the original (due to padding), and that would mess up the image or require a change in dimensions.</p>
<p>Since standard encryption algorithms cannot be used, alternative, <em>graphical</em> methods must be used instead. A quick google search for &#8220;image encryption&#8221; summons lots of results with (apparently) advanced stuff, but here I will discuss a very simple method I invented to &#8220;encrypt&#8221; images that has a few advantages. I write &#8220;encrypt&#8221;, in quotes, because this simple method is not cryptographically secure at all, but it may be used to prevent image hotlinking, or to make it hard for someone to steal (especially mass-steal) images from your server or app, etc. In fact, I came up with this technique while working on a <a href="http://apps.facebook.com/playmegaslots">game</a>, to protect my artwork, because a few images from other apps of mine had been stolen, and I was a little paranoid back then.</p>
<p>Perhaps the best term to describe this method is <em>scrambling</em> rather than encryption. The idea is very simple: slice the image into small squares and shuffle them, making it look like an unsolved picture puzzle.</p>
<p>The shuffling has to look random, but it must be deterministic and repeatable in order to be easily reversible. This can be achieved with any PRNG (pseudo-random number generator), like the stock ones provided in most platforms (e.g  <code>Math.random()</code> in JavaScript). The seed can be thought of as the &#8220;encryption&#8221; key: each seed number will yield a different block arrangement, and &#8220;decryption&#8221; is performed by reversing the shuffle process using the same seed.</p>
<p>There&#8217;s a problem, though: using stock <code>random()</code> functions will not work across platforms! Each language/platform may use a different algorithm &#8211;or different algorithm settings&#8211; and this may even happen within a single &#8220;platform&#8221;, as is the case with JavaScript in browsers (each one has a different <code>Math.random()</code> implementation).</p>
<p>The solution for this is simple: we just write our own PRNG! Since this method is not meant to be cryptographically secure, we can use a simple PRNG like an LCG (linear congruntial generator), which is really easy to code (it just involves a state variable, three carefully chosen constants, one multiplication, one addition, one modulus and one division). My Javascript version is:</p>
<p></p><pre class="crayon-plain-tag">var LCG = function(seed) {
  this.state = seed;
};
(function() {
  var m = 4294967296,
      a = 1664525,
      c = 1013904223;

  LCG.prototype.rand = function() {
    this.state = (this.state * a + c) % m;
    return this.state / m;
  }
})();</pre><p></p>
<p>One last thing to consider is block size: how small should the squares be? It is clear that, the smaller they are, the harder it will be to reconstruct the image without the key, and that is visually intuitive: if we just slice an image into four quadrants, it&#8217;s trivial to figure out their order in one glance, as the image is still perfectly clear. If we split each quadrant into another four quadrants, and so on, it quickly becomes much harder to guess what it is you are looking at. The extreme case would be to use 1&#215;1 blocks, effectively shuffling individual pixels into a big block of colourful noise.</p>
<p>As we shrink the blocks, however, two problems may arise. One is that the computational cost increases, but this may not be much of an issue, since it&#8217;ll probably be fast enough anyway. The real problem is image compression: the more the image looks like noise, the worse all algorithms will perform. This is because large solid color areas, repeating patterns or smooth transitions (which are features that different algorithms target or expect) will be destroyed.</p>
<p>I planned to use JPEG, and in that specific case, scrambling not only causes poor compression but may also introduce undesirable artifacts or noise in the image, particularly when low quality settings are used. Luckily, knowing a little about the internals of the JPEG algorithm allowed me to solve that problem: since JPEG slices the image in 8&#215;8 squares (or 16&#215;16, or 16&#215;8  in some cases) and processes those independently, minimum interference is achieved by using 8 or 16 as the scrambler&#8217;s block size.</p>
<p>The following images illustrate all this. Consider this source image:</p>
<p><a href="http://jpcarzolio.com/extra/demos/imgScrambler/landscape.jpg"><img src="http://jpcarzolio.com/extra/demos/imgScrambler/landscape.jpg"
alt="Our source image"
/></a></p>
<p>Its file size is 285.9 KB. Scrambling it with a block size of 4, we get this one:</p>
<p><a href="http://jpcarzolio.com/wp-content/uploads/2015/08/canvas4.jpg"><img src="http://jpcarzolio.com/wp-content/uploads/2015/08/canvas4.jpg" width="1024" height="640" class="alignnone size-full wp-image-159"
alt="After scrambling with block size 4" /></a></p>
<p>which is pretty scrambled indeed (you can&#8217;t tell what the original image looked like). But this image has a file size of 566.8 KB. Twice as big! Here we observe the scrambling interefering with compression. On the other hand, if we use a block size of 8 to minimize interference, we get:</p>
<p><a href="http://jpcarzolio.com/wp-content/uploads/2015/08/canvas8.jpg"><img src="http://jpcarzolio.com/wp-content/uploads/2015/08/canvas8.jpg" width="1024" height="640" class="alignnone size-full wp-image-160"
alt="After scrambling with block size 8" /></a></p>
<p>which is still pretty unintelligible, but has a file size of 286.2 KB. The size difference is now only just about 300 bytes!</p>
<p>You can try a <a href="http://jpcarzolio.com/extra/demos/imgScrambler/image_scrambler.html" target="_blank">demo</a>, or check my JavaScript <a href="https://github.com/jpcarzolio/img-scrambler" target="_blank">source code</a> on GitHub. I adapted it from a previous ActionScript version I had written for my game.</p>
]]></content:encoded>
			<wfw:commentRss>http://jpcarzolio.com/2015/image-encryption/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A promising new PRNG (Pseudo Random Number Generator)</title>
		<link>http://jpcarzolio.com/2015/a-promising-new-prng-pseudo-random-number-generator/</link>
		<comments>http://jpcarzolio.com/2015/a-promising-new-prng-pseudo-random-number-generator/#comments</comments>
		<pubDate>Wed, 05 Aug 2015 18:36:49 +0000</pubDate>
		<dc:creator><![CDATA[Juan]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[LCG]]></category>
		<category><![CDATA[PCG]]></category>
		<category><![CDATA[PRNGs]]></category>
		<category><![CDATA[randomness]]></category>

		<guid isPermaLink="false">https://jpcarzolio.com/?p=152</guid>
		<description><![CDATA[I&#8217;m no expert in the subject of PRNGs, but I find it quite interesting. Developers usually don&#8217;t give much thought to this and just use whatever rand(), random(), Math.random() function is available in their language of choice. And that&#8217;s fine for most purposes, but there are situations where (pseudo) randomness matters, and different algorithms may...]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m no expert in the subject of PRNGs, but I find it quite interesting. Developers usually don&#8217;t give much thought to this and just use whatever <code>rand()</code>, <code>random()</code>, <code>Math.random()</code> function is available in their language of choice. And that&#8217;s fine for most purposes, but there are situations where (pseudo) randomness matters, and different algorithms may have very different quality and properties. For instance, PHP users may have noticed that there are two functions to get a random number: <code>rand()</code> and <code>mt_rand()</code>. In this case, the latter, based on the Mersenne Twister algorithm, was introduced as a better alternative to the former (which uses the underlying system implementation).</p>
<p>While googling for different generators and their properties, I recently stumbled upon a website that describes a new algorithm and shows some comparisons between that novel approach and many other popular ones (including LCGs, Mersenne Twister, etc) on different quality metrics (statistical quality, prediction difficulty, time and space efficiency, and more). It&#8217;s called <a href="http://www.pcg-random.org/">PCG</a>, (Permuted Congruential Generator), and, judging by the comparisons shown there, it beats the crap out of most other generators!</p>
<p>What makes it so good? Well, as the site states, the PCG family combines properties not previously seen together in a single algorithm, including: small code size, low memory and CPU usage, powerful features (jump ahead, distance), low predictability, excellent performance in statistical tests, and a permissive open source license (the Apache license).</p>
<p>How does it achieve all that? Its power apparently comes from the combination of an LCG (Linear Congruential Generator) as its state-transition function (the function that dictates how the internal state changes every time a new number is returned) and a new technique called <em>permutation functions on tuples</em> as its output function (the one that turns the internal state into the actual random number returned).</p>
<p>LCG is one of the simplest and fastest algorithms known, and has some desirable features, but used on its own it has horrible statistical performance (among other drawbacks) . It seems that by combining it with a clever output function, though, an excellent generation scheme is achieved.</p>
<p>The <a href="http://www.pcg-random.org/">website</a> is full of interesting information and explanations, and you can download the full technical paper and the reference C/C++ implementations.</p>
]]></content:encoded>
			<wfw:commentRss>http://jpcarzolio.com/2015/a-promising-new-prng-pseudo-random-number-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
