[Cryptech-Commits] [user/shatov/modexpng] 09/09: Added optional output of intermediate quantities for debugging. Reworked index rotation code for better readability.

git at cryptech.is git at cryptech.is
Sat Mar 23 08:10:38 UTC 2019


This is an automated email from the git hooks/post-receive script.

meisterpaul1 at yandex.ru pushed a commit to branch master
in repository user/shatov/modexpng.

commit 701e3f1670042c9f9c7c76c529eac43802b20c24
Author: Pavel V. Shatov (Meister) <meisterpaul1 at yandex.ru>
AuthorDate: Sat Mar 23 11:06:42 2019 +0300

    Added optional output of intermediate quantities for debugging.
    Reworked index rotation code for better readability.
---
 modexpng_fpga_model.py | 61 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 54 insertions(+), 7 deletions(-)

diff --git a/modexpng_fpga_model.py b/modexpng_fpga_model.py
index 54db95f..d3b7841 100644
--- a/modexpng_fpga_model.py
+++ b/modexpng_fpga_model.py
@@ -71,6 +71,15 @@ _VECTOR_PATH = "/vector"
 _VECTOR_CLASS = "Vector"
 
 
+# ------------------
+# Debugging Settings
+# ------------------
+DUMP_VECTORS = False
+DUMP_INDICES = False
+DUMP_MACS_CLEARING = False
+DUMP_MACS_ACCUMULATION = True
+
+
 #
 # Multi-Precision Integer
 #
@@ -94,6 +103,15 @@ class ModExpNG_Operand():
 
             self._init_from_words(words, length)
 
+    def format_verilog_concat(self, name):
+
+        for i in range(len(self.words)):
+            if i > 0:
+                if (i % 4) == 0: print("")
+                else:            print(" ", end='')
+            print("%s[%2d] = 17'h%05x;" % (name, i, self.words[i]), end='')
+        print("")
+
     def _init_from_words(self, words, count):
 
         for i in range(count):
@@ -315,14 +333,17 @@ class ModExpNG_WordMultiplier():
 
     def _rotate_indices(self, num_words):
         for x in range(len(self._indices)):
-            self._indices[x] -= 1
-            if self._indices[x] < 0:
-                self._indices[x] += num_words
+            if self._indices[x] > 0:
+                self._indices[x] -= 1
+            else:
+                self._indices[x] = num_words - 1
 
     def _rotate_index_aux(self):
         self._index_aux[0] -= 1
 
-    def multiply_square(self, a_wide, b_narrow, ab_num_words):
+    def multiply_square(self, a_wide, b_narrow, ab_num_words, dump=False):
+
+        if dump: print("multiply_square()")
 
         num_cols = ab_num_words // NUM_MULTS
 
@@ -335,8 +356,17 @@ class ModExpNG_WordMultiplier():
             self._clear_all_macs()
             self._preset_indices(col)
 
+            if dump and DUMP_MACS_CLEARING:
+                print("t= 0, col=%2d > clear > all" % (col))
+
             for t in range(ab_num_words):
 
+                if dump and DUMP_INDICES:
+                    print("t=%2d, col=%2d > indices:" % (t, col), end='')
+                    for i in range(NUM_MULTS):
+                        print(" %2d" % self._indices[i], end='')
+                    print("")
+
                 # current b-word
                 bt = b_narrow.words[t]
 
@@ -348,6 +378,16 @@ class ModExpNG_WordMultiplier():
                     if t == (col * NUM_MULTS + x):
                         parts[t] = self._macs[x]
                         self._clear_one_mac(x)
+                        if dump and DUMP_MACS_CLEARING:
+                            print("t=%2d, col=%2d > clear > x=%d:" % (t, col, x))
+
+                if dump and DUMP_MACS_ACCUMULATION:
+                    for i in range(NUM_MULTS):
+                        if i > 0: print(" | ", end='')
+                        print("[%d]: 0x%012x" % (i, self._macs[i]), end='')
+                    print("")
+
+
 
                 # save the uppers part of product at end of column,
                 # for the last column don't save the very last part
@@ -570,13 +610,20 @@ class ModExpNG_Worker():
 
         return ModExpNG_Operand(None, 2*ab_num_words, ab)
 
-    def multiply(self, a, b, n, n_coeff, ab_num_words, reduce_only=False, multiply_only=False):
+    def multiply(self, a, b, n, n_coeff, ab_num_words, reduce_only=False, multiply_only=False, dump=False):
+
+        if dump and DUMP_VECTORS:
+            print("num_words = %d" % ab_num_words)
+            a.format_verilog_concat("A")
+            b.format_verilog_concat("B")
+            n.format_verilog_concat("N")
+            n_coeff.format_verilog_concat("N_COEFF")
 
         # 1.
         if reduce_only:
             ab = a
         else:
-            ab_parts = self.multiplier.multiply_square(a, b, ab_num_words)
+            ab_parts = self.multiplier.multiply_square(a, b, ab_num_words, dump)
             ab_words = self.recombinator.recombine_square(ab_parts, ab_num_words)
             ab = ModExpNG_Operand(None, 2 * ab_num_words, ab_words)
 
@@ -678,7 +725,7 @@ if __name__ == "__main__":
     mp_blind                     = worker.multiply(mp_blind_inverse_factor,      vector.p_factor,  vector.p, vector.p_coeff, pq_num_words)
     mq_blind                     = worker.multiply(mq_blind_inverse_factor,      vector.q_factor,  vector.q, vector.q_coeff, pq_num_words)
 
-    mp_blind_factor              = worker.multiply(mp_blind,                     vector.p_factor,  vector.p, vector.p_coeff, pq_num_words)
+    mp_blind_factor              = worker.multiply(mp_blind,                     vector.p_factor,  vector.p, vector.p_coeff, pq_num_words, dump=True)
     mq_blind_factor              = worker.multiply(mq_blind,                     vector.q_factor,  vector.q, vector.q_coeff, pq_num_words)
 
     sp_blind_factor              = worker.exponentiate(ip_factor, mp_blind_factor, vector.dp, vector.p, vector.p_factor, vector.p_coeff, pq_num_words)



More information about the Commits mailing list