Skip to content

Commit

Permalink
Add atbash-cipher exercise (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 21, 2024
1 parent 783616c commit a469dda
Show file tree
Hide file tree
Showing 12 changed files with 3,665 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
"uuid": "309d7449-4f2e-40cb-b38b-faedcf2b88bb",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "isbn-verifier",
"name": "ISBN Verifier",
Expand Down
27 changes: 27 additions & 0 deletions exercises/practice/atbash-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Instructions

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards.
The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

```text
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba
```

It is a very weak cipher because it only has one possible key, and it is a simple mono-alphabetic substitution cipher.
However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, leaving numbers unchanged, and punctuation is excluded.
This is to make it harder to guess things based on word boundaries.
All text will be encoded as lowercase letters.

## Examples

- Encoding `test` gives `gvhg`
- Encoding `x123 yes` gives `c123b vh`
- Decoding `gvhg` gives `test`
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`
19 changes: 19 additions & 0 deletions exercises/practice/atbash-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"atbash_cipher.s"
],
"test": [
"atbash_cipher_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Atbash"
}
51 changes: 51 additions & 0 deletions exercises/practice/atbash-cipher/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.text
.globl encode
.globl decode

/* extern void encode(char *buffer, const char *phrase); */
encode:
mov x2, #5 /* no groups */
b process
ret

/* extern void decode(char *buffer, const char *phrase); */
decode:
mov x2, #-1 /* no groups */
b process

process:
mov x3, x2 /* remaining letters in group */
mov w6, 32 /* space */
mov w7, #122 /* 'z' */

.read:
ldrb w4, [x1], #1 /* load byte, post-increment */
cbz w4, .end

sub w5, w4, #48 /* '0' */
cmp w5, #10
blo .accept /* unsigned < */

orr w5, w4, #32 /* force lower case */
sub w5, w5, 97 /* 'a' */
cmp w5, #26
bhs .read /* unsigned >= */

sub w5, w7, w5 /* 'z' - letter index */
and w4, w4, #32
orr w4, w4, w5 /* rotated letter, with original case */

.accept:
cbnz x3, .write

strb w6, [x0], #1 /* store space, post-increment */
mov x3, x2 /* remaining letters in group */

.write:
strb w4, [x0], #1 /* store byte, post-increment */
sub x3, x3, 1 /* decrement remaining letters in group */
b .read

.end:
strb w4, [x0] /* store byte */
ret
52 changes: 52 additions & 0 deletions exercises/practice/atbash-cipher/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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.

[2f47ebe1-eab9-4d6b-b3c6-627562a31c77]
description = "encode -> encode yes"

[b4ffe781-ea81-4b74-b268-cc58ba21c739]
description = "encode -> encode no"

[10e48927-24ab-4c4d-9d3f-3067724ace00]
description = "encode -> encode OMG"

[d59b8bc3-509a-4a9a-834c-6f501b98750b]
description = "encode -> encode spaces"

[31d44b11-81b7-4a94-8b43-4af6a2449429]
description = "encode -> encode mindblowingly"

[d503361a-1433-48c0-aae0-d41b5baa33ff]
description = "encode -> encode numbers"

[79c8a2d5-0772-42d4-b41b-531d0b5da926]
description = "encode -> encode deep thought"

[9ca13d23-d32a-4967-a1fd-6100b8742bab]
description = "encode -> encode all the letters"

[bb50e087-7fdf-48e7-9223-284fe7e69851]
description = "decode -> decode exercism"

[ac021097-cd5d-4717-8907-b0814b9e292c]
description = "decode -> decode a sentence"

[18729de3-de74-49b8-b68c-025eaf77f851]
description = "decode -> decode numbers"

[0f30325f-f53b-415d-ad3e-a7a4f63de034]
description = "decode -> decode all the letters"

[39640287-30c6-4c8c-9bac-9d613d1a5674]
description = "decode -> decode with too many spaces"

[b34edf13-34c0-49b5-aa21-0768928000d5]
description = "decode -> decode with no spaces"
36 changes: 36 additions & 0 deletions exercises/practice/atbash-cipher/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
9 changes: 9 additions & 0 deletions exercises/practice/atbash-cipher/atbash_cipher.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.text
.globl encode
.globl decode

encode:
ret

decode:
ret
143 changes: 143 additions & 0 deletions exercises/practice/atbash-cipher/atbash_cipher_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "vendor/unity.h"

#define BUFFER_SIZE 80

extern void encode(char *buffer, const char *phrase);

extern void decode(char *buffer, const char *phrase);

void setUp(void) {
}

void tearDown(void) {
}

void test_encode_yes(void) {
char buffer[BUFFER_SIZE];

encode(buffer, "yes");
TEST_ASSERT_EQUAL_STRING("bvh", buffer);
}

void test_encode_no(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

encode(buffer, "no");
TEST_ASSERT_EQUAL_STRING("ml", buffer);
}

void test_encode_omg(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

encode(buffer, "OMG");
TEST_ASSERT_EQUAL_STRING("lnt", buffer);
}

void test_encode_spaces(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

encode(buffer, "O M G");
TEST_ASSERT_EQUAL_STRING("lnt", buffer);
}

void test_encode_mindblowingly(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

encode(buffer, "mindblowingly");
TEST_ASSERT_EQUAL_STRING("nrmwy oldrm tob", buffer);
}

void test_encode_numbers(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

encode(buffer, "Testing,1 2 3, testing.");
TEST_ASSERT_EQUAL_STRING("gvhgr mt123 gvhgr mt", buffer);
}

void test_encode_deep_thought(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

encode(buffer, "Truth is fiction.");
TEST_ASSERT_EQUAL_STRING("gifgs rhurx grlm", buffer);
}

void test_encode_all_the_letters(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

encode(buffer, "The quick brown fox jumps over the lazy dog.");
TEST_ASSERT_EQUAL_STRING("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt", buffer);
}

void test_decode_exercism(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

decode(buffer, "vcvix rhn");
TEST_ASSERT_EQUAL_STRING("exercism", buffer);
}

void test_decode_a_sentence(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

decode(buffer, "zmlyh gzxov rhlug vmzhg vkkrm thglm v");
TEST_ASSERT_EQUAL_STRING("anobstacleisoftenasteppingstone", buffer);
}

void test_decode_numbers(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

decode(buffer, "gvhgr mt123 gvhgr mt");
TEST_ASSERT_EQUAL_STRING("testing123testing", buffer);
}

void test_decode_all_the_letters(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

decode(buffer, "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
TEST_ASSERT_EQUAL_STRING("thequickbrownfoxjumpsoverthelazydog", buffer);
}

void test_decode_with_too_many_spaces(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

decode(buffer, "vc vix r hn");
TEST_ASSERT_EQUAL_STRING("exercism", buffer);
}

void test_decode_with_no_spaces(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

decode(buffer, "zmlyhgzxovrhlugvmzhgvkkrmthglmv");
TEST_ASSERT_EQUAL_STRING("anobstacleisoftenasteppingstone", buffer);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_encode_yes);
RUN_TEST(test_encode_no);
RUN_TEST(test_encode_omg);
RUN_TEST(test_encode_spaces);
RUN_TEST(test_encode_mindblowingly);
RUN_TEST(test_encode_numbers);
RUN_TEST(test_encode_deep_thought);
RUN_TEST(test_encode_all_the_letters);
RUN_TEST(test_decode_exercism);
RUN_TEST(test_decode_a_sentence);
RUN_TEST(test_decode_numbers);
RUN_TEST(test_decode_all_the_letters);
RUN_TEST(test_decode_with_too_many_spaces);
RUN_TEST(test_decode_with_no_spaces);
return UNITY_END();
}
Loading

0 comments on commit a469dda

Please sign in to comment.