summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonsykkel <jonrevold@gmail.com>2021-09-18 16:46:20 +0200
committerjonsykkel <jonrevold@gmail.com>2021-09-18 16:46:20 +0200
commit50d1ad7483665bcfa1859e48272a34038986cf1e (patch)
treecd5ad69063d8e45c584942dcae2eae80682933b1
parent8cf5f098d2aaa6c6472184245e3a3c9d56ea2bc2 (diff)
downloadokeffa-50d1ad7483665bcfa1859e48272a34038986cf1e.tar.gz
mod mul
-rw-r--r--calc/main.c13
-rw-r--r--inc/okeffa/fz_arith.h2
-rw-r--r--makefile2
-rw-r--r--prog/ch61
-rw-r--r--src/fz_mod.c17
5 files changed, 33 insertions, 2 deletions
diff --git a/calc/main.c b/calc/main.c
index 7397522..4c64e3f 100644
--- a/calc/main.c
+++ b/calc/main.c
@@ -225,6 +225,18 @@ static void op_normal(char c){
break;
////////////////////////////////////////////////////////////////
+ //modular arithmetic
+ ////////////////////////////////////////////////////////////////
+
+ case 'M': //modular multiply
+ want(3);
+ mustnotzero(STACK(sp));
+ fz_mod_mul(STACK(sp-2),STACK(sp-1),STACK(sp),STACK(sp-2),fl);
+ drop();
+ drop();
+ break;
+
+ ////////////////////////////////////////////////////////////////
//bitwise ops
////////////////////////////////////////////////////////////////
@@ -285,7 +297,6 @@ static void op_normal(char c){
puts_stack(x);
}
exit(0);
- break;
////////////////////////////////////////////////////////////////
//immediates
diff --git a/inc/okeffa/fz_arith.h b/inc/okeffa/fz_arith.h
index d472eba..b5f5d88 100644
--- a/inc/okeffa/fz_arith.h
+++ b/inc/okeffa/fz_arith.h
@@ -13,4 +13,6 @@ void fz_idiv (word_t *a,word_t *b,word_t *q,word_t *r,size_
void fz_div (word_t *a,word_t *b,word_t *q,size_t len);
void fz_mod (word_t *a,word_t *b,word_t *r,size_t len);
+void fz_mod_mul (word_t *a,word_t *b,word_t *m,word_t *o,size_t len);
+
#endif
diff --git a/makefile b/makefile
index 27cf191..e19a095 100644
--- a/makefile
+++ b/makefile
@@ -66,7 +66,7 @@ out := $(bin_dir)/$(out)
ifneq ($(run),)
run_cmd := @echo "run $(out)" 1>&2
#run_cmd += && ./$(out) 256 16
- run_cmd += && cat prog/ch5_input prog/ch5_hw1 | $(out) 256 16
+ run_cmd += && cat prog/ch6 | $(out) 256 16
endif
ifneq ($(debug),)
diff --git a/prog/ch6 b/prog/ch6
new file mode 100644
index 0000000..b88b94e
--- /dev/null
+++ b/prog/ch6
@@ -0,0 +1 @@
+.4.5.7MQ
diff --git a/src/fz_mod.c b/src/fz_mod.c
new file mode 100644
index 0000000..7fa6e30
--- /dev/null
+++ b/src/fz_mod.c
@@ -0,0 +1,17 @@
+#include <okeffa/fz_arith.h>
+#include <okeffa/fz_basic.h>
+
+void fz_mod_mul(word_t *a,word_t *b,word_t *m,word_t *o,size_t len){
+ size_t len2 = len*2;
+ word_t xy[len2];
+ word_t m2[len2];
+ word_t *xy_lo = xy;
+ word_t *xy_hi = xy+len;
+
+ fz_copy(m,m2,len);
+ fz_clear(m2+len,len);
+ fz_mul_egypt(a,b,xy_lo,xy_hi,len);
+ fz_mod(xy,m2,xy,len2);
+ fz_copy(xy_lo,o,len);
+}
+