-
-
Notifications
You must be signed in to change notification settings - Fork 406
[#763] New practice exercise affine-cipher
#773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
55595a9
[#763] New practice exercise
jiegillet a1bea74
Update @spec type
jiegillet 28fbe63
Change practice concept
jiegillet 64b46ec
Merge branch 'main' into jie-affine-cipher
jiegillet cb28980
Delete boostrap_practice_exercise.exs
jiegillet ae76d5a
Apply suggestions from code review
jiegillet ab20dbd
Make a type for key
jiegillet c4c5ed5
Typo
jiegillet c9fc692
I should not rush
jiegillet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2464,6 +2464,33 @@ | |
| "multiple-clause-matching" | ||
| ], | ||
| "difficulty": 5 | ||
| }, | ||
| { | ||
| "slug": "affine-cipher", | ||
| "name": "Affine Cipher", | ||
| "uuid": "18d12bac-ead0-456c-bcef-ccb2fd06fe72", | ||
| "prerequisites": [ | ||
| "pattern-matching", | ||
| "guards", | ||
| "multiple-clause-functions", | ||
| "atoms", | ||
| "pipe-operator", | ||
| "enum", | ||
| "integers", | ||
| "if", | ||
jiegillet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "cond", | ||
| "case", | ||
| "strings", | ||
| "tuples", | ||
| "maps", | ||
| "list-comprehensions", | ||
| "charlists" | ||
| ], | ||
| "practices": [ | ||
| "pipe-operator", | ||
| "strings" | ||
| ], | ||
|
Comment on lines
+2489
to
+2492
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a lot going on in this exercise, so I find it really hard to say what "practices" would be best. I think those make sense. |
||
| "difficulty": 5 | ||
| } | ||
| ], | ||
| "foregone": [ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Description | ||
|
|
||
| Create an implementation of the affine cipher, | ||
| an ancient encryption system created in the Middle East. | ||
|
|
||
| The affine cipher is a type of monoalphabetic substitution cipher. | ||
| Each character is mapped to its numeric equivalent, encrypted with | ||
| a mathematical function and then converted to the letter relating to | ||
| its new numeric value. Although all monoalphabetic ciphers are weak, | ||
| the affine cypher is much stronger than the atbash cipher, | ||
| because it has many more keys. | ||
|
|
||
| The encryption function is: | ||
|
|
||
| `E(x) = (ax + b) mod m` | ||
| - where `x` is the letter's index from 0 - length of alphabet - 1 | ||
| - `m` is the length of the alphabet. For the roman alphabet `m == 26`. | ||
| - and `a` and `b` make the key | ||
|
|
||
| The decryption function is: | ||
|
|
||
| `D(y) = a^-1(y - b) mod m` | ||
| - where `y` is the numeric value of an encrypted letter, ie. `y = E(x)` | ||
| - it is important to note that `a^-1` is the modular multiplicative inverse | ||
| of `a mod m` | ||
| - the modular multiplicative inverse of `a` only exists if `a` and `m` are | ||
| coprime. | ||
|
|
||
| To find the MMI of `a`: | ||
|
|
||
| `an mod m = 1` | ||
| - where `n` is the modular multiplicative inverse of `a mod m` | ||
|
|
||
| More information regarding how to find a Modular Multiplicative Inverse | ||
| and what it means can be found [here.](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) | ||
|
|
||
| Because automatic decryption fails if `a` is not coprime to `m` your | ||
| program should return status 1 and `"Error: a and m must be coprime."` | ||
| if they are not. Otherwise it should encode or decode with the | ||
| provided key. | ||
|
|
||
| The Caesar (shift) cipher is a simple affine cipher where `a` is 1 and | ||
| `b` as the magnitude results in a static displacement of the letters. | ||
| This is much less secure than a full implementation of the affine cipher. | ||
|
|
||
| Ciphertext is written out in groups of fixed length, the traditional group | ||
| size being 5 letters, and punctuation is excluded. This is to make it | ||
| harder to guess things based on word boundaries. | ||
|
|
||
| ## General Examples | ||
|
|
||
| - Encoding `test` gives `ybty` with the key a=5 b=7 | ||
| - Decoding `ybty` gives `test` with the key a=5 b=7 | ||
| - Decoding `ybty` gives `lqul` with the wrong key a=11 b=7 | ||
| - Decoding `kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx` | ||
| - gives `thequickbrownfoxjumpsoverthelazydog` with the key a=19 b=13 | ||
| - Encoding `test` with the key a=18 b=13 | ||
| - gives `Error: a and m must be coprime.` | ||
| - because a and m are not relatively prime | ||
|
|
||
| ## Examples of finding a Modular Multiplicative Inverse (MMI) | ||
|
|
||
| - simple example: | ||
| - `9 mod 26 = 9` | ||
| - `9 * 3 mod 26 = 27 mod 26 = 1` | ||
| - `3` is the MMI of `9 mod 26` | ||
| - a more complicated example: | ||
| - `15 mod 26 = 15` | ||
| - `15 * 7 mod 26 = 105 mod 26 = 1` | ||
| - `7` is the MMI of `15 mod 26` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Used by "mix format" | ||
| [ | ||
| inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "authors": ["jiegillet"], | ||
| "contributors": [], | ||
| "files": { | ||
| "example": [ | ||
| ".meta/example.ex" | ||
| ], | ||
| "solution": [ | ||
| "lib/affine_cipher.ex" | ||
| ], | ||
| "test": [ | ||
| "test/affine_cipher_test.exs" | ||
| ] | ||
| }, | ||
| "blurb": "Create an implementation of the Affine cipher, an ancient encryption algorithm from the Middle East.", | ||
| "source": "Wikipedia", | ||
| "source_url": "http://en.wikipedia.org/wiki/Affine_cipher" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| defmodule AffineCipher do | ||
| @typedoc """ | ||
| A type for the encryption key | ||
| """ | ||
| @type key() :: %{a: integer, b: integer} | ||
|
|
||
| @alphabet_size 26 | ||
| @ignored ~r/[ ,.]/ | ||
|
|
||
| @doc """ | ||
| Encode an encrypted message using a key | ||
| """ | ||
| @spec encode(key :: key(), message :: String.t()) :: {:ok, String.t()} | {:error, String.t()} | ||
| def(encode(%{a: a, b: b}, message)) do | ||
| if Integer.gcd(a, @alphabet_size) != 1 do | ||
| {:error, "a and m must be coprime."} | ||
| else | ||
| encrypted = | ||
| message | ||
| |> String.downcase() | ||
| |> String.replace(@ignored, "") | ||
| |> to_charlist() | ||
| |> Enum.map(fn | ||
| digit when ?0 <= digit and digit <= ?9 -> digit | ||
| char -> Integer.mod(a * (char - ?a) + b, @alphabet_size) + ?a | ||
| end) | ||
| |> Enum.chunk_every(5) | ||
| |> Enum.map_join(" ", &to_string/1) | ||
|
|
||
| {:ok, encrypted} | ||
| end | ||
| end | ||
|
|
||
| @doc """ | ||
| Decode an encrypted message using a key | ||
| """ | ||
| @spec decode(key :: key(), message :: String.t()) :: {:ok, String.t()} | {:error, String.t()} | ||
| def decode(%{a: a, b: b}, encrypted) do | ||
| if Integer.gcd(a, @alphabet_size) != 1 do | ||
| {:error, "a and m must be coprime."} | ||
| else | ||
| mmi = modular_multiplicative_inverse(a, @alphabet_size) | ||
|
|
||
| message = | ||
| encrypted | ||
| |> String.replace(@ignored, "") | ||
| |> to_charlist() | ||
| |> Enum.map(fn | ||
| digit when ?0 <= digit and digit <= ?9 -> digit | ||
| char -> Integer.mod(mmi * (char - ?a - b), @alphabet_size) + ?a | ||
| end) | ||
| |> to_string | ||
|
|
||
| {:ok, message} | ||
| end | ||
| end | ||
|
|
||
| def modular_multiplicative_inverse(a, m) do | ||
| modular_multiplicative_inverse(a, m, 1, 0) | ||
| |> Integer.mod(m) | ||
| end | ||
|
|
||
| def modular_multiplicative_inverse(0, r0, _t1, _t0) when r0 > 1, do: raise("Not invertible") | ||
| def modular_multiplicative_inverse(0, _r0, _t1, t0), do: t0 | ||
|
|
||
| def modular_multiplicative_inverse(r1, r0, t1, t0) do | ||
| q = div(r0, r1) | ||
| modular_multiplicative_inverse(r0 - q * r1, r1, t0 - q * t1, t1) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # This is an auto-generated file. | ||
| # | ||
| # Regenerating this file via `configlet sync` will: | ||
| # - Recreate every `description` key/value pair | ||
| # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
| # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
| # - Preserve any other key/value pair | ||
| # | ||
| # As user-added comments (using the # character) will be removed when this file | ||
| # is regenerated, comments can be added via a `comment` key. | ||
| [2ee1d9af-1c43-416c-b41b-cefd7d4d2b2a] | ||
| description = "encode -> encode yes" | ||
|
|
||
| [785bade9-e98b-4d4f-a5b0-087ba3d7de4b] | ||
| description = "encode -> encode no" | ||
|
|
||
| [2854851c-48fb-40d8-9bf6-8f192ed25054] | ||
| description = "encode -> encode OMG" | ||
|
|
||
| [bc0c1244-b544-49dd-9777-13a770be1bad] | ||
| description = "encode -> encode O M G" | ||
|
|
||
| [381a1a20-b74a-46ce-9277-3778625c9e27] | ||
| description = "encode -> encode mindblowingly" | ||
|
|
||
| [6686f4e2-753b-47d4-9715-876fdc59029d] | ||
| description = "encode -> encode numbers" | ||
|
|
||
| [ae23d5bd-30a8-44b6-afbe-23c8c0c7faa3] | ||
| description = "encode -> encode deep thought" | ||
|
|
||
| [c93a8a4d-426c-42ef-9610-76ded6f7ef57] | ||
| description = "encode -> encode all the letters" | ||
|
|
||
| [0673638a-4375-40bd-871c-fb6a2c28effb] | ||
| description = "encode -> encode with a not coprime to m" | ||
|
|
||
| [3f0ac7e2-ec0e-4a79-949e-95e414953438] | ||
| description = "decode -> decode exercism" | ||
|
|
||
| [241ee64d-5a47-4092-a5d7-7939d259e077] | ||
| description = "decode -> decode a sentence" | ||
|
|
||
| [33fb16a1-765a-496f-907f-12e644837f5e] | ||
| description = "decode -> decode numbers" | ||
|
|
||
| [20bc9dce-c5ec-4db6-a3f1-845c776bcbf7] | ||
| description = "decode -> decode all the letters" | ||
|
|
||
| [623e78c0-922d-49c5-8702-227a3e8eaf81] | ||
| description = "decode -> decode with no spaces in input" | ||
|
|
||
| [58fd5c2a-1fd9-4563-a80a-71cff200f26f] | ||
| description = "decode -> decode with too many spaces" | ||
|
|
||
| [b004626f-c186-4af9-a3f4-58f74cdb86d5] | ||
| description = "decode -> decode with a not coprime to m" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| defmodule AffineCipher do | ||
| @typedoc """ | ||
| A type for the encryption key | ||
| """ | ||
| @type key() :: %{a: integer, b: integer} | ||
|
|
||
| @doc """ | ||
| Encode an encrypted message using a key | ||
| """ | ||
| @spec encode(key :: key(), message :: String.t()) :: {:ok, String.t()} | {:error, String.t()} | ||
| def encode(%{a: a, b: b}, message) do | ||
| end | ||
|
|
||
| @doc """ | ||
| Decode an encrypted message using a key | ||
| """ | ||
| @spec decode(key :: key(), message :: String.t()) :: {:ok, String.t()} | {:error, String.t()} | ||
| def decode(%{a: a, b: b}, encrypted) do | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| defmodule AffineCipher.MixProject do | ||
| use Mix.Project | ||
|
|
||
| def project do | ||
| [ | ||
| app: :affine_cipher, | ||
| version: "0.1.0", | ||
| # elixir: "~> 1.8", | ||
| start_permanent: Mix.env() == :prod, | ||
| deps: deps() | ||
| ] | ||
| end | ||
|
|
||
| # Run "mix help compile.app" to learn about applications. | ||
| def application do | ||
| [ | ||
| extra_applications: [:logger] | ||
| ] | ||
| end | ||
|
|
||
| # Run "mix help deps" to learn about dependencies. | ||
| defp deps do | ||
| [ | ||
| # {:dep_from_hexpm, "~> 0.3.0"}, | ||
| # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} | ||
| ] | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.