<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Regan Meloche on Medium]]></title>
        <description><![CDATA[Stories by Regan Meloche on Medium]]></description>
        <link>https://medium.com/@reganmeloche?source=rss-c68f8468b934------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fsLG4dsuYQkoFBM219hKgg.jpeg</url>
            <title>Stories by Regan Meloche on Medium</title>
            <link>https://medium.com/@reganmeloche?source=rss-c68f8468b934------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:22:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@reganmeloche/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rules-as-Code: Encoding Rules in a Government Context — Part 3]]></title>
            <link>https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-3-360347dcb573?source=rss-c68f8468b934------2</link>
            <guid isPermaLink="false">https://medium.com/p/360347dcb573</guid>
            <category><![CDATA[policy-making]]></category>
            <category><![CDATA[civic-technology]]></category>
            <category><![CDATA[rules-as-code]]></category>
            <category><![CDATA[open-government-software]]></category>
            <dc:creator><![CDATA[Regan Meloche]]></dc:creator>
            <pubDate>Fri, 28 May 2021 15:33:56 GMT</pubDate>
            <atom:updated>2021-05-28T15:33:56.469Z</atom:updated>
            <content:encoded><![CDATA[<h3>Rules-as-Code: Encoding Rules in a Government Context — Part 3</h3><p><em>I am a developer fellow for </em><a href="https://codefor.ca/"><em>Code for Canada</em></a><em>, a non-profit organization that works with government agencies to deliver better digital services to the public. I am part of a team of three that is working with Employment and Social Development Canada (ESDC) for a 9-month fellowship. We have been working on a prototype for a tool called a Policy Difference Engine (PDE). The purpose of this tool is to measure how a change to a rule may impact a population. The key component of a PDE is a reliable encoding of the “rule”, which here refers mainly to a piece of government policy/regulation/legislation. In this series, we’ll introduce the concept of Rules-as-Code (RaC), discuss the value behind it, and show you what it looks like in practice using an example from our fellowship.</em></p><p><em>The </em><a href="https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-1-d4bc746b8063"><em>first post</em></a><em> in the series presented a working definition of RaC. The </em><a href="https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-2-77892ff13330"><em>second post</em></a><em> discussed the value of applying RaC to a government context. In this final post we will present a high-level walkthrough of a RaC project that our team participated in during the fellowship.</em></p><h3><strong>The RaC Process</strong></h3><p>Many of the techniques and concepts related to RaC align with some well-known best practices in software development, such as flexible/reusable design and test-driven development. One area where there appears to be extra complexity compared to the typical software development flow is in the collaborative process of converting the rules — written in natural human language — into a machine-friendly version which can be executed as code. Our team was integrated into a large collaborative group for a 3-week sprint to build a RaC prototype system for the <a href="https://laws-lois.justice.gc.ca/eng/regulations/C.R.C.,_c._990/">Motor Vehicle Regulations</a> (MVOHWR), introduced in the previous section. This final section will summarize some of the processes involved with this sprint that we found to be effective for building the RaC system.</p><p>There were primarily two goals for the RaC sprint:</p><ul><li>Build a reusable RaC system that captures the entire MVOHWR, and expose it as an API</li><li>Build a web application that makes use of this API under the hood</li></ul><p>Three weeks is a fairly short timespan to develop two applications with a small team of developers, but it speaks to how crucial the collaborative aspect is, which facilitates the process of breaking down written rules into machine-friendly requirements. While the work of the sprint was three weeks, there was a lot of prior work that went into the preparation, which included aligning all of the different groups involved. During these earlier preparation meetings to discuss the scope of the sprint, we found that there were some technical knowledge gaps. Many of the collaborators on the legal and policy side were not familiar with some important software development concepts that would be prevalent throughout the sprint. This unfamiliarity can understandably create apprehension and lack of clarity around the exact nature and scope of the project. To address this, one of our first activities of the sprint was to create and deliver a short knowledge translation presentation that broadly touched on these topics, giving everyone enough information to speak comfortably about it, and contributing to a common language for us all to communicate in. Some specific areas addressed in the presentation included security, source control, cloud development, and APIs.</p><p>Once we were all aligned with the technical terminology, we began the process of converting the regulations to Rules-as-Code. We were largely building off of some previous work done in this space, and also adding in some of our own practices. Below are some activities that were crucial to this process. Most of the collaborative work was done on a shared virtual whiteboard, which proved to be a very valuable tool for the sprint. We will not go into the details of the attached screenshots — they are just meant to give a taste for what these processes look like.</p><p><strong>Concept Modelling</strong></p><p>We went through the regulation line by line and tried to get a sense of the basic concepts involved, without going too far into the finer details. This involved some informal diagrams on our whiteboard. The main result of this was an increased understanding for everyone involved, and we started to highlight some questions about the rules for further exploration. This helped to identify some of the concrete questions that are answered by the regulations, and would therefore need to be coded into the system.</p><figure><img alt="Cropped screenshot of the decision tree modeling exercise created during the RaC exercise during the Labour sprint." src="https://cdn-images-1.medium.com/max/1024/0*C7sRYx3BcFQPU05D" /><figcaption>Decision Tree Modeling Example</figcaption></figure><p><strong>Example Walkthroughs</strong></p><p>Once we identified the questions that were answered (i.e. how much overtime am I entitled to), we started to walk through some concrete examples. We came up with examples of motor vehicle operator schedules — trying to test typical values and also edge cases. We walked through these very carefully together with the experts. And this is where we began to ask very specific questions, with the goal of figuring out how the language could reliably be translated into code. These examples also served as test cases that were later programmed into the system.</p><figure><img alt="Three individual scenario tables filled out with sample motor vehicle operator schedules based on regulatory calculations" src="https://cdn-images-1.medium.com/max/704/0*cndfksmaKFf83wIa" /><figcaption>Schedule Scenario Table Examples</figcaption></figure><p><strong>Flowcharts</strong></p><p>After walking through some examples and getting a better understanding of the concrete flows, we started to capture the precise logic that was being followed in the examples in flowcharts. It was an opportunity to verify that the logic was being applied consistently across all examples. And if it wasn’t being applied consistently, then we want to ensure the difference was being explicitly captured in the regulation. We found that there are multiple ways to represent the flows for these decision-making algorithms. It’s also worth pointing out that these processes were not necessarily sequential. We would go back and forth between doing examples and flowcharts as needed. If there was a conflict in the flowchart process, we would go back to our examples to see what thinking process was used. This may result in refining the examples with some new information, or coming up with other examples to further stress-test the flowcharts.</p><figure><img alt="Cropped screenshot of the flowcharts generated by applying the codified calculations to the Motor Vehicle Operators regulations." src="https://cdn-images-1.medium.com/max/1024/0*pM0eETH3LXb2DmU2" /><figcaption>Calculation and Decision-making flowcharts</figcaption></figure><p><strong>Blackhat session</strong></p><p>This was a one-time exercise we did with everyone involved. We had a brainstorming session to come up with scenarios where the regulations could potentially be exploited, whether it be intentional or unintentional. We came up with a variety of personas which made for lots of discussion with the policy experts around if and how these actions are being mitigated. We certainly aren’t able to solve all of the potential cases of misuse, but it’s important to keep conversations like this as part of the ongoing process.</p><figure><img alt="Brainstorming board consisting of three categories: persona brainstorm, persona groups, and results. Each category has digital sticky notes organized by overall theme, which was the result of a cross-collaborative effort." src="https://cdn-images-1.medium.com/max/854/0*2NwEPqMn_80cB8N_" /><figcaption>Blackhat brainstorming session</figcaption></figure><p><strong>Coding and testing</strong></p><p>Once the requirements have some level of refinement, the developers are able to iterate on a coded version of them. In addition, we can also write tests for the code we write, which is a great way to verify the level of completion with the policy experts. Once again, this does not necessarily have to be a linear process. We attempted to take a more agile approach, where we would go through some of the above processes to refine the requirements, and then code those requirements in. Throughout the coding and verification process, we would inevitably come up with more edge cases that required further insight. Then we could go back and iterate on the flowcharts and examples to ensure there was a shared understanding and refine the requirements. We were iterating fairly quickly, since it was only a three week sprint, but the process could absolutely be applied to a project with a longer timeline.</p><p>Those are some of the valuable processes that were involved in our collaborative RaC sprint. Here are some of the key results of the sprint</p><ul><li>A Rules as Code engine, exposed as an API, which answered the questions addressed specifically by the MVOHWR</li><li>A web application that simulated potential changes to the rules, for example by tweaking some of the numeric parameters of the rule to see how it affected the overtime hours for a given schedule</li><li>An increased shared understanding on both the tech and policy side. The policy side got to look at the rules through a technical lens, resulting in the clarification of some ambiguities. And the technical side got an appreciation and deeper understanding of the complexity inherent in these government rules</li><li>A wireframe prototype of a more sophisticated multi-purpose application that could be built on top of the RaC engine</li></ul><h3><strong>Conclusion</strong></h3><p>The idea of coding government rules is nothing new. These coded rules will appear in any digital system. “Rules-as-Code” is an extension of the idea of coded rules that includes properties such as transparency, precision, reusability, collaboration, and testability. It may not be the case that all written rules can be immediately converted into a reliable machine-friendly encoding. But the process of subjecting the written rule to a technical lens can at least bring about any sort of ambiguities in the rule that would be blocking that conversion process.</p><p>The value of RaC lies in the fact that the regulations and legislation are mired in legalese and it is very difficult to form an accurate interpretation without the input from policy experts. If there are multiple user-facing applications that rely on different interpretations of the rules, then these rules are going to be inconsistently applied. A single RaC engine solves this by acting as that underlying system for any application that needs it. Since it is the definitive source for the rule, it must be accurate. This requires collaboration with the policy and legal experts in creating the requirements.</p><p>The process for creating the RaC engine may include some processes such as concept modelling, the creation of flowcharts, example walkthroughs, blackhat sessions, and agile iteration. A very important aspect of this process is the testability of the system. The tests to verify the accuracy of the coded system can be done collaboratively and verified by the policy experts. While a single, definitive rules engine is very ambitious and somewhat speculative at this point, valuable work is already being done not just in Canada, but across the world, in countries such as <a href="https://serviceinnovationlab.github.io/projects/legislation-as-code/">New Zealand</a> and <a href="https://fr.openfisca.org/showcase/">France</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=360347dcb573" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rules-as-Code: Encoding Rules in a Government Context — Part 2]]></title>
            <link>https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-2-77892ff13330?source=rss-c68f8468b934------2</link>
            <guid isPermaLink="false">https://medium.com/p/77892ff13330</guid>
            <category><![CDATA[policy-making]]></category>
            <category><![CDATA[open-government-software]]></category>
            <category><![CDATA[rules-as-code]]></category>
            <category><![CDATA[civic-technology]]></category>
            <dc:creator><![CDATA[Regan Meloche]]></dc:creator>
            <pubDate>Wed, 26 May 2021 20:25:27 GMT</pubDate>
            <atom:updated>2021-05-28T15:34:58.390Z</atom:updated>
            <content:encoded><![CDATA[<h3>Rules-as-Code: Encoding Rules in a Government Context — Part 2</h3><p><em>I am a developer fellow for </em><a href="https://codefor.ca/"><em>Code for Canada</em></a><em>, a non-profit organization that works with government agencies to deliver better digital services to the public. I am part of a team of three that is working with Employment and Social Development Canada (ESDC) for a 9-month fellowship. We have been working on a prototype for a tool called a Policy Difference Engine (PDE). The purpose of this tool is to measure how a change to a rule may impact a population. The key component of a PDE is a reliable encoding of the “rule”, which here refers mainly to a piece of government policy/regulation/legislation. In this series, we’ll introduce the concept of Rules-as-Code, discuss the value behind it, and show you what it looks like in practice using an example from our fellowship.</em></p><p><em>In the </em><a href="https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-1-d4bc746b8063"><em>previous post</em></a><em>, we presented a working definition of Rules-as-Code (RaC). In this post we will discuss the value it has in a government context. The </em><a href="https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-3-360347dcb573"><em>third and final post</em></a><em> will present a high-level walkthrough of a RaC project that our team participated in during the fellowship.</em></p><h3><strong>Value of RaC</strong></h3><p>Now that we have a working definition of RaC, let’s use an example to understand the value that it can have in a government context. For our example, we will use the <a href="https://laws-lois.justice.gc.ca/eng/regulations/C.R.C.,_c._990/index.html">Motor Vehicle Operator Hours of Work Regulations (MVOWR)</a>. These regulations are primarily responsible for describing how much overtime a motor vehicle operator is entitled to. Overtime hours for general employees are outlined in the <a href="https://laws-lois.justice.gc.ca/eng/acts/L-2/">Canada Labour Code</a>. This regulation overrides that situation specifically for motor vehicle operators. In order to do this, it must first define what is meant by a motor vehicle operator. So already, we can start to pull out some key questions that are addressed by this rule. These are the questions that we should be encoding into our RaC engine. The main question is: Given a particular schedule, how much time is a motor vehicle operator entitled to? The answer depends on many different factors related to the nature of their work and the number of hours they work, so it isn’t exactly back-of-the-napkin math. While this question seems to be the main one, other questions can also be extracted from this rule, such as:</p><ul><li>What classification of motor vehicle operator am I?</li><li>What are the maximum hours that a highway motor vehicle operator can work per week before being entitled to overtime?</li></ul><p>Questions like this can be valuable to a variety of stakeholders:</p><ul><li>Motor vehicle operators themselves, who want to know how much overtime they are legally entitled to</li><li>These operators may also deal with service agents who need to know important information such as this, in order to help address unique situations that are affected by other government rules</li><li>Employers will need to know this information for HR and accounting purposes</li><li>Policy researchers may want to use this to understand how this regulation may intersect with other rules related to motor vehicle operators</li><li>Social researchers may want to test scenarios with real data to identify if any particular groups of motor vehicle operators are being disproportionately affected under the existing system</li><li>Economists may want to use the information to estimate the costs associated with the rule</li></ul><p>Note that some of these use cases align very closely with the idea of the policy difference engine mentioned in the introduction. These examples are specific to this regulation, but you can imagine how a similar line of thinking could lead to many use cases for many other government rules. Based on these use cases, we may need mobile apps, chatbots, web applications, business software, and research tools that all need to make use of the same set of rules. Many of these rules may have never been scrutinized under a technical lens alongside policy experts. And if they have, then they may be sitting inside a blackbox and therefore unusable to anyone else.</p><p>It may also be the case that some obligations are inferred or implied by the rule, rather than being written explicitly. So if you ask 10 product teams to interpret and implement these coded rules, you can end up with 10 completely different systems. So the accounting software may have different rules than a user-friendly chatbot, and both of these may be different interpretations than a research tool that is using the rule to inform policy. Next, Imagine that a change is made to the regulation. Now those 10 different applications need to interpret the change, leading to even greater possible variation in functionality. This all has potential to create major blockages in the implementation, delivery, and improvement of the rule.</p><p>This is where Rules-as-Code comes in as an approach to help alleviate issues such as this. Suppose we had a single system that is being used under the hood (maybe exposed as an API) by all of these motor vehicle-related applications. This makes the development of these applications much easier, and it helps ensure a consistent experience. This system was developed, is maintained, is tested by, and is iterated upon by a cross-disciplinary team of technical, legal, policy, and design experts. This gives us reliability and accuracy on the rules. The source code for the system is available for all to see so people are able to understand exactly how these decisions are made for them. This system could also be a single component of a larger system that contains many more government rules.</p><p>In the<a href="https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-3-360347dcb573"> final post</a> in this series, we will present a high-level walkthrough of a RaC project that we participated in as part of our fellowship.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77892ff13330" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rules-as-Code: Encoding Rules in a Government Context — Part 1]]></title>
            <link>https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-1-d4bc746b8063?source=rss-c68f8468b934------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4bc746b8063</guid>
            <category><![CDATA[civic-technology]]></category>
            <category><![CDATA[policy-making]]></category>
            <category><![CDATA[open-government-software]]></category>
            <category><![CDATA[rules-as-code]]></category>
            <dc:creator><![CDATA[Regan Meloche]]></dc:creator>
            <pubDate>Tue, 25 May 2021 15:12:10 GMT</pubDate>
            <atom:updated>2021-05-28T15:34:21.174Z</atom:updated>
            <content:encoded><![CDATA[<h3>Rules-as-Code: Encoding Rules in a Government Context — Part 1</h3><p><em>I am a developer fellow for </em><a href="https://codefor.ca/"><em>Code for Canada</em></a><em>, a non-profit organization that works with government agencies to deliver better digital services to the public. I am part of a team of three that is working with Employment and Social Development Canada (ESDC) for a 9-month fellowship. We have been working on a prototype for a tool called a Policy Difference Engine (PDE). The purpose of this tool is to measure how a change to a rule may impact a population. The key component of a PDE is a reliable encoding of the “rule”, which here refers mainly to a piece of government policy/regulation/legislation. In this series, we’ll introduce the concept of Rules-as-Code, discuss the value behind it, and show you what it looks like in practice using an example from our fellowship.</em></p><p><em>This is the first post of the series, where we present a working definition of Rules-as-Code. The </em><a href="https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-2-77892ff13330"><em>second</em></a><em> and </em><a href="https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-3-360347dcb573"><em>third </em></a><em>posts will follow.</em></p><h3>Introduction</h3><p>We will begin with a brief example to illustrate a Policy Difference Engine (PDE). Suppose we have a government regulation that determines how much money a parent/caregiver is eligible for under a childcare benefit. We want to study the impact of changing this rule. If we take 100 simulated individual applicants, and run them against a coded version of the existing rule, then we can see how much each person is entitled to under the current system. Next, we take an updated version of this rule representing the proposed changes, and we run the same 100 individuals against this updated rule. This tells us how much each person is entitled to under the new rules. This process gives us a piece of the picture of how this rule change might impact the population. Conceptually it is a simple tool — we need a set of data points representing individuals, and we need the set of rules and the proposed change. We run each person against the rule and the change, and we aggregate and visualize the results.</p><p>The PDE relies on having a reliable encoding of the rules. The coded regulation/legislation must be captured accurately in the system, otherwise the results are unreliable. Furthermore, this PDE will not be the only application that could benefit from a reliable encoding of the rules — other types of applications that the public would interact with on a daily basis could make use of this as well. This may sound like the common software development practice of ensuring you have the right business requirements and reusing components — and it is! But there are some important aspects of working in the government space that can add some extra complexity. This complexity motivates an approach called “Rules-as-Code”.</p><h3><strong>Defining RaC</strong></h3><p>What is Rules-as-Code? Your first impression might be that it’s simply a coded version of some rules. Technically true, but far from the whole story. First we will start with the idea of “rules”. While the approaches listed here may apply beyond a government context, we will narrow our definition of rules to refer to some government document written in a human language. This could be a large piece of legislation, a 3-page regulation, a 12-line policy, and anything in between. It is still a fairly broad category, but the point is that we are operating in a government context. These rules will posit important definitions (for example, concretely defining a benefit), answer important questions (am I eligible for a certain benefit?), and lay out calculations (How much am I entitled to?).</p><p>To make use of these rules in digital systems, they need to be coded. These “coded rules” are everywhere — any application that makes use of some government rule will have the rule coded somewhere in the system. So the question becomes: what extra characteristics should these coded rules have when working in a government context? Here are some that we’ve identified:</p><h4>Transparency</h4><p>The code for these rules should be visible to all — no black boxes. Giving people access to the code that makes important decisions about their lives can help build trust. It also allows the systems to be open to feedback, since anyone can look at the code and identify any issues with the system, whether it be technical bugs or more systemic issues and gaps in policies.</p><h4>Precision</h4><p>It is important to keep the rules precise in their scope. It should be encoding the questions and definitions laid out by the written rule, and nothing more. So you might have one system that encodes the entire <a href="https://laws-lois.justice.gc.ca/eng/acts/o-9/">Old Age Security Act</a>. This system would answer questions such as “Am I eligible for OAS”, “How much am I eligible for”, “What is the maximum amount that someone can receive?”, etc. Then you would have another system that encodes the entire <a href="https://laws-lois.justice.gc.ca/eng/acts/e-5.6/">Employment Insurance Act</a>, which is much larger in scope (in this case you may break this into a few inter-connected systems). The goal is to make these systems a coded version of the rule in question, so answering questions beyond the scope of the written document may cause confusion. This has interesting design and architectural considerations, but we will not discuss them here.</p><h4>Reusability</h4><p>Another goal of these rules systems is that many different applications can be built on top of them. If two separate applications, say a chatbot and an accounting tool — need some info about the <a href="https://laws-lois.justice.gc.ca/eng/acts/L-2/">Canada Labour Code</a>, then we don’t want to be building this underlying system twice. Build it once and allow these user-facing applications to make use of it (for example, as an API or software library). This is a standard goal for most software development contexts — “Don’t repeat yourself” — but it is worth emphasizing here as well. These are the rules that define important aspects of our lives, so we want a consistent experience when navigating the different tools that rely on them. The written rules themselves are heavy in legalese and ambiguities abound, which leaves them open to different interpretations. If we have two separate applications that have their own interpretation of an important government rule, then the people that the rule affects will have a conflicting experience. Ideally we could have a single source of truth that is open, robust, and reusable.</p><h4>Collaborative</h4><p>If we want to have a single source of truth for these government rules, then we need to make sure that these rules are accurate and reliable. The best way to guarantee reliability with these documents that are mired in ambiguous legalese is to bring policy experts into the process. Subjecting a written rule to a technical lens will bring out any inherent ambiguities. Machines are not able to make guesses (not counting randomization), so it will require that the rules have a clear and logical flow. Experts are needed to help identify and analyze these ambiguities, provide insight to how they are resolved in practice, and help ensure that nothing is lost in the process of translating the written rule into concrete technical requirements.</p><h4>Testable</h4><p>Suppose we start coding a robust and reusable rules system collaboratively with policy experts. How do we know when we are finished? This is largely where the policy experts will come in. We can join this expertise with another best-practice from software development: test-driven development. As the rules are developed (or even before), the collaborative team should be coming up with concrete cases to test the rules against. The nature of these tests will of course depend on the rule you are working with, but suppose we are building an eligibility rule that determines eligibility based on several factors. We can write tests for all possible combinations of factors with their expected output (with validation from the policy experts) and ensure these are passing as the code is developed.</p><p>So while there may be other characteristics that are important when working in a government context, the ones listed above are an important starting point. We can now define systems that meet these criteria as “Rules-as-Code” systems.</p><p>In the <a href="https://reganmeloche.medium.com/rules-as-code-encoding-rules-in-a-government-context-part-2-77892ff13330">next part</a> of the series, we will discuss the value of RaC by walking through a concrete scenario.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4bc746b8063" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mathematical Foundations For Cryptography: Part III]]></title>
            <link>https://reganmeloche.medium.com/mathematical-foundations-for-cryptography-part-iii-235f1d3ce271?source=rss-c68f8468b934------2</link>
            <guid isPermaLink="false">https://medium.com/p/235f1d3ce271</guid>
            <dc:creator><![CDATA[Regan Meloche]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 01:41:05 GMT</pubDate>
            <atom:updated>2021-02-13T00:35:11.687Z</atom:updated>
            <content:encoded><![CDATA[<p>In <a href="https://reganmeloche.medium.com/mathematical-foundations-for-cryptography-part-ii-50a456af317c">part II</a> of this series, we looked at how two entities could share a secret in a public space using a key exchange. In this final part, we will look at authentication.</p><p><strong>PART III: AUTHENTICATION AND RSA</strong></p><p><strong>Asymmetric Encryption</strong></p><p>Faced with two cloaked figures who both are both claiming to be Beta, Alpha must find a way to determine which one is actually Beta. In order to do this, we are going to need to introduce some form of higher “authority”. Before going into what that means, let’s outline the general flow. We are going to be using <strong>asymmetric keys</strong>. This means that the key used to encrypt our text will be <em>different </em>than the key used to decrypt it. Contrast this with <strong>symmetric encryption</strong>, such as Diffie-Hellman, where we used the same key to encrypt and decrypt.</p><p>Beta is going to have two keys. One will be public, and known to everyone else, and one will be private, known only to him. If a message is encrypted with the public key, then it can only be decrypted with the private key. Conversely, If a message is encrypted with the private key, then it can only be decrypted with the public key.</p><p>The reason we take this approach is for authentication. If Beta’s <em>public </em>key is known to everyone, then she can encrypt using her <em>private </em>key. The private key is only known to her, so <strong>no one else can encrypt with the same private key as her</strong>. So suppose Beta and Omega are both trying to convince Alpha that they are the true Beta. Alpha now has a way of knowing who is the real Beta. Alpha asks both Beta and Omega to encrypt the word “HELLO” using their own private keys. They do so, and they present two different pieces of ciphertext on their whiteboards. Alpha will then take Beta’s public key, and decrypt both messages. One of the messages will be gibberish — that’s from Omega. The other will be decrypted back to the initial text “HELLO”. The one who wrote that ciphertext is Beta.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/0*stqz99HTH-JhMidG" /></figure><p><strong>The Higher Authority</strong></p><p>So now you might be wondering — what about this public key? Isn’t that information that needs to be shared in advance? And if so, aren’t we back at square one, since we need a way for Beta to share that, but we have no way of knowing if it’s Beta sharing the public key, or if it’s Omega planting a trap. This is where we must bring in a <em>higher authority</em>. We need a way to definitively ascribe a public key to Beta and another to Omega, and another to Alpha, and so on. We want to minimize the need for this authority, but we can’t do proper authentication without it. We need to adjust our microcosm slightly, so let’s rewind…</p><blockquote><em>Suppose a group of mathematicians, tired of a world where math is under-appreciated, blah blah blah… There are no secrets. Since they are cloaked, the only way one could tell who is writing is if they sign their name.</em></blockquote><p>Ok, now for the new info: Before they are let loose into this new world, a higher authority, perhaps the trustworthy custodian of the temple, asks each mathematician, in private, to remove their hood, verify their identity, and etch their public key into a stone tablet. The authority ensures that each mathematician has a unique public key. Note however, that the authority still does not get access to their private key — that remains private to each individual. We just need a way to ascribe a public key to each person.</p><p>So with this information, Alpha can look at this stone tablet containing 24 different public keys, find the one that belongs to Beta, and decrypt both ciphertexts using that public key, and learn which cloaked figure is the real Beta. Once that is complete, Alpha and Beta can continue along by generating a shared key. We have both authentication and encryption. Now let’s get into the mathematical details of how this works.</p><p><strong>Phi function</strong></p><p>Our big question now is — how do we make this happen? How do we reliably generate a pair of keys (one public, one private) such that one is able to decrypt the other, and vice versa? The strategy we are going to use is called RSA, which stands for Rivest-Shamir-Adleman, the names of the three scientists who described the algorithm in 1977.</p><p>In order to understand RSA, we are going to need to learn a few more important mathematical concepts. The first is the phi (φ) function. The φ value of a given number x is the amount of numbers between 1 and x that do not share a factor with x. Let’s look at some examples:</p><p>Phi of 8, aka φ(8): Let’s take all the numbers between 1 and 8, including 1, but not 8:</p><p>1 2 3 4 5 6 7</p><p>Now let’s look at each number to see if it shares factors with 8.</p><ul><li>1: No factors in common with 8 (or any number, for that matter)</li><li>2: This shares a factor of 2 with 8: 2 = 2 * 1, 8 = 2 * 4, so 2 is common</li><li>3: Nothing in common</li><li>4: Shares a factor of both 2 and 4 with 8</li><li>5: Nothing in common</li><li>6: Shares a factor of 2: 6 = 2 * 3, 8 = 2 * 4. So 2 is common</li><li>7: Nothing in common</li></ul><p>So after removing the numbers that share a factor with 8, we are left with: 1 3 5 7</p><p>We have 4 numbers in total, so we say the value for phi of 8, or φ(8) = 4</p><p>Let’s do another example, φ(15). Again, we start by writing all the numbers from 1–14.</p><p>1 2 3 4 5 6 7 8 9 10 11 12 13 14</p><p>Now let’s look at each of these to see if they have a factor in common with 15:</p><ul><li>1: As always, nothing in common</li><li>2: Nothing in common</li><li>3: 3 is a factor of 15, so cross it out</li><li>4: Nothing in common</li><li>5: 5 is a factor of 15, so cross it out</li><li>6: 6 = 3 * 2, and 15 = 3 * 5, so 3 is common. Cross it out</li><li>7: Nothing in common</li><li>8: Nothing in common</li><li>9: 9 = 3 * 3, 15 = 3 * 5, so 3 is common. Cross it out</li><li>10: 10 = 5 * 2, and 15 = 5 * 3, so 5 is common. Cross it out</li><li>11: Nothing in common</li><li>12: 12 = 3 * 4, and 15 = 3 * 5, so 3 is common. Cross it out</li><li>13: Nothing in common</li><li>14: Nothing in common</li></ul><p>Now we are left with: 1 2 4 7 8 11 13 14</p><p>We have 8 numbers remaining, so φ(15) = 8</p><p>One more example: φ(11). If you try out numbers from 1 to 10, you will see that none of them share any factors with 11. This is because 11 is a prime number! So by definition, it has no factors in common with any other number, so we don’t cross out any numbers, and φ(11) is equal to 10. In fact, with any prime number P, φ(P) = P — 1. The numbers 7, 13, 43, and 991 are all prime numbers, so their phi values are 6, 12, 42, and 990 respectively. If you know that your number is prime, then the phi value is trivial to calculate.</p><p>One other important fact about the phi function. Suppose we asked for φ(88). This is a bit bigger than 8 and 15, so it might take a while to calculate. But we can actually use an important property of the phi function to make it easier. First, we note that 88 = 8 * 11. Now to find φ(88), we can take the phi of those two smaller numbers, and multiply their results together:</p><p>φ(88) = φ(8 * 11) = φ(8) * φ(11) = 4 * 10 = 40</p><p>This is much easier than going through all the numbers from 1–87.</p><p>Ok, so we have a grasp on the phi function. Now we can briefly go back to our temple and see what Beta might be doing to generate his public and private keys. Beta is going to start by choosing two prime numbers, say 11 and 13 (we’ll see why he chooses primes later on). He is going to multiply them together to get 11 * 13 = 143. Let’s call this number N. N = 143. Next he is going to calculate φ(N). Based on what we just learned, this shouldn’t be too hard, since both 11 and 13 are primes:</p><p>φ(143) = φ(11 * 13) = φ(11) * φ(13) = (11–1) * (13–1) = 10 * 12 = 120</p><p><strong>Back to the modulus</strong></p><p>Ok, let’s keep N = 143 and φ(N) = 120 in the back of our head for now, and turn back to the modulus and powers. Beta wants to encrypt the text “HELLO”. Here is the encryption scheme he will use. He is going to start with the letter H, and convert that to the number 8, like before. Now he comes up with a formula to encrypt that number to a new number, for which we will use the placeholder ‘c’. That formula is as follows:</p><p>8ᵉ mod 143 = c</p><p>He is going to pick some value for e, say 3, and then apply the formula: 8³ mod 143. 8³ = 512, so we have 512 mod 143. The answer is 83: 8³ mod 143 = 83. He obviously can’t turn this back into a letter, unless he applies a modulus of 26. We are not going to worry about converting back into letters for this encryption. Beta is just going to encrypt into a new collection of numbers. Assuming Alpha has the public key, she can then decrypt those numbers back into the original numbers, which will correspond to “HELLO”. So Beta ends up with 83 as the first number in his ciphertext.</p><p>Now what he needs is a way to get <em>back</em> to that original value of 8, starting with 83. This is the <em>decryption </em>process<em>.</em> He will use a similar formula: 83ᵈ mod 143 = 8. So he needs to find a value d that will do this for him. The number 7 works. So this could be an example of a public-private key pair. Beta can keep 3 as his private key, and share 7 as the public key. He will encrypt his message with the formula x³ mod 143 = c, and Alpha can then decrypt using c⁷ mod 143 = x. Here is what would happen with the word “HELLO”:</p><ul><li>H = 8: 8³ mod 143 = 83</li><li>E = 5: 5³ mod 143 = 125</li><li>L = 12: 12³ mod 143 = 12</li><li>L = 12: 12³ mod 143 = 12</li><li>O = 15: 15³ mod 143 = 86</li></ul><p>So the ciphertext becomes “83 125 12 12 86”. Notice that 12 maps to 12 in this case. This could potentially be a weakness with the encryption, but we will be looking at a more complex scenario later. Now let’s see what happens when Alpha tries to decrypt:</p><ul><li>83⁷ mod 143 = 8 = H</li><li>125⁷ mod 143 = 5 = E</li><li>12⁷ mod 143 = 12 = L</li><li>12⁷ mod 143 = 12 = L</li><li>86⁷ mod 143 = 15 = O</li></ul><p>So we’ve successfully found a pair of numbers that could work as a public-private key pair. Let’s go into more detail about what Beta is looking for when making his public and private key pair. He wants to find values for e and d so that:</p><p><strong>x</strong>ᵉ<strong> mod N = c </strong>AND <strong>c</strong>ᵈ<strong> mod N = x</strong></p><p>We can actually put these two formulas together. If xᵉ mod N = c, then we can replace c in the second equation with xᵉ and we get xᵉᵈ mod N = x, as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/0*UuPRvIo13W2dcCDh" /></figure><p>xᵉᵈ mod N = x. So x to the power of the product of e*d will give x itself back when applying mod N. So we are looking for e and d.</p><p>To understand the mechanics of how these can be found, we need to look at one more important mathematical concept, Euler’s Theorem:</p><p><strong>Euler’s Theorem</strong></p><p>First off — out of the box, Medium doesn’t support the ability to put special characters like φ into superscript to represent a power. So I’m going to switch to a slightly clunkier notation, using the ^ symbol to represent a power. 5^3 is the same as 5³. y^(3x) is the same as y³ˣ. <strong>And x^(φ(N)) means x to the power of φ(N).</strong></p><p>Euler’s Theorem can be stated as follows: x^(φ(N)) = 1 mod N. This is true as long as the values for x and N do not share any common factors. Start with some numbers x and N. Find the phi value for N, and raise x to that power, and you will end up with 1 mod N. Let’s look at some examples:</p><p>Suppose x = 3, and N = 5: 3^(φ(5)) mod 5 = 3⁴ mod 5 = 81 mod 5 = 1. This works.</p><p>Suppose x = 7, and N = 18: 7^(φ(18)) mod 18 = 7⁶ mod 18 = 1. This also works</p><p>Suppose x = 3, and N = 6: 3^(φ(6)) mod 6 = 3² mod 6 = 9 mod 6 = 3. This does <em>not </em>work, because 3 and 6 share a common factor of 3.</p><p>Let’s plug in our value of N = 143 into this function now: x^(φ(143)) = 1 mod 143, or x¹²⁰ = 1 mod 143.</p><p>Now using mathematical rules, we can alter Euler’s theorem slightly:</p><p>Let’s set both sides to the power of k: x¹²⁰ᵏ = 1ᵏ mod 143.</p><p>Recall that if we put 1 to the power of ANY value, then the result is still 1, so we can actually just drop the k from the right side of the equation, and the sides are still equal:</p><p>x¹²⁰ᵏ = 1 mod 143</p><p>Now let’s multiply both sides by x: x * x¹²⁰ᵏ= x * 1 mod 143</p><p>If we’re multiplying x¹²⁰ᵏ by another x, that is equivalent to x¹²⁰ᵏ + 1, so let’s put that on the left side. The right side simply becomes x * 1 = x:</p><p>x¹²⁰ᵏ⁺¹ = x mod 143</p><p>Now this is actually starting to look a little bit like what Beta was aiming for: xᵉᵈ = x mod 143. Looking at the powers on both of these, we just need e*d = 120k + 1.</p><p>Beta can start by choosing a value for e. Let’s use a value of 23. We now have:</p><p>23d = 120k + 1</p><p>Using a bit of algebra, we can work out some possible values for d and k. We get a value of d = 47, and k = 9. The value of d is the one we are interested in. The process to find these values is called the <strong>Extended Euclidean algorithm</strong>, but we won’t be covering it here.</p><p>23 * 47 = (120 * 9) + 1 = 1081</p><p>So we have values for e, d, and k where e*d = 120k + 1. e = 23, and d = 47. These are our two keys. Let’s say e is the private key, which will be used by Beta to encrypt, and d is the public key. If we try this on the text “HELLO”, we would actually map to the same values as we had with the 3 and the 7.</p><p><strong>Cracking the code</strong></p><p>So Beta has a private and a public key, and they can be used for encrypting and decrypting each other. He generates these before initially entering the temple and shares the public key with the custodian, who also verifies that he is in fact, Beta. After that, Beta’s hood goes up, and is no longer distinguishable from the others.</p><p>Once again, let’s look at things from Omega’s perspective. On the whiteboard, Alpha writes:</p><p><strong>Alpha: I want to talk to Beta</strong></p><p>At this point, Omega and Beta approach the whiteboard, both claiming to be Beta. Alpha now writes</p><p><strong>Alpha: Say HELLO using your private key</strong></p><p>Both Beta and Omega use the private keys they have memorized to encrypt the word HELLO.</p><p>Beta writes the following:</p><p><strong>Beta: 83 125 12 12 86 … Use Mod 143</strong></p><p>Omega, also posing as Beta, writes (these numbers are arbitrary — the point is that they are different than the <em>real </em>Beta’s):</p><p><strong>Beta: 67 89 45 45 99 … Use Mod 4661</strong></p><p>Alpha takes both texts and uses Beta’s public key of 47 to decrypt both messages. Omega’s ciphertext decrypts to gibberish and Beta’s decrypts to “HELLO”, and the real Beta has been identified.</p><p>Now what if Omega keeps track of what Beta wrote, so that for a future interaction, she can try to crack his private key. Omega knows that the original message is “HELLO”, so the first number is generated from 8 using Beta’s private key is 83: 8ᵉ mod 143 = 143. So Omega knows this, and Omega also knows that the public key is 47. Omega just needs to know what e is. So there are two possibilities for attacking.</p><p>First of all, she can use trial and error. She can cycle through all the numbers from 1–143 and try to learn what value gives 8ᵉ mod 143 = 83. 143 is not that huge a number, so this may not take too long. As with the shared secret, the way around this is to pick a large number for N, so that Omega decides it will take too long to even try to crack.</p><p>The other way to attack is a little more subtle. Omega knows that they used the phi function to generate these numbers, and she knows that they used 143 in the modulus. So all she needs to do is take the φ(143), which is 120, and then work backwards from 120k + 1 = 47e, and solve for e. This appears to be even easier to crack! Once again, this is true for smaller numbers, but will become a very different story as the numbers get bigger.</p><p><strong>Prime Factorization</strong></p><p>The reason that this will work lies in the phi function. It was very important that we pick our value of N to be made up of two prime numbers. To calculate φ(143), we just had to break 143 up into its two primes, 11 and 13. This seems simple, right? Yes, but only for small numbers. If we were to use a higher value of N, say 26797, then it’s harder to figure out the prime factors. There are tricks that can be used to make the process easier, but ultimately the prime factorization relies on trial and error — you must test a bunch of different prime numbers to see if they are a factor of 26797. By hand, this would be very tedious. After much trial and error, you would eventually find that the answer is 127 * 211. Back in the real world, if we want to authenticate over a computer network, we would need even bigger prime numbers, since a computer could crack 26797 in under a second. Our N (which is composed of two primes) needs to be <em>so big </em>that it would take way too long for even a powerful computer to factor a number into primes. And this inability to easily factor primes is the underlying mechanism behind the effectiveness of the RSA algorithm.</p><p>Back to our microcosm. When generating their public-private key pairs, each mathematician will select their own two large prime numbers, which will be multiplied to get N, and will then be used to generate their two keys. The public key is shared on a board for all to see, and the private keys are kept private.</p><p>Let’s go through the process once more, supposing Beta were to use 127 and 211 as his prime numbers:</p><ul><li>Pick two primes for N: N = 127 * 211 = 26797</li><li>Calculate the phi: φ(26797) = φ(127) * φ(211) = 126 * 210 = 26460</li><li>Apply Euler’s Theorem: x^(φ(26797)k + 1) mod 26797= x mod 26797</li><li>So we need to find e*d = 26460k + 1</li><li>Choose e to be 823, for example</li><li>Now we need values for d and k where 823d= 26460k + 1</li><li>The value 1181 works for d (k = 42). This is done using the Extended Euclidean Algorithm</li><li>So the private key is e = 823, and the public key is d = 1181</li></ul><p>Now when Alpha makes her request on the whiteboard, asking Beta to encrypt “HELLO” with this private key, he does the following:</p><ul><li>H = 8: 8⁹⁴¹ mod 26797 = 16133</li><li>E = 5: 5⁹⁴¹ mod 26797= 23711</li><li>L = 12: 12⁹⁴¹ mod 26797= 16920</li><li>L = 12: 12⁹⁴¹ mod 26797= 16920</li><li>O = 15: 15⁹⁴¹ mod 26797= 23196</li></ul><p>Then on the whiteboard he writes:</p><p><strong>16133 23711 16920 16920 23196. Use mod 26797</strong></p><p>Then Alpha uses Beta’s public key of 1181 to decrypt:</p><ul><li>16133¹¹⁸¹ mod 26797 = 8 = H</li><li>23711¹¹⁸¹ mod 26797 = 5 = E</li><li>16920¹¹⁸¹ mod 26797 = 12 = L</li><li>16920¹¹⁸¹ mod 26797 = 12 = L</li><li>23196¹¹⁸¹ mod 26797 = 15 = O</li></ul><p>Again, consider this from Omega’s point of view. Supposing she wants to discover Beta’s private key of 941. She knows she needs to find the value for x in the equation x¹¹⁸¹ mod 26797 = 8. She’ll either need to step through all the numbers up to 26797 until she finds the one that results in 8. This will take a veeeery long time. The other option is if she knows the phi value of 26797. She knows it is composed of two primes, but she doesn’t know which ones. She will need to do more trial and error with different primes until she figures it out. Again, this will take too long for her to do, so Alpha and Beta can be sure that they have both authentication and encryption.</p><p><strong>CONCLUSION</strong></p><p>Now we’ve seen the entire process. Let’s do a recap of all the steps involved, this time in the right order. Underneath each step is the “roughly equivalent” step in modern web technology.</p><p>The mathematicians decide to live in a temple in the Himalayas.</p><ul><li><em>We connect to the world wide web through a browser, such as Chrome or Firefox. We want to go to Wikipedia.</em></li></ul><p>Each mathematician will choose two large prime numbers, and use the RSA algorithm to generate a key-pair.</p><ul><li><em>Wikipedia generates a key-pair using the RSA algorithm.</em></li></ul><p>The custodian of the temple will verify the identity of each person and etch their public keys onto a stone tablet for all to see. The private keys are kept private.</p><ul><li><em>Wikipedia shares it’s public key with a higher authority, called a “Certificate Authority”, who verifies that they are indeed Wikipedia. The public key is shared publicly across browsers.</em></li></ul><p>Each mathematician dons a robe, concealing their identity. They take their vow of silence and agree to only communicate on the whiteboard.</p><ul><li><em>We browse the web, communicating with a bunch of different websites.</em></li></ul><p>Alpha makes a request on the whiteboard to speak with Beta.</p><ul><li><em>We want to make a donation with our credit card to Wikipedia.</em></li></ul><p>This message is seen by all, including Omega. Omega decides she wants to try to intercept the message, so she poses as Beta by approaching Alpha at the whiteboard, alongside the <em>real </em>Beta.</p><ul><li><em>A hacker on the network sees this communication, and they try to insert themselves in the middle of our communication by posing as Wikipedia. They’ve also gone through the process of generating a key-pair).</em></li></ul><p>Alpha cannot tell them apart at this point, so she asks both of them to send some sort of greeting. Both Beta and Omega encrypt the message “HELLO” using their private keys, and show them publicly, for Alpha to see.</p><ul><li><em>Wikipedia sends a message encrypted using their private key. The impostor tries the same thing using their private key.</em></li></ul><p>Alpha takes these two messages and attempts to decrypt both of them using Beta’s public key.</p><ul><li><em>The browser is going to look at the publicly available public key for Wikipedia, and use it to decrypt all messages from the site that is claiming to be Wikipedia.</em></li></ul><p>Omega’s message decrypts to gibberish, and Beta’s message decrypts to “HELLO”. Alpha identifies Beta.</p><ul><li><em>The public key for Wikipedia fails to decrypt the bogus message from the impostor and correctly decrypts the message from the real Wikipedia. Our browser stops communicating with the fraud.</em></li></ul><p>Alpha publicly shares some information about how she will communicate with Beta: “Diffie Hellman, mod ##). They generate some numbers, share them, and then together they generate a shared secret.</p><ul><li><em>Our browser negotiates with Wikipedia on the details of how encryption will proceed.</em></li></ul><p>Alpha and Beta encrypt their messages using the shared secret, so Omega cannot listen in.</p><ul><li><em>We send our credit card info to Wikipedia securely. A security icon near the URL (e.g. a padlock) indicates that the communication is authenticated and encrypted.</em></li></ul><p>Important details were omitted for clarity and brevity, such as the extended Euclidean Algorithm, a proof of Euler’s Theorem, and the more technical details of web encryption, but I hope that this article has convinced you that it is possible for two parties to communicate securely over a public network using fundamental mathematics.</p><p>Thank you for reading!</p><p><strong>USEFUL RESOURCES</strong></p><ul><li>Computerphile: <a href="https://www.youtube.com/channel/UC9-y-6csu5WGm29I7JiwpnA">https://www.youtube.com/channel/UC9-y-6csu5WGm29I7JiwpnA</a></li><li>Free Crypto 101 book by Laurens Van Houtven: <a href="https://www.crypto101.io/">https://www.crypto101.io/</a></li><li>Khan Academy Cryptography course: <a href="https://www.khanacademy.org/computing/computer-science/cryptography">https://www.khanacademy.org/computing/computer-science/cryptography</a></li><li>Cybersecurity Udemy course: <a href="https://www.udemy.com/course/the-complete-internet-security-privacy-course-volume-1">https://www.udemy.com/course/the-complete-internet-security-privacy-course-volume-1</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=235f1d3ce271" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mathematical Foundations for Cryptography: Part II]]></title>
            <link>https://reganmeloche.medium.com/mathematical-foundations-for-cryptography-part-ii-50a456af317c?source=rss-c68f8468b934------2</link>
            <guid isPermaLink="false">https://medium.com/p/50a456af317c</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[encryption]]></category>
            <dc:creator><![CDATA[Regan Meloche]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 01:21:44 GMT</pubDate>
            <atom:updated>2021-02-13T00:26:56.129Z</atom:updated>
            <content:encoded><![CDATA[<p>In <a href="https://reganmeloche.medium.com/mathematical-foundations-for-cryptography-part-i-74fddf85badd">part I </a>of this series, we introduced the idea of a cipher for encrypting and decrypting text between two entities. We now continue with part II, where we will learn how two different parties can establish a shared secret.</p><p><strong>PART II: A SHARED SECRET</strong></p><p><strong>Mathematical tools: The modulus and the power</strong></p><p>The first mathematical tool we are going to use is called the modulus function. To understand the modulus function, let’s look at an example. We want to know the value for <strong>29 modulus 8</strong>, or <strong>29 mod 8</strong> for short (or 29%8 for even shorter). The modulus is the <em>remainder </em>after dividing the first number (29) by the second number (8). So 29/8 = 3 with a remainder of 5. We don’t care about the 3; we care about the 5. That is the modulus: <strong>29 mod 8 = 5</strong>.</p><p>Some more examples:</p><ul><li>7 mod 2 = 1; 7/2 = 3 with remainder of 1. We just care about the 1</li><li>54 mod 20 = 14; 54/20 = 2 with remainder of 14. We just care about the 14</li></ul><p>Note that the result must always be less than the second number.</p><p>If the first number evenly divides the second, then the result is 0.</p><ul><li>14 mod 7 = 0</li><li>6 mod 2 = 0</li></ul><p>If the first number is <strong>less</strong> than the second number, the result is simply that first number.</p><ul><li>3 mod 10 = 3</li><li>56 mod 948 = 56</li></ul><p>We also need to make sure we understand powers. For example, 2 to the power 5 means we multiply 2 * 2 * 2 * 2 * 2 = 32. We multiply it 5 times: 2⁵ = 32</p><p>Another example: 3⁶ = 3 * 3 * 3 * 3 * 3 * 3 = 729</p><p>Finally, 1 to the power of any number is always 1: 1⁷⁸ = 1</p><p>We are going to use both of these mathematical tools to come up with a shared secret between Alpha and Beta.</p><h4><strong>Key Exchange</strong></h4><p>First of all, Alpha and Beta are going to share some information publicly (they have no other choice). Alpha is going to write the following on the whiteboard:</p><p><strong>Alpha: Hey Beta, Diffie-Hellman: 3ˣ mod 17</strong></p><p>Beta (and everyone else) will see this and understand what is meant, and <em>we </em>will also understand very soon.</p><p>Alpha and Beta will now choose their own private numbers. They will not share this number with anyone else, not even each other. Let’s suppose Alpha chooses 4, and Beta chooses 2. They will now plug this number into the “x” on the equation that they shared publicly (3ˣ mod 17). The “x” is simply a placeholder for another possible value.</p><ul><li>Alpha chose x = 4, and she will get: 3⁴ mod 17</li><li>First she needs to calculate 3⁴ = 3 * 3 * 3 * 3 = 81</li><li>Then she takes 81 mod 17 = ?</li><li>81 divides 17 four times with a remainder of 13 (4*17 + 13 = 81), so the result is 13: <strong>3</strong>⁴<strong> mod 17 = 13</strong></li><li>Beta does the same thing with his private number: <strong>3</strong>²<strong> mod 17 = 9</strong></li></ul><p>They will then share these results publicly, while still keeping their private numbers private. So Alpha will write 13 on the whiteboard, and Beta will write 9.</p><p>Now Alpha is going to take the number that Beta shared (9) and “mix” it with her private number of 4 in the following way: 9⁴ mod 17 = 6561 mod 17 = 16</p><p>Likewise, Beta is going to take the number that Alpha shared (13) and mix it in with his private number (2) in the same way: 13² mod 17 = 169 mod 17 = 16.</p><p>Alpha and Beta both get the number 16. This is a shared key! Is it a coincidence? Not at all! Let’s see why:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*hx6IXhmYY_5XrTkevmVtUA.png" /></figure><p>Beta shared the number 9, which was generated from his private number of 2 and the formula of 3ˣ mod 17 that they shared: 3² mod 17 = 9.</p><p>So 3² = 9, and 9 = 3². When Alpha mixed that in with her own private number 4, she did: 9⁴ mod 17. Since 9 = 3², this is the same as doing (3²)⁴— we replaced the 9 with 3². The mathematical rule for when you have powers to other powers is that you can simply multiply them together, so really Alpha had 3⁸ mod 17 = 16.</p><p>Conversely, Alpha gave Beta the number 13, which was generated from her private number 4: 3⁴ mod 17 = 13. So when Beta mixes that with his own private number of 2, it was the same as doing: (3⁴)² mod 17, which is the same as 3⁸ mod 17, which is exactly what we saw with Alpha.</p><p>So Alpha and Beta were able to get the shared number of 16 using this method. They never once shared their private numbers, only the results that got generated from those numbers. This is called a <strong>Diffie-Hellman Key Exchange</strong>. Recall that Alpha wrote the following on the whiteboard:</p><p><strong>Alpha: Hey Beta, Diffie-Hellman: 3</strong>ˣ<strong> mod 17</strong></p><p>Beta knew to follow the steps that we just took in order to generate a shared key with Alpha. Now that they have a shared key that no one else can see, they can use this as a polyalphabetic cipher with that shared key as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/0*EY84eAGvz9jr7pFE" /></figure><p>Alpha writes the following on her whiteboard:</p><p><strong>Alpha: N E S K B R O G N K J Y B R J I F</strong></p><p>Since Beta has the shared key of 16, he can work backwards to decrypt the ciphertext back into the original message. Here is a diagram of the process:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*1lQHSI4-O6-3gJUlV2LyJQ.png" /></figure><p><strong>Cracking the Code</strong></p><p>Technically this works to create a shared secret, but it has a severe practical drawback. Let’s consider it from our attacker, Omega’s, point of view. She sees the following information exchanged on the whiteboard:</p><p><strong>Alpha: Hey Beta, Diffie-Hellman: 3</strong>ˣ<strong> mod 17</strong></p><p><strong>Beta: Ok Alpha</strong></p><p><strong>Alpha: 13</strong></p><p><strong>Beta: 9</strong></p><p><strong>Alpha: N E S K B R O G N K J Y B R J I F</strong></p><p>While Omega may not be able to figure out the key of 16 right off the bat, she has two avenues to cracking it. First she can try to crack it by looking for patterns in the ciphertext. It’s a polyalphabetic cipher that only has length 2, and the shorter the cipher key, the less time it takes to crack. Alternatively, she could use the information they shared publicly to try to crack the key of 16. She knows the formula they used: 3ˣ mod 17. She knows that Alpha picked some number for x that resulted in 3ˣ mod 17 = 13. So now all she needs to do is cycle through values for x until she gets the result of 13:</p><ul><li>She tries 1: 3¹ mod 17 = 3 — nope</li><li>She tries 2: 3² mod 17 = 9 — nope</li><li>She tries 3: 3³ mod 17 = 10 — nope</li><li>She tries 4: 3⁴ mod 17 = 13 — bingo!</li></ul><p>She now knows Alpha picked a key of 4. She takes the 9 that Beta publicly shared and gets the key: 9⁴ mod 17 = 16. The code is cracked. She can look at any of the encrypted text shared between Alpha and Beta, and easily decrypt it.</p><p>So why did we spend all that time learning about the modulus when Omega was able to crack it after 4 simple calculations? The important thing to note here is that Omega needed to use trial and error . She tested 1, and it didn’t work, then she tested 2, 3, and 4. In the worst case, she would’ve had to test all the way up to 16, since we used mod 17, which still isn’t too bad.</p><p><strong>Bigger Numbers</strong></p><p>But what if instead of mod 17, Alpha and Beta chose a much higher number, say 3659. So Alpha writes:</p><p><strong>Alpha: Hey Beta, Diffie-Hellman: 3</strong>ˣ<strong> mod 3659</strong></p><p>Beta agrees and they choose their private numbers:</p><ul><li>Alpha chooses 28: 3²⁸ mod 3659 = 2233. The value 2233 is shared publicly</li><li>Beta chooses 11: 3¹¹ mod 3659 = 1515. The value 1515 is shared publicly</li><li>Alpha mixes Beta’s public portion with her own private number: 1515²⁸ mod 3659 = 1576</li><li>Beta mixes Alpha’s public portion with his own private number: 2233¹¹ mod 3659 = 1576</li></ul><p>Once again, Alpha and Beta have established a shared key, which they can now use with a polyalphabetic cipher. If Omega wants to crack it, she needs to play the same game as before — trial and error. She must iterate from 1 to 3658, looking for a number that gives 3ˣ mod 3659 = 28. This could take an extremely long time, even for an expert mathematician such as herself. She realizes it would take way too long, so she doesn’t even bother. Alpha and Beta now have a successful encryption system. This is called “Symmetric” encryption, since they use the <em>same </em>key (1–5–7–6) to encrypt and decrypt the messages.</p><p>This method could work quite well in our microcosm, but if Omega <em>really </em>wants to intercept what Alpha and Beta are communicating, she has one more trick up her sleeve. Recall one of the important facts we acknowledged about this scenario: The members are all wearing big cloaks that cover their bodies and mask their faces. It is impossible to tell who is who, unless they write their name on the board, as a sort of signature.</p><p>But what if Omega suspected that Alpha and Beta wanted to talk, and decided to pose as Beta by simply signing her name as Beta on everything she writes. Being bound by his vow, Beta has no way to prove that he is the real Beta. All he can really do is write “No I’m Beta” on the whiteboard, and Alpha must guess who is the true Beta. If Alpha guesses wrong, then all the other measures taken to encrypt the message are meaningless! The messages can be encrypted, but the communication is happening with the wrong person. We’re missing authentication. Once again, we must reach even further into our mathematical toolkit to solve this problem. This is where we will begin in the <a href="https://reganmeloche.medium.com/mathematical-foundations-for-cryptography-part-iii-235f1d3ce271">3rd and final part</a> of the series.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50a456af317c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mathematical Foundations for Cryptography: Part I]]></title>
            <link>https://reganmeloche.medium.com/mathematical-foundations-for-cryptography-part-i-74fddf85badd?source=rss-c68f8468b934------2</link>
            <guid isPermaLink="false">https://medium.com/p/74fddf85badd</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[cryptography]]></category>
            <dc:creator><![CDATA[Regan Meloche]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 01:08:24 GMT</pubDate>
            <atom:updated>2021-02-13T00:17:45.983Z</atom:updated>
            <content:encoded><![CDATA[<p>Cryptography is the science of secure communication. Two entities (people) want to communicate in some sort of public space without anyone being able to understand what is being said, except for the two entities involved. While this field has ancient roots, it is a very important study today, when millions of people are sending sensitive information over the internet every day. The processes to achieve secrecy on the web are complex and sophisticated, but underlying them are some fundamental mathematical concepts.</p><p>The goal of this series is to convince the reader that it is possible for two entities to communicate securely on a public network using pure math — no electronics required (although a calculator would be handy). “Securely” here means two things:</p><ul><li>The parties know that the person they are communicating with is who they say they are</li><li>The messages that they send cannot be understood by anyone else that may be reading them</li></ul><p>I recommend grabbing a pad of paper and a pencil to write out some of the math that we will be going through.</p><p><strong>HTTPS</strong></p><p>The methods we will explore are the basis of modern web security. HTTP (Hypertext transfer protocol) is a standard that defines how two machines on the web communicate with each other. The two machines are typically the client (the browser running on your machine) and the server (the website you are interacting with). In regular HTTP, the messages sent between the two parties are essentially in plain text. So if you type your email or credit card details into a form and press “submit”, then that information — as you typed it — travels along the network. If anyone else is listening in on the network, then they can peer in and see your information. HTTPS (note the S) ensures both aspects of security mentioned as part of our goal: Authentication and encryption. We are going to focus on the mathematical principles that underlie this layer of security.</p><p><strong>Setting the Scene</strong></p><p>To help illustrate this scenario, we are going to set up a contrived microcosm of the web. Suppose a group of mathematicians, tired of a world where math is under-appreciated, have decided to live a secluded and monastic mathematical life in the Himalayas. They don heavy maroon robes that cover their bodies and faces, and they take a vow of silence. There are 24 of them, so each is assigned a Greek letter as their new name (Alpha, Beta, Gamma, … Omega). In the common area of their temple is a large whiteboard. The only way they are allowed to communicate is by drawing on the whiteboard. This means that all the other mathematicians will be able to read what everyone else has written. There are no secrets. Since they are fully cloaked with their faces covered, the only way one could tell who is writing is if they sign their name.</p><p>Communication might proceed as follows. The mathematician named ‘Theta’ might write the following on the whiteboard, signed with his Greek letter name:</p><p><strong>Theta: The sum of the square roots of any two sides of an isosceles triangle is equal to the square root of the remaining side.</strong></p><p>Then the mathematician named ‘Iota’ might reply below it:</p><p><strong>Iota: That’s a right triangle, you idiot!</strong></p><p>Life starts out well for these mathematical monks, but eventually friendships and rivalries begin to form. One in particular, Alpha, decides she wants to send a private message to another mathematician, Beta. But she also wants to stick to her vows. So here is a summary of the situation:</p><ul><li>Alpha wants to send a private message to Beta</li><li>She can only communicate by drawing on the whiteboard</li><li>Anything written on the whiteboard can be seen by everyone</li><li>No one can be uniquely identified, other than by signing their name on the whiteboard after writing a message</li></ul><p>Again, this is a very contrived scenario to illustrate communication over the web. We want to send private information to certain websites, without the messages being readable to outside observers, and we want to be able to know that the entity we are communicating with is who they say they are.</p><h4><strong>PART I: CIPHERS</strong></h4><p><strong>Caesar Cipher</strong></p><p>Being mathematicians, everyone in the temple is familiar with the various cryptographic systems out there. One such system is the Caesar cipher, and it could work as follows. Suppose Alpha wants to send the following message to Beta, and only to Beta:</p><p><strong>“MY REAL NAME IS ALICE”</strong></p><p>She can’t simply write that on the board, since then everyone else will be able to see it. She needs to use a cipher. Let’s assume for now that Alpha and Beta have somehow planned in advance to communicate using a Caesar cipher with a key of 5.</p><p>So instead of writing “MY REAL NAME IS ALICE” on the whiteboard, Alpha would do the following:</p><p>1. Convert every letter to a number (A = 1, B = 2, C = 3, …. Z = 26)</p><p>2. Use her “key” of 5, and add 5 on to each new letter</p><p>3. She would then convert the new letter back to a number (1 = A, 2 = B, … 26 = Z). If any of the numbers went over 26 when she applied her key, she would restart back at 1. So 27 would be the same as 1, 28 is the same as 2, 29 is 3, and so on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/0*9prTamLclvFcLZMy" /></figure><p>Note that the Y (25) becomes 30 when 5 is added to it. This is a case where we would “restart” back at the beginning of the alphabet. 27 corresponds to A, 28 = B, 29 = C, and 30 = D, which is equivalent to 4. Alpha would perform these calculations on her own, and then on the whiteboard she would write:</p><p><strong>Alpha: R D W J F Q S F R J N X F Q N H J</strong></p><p>This process of <em>encryption </em>gives an encrypted text, also called the <strong>ciphertext</strong>. Since Beta knows that Alpha is communicating using a Caesar cipher with a key of 5, he would just need to work backwards. He would convert each letter into a number, subtract 5, and then convert back to a letter. This is the process of <em>decryption</em>. He would be able to calculate that the message reads “M Y R E A L N A M E I S A L I C E”.</p><p>This is a very simple and intuitive cipher, but it is also incredibly easy to break. Suppose Omega wants to eavesdrop on the conversation. She can read the encrypted message from Alpha, since everything is public. Even if she doesn’t know the key of 5, if she suspects they’re using a Caesar cipher, she can easily crack the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/0*rvwFQBFvn5Dbh9vz" /></figure><p>She knows that some letters appear more than others in the English language. ‘E’ is much more common than letters like ‘Z’, ‘Q’, or ‘X’. So if a certain letter shows up quite often in a Caesar Cipher, then it’s a good indicator that it corresponds to the letter E. So if Omega reads the ciphertext, she notices that both F and J appear 3 times each. So she starts by supposing that F corresponds to E. F is 1 letter ahead of E, so if that corresponds to a key of 1. So Omega makes this guess and works backwards from the ciphertext that she sees. She ends up with the following: “Q C V I E P R E Q I M W E P M G I”</p><p>This is nonsense, so Omega concludes the key is not 1, and moves on to her next guess, J. J is 5 letters away from E, so that corresponds to a key of 5. Working backwards, subtracting 5 from each letter, she will get the original text. The code would be cracked in only 2 tries. The Caesar cipher is not a very secure key. Alpha understands this drawback very well, so she decides she will not be using a Caesar cipher.</p><p><strong>The Polyalphabetic Cipher</strong></p><p>Alpha and Beta must agree on a more sophisticated encryption scheme if they are to communicate privately. Instead of a Caesar cipher, Alpha considers the Polyalphabetic cipher (also called the Vigenere Cipher). In this case, instead of choosing a single number (like 5), she is going to pick a longer key, say 1–5–7–6.</p><p>To use this key, she starts by once again lining up the letters of her plain text “M Y R E A L N A M E I S A L I C E” and converting to the appropriate numbers. Next, she will line up her key, which consists of 4 digits, in a repeating pattern below those numbers, and then add the corresponding amount to the original letters. Once again, she can convert these back into letters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/0*568DlUWDodJ4cgiW" /></figure><p>On the whiteboard, Alpha could then write:</p><p><strong>Alpha: N D Y K B Q U G N J P Y B Q P I F</strong></p><p>This is going to be quite a bit harder to crack, since Omega won’t be able to simply look at the frequency of letters and work backwards. However, If Omega is able to guess the <strong>length</strong> of the key, which in this case is 4 (number of digits in 1576), then she can start to look for patterns within those chunks. So she could look at all the letters that are 4 spots apart and see if there are any higher frequency patterns that stand out. This is still breakable, but it’s an extra layer of security that may just deter Omega from trying. Alpha weighs the pros and cons of this approach, and decides this type of cipher is safe enough for her purposes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/0*cXV4I8UbCz1juxAI" /></figure><p>There are many other encryption schemes that could be used. A common one used in modern web security is AES (Advanced Encryption Standard). We will not cover it here, but it is enough to know that like these first two ciphers, it relies on a special “key” that must be kept secret.</p><p>So Alpha has decided on the polyalphabetic cipher, but there is still a major issue with this system. Remember, all communication in this temple is public and takes place on the whiteboard. So how does Alpha communicate to B that she wants to use the Polyalphabetic cipher with a key of 1576? Suppose she writes the following on the board:</p><p><strong>Alpha: Hey Beta, let’s communicate using a polyalphabetic cipher with key 1576</strong></p><p>Since all communication is public, Omega can see this as well, and there is no point in encrypting anything, since the attacker knows the key and the encryption method. So we need a way for Alpha and Beta to generate a secret key that only the two of them will know, <em>and</em><strong> </strong>this must be done while communicating in the open. Sounds impossible, right? Not if you have the right mathematical tools.</p><p>The <a href="https://reganmeloche.medium.com/mathematical-foundations-for-cryptography-part-ii-50a456af317c">second part</a> of this 3-part series will address this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74fddf85badd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing the Policy Difference Engine]]></title>
            <link>https://reganmeloche.medium.com/introducing-the-policy-difference-engine-1114cc444a84?source=rss-c68f8468b934------2</link>
            <guid isPermaLink="false">https://medium.com/p/1114cc444a84</guid>
            <category><![CDATA[computational-law]]></category>
            <category><![CDATA[civictech]]></category>
            <category><![CDATA[canada]]></category>
            <category><![CDATA[rules-as-code]]></category>
            <category><![CDATA[government]]></category>
            <dc:creator><![CDATA[Regan Meloche]]></dc:creator>
            <pubDate>Fri, 27 Nov 2020 18:01:41 GMT</pubDate>
            <atom:updated>2020-11-27T18:01:41.742Z</atom:updated>
            <content:encoded><![CDATA[<h4>An introduction to modeling legislation with Rules as Code</h4><p>I am a developer with Code for Canada, a non-profit organization that works with government partners to deliver better digital services to the public. Every year, Code for Canada selects a cohort of fellows for a 10-month fellowship, lasting from November to August. I have the pleasure of being a developer fellow alongside UX Designer, <a href="https://sidramatik.medium.com/">Sidra</a>, and Product Manager, <a href="https://www.linkedin.com/in/dena-sotoudeh-09724366/">Dena</a>. For the duration of the fellowship, we will be working with <a href="https://www.canada.ca/en/employment-social-development.html">Employment and Social Development Canada (ESDC)</a>. This article is a brief intro to some of the more technical aspects of the project that have emerged during our introductory research, as well as the concept of ‘Rules as Code’, which may play an important role in the project.</p><h3>The Current Landscape</h3><p>ESDC is a government department that handles many public services such as employment insurance, pensions, and disability benefits. These services are bound by a wide variety of legislations, regulations, and policies, which we will generalize here under the term “rules,” which often need to be updated to adjust to new realities. Examples may include changing an age requirement for eligibility of an allowance, changing the dollar amount of a benefit, re-defining what is meant by a “vehicle”, etc. With technologies that directly impact the public growing in complexity, and the population experiencing increasingly diverse living situations, it’s important to keep the rules up to date. One challenge that quickly emerges is effectively measuring the impact of changing a given rule. This is the challenge that ESDC and Code for Canada will be eagerly addressing. At the time of writing (November 2020), we are at the beginning of the fellowship, which will last until August 2021. We will be working closely with ESDC over the next nine months to make measurable progress on the challenge, and then smoothly hand off our work to ESDC at the end of the fellowship.</p><h3>Refining the Project</h3><p>The overarching challenge is to measure the impact of changing a rule. This problem statement is quite general, and can be broken down into different components, and we will focus on two of these. First of all, many rules will be inextricably linked to other rules. When a change is proposed to policy A, and policy A is linked to policy B, C, and D, those three policies must also be investigated to verify if the change will have any unintended side effects. This may further cascade to even more policies, exponentially increasing the amount of investigation. Much of this investigation is manual, so it can be difficult to keep track of all potential side effects.</p><p>The other issue is concretely measuring the impacts that a change may have. This includes the impact on the ability for ESDC to effectively deliver services, the impact on the cost of delivering the service, and — perhaps most importantly — the impact on the people that the rule is written for. Let’s use an income tax rate as a straightforward example. How does changing an income tax rate by a certain amount affect a family of four with a monthly income of $5,000? How does it affect a single person with a monthly income of less than $1,000? There are many cases of households and individuals that must be considered for any proposed change. If we could run simulations on a model population that approximates the population of the area in question, then we’ve made progress towards evidence-based policy. The example of changing income tax is fairly straightforward to reason about, since it largely deals with numbers. This is not the case for all legislation. How would we measure the impact of changing some policy surrounding something more complex, such as changing the definition of a “vehicle” in some rules to include the latest and trendiest means of individual transport in a big city (e.g. motorized scooters)? On the surface, this does not appear to lend itself to a simulation in the same way that a number-heavy rule such as income tax might, and this will likely prove to be one of the more challenging aspects of the project.</p><h3>Rules as Code</h3><p>In order to run simulations efficiently, the rules must be programmed into a computer, which in turn requires that any ambiguities be removed. Many existing rules were not written with this in mind, so the translation from written rules to machine-consumable logic is a prerequisite for the project. This is the concept of Rules as Code (sometimes known as computational law).</p><h4>Example: New Zealand Child Disability Allowance</h4><p><a href="https://www.workandincome.govt.nz/products/a-z-benefits/child-disability-allowance.html">View the requirements for the New Zealand Child Disability Allowance</a>.</p><p>Let’s take an example that comes from a rule in New Zealand concerning the eligibility to receive a child disability allowance. The requirements can be broken down into the following components:</p><ul><li>You are a NZ citizen or resident</li><li>You are the principal caregiver</li><li>The child meets the eligibility criteria</li><li>Both you and the child should ordinarily reside in New Zealand</li></ul><p>Each of these components can in turn be broken down into even simpler requirements. For example, what does it mean for the child to meet the eligibility criteria? First of all, they must meet the age requirement of being under 18. On top of that, they must meet the definition of disability, which upon further digging, is outlined in the Social Security Act of 1964. The child must also require constant care and attention. This particular piece is not rooted in any sort of threshold number or existing act, but is simply an observed fact. Finally, they must also meet the requirement that they are expected to have the disability for a minimum of 12 months. This is another threshold value. If we flesh out all of these requirements, we can build a tree-like structure that breaks each rule down into its atomic components:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Udfc3quSKCvtS_u-" /><figcaption><em>A flow diagram that shows how eligibility is determined for child disability allowance. This diagram demonstrates that residential status (resident/citizen), caregiver role/guardianship, as well as the child’s needs and months certified with disability status can all impact eligibility for this particular benefit.</em></figcaption></figure><p>So we can see that ultimately these can break down into observed true/false facts (requires constant care and attention), facts based in prior legislation (definition of disability), or defined threshold values (age of requirement). This breakdown is starting to look much more machine-friendly and we could very easily capture these rules in computer code, thus giving us the concept of ‘Rules as Code’ (RaC). Again, note that this will not be the case with just any rule. Part of the challenge will be to identify rules that are suitable for this translation process.</p><h4>Using the Transformed Rule</h4><p>Once a given rule has been converted into RaC, we open up all the possibilities that come with any coded system. We can easily check for eligibility, we can make illustrative visualizations, and most applicable to us — we can run simulations on a model population. To this end, we can create a list of test cases or personas. This may be a collection of households with different configurations of ages, children, incomes, disability statuses, etc. Once these test cases are in the system, we can represent a rule change by toggling values in the coded rules, and quantify the impact on the different personas.</p><h4>Ethical and Legal Considerations</h4><p>At this point it is worth pointing out an important ethical consideration. When modelling humans as data, we must always be careful about the data we are considering and the data we are not considering. A simulation on a model may show a net positive effect on the population in terms of overall cost, but if for example, we leave out key pieces of data (race, gender, age), and it turns out that a policy change actually negatively affects one group, then the simulation is largely incomplete. For this article, we will leave aside the discussion on whether <em>any </em>model may be better than no model at all, but it is an important consideration, and the discussion of any important omissions and oversights must remain part of the conversation.</p><p>One more issue with translating written rules to RaC is the preservation of legal intent. Again, most rules were not written with automation in mind, and automation demands clear rules without ambiguities. An ambiguous rule cannot be encoded into a rule without some bias on the translator’s part, so an awareness of this is important. On a positive note, the act of translating may in fact bring to light these ambiguities and encourage an official clarification of the rule.</p><h3>Next Steps</h3><p>We’ve defined the high-level goal of the solution as measuring the impact of changing rules. We can break this down into two more concrete subgoals:</p><ul><li>Finding relevant connections between different rules</li><li>Running valid simulations on proposed rule changes</li></ul><p>With these defined, the next step will be to outline a general solution, which will be part of what we call a Policy Difference Engine (PDE). This proposed tool (or set of tools) may be primarily used by someone responsible for proposing changes to existing legislation, regulations, or policies. In a future article, we hope to present this outline, and explore some technologies that may help serve the needs of the PDE.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1114cc444a84" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>