It has been more than a year since the submission of candidates for the first round of NIST’s post-quantum cryptography started (Candidates qualifying for 2nd round announced on 30th January 2019 ). During this period, we have seen extensive and enthusiastic discussions, analysis and debates among researchers working in this field which has led to the improvement, new parameter proposals and in some cases withdrawal of some submissions. Parallel to this, many works have been done to efficiently implement various schemes on a wide range of platforms to bolster the claims of versatility and adaptability of various schemes.
There is little doubt that the Regev’s learning with errors (LWE) or its’ variants are preferred choice of researchers to build post-quantum schemes mostly due to its’ comparatively easier constructions and efficiency. However, one major drawback of schemes based on LWE is a relatively larger key size and memory footprint compared to other post-quantum schemes (e.g.: supersingular isogeny based schemes) for similar levels of security. So, the challenge is to show that LWE based cryptography can be practical on resource constrained devices too.
Our paper[1] in TCHES-2018 specifically tackles this challenge. Here, we briefly describe two of the strategies that were used in the paper. Though we have used `Saber’[2] which is lattice-based key encapsulation mechanism to demonstrate the techniques these methods, these methods are very generic and can be used with many different Lattice-based cryptosystems.
Large matrices in small memory, thanks to a Just-In-Time strategy
Unlike LWE or ring-LWE based schemes, module lattices use a matrix of polynomials. In ‘Saber’ this requires storing 9 polynomials each with 256 coefficients of 13 bits. This consumes almost 3.8 KB memory just to store the public matrix. For, smaller microcontrollers like the Cortex-M0 with typically 8 or 16 KB of memory, this is prohibitively high.
Usually, this matrix is generated by expanding a random seed using a hash function sunch as Keccak. This is shown in Fig.1.
However, this can be reduced drastically using a Just-In-Time strategy[1] meaningwe don’t generate a polynomial of the matrix until it is required in the matrix-vector multiplication. Specifically, we exploit the Sponge construction (absorb-squeeze-squeeze-. . .) of Keccak to achieve this. The random seed is absorbed in the beginning and the subsequent stages of squeezes are interleaved between each polynomial generation i.e., we run the squeeze just in time and just enough times to generate the necessary pseudorandom bytes for one polynomial, perform the matrix-vector multiplication with that polynomial and then run the squeeze(s) again, reusing the space used to store the previous polynomial. So, technically we can perform the whole matrix-vector multiplication by allocating space for one polynomial only instead of 9 polynomials. This is shown in Fig.2.
It should be noted that each squeeze stage generates 168 pseudorandom bytes, which is not a multiple of the number of bytes required for each polynomial in ‘Saber’. This problem can be solved if we generate the required number of pseudorandom bytes and discard the rest. But it will destroy the compatibility of the implementation with the actual proposal. Hence, this requires allocating a small buffer space and using sophisticated bookkeeping strategy which can be found in detail in the paper and omitted here for the sake of brevity.
SIMD instructions and speeding up schoolbook multiplication
Since the inception of ring-LWE, there has been a strong urge to use number theoretic transform (NTT) or fast Fourier transform (FFT) based multiplications which runs in complexity. But these multiplications require the modulus to be prime and the polynomial degree such that which severely restricts the possible choices of these parameters. On the other hand multiplication algorithms such as Toom-Cook, Karatsuba and schoolbook multiplication do not place any restriction on choice of and but runs in thus allowing designers a more liberal choice of modulus and polynomial degree.
However, it is not unforeseen in computer science that for small problem sizes, asymptotically slower solutions sometimes outperforms asymptotically faster solutions. For example, sorting algorithms like quicksort or mergesort are often outperformed by slower sorting algorithms for small arrays as factors like memory latency, recursion overhead, etc. overcome the asymptotical advantage.
Similarly, here we show that schemes like ‘Saber’, NTRU-KEM, etc. which use a combination of Toom-Cook, Karatsuba and schoolbook multiplication can be highly competitive and sometimes even outperform NTT or FFT based schemes by proper memory management and utilizing of special instructions. In this multiplication hierarchy, schoolbook multiplication lies at the lowest level hence executed most frequently, for example, in ‘Saber’ multiplication requires 63, schoolbook multiplications. Hence, improving the efficiency of the schoolbook multiplication improves the efficiency of the scheme to a great extent.
Each coefficient of polynomials used in Saber fits into a half word of Cortex-M4. As the coefficients are accessed and stored in sequential order during schoolbook multiplication, two coefficients can be loaded into a register using full word load and store (LDR and STR) instructions. This saves many clock cycles for memory operations. Contrast this to the irregular memory access pattern of NTT multiplication where the consecutive memory accesses are apart from each other by n/8,n/4,… etc distances. This precludes such efficient memory operation. Further, using DSP instructions of Cortex-M4 which can cross-multiply and accumulate two half words of a register, we can speed-up the schoolbook multiplication even more. To understand this, consider the small schoolbook multiplication of two polynomials of 4 coefficients in Fig.3. Using normal multiplication instructions it would take at least 16 multiply and accumulate instructions to complete the multiplication.
However, the multiply and accumulate operation (marked in blue rectangle) requires two multiply instructions. If we use SMLADX instruction which cross multiplies and accumulates half words of registers, on registers containing and this can be done in one instruction only. We apply this same technique for other registers too. Also, we can use packing instructions on adjacent coefficients which resides on different registers to pack them in a single spare register and use the above technique(marked with the green rectangle). In the above example, the whole multiplication can be done in 11 instructions instead of 16 instructions. In our schoolbook multiplication with 16 coefficient polynomials we need only 168 multiply and accumulate instructions instead of 256 instructions which is approximately 37.5% reduction in instruction count. As all the multiplication instructions i.e., DSP and non-DSP takes the same number of clock cycles in Cortex-M4 this translates to a significant speedup of the scheme.
Summary
To summarize, here two methods for an efficient and compact implementation of Saber have been described. Our paper[1] also includes other novel techniques like efficient in-place Karatsuba multiplication, vertical coefficient scanning Toom-Cook multiplication, a modified Saber named as Saber# to reduce memory footprint during the encapsulation stage, etc. As mentioned earlier these techniques can be easily applied to other schemes. For example, the Just-in-time strategy to generate module lattice can be used in any module lattice-based cryptosystem like the Kyber[3] or the Dilithium[4] digital signature scheme. Similarly, the efficient SIMD based schoolbook multiplication can be used in any scheme where NTT cannot be used e.g.: NTRU-KEM[5] or NTRU prime[6].
Lastly, we want to mention that though, in theory, schemes based on module-lattices have larger memory requirement than ring-LWE based schemes and Toom-Cook, Karatsuba and schoolbook based multiplications are complexity wise slower than NTT multiplications, but for small dimensions(), careful usage of underlying instructions and efficient usage of memory they outperform schemes with faster algorithms and lower memory requirements. This is similar to the proverbial hare and tortoise race.
Adaptation in other works
The techniques described above have been adapted in two more works to speed up other candidates in post-quantum standardization competition.
In [7], the authors applied the SIMD based multiplication to different schemes. Also, they did an extensive study to determine the cut-off for schoolbook multiplication i.e., when the Karatsuba should be stopped and schoolbook multiplication should be started for optimal performance. For Saber implementation, they have unrolled the schoolbook multiplication loops which enabled for more usage of SIMD instructions resulting in better performance than our work.
The work in [8], solely focused on Frodo key encapsulation mechanism. Apart from speeding up the matrix-vector multiplication using SIMD instructions, they have also reduced the memory requirement for the matrix generation using the Just-In-Time or On-The-Fly technique. However, in their method, they adapted the Xorshift instead of Keccak to generate the pseudorandom numbers.
References
[1] “Saber on ARM, CCA-secure module lattice-based key encapsulation on ARM.”- Angshuman Karmakar, Jose Maria Bermudo Mera, Sujoy Sinha Roy and Ingrid Verbauwhede ( https://eprint.iacr.org/2018/682.pdf )
[2] “Saber: Module-LWR based key exchange, CPA-secure encryption and CCA-secure KEM.”-Jan-Pieter D’Anvers, Angshuman Karmakar Sujoy Sinha Roy, and Frederik Vercauteren ( https://eprint.iacr.org/2018/230.pdf )
[3] “CRYSTALS – Kyber: a CCA-secure module-lattice-based KEM.”-Joppe Bos, Léo Ducas, Eike Kiltz, Tancrède Lepoint, Vadim Lyubashevsky, John M. Schanck, Peter Schwabe, Gregor Seiler, Damien Stehlé ( https://eprint.iacr.org/2017/634.pdf )
[4] “CRYSTALS-Dilithium: A Lattice-Based Digital Signature Scheme.”- Léo Ducas, Eike Kiltz, Tancrède Lepoint, Vadim Lyubashevsky, Peter Schwabe, Gregor Seiler, and Damien Stehlé ( https://eprint.iacr.org/2017/633.pdf )
[5] “High-speed key encapsulation from NTRU.”-Andreas Hülsing, Joost Rijneveld, John Schanck, and Peter Schwabe ( https://cryptojedi.org/papers/ntrukem-20170627.pdf )
[6] “NTRU Prime.”-Daniel J. Bernstein, Chitchanok Chuengsatiansup, Tanja Lange, and Christine van Vredendaal ( https://ntruprime.cr.yp.to/ntruprime-20160511.pdf )
[7] “Faster multiplication in on Cortex-M4 to speed up NIST PQC candidates.”-Matthias J. Kannwischer, Joost Rijneveld and Peter Schwabe ( https://cryptojedi.org/papers/latticem4-20181019.pdf )
[8] “Fly, you fool! Faster Frodo for the ARM Cortex-M4”-Joppe W. Bos, Simon Friedberger, Marco Martinoli, Elisabeth Oswald, and Martijn Stam ( https://eprint.iacr.org/2018/1116.pdf )
Acknowledgement
Thank you to Ingrid Verbauwhede and Jose Maria Bermudo Mera for their contributions and for proofreading this blog post.


