-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
68 changed files
with
2,345 additions
and
5,470 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2023 Junhao Huang ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0(the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* NTT and inverse NTT code from: | ||
* Huang, J. et al. 2024. Revisiting Keccak and Dilithium Implementations on ARMv7-M. | ||
* IACR Transactions on Cryptographic Hardware and Embedded Systems. 2024, 2 (Mar. 2024), 1–24. | ||
* DOI:https://doi.org/10.46586/tches.v2024.i2.1-24. | ||
* https://github.com/UIC-ESLAS/Dilithium-Multi-Moduli/blob/332a32cc02d407020e48a4f9b3a0dc78d4c8b0bc/M4/crypto_sign/dilithium3/m4plant/smallntt_769.S | ||
*/ | ||
|
||
#ifndef MACROS_SMALLNTT_I | ||
#define MACROS_SMALLNTT_I | ||
|
||
// general macros | ||
.macro load a, a0, a1, a2, a3, mem0, mem1, mem2, mem3 | ||
ldr.w \a0, [\a, \mem0] | ||
ldr.w \a1, [\a, \mem1] | ||
ldr.w \a2, [\a, \mem2] | ||
ldr.w \a3, [\a, \mem3] | ||
.endm | ||
|
||
.macro store a, a0, a1, a2, a3, mem0, mem1, mem2, mem3 | ||
str.w \a0, [\a, \mem0] | ||
str.w \a1, [\a, \mem1] | ||
str.w \a2, [\a, \mem2] | ||
str.w \a3, [\a, \mem3] | ||
.endm | ||
|
||
.macro doubleplant a, tmp, q, qa, plantconst | ||
smulwb \tmp, \plantconst, \a | ||
smulwt \a, \plantconst, \a | ||
smlabt \tmp, \tmp, \q, \qa | ||
smlabt \a, \a, \q, \qa | ||
pkhtb \a, \a, \tmp, asr#16 | ||
.endm | ||
|
||
.macro doublebarrett a, tmp, tmp2, q, barrettconst | ||
smulbb \tmp, \a, \barrettconst | ||
smultb \tmp2, \a, \barrettconst | ||
asr \tmp, \tmp, #26 | ||
asr \tmp2, \tmp2, #26 | ||
smulbb \tmp, \tmp, \q | ||
smulbb \tmp2, \tmp2, \q | ||
pkhbt \tmp, \tmp, \tmp2, lsl#16 | ||
usub16 \a, \a, \tmp | ||
.endm | ||
|
||
// q locate in the top half of the register | ||
.macro plant_red q, qa, qinv, tmp | ||
mul \tmp, \tmp, \qinv | ||
//tmp*qinv mod 2^2n/ 2^n; in high half | ||
smlatt \tmp, \tmp, \q, \qa | ||
// result in high half | ||
.endm | ||
|
||
.macro mul_twiddle_plant a, twiddle, tmp, q, qa | ||
smulwb \tmp, \twiddle, \a | ||
smulwt \a, \twiddle, \a | ||
smlabt \tmp, \tmp, \q, \qa | ||
smlabt \a, \a, \q, \qa | ||
pkhtb \a, \a, \tmp, asr#16 | ||
.endm | ||
|
||
.macro doublebutterfly_plant a0, a1, twiddle, tmp, q, qa | ||
smulwb \tmp, \twiddle, \a1 | ||
smulwt \a1, \twiddle, \a1 | ||
smlabt \tmp, \tmp, \q, \qa | ||
smlabt \a1, \a1, \q, \qa | ||
pkhtb \tmp, \a1, \tmp, asr#16 | ||
usub16 \a1, \a0, \tmp | ||
uadd16 \a0, \a0, \tmp | ||
.endm | ||
|
||
.macro two_doublebutterfly_plant a0, a1, a2, a3, twiddle0, twiddle1, tmp, q, qa | ||
doublebutterfly_plant \a0, \a1, \twiddle0, \tmp, \q, \qa | ||
doublebutterfly_plant \a2, \a3, \twiddle1, \tmp, \q, \qa | ||
.endm | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#ifndef REDUCE_H | ||
#define REDUCE_H | ||
|
||
#include <stdint.h> | ||
#include "params.h" | ||
|
||
#define MONT -4186625 // 2^32 % Q | ||
#define QINV 58728449 // q^(-1) mod 2^32 | ||
|
||
#define montgomery_reduce DILITHIUM_NAMESPACE(montgomery_reduce) | ||
/************************************************* | ||
* Name: montgomery_reduce | ||
* | ||
* Description: For finite field element a with -2^{31}Q <= a <= Q*2^31, | ||
* compute r \equiv a*2^{-32} (mod Q) such that -Q < r < Q. | ||
* | ||
* Arguments: - int64_t: finite field element a | ||
* | ||
* Returns r. | ||
**************************************************/ | ||
static inline int32_t montgomery_reduce(int64_t a) { | ||
int32_t t; | ||
|
||
t = (int64_t)(int32_t)a*QINV; | ||
t = (a - (int64_t)t*Q) >> 32; | ||
return t; | ||
} | ||
|
||
/************************************************* | ||
* Name: reduce32 | ||
* | ||
* Description: For finite field element a with a <= 2^{31} - 2^{22} - 1, | ||
* compute r \equiv a (mod Q) such that -6283009 <= r <= 6283007. | ||
* | ||
* Arguments: - int32_t: finite field element a | ||
* | ||
* Returns r. | ||
**************************************************/ | ||
static int32_t reduce32(int32_t a) { | ||
int32_t t; | ||
|
||
t = (a + (1 << 22)) >> 23; | ||
t = a - t*Q; | ||
return t; | ||
} | ||
|
||
/************************************************* | ||
* Name: caddq | ||
* | ||
* Description: Add Q if input coefficient is negative. | ||
* | ||
* Arguments: - int32_t: finite field element a | ||
* | ||
* Returns r. | ||
**************************************************/ | ||
static int32_t caddq(int32_t a) { | ||
a += (a >> 31) & Q; | ||
return a; | ||
} | ||
|
||
/************************************************* | ||
* Name: freeze | ||
* | ||
* Description: For finite field element a, compute standard | ||
* representative r = a mod^+ Q. | ||
* | ||
* Arguments: - int32_t: finite field element a | ||
* | ||
* Returns r. | ||
**************************************************/ | ||
static int32_t freeze(int32_t a) { | ||
a = reduce32(a); | ||
a = caddq(a); | ||
return a; | ||
} | ||
|
||
|
||
|
||
#endif |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.