about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2020-11-14 11:10:22 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2020-11-14 11:10:22 +0700
commit82048ad9e3e4a96a38c8fa6a529798f40e33acb1 (patch)
treead1d22488baa85f0b0f6bde25cfd75375eeda5b6
parent4e280d6460a16b971ca76da632c9b20e55812f0c (diff)
downloadcp-82048ad9e3e4a96a38c8fa6a529798f40e33acb1.tar.gz
[usth/ICT3.4] Secure information
-rw-r--r--.gitignore1
-rw-r--r--usth/ICT3.4/a51.c54
-rw-r--r--usth/ICT3.4/powmod.scm9
-rw-r--r--usth/ICT3.4/pset.pdfbin0 -> 70440 bytes
-rw-r--r--usth/ICT3.4/rsa.lsp20
5 files changed, 84 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 3d894d7..1338573 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 *~
 *.fasl
+a.out
diff --git a/usth/ICT3.4/a51.c b/usth/ICT3.4/a51.c
new file mode 100644
index 0000000..7925f02
--- /dev/null
+++ b/usth/ICT3.4/a51.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+
+#define READ(t, n) for (int i = 0; i < n; ++i) t = t << 1 | (getchar() ^ 48)
+#define POW2(t) (1 << (t))
+
+#define MAXX POW2(18)
+#define MAXY POW2(21)
+#define MAXZ POW2(22)
+
+#define CLOCKX(x) ((x) & POW2(10))
+#define CLOCKY(y) ((y) & POW2(11))
+#define CLOCKZ(z) ((z) & POW2(12))
+
+#define BIT(n, t) (((t) & POW2(n)) != 0)
+
+#define TAPX(x) ((BIT(0, x) ^ BIT(1, x) ^ BIT(2, x) ^ BIT(5, x)) << 18)
+#define TAPY(y) ((BIT(0, y) ^ BIT(1, y)) << 21)
+#define TAPZ(z) ((BIT(0, z) ^ BIT(1, z) ^ BIT(2, z) ^ BIT(15, z)) << 22)
+
+int main()
+{
+	int x = 0, y = 0, z = 0;
+	READ(x, 19);
+	getchar();  /* assume one-char sep */
+	READ(y, 22);
+	getchar();  /* assume one-char sep */
+	READ(z, 23);
+
+	int n;
+	scanf("%d", &n);
+	while (n--) {
+		int minor = !CLOCKX(x) + !CLOCKY(y) + !CLOCKZ(z) > 1;
+		putchar(((x ^ y ^ z) & 1) + '0');
+
+		if (minor == !CLOCKX(x)) {
+			x >>= 1;
+			x &= ~MAXX;
+			x |= TAPX(x);
+		}
+		if (minor == !CLOCKY(y)) {
+			y >>= 1;
+			y &= ~MAXY;
+			y |= TAPY(y);
+		}
+		if (minor == !CLOCKZ(z)) {
+			z >>= 1;
+			z &= z & ~MAXZ;
+			z |= TAPZ(z);
+		}
+	}
+
+	putchar(10);
+	return 0;
+}
diff --git a/usth/ICT3.4/powmod.scm b/usth/ICT3.4/powmod.scm
new file mode 100644
index 0000000..b21e995
--- /dev/null
+++ b/usth/ICT3.4/powmod.scm
@@ -0,0 +1,9 @@
+(define (square x) (* x x))
+(display
+  (let powmod ((x (read)) (e (read)) (m (read)))
+    ; let's ignore negative e here
+    (cond ((= e 0) 1)
+          ((= e 1) (remainder x m))
+          ((even? e) (remainder (square (powmod x (/ e 2) m)) m))
+          (else (remainder (* (square (powmod x (quotient e 2) m)) x) m)))))
+(newline)
diff --git a/usth/ICT3.4/pset.pdf b/usth/ICT3.4/pset.pdf
new file mode 100644
index 0000000..e7af75f
--- /dev/null
+++ b/usth/ICT3.4/pset.pdf
Binary files differdiff --git a/usth/ICT3.4/rsa.lsp b/usth/ICT3.4/rsa.lsp
new file mode 100644
index 0000000..0461ea3
--- /dev/null
+++ b/usth/ICT3.4/rsa.lsp
@@ -0,0 +1,20 @@
+(defun square (x) (* x x))
+
+(defun modexpt (x e m)
+  (cond ((= e 0) 1)
+        ((= e 1) (mod x m))
+        ((evenp e) (mod (square (modexpt x (/ e 2) m)) m))
+        (t (mod (* (square (modexpt x (floor e 2) m)) x) m))))
+
+(defun modinv (x m &optional (b m) (a 0) (u 1))
+  (if (= x 0)
+      (when (= b 1) (mod a m))
+      (modinv (mod b x) m x u (- a (* u (floor b x))))))
+
+(defun rsa (p q &optional e d m c)
+  (let ((n (* p q))
+        (l (* (1- p) (1- q))))
+    (cond ((not e) (rsa p q (modinv d l) d m c))
+          ((not d) (rsa p q e (modinv e l) m c))
+          ((not m) (modexpt c d n))
+          ((not c) (modexpt m e n)))))