mirror of
https://github.com/kmackay/micro-ecc.git
synced 2026-06-05 21:14:51 +00:00
Fix for #113
This commit is contained in:
+105
-105
@@ -37,12 +37,12 @@ uECC_VLI_API void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words) {
|
||||
"sbc r31, __zero_reg__ \n\t"
|
||||
IJMP
|
||||
#endif
|
||||
|
||||
|
||||
REPEAT(uECC_MAX_WORDS, "st x+, __zero_reg__ \n\t")
|
||||
"1: \n\t"
|
||||
: "+x" (v)
|
||||
: [num] "r" (num_words)
|
||||
:
|
||||
:
|
||||
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
|
||||
"r30", "r31", "cc"
|
||||
#endif
|
||||
@@ -60,16 +60,16 @@ uECC_VLI_API void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src, wordco
|
||||
"sbc r31, __zero_reg__ \n\t"
|
||||
IJMP
|
||||
#endif
|
||||
|
||||
|
||||
REPEAT(uECC_MAX_WORDS,
|
||||
"ld r0, y+ \n\t"
|
||||
"st x+, r0 \n\t")
|
||||
"1: \n\t"
|
||||
: "+x" (d), "+y" (src)
|
||||
: [num] "r" ((uint8_t)(num_words * 2))
|
||||
: "r0",
|
||||
: "r0"
|
||||
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
|
||||
"r30", "r31", "cc"
|
||||
, "r30", "r31", "cc"
|
||||
#endif
|
||||
);
|
||||
}
|
||||
@@ -84,7 +84,7 @@ uECC_VLI_API void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words) {
|
||||
"sub r30, %[jump] \n\t"
|
||||
"sbc r31, __zero_reg__ \n\t"
|
||||
#endif
|
||||
|
||||
|
||||
"add r26, %[num] \n\t"
|
||||
"adc r27, __zero_reg__ \n\t"
|
||||
"ld r0, -x \n\t"
|
||||
@@ -93,7 +93,7 @@ uECC_VLI_API void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words) {
|
||||
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
|
||||
IJMP
|
||||
#endif
|
||||
|
||||
|
||||
REPEAT(DEC(uECC_MAX_WORDS),
|
||||
"ld r0, -x \n\t"
|
||||
"ror r0 \n\t"
|
||||
@@ -137,15 +137,15 @@ uECC_VLI_API uECC_word_t uECC_vli_add(uECC_word_t *result,
|
||||
"sub r30, %[num] \n\t"
|
||||
"sbc r31, __zero_reg__ \n\t"
|
||||
#endif
|
||||
|
||||
|
||||
"clc \n\t"
|
||||
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
|
||||
IJMP
|
||||
REPEATM(uECC_MAX_WORDS, ADD_RJPM_TABLE)
|
||||
#endif
|
||||
|
||||
|
||||
REPEATM(uECC_MAX_WORDS, ADD_RJPM_DEST)
|
||||
|
||||
|
||||
"mov %[clb], __zero_reg__ \n\t"
|
||||
"adc %[clb], %[clb] \n\t" /* Store carry bit. */
|
||||
|
||||
@@ -184,15 +184,15 @@ uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result,
|
||||
"sub r30, %[num] \n\t"
|
||||
"sbc r31, __zero_reg__ \n\t"
|
||||
#endif
|
||||
|
||||
|
||||
"clc \n\t"
|
||||
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
|
||||
IJMP
|
||||
REPEATM(uECC_MAX_WORDS, SUB_RJPM_TABLE)
|
||||
#endif
|
||||
|
||||
|
||||
REPEATM(uECC_MAX_WORDS, SUB_RJPM_DEST)
|
||||
|
||||
|
||||
"mov %[clb], __zero_reg__ \n\t"
|
||||
"adc %[clb], %[clb] \n\t" /* Store carry bit. */
|
||||
|
||||
@@ -216,7 +216,7 @@ uECC_VLI_API void uECC_vli_mult(uECC_word_t *result,
|
||||
wordcount_t num_words) {
|
||||
/* num_words should already be in r18. */
|
||||
register wordcount_t r18 __asm__("r18") = num_words;
|
||||
|
||||
|
||||
__asm__ volatile (
|
||||
"push r18 \n\t"
|
||||
#if (uECC_MIN_WORDS == 20)
|
||||
@@ -268,7 +268,7 @@ uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
|
||||
wordcount_t num_words) {
|
||||
/* num_words should already be in r20. */
|
||||
register wordcount_t r20 __asm__("r20") = num_words;
|
||||
|
||||
|
||||
__asm__ volatile (
|
||||
"push r20 \n\t"
|
||||
#if (uECC_MIN_WORDS == 20)
|
||||
@@ -329,7 +329,7 @@ static void vli_mmod_fast_secp160r1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"out __SP_H__, r31 \n\t"
|
||||
"out __SREG__, r0 \n\t"
|
||||
"out __SP_L__, r30 \n\t"
|
||||
|
||||
|
||||
"adiw r30, 25 \n\t" /* we are shifting by 31 bits, so shift over 4 bytes
|
||||
(+ 1 since z initially points below the stack) */
|
||||
"adiw r26, 40 \n\t" /* end of product */
|
||||
@@ -384,10 +384,10 @@ static void vli_mmod_fast_secp160r1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"ld r18, z \n\t"
|
||||
"adc r18, r1 \n\t"
|
||||
"st z+, r18 \n\t"
|
||||
|
||||
|
||||
"sbiw r30, 24 \n\t" /* move z back to point at tmp */
|
||||
"sbiw r26, 40 \n\t" /* move x back to point at product */
|
||||
|
||||
|
||||
/* add low bytes of tmp to product, storing in result */
|
||||
"ld r18, z+ \n\t"
|
||||
"ld r19, x+ \n\t"
|
||||
@@ -403,13 +403,13 @@ static void vli_mmod_fast_secp160r1(uECC_word_t *result, uECC_word_t *product) {
|
||||
z is 20 bytes into tmp */
|
||||
"sbiw r28, 20 \n\t" /* move y back to point at result */
|
||||
"adiw r30, 4 \n\t" /* move z to point to the end of tmp */
|
||||
|
||||
|
||||
/* do omega_mult again with the 4 relevant bytes */
|
||||
/* z points to the end of tmp, x points to the end of product */
|
||||
"ld r18, -z \n\t" /* Load word. */
|
||||
"lsr r18 \n\t" /* Shift. */
|
||||
"st -x, r18 \n\t" /* Store the first result word. */
|
||||
|
||||
|
||||
"ld r18, -z \n\t"
|
||||
"ror r18 \n\t"
|
||||
"st -x, r18 \n\t"
|
||||
@@ -419,11 +419,11 @@ static void vli_mmod_fast_secp160r1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"ld r18, -z \n\t"
|
||||
"ror r18 \n\t"
|
||||
"st -x, r18 \n\t"
|
||||
|
||||
|
||||
"eor r18, r18 \n\t" /* r18 = 0 */
|
||||
"ror r18 \n\t" /* get last bit */
|
||||
"st -x, r18 \n\t" /* store it */
|
||||
|
||||
|
||||
"sbiw r26, 3 \n\t" /* move x back to point at beginning */
|
||||
/* now we add a copy of the 4 bytes */
|
||||
"ld r18, z+ \n\t"
|
||||
@@ -432,29 +432,29 @@ static void vli_mmod_fast_secp160r1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"st x+, r18 \n\t"
|
||||
"ld r18, z+ \n\t"
|
||||
"st x+, r18 \n\t"
|
||||
|
||||
|
||||
"ld r18, z+ \n\t"
|
||||
"ld r19, x \n\t"
|
||||
"add r18, r19 \n\t"
|
||||
"st x+, r18 \n\t"
|
||||
|
||||
|
||||
/* Propagate over the remaining bytes */
|
||||
"ld r18, x \n\t"
|
||||
"adc r18, r1 \n\t"
|
||||
"st x+, r18 \n\t"
|
||||
|
||||
|
||||
"ld r18, x \n\t"
|
||||
"adc r18, r1 \n\t"
|
||||
"st x+, r18 \n\t"
|
||||
|
||||
|
||||
"ld r18, x \n\t"
|
||||
"adc r18, r1 \n\t"
|
||||
"st x+, r18 \n\t"
|
||||
|
||||
|
||||
"ld r18, x \n\t"
|
||||
"adc r18, r1 \n\t"
|
||||
"st x+, r18 \n\t"
|
||||
|
||||
|
||||
/* now z points to the end of tmp, x points to the end of product
|
||||
(y still points at result) */
|
||||
"sbiw r26, 8 \n\t" /* move x back to point at beginning of actual data */
|
||||
@@ -468,23 +468,23 @@ static void vli_mmod_fast_secp160r1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"ld r19, y \n\t"
|
||||
"adc r18, r19 \n\t"
|
||||
"st y+, r18 \n\t")
|
||||
|
||||
|
||||
/* Done adding, now propagate carry bit */
|
||||
REPEAT(12,
|
||||
"ld r18, y \n\t"
|
||||
"adc r18, __zero_reg__ \n\t"
|
||||
"st y+, r18 \n\t")
|
||||
|
||||
|
||||
"adc %[carry], __zero_reg__ \n\t" /* Store carry bit (carry flag is cleared). */
|
||||
"sbiw r28, 20 \n\t" /* move y back to point at result */
|
||||
|
||||
|
||||
"sbiw r30, 1 \n\t" /* fix stack pointer */
|
||||
"in r0, __SREG__ \n\t"
|
||||
"cli \n\t"
|
||||
"out __SP_H__, r31 \n\t"
|
||||
"out __SREG__, r0 \n\t"
|
||||
"out __SP_L__, r30 \n\t"
|
||||
|
||||
|
||||
: "+x" (product), [carry] "+r" (carry)
|
||||
: "y" (result)
|
||||
: "r0", "r18", "r19", "r30", "r31", "cc"
|
||||
@@ -517,7 +517,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"out __SP_H__, r31 \n\t"
|
||||
"out __SREG__, r0 \n\t"
|
||||
"out __SP_L__, r30 \n\t"
|
||||
|
||||
|
||||
"adiw r30, 1 \n\t" /* add 1 since z initially points below the stack */
|
||||
"adiw r26, 32 \n\t" /* product + uECC_WORDS */
|
||||
"ldi r25, 0x03 \n\t"
|
||||
@@ -526,12 +526,12 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"ld r19, x+ \n\t"
|
||||
"ld r20, x+ \n\t"
|
||||
"ld r21, x+ \n\t"
|
||||
|
||||
|
||||
"mul r24, r18 \n\t"
|
||||
"st z+, r0 \n\t"
|
||||
"mov r22, r1 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"mul r24, r19 \n\t"
|
||||
"add r22, r0 \n\t"
|
||||
"adc r23, r1 \n\t" /* can't overflow */
|
||||
@@ -540,7 +540,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r23, r1 \n\t" /* can't overflow */
|
||||
"st z+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
"mul r24, r20 \n\t"
|
||||
"add r23, r0 \n\t"
|
||||
"adc r22, r1 \n\t"
|
||||
@@ -549,7 +549,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r22, r1 \n\t"
|
||||
"st z+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"mul r24, r21 \n\t"
|
||||
"add r22, r0 \n\t"
|
||||
"adc r23, r1 \n\t"
|
||||
@@ -558,7 +558,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r23, r1 \n\t"
|
||||
"st z+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
/* now we start adding the 2^32 part as well */
|
||||
"add r23, r18 \n\t" // 28
|
||||
"adc r22, r22 \n\t"
|
||||
@@ -571,7 +571,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r22, r1 \n\t"
|
||||
"st z+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"add r22, r19 \n\t" // 27
|
||||
"adc r23, r23 \n\t"
|
||||
"ld r19, x+ \n\t"
|
||||
@@ -583,7 +583,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r23, r1 \n\t"
|
||||
"st z+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
REPEAT(6, // 26 - 3
|
||||
"add r23, r20 \n\t"
|
||||
"adc r22, r22 \n\t"
|
||||
@@ -596,7 +596,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r22, r1 \n\t"
|
||||
"st z+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"add r22, r21 \n\t"
|
||||
"adc r23, r23 \n\t"
|
||||
"ld r21, x+ \n\t"
|
||||
@@ -608,7 +608,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r23, r1 \n\t"
|
||||
"st z+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
"add r23, r18 \n\t"
|
||||
"adc r22, r22 \n\t"
|
||||
"ld r18, x+ \n\t"
|
||||
@@ -620,7 +620,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r22, r1 \n\t"
|
||||
"st z+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"add r22, r19 \n\t"
|
||||
"adc r23, r23 \n\t"
|
||||
"ld r19, x+ \n\t"
|
||||
@@ -644,7 +644,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r22, r1 \n\t"
|
||||
"st z+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"add r22, r21 \n\t" // 1
|
||||
"adc r23, r23 \n\t"
|
||||
"ld r21, x+ \n\t"
|
||||
@@ -656,7 +656,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r23, r1 \n\t"
|
||||
"st z+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
/* Now finish the carries etc */
|
||||
"add r23, r18 \n\t"
|
||||
"adc r22, r22 \n\t"
|
||||
@@ -665,27 +665,27 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r22, r1 \n\t"
|
||||
"st z+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"add r22, r19 \n\t"
|
||||
"adc r23, r23 \n\t"
|
||||
"st z+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
"add r23, r20 \n\t"
|
||||
"adc r22, r22 \n\t"
|
||||
"st z+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"add r22, r21 \n\t"
|
||||
"adc r23, r23 \n\t"
|
||||
"st z+, r22 \n\t"
|
||||
"st z+, r23 \n\t"
|
||||
"eor r1, r1 \n\t" /* make r1 be 0 again */
|
||||
|
||||
|
||||
"sbiw r30, 37 \n\t" /* move z back to point at tmp */
|
||||
"subi r26, 64 \n\t" /* move x back to point at product */
|
||||
"sbc r27, __zero_reg__ \n\t"
|
||||
|
||||
|
||||
/* add low bytes of tmp to product, storing in result */
|
||||
"ld r18, z+ \n\t"
|
||||
"ld r19, x+ \n\t"
|
||||
@@ -696,7 +696,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"ld r19, x+ \n\t"
|
||||
"adc r18, r19 \n\t"
|
||||
"st y+, r18 \n\t")
|
||||
|
||||
|
||||
"adc %[carry], __zero_reg__ \n\t" /* Store carry bit (carry flag is cleared). */
|
||||
/* at this point x is at the end of product, y is at the end of result,
|
||||
z is 32 bytes into tmp */
|
||||
@@ -710,12 +710,12 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"ld r19, z+ \n\t"
|
||||
"ld r20, z+ \n\t"
|
||||
"ld r21, z+ \n\t"
|
||||
|
||||
|
||||
"mul r24, r18 \n\t"
|
||||
"st x+, r0 \n\t"
|
||||
"mov r22, r1 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"mul r24, r19 \n\t"
|
||||
"add r22, r0 \n\t"
|
||||
"adc r23, r1 \n\t" /* can't overflow */
|
||||
@@ -724,7 +724,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r23, r1 \n\t" /* can't overflow */
|
||||
"st x+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
"mul r24, r20 \n\t"
|
||||
"add r23, r0 \n\t"
|
||||
"adc r22, r1 \n\t"
|
||||
@@ -733,7 +733,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r22, r1 \n\t"
|
||||
"st x+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"mul r24, r21 \n\t"
|
||||
"add r22, r0 \n\t"
|
||||
"adc r23, r1 \n\t"
|
||||
@@ -742,7 +742,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r23, r1 \n\t"
|
||||
"st x+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
"add r23, r18 \n\t"
|
||||
"adc r22, r22 \n\t"
|
||||
"ld r18, z+ \n\t"
|
||||
@@ -754,7 +754,7 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r22, r1 \n\t"
|
||||
"st x+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
/* Now finish the carries etc */
|
||||
"add r22, r19 \n\t"
|
||||
"adc r23, r23 \n\t"
|
||||
@@ -763,23 +763,23 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"adc r23, r1 \n\t"
|
||||
"st x+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
"add r23, r20 \n\t"
|
||||
"adc r22, r22 \n\t"
|
||||
"st x+, r23 \n\t"
|
||||
"ldi r23, 0 \n\t"
|
||||
|
||||
|
||||
"add r22, r21 \n\t"
|
||||
"adc r23, r23 \n\t"
|
||||
"st x+, r22 \n\t"
|
||||
"ldi r22, 0 \n\t"
|
||||
|
||||
|
||||
"add r23, r18 \n\t"
|
||||
"adc r22, r22 \n\t"
|
||||
"st x+, r23 \n\t"
|
||||
"st x+, r22 \n\t"
|
||||
"eor r1, r1 \n\t" /* make r1 be 0 again */
|
||||
|
||||
|
||||
/* now z points to the end of tmp, x points to the end of product
|
||||
(y still points at result) */
|
||||
"sbiw r26, 10 \n\t" /* move x back to point at beginning of actual data */
|
||||
@@ -793,28 +793,28 @@ static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
|
||||
"ld r19, y \n\t"
|
||||
"adc r18, r19 \n\t"
|
||||
"st y+, r18 \n\t")
|
||||
|
||||
|
||||
/* Done adding, now propagate carry bit */
|
||||
REPEAT(22,
|
||||
"ld r18, y \n\t"
|
||||
"adc r18, __zero_reg__ \n\t"
|
||||
"st y+, r18 \n\t")
|
||||
|
||||
|
||||
"adc %[carry], __zero_reg__ \n\t" /* Store carry bit (carry flag is cleared). */
|
||||
"sbiw r28, 32 \n\t" /* move y back to point at result */
|
||||
|
||||
|
||||
"sbiw r30, 1 \n\t" /* fix stack pointer */
|
||||
"in r0, __SREG__ \n\t"
|
||||
"cli \n\t"
|
||||
"out __SP_H__, r31 \n\t"
|
||||
"out __SREG__, r0 \n\t"
|
||||
"out __SP_L__, r30 \n\t"
|
||||
|
||||
|
||||
: "+x" (product), [carry] "+r" (carry)
|
||||
: "y" (result)
|
||||
: "r0", "r18", "r19", "r20", "r21", "r22", "r23", "r24", "r25", "r30", "r31", "cc"
|
||||
);
|
||||
|
||||
|
||||
if (carry > 0) {
|
||||
--carry;
|
||||
uECC_vli_sub(result, result, curve_secp256k1.p, 32);
|
||||
@@ -845,7 +845,7 @@ uECC_VLI_API uECC_word_t uECC_vli_add(uECC_word_t *result,
|
||||
|
||||
__asm__ volatile (
|
||||
"clc \n\t"
|
||||
|
||||
|
||||
"1: \n\t"
|
||||
"ld %[left], x+ \n\t" /* Load left byte. */
|
||||
"ld %[right], y+ \n\t" /* Load right byte. */
|
||||
@@ -853,12 +853,12 @@ uECC_VLI_API uECC_word_t uECC_vli_add(uECC_word_t *result,
|
||||
"st z+, %[left] \n\t" /* Store the result. */
|
||||
"dec %[i] \n\t"
|
||||
"brne 1b \n\t"
|
||||
|
||||
|
||||
"adc %[carry], %[carry] \n\t" /* Store carry bit. */
|
||||
|
||||
: "+z" (r), "+x" (left), "+y" (right), [i] "+r" (num_words),
|
||||
[carry] "+r" (carry), [left] "=&r" (left_byte), [right] "=&r" (right_byte)
|
||||
:
|
||||
:
|
||||
: "cc"
|
||||
);
|
||||
return carry;
|
||||
@@ -878,7 +878,7 @@ uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result,
|
||||
|
||||
__asm__ volatile (
|
||||
"clc \n\t"
|
||||
|
||||
|
||||
"1: \n\t"
|
||||
"ld %[left], x+ \n\t" /* Load left byte. */
|
||||
"ld %[right], y+ \n\t" /* Load right byte. */
|
||||
@@ -886,7 +886,7 @@ uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result,
|
||||
"st z+, %[left] \n\t" /* Store the result. */
|
||||
"dec %[i] \n\t"
|
||||
"brne 1b \n\t"
|
||||
|
||||
|
||||
"adc %[borrow], %[borrow] \n\t" /* Store carry bit in borrow. */
|
||||
|
||||
: "+z" (r), "+x" (left), "+y" (right), [i] "+r" (num_words),
|
||||
@@ -911,86 +911,86 @@ uECC_VLI_API void uECC_vli_mult(uECC_word_t *result,
|
||||
uint8_t r2 = 0;
|
||||
uint8_t zero = 0;
|
||||
uint8_t k, i;
|
||||
|
||||
|
||||
__asm__ volatile (
|
||||
"ldi %[k], 1 \n\t" /* k = 1; k < num_words; ++k */
|
||||
|
||||
|
||||
"1: \n\t"
|
||||
"ldi %[i], 0 \n\t" /* i = 0; i < k; ++i */
|
||||
|
||||
|
||||
"add r28, %[k] \n\t" /* pre-add right ptr */
|
||||
"adc r29, %[zero] \n\t"
|
||||
|
||||
|
||||
"2: \n\t"
|
||||
"ld r0, x+ \n\t"
|
||||
"ld r1, -y \n\t"
|
||||
"mul r0, r1 \n\t"
|
||||
|
||||
|
||||
"add %[r0], r0 \n\t"
|
||||
"adc %[r1], r1 \n\t"
|
||||
"adc %[r2], %[zero] \n\t"
|
||||
|
||||
|
||||
"inc %[i] \n\t"
|
||||
"cp %[i], %[k] \n\t"
|
||||
"brlo 2b \n\t" /* loop if i < k */
|
||||
|
||||
|
||||
"sub r26, %[k] \n\t" /* fix up left ptr */
|
||||
"sbc r27, %[zero] \n\t"
|
||||
|
||||
|
||||
"st z+, %[r0] \n\t" /* Store the result. */
|
||||
"mov %[r0], %[r1] \n\t"
|
||||
"mov %[r1], %[r2] \n\t"
|
||||
"mov %[r2], %[zero] \n\t"
|
||||
|
||||
|
||||
"inc %[k] \n\t"
|
||||
"cp %[k], %[num] \n\t"
|
||||
"brlo 1b \n\t" /* loop if k < num_words */
|
||||
|
||||
|
||||
/* second half */
|
||||
"mov %[k], %[num] \n\t" /* k = num_words; k > 0; --k */
|
||||
"add r28, %[num] \n\t" /* move right ptr to point at the end of right */
|
||||
"adc r29, %[zero] \n\t"
|
||||
|
||||
|
||||
"1: \n\t"
|
||||
"ldi %[i], 0 \n\t" /* i = 0; i < k; ++i */
|
||||
|
||||
|
||||
"2: \n\t"
|
||||
"ld r0, x+ \n\t"
|
||||
"ld r1, -y \n\t"
|
||||
"mul r0, r1 \n\t"
|
||||
|
||||
|
||||
"add %[r0], r0 \n\t"
|
||||
"adc %[r1], r1 \n\t"
|
||||
"adc %[r2], %[zero] \n\t"
|
||||
|
||||
|
||||
"inc %[i] \n\t"
|
||||
"cp %[i], %[k] \n\t"
|
||||
"brlo 2b \n\t" /* loop if i < k */
|
||||
|
||||
|
||||
"add r28, %[k] \n\t" /* fix up right ptr */
|
||||
"adc r29, %[zero] \n\t"
|
||||
|
||||
|
||||
"st z+, %[r0] \n\t" /* Store the result. */
|
||||
"mov %[r0], %[r1] \n\t"
|
||||
"mov %[r1], %[r2] \n\t"
|
||||
"mov %[r2], %[zero] \n\t"
|
||||
|
||||
|
||||
"dec %[k] \n\t"
|
||||
"sub r26, %[k] \n\t" /* fix up left ptr (after k is decremented, so next time
|
||||
we start 1 higher) */
|
||||
"sbc r27, %[zero] \n\t"
|
||||
|
||||
|
||||
"cp %[k], %[zero] \n\t"
|
||||
"brne 1b \n\t" /* loop if k > 0 */
|
||||
|
||||
|
||||
"st z+, %[r0] \n\t" /* Store last result byte. */
|
||||
"eor r1, r1 \n\t" /* fix r1 to be 0 again */
|
||||
|
||||
|
||||
: "+z" (result), "+x" (left), "+y" (right),
|
||||
[r0] "+r" (r0), [r1] "+r" (r1), [r2] "+r" (r2),
|
||||
[zero] "+r" (zero), [num] "+r" (num_words),
|
||||
[k] "=&r" (k), [i] "=&r" (i)
|
||||
:
|
||||
:
|
||||
: "r0", "cc"
|
||||
);
|
||||
}
|
||||
@@ -1007,18 +1007,18 @@ uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
|
||||
uint8_t r2 = 0;
|
||||
uint8_t zero = 0;
|
||||
uint8_t k;
|
||||
|
||||
|
||||
__asm__ volatile (
|
||||
"ldi %[k], 1 \n\t" /* k = 1; k < num_words * 2; ++k */
|
||||
|
||||
|
||||
"1: \n\t"
|
||||
|
||||
|
||||
"movw r26, %[orig] \n\t" /* copy orig ptr to 'left' ptr */
|
||||
"movw r30, %[orig] \n\t" /* copy orig ptr to 'right' ptr */
|
||||
"cp %[k], %[num] \n\t"
|
||||
"brlo 2f \n\t"
|
||||
"breq 2f \n\t"
|
||||
|
||||
|
||||
/* when k > num_words, we start from (k - num_words) on the 'left' ptr */
|
||||
"add r26, %[k] \n\t"
|
||||
"adc r27, %[zero] \n\t"
|
||||
@@ -1027,11 +1027,11 @@ uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
|
||||
"add r30, %[num] \n\t" /* move right ptr to point at the end */
|
||||
"adc r31, %[zero] \n\t"
|
||||
"rjmp 3f \n\t"
|
||||
|
||||
|
||||
"2: \n\t" /* when k <= num_words, we add k to the 'right' ptr */
|
||||
"add r30, %[k] \n\t" /* pre-add 'right' ptr */
|
||||
"adc r31, %[zero] \n\t"
|
||||
|
||||
|
||||
"3: \n\t"
|
||||
"ld r0, x+ \n\t"
|
||||
"cp r26, r30 \n\t" /* if left == right here, then we are done after this mult
|
||||
@@ -1039,7 +1039,7 @@ uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
|
||||
"breq 4f \n\t"
|
||||
"ld r1, -z \n\t"
|
||||
"mul r0, r1 \n\t"
|
||||
|
||||
|
||||
/* add twice since it costs the same as doubling */
|
||||
"add %[r0], r0 \n\t"
|
||||
"adc %[r1], r1 \n\t"
|
||||
@@ -1047,18 +1047,18 @@ uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
|
||||
"add %[r0], r0 \n\t"
|
||||
"adc %[r1], r1 \n\t"
|
||||
"adc %[r2], %[zero] \n\t"
|
||||
|
||||
|
||||
"cpse r26, r30 \n\t" /* if left == right here, then we are done */
|
||||
"rjmp 3b \n\t"
|
||||
"rjmp 5f \n\t" /* skip code for non-doubled mult */
|
||||
|
||||
|
||||
"4: \n\t"
|
||||
"ld r1, -z \n\t"
|
||||
"mul r0, r1 \n\t"
|
||||
"add %[r0], r0 \n\t"
|
||||
"adc %[r1], r1 \n\t"
|
||||
"adc %[r2], %[zero] \n\t"
|
||||
|
||||
|
||||
"5: \n\t"
|
||||
"movw r30, %[result] \n\t" /* make z point to result */
|
||||
"st z+, %[r0] \n\t" /* Store the result. */
|
||||
@@ -1066,15 +1066,15 @@ uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
|
||||
"mov %[r0], %[r1] \n\t"
|
||||
"mov %[r1], %[r2] \n\t"
|
||||
"mov %[r2], %[zero] \n\t"
|
||||
|
||||
|
||||
"inc %[k] \n\t"
|
||||
"cp %[k], %[max] \n\t"
|
||||
"brlo 1b \n\t" /* loop if k < num_words * 2 */
|
||||
|
||||
|
||||
"movw r30, %[result] \n\t" /* make z point to result */
|
||||
"st z+, %[r0] \n\t" /* Store last result byte. */
|
||||
"eor r1, r1 \n\t" /* fix r1 to be 0 again */
|
||||
|
||||
|
||||
: [result] "+r" (r),
|
||||
[r0] "+r" (r0), [r1] "+r" (r1), [r2] "+r" (r2), [zero] "+r" (zero),
|
||||
[k] "=&a" (k)
|
||||
|
||||
Reference in New Issue
Block a user