<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Inter res et verba: Blog</title><link>http://epestr.com/blog/</link><description/><language>en</language><generator>Hugo</generator><atom:link href="http://epestr.com/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Thu, 29 Oct 2026 20:53:00 +0530</lastBuildDate><item><title>Using my reMarkable as a drawing tab for my laptop</title><link>http://epestr.com/blog/using-my-remarkable-as-a-drawing-tab-for-my-laptop/</link><guid isPermaLink="true">http://epestr.com/blog/using-my-remarkable-as-a-drawing-tab-for-my-laptop/</guid><pubDate>Thu, 29 Oct 2026 20:53:00 +0530</pubDate><description>&lt;p>I had always wanted to use my &lt;a class="citation-link" href="https://remarkable.com/" title="reMarkable — paper tablets">reMarkable&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-0" aria-label="Reference 0">[0]&lt;/a>&lt;/sup> as a second monitor on Linux, but had always procrastinated given my other monitor. Recently I had another idea, to do the same with the pen working in &lt;a class="citation-link" href="https://www.gimp.org/" title="GIMP — GNU Image Manipulation Program">GIMP&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-1" aria-label="Reference 1">[1]&lt;/a>&lt;/sup>, that is use it as a touch-screen, like I used my iPad with a Mac some eight years back, also like it did with the native drawing client (xochitl /ksoχɪtl/?) so I can save my notes directly on my laptop when possible. Given that writing in its native editor felt almost immediate, and the connection would be over USB, I expected drawing on it to feel fairly similar so I began hacking.&lt;/p></description><content:encoded>&lt;p>I had always wanted to use my &lt;a class="citation-link" href="https://remarkable.com/" title="reMarkable — paper tablets">reMarkable&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-0" aria-label="Reference 0">[0]&lt;/a>&lt;/sup> as a second monitor on Linux, but had always procrastinated given my other monitor. Recently I had another idea, to do the same with the pen working in &lt;a class="citation-link" href="https://www.gimp.org/" title="GIMP — GNU Image Manipulation Program">GIMP&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-1" aria-label="Reference 1">[1]&lt;/a>&lt;/sup>, that is use it as a touch-screen, like I used my iPad with a Mac some eight years back, also like it did with the native drawing client (xochitl /ksoχɪtl/?) so I can save my notes directly on my laptop when possible. Given that writing in its native editor felt almost immediate, and the connection would be over USB, I expected drawing on it to feel fairly similar so I began hacking.&lt;/p>
&lt;h2 id="fixing-the-setup">Fixing the setup&lt;/h2>
&lt;p>I started with &lt;a class="citation-link" href="https://github.com/matteodelabre/vnsee/" title="VNSee — a VNC client for reMarkable">VNSee&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-2" aria-label="Reference 2">[2]&lt;/a>&lt;/sup>, which runs a &lt;a class="citation-link" href="https://www.rfc-editor.org/rfc/rfc6143" title="RFC 6143 — The Remote Framebuffer Protocol">VNC&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-3" aria-label="Reference 3">[3]&lt;/a>&lt;/sup> client on the tablet. On the laptop, &lt;a class="citation-link" href="https://github.com/LibVNC/x11vnc" title="x11vnc — VNC server for real X displays">x11vnc&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-4" aria-label="Reference 4">[4]&lt;/a>&lt;/sup> sends its contents of an extra X11 monitor, and pen input travels back as different mouse events.&lt;/p>
&lt;p>The existing &lt;a class="citation-link" href="https://github.com/ddvk/remarkable2-framebuffer" title="remarkable2-framebuffer">rm2fb&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-5" aria-label="Reference 5">[5]&lt;/a>&lt;/sup> setup did not work with my tablet&amp;rsquo;s firmware, so the client was adapted to use the native &lt;a class="citation-link" href="https://github.com/timower/rM2-stuff/tree/451577081b9eaa3d582e50bbca8c4adfd4911b53/libs/swtcon" title="SWTCON implementation in rM2-stuff">SWTCON implementation&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-6" aria-label="Reference 6">[6]&lt;/a>&lt;/sup> from &lt;a class="citation-link" href="https://github.com/timower/rM2-stuff" title="rM2-stuff">rM2-stuff&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-7" aria-label="Reference 7">[7]&lt;/a>&lt;/sup>.&lt;/p>
&lt;p>After fixing issues with default vnc setup like rotation, monitor placement and missing cursor, I could move a window onto it through &lt;a class="citation-link" href="https://dwm.suckless.org/" title="dwm — dynamic window manager">dwm&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-8" aria-label="Reference 8">[8]&lt;/a>&lt;/sup> with the pen contact being held stand for the left mouse button for drawing, while fingers could tap without moving the pointer.&lt;sup class="sidenote-number">&lt;a href="#sidenote-1">[1]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-1">&lt;span class="sidenote-label">[1]&lt;/span> The pen being held slightly over the monitor still was still detected, in ways my hand wasn&amp;rsquo;t.&lt;/span>&lt;/p>
&lt;p>When I finally held the pen down to draw, the line took roughly a second to appear. So I started looking for places to optimise.&lt;/p>
&lt;h2 id="measuring-it">Measuring it&lt;/h2>
&lt;p>I decided to profile it manually through timestamps adding them across the input, VNC, application and display layers.&lt;/p>
&lt;p>The first test used a temporary X11 window which drew black dots as soon as it received input. Given GIMP could also spend time processing the brush, this let me test the display path without it. Some strokes and short taps were sent through the tablet&amp;rsquo;s actual &lt;a class="citation-link" href="https://www.kernel.org/doc/html/latest/input/event-codes.html" title="Linux input event codes">input device&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-9" aria-label="Reference 9">[9]&lt;/a>&lt;/sup>, at about $50$ reports per second.&lt;/p>
&lt;p>The &lt;a class="citation-link" href="https://man.openbsd.org/ssh#R" title="OpenSSH remote port forwarding">SSH tunnel&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-10" aria-label="Reference 10">[10]&lt;/a>&lt;/sup> loopback latency was about $0.8\,\mathrm{ms}$. Input management was relatively cheap, so it wasn&amp;rsquo;t really a bottleneck either.&lt;/p>
&lt;p>The client did, however, wait $50\,\mathrm{ms}$ between drawing refreshes and $400\,\mathrm{ms}$ between normal updates. Lifting the pen immediately restored normal mode, so even the pixels from a short tap could return after it had switched to the slower interval.&lt;/p>
&lt;p>Similarly, the display backend was not using the driver&amp;rsquo;s &lt;code>FastDraw&lt;/code> flag. The &lt;a class="citation-link" href="https://github.com/timower/rM2-stuff/blob/451577081b9eaa3d582e50bbca8c4adfd4911b53/doc/RENDERING_PIPELINE.md">native rendering pipeline docs&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-11" aria-label="Reference 11">[11]&lt;/a>&lt;/sup> described a pen path using it, which allowed overlapping asynchronous updates to avoid waiting for an earlier waveform to finish. The backend was changed to use it, with an $8\,\mathrm{ms}$ drawing interval, and cleanup left until the pen was away and interaction had been idle for $800\,\mathrm{ms}$, much like the native rendering de-bouncing on xochitl.&lt;/p>
&lt;p>The USB SSH listener also had &lt;code>NoDelay=no&lt;/code>, allowing batching of &lt;a class="citation-link" href="https://datatracker.ietf.org/doc/html/rfc896" title="RFC 896 — Congestion Control in IP/TCP Internetworks">packets&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-12" aria-label="Reference 12">[12]&lt;/a>&lt;/sup>. I&amp;rsquo;d also managed to reduce some further overhead enabling damage tracking through &lt;a class="citation-link" href="https://www.x.org/releases/current/doc/damageproto/damageproto.txt" title="X Damage Extension Protocol">XDamage&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-13" aria-label="Reference 13">[13]&lt;/a>&lt;/sup> and have separate threads for input and output. Surprising using lossless compression when sending also helped with the latency.&lt;/p>
&lt;p>Median input delay:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>Before: ~21 ms
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>After: ~3 ms
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Longest wait after drawing submission (leaving the rendering on rM2 out):&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>Before: 193 ms
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>After: 14 ms
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="and-now-lines">And now lines&lt;/h2>
&lt;p>This reduced the inherent latency and rendering optimization work I expected, but lines were still slower for some reason.&lt;/p>
&lt;p>I had the test changed to send $960$ drag-draw event over $4$ seconds, while holding the button down throughout. Repeating it after the server changes and then the client changes would let me compare their effects.&lt;/p>
&lt;p>This time, the input reached the host sometimes about $350\,\mathrm{ms}$ late, and only $11$ drawing updates were submitted over the stroke and the following some seconds. Given the mouse events were forwarded to X11 by &lt;a class="citation-link" href="https://github.com/LibVNC/x11vnc/blob/master/src/pointer.c">x11vnc&amp;rsquo;s pointer handler&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-14" aria-label="Reference 14">[14]&lt;/a>&lt;/sup>, I tried looking up for it&amp;rsquo;s &lt;a class="citation-link" href="https://github.com/LibVNC/x11vnc/blob/master/src/help.c">configuration around dragging&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-15" aria-label="Reference 15">[15]&lt;/a>&lt;/sup> and found that it had heuristics for dragging windows and copying scrolled regions, which, along with its input scheduling, got in the way of capturing changes while the button was held down. Disabling them with&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>-nowireframe -noscrollcopyrect -noxrecord -input_skip &lt;span style="color:#d3869b">1&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>brought the count to $315$ drawing submissions for the same stroke.&lt;/p>
&lt;h2 id="following-the-queue">Following the queue&lt;/h2>
&lt;p>The tablet client read only one queued pen report before returning to display work (&lt;a class="citation-link" href="https://github.com/matteodelabre/vnsee/blob/6ed9593769471133d9602108ce6855dd53d54931/src/app/pen.cpp#L22-L26">pen handler&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-16" aria-label="Reference 16">[16]&lt;/a>&lt;/sup> and &lt;a class="citation-link" href="https://github.com/matteodelabre/vnsee/blob/6ed9593769471133d9602108ce6855dd53d54931/src/rmioc/input.cpp#L163-L169">input reader&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-17" aria-label="Reference 17">[17]&lt;/a>&lt;/sup>), so reports could build up while it was busy with an update. It was changed to read them in bounded batches by looping over &lt;code>device.process_events()&lt;/code> in &lt;code>app::pen::process_events()&lt;/code>, stopping when the queue was empty or after $128$ reports (given $128>960/11$) or about $8\,\mathrm{ms}$.&lt;/p>
&lt;p>There was a similar problem with &lt;a class="citation-link" href="https://github.com/LibVNC/libvncserver" title="LibVNCServer and LibVNCClient">&lt;code>LibVNCClient&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-18" aria-label="Reference 18">[18]&lt;/a>&lt;/sup>.&lt;/p>
&lt;p>Its &lt;a class="citation-link" href="https://github.com/LibVNC/libvncserver/blob/2aa20dad4c23c18948d3f63b33f9dfec1f837729/libvncclient/sockets.c#L126-L183">socket reader&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-19" aria-label="Reference 19">[19]&lt;/a>&lt;/sup> could already have read bytes from the next framebuffer update message into its own buffer, when VNSee&amp;rsquo;s loop only checked whether the socket was readable. Given these bytes had already been read from the socket into LibVNCClient&amp;rsquo;s buffer, the client would only be able to process the first screen update and wait for more network data to come through before it&amp;rsquo;s run again and catch up.&lt;/p>
&lt;p>And I changed it to check &lt;code>vnc_client-&amp;gt;buffered&lt;/code> too, so it could process already-received updates without waiting for more network traffic. Along with reading pen reports in batches, this gave&lt;/p>
&lt;p>Count of submissions over the stroke and some time afterward to pad:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>Before: 315 submissions
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>After: 386 submissions
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Comparing a larger region took more time too, as the code extended the damaged rectangle for every changed pixel. Finding the first and last changed pixel in each row was enough to extend it once per row instead.&lt;/p>
&lt;p>The earlier input delays scaled to a stroke across the whole screen:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>E[Before]: 94 ms
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>E[After]: 44 ms
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>which is more given the damage tracking work done.&lt;/p>
&lt;h2 id="output">Output&lt;/h2>
&lt;p>Trying it on the tablet again, I could finally draw normally in GIMP without waiting for the line to catch up, including when I kept the pen held down.
A recording of the buffer rendered by the buffer before and after can be found side-by-side.&lt;/p>
&lt;figure class="video-comparison" aria-label="Drawing latency before and after the changes">
 &lt;div class="video-comparison-grid">
 &lt;div class="video-comparison-item">
 &lt;div class="video-comparison-label">Before&lt;/div>
 &lt;video controls playsinline preload="none" aria-label="Drawing before the latency changes">
 &lt;source src="http://epestr.com/videos/remarkable-before.mp4" type="video/mp4">
 &lt;a href="http://epestr.com/videos/remarkable-before.mp4">Open the before recording&lt;/a>
 &lt;/video>
 &lt;/div>
 &lt;div class="video-comparison-item">
 &lt;div class="video-comparison-label">After&lt;/div>
 &lt;video controls playsinline preload="none" aria-label="Drawing after the latency changes">
 &lt;source src="http://epestr.com/videos/remarkable-after.mp4" type="video/mp4">
 &lt;a href="http://epestr.com/videos/remarkable-after.mp4">Open the after recording&lt;/a>
 &lt;/video>
 &lt;/div>
 &lt;/div>
 &lt;figcaption>The tablet display before and after the latency changes.&lt;/figcaption>
&lt;/figure>

&lt;h2 id="bibliography">Bibliography&lt;/h2></content:encoded></item><item><title>Reverse engineering a commercial spatial audio effect</title><link>http://epestr.com/blog/reverse-engineering-a-commercial-spatial-audio-effect/</link><guid isPermaLink="true">http://epestr.com/blog/reverse-engineering-a-commercial-spatial-audio-effect/</guid><pubDate>Sat, 10 Oct 2026 05:30:23 +0530</pubDate><description>&lt;p>Back while I was using a different OS, I used a program which processed the audio being played in real time and made it sound much better. It made the sound feel farther away and a bit lighter to hear, while carrying the relevant information. I&amp;rsquo;d always wanted a similar program which was equally simple to use for Linux, and decided to write one.&lt;/p>
&lt;section class="audio-comparison"
 data-audio-comparison
 data-original="/audio/spatial-comparison-original.mp3"
 data-spatial="/audio/spatial-comparison-processed.mp3"
 aria-label="Original and surround-sound audio comparison">
 &lt;div class="audio-comparison-options" role="group" aria-label="Version">
 &lt;button type="button" class="audio-comparison-option selected" data-version="original" aria-pressed="true">
 &lt;span>Original&lt;/span>
 &lt;/button>
 &lt;button type="button" class="audio-comparison-option" data-version="spatial" aria-pressed="false">
 &lt;span>Surround sound&lt;/span>
 &lt;/button>
 &lt;/div>
 &lt;div class="audio-comparison-transport">
 &lt;button type="button" class="audio-comparison-play">Play&lt;/button>
 &lt;input class="audio-comparison-seek" type="range" min="0" max="1000" value="0" aria-label="Excerpt progress">
 &lt;output class="audio-comparison-time" aria-live="off">0:00 / 0:10&lt;/output>
 &lt;/div>
 &lt;p class="audio-comparison-status" role="status" aria-live="polite">&lt;/p></description><content:encoded>&lt;p>Back while I was using a different OS, I used a program which processed the audio being played in real time and made it sound much better. It made the sound feel farther away and a bit lighter to hear, while carrying the relevant information. I&amp;rsquo;d always wanted a similar program which was equally simple to use for Linux, and decided to write one.&lt;/p>
&lt;section class="audio-comparison"
 data-audio-comparison
 data-original="/audio/spatial-comparison-original.mp3"
 data-spatial="/audio/spatial-comparison-processed.mp3"
 aria-label="Original and surround-sound audio comparison">
 &lt;div class="audio-comparison-options" role="group" aria-label="Version">
 &lt;button type="button" class="audio-comparison-option selected" data-version="original" aria-pressed="true">
 &lt;span>Original&lt;/span>
 &lt;/button>
 &lt;button type="button" class="audio-comparison-option" data-version="spatial" aria-pressed="false">
 &lt;span>Surround sound&lt;/span>
 &lt;/button>
 &lt;/div>
 &lt;div class="audio-comparison-transport">
 &lt;button type="button" class="audio-comparison-play">Play&lt;/button>
 &lt;input class="audio-comparison-seek" type="range" min="0" max="1000" value="0" aria-label="Excerpt progress">
 &lt;output class="audio-comparison-time" aria-live="off">0:00 / 0:10&lt;/output>
 &lt;/div>
 &lt;p class="audio-comparison-status" role="status" aria-live="polite">&lt;/p>
 &lt;noscript>JavaScript is required for the synchronized comparison.&lt;/noscript>
&lt;/section>

&lt;p>Listen with headphones. The excerpt is from &lt;a class="citation-link" href="https://bacaorhythmandsteelband.bandcamp.com/track/p-i-m-p" title="Bacao Rhythm &amp;amp; Steel Band — P.I.M.P. official release">P.I.M.P. by Bacao Rhythm &amp;amp; Steel Band&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-0" aria-label="Reference 0">[0]&lt;/a>&lt;/sup>.&lt;/p>
&lt;p>The processed version sounds farther from the ears and feels less &amp;ldquo;on the ear&amp;rdquo; making it feel less &amp;ldquo;blocked&amp;rdquo; after listening to music for a while.&lt;/p>
&lt;h2 id="first-attempt">First attempt&lt;/h2>
&lt;p>Since I was writing for Linux, my base stack was &lt;a class="citation-link" href="https://pipewire.org/" title="PipeWire — multimedia processing for Linux">PipeWire&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-1" aria-label="Reference 1">[1]&lt;/a>&lt;/sup>. I had no prior audio experience, so I began by using an LLM as a sounding board while experimenting with standard audio filters and psychoacoustic techniques.&lt;/p>
&lt;p>Somewhere along the way I learnt about HRTFs, which describe how sound arriving from a particular direction reaches the ears, depending on the direction and geometry of the listener. I tried the &lt;a class="citation-link" href="https://sound.media.mit.edu/resources/KEMAR.html" title="MIT KEMAR head-related transfer-function measurements">MIT KEMAR database&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-2" aria-label="Reference 2">[2]&lt;/a>&lt;/sup> first and thought it sounded awful, because it had been recorded in an anechoic chamber, so there were no room echoes, which were a desirable psychoacoustic effect here. The result sounded cheap and more inwards to the ear. Later I found the &lt;a class="citation-link" href="https://www.york.ac.uk/sadie-project/database.html" title="SADIE II spatial-audio and binaural room-response database">SADIE II database&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-3" aria-label="Reference 3">[3]&lt;/a>&lt;/sup>, which also came with room responses, and that sounded much closer to what I remembered.&lt;/p>
&lt;p>But this approach soon showed its limits. The LLM began talking about things like &amp;ldquo;perceptual envelopment&amp;rdquo;, &amp;ldquo;late-field decorrelation&amp;rdquo;, &amp;ldquo;spectral diffusion&amp;rdquo; and &amp;ldquo;diffuse spatial energy&amp;rdquo;.&lt;sup class="sidenote-number">&lt;a href="#sidenote-1">[1]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-1">&lt;span class="sidenote-label">[1]&lt;/span> Gemini was particularly good at this. It was also the only one with a free student program.&lt;/span> While I could hear that something was wrong, those descriptions didn&amp;rsquo;t point to anything I could understand or use to improve the effect.&lt;/p>
&lt;p>I left the project dead for a while with no obvious direction to proceed with.&lt;/p>
&lt;h2 id="re-attempt">Re-attempt&lt;/h2>
&lt;p>Much later, I had a different idea. Instead of trying to discover the exact parameters or techniques and algorithms the program used internally, I could study the behaviour of the filter itself.&lt;/p>
&lt;p>This seems obvious in retrospect, but I did not have a copy of the program. I could only experiment on a friend&amp;rsquo;s laptop, so I could not keep changing my implementation and comparing it with the reference whenever I wanted.&lt;/p>
&lt;p>I began reading &lt;a class="citation-link" href="https://signalsmith-audio.com/writing/" title="Signalsmith Audio — DSP investigations and writing">Signalsmith&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-4" aria-label="Reference 4">[4]&lt;/a>&lt;/sup> and some blogs by audio engineers to build enough intuition to know what I should measure, and soon realised that I didn&amp;rsquo;t need to keep using the program to capture its impulse responses and other measurable features. I generated an audio file on Linux, got my friend&amp;rsquo;s laptop for a while, played the generated file through the program and recorded the output.&lt;/p>
&lt;h2 id="the-assembly-of-tests">The assembly of tests&lt;/h2>
&lt;p>The question became what I should put into that file so it could capture the relevant details which differentiated the program without having to re-record features or filters.&lt;/p>
&lt;p>The first useful thing I found was impulse-response measurement.&lt;/p>
&lt;p>If I pass a file containing a single nonzero sample through the filter, everything which appears after it in the recording would&amp;rsquo;ve been added by the filter. Features such as the extent of its decay over time, the shape of the decay and the phase delays associated with it would be present in the output. Playing the impulse in the right channel allows extracting both outputs&amp;rsquo; responses to sound played on the right, and likewise for the left.&lt;/p>
&lt;p>If the response to one impulse is $h$, then the response to an impulse at position $k$ is the same $h$ shifted to $k$ and scaled by $x[k]$. Adding all of those responses gives&lt;/p>
$$
y[n]=\sum_k x[k]h[n-k]=(x*h)[n].
$$&lt;p>This of course depends on the filter being a &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Linear_time-invariant_system" title="Linear time-invariant systems">linear time-invariant filter&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-5" aria-label="Reference 5">[5]&lt;/a>&lt;/sup>.&lt;/p>
&lt;p>A louder impulse should produce the same response made proportionally louder:&lt;/p>
$$
F(ax)=aF(x).
$$&lt;p>Two inputs played together should produce the sum of what each input produces separately:&lt;/p>
$$
F(x_1+x_2)=F(x_1)+F(x_2).
$$&lt;p>And playing the same input later should produce the same response later.&lt;/p>
&lt;p>And given it&amp;rsquo;s stereo, the full equation would be:&lt;/p>
$$
\begin{bmatrix}
y_L\\
y_R
\end{bmatrix}
{}={}
\begin{bmatrix}
h_{LL} &amp; h_{LR}\\
h_{RL} &amp; h_{RR}
\end{bmatrix}
*
\begin{bmatrix}
x_L\\
x_R
\end{bmatrix}.
$$&lt;p>Given I was sending one file to have it recorded through every mode, I added impulses at different amplitudes to check for level-dependent processing, and played the same impulse in both channels, then with the right channel inverted. The outputs should match the isolated responses added together in the first case and subtracted in the second, checking the conditions above. I also left enough silence between them for the response to finish before the next impulse.&lt;/p>
&lt;figure class="audio-example">
 &lt;figcaption>Impulses at three levels, played on the left, right, both channels and with opposite signs.&lt;/figcaption>
 &lt;audio controls preload="metadata">
 &lt;source src="http://epestr.com/audio/probe-impulses.mp3" type="audio/mpeg">
 &lt;a href="http://epestr.com/audio/probe-impulses.mp3">Download the audio example&lt;/a>
 &lt;/audio>
&lt;/figure>

&lt;p>While reading about this, I noticed that papers on measurement tended to use &lt;a class="citation-link" href="https://angelofarina.it/Public/Papers/134-AES00.PDF" title="Farina — Simultaneous Measurement of Impulse Response and Distortion with a Swept-Sine Technique">sine sweeps rather than impulses&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-6" aria-label="Reference 6">[6]&lt;/a>&lt;/sup>.&lt;sup class="sidenote-number">&lt;a href="#sidenote-2">[2]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-2">&lt;span class="sidenote-label">[2]&lt;/span> An impulse already contained every frequency, but having only one sample meant there was little energy at each frequency, so a long sweep would make the response easier to distinguish from recording noise.&lt;/span>&lt;/p>
&lt;figure class="audio-example">
 &lt;figcaption>A shortened logarithmic sweep.&lt;/figcaption>
 &lt;audio controls preload="metadata">
 &lt;source src="http://epestr.com/audio/probe-sweep.mp3" type="audio/mpeg">
 &lt;a href="http://epestr.com/audio/probe-sweep.mp3">Download the audio example&lt;/a>
 &lt;/audio>
&lt;/figure>

$$
Y(f)=H(f)X(f),
\qquad
H(f)=\frac{Y(f)}{X(f)}.
$$&lt;p>This would work for most of the range, but if $X(f)$ was close to zero, dividing recording noise by it would make a rather impressive filter which had little to do with the program. This could be avoided with a regularized version.&lt;/p>
$$
\hat H(f)=\frac{Y(f)X^*(f)}{|X(f)|^2+\lambda}.
$$&lt;p>With enough input energy this would approximately be the same division, while $\lambda$ would limit the result elsewhere. The complex values also retain the phase, which was required given sound sent to one channel could appear in the other with a delay.&lt;/p>
&lt;p>The sweep gave another measurement to compare with the impulse, but it also took much longer to play, during which the program could change its behaviour. I added steady tones at different frequencies and amplitudes, and longer tones going from quiet to loud and back, to see whether the gain changed and how long it took to do so.&lt;/p>
&lt;figure class="audio-example">
 &lt;figcaption>The steady tones, shortened and played through both channels.&lt;/figcaption>
 &lt;audio controls preload="metadata">
 &lt;source src="http://epestr.com/audio/probe-tones.mp3" type="audio/mpeg">
 &lt;a href="http://epestr.com/audio/probe-tones.mp3">Download the audio example&lt;/a>
 &lt;/audio>
&lt;/figure>

&lt;figure class="audio-example">
 &lt;figcaption>A 1000 Hz tone alternating between two levels.&lt;/figcaption>
 &lt;audio controls preload="metadata">
 &lt;source src="http://epestr.com/audio/probe-stepped-tone.mp3" type="audio/mpeg">
 &lt;a href="http://epestr.com/audio/probe-stepped-tone.mp3">Download the audio example&lt;/a>
 &lt;/audio>
&lt;/figure>

&lt;p>And a multitone to check if any new frequency pops up:&lt;/p>
&lt;figure class="audio-example">
 &lt;figcaption>The eight-frequency multitone.&lt;/figcaption>
 &lt;audio controls preload="metadata">
 &lt;source src="http://epestr.com/audio/probe-multitone.mp3" type="audio/mpeg">
 &lt;a href="http://epestr.com/audio/probe-multitone.mp3">Download the audio example&lt;/a>
 &lt;/audio>
&lt;/figure>

&lt;p>And repetition to check for time-invariance:&lt;/p>
&lt;figure class="audio-example">
 &lt;figcaption>Three identical copies of the multitone.&lt;/figcaption>
 &lt;audio controls preload="metadata">
 &lt;source src="http://epestr.com/audio/probe-repeats.mp3" type="audio/mpeg">
 &lt;a href="http://epestr.com/audio/probe-repeats.mp3">Download the audio example&lt;/a>
 &lt;/audio>
&lt;/figure>

&lt;p>And I kept a section of noise aside, to test whether the extracted filter could reproduce its recorded output afterwards.&lt;/p>
&lt;figure class="audio-example">
 &lt;figcaption>Noise covering the low, middle and high frequency ranges in sequence.&lt;/figcaption>
 &lt;audio controls preload="metadata">
 &lt;source src="http://epestr.com/audio/probe-noise.mp3" type="audio/mpeg">
 &lt;a href="http://epestr.com/audio/probe-noise.mp3">Download the audio example&lt;/a>
 &lt;/audio>
&lt;/figure>

&lt;h2 id="recovering-the-filter">Recovering the filter&lt;/h2>
&lt;p>Given recording and playback were started manually, the recording first needed an offset. I had put evenly spaced clicks near the start, which I initially tried finding by correlating the recording with the original clicks. Since the filter could spread each click over time, I summed the squared samples over a window $W$ to estimate the energy around it instead,&lt;/p>
&lt;figure class="audio-example">
 &lt;figcaption>The eight synchronization clicks.&lt;/figcaption>
 &lt;audio controls preload="metadata">
 &lt;source src="http://epestr.com/audio/probe-sync.mp3" type="audio/mpeg">
 &lt;a href="http://epestr.com/audio/probe-sync.mp3">Download the audio example&lt;/a>
 &lt;/audio>
&lt;/figure>

$$
e[n]=\sum_{m\in W}\left(y_L[n+m]^2+y_R[n+m]^2\right),
$$&lt;p>and found the offset through&lt;/p>
$$
\hat\tau=\arg\max_\tau\sum_n e_{\text{probe}}[n]e_{\text{recording}}[n+\tau].
$$&lt;p>I applied the same offset to both channels so the delay between them was retained.&lt;/p>
&lt;h2 id="output">Output&lt;/h2>
&lt;p>For the main surround setting, the scaled impulses were nearly identical, the combined inputs matched the sum of their separate responses, and the multitone produced almost no new frequencies. To compare the reconstructed output with the recording, I used&lt;/p>
$$
\varepsilon=\frac{\lVert y-\hat y\rVert_2}{\lVert y\rVert_2},
$$&lt;p>where $y$ was the recorded output and $\hat y$ the output produced by the extracted filter. Given the identical repeated sections already differed slightly, I compared the model&amp;rsquo;s error with that difference,&lt;/p>
$$
\varepsilon_{\text{model}}\approx\varepsilon_{\text{repeat}}\ll1.
$$&lt;p>The volume-reducing mode changed its gain with amplitude and took time to restore it, while another spatial mode changed between repeats, so neither could be reproduced with a fixed convolution filter.&lt;/p>
&lt;p>Recording both output channels for an impulse in each input channel gave me the four filters.&lt;/p>
&lt;p>Sound also appeared in the opposite channel, quieter and slightly delayed, followed by a long tail. Together these contained the out-of-head effect I had been trying to make earlier.&lt;/p>
&lt;p>To decide how much of this response to keep, I tried increasingly long filters against the noise section,&lt;/p>
$$
\varepsilon_N=
\frac{\lVert y_{\text{noise}}-h^{(N)}*x_{\text{noise}}\rVert_2}
{\lVert y_{\text{noise}}\rVert_2},
$$&lt;p>and kept the full response, which predicted the noise more accurately than the shorter versions. The response extracted from the sweep also agreed over the range where it had enough input energy.&lt;/p>
&lt;p>Loading the four responses into &lt;a class="citation-link" href="https://github.com/mTvare6/saqol" title="Saq — a real-time audio enhancer for Linux">Saq&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-7" aria-label="Reference 7">[7]&lt;/a>&lt;/sup> gave me the placement I had been trying to reproduce by hand.&lt;/p>
&lt;p>You can try the result here. Choose your own local audio file or capture a tab (when not in a &lt;a class="citation-link" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1541425" title="Implement audio capture for getDisplayMedia">Firefox browser&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-8" aria-label="Reference 8">[8]&lt;/a>&lt;/sup>), then click the square surround icon to toggle the effect.&lt;/p>
&lt;div class="saqol-demo">
 &lt;iframe
 src="http://epestr.com/demos/saqol/"
 title="Saqol spatial-audio demo"
 loading="lazy"
 allow="autoplay; display-capture">
 &lt;/iframe>
 &lt;p>&lt;a href="https://github.com/mTvare6/saqol" target="_blank" rel="noreferrer">Code can be found here&lt;/a>&lt;/p>
&lt;/div>

&lt;h2 id="bibliography">Bibliography&lt;/h2></content:encoded></item><item><title>Writing a ray tracer in Brainfuck</title><link>http://epestr.com/blog/writing-a-ray-tracer-in-brainfuck/</link><guid isPermaLink="true">http://epestr.com/blog/writing-a-ray-tracer-in-brainfuck/</guid><pubDate>Thu, 24 Sep 2026 16:11:34 +0530</pubDate><description>&lt;p>As I was preparing for a systems programming competition in C++, I began to relearn CMake, as Cargo had spoilt me too much in the meantime, and I noticed an interesting claim in the tutorial.&lt;/p>
&lt;blockquote>
&lt;p>Oftentimes the correct answer is to write a tool in a general purpose
programming language which solves the problem, and teach CMake how to invoke
that tool as part of the build process. Code generation, cryptographic
signature utilities, and even ray-tracers have been written in CMake Language,
but this is not a recommended practice.&lt;/p></description><content:encoded>&lt;p>As I was preparing for a systems programming competition in C++, I began to relearn CMake, as Cargo had spoilt me too much in the meantime, and I noticed an interesting claim in the tutorial.&lt;/p>
&lt;blockquote>
&lt;p>Oftentimes the correct answer is to write a tool in a general purpose
programming language which solves the problem, and teach CMake how to invoke
that tool as part of the build process. Code generation, cryptographic
signature utilities, and even ray-tracers have been written in CMake Language,
but this is not a recommended practice.&lt;/p>
&lt;p>&lt;a class="citation-link" href="https://cmake.org/cmake/help/latest/guide/tutorial/CMake%20Language%20Fundamentals.html">CMake Language Fundamentals&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-0" aria-label="Reference 0">[0]&lt;/a>&lt;/sup>&lt;/p>
&lt;/blockquote>
&lt;p>Having written a raytracer earlier, and re-written it for the GPU, this statement caught my eye and made me wonder what would be an even better language to write a raytracer in.&lt;/p>
&lt;p>The last re-write involved writing code which had little of a first-principles based approach and mostly depended on a comparatively more complex set of APIs. So I picked the simplest language I knew, BF, because a simple language obviously results in a very simple codebase. In fact, codebases in BF regularly tend to be only a few lines long. Further, &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Brainfuck#History" title="Urban Müller&amp;#39;s comment on Brainfuck&amp;#39;s intentionally tiny compiler">Muller&amp;rsquo;s comment&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-1" aria-label="Reference 1">[1]&lt;/a>&lt;/sup> in the README made me want to show a counter example.&lt;/p>
&lt;p>The code is available at &lt;a class="citation-link" href="https://github.com/mTvare6/rayfuck" title="rayfuck — a ray tracer generated in Brainfuck">mTvare6/rayfuck&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-2" aria-label="Reference 2">[2]&lt;/a>&lt;/sup>.&lt;/p>
&lt;h2 id="primer">Primer&lt;/h2>
&lt;p>BF is a decidedly simple language, involving only 8 operations and one &amp;ldquo;data structure&amp;rdquo;: a one-sided infinite tape of cells, each capable of storing a &lt;code>u8&lt;/code>.&lt;/p>
&lt;p>On seeing the character &lt;code>&amp;gt;&lt;/code>, the data pointer, which points to a cell on the tape, moves rightward, and vice versa on &lt;code>&amp;lt;&lt;/code>.&lt;/p>
&lt;p>I/O is managed through &lt;code>,&lt;/code> and &lt;code>.&lt;/code>. The first stores the input byte where the data pointer points and the latter prints it out.&lt;/p>
&lt;p>The only primitives other than I/O which allow changing a value are increment and decrement at the data pointer, through &lt;code>+&lt;/code> and &lt;code>-&lt;/code>.&lt;/p>
&lt;p>The limitations should be obvious: there are no $n>1$ registers as other machines tend to have, no instruction operating on more than one cell, and no instructions for addition or multiplication.&lt;/p>
&lt;p>The last ingredient required to make BF Turing-complete is its loop, written using &lt;code>[&lt;/code> and &lt;code>]&lt;/code>. When the token &lt;code>[&lt;/code> is met, the runtime checks the cell at the data pointer: if it is zero, execution jumps past the matching &lt;code>]&lt;/code>, otherwise it enters the loop. At &lt;code>]&lt;/code>, it returns to the matching &lt;code>[&lt;/code> if the cell is non-zero and exits the loop otherwise.&lt;/p>
&lt;p>A quick exercise would be writing a &lt;code>cat&lt;/code> program, try writing one with just 5 characters to get some intuition about the environment.&lt;/p>
&lt;h2 id="premeditation">Premeditation&lt;/h2>
&lt;p>Having read about it before starting this, I decided to avoid looking up any result or implementation detail and to write down as much as possible from first principles. To keep the scope minimal, and the program an obvious raytracer, I decided to have it render the exact image rendered through the Metal section of &lt;a class="citation-link" href="https://raytracing.github.io/books/RayTracingInOneWeekend.html#metal" title="Ray Tracing in One Weekend — Metal chapter">RIW&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-3" aria-label="Reference 3">[3]&lt;/a>&lt;/sup>.&lt;/p>
&lt;p>The C code was a bit too complex regardless, and writing a C parser was clearly out of scope. Writing an unmaintained C parser is something better handled by &lt;a class="citation-link" href="https://www.anthropic.com/engineering/building-c-compiler" title="Anthropic — Building a C compiler with parallel agents">Anthropic&amp;rsquo;s C compiler&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-4" aria-label="Reference 4">[4]&lt;/a>&lt;/sup>.&lt;/p>
&lt;p>I decided that every double would be represented by combining cells, with half the bits representing the fractional part and the other half representing the integer part, effectively placing a fixed binary point between them.&lt;sup class="sidenote-number">&lt;a href="#sidenote-1">[1]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-1">&lt;span class="sidenote-label">[1]&lt;/span> The same approach would cover the other datatypes I needed, such as &lt;code>bool&lt;/code>, rather than only doubles.&lt;/span> I later got to know that this is called a Q format. Going with the cheaper signed Q8.8 would give a resolution of $1/256$ and a range of approximately $[-128,128)$. But clearly, that wouldn&amp;rsquo;t be enough, as the sphere used for the ground in the scene had to have $r=1000$ to appear flat, so I went with the more expensive signed Q16.16 format. It has a resolution of $1/2^{16}$ and a range of $[-2^{15},2^{15})$, which is sufficient.&lt;/p>
&lt;p>I decided to have the code converted to an SSA-like format, where recursive code is made iterative, and variables defined in functions are prefixed in a Hungarian-style notation to avoid name collisions during address lookup for a name.&lt;sup class="sidenote-number">&lt;a href="#sidenote-2">[2]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-2">&lt;span class="sidenote-label">[2]&lt;/span> I decided this conversion would be the only job for an LLM; the rest would follow the first-principles constraint I had set for the project.&lt;/span>&lt;/p>
&lt;p>Similarly, separating the parsing and codegen seemed necessary, dividing complexity into two code regions, with an intermediate &amp;ldquo;DSL&amp;rdquo; being used as an IR. The DSL contained simple operations such as &lt;code>abs&lt;/code>, &lt;code>add&lt;/code>, &lt;code>and&lt;/code>, &lt;code>call&lt;/code>, &lt;code>copy&lt;/code>, &lt;code>div&lt;/code>, &lt;code>else&lt;/code>, &lt;code>end&lt;/code>, &lt;code>eq&lt;/code>, &lt;code>func&lt;/code>, &lt;code>ge&lt;/code>, &lt;code>gt&lt;/code>, &lt;code>if&lt;/code>, &lt;code>int&lt;/code>, &lt;code>le&lt;/code>, &lt;code>lt&lt;/code>, &lt;code>mul&lt;/code>, &lt;code>neg&lt;/code>, &lt;code>not&lt;/code>, &lt;code>or&lt;/code>, &lt;code>print2&lt;/code>, &lt;code>print3&lt;/code>, &lt;code>set&lt;/code>, &lt;code>sqrt&lt;/code>, &lt;code>sub&lt;/code>, &lt;code>text&lt;/code>, &lt;code>var&lt;/code>, and &lt;code>while&lt;/code>.&lt;/p>
&lt;p>The next tricky part was a few library calls. The ones used were &lt;code>sqrt&lt;/code>, &lt;code>rand&lt;/code> and &lt;code>abs&lt;/code>. Initially I planned on using a two-state solution like:&lt;/p>
$$
\begin{aligned}
A &amp;\gets (A-B) \bmod 256,\\
B &amp;\gets (B+1) \bmod 256,
\end{aligned}
$$&lt;p>or $B\gets(B+A+p)\bmod256$ for some prime $p$,&lt;/p>
&lt;p>but most of these variants have a poor period. I decided to go with the simpler&lt;/p>
$$
A\gets(5A+1)\bmod256,
$$&lt;p>given that it is guaranteed to repeat only after a full sequence of 256 values, which isn&amp;rsquo;t too bad for this use-case.&lt;sup class="sidenote-number">&lt;a href="#sidenote-5">[5]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-5">&lt;span class="sidenote-label">[5]&lt;/span> The use-case here is supersampling anti-aliasing, which needs a stream of slightly different sample positions rather than cryptographically strong randomness.&lt;/span>&lt;/p>
&lt;p>&lt;code>sqrt&lt;/code> has one obvious candidate, Heron&amp;rsquo;s formula.&lt;sup class="sidenote-number">&lt;a href="#sidenote-3">[3]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-3">&lt;span class="sidenote-label">[3]&lt;/span> The same CMake tutorial mentioned earlier had refreshed my memory of Heron&amp;rsquo;s formula.&lt;/span> But it was pretty obvious it&amp;rsquo;d be bad, given it involved division. Repeated subtraction, while producing smaller generated code, was still relatively expensive to do.&lt;sup class="sidenote-number">&lt;a href="#sidenote-4">[4]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-4">&lt;span class="sidenote-label">[4]&lt;/span> Smaller generated code is preferable because it leaves the interpreter with less code to move through.&lt;/span>
The other candidates were the Taylor series and the &amp;ldquo;School Method&amp;rdquo;, which involves long-division.&lt;/p>
$$
\begin{aligned}
\sqrt{1+x} &amp;\approx 1+\frac{x}{2}-\frac{x^2}{8},\\
y &amp;= 2^{16}x,\\
\frac{\sqrt{2^{16}+y}}{2^8} &amp;\approx 1+\frac{y}{2^{17}}-\frac{y^2}{2^{35}},\\
\frac{\sqrt y}{2^8} &amp;\approx 1+\frac{y-2^{16}}{2^{17}}-\frac{(y-2^{16})^2}{2^{35}}.
\end{aligned}
$$&lt;p>Plotting this on Desmos revealed that the fit was poor below an encoded value of 20k, that is, below roughly &lt;code>0.305&lt;/code>, which was a pretty important region.
This left me with the long-division method, which was pretty simple. If the real value was &lt;code>x&lt;/code>, then the represented value was:&lt;/p>
$$
N=x\,2^{16}.
$$&lt;p>To represent $\sqrt{x}$, we need:&lt;/p>
$$
\begin{aligned}
N_0 &amp;= \sqrt{x}\,2^{16},\\
\operatorname{isqrt}(N) &amp;= \sqrt{x}\,2^8,\\
\operatorname{isqrt}(2^{16}N) &amp;= \sqrt{x}\,2^{16}=N_0.
\end{aligned}
$$&lt;p>&lt;code>isqrt&lt;/code> is justified here, as a difference of one in the encoded result changes the decoded square root by less than $1/2^{16}$, or approximately $0.00001526$.&lt;/p>
&lt;p>And finally, the whole variable map and corresponding BF addresses would be maintained with a dictionary.&lt;/p>
&lt;h2 id="implementation">Implementation&lt;/h2>
&lt;p>With the theoretical bits set up, only clearly simple implementation details were left. Two important primitives were &lt;code>move&lt;/code> and &lt;code>copy&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>[a, &lt;span style="color:#d3869b">0&lt;/span>]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Move works by continually lowering a value until the cell at the initial data pointer becomes zero, and incrementing the other cell equally every time.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-brainfuck" data-lang="brainfuck">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#fe8019">[&lt;/span>&lt;span style="color:#928374;font-style:italic"> # start loop
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#928374;font-style:italic"> &lt;/span>&lt;span style="color:#fabd2f">-&lt;/span>&lt;span style="color:#928374;font-style:italic"> # decrement
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#928374;font-style:italic"> &lt;/span>&amp;gt;&lt;span style="color:#fabd2f">+&lt;/span>&lt;span style="color:#928374;font-style:italic"> # move right and increment
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#928374;font-style:italic"> &lt;/span>&amp;lt;&lt;span style="color:#928374;font-style:italic"> # come back&lt;/span>&lt;span style="color:#fb4934">,&lt;/span>&lt;span style="color:#928374;font-style:italic"> this cell is used to control the loop
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>&lt;span style="color:#fe8019">]&lt;/span>&lt;span style="color:#928374;font-style:italic">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>as one-liner&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-brainfuck" data-lang="brainfuck">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#fe8019">[&lt;/span>&lt;span style="color:#fabd2f">-&lt;/span>&amp;gt;&lt;span style="color:#fabd2f">+&lt;/span>&amp;lt;&lt;span style="color:#fe8019">]&lt;/span>&lt;span style="color:#928374;font-style:italic">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And copy works as below, starting with this array:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>[a, &lt;span style="color:#d3869b">0&lt;/span>, &lt;span style="color:#d3869b">0&lt;/span>]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>using the code.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-brainfuck" data-lang="brainfuck">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#fe8019">[&lt;/span>&lt;span style="color:#fabd2f">-&lt;/span>&amp;gt;&lt;span style="color:#fabd2f">+&lt;/span>&amp;gt;&lt;span style="color:#fabd2f">+&lt;/span>&amp;lt;&amp;lt;&lt;span style="color:#fe8019">]&lt;/span>&lt;span style="color:#928374;font-style:italic">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Turning it into:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>[&lt;span style="color:#d3869b">0&lt;/span>, a, a]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And now, if needed, the terminal &lt;code>a&lt;/code> can be moved inward.&lt;/p>
&lt;p>Given that addition, and later division, would involve repeated use of temporary values, which have to be near the value to avoid moving the data pointer around too much, every value in the &lt;code>map&lt;/code> also has its temporary-variable slots nearby. These also provide carry cells and other useful scratch space, keeping copies contained.&lt;/p>
&lt;p>Multiplication was similarly straightforward, involving multiplying each cell, storing the results and adding them together later. The multiplication step is taken care of through repeated addition.
For multiplying two cells, one of them is copied to a temporary place and used as the outer loop, and the other is copied once for every iteration to act as the inner loop. Every iteration of the inner loop increments the result once.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>[a, b, a&lt;span style="color:#fe8019">-&amp;gt;&lt;/span>&lt;span style="color:#d3869b">0&lt;/span>, b&lt;span style="color:#fe8019">-&amp;gt;&lt;/span>&lt;span style="color:#d3869b">0&lt;/span>, a &lt;span style="color:#fe8019">+&lt;/span> &lt;span style="color:#fe8019">...&lt;/span> &lt;span style="color:#fe8019">+&lt;/span> a]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here &lt;code>a&lt;/code> runs out every time and &lt;code>b&lt;/code> is decremented when &lt;code>a&lt;/code> is zeroed, producing &lt;code>b&lt;/code> copies of &lt;code>a&lt;/code>.&lt;/p>
&lt;p>For the four-cell values, every cell in one is paired with every cell in the other. A multiplication of the cells at &lt;code>i&lt;/code> and &lt;code>j&lt;/code> is added at &lt;code>i+j&lt;/code> in an eight-cell result.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>[a0, a1, a2, a3] &lt;span style="color:#fe8019">*&lt;/span> [b0, b1, b2, b3]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>result[i&lt;span style="color:#fe8019">+&lt;/span>j] &lt;span style="color:#fe8019">+=&lt;/span> a_i&lt;span style="color:#fe8019">*&lt;/span>b_j
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Since both inputs already had $2^{16}$ in their representation, the lowest 2 cells are discarded when copying back the result.&lt;/p>
$$
\begin{aligned}
N_1 &amp;= x_1\,2^{16},\\
N_2 &amp;= x_2\,2^{16},\\
\frac{N_1N_2}{2^{16}} &amp;= N=(x_1x_2)2^{16}.
\end{aligned}
$$&lt;p>Division was slightly less direct but could still be done the way manual long-division is done, by having the dividend be read from its most significant cell and at every step, the old remainder is carried over a cell onto the next.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>R &lt;span style="color:#fe8019">=&lt;/span> R &lt;span style="color:#fe8019">*&lt;/span> &lt;span style="color:#d3869b">10&lt;/span> &lt;span style="color:#fe8019">+&lt;/span> A[&lt;span style="color:#fabd2f">next&lt;/span>] &lt;span style="color:#928374;font-style:italic"># school&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>R &lt;span style="color:#fe8019">=&lt;/span> R &lt;span style="color:#fe8019">*&lt;/span> &lt;span style="color:#d3869b">256&lt;/span> &lt;span style="color:#fe8019">+&lt;/span> A[&lt;span style="color:#fabd2f">next&lt;/span>] &lt;span style="color:#928374;font-style:italic"># here&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The divisor then is subtracted from this remainder repeatedly, and one gets added to the result cell. When the remainder becomes negative, the step is reverted and we move to the next cell.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#fe8019">while&lt;/span> R &lt;span style="color:#fe8019">&amp;gt;=&lt;/span> D:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> R &lt;span style="color:#fe8019">-=&lt;/span> D
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> result &lt;span style="color:#fe8019">+=&lt;/span> &lt;span style="color:#d3869b">1&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This requires at most 255 subtractions per cell as we divide across cells and combine them later.&lt;/p>
&lt;p>Just as the representation is shifted rightward inflating itself during multiplication, division loses information due to the leftward shift, and some shifting is required in its representation before dividing.&lt;/p>
$$
\begin{aligned}
N_1 &amp;= x_1\,2^{16},\\
N_2 &amp;= x_2\,2^{16},\\
\frac{N_1}{N_2}\,2^{16} &amp;= \frac{x_1}{x_2}\,2^{16} &amp;&amp; \text{bits already lost},\\
\frac{N_1\,2^{16}}{N_2} &amp;= \frac{x_1}{x_2}\,2^{16}.
\end{aligned}
$$&lt;p>Throughout these operations, the signs are removed first, and the result is made negative if only one input was negative.&lt;/p>
&lt;p>Comparisons share the same smaller operation. Two cells are decremented together until at least one becomes zero, and this continues until there is a difference or the temporary copies are completely zeroed. There was some minor processing involving adding $2^7$ to the most significant cell, as otherwise negative numbers technically have a higher value when viewed plainly as bytes.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#d3869b">00&lt;/span> &lt;span style="color:#fe8019">...&lt;/span> &lt;span style="color:#d3869b">7&lt;/span>f &lt;span style="color:#fe8019">-&amp;gt;&lt;/span> positive half
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#d3869b">80&lt;/span> &lt;span style="color:#fe8019">...&lt;/span> ff &lt;span style="color:#fe8019">-&amp;gt;&lt;/span> negative half
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>after adding 128 and wrapping over&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#d3869b">80&lt;/span> &lt;span style="color:#fe8019">...&lt;/span> ff &lt;span style="color:#fe8019">-&amp;gt;&lt;/span> positive half
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#d3869b">00&lt;/span> &lt;span style="color:#fe8019">...&lt;/span> &lt;span style="color:#d3869b">7&lt;/span>f &lt;span style="color:#fe8019">-&amp;gt;&lt;/span> negative half
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Boolean checks involved reading the cells and setting the output to one if any of them was non-zero, to take into account the truthiness tendency of C. The constructed representation had the lowest bit set for true and all bits zero for false. Boolean operations such as &lt;code>and&lt;/code>, &lt;code>or&lt;/code> and &lt;code>not&lt;/code> worked using that bit representation.&lt;/p>
&lt;p>Negation uses the $-x=\mathord{\sim}x+1$ trick. Every cell $x$ is complemented, and then one is added to the lowest cell, carrying over.&lt;sup class="sidenote-number">&lt;a href="#sidenote-6">[6]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-6">&lt;span class="sidenote-label">[6]&lt;/span> Since each cell is one byte, complementing a cell is equivalent to calculating $255-x$.&lt;/span> &lt;code>abs&lt;/code> only checks the highest bit of the last cell and performs this negation if it is set.&lt;/p>
&lt;p>Given the earlier decision to scope variable names by function and use SSA-style code, functions were extremely straightforward. The codegen notes the function body under its name and emits it inline when &lt;code>call func&lt;/code> is seen.&lt;/p>
&lt;p>Given loops, &lt;code>if&lt;/code> was straightforward.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>[&lt;span style="color:#fe8019">-&lt;/span> body ]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>For an &lt;code>else&lt;/code>, another flag starts at one and is cleared by the first body.&lt;/p>
&lt;p>And &lt;code>while&lt;/code> likewise.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>condition
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>[ 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> body
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> move to condition &lt;span style="color:#fe8019">and&lt;/span> calculate
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="artifact">Artifact&lt;/h2>
&lt;p style="text-align: center;">&lt;img src="http://epestr.com/images/rayfuck.png" alt="C version's output">&lt;/p>
&lt;p>The program was finally &lt;code>23MB&lt;/code>, which makes it a rather poor choice for a compression technique.&lt;sup class="sidenote-number">&lt;a href="#sidenote-7">[7]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-7">&lt;span class="sidenote-label">[7]&lt;/span> The image it was intended to produce was only about &lt;code>0.9MB&lt;/code>.&lt;/span>&lt;/p>
&lt;p>Using crude calculations, I found that it did 100 ray calculations per minute, that is, one pixel per minute. Given that the image was &lt;code>400x225&lt;/code>, my initial estimate should have been about 62.5 days on my laptop with no further optimizations, but I realized I&amp;rsquo;d only seen the sky, the bouncing around the spheres would delay the ETA by a lot. So the image above is an approximation of what would be rendered, made using the C code. Of the &lt;code>1229&lt;/code> pixels generated at the time of writing,&lt;sup class="sidenote-number">&lt;a href="#sidenote-8">[8]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-8">&lt;span class="sidenote-label">[8]&lt;/span> The complete &lt;code>400x225&lt;/code> render contains 90,000 pixels.&lt;/span> only &lt;code>10&lt;/code> differ, mostly by a value of one.&lt;/p>
&lt;p>Some optimisation is possible. Losing precision to reduce the number of cells touched is the first option if the ground can be approximated worse. The normalization step for random vectors can also be skipped, although that changes the scattering distribution, so it would no longer run the exact bit of code I aimed to reproduce here.&lt;/p>
&lt;h2 id="update">Update&lt;/h2>
&lt;p>A comment on my &lt;a class="citation-link" href="https://www.reddit.com/r/programming/comments/1wptfjd/" title="Reddit discussion — parallelizing the Brainfuck ray tracer">Reddit thread&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-5" aria-label="Reference 5">[5]&lt;/a>&lt;/sup> asked how I might improve it with fork/join primitives. Finding the challenge interesting, I got nerd-sniped into improving the JIT interpreter I used, which led to a massive improvement in its performance.&lt;sup class="sidenote-number">&lt;a href="#sidenote-9">[9]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-9">&lt;span class="sidenote-label">[9]&lt;/span> Earlier experiments with JIT compilers, and what I learned from them, helped with this work.&lt;/span> The actual render looks a bit like a Van Gogh painting, likely due to precision errors.&lt;/p>
&lt;p style="text-align: center;">&lt;img src="http://epestr.com/images/vangogh.png" alt="BF version's output">&lt;/p>
&lt;h2 id="bibliography">Bibliography&lt;/h2></content:encoded></item><item><title>A WiFi outage and its relation to a DEF CON 20 talk</title><link>http://epestr.com/blog/a-wifi-outage-and-its-relation-to-a-def-con-20-talk/</link><guid isPermaLink="true">http://epestr.com/blog/a-wifi-outage-and-its-relation-to-a-def-con-20-talk/</guid><pubDate>Mon, 11 May 2026 16:07:06 +0530</pubDate><description>&lt;p>On April 22, Tuesday, 13:29 IST, my WiFi broke. &lt;/br>
This was unexpected since I had switched from my older
&lt;a class="citation-link" href="https://wiki.archlinux.org/title/Wpa_supplicant" title="ArchWiki: wpa_supplicant">&lt;code>wpa_supplicant&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-0" aria-label="Reference 0">[0]&lt;/a>&lt;/sup> setup to
&lt;a class="citation-link" href="https://wiki.archlinux.org/title/Iwd" title="ArchWiki: iwd">&lt;code>iwd&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-1" aria-label="Reference 1">[1]&lt;/a>&lt;/sup> in the to avoid the frequent
disconnection that happens. I live in a hostel with a lot of access points and
the supplicant program decides when to switch as one moves around.&lt;sup class="sidenote-number">&lt;a href="#sidenote-1">[1]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-1">&lt;span class="sidenote-label">[1]&lt;/span> The access points belong to the same campus network; roaming is the client deciding which one to associate with as signal conditions change.&lt;/span>
A supplicant like &lt;a class="citation-link" href="https://wiki.archlinux.org/title/Wpa_supplicant" title="ArchWiki: wpa_supplicant">&lt;code>wpa_supplicant&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-0" aria-label="Reference 0">[0]&lt;/a>&lt;/sup>
oscillates between points on marginal signal differences and leaves me
disconnected for some time . I migrated to
&lt;a class="citation-link" href="https://wiki.archlinux.org/title/Iwd" title="ArchWiki: iwd">&lt;code>iwd&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-1" aria-label="Reference 1">[1]&lt;/a>&lt;/sup> to avoid this given its &lt;a class="citation-link" href="https://www.youtube.com/watch?v=QIqT2obSPDk" title="The New Wi-Fi Experience for Linux — Marcel Holtmann">better
roaming algorithm&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-2" aria-label="Reference 2">[2]&lt;/a>&lt;/sup> which is less
twitchy.&lt;/p></description><content:encoded>&lt;p>On April 22, Tuesday, 13:29 IST, my WiFi broke. &lt;/br>
This was unexpected since I had switched from my older
&lt;a class="citation-link" href="https://wiki.archlinux.org/title/Wpa_supplicant" title="ArchWiki: wpa_supplicant">&lt;code>wpa_supplicant&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-0" aria-label="Reference 0">[0]&lt;/a>&lt;/sup> setup to
&lt;a class="citation-link" href="https://wiki.archlinux.org/title/Iwd" title="ArchWiki: iwd">&lt;code>iwd&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-1" aria-label="Reference 1">[1]&lt;/a>&lt;/sup> in the to avoid the frequent
disconnection that happens. I live in a hostel with a lot of access points and
the supplicant program decides when to switch as one moves around.&lt;sup class="sidenote-number">&lt;a href="#sidenote-1">[1]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-1">&lt;span class="sidenote-label">[1]&lt;/span> The access points belong to the same campus network; roaming is the client deciding which one to associate with as signal conditions change.&lt;/span>
A supplicant like &lt;a class="citation-link" href="https://wiki.archlinux.org/title/Wpa_supplicant" title="ArchWiki: wpa_supplicant">&lt;code>wpa_supplicant&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-0" aria-label="Reference 0">[0]&lt;/a>&lt;/sup>
oscillates between points on marginal signal differences and leaves me
disconnected for some time . I migrated to
&lt;a class="citation-link" href="https://wiki.archlinux.org/title/Iwd" title="ArchWiki: iwd">&lt;code>iwd&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-1" aria-label="Reference 1">[1]&lt;/a>&lt;/sup> to avoid this given its &lt;a class="citation-link" href="https://www.youtube.com/watch?v=QIqT2obSPDk" title="The New Wi-Fi Experience for Linux — Marcel Holtmann">better
roaming algorithm&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-2" aria-label="Reference 2">[2]&lt;/a>&lt;/sup> which is less
twitchy.&lt;/p>
&lt;h2 id="how-enterprise-wifi-authentication-works">How enterprise WiFi authentication works&lt;/h2>
&lt;p>Enterprise WiFi like iitk-sec uses the &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Extensible_Authentication_Protocol" title="Extensible Authentication Protocol (EAP)">Extensible Authentication
Protocol&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-3" aria-label="Reference 3">[3]&lt;/a>&lt;/sup>
(EAP) for negotiation. It happens at &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Data_link_layer" title="OSI data-link layer">layer
2&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-4" aria-label="Reference 4">[4]&lt;/a>&lt;/sup>,&lt;sup class="sidenote-number">&lt;a href="#sidenote-2">[2]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-2">&lt;span class="sidenote-label">[2]&lt;/span> This ordering matters later: without an IP connection, ordinary TCP tools cannot directly query the authentication server.&lt;/span> and thus the laptop has to
prove its identity to the server before it connects with the general internet.
Then there is the bouncer (&lt;a class="citation-link" href="https://en.wikipedia.org/wiki/RADIUS" title="RADIUS authentication">RADIUS&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-5" aria-label="Reference 5">[5]&lt;/a>&lt;/sup>)
checking credentials against a &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Directory_service" title="Directory services">directory
service&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-6" aria-label="Reference 6">[6]&lt;/a>&lt;/sup> like &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Active_Directory" title="Microsoft Active Directory">Active
Directory&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-7" aria-label="Reference 7">[7]&lt;/a>&lt;/sup> and the tunnel
(&lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Protected_Extensible_Authentication_Protocol" title="Protected EAP (PEAP)">PEAP&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-8" aria-label="Reference 8">[8]&lt;/a>&lt;/sup>
or
&lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Extensible_Authentication_Protocol#EAP-TTLS" title="EAP-TTLS">TTLS&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-9" aria-label="Reference 9">[9]&lt;/a>&lt;/sup>)
creating an encrypted TLS channel between the laptop and the RADIUS server. And
the there is the payload, the inner method actually verifying the password.
PEAP almost always forces &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/MS-CHAP" title="MS-CHAPv2">MSCHAPv2&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-10" aria-label="Reference 10">[10]&lt;/a>&lt;/sup> as
it&amp;rsquo;s used in my school. TTLS is flexible enough to allow protocols like
&lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Password_Authentication_Protocol" title="Password Authentication Protocol (PAP)">PAP&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-11" aria-label="Reference 11">[11]&lt;/a>&lt;/sup> to pass
plaintext straight down the tunnel.&lt;/p>
&lt;h2 id="certificate-issues">Certificate issues&lt;/h2>
&lt;p>To check why I wasn&amp;rsquo;t connecting, the first thing I ran was &lt;code>dmesg&lt;/code>, then
&lt;code>journalctl -u iwd&lt;/code>. The logs had some answer in it, which I missed earlier.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>PEAP: Tunnel has disconnected with alert: bad_certificate
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>My school&amp;rsquo;s Computer Center&amp;rsquo;s setup guide states&lt;sup class="sidenote-number">&lt;a href="#sidenote-3">[3]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-3">&lt;span class="sidenote-label">[3]&lt;/span> The Computer Center, or CC, manages the campus infrastructure involved here.&lt;/span>
to set the CA certificate to &amp;ldquo;Do not validate&amp;rdquo; if the system certificate fails.
&lt;code>wpa_supplicant&lt;/code> lets you do that but &lt;code>iwd&lt;/code> deliberately doesn&amp;rsquo;t expose the
option, at least not easily. I didn&amp;rsquo;t want to skip certificate validation as
that would leave me susceptible to &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Evil_twin_%28wireless_networks%29" title="Evil-twin WiFi attacks">evil-twin
attacks&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-12" aria-label="Reference 12">[12]&lt;/a>&lt;/sup> which is
when a device faking the iitk-sec SSID intercepts authentication and walks away
with the credentials. &lt;code>iwd&lt;/code> is safer in this regard, enforcing server-side
identity verification. So what cound be the problem here? Among other things, I
guessed that the CC had changed the RADIUS server certificate, because of which
&amp;lsquo;iwd&amp;rsquo; couldn&amp;rsquo;t validate my connection as before.&lt;/p>
&lt;p>I needed the new certificate then. I had some options at my hand, running
&lt;code>openssl s_client&lt;/code>, but that didn&amp;rsquo;t work as EAP runs at layer 2 which is before
the system has an IP, so there&amp;rsquo;s no
&lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol" title="Transmission Control Protocol (TCP)">TCP&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-13" aria-label="Reference 13">[13]&lt;/a>&lt;/sup> endpoint to
query. Or I could have pulled the cert off another device, but &lt;code>iwd&lt;/code> itself
made it easier.&lt;/p>
&lt;p>Running &lt;code>iwd&lt;/code> with the TLS debug flag enables certificate dumping due to its
&lt;a class="citation-link" href="https://git.kernel.org/pub/scm/libs/ell/ell.git" title="Embedded Linux Library (ELL)">&lt;code>ell&lt;/code>&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-14" aria-label="Reference 14">[14]&lt;/a>&lt;/sup> backend, which I got
to know reading the source, and the rejected server certificate ends up in
&lt;code>/tmp&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>sudo IWD_TLS_DEBUG&lt;span style="color:#fe8019">=&lt;/span>&lt;span style="color:#d3869b">1&lt;/span> /usr/lib/iwd/iwd -d
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The fix involved moving &lt;code>iwd-tls-debug-server-cert.pem&lt;/code> from &lt;code>/tmp&lt;/code> to
&lt;code>/etc/ssl/certs/iitk-radius.pem&lt;/code> and pointing the &lt;code>iwd&lt;/code> profile at it
explicitly. Though
&lt;a class="citation-link" href="https://wiki.archlinux.org/title/NetworkManager" title="ArchWiki: NetworkManager">NetworkManager&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-15" aria-label="Reference 15">[15]&lt;/a>&lt;/sup> kept
overwriting it which stopped once I marked the file immutable with &lt;code>chattr +i&lt;/code>.
I restarted, and the TLS tunnel came up&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>TTLS: tls_rsa_verify:240 Peer signature verified
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="protocols-switcheroo">Protocols switcheroo&lt;/h2>
&lt;p>I looked deeper and found more issues. Now with the EAP negotiation.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>EAP server tried method 4 while client was configured for method 25 EAP
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>completed with eapFail
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The server was rejecting PEAP and proposing EAP-MD5 instead.&lt;sup class="sidenote-number">&lt;a href="#sidenote-4">[4]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-4">&lt;span class="sidenote-label">[4]&lt;/span> PEAP is Method 25, while EAP-MD5 is Method 4.&lt;/span> &lt;code>iwd&lt;/code> refused it, as it isn&amp;rsquo;t
particularly safe.&lt;sup class="sidenote-number">&lt;a href="#sidenote-5">[5]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-5">&lt;span class="sidenote-label">[5]&lt;/span> EAP-MD5 gets offered when nothing better is available.&lt;/span>&lt;/p>
&lt;p>The server was negotiating PEAP early morning.&lt;sup class="sidenote-number">&lt;a href="#sidenote-6">[6]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-6">&lt;span class="sidenote-label">[6]&lt;/span> I don&amp;rsquo;t have access to the CC servers, so the exact cause isn&amp;rsquo;t confirmable.&lt;/span> However, I found a detail in
PEAP authentication that might have been causing the issue.&lt;/p>
&lt;p>The RADIUS backend has to compute the response for the MSCHAPv2 challenge,
which means it requires either the plaintext password or an &lt;a class="citation-link" href="https://en.wikipedia.org/wiki/NTLM" title="NTLM and NT hashes">NT-hash-equivalent
credential&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-16" aria-label="Reference 16">[16]&lt;/a>&lt;/sup> on hand. MSCHAPv2 challenges
are two one, and the device solves a challenge sending both the answer and
password. And since this can&amp;rsquo;t work with one-way passwords, the RADIUS server
had to have stored plaintext or unsalted NT-hashed passwords. The CC probably
tightened backend hashing, unsalted NT hashes are a well-known liability. This
could have led to MSCHAPv2, and thus PEAP being disallowed. More details on how
MSCHAPv2 was broken can be found in this
&lt;a class="citation-link" href="https://www.youtube.com/watch?v=gkPvZDcrLFk" title="Divide and Conquer: Cracking MS-CHAPv2 — DEF CON 20">DEF CON 20 talk&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-17" aria-label="Reference 17">[17]&lt;/a>&lt;/sup>.&lt;/p>
&lt;h2 id="resolve">Resolve&lt;/h2>
&lt;p>So, MSCHAPv2 had to be scrapped, and PEAP with it. The CC guide lists TTLS as a
fallback, so I switched to
&lt;a class="citation-link" href="https://en.wikipedia.org/wiki/Extensible_Authentication_Protocol#EAP-TTLS" title="EAP-TTLS">EAP-TTLS&lt;/a>&lt;sup class="citation-number">&lt;a href="#reference-9" aria-label="Reference 9">[9]&lt;/a>&lt;/sup>
with PAP. PAP also skips certificate validation, as it work by transmitting the
password as plaintext along the TLS tunnel,&lt;sup class="sidenote-number">&lt;a href="#sidenote-7">[7]&lt;/a>&lt;/sup>&lt;span class="sidenote" id="sidenote-7">&lt;span class="sidenote-label">[7]&lt;/span> “Plaintext” here describes the inner authentication method. The outer TLS tunnel still encrypts it in transit.&lt;/span> but since &lt;code>iwd&lt;/code> already forces the
validation of the server&amp;rsquo;s identity in the first step, it&amp;rsquo;s should be fine.&lt;/p>
&lt;p>Profile update&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#fe8019">[Security]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#b8bb26;font-weight:bold">EAP-Method&lt;/span>&lt;span style="color:#fe8019">=&lt;/span>&lt;span style="color:#b8bb26">TTLS EAP-Identity=username&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#b8bb26;font-weight:bold">EAP-TTLS-Phase2-Method&lt;/span>&lt;span style="color:#fe8019">=&lt;/span>&lt;span style="color:#b8bb26">Tunneled-PAP EAP-TTLS-Phase2-Identity=username&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#b8bb26;font-weight:bold">EAP-TTLS-Phase2-Password&lt;/span>&lt;span style="color:#fe8019">=&lt;/span>&lt;span style="color:#b8bb26">password&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#b8bb26;font-weight:bold">EAP-TTLS-CACert&lt;/span>&lt;span style="color:#fe8019">=&lt;/span>&lt;span style="color:#b8bb26">/etc/ssl/certs/iitk-radius.pem&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>After updating the profile and restarting the service, the handshake finished
and the interface connected.&lt;/p>
&lt;p>Surely, it would have been easier to use &lt;code>wpa_supplicant&lt;/code> with &amp;ldquo;Do not
validate&amp;rdquo; and ignore the insecure fallbacks. But then I wouldn&amp;rsquo;t have gotten to
deepdive into network fundamentals. &lt;code>iwd&lt;/code> forced me to fix and understand the
broken system by being less permisive.&lt;/p>
&lt;h2 id="bibliography">Bibliography&lt;/h2></content:encoded></item></channel></rss>