From 7a138263197006d74661fbe446f33d6014777245 Mon Sep 17 00:00:00 2001 From: bg Date: Wed, 26 Dec 2012 16:59:29 +0100 Subject: [PATCH] [keccak-asm] rho & pi in asm --- keccak/keccak-asm.S | 90 ++++++++++++++++++++++++++++++++++++++++++++ keccak/keccak-stub.c | 39 +++++++++++++++---- 2 files changed, 121 insertions(+), 8 deletions(-) diff --git a/keccak/keccak-asm.S b/keccak/keccak-asm.S index 9ccd79e..95d8aa6 100644 --- a/keccak/keccak-asm.S +++ b/keccak/keccak-asm.S @@ -30,6 +30,16 @@ .list .equ __zero_reg__, 1 + +.global rho_pi_idx_table +rho_pi_idx_table: + .irp i, 0, 1, 2, 3, 4 + .irp j, 0, 1, 2, 3, 4 + .byte (((2 * \j + 3 * \i) % 5) * 5 + \i) * 8 + .endr + .endr + +.align 2 /* void keccak_theta (uint64_t *a, uint64_t *b){ // uint64_t b[5][5]; @@ -128,8 +138,14 @@ theta_2b: .global keccak_theta keccak_theta: + push_range 2, 8 + push r16 + push_range 28, 29 + movw r30, r24 ; Z = a movw r26, r22 ; X = b + push_range 22, 25 + ldi r19, 5 10: ldi r20, 8 @@ -208,5 +224,79 @@ keccak_theta: sbci XH, hi8(4 * 5 * 8 + 8) rcall theta_2b +; ret +/* + rho & pi + for(i = 0; i < 5; ++i){ + for(j = 0; j < 5; ++j){ + b[(2 * i + 3 * j) % 5][j] = + rotate64left_code(a[j][i], pgm_read_byte(&(keccak_rotate_codes[i][j]))); + } + } + + -- or -- + + const uint8_t* rot_code = (const uint8_t*)keccak_rotate_codes; + const uint8_t* idx_idx = (const uint8_t*)rho_pi_idx_table; + uint64_t *a_tmp = (uint64_t*)a; + for(i = 0; i < 25; ++i){ + *((uint64_t*)(((uint8_t*)b) + pgm_read_byte(idx_idx++))) = + rotate64left_code(*a_tmp++, pgm_read_byte(rot_code++)); + + } + +*/ + +.equ B_REG_L, 6 +.equ B_REG_H, 7 + + ldi r18, lo8(keccak_rotate_codes) + ldi r19, hi8(keccak_rotate_codes) + movw r2, r18 + ldi r18, lo8(rho_pi_idx_table) + ldi r19, hi8(rho_pi_idx_table) + movw r4, r18 + ldi r16, 25 + mov r8, r16 + + pop YH + pop YL + pop B_REG_H + pop B_REG_L + +10: + ld r18, Y+ + ld r19, Y+ + ld r20, Y+ + ld r21, Y+ + ld r22, Y+ + ld r23, Y+ + ld r24, Y+ + ld r25, Y+ + movw ZL, r2 + lpm r16, Z+ + movw r2, ZL + call rotate64left_code + movw ZL, r4 + lpm r16, Z+ + movw r4, ZL + movw XL, B_REG_L + add XL, r16 + adc XH, __zero_reg__ + st X+, r18 + st X+, r19 + st X+, r20 + st X+, r21 + st X+, r22 + st X+, r23 + st X+, r24 + st X+, r25 + + dec r8 + brne 10b + + pop_range 28, 29 + pop r16 + pop_range 2, 8 ret diff --git a/keccak/keccak-stub.c b/keccak/keccak-stub.c index d1eb501..ec61f83 100644 --- a/keccak/keccak-stub.c +++ b/keccak/keccak-stub.c @@ -87,21 +87,30 @@ const static uint8_t rc_comp[] PROGMEM = { 0x52, 0xc0, 0x1a, 0x6a, 0xf1, 0xd0, 0x21, 0x78, }; - -const static uint8_t r[5][5] PROGMEM = { +/* +const uint8_t keccak_rotate_codes[5][5] PROGMEM = { { ROT_CODE( 0), ROT_CODE(36), ROT_CODE( 3), ROT_CODE(41), ROT_CODE(18) }, { ROT_CODE( 1), ROT_CODE(44), ROT_CODE(10), ROT_CODE(45), ROT_CODE( 2) }, { ROT_CODE(62), ROT_CODE( 6), ROT_CODE(43), ROT_CODE(15), ROT_CODE(61) }, { ROT_CODE(28), ROT_CODE(55), ROT_CODE(25), ROT_CODE(21), ROT_CODE(56) }, { ROT_CODE(27), ROT_CODE(20), ROT_CODE(39), ROT_CODE( 8), ROT_CODE(14) } }; +*/ +const uint8_t keccak_rotate_codes[5][5] PROGMEM = { + { ROT_CODE( 0), ROT_CODE( 1), ROT_CODE(62), ROT_CODE(28), ROT_CODE(27) }, + { ROT_CODE(36), ROT_CODE(44), ROT_CODE( 6), ROT_CODE(55), ROT_CODE(20) }, + { ROT_CODE( 3), ROT_CODE(10), ROT_CODE(43), ROT_CODE(25), ROT_CODE(39) }, + { ROT_CODE(41), ROT_CODE(45), ROT_CODE(15), ROT_CODE(21), ROT_CODE( 8) }, + { ROT_CODE(18), ROT_CODE( 2), ROT_CODE(61), ROT_CODE(56), ROT_CODE(14) } +}; void keccak_theta(uint64_t *a, uint64_t *b); +extern const uint8_t rho_pi_idx_table[25] PROGMEM; static inline void keccak_round(uint64_t a[5][5], uint8_t rci){ uint64_t b[5][5]; - uint8_t i, j; + uint8_t i; // j; union { uint64_t v64; uint8_t v8[8]; @@ -113,20 +122,34 @@ void keccak_round(uint64_t a[5][5], uint8_t rci){ keccak_dump_state(a); #endif /* rho & pi */ - for(i = 0; i < 5; ++i){ - for(j = 0; j < 5; ++j){ - b[(2 * i + 3 * j) % 5][j] = rotate64left_code(a[j][i], pgm_read_byte(&(r[i][j]))); - } +/* + const uint8_t* rot_code = (const uint8_t*)keccak_rotate_codes; + const uint8_t* idx_idx = (const uint8_t*)rho_pi_idx_table; + uint64_t *a_tmp = (uint64_t*)a; + for(i = 0; i < 25; ++i){ + *((uint64_t*)(((uint8_t*)b) + pgm_read_byte(idx_idx++))) = + rotate64left_code(*a_tmp++, pgm_read_byte(rot_code++)); + } +*/ #if DEBUG & 0 cli_putstr_P(PSTR("\r\n--- after rho & pi ---")); keccak_dump_state(a); #endif /* chi */ + memcpy(a, b, 5 * 5 * 8); for(i = 0; i < 5; ++i){ +/* for(j = 0; j < 5; ++j){ - a[j][i] = b[j][i] ^ ((~(b[j][(i + 1) % 5])) & (b[j][(i + 2) % 5])); + a[i][j] = b[i][j] ^ ((~(b[i][(j + 1) % 5])) & (b[i][(j + 2) % 5])); } +*/ + a[i][0] ^= ((~(b[i][1])) & (b[i][2])); + a[i][1] ^= ((~(b[i][2])) & (b[i][3])); + a[i][2] ^= ((~(b[i][3])) & (b[i][4])); + a[i][3] ^= ((~(b[i][4])) & (b[i][0])); + a[i][4] ^= ((~(b[i][0])) & (b[i][1])); + } #if DEBUG & 0 cli_putstr_P(PSTR("\r\nAfter chi:")); -- 2.39.5