Ai
1 Star 0 Fork 0

Espressif Systems/micro-ecc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
asm_avr.inc 30.81 KB
一键复制 编辑 原始数据 按行查看 历史
Ken MacKay 提交于 2020-10-08 03:27 +08:00 . Fix for #113
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
/* Copyright 2015, Kenneth MacKay. Licensed under the BSD 2-clause license. */
#ifndef _UECC_ASM_AVR_H_
#define _UECC_ASM_AVR_H_
#if (uECC_SUPPORTS_secp256r1 || uECC_SUPPORTS_secp256k1)
#define uECC_MIN_WORDS 32
#endif
#if uECC_SUPPORTS_secp224r1
#undef uECC_MIN_WORDS
#define uECC_MIN_WORDS 28
#endif
#if uECC_SUPPORTS_secp192r1
#undef uECC_MIN_WORDS
#define uECC_MIN_WORDS 24
#endif
#if uECC_SUPPORTS_secp160r1
#undef uECC_MIN_WORDS
#define uECC_MIN_WORDS 20
#endif
#if __AVR_HAVE_EIJMP_EICALL__
#define IJMP "eijmp \n\t"
#else
#define IJMP "ijmp \n\t"
#endif
#if (uECC_OPTIMIZATION_LEVEL >= 2)
uECC_VLI_API void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words) {
volatile uECC_word_t *v = vli;
__asm__ volatile (
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
"ldi r30, pm_lo8(1f) \n\t"
"ldi r31, pm_hi8(1f) \n\t"
"sub r30, %[num] \n\t"
"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
);
}
#define asm_clear 1
uECC_VLI_API void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src, wordcount_t num_words) {
volatile uECC_word_t *d = dest;
__asm__ volatile (
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
"ldi r30, pm_lo8(1f) \n\t"
"ldi r31, pm_hi8(1f) \n\t"
"sub r30, %[num] \n\t"
"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"
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
, "r30", "r31", "cc"
#endif
);
}
#define asm_set 1
uECC_VLI_API void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words) {
volatile uECC_word_t *v = vli;
__asm__ volatile (
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
"ldi r30, pm_lo8(1f) \n\t"
"ldi r31, pm_hi8(1f) \n\t"
"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"
"lsr r0 \n\t"
"st x, r0 \n\t"
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
IJMP
#endif
REPEAT(DEC(uECC_MAX_WORDS),
"ld r0, -x \n\t"
"ror r0 \n\t"
"st x, r0 \n\t")
"1: \n\t"
: "+x" (v)
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
: [num] "r" (num_words), [jump] "r" ((uint8_t)(3 * (num_words - 1)))
: "r0", "r30", "r31", "cc"
#else
: [num] "r" (num_words)
: "r0", "cc"
#endif
);
}
#define asm_rshift1 1
#define ADD_RJPM_TABLE(N) \
"movw r30, %A[result] \n\t" \
"rjmp add_%=_" #N " \n\t"
#define ADD_RJPM_DEST(N) \
"add_%=_" #N ":" \
"ld %[clb], x+ \n\t" \
"ld %[rb], y+ \n\t" \
"adc %[clb], %[rb] \n\t" \
"st z+, %[clb] \n\t"
uECC_VLI_API uECC_word_t uECC_vli_add(uECC_word_t *result,
const uECC_word_t *left,
const uECC_word_t *right,
wordcount_t num_words) {
volatile uECC_word_t *r = result;
uint8_t carry;
uint8_t right_byte;
__asm__ volatile (
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
"ldi r30, pm_lo8(add_%=_" STR(uECC_MAX_WORDS) ") \n\t"
"ldi r31, pm_hi8(add_%=_" STR(uECC_MAX_WORDS) ") \n\t"
"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. */
: "+x" (left), "+y" (right),
[clb] "=&r" (carry), [rb] "=&r" (right_byte)
: [result] "r" (r), [num] "r" ((uint8_t)(num_words * 2))
: "r30", "r31", "cc"
);
return carry;
}
#define asm_add 1
#define SUB_RJPM_TABLE(N) \
"movw r30, %A[result] \n\t" \
"rjmp sub_%=_" #N " \n\t"
#define SUB_RJPM_DEST(N) \
"sub_%=_" #N ":" \
"ld %[clb], x+ \n\t" \
"ld %[rb], y+ \n\t" \
"sbc %[clb], %[rb] \n\t" \
"st z+, %[clb] \n\t"
uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result,
const uECC_word_t *left,
const uECC_word_t *right,
wordcount_t num_words) {
volatile uECC_word_t *r = result;
uint8_t carry;
uint8_t right_byte;
__asm__ volatile (
#if (uECC_MAX_WORDS != uECC_MIN_WORDS)
"ldi r30, pm_lo8(sub_%=_" STR(uECC_MAX_WORDS) ") \n\t"
"ldi r31, pm_hi8(sub_%=_" STR(uECC_MAX_WORDS) ") \n\t"
"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. */
: "+x" (left), "+y" (right),
[clb] "=&r" (carry), [rb] "=&r" (right_byte)
: [result] "r" (r), [num] "r" ((uint8_t)(num_words * 2))
: "r30", "r31", "cc"
);
return carry;
}
#define asm_sub 1
#if (uECC_OPTIMIZATION_LEVEL >= 3)
#include "asm_avr_mult_square.inc"
__attribute((noinline))
uECC_VLI_API void uECC_vli_mult(uECC_word_t *result,
const uECC_word_t *left,
const uECC_word_t *right,
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)
FAST_MULT_ASM_20
"pop r18 \n\t"
#if (uECC_MAX_WORDS > 20)
FAST_MULT_ASM_20_TO_24
#endif
#if (uECC_MAX_WORDS > 24)
FAST_MULT_ASM_24_TO_28
#endif
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ASM_28_TO_32
#endif
#elif (uECC_MIN_WORDS == 24)
FAST_MULT_ASM_24
"pop r18 \n\t"
#if (uECC_MAX_WORDS > 24)
FAST_MULT_ASM_24_TO_28
#endif
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ASM_28_TO_32
#endif
#elif (uECC_MIN_WORDS == 28)
FAST_MULT_ASM_28
"pop r18 \n\t"
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ASM_28_TO_32
#endif
#elif (uECC_MIN_WORDS == 32)
FAST_MULT_ASM_32
"pop r18 \n\t"
#endif
"2: \n\t"
"eor r1, r1 \n\t"
: "+x" (left), "+y" (right), "+z" (result)
: "r" (r18)
: "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
"r11", "r12", "r13", "r14", "r15", "r16", "r17", "r19", "r20",
"r21", "r22", "r23", "r24", "r25", "cc"
);
}
#define asm_mult 1
#if uECC_SQUARE_FUNC
__attribute((noinline))
uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
const uECC_word_t *left,
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)
FAST_SQUARE_ASM_20
"pop r20 \n\t"
#if (uECC_MAX_WORDS > 20)
FAST_SQUARE_ASM_20_TO_24
#endif
#if (uECC_MAX_WORDS > 24)
FAST_SQUARE_ASM_24_TO_28
#endif
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ASM_28_TO_32
#endif
#elif (uECC_MIN_WORDS == 24)
FAST_SQUARE_ASM_24
"pop r20 \n\t"
#if (uECC_MAX_WORDS > 24)
FAST_SQUARE_ASM_24_TO_28
#endif
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ASM_28_TO_32
#endif
#elif (uECC_MIN_WORDS == 28)
FAST_SQUARE_ASM_28
"pop r20 \n\t"
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ASM_28_TO_32
#endif
#elif (uECC_MIN_WORDS == 32)
FAST_SQUARE_ASM_32
"pop r20 \n\t"
#endif
"2: \n\t"
"eor r1, r1 \n\t"
: "+x" (left), "+z" (result)
: "r" (r20)
: "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
"r11", "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19",
"r21", "r22", "r23", "r24", "r25", "r28", "r29", "cc"
);
}
#define asm_square 1
#endif /* uECC_SQUARE_FUNC */
#endif /* (uECC_OPTIMIZATION_LEVEL >= 3) */
#if uECC_SUPPORTS_secp160r1
static const struct uECC_Curve_t curve_secp160r1;
static void vli_mmod_fast_secp160r1(uECC_word_t *result, uECC_word_t *product) {
uint8_t carry = 0;
__asm__ volatile (
"in r30, __SP_L__ \n\t"
"in r31, __SP_H__ \n\t"
"sbiw r30, 24 \n\t"
"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"
"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 */
"ld r18, -x \n\t" /* Load word. */
"lsr r18 \n\t" /* Shift. */
"st -z, r18 \n\t" /* Store the first result word. */
/* Now we just do the remaining words with the carry bit (using ROR) */
REPEAT(19,
"ld r18, -x \n\t"
"ror r18 \n\t"
"st -z, r18 \n\t")
"eor r18, r18 \n\t" /* r18 = 0 */
"ror r18 \n\t" /* get last bit */
"st -z, r18 \n\t" /* store it */
"sbiw r30, 3 \n\t" /* move z back to point at tmp */
/* now we add right */
"ld r18, x+ \n\t"
"st z+, r18 \n\t" /* the first 3 bytes do not need to be added */
"ld r18, x+ \n\t"
"st z+, r18 \n\t"
"ld r18, x+ \n\t"
"st z+, r18 \n\t"
"ld r18, x+ \n\t"
"ld r19, z \n\t"
"add r18, r19 \n\t"
"st z+, r18 \n\t"
/* Now we just do the remaining words with the carry bit (using ADC) */
REPEAT(16,
"ld r18, x+ \n\t"
"ld r19, z \n\t"
"adc r18, r19 \n\t"
"st z+, r18 \n\t")
/* Propagate over the remaining bytes of result */
"ld r18, z \n\t"
"adc r18, r1 \n\t"
"st z+, r18 \n\t"
"ld r18, z \n\t"
"adc r18, r1 \n\t"
"st z+, r18 \n\t"
"ld r18, z \n\t"
"adc r18, r1 \n\t"
"st z+, r18 \n\t"
"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"
"add r18, r19 \n\t"
"st y+, r18 \n\t"
REPEAT(19,
"ld r18, z+ \n\t"
"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 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"
"ld r18, -z \n\t"
"ror r18 \n\t"
"st -x, r18 \n\t"
"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"
"st x+, r18 \n\t" /* the first 3 bytes do not need to be added */
"ld r18, z+ \n\t"
"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 */
/* add into result */
"ld r18, x+ \n\t"
"ld r19, y \n\t"
"add r18, r19 \n\t"
"st y+, r18 \n\t"
REPEAT(7,
"ld r18, x+ \n\t"
"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"
);
if (carry > 0) {
--carry;
uECC_vli_sub(result, result, curve_secp160r1.p, 20);
}
if (carry > 0) {
uECC_vli_sub(result, result, curve_secp160r1.p, 20);
}
if (uECC_vli_cmp_unsafe(result, curve_secp160r1.p, 20) > 0) {
uECC_vli_sub(result, result, curve_secp160r1.p, 20);
}
}
#define asm_mmod_fast_secp160r1 1
#endif /* uECC_SUPPORTS_secp160r1 */
#if uECC_SUPPORTS_secp256k1
static const struct uECC_Curve_t curve_secp256k1;
static void vli_mmod_fast_secp256k1(uECC_word_t *result, uECC_word_t *product) {
uint8_t carry = 0;
__asm__ volatile (
"in r30, __SP_L__ \n\t"
"in r31, __SP_H__ \n\t"
"sbiw r30, 37 \n\t"
"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"
"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"
"ldi r24, 0xD1 \n\t"
"ld r18, x+ \n\t"
"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 */
"mul r25, r18 \n\t"
"add r22, r0 \n\t"
"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"
"mul r25, r19 \n\t"
"add r23, r0 \n\t"
"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"
"mul r25, r20 \n\t"
"add r22, r0 \n\t"
"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"
"ld r18, x+ \n\t"
"mul r24, r18 \n\t"
"add r23, r0 \n\t"
"adc r22, r1 \n\t"
"mul r25, r21 \n\t"
"add r23, r0 \n\t"
"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"
"mul r24, r19 \n\t"
"add r22, r0 \n\t"
"adc r23, r1 \n\t"
"mul r25, r18 \n\t"
"add r22, r0 \n\t"
"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"
"ld r20, x+ \n\t"
"mul r24, r20 \n\t"
"add r23, r0 \n\t"
"adc r22, r1 \n\t"
"mul r25, r19 \n\t"
"add r23, r0 \n\t"
"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"
"mul r24, r21 \n\t"
"add r22, r0 \n\t"
"adc r23, r1 \n\t"
"mul r25, r20 \n\t"
"add r22, r0 \n\t"
"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"
"mul r24, r18 \n\t"
"add r23, r0 \n\t"
"adc r22, r1 \n\t"
"mul r25, r21 \n\t"
"add r23, r0 \n\t"
"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"
"mul r24, r19 \n\t"
"add r22, r0 \n\t"
"adc r23, r1 \n\t"
"mul r25, r18 \n\t"
"add r22, r0 \n\t"
"adc r23, r1 \n\t"
"st z+, r22 \n\t"
"ldi r22, 0 \n\t")
"add r23, r20 \n\t" // 2
"adc r22, r22 \n\t"
"ld r20, x+ \n\t"
"mul r24, r20 \n\t"
"add r23, r0 \n\t"
"adc r22, r1 \n\t"
"mul r25, r19 \n\t"
"add r23, r0 \n\t"
"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"
"mul r24, r21 \n\t"
"add r22, r0 \n\t"
"adc r23, r1 \n\t"
"mul r25, r20 \n\t"
"add r22, r0 \n\t"
"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"
"mul r25, r21 \n\t"
"add r23, r0 \n\t"
"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"
"add r18, r19 \n\t"
"st y+, r18 \n\t"
REPEAT(31,
"ld r18, z+ \n\t"
"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 */
"sbiw r28, 32 \n\t" /* move y back to point at result */
/* do omega_mult again with the 5 relevant bytes */
/* z points to tmp + uECC_WORDS, x points to the end of product */
"sbiw r26, 32 \n\t" /* shift x back to point into the product buffer
(we can overwrite it now) */
"ld r18, z+ \n\t"
"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 */
"mul r25, r18 \n\t"
"add r22, r0 \n\t"
"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"
"mul r25, r19 \n\t"
"add r23, r0 \n\t"
"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"
"mul r25, r20 \n\t"
"add r22, r0 \n\t"
"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"
"mul r24, r18 \n\t"
"add r23, r0 \n\t"
"adc r22, r1 \n\t"
"mul r25, r21 \n\t"
"add r23, r0 \n\t"
"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"
"mul r25, r18 \n\t"
"add r22, r0 \n\t"
"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 */
/* add into result */
"ld r18, x+ \n\t"
"ld r19, y \n\t"
"add r18, r19 \n\t"
"st y+, r18 \n\t"
REPEAT(9,
"ld r18, x+ \n\t"
"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);
}
if (carry > 0) {
uECC_vli_sub(result, result, curve_secp256k1.p, 32);
}
if (uECC_vli_cmp_unsafe(result, curve_secp256k1.p, 32) > 0) {
uECC_vli_sub(result, result, curve_secp256k1.p, 32);
}
}
#define asm_mmod_fast_secp256k1 1
#endif /* uECC_SUPPORTS_secp256k1 */
#endif /* (uECC_OPTIMIZATION_LEVEL >= 2) */
/* ---- "Small" implementations ---- */
#if !asm_add
uECC_VLI_API uECC_word_t uECC_vli_add(uECC_word_t *result,
const uECC_word_t *left,
const uECC_word_t *right,
wordcount_t num_words) {
volatile uECC_word_t *r = result;
uint8_t carry = 0;
uint8_t left_byte;
uint8_t right_byte;
__asm__ volatile (
"clc \n\t"
"1: \n\t"
"ld %[left], x+ \n\t" /* Load left byte. */
"ld %[right], y+ \n\t" /* Load right byte. */
"adc %[left], %[right] \n\t" /* Add. */
"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;
}
#define asm_add 1
#endif
#if !asm_sub
uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result,
const uECC_word_t *left,
const uECC_word_t *right,
wordcount_t num_words) {
volatile uECC_word_t *r = result;
uint8_t borrow = 0;
uint8_t left_byte;
uint8_t right_byte;
__asm__ volatile (
"clc \n\t"
"1: \n\t"
"ld %[left], x+ \n\t" /* Load left byte. */
"ld %[right], y+ \n\t" /* Load right byte. */
"sbc %[left], %[right] \n\t" /* Subtract. */
"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),
[borrow] "+r" (borrow), [left] "=&r" (left_byte), [right] "=&r" (right_byte)
:
: "cc"
);
return borrow;
}
#define asm_sub 1
#endif
#if !asm_mult
__attribute((noinline))
uECC_VLI_API void uECC_vli_mult(uECC_word_t *result,
const uECC_word_t *left,
const uECC_word_t *right,
wordcount_t num_words) {
volatile uECC_word_t *r = result;
uint8_t r0 = 0;
uint8_t r1 = 0;
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"
);
}
#define asm_mult 1
#endif
#if (uECC_SQUARE_FUNC && !asm_square)
uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
const uECC_word_t *left,
wordcount_t num_words) {
volatile uECC_word_t *r = result;
uint8_t r0 = 0;
uint8_t r1 = 0;
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"
"sub r26, %[num] \n\t"
"sbc r27, %[zero] \n\t"
"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
(and we don't need to double) */
"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"
"adc %[r2], %[zero] \n\t"
"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. */
"movw %[result], r30 \n\t" /* update result ptr*/
"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)
: [orig] "r" (left), [max] "r" ((uint8_t)(2 * num_words)),
[num] "r" (num_words)
: "r0", "r26", "r27", "r30", "r31", "cc"
);
}
#define asm_square 1
#endif /* uECC_SQUARE_FUNC && !asm_square */
#endif /* _UECC_ASM_AVR_H_ */
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/espressif-systems/micro-ecc.git
git@gitee.com:espressif-systems/micro-ecc.git
espressif-systems
micro-ecc
micro-ecc
master

搜索帮助