about summary refs log tree commit diff
path: root/usth/ICT3.4/powmod.scm
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 /usth/ICT3.4/powmod.scm
parent4e280d6460a16b971ca76da632c9b20e55812f0c (diff)
downloadcp-82048ad9e3e4a96a38c8fa6a529798f40e33acb1.tar.gz
[usth/ICT3.4] Secure information
Diffstat (limited to 'usth/ICT3.4/powmod.scm')
-rw-r--r--usth/ICT3.4/powmod.scm9
1 files changed, 9 insertions, 0 deletions
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)