]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
progress in asm version of keccak (theta now in asm)
authorbg <daniel.otte@rub.de>
Tue, 18 Dec 2012 22:04:45 +0000 (23:04 +0100)
committerbg <daniel.otte@rub.de>
Tue, 18 Dec 2012 22:04:45 +0000 (23:04 +0100)
keccak/keccak-asm.S
keccak/keccak-stub.c

index 328476372b75f06126b5f1ee037adc9be751cdc7..9ccd79e0d8c9d3d7c6dafdb0c09b119620276347 100644 (file)
@@ -28,6 +28,8 @@
 .nolist
 #include "avr-asm-macros.S"
 .list
+
+.equ __zero_reg__, 1
 /*
        void keccak_theta (uint64_t *a, uint64_t *b){
        // uint64_t b[5][5];
@@ -37,8 +39,9 @@
        }
 */
 
-theta_2a:
-/*
+/*********************************************
+ * theta_2a
+ *********************************************
        input:
                r24:r25 = a ; uint64_t a[5][5]
                X = b       ; uint64_t *b
@@ -51,6 +54,7 @@ theta_2a:
                X += 8
                Z = r24:r25 + 7 + 4 * 40
 */
+theta_2a:
        ldi r20, 8
 10:
        movw ZL, r24
@@ -68,6 +72,60 @@ theta_2a:
        brne 10b
        ret
 
+/*********************************************
+ * theta_2b
+ *********************************************
+       input:
+               r24:r25 = a+1 ; uint64_t a[5][5]
+               X = b       ; uint64_t *b
+       output:
+               a[0..4][0] ^= rol(b,1)
+               r19 = XX
+               r20 = 0
+               r21 = XX
+               r22 = XX
+               r24:r25 += 8
+               X += 8
+               Z = r24:r25 + 7 + 4 * 40
+*/
+theta_2b:
+       ldi r20, 7
+       ld r19, X+
+       lsl r19
+       rol __zero_reg__
+10:
+       movw ZL, r24
+       ld  r21, X+
+       ror __zero_reg__
+       rol r21
+       rol __zero_reg__
+       .irp r, 0, 1, 2, 3, 4
+               ld  r22, Z
+               eor r22, r21
+               st  Z, r22
+       .if \r != 4
+               adiw ZL, 40
+       .endif
+       .endr
+       adiw r24, 1
+       dec r20
+       brne 10b
+       add r19, __zero_reg__
+       sbiw r24, 8
+       movw ZL, r24
+       .irp r, 0, 1, 2, 3, 4
+               ld  r22, Z
+               eor r22, r19
+               st  Z, r22
+       .if \r != 4
+               adiw ZL, 40
+       .endif
+       .endr
+       adiw r24, 9
+       clr __zero_reg__
+       ret
+
+
 .global keccak_theta
 keccak_theta:
        movw r30, r24 ; Z = a
@@ -104,17 +162,11 @@ keccak_theta:
                        a[j][i] ^= b[(4 + i) % 5][0];
                }
        }
-       for(i = 0; i < 5; ++i){
-        for(j = 0; j < 5; ++j){
-            a[j][i] ^= rotate64_1bit_left(b[(i + 1) % 5][0]);
-        }
-    }
 
 */
+/* a[0..4][0]{0..7} ^= b[4][0]{0..7} */
        sbiw XL, 5 * 8
-
        sbiw r24, 40
-/* a[0..4][0]{0..7} ^= b[4][0]{0..7} */
        rcall theta_2a
 /* a[0..4][1]{0..7} ^= b[0][0]{0..7} */
        subi XL, lo8(4 * 5 * 8 + 8)
@@ -129,25 +181,32 @@ keccak_theta:
 /* a[0..4][4]{0..7} ^= b[3][0]{0..7} */
        adiw XL, 4 * 8
        rcall theta_2a
-
-       ret
-
-       ldi r20, 8
-
-10:
-       movw ZL, r24
-       ld  r21, X+
-       .irp r, 0, 1, 2, 3, 4
-               ld  r22, Z
-               eor r22, r21
-               st  Z, r22
-       .if \r != 4
-               adiw ZL, 40
-       .endif
-       .endr
-       adiw r24, 1
-       dec r20
-       brne 10b
+/*
+       for(i = 0; i < 5; ++i){
+        for(j = 0; j < 5; ++j){
+            a[j][i] ^= rotate64_1bit_left(b[(i + 1) % 5][0]);
+        }
+    }
+*/
+/* a[0..4][0]{0..7} ^= rol(b[1][0]{0..7}) */
+       subi r24, lo8(5 * 8 - 1)
+       sbci r25, hi8(5 * 8 - 1)
+       subi XL, lo8(2 * 5 * 8 + 8)
+       sbci XH, hi8(2 * 5 * 8 + 8)
+       rcall theta_2b
+/* a[0..4][1]{0..7} ^= rol(b[2][0]{0..7}) */
+       adiw XL, 4 * 8
+       rcall theta_2b
+/* a[0..4][21]{0..7} ^= rol(b[3][0]{0..7}) */
+       adiw XL, 4 * 8
+       rcall theta_2b
+/* a[0..4][3]{0..7} ^= rol(b[4][0]{0..7}) */
+       adiw XL, 4 * 8
+       rcall theta_2b
+/* a[0..4][4]{0..7} ^= rol(b[0][0]{0..7}) */
+       subi XL, lo8(4 * 5 * 8 + 8)
+       sbci XH, hi8(4 * 5 * 8 + 8)
+       rcall theta_2b
 
 
        ret
index 60cf2d19af227562f9500f59c95ad2aad8dae317..d1eb501d0a7f76a1987225230dc2b904071a1703 100644 (file)
@@ -30,7 +30,7 @@
 #  undef DEBUG
 #endif
 
-#define DEBUG 1
+#define DEBUG 0
 
 #if DEBUG
 #include "cli.h"
@@ -107,20 +107,7 @@ void keccak_round(uint64_t a[5][5], uint8_t rci){
                        uint8_t v8[8];
                } t;
        /* theta */
-#if DEBUG & 0
-    printf_P(PSTR("\npre-theta(1):"));
-    keccak_dump_state(b);
-#endif
        keccak_theta((uint64_t*)a, (uint64_t*)b);
-#if DEBUG
-    printf_P(PSTR("\npost-theta(1):"));
-       keccak_dump_state(b);
-#endif
-       for(i = 0; i < 5; ++i){
-        for(j = 0; j < 5; ++j){
-            a[j][i] ^= rotate64_1bit_left(b[(i + 1) % 5][0]);
-        }
-    }
 #if DEBUG
        cli_putstr_P(PSTR("\r\nAfter theta:"));
        keccak_dump_state(a);