From 60e3ce1c960aaa42a8481d3ffc2b15056bb56753 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 18:19:02 +0000 Subject: [PATCH] ocaml: phase 5.1 xor_cipher.ml baseline (XOR roll-key encryption, round-trip = 601) For each character, XOR with the corresponding key char (key cycled via 'i mod kn'): let xor_cipher key text = let buf = Buffer.create n in for i = 0 to n - 1 do let c = Char.code text.[i] in let k = Char.code key.[i mod kn] in Buffer.add_string buf (String.make 1 (Char.chr (c lxor k))) done; Buffer.contents buf XOR is its own inverse, so encrypt + decrypt with the same key yields the original. Test combines: - String.length decoded = 6 - decoded = 'Hello!' -> 1 - 6 * 100 + 1 = 601 Tests Char.code + Char.chr round-trip, the iter-127 lxor operator, Buffer.add_string + String.make 1, and key-cycling via mod. 90 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/xor_cipher.ml | 16 ++++++++++++++++ plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 24 insertions(+) create mode 100644 lib/ocaml/baseline/xor_cipher.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 2601a34b..b767248a 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -80,6 +80,7 @@ "subset_sum.ml": 8, "tic_tac_toe.ml": 1, "word_freq.ml": 8, + "xor_cipher.ml": 601, "zigzag.ml": 55, "zip_unzip.ml": 1000, "sieve.ml": 15, diff --git a/lib/ocaml/baseline/xor_cipher.ml b/lib/ocaml/baseline/xor_cipher.ml new file mode 100644 index 00000000..61ca6775 --- /dev/null +++ b/lib/ocaml/baseline/xor_cipher.ml @@ -0,0 +1,16 @@ +let xor_cipher key text = + let n = String.length text in + let kn = String.length key in + let buf = Buffer.create n in + for i = 0 to n - 1 do + let c = Char.code text.[i] in + let k = Char.code key.[i mod kn] in + Buffer.add_string buf (String.make 1 (Char.chr (c lxor k))) + done; + Buffer.contents buf + +;; + +let encoded = xor_cipher "key" "Hello!" in +let decoded = xor_cipher "key" encoded in +String.length decoded * 100 + (if decoded = "Hello!" then 1 else 0) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 10c89eb5..3e1b5c6f 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — xor_cipher.ml baseline (XOR roll-key + encryption, round-trip → 601). For each character, XOR with the + corresponding key char (key cycled via `i mod kn`). Encrypts + "Hello!" with key "key", decrypts the result, and verifies the + round-trip preserves both length (6) and equality. Tests + Char.code + Char.chr round-trip + the iter-127 `lxor` operator + + Buffer.add_string + String.make 1. 90 baseline programs total. - 2026-05-09 Phase 5.1 — simpson_int.ml baseline (Simpson's rule numerical integration, ∫₀¹ x² dx ≈ 1/3, scaled = 10000). Composite Simpson's 1/3 rule with 100 panels. Coefficients 1-4-2-...-2-4-1