<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us">
  <title type="html">dpitt.me</title>
  <link href="https://dpitt.me" rel="alternate" type="text/html" title="html"/>
  <link href="https://dpitt.me/feed.xml" rel="self" type="application/atom+xml" title="atom"/>
  <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>
  <id>https://dpitt.me</id>
  
  <entry>
    <title type="html">The Decade that Art Died</title>
    <link href="https://dpitt.me/forest/dsp-2T7C.html" rel="alternate" type="text/html"/>
    <published>Fri, 15 May 2026 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><em>This post contains a position that I can only tenuously defend, but it is
something I find myself thinking a lot about lately. I tried to work out some of
the details with a more learned (please read that with two syllables) friend,
but they were unconvinced as well. So what better option do I have than to post
that bad idea publicly so it will forever be associated with me.</em></p>
<hr />
<p>Here it is, plain and simple: the 90’s are the decade that art died.</p>
<p>Okay, so bear with me. To start, I have two disclosures. Well, one really, and
then a thought that followed as a result of it. I’ve been rereading Infinite
Jest. I read IJ in ‘08 when the web was atwitter about DFW because of his death,
I’d never heard of the man before then. When I read it in ‘08, it took me
probably 8 months to finish, but it also sparked something in me that I didn’t
know existed before. I’m not sure I would really be able to call myself a reader
before then, but this book woke something up in me. I’d say I understood about
30% of it at the time. I was 25. On the reread, I’ve found myself obsessing over
how much he got right, the video content via ‘TP’ (teleputer) is an uncanny
presage of Instagram-style front camera content with subtle beauty-related
filters. Hell, he even blessed one of the ETA boy’s parents with the invention
of Instacart:</p>
<blockquote>
<p>Ingersoll’s parents founded the Rhode Island version of the service where you
order groceries by TP and teenagers in fleets of station wagons bring them out
to you, instead of supermarkets.</p>
</blockquote>
<p>I guess what he failed to see is the globalization created by the Internet, or,
put another way, the erasure of locality. There isn’t a ‘Rhode Island version’
of Instacart, there is the homogenous convenience of having groceries delivered,
wherever you may find yourself. This is my segue into one of the pillars of the
case I’m trying to make, but it’s an imperfect one because, prima facie, it’s
stating the opposite of what I’m trying to say.</p>
<h2 id="i.-monoculture-the-end-thereof">I. Monoculture, the end thereof</h2>
<p>From an art/entertainment perspective, the 90’s were the zenith of monoculture
in the US. The art that made its way into the collective psyche of the country
was the last of which that was not fractured by the delocalization of the
Internet. It’s subtle, and it may feel like what I’m saying is in conflict with
my thesis, but return for a second to the Instacart point I made earlier: there
isn’t a ‘Rhode Island version’ of Instacart because the Internet homogenized
access, not culture. That uniform access destroyed the monoculture because of
what we did with it: find our people—which we could do, unprecedentedly, because
of its erasure of locality. But up to this point, monoculture had been
snowballing for decades, making commercial success within reach for real weirdos
who’d, today, be relegated to the artist class, toiling away in obscurity.</p>
<h3 id="exhibit-i-infinite-jest">Exhibit I: Infinite Jest</h3>
<p>Given this piece of art is the inspiration for this whole thesis, it of course
appears in the list of works of art by weirdos that had inordinate commercial
success. Imagine the author of a book today, which was deemed, “a
plutonium-dense, satirical quiz-kid opus that runs to almost a thousand pages
(not including footnotes)” making the morning talk show rounds. You can’t! Not
just because no one watches Good Morning America anymore, but because today this
type of book would be released quietly—especially given it was by a generally
unknown author at the time—to be chum to the Goodreads fishes. You might be
thinking, but, the Times and NPR and everyone else reviewed McCarthy’s (very
dense, abstract, and “arty”) final duology, but this isn’t a fair
counterexample. Cormac McCarthy, at the time, was the greatest living American
novelist. No, a fair comparison in my opinion is Greg Jackson’s <em>Dimensions of a
Cave</em>. Have you read it? Do you know anyone who has? I can tell you that you
certainly won’t find a review of it in the Times’ <em>Weekender</em><span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"><em>Dimensions</em> did make <a href="https://www.newyorker.com/magazine/2023/11/13/dimensions-of-a-cave-a-volga-tale-omega-farm-and-crossings">Briefly Noted in The New
Yorker</a>
upon its release, but it’s the only major publication I could find that
mentioned the book at all. It should be noted that Jackson is a regular
contributor of fiction to the magazine.<br />
<br />
</span></span>.</p>
<p>This is not because people don’t read anymore.</p>
<p><img src="/images/booksales.jpg" width="100%"></p>
<div class="quote-ref">
<p>It’s really hard to compile this data as a regular guy, so I asked ChatGPT to
help. It cited 12 sources for this data, from Statista, Association of American
Publishers, and Publishers Weekly.</p>
</div>
<p>It’s because the Internet access fractured our monoculture into a million little
pieces. The only fiction that floats to the top of our collective consciousness
is fiction that has the most overlap in the millions of tiny bubbles comprising
the Venn diagram of our current reality. Culture is determined a bell curve now,
while it used to be by megaphone.</p>
<h2 id="exhibit-ii-weirdo-cinema">Exhibit II: Weirdo Cinema</h2>
<p><img src="/images/videodrome.jpg" width="100%"></p>
<div class="quote-ref">
<p>A biological television with a gun from David Cronenberg’s <em>Videodrome</em></p>
</div>
<p>In the summer of 2005, I had a girl over to the apartment I shared with my
friend Brian. Her, Brian, and I sat down and watched <em>Videodrome</em>, stone cold
sober, all the way through. She had about 2-3 hours to hang, and that’s what I
chose for us to do. When it was over, she stood up from the couch and said, “I
think that’s the worst movie I’ve ever seen in my life” and left. I think if I
asked her today if that was still true, she’d say yes (and I could, ask her,
because we’re married and have 3 children now).</p>
<p>So, to be fair, <em>Videodrome</em> was released in the 80’s, in fact it looks like
Cronenberg’s peak was really in that decade, but in the 90’s, he put out <em>Crash</em>
and <em>Existenz</em>, Lynch put out <em>Lost Highway</em>, <em>Wild at Heart</em>, <em>Twin Peaks</em>, and
<em>Mulholland Drive</em>, and Kubrick released <em>Eyes Wide Shut</em>. With Lynch’s films in
particular, it’s an undeniable epoch in cinema. A time where free association
visual art passed mainstream muster. It was a time where a film needn’t be
tethered, not just a to linear narrative, but to a coherent one at all!</p>
<p><img src="/images/loglady.jpg" width="%100"></p>
<div class="quote-ref">
<p>Recall that <em>Twin Peaks</em> had a reoccurring character who communed with a magic log.</p>
</div>
<p>Today, the only thing making its way into the megaplex is that same top of the
bell curve bullshit, but it’s so much worse in the case of film. The Disney
Corporation has a cultural hegemony on an entire standard deviation around the
mean of that curve, and as studio power is centralized, art for art’s sake
suffers. Today’s version of arthouse cinema is an aesthetic commercialized. A24
has more recognition than the films it produces because it represents one of the
fractures of today’s culture. I don’t think even A24 would put out Mulholland
Drive today, excepting the fact that it’s David Lynch of course, and in ‘99 it
was distributed by Universal!</p>
<p>With the end of a monoculture came the need for the capitalist class of the art
world to adapt, and the natural adaptation was to stop scaling up and start
scaling out. But the result of scaling out, on smaller and smaller units, was
that the margin between profit and loss shrunk to damn near nothing. This has a
multifaceted effect. In the old days, a studio could afford to lob some real
capital-A art into the monoculture whether or not it was ever intended to be
commercially successful. For instance, during the 90’s, when Universal
distributed <em>Mulholland Drive</em>, they were buoyed by the blockbuster success of
the first two <em>Jurassic Park films</em>, making a Lynchian nonsense-fest a
worthwhile endeavor. But also, <em>Mulholland Drive</em> was profitable! Because we as
Americans were amenable to the nonsense in the first place! And this is exactly
my point. David Lynch is not to be fucked with. If Universal came in with focus
groups wanted to audience test the film, he wouldn’t have abided.</p>
<p><img src="/images/angrylynch.jpg" width="100%"></p>
<div class="quote-ref">
<p>“Fuckin’ A, man. Who gives a fucking shit how long a scene is.”</p>
</div>
<p>Mulholland Drive is art in the pure sense. An unalloyed vision. And we all went
to see it.</p> ]]>
    </content>

    
  </entry>
  
  <entry>
    <title type="html">With All Sincerity</title>
    <link href="https://dpitt.me/forest/dsp-8K0M.html" rel="alternate" type="text/html"/>
    <published>Thu, 13 Nov 2025 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><em>If God is unknowable, and language is the limit of our knowledge, and art pushes
the bounds of language, and true expression is dependent upon sincerity, then
sincerity is the only avenue to spiritual seriousness.</em></p>
<hr />
<p>This is what David Foster Wallace is getting at in his review of Feodor Frank’s
<em>Dostoyevsky</em>:</p>
<blockquote>
<p>Frank’s bio prompts us to ask ourselves why we seem to require of our art an
ironic distance from deep convictions or desperate questions, so that
contemporary writers either have to make jokes of them or else try to work them
in under cover of some formal trick like intertextual quotation or incongruous
juxtaposition, sticking the really urgent stuff inside asterisks as part of some
multivalent defamiliarization-flourish or some such shit.</p>
</blockquote>
<p>Which is to say, irony gives artists cover against sincerity. I think this gives
more weight to the meaning of “meaning” in “the death of meaning”—not meaning
literally, as in definition, but meaning as in the meaning of life.</p> ]]>
    </content>

    
  </entry>
  
  <entry>
    <title type="html">A Quiet (Personal) Revolution</title>
    <link href="https://dpitt.me/forest/dsp-4U8B.html" rel="alternate" type="text/html"/>
    <published>Fri, 16 May 2025 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><span class="para-begin"> In 2009</span>, Mark Fisher published
<em>Capitalist Realism: Is There No Alternative?</em> where he wrote that “it
is easier to imagine the end of the world than it is to imagine the
end of capitalism.” Amongst many other things (including a critical
viewing of Disney Pixar’s <em>WALL-E</em>), Fisher updates historical
materialism for the modern world. If our experience is formed by our
material conditions, as opposed the Hegelian notion that ideas form
our conditions, then so too must our mental health be driven by the
structural issues inherent in unchecked neoliberalism. This isn’t
driven by precarity alone; no, to Fisher, capitalism even codes our
desires, our identities. It would be Pescatarian<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">In one of my corners of the Dark Forest, I asked how one
would say “fisher-esque”, the best response was <em>pescatarian</em>.<br />
<br />
</span></span> to say that
<em>capitalism colonizes subjectivity</em>, which is to say it even defines
how we define ourselves. It enforces a normative form for the self—one
we could never obtain—while also trapping us, leaving us incapable of
seeing an alternative.</p>
<p>About 30 years before that, Deleuze wrote about the “Oedipal Triangle”
which—as he and Guattari were wont to do—was used as a illustration
for the normative power of Freudian psychoanalysis, the State, and the
family—three sides, completing the triangle. An Oedipalized individual
is under the influence of these three forces, he is formed by them
into a “productive member of society”. This the the delineation of the
same forces that Fisher wrote about in <em>Capitalist Realism</em>. Deleuze
and Guattari, however, <em>could</em> imagine an alternative, which they
deemed the Schizophrenic—the anti-oedipus—he is untethered, free in
thought and will, he’s a nomad following a line of flight. D&amp;G wrote
lyrically, like a fever dream, about an alternative to the normative
forces of capitalism and its epistemological gangsters. They gave this
radical state of being many names: the body without organs, the
schizophrenic, the nomad. D&amp;G wanted to eke out an archetype without
overcoding it, to try and say how something felt without saying what
it was. It’s helpful to think of their writing like poetry because
this is its task: expressing the inutterable, leaving the ripples of a
vibe on the surface of thought. The strictures of writing about the
ineffable prevented them from saying in clear terms what an
alternative would be, but nevertheless, they spent much of their
careers vacillating around the idea that there was one, and that its
pursuit was the pursuit of freedom.</p>
<p>About 70 years before that, Tolstoy was writing about these normative
forces as well, briefly in <em>On Anarchy</em>, but extensively in <em>The Law
of Love and the Law of Violence</em>. He was on the heels of Engels’ and
Marx’s writings on anarchy and communism, he was vehemently anti-war,
and he found himself trying to reconcile his reading of Jesus’
teachings with the hermeneutics of the Church. His reading was radical
and resulted in his excommunication. In Tolstoy’s mind, the very
ontology of the State <strong>is</strong> violence, while at the same time, the
prevailing message of Christ is in dialectical opposition to that: a
message of anti-violence, of love.</p>
<p><img src="/images/anarch.png" width="55%"></p>
<p>What we would call today Trotskyist—the belief in permanent revolution—is not
dissimilar, structurally, to Tolstoy’s vision. In <em>The Law of Love and the Law
of Violence</em> he wrote<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"><a href="https://theanarchistlibrary.org/library/leo-tolstoy-the-law-of-violence-and-the-law-of-love?v=1627426747#toc21"><em>The Law of Love and the Law of Violence</em></a><br />
<br />
</span></span>:</p>
<blockquote>
<p>[T]he eternal spirit that penetrates each and every one of us in unity
and fills us with the ambition to attain that which we ought; it is
the same spirit that urges the tree to grow towards the sun, the
flower to drop its seeds in autumn, and which urges us to strive after
God, thereby uniting ourselves. [..] Salvation does not lie in the
rituals and profession of faith, but in a clear understanding of the
meaning of our life.</p>
</blockquote>
<p>In <em>On Anarchy</em>, he elaborates on what he believes the pursuit of that
meaning leads to<span class="sidenote-wrapper"><label for="sn-2" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-2" class="margin-toggle"/><span class="sidenote"><a href="https://theanarchistlibrary.org/library/leo-tolstoy-on-anarchy"><em>On Anarchy</em></a><br />
<br />
</span></span>.</p>
<blockquote>
<p>But what should be done, I do unmistakably know. And if you ask:
“What will happen?”, then I reply that good will certainly happen;
because, acting in the way indicated by reason and love, I am acting
in accordance with the highest law known to me. [..] To use
violence is impossible; it would only cause reaction. To join the
ranks of the Government is also impossible—one would only become
its instrument. One course therefore remains—to fight the
Government by means of thought, speech, actions, life, neither
yielding to Government nor joining its ranks and thereby increasing
its power.</p>
</blockquote>
<p>To Tolstoy, the armature of revolution is love.</p>
<p>As I mentioned above, around this time Marx wrote about the end of the
State post-revolution, wherein the proletariat seize control from the
bourgeoisie and eradicate the idea of it. Marxism, in its original
form, was teleological; the goal wasn’t to simply raise up the
proletariat and defeat the bourgeoisie, it was to erase class
altogether. I won’t elaborate much further on Marxism here, as
thousands of gallons of ink and millions of bits have been spilled on
the topic (the four thinkers above and few dozen between them
notwithstanding) but I wanted to underscore the Marxist idea of
permanent revolution because it’s crucial to our quest. It’s a key
precept in Marxian logic and its modern, most orthodox
adherents. Materially, it refers to the fact that a socialist
revolution requires an interminable revolt against the forces of
capitalism, a vigilance unending. I’ll leave it there for now, but the
takeaway here is that the only successful revolution is one that goes
forever.</p>
<p>Just shy of two millennia before that, Jesus was giving warnings about
what happens when these normative forces are met with resistance.</p>
<blockquote>
<p>Do not think that I have come to bring peace to the earth; I have
not come to bring peace, but a sword.</p>
</blockquote>
<p>What Jesus is saying here isn’t about violence, it’s about
radicalism<span class="sidenote-wrapper"><label for="sn-3" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-3" class="margin-toggle"/><span class="sidenote">I think an alternative reading could be the “Armor of God”
where the sword is the Word, but this works too: Jesus’ message of
radicalism is polemical, an inherent source of division.<br />
<br />
</span></span>. The sword isn’t literal, it’s a metaphor for
division: the two references you find at this verse in the NRSV are
Luke 12:51-52<span class="sidenote-wrapper"><label for="sn-4" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-4" class="margin-toggle"/><span class="sidenote">Do you think that I have come to bring peace to the earth?
No, I tell you, but rather division! From now on five in one
household will be divided, three against two and two against
three[.]<br />
<br />
</span></span>, and Mark 13:12, both of which state the division
literally.</p>
<p>Let’s take it a little further. The most on the nose example has to be
Acts 2:44-45<span class="sidenote-wrapper"><label for="sn-5" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-5" class="margin-toggle"/><span class="sidenote">Just as Marx said about 2000 years later, “from each
according to his ability to each according to his need.”<br />
<br />
</span></span>:</p>
<blockquote>
<p>All who believed were together and had all things in common; they
would sell their possessions and goods and distribute the proceeds
to all, as any had need.</p>
</blockquote>
<p>Jesus’ teachings were radical in nature, he was aware of this
radicalization, and he talked about it directly. Unfortunately for
Christian Nationalist weirdos, Matthew 10:34 isn’t a call to arms;
it’s a warning about the effects of committing to a cause, because he
knew these effects have no limits. As the story goes, the Romans left
it up to his own people to decide his fate, and they chose to kill him
for his aberrations. He knew what would happen, and tought that
message of love and community anyway. It is through Tolstoy that I
begin to see that Jesus’ message is the missing alternative. A quiet
revolution.</p>
<hr />
<p>Mia Hansen-Løve’s wonderful film <em>Things to Come</em> is about this quiet
radicalism that I’m circling here. The film follows a middle-aged high
school philosophy teacher, Nathalie, whose life is turned upside down
when her husband decides to leave her. The most affecting part of the
film, though, is the thinking she does as she reorients after this
change in her trajectory. This culminates in a debate with a former
student who’s hit the eject button on life by joining a commune in the
mountains. She sees him as having a selfish and impotent vantage on
radicalism. After seeing a copy of Ted Kaczynski‘s manifesto on his
bookshelf, she tells him, “I hope you place more value on human life.”
While pushing against those normative forces, yes, her student did so
while denying the responsibilty he had to his fellow man. Why turn
yourself inside out if you’re the only one to benefit from it?</p>
<p>Radicalism is the reification of an absolute ideology. As we get
older, or at least as I get older, that conviction is alloyed by
nuance—black and white become grey through dilution. This does not
pollute utopia’s purity nor does it change our (again, my) fundamental
belief about what’s necessary to get there, but it does ground my
expectations in the material. That’s what Hansen-Løve is trying to
communicate through Nathalie. As you reach middle age, you have two
choices—quiet radicalism made intergenerational by conferring a moral
system and its sources to our children or to others’ as an educator,
or to eject and start an anarchist commune in the mountains. We
valorize the latter but it’s the former that gives me hope. That moral
system must be one rooted in brotherly love, turning towards one
another, and a constant vigilance for care. This requires the
permanent revolution of the self. When our material conditions impel
us to individuate, there is revolution in community.</p>
<blockquote>
<p>This alone is needed, will be successful. That is all I have to say.</p>
<p>And this is the will of God, the teaching of Christ. There can be
only one permanent revolution — a moral one: the regeneration of the
inner man. How is this revolution to take place? Nobody knows how it
will take place in humanity, but every man feels it clearly in
himself. And yet in our world <strong>everybody thinks of changing humanity,
and nobody thinks of changing himself</strong>.</p>
</blockquote>
<div class="quote-ref">
<p><em>On Anarchy</em>, Leo Tolstoy, 1900</p>
</div> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/god/index.html" term="god" label="god"/>
    
    <category scheme="https://dpitt.me/tags/self/index.html" term="self" label="self"/>
    
    <category scheme="https://dpitt.me/tags/philosophy/index.html" term="philosophy" label="philosophy"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Vectorization Talk</title>
    <link href="https://dpitt.me/forest/dsp-4M8H.html" rel="alternate" type="text/html"/>
    <published>Fri, 11 Apr 2025 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>A talk I gave which covers real world examples of vectorization by
using AES-XTS as a… vector.</p>
<p>Slides are <a href="/trex/vectorization/deck.html">here</a>.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/talk/index.html" term="talk" label="talk"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/assembly/index.html" term="assembly" label="assembly"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Vectorized XTS</title>
    <link href="https://dpitt.me/forest/dsp-7B7W.html" rel="alternate" type="text/html"/>
    <published>Fri, 14 Mar 2025 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>So first the tweak is encrypted and written to the stack.</p>
<h2 id="tweak-generation">Tweak Generation</h2>
<div class="sourceCode" id="cb1"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># load the initial tweak / iv in to zmm0</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>vbroadcasti32x4 	 (<span class="dt">$TW</span>),<span class="dt">%zmm0</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>vbroadcasti32x4 shufb_15_7(<span class="dt">%rip</span>),<span class="dt">%zmm8</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>mov 	 \<span class="wa">$0</span><span class="dt">xaa</span>,<span class="dt">$tmp1</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>kmovq 	 <span class="dt">$tmp1</span>,<span class="dt">%k2</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Because of the mask loaded into zmm8, this is taking the MSB from each 64-bit</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co"># half of each tweak in the vector and placing it in the LSB position.</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>vpshufb 	 <span class="dt">%zmm8</span>,<span class="dt">%zmm0</span>,<span class="dt">%zmm1</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="co"># The broadcasted tweaks are variably shifted left, by 0, 1, 2, or 3. This is</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="co"># the tweak multiplication step.</span></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a>vpsllvq const_dq3210(<span class="dt">%rip</span>),<span class="dt">%zmm0</span>,<span class="dt">%zmm4</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a><span class="co"># Those MSB to LSB bytes are then variably shifted _right_ by 8, 7, 6, 5;</span></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a><span class="co"># extracting (i.e., leaving only the) carry bits from each sequential tweak.</span></span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a>vpsrlvq const_dq5678(<span class="dt">%rip</span>),<span class="dt">%zmm1</span>,<span class="dt">%zmm2</span></span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a><span class="co"># The carry bits are then multiplied, carry-lessly, by the modulus of the GF</span></span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a><span class="co"># (0x87) held in $ZPOLY.</span></span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a>vpclmulqdq 	 \<span class="wa">$0</span><span class="dt">x0</span>,<span class="dt">$ZPOLY</span>,<span class="dt">%zmm2</span>,<span class="dt">%zmm3</span></span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a><span class="co"># zmm2 has the carried bits, and zmm4 has the multiplied/left</span></span>
<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a><span class="co"># shifted tweaks. Here we are selectively xor-ing every other 64-bit lane; the</span></span>
<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a><span class="co"># lsb of each shifted tweak with carry bits from each shifted tweak. They should</span></span>
<span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a><span class="co"># match—we save the msb, shift variably, then xor the msb&#39;s carried bits back in</span></span>
<span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a><span class="co"># at the bottom of the tweak where the number of bits matches, this is a literal</span></span>
<span id="cb1-27"><a href="#cb1-27" aria-hidden="true" tabindex="-1"></a><span class="co"># wrap around.</span></span>
<span id="cb1-28"><a href="#cb1-28" aria-hidden="true" tabindex="-1"></a>vpxorq 	 <span class="dt">%zmm2</span>,<span class="dt">%zmm4</span>,<span class="dt">%zmm4</span>{<span class="dt">%k2</span>}</span>
<span id="cb1-29"><a href="#cb1-29" aria-hidden="true" tabindex="-1"></a><span class="co"># now we xor the corrected carries in, if the carries were 0, this has no effect.</span></span>
<span id="cb1-30"><a href="#cb1-30" aria-hidden="true" tabindex="-1"></a><span class="co"># zmm9 now holds the first 4 tweaks.</span></span>
<span id="cb1-31"><a href="#cb1-31" aria-hidden="true" tabindex="-1"></a>vpxord 	 <span class="dt">%zmm4</span>,<span class="dt">%zmm3</span>,<span class="dt">%zmm9</span></span>
<span id="cb1-32"><a href="#cb1-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-33"><a href="#cb1-33" aria-hidden="true" tabindex="-1"></a><span class="co"># Here we do the same routine a second time, but the shifts are adjusted to</span></span>
<span id="cb1-34"><a href="#cb1-34" aria-hidden="true" tabindex="-1"></a><span class="co"># generate the next set from the same basis, zmm0, or the initial tweak.</span></span>
<span id="cb1-35"><a href="#cb1-35" aria-hidden="true" tabindex="-1"></a>vpsllvq const_dq7654(<span class="dt">%rip</span>),<span class="dt">%zmm0</span>,<span class="dt">%zmm5</span></span>
<span id="cb1-36"><a href="#cb1-36" aria-hidden="true" tabindex="-1"></a>vpsrlvq const_dq1234(<span class="dt">%rip</span>),<span class="dt">%zmm1</span>,<span class="dt">%zmm6</span></span>
<span id="cb1-37"><a href="#cb1-37" aria-hidden="true" tabindex="-1"></a>vpclmulqdq 	 \<span class="wa">$0</span><span class="dt">x0</span>,<span class="dt">%zmm25</span>,<span class="dt">%zmm6</span>,<span class="dt">%zmm7</span></span>
<span id="cb1-38"><a href="#cb1-38" aria-hidden="true" tabindex="-1"></a>vpxorq 	 <span class="dt">%zmm6</span>,<span class="dt">%zmm5</span>,<span class="dt">%zmm5</span>{<span class="dt">%k2</span>}</span>
<span id="cb1-39"><a href="#cb1-39" aria-hidden="true" tabindex="-1"></a>vpxord 	 <span class="dt">%zmm5</span>,<span class="dt">%zmm7</span>,<span class="dt">%zmm10</span></span>
<span id="cb1-40"><a href="#cb1-40" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-41"><a href="#cb1-41" aria-hidden="true" tabindex="-1"></a><span class="co"># Now that we have two sets, tweak generation is simpler, rather than beginning</span></span>
<span id="cb1-42"><a href="#cb1-42" aria-hidden="true" tabindex="-1"></a><span class="co"># from the initial tweak, we start from two generations back and tweak</span></span>
<span id="cb1-43"><a href="#cb1-43" aria-hidden="true" tabindex="-1"></a><span class="co"># generation is as simple as wrapping the msb around to the lsb, correcting the</span></span>
<span id="cb1-44"><a href="#cb1-44" aria-hidden="true" tabindex="-1"></a><span class="co"># new lsb, and zipping it into the shifted tweak.</span></span>
<span id="cb1-45"><a href="#cb1-45" aria-hidden="true" tabindex="-1"></a>vpsrldq 	 \<span class="wa">$0</span><span class="dt">xf</span>,<span class="dt">%zmm9</span>,<span class="dt">%zmm13</span></span>
<span id="cb1-46"><a href="#cb1-46" aria-hidden="true" tabindex="-1"></a>vpclmulqdq 	 \<span class="wa">$0</span><span class="dt">x0</span>,<span class="dt">%zmm25</span>,<span class="dt">%zmm13</span>,<span class="dt">%zmm14</span></span>
<span id="cb1-47"><a href="#cb1-47" aria-hidden="true" tabindex="-1"></a>vpslldq 	 \<span class="wa">$0</span><span class="dt">x1</span>,<span class="dt">%zmm9</span>,<span class="dt">%zmm11</span></span>
<span id="cb1-48"><a href="#cb1-48" aria-hidden="true" tabindex="-1"></a>vpxord 	 <span class="dt">%zmm14</span>,<span class="dt">%zmm11</span>,<span class="dt">%zmm11</span></span>
<span id="cb1-49"><a href="#cb1-49" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-50"><a href="#cb1-50" aria-hidden="true" tabindex="-1"></a>vpsrldq 	 \<span class="wa">$0</span><span class="dt">xf</span>,<span class="dt">%zmm10</span>,<span class="dt">%zmm15</span></span>
<span id="cb1-51"><a href="#cb1-51" aria-hidden="true" tabindex="-1"></a>vpclmulqdq 	 \<span class="wa">$0</span><span class="dt">x0</span>,<span class="dt">%zmm25</span>,<span class="dt">%zmm15</span>,<span class="dt">%zmm16</span></span>
<span id="cb1-52"><a href="#cb1-52" aria-hidden="true" tabindex="-1"></a>vpslldq 	 \<span class="wa">$0</span><span class="dt">x1</span>,<span class="dt">%zmm10</span>,<span class="dt">%zmm12</span></span>
<span id="cb1-53"><a href="#cb1-53" aria-hidden="true" tabindex="-1"></a>vpxord 	 <span class="dt">%zmm16</span>,<span class="dt">%zmm12</span>,<span class="dt">%zmm12</span></span></code></pre></div>
<p>The reason we need a generation gap is because, once we have a gap,
each tweak is just 1 byte left shifted of the previous set, when we’re
working from only a single generation back, we have to do the variable
shift. Either by 0/8 1/7 2/6 3/5 and then 1/7 2/6 3/5 4/4, both from
the basis of zmm0, or the same 0/8… route could be taken with zmm9 as
the basis for the second round.</p>
<p>There are performance reasons to use zmm0 a second time, the biggest
being that if we were to use zmm9 for the second basis, it chains the
computation of tweaks 4-7 to the completion of tweaks 0-3. If they
both compute independently off of zmm0, they can be parallelized via
<strong>ILP (instruction-level parallelization)</strong>.</p>
<div class="insight">
<div class="insight-header">
🧠 Insight
</div>
<p>Parallel tweak generation should be reusable, it’s exactly the same in
both directions. Maybe see if there is a way reuse a 8-tweak
generation routine.</p>
</div>
<p>One way to think about the <code>encrypt_by</code> implementations is that they
<em>flatten</em> the AES to XTS relationship. See below where the xor step
from XTS is inline with the steps of an AES round:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="dt">$code</span> .= <span class="ot">&lt;&lt;</span><span class="kw">___;</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="st"># xor Tweak values</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="st">vpxorq    </span><span class="dt">$tw1</span><span class="st">, </span><span class="dt">$st1</span><span class="st">, </span><span class="dt">$st1</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="st">vpxorq    </span><span class="dt">$tw2</span><span class="st">, </span><span class="dt">$st2</span><span class="st">, </span><span class="dt">$st2</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="st"># ARK</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="st">vbroadcasti32x4 0x80(</span><span class="dt">$TW</span><span class="st">), </span><span class="dt">$t0</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="st">vpxorq    </span><span class="dt">$t0</span><span class="st">, </span><span class="dt">$st1</span><span class="st">, </span><span class="dt">$st1</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="st">vpxorq    </span><span class="dt">$t0</span><span class="st">, </span><span class="dt">$st2</span><span class="st">, </span><span class="dt">$st2</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="kw">___</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="dv">0</span> == <span class="dt">$last_eight</span>) {</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a>  <span class="dt">$code</span> .= <span class="ot">&lt;&lt;</span><span class="kw">___;</span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a><span class="st">  vpsrldq		\$0xf, </span><span class="dt">$tw1</span><span class="st">, </span><span class="dt">%zmm13</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a><span class="st">  vpclmulqdq	\$0x0, </span><span class="dt">$ZPOLY</span><span class="st">, </span><span class="dt">%zmm13</span><span class="st">, </span><span class="dt">%zmm14</span></span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a><span class="st">  vpslldq		\$0x1, </span><span class="dt">$tw1</span><span class="st">, </span><span class="dt">%zmm15</span></span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a><span class="st">  vpxord		</span><span class="dt">%zmm14</span><span class="st">, </span><span class="dt">%zmm15</span><span class="st">, </span><span class="dt">%zmm15</span></span>
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a><span class="kw">___</span></span>
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true" tabindex="-1"></a><span class="co"># round 1</span></span>
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true" tabindex="-1"></a><span class="dt">$code</span> .= <span class="ot">&lt;&lt;</span><span class="kw">___;</span></span>
<span id="cb2-22"><a href="#cb2-22" aria-hidden="true" tabindex="-1"></a><span class="st">vbroadcasti32x4 0x90(</span><span class="dt">$TW</span><span class="st">), </span><span class="dt">$t0</span></span>
<span id="cb2-23"><a href="#cb2-23" aria-hidden="true" tabindex="-1"></a><span class="st">vaesenc  </span><span class="dt">$t0</span><span class="st">, </span><span class="dt">$st1</span><span class="st">, </span><span class="dt">$st1</span></span>
<span id="cb2-24"><a href="#cb2-24" aria-hidden="true" tabindex="-1"></a><span class="st">vaesenc  </span><span class="dt">$t0</span><span class="st">, </span><span class="dt">$st2</span><span class="st">, </span><span class="dt">$st2</span></span>
<span id="cb2-25"><a href="#cb2-25" aria-hidden="true" tabindex="-1"></a><span class="st">#...</span></span></code></pre></div>
<p>The <code>if (0 == $last_eight)</code> checks are generating the next
tweaks. Interspersing these this way allows for tweak generation to be
parallelized with the AES rounds because of OoO execution. Even if a
program is pinned to a single logical core, OoOE is still applicable
because there are 4 execution pipelines per physical core:</p>
<blockquote>
<p>The Front-end of the pipeline on recent Intel microarchitectures can
allocate four µOps per cycle, while the Back-end can retire four
µOps per cycle. From these capabilities the abstract concept of a
pipeline slot can be derived. A pipeline slot represents the
hardware resources needed to process one µOp. The Top-Down
Characterization assumes that for each CPU core, on each clock
cycle, there are four pipeline slots available.</p>
</blockquote>
<div class="quote-ref">
<p><a href="https://www.intel.com/content/www/us/en/docs/vtune-profiler/cookbook/2023-0/top-down-microarchitecture-analysis-method.html">Intel TMA</a></p>
</div>
<p>This works via the prefetcher, which can determine data dependencies.</p>
<h3 id="generating-a-single-tweak">Generating a single tweak</h3>
<div class="sourceCode" id="cb3"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>vmovdqa	shufb_15_7(<span class="dt">%rip</span>),<span class="dt">%xmm11</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>mov 	 \<span class="wa">$0</span><span class="dt">xaa</span>,<span class="dt">$tmp1</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>kmovq 	 <span class="dt">$tmp1</span>,<span class="dt">%k2</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>vpshufb 	 <span class="dt">%xmm11</span>,<span class="dt">%xmm0</span>,<span class="dt">%xmm12</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>vpsllq	\<span class="wa">$0</span><span class="dt">x1</span>,<span class="dt">%xmm0</span>,<span class="dt">%xmm13</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>vpsrlq \<span class="wa">$0</span><span class="dt">xf</span>,<span class="dt">%xmm12</span>,<span class="dt">%xmm14</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>vpclmulqdq 	 \<span class="wa">$0</span><span class="dt">x0</span>,<span class="dt">%xmm25</span>,<span class="dt">%xmm14</span>,<span class="dt">%xmm15</span> <span class="co"># just the first lane of ZPOLY</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>vpxorq 	 <span class="dt">%xmm13</span>,<span class="dt">%xmm14</span>,<span class="dt">%xmm13</span>{<span class="dt">%k2</span>}</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a>vpxord 	 <span class="dt">%xmm13</span>,<span class="dt">%xmm15</span>,<span class="dt">%xmm15</span></span></code></pre></div> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/assembly/index.html" term="assembly" label="assembly"/>
    
    
  </entry>
  
  <entry>
    <title type="html">XTS</title>
    <link href="https://dpitt.me/forest/dsp-6K6O.html" rel="alternate" type="text/html"/>
    <published>Wed, 12 Mar 2025 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><strong>IEEE 1619</strong></p>
<hr />
<p><strong>Things to Remember, that I keep forgetting:</strong></p>
<ul>
<li>A <strong>Data Unit</strong> can be many 128-bit blocks (the standard says up to
<span class="math inline">\(2^{20}\)</span> to be precise), but a single <strong>Data Unit</strong> has a single
<strong>Tweak</strong>.</li>
</ul>
<p><img src="/images/Screenshot 2025-02-12 at 11.19.42 AM.png"></p>
<ul>
<li><del>The <strong>Data Unit</strong> number is an input into the algorithm (where
does this happen in the assembly??).</del> It doesn’t. See below.</li>
</ul>
<div class="insight">
<div class="insight-header">
🧠 Insight
</div>
<p>The standard generates the tweak from the sector / <strong>Data Unit</strong>
Number, but OpenSSL does not do this, it treats the Tweak as a random
nonce/iv and encrypts it via <code>$key2</code>. I think the insight here is that it
makes OpenSSL’s implementation more general purpose by making the
initial tweak independent of key 2.</p>
<p><strong>The implication of this is that a call to an XTS cipher in OpenSSL
et al. is always on a single Data Unit.</strong></p>
</div>
<h2 id="cipher-stealing">Cipher Stealing</h2>
<ol type="1">
<li>Steal enough bytes from the last complete cipher text block for our
remaining bytes to make up a complete AES plaintext block.</li>
<li>Encrypt this block.</li>
<li>The last complete cipher text block is <em>replaced</em> with this new,
now complete, block.</li>
<li>the <em>unstolen</em> bytes from the previously last complete block are
then tacked on to the end as the remainder.</li>
</ol> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Working with the Garage Door Open</title>
    <link href="https://dpitt.me/forest/dsp-0H0E.html" rel="alternate" type="text/html"/>
    <published>Wed, 12 Mar 2025 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>When I converted this blog from the conventional reverse-chronological
ordering to something you had to follow links to get around in, like
it was a dark forest and you were wandering from tree to tree, it was
heavily influenced by the <a href="https://github.com/KasperZutterman/Second-Brain">many people who’d done it
before</a>. Even
“working with the garage door open” is
<a href="https://notes.andymatuschak.org/About_these_notes?stackedNotes=zCMhncA1iSE74MKKYQS5PBZ">borrowed</a>. I
found myself thinking again about that garage door this morning, after
a month or so of heavy notes-to-myself taking as I learned the ins and
outs of a number of new things at work, and it occurred to me that I
ought to act on that sentiment if it resonated with me so much. So
today I added a new key/value pair to post metadata called <strong>garage</strong>,
where I’ll denote the status of a page according to its completeness
&amp;c. I say &amp;c. because I think the in/out of the garage concept has
more to do with than just completeness or polish; I think in a lot of
ways, it comes down to intention. Something that I intended to be for
public consumption from the beginning, an essay or blog post I set to
write intentionally, may still be a WIP, it may be completed, or
something in between, but notes that I wrote while I learned something
that I never really intended to consider as a “piece of writing”
doesn’t really fit in that spectrum. Anyway, here are the states I’ll
be using for the <strong>garage</strong> key and what they mean:</p>
<ul>
<li><p><strong>open</strong>: you’re just seeing me work something out in the open,
these are notes mostly for me, it is not something that I edited or
thought about word choice or cadence or anything like that. It may
not “make sense” in a narrative or continuity way, but you may find
some useful nuggets of information.</p></li>
<li><p><strong>closed</strong>: The garage door is closed and I’m not home, I’m out
pitching this piece. Something with “<strong>garage</strong>: closed” was always
intended to be for public consumption, and is—modulo the handful
of typos I’m sure you’ll find—done. A piece labeled closed is more
like a conventional blog post.</p></li>
<li><p><strong>light on</strong>: A piece with public-facing intentions but is still a
work in progress. These I don’t think you’ll find much of, but if
you do it’s likely because I published to get input on, and you’re
seeing the progress through the frosted glass of the garage door.</p></li>
<li><p><strong>light off</strong>: A piece with public-facing intentions that was
abandoned or left unfinished.</p></li>
</ul> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/meta/index.html" term="meta" label="meta"/>
    
    
  </entry>
  
  <entry>
    <title type="html">A Sad Story About the Meaning of 'Constant Time'</title>
    <link href="https://dpitt.me/forest/dsp-4S2I.html" rel="alternate" type="text/html"/>
    <published>Mon,  7 Oct 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><em>A few weeks ago, I finally got the massive RSA OpenSSL port merged
into AWS-LC, after a months-long effort of documenting, refactoring,
and justifying someone else’s decisions</em><span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">You may recall that this is code that was originally written
by Intel upon the release of Icelake in 2017, but it had never
been ported to BoringSSL, which is the ancestor of AWS-LC.<br />
<br />
</span></span><em>. The most difficult
part of the documentation process was
<a href="https://github.com/aws/aws-lc/blob/bda01b40727126e8cc2ec1e375301700b0fa6ba7/crypto/fipsmodule/bn/rsaz_exp_x2.c#L346-L363">this</a>,
where a pretty wild optimization from the original <a href="https://eprint.iacr.org/2011/239.pdf">AMM
paper</a> needed to be
documented. It took me a few days to internalize it well enough to
know what exactly was going on, and how it corresponded to the
algorithm in the paper. On top of this, the <strong>extraction</strong> step of the
algorithm is implemented in
<a href="https://github.com/aws/aws-lc/blob/bda01b40727126e8cc2ec1e375301700b0fa6ba7/crypto/fipsmodule/bn/asm/rsaz-2k-avx512.pl#L505-L549">perlasm</a>
which, despite only being ~40 lines, is very abstruse. But, once I had
my bearings about me, I noticed an opportunity.</em></p>
<hr />
<h1 id="i.-the-algorithm">I. The Algorithm</h1>
<h2 id="motivation">Motivation</h2>
<p>What’s happening here is the modular exponentiation of very large
numbers, on the order of <span class="math inline">\(2^{1024}\)</span> (in the least)<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">RSA’s modulus is the product of two large primes <span class="math inline">\(p\)</span> and
<span class="math inline">\(q\)</span>, we call that product <span class="math inline">\(n\)</span>. In RSA 2k, <span class="math inline">\(p\)</span>’s and <span class="math inline">\(q\)</span>’s
bit-width is 1024.<br />
<br />
</span></span>. These
exponentiations are done with Montgomery Multiplication, which I have
documented elsewhere. The part I want to cover here <em>uses</em> that
routine, but does further optimizations on top of that implementation
to make these massive exponentiation problems more tractable to do on
a computer. The paper calls this “w-ary exponentiation”.</p>
<h2 id="in-detail">In Detail</h2>
<p>Compute <span class="math inline">\(a^b\)</span> by <em>windowing</em> the bits of <span class="math inline">\(b\)</span> into windows <span class="math inline">\(w\)</span>
of bit length <span class="math inline">\(l\)</span>.</p>
<table>
<tr>
<td>
<p>
<strong>Input:</strong>
</p>
</td>
<td>
<p>
<span class="math inline">\(a\)</span>, <span class="math inline">\(b\)</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
<strong>Return:</strong>
</p>
</td>
<td>
<p>
<span class="math inline">\(a^b\)</span>
</p>
</td>
</tr>
</table>
<p><strong>Formally, we’re computing</strong></p>
<p><span class="math display">\[
\textbf{exp}(a, w) = \prod_{i=0}^{|w|} (a^{w_i})^{2^{li}}
\]</span></p>
<p>Which is equivalent to <span class="math inline">\(a^b\)</span>.</p>
<p><strong>Step-by-Step:</strong></p>
<ol type="1">
<li>Precompute <span class="math inline">\(a^{[0 \ldots 2^l)}\)</span>, store the results in the
table <span class="math inline">\(T\)</span>, indexed by the exponent.</li>
<li>Initialize <strong>Result</strong> to <span class="math inline">\(1\)</span>.</li>
<li>For each <span class="math inline">\(w_i\)</span>:
<ol type="1">
<li>Look up the result of <span class="math inline">\(a^{w_i}\)</span> in <span class="math inline">\(T\)</span>, this is <span class="math inline">\(T_i\)</span>
(<span class="math inline">\(a^{w_i} = T_i\)</span>).</li>
<li>Square <span class="math inline">\(T_i\)</span> <span class="math inline">\(li\)</span> times, this is a temporary value we can
call <span class="math inline">\(t\)</span>. This squaring is what accounts for the current
window’s position in the whole of the larger exponent.</li>
<li><strong>Result</strong> is set to the current <strong>Result</strong> times <span class="math inline">\(t\)</span>.</li>
</ol></li>
</ol>
<h2 id="example">Example</h2>
<p>We’ll use this method to solve <span class="math inline">\(5^{273}\)</span> with <span class="math inline">\(l = 3\)</span>.</p>
<p>To begin, let’s ask Haskell to do this for us:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">&gt;</span> <span class="dv">5</span><span class="op">^</span><span class="dv">273</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="dv">65888737145190770152178587820437618798187258639803172187986018906925901818560997451564846065309816181644800521439740475440941611838012288617347746101822469899644829638418741524219512939453125</span></span></code></pre></div>
<p>Yikes. Let’s just call that <code>expected</code>:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">&gt;</span> expected <span class="ot">=</span> <span class="dv">5</span><span class="op">^</span><span class="dv">273</span></span></code></pre></div>
<p>Now let’s window the exponent into 3-bit windows.</p>
<pre><code>100 010 001
  4   2   1</code></pre>
<p>This means our reassembled exponentiation is</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">&gt;</span> <span class="op">:</span>{</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">|</span> (<span class="dv">1</span> <span class="op">*</span>                    <span class="co">-- Initial value</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">|</span>   ((<span class="dv">5</span><span class="op">^</span><span class="dv">1</span>) <span class="op">^</span> (<span class="dv">2</span><span class="op">^</span>(<span class="dv">3</span><span class="op">*</span><span class="dv">0</span>))) <span class="op">*</span> <span class="co">-- First window, 5^1 would be in the table.</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">|</span>   ((<span class="dv">5</span><span class="op">^</span><span class="dv">2</span>) <span class="op">^</span> (<span class="dv">2</span><span class="op">^</span>(<span class="dv">3</span><span class="op">*</span><span class="dv">1</span>))) <span class="op">*</span> <span class="co">-- Second window, 5^2 would be in the table.</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">|</span>   ((<span class="dv">5</span><span class="op">^</span><span class="dv">4</span>) <span class="op">^</span> (<span class="dv">2</span><span class="op">^</span>(<span class="dv">3</span><span class="op">*</span><span class="dv">2</span>)))   <span class="co">-- Third window, 5^4 would be in the table.</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">|</span> ) <span class="op">==</span> expected</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>ghci<span class="op">|</span> <span class="op">:</span>}</span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a><span class="dt">True</span></span></code></pre></div>
<h1 id="ii.-the-opportunity">II. The Opportunity</h1>
<p>In the preamble to this post I linked to some perlasm code which
constitutes the table lookup discussed in the previous section. This
routine takes a pointer to the table and a pair of indices. The table
is a nested array. The outer array holds the powers we’re looking for,
and each inner array is the collection of 64-bit digits needed to
represent that power. This particular implementation is looking up the
power of two separate exponents because the larger routine is doing
parallel exponentiations. For readability, I expanded and gently
scrubbed the perlasm below, and added comments.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># set up our iterator.</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>vmovdqa64   .Lones(<span class="dt">%rip</span>), <span class="dt">%ymm24</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="co"># broadcast the indices</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>vpbroadcastq    <span class="dt">%rdx</span>, <span class="dt">%ymm22</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>vpbroadcastq    <span class="dt">%rcx</span>, <span class="dt">%ymm23</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a><span class="co"># put a pointer to the end of the table in rax.</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a>leaq   <span class="dv">10240</span>(<span class="dt">%rsi</span>), <span class="dt">%rax</span> </span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a><span class="co"># zero out ymm0</span></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a>vpxor   <span class="dt">%xmm0</span>, <span class="dt">%xmm0</span>, <span class="dt">%xmm0</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a><span class="co"># zeroing out ymm21 and ymm[1-5,16-19]</span></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm21</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm1</span></span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm2</span></span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm3</span></span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm4</span></span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm5</span></span>
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm16</span></span>
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm17</span></span>
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm18</span></span>
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a>vmovdqa64   <span class="dt">%ymm0</span>, <span class="dt">%ymm19</span></span>
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a>.Lloop:</span>
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Check if our current index matches either of the given indices. When they</span></span>
<span id="cb5-28"><a href="#cb5-28" aria-hidden="true" tabindex="-1"></a>    <span class="co"># match, k1 or k2 is a mask of all 1&#39;s.</span></span>
<span id="cb5-29"><a href="#cb5-29" aria-hidden="true" tabindex="-1"></a>    vpcmpq  <span class="wa">$0</span>, <span class="dt">%ymm21</span>, <span class="dt">%ymm22</span>, <span class="dt">%k1</span></span>
<span id="cb5-30"><a href="#cb5-30" aria-hidden="true" tabindex="-1"></a>    vpcmpq  <span class="wa">$0</span>, <span class="dt">%ymm21</span>, <span class="dt">%ymm23</span>, <span class="dt">%k2</span></span>
<span id="cb5-31"><a href="#cb5-31" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-32"><a href="#cb5-32" aria-hidden="true" tabindex="-1"></a>    <span class="co"># vpblend brings over the bytes from the table when the indices match, but</span></span>
<span id="cb5-33"><a href="#cb5-33" aria-hidden="true" tabindex="-1"></a>    <span class="co"># leaves things in the destination register (because it is the same as the</span></span>
<span id="cb5-34"><a href="#cb5-34" aria-hidden="true" tabindex="-1"></a>    <span class="co"># second source register) when the indices do not match.</span></span>
<span id="cb5-35"><a href="#cb5-35" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  (<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-36"><a href="#cb5-36" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm0</span>, <span class="dt">%ymm0</span> {<span class="dt">%k1</span>}</span>
<span id="cb5-37"><a href="#cb5-37" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">32</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-38"><a href="#cb5-38" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm1</span>, <span class="dt">%ymm1</span> {<span class="dt">%k1</span>}</span>
<span id="cb5-39"><a href="#cb5-39" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">64</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-40"><a href="#cb5-40" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm2</span>, <span class="dt">%ymm2</span> {<span class="dt">%k1</span>}</span>
<span id="cb5-41"><a href="#cb5-41" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">96</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-42"><a href="#cb5-42" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm3</span>, <span class="dt">%ymm3</span> {<span class="dt">%k1</span>}</span>
<span id="cb5-43"><a href="#cb5-43" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">128</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-44"><a href="#cb5-44" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm4</span>, <span class="dt">%ymm4</span> {<span class="dt">%k1</span>}</span>
<span id="cb5-45"><a href="#cb5-45" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">160</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-46"><a href="#cb5-46" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm5</span>, <span class="dt">%ymm5</span> {<span class="dt">%k2</span>}</span>
<span id="cb5-47"><a href="#cb5-47" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">192</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-48"><a href="#cb5-48" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm16</span>, <span class="dt">%ymm16</span> {<span class="dt">%k2</span>}</span>
<span id="cb5-49"><a href="#cb5-49" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">224</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-50"><a href="#cb5-50" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm17</span>, <span class="dt">%ymm17</span> {<span class="dt">%k2</span>}</span>
<span id="cb5-51"><a href="#cb5-51" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">256</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-52"><a href="#cb5-52" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm18</span>, <span class="dt">%ymm18</span> {<span class="dt">%k2</span>}</span>
<span id="cb5-53"><a href="#cb5-53" aria-hidden="true" tabindex="-1"></a>    vmovdqu64  <span class="dv">288</span>(<span class="dt">%rsi</span>), <span class="dt">%ymm20</span></span>
<span id="cb5-54"><a href="#cb5-54" aria-hidden="true" tabindex="-1"></a>    vpblendmq  <span class="dt">%ymm20</span>, <span class="dt">%ymm19</span>, <span class="dt">%ymm19</span> {<span class="dt">%k2</span>}</span>
<span id="cb5-55"><a href="#cb5-55" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-56"><a href="#cb5-56" aria-hidden="true" tabindex="-1"></a>    <span class="co"># loop control. Increment each value in 21, bump the table pointer up, and</span></span>
<span id="cb5-57"><a href="#cb5-57" aria-hidden="true" tabindex="-1"></a>    <span class="co"># make sure we haven&#39;t reached the end of the table.</span></span>
<span id="cb5-58"><a href="#cb5-58" aria-hidden="true" tabindex="-1"></a>    vpaddq  <span class="dt">%ymm24</span>, <span class="dt">%ymm21</span>, <span class="dt">%ymm21</span></span>
<span id="cb5-59"><a href="#cb5-59" aria-hidden="true" tabindex="-1"></a>    addq    <span class="wa">$320</span>, <span class="dt">%rsi</span></span>
<span id="cb5-60"><a href="#cb5-60" aria-hidden="true" tabindex="-1"></a>    cmpq    <span class="dt">%rsi</span>, <span class="dt">%rax</span></span>
<span id="cb5-61"><a href="#cb5-61" aria-hidden="true" tabindex="-1"></a>    jne .Lloop</span>
<span id="cb5-62"><a href="#cb5-62" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-63"><a href="#cb5-63" aria-hidden="true" tabindex="-1"></a><span class="co"># copy the extract results to the destination pointer.</span></span>
<span id="cb5-64"><a href="#cb5-64" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm0</span>,  (<span class="dt">%rdi</span>)</span>
<span id="cb5-65"><a href="#cb5-65" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm1</span>,  <span class="dv">32</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-66"><a href="#cb5-66" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm2</span>,  <span class="dv">64</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-67"><a href="#cb5-67" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm3</span>,  <span class="dv">96</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-68"><a href="#cb5-68" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm4</span>,  <span class="dv">128</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-69"><a href="#cb5-69" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm5</span>,  <span class="dv">160</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-70"><a href="#cb5-70" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm16</span>, <span class="dv">192</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-71"><a href="#cb5-71" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm17</span>, <span class="dv">224</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-72"><a href="#cb5-72" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm18</span>, <span class="dv">256</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-73"><a href="#cb5-73" aria-hidden="true" tabindex="-1"></a>vmovdqu64   <span class="dt">%ymm19</span>, <span class="dv">288</span>(<span class="dt">%rdi</span>)</span>
<span id="cb5-74"><a href="#cb5-74" aria-hidden="true" tabindex="-1"></a>ret</span></code></pre></div>
<p>If you’re anything like me, once you’ve internalized the details of
the algorithm and understand the assembly above, you’ll also notice
that what is happening here is pretty inefficient. If we were writing
this in an imperative language, we’d probably do something like:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(n_windows):</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> idx <span class="op">==</span> i:</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>        out <span class="op">=</span> table[i]</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>        <span class="cf">break</span></span></code></pre></div>
<p>Because once we’ve found what we’re looking for, we should stop
looking! The opportunity we have here, though, is even better. The
results in the table are ordered, so we can compute <em>exactly</em> where
the power(s) we’re looking for is(are). Something like:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>out <span class="op">=</span> table[idx <span class="op">*</span> num_digits]</span></code></pre></div>
<p>Well, that implementation ends up looking like
<a href="https://github.com/pittma/aws-lc/blob/becc97a05c568fad2f13ea6ed46264ea1ffbd072/crypto/fipsmodule/bn/asm/rsaz-2k-avx512.pl#L505-L555">this</a>:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Copy the table pointer into %r11 so we can mess with it.</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>    movq <span class="dt">$red_tbl</span>, <span class="dt">%r11</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Calculate the offset for idx1</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>    mov \<span class="wa">$320</span>, <span class="dt">%r10d</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a>    imul <span class="dt">$red_tbl_idx1</span>, <span class="dt">%r10d</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Bump the pointer</span></span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a>    addq <span class="dt">%r10</span>, <span class="dt">%r11</span></span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Zero the temp register</span></span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a>    vpxor <span class="dt">%xmm0</span>, <span class="dt">%xmm0</span>, <span class="dt">%xmm0</span></span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Copy to the result pointer</span></span>
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 (<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, (<span class="dt">$out</span>)</span>
<span id="cb8-22"><a href="#cb8-22" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">32</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-23"><a href="#cb8-23" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">32</span>(<span class="dt">$out</span>)</span>
<span id="cb8-24"><a href="#cb8-24" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">64</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">64</span>(<span class="dt">$out</span>)</span>
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">96</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">96</span>(<span class="dt">$out</span>)</span>
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">128</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-29"><a href="#cb8-29" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">128</span>(<span class="dt">$out</span>)</span>
<span id="cb8-30"><a href="#cb8-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-31"><a href="#cb8-31" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-32"><a href="#cb8-32" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Reset table pointer</span></span>
<span id="cb8-33"><a href="#cb8-33" aria-hidden="true" tabindex="-1"></a>    subq <span class="dt">%r10</span>, <span class="dt">%r11</span></span>
<span id="cb8-34"><a href="#cb8-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-35"><a href="#cb8-35" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-36"><a href="#cb8-36" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Calculate the offset for idx2</span></span>
<span id="cb8-37"><a href="#cb8-37" aria-hidden="true" tabindex="-1"></a>    mov \<span class="wa">$320</span>, <span class="dt">%r10d</span></span>
<span id="cb8-38"><a href="#cb8-38" aria-hidden="true" tabindex="-1"></a>    imul <span class="dt">$red_tbl_idx2</span>, <span class="dt">%r10d</span></span>
<span id="cb8-39"><a href="#cb8-39" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-40"><a href="#cb8-40" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-41"><a href="#cb8-41" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Bump the pointer</span></span>
<span id="cb8-42"><a href="#cb8-42" aria-hidden="true" tabindex="-1"></a>    addq <span class="dt">%r10</span>, <span class="dt">%r11</span></span>
<span id="cb8-43"><a href="#cb8-43" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-44"><a href="#cb8-44" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-45"><a href="#cb8-45" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Re-zero the temp register</span></span>
<span id="cb8-46"><a href="#cb8-46" aria-hidden="true" tabindex="-1"></a>    vpxor <span class="dt">%xmm0</span>, <span class="dt">%xmm0</span>, <span class="dt">%xmm0</span></span>
<span id="cb8-47"><a href="#cb8-47" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-48"><a href="#cb8-48" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-49"><a href="#cb8-49" aria-hidden="true" tabindex="-1"></a>    <span class="co"># Copy to the result pointer</span></span>
<span id="cb8-50"><a href="#cb8-50" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">160</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-51"><a href="#cb8-51" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">160</span>(<span class="dt">$out</span>)</span>
<span id="cb8-52"><a href="#cb8-52" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">192</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-53"><a href="#cb8-53" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">192</span>(<span class="dt">$out</span>)</span>
<span id="cb8-54"><a href="#cb8-54" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">224</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-55"><a href="#cb8-55" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">224</span>(<span class="dt">$out</span>)</span>
<span id="cb8-56"><a href="#cb8-56" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">256</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-57"><a href="#cb8-57" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">256</span>(<span class="dt">$out</span>)</span>
<span id="cb8-58"><a href="#cb8-58" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dv">288</span>(<span class="dt">%r11</span>), <span class="dt">%ymm0</span></span>
<span id="cb8-59"><a href="#cb8-59" aria-hidden="true" tabindex="-1"></a>    vmovdqu64 <span class="dt">%ymm0</span>, <span class="dv">288</span>(<span class="dt">$out</span>)</span>
<span id="cb8-60"><a href="#cb8-60" aria-hidden="true" tabindex="-1"></a>    </span>
<span id="cb8-61"><a href="#cb8-61" aria-hidden="true" tabindex="-1"></a>    ret</span></code></pre></div>
<p>And yeah, this turns out to be a lot faster! I saw a ~4% improvement
in throughput for RSA 2k signing and I suspect this would be the same
or better for 3k and 4k. Unfortunately, the story does not end here.</p>
<h1 id="iii.-constant-time-with-scare-quotes">III. “Constant Time” (with scare quotes)</h1>
<p>If you’re a cryptographer and you’ve made it this far, I’m sorry. I
can imagine you screaming into your monitor as you read the previous
section.</p>
<p>To explain why a seasoned cryptographer would be getting paroxysmic
about the code I pasted above, I need to expand on the definition of
the term “constant time”.</p>
<h2 id="constant-time-without-scare-quotes">Constant Time (without scare quotes)</h2>
<p><strong>The part that I knew</strong> (which was wrong):</p>
<p>Don’t do things that are not constant in <em>time</em>. For example, division
on a computer is not constant unless the divisor is a power of 2.</p>
<p><strong>The part that I learned along the way:</strong></p>
<p>Don’t use secret data to do anything that is not constant in time
because it is a side-channel vulnerability. An attacker can determine
the details of your secrets by watching how long it takes you to do
stuff. If we’d done something like the loop with <code>break</code> above, this
wouldn’t be constant w/r/t time, but because it’s done with a
multiplication to jump directly to the result we’re looking for, I
thought I was safe.</p>
<p><strong>The part I didn’t learn until I made a fool of myself by offering
this optimization:</strong></p>
<p>“Time” in “constant time” actually means time <em>and space</em>. This means
you cannot use secret data to determine your memory accesses because
an attacker can suss out details about your secrets by watching your
memory access patterns. Because the windows we’re using as indices are
chunks of the exponent, and the exponent is secret, we can’t use it to
control our memory accesses.</p>
<p>Ultimately, it’s the class of functions in OpenSSL et al. that deal
with secret data that are called “constant-time”. That’s the
extent of its meaning. The problem with this optimization, though, is
called “data dependency”—its logic depends on secret data.</p>
<h2 id="how-exactly">How Exactly?</h2>
<p>Doing this would require two precise attack vectors, but just knowing
it’s possible is enough to avoid it altogether.</p>
<ol type="1">
<li><strong>Flush and reload</strong> to know the victim process is currently doing
an RSA signature by flushing the cache in the attacking process
which has the shared library loaded. The attacker can do this
because it knows which parts of the program are back in the cache
because the library is shared.</li>
<li>Then a <strong>prime and probe</strong> attack which does the opposite. The
attacking process fills the cache and then tracks which lines are
evicted. Through this it records, in order, the distance the
pointer moves during an RSA signature, which it knows is happening
because of the flush and reload attack. This can then be used to
decode the private exponent.</li>
</ol>
<h1 id="iv.-as-it-turns-out">IV. As It Turns Out</h1>
<p>So, as it turns out, that original implementation that walks the table
end-to-end and uses <code>vpblend</code> to copy the right data out was on
purpose! It has to be done this way so memory is always accessed the
same way regardless of what the indices are, preventing the leaking of
any secret information. This is what I get for thinking I was onto something!</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/programming/index.html" term="programming" label="programming"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Linear Algebra Fundamentals</title>
    <link href="https://dpitt.me/forest/dsp-8M1W.html" rel="alternate" type="text/html"/>
    <published>Mon,  3 Jun 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>I have never studied linear algebra at all, so we’re going back to the
beginning. Each header below are parts that I had to think hard about,
so it may skip over stuff.</p>
<h1 id="matrix-multiplication">Matrix Multiplication</h1>
<p>To multiply two matrices <span class="math inline">\(A\)</span> and <span class="math inline">\(B\)</span>, <span class="math inline">\(B\)</span> must have the same
number of rows as <span class="math inline">\(A\)</span> has columns. For the equation <span class="math inline">\(A \times B =
C\)</span>, each index in <span class="math inline">\(C\)</span> is determined by:</p>
<p><span class="math display">\[
C_{i, j} = \sum_k A_{i, k} B_{k, j}
\]</span></p>
<p>Where <span class="math inline">\(k\)</span> is that matched rows in <span class="math inline">\(B\)</span> and columns in <span class="math inline">\(A\)</span>—which
you can see in the equation above—is what we’re iterating over,
respectively.</p>
<p>Thinking about this by indices in <span class="math inline">\(C\)</span> was the missing link for
anytime I’ve tried to learn this in the past, it feels like one of
those things that’s obvious to everyone who knows it already.</p>
<h1 id="eigendecomposition">Eigendecomposition</h1>
<p>Not my first run-in with the Eigen-family of mathematical objects, but
it is my first time to buckle in and figure out how and why exactly
they work. The book sets this up by talking about representation and
gives the example of how an integer, whether represented in binary or
base-10, can still be represented via its prime
factors. Eigendecomposition is purportedly a way to represent matrices
via their functional properties.</p>
<p>The core idea is this: The eigendecomposition of a matrix <span class="math inline">\(A\)</span> is the
factorization <span class="math inline">\(A = Q \Lambda Q^{-1}\)</span>, where <span class="math inline">\(\Lambda\)</span> is a
diagonal matrix containing the eigenvalues of <span class="math inline">\(A\)</span>.</p>
<p>An eigenvalue has a corresponding vector <span class="math inline">\(\mathbf{v}\)</span> with respect
to <span class="math inline">\(A\)</span> s.t. <span class="math inline">\(A \mathbf{v} = \lambda \mathbf{v}\)</span> where <span class="math inline">\(\lambda\)</span>
is the eigenvalue corresponding to the vector <span class="math inline">\(\mathbf{v}\)</span>.</p>
<p>To compute Eigendecomposition we first find the eigenvalues and vectors.</p>
<p>For the following examples, we’ll use</p>
<p><span class="math display">\[
A = \begin{pmatrix}
4 &amp; 1 \\
2 &amp; 3
\end{pmatrix}
\]</span></p>
<h2 id="eigenvalues">Eigenvalues</h2>
<p>Solve <span class="math inline">\(\operatorname{det}(A - \lambda I) = 0\)</span>, the results are the
eigenvalues. <span class="math inline">\(I\)</span> is the identity matrix. The identity matrix is a
square matrix of a specific size which has 1’s along its diagonal and
0’s elsewhere.</p>
<h3 id="determinates">Determinates</h3>
<p><span class="math inline">\(\operatorname{det}\)</span> is a <em>determinate</em>. For 2x2 matrices of the form</p>
<p><span class="math display">\[
\begin{pmatrix}
a &amp; b \\
c &amp; d
\end{pmatrix}
\]</span></p>
<p><span class="math inline">\(\operatorname{det}\)</span> is simply <span class="math inline">\(ad - bc\)</span>, but apparently for
larger matrices, computing the determinate is much more complicated.</p>
<ol type="1">
<li>Compute the matrix difference, which is done elementwise. <span class="math inline">\(A -
\lambda I\)</span> becomes</li>
</ol>
<p><span class="math display">\[
\begin{pmatrix}
\lambda 1 &amp; \lambda 0 \\
\lambda 0 &amp; \lambda 1
\end{pmatrix} =
\begin{pmatrix}
\lambda &amp; 0 \\
0 &amp; \lambda
\end{pmatrix}
\]</span></p>
<p>so we end up with</p>
<p><span class="math display">\[
\begin{pmatrix}
4 - \lambda &amp; 1 - 0 \\
2 - 0 &amp; 3 - \lambda
\end{pmatrix} =
\begin{pmatrix}
4 - \lambda &amp; 1 \\
2 &amp; 3 - \lambda
\end{pmatrix}
\]</span></p>
<ol start="2" type="1">
<li>Then compute the determinate</li>
</ol>
<p><span class="math display">\[
\operatorname{det}(\begin{pmatrix}
4 - \lambda &amp; 1 \\
2 &amp; 3 - \lambda
\end{pmatrix}) = 
(4 - \lambda)(3 - \lambda) - (2 \times 1) = 
(4 - \lambda)(3 - \lambda) - 2 
\]</span></p>
<p>Which we FOIL-ify to</p>
<p><span class="math display">\[
12 - 4\lambda - 3\lambda + \lambda^2 - 2
\]</span></p>
<p>Yielding us the polynomial (recall that in computing the eigenvalues,
<span class="math inline">\(\operatorname{det}(A - \lambda I) = 0\)</span>).</p>
<p><span class="math display">\[
\lambda^2 - 7\lambda + 10 = 0
\]</span></p>
<p>The solutions to this polynomial are the eigenvalues. We factor into</p>
<p><span class="math display">\[
(\lambda - 5)(\lambda - 2) = 0
\]</span></p>
<p>Because <span class="math inline">\(-5 \times -2 = 10\)</span> and <span class="math inline">\(-5 + -2 = -7\)</span>, which we can then
isolate individually to get <span class="math inline">\(\lambda - 5 = 0\)</span> so <span class="math inline">\(\lambda = 5\)</span> and
<span class="math inline">\(\lambda - 2 = 0\)</span> so <span class="math inline">\(\lambda = 2\)</span>. <strong>These are our eigenvalues</strong>.</p>
<h2 id="eigenvectors">Eigenvectors</h2>
<p>For each eigenvalue <span class="math inline">\(\lambda_i\)</span>, we solve <span class="math inline">\((A - \lambda_i I)
\mathbf{v} = \mathbf{0}\)</span><span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">I’m using bold face here on <span class="math inline">\(\mathbf{0}\)</span> to denote the zero
vector.<br />
<br />
</span></span>.</p>
<p>Continuing with our example, first we solve with <span class="math inline">\(\lambda = 5\)</span>:</p>
<p>To start</p>
<p><span class="math display">\[
5I = \begin{pmatrix} 5 &amp; 0 \\ 0 &amp; 5 \end{pmatrix}
\]</span></p>
<p>Then</p>
<p><span class="math display">\[
\begin{pmatrix}
4 &amp; 1 \\ 
2 &amp; 3
\end{pmatrix} - 
\begin{pmatrix}
5 &amp; 0 \\
0 &amp; 5
\end{pmatrix} = 
\begin{pmatrix}
-1 &amp; 1 \\
2 &amp; -2
\end{pmatrix}
\]</span></p>
<p>So we’re solving</p>
<p><span class="math display">\[
\begin{pmatrix}
-1 &amp; 1 \\
2 &amp; -2
\end{pmatrix}
\begin{pmatrix}
v_1 \\
v_2
\end{pmatrix} =
\begin{pmatrix}
0 \\
0
\end{pmatrix}
\]</span></p>
<p>Which we can decompose (possibly a poor word choice) into</p>
<p><span class="math display">\[
-1v_1 + 1v_2 = 0 \\
2v_1 - 2v_2 = 0
\]</span></p>
<p>In both cases, we end up with a case s.t. <span class="math inline">\(v_1 = v_2\)</span>, so “the
simplest option” is “chosen”<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">I thought this may be an example of the axiom of choice,
choosing arbitrarily from the infinite set of equal tuples. After
asking around, consensus was that no, this is a “practical
selection”, not an arbitrary one.<br />
<br />
</span></span>, yielding us the eigenvector</p>
<p><span class="math display">\[
\begin{pmatrix}
1 \\
1
\end{pmatrix}
\]</span></p>
<p>with respect to <span class="math inline">\(\lambda = 5\)</span>.</p>
<p><strong>Let’s work through an example with our other eigenvalue, 2.</strong></p>
<p>To start</p>
<p><span class="math display">\[
2I = \begin{pmatrix} 2 &amp; 0 \\ 0 &amp; 2 \end{pmatrix}
\]</span></p>
<p>Then</p>
<p><span class="math display">\[
\begin{pmatrix}
4 &amp; 1 \\ 
2 &amp; 3
\end{pmatrix} - 
\begin{pmatrix}
2 &amp; 0 \\
0 &amp; 2
\end{pmatrix} = 
\begin{pmatrix}
2 &amp; 1 \\
2 &amp; 1
\end{pmatrix}
\]</span></p>
<p>So we’re solving</p>
<p><span class="math display">\[
\begin{pmatrix}
2 &amp; 1 \\
2 &amp; 1
\end{pmatrix}
\begin{pmatrix}
v_1 \\
v_2
\end{pmatrix} =
\begin{pmatrix}
0 \\
0
\end{pmatrix}
\]</span></p>
<p>Which we can decompose into</p>
<p><span class="math display">\[
2v_1 + 1v_2 = 0 \\
2v_1 + 1v_2 = 0
\]</span></p>
<p>These two equations are identical, and their simplest solution is</p>
<p><span class="math display">\[
\begin{pmatrix}
1 \\
-2
\end{pmatrix}
\]</span></p>
<h2 id="finally-we-arrive">Finally, we arrive</h2>
<p>Recall that <strong>The eigendecomposition of a matrix <span class="math inline">\(A\)</span> is the
factorization <span class="math inline">\(A = Q \Lambda Q^{-1}\)</span></strong>.</p>
<ul>
<li><p><span class="math inline">\(Q\)</span> consists of the two eigenvectors as columns:</p>
<p><span class="math display">\[
\begin{pmatrix}
1 &amp; 1 \\
1 &amp; -2
\end{pmatrix}
\]</span></p></li>
<li><p>And <span class="math inline">\(\Lambda\)</span> has the two eigenvalues along its diagonal, the
other elements are 0:</p>
<p><span class="math display">\[
\begin{pmatrix}
5 &amp; 0 \\
0 &amp; 2
\end{pmatrix}
\]</span></p></li>
</ul> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Notes from "Deep Learning", Goodfellow et al.</title>
    <link href="https://dpitt.me/forest/dsp-2B0B.html" rel="alternate" type="text/html"/>
    <published>Mon,  3 Jun 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><em>Generally, what you find here is a mix of learning directly from the
book and the results of conversations I had with ChatGPT.</em></p>
<p><em>The goal of this exploration is to arrive at an AI/ML proficiency via
first principles.</em></p>
<hr class='transclude-hr'/>
<p><span class="transclusion-title">Linear Algebra Fundamentals</span> <span class="transclusion-link">[<a href="/forest/dsp-8M1W.html">dsp-8M1W</a>]</span></p>
<p>I have never studied linear algebra at all, so we’re going back to the
beginning. Each header below are parts that I had to think hard about,
so it may skip over stuff.</p>
<h1 id="matrix-multiplication">Matrix Multiplication</h1>
<p>To multiply two matrices <span class="math inline">\(A\)</span> and <span class="math inline">\(B\)</span>, <span class="math inline">\(B\)</span> must have the same
number of rows as <span class="math inline">\(A\)</span> has columns. For the equation <span class="math inline">\(A \times B =
C\)</span>, each index in <span class="math inline">\(C\)</span> is determined by:</p>
<p><span class="math display">\[
C_{i, j} = \sum_k A_{i, k} B_{k, j}
\]</span></p>
<p>Where <span class="math inline">\(k\)</span> is that matched rows in <span class="math inline">\(B\)</span> and columns in <span class="math inline">\(A\)</span>—which
you can see in the equation above—is what we’re iterating over,
respectively.</p>
<p>Thinking about this by indices in <span class="math inline">\(C\)</span> was the missing link for
anytime I’ve tried to learn this in the past, it feels like one of
those things that’s obvious to everyone who knows it already.</p>
<h1 id="eigendecomposition">Eigendecomposition</h1>
<p>Not my first run-in with the Eigen-family of mathematical objects, but
it is my first time to buckle in and figure out how and why exactly
they work. The book sets this up by talking about representation and
gives the example of how an integer, whether represented in binary or
base-10, can still be represented via its prime
factors. Eigendecomposition is purportedly a way to represent matrices
via their functional properties.</p>
<p>The core idea is this: The eigendecomposition of a matrix <span class="math inline">\(A\)</span> is the
factorization <span class="math inline">\(A = Q \Lambda Q^{-1}\)</span>, where <span class="math inline">\(\Lambda\)</span> is a
diagonal matrix containing the eigenvalues of <span class="math inline">\(A\)</span>.</p>
<p>An eigenvalue has a corresponding vector <span class="math inline">\(\mathbf{v}\)</span> with respect
to <span class="math inline">\(A\)</span> s.t. <span class="math inline">\(A \mathbf{v} = \lambda \mathbf{v}\)</span> where <span class="math inline">\(\lambda\)</span>
is the eigenvalue corresponding to the vector <span class="math inline">\(\mathbf{v}\)</span>.</p>
<p>To compute Eigendecomposition we first find the eigenvalues and vectors.</p>
<p>For the following examples, we’ll use</p>
<p><span class="math display">\[
A = \begin{pmatrix}
4 &amp; 1 \\
2 &amp; 3
\end{pmatrix}
\]</span></p>
<h2 id="eigenvalues">Eigenvalues</h2>
<p>Solve <span class="math inline">\(\operatorname{det}(A - \lambda I) = 0\)</span>, the results are the
eigenvalues. <span class="math inline">\(I\)</span> is the identity matrix. The identity matrix is a
square matrix of a specific size which has 1’s along its diagonal and
0’s elsewhere.</p>
<h3 id="determinates">Determinates</h3>
<p><span class="math inline">\(\operatorname{det}\)</span> is a <em>determinate</em>. For 2x2 matrices of the form</p>
<p><span class="math display">\[
\begin{pmatrix}
a &amp; b \\
c &amp; d
\end{pmatrix}
\]</span></p>
<p><span class="math inline">\(\operatorname{det}\)</span> is simply <span class="math inline">\(ad - bc\)</span>, but apparently for
larger matrices, computing the determinate is much more complicated.</p>
<ol type="1">
<li>Compute the matrix difference, which is done elementwise. <span class="math inline">\(A -
\lambda I\)</span> becomes</li>
</ol>
<p><span class="math display">\[
\begin{pmatrix}
\lambda 1 &amp; \lambda 0 \\
\lambda 0 &amp; \lambda 1
\end{pmatrix} =
\begin{pmatrix}
\lambda &amp; 0 \\
0 &amp; \lambda
\end{pmatrix}
\]</span></p>
<p>so we end up with</p>
<p><span class="math display">\[
\begin{pmatrix}
4 - \lambda &amp; 1 - 0 \\
2 - 0 &amp; 3 - \lambda
\end{pmatrix} =
\begin{pmatrix}
4 - \lambda &amp; 1 \\
2 &amp; 3 - \lambda
\end{pmatrix}
\]</span></p>
<ol start="2" type="1">
<li>Then compute the determinate</li>
</ol>
<p><span class="math display">\[
\operatorname{det}(\begin{pmatrix}
4 - \lambda &amp; 1 \\
2 &amp; 3 - \lambda
\end{pmatrix}) = 
(4 - \lambda)(3 - \lambda) - (2 \times 1) = 
(4 - \lambda)(3 - \lambda) - 2 
\]</span></p>
<p>Which we FOIL-ify to</p>
<p><span class="math display">\[
12 - 4\lambda - 3\lambda + \lambda^2 - 2
\]</span></p>
<p>Yielding us the polynomial (recall that in computing the eigenvalues,
<span class="math inline">\(\operatorname{det}(A - \lambda I) = 0\)</span>).</p>
<p><span class="math display">\[
\lambda^2 - 7\lambda + 10 = 0
\]</span></p>
<p>The solutions to this polynomial are the eigenvalues. We factor into</p>
<p><span class="math display">\[
(\lambda - 5)(\lambda - 2) = 0
\]</span></p>
<p>Because <span class="math inline">\(-5 \times -2 = 10\)</span> and <span class="math inline">\(-5 + -2 = -7\)</span>, which we can then
isolate individually to get <span class="math inline">\(\lambda - 5 = 0\)</span> so <span class="math inline">\(\lambda = 5\)</span> and
<span class="math inline">\(\lambda - 2 = 0\)</span> so <span class="math inline">\(\lambda = 2\)</span>. <strong>These are our eigenvalues</strong>.</p>
<h2 id="eigenvectors">Eigenvectors</h2>
<p>For each eigenvalue <span class="math inline">\(\lambda_i\)</span>, we solve <span class="math inline">\((A - \lambda_i I)
\mathbf{v} = \mathbf{0}\)</span><span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">I’m using bold face here on <span class="math inline">\(\mathbf{0}\)</span> to denote the zero
vector.<br />
<br />
</span></span>.</p>
<p>Continuing with our example, first we solve with <span class="math inline">\(\lambda = 5\)</span>:</p>
<p>To start</p>
<p><span class="math display">\[
5I = \begin{pmatrix} 5 &amp; 0 \\ 0 &amp; 5 \end{pmatrix}
\]</span></p>
<p>Then</p>
<p><span class="math display">\[
\begin{pmatrix}
4 &amp; 1 \\ 
2 &amp; 3
\end{pmatrix} - 
\begin{pmatrix}
5 &amp; 0 \\
0 &amp; 5
\end{pmatrix} = 
\begin{pmatrix}
-1 &amp; 1 \\
2 &amp; -2
\end{pmatrix}
\]</span></p>
<p>So we’re solving</p>
<p><span class="math display">\[
\begin{pmatrix}
-1 &amp; 1 \\
2 &amp; -2
\end{pmatrix}
\begin{pmatrix}
v_1 \\
v_2
\end{pmatrix} =
\begin{pmatrix}
0 \\
0
\end{pmatrix}
\]</span></p>
<p>Which we can decompose (possibly a poor word choice) into</p>
<p><span class="math display">\[
-1v_1 + 1v_2 = 0 \\
2v_1 - 2v_2 = 0
\]</span></p>
<p>In both cases, we end up with a case s.t. <span class="math inline">\(v_1 = v_2\)</span>, so “the
simplest option” is “chosen”<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">I thought this may be an example of the axiom of choice,
choosing arbitrarily from the infinite set of equal tuples. After
asking around, consensus was that no, this is a “practical
selection”, not an arbitrary one.<br />
<br />
</span></span>, yielding us the eigenvector</p>
<p><span class="math display">\[
\begin{pmatrix}
1 \\
1
\end{pmatrix}
\]</span></p>
<p>with respect to <span class="math inline">\(\lambda = 5\)</span>.</p>
<p><strong>Let’s work through an example with our other eigenvalue, 2.</strong></p>
<p>To start</p>
<p><span class="math display">\[
2I = \begin{pmatrix} 2 &amp; 0 \\ 0 &amp; 2 \end{pmatrix}
\]</span></p>
<p>Then</p>
<p><span class="math display">\[
\begin{pmatrix}
4 &amp; 1 \\ 
2 &amp; 3
\end{pmatrix} - 
\begin{pmatrix}
2 &amp; 0 \\
0 &amp; 2
\end{pmatrix} = 
\begin{pmatrix}
2 &amp; 1 \\
2 &amp; 1
\end{pmatrix}
\]</span></p>
<p>So we’re solving</p>
<p><span class="math display">\[
\begin{pmatrix}
2 &amp; 1 \\
2 &amp; 1
\end{pmatrix}
\begin{pmatrix}
v_1 \\
v_2
\end{pmatrix} =
\begin{pmatrix}
0 \\
0
\end{pmatrix}
\]</span></p>
<p>Which we can decompose into</p>
<p><span class="math display">\[
2v_1 + 1v_2 = 0 \\
2v_1 + 1v_2 = 0
\]</span></p>
<p>These two equations are identical, and their simplest solution is</p>
<p><span class="math display">\[
\begin{pmatrix}
1 \\
-2
\end{pmatrix}
\]</span></p>
<h2 id="finally-we-arrive">Finally, we arrive</h2>
<p>Recall that <strong>The eigendecomposition of a matrix <span class="math inline">\(A\)</span> is the
factorization <span class="math inline">\(A = Q \Lambda Q^{-1}\)</span></strong>.</p>
<ul>
<li><p><span class="math inline">\(Q\)</span> consists of the two eigenvectors as columns:</p>
<p><span class="math display">\[
\begin{pmatrix}
1 &amp; 1 \\
1 &amp; -2
\end{pmatrix}
\]</span></p></li>
<li><p>And <span class="math inline">\(\Lambda\)</span> has the two eigenvalues along its diagonal, the
other elements are 0:</p>
<p><span class="math display">\[
\begin{pmatrix}
5 &amp; 0 \\
0 &amp; 2
\end{pmatrix}
\]</span></p></li>
</ul>
<hr class='transclude-hr' /> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/ai/index.html" term="ai" label="ai"/>
    
    <category scheme="https://dpitt.me/tags/machine learning/index.html" term="machine learning" label="machine learning"/>
    
    <category scheme="https://dpitt.me/tags/book/index.html" term="book" label="book"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Zero Knowledge Notes on "Attention is All You Need"</title>
    <link href="https://dpitt.me/forest/dsp-2P7F.html" rel="alternate" type="text/html"/>
    <published>Fri, 17 May 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>As I understand it, as a person with nearly zero knowledge,
transformers are the ML/AI architecture that got us to LLMs. Also as I
understand it, their origin is in the primary contribution from the
Google AI paper <em>Attention is All You Need</em>. This page will serve as
the central place for anything that I—so therefore possibly you—need
to know to understand their motivation, function, and caveats.</p>
<hr />
<p>The abstract is straight-up offensive:</p>
<blockquote>
<p>The <strong>dominant sequence transduction models</strong> are based on complex
recurrent or convolutional neural networks that include an encoder
and a decoder.</p>
</blockquote>
<p>(emphasis is mine, I have no idea what those words in
that…sequence…mean)</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/ml/index.html" term="ml" label="ml"/>
    
    <category scheme="https://dpitt.me/tags/ai/index.html" term="ai" label="ai"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/paper/index.html" term="paper" label="paper"/>
    
    <category scheme="https://dpitt.me/tags/transformers/index.html" term="transformers" label="transformers"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Message Digest V</title>
    <link href="https://dpitt.me/forest/dsp-5E7S.html" rel="alternate" type="text/html"/>
    <published>Wed,  1 May 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>md5 replaces, you guessed it, md4—the series of which was created by
Ronald Rivest, who is the R in <a href="/forest/dsp-9F0P.html">RSA</a>.</p>
<h1 id="algorithm">Algorithm</h1>
<p>Let’s say our message is <span class="math inline">\(M\)</span> whose length is <span class="math inline">\(l\)</span>.</p>
<ol type="1">
<li><p>First <span class="math inline">\(M\)</span> is divided into 512-bit blocks.</p></li>
<li><p>Pad the final block, first with a single <code>1</code>, then <code>0</code>’s until the
final block’s length is 448 bits (<span class="math inline">\(512 - 448\)</span>), the last 64 bits
are used to contain <span class="math inline">\(l\ \text{mod}\ 2^{64}\)</span>.</p></li>
<li><p>Initialize 4 32-bit values (these constants come directly from the
<a href="https://www.rfc-editor.org/rfc/rfc1321#section-3.3">RFC that specifies
MD5</a>)<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">I’m going to use Rust here instead of Haskell because bit
twiddling in Haskell is no fun at all.<br />
<br />
</span></span>.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> A<span class="op">:</span> [<span class="dt">u8</span><span class="op">;</span> <span class="dv">4</span>] <span class="op">=</span> [<span class="dv">0x01</span><span class="op">,</span> <span class="dv">0x23</span><span class="op">,</span> <span class="dv">0x45</span><span class="op">,</span> <span class="dv">0x67</span>]<span class="op">;</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> B<span class="op">:</span> [<span class="dt">u8</span><span class="op">;</span> <span class="dv">4</span>] <span class="op">=</span> [<span class="dv">0x89</span><span class="op">,</span> <span class="dv">0xab</span><span class="op">,</span> <span class="dv">0xcd</span><span class="op">,</span> <span class="dv">0xef</span>]<span class="op">;</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> C<span class="op">:</span> [<span class="dt">u8</span><span class="op">;</span> <span class="dv">4</span>] <span class="op">=</span> [<span class="dv">0xfe</span><span class="op">,</span> <span class="dv">0xdc</span><span class="op">,</span> <span class="dv">0xba</span><span class="op">,</span> <span class="dv">0x98</span>]<span class="op">;</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> D<span class="op">:</span> [<span class="dt">u8</span><span class="op">;</span> <span class="dv">4</span>] <span class="op">=</span> [<span class="dv">0x76</span><span class="op">,</span> <span class="dv">0x54</span><span class="op">,</span> <span class="dv">0x32</span><span class="op">,</span> <span class="dv">0x10</span>]<span class="op">;</span></span></code></pre></div>
<p>Wikipedia actually has these buffers as 32-bit hex values, in
little endian form, e.g. <code>A = 0x67452301</code>. We can of course get
there from here with a little bit fiddling:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> <span class="kw">mut</span> a<span class="op">:</span> <span class="dt">u32</span> <span class="op">=</span> A[<span class="dv">0</span>]<span class="op">.</span>into()<span class="op">;</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>a <span class="op">|=</span> (A[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u32</span>) <span class="op">&lt;&lt;</span> <span class="dv">8</span><span class="op">;</span> </span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>a <span class="op">|=</span> (A[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u32</span>) <span class="op">&lt;&lt;</span> <span class="dv">16</span><span class="op">;</span> </span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>a <span class="op">|=</span> (A[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u32</span>) <span class="op">&lt;&lt;</span> <span class="dv">24</span><span class="op">;</span> </span></code></pre></div></li>
<li><p>Next we define 4 functions</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">// F(X,Y,Z) = XY v not(X) Z</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> f(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>    (x <span class="op">&amp;</span> y) <span class="op">|</span> (<span class="op">!</span>x <span class="op">&amp;</span> z)</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co">// G(X,Y,Z) = XZ v Y not(Z)</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> g(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>    (x <span class="op">&amp;</span> z) <span class="op">|</span> (y <span class="op">&amp;</span> <span class="op">!</span>z)</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a><span class="co">// H(X,Y,Z) = X xor Y xor Z</span></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> h(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a>    x <span class="op">^</span> y <span class="op">^</span> z</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a><span class="co">// I(X,Y,Z) = Y xor (X v not(Z))</span></span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> i(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a>    y <span class="op">^</span> (x <span class="op">|</span> <span class="op">!</span>z)</span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>And another constant<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">Wikipedia actually contains the precomputed table for <span class="math inline">\(T\)</span>, I’d
imagine that most real-life implementations do the same.<br />
<br />
</span></span>, a set <span class="math inline">\(T\)</span>, where <span class="math inline">\(T_i = 2^{32} *
|\textbf{sin}(i)|\)</span> and <span class="math inline">\(i = [1\ldots64]\)</span>.</p></li>
<li><p>There is then 4 rounds of several permutations between 512-bit
chunks of the payload, 32-bits at a time, members of the <span class="math inline">\(T\)</span> set,
and <span class="math inline">\(A\)</span>, <span class="math inline">\(B\)</span>, <span class="math inline">\(C\)</span>, and <span class="math inline">\(D\)</span>, where the results of those
rounds are accumulated in those variables; it’s all too long to
type out here, to see the details of these rounds, click
<a href="https://www.ietf.org/rfc/rfc1321.html#section-3.4">here</a>.</p></li>
<li><p>The final result is the concatenation of those 32-bit variables a
into 128-bit hexadecimal value.</p></li>
</ol>
<p>I have a non-optimized (both in terms of performance and in terms of
the repetitive nature of handwriting an MD5 implementation)
implementation in rust
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=faf376ada0822e3b54c40a6c20858f38">here</a>,
if you want to see the whole thing end-to-end.</p>
<h1 id="weird-things-ive-come-across-so-far">Weird things I’ve come across so far</h1>
<h2 id="not-x-y-x-z-actually">Not <code>(x &amp; y) | (!x &amp; z)</code> actually</h2>
<p><span class="math inline">\(F\)</span>, the function used in the first round, does the following: <span class="math inline">\((x
\land y) \lor (\neg x \land z)\)</span>. At least…on paper. Implementations
typically do this instead: <span class="math inline">\(z \oplus (x \land (y \oplus z))\)</span>. And it
turns out <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=b32a0ed73a1b848d416863748cf5b5ee">these are the same
thing</a>.</p>
<p>The cool thing about the latter is that it’s only 3 operations—2 XORs
and an AND—but also that it’s right-associative. It’s a fold. This
means that the second set of operations can accumulate into a single
register, while with the former needs a register for the LHS of the
outer OR, and one for the RHS, after which an OR can be applied to the
two.</p>
<h1 id="avx-512-ifying-this">AVX-512-ifying this</h1>
<p>There are multiple lines of thinking to be done here, I’ll start with
the simplest.</p>
<h2 id="vpternlog"><code>vpternlog</code></h2>
<p>The instruction <code>vpternlog[d|q]</code> is a SIMD instruction
that can do ternary operations on data in its operands. Intel has also
been so kind as to provide implementations for the round functions
above (<span class="math inline">\(F \ldots I\)</span>), which are delineated by passing <code>vpternlog</code> one
of these constants:</p>
<table>
<thead>
<tr>
<th>Function</th>
<th>Expression</th>
<th>Constant</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="math inline">\(F\)</span></td>
<td><span class="math inline">\((x \land y) \lor (\neg x \land z)\)</span></td>
<td><code>0xca</code></td>
</tr>
<tr>
<td><span class="math inline">\(G\)</span></td>
<td><span class="math inline">\((x \land z) \lor (y \land \neg z)\)</span></td>
<td><code>0xe4</code></td>
</tr>
<tr>
<td><span class="math inline">\(H\)</span></td>
<td><span class="math inline">\(x \oplus y \oplus z\)</span></td>
<td><code>0x96</code></td>
</tr>
<tr>
<td><span class="math inline">\(I\)</span></td>
<td><span class="math inline">\(y \oplus (x \lor z)\)</span></td>
<td><code>0x39</code></td>
</tr>
</tbody>
</table>
<p>So essentially what we need to do is load up <code>x</code>, <code>y</code>, and <code>z</code> into
vectors and let <code>vpternlog</code> go at them to do these operations for us.</p>
<h2 id="current-implementation">Current Implementation</h2>
<p>Here’s the round 1 function.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># round1_step() does:</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="co">#   dst = x + ((dst + F(x,y,z) + X[k] + T_i) &lt;&lt;&lt; s)</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co">#   %r10d = X[k_next]</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="co">#   %r11d = z&#39; (copy of z for the next step)</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Each round1_step() takes about 5.3 clocks (9 instructions, 1.7 IPC)</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="kw">sub </span><span class="fu">round1_step</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a>    <span class="kw">my</span> (<span class="dt">$pos</span>, <span class="dt">$dst</span>, <span class="dt">$x</span>, <span class="dt">$y</span>, <span class="dt">$z</span>, <span class="dt">$k_next</span>, <span class="dt">$T_i</span>, <span class="dt">$s</span>) = <span class="dt">@_</span>;</span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a>    <span class="dt">$code</span> .= <span class="ot">&quot;</span><span class="st"> mov	0*4(</span><span class="dt">%rsi</span><span class="st">),	</span><span class="dt">%r10d</span><span class="st">		/* (NEXT STEP) X[0] */</span><span class="ch">\n</span><span class="ot">&quot;</span> <span class="cf">if</span> (<span class="dt">$pos</span> == -<span class="dv">1</span>);</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a>    <span class="dt">$code</span> .= <span class="ot">&quot;</span><span class="st"> mov	</span><span class="dt">%edx</span><span class="st">,		</span><span class="dt">%r11d</span><span class="st">		/* (NEXT STEP) z&#39; = </span><span class="dt">%edx</span><span class="st"> */</span><span class="ch">\n</span><span class="ot">&quot;</span> <span class="cf">if</span> (<span class="dt">$pos</span> == -<span class="dv">1</span>);</span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a>    <span class="dt">$code</span> .= <span class="ot">&lt;&lt;</span><span class="kw">EOF;</span></span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a><span class="st">	xor	</span><span class="dt">$y</span><span class="st">,		</span><span class="dt">%r11d</span><span class="st">		/* y ^ ... */</span></span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a><span class="st">	lea	</span><span class="dt">$T_i</span><span class="st">(</span><span class="dt">$dst</span><span class="st">,</span><span class="dt">%r10d</span><span class="st">),</span><span class="dt">$dst</span><span class="st">		/* Const + dst + ... */</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a><span class="st">	and	</span><span class="dt">$x</span><span class="st">,		</span><span class="dt">%r11d</span><span class="st">		/* x &amp; ... */</span></span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a><span class="st">	xor	</span><span class="dt">$z</span><span class="st">,		</span><span class="dt">%r11d</span><span class="st">		/* z ^ ... */</span></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a><span class="st">	mov	</span><span class="dt">$k_next</span><span class="st">*4(</span><span class="dt">%rsi</span><span class="st">),</span><span class="dt">%r10d</span><span class="st">		/* (NEXT STEP) X[</span><span class="dt">$k_next</span><span class="st">] */</span></span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a><span class="st">	add	</span><span class="dt">%r11d</span><span class="st">,		</span><span class="dt">$dst</span><span class="st">		/* dst += ... */</span></span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a><span class="st">	rol	\$</span><span class="dt">$s</span><span class="st">,		</span><span class="dt">$dst</span><span class="st">		/* dst &lt;&lt;&lt; s */</span></span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a><span class="st">	mov	</span><span class="dt">$y</span><span class="st">,		</span><span class="dt">%r11d</span><span class="st">		/* (NEXT STEP) z&#39; = </span><span class="dt">$y</span><span class="st"> */</span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a><span class="st">	add	</span><span class="dt">$x</span><span class="st">,		</span><span class="dt">$dst</span><span class="st">		/* dst += x */</span></span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a><span class="kw">EOF</span></span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p><span class="math inline">\(F\)</span> is found in the lines (recall that it’s implemented as <span class="math inline">\(z
\oplus (x \land (y \oplus z))\)</span>).</p>
<pre><code>	xor	$y,		%r11d		/* y ^ ... */
	and	$x,		%r11d		/* x &amp; ... */
	xor	$z,		%r11d		/* z ^ ... */</code></pre>
<h2 id="avx-512-implementation-ideas">AVX-512 Implementation Ideas</h2>
<h3 id="basic-vpternlog-impl">Basic <code>vpternlog</code> impl</h3>
<p>An AVX-512 implementation would use the vector registers for <code>A</code>, <code>B</code>,
<code>C</code>, and <code>D</code>, as opposed to <code>%r11d</code>, mask out everything but the
bottom 32 bits, and then, I guess, use <code>vadd</code> instructions for the
additions?</p>
<h3 id="parallel-additions">Parallel additions</h3>
<p>These inner three additions <code>(dst + F(x,y,z) + X[k] + T_i)</code> could
theoretically be done in parallel, if we can get the vectors set up in
few enough instructions to make it worth doing this in parallel.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Software Implementation of Modular Exponentiation, Using Advanced Vector Instructions Architectures</title>
    <link href="https://dpitt.me/forest/dsp-9X1T.html" rel="alternate" type="text/html"/>
    <published>Tue, 16 Apr 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>This was published in <a href="https://link.springer.com/book/10.1007/978-3-642-31662-3">Arithmetic of Finite
Fields</a>, but
I’ve grabbed just this chapter <a href="/files/sime.pdf">here</a>.</p>
<p>This paper goes into detail about the algorithms used, originally for
AVX2, the x86 instructions that those algorithms employ, and so on,
but what I was really looking for is the origin of the 52-bit
representation found in the current AVX-512 implementation found in
OpenSSL, <a href="https://github.com/aws/aws-lc/pull/1273">soon AWS-LC</a>, and
maybe some day in BoringSSL. It turns out, this paper contains that
origin, only here we’re talking about the conversion of 32-bit numbers
to 29-bit numbers (arbitrarily, I think) and are given the following
equation as the way in which to convert numbers from one radix to
another.</p>
<p><span class="math display">\[
A = \sum^{k-1}_{i=0} X_i \times 2^{m \times i}
\]</span></p>
<p>Where <span class="math inline">\(A\)</span> is the value we wish to represent, <span class="math inline">\(X_i\)</span> is the value of
the <span class="math inline">\(i\)</span>th <em>window</em>, or as the paper calls it, the <span class="math inline">\(i\)</span>th digit, and
<span class="math inline">\(m\)</span> is the window size in bits.</p>
<p>So, say we want to represent the 16-bit number 24023 in windows of 5 bits.</p>
<pre><code>0101110111010111</code></pre>
<p>We think of as the following windows:</p>
<pre><code>0 10111 01110 10111</code></pre>
<p>Where <span class="math inline">\(m = 5\)</span> and <span class="math inline">\(k = 4\)</span>, giving us</p>
<pre><code>0   10111              01110             10111
0 + (23 * 2^(5 * 2)) + (14 *2^(5 * 1)) + (23 * 2^(5 * 0))
0 + 23552            + 448             + 23               = 24023</code></pre>
<p>This also holds if we choose 3-bit windows:</p>
<pre><code>0   101               110               111               010               111
0 + (5 * 2^(3 * 4)) + (6 * 2^(3 * 3)) + (7 * 2^(3 * 2)) + (2 * 2^(3 * 1)) + (7 * 2^(3 * 0))
0 + 20480           + 3072            + 448             + 16              + 7               = 24023</code></pre>
<p>This representation is the one used in the AVX-512 implementation
where <span class="math inline">\(m = 52\)</span>, carving up, for instance, the 1024-bit <code>bignum</code>s
into 52-bit windows.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/paper notes/index.html" term="paper notes" label="paper notes"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Why 52?</title>
    <link href="https://dpitt.me/forest/dsp-2O6M.html" rel="alternate" type="text/html"/>
    <published>Tue, 16 Apr 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><a href="https://eprint.iacr.org/2018/335.pdf">Fast modular squaring with
AVX512IFMA</a> seems to be the
entrypoint here, I found it simply by googling the instruction
<a href="https://www.felixcloutier.com/x86/vpmadd52luq"><code>VPMADD52LUQ</code></a>. It
cites <a href="https://eprint.iacr.org/2011/239.pdf"><em>Efficient Software Implementations of Modular
Exponentiation</em></a> stating</p>
<blockquote>
<p>Our developments build on top of the Almost Montgomery Square (AMS)
optimization of [8]</p>
</blockquote>
<p>But also cites <a href="/files/sime.pdf"><em>Software Implementation of Modular Exponentiation,
Using Advanced Vector Instructions Architectures</em></a> as
the reference for the 52-bit representation:</p>
<blockquote>
<p>These instructions multiply eight 52-bit unsigned integers residing
in wide 512-bit registers, produce the low (VPMADD52LUQ) and high
(VPMADD52HUQ) halves of the 104-bit products, and add the results to
64-bit accumulators (i. e., SIMD elements), placing them in the
destination register. They are designed for supporting big number
multiplications, when the inputs are stored in a ”redundant
representation” using radix 252 (as explained in [9]).</p>
</blockquote>
<p>This paper though, cites <a href="https://ieeexplore.ieee.org/document/1336767"><em>Parallel Cryptographic Arithmetic Using a
Redundant Montgomery
Representation</em></a> as a
starting place.</p>
<blockquote>
<p>For example, Page and Smart [19] suggested using SIMD architectures
to calculate several exponentiations in parallel, and using a
“redundant Montgomery representation” (which we call here Non
Reduced) to avoid conditional final subtractions in Montgomery
Multiplications.</p>
</blockquote>
<p>This seems to the the origin of the “redundant” terminology we find
all over the code, but the Gueron paper states “which we call here Non
Reduced”, so perhaps we’ll find that <em>Software Implementation of
Modular Exponentiation, Using Advanced Vector Instructions
Architectures</em> is the right place to start for the current RSA
implementation.</p>
<hr />
<p>It does look like that’s the case. The windows—which they refer to as
<em>digits</em>—of large numbers does come from <em>Software Implementations of
Modular Exponentiations</em>, which I’ve transcluded below for
simplicity’s sake.</p>
<hr class='transclude-hr'/>
<p><span class="transclusion-title">Software Implementation of Modular Exponentiation, Using Advanced Vector Instructions Architectures</span> <span class="transclusion-link">[<a href="/forest/dsp-9X1T.html">dsp-9X1T</a>]</span></p>
<p>This was published in <a href="https://link.springer.com/book/10.1007/978-3-642-31662-3">Arithmetic of Finite
Fields</a>, but
I’ve grabbed just this chapter <a href="/files/sime.pdf">here</a>.</p>
<p>This paper goes into detail about the algorithms used, originally for
AVX2, the x86 instructions that those algorithms employ, and so on,
but what I was really looking for is the origin of the 52-bit
representation found in the current AVX-512 implementation found in
OpenSSL, <a href="https://github.com/aws/aws-lc/pull/1273">soon AWS-LC</a>, and
maybe some day in BoringSSL. It turns out, this paper contains that
origin, only here we’re talking about the conversion of 32-bit numbers
to 29-bit numbers (arbitrarily, I think) and are given the following
equation as the way in which to convert numbers from one radix to
another.</p>
<p><span class="math display">\[
A = \sum^{k-1}_{i=0} X_i \times 2^{m \times i}
\]</span></p>
<p>Where <span class="math inline">\(A\)</span> is the value we wish to represent, <span class="math inline">\(X_i\)</span> is the value of
the <span class="math inline">\(i\)</span>th <em>window</em>, or as the paper calls it, the <span class="math inline">\(i\)</span>th digit, and
<span class="math inline">\(m\)</span> is the window size in bits.</p>
<p>So, say we want to represent the 16-bit number 24023 in windows of 5 bits.</p>
<pre><code>0101110111010111</code></pre>
<p>We think of as the following windows:</p>
<pre><code>0 10111 01110 10111</code></pre>
<p>Where <span class="math inline">\(m = 5\)</span> and <span class="math inline">\(k = 4\)</span>, giving us</p>
<pre><code>0   10111              01110             10111
0 + (23 * 2^(5 * 2)) + (14 *2^(5 * 1)) + (23 * 2^(5 * 0))
0 + 23552            + 448             + 23               = 24023</code></pre>
<p>This also holds if we choose 3-bit windows:</p>
<pre><code>0   101               110               111               010               111
0 + (5 * 2^(3 * 4)) + (6 * 2^(3 * 3)) + (7 * 2^(3 * 2)) + (2 * 2^(3 * 1)) + (7 * 2^(3 * 0))
0 + 20480           + 3072            + 448             + 16              + 7               = 24023</code></pre>
<p>This representation is the one used in the AVX-512 implementation
where <span class="math inline">\(m = 52\)</span>, carving up, for instance, the 1024-bit <code>bignum</code>s
into 52-bit windows.</p>
<hr class='transclude-hr' /> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Montgomery Multiplication</title>
    <link href="https://dpitt.me/forest/dsp-5U7X.html" rel="alternate" type="text/html"/>
    <published>Wed, 13 Mar 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>Montgomery multiplication is a faster way to do multiplication which
prefers base 2 to base 10, and shines when used iteratively. Numbers
are “represented” in montgomery form during the primary operations,
and then once those operations are complete, they are transported back
to their original form. It solves a modular multiplication of the
form:</p>
<p><span class="math display">\[
a * b\ \text{mod}\ m
\]</span></p>
<p>We will use <span class="math inline">\(a\)</span>, <span class="math inline">\(b\)</span> and <span class="math inline">\(m\)</span> in our example below.</p>
<p>First we compute or choose a few constants. These can be precomputed
and then retained, they don’t need to be regenerated each time.</p>
<ol type="1">
<li><p>Choose a power of two that’s greater than <span class="math inline">\(m\)</span>, this is
<span class="math inline">\(r\)</span>. Below I’m computing a number, but this could just as easily
be chosen to be constant <span class="math inline">\(2^{64}\)</span> or something like that.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>r <span class="ot">=</span> <span class="dv">2</span> <span class="op">^</span> <span class="fu">ceiling</span> (<span class="fu">logBase</span> <span class="dv">2</span> (<span class="fu">fromIntegral</span> m))</span></code></pre></div></li>
<li><p>Square <span class="math inline">\(r\)</span>, modulo <span class="math inline">\(m\)</span>. This we’ll call <span class="math inline">\(r_2\)</span>.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>r2 <span class="ot">=</span> r <span class="op">*</span> r <span class="ot">`mod`</span> m</span></code></pre></div></li>
<li><p>Compute the multiplicative inverse of <span class="math inline">\(-m\ \text{mod}\ r\)</span>, this is
<span class="math inline">\(m&#39;\)</span></p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>m&#39; <span class="ot">=</span> inv (<span class="fu">negate</span> m) r</span></code></pre></div></li>
</ol>
<p>Now we’ll use these to compute the montgomery form of our input
variables.</p>
<ol type="1">
<li><p>Multiply <span class="math inline">\(a * r_2\)</span>, this is <span class="math inline">\(a_m\)</span>.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>am <span class="ot">=</span> a <span class="op">*</span> r2</span></code></pre></div></li>
<li><p>Multiply <span class="math inline">\(a_m\)</span> by <span class="math inline">\(m&#39;\ \text{mod}\ r\)</span>, I’ll call this <span class="math inline">\(a_m&#39;\)</span></p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>am&#39; <span class="ot">=</span> am <span class="op">*</span> m&#39; <span class="ot">`mod`</span> r</span></code></pre></div></li>
</ol>
<p>Next we do a reduction of the montgomery form.</p>
<ol type="1">
<li><p>Do <span class="math inline">\(\cfrac{a_m + a_m&#39; * m}{r}\)</span>. This temp value we’ll call <span class="math inline">\(t\)</span>.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>t <span class="ot">=</span> am <span class="op">+</span> am&#39; <span class="op">*</span> m <span class="ot">`div`</span> r</span></code></pre></div></li>
<li><p>The result montgomery form is <span class="math inline">\(t\)</span> if <span class="math inline">\(t &lt; m\)</span>, otherwise it’s <span class="math inline">\(t - m\)</span>.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a> <span class="kw">if</span> t <span class="op">&lt;</span> m</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>   <span class="kw">then</span> t</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>   <span class="kw">else</span> t <span class="op">-</span> m</span></code></pre></div></li>
</ol>
<p>We do the same for <span class="math inline">\(b\)</span>, then</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    
  </entry>
  
  <entry>
    <title type="html">RSA mathematics deep-dive talk</title>
    <link href="https://dpitt.me/forest/dsp-5Q3M.html" rel="alternate" type="text/html"/>
    <published>Sat,  9 Mar 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>This is a talk I gave at work when tasked documenting the AVX-512
modular exponentiation implementation we ported from OpenSSL to
Amazon’s <a href="https://github.com/aws/aws-lc"><code>libcrypto</code> implementation</a>.</p>
<ul>
<li><a href="/trex/rsa/deck.html">slides</a></li>
</ul> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/talk/index.html" term="talk" label="talk"/>
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    
  </entry>
  
  <entry>
    <title type="html">The Chinese Remainder Theorem</title>
    <link href="https://dpitt.me/forest/dsp-6D8F.html" rel="alternate" type="text/html"/>
    <published>Fri,  2 Feb 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>The CRT states that, given a set of congruences (where all of the
<span class="math inline">\(m\)</span>s are coprime):</p>
<p><span class="math display">\[
x \equiv a_1\ \text{mod}\ m_1 \\
x \equiv a_2\ \text{mod}\ m_2 \\
\vdots \\
x \equiv a_n\ \text{mod}\ m_n \\
\]</span></p>
<p>We can solve for <span class="math inline">\(x\)</span> with the equation:</p>
<p><span class="math display">\[
x \equiv (\sum_{i=1}^n a_i \cdot M_i \cdot M_{i}^{-1})\ \text{mod}\ N
\]</span></p>
<p>Where <span class="math inline">\(M_i\)</span> is <span class="math inline">\(\cfrac{m_1m_2 \dots m_n}{m_i}\)</span> and <span class="math inline">\(M_{i}^{-1}\)</span>
is <span class="math inline">\(M_i\)</span>’s multiplicative inverse modulo <span class="math inline">\(m_i\)</span>.</p>
<p><strong>Example</strong></p>
<p>We will start with the congruences</p>
<p><span class="math display">\[
x \equiv 1\ \text{mod}\ 3 \\
x \equiv 2\ \text{mod}\ 7 \\
\]</span></p>
<p>This gives us the equation:</p>
<p><span class="math display">\[
(1 \times 7 \times 1) + (2 \times 3 \times 5)
\]</span></p>
<p>Because, for <span class="math inline">\(M_1\)</span>, <span class="math inline">\(\cfrac{3 \cdot 7}{3} = 7\)</span> and <span class="math inline">\(7^{-1}\
\text{mod}\ 3 = 1\)</span> and for <span class="math inline">\(M_2\)</span>, <span class="math inline">\(\cfrac{3 \cdot 7}{7} = 3\)</span> and
<span class="math inline">\(3^{-1}\ \text{mod}\ 7 = 5\)</span></p>
<p>This results in <span class="math inline">\(7 + 30 = 37\ \text{mod}\ 21\)</span>, or <span class="math inline">\(16\)</span>, which we
can check: <span class="math inline">\(16\ \text{mod}\ 3 \equiv 1\)</span> and <span class="math inline">\(16\ \text{mod}\ 7
\equiv 2\)</span>.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    
  </entry>
  
  <entry>
    <title type="html">RSA for the impatient</title>
    <link href="https://dpitt.me/forest/dsp-9F0P.html" rel="alternate" type="text/html"/>
    <published>Wed, 31 Jan 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><em>I’ve been asked to document the steps in an Almost Montgomery
Multiplication implementation at work which is terrifying, because
thus far, all I’ve really needed to know to port this patch from
OpenSSL to BoringSSL is a little about AVX-512 instruction set
extensions and some details on Linux’s or Windows’ calling
conventions. The hardest part of all of this has been teaching the
FIPS assembly parser how to understand mask register syntax. Until
now…</em></p>
<p><em>I’ve decided to walk, step-by-step, through RSA encryption, starting
with the most naive exposition possible.</em></p>
<hr />
<h1 id="simplest-possible-explanation">Simplest Possible Explanation</h1>
<h2 id="i.-key-generation">I. Key Generation</h2>
<p><strong>Choose two large primes <span class="math inline">\(p\)</span> and <span class="math inline">\(q\)</span></strong>. For our sake, we’re going
to use the absolutely massive numbers 7 and 13.</p>
<p><strong>Compute their product</strong>; this is <span class="math inline">\(n\)</span>: <span class="math inline">\(7 \times 13 = 91\)</span>.</p>
<p><strong>Calculate Euler’s totient function of <span class="math inline">\(n\)</span></strong>, denoted as
<span class="math inline">\(\phi(n)\)</span>, where <span class="math inline">\(\phi(n) = (p - 1)(q - 1)\)</span><span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">Euler’s totient function counts the number of coprime positive
integers less than <span class="math inline">\(n\)</span>. The product of two primes is a special
case and that’s what we’re seeing here.<br />
<br />
</span></span>.</p>
<p><strong>Chose a public exponent <span class="math inline">\(e\)</span> such that <span class="math inline">\(1 \lt e \lt \phi(n)\)</span>, and
that <span class="math inline">\(e\)</span> is coprime with <span class="math inline">\(\phi(n)\)</span></strong>. <span class="math inline">\(e\)</span> is a part of the
public key. Well, our <span class="math inline">\(\phi(n) = (7 - 1)(13 - 1) = 6 \times 12 =
72\)</span>, so why don’t we “choose” <span class="math inline">\(17\)</span> as our <span class="math inline">\(e\)</span>.</p>
<p><strong>Compute the private exponent <span class="math inline">\(d\)</span> such that <span class="math inline">\(d \times e =\ 1\
\text{mod}\ \phi(n)\)</span></strong> (<span class="math inline">\(d\)</span> is <span class="math inline">\(e\)</span>’s multiplicative inverse
<span class="math inline">\(\text{mod}\ \phi(n)\)</span>).</p>
<p>This inversion is what allows <span class="math inline">\(e\)</span> and <span class="math inline">\(d\)</span> to “undo” each other,
which is what allows the original message to be retrieved from the
cipher text. So our modulus is <span class="math inline">\(72\)</span>, and our <span class="math inline">\(e\)</span> is <span class="math inline">\(17\)</span>, from
here we need to compute <span class="math inline">\(17\)</span>’s multiplicative inverse <span class="math inline">\(\text{mod}\
72\)</span>, which we can do using using the Euclidean algorithm. Here’s a
small Haskell function that computes an inverse given the modulus and</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ot">inv ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>inv a m <span class="ot">=</span> f <span class="dv">0</span> m <span class="dv">1</span> a</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="kw">where</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>  f t r newt newr</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>    <span class="op">|</span> newr <span class="op">==</span> <span class="dv">0</span> <span class="ot">=</span> <span class="fu">abs</span> t</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>    <span class="op">|</span> <span class="fu">otherwise</span> <span class="ot">=</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>      <span class="kw">let</span> q <span class="ot">=</span> r <span class="ot">`div`</span> newr</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>      <span class="kw">in</span> f newt newr (t <span class="op">-</span> (q <span class="op">*</span> newt)) (r <span class="op">-</span> (q <span class="op">*</span> newr))</span></code></pre></div>
<p>This we can break down with the recursion of our <code>inv</code> function:</p>
<ul>
<li>We call <code>inv 17 72</code>, this means we end up calling the recursor as <code>f 0 72 1 17</code>.</li>
<li>We catch the <code>otherwise</code> case, this sets <code>q = 72 / 17 = 4</code>.</li>
<li>So we recurse with <code>f 1 17 (0 - (4 * 1)) (72 - (4 * 17))</code> or <code>f 1 17 -4 4</code>.</li>
<li>We hit the <code>otherwise</code> case again. This time <code>q = 17 / 4 = 4</code> so we
recurse with <code>f -4 4 (1 - (4 * -4)) (17 - (4 * 4))</code> or <code>f -4 4 17 1</code>.</li>
<li>still in the <code>otherwise</code> case, so we compute <code>q = 4 / 1 = 4</code> and
recurse with <code>f 17 1 (-4 - (4 * 17)) (4 - (4 * 1))</code> or <code>f 17 1 -72 0</code>.</li>
<li>This time we hit the base case because <code>newr</code> is <code>0</code>, so we return
our answer, which is <code>abs t</code>, or <code>17</code>.</li>
<li>We can check this because <code>17 * 17 (mod 72) = 1</code> (what are the odds
that I picked an <code>e</code> that’s its own inverse?).</li>
</ul>
<h2 id="ii.-encryption">II. Encryption</h2>
<p>“To encrypt a message <span class="math inline">\(M\)</span>, represent it as a number smaller than
<span class="math inline">\(n\)</span>”. Crap. Not much information in the 6 bits it takes to contain
the number 91.</p>
<p>Let’s see, could we “represent” a three-letter message: <span class="math inline">\(26 \times 3
= 78\)</span>? We could add together each digit’s numerical representation…
“dog” would be <span class="math inline">\(4 + 15 + 7 = 26\)</span>. Well, no, because how do we
separate it again? How do we get “dog” out of 26? It could just as
easily be “balk”: <span class="math inline">\(2 + 1 + 12 + 11\)</span>. I think the best we can do is
have a 3-digit code where each digit is 0-3, dividing our 6 bits into
3 groups of 2. So why don’t we send the message “213”, this would be
<code>10 01 11</code> in binary, or, in base 10, 39.</p>
<p>Next we compute the cipher text, <span class="math inline">\(C = M^e\ \text{mod}\ n\)</span>. Our
cipher text is <span class="math inline">\(39^{17}\ \text{mod}\ 91 = 65\)</span>.</p>
<h2 id="iii.-decryption">III. Decryption</h2>
<p>Simply do the inverse! <span class="math inline">\(M = C^d\ \text{mod}\ n = 65^{17}\ \text{mod}\
91 = 39\)</span> Which, of course, we can de-numerical-ize as 213, left to
right, via its couplets, <code>10 01 11</code>.</p>
<p>Cool, we did it. We’re cryptographers now.</p>
<h1 id="optimizations">Optimizations</h1>
<hr class='transclude-hr'/>
<p><span class="transclusion-title">The Chinese Remainder Theorem</span> <span class="transclusion-link">[<a href="/forest/dsp-6D8F.html">dsp-6D8F</a>]</span></p>
<p>The CRT states that, given a set of congruences (where all of the
<span class="math inline">\(m\)</span>s are coprime):</p>
<p><span class="math display">\[
x \equiv a_1\ \text{mod}\ m_1 \\
x \equiv a_2\ \text{mod}\ m_2 \\
\vdots \\
x \equiv a_n\ \text{mod}\ m_n \\
\]</span></p>
<p>We can solve for <span class="math inline">\(x\)</span> with the equation:</p>
<p><span class="math display">\[
x \equiv (\sum_{i=1}^n a_i \cdot M_i \cdot M_{i}^{-1})\ \text{mod}\ N
\]</span></p>
<p>Where <span class="math inline">\(M_i\)</span> is <span class="math inline">\(\cfrac{m_1m_2 \dots m_n}{m_i}\)</span> and <span class="math inline">\(M_{i}^{-1}\)</span>
is <span class="math inline">\(M_i\)</span>’s multiplicative inverse modulo <span class="math inline">\(m_i\)</span>.</p>
<p><strong>Example</strong></p>
<p>We will start with the congruences</p>
<p><span class="math display">\[
x \equiv 1\ \text{mod}\ 3 \\
x \equiv 2\ \text{mod}\ 7 \\
\]</span></p>
<p>This gives us the equation:</p>
<p><span class="math display">\[
(1 \times 7 \times 1) + (2 \times 3 \times 5)
\]</span></p>
<p>Because, for <span class="math inline">\(M_1\)</span>, <span class="math inline">\(\cfrac{3 \cdot 7}{3} = 7\)</span> and <span class="math inline">\(7^{-1}\
\text{mod}\ 3 = 1\)</span> and for <span class="math inline">\(M_2\)</span>, <span class="math inline">\(\cfrac{3 \cdot 7}{7} = 3\)</span> and
<span class="math inline">\(3^{-1}\ \text{mod}\ 7 = 5\)</span></p>
<p>This results in <span class="math inline">\(7 + 30 = 37\ \text{mod}\ 21\)</span>, or <span class="math inline">\(16\)</span>, which we
can check: <span class="math inline">\(16\ \text{mod}\ 3 \equiv 1\)</span> and <span class="math inline">\(16\ \text{mod}\ 7
\equiv 2\)</span>.</p>
<hr class='transclude-hr' />
<p>When we break our encryption and decryption operations up into CRT
form, where</p>
<p><span class="math display">\[
C = M^e\  \text{mod}\ n
\]</span></p>
<p>Becomes</p>
<p><span class="math display">\[
C \equiv M^e\ \text{mod}\ p \\
C \equiv M^e\ \text{mod}\ q
\]</span></p>
<p>Because, recall, <span class="math inline">\(n = p \cdot q\)</span>.</p>
<p>We can now use Euler’s Theorem, which tells us that</p>
<p><span class="math display">\[
x^{e\ \text{mod}\  \phi(m)} \equiv x^e\ \text{mod}\ m
\]</span></p>
<p>Which reduces significantly the number of multiplications needed
because the exponent on the left-hand side of this congruence is much
smaller than the one the right. Once we’ve done this smaller
exponentiation, we simply use the CRT to recombine the results.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Zeros, Stacks, and Paranoia</title>
    <link href="https://dpitt.me/forest/dsp-7M4Z.html" rel="alternate" type="text/html"/>
    <published>Fri, 12 Jan 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><em>Boring and OpenSSL have a lot of “hardware”
implementations. Handwritten assembly routines for various algorithms
and modes, targeting specific architectures and features. If you were
to hazard a guess, would you say that used stack frames were typically
explicitly zeroed, or just “discarded” (by resetting the stack
pointer)?</em></p>
<hr />
<p>At the time of this writing, I work at Intel. My current job has me
porting hard things from one place to another, typically to
<a href="https://github.com/aws/aws-lc">AWS-LC</a>, as my team is dedicated to
co-engineering work with the folks at AWS. I’m not one of the people
who intrinsically just knows how to do hard things (yet), but I am one
of the people who can spend some time with an already done hard thing,
learn how it works, and then can contribute to it, translate it, or
whatever it is that needs to be done.</p>
<p>Most recently, we’ve been looking at the AVX-512 hardware
implementation for XTS<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">XOR-Encrypt-XOR Tweakable Block Cipher Stealing mode; an AES
mode typically used for disk encryption.<br />
<br />
</span></span>. The question was, “where is the stack
<em>cleared</em>?”</p>
<p>We can see that, at the beginning of the encryption routine, the
incoming <a href="https://github.com/aws/aws-lc/blob/d9caacae8c44227baa41e970cbed0dbfd4eef9c2/crypto/fipsmodule/aes/asm/aesni-xts-avx512.pl#L1476-L1480">stack pointer is saved into
<code>rbp</code></a>.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>  <span class="dt">$code</span> .= <span class="ot">&quot;</span><span class="st">push         </span><span class="dt">%rbp</span><span class="ch">\n</span><span class="ot">&quot;</span>;</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>  <span class="dt">$code</span> .= <span class="ot">&quot;</span><span class="st">mov          </span><span class="dt">%rsp</span><span class="st">,</span><span class="dt">%rbp</span><span class="ch">\n</span><span class="ot">&quot;</span>;</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="dt">$code</span> .= <span class="ot">&quot;</span><span class="st">sub          \$VARIABLE_OFFSET,</span><span class="dt">%rsp</span><span class="ch">\n</span><span class="ot">&quot;</span>;</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>  <span class="dt">$code</span> .= <span class="ot">&quot;</span><span class="st">and          \$0xffffffffffffffc0,</span><span class="dt">%rsp</span><span class="ch">\n</span><span class="ot">&quot;</span>;</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>  <span class="dt">$code</span> .= <span class="ot">&quot;</span><span class="st">mov          </span><span class="dt">%rbx</span><span class="st">,</span><span class="dt">$GP_STORAGE</span><span class="st">(</span><span class="dt">$TW</span><span class="st">)</span><span class="ch">\n</span><span class="ot">&quot;</span>;</span></code></pre></div>
<p>Then, in the routine labeled <code>ret</code>, <a href="https://github.com/aws/aws-lc/blob/d9caacae8c44227baa41e970cbed0dbfd4eef9c2/crypto/fipsmodule/aes/asm/aesni-xts-avx512.pl#L1855-L1859">that pointer is restored to
<code>rsp</code></a>. <code>ret</code>
is a routine that is jumped to from several places in the main
encryption routine upon completion. Searching for the label in its
entirety (<code>L_ret_${rndsuffix}</code>) in this file turns up 27
results<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"><code>vzeroupper</code> is a one-pass no-operand instruction that zeros the
upper (i.e. most significant) half of the vector registers. This is
a good start.<br />
<br />
</span></span>.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>  <span class="dt">$code</span> .= <span class="ot">&lt;&lt;</span><span class="kw">___;</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="st">  mov    </span><span class="dt">%rbp</span><span class="st">,</span><span class="dt">%rsp</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="st">  pop    </span><span class="dt">%rbp</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="st">  vzeroupper</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="st">  ret</span></span></code></pre></div>
<p>This effectively “forgets” the frames used during the encryption
routine, but the bits and bytes written to those frames remain until
the stack grows back into them. And to make matters worse, in this
case, it’s key material that is written into these frames. This of
course could leave a vector of attack open for a malicious but
intelligent attacker.</p>
<p class="pull">
What could be more in need of paranoia than a
<code>libcrypto</code> implementation?
</p>
<p>When I traded emails with the original author of this XTS code—recall
that I’m a porter not a knower—his response was that “a very paranoid
implementation” may zero the used frames before discarding them. While
this is a fair response out of context, what could be more in need of
paranoia than a <code>libcrypto</code> implementation?</p>
<p>So yeah, we need to zero the stack here too. Given this is all a part
of an AVX-512 implementation, we can make clever use of the SIMD
instructions and vector registers to clear those frames, adding the
following lines to this <code>ret</code> routine.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode perl"><code class="sourceCode perl"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>vpxor        <span class="dt">%zmm0</span>,<span class="dt">%zmm0</span>,<span class="dt">%zmm0</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Zero-out the stack frames used for `key1`, 64 bytes at a time.</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>vmovdqa64    <span class="dt">%zmm0</span>,<span class="bn">0x80</span>(<span class="dt">%rsp</span>)</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>vmovdqa64    <span class="dt">%zmm0</span>,<span class="bn">0xc0</span>(<span class="dt">%rsp</span>)</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>vmovdqa64    <span class="dt">%zmm0</span>,<span class="bn">0x100</span>(<span class="dt">%rsp</span>)</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Stack usage is not divisible by 64, so we use a kmask register to</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="co"># only mov 48 of the bytes (6 quad-words).</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a>mov       \<span class="wa">$0</span><span class="dt">x3f</span>,<span class="dt">$tmp1</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>kmovq     <span class="dt">$tmp1</span>,<span class="dt">%k2</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a>vmovdqa64 <span class="dt">%zmm0</span>,<span class="bn">0x140</span>(<span class="dt">%rsp</span>){<span class="dt">%k2</span>}</span></code></pre></div>
<p><code>vmovdqa64</code> moves 512-bits (64 bytes) from the full-width SIMD
register <code>zmm0</code> to the location we specify with <code>&lt;offset&gt;(%rsp)</code> where
those 64 bytes are thought of logically as 64-bit integers, AKA
quad-words. Before those lines we self-exclusive-or <code>zmm0</code>. A
self-exclusive-or’s result is always <code>0</code> because none of the bits are
exclusive when they’re or’d with themselves. After that, we write
zeros, 64 <em>bytes</em> at a time to the stack. The stack usage here isn’t
evenly divisible by 64, so we use a mask register to tell this last
instruction to only use the first 48 bytes of the register when doing
its move. This is achieved by writing <code>0x3f</code>, or written another way
<code>0b_11_1111</code> (6 ones), into the mask register and then including that
mask register with our invocation.</p>
<p>There were a few other places that needed some zeroing attention, but
this is the most interesting part. And to answer the question I asked
in the front matter of this post, well, it’s all of them. Or at least
it is now.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cryptography/index.html" term="cryptography" label="cryptography"/>
    
    <category scheme="https://dpitt.me/tags/programming/index.html" term="programming" label="programming"/>
    
    <category scheme="https://dpitt.me/tags/assembly/index.html" term="assembly" label="assembly"/>
    
    <category scheme="https://dpitt.me/tags/boringssl/index.html" term="boringssl" label="boringssl"/>
    
    
  </entry>
  
  <entry>
    <title type="html">About This Site</title>
    <link href="https://dpitt.me/forest/dsp-0031.html" rel="alternate" type="text/html"/>
    <published>Thu,  4 Jan 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>This site uses the static site generator
<a href="https://jaspervdj.be/hakyll/">Hakyll</a> and <a href="https://edwardtufte.github.io/tufte-css/">Edward Tufte’s Sidenote
CSS</a> by way of <a href="https://github.com/jez/pandoc-sidenote">Pandoc
Sidenote</a>. You can find the
source for it <a href="https://github.com/pittma/dpitt.me">here</a>.</p>
<p>It is a forest. Related items are linked to one another rather than
adhering to a chronological order; I think that’s the best way to find
your way around. You can literally follow my trains of thought.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/meta/index.html" term="meta" label="meta"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Hi, I'm dan</title>
    <link href="https://dpitt.me/forest/dsp-0029.html" rel="alternate" type="text/html"/>
    <published>Wed,  3 Jan 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>I’m a curious exister living in Portland, OR. I’m also a dad and a
husband who currently programs computers for a living. My interests
over the years have spanned from type theory to cryptography and from
distributed consensus to embedded systems.</p>
<p>I’ve tried probably a dozen platforms, approaches, and styles for
places for me to exorcize my thoughts, to <a href="https://www.glass-bead.org/article/a-theory-of-vibe/">emanate my
vibe</a>, but
nothing has ever felt quite right. The purpose of this site is for me
to do just that, but to do it untethered from a need to defend or
justify any of the connections I find in my head. Think of it as a
map—a place to trace any and all of my <a href="https://en.wikipedia.org/wiki/Line_of_flight">lines of
flight</a>.</p>
<p>If I’ve learned anything through everything I’ve tried, it’s that I’m
not really a writer, I’m actually just a blogger. I’ve tried to
categorize everything I’ve written by finding the right outlet for
each little fracture of myself I’m shining light on with any given
piece of writing, trying desperately to keep one identity from
reflecting another. But it turns out what I really need to do to be a
whole person is to put all the fractures together into a singular
mirror, so that’s what you’ll find here. I collected all the writing
I’ve done on the Internet over the last 10 years or so (going back to
2014) and dropped them here, incongruences, fleeting interests, and
all.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/meta/index.html" term="meta" label="meta"/>
    
    
  </entry>
  
  <entry>
    <title type="html">dan pittman</title>
    <link href="https://dpitt.me/index.html" rel="alternate" type="text/html"/>
    <published>Wed,  3 Jan 2024 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <hr class='transclude-hr'/>
<p><span class="transclusion-title">Hi, I’m dan</span> <span class="transclusion-link">[<a href="/forest/dsp-0029.html">dsp-0029</a>]</span></p>
<p>I’m a curious exister living in Portland, OR. I’m also a dad and a
husband who currently programs computers for a living. My interests
over the years have spanned from type theory to cryptography and from
distributed consensus to embedded systems.</p>
<p>I’ve tried probably a dozen platforms, approaches, and styles for
places for me to exorcize my thoughts, to <a href="https://www.glass-bead.org/article/a-theory-of-vibe/">emanate my
vibe</a>, but
nothing has ever felt quite right. The purpose of this site is for me
to do just that, but to do it untethered from a need to defend or
justify any of the connections I find in my head. Think of it as a
map—a place to trace any and all of my <a href="https://en.wikipedia.org/wiki/Line_of_flight">lines of
flight</a>.</p>
<p>If I’ve learned anything through everything I’ve tried, it’s that I’m
not really a writer, I’m actually just a blogger. I’ve tried to
categorize everything I’ve written by finding the right outlet for
each little fracture of myself I’m shining light on with any given
piece of writing, trying desperately to keep one identity from
reflecting another. But it turns out what I really need to do to be a
whole person is to put all the fractures together into a singular
mirror, so that’s what you’ll find here. I collected all the writing
I’ve done on the Internet over the last 10 years or so (going back to
2014) and dropped them here, incongruences, fleeting interests, and
all.</p>
<hr class='transclude-hr' />
<p>Should you want to contact me you can shoot an email to <code>dan at this domain</code>. I have given a few <a href="/tags/talk">talks</a> through the years, and
you can see my <a href="/forest/dsp-0032.html">CV</a> here.</p>
<hr class='transclude-hr'/>
<p><span class="transclusion-title">About This Site</span> <span class="transclusion-link">[<a href="/forest/dsp-0031.html">dsp-0031</a>]</span></p>
<p>This site uses the static site generator
<a href="https://jaspervdj.be/hakyll/">Hakyll</a> and <a href="https://edwardtufte.github.io/tufte-css/">Edward Tufte’s Sidenote
CSS</a> by way of <a href="https://github.com/jez/pandoc-sidenote">Pandoc
Sidenote</a>. You can find the
source for it <a href="https://github.com/pittma/dpitt.me">here</a>.</p>
<p>It is a forest. Related items are linked to one another rather than
adhering to a chronological order; I think that’s the best way to find
your way around. You can literally follow my trains of thought.</p>
<hr class='transclude-hr' />
<p>Here’s a few notes on post metadata that may be useful:</p>
<hr class='transclude-hr'/>
<p><span class="transclusion-title">Working with the Garage Door Open</span> <span class="transclusion-link">[<a href="/forest/dsp-0H0E.html">dsp-0H0E</a>]</span></p>
<p>When I converted this blog from the conventional reverse-chronological
ordering to something you had to follow links to get around in, like
it was a dark forest and you were wandering from tree to tree, it was
heavily influenced by the <a href="https://github.com/KasperZutterman/Second-Brain">many people who’d done it
before</a>. Even
“working with the garage door open” is
<a href="https://notes.andymatuschak.org/About_these_notes?stackedNotes=zCMhncA1iSE74MKKYQS5PBZ">borrowed</a>. I
found myself thinking again about that garage door this morning, after
a month or so of heavy notes-to-myself taking as I learned the ins and
outs of a number of new things at work, and it occurred to me that I
ought to act on that sentiment if it resonated with me so much. So
today I added a new key/value pair to post metadata called <strong>garage</strong>,
where I’ll denote the status of a page according to its completeness
&amp;c. I say &amp;c. because I think the in/out of the garage concept has
more to do with than just completeness or polish; I think in a lot of
ways, it comes down to intention. Something that I intended to be for
public consumption from the beginning, an essay or blog post I set to
write intentionally, may still be a WIP, it may be completed, or
something in between, but notes that I wrote while I learned something
that I never really intended to consider as a “piece of writing”
doesn’t really fit in that spectrum. Anyway, here are the states I’ll
be using for the <strong>garage</strong> key and what they mean:</p>
<ul>
<li><p><strong>open</strong>: you’re just seeing me work something out in the open,
these are notes mostly for me, it is not something that I edited or
thought about word choice or cadence or anything like that. It may
not “make sense” in a narrative or continuity way, but you may find
some useful nuggets of information.</p></li>
<li><p><strong>closed</strong>: The garage door is closed and I’m not home, I’m out
pitching this piece. Something with “<strong>garage</strong>: closed” was always
intended to be for public consumption, and is—modulo the handful
of typos I’m sure you’ll find—done. A piece labeled closed is more
like a conventional blog post.</p></li>
<li><p><strong>light on</strong>: A piece with public-facing intentions but is still a
work in progress. These I don’t think you’ll find much of, but if
you do it’s likely because I published to get input on, and you’re
seeing the progress through the frosted glass of the garage door.</p></li>
<li><p><strong>light off</strong>: A piece with public-facing intentions that was
abandoned or left unfinished.</p></li>
</ul>
<hr class='transclude-hr' />
<p>Below I’ve linked a few favorites to get you started, or you may want
to start with the <a href="/tags.html">list of tags</a>, look at the complete
<a href="/archive.html">archive</a>, or subscribe to the <a href="/feed.xml">feed</a>.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/meta/index.html" term="meta" label="meta"/>
    
    
  </entry>
  
  <entry>
    <title type="html">How to Pitch a Tent</title>
    <link href="https://dpitt.me/forest/dsp-0028.html" rel="alternate" type="text/html"/>
    <published>Tue, 24 Oct 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><em>I’ve been doing some serious excavating in my 1:1 therapy over the
last few months. One of the things we’ve revisited a lot has been my
experience as a young man in my local hardcore scene. It’s been a part
of a broader theme wherein I‘m letting loose of the normative voices
in my head, some of which are projections from my own self-loathing,
some of which are actual, real things people have said to me, and some
of which are probably just revisions of my own history, altered for
the consistency of The Narrative, as history so often is.</em></p>
<h1 id="i.">I.</h1>
<p><img src="/images/drain.jpg"/></p>
<p>Sometimes I will sit and watch live sets of bands I like on YouTube
and I will be inexplicably overcome with emotion; I’ll even feel tears
well up in my eyes. This is certainly not because of the content,
which is usually a display of chaos, aggression, and violence. I’ve
never really been able to articulate the cause of this overcoming, and
trying to explain it to anyone has always been impossible. People from
outside the world of hardcore tend to fixate either on the violence
and make a joke about having good insurance, or poke fun at the
absurdity of “moshing” or dancing. It’s fair—without context, it’s
inscrutable; a signifier with no signified. To be moved to tears by
such a scene is beyond language.</p>
<h1 id="ii.">II.</h1>
<p>Recently I had a conversation about the experience of having a friend
go to church camp and then returning as 100% a different person. I’ll
share my own story on the subject.</p>
<p>I had a friend in high school that we’ll call Chris (because that is
in fact his name). Chris and I spent a ton of time together, he was a
year older than me and played guitar in the youth praise band that I’d
been drumming in. When we weren’t getting nerdy about the new Jars of
Clay record or checking out the latest fare at Abercrombie, we were
getting into the usual type of 9th and 10th grade boy trouble. Pulling
pranks, making PG-13 jokes, and spending a lot of time chasing and
thinking about girls.</p>
<p>Chris drove a Camaro and I have this very clear memory of us leaving
school on a hot, early fall day with plans to go swimming at my
house. Chris was goofing off, driving too fast, and when it came time
for us to make a left turn, he couldn’t slow down in time. He aborted
the turn and continued straight on, albeit, now, on the wrong side of
the median. We had no escape for about a block, until he finally
completed that aborted left turn into the neighborhood. For the
entirety of that block, both of us were screaming, cashing in on
adrenaline to pay the check our lungs couldn’t (regardless of the fact
that we did not see another vehicle for one iota of those interminable
five seconds).</p>
<p>This is the kind of shit I got up to with Chris, right up to the
Friday before we left for a weekend-long discipleship camp. The
weekend went off as every other one that I’d been to in my 14 or so
years at the church I grew up in. I’d been spending weeks and weekends
at Jesus-oriented camps since the summer after 3rd grade, and they
were all about the same things for me: surviving the week on camp
food, spending my parents money on candy, and, starting in about the
5th grade, girls. This particular weekend was so unremarkable to me,
nearly no memory of it still pays rent in my head. There are some hazy
details of unloading bags in the dark Friday night, and maybe one of
sitting next to Chris in a folding chair during a small group
meeting. If nothing else, I was probably incensed that the band wasn’t
playing. The youth pastor brought only his guitar and eschewed the
rest of the band, leaving me with a low-grade irk that I couldn’t play
my drums all weekend.</p>
<p>We drove home Sunday, I think it’s even possible that it was Sunday
afternoon, meaning that I did not have to attend the stuffy Sunday
morning service as I typically would have. I can remember walking out
of the church, Chris at my side, and asking him if we ought to get
lunch. I don’t remember exactly his response, but it was something to
the tune of ‘no, I need to go home and pray and read my bible’.</p>
<p>The crazy thing is, I never really hung out with Chris after that
Sunday despite it being years before I left the church and ultimately
lost my faith. Chris and I instantaneously shifted from a close
friendship to a para-social relationship because the Chris I went to
camp with never really came home. Chris had had a religious
experience, he had felt God’s presence and was changed forever. As far
as I know, after graduating high school, Chris went to seminary and
works as a music pastor.</p>
<p>I tend to use the term “Lifer” for people like Chris, people who stay
after everyone else has left. It’s funny, I use “Lifer” both as a term
of endearment or as a pejorative judgement depending on the
context. For Chris, it’s a judgement, but for myself as a hardcore
kid, I wear it like a tattoo on my chest. I could never relate to
Chris’ experience, just the same as those friends who are no longer
around today but stood next to me at hardcore shows 20 years ago
cannot relate to mine.</p>
<p>Perhaps the reason why these experiences are so difficult to
articulate is because they are intentionally inscrutable; like Moses
who couldn’t look YHWH in the face<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"><em>But He said, “You cannot see My face; for no man shall see
Me, and live.” And the Lord said, “Here is a place by Me, and you
shall stand on the rock. So it shall be, while My glory passes by,
that I will put you in the cleft of the rock, and will cover you
with My hand while I pass by. Then I will take away My hand, and
you shall see My back; but My face shall not be seen.”</em><br />
<br />
‭‭Exodus‬ ‭33‬:‭20‬-‭23‬ ‭(NKJV‬)‬<br />
<br />
</span></span>, or Paul who said we couldn’t ever
truly grasp God’s glory, that we could only see it “through a glass,
darkly<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"><em>When I was a child, I spake as a child, I understood as a
child, I thought as a child: but when I became a man, I put away
childish things. For now we see through a glass, darkly; but then
face to face: now I know in part; but then shall I know even as
also I am known.</em><br />
<br />
1 Corinthians 13:11-12 (NKJV)<br />
<br />
</span></span>”. We can’t know the face of God, only that we’re in his
presence. Concrete language carves out a window, but we can only
grapple with metaphor—the light spilling out from underneath a
doorway.</p>
<h1 id="iii.">III.</h1>
<p>The truth is, no matter how I try and metaphorize it, Chris’
experience and mine are the self-same. The reason watching a
particularly chaotic set—even when displaced by the abstraction of a
screen—can move me to tears is because it is literally scratching my
soul. I lost God at church, but I found him in hardcore.</p>
<p>As I return to this formative part of myself, I’m trying to experience
it without the bindings of normativity. Who is the me that lives fully
in his real self and loves what he loves and how he loves it? How does
he pitch his tent? Here’s what I think the answer is: the poles with
which we ought to pitch our tent are these little religious
experiences we have throughout our lives. Maybe our souls are
pre-exposed to particular things, waiting to be touched, or maybe it’s
entirely incidental; either way, at least I now know, God is but one
stage dive away.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/self/index.html" term="self" label="self"/>
    
    <category scheme="https://dpitt.me/tags/god/index.html" term="god" label="god"/>
    
    
  </entry>
  
  <entry>
    <title type="html">TBCK</title>
    <link href="https://dpitt.me/forest/dsp-0027.html" rel="alternate" type="text/html"/>
    <published>Sat, 16 Sep 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><strong>TBCK: TBC1 Domain Containing Kinase</strong></p>
<blockquote>
<p>This gene encodes a protein that contains a protein kinase domain, a
Rhodanase-like domain and the <code>Tre-2/Bub2/Cdc16</code> (TBC) domain. The
encoded protein is thought to play a role in actin organization,
cell growth and cell proliferation by regulating the mammalian
target of the rapamycin (mTOR) signaling pathway. This protein may
also be involved in the transcriptional regulation of the components
of the mTOR complex. Alternative splicing results in multiple
transcript variants.</p>
</blockquote>
<p><a href="https://www.ncbi.nlm.nih.gov/gene/93627">https://www.ncbi.nlm.nih.gov/gene/93627</a></p>
<h1 id="what-is-the-tbc-domain">What is the TBC Domain?</h1>
<ul>
<li>Evolutionarily conserved protein domain</li>
</ul>
<h1 id="what-is-a-kinase">What is a Kinase?</h1>
<p>Catalyzes transfers of phosphates from high energy phosphate-donating
molecules to specific substrates. E.g., ATP -&gt; ADP, something that
happens 4 times during glycolysis. The production of energy during
glycolysis is exactly that process of ATP -&gt; ADP, as discussed in <a href="https://theproof.com/the-science-of-exercise-mitochondrial-health-and-longevity-with-biochemist-kieron-rooney/">THE
PROOF episode with Kieron
Rooney</a>.</p>
<h2 id="protein-kinase">Protein Kinase</h2>
<p>A Kinase which acts strictly on proteins as opposed to lipids, carbs,
or other molecules.</p>
<h1 id="ribosomes-mitochondria-and-energy-atp">Ribosomes, Mitochondria and Energy (ATP)</h1>
<blockquote>
<p>You can only turn fat into two carbon subunits that get put into the
mitochondria where you <strong>very efficiently</strong> make ATP, carbon
dioxide, and water vapor*.</p>
</blockquote>
<p><em><a href="https://youtu.be/7s7d7WNT1Ek?t=6202">Peter Attia from Rich Roll 695</a>
(citation needed)</em></p>
<p>Glycolysis isn’t less efficient than fat, both pyruvate and fatty
acids as the input into the mitochondria are very efficient, the
problem actually is that there isn’t enough oxygen to oxidize the
pyruvate, so the pyruvate gets turned into lactate, which yields
1/16th the ATP.</p>
<h1 id="what-is-the-mtor-pathway">What is the mTOR Pathway?</h1>
<ul>
<li>Complex 1 (mTORC1)
<ul>
<li>mTOR and Raptor</li>
<li>mLST8 (<span class="math inline">\(G \beta L\)</span>), stabilizes the kinase loop. It allows mTOR to
phosphorlate its target.</li>
<li>PRAS40 and DEPTOR inhibitors
<img src="/images/mtor1.png" alt="mTOR 1 complex" /></li>
</ul></li>
<li>Complex 2 (mTORC2)
<ul>
<li>DEPTOR inhibitor</li>
<li>Protor1/2 and mSin1 are regulators
<img src="/images/mtor2.png" alt="mTOR 2 complex" /></li>
</ul></li>
</ul>
<p>Drugs are used to inhibit the mTOR pathway, one of which is
rapamycin. Apparently rapamycin is only useful in complex 1.</p>
<h1 id="autophagy">Autophagy</h1>
<p>Autophagy is lysosomal recycling, essentially. It works via phagophore
which engulfs intracellular “cargo”, such as protein aggregates,
ribosomes, and organelles. This phagophore then fuses with the
lysosome, where its cargo is degraded via the lysosome’s acid
proteases. The byproducts of degradation are sent back out to the
cytoplasm where they can be reused for metabolism. This includes ATP
generation.</p>
<h1 id="lysosome">Lysosome</h1>
<p>The lysosome is a cell organelle that contains digestive parts. It
breaks down excess or worn out cell parts, they may also be used to
destroy invaders. Additionally, lysosomes may play a role in
self-destruction / programmed cell death / apoptosis.</p>
<h2 id="storage-diseases">Storage Diseases</h2>
<p>The TBCK paper from 2017 mentions similar “coarse facial features” to
those individuals affected by storage diseases. A storage disease is a
lysosomal disease, wherein the lysosome fails to do its autophagic
work, leaving the cell overloaded with substrates.</p>
<h1 id="the-logic-of-protein">The Logic of Protein</h1>
<p>These stream diagrams or whatever they’re called are interesting. The
red T arrow indicates an inhibitory relationship while the black arrow
indicates activation; but, as these things compose, their
relationships follow a logical dual. Here’s a few examples:</p>
<ul>
<li><span class="math inline">\(A \dashv B \to C\)</span>: A inhibits B, which activates C, so A inhibits B</li>
<li><span class="math inline">\(A \dashv B \dashv C\)</span>: A inhibits B, which inhibits C, so A
<em>activates</em> C.</li>
</ul>
<p>It’s the composition of two inhibitory relations that produce an
activating one. And the same for the composition of two activating
relations. Sounds like this is only the case for inhibitory relations.</p>
<h1 id="leucine">Leucine</h1>
<figure>
<img src="/images/leucine.png" alt="Leucine" />
<figcaption aria-hidden="true">Leucine</figcaption>
</figure>
<p>Following the logic here—that the composition of two inhibitors make
an activator—we can see that leucine is an <em>activator</em> for mTORC1:</p>
<p><span class="math display">\[
\text{Leucine} \dashv \text{Sestrin2} \dashv \text{GATOR2} \Rightarrow \text{Leucine} \rightarrow \text{GATOR2} \\
\text{GATOR2} \dashv \text{GATOR1} \dashv \text{RAG} \Rightarrow \text{GATOR2} \rightarrow \text{RAG}
\]</span></p>
<p>Therefore:</p>
<p><span class="math display">\[
\text{Leucine} \rightarrow \text{Rag}\ (\text{mTORC1})
\]</span></p>
<blockquote>
<p>TBCK has been shown to regulate the mammalian target of rapamycin
(mTOR) signaling pathway, which is also stimulated by exogenous
leucine supplementation. <strong>TBCK was absent in cells from affected
individuals</strong>, and decreased phosphorylation of phospho-ribosomal
protein S6 was also observed, a finding suggestive of downregulation
of mTOR signaling.</p>
</blockquote>
<p>This is from the abstract of <em>Mutations in TBCK, Encoding
TBC1-Domain-Containing Kinase, Lead to a Recognizable Syndrome of
Intellectual Disability and Hypotonia</em> (emphasis is mine, though).</p>
<p>It feels like this implies that TBCK and leucine both have the same
job—activating mTORC1, so supplementing with leucine could “make up
for” the missing or pathologically low levels of TBCK. So the
hypothesis is correct here, but the causality is not:</p>
<blockquote>
<p>knockdown of TBCK decreases the transcription of mTOR complex
(mTORC) proteins and downregulates mTOR signaling.</p>
<p>This is why leucine helps, though, as it activates mTORC1.</p>
</blockquote>
<p><del>phospho-ribosomal protein S6, and its phosphorylation, and what
happens to S6 when it isn’t getting phosphorylated. Lysosome stress?
Storage disease?</del></p>
<p>No, rereading this, it sounds like decreased phosphorylation of S6 is
actually a sign that there’s been down regulation along mTOR.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/fern/index.html" term="fern" label="fern"/>
    
    <category scheme="https://dpitt.me/tags/tbck/index.html" term="tbck" label="tbck"/>
    
    <category scheme="https://dpitt.me/tags/genetics/index.html" term="genetics" label="genetics"/>
    
    <category scheme="https://dpitt.me/tags/biology/index.html" term="biology" label="biology"/>
    
    
  </entry>
  
  <entry>
    <title type="html">A refinement of Nietzsche's Discussion on Identity</title>
    <link href="https://dpitt.me/forest/dsp-0026.html" rel="alternate" type="text/html"/>
    <published>Sun, 18 Jun 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>Below are a few paragraphs I wrote in a personal journal in the fall
of 2018. I’d just come off a weekend with my oldest friends and felt
alive and new, ready to face the adult world that I’d been avoiding at
all costs. About two years before this was written, I’d attempted to
<a href="https://obstacleallusion.com/blog/2019/09/deleuze/">slog through <em>Anti-Oedipus: Capitalism and
Schizophrenia</em></a>
where I read a line from Nietzsche that its authors were
discussing. That line said something along the lines of “identity
oscillates around a fixed point”. I glommed on to this and have held
close to it for years, but coming out of that weekend of juvenile
freedom, I realized that this observation was in need of refinement.</p>
<hr />
<p>Identity is, through life, a convergence on some unattainable
state. The problem with Nietzsche’s take is that an oscillation makes
no forward progress. Instead, we’re unraveling our existence like an
infinite ball of yarn, with each revolution of the ball getting us
closer to purpose, to clarity, to zen. If we’re left to oscillate
around the ball, then we never creep nearer its center. The
implication of Nietzsche’s metaphor is a life without self-reflection,
or at least one without any productive reflection. Productive
reflection is the following of the thread, tracing what we know now
and using it make inferences about the paths which lie ahead. If the
center is then unattainable, then perhaps happiness is merely the act
of its pursuit, the act of continuing to follow the thread.</p>
<p>Nevertheless, this realization is one which I came to after a week of
living in my new skin, having removed a layer upon arrival at home. It
was fresh, pink, and unblemished. This is what leads me to believe
that happiness is simply the act of exposing a new self, one that is
slightly nearer the true Self that lies at our core. I personally
experience this as what I’ve deemed an “identity shift”. This one was
particularly affecting, however, because it traveled with an awareness
of <em>why</em>, again reiterating that without forward progress, without a
steady convergence, the shift is merely a shift. An
oscillation. Happiness and contentment are fed by the thread we leave
behind.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/philosophy/index.html" term="philosophy" label="philosophy"/>
    
    <category scheme="https://dpitt.me/tags/self/index.html" term="self" label="self"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Understanding Fibrations</title>
    <link href="https://dpitt.me/forest/dsp-0025.html" rel="alternate" type="text/html"/>
    <published>Wed, 24 May 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>I started reading Bart Jacob’s <em>Categorical Logic and Type Theory</em>
book today after seeing it mentioned on the Category Theory Zulip
because I’ve been seriously itching to get back into some mathy /
formalish stuff—life in hardware/software land just hasn’t been
cutting it. I have heard a dozen times, I once even tried to slog
through the CwF paper, about the categorical semantics for type theory
being presheaf based as the presheaf apparently captures the notion of
type families. However, after reading through Chapter 0: Prospectus in
Jacobs’ book, it sounds like he’s going to use fibrations to talk
about indexing rather than presheafs, so I wanted to update my
understanding, precisely, on what a fibration actually was. After
having moderate success recently in asking it for help to remember how
to do <code>Either</code>-based error <em>returning</em> rather than exception
<em>throwing</em> in Haskell, I thought I’d chat with ChatGPT viz-a-viz
categorical fibrations. Here’s the first thing it told me, which makes
sense:</p>
<div class="multiline-quote">
<p>[A] functor <span class="math inline">\(F: C \to D\)</span> between categories is called a fibration if
for every morphism <span class="math inline">\(f: X \to Y\)</span> in the codomain category D and for
every object <span class="math inline">\(X\)</span> in the domain category <span class="math inline">\(C\)</span>, there exists:</p>
<ol type="1">
<li>An object <span class="math inline">\(\text{fib}(f)\)</span> in <span class="math inline">\(C\)</span>, called the “fiber” or “preimage”
of <span class="math inline">\(f\)</span> under <span class="math inline">\(F\)</span>, and</li>
<li>A morphism <span class="math inline">\(p(f): \text{fib}(f) \to X\)</span> in <span class="math inline">\(C\)</span>, called the
“lifting” or “pullback” of <span class="math inline">\(f\)</span> along <span class="math inline">\(F\)</span>, such that <span class="math inline">\(F(p(f)) = f\)</span>.</li>
</ol>
</div>
<p>Ok GPT, I’m with you so far. So next I asked it if it could give me a
diagram. “Certainly!” it replied, followed with an example using the
category <span class="math inline">\(C\)</span>, a category with two objects <span class="math inline">\(0\)</span> and <span class="math inline">\(1\)</span>, and
<em><em>only</em></em> identity morphisms, and a functor <span class="math inline">\(F : C \to D\)</span> where <span class="math inline">\(D\)</span>
was the category given by <span class="math inline">\(\langle\mathbb{N},\ \le\rangle\)</span>. It then
gave an example of a <span class="math inline">\(p\)</span> that depended on a morphism in <span class="math inline">\(C\)</span> from
<span class="math inline">\(0\)</span> to <span class="math inline">\(1\)</span>. Which, in its own example did not exist.</p>
<p>I corrected it, telling that there was no morphism <span class="math inline">\(f : 0 \to 1\)</span> in
<span class="math inline">\(C\)</span>, so it gave me another example with <span class="math inline">\(C\)</span> being the two object
category with <span class="math inline">\(A\)</span> and <span class="math inline">\(B\)</span> and again only identity morphisms, and
<span class="math inline">\(D\)</span> being <span class="math inline">\(\textbf{Set}\)</span>. And then proceeded to, three different
times, give me examples where the lifting morphism depended on a
morphism <span class="math inline">\(A \to B\)</span> in <span class="math inline">\(C\)</span>.</p>
<p>Finally, it gave me an example that makes sense after I told it,
“That’s the same example! You’re still saying that id_A is A -&gt; B, but
this is only the case if A and B are isomorphic, leaving C to be a
category with one object rather than two.”</p>
<p>This time it told me that <span class="math inline">\(f\)</span> was <span class="math inline">\(\text{Id}_X\)</span> then <span class="math inline">\(p(f)\)</span> was
<span class="math inline">\(\text{Id}_a\)</span>, but if <span class="math inline">\(f = \text{I}_Y\)</span>, then there was no morphism
that lifts <span class="math inline">\(f\)</span> in <span class="math inline">\(C\)</span>. Finally I asked it if the lifting <span class="math inline">\(p\)</span>
could be</p>
<p><span class="math display">\[
p(f) = \left\{\begin{array}{lr}
  f = \text{Id}_X &amp; \text{Id}_A \\
  f = \text{Id}_Y &amp; \text{Id}_B
  \end{array}\right\}
\]</span></p>
<p>To which it said, ah, yes, that should work too:</p>
<blockquote>
<p>Apologies for the confusion caused by my previous incorrect
statement. You are correct. In the given example, if f is the
identity morphism on Y (f = id_Y), then the fiber fib(f) is the
object B in C, and the lifting p(f) is the identity morphism id_B: B
-&gt; B.</p>
</blockquote>
<p>Concluding with:</p>
<div class="multiline-quote">
<p>The fiber <span class="math inline">\(\text{fib}(f)\)</span> for <span class="math inline">\(f\)</span> in <span class="math inline">\(C\)</span> is defined as follows:</p>
<ul>
<li>If <span class="math inline">\(f\)</span> is the identity morphism on <span class="math inline">\(X\ (f\ =\ id_X)\)</span>, then the fiber <span class="math inline">\(\text{fib}(f)\)</span>
is the object <span class="math inline">\(A\)</span> in <span class="math inline">\(C\)</span>.</li>
<li>If <span class="math inline">\(f\)</span> is the identity morphism on <span class="math inline">\(Y\ (f\ =\ id_Y)\)</span>, then the fiber <span class="math inline">\(\text{fib}(f)\)</span>
is the object <span class="math inline">\(B\)</span> in <span class="math inline">\(C\)</span>.</li>
</ul>
<p>To define the lifting <span class="math inline">\(p(f)\)</span> for <span class="math inline">\(f: X \to Y\)</span>:</p>
<ul>
<li>If <span class="math inline">\(f\)</span> is the identity morphism on <span class="math inline">\(X (f = id_X)\)</span>, then the lifting
<span class="math inline">\(p(f)\)</span> is the identity morphism <span class="math inline">\(id_A: A \to A\)</span> in <span class="math inline">\(C\)</span>.</li>
<li>If <span class="math inline">\(f\)</span> is the identity morphism on <span class="math inline">\(Y (f = id_Y)\)</span>, then the lifting
<span class="math inline">\(p(f)\)</span> is the identity morphism <span class="math inline">\(id_B: B \to B\)</span> in <span class="math inline">\(C\)</span>.</li>
</ul>
</div>
<p>So if you’re following along this far you may have noticed that,
because of ChatGPT’s bad examples, I missed an important detail: <span class="math inline">\(f\)</span>
is supposed to be <span class="math inline">\(X \to Y\)</span> in <span class="math inline">\(D\)</span>—it can’t be identities in
<span class="math inline">\(D\)</span>! The only way this works is if in <span class="math inline">\(F\)</span>’s domain category, there
is at least one morphism between <span class="math inline">\(A\)</span> and <span class="math inline">\(B\)</span>. Here’s the last
example that we arrived at together:</p>
<div class="multiline-quote">
<p>Consider the category <span class="math inline">\(C = \{A, B\}\)</span> with two objects <span class="math inline">\(A\)</span> and
<span class="math inline">\(B\)</span> and two non-identity morphisms <span class="math inline">\(f: A \to B\)</span> and <span class="math inline">\(g: A \to
B\)</span>. Let <span class="math inline">\(D\)</span> be the category of sets.</p>
<p>Define a functor <span class="math inline">\(F: C \to D\)</span> as follows:</p>
<ul>
<li><span class="math inline">\(F(A) = X\)</span>, where <span class="math inline">\(X\)</span> is a set,</li>
<li><span class="math inline">\(F(B) = Y\)</span>, where <span class="math inline">\(Y\)</span> is another set,</li>
<li><span class="math inline">\(F(f) = h: X \to Y\)</span>, a function from <span class="math inline">\(X\)</span> to <span class="math inline">\(Y\)</span>,</li>
<li><span class="math inline">\(F(g) = k: X \to Y\)</span>, another function from <span class="math inline">\(X\)</span> to <span class="math inline">\(Y\)</span>.</li>
</ul>
<p>In this case, the functor <span class="math inline">\(F\)</span> maps objects <span class="math inline">\(A\)</span> and <span class="math inline">\(B\)</span> in <span class="math inline">\(C\)</span>
to sets <span class="math inline">\(X\)</span> and <span class="math inline">\(Y\)</span> in <span class="math inline">\(D\)</span>, respectively, and it maps the
non-identity morphisms <span class="math inline">\(f\)</span> and <span class="math inline">\(g\)</span> in <span class="math inline">\(C\)</span> to the functions <span class="math inline">\(h\)</span>
and <span class="math inline">\(k\)</span> in <span class="math inline">\(D\)</span>.</p>
<p>To show that <span class="math inline">\(F\)</span> is a fibration, let’s consider a morphism <span class="math inline">\(f: X
\to Y\)</span> in <span class="math inline">\(D\)</span>.</p>
<p>The fiber <span class="math inline">\(\text{fib}(f)\)</span> for <span class="math inline">\(f\)</span> in <span class="math inline">\(C\)</span> is defined as the
object <span class="math inline">\(A\)</span> in <span class="math inline">\(C\)</span>.</p>
<p>To define the lifting <span class="math inline">\(p(f)\)</span> for <span class="math inline">\(f: X \to Y\)</span>:</p>
<ul>
<li>If <span class="math inline">\(f\)</span> is the function <span class="math inline">\(h: X \to Y\)</span>, the lifting <span class="math inline">\(p(f)\)</span> is the
morphism <span class="math inline">\(f: A \to B\)</span> in <span class="math inline">\(C\)</span>.</li>
<li>If <span class="math inline">\(f\)</span> is the function <span class="math inline">\(k: X \to Y\)</span>, the lifting <span class="math inline">\(p(f)\)</span> is the
morphism <span class="math inline">\(g: A \to B\)</span> in <span class="math inline">\(C\)</span>.</li>
</ul>
<p>In both cases, applying <span class="math inline">\(F\)</span> to the lifting <span class="math inline">\(p(f)\)</span> yields the
original morphism <span class="math inline">\(f: X \to Y\)</span>.</p>
<p>Therefore, the functor <span class="math inline">\(F: C \to D\)</span> is a fibration since, for every
morphism <span class="math inline">\(f: X \to Y\)</span> in <span class="math inline">\(D\)</span>, the fiber <span class="math inline">\(\text{fib}(f) = A\)</span> in
<span class="math inline">\(C\)</span>, and the lifting <span class="math inline">\(p(f)\)</span> satisfies <span class="math inline">\(F(p(f)) = f\)</span>.</p>
</div>
<p>Which, as it states, satisfies <span class="math inline">\(F(p(f)) = f\)</span>. I love that everything
ChatGPT does is in the format that written standardized tests in
middle school were required to be… “In conclusion, …”</p>
<p>Anyway, we got there eventually, and now I feel like I actually know
what the hell a fibration is, and although it was an oblique route,
ChatGPT was yet again helpful.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Alon Systems Biology Course Lecture Notes</title>
    <link href="https://dpitt.me/forest/dsp-6M3W.html" rel="alternate" type="text/html"/>
    <published>Sat, 18 Mar 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <h1 id="lecture-1">Lecture 1</h1>
<ul>
<li>13:30: “Proteins are built as machines that can transition between
states”</li>
<li>E coli changes its composition according to its environment, how
does it compute when to make a protein?
<ul>
<li><p>RNAp binds, transcribes to mRNA, which is translated into a
protein.</p></li>
<li><p>Proteins called transcription factors represent the external
environment internally to the cell.</p></li>
<li><p>transcription factors regulate transcription</p></li>
<li><p>“the cell has 300 degrees of freedom”</p></li>
<li><p>transcription factors have specific binding sites and when they
bind, it “increases the probability” that the RNAp will do its
thing.</p>
<p>We use <span class="math inline">\(X \to Y\)</span> to symbolize this entire interaction.</p></li>
</ul></li>
<li>This happens across several orders of magnitude in terms of
timescales. The binding of a transcription factor to an
environmental trigger is µs, TF to binding site is seconds,
translation and transcription is minutes and finally, making it to
“functional volume” is hours.</li>
<li>The transcription factor binding site can, in humans, have as many
as 20 TFs bound there, and the result of the binding is a
probabilistic logic program.</li>
<li>31:40 “When you collect all the arrows, you get a graph.” GRN: Gene
Regulation Network</li>
<li>34:42 “Evolution didn’t go to engineering school, but it still has
understandable features”
<ul>
<li>In this he was talking about how when engineers build a system,
they build it out of small parts that they
understand. Apparently evolution did the same thing, regardless
of its need to be understandable post facto. This was commentary
on the fact that the parts are the same, designed for reuse
across most species. This seems profound on its face, but the
truth is that it is really that they are descendant from the
same ancestors—single celled organisms, literal prototypes—of
the massively complex multicellular organisms of today.</li>
<li>E coli’s order is ~4500, while <span class="math inline">\(|E|\)</span> is ~10,000.</li>
</ul></li>
<li>The <strong>input function</strong> plots, given an <span class="math inline">\(X \to Y\)</span>, the rate of <span class="math inline">\(Y\)</span>
production given a certain <span class="math inline">\(X^*\)</span> concentration?
<ul>
<li><span class="math inline">\(X^*\)</span> denotes the “activated” version of <span class="math inline">\(X\)</span>, <span class="math inline">\(\beta\)</span> is max
value / concentration, <span class="math inline">\(K\)</span> is 50% activity / concentration and
<span class="math inline">\(n\)</span> is the “steepness” of the hill function, when <span class="math inline">\(N = 1\)</span>, this
function is linear, when it’s <span class="math inline">\(\infty\)</span>, it’s a step function.</li>
<li><span class="math inline">\(n\)</span> is typically between 1 and 4.</li>
</ul></li>
</ul>
<p><span class="math display">\[
\cfrac{\beta (X^{*})^n}{K^n+(X^{*})^n}
\]</span></p>
<ul>
<li>When a gene is bound by two TFs, it is a product of hill functions,
but works like a AND or OR gate. Apparently we don’t know how to
“systematically look at them”.</li>
<li>Synthetic biology</li>
<li>“circuits”—subgraphs of the larger network within a cell.</li>
<li>A proteasome cuts a protein back up into amino acids.</li>
</ul>
<p><span class="math display">\[
\cfrac{dy}{dt} = \beta - \alpha y
\]</span></p>
<ul>
<li>This is for a function <span class="math inline">\(x \to y\)</span>, where <span class="math inline">\(\beta\)</span> is the rate of
production of <span class="math inline">\(x\)</span>, i.e., “signal”, and <span class="math inline">\(\alpha\)</span> is the rate of
degradation. This can be done via dilution or by direct degradation,
via proteasomes.
<ul>
<li>In a steady state, <span class="math inline">\(\cfrac{dy}{dt}\)</span> is <span class="math inline">\(0\)</span>, so <span class="math inline">\(\beta - \alpha y = 0\)</span>,
so you have <span class="math inline">\(\cfrac{\beta}{\alpha}\)</span>—production over removal.</li>
</ul></li>
<li>response time is dependent only on removal rate, there is math to
explain this but I don’t understand it yet. He uses <span class="math inline">\(e\)</span> in the
equations and mentions an eigenvalue, but I don’t know what that is.
<ul>
<li>High response time is given by high removal, but this is
expensive.</li>
<li>production of half the steady state of proteins is 1 generation,
meaning that the cell reach steady state only to divide to give
those proteins to its descendants. This problem will be the
topic of the next lecture.</li>
</ul></li>
</ul> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/biology/index.html" term="biology" label="biology"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Dan Pittman</title>
    <link href="https://dpitt.me/forest/dsp-0032.html" rel="alternate" type="text/html"/>
    <published>Wed,  8 Mar 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>(contact info available by <a href="/files/cv.pdf">downloading a pdf</a> version of
this document)</p>
<h1 id="intel-corporation">Intel Corporation</h1>
<h2 id="technical-lead-cryptography">Technical Lead, Cryptography</h2>
<p><strong>08/2023 - Present</strong></p>
<ul>
<li>Ported OpenSSL AVX-512 RSA Implementation to AWS-LC and BoringSSL</li>
<li>Optimized AVX-512 XTS implementation to use on-stack keys in AWS-LC</li>
<li>Ported that optimized XTS implementation to OpenSSL (in progress)</li>
<li>Mechanical assembly dialect translation (in progress)</li>
</ul>
<h3 id="technologies-involved">Technologies Involved</h3>
<ul>
<li>C, C++, GAS/Perl assembly (perlasm)</li>
<li>Haskell (asm translation)</li>
<li>Extensive mathematical optimizations involved in the RSA algorithm</li>
</ul>
<h2 id="technical-lead-hyperscaler-resource-management">Technical Lead, Hyperscaler Resource Management</h2>
<p><strong>10/2021 - 08/2023</strong></p>
<p>We did weird and innovative Linux things that I can’t really talk about,
both in the kernel and in userspace, to serve Intel’s hyper-scale
customers.</p>
<h3 id="technologies-involved-1">Technologies Involved</h3>
<ul>
<li>C/Rust</li>
<li>Userspace page faults</li>
<li>OoT Kernel Modules</li>
<li>Linux’s virtual file systems (think cgroups’ filesystem)</li>
<li>Intel’s RDT</li>
<li>Intel’s top-down µarch PMUs, used in real time</li>
</ul>
<h1 id="auxon-corporation">Auxon Corporation</h1>
<h2 id="principal-and-founding-engineer">Principal and Founding Engineer</h2>
<p><strong>07/2018 - 10/2021</strong></p>
<p>For the first half of my time at Auxon, I continued my exploration into
applying proof theoretical methods for checking software at both compile
time and runtime.</p>
<p>Once Auxon moved on from developing high-assurance software itself, I
began focussing on the customer-facing edges of our product, finding
that putting myself in the shoes of potential users came naturally.
During this time I developed and implemented Modality’s log, check, and
cumulative report features.</p>
<h3 id="additional-accomplishments">Additional Accomplishments</h3>
<ul>
<li>We made a <a href="https://github.com/auxoncorp/ferros">runtime in Rust on top of
seL4</a>. In this runtime, we went
through great lengths to extend the guarantees that seL4 provides to
also be checkable at compile-time through techniques which can be
summarized as “type-level programming”.</li>
<li>I owned the architecture-agnostic implementation for that runtime
that used type-level induction to provide a mechanism for
architecture-agnostic memory hierarchies.</li>
<li>I designed and implemented a <a href="https://github.com/auxoncorp/bounded-registers">memory-mapped registers
library</a> that
provided:
<ul>
<li>Compile-type checked writes (for values known a priori).</li>
<li>A proof-carrying approach for runtime checking.</li>
<li>Complete proofs of that library’s correctness in Agda.</li>
</ul></li>
<li>I built a tool in Haskell that translates Rust types (and therefore
type-level programs) to Agda. I <a href="https://youtu.be/jrVPB-Ad5Gc?si=Qk3oRQuq9YUpV9p5">spoke about an early iteration of
this at Strange Loop
2018</a>.</li>
</ul>
<h1 id="polysync">Polysync</h1>
<h2 id="principal-engineer">Principal Engineer</h2>
<p><strong>11/2017 - 07/2018</strong></p>
<p>I came to PolySync to contribute to the high-assurance automotive
runtime they were building working as a hybrid proof / software
engineer. PolySync intended to use my work and research to find ways to
blur the line between proof and program to instill further confidence in
the integrity of the software they developed.</p>
<h3 id="additional-accomplishments-1">Additional Accomplishments</h3>
<ul>
<li>We made a toolchain for writing seL4 tasks in Rust.</li>
<li>I designed a distributed system whose core logic was session types
predicated on Atkey-style indexed monads translated to Rust to run
on seL4.</li>
</ul>
<h1 id="intel">Intel</h1>
<h2 id="sr.-cloud-software-engineer">Sr. Cloud Software Engineer</h2>
<p><strong>12/2014-11/2017</strong></p>
<ul>
<li>I led a team that built a distributed execution engine that tried to
model the data center as a category—states as objects, transitions
as morphisms (ask me more about this, it was fun).</li>
<li>I worked on making persistent memory useful in cloud environments
(Go/etcd)</li>
<li>I helped design and build a modular and flexible telemetry system
for use in the data center.</li>
</ul>
<h1 id="vmware">VMware</h1>
<h2 id="sr.-member-of-technical-staff">Sr. Member of Technical Staff</h2>
<p><strong>12/2012-12/2014</strong></p>
<ul>
<li>I was one of the first 10 developers working on VMware’s public
cloud platform, vCHS.</li>
<li>We made a globally distributed system which could deploy complete
private VMware environments to the cloud in minutes. It did this
through a number of interesting technologies including but not
limited to: CQRS &amp; Event Sourcing, AMQP, Cassandra, (and all of its
finicky eventual consistency idiosyncrasies), and a homegrown Paxos
implementation—which I was later responsible for replacing with
Zookeeper.</li>
</ul>
<h1 id="enterhost-consuro">Enterhost / Consuro</h1>
<h2 id="system-administrator">System Administrator</h2>
<p><strong>2/2012-12/2012</strong></p>
<p>At Enterhost, a hosting provider, and its MSP arm Consuro, I was
responsible for designing and then implementing and/or installing
medium-sized networks involving VMware infrastructure, typically on
customer premises with disaster recovery environments collocated at
Enterhost’s datacenter.</p>
<h1 id="wm-automotive">WM Automotive</h1>
<h2 id="system-administrator-1">System Administrator</h2>
<p><strong>7/2008-2/2012</strong></p>
<p>I was the primary IT provider for a midsize enterprise—an aftermarket
automotive parts distributor and retailer. During the time I was there,
I rebuilt their technology infrastructure from the ground up.</p>
<p>This included:
* A network overhaul to include company-owned stores on the LAN.
* Virtualizing the CRM/ERP software stack for the B2B arm of the
business.
* The deployment of Windows’ enterprise stack (Exchange / Active
Directory), desktop imaging and deployment.
* Centralized SAN-based storage.
* An active/passive disaster recovery plan.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/cv/index.html" term="cv" label="cv"/>
    
    <category scheme="https://dpitt.me/tags/self/index.html" term="self" label="self"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Tell Me I'm Worthless</title>
    <link href="https://dpitt.me/forest/dsp-0024.html" rel="alternate" type="text/html"/>
    <published>Sun, 12 Feb 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><img src="/images/worthless.jpg" width="200"></p>
<p>Radical and terrifying. The book opens with a Deleuze quote, and (of
course) I can’t help but think about through that lens. The House is a
palpable and properly insidious expression of the Oedipal triangle,
and it represents all that Deleuze and Guattari originally intended:
gender and sexual normativity, fascism, the strictures of societal
expectations and so on. In a pair of words, Freudian
psychoanalysis. Rumfitt gives her representation of this mental,
sexual, and societal cell the weight that it requires; to her, it’s
not an observation from the relatively safe space of the 1970s like
D&amp;G’s was. No, she knows how dangerous and real The House is. <em>Tell Me
I’m Worthless</em> is presentiment, not in language but in
feeling. Portentous, not through data or rationality, but through
fear. It evinces the existential terror we all felt on January 6th—we
saw irrefutable truth that fascism is here, and real—but Rumfitt does
it through the lens of someone whose whole life has been ruddered by
that terror. She viscerally illustrates that fascism is the cop inside
your head and is asking you to, in the words of Boston hardcore band
Move, “kill the cop inside your head, decarcerate your mind.”</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/book/index.html" term="book" label="book"/>
    
    <category scheme="https://dpitt.me/tags/review/index.html" term="review" label="review"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Aleph</title>
    <link href="https://dpitt.me/forest/dsp-0023.html" rel="alternate" type="text/html"/>
    <published>Fri, 13 Jan 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <blockquote>
<p>He’d come to understand the tongues of beasts at the cost of his
reason, while I was destroying human language to reveal the river of
nonsense coursing just beneath its “good, sound rules.”</p>
</blockquote>
<div class="quote-ref">
<p>Ben Lerner, <em>The Topeka School</em></p>
</div>
<h2 id="i.">I.</h2>
<p>Language is a tool for communication. But for it to be a tool that can act as a
medium, that tool must be concomitant with a set of rules that participating
parties agree on and know ahead of time. Creating a system like this is often called a <em>formalization</em>.
Formalizations are often delineated along an axis of <em>syntax</em> and <em>semantics</em>.
Syntax consists of the rules in practice, and a system’s semantics are the
meaning we get by using that system. In the world of the foundations of
mathematics or analytic philosophy, you may see a diagram that looks like this:</p>
<p><img class="para-centered-image" src="/images/model.png"></p>
<p>These pillars tame entropy into information, without them, the signal is lost to
the noise.</p>
<h2 id="ii.">II.</h2>
<p>Formalization like this is hard, because signifying an intuition through a
syntax is inherently a limiting action. Ideas, intuitions, emotions, all of
these exist in a continuous, entropic space, unbroken by the discretizing force
of syntax. When we attempt to relate an idea via speech or writing, that
continuous space must be reduced into differentiable pieces. Deleuze and
Guattari wrote about this phenomenon and termed it “territorializing”. To get a
grip on their concept, think of a pristine, undiscovered continent. Upon
discovery, its (assumption: capitalist) discoverers break the land up
arbitrarily such that it may be classified as states, countries, counties, or
even parcels of land to be sold. This is a material example of territorializing.
But Deleuze and Guattari didn’t stop there—they abstracted territorializing and
found limitless applications. Language itself is a territorializer to our
thoughts. Their most famous work, the Capitalism and Schizophrenia series,
discusses a “critical schizophrenia”. At its core, it is a rejection of Freudian
psychoanalysis. They saw Freud’s battle with schizophrenia to truly be about the
assimilation of the analysand. D&amp;G’s critical schizophrenia refers to the
untethered and unassimilated analysand—to a mind deterritorialized. Language,
according to Deleuze and Guattari, is a gilded cage for your thoughts.</p>
<p><img class="para-centered-image" src="/images/carrington.jpg"></p>
<div class="quote-ref">
<p>“There are things that are not sayable, that’s why we have art.” -
Leonora Carrington (La maja del tarot, 1965)</p>
</div>
<p>D&amp;G’s isn’t the only attempt to articulate such a phenomenon. It,
albeit in different terms, is also discussed in Bernard Steigler’s
book <em>For a New Critique of Political Economy</em>. Steigler makes the
point that the very act of writing a practice down, i.e.,
externalizing it from one’s mind, is a tool for oppression. His
argument is that <em>hypomnesis</em>, the exteriorization of memory, induces
the possibility of a management class. Hypomnesis’ antonym,
<em>anamnesis</em>, refers to internalized recollection or reference, without
the need for external aids. In a world primarily driven by anamnesis,
each generation learns its trade, its means for survival, from the one
that came before it. Knowledge is passed down directly, its purpose is
for the survival and the flourishing of generations to come. The point
that Stiegler is trying to make is that hypomnesis is how a managerial
class can invent itself, without the abstraction of exteriorization
there cannot be the abstraction of a managerial class; the two are
inextricably combined. Steigler’s is a Marxist argument: the
territorialization of generational knowledge is a cornerstone in
separating real value from use value, precisely through that
generative abstraction.</p>
<h2 id="iii.">III.</h2>
<p>I think Steigler is right w/r/t grammatization (his term for what we’ve been
calling territorializing) being a crucial tool for the oppression of the working
class, however, his theory’s veracity is in my opinion, governed by the intent
of the grammarizer. If you take Steigler’s argument as I understand it to its
terminus, you arrive at a kind of ludditic anarcho-primitivism. This is in
contradiction with a different type of syntax, the syntax of mathematics, whose
goal is to universalize access to precise information. When we externalize a
mathematical thought through syntax, it isn’t for oppression, it’s to <em>verify</em>.
Without a reduction in fidelity, nothing could be checked; not just because
verification inherently entails exteriorization, but also because to verify
infinity, you would first need to calculate it. However, if we can signify
infinity in the finite through syntax, we can verify it as well. In fact, we can
even ask a computer to verify it for us by presenting the computer its own
syntax—a set of rules—which can be used to interpret and then validate the
syntax we’ve input. This is of course something a computer can do faster and
with greater precision than we can as humans for many reasons, not the least of
which is that a computer does not suffer the loss of fidelity or struggle to
discretize intuition because its entire existence is resident in the world of
syntax. What is a computer besides a territorializer of electrons, anyway?</p>
<p class="pull">
What is a computer besides a territorializer of
electrons, anyway?
</p>
<p>An example: The notion that a + b = b + a is something we intuitively know,
we’ve known it since not long after we learned addition. This law is called
commutativity, it generalizes the idea of flipping operands and ending up with
the same result. Below is a proof of the commutativity of addition on the
natural numbers.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span>identᵣ <span class="op">:</span> ∀ (a <span class="op">:</span> ℕ) → a <span class="op">+</span> zero ≡ a</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span>identᵣ zero <span class="ot">=</span> refl</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span>identᵣ (<span class="fu">succ</span> a) <span class="ot">=</span> cong <span class="fu">succ</span> (<span class="op">+-</span>identᵣ a)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span><span class="fu">succ</span> <span class="op">:</span> ∀ (a b <span class="op">:</span> ℕ) → (<span class="fu">succ</span> a) <span class="op">+</span> b ≡ a <span class="op">+</span> (<span class="fu">succ</span> b)</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span><span class="fu">succ</span> zero b <span class="ot">=</span>  refl</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span><span class="fu">succ</span> (<span class="fu">succ</span> a) b <span class="ot">=</span> cong <span class="fu">succ</span> (<span class="op">+-</span><span class="fu">succ</span> a b)</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span>comm <span class="op">:</span> ∀ (a b <span class="op">:</span> ℕ) → a <span class="op">+</span> b ≡ b <span class="op">+</span> a</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span>comm zero b <span class="ot">=</span> sym (<span class="op">+-</span>identᵣ b)</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="op">+-</span>comm (<span class="fu">succ</span> a) b <span class="ot">=</span> trans (cong <span class="fu">succ</span> (<span class="op">+-</span>comm a b)) (<span class="op">+-</span><span class="fu">succ</span> b a)</span></code></pre></div>
<p>This proof, in a way, calculates infinity. The natural numbers are, after all,
an infinite set. The last stanza, those three lines that begin with +-comm,
comprise the commutativity proof, the other two stanzas are lemmas needed to
complete the final proof. This, to me, is irrefutable evidence of the limiting
nature of syntax. It’s through syntax, with which we signify infinity, that we
can begin to wrestle with it.</p>
<h2 id="iv.">IV.</h2>
<p>Another almost-articulation of this is Kant’s analytic and synthetic
divide. I’ll quote the
<a href="https://plato.stanford.edu/entries/analytic-synthetic/">SEP</a>:</p>
<blockquote>
<p>“Analytic” sentences, such as “Pediatricians are doctors,” have
historically been characterized as ones that are true by virtue of the
meanings of their words alone and/or can be known to be so solely by
knowing those meanings. They are contrasted with more usual
“synthetic” sentences, such as “Pediatricians are rich,” (knowledge
of) whose truth depends also upon (knowledge of) the worldly fortunes
of pediatricians.</p>
</blockquote>
<p>What we have above is an <em>analytic</em> proof of the commutativity of
addition. Its <em>synthetic</em> proof, one more representative of our
intuition, would require infinite time and infinite resources. The
synthetic proof requires us to run every possible combination of pairs
of natural numbers, effectively squaring infinity. Now, obviously,
synthetic / analytic are not the same as the phenomenon of syntax’s
discretizing force. It would of course still be syntax that we’d use
to calculate infinity if we had infinite time to calculate it. Like
the sentences you’re reading here, we’d be using syntax to bridle
infinity. My goal instead is to articulate that all of these concepts
are related, somehow, but that we lack the medium through which to
elucidate exactly how. The notion of an unobtainable medium evinces
St. Anselm’s argument for the existence of God (again, from the
<a href="https://plato.stanford.edu/entries/ontological-arguments/">SEP</a>):</p>
<blockquote>
<p>St. Anselm reasoned that, if such a being fails to exist, then a
greater being—namely, a being than which no greater can be conceived,
and which exists—can be conceived. But this would be absurd: nothing
can be greater than a being than which no greater can be conceived. So
a being than which no greater can be conceived—i.e., God—exists.</p>
</blockquote>
<p>St. Anselm’s ontological argument makes the case that God is that unattainable
medium. It’s a medium, a being, a notion, a thought that’s unscratchable by our
feeble minds. In an epigraph from Ursula Le Guin’s <em>The Lathe of Heaven</em> she
quotes Zhuang Zhou<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">This quote has come to be known as a bad translation. At the time of
its writing, the lathe had not been invented yet.<br />
<br />
</span></span>:</p>
<blockquote>
<p>To let understanding stop at what cannot be understood is a high attainment.
Those who cannot do it will be destroyed on the lathe of heaven.</p>
</blockquote>
<p>It’s this that makes art feel holy. It’s this that motivates why grappling with
the foundations of math ensnares me in such a way that I can lose time, whole
hours, when I’m given to its chase. It’s this that is the explanation for the
chill I’m feeling as I type this—I’m reaching out to something I cannot see but
am absolutely compelled by. Something I can only picture through a glass,
darkly. The thrill of the pursuance of God.</p>
<blockquote>
<p>Moses said, “Show me your glory, I pray.” And he said, “I will make all my
goodness pass before you, and will proclaim before you the name, ‘The Lord’;
and I will be gracious to whom I will be gracious, and will show mercy on whom
I will show mercy. But,” he said, “<strong>you cannot see my face; for no one shall
see me and live.</strong>”</p>
</blockquote>
<div class="quote-ref">
<p>Exodus 33:18-20 (NRSV)</p>
</div> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/philosophy/index.html" term="philosophy" label="philosophy"/>
    
    <category scheme="https://dpitt.me/tags/god/index.html" term="god" label="god"/>
    
    
  </entry>
  
  <entry>
    <title type="html">The Whale (2022)</title>
    <link href="https://dpitt.me/forest/dsp-0022.html" rel="alternate" type="text/html"/>
    <published>Mon,  9 Jan 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><img src="/images/whale.jpeg" width="400"></p>
<p>In THE FOUNTAIN, Thomas has found eternity, while in <em>mother!</em>, Him
seeks it through art, a means itself to transfinitude; and now, in THE
WHALE, Aronofsky is eschewing eternity altogether. Instead, it’s
transfixed on life’s finitude; Charlie is dead set on making meaning
in the immediate. Despite Protestant Christianity being a plot point,
there are more subtle religious parallels we see with Charlie
devotedly seeking forgiveness from his estranged daughter. Making
amends before death, despite it being common fodder for storytelling,
has its origins in the forgiveness we must beg God for before we come
to the end of our sinful existence. When we use the
amends-before-the-end-of-life framework for a story but we elide the
forgiveness required from God, it underscores the point that Aronofsky
misses altogether in THE FOUNTAIN, and more importantly a point missed
by religions which promise eternal life: the fact that life has a
fixed beginning and end, that our time to be conscious here on Earth
is limited, is what gives us purpose. To make amends at the end of
life is, flat out, the opposite of the lesson, despite it being
precisely the one taught to us in Bible school. To wait until there’s
no time left is to deny oneself, or those others that we may have
wronged, the amends they’re owed now; the longer we wait, the more we
subtract from our purpose, our calling, or if you’ll allow me it, from
our ability to self-actualize. Charlie waited too long, so his plot
for redemption was doomed from the beginning.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/film/index.html" term="film" label="film"/>
    
    <category scheme="https://dpitt.me/tags/review/index.html" term="review" label="review"/>
    
    
  </entry>
  
  <entry>
    <title type="html">The Lathe of Heaven</title>
    <link href="https://dpitt.me/forest/dsp-0021.html" rel="alternate" type="text/html"/>
    <published>Tue,  3 Jan 2023 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>In <em>This Life</em>, philosopher Martin Hagglund makes the argument that
the very fact that the time we have to be conscious here on Earth is
limited is what gives life value, what gives it meaning. If we have
infinite time, then nothing matters. In <em>The Lathe of Heaven</em>, Le Guin
expands on this existential dialectic to explore not infinite time,
but infinite <em>opportunity</em>. When a man is given infinite opportunity,
it has the same effect—he can erase pain, but pleasure dies with it as
well. The meaning we make from life is the synthesis of these
opposites.</p>
<p><img src="/images/lathe.jpeg"></p>
<p>The <em>Lathe of Heaven</em> is the story of a man, George Orr, who has been
blessed with the curse of effectual dreaming. When he dreams, reality
shifts. Under governmental mandate, Orr must see a therapist after
being caught borrowing a neighbor’s scrip card—he’s exceeded his
ration of anti-onierics—drugs he takes because he fears dreaming, a
transitive fear borne from the terror of the responsibility that
manifesting reality entails. His therapist, William Haber, does not
share this fear. Instead, he is morally, despotically, compelled to
put Orr’s gift-curse to work to “fix” reality. Racism, hunger,
overpopulation, malevolent extraterrestrials, all erased. Haber, a
man, acts as God to flatten life’s curves. The book’s title itself
acts as a warning to men like Haber, but also as a presentiment to the
reader—<a href="https://en.wikipedia.org/wiki/The_Lathe_of_Heaven#Title">a misquote from the writings of Chuang
Tzu</a>:</p>
<blockquote>
<p>To let understanding stop at what cannot be understood is a high
attainment. Those who cannot do it will be destroyed on the lathe of
heaven.</p>
</blockquote>
<p>Haber is unable to stop at what cannot be understood, and exercises
the infinite opportunity given to him through Orr. He doesn’t just
leave meaning behind as a casualty, but instead the very fabric of
reality itself. Le Guin takes to making this dialectic literal, and
Orr must race against the erasure of being and stop the encroaching
nothingness.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/book/index.html" term="book" label="book"/>
    
    <category scheme="https://dpitt.me/tags/review/index.html" term="review" label="review"/>
    
    
  </entry>
  
  <entry>
    <title type="html">W-Types, and how?</title>
    <link href="https://dpitt.me/forest/dsp-6I0N.html" rel="alternate" type="text/html"/>
    <published>Tue, 20 Sep 2022 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <!--
```
{-# OPTIONS --without-K #-}

module W where

open import Data.Unit
open import Data.Empty
```
-->
<p>This is lifted from McBride’s <a href="https://mazzo.li/epilogue/index.html%3Fp=324.html"><em>W-types: good news and bad
news</em></a>. See the
link in the footnote to follow along. Probably as I make progress I’ll
update here. But also maybe not.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode agda"><code class="sourceCode agda"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> Bool <span class="ot">:</span> <span class="dt">Set</span> <span class="kw">where</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>  true <span class="ot">:</span> Bool</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>  false <span class="ot">:</span> Bool</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> W <span class="ot">(</span>S <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">(</span>P <span class="ot">:</span> S <span class="ot">→</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">:</span> <span class="dt">Set</span> <span class="kw">where</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>  <span class="ot">_</span>◂<span class="ot">_</span> <span class="ot">:</span> <span class="ot">(</span>s <span class="ot">:</span> S<span class="ot">)</span> <span class="ot">(</span>f <span class="ot">:</span> P s <span class="ot">→</span> W S P<span class="ot">)</span> <span class="ot">→</span> W S P</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>natf <span class="ot">:</span> Bool <span class="ot">→</span> <span class="dt">Set</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>natf true <span class="ot">=</span> ⊤</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a>natf false <span class="ot">=</span> ⊥</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a>ℕ <span class="ot">:</span> <span class="dt">Set</span></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a>ℕ <span class="ot">=</span> W Bool natf</span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a>zero <span class="ot">:</span> ℕ</span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a>zero <span class="ot">=</span> false ◂ <span class="ot">λ</span> <span class="ot">()</span> </span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a>succ <span class="ot">:</span> ℕ <span class="ot">→</span> ℕ</span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a>succ n <span class="ot">=</span> true ◂ <span class="ot">(λ</span> <span class="ot">_</span> <span class="ot">→</span> n<span class="ot">)</span></span></code></pre></div>
<p>Turns out this probably isn’t possible. I didn’t take it as far as
McBride did, but I think his conclusion is relevant here:</p>
<blockquote>
<p>W-types are a powerful conceptual tool, but they’re no basis for an
implementation of recursive datatypes in decidable type
theories. <strong>To encode a first-order structure by a function space is
to throw away canonical choice of representation.</strong> When you care
about what things are (because they show up in types), not just what
they do (when you run them), that price isn’t worth paying.</p>
</blockquote>
<p>(emphasis mine)</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode agda"><code class="sourceCode agda"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ot">_</span>+<span class="ot">_</span> <span class="ot">:</span> ℕ <span class="ot">→</span> ℕ <span class="ot">→</span> ℕ</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="ot">(</span>false ◂ <span class="ot">_)</span> + m <span class="ot">=</span> m</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="ot">(</span>t ◂ f<span class="ot">)</span> + m <span class="ot">=</span> succ <span class="ot">(</span>n + m<span class="ot">)</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>  <span class="kw">where</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>    n <span class="ot">=</span> f <span class="ot">{!!}</span></span></code></pre></div> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Fern's Joy</title>
    <link href="https://dpitt.me/forest/dsp-0020.html" rel="alternate" type="text/html"/>
    <published>Mon, 14 Feb 2022 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>re: plans.</p>
<p>Saturday we went out to hike with friends. We were 30 minutes
late. The trailhead was so busy the lot was full and there was a
Ranger there telling people to move on, that we couldn’t wait for
hikers to come back and then poach their recently vacated spots. So we
moved to Wahclella Falls, one of our favorite hikes, one we’ve done
dozens of times. This time, though, we learned that we’ve reached that
point—the one where hiking along a cliff’s edge with a 40-pound
toddler on my back who’s thrashing back and forth in elation who
we can’t tell stop, isn’t an option anymore. But the sky was clear
and the sun was high and Fern was perfectly happy to employ those
elated thrashes at the crest of the creek. The cold water didn’t
bother her at all and the cause and effect of dropping rocks in the
water was more exciting than the abstract and unreachable waterfall,
100 yards from where the trail ends. Fern made her fun, she manifested
it, like she always does.</p>
<p>I’m constantly learning and relearning things from her, and this
weekend’s lesson is one I often forget. In my experience, the trauma
and pain of loss is most acutely felt in the little things, but
through Fern I’m constantly retaught that the opposite is also
true. Joy isn’t necessarily in the grand experience of a Family Hike
to a Waterfall, it’s found in gently rolling snowmelt, in the sound a
stone makes when it breaks through the surface tension; it’s in the
shock of ice-cold water slipping into your boot. It’s diffuse and
ubiquitous; you just have to make yourself susceptible to notice it.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/fern/index.html" term="fern" label="fern"/>
    
    <category scheme="https://dpitt.me/tags/self/index.html" term="self" label="self"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Thinking about Equality Philosophically</title>
    <link href="https://dpitt.me/forest/dsp-0019.html" rel="alternate" type="text/html"/>
    <published>Sat, 27 Mar 2021 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>I’ve a long-running obsession with the meaning of equality, generally
as it’s used in type theory, and about the divide between <em>intensional</em>
equality and <em>extensional</em> equality. I’ve had discussions about this
with real type theorists on IRC, and armchair theorists like myself,
and I’m consistently left unsatisfied with the outcome. It seems that
everyone has a different way of articulating this difference, and—as
I’ve said this before—it’s been hard, at least in my experience, to
really land on a common understanding. In this post, I’m going to give
some background, then try and articulate my own way of thinking about
this, and how I think it’s related to Kant’s ideas about synthetic and
analytic utterances.</p>
<h2 id="i.-equalitys-divide">I. Equality’s Divide</h2>
<p>As I alluded to above, a lot of my interest in this topic is anchored
in the distinction between intensional equality and extensional
equality. The dropping of that anchor is frankly due to the struggle
I’ve had myself to grasp this distinction which makes it difficult to
make headway when you’re learning about type theory. To start, let’s
try and give some meaningful definitions to these two types of
equality:</p>
<h3 id="intensional-equality">Intensional equality</h3>
<p>An <em>intensional</em> equality is one whose definition ships with the<em>
theory we’re working within. It’s an equality that is a part of our</em>
<em>metatheory</em>.</p>
<p>Let’s, for example, think about working in Agda’s type theory, which
is <em>pretty</em> damn close to vanilla Martin-Löf dependent type theory
with some inconsequential—at least for the topic at
hand—embellishments. In Agda, equality is intensional, and the
metatheory we’re working within when we’re writing Agda is the
language itself. So another way to think about intensionality within
Agda is that equality is <em>syntactic</em>. When we, for instance, prove
something like <code>x + y ≡ y + x</code> in Agda, we’re tasked with showing the
type checker that the normal forms of the left-hand side of <span class="math inline">\(\equiv\)</span>
is syntactically identical to the right-hand side. The thing to keep
in mind that we <em>don’t</em> have to do, is to illustrate that this is true
for all x’s and y’s by listing them, one by one, doing the two
additions with every possible combination of <code>x</code> and <code>y</code>, and showing
they both have the same results. If we’re using the natural numbers as
the type for <code>x</code> and <code>y</code>, this task would take an infinite span of
time. Instead, we get to use induction to show that, through
reduction, the syntax on the left side <strong>is the same</strong> as the syntax
on the right side. This is because <code>≡</code>’s meaning is <em>intrinsic</em> to the
theory of Agda. When we use Agda what we’re using is the syntax of the
language, and that syntax comes with the notion of sameness, rather
simply in that two things are the same if they’re literally the same
normalized string of characters<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">This is a bit of an over simplification w/r/t what is
essentially an implementation detail. In practice Agda isn’t literally
comparing strings of characters, but a data structure which can be
shown to a human in Agda’s syntax.<br />
<br />
</span></span>.</p>
<h2 id="extensional-equality">Extensional Equality</h2>
<p>With an extensional equality, the equality isn’t a part of theory, but
instead verifies the sameness of the objects created within the
theory. Take for example extensional set theory. The way to determine
that sets are equal is by determining that all of their elements are
equal. It requires enumerating all the constituent pieces and testing
them one by one, also known as “point-wise”.</p>
<h2 id="synthetic-analytic-intensional-extensional">Synthetic, Analytic, Intensional, Extensional</h2>
<p>Western Philosophy’s most famous lover of rules is, far and away,
Immanuel Kant.</p>
<p>Kant is the originator of the synthetic and analytic
divide. Essentially, an analytic sentence or expression is inherently
verifiable, e.g., “all pediatricians are doctors”, while a synthetic
expression requires exterior verification, like “all pediatricians are
rich”, to verify this statement, we’d have to talk with all
pediatricians, while with the former we know it to be true just by the
meaning of the words.</p>
<p>This is, I’d argue, very closely related to the notion of intensional
vs. extensional equality, if not just a mathematician’s way of saying
the same thing that Kant was trying to say. The real question lies,
though, in what system would you use to verify this equational theory?</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/philosophy/index.html" term="philosophy" label="philosophy"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Propositional Resizing</title>
    <link href="https://dpitt.me/forest/dsp-9M7X.html" rel="alternate" type="text/html"/>
    <published>Fri, 19 Feb 2021 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>HoTT book, 3.5 and <a href="https://arxiv.org/abs/2102.08812">Predicative Aspects of Order Theory in Univalent
Foundations</a>. Effectively a way to
recover impredicativity from a predicative theory by making the
following map an equivalence.</p>
<p><span class="math display">\[
\textsf{Prop}_{\mathcal{U}_i} \rightarrow \textsf{Prop}_{\mathcal{U}_{i + 1}}
\]</span></p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/type theory/index.html" term="type theory" label="type theory"/>
    
    <category scheme="https://dpitt.me/tags/hott/index.html" term="hott" label="hott"/>
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Petri Nets, Graphs, and Concurrency</title>
    <link href="https://dpitt.me/forest/dsp-6K8G.html" rel="alternate" type="text/html"/>
    <published>Wed,  3 Feb 2021 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>This is borne from a conversation on the CT Zulip about graphs,
process calculi, and, eventually, Petri nets. It started
<a href="https://categorytheory.zulipchat.com/#narrow/stream/235484-theory.3A-concurrency/topic/proarrow.20equipments.20in.20concurrency">here</a>,
but that lead to
<a href="https://categorytheory.zulipchat.com/#narrow/stream/235484-theory.3A-concurrency/topic/Nets.20Within.20Nets.20From.20Grothendieck">here</a>,
which is where I’ll start.</p>
<h1 id="petri-nets">Petri Nets</h1>
<p>A graphical way to represent states, distributed systems, &amp;c. They’re
bipartite, consisting of /places/ and /transitions/. Places are
represented as circles while transitions are rectangles. There’s a
third part to these graphs, called /arcs/ that are the edges
connecting places and transitions, or vice versa.</p>
<ul>
<li>tokens :: marks in a place that represent a “current state” of that place.</li>
<li>markings :: describes or captures the state of all the places w/r/t
to their tokens in a given instance of a net.</li>
</ul>
<h2 id="grothendieck-contruction">Grothendieck Contruction</h2>
<p>The idea is that you’ve got a <span class="math inline">\(\textbf{Cat}\)</span> valued functor, <span class="math inline">\(F\)</span> for
some category <span class="math inline">\(\mathcal{C}\)</span>: <span class="math inline">\(F: \mathcal{C} \rightarrow \textbf{Cat}\)</span>
which you use to create a new category whose objects are pairs <span class="math inline">\((c,
x)\)</span> where <span class="math inline">\(c \in \mathcal{C}\)</span> and <span class="math inline">\(x\)</span> is in <span class="math inline">\(\textbf{Cat}\)</span> via <span class="math inline">\(F(c)\)</span>
and morphisms are pairs which work the same way, completing the
square. This is why it’s called gluing, because it’s the same idea.</p>
<p><span class="math display">\[
\xymatrix{
  F(c) \ar[r]^{F(f)} &amp; F(c&#39;) \\
  c \ar[r]_f \ar[u]^F &amp; c&#39; \ar[u]_F\\
}
\]</span></p>
<p>Type theoretically, this is a sigma: <span class="math inline">\(\text{Groth}: \Sigma_{(c :
\mathcal{C})}F(c)\)</span> where <span class="math inline">\(\Pi_{(c : \mathcal{C})}F(c)\)</span> (of
course). The inhabitants of <span class="math inline">\(\text{Groth}\)</span> are the usual dependent
pairs: <span class="math inline">\((x, F(x))\)</span>.</p>
<p><del>An intuition that occurred to me while reading the blog post above:+
The purpose of the grothendieck contstruction is to create categories
from arbitrary objects from some other category.</del></p>
<p>It’s actually that it glues things together into one big category,
<span class="math inline">\(\textbf{Cat}\)</span>, because…, as seen above, <span class="math inline">\(F(c)\)</span> and <span class="math inline">\(F(c&#39;)\)</span> aren’t
necessarily in the same category, until they are, in <span class="math inline">\(\textbf{Cat}\)</span>.</p>
<p>Or maybe they are. The Petri net construction in Jade Masters post
below seems to rely on that.</p>
<h1 id="nets-within-nets-within">Nets within Nets within…</h1>
<p>A <a href="https://jadeedenstarmaster.wordpress.com/2021/01/29/nets-within-nets-from-the-grothendieck-construction/">Jade Master blog
post</a>
to cover hierarchical nets where a net’s tokens are themselves marked
nets. This arose after a discussion about representing the two types
of graphs that one may choose to model concurrent actors. Namely, the
timeline view, illustrating interacting actors, and the transition
view, that illustrates the causal relation from event to event.</p>
<p>Masters gives a construction for an /object system/, which consists of
a hierarchy of nets where each place in a global net is its own local
net, made possible through category constructed through Grothendieck
which she calls <span class="math inline">\(\textbf{MkPetri}\)</span> whose objects are pairs of nets and
their firing sequences and morphisms are morphisms between those nets
and their corresponding sequences. A functor <span class="math inline">\(E\)</span> is then given to take
an arbitrary net category, <span class="math inline">\(F P\)</span>, and bring it into $,
effectively flattening the places in <span class="math inline">\(P\)</span>. Something has to retain the
relation for the nested nets and their parents, the intuition I could
come up with was that <span class="math inline">\(E\)</span> acts as a rewriter, rewriting the places in
the parent net with the child net.</p>
<h1 id="the-structure-of-concurrent-process-histories">The Structure of Concurrent Process Histories</h1> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Coherence</title>
    <link href="https://dpitt.me/forest/dsp-2Q9P.html" rel="alternate" type="text/html"/>
    <published>Wed, 11 Nov 2020 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <h1 id="origin">Origin</h1>
<p>HoTT Zulip: <a href="https://hott.zulipchat.com/#narrow/stream/228519-general/topic/terminology.3A.20.22wild.22.20vs.20.22incoherent.22.20vs.20.22H.22%5D">terminology
discussion</a>.</p>
<h1 id="references">References</h1>
<h2 id="wikipedia">Wikipedia</h2>
<blockquote>
<p>[A] coherence condition is a collection of conditions requiring that
various compositions of elementary morphisms are equal.</p>
</blockquote>
<p>Wikipedia has a hilariously bad example that makes no sense, but then
they give a diagram that… actually does? Recapitulating it:</p>
<p><span class="math display">\[
\xymatrix{
    ((A \otimes B) \otimes C) \otimes D \ar[r] \ar[d] &amp;
    (A \otimes (B \otimes C)) \otimes D \ar[r] &amp;
    A \otimes ((B \otimes C) \otimes D) \ar[d] \\
    (A \otimes B) \otimes (C \otimes D) \ar[rr] &amp;&amp;
    A \otimes (B \otimes (C \otimes D))
}
\]</span></p>
<p>Where the point is that if you have this diagram, then you /have
coherence/—you don’t need to prove it <span class="math inline">\(\forall c \in C\)</span>.</p>
<h2 id="nlab">nLab</h2>
<blockquote>
<p>While associativity and uniticity of composition of k-morphisms
holds only up to choices of higher morphisms, coherence is the
demand that the collection of these choices forms a contractible
-groupoid.</p>
</blockquote>
<p>jfc</p>
<p>A little more than half-way down the page and we’re talking about
Trimble <span class="math inline">\(\omega\)</span> categories and yeah, I’m out.</p>
<h1 id="intuition">Intuition</h1>
<h2 id="induction">Induction</h2>
<p>What is coherence’s relationship to induction? Going back to diagram
given in the Wikipedia section of this document and I’m thinking…
could there be a way to express that diagram inductively? A type with
four parameters where each constructor is a different associative
configuration and then a proof that <span class="math inline">\(\forall (m\ n : \text{MCoherence})
\rightarrow m \equiv n\)</span>?</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Internal ∞-Categorical Models of Dependent Type Theory</title>
    <link href="https://dpitt.me/forest/dsp-7Z0O.html" rel="alternate" type="text/html"/>
    <published>Mon, 14 Sep 2020 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <h1 id="introduction">Introduction</h1>
<ul>
<li>Semicategory :: All the category things mod identities (like a
semigroup).</li>
</ul>
<p><strong>Other models of type theory:</strong>
- Setoid (Palmgren 2019)
- Parametric (Bernardy 2015)
- Groupoids (Hofmann &amp; Striecher 1996)
- Sozeau and Tabareau implementation (2014)</p>
<h2 id="initiality">Initiality</h2>
<ul>
<li><p>Syntactic <span class="math inline">\(\equiv\)</span> term</p></li>
<li><p>CwF refresher: A category <span class="math inline">\(\mathcal{C}\)</span> with a presheaf of families
where the presheaf is split into two “special” functors: <span class="math inline">\(Ty\)</span> and
<span class="math inline">\(Tm\)</span>. <span class="math inline">\(\mathcal{C}\)</span> is the category of contexts and substitutions
where the terminal object is the empty context.</p>
<ul>
<li>Terminal object :: That’s the thing that everyone can reach,
uniquely (up to blah blah blah).
<ul>
<li>The singleton set in <span class="math inline">\(\mathbf{Set}\)</span>.</li>
</ul></li>
<li><strong>NOTE</strong>: What’s it mean in terms of relationships between a
category with initial and terminal objects and order theory?</li>
</ul></li>
<li><p>A Generalized Algebraic Theory (GAT) is apparently where /sorts/
come from. The PTS work is creating a GAT out of type theories?</p></li>
<li><p>What it means to have an “initial” model.</p>
<blockquote>
<p>We have a morphism of models from the initial model to any other
model, and in particular to the standard model. This means that a
universe in the host theory is a model of type theory itself.</p>
</blockquote></li>
</ul>
<h3 id="the-initial-model-is-syntax">The initial model is syntax</h3>
<p>They have this rule:</p>
<p><span class="math display">\[
(A B : Ty\ \Gamma) \rightarrow (A = B) + \neg(A = B)
\]</span></p>
<p>Which is to say, it’s decidable because it’s either true or its
not. Since this decidable equality is in the host theory, it’s
intensional! It’s a part of the metatheory, and is therefore
syntactic.</p>
<p>We should, when doing proofs in type theory, think of intensional
equality as /syntactic/.</p>
<p><strong>NOTE</strong>: This really cements the intuition from set theory of what
exactly extensional equality means in a type theory. It also gives way
to the obvious extensional meaning of funext.</p>
<h2 id="uniqueness-of-identity-proofs-uip">Uniqueness of Identity Proofs (UIP)</h2>
<ul>
<li>HoTT rejects UIP (think about it, because univalence turns
not-so-unique equalities into equivalences).</li>
</ul> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    <category scheme="https://dpitt.me/tags/type theory/index.html" term="type theory" label="type theory"/>
    
    <category scheme="https://dpitt.me/tags/paper/index.html" term="paper" label="paper"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Worship</title>
    <link href="https://dpitt.me/forest/dsp-0018.html" rel="alternate" type="text/html"/>
    <published>Fri, 28 Aug 2020 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><img src="/images/jmm.jpeg" width="300"></p>
<p>In the spring of 2013, Meg and I started attending a small, young
church in our neighborhood in East Dallas. We’d been proper parents
for a little over a year—Olivia had been earth-side since February
2012—and we were on the prowl for fellow new parents. It’s a strange
time in a person’s life, that first year as a parent. You’re
constantly reädjusting while desperately seeking consistency. When
they say that becoming a parent completely uproots your existence and
identity, they don’t mean that it uproots them and then re-plants them
somewhere else. No, your identity is now in the bed of a truck, then
maybe a train, eventually landing somewhere to be replanted long
enough to take hold, to be strong enough to stand on its own, only to
be uprooted again and then resold to someone pushing an orange cart at
a Home Depot who plans to put you in their own garden, call you
theirs, and not give a fuck whether you used to play the drums in a
band.</p>
<p>This church seemed to have exactly what we were looking for. In fact,
the music pastor and his wife we’d observed, or at least I had, and
they seemed like cool folks, which is of course to say that they
seemed like us. When I learned that they were the welcoming committee,
I almost engineered a meeting, saying we Were New Here with the
intention to reluctantly mention that I was a drummer. They took us to
dinner, and, as I recall, the evening went exactly as planned. Within
a few weeks, I was playing drums in the band on Sunday mornings. I can
remember the rehearsal for the first Sunday I played. There was a song
with a front-of-the-overalls sized pocket and I was buried in it like
week-old lint. He told me, “yeah, so, there’s ‘in the pocket’, and
then there’s ‘<em>in the pocket</em>’. I think… you’re dragging.” He was right.</p>
<p>Come April, we’d begun to plan a big Easter service. When we started
at the church, it met in a public elementary school’s auditorium. The
school was a mid-century relic nestled behind 100-year-old oak trees,
and the sidewalks had cracked to the trees’ whims. The auditorium was
ninety-eight per cent hardwood—the floor, the stage, the folding
theater chairs. The other two per cent was velvet and iron,
respectively hiding the unkempt borders of the stage and creakily
folding the chairs, some of whom had seen their last fold. Only a few
weeks before Easter, as rigid as the velvet and iron they chartered,
the school district chose not to renew the church’s lease; we needed
to find somewhere else to worship. We soon found ourselves using a
different school’s auditorium—a private school’s. Its auditorium was
all glass and steel and linoleum. It bordered a huge public park, one
that had the tendency to flood during those Texas prairie
thunderstorms.</p>
<p>Easter is the most important holiday to a Christian. On Easter, Christ
is believed to be risen from the dead, to have <em>defeated</em> death, to
have paid for our sins and paved the way for our salvation. The church
planned to rent out the bordering park and have its Easter service
there with the unspoken goal that the message would be loud enough,
literally, to be heard by the whole East Side. A stage was built. It
was one of those rubber and steel contraptions that look like someone
had disassembled a 10-story crane and re-fashioned it, tires and all,
into a stage. We transported a sound board and its serpentine
companion outside. I arrived on the scene at 7:15—the service was at
10:00—and began to unload my drums from the back of my Honda
hatch-back.</p>
<p>The place was bustling. It seemed like the whole church was already
there, their hands busy in service, repeatedly turning to those whose
lives themselves were objects of service, and saying, “what next?” The
sun had begun to peek through a layer of Spring clouds like it was
searing a hole in them in service to God who was determined to see
what we were up to. The grass at the basin of the park glistened with
dew, turning it into a field of interspersed gold and green. It was
like King Midas, turning what it could reach into precious metal. I
nervously opened one drum case at time, starting with the hardware as
I’d done hundreds of times before. When I was asked, “how can I help?”
all I could think to say was, “where should I park my car?” This
resulted in my car being moved for me, and for my keys to no longer be
on my person. Once everything was racked and secured, once I’d made
all the micro-adjustments that are necessary, I set a pair of
sticks on my bass drum, a pair on my snare, and joined the “what
next?” gestalt. I was assigned a broom and I swept the cement floor of
a covered picnic area.</p>
<p>A few minutes later, someone came to find me. It had begun to sprinkle
a little, and they wanted to know, did I need to do anything to
protect my drums. I followed them back out to the stage and we took my
drums, a piece at a time, under the cover of the picnic area I’d just
swept. Then, in an instant, thunder cracked the sky open and that
dappling sunlight was sucked in. A thunderstorm rolled over the park
and darkened the sun-kissed grass, removing its luster like time in
reverse, turning diamonds back to coal. Not long after, the bottom
dropped out of the storm, and sheets of water were falling from the
sky. Time moved in reverse for us too: set-up became tear-down—all of
the gear and the chairs and the food and the bibles were going back
into trucks and boxes. The congregation moved as one, in perfect,
ineffable synchrony, to transport our entire service back inside the
school. Maybe Pastor Jeff made the call, but I didn’t hear it. It was
as if all of their minds were changed in a second, like bees in a
hive, and everyone knew just what to do next.</p>
<p>By 9:45 we had a bare setup on stage inside the school. We’d
sound-checked, but I don’t remember having time to do a run-through of
the set. Normally, my drums would be rear stage right and behind
glass, paneled to tame the sound so that it might be controlled by
electricity rather than left to the unpredictability of the room’s
acoustics and the drummer’s inconsistency. This time, though, I was
dead-center, no shield, and dripping wet. We all were. The whole
church was there, soaked to the bone in their Easter pastels and
humming with an electricity before a single note was played or a
single word said.</p>
<hr />
<p>Worship music in the modern church, particularly those that call
themselves non-denominational or “bible” churches, is a fascinating
capitalist machine. The songs are, in effect, ballads to God, and
often times are not performed, or at least made famous by their
authors. Instead, there is an elite group of worship leaders who play
the songs written by other lesser-known artists. What follows is that,
due to the reach—which one could also interpret as power—that this
elite group has, these songs end up trickling all the way back down to
individual churches and their worship pastors in the impossible
geometric configuration of an unclosed circle. The songs, as they’re
on their journey, receive a number of small changes here and there;
they’re perfected in their manipulative power and scrubbed clean of
any ambiguity, because ambiguity is the fodder of doubt.</p>
<p>One such song that’s received this treatment is the song <em>How He
Loves</em> by John Mark McMillan. McMillan is a Christian song-writer, but
until well after <em>How He Loves</em> was released, he operated entirely
independently, entirely outside the Christian Music Industrial
Complex. <em>How He Loves</em> is a heartbreaking song that McMillan wrote
following the death of a close friend. It paints a picture of the
indescribable power of God’s love:</p>
<div class="poem">
<p>When all of a sudden
I am unaware of these afflictions, eclipsed by glory
And I realize just how beautiful You are
And how great your affections are for me</p>
</div>
<p>The chorus then goes on to repeat, “Whoa, how he loves us so”, first
at a lower register, then later it’s belted an octave up, movingly so.</p>
<p>In the second verse, McMillan sounds like he’s talking himself into
his faith, like so many of us have following a tragedy that stands in
the way of the acceptance of His benevolence.</p>
<div class="poem">
<p>So heaven meets Earth like a sloppy wet kiss
And my heart turns violently inside of my chest
I don’t have time to maintain these regrets
When I think about the way
He loves us</p>
</div>
<p>It’s not that tragedy makes you doubt God’s benevolence, it’s that it
makes you doubt that he exists at all. God, we’re taught, is
omniscient. To me, the logic that follows from an omniscient God leads
to the inevitable conclusion that all events are predetermined. In
fact, this is how the Calvinists saw the world. However, on the other
hand, when face to face with devastation, it is at the same time
impossible to see the world as anything other than cold, impartial,
and entropic. These two notions are in contradiction with one another,
and when faced with a contradiction, the only path forward is to
accept that one of the two contradictory propositions must be
false. For me, it was that the ontological proposition of an
omniscient God must be false. In the face of tragedy, the conclusions
I drew from my reality were too overwhelming to retain my faith. I
couldn’t hate God, but I also couldn’t accept him.</p>
<p>Near the end of the song, barely able to finish the lines, McMillan
mentions his friend by name.</p>
<div class="poem">
<p>Oh, I thought about You the day Stephen died
And You met me between my breaking
I know that I still love You, God
Despite the agony</p>
</div>
<p>It’s these two verses that are so powerful and so rich with emotion
that make <em>How He Loves</em> what it is. To hear McMillan audibly cry and
to cry out to God is to be him, to be anyone who’s suffered
catastrophic loss. It is to feel the lines “I know that I still love
you God / despite the agony” and to feel their implication, that he,
at some point, asked himself the question, “Can I still love the God
who lets this tragic thing happen?” The lines that follow contain his
answer.</p>
<div class="poem">
<p>Listen, people, they want to tell me You’re cruel
But if Stephen could sing
He’d say it’s not true
’Cause He loves us</p>
</div>
<p>The record on which How He Loves appears was released independently
in 2005. It spent no time on any charts, Christian music or otherwise,
and, aside from How He Loves, is frankly unremarkable. Then, an
insider—David Crowder and his David Crowder Band—recorded a cover of
<em>How He Loves</em> for their 2009 release <em>Church Music</em>. <em>Church Music</em>
peaked on the US Contemporary Christian Music charts at number
eleven. In Crowder’s version, the last verse, the one claiming victory
over doubt, is cut altogether. The line “So heaven meets Earth like a
sloppy wet kiss” became “So heaven meets Earth like an unforeseen
kiss” because the church cannot seem to untangle sensuousness from
prurience. The frayed edges of McMillan’s broken heart had been
selvedged away into marketable and nonthreatening copy. I want to know
McMillan’s God, but I was brought up on Crowder’s.</p>
<hr />
<p>That Sunday, we played <em>How He Loves</em>. We played it as it was meant to
be played. It was the last song in the set, and the congregation was
already prostrate at God’s feet on account of the morning’s events,
and How He Loves broke their hearts. When people talk about having a
religious experience, this is what they mean. It’s lost time, a memory
of but not during.</p>
<p>I don’t remember playing the song. There’s a 7-minute gap in my memory
of that Sunday—I can remember the song being over and the silence that
fell over the room. Some were still swaying, some still had their
hands in the air. I could see my ride cymbal was still vibrating, but
I couldn’t hear anything. At that moment, we were all one; reposed in
worship<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">1 Corinthians 12:24-27<br />
<br />
</span></span>:</p>
<blockquote>
<p>But God has so composed the body, giving greater honor to the part
that lacked it, that there may be no division in the body, but that
the members may have the same care for one another. If one member
suffers, all suffer together; if one member is honored, all rejoice
together. Now you are the body of Christ and individually members of
it.</p>
</blockquote>
<p>I don’t remember coming down off the stage, I don’t remember what Jeff
said that day, I don’t remember talking to anyone after the
service. What I’ll never forget, though, is what that minute after the
song ended felt like. That ineffable synchrony. And I’m thinking now,
more than 7 years later, maybe that’s a manifestation of God that I’ve
been missing all along.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/god/index.html" term="god" label="god"/>
    
    <category scheme="https://dpitt.me/tags/music/index.html" term="music" label="music"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Vibin'</title>
    <link href="https://dpitt.me/forest/dsp-0017.html" rel="alternate" type="text/html"/>
    <published>Fri, 31 Jul 2020 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>In the summer of 2017 A friend sent me a job posting from The Orange
Website and said that it looked like a thing I’d be interested in. It
was about 9 p.m. on a Wednesday, and being that you can find me on
just about any Wednesday at 9 p.m. sitting here, at the bar in my
kitchen, typing on this computer, with a beer or two in my belly, I
took it upon myself to whip up a CV and apply.</p>
<p>Thanks to those beers, my CV was snarky, even silly in some places. It
landed in the lap of a person with exactly the right disposition for
this levity, and in a matter of a few months I was writing a
valedictions email to soon-to-be ex-coworkers and starting a
completely new adventure.</p>
<p>At this job, I spent my days reading, writing, and researching ways to
integrate software verification techniques, particularly dependent
type theories, into the practice of writing programs in a language
whose ontology was to <em>run</em>, not to <em>verify</em>. It was the closest I
ever got to research-as-work—to the experience of a PhD recipient.</p>
<p>While I was there, I met an incredible group of people, some of whom
have come to be amongst my closest friends. Outside of work, we’d
quibble about literature or philosophy, and they’d humor me when I
wanted to soliloquy about profunctors.</p>
<p>Like all good things, this had to come to an end. We were unexpectedly
sent our separate ways, and I lost touch with this new
research-as-work identity-appendage I’d grown. The next job left it to
atrophy and I’ve all but forgotten what the beautiful experience of
connecting neurons for money felt like. Until this week.</p>
<p>This week, all at once, a friend told me about he and his partner
talking with one another about when I was finally going to get a PhD,
which prompted me to reëxplore the work of Peli Grietzer, a
mathematician and philosopher—literally mathematized philosophy and
literary criticism—who’s responsible for the theme of this month’s
newsletter. Simultaneously, I read a chapter of the home-schooling
book <em>The Call of the Wild and Free</em> about cultivating a family
culture, and it hit me. It leveled me like a falling tree. I’m
lost. I’ve lost touch with my own vibe.</p>
<hr />
<p>Peli Grietzer’s The <a href="https://www.glass-bead.org/article/a-theory-of-vibe/"><em>Theory of
Vibe</em></a> equates
the semiotic<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">Semiotics is the study of signs and symbols. We talk
about semiotics here because what’s fed into the autoencoder
signifies something, for instance a handwritten word that
signifies an object like “tree”, and what comes out signifies that
originally input signifier.<br />
<br />
</span></span> system derived from a type of AI called an autoencoder to
the semiotics of a universe created in a work of literature:</p>
<blockquote>
<p>Suppose that when a person grasps a style or vibe in a set of worldly
phenomena, part of what she grasps can be compared to the formulae of
an autoencoder trained on this collection.</p>
</blockquote>
<p>An autoencoder is a part of a neural network that reduces the patterns
in its training set to a discretized and necessarily less precise
representation and then back again with the goal of there being zero
errors on the “back again” side. Those from that training set that it
can roundtrip without error are called its canon.</p>
<p>Grietzer wants to make the connection that this “canon” of an
autoencoder—which is the same thing as talking about the autoencoder
itself—is the same idea that one gets from an aesthetically consistent
universe created in a work of literature. He calls this a <em>vibe</em>.</p>
<p>That is, a <em>vibe</em> is the set of things the reader can recognize,
without error, as aesthetically consistent.</p>
<p>This, to me—both the autoencoder and the literary vibe—sound related
to Deleuze and Guattari’s smooth and striated spaces dichotomy. D&amp;G
wrote about this dichotomy in A Thousand Plateaus, the second volume
of their Capitalism and Schizophrenia series. To them, smooth spaces
are heterogeneous and continuous, and also untainted by a State
Apparatus; while a striated space is homogeneous and discretized, and
that discretization is, often times, done by the state. As an example,
consider the idea of property “ownership”. Land itself is continuous,
but a State Apparatus parcels it out and identifies it. Hell, it even
homogenizes it with zoning laws.</p>
<p>I’d like to apply this notion of smooth and striated to both
autoencoders and vibes. For an autoencoder, mapping smoothness and
striation onto its two ends is obvious: an autoencoder’s canon is the
set of “smooth” objects it encodes into a “striated” representation
and then decodes without loss. However, the “vibe” of a work of
art—particularly a work of literature—is more subtle.</p>
<p>Earlier I mentioned that an autoencoder is, mathematically speaking,
the same thing as its canon. Grietzer covers this point early in <em>The
Theory of Vibe</em>:</p>
<blockquote>
<p>A trained autoencoder and its canon are effectively mathematically
equivalent: not only are they roughly logically equivalent, it is also
fast and easy to compute one from the other.</p>
</blockquote>
<p>So let’s look at the autoencoder as a function, one that takes
representations of smooth spaces and in return outputs striated
ones. In this light, what is the vehicle for turning an artist’s brain
into a vibe?</p>
<p><em>Language</em>.</p>
<p>Language is the function that translates the smooth space of
unarticulated thought into an essence that an independent being—the
reader—can interpret. That essence is <em>meaning</em>. I’d posit that
Grietzer’s “vibe”, that canon of an artist-recognizable sort, is the
lexicon that that artist employs to articulate their thoughts.</p>
<p>Mathematically, we’d think of this as, rather than a function’s
codomain or range, its <em>image</em>—the specific subset of possible results
of a function, as opposed to the class of objects that those results
lie in. When we detect that vibe it’s because we’re familiar with,
well, that artist’s canon.</p>
<p>The space (this is a math pun) we’ve reached is rich with study, this
idea of roundtripping from higher to lower and back to higher
“resolutions” or dimensionality has many names: section / retraction,
Galois connection, but the most general notion is a categorical idea
called adjunction. An autoencoder’s input would be right adjoint to
its output. And if that’s the case, then by Grietzer’s own line of
thinking, a vibe would be left adjoint to the author’s mind. We’re
getting into deeply hand-wavy territory now, but it’s not entirely
untrodden. Category theory has been used in linguistics for years now,
most interestingly (to me) in distributional semantics. Distributional
semantics’ raison lies in using the distribution of a word amongst
others to determine its meaning or synonyms. Words that are often seen
in similar positions are thought to have similar meanings<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">It’s more subtle than this and it involves not just
position but also predicates that can make distinctions in the
face of polysemy.<br />
<br />
</span></span>. Maybe a
distributional model over an artist’s canon could suss out that
artist’s vibe? Maybe there’s meaning to be given to adjunction in the
categorical model of distributional semantics?</p>
<p>Maybe it’s actually that a vibe, as relayed through the striated space
of language, is right adjoint to language itself, capturing the
author’s mind at a point and relayed to you, reader, in a form that
you can internalize into your own smooth space, only to map it again
to your own vibe when you relay it to a friend, and so on and so on,
always and forever, composed eternally.</p>
<p>At least until you forget.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    <category scheme="https://dpitt.me/tags/philosophy/index.html" term="philosophy" label="philosophy"/>
    
    
  </entry>
  
  <entry>
    <title type="html">The Real Numbers, Propositionally</title>
    <link href="https://dpitt.me/forest/dsp-0016.html" rel="alternate" type="text/html"/>
    <published>Wed,  1 Jul 2020 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p><strong>NOTE: If you come across this please know that it is a WIP. There is
stuff in here that is <em>wrong</em> because I was still learning it.</strong></p>
<h1 id="part-i-foundations">Part I: Foundations</h1>
<h2 id="introduction">Introduction</h2>
<p>If some framework for reasoning about mathematical objects posits that
it exists as a <em>foundation</em> of mathematics, what good would it be if
something as critical as the real numbers couldn’t be reasoned about,
much less even constructed, in that framework? When Homotopy Type
Theory states that it is such a framework, we must measure such a
postulation against the same requirements for any other foundations of
mathematics. For HoTT to be “foundational” we’re going to need to be
able to reason about the real numbers <em>through</em> it.</p>
<p>The goal of this document is to first build up an intuition for the
requisite pieces of understanding the reals in HoTT. We begin by
giving a whirlwind introduction to Homotopy Type Theory and the
mathematical toolbox it provides us. Next, we’ll explore the
mathematical structure we need to construct the real numbers, namely
Cauchy Sequences. And finally, I’ll elucidate how we can use the tools
provided to us by HoTT to construct, and then reason about the reals.</p>
<h2 id="homotopy-type-theory">Homotopy Type Theory</h2>
<p>Homotopy Type Theory is many things, and its existence has many
elegant applications, but for the purpose at hand, I’m going to cover
just the parts which we need to begin to think about the real
numbers. To begin, why <em>Homotopy</em> Type Theory?</p>
<h3 id="homotopies">2.1 Homotopies</h3>
<p>In topology, a <em>homotopy</em> is a mathematical object which <em>bends</em> one
function into another. More formally:</p>
<p>Given two functions which map between spaces <span class="math inline">\(X\)</span> and <span class="math inline">\(Y\)</span>:</p>
<p><span class="math display">\[
f,\ g : X \rightarrow Y
\]</span></p>
<p>A homotopy <span class="math inline">\(H\)</span> is a function<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">Where <span class="math inline">\([0, 1]\)</span> denotes the <em>real interval</em>, the continuous
and transfinite sequence of the real numbers between <span class="math inline">\(0\)</span> and
<span class="math inline">\(1\)</span>.<br />
<br />
</span></span>:</p>
<p><span class="math display">\[
H : [0, 1] \times X \rightarrow Y
\]</span></p>
<p>Such that</p>
<p><span class="math display">\[
H(0, X) = f
\]</span></p>
<p>And</p>
<p><span class="math display">\[
H(1, X) = g
\]</span></p>
<p>The intuition here is that, in a sense, our homotopy <span class="math inline">\(H\)</span> is a
functions <em>between functions</em>.</p>
<p>We can take this notion a step further too, and use a homotopy as an
equivalence between two topological spaces:</p>
<p>Given two functions:</p>
<p><span class="math display">\[
\begin{align*}
&amp;f : X \rightarrow Y \\
&amp;g : Y \rightarrow X
\end{align*}
\]</span></p>
<p>We can use our homotopy to say, “if the composition of <span class="math inline">\(f\)</span> and <span class="math inline">\(g\)</span>
is homotopic to the function <span class="math inline">\(Id_x\)</span>, then the spaces <span class="math inline">\(X\)</span> and <span class="math inline">\(Y\)</span>
are homotopy equivalent.”</p>
<p><span class="math display">\[
\begin{align*}
H(0, X) &amp;= Id_x \\
H(1, X) &amp;= g \circ f
\end{align*}
\]</span></p>
<p>In topology, it is reasonable to say that a homotopy equivalence is an
<em>isomorphism</em>. That is, an identity preserving map between our two
spaces <span class="math inline">\(X\)</span> and <span class="math inline">\(Y\)</span>.</p>
<p>There are two take-aways that I want to make sure we don’t abscond
from this section without taking them with us.</p>
<ol type="1">
<li><strong>A homotopy can, more generally, be thought of as a function
<em>between</em> functions.</strong></li>
<li><strong>A homotopy can be seen as an isomorphism in the mathematical
field of topology, because it is <em>identity preserving</em>.</strong></li>
</ol>
<p>Now, this notion of functions, their inverses, and functions between
functions can be generalized in category theory in an object called a
<em>groupoid</em>.</p>
<h2 id="groupoids">Groupoids</h2>
<p>In the land of category theory, a groupoid is an object in which the
usual axioms hold—namely composition, the associativity of
composition, an identity morphism, &amp;c. However, in a groupoid, all
morphisms also have an inverse:</p>
<p><span class="math display">\[
\xymatrix{
  A \ar[r]_f &amp; B \ar@{.&gt;}@/_1pc/[l]_{f^{-1}} \ar[d]_g \\
  &amp; C \ar@{.&gt;}@/_1pc/[u]_{g^{-1}}
}
\]</span></p>
<p>In the same way we think of homotopy equivalence as an isomorphism in
topology, we can also think of these morphisms and their inverses as
isomorphisms. Given the diagram above, the composition:</p>
<p><span class="math display">\[
f^{-1} \circ f \simeq Id_A
\]</span></p>
<p>is identity preserving. Through this lens, we can start to think as a
groupoid as a category whose morphisms are equivalences.</p>
<h3 id="infty-groupoids"><span class="math inline">\(\infty\)</span>-Groupoids</h3>
<p>Like our homotopy was a function between functions, an
<span class="math inline">\(\infty\)</span>-groupoid is governed by the precept that not only can we
have morphisms between objects, but also we can have morphisms between
morphisms, and morphisms between morphisms between morphisms, ad
infinitum. However, another way to think of this, given what we’ve
stated before about the composition of a morphism and its inverse as
an isomorphism, this would correspond to stating isomorphisms of
isomorphisms.</p>
<p>For example:</p>
<p>If we have the following diagram:</p>
<p><span class="math display">\[
\xymatrix{
  A \ar@/^/[r]^f \ar@/_/[r]_g &amp; B \ar@{.&gt;}@/_2pc/[l]_{f^{-1}} \ar@{.&gt;}@/^2pc/[l]^{g^{-1}}
}
\]</span></p>
<p>Another way we could state this is like so:</p>
<p><span class="math display">\[
\begin{align*}
p &amp;: A \simeq B \\
p &amp;= f^{-1} \circ f \\ \\
\end{align*}
\]</span></p>
<p>and</p>
<p><span class="math display">\[
\begin{align*}
q &amp;: A \simeq B \\
q &amp;= g^{-1} \circ g
\end{align*}
\]</span></p>
<p>Now, with our higher groupoid structure, we can say things like:</p>
<p><span class="math display">\[
p \simeq q
\]</span></p>
<h2 id="the-univalence-axiom">The Univalence Axiom</h2>
<p>Secondarily to this notion of morphisms of morphisms, concomitant to
HoTT’s conception came the idea of univalence. The univalence axiom
states the following:</p>
<p><span class="math display">\[
(A \simeq B) = (A = B)
\]</span></p>
<p>Before we can cover the implications of such an axiom, we need first
to cover a few prerequisites.</p>
<h2 id="intensional-vs-extensional-type-theories">Intensional vs Extensional Type Theories</h2>
<p>In an <em>intensional</em> type theory, the notion of equality is
definitional, not one that can be expressed as a proposition. This
concept of intensionality is how Per Martin-Löf originally conceived
of type theory. In an intensional type theory, for two objects to be
equivalent, they must, by definition, be the same thing. While
Martin-Löf stated that intensional equality was an equality of
meaning, i.e. synonymy, its implications are such that two objects
must be syntactically equivalent.</p>
<p>However, in an <em>extensional</em> type theory, equivalences can be
expressed as simply another type—a proposition of equality.</p>
<p>This would allow one to, say, state that in our type of topological
spaces, a homotopy equivalence is an equivalence for this type, and to
prove two spaces’ equivalence, we merely need to define an inhabitant
of that type.</p>
<p>This idea of being able to define our own equivalences can plausibly
be seen as a weakening of equality, as we’re now providing a way in
which to arbitrarily state equivalences for our types. But wait!</p>
<h2 id="univalence-as-an-extensional-to-intensional-bridge">Univalence as an Extensional to Intensional Bridge</h2>
<p>Lets look again at the definition of the Univalence Axiom:</p>
<p><span class="math display">\[
(A \simeq B) = (A = B)
\]</span></p>
<p>What this axiom is stating is this: If two objects are isomorphic,
then they are equivalent.</p>
<p>This axiom elides the need for an explicitly extensional type theory,
as those propositional isomorphisms we’ve defined for our types like
the type of topological spaces, are in fact, universally,
equivalences. Univalence <em>universalizes</em> domain specific isomorphisms
stating that they are all equivalent to an equality. The implication
of this axiom is then, a type theory which has propositional
equalities can again be seen as intensional as the definitional-style
of equality is possible by axiomatizing isomorphisms from specific
types to be universal equalities.</p>
<h2 id="higher-inductive-types">Higher Inductive Types</h2>
<p>A higher inductive type is the generalization of the idea that, along
with a type’s definition and the definition of its constructors, we
may also define its equivalences. Something like this:</p>
<p><span class="math display">\[
\begin{align*}
\mathbb{N} &amp;: Type \\
zero &amp;: \mathbb{N} \\
succ &amp;: \mathbb{N} \rightarrow \mathbb{N} \\
\equiv_{\mathbb{N}}\ &amp;: \{n : \mathbb{N}\} \rightarrow n \rightarrow n \rightarrow Type \\
\end{align*}
\]</span></p>
<p>This has a lot of overlap with what we’ve discussed thus far,
i.e. extensional equality, &amp; c., but when we talk about Higher
Inductive Types, rather than merely considering the equality, we want
to think about what happens when we use one of these equalities.</p>
<h2 id="h-levels">H-Levels</h2>
<p>In HoTT, as we’ve discussed previously, we are given the ability to
define functions <em>between</em> functions, and equality is simply a
function. That ladder of equalities has a name: H-Levels. When we’re
working with a type which requires no higher equalities, we say that
type is a <em>quotient type</em>. In the following section, we’ll use
quotient types to try and build an intuition for what the application
of one of these equalities results in.</p>
<h3 id="quotient-types">Quotient Types</h3>
<p>We’ll use quotient types’ categorical semantics to demonstrate the
application of one of these equalities.</p>
<p>To begin, lets say we have some category <span class="math inline">\(D\)</span>, in which we have the
following diagram:</p>
<p><span class="math display">\[
\xymatrix{
  A_1 \ar[r] \ar[d] &amp; A_2 \ar[d] \\
  B_1 \ar[r] \ar[d] &amp; B_2 \ar[d] \\
  C_1 \ar[r] &amp; C_2 \\
}
\]</span></p>
<p>And an equivalence, <span class="math inline">\(E\)</span>, applicable to objects in <span class="math inline">\(D\)</span>. We can then
define a functor <span class="math inline">\(F : D \rightarrow D/E\)</span> which applies that
equivalence and maps a category to its skeleton:</p>
<p><span class="math display">\[
\xymatrix{
  A \ar[d] \\
  B \ar[d] \\
  C
}
\]</span></p>
<p>What we’ve done here is divide our category into its <em>equivalence
classes</em>, precipitated by <span class="math inline">\(E\)</span>.</p>
<hr />
<p>At this point, we covered homotopies as our foundational structure,
and then studied that structure and learned about many interesting
things which shake out of homotopies when we think hard enough about
them. These mathematical tools that we’ve explicated thus far compose
the logical, categorical, and type theoretical desiderata for thinking
about the real numbers in Homotopy Type Theory. We’ll use these tools
to construct the mathematical objects discussed in the following
section, which, when put together give us a <em>construction</em> of the real
numbers.</p>
<p>However, before we can do that, we first need to have an intuition for
this metod of construction.</p>
<h1 id="part-ii-constructing-the-real-numbers">Part II: Constructing the Real Numbers</h1>
<p>We’ve said that we intend <em>construct</em> the real numbers. What I mean
when I say <em>construct</em>, is that we will build them, like a house from
its materials, from other types which, when used together, can realize
the real numbers. The following sections will cover those materials
and how we fasten them together in order to produce the reals.</p>
<h2 id="cauchy">Cauchy</h2>
<p>As presented in the book, there exists two ways in which to construct
the real numbers, heretofore denoted <span class="math inline">\(\mathbb{R}\)</span>. One of those ways
is through Dedekind cuts which we will not elucidate in this
document. The other is the <em>Cauchy</em> real numbers, or
<span class="math inline">\(\mathbb{R}_c\)</span>. This section will focus on this construction.</p>
<h3 id="cauchy-sequences">Cauchy Sequences</h3>
<p>A Cauchy Sequence is a sequence of points, or elements of a set, which
converge on some value. For example, if we were to choose from the set
of the rationals the sequence <span class="math inline">\(\{3, 3.14, 3.141, 3.1415, 3.14159,
…\}\)</span>, we could say that, over time, it converges on famous number
which we know to be irrational: <span class="math inline">\(\pi\)</span>. This number to which our
sequence converges is called its <em>limit</em>.</p>
<p>Stated more formally, a Cauchy sequence is a sequence:</p>
<p><span class="math display">\[
\mathcal{C} := \{x_1, x_2, x_3, …, x_n \}
\]</span></p>
<p>Such that</p>
<p><span class="math display">\[
\forall i.\ \exists \epsilon.\ |x_i - x_{i+1}| &lt; \epsilon
\]</span></p>
<p>Our use of <span class="math inline">\(\pi\)</span> was no accident. It turns out, Cauchy’s sequences
can be used to construct real numbers like it! This is accomplished by
beginning with the rationals, or <span class="math inline">\(\mathbb{Q}\)</span>, and then <em>completing</em>
them, or filling in the gaps between them, by employing sequences of
<span class="math inline">\(\mathbb{Q}\)</span> which converge on some real number—a number which can
be enumerated by —up to some <span class="math inline">\(\epsilon\)</span> precision.</p>
<h3 id="formalizing-mathbbr">Formalizing <span class="math inline">\(\mathbb{R}\)</span></h3>
<p>If through Cauchy sequences we con construct <span class="math inline">\(\mathbb{R}\)</span>, then we
can simply formalize their notion in HoTT. The first way we’d likely
go about this is through quotient types as described above:</p>
<p><span class="math display">\[
\mathbb{R}_c := \mathbb{Q}_c / \approx
\]</span></p>
<p>Where <span class="math inline">\(\approx\)</span> is defined through our Cauchy sequence definition:</p>
<p><span class="math display">\[
\approx_{\epsilon} : \mathbb{R} \rightarrow \mathbb{R} \rightarrow \text{Prop}
\]</span></p>
<p>I.e. the relationship <span class="math inline">\(\approx\)</span> holds for two <span class="math inline">\(r \in \mathbb{R}\)</span>’s
up to some <span class="math inline">\(\epsilon\)</span> precision.</p>
<p>The implication of this is of course that any <span class="math inline">\(r \in \mathbb{R}\)</span> can
be defined as the Cauchy sequences which approximate
it. Unfortunately, this approach requires the axiom of (countable)
choice. AoC provides an axiomatic way in which to arbitrarily “choose”
some set out of a set of set, which is exactly what we’d have to do to
with our set of sequences. While they approximate the same real, even
up to the same precision, we must still select one in particular in
order to deploy it as our construction of that real number it
approximates. We’d like to instead be able to construct <span class="math inline">\(\mathbb{R}\)</span>
without employing such an axiom.</p>
<h3 id="the-cauchy-reals-in-hott">The Cauchy Reals in HoTT</h3>
<p>In order to avoid the AoC, the onus of selection is instead pushed off
to the caller, so to speak. This is through a constructor which
produces a Cauchy <em>approximation</em>.</p>
<p><span class="math display">\[
\text{CApprx} : \Sigma_{x : \mathbb{Q}_+ \rightarrow A}\ \forall \epsilon\
\delta .\  x_{\epsilon} \approx_{\epsilon + \delta} x_{\delta}
\]</span></p>
<p>And given an <span class="math inline">\(a : \text{CApprox}\)</span> we take its limit: <span class="math inline">\(lim :
\text{CApprx} \rightarrow \mathbb{R}\)</span> to produce our real number.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    <category scheme="https://dpitt.me/tags/type theory/index.html" term="type theory" label="type theory"/>
    
    <category scheme="https://dpitt.me/tags/hott/index.html" term="hott" label="hott"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Using Emacs's org-mode As Your Zettelkasten</title>
    <link href="https://dpitt.me/forest/dsp-0015.html" rel="alternate" type="text/html"/>
    <published>Wed, 25 Mar 2020 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>I like to write. I don’t do it as often as I’d like to, but
nonetheless I do like to do it. I’ve found that one of the best
exercises for increasing the bandwidth of the passageway from my brain
to my fingers is superfluous note-taking. Couple this Good Thing with
a Could Appear Like A Good Thing But Is Dubious At Best Thing, namely
ADHD/anxiety-induced procrastinatory over-organizing, one could
imagine that how I choose to organize those notes is something that I
(superfluously) think about. I’ve tried them all, notebooks, Evernote,
Google Keep, <a href="https://github.com/pittma/notes">even a custom markdown-to-jekyll app that was synched by
Dropbox</a>. However, when I switched to
Emacs about 2 years ago and started using <a href="https://orgmode.org/">Org
mode</a>, it, as a medium, really stuck. Since then
I’ve just been spinning and churning with nearly no success on how to
organize my org file(s). And then, about a week ago, I encountered
<a href="https://www.srid.ca/2010101.html"><code>neuron</code></a> in the <a href="https://haskellweekly.news"><em>Haskell
Weekly</em></a> newsletter, and because I’m prone
to buy wholesale anything a smart person says about note-taking, my
interest was piqued. The <code>neuron</code> author linked to this [Zettlekasten
overview](https://writingcooperative.com/zettelkasten-how-one-german-scholar-was-so-freakishly-productive-997e4e0ca125],
which I promptly devoured and, well, I <em>had</em> to do it. Please read
what I linked above for a nice narrative on Zettelkasten’s
origins. What follows is just a skeletal exposition.</p>
<h2 id="zettelkasten">Zettelkasten</h2>
<p>Literally “note box”, the original zettlekasten was a piece of
furniture with index card drawers, six-by-six, like one of those
bygone cabinets you used to see at the library for filing books’
locations with Dewey Decimal Classification. Those index cards
followed four simple rules:</p>
<ol type="1">
<li><strong>A unique id.</strong>[fn:id]</li>
<li><strong>One idea per card.</strong></li>
<li><strong>Tag each idea/card (optionally).</strong></li>
<li><strong>Each idea/card /links/ to related ideas.</strong></li>
</ol>
<p>Given these rules, one can begin to build a cache of knowledge linked
together <em>almost</em> like neurons, but <em>definitely</em> like a graph that you
can explore from any ingress “idea vertex”[fn:conv]. It was this broad
transitivity of ideas that Zettelkasten’s creator attributed their
prolificacy to.</p>
<p>Aha! A system. What kind of programmer would I be if I didn’t automate
as much of this system as possible?</p>
<h3 id="step-one-a-zet-command">Step One: A <code>zet</code> Command</h3>
<p>To begin, I wanted a way to “start the conversation” with my
zettelkasten. To me, that looked like just telling it about a new
topic. Something like this:</p>
<pre class="shell"><code>$ zet the sorites paradox</code></pre>
<p>Which I would expect to open an editor and let me try and get this
thought from my head to my fingers with as few barriers as possible.
To this end, I created a small bash script to put on my path:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co">#! /usr/bin/env bash</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="bu">set</span> <span class="at">-e</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="va">ZROOT</span><span class="op">=</span><span class="va">${ZET_ROOT</span><span class="op">:=</span><span class="kw">`</span><span class="ex">/Dropbox/zet}</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="va">name</span><span class="op">=</span><span class="st">&quot;</span><span class="va">$ZROOT</span><span class="st">/</span><span class="va">$(</span><span class="fu">date</span> +<span class="st">&quot;%Y-%m-%d-%H%M&quot;</span><span class="va">)</span><span class="st">&quot;</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="va">content</span><span class="op">=</span><span class="st">&quot;*&quot;</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> word <span class="kw">in</span> $<span class="kw">;</span> <span class="cf">do</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>  <span class="va">name</span><span class="op">+=</span><span class="st">&quot;-</span><span class="va">$word</span><span class="st">&quot;</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>  <span class="va">content</span><span class="op">+=</span><span class="st">&quot; </span><span class="va">${word</span><span class="op">^</span><span class="va">}</span><span class="st">&quot;</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a><span class="cf">done</span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="bu">[</span> <span class="ot">!</span> <span class="ot">-e</span> <span class="st">&quot;</span><span class="va">$name</span><span class="st">.org&quot;</span> <span class="bu">]</span><span class="kw">;</span> <span class="cf">then</span></span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="at">-e</span> <span class="st">&quot;</span><span class="va">$content</span><span class="st">\n\n#+FILETAGS:&quot;</span> <span class="op">&gt;</span> <span class="va">$name</span>.org</span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span>
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a><span class="ex">emacsclient</span> <span class="at">-n</span> <span class="va">$name</span>.org</span></code></pre></div>
<p>In terms of new code, this is the extent of it for this Org mode-based
zettelkasten. It pokes my pretty-much-always-running Emacs server and
opens a file whose name is prepended by the date and time, followed by
a dash-joined version of what I typed out at the command line. As an
example, right now it’s March 26th, 2020 11:58 AM. So given the <code>zet</code>
command I ran above, I’d get a file created for me called
<code>2020-03-26-1158-sorites-paradox.org</code> and that file’d have the
following content:</p>
<pre><code>* Sorites Paradox

#+FILETAGS:</code></pre>
<p>That I can go on to add content to:</p>
<pre><code>#+FILETAGS: :philosophy:fom:

[[https://en.wikipedia.org/wiki/Sorites_paradox]]

A random philosophical point made very well about the foundations of
mathematics, from the Foundations of Mathematics mailing list no less.

#+BEGIN_QUOTE
Maybe ZFC is not the most natural framework in order to express
emergent properties related to the so-called Sorites Paradox. For
example, one neuron is not an intelligent brain. If we have a system
consisting of n neurons that is not an intelligent brain, to add one
neuron more to the system will not transform it into an intelligent
brain. The conclusion seems to be that there are not intelligent
brains in nature, which is contradicted by experience. Therefore,
sciences depending on emergent properties (neurology, biology, etc)
may need mathematical machinery which is not founded on ZFC.
#+END_QUOTE</code></pre>
<h3 id="step-two-links">Step Two: Links</h3>
<p>Then let’s say that I’d had a thought of my own (mind you, one that I
cannot and will not defend):</p>
<pre><code>* Category Theory Metatheory

#+FILETAGS: :fom:philosophy:

*Category theory is a metatheory for mathematics.*

This idea brought to you by O explaining multiplication to T about how
it&#39;s _like_ addition, but you add one number to itself the other
number of times or something like that. It was this _like addition_
that sparked the thought, because it captures the arity of the
addition and multiplication operator, which is the point she was
making. They have the same shape. With Cartesian closed categories,
you can give a metatheory for this shape, i.e. with currying, and what
is math if not functions with some arity?</code></pre>
<p>These two ideas are related insofar as one is a dilettante spitballing
about the foundations of mathematics and the other is an expert
sharing their insight about the foundations of mathematics. While I
was writing this post and thinking about the neuron to intelligent
brain paradox I remembered the nonsense my own brain synthesized while
O explained multiplication to T, so, yeah, I want to link these two
things in my zettelkasten—because that’s the whole point!</p>
<p>I’ll add that link by typing <kbd>ctrl</kbd> +
<kbd>c</kbd><kbd>ctrl</kbd> + <kbd></kbd> (<code>C-c C-l</code>) then start to
type <code>file</code> which gives me this menu (I’m using
<a href="https://emacs-helm.github.io/helm/">Helm</a>):</p>
<p>[[file:/images/zettel-file.png]]</p>
<p>When I hit html: <kbd>return</kbd>, it shows my list of files in the
current directory. That is, the directory of the file I’m currently
visiting:</p>
<p>[[file:/images/zettel-link.png]]</p>
<p>From here I can use the arrow keys or the completion search that Helm
gives me to find the note I’d like to link. Now I’ve got a link in
this note:</p>
<pre><code>* Category Theory Metatheory

#+FILETAGS: :fom:philosophy:

*Category theory is a metatheory for mathematics.*

This idea brought to you by O explaining multiplication to T about how
it&#39;s /like/ addition, but you add one number to itself the other
number of times or something like that. It was this _like addition_
that sparked the thought, because it captures the arity of the
addition and multiplication operator, which is the point she was
making. They have the same shape. With Cartesian closed categories,
you can give a metatheory for this shape, i.e. with currying, and what
is math if not functions with arity?

** Links

[[file:2020-03-26-1132-sorites-paradox.org]]</code></pre>
<p>And if I’d like to follow it, I can just do <kbd>ctrl</kbd> +
<kbd>c</kbd><kbd>ctrl</kbd> + <kbd>o</kbd> (<code>C-c C-o</code>), or
<code>org-open-link-at-point</code>.</p>
<h3 id="step-three-conversing-search">Step Three: Conversing (search)</h3>
<p>Emacs Org mode has something called an <em>agenda</em>, that works as an
aggregator over a collection of Org files. It can do some pretty
amazing things like building a calendar for you, helping you track
your habits, or organizing your todo list. For us, now, though, we’re
going to use its tag and string matching searches. It does require a
little setup in your <code>init.el</code> file to enable it, something like this:</p>
<pre class="emacs-lisp"><code>(global-set-key (kbd &quot;C-c a&quot;) &#39;org-agenda)
(setq org-agenda-files &#39;(&quot;`/Dropbox/org/notes/todo.org&quot; &quot;`/Dropbox/zet/&quot;))</code></pre>
<p>This results in the key sequence <kbd>ctrl</kbd> +
<kbd>c</kbd><kbd>a</kbd> being globally bound to opening Org Agenda,
which looks like this:</p>
<p>[[file:/images/zettel-agenda.png]]</p>
<p>From here I can use <kbd>m</kbd> to search by tag matches, or
<kbd>s</kbd> for string matches, both of which yield a list of
matches:</p>
<p>[[file:/images/zettel-search.png]]</p>
<p>I can just arrow down and select one of the matching headlines to open
the file containing it.</p>
<h2 id="valedictions">Valedictions</h2>
<p>I’ve only started to build up my own zettelkasten, but it truly feels
like I’ve found a thing that fits, that <em>works</em>.</p>
<p>One important factor I’ve found with note-taking, at least for me, is
for my note compendium to be append-only. I can’t let myself go back
and pare down, edit and reëdit endlessly, because, unfailingly, I
always end up deleting everything since of course no organizational
scheme I can come up with is copacetic. The Zettelkasten approach
actually saves me from myself here because there’s a single note per
file, and I’m almost never inundated with an entire list.</p>
<p>Using my zettelkasten stirs up that nostalgic feeling of going through
an old notebook every time I open it, and I’m only ever confronted
with the ideas I’ve asked of it. It narrows the aperture to a topic in
a way that saves me from myself and the consequences of the inexorable
self-loathing of an anxious mind. If anything from these last few
paragraphs resonates at all, I think it may work for you too.</p>
<p>Happy remembering!</p>
<p>[fn:id] This id needs to be unique, but is in general arbitrary and
needn’t necessarily be related to the note’s content.
[fn:conv] Niklas Luhmann, the creator of the original zettelkasten
occasionally referred to it as a “conversation partner”.</p> ]]>
    </content>

    
  </entry>
  
  <entry>
    <title type="html">Using Type-Level Programming in Rust to Make Safer Hardware Abstractions</title>
    <link href="https://dpitt.me/forest/dsp-0014.html" rel="alternate" type="text/html"/>
    <published>Fri, 25 Oct 2019 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>Introducing
<a href="https://github.com/auxoncorp/bounded-registers"><code>bounded-registers</code></a>.</p>
<p>In the world of systems programming where one may find themselves
writing hardware drivers or interacting directly with memory-mapped
devices, that interaction is almost always through memory-mapped
registers provided by the hardware. We typically interact with these
things through bitwise operations on some fixed-width numeric type.</p>
<p>For instance, imagine an 8-bit register with three fields:</p>
<pre><code>+----------+------+-----------+---------+
| (unused) | Kind | Interrupt | Enabled |
+----------+------+-----------+---------+
   5-7       2-4        1          0</code></pre>
<p>The number below the field name prescribes the bits used by that field
in the register. To enable this register, one would write the value
<code>1</code>, represented in binary as <code>0000_0001</code>, to set the enabled field’s
bit. Often, though, we also have an existing configuration in the
register that we don’t want to disturb. Say we want to enable
interrupts on our device above but also want to be sure that the
device itself remains enabled. To do that, we must combine the
Interrupt field’s value with the <code>Enabled</code> field’s value. We’d do that
with bitwise operations:</p>
<pre><code>1 | (1 &lt;&lt; 1)</code></pre>
<p>This gives us the binary value <code>0000_0011</code> by <code>or</code>-ing 1 with 2, which
we get by shifting 1 left by 1. We can write this to our register,
leaving it enabled but also enabling interrupts.</p>
<p>This is a lot to keep in our heads, especially when dealing with
potentially 100s of registers for a complete system. In practice, we
do this with mnemonics which track a field’s position in a register
and how wide that field is—i.e. what’s its <em>upper bound</em>?</p>
<p>Here’s an example of one of these mnemonics, they’re C macros that
replace their occurrences with the code on the right-hand side. This
is our shorthand for the register we laid out above. The left-hand
side of the <code>&amp;</code> puts us in position for that field and the right-hand
side limits us to only that field’s bits.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#define REG_ENABLED_FIELD</span><span class="op">(</span><span class="pp">x</span><span class="op">)</span><span class="pp"> </span><span class="op">(</span><span class="pp">x </span><span class="op">&lt;&lt;</span><span class="pp"> </span><span class="dv">0</span><span class="op">)</span><span class="pp"> </span><span class="op">&amp;</span><span class="pp"> </span><span class="dv">1</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="pp">#define REG_INTERRUPT_FIELD</span><span class="op">(</span><span class="pp">x</span><span class="op">)</span><span class="pp"> </span><span class="op">(</span><span class="pp">x </span><span class="op">&lt;&lt;</span><span class="pp"> </span><span class="dv">1</span><span class="op">)</span><span class="pp"> </span><span class="op">&amp;</span><span class="pp"> </span><span class="dv">2</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="pp">#define REG_KIND_FIELD</span><span class="op">(</span><span class="pp">x</span><span class="op">)</span><span class="pp"> </span><span class="op">(</span><span class="pp">x </span><span class="op">&lt;&lt;</span><span class="pp"> </span><span class="dv">2</span><span class="op">)</span><span class="pp"> </span><span class="op">&amp;</span><span class="pp"> </span><span class="op">(</span><span class="dv">7</span><span class="pp"> </span><span class="op">&lt;&lt;</span><span class="pp"> </span><span class="dv">2</span><span class="op">)</span></span></code></pre></div>
<p>We’d then use these to abstract over the derivation of a register’s
value with something like:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="dt">void</span> set_reg_val<span class="op">(</span>reg<span class="op">*</span> u8<span class="op">,</span> val u8<span class="op">);</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>fn enable_reg_with_interrupt<span class="op">(</span>reg<span class="op">*</span> u8<span class="op">)</span> <span class="op">{</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>    set_reg_val<span class="op">(</span>reg<span class="op">,</span> REG_ENABLED_FIELD<span class="op">(</span><span class="dv">1</span><span class="op">)</span> <span class="op">|</span> REG_INTERRUPT_FIELD<span class="op">(</span><span class="dv">1</span><span class="op">));</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>And this is the state of the art, really. In fact, this is how the
bulk of the drivers appear in the Linux kernel.</p>
<p><strong>Is there a better way?</strong> Consider the boon to safety and expressibility
if our type system was one borne out of modern programming languages
research. That is, what could we do with a richer, more expressive
type system to make this process safer and more tenable?</p>
<h1 id="level-i-using-rust-to-talk-to-hardware">Level I: Using Rust to talk to hardware</h1>
<p>With Rust we can use data structures to represent fields, we can
attach them to specific registers, and we can provide concise and
sensible ergonomics with interacting with the hardware. This first
example uses the most basic facilities provided by Rust, but
regardless, just the added structure alleviates some of the density
from the C example above. Now, a field is a named thing, not a number
derived from shadowy bitwise operators, and registers are types with
state—one extra layer of abstraction over the hardware.</p>
<p>Continuing with our register above as an example:</p>
<pre><code>+----------+------+-----------+---------+
| (unused) | Kind | Interrupt | Enabled |
+----------+------+-----------+---------+
   5-7       2-4        1          0</code></pre>
<p>How might we want to express such a thing in Rust types?</p>
<p>We’ll start in a similar way, by defining constants for each field’s
<em>offset</em>—that is, how far it is from the least significant bit—and its
mask. A <em>mask</em> is a value whose binary representation can be used to
update or read the field from inside the register.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> ENABLED_MASK<span class="op">:</span> <span class="dt">u8</span> <span class="op">=</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> ENABLED_OFFSET<span class="op">:</span> <span class="dt">u8</span> <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> INTERRUPT_MASK<span class="op">:</span> <span class="dt">u8</span> <span class="op">=</span> <span class="dv">2</span><span class="op">;</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> INTERRUPT_OFFSET<span class="op">:</span> <span class="dt">u8</span> <span class="op">=</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> KIND_MASK<span class="op">:</span> <span class="dt">u8</span> <span class="op">=</span> <span class="dv">7</span> <span class="op">&lt;&lt;</span> <span class="dv">2</span><span class="op">;</span></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> KIND_OFFSET<span class="op">:</span> <span class="dt">u8</span> <span class="op">=</span> <span class="dv">2</span><span class="op">;</span></span></code></pre></div>
<p>Next, we’ll declare a field type and do our operations to convert a
given value into its position-relevant value for use inside the
register.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> Field <span class="op">{</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>    value<span class="op">:</span> <span class="dt">u8</span><span class="op">,</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="kw">impl</span> Field <span class="op">{</span></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a>    <span class="kw">fn</span> new(mask<span class="op">:</span> <span class="dt">u8</span><span class="op">,</span> offset<span class="op">:</span> <span class="dt">u8</span><span class="op">,</span> val<span class="op">:</span> <span class="dt">u8</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a>        Field <span class="op">{</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a>            value<span class="op">:</span> (val <span class="op">&lt;&lt;</span> offset) <span class="op">&amp;</span> mask<span class="op">,</span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a>        <span class="op">}</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>Finally, a <code>Register</code> type, which wraps around the numeric type that
matches the width of our register. <code>Register</code> has an update function
which updates the register with the given field.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> Register(<span class="dt">u8</span>)<span class="op">;</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="kw">impl</span> Register <span class="op">{</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a>    <span class="kw">fn</span> update(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span><span class="op">,</span> val<span class="op">:</span> Field) <span class="op">{</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a>        <span class="kw">self</span><span class="op">.</span><span class="dv">0</span> <span class="op">=</span> <span class="kw">self</span><span class="op">.</span><span class="dv">0</span> <span class="op">|</span> field<span class="op">.</span>value<span class="op">;</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> enable_register(<span class="op">&amp;</span><span class="kw">mut</span> reg) <span class="op">{</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a>    reg<span class="op">.</span>update(<span class="pp">Field::</span>new(ENABLED_MASK<span class="op">,</span> ENABLED_OFFSET<span class="op">,</span> <span class="dv">1</span>))<span class="op">;</span></span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>This is nice, but it’s not ideal. We have to remember to bring the
mask and offset with us, and we’re calculating them ad hoc, by hand,
which could be problematic. We humans aren’t great at precise and
repetitive tasks—we tend to get tired or lose focus and this leads to
mistakes. Transcribing by hand the masks and offsets one register at a
time will almost certainly end badly. This is the kind of task we
ought to leave to a machine.</p>
<p>Secondarily, what if there was a way to have the field’s type itself
carry the mask and offset information? What if we could catch mistakes
in our implementation for how we access and interact with hardware
registers at compile-time instead of discovering them at runtime?
Perhaps we can lean on one of the strategies commonly used to suss out
issues at compile-time, like types.</p>
<h1 id="leveling-up">Leveling Up</h1>
<p>Let’s modify our earlier example by using
<a href="https://docs.rs/crate/typenum">typenum</a>, a library that provides
numbers and arithmetic at the type level. Here we’ll parameterize our
<code>Field</code> type with its mask and offset, making it available for any
instance of <code>Field</code> without needing to include it at the callsite.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span>macro_use<span class="at">]</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="kw">extern</span> <span class="kw">crate</span> typenum<span class="op">;</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="kw">use</span> <span class="pp">core::marker::</span>PhantomData<span class="op">;</span></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="kw">use</span> <span class="pp">typenum::</span><span class="op">*;</span></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a><span class="co">// Now we’ll add Mask and Offset to Field’s type</span></span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> Field<span class="op">&lt;</span>Mask<span class="op">:</span> Unsigned<span class="op">,</span> Offset<span class="op">:</span> Unsigned<span class="op">&gt;</span> <span class="op">{</span></span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a>    value<span class="op">:</span> <span class="dt">u8</span><span class="op">,</span></span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a>    _mask<span class="op">:</span> PhantomData<span class="op">&lt;</span>Mask<span class="op">&gt;,</span></span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a>    _offset<span class="op">:</span> PhantomData<span class="op">&lt;</span>Offset<span class="op">&gt;,</span></span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a><span class="co">// We can use type aliases to give meaningful names to</span></span>
<span id="cb9-16"><a href="#cb9-16" aria-hidden="true" tabindex="-1"></a><span class="co">// our fields (and not have to remember their offsets masks).</span></span>
<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegEnabled <span class="op">=</span> Field<span class="op">&lt;</span>U1<span class="op">,</span> U0<span class="op">&gt;;</span></span>
<span id="cb9-18"><a href="#cb9-18" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegInterrupt <span class="op">=</span> Field<span class="op">&lt;</span>U2<span class="op">,</span> U1<span class="op">&gt;;</span></span>
<span id="cb9-19"><a href="#cb9-19" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegKind <span class="op">=</span> Field<span class="op">&lt;</span><span class="pp">op!</span>(U7 <span class="op">&lt;&lt;</span> U2)<span class="op">,</span> U2<span class="op">&gt;;</span></span></code></pre></div>
<p>And now, when revisiting <code>Field</code>’s constructor, we can elide the mask
and offset parameters, as the type itself contains that information:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">impl</span><span class="op">&lt;</span>Mask<span class="op">:</span> Unsigned<span class="op">,</span> Offset<span class="op">:</span> Unsigned<span class="op">&gt;</span> Field<span class="op">&lt;</span>Mask<span class="op">,</span> Offset<span class="op">&gt;</span> <span class="op">{</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>    <span class="kw">fn</span> new(val<span class="op">:</span> <span class="dt">u8</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>        Field <span class="op">{</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>            value<span class="op">:</span> (val <span class="op">&lt;&lt;</span> <span class="pp">Offset::</span>U8) <span class="op">&amp;</span> <span class="pp">Mask::</span>U8<span class="op">,</span></span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a>            _mask<span class="op">:</span> PhantomData<span class="op">,</span></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a>            _offset<span class="op">:</span> PhantomData<span class="op">,</span></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a>        <span class="op">}</span></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a><span class="co">// And to enable our register...</span></span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> enable_register(<span class="op">&amp;</span><span class="kw">mut</span> reg) <span class="op">{</span></span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a>    reg<span class="op">.</span>update(<span class="pp">RegEnabled::</span>new(<span class="dv">1</span>))<span class="op">;</span></span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>This is looking pretty good, but… what happens when we make a mistake
regarding whether a given value will fit into a field? Consider a
simple typo where we put <code>10</code> instead of <code>1</code>:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> enable_register(<span class="op">&amp;</span><span class="kw">mut</span> reg) <span class="op">{</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>    reg<span class="op">.</span>update(<span class="pp">RegEnabled::</span>new(<span class="dv">10</span>))<span class="op">;</span></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>In our code above? What is the expected outcome? Well, the code we
have now will set that enabled bit to 0 because <code>10 &amp; 1 = 0</code>. That’s
unfortunate; it would be nice to know that a value I’m attempting to
write into a field will actually fit into that field before attempting
a write. As a matter of fact, I’d consider lopping off the high bits
of an errant field value <em>undefined behavior</em> (*gasps*).</p>
<h1 id="safety-first">Safety First</h1>
<p>How can we check that a field’s value fits in its prescribed position
in a general way? More type-level numbers!</p>
<p>We can add a <code>Width</code> parameter to <code>Field</code>, and use it to verify that
we can fit the given value into the field.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> Field<span class="op">&lt;</span>Width<span class="op">:</span> Unsigned<span class="op">,</span> Mask<span class="op">:</span> Unsigned<span class="op">,</span> Offset<span class="op">:</span> Unsigned<span class="op">&gt;</span> <span class="op">{</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>    value<span class="op">:</span> <span class="dt">u8</span><span class="op">,</span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a>    _mask<span class="op">:</span> PhantomData<span class="op">&lt;</span>Mask<span class="op">&gt;,</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a>    _offset<span class="op">:</span> PhantomData<span class="op">&lt;</span>Offset<span class="op">&gt;,</span></span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a>    _width<span class="op">:</span> PhantomData<span class="op">&lt;</span>Width<span class="op">&gt;,</span></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegEnabled <span class="op">=</span> Field<span class="op">&lt;</span>U1<span class="op">,</span>U1<span class="op">,</span> U0<span class="op">&gt;;</span></span>
<span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegInterrupt <span class="op">=</span> Field<span class="op">&lt;</span>U1<span class="op">,</span> U2<span class="op">,</span> U1<span class="op">&gt;;</span></span>
<span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegKind <span class="op">=</span> Field<span class="op">&lt;</span>U3<span class="op">,</span> <span class="pp">op!</span>(U7 <span class="op">&lt;&lt;</span> U2)<span class="op">,</span> U2<span class="op">&gt;;</span></span>
<span id="cb12-11"><a href="#cb12-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-12"><a href="#cb12-12" aria-hidden="true" tabindex="-1"></a><span class="kw">impl</span><span class="op">&lt;</span>Width<span class="op">:</span> Unsigned<span class="op">,</span> Mask<span class="op">:</span> Unsigned<span class="op">,</span> Offset<span class="op">:</span> Unsigned<span class="op">&gt;</span> Field<span class="op">&lt;</span>Width<span class="op">,</span> Mask<span class="op">,</span> Offset<span class="op">&gt;</span> <span class="op">{</span></span>
<span id="cb12-13"><a href="#cb12-13" aria-hidden="true" tabindex="-1"></a>    <span class="kw">fn</span> new(val<span class="op">:</span> <span class="dt">u8</span>) <span class="op">-&gt;</span> <span class="dt">Option</span><span class="op">&lt;</span><span class="dt">Self</span><span class="op">&gt;</span> <span class="op">{</span></span>
<span id="cb12-14"><a href="#cb12-14" aria-hidden="true" tabindex="-1"></a>        <span class="cf">if</span> val <span class="op">&lt;=</span> (<span class="dv">1</span> <span class="op">&lt;&lt;</span> <span class="pp">Width::</span>U8) <span class="op">-</span> <span class="dv">1</span> <span class="op">{</span></span>
<span id="cb12-15"><a href="#cb12-15" aria-hidden="true" tabindex="-1"></a>            <span class="cn">Some</span>(Field <span class="op">{</span></span>
<span id="cb12-16"><a href="#cb12-16" aria-hidden="true" tabindex="-1"></a>                value<span class="op">:</span> (val <span class="op">&lt;&lt;</span> <span class="pp">Offset::</span>U8) <span class="op">&amp;</span> <span class="pp">Mask::</span>U8<span class="op">,</span></span>
<span id="cb12-17"><a href="#cb12-17" aria-hidden="true" tabindex="-1"></a>                _mask<span class="op">:</span> PhantomData<span class="op">,</span></span>
<span id="cb12-18"><a href="#cb12-18" aria-hidden="true" tabindex="-1"></a>                _offset<span class="op">:</span> PhantomData<span class="op">,</span></span>
<span id="cb12-19"><a href="#cb12-19" aria-hidden="true" tabindex="-1"></a>                _width<span class="op">:</span> PhantomData<span class="op">,</span></span>
<span id="cb12-20"><a href="#cb12-20" aria-hidden="true" tabindex="-1"></a>            <span class="op">}</span>)</span>
<span id="cb12-21"><a href="#cb12-21" aria-hidden="true" tabindex="-1"></a>        <span class="op">}</span> <span class="cf">else</span> <span class="op">{</span></span>
<span id="cb12-22"><a href="#cb12-22" aria-hidden="true" tabindex="-1"></a>            <span class="cn">None</span></span>
<span id="cb12-23"><a href="#cb12-23" aria-hidden="true" tabindex="-1"></a>        <span class="op">}</span></span>
<span id="cb12-24"><a href="#cb12-24" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb12-25"><a href="#cb12-25" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>Now we can only construct a <code>Field</code> if the given value fits! Otherwise,
we have <code>None</code>, which would signal to us that an error has occurred,
rather than lop off the high bits of the value and silently write an
unexpected value.</p>
<p>Note, though, this will raise an error at runtime. However, we knew
the value we wanted to write beforehand, remember? Let’s use that fact
to raise the assurance bar. We can teach the compiler to reject
entirely a program which has an invalid field value—we don’t have to
wait until we run it!</p>
<p>This time we’ll add a trait bound (the <code>where</code> clause) to a new
realization of new, called <code>new_checked</code> that asks that the incoming
value be less-than-or-equal-to the maximum possible value a field with
the given <code>Width</code> can hold.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> Field<span class="op">&lt;</span>Width<span class="op">:</span> Unsigned<span class="op">,</span> Mask<span class="op">:</span> Unsigned<span class="op">,</span> Offset<span class="op">:</span> Unsigned<span class="op">&gt;</span> <span class="op">{</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>    value<span class="op">:</span> <span class="dt">u8</span><span class="op">,</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>    _mask<span class="op">:</span> PhantomData<span class="op">&lt;</span>Mask<span class="op">&gt;,</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a>    _offset<span class="op">:</span> PhantomData<span class="op">&lt;</span>Offset<span class="op">&gt;,</span></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a>    _width<span class="op">:</span> PhantomData<span class="op">&lt;</span>Width<span class="op">&gt;,</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegEnabled <span class="op">=</span> Field<span class="op">&lt;</span>U1<span class="op">,</span> U1<span class="op">,</span> U0<span class="op">&gt;;</span></span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegInterrupt <span class="op">=</span> Field<span class="op">&lt;</span>U1<span class="op">,</span> U2<span class="op">,</span> U1<span class="op">&gt;;</span></span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> RegKind <span class="op">=</span> Field<span class="op">&lt;</span>U3<span class="op">,</span> <span class="pp">op!</span>(U7 <span class="op">&lt;&lt;</span> U2)<span class="op">,</span> U2<span class="op">&gt;;</span></span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-12"><a href="#cb13-12" aria-hidden="true" tabindex="-1"></a><span class="kw">impl</span><span class="op">&lt;</span>Width<span class="op">:</span> Unsigned<span class="op">,</span> Mask<span class="op">:</span> Unsigned<span class="op">,</span> Offset<span class="op">:</span> Unsigned<span class="op">&gt;</span> Field<span class="op">&lt;</span>Width<span class="op">,</span> Mask<span class="op">,</span> Offset<span class="op">&gt;</span> <span class="op">{</span></span>
<span id="cb13-13"><a href="#cb13-13" aria-hidden="true" tabindex="-1"></a>    <span class="kw">const</span> <span class="kw">fn</span> new_checked<span class="op">&lt;</span>V<span class="op">:</span> Unsigned<span class="op">&gt;</span>() <span class="op">-&gt;</span> <span class="dt">Self</span></span>
<span id="cb13-14"><a href="#cb13-14" aria-hidden="true" tabindex="-1"></a>    <span class="kw">where</span></span>
<span id="cb13-15"><a href="#cb13-15" aria-hidden="true" tabindex="-1"></a>        V<span class="op">:</span> IsLessOrEqual<span class="op">&lt;</span><span class="pp">op!</span>((U1 <span class="op">&lt;&lt;</span> Width) <span class="op">-</span> U1)<span class="op">,</span> Output <span class="op">=</span> True<span class="op">&gt;,</span></span>
<span id="cb13-16"><a href="#cb13-16" aria-hidden="true" tabindex="-1"></a>    <span class="op">{</span></span>
<span id="cb13-17"><a href="#cb13-17" aria-hidden="true" tabindex="-1"></a>        Field <span class="op">{</span></span>
<span id="cb13-18"><a href="#cb13-18" aria-hidden="true" tabindex="-1"></a>            value<span class="op">:</span> (<span class="pp">V::</span>U8 <span class="op">&lt;&lt;</span> <span class="pp">Offset::</span>U8) <span class="op">&amp;</span> <span class="pp">Mask::</span>U8<span class="op">,</span></span>
<span id="cb13-19"><a href="#cb13-19" aria-hidden="true" tabindex="-1"></a>            _mask<span class="op">:</span> PhantomData<span class="op">,</span></span>
<span id="cb13-20"><a href="#cb13-20" aria-hidden="true" tabindex="-1"></a>            _offset<span class="op">:</span> PhantomData<span class="op">,</span></span>
<span id="cb13-21"><a href="#cb13-21" aria-hidden="true" tabindex="-1"></a>            _width<span class="op">:</span> PhantomData<span class="op">,</span></span>
<span id="cb13-22"><a href="#cb13-22" aria-hidden="true" tabindex="-1"></a>        <span class="op">}</span></span>
<span id="cb13-23"><a href="#cb13-23" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb13-24"><a href="#cb13-24" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>Only numbers which for which this property holds have an
implementation of this trait, so if we give a number which does not
fit, it’ll fail to compile. Take a look!</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> enable_register(<span class="op">&amp;</span><span class="kw">mut</span> reg) <span class="op">{</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>    reg<span class="op">.</span>update(<span class="pp">RegEnabled::new_checked::</span><span class="op">&lt;</span>U10<span class="op">&gt;</span>())<span class="op">;</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<pre><code>12 |     reg.update(RegEnabled::new_checked::&lt;U10&gt;());
   |                           ^^^^^^^^^^^^^^^^ expected struct `typenum::B0`, found struct `typenum::B1`
   |
   = note: expected type `typenum::B0`
           found type `typenum::B1`</code></pre>
<p>Now we’ve really done it. <code>new_checked</code> will result in the failure to
produce a program which has an errant too-high value for a field. So
our typo from before won’t blow up at runtime, as we would never have
gotten an artifact to run. Now we’re nearing the Peak Rust Vicinity
with regard to how safe we can make memory-mapped hardware
interactions.</p>
<p>However, what we wrote down way back in the first example in C was far
more succinct than the type parameter salad we ended up with. Is doing
such a thing even tractable when you’re talking about potentially
hundreds or even thousands of registers?</p>
<h1 id="safe-and-accessible">Safe and Accessible</h1>
<p>Earlier I called out calculating our masks by hand as being
problematic, but then all I really did was the same problematic
thing—albeit at the type-level. While the use of such an approach is
nice, getting to the point that we can actually write any code
requires quite a bit of boilerplate and manual transcription (I’m
talking about the type synonyms here).</p>
<p>We wanted something like the <a href="https://docs.rs/tock-registers/0.3.0/tock_registers/">TockOS mmio
registers</a>, but
one that would generate our typesafe implementations with the least
amount of manual transcription possible. What came out of this was a
macro which generates the necessary boilerplate to get a Tock-like API
plus our type-based bounds checking. To use it, you write down some
information about a register, its fields, their width and offsets, and
optional enum-like values should you want to give “meaning” to the
possible values a field can have.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="pp">register!</span> <span class="op">{</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>    <span class="co">// The register&#39;s name</span></span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a>    Status<span class="op">,</span></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a>    <span class="co">// The type which represents the whole register.</span></span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a>    <span class="dt">u8</span><span class="op">,</span></span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a>    <span class="co">// The register&#39;s mode, ReadOnly, ReadWrite, or WriteOnly.</span></span>
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a>    RW<span class="op">,</span></span>
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a>    <span class="co">// And the fields in this register.</span></span>
<span id="cb16-9"><a href="#cb16-9" aria-hidden="true" tabindex="-1"></a>    Fields [</span>
<span id="cb16-10"><a href="#cb16-10" aria-hidden="true" tabindex="-1"></a>        On    WIDTH(U1) OFFSET(U0)<span class="op">,</span></span>
<span id="cb16-11"><a href="#cb16-11" aria-hidden="true" tabindex="-1"></a>        Dead  WIDTH(U1) OFFSET(U1)<span class="op">,</span></span>
<span id="cb16-12"><a href="#cb16-12" aria-hidden="true" tabindex="-1"></a>        Color WIDTH(U3) OFFSET(U2) [</span>
<span id="cb16-13"><a href="#cb16-13" aria-hidden="true" tabindex="-1"></a>            Red    <span class="op">=</span> U1<span class="op">,</span></span>
<span id="cb16-14"><a href="#cb16-14" aria-hidden="true" tabindex="-1"></a>            Blue   <span class="op">=</span> U2<span class="op">,</span></span>
<span id="cb16-15"><a href="#cb16-15" aria-hidden="true" tabindex="-1"></a>            Green  <span class="op">=</span> U3<span class="op">,</span></span>
<span id="cb16-16"><a href="#cb16-16" aria-hidden="true" tabindex="-1"></a>            Yellow <span class="op">=</span> U4</span>
<span id="cb16-17"><a href="#cb16-17" aria-hidden="true" tabindex="-1"></a>        ]</span>
<span id="cb16-18"><a href="#cb16-18" aria-hidden="true" tabindex="-1"></a>    ]</span>
<span id="cb16-19"><a href="#cb16-19" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>From this, we generate register and field types like the final example
above where the indices—the <code>Width</code>, <code>Mask</code>, and <code>Offset</code>—are derived
from the values input in the <code>WIDTH</code> and <code>OFFSET</code> sections of a
field’s definitions. Also, notice that the numbers here are all
<code>typenums</code>; they’re going to go directly into our <code>Field</code> definitions!</p>
<p>The generated code namespaces registers and their fields through the
name given for the register and the fields, something like this:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="kw">mod</span> Status <span class="op">{</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>    <span class="kw">struct</span> Register(<span class="dt">u8</span>)<span class="op">;</span></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a>    <span class="kw">mod</span> On <span class="op">{</span></span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a>        <span class="kw">struct</span> Field<span class="op">;</span> <span class="co">// There is of course more to this definition</span></span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a>    <span class="kw">mod</span> Dead <span class="op">{</span></span>
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a>        <span class="kw">struct</span> Field<span class="op">;</span></span>
<span id="cb17-8"><a href="#cb17-8" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb17-9"><a href="#cb17-9" aria-hidden="true" tabindex="-1"></a>    <span class="kw">mod</span> Color <span class="op">{</span></span>
<span id="cb17-10"><a href="#cb17-10" aria-hidden="true" tabindex="-1"></a>        <span class="kw">struct</span> Field<span class="op">;</span></span>
<span id="cb17-11"><a href="#cb17-11" aria-hidden="true" tabindex="-1"></a>        <span class="kw">pub</span> <span class="kw">const</span> Red<span class="op">:</span> Field <span class="op">=</span> <span class="pp">Field::</span><span class="op">&lt;</span>U1<span class="op">&gt;</span>new()<span class="op">;</span></span>
<span id="cb17-12"><a href="#cb17-12" aria-hidden="true" tabindex="-1"></a>        <span class="co">// &amp;c.</span></span>
<span id="cb17-13"><a href="#cb17-13" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb17-14"><a href="#cb17-14" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>The generated API contains the nominally expected read and write
primitives to get at the raw register value, but it also has ways to
get a single field’s value, do collective actions, and find out if any
or all of a collection of bits are set. You can read the documentation
on the <a href="https://github.com/auxoncorp/bounded-registers#the-register-api">complete generated API
here</a>.</p>
<h1 id="kicking-the-tires">Kicking the Tires</h1>
<p>So what does it look like to actually use these definitions for a real
device? Is code going to be littered with type parameters, obscuring
any real logic from view?</p>
<p>No! By using type synonyms and type inference, we effectively never
have to think about the type-level part of the program at all. We get
to interact with the hardware in a straightforward way and get those
bounds-related assurances automatically.</p>
<p>Here’s an example of a UART register block. We’ll skip the declaration
of the registers themselves, as that would be too much to include
here. Instead, we’ll start with a register “block” then help the
compiler know how to look these registers up from a pointer to the
head of the block. We do that by implementing <code>Deref</code> and <code>DerefMut</code>.</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span>repr<span class="at">(</span>C<span class="at">)]</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="kw">pub</span> <span class="kw">struct</span> UartBlock <span class="op">{</span></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>    rx<span class="op">:</span> <span class="pp">UartRX::</span>Register<span class="op">,</span></span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a>    _padding1<span class="op">:</span> [<span class="dt">u32</span><span class="op">;</span> <span class="dv">15</span>]<span class="op">,</span></span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a>    tx<span class="op">:</span> <span class="pp">UartTX::</span>Register<span class="op">,</span></span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a>    _padding2<span class="op">:</span> [<span class="dt">u32</span><span class="op">;</span> <span class="dv">15</span>]<span class="op">,</span></span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a>    control1<span class="op">:</span> <span class="pp">UartControl1::</span>Register<span class="op">,</span></span>
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a><span class="kw">pub</span> <span class="kw">struct</span> Regs <span class="op">{</span></span>
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a>    addr<span class="op">:</span> <span class="dt">usize</span><span class="op">,</span></span>
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-14"><a href="#cb18-14" aria-hidden="true" tabindex="-1"></a><span class="kw">impl</span> <span class="bu">Deref</span> <span class="cf">for</span> Regs <span class="op">{</span></span>
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a>    <span class="kw">type</span> Target <span class="op">=</span> UartBlock<span class="op">;</span></span>
<span id="cb18-16"><a href="#cb18-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-17"><a href="#cb18-17" aria-hidden="true" tabindex="-1"></a>    <span class="kw">fn</span> deref(<span class="op">&amp;</span><span class="kw">self</span>) <span class="op">-&gt;</span> <span class="op">&amp;</span>UartBlock <span class="op">{</span></span>
<span id="cb18-18"><a href="#cb18-18" aria-hidden="true" tabindex="-1"></a>        <span class="kw">unsafe</span> <span class="op">{</span> <span class="op">&amp;*</span>(<span class="kw">self</span><span class="op">.</span>addr <span class="kw">as</span> <span class="op">*</span><span class="kw">const</span> UartBlock) <span class="op">}</span></span>
<span id="cb18-19"><a href="#cb18-19" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb18-20"><a href="#cb18-20" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb18-21"><a href="#cb18-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a><span class="kw">impl</span> <span class="bu">DerefMut</span> <span class="cf">for</span> Regs <span class="op">{</span></span>
<span id="cb18-23"><a href="#cb18-23" aria-hidden="true" tabindex="-1"></a>    <span class="kw">fn</span> deref_mut(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span>) <span class="op">-&gt;</span> <span class="op">&amp;</span><span class="kw">mut</span> UartBlock <span class="op">{</span></span>
<span id="cb18-24"><a href="#cb18-24" aria-hidden="true" tabindex="-1"></a>        <span class="kw">unsafe</span> <span class="op">{</span> <span class="op">&amp;</span><span class="kw">mut</span> <span class="op">*</span>(<span class="kw">self</span><span class="op">.</span>addr <span class="kw">as</span> <span class="op">*</span><span class="kw">mut</span> UartBlock) <span class="op">}</span></span>
<span id="cb18-25"><a href="#cb18-25" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb18-26"><a href="#cb18-26" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>Once this is in place, you’ll find that the use of these registers is
as simple as <code>read()</code> and <code>modify()</code>.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> main() <span class="op">{</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>    <span class="co">// A pretend register block.</span></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a>    <span class="kw">let</span> <span class="kw">mut</span> x <span class="op">=</span> [<span class="dv">0_u32</span><span class="op">;</span> <span class="dv">33</span>]<span class="op">;</span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a>    <span class="kw">let</span> <span class="kw">mut</span> regs <span class="op">=</span> Regs <span class="op">{</span></span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a>        <span class="co">// Some shenanigans to get at `x` as though it were a</span></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a>        <span class="co">// pointer. Normally you&#39;d be given some address like</span></span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a>        <span class="co">// `0xDEADBEEF` over which you&#39;d instantiate a `Regs`.</span></span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a>        addr<span class="op">:</span> <span class="op">&amp;</span><span class="kw">mut</span> x <span class="kw">as</span> <span class="op">*</span><span class="kw">mut</span> [<span class="dt">u32</span><span class="op">;</span> <span class="dv">33</span>] <span class="kw">as</span> <span class="dt">usize</span><span class="op">,</span></span>
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true" tabindex="-1"></a>    <span class="op">};</span></span>
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true" tabindex="-1"></a>    <span class="pp">assert_eq!</span>(regs<span class="op">.</span>rx<span class="op">.</span>read()<span class="op">,</span> <span class="dv">0</span>)<span class="op">;</span></span>
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-14"><a href="#cb19-14" aria-hidden="true" tabindex="-1"></a>    regs<span class="op">.</span>control1</span>
<span id="cb19-15"><a href="#cb19-15" aria-hidden="true" tabindex="-1"></a>        <span class="op">.</span>modify(<span class="pp">UartControl1::Enable::</span>Set <span class="op">+</span> <span class="pp">UartControl1::RecvReadyInterrupt::</span>Set)<span class="op">;</span></span>
<span id="cb19-16"><a href="#cb19-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-17"><a href="#cb19-17" aria-hidden="true" tabindex="-1"></a>    <span class="co">// The first bit and the 10th bit should be set.</span></span>
<span id="cb19-18"><a href="#cb19-18" aria-hidden="true" tabindex="-1"></a>    <span class="pp">assert_eq!</span>(regs<span class="op">.</span>control1<span class="op">.</span>read()<span class="op">,</span> <span class="dv">0b_10_0000_0001</span>)<span class="op">;</span></span>
<span id="cb19-19"><a href="#cb19-19" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>For fields whose values we don’t know ahead of time, we can build
one<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"><code>get_field</code> looks a little weird. I’m looking at the <code>Field::Read</code>
part, specifically. <code>Field</code> is a type, and we need an instance of that
type to pass to <code>get_field</code>. A cleaner API maybe something like:<br />
<br />
But remember that Field is a type synonym which has fixed indices for
width, offset, &amp;c. To be able to parameterize get_field like this,
we’d need higher-kinded types!<br />
<br />
</span></span>. I’m using unwrap here, but in a real program with unknown inputs,
you’d probably want to check that you got a <code>Some</code> back from that new
call<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">Technically, a read from a register field is by definition
only going to give you a value within the prescribed bounds, but
we don’t live in a pure world and you never know what’s going to
happen when external systems come into play. We’re at the behest
of the Hardware Gods here, so instead of forcing you into a “might
panic” situation, we give you back the <code>Option</code> to handle a This
Should Never Happen case.<br />
<br />
</span></span>.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> main() <span class="op">{</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>    <span class="co">// A pretend register block.</span></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>    <span class="kw">let</span> <span class="kw">mut</span> x <span class="op">=</span> [<span class="dv">0_u32</span><span class="op">;</span> <span class="dv">33</span>]<span class="op">;</span></span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a>    <span class="kw">let</span> <span class="kw">mut</span> regs <span class="op">=</span> Regs <span class="op">{</span></span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a>        <span class="co">// Some shenanigans to get at `x` as though it were a</span></span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a>        <span class="co">// pointer. Normally you&#39;d be given some address like</span></span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a>        <span class="co">// `0xDEADBEEF` over which you&#39;d instantiate a `Regs`.</span></span>
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a>        addr<span class="op">:</span> <span class="op">&amp;</span><span class="kw">mut</span> x <span class="kw">as</span> <span class="op">*</span><span class="kw">mut</span> [<span class="dt">u32</span><span class="op">;</span> <span class="dv">33</span>] <span class="kw">as</span> <span class="dt">usize</span><span class="op">,</span></span>
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true" tabindex="-1"></a>    <span class="op">};</span></span>
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-12"><a href="#cb20-12" aria-hidden="true" tabindex="-1"></a>    <span class="kw">let</span> input <span class="op">=</span> regs<span class="op">.</span>rx<span class="op">.</span>get_field(<span class="pp">UartRX::Data::Field::</span><span class="bu">Read</span>)<span class="op">.</span>unwrap()<span class="op">;</span></span>
<span id="cb20-13"><a href="#cb20-13" aria-hidden="true" tabindex="-1"></a>    regs<span class="op">.</span>tx<span class="op">.</span>modify(<span class="pp">UartTX::Data::Field::</span>new(input)<span class="op">.</span>unwrap())<span class="op">;</span></span>
<span id="cb20-14"><a href="#cb20-14" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<h1 id="decoding-failure">Decoding Failure</h1>
<p>Depending on your personal pain threshold you may have noticed that
the errors are nearly unintelligible. Let’s look at a not-so-subtle
reminder of what I’m talking about:</p>
<pre><code>error[E0271]: type mismatch resolving `&lt;typenum::UInt&lt;typenum::UInt&lt;typenum::UInt&lt;typenum::UInt&lt;typenum::UInt&lt;typenum::UTerm, typenum::B1&gt;, typenum::B0&gt;, typenum::B1&gt;, typenum::B0&gt;, typenum::B0&gt; as typenum::IsLessOrEqual&lt;typenum::UInt&lt;typenum::UInt&lt;typenum::UInt&lt;typenum::UInt&lt;typenum::UTerm, typenum::B1&gt;, typenum::B0&gt;, typenum::B1&gt;, typenum::B0&gt;&gt;&gt;::Output == typenum::B1`
  --&gt; src/main.rs:12:5
   |
12 |     less_than_ten::&lt;U20&gt;();
   |     ^^^^^^^^^^^^^^^^^^^^ expected struct `typenum::B0`, found struct `typenum::B1`
   |
   = note: expected type `typenum::B0`
       found type `typenum::B1`</code></pre>
<p>The expected <code>typenum::B0</code> found <code>typenum::B1</code> part kind of makes
sense, but what on Earth is the <code>typenum::UInt&lt;typenum::UInt, typenum::UInt</code>… nonsense? Well, <code>typenum</code> represents numbers as
binary cons cells! Errors like this make it hard, especially when you
have several of these type-level numbers confined to tight quarters,
to know which number it’s even talking about. Unless, of course, it’s
second nature for you to translate baroque binary representations to
decimal ones.</p>
<p>After the <code>U100th</code> time attempting to decipher any meaning from this
mess, a teammate got Mad As Hell And Wasn’t Going To Take It Anymore
and made a little utility,
<a href="https://github.com/auxoncorp/tnfilt"><code>tnfilt</code></a>, to parse the meaning
out from the misery that is namespaced binary cons cells. <code>tnfilt</code>
takes the cons cell-style notation and replaces it with sensible
decimal numbers. So, along with bounded-registers we’d like to also
share <code>tnfilt</code>. You use it like this:</p>
<pre><code>$ cargo build 2&gt;&amp;1 | tnfilt</code></pre>
<p>It transforms the output above into something like this:</p>
<pre><code>error[E0271]: type mismatch resolving `&lt;U20 as typenum::IsLessOrEqual&lt;U10&gt;&gt;::Output == typenum::B1`</code></pre>
<p>Now that makes sense!</p>
<h1 id="in-conclusion">In Conclusion</h1>
<p>Memory-mapped registers are used ubiquitously when interacting with
hardware from our software, and there are myriad ways in which to
portray those interactions, each of which has a different place on the
spectra of ease-of-use and safety. With <code>bounded-registers</code>, we
started by locking ourselves in place right at the edge of the
more-safe side of that safety spectrum and then tried to figure out
how to move the ease-of-use slider closer to the easy end. From those
ambitions <code>bounded-registers</code> was born, and I hope you’ll keep it in
mind any time you encounter memory-mapped devices in you adventures.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/programming/index.html" term="programming" label="programming"/>
    
    <category scheme="https://dpitt.me/tags/rust/index.html" term="rust" label="rust"/>
    
    <category scheme="https://dpitt.me/tags/formal methods/index.html" term="formal methods" label="formal methods"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Type Theory for The Working Rustacean</title>
    <link href="https://dpitt.me/forest/dsp-0033.html" rel="alternate" type="text/html"/>
    <published>Sat, 19 Oct 2019 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>Rust really hits a sweet spot with respect to programming languages on
account of a) its usefulness when working at a low level, coupled with
b) its style of type system. Because of a), Rust can be—and is—used
in places which tend to safety-critical: cyber-physical systems,
autonomous vehicle infrastructure, robotics, etc. When building
systems for these safety-critical environments, one also often
formally proves properties about their software. That’s where b) comes
in.</p>
<p>Rust’s type system is borne from the same ilk as those used in proof
assistants like Agda, Coq, or Lean. Because of this, we can use Rust’s
type system in similar ways we’d use a proof assistant to produce
safer and more correct programs. This is envisioned by reducing the
language of these disparate systems into the lingua franca of type
theory.</p>
<p>This talk will explore using (and abusing) Rust’s type system to mimic
the proofs one writes about their Rust programs while also enumerating
how this mimicry is derived from common ground in the worlds of types
or categories.</p>
<ul>
<li><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=5b1cd1dddfb7c374e9271199a08913af">Playground link to the Rust code used in the
talk</a></li>
<li><a href="https://dpitt.me/files/tt-for-rust.pdf">slides</a></li>
<li><a href="https://youtu.be/BdXWlQsd7RI">video</a></li>
</ul> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/talk/index.html" term="talk" label="talk"/>
    
    <category scheme="https://dpitt.me/tags/type theory/index.html" term="type theory" label="type theory"/>
    
    <category scheme="https://dpitt.me/tags/rust/index.html" term="rust" label="rust"/>
    
    
  </entry>
  
  <entry>
    <title type="html">A Quiver of Telescopes</title>
    <link href="https://dpitt.me/forest/dsp-0013.html" rel="alternate" type="text/html"/>
    <published>Mon, 16 Sep 2019 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>A few years ago, as my first foray into philosophy as a Real Adult, I
attempt to power my way through Deleuze and Guattari’s <em>AntiOedipus:
Capitalism and Schizophrenia</em>. It didn’t go particularly well. I think
I understood that there was something revolutionary about it, about
escaping oppressive hierarchies imposed on us by the Oedipal Triangle,
but the lyrical language can be, at times, nearly impossible to
follow. I gave up about half-way through.</p>
<p>Last week, though, I went on tirade during a phone call with a friend
about the aforementioned pair’s <em>What’s Philosophy</em>, and the
juxtaposition of philosophy to aesthetics. To them, philosophy is the
aesthetics of <em>concepts</em>. Because I’m entitled to do so, I contributed
my interpretation of this notion: that philosophy doesn’t tell us what
Is, or Should Be, but instead gives us lenses with which to view what
Has, Will, or Could. Philosophy is a <strong>quiver of telescopes</strong> that we
can use to view the world, discourse, existence, &amp;c.; swapping them
out according to need, and adding cylinders to the ones we use most in
hopes to further their efficacy.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/philosophy/index.html" term="philosophy" label="philosophy"/>
    
    
  </entry>
  
  <entry>
    <title type="html">A Week of ECM</title>
    <link href="https://dpitt.me/forest/dsp-0012.html" rel="alternate" type="text/html"/>
    <published>Tue, 28 May 2019 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>After a week of listening pretty much exclusively to American
Football’s
<a href="https://open.spotify.com/album/7ki5b310cwDVVJBevBLwdw?si%253DjatKDZjtQgaT45nkJGHNHw"><em>LP3</em></a>
I recalled seeing in a review of <em>LP2</em> the mentioning of their musical
kinship, or maybe better put, descendance, from Steve Reich. I pulled
up Reich’s compilation, <em>The ECM Recordings</em> and was immediately
immersed into <em>Music For 18 Musicians</em> for my walk from the bus to the
office.</p>
<h2 id="monday-it-was-actually-tuesday">Monday (It was actually Tuesday)</h2>
<p>As I’d mentioned, I started the week with Reich’s <em>Music for 18
Musicians</em>, and by way of serendipity, I found my brain freed from the
lines of code waiting to be reviewed to be acutely attuned to the
music in my ears right as it reached the 33-minute mark. A
Cabernet-like full body swell fills in the minuscule gaps between the
percussive 16th notes looping over one another and <em>Music For 18
Musicians</em> becomes beautiful all at once.</p>
<h2 id="wednesday-where-wednesday-includes-tuesday-night">Wednesday (where “Wednesday” includes Tuesday night)</h2>
<p>Okay, so, a few years ago I learned about Meredith Monk’s <em>Songs of
Ascension</em> and about how it was performed. <a href="https://www.youtube.com/watch?v%3Dc3mSVR3xtfU">Monk and her Vocal
Ensemble occupied the spirals of Ann Hamilton’s Tower’s
staircase</a> and
literally ascended the stairs while performing the music of
<em>Songs</em>. To watch it is to plot its beauty on all its axes, however,
when only listening, one is left with merely a projection of its
intention, an abstraction of its possibility.</p>
<p>It’s funny, now that I’ve watched the video again, I feel silly
recapitulating my notes from my listening to this earlier this
week. But nevertheless, that <strong>is</strong> why I’m doing this. I found that,
without context, Monk’s music is hard to countenance. Her “vocal
explorations” really push the boundary on what I can take
seriously. Unlike <a href="https://www.youtube.com/watch?v%3D75WFTHpOw8Y">Björk, for instance, who is course not beyond
reproach in terms of
seriousness-taking</a>
whose foray into vocal exploration with <em>Medulla</em> is quite palatable,
insofar as any other Björk record is at least. Monk’s and Björk’s
angles are, quite different, though. Monk is pushing the voice’s
purpose in a piece of music while Björk’s angle is more
paradoxical—hers is to hide it by making it serve <em>all</em> purposes.</p>
<p><em>Mapping</em>, <em>Songs</em>’ sixth track, presents with
Monk’s whimsical yelps and Xenakis-like percussion. It’s moments like
this that this line for me, that of seriousness-taking—which,
obviously, I’ve up to now been unable to give a proper title—is its
most tenuous. It’s a line drawn by our experience with art, by our
<em>context</em>, like, well, most anything else, really. When pondering the
effects of this absence of context, I find myself imagining its
opposite, i.e. to have <em>all</em> the context, about everything. Maybe this
is the distinction between a binary omniscience and something more
fluid, say, <em>enlightenment</em>. To know all is to never learn again,
the death of creativity. I’d posit that Epicureans would
say that this contextual evolution ought to be our <em>raison d’être</em> but
I think it’s more visceral than that; I’d say it how we know we’re
alive at all.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/music/index.html" term="music" label="music"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Proof Theory Impressionism: Blurring the Curry-Howard Line</title>
    <link href="https://dpitt.me/forest/dsp-0030.html" rel="alternate" type="text/html"/>
    <published>Sun, 17 Mar 2019 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>The Curry-Howard Correspondence is the observation that there exists a
correspondence between objects present in disparate formal
systems. Once such instance of this correspondence is the conspicuous
propositions-as-types notion, which draws correspondences between
logical propositions and types in a programming language.</p>
<p>As another more specific instance, one could consider a program
written in a traditional programming language like Rust, and its
formal proof exercised in a language like Coq.</p>
<p>While this is a beautiful revelation in theory, in practice this line
begins to look more like an impenetrable wall as it divides proof from
program. This chasm prevents assurances that, beyond those that can
simply be “observed” by a human, an implementation faithfully abides
its proofs. The conventional solution to this problem is code
extraction. However, in cases like safety-critical system software
(e.g. avionics, medical devices, &amp; autonomous vehicles), the languages
and platforms targeted by extraction tools simply aren’t an
option. What if there was another way?</p>
<p>This talk explores some potential approaches to endow our production
language, mostly Rust in this case (w/ a dash of Haskell), with the
capabilities present in “provable” systems. These explorations will
include spaces like totality, type-level programming, and dependent
types. Ultimately drawing lines from the above explorations back to
how one might write such a thing in the language of a proof assistant.</p>
<ul>
<li><a href="https://dpitt.me/files/proof-theory-impressionism.pdf">slides</a></li>
<li><a href="https://youtu.be/jrVPB-Ad5Gc">video</a></li>
</ul> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/talk/index.html" term="talk" label="talk"/>
    
    <category scheme="https://dpitt.me/tags/type theory/index.html" term="type theory" label="type theory"/>
    
    <category scheme="https://dpitt.me/tags/programming languages/index.html" term="programming languages" label="programming languages"/>
    
    <category scheme="https://dpitt.me/tags/rust/index.html" term="rust" label="rust"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Why Functional Programming: My Emotions Laid Bare</title>
    <link href="https://dpitt.me/forest/dsp-0011.html" rel="alternate" type="text/html"/>
    <published>Mon,  4 Jun 2018 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>A friend asks a harmless question and I ramble for 30 minutes.</p>
<h2 id="writing-prompt">Writing Prompt:</h2>
<p>People talk about functional programming in the same way people talk
about hallucinogens. like FP is “such a beautiful experience” and
“will totally expand your mind”. A friend just semi-jokingly told me
he’s</p>
<blockquote>
<p>gonna smoke some functional programming and play some jazz</p>
</blockquote>
<h2 id="your-response">Your Response:</h2>
<h3 id="why-functional-programming">Why Functional Programming?</h3>
<p>There’s a sentiment that functional programming folks have: With
functional programming you “discover” and not “invent”. While the
veracity of this notion is dubious, I think this purported beauty can
be explained as a byproduct of the processes we go through when
building a program in a language like Haskell. A large example of
this, by definition, would be denotational design, while a smaller one
would be something like <span class="math inline">\(\eta\)</span> reduction. The latter proposition,
i.e., “it will totally expand your mind” is the math bit. Abstraction
and generalization are totally lost in object-oriented land. Lawvere
and Awodey after him wanted to supplant set theory as the foundation
of mathematics with category theory and (a specific) type theory
respectively because they saw that there existed higher order
abstractions that corresponded to adjacent fields! When you’re doing
FP, you’re using the fruits of their labor, which you can’t say about
something as parochial as OO.</p>
<p>Also, it should be said, someone with less of an interest in the why
/ how does not necessarily preclude them from having the <code>:mindblown:</code>
experience when building practical things in a language like
Haskell. In fact, I see it happen all the time. However, I think in
the FP cabal, lots of folks arrive here precisely because they’re
interested in the origins of their tools.</p>
<h3 id="as-we-progress-things-get-more-tenuous">As we progress, things get more tenuous</h3>
<p>Personally, functional programming and its mathematical roots and
adjacencies are what spurred my interests in philosophy. These
interests are very focussed, and were precipitated by continuing to
ask, “but why?” which brought me all the way back to Frege / Kant /
Quine which seems to be an origination point for lots of cool
shit. Including linguistics, which is cool as hell. I finally have the
right prescription to where I can squint and see why there are so many
categorical analogues to computational linguistics.</p>
<h3 id="obligatory-david-foster-wallace-reference">Obligatory David Foster Wallace Reference</h3>
<p>There’s this amazing interview with DFW where he talks about “chasing
the click” and how he felt said click when doing formal logic, but
never like he did when writing fiction. To borrow his turn of phrase, with
FP (and its adjacencies), I get to “clicking like a fucking Geiger
counter”.</p>
<h3 id="a-conclusion-is-in-sight">A Conclusion Is In Sight</h3>
<p>Here’s what it is: It’s because functional programming is an end to
itself and not a means to one. You’re free to exercise your ridiculous
ideas like type-level state machines, “because you can”, not so you
can Make a Webapp For Business. And, at least Haskell, was borne out
of this ethos.</p>
<p>I’m overdoing it now, aren’t I?</p>
<p><em>Goes back to his corner of the lambda cube.</em></p> ]]>
    </content>

    
  </entry>
  
  <entry>
    <title type="html">On “The Strange and Twisted Life of Frankenstein”</title>
    <link href="https://dpitt.me/forest/dsp-0010.html" rel="alternate" type="text/html"/>
    <published>Thu, 22 Feb 2018 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>Cf. <a href="The%20New%20Yorker:%20The%20Strange%20and%20Twisted%20Life%20of%20Frankenstein">https://www.newyorker.com/magazine/2018/02/12/the-strange-and-twisted-life-of-frankenstein</a></p>
<blockquote>
<p>Among the many moral and political ambiguities of Shelley’s novel is
the question of whether Victor Frankenstein is to be blamed for
creating the monster — <strong>usurping the power of God, and of women</strong> —
or for failing to love, care for, and educate him.</p>
</blockquote>
<p>Emphasis mine.</p>
<p>In <em>The History of Western Philosophy</em>, Bertrand Russell’s analysis of
Frankenstein is a bore:</p>
<blockquote>
<p>Mary Shelley’s Frankenstein […] contains what might almost be
regarded as an allegorical prophetic history of the development of
Romanticism.</p>
</blockquote>
<p>This hygienic take leaves Shelley only capable of thinking within the
confines of her epoch — Romanticism’s box — and not as a human, as a
woman, in her own right. As Lepore points out, between the time of the
novel’s commencement and its publishing, Shelley was pregnant four
times and buried three of those four. By this account, Frankenstein’s
nameless monster can be none other than the hell that is loss, the
malignancy of an existence acutely felt by a woman reconciling a
triptych of identity as mother, wife, and artist.</p>
<p>Lepore also calls out The Monster’s ambivalence about his
epistemological journey, wherein he considers it a curse to have
learned to read, only to use that ability to read of his mistreatment
in Frankenstein’s journals. Like Derrida’s specter, neither he nor
Shelley can return to an existence prior to the knowledge—and the
pain that that knowledge causes—of catastrophic loss. This part of
Russell’s analysis I do like, as he plucks out The Monster’s monologue
as he stands over Frankenstein’s body:</p>
<blockquote>
<p>That also is my victim! in his murder my crimes are consummated; the
miserable genius of my being is wound to its close! Oh,
Frankenstein! generous and self-devoted being! What does it avail
that I now ask thee to pardon me? I, who irretrievably destroyed
thee by destroying all that thou lovedst. Alas! he is cold, he
cannot answer me. . . . When I run over the frightful catalogue of
my sins, I cannot believe that I am the same creature whose thoughts
were once filled with sublime and transcendent visions of the beauty
and the majesty of goodness. But it is even so; the fallen angel
becomes a malignant devil. Yet even that enemy of God and man had
friends and associates in his desolation; I am alone.</p>
</blockquote>
<p>Existence is solitary, wishful, and unforgiving.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/book/index.html" term="book" label="book"/>
    
    <category scheme="https://dpitt.me/tags/analysis/index.html" term="analysis" label="analysis"/>
    
    
  </entry>
  
  <entry>
    <title type="html">On Profunctors, Abruptly</title>
    <link href="https://dpitt.me/forest/dsp-0009.html" rel="alternate" type="text/html"/>
    <published>Thu, 15 Feb 2018 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <h2 id="introduction-theses">Introduction (Theses)</h2>
<p>If we’re comfortable with the notion of a functor in Haskell, we can
intuitively infer what might be meant by a <em>bifunctor</em>. Consider the
kind of a type that is a functor: <code>* -&gt; *</code>. That is, it takes <strong>one</strong>
type as an argument and returns a type. A bifunctor’s kind is one
which takes <strong>two</strong> types and returns a type: <code>* -&gt; * -&gt; *</code>. Its
definition looks like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> <span class="dt">Bifunctor</span> (<span class="ot">b ::</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span>) <span class="kw">where</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="ot">  bimap ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> (c <span class="ot">-&gt;</span> d) <span class="ot">-&gt;</span> f a c <span class="ot">-&gt;</span> f b d</span></code></pre></div>
<p>A bifunctor is useful for mapping over product types, such as <code>(,)</code>:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Bifunctor</span> (,) <span class="kw">where</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>  bimap f  g (x, y) <span class="ot">=</span> (f x, g y)</span></code></pre></div>
<p>Now is a good time to take note of the direction of the arrows. We
begin with an <code>f a b</code> and end up with a <code>f c d</code>, by way of two
functions, <code>a -&gt; b</code>, and <code>c -&gt; d</code>. In both of their cases, the arrows
go the same direction: forward.</p>
<ol type="1">
<li>The <code>a -&gt; b</code> follows the same path as the <code>f a _ -&gt; f b _</code> in the
bifunctor <code>f</code>.</li>
<li>Like the function before it, <code>c -&gt; d</code> follows the direction of the
functor as well <code>f _ c -&gt; f _ d</code>.</li>
</ol>
<h2 id="antitheses">Antitheses</h2>
<p>This direction-following is called <em>covariance</em>, and our Bifunctor is
called a <em>covariant</em> functor. However, like all good things, category
theory yields to the Hegelian Spirit and cannot achieve synthesis
without both a thesis and an antithesis<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"><a href="https://twitter.com/pittma_/status/933569372768972800">Here’s a Twitter
thread</a> from
when I discovered Lambek and Lawvere’s explorations of Heraclitus’s
and/or Hegel’s influence on mathematics.<br />
<br />
</span></span>. We have two
antitheses two derive, so let us begin with a regression as a vehicle
to the first.</p>
<h3 id="antithesis-category">Antithesis: Category</h3>
<p>In category theory, A category <span class="math inline">\(C\)</span> has an opposite category wherein
the objects are the same, but the arrows are reversed. <span class="math inline">\(C\)</span>’s opposite
category would be denoted as <span class="math inline">\(C^{op}\)</span>.</p>
<p>if this were C:</p>
<p><span class="math display">\[
\begin{xy}
\xymatrix{
  a \ar[r]^f \ar[d]_{g \circ f} &amp; b \ar[dl]^g \\
  c
}
\end{xy}
\]</span></p>
<p>Then this would be <span class="math inline">\(C^{op}\)</span>:</p>
<p><span class="math display">\[
\begin{xy}
\xymatrix{
  a &amp; b \ar[l]_{f^{op}} \\
  c \ar[u]^{(g \circ f)^{op}} \ar[ur]_{g^{op}}
}
\end{xy}
\]</span></p>
<p>On to the next.</p>
<h3 id="antithesis-covariant">Antithesis: Covariant</h3>
<p>When we define a functor in a categorical sense rather than a Haskell
sense, we define it as a mapping from one category <span class="math inline">\(C\)</span> to another <span class="math inline">\(D\)</span>,
whilst preserving structure: <span class="math inline">\(F : C \rightarrow D\)</span>. To say a functor
is covariant is to state that when a functor <span class="math inline">\(F\)</span>, for example,
represents morphisms on objects from <span class="math inline">\(C\)</span> in <span class="math inline">\(D\)</span>, those morphisms’
arrows’ must go the same direction that they did in <span class="math inline">\(C\)</span>. I.e., given
<span class="math inline">\(F : C \rightarrow D\)</span> and our <span class="math inline">\(C\)</span> defined above, when we take <span class="math inline">\(f\)</span> into
<span class="math inline">\(D\)</span> with <span class="math inline">\(F\)</span>, the arrows must respect their initial directions,
meaning the following statement must hold in <span class="math inline">\(D\)</span>:</p>
<p><span class="math display">\[
F(f) : F(a) \rightarrow F(b)
\]</span></p>
<p>Okay, now that we have a more formal definition of a covariant
functor, let’s use it a baseline to define its antithesis.
Covariant’s antithesis is called <em>contravariant</em>, which, you guessed
it, is a reversal of an arrow’s direction. That said, lets define a
new functor <span class="math inline">\(F&#39; : C \rightarrow D\)</span>, but note that <span class="math inline">\(F&#39;\)</span> is
<em>contravariant</em>, meaning that the following holds in <span class="math inline">\(D\)</span>.</p>
<p><span class="math display">\[
F&#39;(f) : F&#39;(b) \rightarrow F&#39;(a)
\]</span></p>
<h2 id="profunctors">Profunctors</h2>
<p>Having finally met this post’s desiderata, I’ll present a Haskell definition of a profunctor, initially without comment.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> <span class="dt">Profunctor</span> (<span class="ot">p ::</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span> <span class="ot">-&gt;</span> <span class="op">*</span>) <span class="kw">where</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="ot">  dimap ::</span> (a&#39; <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> b&#39;) <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> p a&#39; b&#39;</span></code></pre></div>
<p>Take it in. Ruminate. What do you see?</p>
<p>Here’s a more formal definition as a hint:</p>
<p><span class="math display">\[
P : C^{op} \times C \rightarrow D
\]</span></p>
<p>A profunctor is a mapping from a category <span class="math inline">\(C\)</span> and its opposite
<span class="math inline">\(C^{op}\)</span> to <span class="math inline">\(D\)</span>. To put this in more practical terms, a profunctor is
a bifunctor which is contravariant in one of its parameters. Let’s go
back for now to the Haskell definition above for an illustration. A
profunctor <code>p</code> starts with two functions, and a <code>p a b</code>, and ends up
with a <code>p a' b'</code>. The functions we’re given map from <code>a' -&gt; a</code> and <code>b -&gt; b'</code>, respectively. This tells us that <code>p</code>’s treatment of the first
function, the <code>a</code>’s, will be contravariant. But how is this possible?
How could we end up with a <code>a'</code> if that’s our starting point? Let’s
draw it out!</p>
<ol type="1">
<li><p>Our first function, which we’ll call <span class="math inline">\(f\)</span>, is <code>a' -&gt; a</code>:</p>
<p><span class="math display">\[
\begin{xy}
\xymatrix{
  a&#39; \ar[r]^f &amp; a
}
\end{xy}
\]</span></p></li>
<li><p>And our next function, <span class="math inline">\(g\)</span>, is from <code>b -&gt; b'</code>:</p>
<p><span class="math display">\[
\begin{xy}
\xymatrix{
  a&#39; \ar[r]^f &amp; a &amp; b \ar[dll]^g \\
  b&#39;
}
\end{xy}
\]</span></p></li>
<li><p>Now, for the sake of our explanation, let’s say our third parameter
<code>p a b</code> can be thought of as a more general way of writing the type
of a function from <code>a</code> to <code>b</code>. We can think about it this way by
replacing <code>p</code> with a concrete type constructor, the function type
constructor <code>(-&gt;)</code>, to be specific. In its case we’d write <code>p a b</code>
as <code>(-&gt;) a b</code> or, in its more common form, <code>a -&gt; b</code>. We’ll call
this function <span class="math inline">\(h\)</span>:</p>
<p><span class="math display">\[
\begin{xy}
\xymatrix{
  a&#39; \ar[r]^f &amp; a \ar[r]^h &amp; b \ar[dll]^g \\
  b&#39;
}
\end{xy}
\]</span></p></li>
<li><p>There’s two things I’d like to stop and make note of here.</p>
<ol type="1">
<li>If we can call our third parameter, <code>p a b</code> a more general way
of stating <code>a -&gt; b</code>, then the same applies to <code>dimap</code>’s return
type, <code>p a' b'</code>. It can be thought of as <code>a' -&gt; b'</code>.</li>
<li>Let’s look again at the diagram and use what we know about
category theory’s laws to discern what it would take to get a
return type that was shaped like <code>a' -&gt; b'</code>. It’s composition!
To return our <code>p a' b'</code>, or remember, our <code>a' -&gt; b'</code>, we just
need to compose <span class="math inline">\(f\)</span>, <span class="math inline">\(g\)</span>, and <span class="math inline">\(h\)</span> in the right order: <span class="math inline">\(g \circ h
\circ f\)</span>:</li>
</ol>
<p><span class="math display">\[
\begin{xy}
\xymatrix{
  a&#39; \ar[r]^f \ar[d]_{g \circ h \circ f} &amp; a \ar[r]^h &amp; b \ar[dll]^g \\
  b&#39;
}
\end{xy}
\]</span></p></li>
</ol>
<p>And with that, you can see what makes a profunctor tick!</p>
<h2 id="in-closing">In Closing</h2>
<p>This sums up the technical section of this this post, as its purpose
was to explain what, exactly, a profunctor was, while simultaneously
sharing a manner such that makes contravariance a tractable thing. It
was through this intuition that I personally was able to get a handle
on how to synthesize contravariance, and I hope it was helpful to
anyone who comes across this post.</p>
<h2 id="in-closing-closing">In Closing Closing</h2>
<p>Before I <code>:wq</code> on this one, though, I want to also make mention of the
structure of the final technical section of this post. Drawing out
the commutative diagrams one arrow at a time was huge in getting a
grasp on these concepts for me, and it’s an approach I’ll reach for
regularly from now on. If it’s not something you’ve tried before when
taking on something dense in the land of category theory, I can
certainly attest to its efficacy! Cheers!</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    
  </entry>
  
  <entry>
    <title type="html">The Hauntology of Functional Programming</title>
    <link href="https://dpitt.me/forest/dsp-0008.html" rel="alternate" type="text/html"/>
    <published>Sun, 27 Aug 2017 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>Jacques Derrida coined the term hauntology in his book <em>Specters of
Marx</em>. The term was birthed in a discussion of the notion that the
fall of the Soviet Union meant the death of communism. Derrida posited
however, that, “if communism has always been spectral, what does it
mean to say that it is now dead?” Derrida’s case was that the
post-marxist, capitalist world would forever be haunted by Marx’s
wraith.</p>
<p>His point was that it is irrelevant whether capitalism “won” with the
fall of the Soviet Union, because we as human beings can no longer
exist without knowing the possibilities of Marxism. This idea can be
abstracted into a more general case, expressed as “always-already”.
Always-already encompasses the phenomenon wherein one cannot “unknow”
a concept, that the knowing of such a concept demarcates a diametrical
<em>before</em> and <em>after</em> which will never exist contemporaneously. It’s a
punctuation in existence. Think of a favorite book which you compare
all books to: Going back to a time before you read this book is
impossible, you even compare books you read <em>before</em> this one to this
book. It becomes a scale on which you measure every literary
encounter and is heretofore deeply entangled in your
existence–there’s no going back.</p>
<p>Over the course of the last year, I’ve dipped my toes in the Haskell
pool while simultaneously exploring its algebraic roots in type and
category theory. It was the latest stop in my what I thought was an
interminable mission of curiosity. This trek’s penultimate stop was
an interest in compilers, which undoubtably paved the way to this
mathematical interest. I met with a compiler team at work and
expressed an interest in their work, and mentioned my studies: I’d
been given a book which was touted as the canonical text in learning
about compilers, which, of course, they told me was utterly
irrelevant. But the truth was and is, that “working on compilers” is
not what I was looking for; it was instead a misguided foray into
something much more general.</p>
<p>Fast-forward to a few months later, I’m sitting in an interview and
having an impassioned discussion about a futile attempt to formalize
something at work using Haskell’s <a href="https://www.haskell.org/arrows/"><code>Arrow</code>
typeclass</a>, and how, frankly, hard it
was to even get its requisites in order, namely,
<a href="https://www.haskell.org/arrows/"><code>Category</code></a>.</p>
<p>During the interview, my interviewer said to me (a paraphrasing), “I
was like you. That’s how I ended up here: I was on a constant search
for <em>rigor</em>.”</p>
<p>My head spun. He’d cut down to the brass tacks of my own milieu and
had a line of sight to a destination which hadn’t yet come into my
view. I’ve been haunted by the specter of rigor without knowing its
name.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/self/index.html" term="self" label="self"/>
    
    <category scheme="https://dpitt.me/tags/programming/index.html" term="programming" label="programming"/>
    
    <category scheme="https://dpitt.me/tags/philosophy/index.html" term="philosophy" label="philosophy"/>
    
    
  </entry>
  
  <entry>
    <title type="html">An Algebraic Data Type's Monad</title>
    <link href="https://dpitt.me/forest/dsp-0007.html" rel="alternate" type="text/html"/>
    <published>Wed,  2 Aug 2017 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>I watched Dr. Emily Riehl’s Compose Conf talk<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"><a href="https://www.youtube.com/watch?v=6t6bsWVOIzs">A Categorical View of Computational
Effects</a><br />
<br />
</span></span> last night and a
new intuition emerged. The purpose of the talk was to explicate the
categorical notion of a monad, and while watching it, I was struck
with the realization of how a monad arises from an algebraic data
type.</p>
<h2 id="prereqs">Prereqs</h2>
<p>In her talk, she first discusses <span class="math inline">\(T\)</span> as a computation, and defines a
<span class="math inline">\(T\)</span> as a monad: something that can take an <span class="math inline">\(A\)</span> and lift it to a
<span class="math inline">\(T(A)\)</span>, like this:</p>
<p><span class="math display">\[
A \rightarrow T(A)
\]</span></p>
<p><em>NB: There is another operation that comes with a monad, <code>bind</code>, but
we’ll skip that for now.</em></p>
<p>The canonical example used through the first half of the talk is a
<span class="math inline">\(\text{List}\)</span>: A function from <span class="math inline">\(A \rightarrow T(A)\)</span> could be a
function from an <span class="math inline">\(A\)</span> to a <span class="math inline">\(List\)</span> of <span class="math inline">\(A\)</span>’s where <span class="math inline">\(T\)</span> is the
<em>computation</em> which constructs a list of <span class="math inline">\(A\)</span>’s. That is, <span class="math inline">\(A
\rightarrow T(A)\)</span> is simply a more general version of <span class="math inline">\(A \rightarrow
List(A)\)</span>.</p>
<p>She goes on to use the notation of <span class="math inline">\(\leadsto\)</span> to denote a “program”
which contains one of these lift operations, but with <span class="math inline">\(\leadsto\)</span>, we
elide the <span class="math inline">\(T\)</span>:</p>
<p><span class="math display">\[
A \rightarrow T(B) = A \leadsto B
\]</span></p>
<p>This notation is meant to denote a “weak” map between <span class="math inline">\(A\)</span> and <span class="math inline">\(B\)</span>,
in that it’s not a complete <span class="math inline">\(A \rightarrow B\)</span>, due to the fact that
it requires the computation <span class="math inline">\(T\)</span>. This lift from <span class="math inline">\(A\)</span> to <span class="math inline">\(T\)</span> of
<span class="math inline">\(B\)</span> is called a <em>Kleisli</em> arrow.</p>
<h2 id="an-adt-monad">An ADT Monad</h2>
<p>Later in the talk, she defines a function from <span class="math inline">\(A\)</span> to <span class="math inline">\(A + \bot\)</span>:</p>
<p><span class="math display">\[
A \rightarrow A + \{\bot\}
\]</span></p>
<p>This should look familiar—it contains a <span class="math inline">\(+\)</span> after all! It’s an
algebraic data type (ADT)—a sum type to be specfic. It can give us
either an <span class="math inline">\(A\)</span> or <span class="math inline">\(\bot\)</span>; <span class="math inline">\(\bot\)</span> means “bottom” or <code>false</code> in
this context. It would look something like this in Haskell:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Maybe</span> a <span class="ot">=</span> <span class="dt">Nothing</span> <span class="op">|</span> <span class="dt">Just</span> a</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="ot">f ::</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</span></code></pre></div>
<p>And as we know, <code>Maybe</code> admits a monad where if we have an <code>a</code>, we
apply our lift to it to get a <code>Just a</code>. In Haskell, this lift is
called <code>return</code>, and made available in the Monad typeclass:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">return</span><span class="ot"> ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> m a</span></code></pre></div>
<p>Which, if we squint, looks an awful lot like <code>A -&gt; T(A)</code>. For
edification purposes, our definition of <code>return</code> for <code>Maybe</code> and the
other requisite pieces of a Monad in Haskell are below.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Monad</span> <span class="dt">Maybe</span> <span class="kw">where</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">return</span> <span class="ot">=</span> <span class="dt">Just</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>  (<span class="dt">Just</span> x) <span class="op">&gt;&gt;=</span> f <span class="ot">=</span> f x</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>  <span class="dt">Nothing</span> <span class="op">&gt;&gt;=</span> _ <span class="ot">=</span> <span class="dt">Nothing</span></span></code></pre></div>
<p>If you’re still squinting, you can start to see how:</p>
<ul>
<li>Our ADT becomes <span class="math inline">\(T\)</span>, the <em>computation</em> which can give us either
our <code>a</code>, or <code>Nothing</code>.</li>
<li>Using Dr. Riehl’s notation, we could denote <code>f</code> mathematically as
<span class="math inline">\(f : A \leadsto A\)</span></li>
</ul> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/math/index.html" term="math" label="math"/>
    
    <category scheme="https://dpitt.me/tags/category theory/index.html" term="category theory" label="category theory"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Homotopy Type Theory Desiderata</title>
    <link href="https://dpitt.me/forest/dsp-0006.html" rel="alternate" type="text/html"/>
    <published>Tue, 23 May 2017 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>This is a thing I do. I like to learn things by finding the thing I
want to learn, and then work my way down as I encounter unknowns,
exposing layers like an epistemological onion — a truth vegetable.</p>
<p>The current outermost layer is Homotopy Type Theory which is put forth
by <a href="https://homotopytypetheory.org/book/">the HoTT Book</a>. Per usual,
as I dig down beneath the <em>tunic</em> I’m reminded that, for all intents
and purposes, I know nothing and I spiral out of control wiki link
after wiki link, my only respite a term I actually understand. It
could be argued that this is called “learning”, but that label is
dubious.</p>
<h3 id="homotopy">Homotopy</h3>
<p>Digressions aside, Homotopy is, in my best attempt at lucidity, a
continuous function that can map between two functions, say <span class="math inline">\(f,g: A
\rightarrow B\)</span> mapping from the same space to another same space. If
this function exists, and we’ll call it <span class="math inline">\(H\)</span>, then those two maps are
homotopic. Formally, it looks like this:</p>
<p><span class="math display">\[
f, g: A \rightarrow B \\
H: A \times [0, 1] \rightarrow B \\
H(x, 0) = f(x) \\
H(x, 1) = g(x)
\]</span></p>
<p>To put this in English: We’ve convered the first line. That’s our two
maps: <span class="math inline">\(f\)</span> and <span class="math inline">\(g\)</span>. The second line is illustrating the what <span class="math inline">\(H\)</span>
maps: the <em>product</em> of <span class="math inline">\(X\)</span> and the unit interval to <span class="math inline">\(B\)</span>. A product
in this case means <em>and</em>. As a quick explanation of product in this
context, we can refer to some Haskell:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- This is a sum type, i.e. an or. Meaning a Maybe&#39; can be either a Just&#39; a or</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="co">-- Nothing.</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Maybe&#39;</span> a <span class="ot">=</span> <span class="dt">Just&#39;</span> a <span class="op">|</span> <span class="dt">Nothing&#39;</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co">-- While this is a product, an and. The tuple is the product of something of</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co">-- type a and something of type b.</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Tuple&#39;</span> a b <span class="ot">=</span> (a, b)</span></code></pre></div>
<p>The unit interval however, is another scale to the onion.</p>
<p>The unit interval is the set of all the real numbers between <span class="math inline">\(0\)</span> and
<span class="math inline">\(1\)</span>. We use the bounds of this interval to be equal to our two maps
given some <span class="math inline">\(x\)</span>. This means that our <span class="math inline">\(H\)</span>, our homotopy, is a
function which through a transfinite series of inputs can “bend” <span class="math inline">\(f\)</span>
into <span class="math inline">\(g\)</span>.</p>
<p>That, in a nutshell is homotopy. But, as I delved deeper, like
anything worth learning, that nascent intuition was simply a
prerequisite.</p>
<h3 id="univalence">Univalence</h3>
<p><strong>Univalence Axiom</strong>: <span class="math inline">\((A = B) \approx (A \approx B)\)</span>.</p>
<p>The book says this about Univalence:</p>
<p>It goes on to say that Univalence is informally an equivalence, and
more specifcally <em>homotopy equivalence</em>. Homotopy equivalence is given
by an isomorphism between two spaces where the following holds:</p>
<p>Given spaces <span class="math inline">\(X\)</span> and <span class="math inline">\(Y\)</span> and maps <span class="math inline">\(f: X \rightarrow; Y\)</span> and <span class="math inline">\(g:
Y \rightarrow X\)</span>, the composition <span class="math inline">\(f \circ g\)</span> is homotopic to
<span class="math inline">\(Id_X\)</span> and the composition <span class="math inline">\(g \circ f\)</span> is homotopic to <span class="math inline">\(Id_Y\)</span>.</p>
<p>To put this is another way, and how I actually wrote this down in my
notebook when I was searching for the intuition: Univalence is a
topological bijection and that bijection is a valid isomorphism in the
domain of pure topology. There’s a inside joke about topologists:
Toplogists can’t tell the difference between a donut and a coffee mug.
This is precisely why! The torus (donut) is homotopy equivalent,
i.e. isomorphic, to a coffee mug.</p>
<p>So, when we talk about univalence in the context of Homotopy, we’re
talking about a form of equality, an isomorphism.</p>
<p>This concludes the initial desiderata for grappling with Homotopy Type
Theory, or at least its introduction. There’s more to come, for the
both of us.</p> ]]>
    </content>

    
  </entry>
  
  <entry>
    <title type="html">Notes on existing as a leaf in a world of cars</title>
    <link href="https://dpitt.me/forest/dsp-0005.html" rel="alternate" type="text/html"/>
    <published>Wed, 12 Oct 2016 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>You’re driving.</p>
<p>A leaf falls from a tree near the side of the road.</p>
<p>You’re uniquely positioned for insight into its path as it cascades
down and collides with your windshield. It slides up and to the
left. Out of view. There are no tangible after-effects on you or the
vehicle. You drive on, unaffected, unaware of the change of course
you’ve just abruptly imposed.</p>
<p>I am that leaf.</p>
<p>We are that leaf.</p>
<p>Existing in a world of cars.</p>
<p>Be kind.</p> ]]>
    </content>

    
  </entry>
  
  <entry>
    <title type="html">On Descartes and Microservices</title>
    <link href="https://dpitt.me/forest/dsp-0004.html" rel="alternate" type="text/html"/>
    <published>Fri,  6 May 2016 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>And other annoying shit I’ve just made up.</p>
<p>This post is about dependency injection, a decoupling technique used
by programmers often with wanton abandon.</p>
<p>I’m not going to apologize for the above non-sequitur, but rather to
attempt to contextualize it. To begin, let’s talk about Descartes:</p>
<h3 id="rené-descartes">René Descartes</h3>
<p>Descartes is responsible for what is probably the penultimate
existential statement, eclipsed only by Shakespeare’s <em>“to be or not
to be”</em>. Descartes’s goes like this:</p>
<blockquote>
<p>Cogito ergo sum</p>
</blockquote>
<p>Or</p>
<blockquote>
<p>I think, therefore I am.</p>
</blockquote>
<p>Descartes’s whole spiel was I can’t doubt that I exist, because there
must be an “I” to doubt “I”. That is, doubting my existence is
dependent on me existing and then doubting that I exist. An
interesting strange loop indeed.</p>
<h3 id="computers">Computers</h3>
<p>I recently found myself in an analogous existential argument about
when a component in a program commenced to exist.</p>
<p>This component is <em>dependent</em> on another, and my coworker was of the
school of thought that this component’s constructor should be
<em>decoupled</em> from the injection of its dependency. Something like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">func</span> main<span class="op">()</span> <span class="op">{</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>	t1 <span class="op">:=</span> thing1<span class="op">.</span>New<span class="op">()</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>	t2 <span class="op">:=</span> thing2<span class="op">.</span>New<span class="op">()</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>	t1<span class="op">.</span>BindThing2<span class="op">(</span>t2<span class="op">)</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>However, <em>I</em> believed that there was no reason for <code>t1</code> to even be
constructed if it there was not yet a t2 to meet its dependency. Like
this:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">func</span> main<span class="op">()</span> <span class="op">{</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>	t2 <span class="op">:=</span> thing2<span class="op">.</span>New<span class="op">()</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>	t1 <span class="op">:=</span> thing1<span class="op">.</span>New<span class="op">(</span>thing2<span class="op">)</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>My coworker had a compelling argument. For us to reach optimal
clarity on his argument, we first must be <em>live</em>. That is,
<strong>consistent</strong> on our understanding of a few things, so I must
digress.</p>
<p>—- Begin Digression —-</p>
<h3 id="microservices">Microservices</h3>
<p>A overloaded term which, to me, means Domain Driven Design, where each
“domain” is composed of subdomains (read: microservices) of
functionality.</p>
<p>For example, you have a group of services which mutate <code>Foo</code>s, and a
group of services which mutate <code>Bar</code>s. Each of these “domains” have
independent services which have discrete responsibilities such as
<code>paint-bar</code> or <code>flip-foo</code>. A service in the <code>Foo</code> domain has no
knowledge of a <code>Bar</code> whatsoever. Ideally not even through a
dependency like an ORM with both <code>Foo</code> and <code>Bar</code> declarations within
it. This complete, physical delineation protects each domain from the
other’s faults.</p>
<h3 id="the-go-programming-language">The Go Programming Language</h3>
<p>An interesting thing about the design of the Go programming language
is that the language itself gently leads you down the path towards
something microservice-like even though your software may exist
completely in a single runtime. This is done through goroutines,
channels, and long-running loops. With Go, you find yourself writing a
distributed system, passing messages back and forth between disparate,
long-running, reactive loops all within a single runtime. It’s
important to consider this mindset when considering my coworker’s
argument.</p>
<p>—- End Digression —-</p>
<p>So, given this context, when does a service commence existence? Is it
at instantiation? Or, when it becomes <em>useful</em>, with all its
subsystems in line and ready to take on the world?</p>
<p>What do you think?</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/programming/index.html" term="programming" label="programming"/>
    
    <category scheme="https://dpitt.me/tags/philosophy/index.html" term="philosophy" label="philosophy"/>
    
    
  </entry>
  
  <entry>
    <title type="html">CRDTs, Bounds, SyncFree, and Invariants</title>
    <link href="https://dpitt.me/forest/dsp-0003.html" rel="alternate" type="text/html"/>
    <published>Mon,  2 Feb 2015 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>I finally got around to watching the SyncFree consortium’s <a href="https://www.youtube.com/watch?v=1KP_pxFhlVU">talk about
their work on CRDTs</a> at
RICON 2014. Or at least half of it, rather, and I was really struck
by their Bounded CRDTs<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">http://christophermeiklejohn.com/crdt/2014/07/22/readings-in-crdts.html<br />
<br />
</span></span> idea. The idea of a bounded CRDT is
this: using a predefined <em>invariant</em>, you allow a certain count of
“interactions” per replica. They are likened to leases or
reservations in the talk. I like reservations better:</p>
<p><img src="https://dl.dropboxusercontent.com/u/42154947/blog%20pics/ok.png" alt="Okay" />
(edit 2023: I don’t have these drawings anymore, sorry)</p>
<p>Here, in an over-simplified example, we have a counter. This counter
changes with increments of 1, i.e. <code>counter++</code> or <code>counter--</code>. We
have set an invariant of <code>cannot be &lt; 0</code>, and we use this invariant to
make a <em>guarantee</em>. If each replica can only make 1 operation against
the current known value (4), then we will never violate the invariant.
However, should a second operation be performed on any replica, we are
no longer convergent: the result of this would have counter’s value
equal to -1:</p>
<p><img src="https://dl.dropboxusercontent.com/u/42154947/blog%20pics/not%20ok.png" alt="Not Okay" />
(edit 2023: I don’t have these drawings anymore, sorry)</p>
<p>This is my primitive understanding, or my assumption, after watching
20 minutes of the RICON talk on this today. What I’m most interested
in is the bound that comes from the reservation count. This is the
third time I’ll bring this paper up on this blog, which I can accept
– maybe I’m a bit obsessed. The <a href="http://www.bailis.org/papers/ca-vldb2015.pdf">Peter Balis, et
al. paper</a> on
Coordination Avoidance. This paper, from a 10,000ft perspective, says
only coordinate when you have to. Define invariants, coordinate when
one is violated.</p>
<p>It looks to me that a bounded CRDT is using this same concept, but
using the invariant to avoid conflicts.</p>
<p>As a background process, the “Reservation Manager” above doles out
reservations, allowing each replica to operate on an object <em>N</em>
times. When the Nth operation is reached, consensus is needed. If you
are clever enough, and your strongly consistent Reservation Manager
service can stay far enough ahead, you never have to sync.</p>
<h3 id="here-comes-some-math">Here comes some math</h3>
<p>Mathematically, CRDTs are all about a join semilattice, and so I’m
about to attempt to surmise what the lattice is in the context of a
bounded CRDT.</p>
<p>Reservations is a semilattice <span class="math inline">\((R,\ \lor)\)</span>. The current operation,
if operations are <span class="math inline">\(O \subseteq R\)</span> is <span class="math inline">\(R \lor O\)</span>. That is, the
Least Upper Bound of <span class="math inline">\(R\)</span> is the current Operation Count. This meets
semilattice requirements of <a href="http://en.wikipedia.org/wiki/Semilattice#Algebraic_definition">commutativity, associativity, and
idempotency</a>
in that with each operation <span class="math inline">\(⊤O\)</span> will always be equal to some
element in <span class="math inline">\(R\)</span> so long as <span class="math inline">\(R\)</span> remains unbounded. Therefore <span class="math inline">\(R
\lor O\)</span> holds for any <span class="math inline">\(O \subseteq R\)</span>. Like I said, if you are
clever enough you can keep your Reservation Manager far enough ahead.</p>
<p>This CRDT concept is a bit more complicated than the basic commutative
or monotonic models, but is still farily easy to understand
conceptually. I’m very interested in what comes next from the
SyncFree humans, namely the adaptive stuff, which is used to reduce
replicas – which decreases the LUB, which makes pre-determining
reservations easier, which is <code>:thumbs_up:</code>.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/CRDTs/index.html" term="CRDTs" label="CRDTs"/>
    
    <category scheme="https://dpitt.me/tags/distsys/index.html" term="distsys" label="distsys"/>
    
    
  </entry>
  
  <entry>
    <title type="html">Some Readings on Mixins</title>
    <link href="https://dpitt.me/forest/dsp-0002.html" rel="alternate" type="text/html"/>
    <published>Tue, 27 May 2014 00:00:00 UTC</published>
    <updated>Tue, 11 Aug 2026 00:00:00 UTC</updated>

    <content type="html">
      <![CDATA[ <p>To me, Mixins were always a simple, stateless collection of loosely
related methods that could be included in a class. I mean stateless in
that they hold no state of their own, but rather that they assume a
given state.</p>
<p>As an example, this is the perspective Ruby takes on the topic:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode ruby"><code class="sourceCode ruby"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="cf">module</span> <span class="dt">TestModule</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>   <span class="cf">def</span> <span class="fu">test</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>     <span class="fu">puts</span> <span class="vs">&#39;hi&#39;</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>   <span class="cf">end</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">class</span> <span class="dt">TestClass</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a>   <span class="fu">include</span> <span class="dt">TestModule</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span></code></pre></div>
<pre><code>irb&gt; TestClass.new.test
irb&gt; &quot;hi&quot;</code></pre>
<p>Although quite simple, this perspective is misleading according to the
classical definition of mixins. Modern mixin’s original definition
came from a paper written by Gilad Bracha and William Cook, and their
basis is this:</p>
<p>A mixin is an abstract subclass; i.e. a subclass definition that may
be applied to different superclasses to create a related family of
modified classes. For example, a mixin might be defined that adds a
border to a window class; this mixin could be applied to any kind of
window to create a bordered-window class.</p>
<p>The bulk of this research came from a paper that Richard P. Gabriel
wrote that is actually about the philosophy of science / engineering,
where context is all based around mixins. Because I’m obsessed, I went
and read the bulk of all the papers he cites while illustrating his,
“engineering comes first in computer science” theories. Although these
posts won’t cover any of his fascinating perspectives on this
philosophy, I’d still definitely recommend reading his paper. He also
did a talk on the paper, or at least what inspired him to write the
paper: incommensurability.</p>
<p>To begin the delving into mixins, according to Gabriel, we have to
start with Warren Teitelman, a PhD candidate at MIT in 1966. More than
twenty years before the seminal Bracha and Cook paper, Teitelman wrote
his dissertation on a system called PILOT. PILOT uses something called
Teitelman called advice. Here’s a reductionist explanation taken from
the paper’s abstract:</p>
<p>Advising consists of inserting new procedures at any or all of the
entry or exit points to a particular procedure (or class of
procedures).</p>
<p>Teitelman’s thesis is verbose ~200 pages, of which I did not read even
half. Though, in skimming through, I did stop and read all of chapter
5: Experiments With a Question Answer System. Using a couple of fairly
complex logic problems based around a parallel to McCarthy’s Airport
Problem<span class="sidenote-wrapper"><label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">This is not the googlable “airport problem”, this is why
I’ve included the link to the paper where McCarthy first supposes
the problem in 1959. One of the first of its kind.<br />
<br />
</span></span>, Teitelman shows how tests and protections (for example) can
be predefined as advice, and then a function can use this advice
either before or after its invocation. It is even possible for advice
block a function from being invoked at all. The clearest example from
the paper is still a little murky to me, even after a second and third
read though, so rather than attempting to delve into the weeds, I’ll
try and explain the best I can at a high level.</p>
<p>Let’s assume a function whose job is to solve a problem by recursively
running itself, and, if just the right input is given, it can get
stuck in an infinite loop. Using Teitelman’s advice, a programmer can
predetermine some invariants to run at the functions entry and exit
points to test something like, “how many times have I called myself?”
or “have I adequately solved this problem and can I stop the recursive
calls?”. The programmer writes these advice procedures in a generic
way so that they can be reused by any number of functions who may meet
the same requirements.</p>
<pre><code>(tell solution1, (before number advice),
If (countf history ((solution1 -)) is greater than 2, then quit)</code></pre>
<p>This is some code from the example above, where “number” is the number
of times the recursive function has called itself:</p>
<p>The user tells PILOT to modify SOLUTION1. The phrase “(before number
advice)” tells PILOT to insert this advice immediately before the
advice containing the key word “number”.</p>
<p>The first thought to came to mind after skimming this paper was Python
decorators. Though not exactly PILOT, I do see some strong
similarities. Let’s take the example above, at least the part where we
block a call to a function if its invocation count is greater than a
pre-determined value. This requires a bit of an understanding of
Python and its decorators<span class="sidenote-wrapper"><label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote">Python decorators are this thing where functions can be
passed as parameters to generic functions that utilize the
<strong>call</strong>() method on the type Function. Reminds me, in a way, to a
Ruby function that takes a block (Proc).<br />
<br />
</span></span>, but let’s just assume you “get it”, now
that I’ve briefly described PILOT.</p>
<p>First we’ll set up a class whose constructor accepts a function and a
count. The class then sets these properties as instance variables.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> Decorator(<span class="bu">object</span>):</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, func, count <span class="op">=</span> <span class="dv">0</span>):</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>        <span class="va">self</span>.count <span class="op">=</span> <span class="dv">0</span> <span class="kw">or</span> count</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a>        <span class="va">self</span>.func <span class="op">=</span> func</span></code></pre></div>
<p>Next, we’ll override the <code>__call__()</code> method on the class. This is the
method that is invoked on a decorator.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> <span class="fu">__call__</span>(<span class="va">self</span>):</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>     <span class="va">self</span>.count <span class="op">+=</span> <span class="dv">1</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>     <span class="cf">if</span> <span class="va">self</span>.count <span class="op">&lt;</span> <span class="dv">5</span>:</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>         <span class="va">self</span>.func()</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>     <span class="cf">else</span>:</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>         <span class="bu">print</span> <span class="st">&#39;not calling&#39;</span></span></code></pre></div>
<p>And last, we define a recursive method, and decorate it with our
<code>Decorator</code> class.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="at">@Decorator</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> recurse():</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>     <span class="bu">print</span> <span class="st">&#39;recurse called&#39;</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>     recurse()</span></code></pre></div>
<p>As expected, the decorator blocks the call once count is greater than
five. Output:</p>
<pre><code>recurse called
recurse called
recurse called
recurse called
not calling</code></pre>
<p>On <code>recurse()</code>’s fifth invocation, it is not invoked, and the program
exits. To me, this is exactly the type of invariant that Teitelman is
describing in the PILOT examples. While not quite aligned with the
modern perspective of multiple-inheritance based mixins, this is a
clear precursor to the type of reusable, common functionality we
understand as mixins today; at least on a much smaller (per function)
scale.</p>
<p>(On a side note, how cool is that the same instance is passed to each
call to <code>Decorator</code>. I was surprised that it was this simple to
implement.)</p>
<p>Up next in the mixin rabbit trail is Flavors, another Lisp based
precursor to the Bracha and Cook paper, similar in pattern (before and
after), but quite different in concept.</p> ]]>
    </content>

    
    
    <category scheme="https://dpitt.me/tags/programming languages/index.html" term="programming languages" label="programming languages"/>
    
    
  </entry>
  
</feed>
