From 752ff05b52c4c85a5baf72607b7c5cbaed9ebad8 Mon Sep 17 00:00:00 2001 From: Leon Date: Thu, 16 May 2019 15:20:06 +0200 Subject: [PATCH 01/35] add folder + META.yml for ledacrypt kem lt for category 1 with n0=2 --- crypto_kem/ledacryptkem2128/META.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 crypto_kem/ledacryptkem2128/META.yml diff --git a/crypto_kem/ledacryptkem2128/META.yml b/crypto_kem/ledacryptkem2128/META.yml new file mode 100644 index 00000000..c810d153 --- /dev/null +++ b/crypto_kem/ledacryptkem2128/META.yml @@ -0,0 +1,18 @@ +name: ledacryptkem2128 +type: kem +claimed-nist-level: 1 +claimed-security: IND-CCA2 +length-public-key: 6520 +length-secret-key: 468 +length-ciphertext: 6520 +length-shared-secret: 32 +nistkat-sha256: 3bb5945e0aea26f121e1d56946760e506bdfbebb07e2fb018ce737b90b1eee2b +principal-submitter: Marco Baldi +auxiliary-submitters: + - Alessandro Barenghi + - Franco Chiaraluce + - Gerardo Pelosi + - Paolo Santini +implementations: + - name: clean + version: 2.0 From 587090e744935a96b043a72e37be8b7052a1154d Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 19 May 2019 19:14:46 +0200 Subject: [PATCH 02/35] renamed to something more appropriate, copied source code, ran astyle --- crypto_kem/ledakemlt12/META.yml | 18 + .../clean/H_Q_matrices_generation.c | 71 ++ .../clean/H_Q_matrices_generation.h | 49 ++ crypto_kem/ledakemlt12/clean/Makefile | 55 ++ crypto_kem/ledakemlt12/clean/aes256.c | 600 ++++++++++++++++ crypto_kem/ledakemlt12/clean/aes256.h | 41 ++ crypto_kem/ledakemlt12/clean/api.h | 75 ++ crypto_kem/ledakemlt12/clean/bf_decoding.c | 114 +++ crypto_kem/ledakemlt12/clean/bf_decoding.h | 78 +++ crypto_kem/ledakemlt12/clean/dfr_test.c | 114 +++ crypto_kem/ledakemlt12/clean/dfr_test.h | 3 + crypto_kem/ledakemlt12/clean/gf2x_arith.c | 462 ++++++++++++ crypto_kem/ledakemlt12/clean/gf2x_arith.h | 112 +++ .../clean/gf2x_arith_mod_xPplusOne.c | 656 ++++++++++++++++++ .../clean/gf2x_arith_mod_xPplusOne.h | 300 ++++++++ crypto_kem/ledakemlt12/clean/gf2x_limbs.h | 94 +++ crypto_kem/ledakemlt12/clean/kem.c | 97 +++ crypto_kem/ledakemlt12/clean/niederreiter.h | 58 ++ .../ledakemlt12/clean/niederreiter_decrypt.c | 153 ++++ .../ledakemlt12/clean/niederreiter_decrypt.h | 45 ++ .../ledakemlt12/clean/niederreiter_encrypt.c | 57 ++ .../ledakemlt12/clean/niederreiter_encrypt.h | 41 ++ .../ledakemlt12/clean/niederreiter_keygen.c | 145 ++++ .../ledakemlt12/clean/niederreiter_keygen.h | 43 ++ .../ledakemlt12/clean/qc_ldpc_parameters.h | 157 +++++ crypto_kem/ledakemlt12/clean/rng.c | 336 +++++++++ crypto_kem/ledakemlt12/clean/rng.h | 96 +++ crypto_kem/ledakemlt12/clean/sha3.h | 43 ++ 28 files changed, 4113 insertions(+) create mode 100644 crypto_kem/ledakemlt12/META.yml create mode 100644 crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c create mode 100644 crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h create mode 100644 crypto_kem/ledakemlt12/clean/Makefile create mode 100644 crypto_kem/ledakemlt12/clean/aes256.c create mode 100644 crypto_kem/ledakemlt12/clean/aes256.h create mode 100644 crypto_kem/ledakemlt12/clean/api.h create mode 100644 crypto_kem/ledakemlt12/clean/bf_decoding.c create mode 100644 crypto_kem/ledakemlt12/clean/bf_decoding.h create mode 100644 crypto_kem/ledakemlt12/clean/dfr_test.c create mode 100644 crypto_kem/ledakemlt12/clean/dfr_test.h create mode 100644 crypto_kem/ledakemlt12/clean/gf2x_arith.c create mode 100644 crypto_kem/ledakemlt12/clean/gf2x_arith.h create mode 100644 crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c create mode 100644 crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h create mode 100644 crypto_kem/ledakemlt12/clean/gf2x_limbs.h create mode 100644 crypto_kem/ledakemlt12/clean/kem.c create mode 100644 crypto_kem/ledakemlt12/clean/niederreiter.h create mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c create mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h create mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c create mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h create mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_keygen.c create mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_keygen.h create mode 100644 crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h create mode 100644 crypto_kem/ledakemlt12/clean/rng.c create mode 100644 crypto_kem/ledakemlt12/clean/rng.h create mode 100644 crypto_kem/ledakemlt12/clean/sha3.h diff --git a/crypto_kem/ledakemlt12/META.yml b/crypto_kem/ledakemlt12/META.yml new file mode 100644 index 00000000..426ffdc3 --- /dev/null +++ b/crypto_kem/ledakemlt12/META.yml @@ -0,0 +1,18 @@ +name: LEDAcryptKEM-LT12 +type: kem +claimed-nist-level: 1 +claimed-security: IND-CCA2 +length-public-key: 6520 +length-secret-key: 468 +length-ciphertext: 6520 +length-shared-secret: 32 +nistkat-sha256: 3bb5945e0aea26f121e1d56946760e506bdfbebb07e2fb018ce737b90b1eee2b +principal-submitter: Marco Baldi +auxiliary-submitters: + - Alessandro Barenghi + - Franco Chiaraluce + - Gerardo Pelosi + - Paolo Santini +implementations: + - name: clean + version: 2.0 diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c new file mode 100644 index 00000000..703f316b --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c @@ -0,0 +1,71 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#include "H_Q_matrices_generation.h" +#include "gf2x_arith_mod_xPplusOne.h" + +/*----------------------------------------------------------------------------*/ + +void generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], + POSITION_T HtrPosOnes[N0][DV], + AES_XOF_struct *keys_expander + ) { + for (int i = 0; i < N0; i++) { + /* Generate a random block of Htr */ + rand_circulant_sparse_block(&HtrPosOnes[i][0], + DV, + keys_expander); + } + for (int i = 0; i < N0; i++) { + /* Obtain directly the sparse representation of the block of H */ + for (int k = 0; k < DV; k++) { + HPosOnes[i][k] = (P - HtrPosOnes[i][k]) % P; /* transposes indexes */ + }// end for k + } +} // end generateHtr_HtrPosOnes + +/*----------------------------------------------------------------------------*/ + +void generateQsparse(POSITION_T pos_ones[N0][M], + AES_XOF_struct *keys_expander) { + for (int i = 0; i < N0; i++) { + int placed_ones = 0; + for (int j = 0; j < N0; j++) { + rand_circulant_sparse_block(&pos_ones[i][placed_ones], + qBlockWeights[i][j], + keys_expander); + placed_ones += qBlockWeights[i][j]; + } // end for j + } // end for i +} // end generateQsparse + +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h new file mode 100644 index 00000000..98a67b51 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h @@ -0,0 +1,49 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once +#include "qc_ldpc_parameters.h" +#include "gf2x_limbs.h" +#include "rng.h" +/*----------------------------------------------------------------------------*/ + + +void generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], + POSITION_T HtrPosOnes[N0][DV], + AES_XOF_struct *niederreiter_keys_expander); + +/*----------------------------------------------------------------------------*/ + +void generateQsparse(POSITION_T pos_ones[N0][M], + AES_XOF_struct *niederreiter_keys_expander); + +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/Makefile b/crypto_kem/ledakemlt12/clean/Makefile new file mode 100644 index 00000000..c04d41f5 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/Makefile @@ -0,0 +1,55 @@ +# This Makefile can be used with GNU Make or BSD Make + +SL = 1 +N0 = 2 +DFR_SL_LEVEL = 1 + +LIB=libLEDAkem_sl$(SL)_N0$(N0)_clean.a +HEADERS=aes256.h api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ + gf2x_arith.h gf2x_limbs.h H_Q_matrices_generation.h niederreiter_decrypt.h \ + niederreiter_encrypt.h niederreiter_keygen.h niederreiter.h \ + qc_ldpc_parameters.h rng.h sha3.h + +OBJECTS=aes256.o bf_decoding.o dfr_test.o gf2x_arith_mod_xPplusOne.o \ + gf2x_arith.o H_Q_matrices_generation.o kem.o niederreiter_decrypt.o \ + niederreiter_encrypt.o niederreiter_keygen.o rng.o + +CFLAGS=-O3 -Wall -Wextra -Wpedantic -Wvla -Wmissing-prototypes -std=c99 \ + -DCATEGORY=$(SL) -DN0=$(N0) -DDFR_SL_LEVEL=$(DFR_SL_LEVEL) -I../../../common $(EXTRAFLAGS) + +all: $(LIB) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c -o $@ $< + +$(LIB): $(OBJECTS) + $(AR) -r $@ $(OBJECTS) + +clean: + $(RM) $(OBJECTS) + $(RM) $(LIB) + + +# old makefile +# CFLAGS = -DCATEGORY=$(SL) -DN0=$(N0) -DDFR_SL_LEVEL=$(DFR_SL_LEVEL) -DCPU_WORD_BITS=64 \ +# -std=c11 -Wall -pedantic -Wmaybe-uninitialized -Wuninitialized \ +# -march=native -O3 -g3 +# LDFLAGS = -lm -lkeccak +# INCLUDES = -I./include +# SRCDIR = library +# OBJDIR = bin +# +# # Gathers the names of all C files +# CSRC = $(wildcard $(SRCDIR)/*.c) +# # Produces in $(COBJS) the names of .o object files for all C files +# COBJS = $(CSRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +# +# $(OBJDIR)/%.o: $(SRCDIR)/%.c +# $(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ +# +# .PHONY : all clean +# +# all: $(COBJS) +# ar rcs $(OBJDIR)/libLEDAkem_sl$(SL)_N0$(N0).a $(COBJS) +# clean: +# $(RM) $(OBJDIR)/*.o $(OBJDIR)/libLEDAkem_sl$(SL)_N0$(N0).a diff --git a/crypto_kem/ledakemlt12/clean/aes256.c b/crypto_kem/ledakemlt12/clean/aes256.c new file mode 100644 index 00000000..7d6dea97 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/aes256.c @@ -0,0 +1,600 @@ +/** + * AES-256 self contained implementation derived from : + * + * rijndael-alg-fst.h + * + * @version 3.0 (December 2000) + * + * Optimised ANSI C code for the Rijndael cipher (now AES) + * + * @author Vincent Rijmen + * @author Antoon Bosselaers + * @author Paulo Barreto + * + * originally placed in the public domain by the authors. + * + * + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "aes256.h" + +/* +Te0[x] = S [x].[02, 01, 01, 03]; +Te1[x] = S [x].[03, 02, 01, 01]; +Te2[x] = S [x].[01, 03, 02, 01]; +Te3[x] = S [x].[01, 01, 03, 02]; +Te4[x] = S [x].[01, 01, 01, 01]; +*/ + +static const uint32_t Te0[256] = { + 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, + 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, + 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, + 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, + 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, + 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, + 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, + 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, + 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, + 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, + 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, + 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, + 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, + 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, + 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, + 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, + 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, + 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, + 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, + 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, + 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, + 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, + 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, + 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, + 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, + 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, + 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, + 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, + 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, + 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, + 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, + 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, + 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, + 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, + 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, + 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, + 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, + 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, + 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, + 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, + 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, + 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, + 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, + 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, + 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, + 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, + 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, + 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, + 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, + 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, + 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, + 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, + 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, + 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, + 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, + 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, + 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, + 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, + 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, + 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, + 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, + 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, + 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, + 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, +}; +static const uint32_t Te1[256] = { + 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, + 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, + 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, + 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, + 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, + 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, + 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, + 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, + 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, + 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, + 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, + 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, + 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, + 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, + 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, + 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, + 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, + 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, + 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, + 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, + 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, + 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, + 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, + 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, + 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, + 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, + 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, + 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, + 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, + 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, + 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, + 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, + 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, + 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, + 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, + 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, + 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, + 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, + 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, + 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, + 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, + 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, + 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, + 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, + 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, + 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, + 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, + 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, + 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, + 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, + 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, + 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, + 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, + 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, + 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, + 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, + 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, + 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, + 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, + 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, + 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, + 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, + 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, + 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, +}; +static const uint32_t Te2[256] = { + 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, + 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, + 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, + 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, + 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, + 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, + 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, + 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, + 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, + 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, + 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, + 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, + 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, + 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, + 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, + 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, + 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, + 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, + 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, + 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, + 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, + 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, + 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, + 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, + 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, + 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, + 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, + 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, + 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, + 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, + 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, + 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, + 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, + 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, + 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, + 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, + 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, + 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, + 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, + 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, + 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, + 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, + 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, + 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, + 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, + 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, + 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, + 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, + 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, + 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, + 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, + 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, + 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, + 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, + 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, + 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, + 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, + 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, + 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, + 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, + 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, + 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, + 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, + 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, +}; +static const uint32_t Te3[256] = { + + 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, + 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, + 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, + 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, + 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, + 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, + 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, + 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, + 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, + 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, + 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, + 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, + 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, + 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, + 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, + 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, + 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, + 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, + 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, + 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, + 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, + 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, + 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, + 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, + 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, + 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, + 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, + 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, + 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, + 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, + 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, + 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, + 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, + 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, + 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, + 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, + 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, + 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, + 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, + 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, + 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, + 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, + 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, + 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, + 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, + 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, + 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, + 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, + 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, + 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, + 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, + 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, + 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, + 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, + 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, + 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, + 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, + 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, + 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, + 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, + 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, + 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, + 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, + 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, +}; +static const uint32_t Te4[256] = { + 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, + 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, + 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU, + 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U, + 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU, + 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U, + 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU, + 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U, + 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U, + 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU, + 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U, + 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U, + 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U, + 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU, + 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U, + 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U, + 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU, + 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U, + 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U, + 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U, + 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU, + 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU, + 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U, + 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU, + 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU, + 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U, + 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU, + 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U, + 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU, + 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U, + 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U, + 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U, + 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU, + 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U, + 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU, + 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U, + 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU, + 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U, + 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U, + 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU, + 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU, + 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU, + 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U, + 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U, + 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU, + 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U, + 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU, + 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U, + 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU, + 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U, + 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU, + 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU, + 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U, + 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU, + 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U, + 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU, + 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U, + 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U, + 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U, + 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU, + 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU, + 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U, + 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU, + 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, +}; + +static const uint32_t rcon[] = { + 0x01000000, 0x02000000, 0x04000000, 0x08000000, + 0x10000000, 0x20000000, 0x40000000, 0x80000000, + 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ +}; + +#define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) + +#define GETU32(pt) (((uint32_t)(pt)[0] << 24) ^ ((uint32_t)(pt)[1] << 16) ^ ((uint32_t)(pt)[2] << 8) ^ ((uint32_t)(pt)[3])) +#define PUTU32(ct, st) { (ct)[0] = (uint8_t)((st) >> 24); (ct)[1] = (uint8_t)((st) >> 16); (ct)[2] = (uint8_t)((st) >> 8); (ct)[3] = (uint8_t)(st); } + +/** + * Expand the cipher key into the encryption key schedule. + * + * @return the number of rounds for the given cipher key size. + */ +int rijndaelKeySetupEnc(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], + int keyBits) { + int i = 0; + uint32_t temp; + + rk[0] = GETU32(cipherKey ); + rk[1] = GETU32(cipherKey + 4); + rk[2] = GETU32(cipherKey + 8); + rk[3] = GETU32(cipherKey + 12); + rk[4] = GETU32(cipherKey + 16); + rk[5] = GETU32(cipherKey + 20); + rk[6] = GETU32(cipherKey + 24); + rk[7] = GETU32(cipherKey + 28); + for (;;) { + temp = rk[ 7]; + rk[ 8] = rk[ 0] ^ + (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ + (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ + (Te4[(temp ) & 0xff] & 0x0000ff00) ^ + (Te4[(temp >> 24) ] & 0x000000ff) ^ + rcon[i]; + rk[ 9] = rk[ 1] ^ rk[ 8]; + rk[10] = rk[ 2] ^ rk[ 9]; + rk[11] = rk[ 3] ^ rk[10]; + if (++i == 7) { + return 14; + } + temp = rk[11]; + rk[12] = rk[ 4] ^ + (Te4[(temp >> 24) ] & 0xff000000) ^ + (Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(temp ) & 0xff] & 0x000000ff); + rk[13] = rk[ 5] ^ rk[12]; + rk[14] = rk[ 6] ^ rk[13]; + rk[15] = rk[ 7] ^ rk[14]; + + rk += 8; + } + return 0; +} + +void rijndaelEncrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, + const uint8_t pt[16], uint8_t ct[16]) { + uint32_t s0, s1, s2, s3, t0 = 0, t1 = 0, t2 = 0, t3 = 0; + + /* + * map byte array block to cipher state + * and add initial round key: + */ + s0 = GETU32(pt ) ^ rk[0]; + s1 = GETU32(pt + 4) ^ rk[1]; + s2 = GETU32(pt + 8) ^ rk[2]; + s3 = GETU32(pt + 12) ^ rk[3]; + /* round 1: */ + t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & + 0xff] ^ rk[ 4]; + t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & + 0xff] ^ rk[ 5]; + t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & + 0xff] ^ rk[ 6]; + t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & + 0xff] ^ rk[ 7]; + /* round 2: */ + s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & + 0xff] ^ rk[ 8]; + s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & + 0xff] ^ rk[ 9]; + s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & + 0xff] ^ rk[10]; + s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & + 0xff] ^ rk[11]; + /* round 3: */ + t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & + 0xff] ^ rk[12]; + t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & + 0xff] ^ rk[13]; + t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & + 0xff] ^ rk[14]; + t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & + 0xff] ^ rk[15]; + /* round 4: */ + s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & + 0xff] ^ rk[16]; + s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & + 0xff] ^ rk[17]; + s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & + 0xff] ^ rk[18]; + s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & + 0xff] ^ rk[19]; + /* round 5: */ + t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & + 0xff] ^ rk[20]; + t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & + 0xff] ^ rk[21]; + t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & + 0xff] ^ rk[22]; + t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & + 0xff] ^ rk[23]; + /* round 6: */ + s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & + 0xff] ^ rk[24]; + s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & + 0xff] ^ rk[25]; + s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & + 0xff] ^ rk[26]; + s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & + 0xff] ^ rk[27]; + /* round 7: */ + t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & + 0xff] ^ rk[28]; + t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & + 0xff] ^ rk[29]; + t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & + 0xff] ^ rk[30]; + t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & + 0xff] ^ rk[31]; + /* round 8: */ + s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & + 0xff] ^ rk[32]; + s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & + 0xff] ^ rk[33]; + s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & + 0xff] ^ rk[34]; + s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & + 0xff] ^ rk[35]; + /* round 9: */ + t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & + 0xff] ^ rk[36]; + t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & + 0xff] ^ rk[37]; + t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & + 0xff] ^ rk[38]; + t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & + 0xff] ^ rk[39]; + + /* round 10: */ + s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & + 0xff] ^ rk[40]; + s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & + 0xff] ^ rk[41]; + s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & + 0xff] ^ rk[42]; + s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & + 0xff] ^ rk[43]; + /* round 11: */ + t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & + 0xff] ^ rk[44]; + t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & + 0xff] ^ rk[45]; + t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & + 0xff] ^ rk[46]; + t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & + 0xff] ^ rk[47]; + + /* round 12: */ + s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & + 0xff] ^ rk[48]; + s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & + 0xff] ^ rk[49]; + s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & + 0xff] ^ rk[50]; + s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & + 0xff] ^ rk[51]; + /* round 13: */ + t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & + 0xff] ^ rk[52]; + t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & + 0xff] ^ rk[53]; + t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & + 0xff] ^ rk[54]; + t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & + 0xff] ^ rk[55]; + + + rk += Nr << 2; + /* + * apply last round and + * map cipher state to byte array block: + */ + s0 = + (Te4[(t0 >> 24) ] & 0xff000000) ^ + (Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(t3 ) & 0xff] & 0x000000ff) ^ + rk[0]; + PUTU32(ct, s0); + s1 = + (Te4[(t1 >> 24) ] & 0xff000000) ^ + (Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(t0 ) & 0xff] & 0x000000ff) ^ + rk[1]; + PUTU32(ct + 4, s1); + s2 = + (Te4[(t2 >> 24) ] & 0xff000000) ^ + (Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(t1 ) & 0xff] & 0x000000ff) ^ + rk[2]; + PUTU32(ct + 8, s2); + s3 = + (Te4[(t3 >> 24) ] & 0xff000000) ^ + (Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(t2 ) & 0xff] & 0x000000ff) ^ + rk[3]; + PUTU32(ct + 12, s3); +} diff --git a/crypto_kem/ledakemlt12/clean/aes256.h b/crypto_kem/ledakemlt12/clean/aes256.h new file mode 100644 index 00000000..0bdcce8f --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/aes256.h @@ -0,0 +1,41 @@ +#pragma once +/** + * AES-256 self contained implementation derived from : + * + * rijndael-alg-fst.h + * + * @version 3.0 (December 2000) + * + * Optimised ANSI C code for the Rijndael cipher (now AES) + * + * @author Vincent Rijmen + * @author Antoon Bosselaers + * @author Paulo Barreto + * + * originally placed in the public domain by the authors. + * + * + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#define NROUNDS 14 +#define KEYLEN_b 256 + +int rijndaelKeySetupEnc(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], + int keyBits); +void rijndaelEncrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, + const uint8_t pt[16], uint8_t ct[16]); diff --git a/crypto_kem/ledakemlt12/clean/api.h b/crypto_kem/ledakemlt12/clean/api.h new file mode 100644 index 00000000..cf7cd668 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/api.h @@ -0,0 +1,75 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once + +#include "qc_ldpc_parameters.h" +#include "gf2x_limbs.h" +#include "gf2x_arith_mod_xPplusOne.h" + +#define CRYPTO_ALGNAME "LEDA" + +/* required bytes of input randomness */ +#define CRYPTO_RANDOMBYTES TRNG_BYTE_LENGTH + +/* size in bytes of the secret key */ +#define CRYPTO_SECRETKEYBYTES TRNG_BYTE_LENGTH+1 + +/* size in bytes of the public key */ +#define CRYPTO_PUBLICKEYBYTES ((N0-1)*NUM_DIGITS_GF2X_ELEMENT*DIGIT_SIZE_B) + +/* size in bytes of the shared secret */ +#define CRYPTO_BYTES HASH_BYTE_LENGTH + +/*size in bytes of the ciphertext*/ +#define CRYPTO_CIPHERTEXTBYTES (NUM_DIGITS_GF2X_ELEMENT*DIGIT_SIZE_B) + +/* Your functions must return 0 to indicate success, -1 to indicate an error + * condition */ + +/* Generates a keypair - pk is the public key and sk is the secret key. */ +int crypto_kem_keypair( unsigned char *pk, + unsigned char *sk ); + +/* Encrypt - pk is the public key, ct is a key encapsulation message + (ciphertext), ss is the shared secret.*/ +int crypto_kem_enc( unsigned char *ct, + unsigned char *ss, + const unsigned char *pk ); + + +/* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private + key, ss is the shared secret */ + +int crypto_kem_dec( unsigned char *ss, + const unsigned char *ct, + const unsigned char *sk ); diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/clean/bf_decoding.c new file mode 100644 index 00000000..e0801a2d --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.c @@ -0,0 +1,114 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ +#include "bf_decoding.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include +#include + +#define ROTBYTE(a) ( (a << 8) | (a >> (DIGIT_SIZE_b - 8)) ) +#define ROTUPC(a) ( (a >> 8) | (a << (DIGIT_SIZE_b - 8)) ) + +int thresholds[2] = {B0, (DV * M) / 2 + 1}; + +int bf_decoding(DIGIT out[], // N0 polynomials + const POSITION_T HtrPosOnes[N0][DV], + const POSITION_T QtrPosOnes[N0][M], + DIGIT privateSyndrome[] // 1 polynomial + ) { + #if P < 64 +#error The circulant block size should exceed 64 + #endif + + uint8_t unsatParityChecks[N0 * P]; + POSITION_T currQBlkPos[M], currQBitPos[M]; + DIGIT currSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + int check; + int iteration = 0; + + do { + gf2x_copy(currSyndrome, privateSyndrome); + memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); + for (int i = 0; i < N0; i++) { + for (int valueIdx = 0; valueIdx < P; valueIdx++) { + for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { + POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); + if (gf2x_get_coeff(currSyndrome, tmp)) { + unsatParityChecks[i * P + valueIdx]++; + } + } + } + } + + /* iteration based threshold determination*/ + int corrt_syndrome_based = thresholds[iteration]; + + //Computation of correlation with a full Q matrix + for (int i = 0; i < N0; i++) { + for (int j = 0; j < P; j++) { + int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + int endQblockIdx = 0; + int correlation = 0; + + for (int blockIdx = 0; blockIdx < N0; blockIdx++) { + endQblockIdx += qBlockWeights[blockIdx][i]; + int currblockoffset = blockIdx * P; + for (; currQoneIdx < endQblockIdx; currQoneIdx++) { + int tmp = QtrPosOnes[i][currQoneIdx] + j; + tmp = tmp >= P ? tmp - P : tmp; + currQBitPos[currQoneIdx] = tmp; + currQBlkPos[currQoneIdx] = blockIdx; + correlation += unsatParityChecks[tmp + currblockoffset]; + } + } + /* Correlation based flipping */ + if (correlation >= corrt_syndrome_based) { + gf2x_toggle_coeff(out + NUM_DIGITS_GF2X_ELEMENT * i, j); + for (int v = 0; v < M; v++) { + unsigned syndromePosToFlip; + for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { + syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); + syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; + gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + } + } // end for v + } // end if + } // end for j + } // end for i + + iteration = iteration + 1; + check = 0; + while (check < NUM_DIGITS_GF2X_ELEMENT && privateSyndrome[check++] == 0); + + } while (iteration < ITERATIONS_MAX && check < NUM_DIGITS_GF2X_ELEMENT); + + return (check == NUM_DIGITS_GF2X_ELEMENT); +} // end QdecodeSyndromeThresh_bitFlip_sparse diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.h b/crypto_kem/ledakemlt12/clean/bf_decoding.h new file mode 100644 index 00000000..b14173c2 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.h @@ -0,0 +1,78 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + + +#pragma once +#include "qc_ldpc_parameters.h" +#include "gf2x_limbs.h" +#define ITERATIONS_MAX (2) + + +int bf_decoding(DIGIT err[], + const POSITION_T HtrPosOnes[N0][DV], + const POSITION_T QtrPosOnes[N0][M], // N0 vectors containing exp.s of Qtr ones + DIGIT privateSyndrome[] // 1 polynomial -- param. in/out + ); + +/* Definitions for DFR level 2^-64 */ +#if (CATEGORY == 1) && (N0 == 2) && (DFR_SL_LEVEL == 0) +#define B0 44 +#define T_BAR 4 +#endif + +#if ((CATEGORY == 2) || (CATEGORY == 3)) && (N0 == 2) && (DFR_SL_LEVEL == 0) +#define B0 64 +#define T_BAR 5 +#endif + +#if ((CATEGORY == 4) || (CATEGORY == 5)) && (N0 == 2) && (DFR_SL_LEVEL == 0) +#define B0 89 +#define T_BAR 6 +#endif + +/* Definitions for DFR level 2^-SL */ +#if (CATEGORY == 1) && (N0 == 2) && (DFR_SL_LEVEL == 1) +#define B0 43 +#define T_BAR 4 +#endif + +#if ((CATEGORY == 2) || (CATEGORY == 3)) && (N0 == 2) && (DFR_SL_LEVEL == 1) +#define B0 64 +#define T_BAR 5 +#endif + +#if ((CATEGORY == 4) || (CATEGORY == 5)) && (N0 == 2) && (DFR_SL_LEVEL == 1) +#define B0 88 +#define T_BAR 6 +#endif + + diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt12/clean/dfr_test.c new file mode 100644 index 00000000..d8d93d33 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/dfr_test.c @@ -0,0 +1,114 @@ +#include "qc_ldpc_parameters.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "bf_decoding.h" + +#include + +/*---------------------------------------------------------------------------*/ +/* Tests if the current code attains the desired DFR. If that is the case, + * computes the threshold for the second iteration of the decoder and stores + * it in the globally accessible vector*/ + +extern int thresholds[2]; + +int DFR_test(POSITION_T LSparse[N0][DV * M]) { + + POSITION_T LSparse_loc[N0][DV * M]; + + /*transpose blocks of L, we need its columns */ + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + if (LSparse[i][j] != 0) { + LSparse_loc[i][j] = (P - LSparse[i][j]) ; + } + } + quicksort(LSparse_loc[i], DV * M); + } + /* Gamma matrix: an N0 x N0 block circulant matrix with block size p + * gamma[a][b][c] stores the intersection of the first column of the a-th + * block of L with the c-th column of the b-th block of L */ + /* Gamma computation can be accelerated employing symmetry and QC properties */ + int gamma[N0][N0][P] = {{{0}}}; + unsigned int rotated_column[DV * M]; + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + for (int k = 0; k < P; k++) { + /* compute the rotated sparse column needed */ + for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { + rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; + } + quicksort(rotated_column, DV * M); + /* compute the intersection amount */ + int firstidx = 0, secondidx = 0; + int intersectionval = 0; + while ( (firstidx < DV * M) && (secondidx < DV * M) ) { + if ( LSparse_loc[i][firstidx] == rotated_column[secondidx] ) { + intersectionval++; + firstidx++; + secondidx++; + } + if ( LSparse_loc[i][firstidx] > rotated_column[secondidx] ) { + secondidx++; + } + if ( LSparse_loc[i][firstidx] < rotated_column[secondidx] ) { + firstidx++; + } + } + gamma[i][j][k] = intersectionval; + + } + } + } + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + gamma[i][j][0] = 0; + } + } + /* build histogram of values in gamma */ + unsigned int gammaHist[N0][DV * M + 1] = {{0}}; + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + for (int k = 0; k < P; k++) { + gammaHist[i][gamma[i][j][k]]++; + } + } + } + + int maxMut[N0], maxMutMinusOne[N0]; + int allBlockMaxSumst, allBlockMaxSumstMinusOne; + + for (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0; gammaBlockRowIdx++) { + int toAdd = T_BAR - 1; + maxMutMinusOne[gammaBlockRowIdx] = 0; + int histIdx = DV * M; + while ( (histIdx > 0) && (toAdd > 0)) { + if (gammaHist[gammaBlockRowIdx][histIdx] > toAdd ) { + maxMutMinusOne[gammaBlockRowIdx] += histIdx * toAdd; + toAdd = 0; + } else { + maxMutMinusOne[gammaBlockRowIdx] += histIdx * gammaHist[gammaBlockRowIdx][histIdx]; + toAdd -= gammaHist[gammaBlockRowIdx][histIdx]; + histIdx--; + } + } + maxMut[gammaBlockRowIdx] = histIdx + maxMutMinusOne[gammaBlockRowIdx]; + } + + + /*seek max values across all gamma blocks */ + allBlockMaxSumst = maxMut[0]; + allBlockMaxSumstMinusOne = maxMutMinusOne[0]; + for (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0 ; gammaBlockRowIdx++) { + allBlockMaxSumst = allBlockMaxSumst < maxMut[gammaBlockRowIdx] ? + maxMut[gammaBlockRowIdx] : + allBlockMaxSumst; + allBlockMaxSumstMinusOne = allBlockMaxSumstMinusOne < maxMutMinusOne[gammaBlockRowIdx] ? + maxMutMinusOne[gammaBlockRowIdx] : + allBlockMaxSumstMinusOne; + } + if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { + thresholds[1] = allBlockMaxSumst + 1; + return 1; + } + return 0; +} diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.h b/crypto_kem/ledakemlt12/clean/dfr_test.h new file mode 100644 index 00000000..6e20b5ce --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/dfr_test.h @@ -0,0 +1,3 @@ +#pragma once + +int DFR_test(POSITION_T LSparse[N0][DV * M]); diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/clean/gf2x_arith.c new file mode 100644 index 00000000..d502741c --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.c @@ -0,0 +1,462 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + + +#include "gf2x_arith.h" +#include // memset(...) +#include + +/*----------------------------------------------------------------------------*/ + +/* allows the second operand to be shorter than the first */ +/* the result should be as large as the first operand*/ +static inline void gf2x_add_asymm(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { + int delta = na - nb; + for (unsigned i = 0; i < delta; i++) { + Res[i] = A[i]; + } + for (unsigned i = 0; i < nb; i++) { + Res[i + delta] = A[i + delta] ^ B[i]; + } +} // end gf2x_add + +/*----------------------------------------------------------------------------*/ + +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ + +void right_bit_shift_n(const int length, DIGIT in[], const int amount) { + assert(amount < DIGIT_SIZE_b); + if ( amount == 0 ) { + return; + } + int j; + DIGIT mask; + mask = ((DIGIT)0x01 << amount) - 1; + for (j = length - 1; j > 0 ; j--) { + in[j] >>= amount; + in[j] |= (in[j - 1] & mask) << (DIGIT_SIZE_b - amount); + } + in[j] >>= amount; +} // end right_bit_shift_n + +/*----------------------------------------------------------------------------*/ + +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ +void left_bit_shift_n(const int length, DIGIT in[], const int amount) { + assert(amount < DIGIT_SIZE_b); + if ( amount == 0 ) { + return; + } + int j; + DIGIT mask; + mask = ~(((DIGIT)0x01 << (DIGIT_SIZE_b - amount)) - 1); + for (j = 0 ; j < length - 1 ; j++) { + in[j] <<= amount; + in[j] |= (in[j + 1] & mask) >> (DIGIT_SIZE_b - amount); + } + in[j] <<= amount; +} // end right_bit_shift_n + +/*----------------------------------------------------------------------------*/ + +void gf2x_mul_comb(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { + int i, j, k; + DIGIT u, h; + + memset(Res, 0x00, nr * sizeof(DIGIT)); + + for (k = DIGIT_SIZE_b - 1; k > 0; k--) { + for (i = na - 1; i >= 0; i--) + if ( A[i] & (((DIGIT)0x1) << k) ) + for (j = nb - 1; j >= 0; j--) { + Res[i + j + 1] ^= B[j]; + } + + + u = Res[na + nb - 1]; + Res[na + nb - 1] = u << 0x1; + for (j = 1; j < na + nb; ++j) { + h = u >> (DIGIT_SIZE_b - 1); + u = Res[na + nb - 1 - j]; + Res[na + nb - 1 - j] = h ^ (u << 0x1); + } + } + for (i = na - 1; i >= 0; i--) + if ( A[i] & ((DIGIT)0x1) ) + for (j = nb - 1; j >= 0; j--) { + Res[i + j + 1] ^= B[j]; + } +} + +/*----------------------------------------------------------------------------*/ + +static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { + DIGIT t = 0; + for (int i = na - 1; i >= 0; i--) { + + t ^= A[i]; + + for (int j = 1; j <= DIGIT_SIZE_b / 2; j = j * 2) { + t ^= t << (unsigned) j; + } + A[i] = t; + t >>= DIGIT_SIZE_b - 1; + } +} // end gf2x_exact_div_x_plus_one + +/*---------------------------------------------------------------------------*/ +#define MIN_KAR_DIGITS 20 + +void gf2x_mul_Kar(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { + + if (na < MIN_KAR_DIGITS || nb < MIN_KAR_DIGITS) { + /* fall back to schoolbook */ + gf2x_mul_comb(nr, Res, na, A, nb, B); + return; + } + + if (na % 2 == 0) { + unsigned bih = na / 2; + DIGIT middle[2 * bih], sumA[bih], sumB[bih]; + gf2x_add(bih, sumA, + bih, A, + bih, A + bih); + gf2x_add(bih, sumB, + bih, B, + bih, B + bih); + gf2x_mul_Kar(2 * bih, middle, + bih, sumA, + bih, sumB); + gf2x_mul_Kar(2 * bih, Res + 2 * bih, + bih, A + bih, + bih, B + bih); + gf2x_add(2 * bih, middle, + 2 * bih, middle, + 2 * bih, Res + 2 * bih); + gf2x_mul_Kar(2 * bih, Res, + bih, A, + bih, B); + gf2x_add(2 * bih, middle, + 2 * bih, middle, + 2 * bih, Res); + gf2x_add(2 * bih, Res + bih, + 2 * bih, Res + bih, + 2 * bih, middle); + } else { + unsigned bih = na / 2 + 1; + DIGIT middle[2 * bih], sumA[bih], sumB[bih]; + gf2x_add_asymm(bih, sumA, + bih, A + bih - 1, + bih - 1, A); + gf2x_add_asymm(bih, sumB, + bih, B + bih - 1, + bih - 1, B); + gf2x_mul_Kar(2 * bih, middle, + bih, sumA, + bih, sumB); + gf2x_mul_Kar(2 * bih, Res + 2 * (bih - 1), + bih, A + bih - 1, + bih, B + bih - 1); + gf2x_add(2 * bih, middle, + 2 * bih, middle, + 2 * bih, Res + 2 * (bih - 1)); + gf2x_mul_Kar(2 * (bih - 1), Res, + (bih - 1), A, + (bih - 1), B); + gf2x_add_asymm(2 * bih, middle, + 2 * bih, middle, + 2 * (bih - 1), Res); + gf2x_add(2 * bih, Res + bih - 2, + 2 * bih, Res + bih - 2, + 2 * bih, middle); + } +} + + +/*---------------------------------------------------------------------------*/ +#define MIN_TOOM_DIGITS 35 + +void gf2x_mul_TC3(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { + + if (na < MIN_TOOM_DIGITS || nb < MIN_TOOM_DIGITS) { + /* fall back to schoolbook */ + gf2x_mul_Kar(nr, Res, na, A, nb, B); + return; + } + + unsigned bih; //number of limbs for each part. + if (na % 3 == 0) { + bih = na / 3; + } else { + bih = na / 3 + 1; + } + + DIGIT u2[bih], u1[bih], u0[bih]; + + int leading_slack = (3 - (na) % 3) % 3; +// printf("leading slack %d",leading_slack); + int i; + for (i = 0; i < leading_slack ; i++) { + u2[i] = 0; + } + for (; i < bih; ++i) { + u2[i] = A[i - leading_slack]; + } + /* note: only u2 needs to be a copy, refactor */ + for (; i < 2 * bih; ++i) { + u1[i - bih] = A[i - leading_slack]; + } + for (; i < 3 * bih; ++i) { + u0[i - 2 * bih] = A[i - leading_slack]; + } + + DIGIT v2[bih], v1[bih], v0[bih]; /* partitioned inputs */ + /* note: only v2 needs to be a copy, refactor */ + for (i = 0; i < leading_slack ; i++) { + v2[i] = 0; + } + for (; i < bih; ++i) { + v2[i] = B[i - leading_slack]; + } + /* note , only v2 needs to be a copy */ + for (; i < 2 * bih; ++i) { + v1[i - bih] = B[i - leading_slack]; + } + for (; i < 3 * bih; ++i) { + v0[i - 2 * bih] = B[i - leading_slack]; + } + + DIGIT sum_u[bih]; /*bih digit wide*/ + gf2x_add(bih, sum_u, + bih, u0, + bih, u1); + gf2x_add(bih, sum_u, + bih, sum_u, + bih, u2); + + DIGIT sum_v[bih]; /*bih digit wide*/ + gf2x_add(bih, sum_v, + bih, v0, + bih, v1); + gf2x_add(bih, sum_v, + bih, sum_v, + bih, v2); + + + DIGIT w1[2 * bih]; + gf2x_mul_TC3(2 * bih, w1, + bih, sum_u, + bih, sum_v); + + + DIGIT u2_x2[bih + 1]; + u2_x2[0] = 0; + memcpy(u2_x2 + 1, u2, bih * DIGIT_SIZE_B); + left_bit_shift_n(bih + 1, u2_x2, 2); + + DIGIT u1_x[bih + 1]; + u1_x[0] = 0; + memcpy(u1_x + 1, u1, bih * DIGIT_SIZE_B); + left_bit_shift_n(bih + 1, u1_x, 1); + + DIGIT u1_x1_u2_x2[bih + 1]; + gf2x_add(bih + 1, u1_x1_u2_x2, + bih + 1, u1_x, + bih + 1, u2_x2); + + DIGIT temp_u_components[bih + 1]; + gf2x_add_asymm(bih + 1, temp_u_components, + bih + 1, u1_x1_u2_x2, + bih, sum_u); + + DIGIT v2_x2[bih + 1]; + v2_x2[0] = 0; + memcpy(v2_x2 + 1, v2, bih * DIGIT_SIZE_B); + left_bit_shift_n(bih + 1, v2_x2, 2); + + DIGIT v1_x[bih + 1]; + v1_x[0] = 0; + memcpy(v1_x + 1, v1, bih * DIGIT_SIZE_B); + left_bit_shift_n(bih + 1, v1_x, 1); + + DIGIT v1_x1_v2_x2[bih + 1]; + gf2x_add(bih + 1, v1_x1_v2_x2, + bih + 1, v1_x, + bih + 1, v2_x2); + + DIGIT temp_v_components[bih + 1]; + gf2x_add_asymm(bih + 1, temp_v_components, + bih + 1, v1_x1_v2_x2, + bih, sum_v); + + DIGIT w3[2 * bih + 2]; + gf2x_mul_TC3(2 * bih + 2, w3, + bih + 1, temp_u_components, + bih + 1, temp_v_components); + + gf2x_add_asymm(bih + 1, u1_x1_u2_x2, + bih + 1, u1_x1_u2_x2, + bih, u0); + gf2x_add_asymm(bih + 1, v1_x1_v2_x2, + bih + 1, v1_x1_v2_x2, + bih, v0); + + DIGIT w2[2 * bih + 2]; + gf2x_mul_TC3(2 * bih + 2, w2, + bih + 1, u1_x1_u2_x2, + bih + 1, v1_x1_v2_x2); + + DIGIT w4[2 * bih]; + gf2x_mul_TC3(2 * bih, w4, + bih, u2, + bih, v2); + DIGIT w0[2 * bih]; + gf2x_mul_TC3(2 * bih, w0, + bih, u0, + bih, v0); + + // Interpolation starts + gf2x_add(2 * bih + 2, w3, + 2 * bih + 2, w2, + 2 * bih + 2, w3); + gf2x_add_asymm(2 * bih + 2, w2, + 2 * bih + 2, w2, + 2 * bih, w0); + right_bit_shift_n(2 * bih + 2, w2, 1); + gf2x_add(2 * bih + 2, w2, + 2 * bih + 2, w2, + 2 * bih + 2, w3); + + // w2 + (w4 * x^3+1) = w2 + w4 + w4 << 3 + DIGIT w4_x3_plus_1[2 * bih + 1]; + w4_x3_plus_1[0] = 0; + memcpy(w4_x3_plus_1 + 1, w4, 2 * bih * DIGIT_SIZE_B); + left_bit_shift_n(2 * bih + 1, w4_x3_plus_1, 3); + gf2x_add_asymm(2 * bih + 2, w2, + 2 * bih + 2, w2, + 2 * bih, w4); + gf2x_add_asymm(2 * bih + 2, w2, + 2 * bih + 2, w2, + 2 * bih + 1, w4_x3_plus_1); + + gf2x_exact_div_x_plus_one(2 * bih + 2, w2); + + gf2x_add(2 * bih, w1, + 2 * bih, w1, + 2 * bih, w0); + gf2x_add_asymm(2 * bih + 2, w3, + 2 * bih + 2, w3, + 2 * bih, w1); + + right_bit_shift_n(2 * bih + 2, w3, 1); + gf2x_exact_div_x_plus_one(2 * bih + 2, w3); + + gf2x_add(2 * bih, w1, + 2 * bih, w1, + 2 * bih, w4); + + DIGIT w1_final[2 * bih + 2]; + gf2x_add_asymm(2 * bih + 2, w1_final, + 2 * bih + 2, w2, + 2 * bih, w1); + gf2x_add(2 * bih + 2, w2, + 2 * bih + 2, w2, + 2 * bih + 2, w3); + + // Result recombination starts here + + memset(Res, 0, nr * DIGIT_SIZE_B); + /* optimization: topmost slack digits should be computed, and not addedd, + * zeroization can be avoided altogether with a proper merge of the + * results */ + + int leastSignifDigitIdx = nr - 1; + for (int i = 0; i < 2 * bih; i++) { + Res[leastSignifDigitIdx - i] ^= w0[2 * bih - 1 - i]; + } + leastSignifDigitIdx -= bih; + for (int i = 0; i < 2 * bih + 2; i++) { + Res[leastSignifDigitIdx - i] ^= w1_final[2 * bih + 2 - 1 - i]; + } + leastSignifDigitIdx -= bih; + for (int i = 0; i < 2 * bih + 2; i++) { + Res[leastSignifDigitIdx - i] ^= w2[2 * bih + 2 - 1 - i]; + } + leastSignifDigitIdx -= bih; + for (int i = 0; i < 2 * bih + 2 ; i++) { + Res[leastSignifDigitIdx - i] ^= w3[2 * bih + 2 - 1 - i]; + } + leastSignifDigitIdx -= bih; + for (int i = 0; i < 2 * bih && (leastSignifDigitIdx - i >= 0) ; i++) { + Res[leastSignifDigitIdx - i] ^= w4[2 * bih - 1 - i]; + } +} + + +/*----------------------------------------------------------------------------*/ + +int gf2x_cmp(const unsigned lenA, const DIGIT A[], + const unsigned lenB, const DIGIT B[]) { + + int i; + unsigned lA = lenA, lB = lenB; + for (i = 0; i < lenA && A[i] == 0; i++) { + lA--; + } + for (i = 0; i < lenB && B[i] == 0; i++) { + lB--; + } + if (lA < lB) { + return -1; + } + if (lA > lB) { + return +1; + } + for (i = 0; i < lA; i++) { + if (A[i] > B[i]) { + return +1; + } + if (A[i] < B[i]) { + return -1; + } + } + return 0; + +} // end gf2x_cmp diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.h b/crypto_kem/ledakemlt12/clean/gf2x_arith.h new file mode 100644 index 00000000..fb966ad6 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.h @@ -0,0 +1,112 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once + +#include "gf2x_limbs.h" + +/*----------------------------------------------------------------------------*/ +/* + * Elements of GF(2)[x] are stored in compact dense binary form. + * + * Each bit in a byte is assumed to be the coefficient of a binary + * polynomial f(x), in Big-Endian format (i.e., reading everything from + * left to right, the most significant element is met first): + * + * byte:(0000 0000) == 0x00 ... f(x) == 0 + * byte:(0000 0001) == 0x01 ... f(x) == 1 + * byte:(0000 0010) == 0x02 ... f(x) == x + * byte:(0000 0011) == 0x03 ... f(x) == x+1 + * ... ... ... + * byte:(0000 1111) == 0x0F ... f(x) == x^{3}+x^{2}+x+1 + * ... ... ... + * byte:(1111 1111) == 0xFF ... f(x) == x^{7}+x^{6}+x^{5}+x^{4}+x^{3}+x^{2}+x+1 + * + * + * A "machine word" (A_i) is considered as a DIGIT. + * Bytes in a DIGIT are assumed in Big-Endian format: + * E.g., if sizeof(DIGIT) == 4: + * A_i: A_{i,3} A_{i,2} A_{i,1} A_{i,0}. + * A_{i,3} denotes the most significant byte, A_{i,0} the least significant one. + * f(x) == x^{31} + ... + x^{24} + + * + x^{23} + ... + x^{16} + + * + x^{15} + ... + x^{8} + + * + x^{7} + ... + x^{0} + * + * + * Multi-precision elements (i.e., with multiple DIGITs) are stored in + * Big-endian format: + * A = A_{n-1} A_{n-2} ... A_1 A_0 + * + * position[A_{n-1}] == 0 + * position[A_{n-2}] == 1 + * ... + * position[A_{1}] == n-2 + * position[A_{0}] == n-1 + */ +/*----------------------------------------------------------------------------*/ + +#define TC3 +#if defined(TC3) +#define GF2X_MUL gf2x_mul_TC3 +#else +#define GF2X_MUL gf2x_mul_comb +#endif + + + +/*----------------------------------------------------------------------------*/ + +static inline void gf2x_add(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { + for (unsigned i = 0; i < nr; i++) { + Res[i] = A[i] ^ B[i]; + } +} // end gf2x_add + +/*----------------------------------------------------------------------------*/ + +void GF2X_MUL(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[] + ); + +int gf2x_cmp(const unsigned lenA, const DIGIT A[], + const unsigned lenB, const DIGIT B[]); + +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ +void right_bit_shift_n(const int length, DIGIT in[], const int amount); + +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ +void left_bit_shift_n(const int length, DIGIT in[], const int amount); +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c new file mode 100644 index 00000000..a099ecfb --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -0,0 +1,656 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + + +#include "gf2x_arith_mod_xPplusOne.h" +#include "rng.h" +#include // memcpy(...), memset(...) +#include +#include +/*----------------------------------------------------------------------------*/ + +void gf2x_mod(DIGIT out[], + const int nin, const DIGIT in[]) { + + long int i, j, posTrailingBit, maskOffset; + DIGIT mask, aux[nin]; + + memcpy(aux, in, nin * DIGIT_SIZE_B); + memset(out, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + if (nin < NUM_DIGITS_GF2X_MODULUS) { + for (i = 0; i < nin; i++) { + out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = in[nin - 1 - i]; + } + return; + } + + for (i = 0; i < nin - NUM_DIGITS_GF2X_MODULUS; i += 1) { + for (j = DIGIT_SIZE_b - 1; j >= 0; j--) { + mask = ((DIGIT)0x1) << j; + if (aux[i] & mask) { + aux[i] ^= mask; + posTrailingBit = (DIGIT_SIZE_b - 1 - j) + i * DIGIT_SIZE_b + P; + maskOffset = (DIGIT_SIZE_b - 1 - (posTrailingBit % DIGIT_SIZE_b)); + mask = (DIGIT) 0x1 << maskOffset; + aux[posTrailingBit / DIGIT_SIZE_b] ^= mask; + } + } + } + + for (j = DIGIT_SIZE_b - 1; j >= MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS; j--) { + mask = ((DIGIT)0x1) << j; + if (aux[i] & mask) { + aux[i] ^= mask; + posTrailingBit = (DIGIT_SIZE_b - 1 - j) + i * DIGIT_SIZE_b + P; + maskOffset = (DIGIT_SIZE_b - 1 - (posTrailingBit % DIGIT_SIZE_b)); + mask = (DIGIT) 0x1 << maskOffset; + aux[posTrailingBit / DIGIT_SIZE_b] ^= mask; + } + } + + int to_copy = (nin > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : nin; + + for (i = 0; i < to_copy; i++) { + out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[nin - 1 - i]; + } + +} // end gf2x_mod + +/*----------------------------------------------------------------------------*/ + +static +void left_bit_shift(const int length, DIGIT in[]) { + + int j; + for (j = 0; j < length - 1; j++) { + in[j] <<= 1; /* logical shift does not need clearing */ + in[j] |= in[j + 1] >> (DIGIT_SIZE_b - 1); + } + in[j] <<= 1; +} // end left_bit_shift + +/*----------------------------------------------------------------------------*/ + +static +void right_bit_shift(const int length, DIGIT in[]) { + + int j; + for (j = length - 1; j > 0 ; j--) { + in[j] >>= 1; + in[j] |= (in[j - 1] & (DIGIT)0x01) << (DIGIT_SIZE_b - 1); + } + in[j] >>= 1; +} // end right_bit_shift + +/*----------------------------------------------------------------------------*/ +/* shifts by whole digits */ +static inline +void left_DIGIT_shift_n(const int length, DIGIT in[], int amount) { + int j; + for (j = 0; (j + amount) < length; j++) { + in[j] = in[j + amount]; + } + for (; j < length; j++) { + in[j] = (DIGIT)0; + } +} // end left_bit_shift_n + +/*----------------------------------------------------------------------------*/ +/* may shift by an arbitrary amount*/ + +void left_bit_shift_wide_n(const int length, DIGIT in[], int amount) { + left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); + left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); +} // end left_bit_shift_n + +/*----------------------------------------------------------------------------*/ + +#if (defined(DIGIT_IS_UINT8) || defined(DIGIT_IS_UINT16)) +static +uint8_t byte_reverse_with_less32bitDIGIT(uint8_t b) { + uint8_t r = b; + int s = (sizeof(b) << 3) - 1; + for (b >>= 1; b; b >>= 1) { + r <<= 1; + r |= b & 1; + s--; + } + r <<= s; + return r; +} // end byte_reverse_less32bitDIGIT +#endif + +#if defined(DIGIT_IS_UINT32) +static +uint8_t byte_reverse_with_32bitDIGIT(uint8_t b) { + b = ( (b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU) + ) * 0x10101LU >> 16; + return b; +} // end byte_reverse_32bitDIGIT +#endif + +#if defined(DIGIT_IS_UINT64) +static +uint8_t byte_reverse_with_64bitDIGIT(uint8_t b) { + b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023; + return b; +} // end byte_reverse_64bitDIGIT +#endif + +/*----------------------------------------------------------------------------*/ + +static +DIGIT reverse_digit(const DIGIT b) { + int i; + union toReverse_t { + uint8_t inByte[DIGIT_SIZE_B]; + DIGIT digitValue; + } toReverse; + + toReverse.digitValue = b; + #if defined(DIGIT_IS_UINT64) + for (i = 0; i < DIGIT_SIZE_B; i++) { + toReverse.inByte[i] = byte_reverse_with_64bitDIGIT(toReverse.inByte[i]); + } + return __builtin_bswap64(toReverse.digitValue); + #elif defined(DIGIT_IS_UINT32) + for (i = 0; i < DIGIT_SIZE_B; i++) { + toReverse.inByte[i] = byte_reverse_with_32bitDIGIT(toReverse.inByte[i]); + } + return __builtin_bswap32(toReverse.digitValue); + #elif defined(DIGIT_IS_UINT16) + for (i = 0; i < DIGIT_SIZE_B; i++) { + toReverse.inByte[i] = byte_reverse_with_less32bitDIGIT(toReverse.inByte[i]); + } + reversed = __builtin_bswap16(toReverse.digitValue); + #elif defined(DIGIT_IS_UINT8) + return byte_reverse_with_less32bitDIGIT(toReverse.inByte[0]); + #else +#error "Missing implementation for reverse_digit(...) \ +with this CPU word bitsize !!! " + #endif + return toReverse.digitValue; +} // end reverse_digit + + +/*----------------------------------------------------------------------------*/ + +void gf2x_transpose_in_place(DIGIT A[]) { + /* it keeps the lsb in the same position and + * inverts the sequence of the remaining bits + */ + + DIGIT mask = (DIGIT)0x1; + DIGIT rev1, rev2, a00; + int i, slack_bits_amount = NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - P; + + if (NUM_DIGITS_GF2X_ELEMENT == 1) { + a00 = A[0] & mask; + right_bit_shift(1, A); + rev1 = reverse_digit(A[0]); + #if (NUM_DIGITS_GF2X_MOD_P_ELEMENT*DIGIT_SIZE_b - P) + rev1 >>= (DIGIT_SIZE_b - (P % DIGIT_SIZE_b)); + #endif + A[0] = (rev1 & (~mask)) | a00; + return; + } + + a00 = A[NUM_DIGITS_GF2X_ELEMENT - 1] & mask; + right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, A); + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= (NUM_DIGITS_GF2X_ELEMENT + 1) / 2; i--) { + rev1 = reverse_digit(A[i]); + rev2 = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT - 1 - i]); + A[i] = rev2; + A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; + } + if (NUM_DIGITS_GF2X_ELEMENT % 2 == 1) { + A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); + } + + if (slack_bits_amount) { + right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); + } + A[NUM_DIGITS_GF2X_ELEMENT - 1] = (A[NUM_DIGITS_GF2X_ELEMENT - 1] & (~mask)) | a00; +} // end transpose_in_place + +/*----------------------------------------------------------------------------*/ + +void rotate_bit_left(DIGIT in[]) { /* equivalent to x * in(x) mod x^P+1 */ + + DIGIT mask, rotated_bit; + + if (NUM_DIGITS_GF2X_MODULUS == NUM_DIGITS_GF2X_ELEMENT) { + + int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; + mask = ((DIGIT)0x1) << msb_offset_in_digit; + rotated_bit = !!(in[0] & mask); + in[0] &= ~mask; /* clear shifted bit */ + left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); + } else { + /* NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT and + * MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS == 0 + */ + mask = ((DIGIT)0x1) << (DIGIT_SIZE_b - 1); + rotated_bit = !!(in[0] & mask); + in[0] &= ~mask; /* clear shifted bit */ + left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); + + } + in[NUM_DIGITS_GF2X_ELEMENT - 1] |= rotated_bit; +} // end rotate_bit_left + + + +/*----------------------------------------------------------------------------*/ + +void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ + + DIGIT rotated_bit = in[NUM_DIGITS_GF2X_ELEMENT - 1] & ((DIGIT)0x1); + right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); + + if (NUM_DIGITS_GF2X_MODULUS == NUM_DIGITS_GF2X_ELEMENT) { + int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; + rotated_bit = rotated_bit << msb_offset_in_digit; + } else { + /* NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT and + * MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS == 0 + */ + rotated_bit = rotated_bit << (DIGIT_SIZE_b - 1); + } + in[0] |= rotated_bit; +} // end rotate_bit_right + +/*----------------------------------------------------------------------------*/ + +static +void gf2x_swap(const int length, + DIGIT f[], + DIGIT s[]) { + DIGIT t; + for (int i = length - 1; i >= 0; i--) { + t = f[i]; + f[i] = s[i]; + s[i] = t; + } +} // end gf2x_swap + +/*----------------------------------------------------------------------------*/ + +/* + * Optimized extended GCD algorithm to compute the multiplicative inverse of + * a non-zero element in GF(2)[x] mod x^P+1, in polyn. representation. + * + * H. Brunner, A. Curiger, and M. Hofstetter. 1993. + * On Computing Multiplicative Inverses in GF(2^m). + * IEEE Trans. Comput. 42, 8 (August 1993), 1010-1015. + * DOI=http://dx.doi.org/10.1109/12.238496 + * + * + * Henri Cohen, Gerhard Frey, Roberto Avanzi, Christophe Doche, Tanja Lange, + * Kim Nguyen, and Frederik Vercauteren. 2012. + * Handbook of Elliptic and Hyperelliptic Curve Cryptography, + * Second Edition (2nd ed.). Chapman & Hall/CRC. + * (Chapter 11 -- Algorithm 11.44 -- pag 223) + * + */ + +int gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ + + int i; + long int delta = 0; + alignas(32) DIGIT u[NUM_DIGITS_GF2X_ELEMENT] = {0}, + v[NUM_DIGITS_GF2X_ELEMENT] = {0}, + s[NUM_DIGITS_GF2X_MODULUS] = {0}, + f[NUM_DIGITS_GF2X_MODULUS] = {0}; + + DIGIT mask; + u[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x1; + v[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x0; + + s[NUM_DIGITS_GF2X_MODULUS - 1] = 0x1; + if (MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS == 0) { + mask = 0x1; + } else { + mask = (((DIGIT)0x1) << MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS); + } + s[0] |= mask; + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 && in[i] == 0; i--); + if (i < 0) { + return 0; + } + + if (NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT) + for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 1 ; i--) { + f[i] = in[i - 1]; + } else /* they are equal */ + for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 0 ; i--) { + f[i] = in[i]; + } + + for (i = 1; i <= 2 * P; i++) { + if ( (f[0] & mask) == 0 ) { + left_bit_shift(NUM_DIGITS_GF2X_MODULUS, f); + rotate_bit_left(u); + delta += 1; + } else { + if ( (s[0] & mask) != 0) { + gf2x_add(NUM_DIGITS_GF2X_MODULUS, s, + NUM_DIGITS_GF2X_MODULUS, s, + NUM_DIGITS_GF2X_MODULUS, f); + gf2x_mod_add(v, v, u); + } + left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); + if ( delta == 0 ) { + gf2x_swap(NUM_DIGITS_GF2X_MODULUS, f, s); + gf2x_swap(NUM_DIGITS_GF2X_ELEMENT, u, v); + rotate_bit_left(u); + delta = 1; + } else { + rotate_bit_right(u); + delta = delta - 1; + } + } + } + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 ; i--) { + out[i] = u[i]; + } + + return (delta == 0); +} // end gf2x_mod_inverse + +/*----------------------------------------------------------------------------*/ + +void gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + + DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT]; + GF2X_MUL(2 * NUM_DIGITS_GF2X_ELEMENT, aux, + NUM_DIGITS_GF2X_ELEMENT, A, + NUM_DIGITS_GF2X_ELEMENT, B); + gf2x_mod(Res, 2 * NUM_DIGITS_GF2X_ELEMENT, aux); + +} // end gf2x_mod_mul + +/*----------------------------------------------------------------------------*/ + +/*PRE: the representation of the sparse coefficients is sorted in increasing + order of the coefficients themselves */ +void gf2x_mod_mul_dense_to_sparse(DIGIT Res[], + const DIGIT dense[], + POSITION_T sparse[], + unsigned int nPos) { + DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + DIGIT resDouble[2 * NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + memcpy(aux + NUM_DIGITS_GF2X_ELEMENT, dense, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memcpy(resDouble + NUM_DIGITS_GF2X_ELEMENT, dense, + NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + if (sparse[0] != INVALID_POS_VALUE) { + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, resDouble, sparse[0]); + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, sparse[0]); + + for (unsigned int i = 1; i < nPos; i++) { + if (sparse[i] != INVALID_POS_VALUE) { + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); + gf2x_add(2 * NUM_DIGITS_GF2X_ELEMENT, resDouble, + 2 * NUM_DIGITS_GF2X_ELEMENT, aux, + 2 * NUM_DIGITS_GF2X_ELEMENT, resDouble); + } + } + } + + gf2x_mod(Res, 2 * NUM_DIGITS_GF2X_ELEMENT, resDouble); + +} // end gf2x_mod_mul + +/*----------------------------------------------------------------------------*/ + + +void gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { + + POSITION_T t; + int i = 0, j; + + if (A[i] == 0) { + i = 1; + } + j = i; + + for (; i < sizeA && A[i] != INVALID_POS_VALUE; i++) { + A[i] = P - A[i]; + } + + for (i -= 1; j < i; j++, i--) { + t = A[j]; + A[j] = A[i]; + A[i] = t; + } + +} // end gf2x_transpose_in_place_sparse + +/*----------------------------------------------------------------------------*/ + +void gf2x_mod_mul_sparse(int + sizeR, /*number of ones in the result, max sizeA*sizeB */ + POSITION_T Res[], + int sizeA, /*number of ones in A*/ + const POSITION_T A[], + int sizeB, /*number of ones in B*/ + const POSITION_T B[]) { + /* compute all the coefficients, filling invalid positions with P*/ + unsigned lastFilledPos = 0; + for (int i = 0 ; i < sizeA ; i++) { + for (int j = 0 ; j < sizeB ; j++) { + uint32_t prod = ((uint32_t) A[i]) + ((uint32_t) B[j]); + prod = ( (prod >= P) ? prod - P : prod); + if ((A[i] != INVALID_POS_VALUE) && + (B[j] != INVALID_POS_VALUE)) { + Res[lastFilledPos] = prod; + } else { + Res[lastFilledPos] = INVALID_POS_VALUE; + } + lastFilledPos++; + } + } + while (lastFilledPos < sizeR) { + Res[lastFilledPos] = INVALID_POS_VALUE; + lastFilledPos++; + } + quicksort(Res, sizeR); + /* eliminate duplicates */ + POSITION_T lastReadPos = Res[0]; + int duplicateCount; + int write_idx = 0; + int read_idx = 0; + while (read_idx < sizeR && Res[read_idx] != INVALID_POS_VALUE) { + lastReadPos = Res[read_idx]; + read_idx++; + duplicateCount = 1; + while ( (Res[read_idx] == lastReadPos) && (Res[read_idx] != INVALID_POS_VALUE)) { + read_idx++; + duplicateCount++; + } + if (duplicateCount % 2) { + Res[write_idx] = lastReadPos; + write_idx++; + } + } + /* fill remaining cells with INVALID_POS_VALUE */ + for (; write_idx < sizeR; write_idx++) { + Res[write_idx] = INVALID_POS_VALUE; + } +} // end gf2x_mod_mul_sparse + + +/*----------------------------------------------------------------------------*/ +/* the implementation is safe even in case A or B alias with the result */ +/* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ +void gf2x_mod_add_sparse(int sizeR, + POSITION_T Res[], + int sizeA, + POSITION_T A[], + int sizeB, + POSITION_T B[]) { + + POSITION_T tmpRes[sizeR]; + int idxA = 0, idxB = 0, idxR = 0; + while ( idxA < sizeA && + idxB < sizeB && + A[idxA] != INVALID_POS_VALUE && + B[idxB] != INVALID_POS_VALUE ) { + + if (A[idxA] == B[idxB]) { + idxA++; + idxB++; + } else { + if (A[idxA] < B[idxB]) { + tmpRes[idxR] = A[idxA]; + idxA++; + } else { + tmpRes[idxR] = B[idxB]; + idxB++; + } + idxR++; + } + } + + while (idxA < sizeA && A[idxA] != INVALID_POS_VALUE) { + tmpRes[idxR] = A[idxA]; + idxA++; + idxR++; + } + + while (idxB < sizeB && B[idxB] != INVALID_POS_VALUE) { + tmpRes[idxR] = B[idxB]; + idxB++; + idxR++; + } + + while (idxR < sizeR) { + tmpRes[idxR] = INVALID_POS_VALUE; + idxR++; + } + memcpy(Res, tmpRes, sizeof(POSITION_T)*sizeR); + +} // end gf2x_mod_add_sparse + +/*----------------------------------------------------------------------------*/ + +/* Return a uniform random value in the range 0..n-1 inclusive, + * applying a rejection sampling strategy and exploiting as a random source + * the NIST seedexpander seeded with the proper key. + * Assumes that the maximum value for the range n is 2^32-1 + */ +static +int rand_range(const int n, const int logn, AES_XOF_struct *seed_expander_ctx) { + + unsigned long required_rnd_bytes = (logn + 7) / 8; + unsigned char rnd_char_buffer[4]; + uint32_t rnd_value; + uint32_t mask = ( (uint32_t)1 << logn) - 1; + + do { + seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); + /* obtain an endianness independent representation of the generated random + bytes into an unsigned integer */ + rnd_value = ((uint32_t)rnd_char_buffer[3] << 24) + + ((uint32_t)rnd_char_buffer[2] << 16) + + ((uint32_t)rnd_char_buffer[1] << 8) + + ((uint32_t)rnd_char_buffer[0] << 0) ; + rnd_value = mask & rnd_value; + } while (rnd_value >= n); + + return rnd_value; +} // end rand_range + + + +/*----------------------------------------------------------------------------*/ +/* Obtains fresh randomness and seed-expands it until all the required positions + * for the '1's in the circulant block are obtained */ + +void rand_circulant_sparse_block(POSITION_T *pos_ones, + const int countOnes, + AES_XOF_struct *seed_expander_ctx) { + + int duplicated, placedOnes = 0; + + while (placedOnes < countOnes) { + int p = rand_range(NUM_BITS_GF2X_ELEMENT, + BITS_TO_REPRESENT(P), + seed_expander_ctx); + duplicated = 0; + for (int j = 0; j < placedOnes; j++) if (pos_ones[j] == p) { + duplicated = 1; + } + if (duplicated == 0) { + pos_ones[placedOnes] = p; + placedOnes++; + } + } +} // rand_circulant_sparse_block + +/*----------------------------------------------------------------------------*/ + + +void rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + const int countOnes, + AES_XOF_struct *seed_expander_ctx) { + + int rndPos[countOnes], duplicated, counter = 0; + memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + + while (counter < countOnes) { + int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, BITS_TO_REPRESENT(P), + seed_expander_ctx); + duplicated = 0; + for (int j = 0; j < counter; j++) if (rndPos[j] == p) { + duplicated = 1; + } + if (duplicated == 0) { + rndPos[counter] = p; + counter++; + } + } + for (int j = 0; j < counter; j++) { + int polyIndex = rndPos[j] / P; + int exponent = rndPos[j] % P; + gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + ( (DIGIT) 1)); + } + +} // end rand_circulant_blocks_sequence + +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h new file mode 100644 index 00000000..ce3e2e86 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h @@ -0,0 +1,300 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once + +#include "gf2x_limbs.h" +#include "qc_ldpc_parameters.h" + +#include "gf2x_arith.h" +#include "rng.h" + +/*----------------------------------------------------------------------------*/ + +#define NUM_BITS_GF2X_ELEMENT (P) +#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ( (P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1 ) + +#define NUM_BITS_GF2X_MODULUS (P+1) +#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) + +#define INVALID_POS_VALUE (P) + +#define IS_REPRESENTABLE_IN_D_BITS(D, N) \ + (((unsigned long) N >= (1UL << (D - 1)) && (unsigned long) N < (1UL << D)) ? D : -1) + +#define BITS_TO_REPRESENT(N) \ + (N == 0 ? 1 : (31 \ + + IS_REPRESENTABLE_IN_D_BITS( 1, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 2, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 3, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 4, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 5, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 6, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 7, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 8, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 9, N) \ + + IS_REPRESENTABLE_IN_D_BITS(10, N) \ + + IS_REPRESENTABLE_IN_D_BITS(11, N) \ + + IS_REPRESENTABLE_IN_D_BITS(12, N) \ + + IS_REPRESENTABLE_IN_D_BITS(13, N) \ + + IS_REPRESENTABLE_IN_D_BITS(14, N) \ + + IS_REPRESENTABLE_IN_D_BITS(15, N) \ + + IS_REPRESENTABLE_IN_D_BITS(16, N) \ + + IS_REPRESENTABLE_IN_D_BITS(17, N) \ + + IS_REPRESENTABLE_IN_D_BITS(18, N) \ + + IS_REPRESENTABLE_IN_D_BITS(19, N) \ + + IS_REPRESENTABLE_IN_D_BITS(20, N) \ + + IS_REPRESENTABLE_IN_D_BITS(21, N) \ + + IS_REPRESENTABLE_IN_D_BITS(22, N) \ + + IS_REPRESENTABLE_IN_D_BITS(23, N) \ + + IS_REPRESENTABLE_IN_D_BITS(24, N) \ + + IS_REPRESENTABLE_IN_D_BITS(25, N) \ + + IS_REPRESENTABLE_IN_D_BITS(26, N) \ + + IS_REPRESENTABLE_IN_D_BITS(27, N) \ + + IS_REPRESENTABLE_IN_D_BITS(28, N) \ + + IS_REPRESENTABLE_IN_D_BITS(29, N) \ + + IS_REPRESENTABLE_IN_D_BITS(30, N) \ + + IS_REPRESENTABLE_IN_D_BITS(31, N) \ + + IS_REPRESENTABLE_IN_D_BITS(32, N) \ + ) \ + ) + +/*----------------------------------------------------------------------------*/ + + + +/*----------------------------------------------------------------------------*/ + +static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + dest[i] = in[i]; + } +} // end gf2x_copy + +/*---------------------------------------------------------------------------*/ + +void gf2x_mod(DIGIT out[], + const int nin, const DIGIT in[]); /* out(x) = in(x) mod x^P+1 */ + +/*---------------------------------------------------------------------------*/ + +void gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); + +/*---------------------------------------------------------------------------*/ + +static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + gf2x_add(NUM_DIGITS_GF2X_ELEMENT, Res, + NUM_DIGITS_GF2X_ELEMENT, A, + NUM_DIGITS_GF2X_ELEMENT, B); +} // end gf2x_mod_add + +/*----------------------------------------------------------------------------*/ + +/* + * Optimized extended GCD algorithm to compute the multiplicative inverse of + * a non-zero element in GF(2)[x] mod x^P+1, in polyn. representation. + * + * H. Brunner, A. Curiger, and M. Hofstetter. 1993. + * On Computing Multiplicative Inverses in GF(2^m). + * IEEE Trans. Comput. 42, 8 (August 1993), 1010-1015. + * DOI=http://dx.doi.org/10.1109/12.238496 + * + * + * Henri Cohen, Gerhard Frey, Roberto Avanzi, Christophe Doche, Tanja Lange, + * Kim Nguyen, and Frederik Vercauteren. 2012. + * Handbook of Elliptic and Hyperelliptic Curve Cryptography, + * Second Edition (2nd ed.). Chapman & Hall/CRC. + * (Chapter 11 -- Algorithm 11.44 -- pag 223) + * + */ +int gf2x_mod_inverse(DIGIT out[], const DIGIT in[]);/* ret. 1 if inv. exists */ + +/*---------------------------------------------------------------------------*/ + +void gf2x_transpose_in_place(DIGIT + A[]); /* in place bit-transp. of a(x) % x^P+1 * + * e.g.: a3 a2 a1 a0 --> a1 a2 a3 a0 */ + +/*---------------------------------------------------------------------------*/ + +/* population count for a single polynomial */ +static inline int population_count(DIGIT upc[]) { + int ret = 0; + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + #if defined(DIGIT_IS_ULLONG) + ret += __builtin_popcountll((unsigned long long int) (upc[i])); + #elif defined(DIGIT_IS_ULONG) + ret += __builtin_popcountl((unsigned long int) (upc[i])); + #elif defined(DIGIT_IS_UINT) + ret += __builtin_popcount((unsigned int) (upc[i])); + #elif defined(DIGIT_IS_UCHAR) + const unsigned char split_lookup[] = { + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 + }; + ret += split_lookup[upc[i] & 0xF] + split_lookup[upc[i] >> 4]; + #else +#error "Missing implementation for population_count(...) \ +with this CPU word bitsize !!! " + #endif + } + return ret; +} // end population_count + +/*--------------------------------------------------------------------------*/ + +/* returns the coefficient of the x^exponent term as the LSB of a digit */ +static inline +DIGIT gf2x_get_coeff(const DIGIT poly[], const unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; +} + +/*--------------------------------------------------------------------------*/ + +/* sets the coefficient of the x^exponent term as the LSB of a digit */ +static inline +void gf2x_set_coeff(DIGIT poly[], const unsigned int exponent, DIGIT value) { + int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + int digitIdx = straightIdx / DIGIT_SIZE_b; + + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] & mask; + poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << + (DIGIT_SIZE_b - 1 - inDigitIdx)); +} +/*--------------------------------------------------------------------------*/ + +/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ +static inline +void gf2x_toggle_coeff(DIGIT poly[], const unsigned int exponent) { + + int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] ^ mask; +} +/*--------------------------------------------------------------------------*/ + +void rand_circulant_sparse_block(POSITION_T *pos_ones, + const int countOnes, + AES_XOF_struct *seed_expander_ctx); +/*--------------------------------------------------------------------------*/ + +void rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + const int countOnes, + AES_XOF_struct *seed_expander_ctx + ); + +/*---------------------------------------------------------------------------*/ + + +void gf2x_mod_add_sparse(int sizeR, + POSITION_T Res[], + int sizeA, + POSITION_T A[], + int sizeB, + POSITION_T B[]); + +/*----------------------------------------------------------------------------*/ + +void gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); + +/*----------------------------------------------------------------------------*/ + +void gf2x_mod_mul_sparse(int + sizeR, /*number of ones in the result, max sizeA*sizeB */ + POSITION_T Res[], + int sizeA, /*number of ones in A*/ + const POSITION_T A[], + int sizeB, /*number of ones in B*/ + const POSITION_T B[]); +/*----------------------------------------------------------------------------*/ +void gf2x_mod_mul_dense_to_sparse(DIGIT Res[], + const DIGIT dense[], + POSITION_T sparse[], + unsigned int nPos); +/*----------------------------------------------------------------------------*/ +static inline +int partition (POSITION_T arr[], int lo, int hi) { + POSITION_T x = arr[hi]; + POSITION_T tmp; + int i = (lo - 1); + for (int j = lo; j <= hi - 1; j++) { + if (arr[j] <= x) { + i++; + tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + } + tmp = arr[i + 1]; + arr[i + 1] = arr[hi]; + arr[hi] = tmp; + + return i + 1; +} // end partition + +/*----------------------------------------------------------------------------*/ + +static inline +void quicksort(POSITION_T Res[], unsigned int sizeR) { + /* sort the result */ + int stack[sizeR]; + int hi, lo, pivot, tos = -1; + stack[++tos] = 0; + stack[++tos] = sizeR - 1; + while (tos >= 0 ) { + hi = stack[tos--]; + lo = stack[tos--]; + pivot = partition(Res, lo, hi); + if ( (pivot - 1) > lo) { + stack[++tos] = lo; + stack[++tos] = pivot - 1; + } + if ( (pivot + 1) < hi) { + stack[++tos] = pivot + 1; + stack[++tos] = hi; + } + } +} + +/*---------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h new file mode 100644 index 00000000..6ced9310 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h @@ -0,0 +1,94 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once + +/*----------------------------------------------------------------------------*/ + +#include +#include +#include +#include "qc_ldpc_parameters.h" + +/*----------------------------------------------------------------------------*/ + +#define LITTLE_ENDIAN + +/*----------------------------------------------------------------------------*/ +/* limb size definitions for the multi-precision GF(2^x) library */ +/*----------------------------------------------------------------------------*/ + +#ifndef CPU_WORD_BITS +typedef size_t DIGIT; +#define DIGIT_MAX SIZE_MAX +#else +// gcc -DCPU_WORD_BITS=64 ... +#define CAT(a, b, c) PRIMITIVE_CAT(a, b, c) +#define PRIMITIVE_CAT(a, b, c) a ## b ## c + +typedef CAT( uint, CPU_WORD_BITS, _t ) DIGIT; +#define DIGIT_MAX (CAT(UINT, CPU_WORD_BITS, _MAX)) +#endif + +#if (DIGIT_MAX == ULLONG_MAX) +#define DIGIT_IS_ULLONG +#elif (DIGIT_MAX == ULONG_MAX) +#define DIGIT_IS_ULONG +#elif (DIGIT_MAX == UINT_MAX) +#define DIGIT_IS_UINT +#elif (DIGIT_MAX == UCHAR_MAX) +#define DIGIT_IS_UCHAR +#else +#error "unable to find the type of CPU_WORD_BITS" +#endif + +#if (DIGIT_MAX == UINT64_MAX) +#define DIGIT_IS_UINT64 +#define DIGIT_SIZE_B 8 +#elif (DIGIT_MAX == UINT32_MAX) +#define DIGIT_IS_UINT32 +#define DIGIT_SIZE_B 4 +#elif (DIGIT_MAX == UINT16_MAX) +#define DIGIT_IS_UINT16 +#define DIGIT_SIZE_B 2 +#elif (DIGIT_MAX == UINT8_MAX) +#define DIGIT_IS_UINT8 +#define DIGIT_SIZE_B 1 +#else +#error "unable to find the bitsize of size_t" +#endif + +#define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) + +#define POSITION_T uint32_t +/*----------------------------------------------------------------------------*/ + diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt12/clean/kem.c new file mode 100644 index 00000000..28b55695 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/kem.c @@ -0,0 +1,97 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#include "niederreiter_keygen.h" +#include "niederreiter_encrypt.h" +#include "niederreiter_decrypt.h" +#include "rng.h" +#include "sha3.h" +#include +/* Generates a keypair - pk is the public key and sk is the secret key. */ +int crypto_kem_keypair( unsigned char *pk, + unsigned char *sk ) { + + AES_XOF_struct niederreiter_keys_expander; + randombytes( ((privateKeyNiederreiter_t *)sk)->prng_seed, + TRNG_BYTE_LENGTH); + seedexpander_from_trng(&niederreiter_keys_expander, + ((privateKeyNiederreiter_t *)sk)->prng_seed); + key_gen_niederreiter((publicKeyNiederreiter_t *) pk, + (privateKeyNiederreiter_t *) sk, + &niederreiter_keys_expander); + return 0; +} + +/* Encrypt - pk is the public key, ct is a key encapsulation message + (ciphertext), ss is the shared secret.*/ +int crypto_kem_enc( unsigned char *ct, + unsigned char *ss, + const unsigned char *pk ) { + + AES_XOF_struct niederreiter_encap_key_expander; + unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; + randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); + seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); + + DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + rand_circulant_blocks_sequence(error_vector, + NUM_ERRORS_T, + &niederreiter_encap_key_expander); + + HASH_FUNCTION((const unsigned char *) error_vector, // input + (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), // input Length + ss); + + encrypt_niederreiter((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); + return 0; +} + + +/* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private + key, ss is the shared secret */ + +int crypto_kem_dec( unsigned char *ss, + const unsigned char *ct, + const unsigned char *sk ) { + DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + + int decode_ok = decrypt_niederreiter(decoded_error_vector, + (privateKeyNiederreiter_t *)sk, + (DIGIT *)ct); + HASH_FUNCTION((const unsigned char *) decoded_error_vector, + (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), + ss); + if (decode_ok == 1) { + return 0; + } + return 1; +} diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.h b/crypto_kem/ledakemlt12/clean/niederreiter.h new file mode 100644 index 00000000..34c05e73 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/niederreiter.h @@ -0,0 +1,58 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once +#include "qc_ldpc_parameters.h" +#include "gf2x_limbs.h" +#include "gf2x_arith_mod_xPplusOne.h" + + +/*----------------------------------------------------------------------------*/ +#pragma pack(1) + +typedef struct { + /* raw entropy extracted from TRNG, will be deterministically expanded into + * H and Q during decryption */ + unsigned char prng_seed[TRNG_BYTE_LENGTH]; + int8_t rejections; +} privateKeyNiederreiter_t; + +typedef struct { + DIGIT Mtr[(N0 - 1)*NUM_DIGITS_GF2X_ELEMENT]; + // Dense representation of the matrix M=Ln0*L, + // An array including a sequence of (N0-1) gf2x elements; + // each gf2x element is stored as a binary polynomial(mod x^P+1) + // with P coefficients. +} publicKeyNiederreiter_t; + +#pragma pack() +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c new file mode 100644 index 00000000..13023318 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c @@ -0,0 +1,153 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#include "niederreiter_decrypt.h" + +#include "qc_ldpc_parameters.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "H_Q_matrices_generation.h" + +#include "bf_decoding.h" +#include "dfr_test.h" +#include + +/*----------------------------------------------------------------------------*/ + +int decrypt_niederreiter(DIGIT err[], // N0 circ poly + const privateKeyNiederreiter_t *const sk, + const DIGIT syndrome[] // 1 circ poly + ) { + AES_XOF_struct niederreiter_decrypt_expander; + seedexpander_from_trng(&niederreiter_decrypt_expander, + sk->prng_seed); + + /**************************************************************************/ + // sequence of N0 circ block matrices (p x p): + POSITION_T HPosOnes[N0][DV]; + POSITION_T HtrPosOnes[N0][DV]; + POSITION_T QPosOnes[N0][M]; + int rejections = sk->rejections; + POSITION_T LPosOnes[N0][DV * M]; + do { + generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, + &niederreiter_decrypt_expander); + generateQsparse(QPosOnes, &niederreiter_decrypt_expander); + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + LPosOnes[i][j] = INVALID_POS_VALUE; + } + } + + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0] = {0}; + for (int colQ = 0; colQ < N0; colQ++) { + for (int i = 0; i < N0; i++) { + gf2x_mod_mul_sparse(DV * M, auxPosOnes, + DV, HPosOnes[i], + qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); + gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + DV * M, LPosOnes[colQ], + DV * M, auxPosOnes); + processedQOnes[i] += qBlockWeights[i][colQ]; + } + } + rejections--; + } while (rejections >= 0); + + POSITION_T QtrPosOnes[N0][M]; + unsigned transposed_ones_idx[N0] = {0x00}; + for (unsigned source_row_idx = 0; source_row_idx < N0 ; source_row_idx++) { + int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + int endQblockIdx = 0; + for (int blockIdx = 0; blockIdx < N0; blockIdx++) { + endQblockIdx += qBlockWeights[source_row_idx][blockIdx]; + for (; currQoneIdx < endQblockIdx; currQoneIdx++) { + QtrPosOnes[blockIdx][transposed_ones_idx[blockIdx]] = (P - + QPosOnes[source_row_idx][currQoneIdx]) % P; + transposed_ones_idx[blockIdx]++; + } + } + } + + POSITION_T auxSparse[DV * M]; + POSITION_T Ln0trSparse[DV * M]; + for (int i = 0; i < DV * M; i++) { + Ln0trSparse[i] = INVALID_POS_VALUE; + auxSparse[i] = INVALID_POS_VALUE; + } + + for (int i = 0; i < N0; i++) { + gf2x_mod_mul_sparse(DV * M, auxSparse, + DV, HPosOnes[i], + qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1] ] + ); + gf2x_mod_add_sparse(DV * M, Ln0trSparse, + DV * M, Ln0trSparse, + DV * M, auxSparse + ); + } // end for i + gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); + + DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + gf2x_mod_mul_dense_to_sparse(privateSyndrome, + syndrome, + Ln0trSparse, + DV * M); + + /* prepare mockup error vector in case a decoding failure occurs */ + DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + seedexpander(&niederreiter_decrypt_expander, + ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), TRNG_BYTE_LENGTH); + + int decryptOk = 0; + memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + decryptOk = bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, + (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); + + int err_weight = 0; + for (int i = 0 ; i < N0; i++) { + err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + } + decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); + + if (!decryptOk) { + memcpy(err, mockup_error_vector, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + } + + return decryptOk; +} // end decrypt_niederreiter + +/*----------------------------------------------------------------------------*/ + + diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h new file mode 100644 index 00000000..7ac486e0 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h @@ -0,0 +1,45 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once + +#include "niederreiter.h" +#include "gf2x_limbs.h" + +/*----------------------------------------------------------------------------*/ + +int decrypt_niederreiter(DIGIT err[], // return 1 if everything is ok + const privateKeyNiederreiter_t *const sk, + const DIGIT syndrome[] + ); + +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c new file mode 100644 index 00000000..a4ed2b4a --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c @@ -0,0 +1,57 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#include "niederreiter_encrypt.h" +#include "qc_ldpc_parameters.h" +#include "gf2x_arith_mod_xPplusOne.h" + +#include // memset(...) + +void encrypt_niederreiter(DIGIT syndrome[], // 1 polynomial + const publicKeyNiederreiter_t *const pk, + const DIGIT err[]) { // N0 polynomials + int i; + DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; + + memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + for (i = 0; i < N0 - 1; i++) { + gf2x_mod_mul(saux, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + err + i * NUM_DIGITS_GF2X_ELEMENT + ); + gf2x_mod_add(syndrome, syndrome, saux); + } // end for + gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); +} // end encrypt_niederreiter + +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h new file mode 100644 index 00000000..9da712f7 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h @@ -0,0 +1,41 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once + +#include "niederreiter.h" +#include "gf2x_limbs.h" + +void encrypt_niederreiter(DIGIT syndrome[], + const publicKeyNiederreiter_t *const pk, + const DIGIT err[] + ); diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c new file mode 100644 index 00000000..9e5a7a32 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c @@ -0,0 +1,145 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#include "niederreiter_keygen.h" + +#include "H_Q_matrices_generation.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "rng.h" +#include "dfr_test.h" + +#include +/*----------------------------------------------------------------------------*/ +/* Implementation that should never be optimized out by the compiler */ +static inline void zeroize( void *v, size_t n ) { + volatile unsigned char *p = v; + while ( n-- ) { + *p++ = 0; + } +} // end zeroize + +/*----------------------------------------------------------------------------*/ + +void key_gen_niederreiter(publicKeyNiederreiter_t *const pk, + privateKeyNiederreiter_t *const sk, + AES_XOF_struct *keys_expander) { + // sequence of N0 circ block matrices (p x p): Hi + + POSITION_T HPosOnes[N0][DV]; + POSITION_T HtrPosOnes[N0][DV]; + /* Sparse representation of the transposed circulant matrix H, + with weight DV. Each index contains the position of a '1' digit in the + corresponding Htr block */ + + /* Sparse representation of the matrix (Q). + A matrix containing the positions of the ones in the circulant + blocks of Q. Each row contains the position of the + ones of all the blocks of a row of Q as exponent+ + P*block_position */ + POSITION_T QPosOnes[N0][M]; + + /*Rejection-sample for a full L*/ + POSITION_T LPosOnes[N0][DV * M]; + int is_L_full; + int isDFRok; + sk->rejections = (int8_t) 0; + do { + generateHPosOnes_HtrPosOnes(HPosOnes, + HtrPosOnes, + keys_expander); + + generateQsparse(QPosOnes, + keys_expander); + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + LPosOnes[i][j] = INVALID_POS_VALUE; + } + } + + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0] = {0}; + for (int colQ = 0; colQ < N0; colQ++) { + for (int i = 0; i < N0; i++) { + gf2x_mod_mul_sparse(DV * M, auxPosOnes, + DV, HPosOnes[i], + qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); + gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + DV * M, LPosOnes[colQ], + DV * M, auxPosOnes); + processedQOnes[i] += qBlockWeights[i][colQ]; + } + } + is_L_full = 1; + for (int i = 0; i < N0; i++) { + is_L_full = is_L_full && (LPosOnes[i][DV * M - 1] != INVALID_POS_VALUE); + } + sk->rejections = sk->rejections + 1; + if (is_L_full) { + isDFRok = DFR_test(LPosOnes); + } + } while (!is_L_full || !isDFRok); + sk->rejections = sk->rejections - 1; + + DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + for (int j = 0; j < DV * M; j++) { + if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { + gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + } + } + DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + gf2x_mod_inverse(Ln0Inv, Ln0dense); + for (int i = 0; i < N0 - 1; i++) { + gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + Ln0Inv, + LPosOnes[i], + DV * M); + } + + for (int i = 0; i < N0 - 1; i++) { + gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + } +} // end key_gen_niederreiter + +/*----------------------------------------------------------------------------*/ + +void publicKey_deletion_niederreiter(publicKeyNiederreiter_t *const pk) { + + zeroize(pk, sizeof(publicKeyNiederreiter_t)); +} // publicKey_deletion_niederreiter + +/*----------------------------------------------------------------------------*/ + +void privateKey_deletion_niederreiter(privateKeyNiederreiter_t *const sk) { + zeroize(sk, sizeof(privateKeyNiederreiter_t)); +} // privateKey_deletion_niederreiter + +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h new file mode 100644 index 00000000..ae5210b7 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h @@ -0,0 +1,43 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once + +#include "niederreiter.h" +#include "rng.h" + +void key_gen_niederreiter(publicKeyNiederreiter_t *const pk, + privateKeyNiederreiter_t *const sk, + AES_XOF_struct *keys_expander); + +void publicKey_deletion_niederreiter(publicKeyNiederreiter_t *const pk); +void privateKey_deletion_niederreiter(privateKeyNiederreiter_t *const sk); diff --git a/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h b/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h new file mode 100644 index 00000000..ab43d044 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h @@ -0,0 +1,157 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ +#pragma once +// CATEGORY defined in the makefile + +/*----------------------------------------------------------------------------*/ +#if CATEGORY == 1 +#define TRNG_BYTE_LENGTH (24) +#define HASH_FUNCTION sha3_256 +#define HASH_BYTE_LENGTH (32) +// N0 defined in the makefile +#if (DFR_SL_LEVEL == 0) +#define P (35899) // modulus(x) = x^P-1 +#define DV (9) // odd number +#define M (9) +#define M0 (5) +#define M1 (4) +#define NUM_ERRORS_T (136) + +#elif (DFR_SL_LEVEL == 1) +#define P (52147) // modulus(x) = x^P-1 +#define DV (9) // odd number +#define M (9) +#define M0 (5) +#define M1 (4) +#define NUM_ERRORS_T (136) + +#else +#error "Unsupported number of circulant blocks" +#endif +#endif // end CATEGORY == 1 + +/*----------------------------------------------------------------------------*/ + +// We employ the parameters for Category 3 also in the case where the required +// security level is Category 2, where Category 2 has the following parameters. +// #define TRNG_BYTE_LENGTH (32) +// #define HASH_FUNCTION sha3_256 +// #define HASH_BYTE_LENGTH (32) + +/*----------------------------------------------------------------------------*/ + +#if (CATEGORY == 2) || (CATEGORY == 3) +#define TRNG_BYTE_LENGTH (32) +#define HASH_FUNCTION sha3_384 +#define HASH_BYTE_LENGTH (48) +// N0 defined in the makefile +#if (DFR_SL_LEVEL == 0) +#define P (57899) // modulus(x) = x^P-1 +#define DV (11) // odd number +#define M (11) +#define M0 (6) +#define M1 (5) +#define NUM_ERRORS_T (199) + +#elif (DFR_SL_LEVEL == 1) +#define P (96221) // modulus(x) = x^P-1 +#define DV (11) // odd number +#define M (11) +#define M0 (6) +#define M1 (5) +#define NUM_ERRORS_T (199) + +#else +#error "Unsupported number of circulant blocks" +#endif +#endif + +/*----------------------------------------------------------------------------*/ + +// We employ the parameters for Category 4 also in the case where the required +// security level is Category 5, where Category 4 has the following parameters. +// #if CATEGORY == 4 +// #define TRNG_BYTE_LENGTH (40) +// #define HASH_FUNCTION sha3_384 +// #define HASH_BYTE_LENGTH (48) +// #endif + +/*----------------------------------------------------------------------------*/ + +#if (CATEGORY == 4) || (CATEGORY == 5) +#define TRNG_BYTE_LENGTH (40) +#define HASH_FUNCTION sha3_512 +#define HASH_BYTE_LENGTH (64) +// N0 defined in the makefile +#if (DFR_SL_LEVEL == 0) +#define P (89051) // modulus(x) = x^P-1 +#define DV (13) // odd number +#define M (13) +#define M0 (7) +#define M1 (6) +#define NUM_ERRORS_T (267) + +#elif (DFR_SL_LEVEL == 1) +#define P (152267) // modulus(x) = x^P-1 +#define DV (13) // odd number +#define M (13) +#define M0 (7) +#define M1 (6) +#define NUM_ERRORS_T (267) + +#else +#error "Unsupported number of circulant blocks" +#endif +#endif +/*----------------------------------------------------------------------------*/ + +// Derived parameters, they are useful for QC-LDPC algorithms +#define HASH_BIT_LENGTH (HASH_BYTE_LENGTH << 3) +#define K ((N0-1)*P) +#define N (N0*P) +#define DC (N0*DV) + +// Circulant weight structure of the Q matrix, specialized per value of N0 +#if N0 == 2 +#define Q_BLOCK_WEIGHTS {{M0,M1},{M1,M0}} +#elif N0 == 3 +#define Q_BLOCK_WEIGHTS {{M0,M1,M2},{M2,M0,M1},{M1,M2,M0}} +#elif N0 == 4 +#define Q_BLOCK_WEIGHTS {{M0,M1,M2,M3},{M3,M0,M1,M2},{M2,M3,M0,M1},{M1,M2,M3,M0}} +#else +#error "Unsupported number of circulant blocks" +#endif + +static const unsigned char qBlockWeights[N0][N0] = Q_BLOCK_WEIGHTS; + + +/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/rng.c b/crypto_kem/ledakemlt12/clean/rng.c new file mode 100644 index 00000000..cff6f77f --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/rng.c @@ -0,0 +1,336 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + + +#include "rng.h" + +#include +#include +#include // void srand(unsigned int seed); int rand(void); RAND_MAX +#include // void *memset(void *s, int c, size_t n); +#define __USE_POSIX199309 +#include // struct timespec; clock_gettime(...); CLOCK_REALTIME + +#include "aes256.h" +#include "qc_ldpc_parameters.h" + + +/******************************************************************************/ +/*----------------------------------------------------------------------------*/ +/* start PSEUDO-RAND GENERATOR ROUTINES for rnd.h */ +/*----------------------------------------------------------------------------*/ + + +void initialize_pseudo_random_generator_seed(int ac, char *av[]) { + + if (ac == 2) { + srand(atoi(av[1])); + } else { + struct timespec seedValue; + clock_gettime(CLOCK_REALTIME, &seedValue); + srand(seedValue.tv_nsec); + } // end else-if + unsigned char pseudo_entropy[48]; + for (int i = 0; i < 48; i++) { + pseudo_entropy[i] = rand() & 0xff; + } + randombytes_init(pseudo_entropy, + NULL, + 0 /*unused in NIST function*/); + + +} // end initilize_pseudo_random_sequence_seed + + +/*----------------------------------------------------------------------------*/ + +/* Initializes a dedicated DRBG context to avoid conflicts with the global one + * declared by NIST for KATs. Provides the output of the DRBG in output, for + * the given length */ + + +/*----------------------------------------------------------------------------*/ +/* end PSEUDO-RAND GENERATOR ROUTINES for rnd.h */ +/*----------------------------------------------------------------------------*/ + +AES256_CTR_DRBG_struct DRBG_ctx; + +void AES256_ECB(unsigned char *key, unsigned char *ctr, + unsigned char *buffer); + +/* + seedexpander_init() + ctx - stores the current state of an instance of the seed expander + seed - a 32 byte random value + diversifier - an 8 byte diversifier + maxlen - maximum number of bytes (less than 2**32) generated under this seed and diversifier + */ +int +seedexpander_init(AES_XOF_struct *ctx, + unsigned char *seed, + unsigned char *diversifier, + unsigned long maxlen) { + if ( maxlen >= 0x100000000 ) { + return RNG_BAD_MAXLEN; + } + + ctx->length_remaining = maxlen; + + memset(ctx->key, 0, 32); + int max_accessible_seed_len = TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH; + memcpy(ctx->key, seed, max_accessible_seed_len); + + memcpy(ctx->ctr, diversifier, 8); + ctx->ctr[11] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[10] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[9] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[8] = maxlen % 256; + memset(ctx->ctr + 12, 0x00, 4); + + ctx->buffer_pos = 16; + memset(ctx->buffer, 0x00, 16); + + return RNG_SUCCESS; +} + +/* + seedexpander() + ctx - stores the current state of an instance of the seed expander + x - returns the XOF data + xlen - number of bytes to return + */ +int +seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen) { + unsigned long offset; + + if ( x == NULL ) { + return RNG_BAD_OUTBUF; + } + if ( xlen >= ctx->length_remaining ) { + return RNG_BAD_REQ_LEN; + } + + ctx->length_remaining -= xlen; + + offset = 0; + while ( xlen > 0 ) { + if ( xlen <= (16 - ctx->buffer_pos) ) { // buffer has what we need + memcpy(x + offset, ctx->buffer + ctx->buffer_pos, xlen); + ctx->buffer_pos += xlen; + + return RNG_SUCCESS; + } + + // take what's in the buffer + memcpy(x + offset, ctx->buffer + ctx->buffer_pos, 16 - ctx->buffer_pos); + xlen -= 16 - ctx->buffer_pos; + offset += 16 - ctx->buffer_pos; + + AES256_ECB(ctx->key, ctx->ctr, ctx->buffer); + ctx->buffer_pos = 0; + + //increment the counter + for (int i = 15; i >= 12; i--) { + if ( ctx->ctr[i] == 0xff ) { + ctx->ctr[i] = 0x00; + } else { + ctx->ctr[i]++; + break; + } + } + + } + + return RNG_SUCCESS; +} + +// Use whatever AES implementation you have. This uses AES from openSSL library +// key - 256-bit AES key +// ptx - a 128-bit plaintext value +// ctx - a 128-bit ciphertext value + +void +AES256_ECB(unsigned char *key, unsigned char *ptx, unsigned char *ctx) { + uint32_t round_key[4 * (NROUNDS + 1)] = {0x00}; + rijndaelKeySetupEnc(round_key, key, KEYLEN_b); + rijndaelEncrypt(round_key, NROUNDS, ptx, ctx); +} + +void +randombytes_init(unsigned char *entropy_input, + unsigned char *personalization_string, + int security_strength) { + unsigned char seed_material[48]; + + memcpy(seed_material, entropy_input, 48); + if (personalization_string) + for (int i = 0; i < 48; i++) { + seed_material[i] ^= personalization_string[i]; + } + memset(DRBG_ctx.Key, 0x00, 32); + memset(DRBG_ctx.V, 0x00, 16); + AES256_CTR_DRBG_Update(seed_material, DRBG_ctx.Key, DRBG_ctx.V); + DRBG_ctx.reseed_counter = 1; +} + +int +randombytes(unsigned char *x, unsigned long long xlen) { + unsigned char block[16]; + int i = 0; + + while ( xlen > 0 ) { + //increment V + for (int j = 15; j >= 0; j--) { + if ( DRBG_ctx.V[j] == 0xff ) { + DRBG_ctx.V[j] = 0x00; + } else { + DRBG_ctx.V[j]++; + break; + } + } + AES256_ECB(DRBG_ctx.Key, DRBG_ctx.V, block); + if ( xlen > 15 ) { + memcpy(x + i, block, 16); + i += 16; + xlen -= 16; + } else { + memcpy(x + i, block, xlen); + xlen = 0; + } + } + AES256_CTR_DRBG_Update(NULL, DRBG_ctx.Key, DRBG_ctx.V); + DRBG_ctx.reseed_counter++; + + return RNG_SUCCESS; +} + +void +AES256_CTR_DRBG_Update(unsigned char *provided_data, + unsigned char *Key, + unsigned char *V) { + unsigned char temp[48]; + + for (int i = 0; i < 3; i++) { + //increment V + for (int j = 15; j >= 0; j--) { + if ( V[j] == 0xff ) { + V[j] = 0x00; + } else { + V[j]++; + break; + } + } + + AES256_ECB(Key, V, temp + 16 * i); + } + if ( provided_data != NULL ) + for (int i = 0; i < 48; i++) { + temp[i] ^= provided_data[i]; + } + memcpy(Key, temp, 32); + memcpy(V, temp + 32, 16); +} + + + +void deterministic_random_byte_generator(unsigned char *const output, + const unsigned long long output_len, + const unsigned char *const seed, + const unsigned long long seed_length + ) { + /* DRBG context initialization */ + AES256_CTR_DRBG_struct ctx; + unsigned char seed_material[48]; + memset(seed_material, 0x00, 48); + memcpy(seed_material, seed, seed_length); + + memset(ctx.Key, 0x00, 32); + memset(ctx.V, 0x00, 16); + AES256_CTR_DRBG_Update(seed_material, ctx.Key, ctx.V); + ctx.reseed_counter = 1; + + /* Actual DRBG computation as from the randombytes(unsigned char *x, + * unsigned long long xlen) from NIST */ + + unsigned char block[16]; + int i = 0, length_remaining; + + length_remaining = output_len; + + while ( length_remaining > 0 ) { + //increment V + for (int j = 15; j >= 0; j--) { + if ( ctx.V[j] == 0xff ) { + ctx.V[j] = 0x00; + } else { + ctx.V[j]++; + break; + } + } + AES256_ECB(ctx.Key, ctx.V, block); + if ( length_remaining > 15 ) { + memcpy(output + i, block, 16); + i += 16; + length_remaining -= 16; + } else { + memcpy(output + i, block, length_remaining); + length_remaining = 0; + } + } + AES256_CTR_DRBG_Update(NULL, ctx.Key, ctx.V); + ctx.reseed_counter++; + +} // end deterministic_random_byte_generator + +void seedexpander_from_trng(AES_XOF_struct *ctx, + const unsigned char *trng_entropy + /* TRNG_BYTE_LENGTH wide buffer */) { + + /*the NIST seedexpander will however access 32B from this buffer */ + unsigned int prng_buffer_size = TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH; + unsigned char prng_buffer[TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH] = { 0x00 }; + memcpy(prng_buffer, + trng_entropy, + TRNG_BYTE_LENGTH < prng_buffer_size ? TRNG_BYTE_LENGTH : prng_buffer_size); + /* if extra entropy is provided, add it to the diversifier */ + #if TRNG_BYTE_LENGTH == 40 + unsigned char *diversifier = ((unsigned char *)trng_entropy) + 32; + #else + unsigned char diversifier[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + #endif + /* the required seed expansion will be quite small, set the max number of + * bytes conservatively to 10 MiB*/ + seedexpander_init(ctx, prng_buffer, diversifier, 10 * 1024 * 1024); +} diff --git a/crypto_kem/ledakemlt12/clean/rng.h b/crypto_kem/ledakemlt12/clean/rng.h new file mode 100644 index 00000000..6615c529 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/rng.h @@ -0,0 +1,96 @@ +/** + * + * + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ + +#pragma once + +/****** From this point on, the code was supplied by NIST ****************/ +// Created by Bassham, Lawrence E (Fed) on 8/29/17. +// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved. +// +/****** from NIST ****************/ + +#include + +#define RNG_SUCCESS 0 +#define RNG_BAD_MAXLEN -1 +#define RNG_BAD_OUTBUF -2 +#define RNG_BAD_REQ_LEN -3 + +typedef struct { + unsigned char buffer[16]; + int buffer_pos; + unsigned long length_remaining; + unsigned char key[32]; + unsigned char ctr[16]; +} AES_XOF_struct; + +typedef struct { + unsigned char Key[32]; + unsigned char V[16]; + int reseed_counter; +} AES256_CTR_DRBG_struct; + + +void +AES256_CTR_DRBG_Update(unsigned char *provided_data, + unsigned char *Key, + unsigned char *V); + +int +seedexpander_init(AES_XOF_struct *ctx, + unsigned char *seed, + unsigned char *diversifier, + unsigned long maxlen); + +int +seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen); + +void +randombytes_init(unsigned char *entropy_input, + unsigned char *personalization_string, + int security_strength); + +int +randombytes(unsigned char *x, unsigned long long xlen); + +/****** End of NIST supplied code ****************/ + +void initialize_pseudo_random_generator_seed(int ac, char *av[]); + +void deterministic_random_byte_generator(unsigned char *const output, + const unsigned long long output_len, + const unsigned char *const seed, + const unsigned long long seed_length); + +void seedexpander_from_trng(AES_XOF_struct *ctx, + const unsigned char *trng_entropy + /* TRNG_BYTE_LENGTH wide buffer */); diff --git a/crypto_kem/ledakemlt12/clean/sha3.h b/crypto_kem/ledakemlt12/clean/sha3.h new file mode 100644 index 00000000..642843de --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/sha3.h @@ -0,0 +1,43 @@ +#pragma once + +#define LITTLE_ENDIAN +#include + +static inline +void sha3_256(const unsigned char *input, + unsigned int inputByteLen, + unsigned char *output) { + Keccak_HashInstance state; + Keccak_HashInitialize(&state, 1088, 512, 256, 0x06); + Keccak_HashUpdate(&state, input, inputByteLen * 8); + Keccak_HashFinal(&state, output); +} + +/** + * Function to compute SHA3-384 on the input message. + * The output length is fixed to 48 bytes. + */ +static inline +void sha3_384(const unsigned char *input, + unsigned int inputByteLen, + unsigned char *output) { + Keccak_HashInstance state; + Keccak_HashInitialize(&state, 832, 768, 384, 0x06); + Keccak_HashUpdate(&state, input, inputByteLen * 8); + Keccak_HashFinal(&state, output); +} + +/** + * Function to compute SHA3-512 on the input message. + * The output length is fixed to 64 bytes. + */ +static inline +void sha3_512(const unsigned char *input, + unsigned int inputByteLen, + unsigned char *output) { + Keccak_HashInstance state; + Keccak_HashInitialize(&state, 576, 1024, 512, 0x06); + Keccak_HashUpdate(&state, input, inputByteLen * 8); + Keccak_HashFinal(&state, output); +} + From 10aa790154d04d7920a8a9c850c34db71b115d34 Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 19 May 2019 19:16:16 +0200 Subject: [PATCH 03/35] remove this one --- crypto_kem/ledacryptkem2128/META.yml | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 crypto_kem/ledacryptkem2128/META.yml diff --git a/crypto_kem/ledacryptkem2128/META.yml b/crypto_kem/ledacryptkem2128/META.yml deleted file mode 100644 index c810d153..00000000 --- a/crypto_kem/ledacryptkem2128/META.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: ledacryptkem2128 -type: kem -claimed-nist-level: 1 -claimed-security: IND-CCA2 -length-public-key: 6520 -length-secret-key: 468 -length-ciphertext: 6520 -length-shared-secret: 32 -nistkat-sha256: 3bb5945e0aea26f121e1d56946760e506bdfbebb07e2fb018ce737b90b1eee2b -principal-submitter: Marco Baldi -auxiliary-submitters: - - Alessandro Barenghi - - Franco Chiaraluce - - Gerardo Pelosi - - Paolo Santini -implementations: - - name: clean - version: 2.0 From b40d53b0d81b4cd58df3e794968b391e7e61d144 Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 19 May 2019 19:39:57 +0200 Subject: [PATCH 04/35] added license --- crypto_kem/ledakemlt12/clean/LICENSE | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 crypto_kem/ledakemlt12/clean/LICENSE diff --git a/crypto_kem/ledakemlt12/clean/LICENSE b/crypto_kem/ledakemlt12/clean/LICENSE new file mode 100644 index 00000000..f6004c1a --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/LICENSE @@ -0,0 +1,32 @@ +/** + * + * LEDAcryptKEM + * + * @version 2.0 (March 2019) + * + * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. + * Also contains public domain implementations of AES and Keccak + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ From d02c1879e93bb3709aed6273e36ba6d6be281ebf Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 19 May 2019 19:40:38 +0200 Subject: [PATCH 05/35] added todo --- crypto_kem/ledakemlt12/clean/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crypto_kem/ledakemlt12/clean/Makefile b/crypto_kem/ledakemlt12/clean/Makefile index c04d41f5..5679cd70 100644 --- a/crypto_kem/ledakemlt12/clean/Makefile +++ b/crypto_kem/ledakemlt12/clean/Makefile @@ -17,6 +17,8 @@ OBJECTS=aes256.o bf_decoding.o dfr_test.o gf2x_arith_mod_xPplusOne.o \ CFLAGS=-O3 -Wall -Wextra -Wpedantic -Wvla -Wmissing-prototypes -std=c99 \ -DCATEGORY=$(SL) -DN0=$(N0) -DDFR_SL_LEVEL=$(DFR_SL_LEVEL) -I../../../common $(EXTRAFLAGS) +# TODO: -Werror + all: $(LIB) %.o: %.c $(HEADERS) From fcbd6918c249aa28b86404032e39de9d9e43a9ce Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 19 May 2019 19:41:03 +0200 Subject: [PATCH 06/35] create api.h --- crypto_kem/ledakemlt12/clean/api.h | 79 +++++------------------------- 1 file changed, 11 insertions(+), 68 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/api.h b/crypto_kem/ledakemlt12/clean/api.h index cf7cd668..6fc42ab8 100644 --- a/crypto_kem/ledakemlt12/clean/api.h +++ b/crypto_kem/ledakemlt12/clean/api.h @@ -1,75 +1,18 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ +#ifndef PQCLEAN_LEDAKEMLT12_CLEAN_API_H +#define PQCLEAN_LEDAKEMLT12_CLEAN_API_H -#pragma once +#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_SECRETKEYBYTES 25 +#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_PUBLICKEYBYTES 6520 +#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_CIPHERTEXTBYTES 6520 +#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_BYTES 32 -#include "qc_ldpc_parameters.h" -#include "gf2x_limbs.h" -#include "gf2x_arith_mod_xPplusOne.h" +#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT12" -#define CRYPTO_ALGNAME "LEDA" +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk); -/* required bytes of input randomness */ -#define CRYPTO_RANDOMBYTES TRNG_BYTE_LENGTH +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk); -/* size in bytes of the secret key */ -#define CRYPTO_SECRETKEYBYTES TRNG_BYTE_LENGTH+1 - -/* size in bytes of the public key */ -#define CRYPTO_PUBLICKEYBYTES ((N0-1)*NUM_DIGITS_GF2X_ELEMENT*DIGIT_SIZE_B) - -/* size in bytes of the shared secret */ -#define CRYPTO_BYTES HASH_BYTE_LENGTH - -/*size in bytes of the ciphertext*/ -#define CRYPTO_CIPHERTEXTBYTES (NUM_DIGITS_GF2X_ELEMENT*DIGIT_SIZE_B) - -/* Your functions must return 0 to indicate success, -1 to indicate an error - * condition */ - -/* Generates a keypair - pk is the public key and sk is the secret key. */ -int crypto_kem_keypair( unsigned char *pk, - unsigned char *sk ); - -/* Encrypt - pk is the public key, ct is a key encapsulation message - (ciphertext), ss is the shared secret.*/ -int crypto_kem_enc( unsigned char *ct, - unsigned char *ss, - const unsigned char *pk ); +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk); -/* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private - key, ss is the shared secret */ - -int crypto_kem_dec( unsigned char *ss, - const unsigned char *ct, - const unsigned char *sk ); +#endif From 7cbeeaee59f10e7af82ae490a60bcb45b2d8f5a5 Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 19 May 2019 19:51:36 +0200 Subject: [PATCH 07/35] update META.yml --- crypto_kem/ledakemlt12/META.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crypto_kem/ledakemlt12/META.yml b/crypto_kem/ledakemlt12/META.yml index 426ffdc3..a213611a 100644 --- a/crypto_kem/ledakemlt12/META.yml +++ b/crypto_kem/ledakemlt12/META.yml @@ -1,9 +1,9 @@ -name: LEDAcryptKEM-LT12 +name: LEDAcryptKEMLT12 type: kem claimed-nist-level: 1 claimed-security: IND-CCA2 length-public-key: 6520 -length-secret-key: 468 +length-secret-key: 25 length-ciphertext: 6520 length-shared-secret: 32 nistkat-sha256: 3bb5945e0aea26f121e1d56946760e506bdfbebb07e2fb018ce737b90b1eee2b From 17ea905952189df80f84976feadb4883536dddfc Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 19 May 2019 19:52:19 +0200 Subject: [PATCH 08/35] moved license to one license file --- .../clean/H_Q_matrices_generation.c | 32 ----------------- .../clean/H_Q_matrices_generation.h | 32 ----------------- crypto_kem/ledakemlt12/clean/bf_decoding.c | 31 ---------------- crypto_kem/ledakemlt12/clean/bf_decoding.h | 35 ------------------- crypto_kem/ledakemlt12/clean/gf2x_arith.c | 33 ----------------- crypto_kem/ledakemlt12/clean/gf2x_arith.h | 32 ----------------- .../clean/gf2x_arith_mod_xPplusOne.c | 33 ----------------- .../clean/gf2x_arith_mod_xPplusOne.h | 32 ----------------- crypto_kem/ledakemlt12/clean/gf2x_limbs.h | 33 ----------------- crypto_kem/ledakemlt12/clean/kem.c | 32 ----------------- crypto_kem/ledakemlt12/clean/niederreiter.h | 32 ----------------- .../ledakemlt12/clean/niederreiter_decrypt.c | 34 ------------------ .../ledakemlt12/clean/niederreiter_decrypt.h | 32 ----------------- .../ledakemlt12/clean/niederreiter_encrypt.c | 32 ----------------- .../ledakemlt12/clean/niederreiter_encrypt.h | 32 ----------------- .../ledakemlt12/clean/niederreiter_keygen.c | 32 ----------------- .../ledakemlt12/clean/niederreiter_keygen.h | 32 ----------------- .../ledakemlt12/clean/qc_ldpc_parameters.h | 31 ---------------- crypto_kem/ledakemlt12/clean/rng.c | 33 ----------------- crypto_kem/ledakemlt12/clean/rng.h | 32 ----------------- 20 files changed, 647 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c index 703f316b..65d1d115 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #include "H_Q_matrices_generation.h" #include "gf2x_arith_mod_xPplusOne.h" diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h index 98a67b51..fd4ee385 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once #include "qc_ldpc_parameters.h" #include "gf2x_limbs.h" diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/clean/bf_decoding.c index e0801a2d..2c9156cb 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.c @@ -1,34 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ #include "bf_decoding.h" #include "gf2x_arith_mod_xPplusOne.h" #include diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.h b/crypto_kem/ledakemlt12/clean/bf_decoding.h index b14173c2..1c8b5852 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.h @@ -1,36 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - - #pragma once #include "qc_ldpc_parameters.h" #include "gf2x_limbs.h" @@ -74,5 +41,3 @@ int bf_decoding(DIGIT err[], #define B0 88 #define T_BAR 6 #endif - - diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/clean/gf2x_arith.c index d502741c..745fb0fc 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.c @@ -1,36 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - - #include "gf2x_arith.h" #include // memset(...) #include diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.h b/crypto_kem/ledakemlt12/clean/gf2x_arith.h index fb966ad6..08e15989 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once #include "gf2x_limbs.h" diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index a099ecfb..f8858d95 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -1,36 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - - #include "gf2x_arith_mod_xPplusOne.h" #include "rng.h" #include // memcpy(...), memset(...) diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h index ce3e2e86..acc0919c 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once #include "gf2x_limbs.h" diff --git a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h index 6ced9310..7e331084 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once /*----------------------------------------------------------------------------*/ @@ -91,4 +59,3 @@ typedef CAT( uint, CPU_WORD_BITS, _t ) DIGIT; #define POSITION_T uint32_t /*----------------------------------------------------------------------------*/ - diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt12/clean/kem.c index 28b55695..f9903718 100644 --- a/crypto_kem/ledakemlt12/clean/kem.c +++ b/crypto_kem/ledakemlt12/clean/kem.c @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #include "niederreiter_keygen.h" #include "niederreiter_encrypt.h" #include "niederreiter_decrypt.h" diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.h b/crypto_kem/ledakemlt12/clean/niederreiter.h index 34c05e73..e7ba199d 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.h +++ b/crypto_kem/ledakemlt12/clean/niederreiter.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once #include "qc_ldpc_parameters.h" #include "gf2x_limbs.h" diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c index 13023318..9b34215e 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #include "niederreiter_decrypt.h" #include "qc_ldpc_parameters.h" @@ -149,5 +117,3 @@ int decrypt_niederreiter(DIGIT err[], // N0 circ poly } // end decrypt_niederreiter /*----------------------------------------------------------------------------*/ - - diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h index 7ac486e0..66938700 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h +++ b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once #include "niederreiter.h" diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c index a4ed2b4a..fb075873 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #include "niederreiter_encrypt.h" #include "qc_ldpc_parameters.h" #include "gf2x_arith_mod_xPplusOne.h" diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h index 9da712f7..e69f0120 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h +++ b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once #include "niederreiter.h" diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c index 9e5a7a32..679e6716 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #include "niederreiter_keygen.h" #include "H_Q_matrices_generation.h" diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h index ae5210b7..a775028b 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h +++ b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once #include "niederreiter.h" diff --git a/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h b/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h index ab43d044..be88e080 100644 --- a/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h +++ b/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h @@ -1,34 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ #pragma once // CATEGORY defined in the makefile diff --git a/crypto_kem/ledakemlt12/clean/rng.c b/crypto_kem/ledakemlt12/clean/rng.c index cff6f77f..02382074 100644 --- a/crypto_kem/ledakemlt12/clean/rng.c +++ b/crypto_kem/ledakemlt12/clean/rng.c @@ -1,36 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - - #include "rng.h" #include diff --git a/crypto_kem/ledakemlt12/clean/rng.h b/crypto_kem/ledakemlt12/clean/rng.h index 6615c529..5e7724bc 100644 --- a/crypto_kem/ledakemlt12/clean/rng.h +++ b/crypto_kem/ledakemlt12/clean/rng.h @@ -1,35 +1,3 @@ -/** - * - * - * - * @version 2.0 (March 2019) - * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * - * In alphabetical order: - * - * @author Marco Baldi - * @author Alessandro Barenghi - * @author Franco Chiaraluce - * @author Gerardo Pelosi - * @author Paolo Santini - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - **/ - #pragma once /****** From this point on, the code was supplied by NIST ****************/ From cc551546bffde000240e8ef4a50b4c7c10ec7b15 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 24 May 2019 18:38:54 +0200 Subject: [PATCH 09/35] cleaning & fixing gcc warnings --- .../clean/H_Q_matrices_generation.c | 35 +- .../clean/H_Q_matrices_generation.h | 22 +- crypto_kem/ledakemlt12/clean/Makefile | 49 +- crypto_kem/ledakemlt12/clean/aes256.c | 600 ------------------ crypto_kem/ledakemlt12/clean/aes256.h | 41 -- crypto_kem/ledakemlt12/clean/bf_decoding.c | 15 +- crypto_kem/ledakemlt12/clean/bf_decoding.h | 48 +- crypto_kem/ledakemlt12/clean/dfr_test.c | 43 +- crypto_kem/ledakemlt12/clean/dfr_test.h | 7 +- crypto_kem/ledakemlt12/clean/gf2x_arith.c | 282 ++++---- crypto_kem/ledakemlt12/clean/gf2x_arith.h | 39 +- .../clean/gf2x_arith_mod_xPplusOne.c | 227 +++---- .../clean/gf2x_arith_mod_xPplusOne.h | 192 ++---- crypto_kem/ledakemlt12/clean/gf2x_limbs.h | 94 +-- crypto_kem/ledakemlt12/clean/kem.c | 73 +-- crypto_kem/ledakemlt12/clean/niederreiter.c | 215 +++++++ crypto_kem/ledakemlt12/clean/niederreiter.h | 28 +- .../ledakemlt12/clean/niederreiter_decrypt.c | 119 ---- .../ledakemlt12/clean/niederreiter_decrypt.h | 13 - .../ledakemlt12/clean/niederreiter_encrypt.c | 25 - .../ledakemlt12/clean/niederreiter_encrypt.h | 9 - .../ledakemlt12/clean/niederreiter_keygen.c | 113 ---- .../ledakemlt12/clean/niederreiter_keygen.h | 11 - .../ledakemlt12/clean/qc_ldpc_parameters.h | 133 +--- crypto_kem/ledakemlt12/clean/rng.c | 246 +------ crypto_kem/ledakemlt12/clean/rng.h | 58 +- crypto_kem/ledakemlt12/clean/sha3.h | 43 -- 27 files changed, 731 insertions(+), 2049 deletions(-) delete mode 100644 crypto_kem/ledakemlt12/clean/aes256.c delete mode 100644 crypto_kem/ledakemlt12/clean/aes256.h create mode 100644 crypto_kem/ledakemlt12/clean/niederreiter.c delete mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c delete mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h delete mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c delete mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h delete mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_keygen.c delete mode 100644 crypto_kem/ledakemlt12/clean/niederreiter_keygen.h delete mode 100644 crypto_kem/ledakemlt12/clean/sha3.h diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c index 65d1d115..9b603c5a 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c @@ -1,17 +1,15 @@ #include "H_Q_matrices_generation.h" #include "gf2x_arith_mod_xPplusOne.h" -/*----------------------------------------------------------------------------*/ - -void generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], - POSITION_T HtrPosOnes[N0][DV], - AES_XOF_struct *keys_expander - ) { +void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes( + POSITION_T HPosOnes[N0][DV], + POSITION_T HtrPosOnes[N0][DV], + AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { /* Generate a random block of Htr */ - rand_circulant_sparse_block(&HtrPosOnes[i][0], - DV, - keys_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(&HtrPosOnes[i][0], + DV, + keys_expander); } for (int i = 0; i < N0; i++) { /* Obtain directly the sparse representation of the block of H */ @@ -19,21 +17,18 @@ void generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], HPosOnes[i][k] = (P - HtrPosOnes[i][k]) % P; /* transposes indexes */ }// end for k } -} // end generateHtr_HtrPosOnes +} -/*----------------------------------------------------------------------------*/ - -void generateQsparse(POSITION_T pos_ones[N0][M], - AES_XOF_struct *keys_expander) { +void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse( + POSITION_T pos_ones[N0][M], + AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { int placed_ones = 0; for (int j = 0; j < N0; j++) { - rand_circulant_sparse_block(&pos_ones[i][placed_ones], - qBlockWeights[i][j], - keys_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(&pos_ones[i][placed_ones], + qBlockWeights[i][j], + keys_expander); placed_ones += qBlockWeights[i][j]; } // end for j } // end for i -} // end generateQsparse - -/*----------------------------------------------------------------------------*/ +} diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h index fd4ee385..c05a72fb 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h @@ -1,17 +1,17 @@ -#pragma once +#ifndef H_Q_MATRICES_GENERATION_H +#define H_Q_MATRICES_GENERATION_H + #include "qc_ldpc_parameters.h" #include "gf2x_limbs.h" #include "rng.h" -/*----------------------------------------------------------------------------*/ +void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes( + POSITION_T HPosOnes[N0][DV], + POSITION_T HtrPosOnes[N0][DV], + AES_XOF_struct *niederreiter_keys_expander); -void generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], - POSITION_T HtrPosOnes[N0][DV], - AES_XOF_struct *niederreiter_keys_expander); +void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse( + POSITION_T pos_ones[N0][M], + AES_XOF_struct *niederreiter_keys_expander); -/*----------------------------------------------------------------------------*/ - -void generateQsparse(POSITION_T pos_ones[N0][M], - AES_XOF_struct *niederreiter_keys_expander); - -/*----------------------------------------------------------------------------*/ +#endif diff --git a/crypto_kem/ledakemlt12/clean/Makefile b/crypto_kem/ledakemlt12/clean/Makefile index 5679cd70..e3548212 100644 --- a/crypto_kem/ledakemlt12/clean/Makefile +++ b/crypto_kem/ledakemlt12/clean/Makefile @@ -1,23 +1,17 @@ # This Makefile can be used with GNU Make or BSD Make -SL = 1 -N0 = 2 -DFR_SL_LEVEL = 1 +LIB=libledakemlt12_clean.a +HEADERS=api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ + gf2x_arith.h gf2x_limbs.h H_Q_matrices_generation.h \ + niederreiter.h qc_ldpc_parameters.h rng.h -LIB=libLEDAkem_sl$(SL)_N0$(N0)_clean.a -HEADERS=aes256.h api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ - gf2x_arith.h gf2x_limbs.h H_Q_matrices_generation.h niederreiter_decrypt.h \ - niederreiter_encrypt.h niederreiter_keygen.h niederreiter.h \ - qc_ldpc_parameters.h rng.h sha3.h +OBJECTS=bf_decoding.o dfr_test.o gf2x_arith_mod_xPplusOne.o \ + gf2x_arith.o H_Q_matrices_generation.o kem.o niederreiter.o rng.o -OBJECTS=aes256.o bf_decoding.o dfr_test.o gf2x_arith_mod_xPplusOne.o \ - gf2x_arith.o H_Q_matrices_generation.o kem.o niederreiter_decrypt.o \ - niederreiter_encrypt.o niederreiter_keygen.o rng.o +CFLAGS=-O3 -Wall -Wextra -Wpedantic -Wmissing-prototypes -std=c99 \ + -I../../../common $(EXTRAFLAGS) -CFLAGS=-O3 -Wall -Wextra -Wpedantic -Wvla -Wmissing-prototypes -std=c99 \ - -DCATEGORY=$(SL) -DN0=$(N0) -DDFR_SL_LEVEL=$(DFR_SL_LEVEL) -I../../../common $(EXTRAFLAGS) - -# TODO: -Werror +# TODO: -Werror -Wvla all: $(LIB) @@ -30,28 +24,3 @@ $(LIB): $(OBJECTS) clean: $(RM) $(OBJECTS) $(RM) $(LIB) - - -# old makefile -# CFLAGS = -DCATEGORY=$(SL) -DN0=$(N0) -DDFR_SL_LEVEL=$(DFR_SL_LEVEL) -DCPU_WORD_BITS=64 \ -# -std=c11 -Wall -pedantic -Wmaybe-uninitialized -Wuninitialized \ -# -march=native -O3 -g3 -# LDFLAGS = -lm -lkeccak -# INCLUDES = -I./include -# SRCDIR = library -# OBJDIR = bin -# -# # Gathers the names of all C files -# CSRC = $(wildcard $(SRCDIR)/*.c) -# # Produces in $(COBJS) the names of .o object files for all C files -# COBJS = $(CSRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) -# -# $(OBJDIR)/%.o: $(SRCDIR)/%.c -# $(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ -# -# .PHONY : all clean -# -# all: $(COBJS) -# ar rcs $(OBJDIR)/libLEDAkem_sl$(SL)_N0$(N0).a $(COBJS) -# clean: -# $(RM) $(OBJDIR)/*.o $(OBJDIR)/libLEDAkem_sl$(SL)_N0$(N0).a diff --git a/crypto_kem/ledakemlt12/clean/aes256.c b/crypto_kem/ledakemlt12/clean/aes256.c deleted file mode 100644 index 7d6dea97..00000000 --- a/crypto_kem/ledakemlt12/clean/aes256.c +++ /dev/null @@ -1,600 +0,0 @@ -/** - * AES-256 self contained implementation derived from : - * - * rijndael-alg-fst.h - * - * @version 3.0 (December 2000) - * - * Optimised ANSI C code for the Rijndael cipher (now AES) - * - * @author Vincent Rijmen - * @author Antoon Bosselaers - * @author Paulo Barreto - * - * originally placed in the public domain by the authors. - * - * - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include "aes256.h" - -/* -Te0[x] = S [x].[02, 01, 01, 03]; -Te1[x] = S [x].[03, 02, 01, 01]; -Te2[x] = S [x].[01, 03, 02, 01]; -Te3[x] = S [x].[01, 01, 03, 02]; -Te4[x] = S [x].[01, 01, 01, 01]; -*/ - -static const uint32_t Te0[256] = { - 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, - 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, - 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, - 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, - 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, - 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, - 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, - 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, - 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, - 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, - 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, - 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, - 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, - 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, - 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, - 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, - 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, - 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, - 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, - 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, - 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, - 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, - 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, - 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, - 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, - 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, - 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, - 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, - 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, - 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, - 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, - 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, - 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, - 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, - 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, - 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, - 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, - 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, - 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, - 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, - 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, - 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, - 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, - 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, - 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, - 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, - 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, - 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, - 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, - 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, - 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, - 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, - 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, - 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, - 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, - 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, - 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, - 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, - 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, - 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, - 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, - 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, - 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, - 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, -}; -static const uint32_t Te1[256] = { - 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, - 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, - 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, - 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, - 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, - 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, - 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, - 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, - 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, - 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, - 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, - 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, - 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, - 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, - 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, - 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, - 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, - 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, - 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, - 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, - 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, - 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, - 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, - 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, - 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, - 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, - 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, - 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, - 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, - 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, - 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, - 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, - 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, - 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, - 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, - 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, - 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, - 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, - 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, - 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, - 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, - 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, - 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, - 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, - 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, - 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, - 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, - 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, - 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, - 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, - 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, - 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, - 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, - 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, - 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, - 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, - 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, - 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, - 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, - 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, - 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, - 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, - 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, - 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, -}; -static const uint32_t Te2[256] = { - 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, - 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, - 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, - 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, - 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, - 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, - 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, - 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, - 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, - 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, - 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, - 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, - 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, - 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, - 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, - 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, - 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, - 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, - 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, - 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, - 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, - 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, - 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, - 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, - 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, - 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, - 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, - 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, - 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, - 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, - 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, - 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, - 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, - 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, - 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, - 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, - 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, - 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, - 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, - 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, - 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, - 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, - 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, - 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, - 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, - 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, - 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, - 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, - 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, - 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, - 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, - 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, - 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, - 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, - 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, - 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, - 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, - 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, - 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, - 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, - 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, - 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, - 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, - 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, -}; -static const uint32_t Te3[256] = { - - 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, - 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, - 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, - 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, - 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, - 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, - 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, - 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, - 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, - 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, - 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, - 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, - 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, - 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, - 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, - 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, - 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, - 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, - 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, - 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, - 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, - 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, - 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, - 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, - 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, - 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, - 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, - 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, - 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, - 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, - 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, - 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, - 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, - 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, - 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, - 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, - 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, - 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, - 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, - 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, - 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, - 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, - 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, - 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, - 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, - 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, - 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, - 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, - 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, - 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, - 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, - 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, - 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, - 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, - 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, - 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, - 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, - 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, - 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, - 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, - 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, - 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, - 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, - 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, -}; -static const uint32_t Te4[256] = { - 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, - 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, - 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU, - 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U, - 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU, - 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U, - 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU, - 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U, - 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U, - 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU, - 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U, - 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U, - 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U, - 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU, - 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U, - 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U, - 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU, - 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U, - 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U, - 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U, - 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU, - 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU, - 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U, - 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU, - 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU, - 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U, - 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU, - 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U, - 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU, - 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U, - 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U, - 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U, - 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU, - 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U, - 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU, - 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U, - 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU, - 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U, - 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U, - 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU, - 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU, - 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU, - 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U, - 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U, - 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU, - 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U, - 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU, - 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U, - 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU, - 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U, - 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU, - 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU, - 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U, - 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU, - 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U, - 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU, - 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U, - 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U, - 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U, - 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU, - 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU, - 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U, - 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU, - 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, -}; - -static const uint32_t rcon[] = { - 0x01000000, 0x02000000, 0x04000000, 0x08000000, - 0x10000000, 0x20000000, 0x40000000, 0x80000000, - 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ -}; - -#define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) - -#define GETU32(pt) (((uint32_t)(pt)[0] << 24) ^ ((uint32_t)(pt)[1] << 16) ^ ((uint32_t)(pt)[2] << 8) ^ ((uint32_t)(pt)[3])) -#define PUTU32(ct, st) { (ct)[0] = (uint8_t)((st) >> 24); (ct)[1] = (uint8_t)((st) >> 16); (ct)[2] = (uint8_t)((st) >> 8); (ct)[3] = (uint8_t)(st); } - -/** - * Expand the cipher key into the encryption key schedule. - * - * @return the number of rounds for the given cipher key size. - */ -int rijndaelKeySetupEnc(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], - int keyBits) { - int i = 0; - uint32_t temp; - - rk[0] = GETU32(cipherKey ); - rk[1] = GETU32(cipherKey + 4); - rk[2] = GETU32(cipherKey + 8); - rk[3] = GETU32(cipherKey + 12); - rk[4] = GETU32(cipherKey + 16); - rk[5] = GETU32(cipherKey + 20); - rk[6] = GETU32(cipherKey + 24); - rk[7] = GETU32(cipherKey + 28); - for (;;) { - temp = rk[ 7]; - rk[ 8] = rk[ 0] ^ - (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ - (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ - (Te4[(temp ) & 0xff] & 0x0000ff00) ^ - (Te4[(temp >> 24) ] & 0x000000ff) ^ - rcon[i]; - rk[ 9] = rk[ 1] ^ rk[ 8]; - rk[10] = rk[ 2] ^ rk[ 9]; - rk[11] = rk[ 3] ^ rk[10]; - if (++i == 7) { - return 14; - } - temp = rk[11]; - rk[12] = rk[ 4] ^ - (Te4[(temp >> 24) ] & 0xff000000) ^ - (Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^ - (Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^ - (Te4[(temp ) & 0xff] & 0x000000ff); - rk[13] = rk[ 5] ^ rk[12]; - rk[14] = rk[ 6] ^ rk[13]; - rk[15] = rk[ 7] ^ rk[14]; - - rk += 8; - } - return 0; -} - -void rijndaelEncrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, - const uint8_t pt[16], uint8_t ct[16]) { - uint32_t s0, s1, s2, s3, t0 = 0, t1 = 0, t2 = 0, t3 = 0; - - /* - * map byte array block to cipher state - * and add initial round key: - */ - s0 = GETU32(pt ) ^ rk[0]; - s1 = GETU32(pt + 4) ^ rk[1]; - s2 = GETU32(pt + 8) ^ rk[2]; - s3 = GETU32(pt + 12) ^ rk[3]; - /* round 1: */ - t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & - 0xff] ^ rk[ 4]; - t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & - 0xff] ^ rk[ 5]; - t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & - 0xff] ^ rk[ 6]; - t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & - 0xff] ^ rk[ 7]; - /* round 2: */ - s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & - 0xff] ^ rk[ 8]; - s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & - 0xff] ^ rk[ 9]; - s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & - 0xff] ^ rk[10]; - s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & - 0xff] ^ rk[11]; - /* round 3: */ - t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & - 0xff] ^ rk[12]; - t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & - 0xff] ^ rk[13]; - t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & - 0xff] ^ rk[14]; - t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & - 0xff] ^ rk[15]; - /* round 4: */ - s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & - 0xff] ^ rk[16]; - s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & - 0xff] ^ rk[17]; - s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & - 0xff] ^ rk[18]; - s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & - 0xff] ^ rk[19]; - /* round 5: */ - t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & - 0xff] ^ rk[20]; - t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & - 0xff] ^ rk[21]; - t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & - 0xff] ^ rk[22]; - t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & - 0xff] ^ rk[23]; - /* round 6: */ - s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & - 0xff] ^ rk[24]; - s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & - 0xff] ^ rk[25]; - s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & - 0xff] ^ rk[26]; - s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & - 0xff] ^ rk[27]; - /* round 7: */ - t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & - 0xff] ^ rk[28]; - t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & - 0xff] ^ rk[29]; - t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & - 0xff] ^ rk[30]; - t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & - 0xff] ^ rk[31]; - /* round 8: */ - s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & - 0xff] ^ rk[32]; - s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & - 0xff] ^ rk[33]; - s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & - 0xff] ^ rk[34]; - s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & - 0xff] ^ rk[35]; - /* round 9: */ - t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & - 0xff] ^ rk[36]; - t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & - 0xff] ^ rk[37]; - t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & - 0xff] ^ rk[38]; - t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & - 0xff] ^ rk[39]; - - /* round 10: */ - s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & - 0xff] ^ rk[40]; - s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & - 0xff] ^ rk[41]; - s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & - 0xff] ^ rk[42]; - s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & - 0xff] ^ rk[43]; - /* round 11: */ - t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & - 0xff] ^ rk[44]; - t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & - 0xff] ^ rk[45]; - t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & - 0xff] ^ rk[46]; - t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & - 0xff] ^ rk[47]; - - /* round 12: */ - s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & - 0xff] ^ rk[48]; - s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & - 0xff] ^ rk[49]; - s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & - 0xff] ^ rk[50]; - s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & - 0xff] ^ rk[51]; - /* round 13: */ - t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & - 0xff] ^ rk[52]; - t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & - 0xff] ^ rk[53]; - t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & - 0xff] ^ rk[54]; - t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & - 0xff] ^ rk[55]; - - - rk += Nr << 2; - /* - * apply last round and - * map cipher state to byte array block: - */ - s0 = - (Te4[(t0 >> 24) ] & 0xff000000) ^ - (Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ - (Te4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ - (Te4[(t3 ) & 0xff] & 0x000000ff) ^ - rk[0]; - PUTU32(ct, s0); - s1 = - (Te4[(t1 >> 24) ] & 0xff000000) ^ - (Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ - (Te4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ - (Te4[(t0 ) & 0xff] & 0x000000ff) ^ - rk[1]; - PUTU32(ct + 4, s1); - s2 = - (Te4[(t2 >> 24) ] & 0xff000000) ^ - (Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ - (Te4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ - (Te4[(t1 ) & 0xff] & 0x000000ff) ^ - rk[2]; - PUTU32(ct + 8, s2); - s3 = - (Te4[(t3 >> 24) ] & 0xff000000) ^ - (Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ - (Te4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ - (Te4[(t2 ) & 0xff] & 0x000000ff) ^ - rk[3]; - PUTU32(ct + 12, s3); -} diff --git a/crypto_kem/ledakemlt12/clean/aes256.h b/crypto_kem/ledakemlt12/clean/aes256.h deleted file mode 100644 index 0bdcce8f..00000000 --- a/crypto_kem/ledakemlt12/clean/aes256.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -/** - * AES-256 self contained implementation derived from : - * - * rijndael-alg-fst.h - * - * @version 3.0 (December 2000) - * - * Optimised ANSI C code for the Rijndael cipher (now AES) - * - * @author Vincent Rijmen - * @author Antoon Bosselaers - * @author Paulo Barreto - * - * originally placed in the public domain by the authors. - * - * - * - * This code is hereby placed in the public domain. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - - -#define NROUNDS 14 -#define KEYLEN_b 256 - -int rijndaelKeySetupEnc(uint32_t rk[/*4*(Nr + 1)*/], const uint8_t cipherKey[], - int keyBits); -void rijndaelEncrypt(const uint32_t rk[/*4*(Nr + 1)*/], int Nr, - const uint8_t pt[16], uint8_t ct[16]); diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/clean/bf_decoding.c index 2c9156cb..9d5fc7d9 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.c @@ -1,5 +1,6 @@ #include "bf_decoding.h" #include "gf2x_arith_mod_xPplusOne.h" + #include #include @@ -8,14 +9,10 @@ int thresholds[2] = {B0, (DV * M) / 2 + 1}; -int bf_decoding(DIGIT out[], // N0 polynomials - const POSITION_T HtrPosOnes[N0][DV], - const POSITION_T QtrPosOnes[N0][M], - DIGIT privateSyndrome[] // 1 polynomial - ) { - #if P < 64 -#error The circulant block size should exceed 64 - #endif +int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT out[], // N0 polynomials + const POSITION_T HtrPosOnes[N0][DV], + const POSITION_T QtrPosOnes[N0][M], + DIGIT privateSyndrome[]) { uint8_t unsatParityChecks[N0 * P]; POSITION_T currQBlkPos[M], currQBitPos[M]; @@ -80,4 +77,4 @@ int bf_decoding(DIGIT out[], // N0 polynomials } while (iteration < ITERATIONS_MAX && check < NUM_DIGITS_GF2X_ELEMENT); return (check == NUM_DIGITS_GF2X_ELEMENT); -} // end QdecodeSyndromeThresh_bitFlip_sparse +} diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.h b/crypto_kem/ledakemlt12/clean/bf_decoding.h index 1c8b5852..5b5bbeb2 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.h @@ -1,43 +1,17 @@ -#pragma once +#ifndef BF_DECODING_H +#define BF_DECODING_H + #include "qc_ldpc_parameters.h" #include "gf2x_limbs.h" -#define ITERATIONS_MAX (2) +/* Definitions for DFR level 2^-SL with SL=128 */ +#define ITERATIONS_MAX (2) +#define B0 (43) +#define T_BAR (4) -int bf_decoding(DIGIT err[], - const POSITION_T HtrPosOnes[N0][DV], - const POSITION_T QtrPosOnes[N0][M], // N0 vectors containing exp.s of Qtr ones - DIGIT privateSyndrome[] // 1 polynomial -- param. in/out - ); +int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], + const POSITION_T HtrPosOnes[N0][DV], + const POSITION_T QtrPosOnes[N0][M], // N0 vectors containing exp.s of Qtr ones + DIGIT privateSyndrome[]); -/* Definitions for DFR level 2^-64 */ -#if (CATEGORY == 1) && (N0 == 2) && (DFR_SL_LEVEL == 0) -#define B0 44 -#define T_BAR 4 -#endif - -#if ((CATEGORY == 2) || (CATEGORY == 3)) && (N0 == 2) && (DFR_SL_LEVEL == 0) -#define B0 64 -#define T_BAR 5 -#endif - -#if ((CATEGORY == 4) || (CATEGORY == 5)) && (N0 == 2) && (DFR_SL_LEVEL == 0) -#define B0 89 -#define T_BAR 6 -#endif - -/* Definitions for DFR level 2^-SL */ -#if (CATEGORY == 1) && (N0 == 2) && (DFR_SL_LEVEL == 1) -#define B0 43 -#define T_BAR 4 -#endif - -#if ((CATEGORY == 2) || (CATEGORY == 3)) && (N0 == 2) && (DFR_SL_LEVEL == 1) -#define B0 64 -#define T_BAR 5 -#endif - -#if ((CATEGORY == 4) || (CATEGORY == 5)) && (N0 == 2) && (DFR_SL_LEVEL == 1) -#define B0 88 -#define T_BAR 6 #endif diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt12/clean/dfr_test.c index d8d93d33..66582346 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.c +++ b/crypto_kem/ledakemlt12/clean/dfr_test.c @@ -1,20 +1,35 @@ -#include "qc_ldpc_parameters.h" -#include "gf2x_arith_mod_xPplusOne.h" #include "bf_decoding.h" +#include "dfr_test.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "qc_ldpc_parameters.h" #include -/*---------------------------------------------------------------------------*/ /* Tests if the current code attains the desired DFR. If that is the case, * computes the threshold for the second iteration of the decoder and stores - * it in the globally accessible vector*/ + * it in the globally accessible vector */ extern int thresholds[2]; -int DFR_test(POSITION_T LSparse[N0][DV * M]) { +int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { POSITION_T LSparse_loc[N0][DV * M]; + /* Gamma matrix: an N0 x N0 block circulant matrix with block size p + * gamma[a][b][c] stores the intersection of the first column of the a-th + * block of L with the c-th column of the b-th block of L */ + /* Gamma computation can be accelerated employing symmetry and QC properties */ + int gamma[N0][N0][P] = {{{0}}}; + uint32_t rotated_column[DV * M]; + int firstidx, secondidx, intersectionval; + + unsigned int gammaHist[N0][DV * M + 1] = {{0}}; + + int maxMut[N0], maxMutMinusOne[N0]; + int allBlockMaxSumst, allBlockMaxSumstMinusOne; + + unsigned int toAdd, histIdx; + /*transpose blocks of L, we need its columns */ for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { @@ -24,12 +39,7 @@ int DFR_test(POSITION_T LSparse[N0][DV * M]) { } quicksort(LSparse_loc[i], DV * M); } - /* Gamma matrix: an N0 x N0 block circulant matrix with block size p - * gamma[a][b][c] stores the intersection of the first column of the a-th - * block of L with the c-th column of the b-th block of L */ - /* Gamma computation can be accelerated employing symmetry and QC properties */ - int gamma[N0][N0][P] = {{{0}}}; - unsigned int rotated_column[DV * M]; + for (int i = 0; i < N0; i++ ) { for (int j = 0; j < N0; j++ ) { for (int k = 0; k < P; k++) { @@ -39,8 +49,8 @@ int DFR_test(POSITION_T LSparse[N0][DV * M]) { } quicksort(rotated_column, DV * M); /* compute the intersection amount */ - int firstidx = 0, secondidx = 0; - int intersectionval = 0; + firstidx = 0, secondidx = 0; + intersectionval = 0; while ( (firstidx < DV * M) && (secondidx < DV * M) ) { if ( LSparse_loc[i][firstidx] == rotated_column[secondidx] ) { intersectionval++; @@ -65,7 +75,6 @@ int DFR_test(POSITION_T LSparse[N0][DV * M]) { } } /* build histogram of values in gamma */ - unsigned int gammaHist[N0][DV * M + 1] = {{0}}; for (int i = 0; i < N0; i++ ) { for (int j = 0; j < N0; j++ ) { for (int k = 0; k < P; k++) { @@ -74,13 +83,11 @@ int DFR_test(POSITION_T LSparse[N0][DV * M]) { } } - int maxMut[N0], maxMutMinusOne[N0]; - int allBlockMaxSumst, allBlockMaxSumstMinusOne; for (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0; gammaBlockRowIdx++) { - int toAdd = T_BAR - 1; + toAdd = T_BAR - 1; maxMutMinusOne[gammaBlockRowIdx] = 0; - int histIdx = DV * M; + histIdx = DV * M; while ( (histIdx > 0) && (toAdd > 0)) { if (gammaHist[gammaBlockRowIdx][histIdx] > toAdd ) { maxMutMinusOne[gammaBlockRowIdx] += histIdx * toAdd; diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.h b/crypto_kem/ledakemlt12/clean/dfr_test.h index 6e20b5ce..da98b2f2 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.h +++ b/crypto_kem/ledakemlt12/clean/dfr_test.h @@ -1,3 +1,6 @@ -#pragma once +#ifndef DFR_TEST_H +#define DFR_TEST_H -int DFR_test(POSITION_T LSparse[N0][DV * M]); +int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); + +#endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/clean/gf2x_arith.c index 745fb0fc..ec3b396a 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.c @@ -2,27 +2,24 @@ #include // memset(...) #include -/*----------------------------------------------------------------------------*/ - /* allows the second operand to be shorter than the first */ /* the result should be as large as the first operand*/ -static inline void gf2x_add_asymm(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { - int delta = na - nb; - for (unsigned i = 0; i < delta; i++) { +static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], + const size_t na, const DIGIT A[], + const size_t nb, const DIGIT B[]) { + assert(nr >= na && na >= nb); + size_t i; + size_t delta = na - nb; + for (i = 0; i < delta; i++) { Res[i] = A[i]; } - for (unsigned i = 0; i < nb; i++) { + for (i = 0; i < nb; i++) { Res[i + delta] = A[i + delta] ^ B[i]; } -} // end gf2x_add - -/*----------------------------------------------------------------------------*/ +} /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ - -void right_bit_shift_n(const int length, DIGIT in[], const int amount) { +void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(const int length, DIGIT in[], const int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -35,12 +32,10 @@ void right_bit_shift_n(const int length, DIGIT in[], const int amount) { in[j] |= (in[j - 1] & mask) << (DIGIT_SIZE_b - amount); } in[j] >>= amount; -} // end right_bit_shift_n - -/*----------------------------------------------------------------------------*/ +} /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void left_bit_shift_n(const int length, DIGIT in[], const int amount) { +void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(const int length, DIGIT in[], const int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -53,13 +48,12 @@ void left_bit_shift_n(const int length, DIGIT in[], const int amount) { in[j] |= (in[j + 1] & mask) >> (DIGIT_SIZE_b - amount); } in[j] <<= amount; -} // end right_bit_shift_n +} -/*----------------------------------------------------------------------------*/ -void gf2x_mul_comb(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { +static void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { int i, j, k; DIGIT u, h; @@ -88,8 +82,6 @@ void gf2x_mul_comb(const int nr, DIGIT Res[], } } -/*----------------------------------------------------------------------------*/ - static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { DIGIT t = 0; for (int i = na - 1; i >= 0; i--) { @@ -102,48 +94,37 @@ static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { A[i] = t; t >>= DIGIT_SIZE_b - 1; } -} // end gf2x_exact_div_x_plus_one +} -/*---------------------------------------------------------------------------*/ #define MIN_KAR_DIGITS 20 -void gf2x_mul_Kar(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { +static void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { if (na < MIN_KAR_DIGITS || nb < MIN_KAR_DIGITS) { /* fall back to schoolbook */ - gf2x_mul_comb(nr, Res, na, A, nb, B); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(nr, Res, na, A, nb, B); return; } if (na % 2 == 0) { unsigned bih = na / 2; DIGIT middle[2 * bih], sumA[bih], sumB[bih]; - gf2x_add(bih, sumA, - bih, A, - bih, A + bih); - gf2x_add(bih, sumB, - bih, B, - bih, B + bih); - gf2x_mul_Kar(2 * bih, middle, - bih, sumA, - bih, sumB); - gf2x_mul_Kar(2 * bih, Res + 2 * bih, - bih, A + bih, - bih, B + bih); - gf2x_add(2 * bih, middle, - 2 * bih, middle, - 2 * bih, Res + 2 * bih); - gf2x_mul_Kar(2 * bih, Res, - bih, A, - bih, B); - gf2x_add(2 * bih, middle, - 2 * bih, middle, - 2 * bih, Res); - gf2x_add(2 * bih, Res + bih, - 2 * bih, Res + bih, - 2 * bih, middle); + gf2x_add(sumA, A, A + bih, bih); + gf2x_add(sumB, B, B + bih, bih); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, middle, + bih, sumA, + bih, sumB); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res + 2 * bih, + bih, A + bih, + bih, B + bih); + gf2x_add(middle, middle, Res + 2 * bih, 2 * bih); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res, + bih, A, + bih, B); + gf2x_add(middle, middle, Res, 2 * bih); + gf2x_add(Res + bih, Res + bih, middle, 2 * bih); } else { unsigned bih = na / 2 + 1; DIGIT middle[2 * bih], sumA[bih], sumB[bih]; @@ -153,42 +134,36 @@ void gf2x_mul_Kar(const int nr, DIGIT Res[], gf2x_add_asymm(bih, sumB, bih, B + bih - 1, bih - 1, B); - gf2x_mul_Kar(2 * bih, middle, - bih, sumA, - bih, sumB); - gf2x_mul_Kar(2 * bih, Res + 2 * (bih - 1), - bih, A + bih - 1, - bih, B + bih - 1); - gf2x_add(2 * bih, middle, - 2 * bih, middle, - 2 * bih, Res + 2 * (bih - 1)); - gf2x_mul_Kar(2 * (bih - 1), Res, - (bih - 1), A, - (bih - 1), B); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, middle, + bih, sumA, + bih, sumB); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res + 2 * (bih - 1), + bih, A + bih - 1, + bih, B + bih - 1); + gf2x_add(middle, middle, Res + 2 * (bih - 1), 2 * bih); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * (bih - 1), Res, + (bih - 1), A, + (bih - 1), B); gf2x_add_asymm(2 * bih, middle, 2 * bih, middle, 2 * (bih - 1), Res); - gf2x_add(2 * bih, Res + bih - 2, - 2 * bih, Res + bih - 2, - 2 * bih, middle); + gf2x_add(Res + bih - 2, Res + bih - 2, middle, 2 * bih); } } - -/*---------------------------------------------------------------------------*/ #define MIN_TOOM_DIGITS 35 -void gf2x_mul_TC3(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { if (na < MIN_TOOM_DIGITS || nb < MIN_TOOM_DIGITS) { - /* fall back to schoolbook */ - gf2x_mul_Kar(nr, Res, na, A, nb, B); + /* fall back to Karatsuba */ + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(nr, Res, na, A, nb, B); return; } - unsigned bih; //number of limbs for each part. + unsigned int bih; //number of limbs for each part. if (na % 3 == 0) { bih = na / 3; } else { @@ -197,9 +172,9 @@ void gf2x_mul_TC3(const int nr, DIGIT Res[], DIGIT u2[bih], u1[bih], u0[bih]; - int leading_slack = (3 - (na) % 3) % 3; + unsigned int leading_slack = (3 - (na) % 3) % 3; // printf("leading slack %d",leading_slack); - int i; + unsigned int i; for (i = 0; i < leading_slack ; i++) { u2[i] = 0; } @@ -231,42 +206,32 @@ void gf2x_mul_TC3(const int nr, DIGIT Res[], } DIGIT sum_u[bih]; /*bih digit wide*/ - gf2x_add(bih, sum_u, - bih, u0, - bih, u1); - gf2x_add(bih, sum_u, - bih, sum_u, - bih, u2); + gf2x_add(sum_u, u0, u1, bih); + gf2x_add(sum_u, sum_u, u2, bih); DIGIT sum_v[bih]; /*bih digit wide*/ - gf2x_add(bih, sum_v, - bih, v0, - bih, v1); - gf2x_add(bih, sum_v, - bih, sum_v, - bih, v2); + gf2x_add(sum_v, v0, v1, bih); + gf2x_add(sum_v, sum_v, v2, bih); DIGIT w1[2 * bih]; - gf2x_mul_TC3(2 * bih, w1, - bih, sum_u, - bih, sum_v); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w1, + bih, sum_u, + bih, sum_v); DIGIT u2_x2[bih + 1]; u2_x2[0] = 0; memcpy(u2_x2 + 1, u2, bih * DIGIT_SIZE_B); - left_bit_shift_n(bih + 1, u2_x2, 2); + PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, u2_x2, 2); DIGIT u1_x[bih + 1]; u1_x[0] = 0; memcpy(u1_x + 1, u1, bih * DIGIT_SIZE_B); - left_bit_shift_n(bih + 1, u1_x, 1); + PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, u1_x, 1); DIGIT u1_x1_u2_x2[bih + 1]; - gf2x_add(bih + 1, u1_x1_u2_x2, - bih + 1, u1_x, - bih + 1, u2_x2); + gf2x_add(u1_x1_u2_x2, u1_x, u2_x2, bih + 1); DIGIT temp_u_components[bih + 1]; gf2x_add_asymm(bih + 1, temp_u_components, @@ -276,17 +241,15 @@ void gf2x_mul_TC3(const int nr, DIGIT Res[], DIGIT v2_x2[bih + 1]; v2_x2[0] = 0; memcpy(v2_x2 + 1, v2, bih * DIGIT_SIZE_B); - left_bit_shift_n(bih + 1, v2_x2, 2); + PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, v2_x2, 2); DIGIT v1_x[bih + 1]; v1_x[0] = 0; memcpy(v1_x + 1, v1, bih * DIGIT_SIZE_B); - left_bit_shift_n(bih + 1, v1_x, 1); + PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, v1_x, 1); DIGIT v1_x1_v2_x2[bih + 1]; - gf2x_add(bih + 1, v1_x1_v2_x2, - bih + 1, v1_x, - bih + 1, v2_x2); + gf2x_add(v1_x1_v2_x2, v1_x, v2_x2, bih + 1); DIGIT temp_v_components[bih + 1]; gf2x_add_asymm(bih + 1, temp_v_components, @@ -294,9 +257,9 @@ void gf2x_mul_TC3(const int nr, DIGIT Res[], bih, sum_v); DIGIT w3[2 * bih + 2]; - gf2x_mul_TC3(2 * bih + 2, w3, - bih + 1, temp_u_components, - bih + 1, temp_v_components); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih + 2, w3, + bih + 1, temp_u_components, + bih + 1, temp_v_components); gf2x_add_asymm(bih + 1, u1_x1_u2_x2, bih + 1, u1_x1_u2_x2, @@ -306,36 +269,32 @@ void gf2x_mul_TC3(const int nr, DIGIT Res[], bih, v0); DIGIT w2[2 * bih + 2]; - gf2x_mul_TC3(2 * bih + 2, w2, - bih + 1, u1_x1_u2_x2, - bih + 1, v1_x1_v2_x2); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih + 2, w2, + bih + 1, u1_x1_u2_x2, + bih + 1, v1_x1_v2_x2); DIGIT w4[2 * bih]; - gf2x_mul_TC3(2 * bih, w4, - bih, u2, - bih, v2); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w4, + bih, u2, + bih, v2); DIGIT w0[2 * bih]; - gf2x_mul_TC3(2 * bih, w0, - bih, u0, - bih, v0); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w0, + bih, u0, + bih, v0); // Interpolation starts - gf2x_add(2 * bih + 2, w3, - 2 * bih + 2, w2, - 2 * bih + 2, w3); + gf2x_add(w3, w2, w3, 2 * bih + 2); gf2x_add_asymm(2 * bih + 2, w2, 2 * bih + 2, w2, 2 * bih, w0); - right_bit_shift_n(2 * bih + 2, w2, 1); - gf2x_add(2 * bih + 2, w2, - 2 * bih + 2, w2, - 2 * bih + 2, w3); + PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(2 * bih + 2, w2, 1); + gf2x_add(w2, w2, w3, 2 * bih + 2); // w2 + (w4 * x^3+1) = w2 + w4 + w4 << 3 DIGIT w4_x3_plus_1[2 * bih + 1]; w4_x3_plus_1[0] = 0; memcpy(w4_x3_plus_1 + 1, w4, 2 * bih * DIGIT_SIZE_B); - left_bit_shift_n(2 * bih + 1, w4_x3_plus_1, 3); + PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(2 * bih + 1, w4_x3_plus_1, 3); gf2x_add_asymm(2 * bih + 2, w2, 2 * bih + 2, w2, 2 * bih, w4); @@ -345,27 +304,21 @@ void gf2x_mul_TC3(const int nr, DIGIT Res[], gf2x_exact_div_x_plus_one(2 * bih + 2, w2); - gf2x_add(2 * bih, w1, - 2 * bih, w1, - 2 * bih, w0); + gf2x_add(w1, w1, w0, 2 * bih); gf2x_add_asymm(2 * bih + 2, w3, 2 * bih + 2, w3, 2 * bih, w1); - right_bit_shift_n(2 * bih + 2, w3, 1); + PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(2 * bih + 2, w3, 1); gf2x_exact_div_x_plus_one(2 * bih + 2, w3); - gf2x_add(2 * bih, w1, - 2 * bih, w1, - 2 * bih, w4); + gf2x_add(w1, w1, w4, 2 * bih); DIGIT w1_final[2 * bih + 2]; gf2x_add_asymm(2 * bih + 2, w1_final, 2 * bih + 2, w2, 2 * bih, w1); - gf2x_add(2 * bih + 2, w2, - 2 * bih + 2, w2, - 2 * bih + 2, w3); + gf2x_add(w2, w2, w3, 2 * bih + 2); // Result recombination starts here @@ -395,35 +348,32 @@ void gf2x_mul_TC3(const int nr, DIGIT Res[], Res[leastSignifDigitIdx - i] ^= w4[2 * bih - 1 - i]; } } - - -/*----------------------------------------------------------------------------*/ - -int gf2x_cmp(const unsigned lenA, const DIGIT A[], - const unsigned lenB, const DIGIT B[]) { - - int i; - unsigned lA = lenA, lB = lenB; - for (i = 0; i < lenA && A[i] == 0; i++) { - lA--; - } - for (i = 0; i < lenB && B[i] == 0; i++) { - lB--; - } - if (lA < lB) { - return -1; - } - if (lA > lB) { - return +1; - } - for (i = 0; i < lA; i++) { - if (A[i] > B[i]) { - return +1; - } - if (A[i] < B[i]) { - return -1; - } - } - return 0; - -} // end gf2x_cmp +// // Unused +// static int gf2x_cmp(const unsigned lenA, const DIGIT A[], +// const unsigned lenB, const DIGIT B[]) { +// +// int i; +// unsigned lA = lenA, lB = lenB; +// for (i = 0; i < lenA && A[i] == 0; i++) { +// lA--; +// } +// for (i = 0; i < lenB && B[i] == 0; i++) { +// lB--; +// } +// if (lA < lB) { +// return -1; +// } +// if (lA > lB) { +// return +1; +// } +// for (i = 0; i < lA; i++) { +// if (A[i] > B[i]) { +// return +1; +// } +// if (A[i] < B[i]) { +// return -1; +// } +// } +// return 0; +// +// } diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.h b/crypto_kem/ledakemlt12/clean/gf2x_arith.h index 08e15989..fedab74e 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.h @@ -1,8 +1,8 @@ -#pragma once +#ifndef GF2X_ARITH_H +#define GF2X_ARITH_H #include "gf2x_limbs.h" -/*----------------------------------------------------------------------------*/ /* * Elements of GF(2)[x] are stored in compact dense binary form. * @@ -41,40 +41,29 @@ * position[A_{1}] == n-2 * position[A_{0}] == n-1 */ -/*----------------------------------------------------------------------------*/ - -#define TC3 -#if defined(TC3) -#define GF2X_MUL gf2x_mul_TC3 -#else -#define GF2X_MUL gf2x_mul_comb -#endif +#define GF2X_MUL PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3 +// #define GF2X_MUL gf2x_mul_comb -/*----------------------------------------------------------------------------*/ +static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], const unsigned int nr) { -static inline void gf2x_add(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { - for (unsigned i = 0; i < nr; i++) { + unsigned int i; + for (i = 0; i < nr; i++) { Res[i] = A[i] ^ B[i]; } -} // end gf2x_add - -/*----------------------------------------------------------------------------*/ +} void GF2X_MUL(const int nr, DIGIT Res[], const int na, const DIGIT A[], - const int nb, const DIGIT B[] - ); + const int nb, const DIGIT B[]); -int gf2x_cmp(const unsigned lenA, const DIGIT A[], - const unsigned lenB, const DIGIT B[]); /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void right_bit_shift_n(const int length, DIGIT in[], const int amount); +void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(const int length, DIGIT in[], const int amount); /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void left_bit_shift_n(const int length, DIGIT in[], const int amount); -/*----------------------------------------------------------------------------*/ +void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(const int length, DIGIT in[], const int amount); + + +#endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index f8858d95..92387b97 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -1,12 +1,11 @@ #include "gf2x_arith_mod_xPplusOne.h" #include "rng.h" + #include // memcpy(...), memset(...) #include -#include -/*----------------------------------------------------------------------------*/ -void gf2x_mod(DIGIT out[], - const int nin, const DIGIT in[]) { +static void gf2x_mod(DIGIT out[], + const int nin, const DIGIT in[]) { long int i, j, posTrailingBit, maskOffset; DIGIT mask, aux[nin]; @@ -50,12 +49,9 @@ void gf2x_mod(DIGIT out[], out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[nin - 1 - i]; } -} // end gf2x_mod +} -/*----------------------------------------------------------------------------*/ - -static -void left_bit_shift(const int length, DIGIT in[]) { +static void left_bit_shift(const int length, DIGIT in[]) { int j; for (j = 0; j < length - 1; j++) { @@ -63,12 +59,9 @@ void left_bit_shift(const int length, DIGIT in[]) { in[j] |= in[j + 1] >> (DIGIT_SIZE_b - 1); } in[j] <<= 1; -} // end left_bit_shift +} -/*----------------------------------------------------------------------------*/ - -static -void right_bit_shift(const int length, DIGIT in[]) { +static void right_bit_shift(const int length, DIGIT in[]) { int j; for (j = length - 1; j > 0 ; j--) { @@ -76,12 +69,11 @@ void right_bit_shift(const int length, DIGIT in[]) { in[j] |= (in[j - 1] & (DIGIT)0x01) << (DIGIT_SIZE_b - 1); } in[j] >>= 1; -} // end right_bit_shift +} + -/*----------------------------------------------------------------------------*/ /* shifts by whole digits */ -static inline -void left_DIGIT_shift_n(const int length, DIGIT in[], int amount) { +static inline void left_DIGIT_shift_n(const int length, DIGIT in[], int amount) { int j; for (j = 0; (j + amount) < length; j++) { in[j] = in[j + amount]; @@ -89,21 +81,17 @@ void left_DIGIT_shift_n(const int length, DIGIT in[], int amount) { for (; j < length; j++) { in[j] = (DIGIT)0; } -} // end left_bit_shift_n +} + -/*----------------------------------------------------------------------------*/ /* may shift by an arbitrary amount*/ - -void left_bit_shift_wide_n(const int length, DIGIT in[], int amount) { +static void left_bit_shift_wide_n(const int length, DIGIT in[], int amount) { left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); - left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); -} // end left_bit_shift_n - -/*----------------------------------------------------------------------------*/ + PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); +} #if (defined(DIGIT_IS_UINT8) || defined(DIGIT_IS_UINT16)) -static -uint8_t byte_reverse_with_less32bitDIGIT(uint8_t b) { +static uint8_t byte_reverse_with_less32bitDIGIT(uint8_t b) { uint8_t r = b; int s = (sizeof(b) << 3) - 1; for (b >>= 1; b; b >>= 1) { @@ -113,30 +101,25 @@ uint8_t byte_reverse_with_less32bitDIGIT(uint8_t b) { } r <<= s; return r; -} // end byte_reverse_less32bitDIGIT +} #endif #if defined(DIGIT_IS_UINT32) -static -uint8_t byte_reverse_with_32bitDIGIT(uint8_t b) { +static uint8_t byte_reverse_with_32bitDIGIT(uint8_t b) { b = ( (b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU) ) * 0x10101LU >> 16; return b; -} // end byte_reverse_32bitDIGIT +} #endif #if defined(DIGIT_IS_UINT64) -static -uint8_t byte_reverse_with_64bitDIGIT(uint8_t b) { +static uint8_t byte_reverse_with_64bitDIGIT(uint8_t b) { b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023; return b; -} // end byte_reverse_64bitDIGIT +} #endif -/*----------------------------------------------------------------------------*/ - -static -DIGIT reverse_digit(const DIGIT b) { +static DIGIT reverse_digit(const DIGIT b) { int i; union toReverse_t { uint8_t inByte[DIGIT_SIZE_B]; @@ -166,15 +149,11 @@ DIGIT reverse_digit(const DIGIT b) { with this CPU word bitsize !!! " #endif return toReverse.digitValue; -} // end reverse_digit +} - -/*----------------------------------------------------------------------------*/ - -void gf2x_transpose_in_place(DIGIT A[]) { +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { /* it keeps the lsb in the same position and - * inverts the sequence of the remaining bits - */ + * inverts the sequence of the remaining bits */ DIGIT mask = (DIGIT)0x1; DIGIT rev1, rev2, a00; @@ -205,14 +184,12 @@ void gf2x_transpose_in_place(DIGIT A[]) { } if (slack_bits_amount) { - right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); + PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); } A[NUM_DIGITS_GF2X_ELEMENT - 1] = (A[NUM_DIGITS_GF2X_ELEMENT - 1] & (~mask)) | a00; -} // end transpose_in_place +} -/*----------------------------------------------------------------------------*/ - -void rotate_bit_left(DIGIT in[]) { /* equivalent to x * in(x) mod x^P+1 */ +static void rotate_bit_left(DIGIT in[]) { /* equivalent to x * in(x) mod x^P+1 */ DIGIT mask, rotated_bit; @@ -234,13 +211,9 @@ void rotate_bit_left(DIGIT in[]) { /* equivalent to x * in(x) mod x^P+1 */ } in[NUM_DIGITS_GF2X_ELEMENT - 1] |= rotated_bit; -} // end rotate_bit_left +} - - -/*----------------------------------------------------------------------------*/ - -void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ +static void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ DIGIT rotated_bit = in[NUM_DIGITS_GF2X_ELEMENT - 1] & ((DIGIT)0x1); right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); @@ -255,23 +228,18 @@ void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ rotated_bit = rotated_bit << (DIGIT_SIZE_b - 1); } in[0] |= rotated_bit; -} // end rotate_bit_right +} -/*----------------------------------------------------------------------------*/ - -static -void gf2x_swap(const int length, - DIGIT f[], - DIGIT s[]) { +static void gf2x_swap(const int length, + DIGIT f[], + DIGIT s[]) { DIGIT t; for (int i = length - 1; i >= 0; i--) { t = f[i]; f[i] = s[i]; s[i] = t; } -} // end gf2x_swap - -/*----------------------------------------------------------------------------*/ +} /* * Optimized extended GCD algorithm to compute the multiplicative inverse of @@ -291,14 +259,14 @@ void gf2x_swap(const int length, * */ -int gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ +int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ int i; long int delta = 0; - alignas(32) DIGIT u[NUM_DIGITS_GF2X_ELEMENT] = {0}, - v[NUM_DIGITS_GF2X_ELEMENT] = {0}, - s[NUM_DIGITS_GF2X_MODULUS] = {0}, - f[NUM_DIGITS_GF2X_MODULUS] = {0}; + DIGIT u[NUM_DIGITS_GF2X_ELEMENT] = {0}; + DIGIT v[NUM_DIGITS_GF2X_ELEMENT] = {0}; + DIGIT s[NUM_DIGITS_GF2X_MODULUS] = {0}; + DIGIT f[NUM_DIGITS_GF2X_MODULUS] = {0}; // alignas(32)? DIGIT mask; u[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x1; @@ -332,9 +300,7 @@ int gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ delta += 1; } else { if ( (s[0] & mask) != 0) { - gf2x_add(NUM_DIGITS_GF2X_MODULUS, s, - NUM_DIGITS_GF2X_MODULUS, s, - NUM_DIGITS_GF2X_MODULUS, f); + gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); gf2x_mod_add(v, v, u); } left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); @@ -355,11 +321,9 @@ int gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ } return (delta == 0); -} // end gf2x_mod_inverse +} -/*----------------------------------------------------------------------------*/ - -void gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT]; GF2X_MUL(2 * NUM_DIGITS_GF2X_ELEMENT, aux, @@ -367,16 +331,15 @@ void gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { NUM_DIGITS_GF2X_ELEMENT, B); gf2x_mod(Res, 2 * NUM_DIGITS_GF2X_ELEMENT, aux); -} // end gf2x_mod_mul - -/*----------------------------------------------------------------------------*/ +} /*PRE: the representation of the sparse coefficients is sorted in increasing order of the coefficients themselves */ -void gf2x_mod_mul_dense_to_sparse(DIGIT Res[], - const DIGIT dense[], - POSITION_T sparse[], - unsigned int nPos) { +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse( + DIGIT Res[], + const DIGIT dense[], + POSITION_T sparse[], unsigned int nPos) { + DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT] = {0x00}; DIGIT resDouble[2 * NUM_DIGITS_GF2X_ELEMENT] = {0x00}; memcpy(aux + NUM_DIGITS_GF2X_ELEMENT, dense, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); @@ -390,21 +353,16 @@ void gf2x_mod_mul_dense_to_sparse(DIGIT Res[], for (unsigned int i = 1; i < nPos; i++) { if (sparse[i] != INVALID_POS_VALUE) { left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); - gf2x_add(2 * NUM_DIGITS_GF2X_ELEMENT, resDouble, - 2 * NUM_DIGITS_GF2X_ELEMENT, aux, - 2 * NUM_DIGITS_GF2X_ELEMENT, resDouble); + gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); } } } gf2x_mod(Res, 2 * NUM_DIGITS_GF2X_ELEMENT, resDouble); -} // end gf2x_mod_mul +} -/*----------------------------------------------------------------------------*/ - - -void gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { POSITION_T t; int i = 0, j; @@ -424,21 +382,16 @@ void gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { A[i] = t; } -} // end gf2x_transpose_in_place_sparse +} -/*----------------------------------------------------------------------------*/ +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], + size_t sizeA, const POSITION_T A[], + size_t sizeB, const POSITION_T B[]) { -void gf2x_mod_mul_sparse(int - sizeR, /*number of ones in the result, max sizeA*sizeB */ - POSITION_T Res[], - int sizeA, /*number of ones in A*/ - const POSITION_T A[], - int sizeB, /*number of ones in B*/ - const POSITION_T B[]) { /* compute all the coefficients, filling invalid positions with P*/ - unsigned lastFilledPos = 0; - for (int i = 0 ; i < sizeA ; i++) { - for (int j = 0 ; j < sizeB ; j++) { + size_t lastFilledPos = 0; + for (size_t i = 0 ; i < sizeA ; i++) { + for (size_t j = 0 ; j < sizeB ; j++) { uint32_t prod = ((uint32_t) A[i]) + ((uint32_t) B[j]); prod = ( (prod >= P) ? prod - P : prod); if ((A[i] != INVALID_POS_VALUE) && @@ -458,8 +411,8 @@ void gf2x_mod_mul_sparse(int /* eliminate duplicates */ POSITION_T lastReadPos = Res[0]; int duplicateCount; - int write_idx = 0; - int read_idx = 0; + size_t write_idx = 0; + size_t read_idx = 0; while (read_idx < sizeR && Res[read_idx] != INVALID_POS_VALUE) { lastReadPos = Res[read_idx]; read_idx++; @@ -477,18 +430,14 @@ void gf2x_mod_mul_sparse(int for (; write_idx < sizeR; write_idx++) { Res[write_idx] = INVALID_POS_VALUE; } -} // end gf2x_mod_mul_sparse +} - -/*----------------------------------------------------------------------------*/ /* the implementation is safe even in case A or B alias with the result */ /* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ -void gf2x_mod_add_sparse(int sizeR, - POSITION_T Res[], - int sizeA, - POSITION_T A[], - int sizeB, - POSITION_T B[]) { +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse( + int sizeR, POSITION_T Res[], + int sizeA, POSITION_T A[], + int sizeB, POSITION_T B[]) { POSITION_T tmpRes[sizeR]; int idxA = 0, idxB = 0, idxR = 0; @@ -530,25 +479,21 @@ void gf2x_mod_add_sparse(int sizeR, } memcpy(Res, tmpRes, sizeof(POSITION_T)*sizeR); -} // end gf2x_mod_add_sparse - -/*----------------------------------------------------------------------------*/ +} /* Return a uniform random value in the range 0..n-1 inclusive, * applying a rejection sampling strategy and exploiting as a random source * the NIST seedexpander seeded with the proper key. * Assumes that the maximum value for the range n is 2^32-1 */ -static -int rand_range(const int n, const int logn, AES_XOF_struct *seed_expander_ctx) { - +static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct *seed_expander_ctx) { unsigned long required_rnd_bytes = (logn + 7) / 8; unsigned char rnd_char_buffer[4]; uint32_t rnd_value; uint32_t mask = ( (uint32_t)1 << logn) - 1; do { - seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); /* obtain an endianness independent representation of the generated random bytes into an unsigned integer */ rnd_value = ((uint32_t)rnd_char_buffer[3] << 24) + @@ -559,24 +504,21 @@ int rand_range(const int n, const int logn, AES_XOF_struct *seed_expander_ctx) { } while (rnd_value >= n); return rnd_value; -} // end rand_range +} - - -/*----------------------------------------------------------------------------*/ /* Obtains fresh randomness and seed-expands it until all the required positions * for the '1's in the circulant block are obtained */ - -void rand_circulant_sparse_block(POSITION_T *pos_ones, - const int countOnes, - AES_XOF_struct *seed_expander_ctx) { +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, + const int countOnes, + AES_XOF_struct *seed_expander_ctx) { int duplicated, placedOnes = 0; + uint32_t p; while (placedOnes < countOnes) { - int p = rand_range(NUM_BITS_GF2X_ELEMENT, - BITS_TO_REPRESENT(P), - seed_expander_ctx); + p = rand_range(NUM_BITS_GF2X_ELEMENT, + BITS_TO_REPRESENT(P), + seed_expander_ctx); duplicated = 0; for (int j = 0; j < placedOnes; j++) if (pos_ones[j] == p) { duplicated = 1; @@ -586,14 +528,11 @@ void rand_circulant_sparse_block(POSITION_T *pos_ones, placedOnes++; } } -} // rand_circulant_sparse_block +} -/*----------------------------------------------------------------------------*/ - - -void rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], - const int countOnes, - AES_XOF_struct *seed_expander_ctx) { +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + const int countOnes, + AES_XOF_struct *seed_expander_ctx) { int rndPos[countOnes], duplicated, counter = 0; memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); @@ -618,6 +557,4 @@ void rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT] ( (DIGIT) 1)); } -} // end rand_circulant_blocks_sequence - -/*----------------------------------------------------------------------------*/ +} diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h index acc0919c..9e08ffc0 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef GF2X_ARITH_MOD_XPLUSONE_H +#define GF2X_ARITH_MOD_XPLUSONE_H #include "gf2x_limbs.h" #include "qc_ldpc_parameters.h" @@ -6,8 +7,6 @@ #include "gf2x_arith.h" #include "rng.h" -/*----------------------------------------------------------------------------*/ - #define NUM_BITS_GF2X_ELEMENT (P) #define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) #define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ( (P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1 ) @@ -58,63 +57,45 @@ ) \ ) -/*----------------------------------------------------------------------------*/ - - - -/*----------------------------------------------------------------------------*/ static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { dest[i] = in[i]; } -} // end gf2x_copy +} -/*---------------------------------------------------------------------------*/ +/* returns the coefficient of the x^exponent term as the LSB of a digit */ +static inline DIGIT gf2x_get_coeff(const DIGIT poly[], const unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; +} -void gf2x_mod(DIGIT out[], - const int nin, const DIGIT in[]); /* out(x) = in(x) mod x^P+1 */ +/* sets the coefficient of the x^exponent term as the LSB of a digit */ +static inline void gf2x_set_coeff(DIGIT poly[], const unsigned int exponent, DIGIT value) { + int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + int digitIdx = straightIdx / DIGIT_SIZE_b; -/*---------------------------------------------------------------------------*/ + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; -void gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); + /* clear given coefficient */ + DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] & mask; + poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << + (DIGIT_SIZE_b - 1 - inDigitIdx)); +} -/*---------------------------------------------------------------------------*/ +/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ +static inline void gf2x_toggle_coeff(DIGIT poly[], const unsigned int exponent) { + int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; -static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { - gf2x_add(NUM_DIGITS_GF2X_ELEMENT, Res, - NUM_DIGITS_GF2X_ELEMENT, A, - NUM_DIGITS_GF2X_ELEMENT, B); -} // end gf2x_mod_add - -/*----------------------------------------------------------------------------*/ - -/* - * Optimized extended GCD algorithm to compute the multiplicative inverse of - * a non-zero element in GF(2)[x] mod x^P+1, in polyn. representation. - * - * H. Brunner, A. Curiger, and M. Hofstetter. 1993. - * On Computing Multiplicative Inverses in GF(2^m). - * IEEE Trans. Comput. 42, 8 (August 1993), 1010-1015. - * DOI=http://dx.doi.org/10.1109/12.238496 - * - * - * Henri Cohen, Gerhard Frey, Roberto Avanzi, Christophe Doche, Tanja Lange, - * Kim Nguyen, and Frederik Vercauteren. 2012. - * Handbook of Elliptic and Hyperelliptic Curve Cryptography, - * Second Edition (2nd ed.). Chapman & Hall/CRC. - * (Chapter 11 -- Algorithm 11.44 -- pag 223) - * - */ -int gf2x_mod_inverse(DIGIT out[], const DIGIT in[]);/* ret. 1 if inv. exists */ - -/*---------------------------------------------------------------------------*/ - -void gf2x_transpose_in_place(DIGIT - A[]); /* in place bit-transp. of a(x) % x^P+1 * - * e.g.: a3 a2 a1 a0 --> a1 a2 a3 a0 */ - -/*---------------------------------------------------------------------------*/ + /* clear given coefficient */ + DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] ^ mask; +} /* population count for a single polynomial */ static inline int population_count(DIGIT upc[]) { @@ -139,90 +120,48 @@ with this CPU word bitsize !!! " return ret; } // end population_count -/*--------------------------------------------------------------------------*/ - -/* returns the coefficient of the x^exponent term as the LSB of a digit */ -static inline -DIGIT gf2x_get_coeff(const DIGIT poly[], const unsigned int exponent) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; +static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); } -/*--------------------------------------------------------------------------*/ +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); -/* sets the coefficient of the x^exponent term as the LSB of a digit */ -static inline -void gf2x_set_coeff(DIGIT poly[], const unsigned int exponent, DIGIT value) { - int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - int digitIdx = straightIdx / DIGIT_SIZE_b; +int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]);/* ret. 1 if inv. exists */ - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; +/* in place bit-transp. of a(x) % x^P+1, e.g.: a3 a2 a1 a0 --> a1 a2 a3 a0 */ +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]); - /* clear given coefficient */ - DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); - poly[digitIdx] = poly[digitIdx] & mask; - poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << - (DIGIT_SIZE_b - 1 - inDigitIdx)); -} -/*--------------------------------------------------------------------------*/ +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block( + POSITION_T *pos_ones, + const int countOnes, + AES_XOF_struct *seed_expander_ctx); -/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ -static inline -void gf2x_toggle_coeff(DIGIT poly[], const unsigned int exponent) { +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( + DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + const int countOnes, + AES_XOF_struct *seed_expander_ctx); - int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse( + int sizeR, POSITION_T Res[], + int sizeA, POSITION_T A[], + int sizeB, POSITION_T B[]); - /* clear given coefficient */ - DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); - poly[digitIdx] = poly[digitIdx] ^ mask; -} -/*--------------------------------------------------------------------------*/ +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse( + int sizeA, + POSITION_T A[]); -void rand_circulant_sparse_block(POSITION_T *pos_ones, - const int countOnes, - AES_XOF_struct *seed_expander_ctx); -/*--------------------------------------------------------------------------*/ +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse( + size_t sizeR, POSITION_T Res[], + size_t sizeA, const POSITION_T A[], + size_t sizeB, const POSITION_T B[]); -void rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], - const int countOnes, - AES_XOF_struct *seed_expander_ctx - ); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse( + DIGIT Res[], + const DIGIT dense[], + POSITION_T sparse[], + unsigned int nPos); -/*---------------------------------------------------------------------------*/ - - -void gf2x_mod_add_sparse(int sizeR, - POSITION_T Res[], - int sizeA, - POSITION_T A[], - int sizeB, - POSITION_T B[]); - -/*----------------------------------------------------------------------------*/ - -void gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); - -/*----------------------------------------------------------------------------*/ - -void gf2x_mod_mul_sparse(int - sizeR, /*number of ones in the result, max sizeA*sizeB */ - POSITION_T Res[], - int sizeA, /*number of ones in A*/ - const POSITION_T A[], - int sizeB, /*number of ones in B*/ - const POSITION_T B[]); -/*----------------------------------------------------------------------------*/ -void gf2x_mod_mul_dense_to_sparse(DIGIT Res[], - const DIGIT dense[], - POSITION_T sparse[], - unsigned int nPos); -/*----------------------------------------------------------------------------*/ -static inline -int partition (POSITION_T arr[], int lo, int hi) { +static inline int partition(POSITION_T arr[], int lo, int hi) { POSITION_T x = arr[hi]; POSITION_T tmp; int i = (lo - 1); @@ -239,12 +178,9 @@ int partition (POSITION_T arr[], int lo, int hi) { arr[hi] = tmp; return i + 1; -} // end partition +} -/*----------------------------------------------------------------------------*/ - -static inline -void quicksort(POSITION_T Res[], unsigned int sizeR) { +static inline void quicksort(POSITION_T Res[], unsigned int sizeR) { /* sort the result */ int stack[sizeR]; int hi, lo, pivot, tos = -1; @@ -265,4 +201,4 @@ void quicksort(POSITION_T Res[], unsigned int sizeR) { } } -/*---------------------------------------------------------------------------*/ +#endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h index 7e331084..429a3cf9 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h @@ -1,61 +1,65 @@ -#pragma once - -/*----------------------------------------------------------------------------*/ +#ifndef GF2X_LIMBS_H +#define GF2X_LIMBS_H #include #include #include #include "qc_ldpc_parameters.h" -/*----------------------------------------------------------------------------*/ -#define LITTLE_ENDIAN /*----------------------------------------------------------------------------*/ /* limb size definitions for the multi-precision GF(2^x) library */ /*----------------------------------------------------------------------------*/ -#ifndef CPU_WORD_BITS -typedef size_t DIGIT; -#define DIGIT_MAX SIZE_MAX -#else -// gcc -DCPU_WORD_BITS=64 ... -#define CAT(a, b, c) PRIMITIVE_CAT(a, b, c) -#define PRIMITIVE_CAT(a, b, c) a ## b ## c +// #ifndef CPU_WORD_BITS +// typedef size_t DIGIT; +// #define DIGIT_MAX SIZE_MAX +// #else +// // gcc -DCPU_WORD_BITS=64 ... +// #define CAT(a, b, c) PRIMITIVE_CAT(a, b, c) +// #define PRIMITIVE_CAT(a, b, c) a ## b ## c +// +// typedef CAT( uint, CPU_WORD_BITS, _t ) DIGIT; +// #define DIGIT_MAX (CAT(UINT, CPU_WORD_BITS, _MAX)) +// #endif +// +// #if (DIGIT_MAX == ULLONG_MAX) +// #define DIGIT_IS_ULLONG +// #elif (DIGIT_MAX == ULONG_MAX) +// #define DIGIT_IS_ULONG +// #elif (DIGIT_MAX == UINT_MAX) +// #define DIGIT_IS_UINT +// #elif (DIGIT_MAX == UCHAR_MAX) +// #define DIGIT_IS_UCHAR +// #else +// #error "unable to find the type of CPU_WORD_BITS" +// #endif +// +// #if (DIGIT_MAX == UINT64_MAX) +// #define DIGIT_IS_UINT64 +// #define DIGIT_SIZE_B 8 +// #elif (DIGIT_MAX == UINT32_MAX) +// #define DIGIT_IS_UINT32 +// #define DIGIT_SIZE_B 4 +// #elif (DIGIT_MAX == UINT16_MAX) +// #define DIGIT_IS_UINT16 +// #define DIGIT_SIZE_B 2 +// #elif (DIGIT_MAX == UINT8_MAX) +// #define DIGIT_IS_UINT8 +// #define DIGIT_SIZE_B 1 +// #else +// #error "unable to find the bitsize of size_t" +// #endif -typedef CAT( uint, CPU_WORD_BITS, _t ) DIGIT; -#define DIGIT_MAX (CAT(UINT, CPU_WORD_BITS, _MAX)) -#endif +// #define LITTLE_ENDIAN -#if (DIGIT_MAX == ULLONG_MAX) -#define DIGIT_IS_ULLONG -#elif (DIGIT_MAX == ULONG_MAX) -#define DIGIT_IS_ULONG -#elif (DIGIT_MAX == UINT_MAX) -#define DIGIT_IS_UINT -#elif (DIGIT_MAX == UCHAR_MAX) -#define DIGIT_IS_UCHAR -#else -#error "unable to find the type of CPU_WORD_BITS" -#endif - -#if (DIGIT_MAX == UINT64_MAX) +typedef uint64_t DIGIT; #define DIGIT_IS_UINT64 -#define DIGIT_SIZE_B 8 -#elif (DIGIT_MAX == UINT32_MAX) -#define DIGIT_IS_UINT32 -#define DIGIT_SIZE_B 4 -#elif (DIGIT_MAX == UINT16_MAX) -#define DIGIT_IS_UINT16 -#define DIGIT_SIZE_B 2 -#elif (DIGIT_MAX == UINT8_MAX) -#define DIGIT_IS_UINT8 -#define DIGIT_SIZE_B 1 -#else -#error "unable to find the bitsize of size_t" +#define DIGIT_IS_ULLONG +#define DIGIT_SIZE_B (8) +#define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) + +#define POSITION_T uint32_t + #endif - -#define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) - -#define POSITION_T uint32_t -/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt12/clean/kem.c index f9903718..41f897ce 100644 --- a/crypto_kem/ledakemlt12/clean/kem.c +++ b/crypto_kem/ledakemlt12/clean/kem.c @@ -1,65 +1,58 @@ -#include "niederreiter_keygen.h" -#include "niederreiter_encrypt.h" -#include "niederreiter_decrypt.h" +#include "api.h" +#include "niederreiter.h" +#include "randombytes.h" #include "rng.h" -#include "sha3.h" -#include -/* Generates a keypair - pk is the public key and sk is the secret key. */ -int crypto_kem_keypair( unsigned char *pk, - unsigned char *sk ) { +#include + +/* Generates a keypair - pk is the public key and sk is the secret key. */ +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { AES_XOF_struct niederreiter_keys_expander; - randombytes( ((privateKeyNiederreiter_t *)sk)->prng_seed, - TRNG_BYTE_LENGTH); - seedexpander_from_trng(&niederreiter_keys_expander, - ((privateKeyNiederreiter_t *)sk)->prng_seed); - key_gen_niederreiter((publicKeyNiederreiter_t *) pk, - (privateKeyNiederreiter_t *) sk, - &niederreiter_keys_expander); + randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, + ((privateKeyNiederreiter_t *)sk)->prng_seed); + PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen((publicKeyNiederreiter_t *) pk, + (privateKeyNiederreiter_t *) sk, + &niederreiter_keys_expander); return 0; } /* Encrypt - pk is the public key, ct is a key encapsulation message (ciphertext), ss is the shared secret.*/ -int crypto_kem_enc( unsigned char *ct, - unsigned char *ss, - const unsigned char *pk ) { - +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc( unsigned char *ct, unsigned char *ss, const unsigned char *pk) { AES_XOF_struct niederreiter_encap_key_expander; unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; - randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); - seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); - DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; - rand_circulant_blocks_sequence(error_vector, - NUM_ERRORS_T, - &niederreiter_encap_key_expander); - HASH_FUNCTION((const unsigned char *) error_vector, // input - (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), // input Length - ss); + randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); - encrypt_niederreiter((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); + PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(error_vector, + NUM_ERRORS_T, + &niederreiter_encap_key_expander); + + HASH_FUNCTION(ss, (const uint8_t *) error_vector, // input + (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); // input length + + PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); return 0; } /* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private key, ss is the shared secret */ - -int crypto_kem_dec( unsigned char *ss, - const unsigned char *ct, - const unsigned char *sk ) { +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, + const unsigned char *ct, + const unsigned char *sk ) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; - int decode_ok = decrypt_niederreiter(decoded_error_vector, - (privateKeyNiederreiter_t *)sk, - (DIGIT *)ct); - HASH_FUNCTION((const unsigned char *) decoded_error_vector, - (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), - ss); + int decode_ok = PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, + (privateKeyNiederreiter_t *)sk, + (DIGIT *)ct); + HASH_FUNCTION(ss, (const unsigned char *) decoded_error_vector, + (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); if (decode_ok == 1) { return 0; } - return 1; + return -1; } diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c new file mode 100644 index 00000000..ab084096 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -0,0 +1,215 @@ +#include "niederreiter.h" +#include "H_Q_matrices_generation.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "rng.h" +#include "dfr_test.h" +#include "bf_decoding.h" +#include "qc_ldpc_parameters.h" + +#include + + +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *const pk, + privateKeyNiederreiter_t *const sk, + AES_XOF_struct *keys_expander) { + // sequence of N0 circ block matrices (p x p): Hi + + POSITION_T HPosOnes[N0][DV]; + POSITION_T HtrPosOnes[N0][DV]; + /* Sparse representation of the transposed circulant matrix H, + with weight DV. Each index contains the position of a '1' digit in the + corresponding Htr block */ + + /* Sparse representation of the matrix (Q). + A matrix containing the positions of the ones in the circulant + blocks of Q. Each row contains the position of the + ones of all the blocks of a row of Q as exponent+ + P*block_position */ + POSITION_T QPosOnes[N0][M]; + + /*Rejection-sample for a full L*/ + POSITION_T LPosOnes[N0][DV * M]; + int is_L_full; + int isDFRok; + sk->rejections = (int8_t) 0; + do { + PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, + HtrPosOnes, + keys_expander); + + PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, + keys_expander); + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + LPosOnes[i][j] = INVALID_POS_VALUE; + } + } + + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0] = {0}; + for (int colQ = 0; colQ < N0; colQ++) { + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + DV, HPosOnes[i], + qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + DV * M, LPosOnes[colQ], + DV * M, auxPosOnes); + processedQOnes[i] += qBlockWeights[i][colQ]; + } + } + is_L_full = 1; + for (int i = 0; i < N0; i++) { + is_L_full = is_L_full && (LPosOnes[i][DV * M - 1] != INVALID_POS_VALUE); + } + sk->rejections = sk->rejections + 1; + if (is_L_full) { + isDFRok = PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(LPosOnes); + } + } while (!is_L_full || !isDFRok); + sk->rejections = sk->rejections - 1; + + DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + for (int j = 0; j < DV * M; j++) { + if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { + gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + } + } + DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); + for (int i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + Ln0Inv, + LPosOnes[i], + DV * M); + } + + for (int i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + + +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT syndrome[], // 1 polynomial + const publicKeyNiederreiter_t *const pk, + const DIGIT err[]) { // N0 polynomials + int i; + DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; + + memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(saux, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + err + i * NUM_DIGITS_GF2X_ELEMENT + ); + gf2x_mod_add(syndrome, syndrome, saux); + } // end for + gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); +} + + +int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT err[], // N0 circ poly + const privateKeyNiederreiter_t *const sk, + const DIGIT syndrome[]) { + + AES_XOF_struct niederreiter_decrypt_expander; + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, + sk->prng_seed); + + // sequence of N0 circ block matrices (p x p): + POSITION_T HPosOnes[N0][DV]; + POSITION_T HtrPosOnes[N0][DV]; + POSITION_T QPosOnes[N0][M]; + int rejections = sk->rejections; + POSITION_T LPosOnes[N0][DV * M]; + do { + PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, + &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + LPosOnes[i][j] = INVALID_POS_VALUE; + } + } + + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0] = {0}; + for (int colQ = 0; colQ < N0; colQ++) { + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + DV, HPosOnes[i], + qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + DV * M, LPosOnes[colQ], + DV * M, auxPosOnes); + processedQOnes[i] += qBlockWeights[i][colQ]; + } + } + rejections--; + } while (rejections >= 0); + + POSITION_T QtrPosOnes[N0][M]; + unsigned transposed_ones_idx[N0] = {0x00}; + for (unsigned source_row_idx = 0; source_row_idx < N0 ; source_row_idx++) { + int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + int endQblockIdx = 0; + for (int blockIdx = 0; blockIdx < N0; blockIdx++) { + endQblockIdx += qBlockWeights[source_row_idx][blockIdx]; + for (; currQoneIdx < endQblockIdx; currQoneIdx++) { + QtrPosOnes[blockIdx][transposed_ones_idx[blockIdx]] = (P - + QPosOnes[source_row_idx][currQoneIdx]) % P; + transposed_ones_idx[blockIdx]++; + } + } + } + + POSITION_T auxSparse[DV * M]; + POSITION_T Ln0trSparse[DV * M]; + for (int i = 0; i < DV * M; i++) { + Ln0trSparse[i] = INVALID_POS_VALUE; + auxSparse[i] = INVALID_POS_VALUE; + } + + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxSparse, + DV, HPosOnes[i], + qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1] ] + ); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(DV * M, Ln0trSparse, + DV * M, Ln0trSparse, + DV * M, auxSparse + ); + } // end for i + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); + + DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, + syndrome, + Ln0trSparse, + DV * M); + + /* prepare mockup error vector in case a decoding failure occurs */ + DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(&niederreiter_decrypt_expander, + ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), TRNG_BYTE_LENGTH); + + int decryptOk = 0; + memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + decryptOk = PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, + (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); + + int err_weight = 0; + for (int i = 0 ; i < N0; i++) { + err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + } + decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); + + if (!decryptOk) { + memcpy(err, mockup_error_vector, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + } + + return decryptOk; +} diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.h b/crypto_kem/ledakemlt12/clean/niederreiter.h index e7ba199d..527e3101 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.h +++ b/crypto_kem/ledakemlt12/clean/niederreiter.h @@ -1,11 +1,10 @@ -#pragma once +#ifndef NIEDERREITER_H +#define NIEDERREITER_H + #include "qc_ldpc_parameters.h" #include "gf2x_limbs.h" #include "gf2x_arith_mod_xPplusOne.h" - - -/*----------------------------------------------------------------------------*/ -#pragma pack(1) +#include "rng.h" typedef struct { /* raw entropy extracted from TRNG, will be deterministically expanded into @@ -22,5 +21,20 @@ typedef struct { // with P coefficients. } publicKeyNiederreiter_t; -#pragma pack() -/*----------------------------------------------------------------------------*/ + + +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *const pk, + privateKeyNiederreiter_t *const sk, + AES_XOF_struct *keys_expander); + +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT syndrome[], + const publicKeyNiederreiter_t *const pk, + const DIGIT err[]); + +// return 1 if everything is ok +int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT err[], + const privateKeyNiederreiter_t *const sk, + const DIGIT syndrome[]); + + +#endif diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c deleted file mode 100644 index 9b34215e..00000000 --- a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.c +++ /dev/null @@ -1,119 +0,0 @@ -#include "niederreiter_decrypt.h" - -#include "qc_ldpc_parameters.h" -#include "gf2x_arith_mod_xPplusOne.h" -#include "H_Q_matrices_generation.h" - -#include "bf_decoding.h" -#include "dfr_test.h" -#include - -/*----------------------------------------------------------------------------*/ - -int decrypt_niederreiter(DIGIT err[], // N0 circ poly - const privateKeyNiederreiter_t *const sk, - const DIGIT syndrome[] // 1 circ poly - ) { - AES_XOF_struct niederreiter_decrypt_expander; - seedexpander_from_trng(&niederreiter_decrypt_expander, - sk->prng_seed); - - /**************************************************************************/ - // sequence of N0 circ block matrices (p x p): - POSITION_T HPosOnes[N0][DV]; - POSITION_T HtrPosOnes[N0][DV]; - POSITION_T QPosOnes[N0][M]; - int rejections = sk->rejections; - POSITION_T LPosOnes[N0][DV * M]; - do { - generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, - &niederreiter_decrypt_expander); - generateQsparse(QPosOnes, &niederreiter_decrypt_expander); - for (int i = 0; i < N0; i++) { - for (int j = 0; j < DV * M; j++) { - LPosOnes[i][j] = INVALID_POS_VALUE; - } - } - - POSITION_T auxPosOnes[DV * M]; - unsigned char processedQOnes[N0] = {0}; - for (int colQ = 0; colQ < N0; colQ++) { - for (int i = 0; i < N0; i++) { - gf2x_mod_mul_sparse(DV * M, auxPosOnes, - DV, HPosOnes[i], - qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); - gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], - DV * M, LPosOnes[colQ], - DV * M, auxPosOnes); - processedQOnes[i] += qBlockWeights[i][colQ]; - } - } - rejections--; - } while (rejections >= 0); - - POSITION_T QtrPosOnes[N0][M]; - unsigned transposed_ones_idx[N0] = {0x00}; - for (unsigned source_row_idx = 0; source_row_idx < N0 ; source_row_idx++) { - int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] - int endQblockIdx = 0; - for (int blockIdx = 0; blockIdx < N0; blockIdx++) { - endQblockIdx += qBlockWeights[source_row_idx][blockIdx]; - for (; currQoneIdx < endQblockIdx; currQoneIdx++) { - QtrPosOnes[blockIdx][transposed_ones_idx[blockIdx]] = (P - - QPosOnes[source_row_idx][currQoneIdx]) % P; - transposed_ones_idx[blockIdx]++; - } - } - } - - POSITION_T auxSparse[DV * M]; - POSITION_T Ln0trSparse[DV * M]; - for (int i = 0; i < DV * M; i++) { - Ln0trSparse[i] = INVALID_POS_VALUE; - auxSparse[i] = INVALID_POS_VALUE; - } - - for (int i = 0; i < N0; i++) { - gf2x_mod_mul_sparse(DV * M, auxSparse, - DV, HPosOnes[i], - qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1] ] - ); - gf2x_mod_add_sparse(DV * M, Ln0trSparse, - DV * M, Ln0trSparse, - DV * M, auxSparse - ); - } // end for i - gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); - - DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; - gf2x_mod_mul_dense_to_sparse(privateSyndrome, - syndrome, - Ln0trSparse, - DV * M); - - /* prepare mockup error vector in case a decoding failure occurs */ - DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; - memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - seedexpander(&niederreiter_decrypt_expander, - ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), TRNG_BYTE_LENGTH); - - int decryptOk = 0; - memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - decryptOk = bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, - (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); - - int err_weight = 0; - for (int i = 0 ; i < N0; i++) { - err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); - } - decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); - - if (!decryptOk) { - memcpy(err, mockup_error_vector, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - } - - return decryptOk; -} // end decrypt_niederreiter - -/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h b/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h deleted file mode 100644 index 66938700..00000000 --- a/crypto_kem/ledakemlt12/clean/niederreiter_decrypt.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include "niederreiter.h" -#include "gf2x_limbs.h" - -/*----------------------------------------------------------------------------*/ - -int decrypt_niederreiter(DIGIT err[], // return 1 if everything is ok - const privateKeyNiederreiter_t *const sk, - const DIGIT syndrome[] - ); - -/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c deleted file mode 100644 index fb075873..00000000 --- a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "niederreiter_encrypt.h" -#include "qc_ldpc_parameters.h" -#include "gf2x_arith_mod_xPplusOne.h" - -#include // memset(...) - -void encrypt_niederreiter(DIGIT syndrome[], // 1 polynomial - const publicKeyNiederreiter_t *const pk, - const DIGIT err[]) { // N0 polynomials - int i; - DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; - - memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - - for (i = 0; i < N0 - 1; i++) { - gf2x_mod_mul(saux, - pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, - err + i * NUM_DIGITS_GF2X_ELEMENT - ); - gf2x_mod_add(syndrome, syndrome, saux); - } // end for - gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); -} // end encrypt_niederreiter - -/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h b/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h deleted file mode 100644 index e69f0120..00000000 --- a/crypto_kem/ledakemlt12/clean/niederreiter_encrypt.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include "niederreiter.h" -#include "gf2x_limbs.h" - -void encrypt_niederreiter(DIGIT syndrome[], - const publicKeyNiederreiter_t *const pk, - const DIGIT err[] - ); diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c deleted file mode 100644 index 679e6716..00000000 --- a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.c +++ /dev/null @@ -1,113 +0,0 @@ -#include "niederreiter_keygen.h" - -#include "H_Q_matrices_generation.h" -#include "gf2x_arith_mod_xPplusOne.h" -#include "rng.h" -#include "dfr_test.h" - -#include -/*----------------------------------------------------------------------------*/ -/* Implementation that should never be optimized out by the compiler */ -static inline void zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; - while ( n-- ) { - *p++ = 0; - } -} // end zeroize - -/*----------------------------------------------------------------------------*/ - -void key_gen_niederreiter(publicKeyNiederreiter_t *const pk, - privateKeyNiederreiter_t *const sk, - AES_XOF_struct *keys_expander) { - // sequence of N0 circ block matrices (p x p): Hi - - POSITION_T HPosOnes[N0][DV]; - POSITION_T HtrPosOnes[N0][DV]; - /* Sparse representation of the transposed circulant matrix H, - with weight DV. Each index contains the position of a '1' digit in the - corresponding Htr block */ - - /* Sparse representation of the matrix (Q). - A matrix containing the positions of the ones in the circulant - blocks of Q. Each row contains the position of the - ones of all the blocks of a row of Q as exponent+ - P*block_position */ - POSITION_T QPosOnes[N0][M]; - - /*Rejection-sample for a full L*/ - POSITION_T LPosOnes[N0][DV * M]; - int is_L_full; - int isDFRok; - sk->rejections = (int8_t) 0; - do { - generateHPosOnes_HtrPosOnes(HPosOnes, - HtrPosOnes, - keys_expander); - - generateQsparse(QPosOnes, - keys_expander); - for (int i = 0; i < N0; i++) { - for (int j = 0; j < DV * M; j++) { - LPosOnes[i][j] = INVALID_POS_VALUE; - } - } - - POSITION_T auxPosOnes[DV * M]; - unsigned char processedQOnes[N0] = {0}; - for (int colQ = 0; colQ < N0; colQ++) { - for (int i = 0; i < N0; i++) { - gf2x_mod_mul_sparse(DV * M, auxPosOnes, - DV, HPosOnes[i], - qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); - gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], - DV * M, LPosOnes[colQ], - DV * M, auxPosOnes); - processedQOnes[i] += qBlockWeights[i][colQ]; - } - } - is_L_full = 1; - for (int i = 0; i < N0; i++) { - is_L_full = is_L_full && (LPosOnes[i][DV * M - 1] != INVALID_POS_VALUE); - } - sk->rejections = sk->rejections + 1; - if (is_L_full) { - isDFRok = DFR_test(LPosOnes); - } - } while (!is_L_full || !isDFRok); - sk->rejections = sk->rejections - 1; - - DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; - for (int j = 0; j < DV * M; j++) { - if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { - gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); - } - } - DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; - gf2x_mod_inverse(Ln0Inv, Ln0dense); - for (int i = 0; i < N0 - 1; i++) { - gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, - Ln0Inv, - LPosOnes[i], - DV * M); - } - - for (int i = 0; i < N0 - 1; i++) { - gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); - } -} // end key_gen_niederreiter - -/*----------------------------------------------------------------------------*/ - -void publicKey_deletion_niederreiter(publicKeyNiederreiter_t *const pk) { - - zeroize(pk, sizeof(publicKeyNiederreiter_t)); -} // publicKey_deletion_niederreiter - -/*----------------------------------------------------------------------------*/ - -void privateKey_deletion_niederreiter(privateKeyNiederreiter_t *const sk) { - zeroize(sk, sizeof(privateKeyNiederreiter_t)); -} // privateKey_deletion_niederreiter - -/*----------------------------------------------------------------------------*/ diff --git a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h b/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h deleted file mode 100644 index a775028b..00000000 --- a/crypto_kem/ledakemlt12/clean/niederreiter_keygen.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "niederreiter.h" -#include "rng.h" - -void key_gen_niederreiter(publicKeyNiederreiter_t *const pk, - privateKeyNiederreiter_t *const sk, - AES_XOF_struct *keys_expander); - -void publicKey_deletion_niederreiter(publicKeyNiederreiter_t *const pk); -void privateKey_deletion_niederreiter(privateKeyNiederreiter_t *const sk); diff --git a/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h b/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h index be88e080..01c9c204 100644 --- a/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h +++ b/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h @@ -1,126 +1,27 @@ -#pragma once -// CATEGORY defined in the makefile +#ifndef QC_LDPC_PARAMETERS_H +#define QC_LDPC_PARAMETERS_H + +#include "fips202.h" -/*----------------------------------------------------------------------------*/ -#if CATEGORY == 1 #define TRNG_BYTE_LENGTH (24) -#define HASH_FUNCTION sha3_256 #define HASH_BYTE_LENGTH (32) -// N0 defined in the makefile -#if (DFR_SL_LEVEL == 0) -#define P (35899) // modulus(x) = x^P-1 -#define DV (9) // odd number -#define M (9) -#define M0 (5) -#define M1 (4) -#define NUM_ERRORS_T (136) +#define HASH_FUNCTION sha3_256 -#elif (DFR_SL_LEVEL == 1) -#define P (52147) // modulus(x) = x^P-1 -#define DV (9) // odd number -#define M (9) -#define M0 (5) -#define M1 (4) -#define NUM_ERRORS_T (136) - -#else -#error "Unsupported number of circulant blocks" -#endif -#endif // end CATEGORY == 1 - -/*----------------------------------------------------------------------------*/ - -// We employ the parameters for Category 3 also in the case where the required -// security level is Category 2, where Category 2 has the following parameters. -// #define TRNG_BYTE_LENGTH (32) -// #define HASH_FUNCTION sha3_256 -// #define HASH_BYTE_LENGTH (32) - -/*----------------------------------------------------------------------------*/ - -#if (CATEGORY == 2) || (CATEGORY == 3) -#define TRNG_BYTE_LENGTH (32) -#define HASH_FUNCTION sha3_384 -#define HASH_BYTE_LENGTH (48) -// N0 defined in the makefile -#if (DFR_SL_LEVEL == 0) -#define P (57899) // modulus(x) = x^P-1 -#define DV (11) // odd number -#define M (11) -#define M0 (6) -#define M1 (5) -#define NUM_ERRORS_T (199) - -#elif (DFR_SL_LEVEL == 1) -#define P (96221) // modulus(x) = x^P-1 -#define DV (11) // odd number -#define M (11) -#define M0 (6) -#define M1 (5) -#define NUM_ERRORS_T (199) - -#else -#error "Unsupported number of circulant blocks" -#endif -#endif - -/*----------------------------------------------------------------------------*/ - -// We employ the parameters for Category 4 also in the case where the required -// security level is Category 5, where Category 4 has the following parameters. -// #if CATEGORY == 4 -// #define TRNG_BYTE_LENGTH (40) -// #define HASH_FUNCTION sha3_384 -// #define HASH_BYTE_LENGTH (48) -// #endif - -/*----------------------------------------------------------------------------*/ - -#if (CATEGORY == 4) || (CATEGORY == 5) -#define TRNG_BYTE_LENGTH (40) -#define HASH_FUNCTION sha3_512 -#define HASH_BYTE_LENGTH (64) -// N0 defined in the makefile -#if (DFR_SL_LEVEL == 0) -#define P (89051) // modulus(x) = x^P-1 -#define DV (13) // odd number -#define M (13) -#define M0 (7) -#define M1 (6) -#define NUM_ERRORS_T (267) - -#elif (DFR_SL_LEVEL == 1) -#define P (152267) // modulus(x) = x^P-1 -#define DV (13) // odd number -#define M (13) -#define M0 (7) -#define M1 (6) -#define NUM_ERRORS_T (267) - -#else -#error "Unsupported number of circulant blocks" -#endif -#endif -/*----------------------------------------------------------------------------*/ +#define N0 (2) +#define P (52147) // modulus(x) = x^P-1 +#define DV (9) // odd number +#define M (9) +#define M0 (5) +#define M1 (4) +#define NUM_ERRORS_T (136) // Derived parameters, they are useful for QC-LDPC algorithms #define HASH_BIT_LENGTH (HASH_BYTE_LENGTH << 3) -#define K ((N0-1)*P) -#define N (N0*P) -#define DC (N0*DV) - -// Circulant weight structure of the Q matrix, specialized per value of N0 -#if N0 == 2 -#define Q_BLOCK_WEIGHTS {{M0,M1},{M1,M0}} -#elif N0 == 3 -#define Q_BLOCK_WEIGHTS {{M0,M1,M2},{M2,M0,M1},{M1,M2,M0}} -#elif N0 == 4 -#define Q_BLOCK_WEIGHTS {{M0,M1,M2,M3},{M3,M0,M1,M2},{M2,M3,M0,M1},{M1,M2,M3,M0}} -#else -#error "Unsupported number of circulant blocks" -#endif +#define K ((N0-1)*P) +#define N (N0*P) +#define DC (N0*DV) +#define Q_BLOCK_WEIGHTS {{M0,M1},{M1,M0}} static const unsigned char qBlockWeights[N0][N0] = Q_BLOCK_WEIGHTS; - -/*----------------------------------------------------------------------------*/ +#endif diff --git a/crypto_kem/ledakemlt12/clean/rng.c b/crypto_kem/ledakemlt12/clean/rng.c index 02382074..d9e9bc66 100644 --- a/crypto_kem/ledakemlt12/clean/rng.c +++ b/crypto_kem/ledakemlt12/clean/rng.c @@ -1,59 +1,10 @@ #include "rng.h" -#include -#include -#include // void srand(unsigned int seed); int rand(void); RAND_MAX #include // void *memset(void *s, int c, size_t n); -#define __USE_POSIX199309 -#include // struct timespec; clock_gettime(...); CLOCK_REALTIME -#include "aes256.h" +#include "aes.h" #include "qc_ldpc_parameters.h" - -/******************************************************************************/ -/*----------------------------------------------------------------------------*/ -/* start PSEUDO-RAND GENERATOR ROUTINES for rnd.h */ -/*----------------------------------------------------------------------------*/ - - -void initialize_pseudo_random_generator_seed(int ac, char *av[]) { - - if (ac == 2) { - srand(atoi(av[1])); - } else { - struct timespec seedValue; - clock_gettime(CLOCK_REALTIME, &seedValue); - srand(seedValue.tv_nsec); - } // end else-if - unsigned char pseudo_entropy[48]; - for (int i = 0; i < 48; i++) { - pseudo_entropy[i] = rand() & 0xff; - } - randombytes_init(pseudo_entropy, - NULL, - 0 /*unused in NIST function*/); - - -} // end initilize_pseudo_random_sequence_seed - - -/*----------------------------------------------------------------------------*/ - -/* Initializes a dedicated DRBG context to avoid conflicts with the global one - * declared by NIST for KATs. Provides the output of the DRBG in output, for - * the given length */ - - -/*----------------------------------------------------------------------------*/ -/* end PSEUDO-RAND GENERATOR ROUTINES for rnd.h */ -/*----------------------------------------------------------------------------*/ - -AES256_CTR_DRBG_struct DRBG_ctx; - -void AES256_ECB(unsigned char *key, unsigned char *ctr, - unsigned char *buffer); - /* seedexpander_init() ctx - stores the current state of an instance of the seed expander @@ -61,11 +12,10 @@ void AES256_ECB(unsigned char *key, unsigned char *ctr, diversifier - an 8 byte diversifier maxlen - maximum number of bytes (less than 2**32) generated under this seed and diversifier */ -int -seedexpander_init(AES_XOF_struct *ctx, - unsigned char *seed, - unsigned char *diversifier, - unsigned long maxlen) { +static int seedexpander_init(AES_XOF_struct *ctx, + unsigned char *seed, + unsigned char *diversifier, + uint64_t maxlen) { if ( maxlen >= 0x100000000 ) { return RNG_BAD_MAXLEN; } @@ -92,15 +42,33 @@ seedexpander_init(AES_XOF_struct *ctx, return RNG_SUCCESS; } +void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, + const unsigned char *trng_entropy + /* TRNG_BYTE_LENGTH wide buffer */) { + + /*the NIST seedexpander will however access 32B from this buffer */ + unsigned int prng_buffer_size = TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH; + unsigned char prng_buffer[TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH] = { 0x00 }; + unsigned char diversifier[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + memcpy(prng_buffer, + trng_entropy, + TRNG_BYTE_LENGTH < prng_buffer_size ? TRNG_BYTE_LENGTH : prng_buffer_size); + + /* the required seed expansion will be quite small, set the max number of + * bytes conservatively to 10 MiB*/ + seedexpander_init(ctx, prng_buffer, diversifier, 10 * 1024 * 1024); +} + /* seedexpander() ctx - stores the current state of an instance of the seed expander x - returns the XOF data xlen - number of bytes to return */ -int -seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen) { - unsigned long offset; +int PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { + uint32_t offset; + aes256ctx ctx256; if ( x == NULL ) { return RNG_BAD_OUTBUF; @@ -109,6 +77,7 @@ seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen) { return RNG_BAD_REQ_LEN; } + aes256_keyexp(&ctx256, ctx->key); ctx->length_remaining -= xlen; offset = 0; @@ -125,7 +94,7 @@ seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen) { xlen -= 16 - ctx->buffer_pos; offset += 16 - ctx->buffer_pos; - AES256_ECB(ctx->key, ctx->ctr, ctx->buffer); + aes256_ecb(ctx->buffer, ctx->ctr, 16 / AES_BLOCKBYTES, &ctx256); ctx->buffer_pos = 0; //increment the counter @@ -142,162 +111,3 @@ seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen) { return RNG_SUCCESS; } - -// Use whatever AES implementation you have. This uses AES from openSSL library -// key - 256-bit AES key -// ptx - a 128-bit plaintext value -// ctx - a 128-bit ciphertext value - -void -AES256_ECB(unsigned char *key, unsigned char *ptx, unsigned char *ctx) { - uint32_t round_key[4 * (NROUNDS + 1)] = {0x00}; - rijndaelKeySetupEnc(round_key, key, KEYLEN_b); - rijndaelEncrypt(round_key, NROUNDS, ptx, ctx); -} - -void -randombytes_init(unsigned char *entropy_input, - unsigned char *personalization_string, - int security_strength) { - unsigned char seed_material[48]; - - memcpy(seed_material, entropy_input, 48); - if (personalization_string) - for (int i = 0; i < 48; i++) { - seed_material[i] ^= personalization_string[i]; - } - memset(DRBG_ctx.Key, 0x00, 32); - memset(DRBG_ctx.V, 0x00, 16); - AES256_CTR_DRBG_Update(seed_material, DRBG_ctx.Key, DRBG_ctx.V); - DRBG_ctx.reseed_counter = 1; -} - -int -randombytes(unsigned char *x, unsigned long long xlen) { - unsigned char block[16]; - int i = 0; - - while ( xlen > 0 ) { - //increment V - for (int j = 15; j >= 0; j--) { - if ( DRBG_ctx.V[j] == 0xff ) { - DRBG_ctx.V[j] = 0x00; - } else { - DRBG_ctx.V[j]++; - break; - } - } - AES256_ECB(DRBG_ctx.Key, DRBG_ctx.V, block); - if ( xlen > 15 ) { - memcpy(x + i, block, 16); - i += 16; - xlen -= 16; - } else { - memcpy(x + i, block, xlen); - xlen = 0; - } - } - AES256_CTR_DRBG_Update(NULL, DRBG_ctx.Key, DRBG_ctx.V); - DRBG_ctx.reseed_counter++; - - return RNG_SUCCESS; -} - -void -AES256_CTR_DRBG_Update(unsigned char *provided_data, - unsigned char *Key, - unsigned char *V) { - unsigned char temp[48]; - - for (int i = 0; i < 3; i++) { - //increment V - for (int j = 15; j >= 0; j--) { - if ( V[j] == 0xff ) { - V[j] = 0x00; - } else { - V[j]++; - break; - } - } - - AES256_ECB(Key, V, temp + 16 * i); - } - if ( provided_data != NULL ) - for (int i = 0; i < 48; i++) { - temp[i] ^= provided_data[i]; - } - memcpy(Key, temp, 32); - memcpy(V, temp + 32, 16); -} - - - -void deterministic_random_byte_generator(unsigned char *const output, - const unsigned long long output_len, - const unsigned char *const seed, - const unsigned long long seed_length - ) { - /* DRBG context initialization */ - AES256_CTR_DRBG_struct ctx; - unsigned char seed_material[48]; - memset(seed_material, 0x00, 48); - memcpy(seed_material, seed, seed_length); - - memset(ctx.Key, 0x00, 32); - memset(ctx.V, 0x00, 16); - AES256_CTR_DRBG_Update(seed_material, ctx.Key, ctx.V); - ctx.reseed_counter = 1; - - /* Actual DRBG computation as from the randombytes(unsigned char *x, - * unsigned long long xlen) from NIST */ - - unsigned char block[16]; - int i = 0, length_remaining; - - length_remaining = output_len; - - while ( length_remaining > 0 ) { - //increment V - for (int j = 15; j >= 0; j--) { - if ( ctx.V[j] == 0xff ) { - ctx.V[j] = 0x00; - } else { - ctx.V[j]++; - break; - } - } - AES256_ECB(ctx.Key, ctx.V, block); - if ( length_remaining > 15 ) { - memcpy(output + i, block, 16); - i += 16; - length_remaining -= 16; - } else { - memcpy(output + i, block, length_remaining); - length_remaining = 0; - } - } - AES256_CTR_DRBG_Update(NULL, ctx.Key, ctx.V); - ctx.reseed_counter++; - -} // end deterministic_random_byte_generator - -void seedexpander_from_trng(AES_XOF_struct *ctx, - const unsigned char *trng_entropy - /* TRNG_BYTE_LENGTH wide buffer */) { - - /*the NIST seedexpander will however access 32B from this buffer */ - unsigned int prng_buffer_size = TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH; - unsigned char prng_buffer[TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH] = { 0x00 }; - memcpy(prng_buffer, - trng_entropy, - TRNG_BYTE_LENGTH < prng_buffer_size ? TRNG_BYTE_LENGTH : prng_buffer_size); - /* if extra entropy is provided, add it to the diversifier */ - #if TRNG_BYTE_LENGTH == 40 - unsigned char *diversifier = ((unsigned char *)trng_entropy) + 32; - #else - unsigned char diversifier[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - #endif - /* the required seed expansion will be quite small, set the max number of - * bytes conservatively to 10 MiB*/ - seedexpander_init(ctx, prng_buffer, diversifier, 10 * 1024 * 1024); -} diff --git a/crypto_kem/ledakemlt12/clean/rng.h b/crypto_kem/ledakemlt12/clean/rng.h index 5e7724bc..40226208 100644 --- a/crypto_kem/ledakemlt12/clean/rng.h +++ b/crypto_kem/ledakemlt12/clean/rng.h @@ -1,12 +1,8 @@ -#pragma once +#ifndef RNG_H +#define RNG_H -/****** From this point on, the code was supplied by NIST ****************/ -// Created by Bassham, Lawrence E (Fed) on 8/29/17. -// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved. -// -/****** from NIST ****************/ - -#include +#include +#include #define RNG_SUCCESS 0 #define RNG_BAD_MAXLEN -1 @@ -15,50 +11,16 @@ typedef struct { unsigned char buffer[16]; - int buffer_pos; - unsigned long length_remaining; + unsigned int buffer_pos; + uint64_t length_remaining; unsigned char key[32]; unsigned char ctr[16]; } AES_XOF_struct; -typedef struct { - unsigned char Key[32]; - unsigned char V[16]; - int reseed_counter; -} AES256_CTR_DRBG_struct; +int PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); -void -AES256_CTR_DRBG_Update(unsigned char *provided_data, - unsigned char *Key, - unsigned char *V); +/* TRNG_BYTE_LENGTH wide buffer */ +void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); -int -seedexpander_init(AES_XOF_struct *ctx, - unsigned char *seed, - unsigned char *diversifier, - unsigned long maxlen); - -int -seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen); - -void -randombytes_init(unsigned char *entropy_input, - unsigned char *personalization_string, - int security_strength); - -int -randombytes(unsigned char *x, unsigned long long xlen); - -/****** End of NIST supplied code ****************/ - -void initialize_pseudo_random_generator_seed(int ac, char *av[]); - -void deterministic_random_byte_generator(unsigned char *const output, - const unsigned long long output_len, - const unsigned char *const seed, - const unsigned long long seed_length); - -void seedexpander_from_trng(AES_XOF_struct *ctx, - const unsigned char *trng_entropy - /* TRNG_BYTE_LENGTH wide buffer */); +#endif diff --git a/crypto_kem/ledakemlt12/clean/sha3.h b/crypto_kem/ledakemlt12/clean/sha3.h deleted file mode 100644 index 642843de..00000000 --- a/crypto_kem/ledakemlt12/clean/sha3.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#define LITTLE_ENDIAN -#include - -static inline -void sha3_256(const unsigned char *input, - unsigned int inputByteLen, - unsigned char *output) { - Keccak_HashInstance state; - Keccak_HashInitialize(&state, 1088, 512, 256, 0x06); - Keccak_HashUpdate(&state, input, inputByteLen * 8); - Keccak_HashFinal(&state, output); -} - -/** - * Function to compute SHA3-384 on the input message. - * The output length is fixed to 48 bytes. - */ -static inline -void sha3_384(const unsigned char *input, - unsigned int inputByteLen, - unsigned char *output) { - Keccak_HashInstance state; - Keccak_HashInitialize(&state, 832, 768, 384, 0x06); - Keccak_HashUpdate(&state, input, inputByteLen * 8); - Keccak_HashFinal(&state, output); -} - -/** - * Function to compute SHA3-512 on the input message. - * The output length is fixed to 64 bytes. - */ -static inline -void sha3_512(const unsigned char *input, - unsigned int inputByteLen, - unsigned char *output) { - Keccak_HashInstance state; - Keccak_HashInitialize(&state, 576, 1024, 512, 0x06); - Keccak_HashUpdate(&state, input, inputByteLen * 8); - Keccak_HashFinal(&state, output); -} - From 1680f3f1253e971ef4331b31ee5231d669f36650 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 27 May 2019 19:16:31 +0200 Subject: [PATCH 10/35] add nmake makefile --- .../clean/Makefile.Microsoft_nmake | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 crypto_kem/ledakemlt12/clean/Makefile.Microsoft_nmake diff --git a/crypto_kem/ledakemlt12/clean/Makefile.Microsoft_nmake b/crypto_kem/ledakemlt12/clean/Makefile.Microsoft_nmake new file mode 100644 index 00000000..e4bfc760 --- /dev/null +++ b/crypto_kem/ledakemlt12/clean/Makefile.Microsoft_nmake @@ -0,0 +1,19 @@ +# This Makefile can be used with Microsoft Visual Studio's nmake using the command: +# nmake /f Makefile.Microsoft_nmake + +LIBRARY=libledakemlt12_clean.lib +OBJECTS=bf_decoding.obj dfr_test.obj gf2x_arith_mod_xPplusOne.obj gf2x_arith.obj H_Q_matrices_generation.obj kem.obj niederreiter.obj rng.obj + +CFLAGS=/nologo /I ..\..\..\common /W4 /WX + +all: $(LIBRARY) + +# Make sure objects are recompiled if headers change. +$(OBJECTS): *.h + +$(LIBRARY): $(OBJECTS) + LIB.EXE /NOLOGO /WX /OUT:$@ $** + +clean: + -DEL $(OBJECTS) + -DEL $(LIBRARY) From a7b3aa73b27238a6a99f975423ab6d1959694974 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 27 May 2019 20:17:53 +0200 Subject: [PATCH 11/35] fix gcc/clang-tidy warnings, remove preprocessor conditionals --- .../clean/H_Q_matrices_generation.h | 2 +- crypto_kem/ledakemlt12/clean/Makefile | 4 +- crypto_kem/ledakemlt12/clean/bf_decoding.c | 20 +- crypto_kem/ledakemlt12/clean/bf_decoding.h | 2 +- crypto_kem/ledakemlt12/clean/dfr_test.c | 19 +- crypto_kem/ledakemlt12/clean/gf2x_arith.c | 535 +++++++++--------- crypto_kem/ledakemlt12/clean/gf2x_arith.h | 19 +- .../clean/gf2x_arith_mod_xPplusOne.c | 126 ++--- .../clean/gf2x_arith_mod_xPplusOne.h | 130 ++--- crypto_kem/ledakemlt12/clean/gf2x_limbs.h | 51 +- crypto_kem/ledakemlt12/clean/kem.c | 22 +- crypto_kem/ledakemlt12/clean/niederreiter.c | 11 +- crypto_kem/ledakemlt12/clean/niederreiter.h | 25 +- crypto_kem/ledakemlt12/clean/rng.h | 10 +- 14 files changed, 434 insertions(+), 542 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h index c05a72fb..11509bf4 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h @@ -1,8 +1,8 @@ #ifndef H_Q_MATRICES_GENERATION_H #define H_Q_MATRICES_GENERATION_H -#include "qc_ldpc_parameters.h" #include "gf2x_limbs.h" +#include "qc_ldpc_parameters.h" #include "rng.h" void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes( diff --git a/crypto_kem/ledakemlt12/clean/Makefile b/crypto_kem/ledakemlt12/clean/Makefile index e3548212..08c019ee 100644 --- a/crypto_kem/ledakemlt12/clean/Makefile +++ b/crypto_kem/ledakemlt12/clean/Makefile @@ -8,11 +8,9 @@ HEADERS=api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ OBJECTS=bf_decoding.o dfr_test.o gf2x_arith_mod_xPplusOne.o \ gf2x_arith.o H_Q_matrices_generation.o kem.o niederreiter.o rng.o -CFLAGS=-O3 -Wall -Wextra -Wpedantic -Wmissing-prototypes -std=c99 \ +CFLAGS=-O3 -Wall -Werror -Wextra -Wvla -Wpedantic -Wmissing-prototypes -std=c99 \ -I../../../common $(EXTRAFLAGS) -# TODO: -Werror -Wvla - all: $(LIB) %.o: %.c $(HEADERS) diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/clean/bf_decoding.c index 9d5fc7d9..cd55fd83 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.c @@ -1,15 +1,15 @@ #include "bf_decoding.h" #include "gf2x_arith_mod_xPplusOne.h" -#include #include +#include -#define ROTBYTE(a) ( (a << 8) | (a >> (DIGIT_SIZE_b - 8)) ) -#define ROTUPC(a) ( (a >> 8) | (a << (DIGIT_SIZE_b - 8)) ) +#define ROTBYTE(a) ( ((a) << 8) | ((a) >> (DIGIT_SIZE_b - 8)) ) +#define ROTUPC(a) ( ((a) >> 8) | ((a) << (DIGIT_SIZE_b - 8)) ) -int thresholds[2] = {B0, (DV * M) / 2 + 1}; +unsigned int thresholds[2] = {B0, (DV * M) / 2 + 1}; -int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT out[], // N0 polynomials +int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], DIGIT privateSyndrome[]) { @@ -35,20 +35,20 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT out[], // N0 polynomials } /* iteration based threshold determination*/ - int corrt_syndrome_based = thresholds[iteration]; + unsigned int corrt_syndrome_based = thresholds[iteration]; //Computation of correlation with a full Q matrix for (int i = 0; i < N0; i++) { for (int j = 0; j < P; j++) { int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] int endQblockIdx = 0; - int correlation = 0; + unsigned int correlation = 0; for (int blockIdx = 0; blockIdx < N0; blockIdx++) { endQblockIdx += qBlockWeights[blockIdx][i]; int currblockoffset = blockIdx * P; for (; currQoneIdx < endQblockIdx; currQoneIdx++) { - int tmp = QtrPosOnes[i][currQoneIdx] + j; + uint32_t tmp = QtrPosOnes[i][currQoneIdx] + j; tmp = tmp >= P ? tmp - P : tmp; currQBitPos[currQoneIdx] = tmp; currQBlkPos[currQoneIdx] = blockIdx; @@ -57,7 +57,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT out[], // N0 polynomials } /* Correlation based flipping */ if (correlation >= corrt_syndrome_based) { - gf2x_toggle_coeff(out + NUM_DIGITS_GF2X_ELEMENT * i, j); + gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { unsigned syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { @@ -72,7 +72,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT out[], // N0 polynomials iteration = iteration + 1; check = 0; - while (check < NUM_DIGITS_GF2X_ELEMENT && privateSyndrome[check++] == 0); + while (check < NUM_DIGITS_GF2X_ELEMENT && privateSyndrome[check++] == 0) {}; } while (iteration < ITERATIONS_MAX && check < NUM_DIGITS_GF2X_ELEMENT); diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.h b/crypto_kem/ledakemlt12/clean/bf_decoding.h index 5b5bbeb2..927a5e71 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.h @@ -1,8 +1,8 @@ #ifndef BF_DECODING_H #define BF_DECODING_H -#include "qc_ldpc_parameters.h" #include "gf2x_limbs.h" +#include "qc_ldpc_parameters.h" /* Definitions for DFR level 2^-SL with SL=128 */ #define ITERATIONS_MAX (2) diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt12/clean/dfr_test.c index 66582346..c24e0d9a 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.c +++ b/crypto_kem/ledakemlt12/clean/dfr_test.c @@ -9,7 +9,7 @@ * computes the threshold for the second iteration of the decoder and stores * it in the globally accessible vector */ -extern int thresholds[2]; +extern unsigned int thresholds[2]; int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { @@ -19,14 +19,15 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { * gamma[a][b][c] stores the intersection of the first column of the a-th * block of L with the c-th column of the b-th block of L */ /* Gamma computation can be accelerated employing symmetry and QC properties */ - int gamma[N0][N0][P] = {{{0}}}; - uint32_t rotated_column[DV * M]; - int firstidx, secondidx, intersectionval; + unsigned int gamma[N0][N0][P] = {{{0}}}; + unsigned int rotated_column[DV * M]; + + unsigned int firstidx, secondidx, intersectionval; unsigned int gammaHist[N0][DV * M + 1] = {{0}}; - int maxMut[N0], maxMutMinusOne[N0]; - int allBlockMaxSumst, allBlockMaxSumstMinusOne; + unsigned int maxMut[N0], maxMutMinusOne[N0]; + unsigned int allBlockMaxSumst, allBlockMaxSumstMinusOne; unsigned int toAdd, histIdx; @@ -34,10 +35,10 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { if (LSparse[i][j] != 0) { - LSparse_loc[i][j] = (P - LSparse[i][j]) ; + LSparse_loc[i][j] = (P - LSparse[i][j]); } } - quicksort(LSparse_loc[i], DV * M); + quicksort_sparse(LSparse_loc[i]); } for (int i = 0; i < N0; i++ ) { @@ -47,7 +48,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; } - quicksort(rotated_column, DV * M); + quicksort_sparse(rotated_column); /* compute the intersection amount */ firstidx = 0, secondidx = 0; intersectionval = 0; diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/clean/gf2x_arith.c index ec3b396a..31e505f1 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.c @@ -1,6 +1,7 @@ #include "gf2x_arith.h" -#include // memset(...) + #include +#include // memset(...) /* allows the second operand to be shorter than the first */ /* the result should be as large as the first operand*/ @@ -19,12 +20,12 @@ static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(const int length, DIGIT in[], const int amount) { +void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } - int j; + unsigned int j; DIGIT mask; mask = ((DIGIT)0x01 << amount) - 1; for (j = length - 1; j > 0 ; j--) { @@ -35,12 +36,12 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(const int length, DIGIT in[], c } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(const int length, DIGIT in[], const int amount) { +void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } - int j; + size_t j; DIGIT mask; mask = ~(((DIGIT)0x01 << (DIGIT_SIZE_b - amount)) - 1); for (j = 0 ; j < length - 1 ; j++) { @@ -50,22 +51,22 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(const int length, DIGIT in[], co in[j] <<= amount; } - -static void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], + int na, const DIGIT A[], + int nb, const DIGIT B[]) { int i, j, k; DIGIT u, h; memset(Res, 0x00, nr * sizeof(DIGIT)); for (k = DIGIT_SIZE_b - 1; k > 0; k--) { - for (i = na - 1; i >= 0; i--) - if ( A[i] & (((DIGIT)0x1) << k) ) + for (i = na - 1; i >= 0; i--) { + if ( A[i] & (((DIGIT)0x1) << k) ) { for (j = nb - 1; j >= 0; j--) { Res[i + j + 1] ^= B[j]; } - + } + } u = Res[na + nb - 1]; Res[na + nb - 1] = u << 0x1; @@ -75,11 +76,13 @@ static void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(const int nr, DIGIT Res[], Res[na + nb - 1 - j] = h ^ (u << 0x1); } } - for (i = na - 1; i >= 0; i--) - if ( A[i] & ((DIGIT)0x1) ) + for (i = na - 1; i >= 0; i--) { + if ( A[i] & ((DIGIT)0x1) ) { for (j = nb - 1; j >= 0; j--) { Res[i + j + 1] ^= B[j]; } + } + } } static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { @@ -96,258 +99,258 @@ static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { } } -#define MIN_KAR_DIGITS 20 - -static void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { - - if (na < MIN_KAR_DIGITS || nb < MIN_KAR_DIGITS) { - /* fall back to schoolbook */ - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(nr, Res, na, A, nb, B); - return; - } - - if (na % 2 == 0) { - unsigned bih = na / 2; - DIGIT middle[2 * bih], sumA[bih], sumB[bih]; - gf2x_add(sumA, A, A + bih, bih); - gf2x_add(sumB, B, B + bih, bih); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, middle, - bih, sumA, - bih, sumB); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res + 2 * bih, - bih, A + bih, - bih, B + bih); - gf2x_add(middle, middle, Res + 2 * bih, 2 * bih); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res, - bih, A, - bih, B); - gf2x_add(middle, middle, Res, 2 * bih); - gf2x_add(Res + bih, Res + bih, middle, 2 * bih); - } else { - unsigned bih = na / 2 + 1; - DIGIT middle[2 * bih], sumA[bih], sumB[bih]; - gf2x_add_asymm(bih, sumA, - bih, A + bih - 1, - bih - 1, A); - gf2x_add_asymm(bih, sumB, - bih, B + bih - 1, - bih - 1, B); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, middle, - bih, sumA, - bih, sumB); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res + 2 * (bih - 1), - bih, A + bih - 1, - bih, B + bih - 1); - gf2x_add(middle, middle, Res + 2 * (bih - 1), 2 * bih); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * (bih - 1), Res, - (bih - 1), A, - (bih - 1), B); - gf2x_add_asymm(2 * bih, middle, - 2 * bih, middle, - 2 * (bih - 1), Res); - gf2x_add(Res + bih - 2, Res + bih - 2, middle, 2 * bih); - } -} - -#define MIN_TOOM_DIGITS 35 - -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { - - if (na < MIN_TOOM_DIGITS || nb < MIN_TOOM_DIGITS) { - /* fall back to Karatsuba */ - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(nr, Res, na, A, nb, B); - return; - } - - unsigned int bih; //number of limbs for each part. - if (na % 3 == 0) { - bih = na / 3; - } else { - bih = na / 3 + 1; - } - - DIGIT u2[bih], u1[bih], u0[bih]; - - unsigned int leading_slack = (3 - (na) % 3) % 3; -// printf("leading slack %d",leading_slack); - unsigned int i; - for (i = 0; i < leading_slack ; i++) { - u2[i] = 0; - } - for (; i < bih; ++i) { - u2[i] = A[i - leading_slack]; - } - /* note: only u2 needs to be a copy, refactor */ - for (; i < 2 * bih; ++i) { - u1[i - bih] = A[i - leading_slack]; - } - for (; i < 3 * bih; ++i) { - u0[i - 2 * bih] = A[i - leading_slack]; - } - - DIGIT v2[bih], v1[bih], v0[bih]; /* partitioned inputs */ - /* note: only v2 needs to be a copy, refactor */ - for (i = 0; i < leading_slack ; i++) { - v2[i] = 0; - } - for (; i < bih; ++i) { - v2[i] = B[i - leading_slack]; - } - /* note , only v2 needs to be a copy */ - for (; i < 2 * bih; ++i) { - v1[i - bih] = B[i - leading_slack]; - } - for (; i < 3 * bih; ++i) { - v0[i - 2 * bih] = B[i - leading_slack]; - } - - DIGIT sum_u[bih]; /*bih digit wide*/ - gf2x_add(sum_u, u0, u1, bih); - gf2x_add(sum_u, sum_u, u2, bih); - - DIGIT sum_v[bih]; /*bih digit wide*/ - gf2x_add(sum_v, v0, v1, bih); - gf2x_add(sum_v, sum_v, v2, bih); - - - DIGIT w1[2 * bih]; - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w1, - bih, sum_u, - bih, sum_v); - - - DIGIT u2_x2[bih + 1]; - u2_x2[0] = 0; - memcpy(u2_x2 + 1, u2, bih * DIGIT_SIZE_B); - PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, u2_x2, 2); - - DIGIT u1_x[bih + 1]; - u1_x[0] = 0; - memcpy(u1_x + 1, u1, bih * DIGIT_SIZE_B); - PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, u1_x, 1); - - DIGIT u1_x1_u2_x2[bih + 1]; - gf2x_add(u1_x1_u2_x2, u1_x, u2_x2, bih + 1); - - DIGIT temp_u_components[bih + 1]; - gf2x_add_asymm(bih + 1, temp_u_components, - bih + 1, u1_x1_u2_x2, - bih, sum_u); - - DIGIT v2_x2[bih + 1]; - v2_x2[0] = 0; - memcpy(v2_x2 + 1, v2, bih * DIGIT_SIZE_B); - PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, v2_x2, 2); - - DIGIT v1_x[bih + 1]; - v1_x[0] = 0; - memcpy(v1_x + 1, v1, bih * DIGIT_SIZE_B); - PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, v1_x, 1); - - DIGIT v1_x1_v2_x2[bih + 1]; - gf2x_add(v1_x1_v2_x2, v1_x, v2_x2, bih + 1); - - DIGIT temp_v_components[bih + 1]; - gf2x_add_asymm(bih + 1, temp_v_components, - bih + 1, v1_x1_v2_x2, - bih, sum_v); - - DIGIT w3[2 * bih + 2]; - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih + 2, w3, - bih + 1, temp_u_components, - bih + 1, temp_v_components); - - gf2x_add_asymm(bih + 1, u1_x1_u2_x2, - bih + 1, u1_x1_u2_x2, - bih, u0); - gf2x_add_asymm(bih + 1, v1_x1_v2_x2, - bih + 1, v1_x1_v2_x2, - bih, v0); - - DIGIT w2[2 * bih + 2]; - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih + 2, w2, - bih + 1, u1_x1_u2_x2, - bih + 1, v1_x1_v2_x2); - - DIGIT w4[2 * bih]; - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w4, - bih, u2, - bih, v2); - DIGIT w0[2 * bih]; - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w0, - bih, u0, - bih, v0); - - // Interpolation starts - gf2x_add(w3, w2, w3, 2 * bih + 2); - gf2x_add_asymm(2 * bih + 2, w2, - 2 * bih + 2, w2, - 2 * bih, w0); - PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(2 * bih + 2, w2, 1); - gf2x_add(w2, w2, w3, 2 * bih + 2); - - // w2 + (w4 * x^3+1) = w2 + w4 + w4 << 3 - DIGIT w4_x3_plus_1[2 * bih + 1]; - w4_x3_plus_1[0] = 0; - memcpy(w4_x3_plus_1 + 1, w4, 2 * bih * DIGIT_SIZE_B); - PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(2 * bih + 1, w4_x3_plus_1, 3); - gf2x_add_asymm(2 * bih + 2, w2, - 2 * bih + 2, w2, - 2 * bih, w4); - gf2x_add_asymm(2 * bih + 2, w2, - 2 * bih + 2, w2, - 2 * bih + 1, w4_x3_plus_1); - - gf2x_exact_div_x_plus_one(2 * bih + 2, w2); - - gf2x_add(w1, w1, w0, 2 * bih); - gf2x_add_asymm(2 * bih + 2, w3, - 2 * bih + 2, w3, - 2 * bih, w1); - - PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(2 * bih + 2, w3, 1); - gf2x_exact_div_x_plus_one(2 * bih + 2, w3); - - gf2x_add(w1, w1, w4, 2 * bih); - - DIGIT w1_final[2 * bih + 2]; - gf2x_add_asymm(2 * bih + 2, w1_final, - 2 * bih + 2, w2, - 2 * bih, w1); - gf2x_add(w2, w2, w3, 2 * bih + 2); - - // Result recombination starts here - - memset(Res, 0, nr * DIGIT_SIZE_B); - /* optimization: topmost slack digits should be computed, and not addedd, - * zeroization can be avoided altogether with a proper merge of the - * results */ - - int leastSignifDigitIdx = nr - 1; - for (int i = 0; i < 2 * bih; i++) { - Res[leastSignifDigitIdx - i] ^= w0[2 * bih - 1 - i]; - } - leastSignifDigitIdx -= bih; - for (int i = 0; i < 2 * bih + 2; i++) { - Res[leastSignifDigitIdx - i] ^= w1_final[2 * bih + 2 - 1 - i]; - } - leastSignifDigitIdx -= bih; - for (int i = 0; i < 2 * bih + 2; i++) { - Res[leastSignifDigitIdx - i] ^= w2[2 * bih + 2 - 1 - i]; - } - leastSignifDigitIdx -= bih; - for (int i = 0; i < 2 * bih + 2 ; i++) { - Res[leastSignifDigitIdx - i] ^= w3[2 * bih + 2 - 1 - i]; - } - leastSignifDigitIdx -= bih; - for (int i = 0; i < 2 * bih && (leastSignifDigitIdx - i >= 0) ; i++) { - Res[leastSignifDigitIdx - i] ^= w4[2 * bih - 1 - i]; - } -} +// #define MIN_KAR_DIGITS 20 +// +// static void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(const int nr, DIGIT Res[], +// const int na, const DIGIT A[], +// const int nb, const DIGIT B[]) { +// +// if (na < MIN_KAR_DIGITS || nb < MIN_KAR_DIGITS) { +// /* fall back to schoolbook */ +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(nr, Res, na, A, nb, B); +// return; +// } +// +// if (na % 2 == 0) { +// unsigned bih = na / 2; +// DIGIT middle[2 * bih], sumA[bih], sumB[bih]; +// gf2x_add(sumA, A, A + bih, bih); +// gf2x_add(sumB, B, B + bih, bih); +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, middle, +// bih, sumA, +// bih, sumB); +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res + 2 * bih, +// bih, A + bih, +// bih, B + bih); +// gf2x_add(middle, middle, Res + 2 * bih, 2 * bih); +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res, +// bih, A, +// bih, B); +// gf2x_add(middle, middle, Res, 2 * bih); +// gf2x_add(Res + bih, Res + bih, middle, 2 * bih); +// } else { +// unsigned bih = na / 2 + 1; +// DIGIT middle[2 * bih], sumA[bih], sumB[bih]; +// gf2x_add_asymm(bih, sumA, +// bih, A + bih - 1, +// bih - 1, A); +// gf2x_add_asymm(bih, sumB, +// bih, B + bih - 1, +// bih - 1, B); +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, middle, +// bih, sumA, +// bih, sumB); +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res + 2 * (bih - 1), +// bih, A + bih - 1, +// bih, B + bih - 1); +// gf2x_add(middle, middle, Res + 2 * (bih - 1), 2 * bih); +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * (bih - 1), Res, +// (bih - 1), A, +// (bih - 1), B); +// gf2x_add_asymm(2 * bih, middle, +// 2 * bih, middle, +// 2 * (bih - 1), Res); +// gf2x_add(Res + bih - 2, Res + bih - 2, middle, 2 * bih); +// } +// } +// +// #define MIN_TOOM_DIGITS 35 +// +// void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(const int nr, DIGIT Res[], +// const int na, const DIGIT A[], +// const int nb, const DIGIT B[]) { +// +// if (na < MIN_TOOM_DIGITS || nb < MIN_TOOM_DIGITS) { +// /* fall back to Karatsuba */ +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(nr, Res, na, A, nb, B); +// return; +// } +// +// int bih; //number of limbs for each part. +// if (na % 3 == 0) { +// bih = na / 3; +// } else { +// bih = na / 3 + 1; +// } +// +// DIGIT u2[bih], u1[bih], u0[bih]; +// +// int leading_slack = (3 - (na) % 3) % 3; +// // printf("leading slack %d",leading_slack); +// int i; +// for (i = 0; i < leading_slack ; i++) { +// u2[i] = 0; +// } +// for (; i < bih; ++i) { +// u2[i] = A[i - leading_slack]; +// } +// /* note: only u2 needs to be a copy, refactor */ +// for (; i < 2 * bih; ++i) { +// u1[i - bih] = A[i - leading_slack]; +// } +// for (; i < 3 * bih; ++i) { +// u0[i - 2 * bih] = A[i - leading_slack]; +// } +// +// DIGIT v2[bih], v1[bih], v0[bih]; /* partitioned inputs */ +// /* note: only v2 needs to be a copy, refactor */ +// for (i = 0; i < leading_slack ; i++) { +// v2[i] = 0; +// } +// for (; i < bih; ++i) { +// v2[i] = B[i - leading_slack]; +// } +// /* note , only v2 needs to be a copy */ +// for (; i < 2 * bih; ++i) { +// v1[i - bih] = B[i - leading_slack]; +// } +// for (; i < 3 * bih; ++i) { +// v0[i - 2 * bih] = B[i - leading_slack]; +// } +// +// DIGIT sum_u[bih]; /*bih digit wide*/ +// gf2x_add(sum_u, u0, u1, bih); +// gf2x_add(sum_u, sum_u, u2, bih); +// +// DIGIT sum_v[bih]; /*bih digit wide*/ +// gf2x_add(sum_v, v0, v1, bih); +// gf2x_add(sum_v, sum_v, v2, bih); +// +// +// DIGIT w1[2 * bih]; +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w1, +// bih, sum_u, +// bih, sum_v); +// +// +// DIGIT u2_x2[bih + 1]; +// u2_x2[0] = 0; +// memcpy(u2_x2 + 1, u2, bih * DIGIT_SIZE_B); +// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, u2_x2, 2); +// +// DIGIT u1_x[bih + 1]; +// u1_x[0] = 0; +// memcpy(u1_x + 1, u1, bih * DIGIT_SIZE_B); +// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, u1_x, 1); +// +// DIGIT u1_x1_u2_x2[bih + 1]; +// gf2x_add(u1_x1_u2_x2, u1_x, u2_x2, bih + 1); +// +// DIGIT temp_u_components[bih + 1]; +// gf2x_add_asymm(bih + 1, temp_u_components, +// bih + 1, u1_x1_u2_x2, +// bih, sum_u); +// +// DIGIT v2_x2[bih + 1]; +// v2_x2[0] = 0; +// memcpy(v2_x2 + 1, v2, bih * DIGIT_SIZE_B); +// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, v2_x2, 2); +// +// DIGIT v1_x[bih + 1]; +// v1_x[0] = 0; +// memcpy(v1_x + 1, v1, bih * DIGIT_SIZE_B); +// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, v1_x, 1); +// +// DIGIT v1_x1_v2_x2[bih + 1]; +// gf2x_add(v1_x1_v2_x2, v1_x, v2_x2, bih + 1); +// +// DIGIT temp_v_components[bih + 1]; +// gf2x_add_asymm(bih + 1, temp_v_components, +// bih + 1, v1_x1_v2_x2, +// bih, sum_v); +// +// DIGIT w3[2 * bih + 2]; +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih + 2, w3, +// bih + 1, temp_u_components, +// bih + 1, temp_v_components); +// +// gf2x_add_asymm(bih + 1, u1_x1_u2_x2, +// bih + 1, u1_x1_u2_x2, +// bih, u0); +// gf2x_add_asymm(bih + 1, v1_x1_v2_x2, +// bih + 1, v1_x1_v2_x2, +// bih, v0); +// +// DIGIT w2[2 * bih + 2]; +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih + 2, w2, +// bih + 1, u1_x1_u2_x2, +// bih + 1, v1_x1_v2_x2); +// +// DIGIT w4[2 * bih]; +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w4, +// bih, u2, +// bih, v2); +// DIGIT w0[2 * bih]; +// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w0, +// bih, u0, +// bih, v0); +// +// // Interpolation starts +// gf2x_add(w3, w2, w3, 2 * bih + 2); +// gf2x_add_asymm(2 * bih + 2, w2, +// 2 * bih + 2, w2, +// 2 * bih, w0); +// PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(2 * bih + 2, w2, 1); +// gf2x_add(w2, w2, w3, 2 * bih + 2); +// +// // w2 + (w4 * x^3+1) = w2 + w4 + w4 << 3 +// DIGIT w4_x3_plus_1[2 * bih + 1]; +// w4_x3_plus_1[0] = 0; +// memcpy(w4_x3_plus_1 + 1, w4, 2 * bih * DIGIT_SIZE_B); +// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(2 * bih + 1, w4_x3_plus_1, 3); +// gf2x_add_asymm(2 * bih + 2, w2, +// 2 * bih + 2, w2, +// 2 * bih, w4); +// gf2x_add_asymm(2 * bih + 2, w2, +// 2 * bih + 2, w2, +// 2 * bih + 1, w4_x3_plus_1); +// +// gf2x_exact_div_x_plus_one(2 * bih + 2, w2); +// +// gf2x_add(w1, w1, w0, 2 * bih); +// gf2x_add_asymm(2 * bih + 2, w3, +// 2 * bih + 2, w3, +// 2 * bih, w1); +// +// PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(2 * bih + 2, w3, 1); +// gf2x_exact_div_x_plus_one(2 * bih + 2, w3); +// +// gf2x_add(w1, w1, w4, 2 * bih); +// +// DIGIT w1_final[2 * bih + 2]; +// gf2x_add_asymm(2 * bih + 2, w1_final, +// 2 * bih + 2, w2, +// 2 * bih, w1); +// gf2x_add(w2, w2, w3, 2 * bih + 2); +// +// // Result recombination starts here +// +// memset(Res, 0, nr * DIGIT_SIZE_B); +// /* optimization: topmost slack digits should be computed, and not addedd, +// * zeroization can be avoided altogether with a proper merge of the +// * results */ +// +// int leastSignifDigitIdx = nr - 1; +// for (int i = 0; i < 2 * bih; i++) { +// Res[leastSignifDigitIdx - i] ^= w0[2 * bih - 1 - i]; +// } +// leastSignifDigitIdx -= bih; +// for (int i = 0; i < 2 * bih + 2; i++) { +// Res[leastSignifDigitIdx - i] ^= w1_final[2 * bih + 2 - 1 - i]; +// } +// leastSignifDigitIdx -= bih; +// for (int i = 0; i < 2 * bih + 2; i++) { +// Res[leastSignifDigitIdx - i] ^= w2[2 * bih + 2 - 1 - i]; +// } +// leastSignifDigitIdx -= bih; +// for (int i = 0; i < 2 * bih + 2 ; i++) { +// Res[leastSignifDigitIdx - i] ^= w3[2 * bih + 2 - 1 - i]; +// } +// leastSignifDigitIdx -= bih; +// for (int i = 0; i < 2 * bih && (leastSignifDigitIdx - i >= 0) ; i++) { +// Res[leastSignifDigitIdx - i] ^= w4[2 * bih - 1 - i]; +// } +// } // // Unused // static int gf2x_cmp(const unsigned lenA, const DIGIT A[], // const unsigned lenB, const DIGIT B[]) { diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.h b/crypto_kem/ledakemlt12/clean/gf2x_arith.h index fedab74e..b9a512a9 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.h @@ -43,27 +43,22 @@ */ -#define GF2X_MUL PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3 +#define GF2X_MUL PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb // #define GF2X_MUL gf2x_mul_comb -static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], const unsigned int nr) { - - unsigned int i; - for (i = 0; i < nr; i++) { +static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], size_t nr) { + for (size_t i = 0; i < nr; i++) { Res[i] = A[i] ^ B[i]; } } -void GF2X_MUL(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]); - +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ +void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(const int length, DIGIT in[], const int amount); +void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); -/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(const int length, DIGIT in[], const int amount); +void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); #endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index 92387b97..69f09833 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -1,25 +1,25 @@ #include "gf2x_arith_mod_xPplusOne.h" #include "rng.h" -#include // memcpy(...), memset(...) #include +#include // memcpy(...), memset(...) -static void gf2x_mod(DIGIT out[], - const int nin, const DIGIT in[]) { - long int i, j, posTrailingBit, maskOffset; - DIGIT mask, aux[nin]; +static void gf2x_mod(DIGIT out[], const DIGIT in[]) { - memcpy(aux, in, nin * DIGIT_SIZE_B); + int i, j, posTrailingBit, maskOffset; + DIGIT mask, aux[2 * NUM_DIGITS_GF2X_ELEMENT]; + + memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memset(out, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - if (nin < NUM_DIGITS_GF2X_MODULUS) { - for (i = 0; i < nin; i++) { - out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = in[nin - 1 - i]; + if (2 * NUM_DIGITS_GF2X_ELEMENT < NUM_DIGITS_GF2X_MODULUS) { + for (i = 0; i < 2 * NUM_DIGITS_GF2X_ELEMENT; i++) { + out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = in[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; } return; } - for (i = 0; i < nin - NUM_DIGITS_GF2X_MODULUS; i += 1) { + for (i = 0; i < (2 * NUM_DIGITS_GF2X_ELEMENT) - NUM_DIGITS_GF2X_MODULUS; i += 1) { for (j = DIGIT_SIZE_b - 1; j >= 0; j--) { mask = ((DIGIT)0x1) << j; if (aux[i] & mask) { @@ -43,10 +43,10 @@ static void gf2x_mod(DIGIT out[], } } - int to_copy = (nin > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : nin; + int to_copy = (2 * NUM_DIGITS_GF2X_ELEMENT > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : 2 * NUM_DIGITS_GF2X_ELEMENT; for (i = 0; i < to_copy; i++) { - out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[nin - 1 - i]; + out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; } } @@ -61,9 +61,9 @@ static void left_bit_shift(const int length, DIGIT in[]) { in[j] <<= 1; } -static void right_bit_shift(const int length, DIGIT in[]) { +static void right_bit_shift(unsigned int length, DIGIT in[]) { - int j; + unsigned int j; for (j = length - 1; j > 0 ; j--) { in[j] >>= 1; in[j] |= (in[j - 1] & (DIGIT)0x01) << (DIGIT_SIZE_b - 1); @@ -73,8 +73,8 @@ static void right_bit_shift(const int length, DIGIT in[]) { /* shifts by whole digits */ -static inline void left_DIGIT_shift_n(const int length, DIGIT in[], int amount) { - int j; +static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { + unsigned int j; for (j = 0; (j + amount) < length; j++) { in[j] = in[j + amount]; } @@ -85,39 +85,15 @@ static inline void left_DIGIT_shift_n(const int length, DIGIT in[], int amount) /* may shift by an arbitrary amount*/ -static void left_bit_shift_wide_n(const int length, DIGIT in[], int amount) { +static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); } -#if (defined(DIGIT_IS_UINT8) || defined(DIGIT_IS_UINT16)) -static uint8_t byte_reverse_with_less32bitDIGIT(uint8_t b) { - uint8_t r = b; - int s = (sizeof(b) << 3) - 1; - for (b >>= 1; b; b >>= 1) { - r <<= 1; - r |= b & 1; - s--; - } - r <<= s; - return r; -} -#endif - -#if defined(DIGIT_IS_UINT32) -static uint8_t byte_reverse_with_32bitDIGIT(uint8_t b) { - b = ( (b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU) - ) * 0x10101LU >> 16; - return b; -} -#endif - -#if defined(DIGIT_IS_UINT64) static uint8_t byte_reverse_with_64bitDIGIT(uint8_t b) { b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023; return b; } -#endif static DIGIT reverse_digit(const DIGIT b) { int i; @@ -127,28 +103,11 @@ static DIGIT reverse_digit(const DIGIT b) { } toReverse; toReverse.digitValue = b; - #if defined(DIGIT_IS_UINT64) for (i = 0; i < DIGIT_SIZE_B; i++) { toReverse.inByte[i] = byte_reverse_with_64bitDIGIT(toReverse.inByte[i]); } + return __builtin_bswap64(toReverse.digitValue); - #elif defined(DIGIT_IS_UINT32) - for (i = 0; i < DIGIT_SIZE_B; i++) { - toReverse.inByte[i] = byte_reverse_with_32bitDIGIT(toReverse.inByte[i]); - } - return __builtin_bswap32(toReverse.digitValue); - #elif defined(DIGIT_IS_UINT16) - for (i = 0; i < DIGIT_SIZE_B; i++) { - toReverse.inByte[i] = byte_reverse_with_less32bitDIGIT(toReverse.inByte[i]); - } - reversed = __builtin_bswap16(toReverse.digitValue); - #elif defined(DIGIT_IS_UINT8) - return byte_reverse_with_less32bitDIGIT(toReverse.inByte[0]); - #else -#error "Missing implementation for reverse_digit(...) \ -with this CPU word bitsize !!! " - #endif - return toReverse.digitValue; } void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { @@ -159,17 +118,6 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { DIGIT rev1, rev2, a00; int i, slack_bits_amount = NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - P; - if (NUM_DIGITS_GF2X_ELEMENT == 1) { - a00 = A[0] & mask; - right_bit_shift(1, A); - rev1 = reverse_digit(A[0]); - #if (NUM_DIGITS_GF2X_MOD_P_ELEMENT*DIGIT_SIZE_b - P) - rev1 >>= (DIGIT_SIZE_b - (P % DIGIT_SIZE_b)); - #endif - A[0] = (rev1 & (~mask)) | a00; - return; - } - a00 = A[NUM_DIGITS_GF2X_ELEMENT - 1] & mask; right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, A); @@ -280,18 +228,20 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { } s[0] |= mask; - for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 && in[i] == 0; i--); + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 && in[i] == 0; i--) { }; if (i < 0) { return 0; } - if (NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT) + if (NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT) { for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 1 ; i--) { f[i] = in[i - 1]; - } else /* they are equal */ + } + } else { /* they are equal */ for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 0 ; i--) { f[i] = in[i]; } + } for (i = 1; i <= 2 * P; i++) { if ( (f[0] & mask) == 0 ) { @@ -329,7 +279,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const GF2X_MUL(2 * NUM_DIGITS_GF2X_ELEMENT, aux, NUM_DIGITS_GF2X_ELEMENT, A, NUM_DIGITS_GF2X_ELEMENT, B); - gf2x_mod(Res, 2 * NUM_DIGITS_GF2X_ELEMENT, aux); + gf2x_mod(Res, aux); } @@ -358,7 +308,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse( } } - gf2x_mod(Res, 2 * NUM_DIGITS_GF2X_ELEMENT, resDouble); + gf2x_mod(Res, resDouble); } @@ -392,7 +342,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ size_t lastFilledPos = 0; for (size_t i = 0 ; i < sizeA ; i++) { for (size_t j = 0 ; j < sizeB ; j++) { - uint32_t prod = ((uint32_t) A[i]) + ((uint32_t) B[j]); + uint32_t prod = A[i] + B[j]; prod = ( (prod >= P) ? prod - P : prod); if ((A[i] != INVALID_POS_VALUE) && (B[j] != INVALID_POS_VALUE)) { @@ -407,7 +357,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ Res[lastFilledPos] = INVALID_POS_VALUE; lastFilledPos++; } - quicksort(Res, sizeR); + quicksort_sparse(Res); /* eliminate duplicates */ POSITION_T lastReadPos = Res[0]; int duplicateCount; @@ -436,10 +386,10 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ /* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse( int sizeR, POSITION_T Res[], - int sizeA, POSITION_T A[], - int sizeB, POSITION_T B[]) { + int sizeA, const POSITION_T A[], + int sizeB, const POSITION_T B[]) { - POSITION_T tmpRes[sizeR]; + POSITION_T tmpRes[DV * M]; // TODO: now function only works for adding (disjunct) DV and M positions int idxA = 0, idxB = 0, idxR = 0; while ( idxA < sizeA && idxB < sizeB && @@ -509,7 +459,7 @@ static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct /* Obtains fresh randomness and seed-expands it until all the required positions * for the '1's in the circulant block are obtained */ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, - const int countOnes, + int countOnes, AES_XOF_struct *seed_expander_ctx) { int duplicated, placedOnes = 0; @@ -520,9 +470,11 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, BITS_TO_REPRESENT(P), seed_expander_ctx); duplicated = 0; - for (int j = 0; j < placedOnes; j++) if (pos_ones[j] == p) { + for (int j = 0; j < placedOnes; j++) { + if (pos_ones[j] == p) { duplicated = 1; } + } if (duplicated == 0) { pos_ones[placedOnes] = p; placedOnes++; @@ -530,21 +482,23 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, } } +/* Returns random weight-t circulant block */ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], - const int countOnes, AES_XOF_struct *seed_expander_ctx) { - int rndPos[countOnes], duplicated, counter = 0; + int rndPos[NUM_ERRORS_T], duplicated, counter = 0; memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - while (counter < countOnes) { + while (counter < NUM_ERRORS_T) { int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, BITS_TO_REPRESENT(P), seed_expander_ctx); duplicated = 0; - for (int j = 0; j < counter; j++) if (rndPos[j] == p) { + for (int j = 0; j < counter; j++) { + if (rndPos[j] == p) { duplicated = 1; } + } if (duplicated == 0) { rndPos[counter] = p; counter++; diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h index 9e08ffc0..0d6be87b 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h @@ -18,43 +18,43 @@ #define INVALID_POS_VALUE (P) #define IS_REPRESENTABLE_IN_D_BITS(D, N) \ - (((unsigned long) N >= (1UL << (D - 1)) && (unsigned long) N < (1UL << D)) ? D : -1) + (((unsigned long) (N) >= (1UL << ((D) - 1)) && (unsigned long) (N) < (1UL << (D))) ? (D) : -1) #define BITS_TO_REPRESENT(N) \ - (N == 0 ? 1 : (31 \ - + IS_REPRESENTABLE_IN_D_BITS( 1, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 2, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 3, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 4, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 5, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 6, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 7, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 8, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 9, N) \ - + IS_REPRESENTABLE_IN_D_BITS(10, N) \ - + IS_REPRESENTABLE_IN_D_BITS(11, N) \ - + IS_REPRESENTABLE_IN_D_BITS(12, N) \ - + IS_REPRESENTABLE_IN_D_BITS(13, N) \ - + IS_REPRESENTABLE_IN_D_BITS(14, N) \ - + IS_REPRESENTABLE_IN_D_BITS(15, N) \ - + IS_REPRESENTABLE_IN_D_BITS(16, N) \ - + IS_REPRESENTABLE_IN_D_BITS(17, N) \ - + IS_REPRESENTABLE_IN_D_BITS(18, N) \ - + IS_REPRESENTABLE_IN_D_BITS(19, N) \ - + IS_REPRESENTABLE_IN_D_BITS(20, N) \ - + IS_REPRESENTABLE_IN_D_BITS(21, N) \ - + IS_REPRESENTABLE_IN_D_BITS(22, N) \ - + IS_REPRESENTABLE_IN_D_BITS(23, N) \ - + IS_REPRESENTABLE_IN_D_BITS(24, N) \ - + IS_REPRESENTABLE_IN_D_BITS(25, N) \ - + IS_REPRESENTABLE_IN_D_BITS(26, N) \ - + IS_REPRESENTABLE_IN_D_BITS(27, N) \ - + IS_REPRESENTABLE_IN_D_BITS(28, N) \ - + IS_REPRESENTABLE_IN_D_BITS(29, N) \ - + IS_REPRESENTABLE_IN_D_BITS(30, N) \ - + IS_REPRESENTABLE_IN_D_BITS(31, N) \ - + IS_REPRESENTABLE_IN_D_BITS(32, N) \ - ) \ + ((N) == 0 ? 1 : (31 \ + + IS_REPRESENTABLE_IN_D_BITS( 1, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 2, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 3, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 4, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 5, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 6, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 7, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 8, N) \ + + IS_REPRESENTABLE_IN_D_BITS( 9, N) \ + + IS_REPRESENTABLE_IN_D_BITS(10, N) \ + + IS_REPRESENTABLE_IN_D_BITS(11, N) \ + + IS_REPRESENTABLE_IN_D_BITS(12, N) \ + + IS_REPRESENTABLE_IN_D_BITS(13, N) \ + + IS_REPRESENTABLE_IN_D_BITS(14, N) \ + + IS_REPRESENTABLE_IN_D_BITS(15, N) \ + + IS_REPRESENTABLE_IN_D_BITS(16, N) \ + + IS_REPRESENTABLE_IN_D_BITS(17, N) \ + + IS_REPRESENTABLE_IN_D_BITS(18, N) \ + + IS_REPRESENTABLE_IN_D_BITS(19, N) \ + + IS_REPRESENTABLE_IN_D_BITS(20, N) \ + + IS_REPRESENTABLE_IN_D_BITS(21, N) \ + + IS_REPRESENTABLE_IN_D_BITS(22, N) \ + + IS_REPRESENTABLE_IN_D_BITS(23, N) \ + + IS_REPRESENTABLE_IN_D_BITS(24, N) \ + + IS_REPRESENTABLE_IN_D_BITS(25, N) \ + + IS_REPRESENTABLE_IN_D_BITS(26, N) \ + + IS_REPRESENTABLE_IN_D_BITS(27, N) \ + + IS_REPRESENTABLE_IN_D_BITS(28, N) \ + + IS_REPRESENTABLE_IN_D_BITS(29, N) \ + + IS_REPRESENTABLE_IN_D_BITS(30, N) \ + + IS_REPRESENTABLE_IN_D_BITS(31, N) \ + + IS_REPRESENTABLE_IN_D_BITS(32, N) \ + ) \ ) @@ -65,7 +65,7 @@ static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { } /* returns the coefficient of the x^exponent term as the LSB of a digit */ -static inline DIGIT gf2x_get_coeff(const DIGIT poly[], const unsigned int exponent) { +static inline DIGIT gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -73,10 +73,9 @@ static inline DIGIT gf2x_get_coeff(const DIGIT poly[], const unsigned int expone } /* sets the coefficient of the x^exponent term as the LSB of a digit */ -static inline void gf2x_set_coeff(DIGIT poly[], const unsigned int exponent, DIGIT value) { - int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - int digitIdx = straightIdx / DIGIT_SIZE_b; - +static inline void gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; /* clear given coefficient */ @@ -87,9 +86,9 @@ static inline void gf2x_set_coeff(DIGIT poly[], const unsigned int exponent, DIG } /* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ -static inline void gf2x_toggle_coeff(DIGIT poly[], const unsigned int exponent) { - int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - int digitIdx = straightIdx / DIGIT_SIZE_b; +static inline void gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; /* clear given coefficient */ @@ -97,28 +96,23 @@ static inline void gf2x_toggle_coeff(DIGIT poly[], const unsigned int exponent) poly[digitIdx] = poly[digitIdx] ^ mask; } + +/* population count for an unsigned 64-bit integer */ +static int popcount_uint64t(uint64_t x) { + x -= (x >> 1) & 0x5555555555555555; + x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); + x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; + return (int)((x * 0x0101010101010101) >> 56); +} + /* population count for a single polynomial */ -static inline int population_count(DIGIT upc[]) { +static inline int population_count(DIGIT *poly) { int ret = 0; for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { - #if defined(DIGIT_IS_ULLONG) - ret += __builtin_popcountll((unsigned long long int) (upc[i])); - #elif defined(DIGIT_IS_ULONG) - ret += __builtin_popcountl((unsigned long int) (upc[i])); - #elif defined(DIGIT_IS_UINT) - ret += __builtin_popcount((unsigned int) (upc[i])); - #elif defined(DIGIT_IS_UCHAR) - const unsigned char split_lookup[] = { - 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 - }; - ret += split_lookup[upc[i] & 0xF] + split_lookup[upc[i] >> 4]; - #else -#error "Missing implementation for population_count(...) \ -with this CPU word bitsize !!! " - #endif + ret += popcount_uint64t(poly[i]); } return ret; -} // end population_count +} static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); @@ -126,25 +120,24 @@ static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); -int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]);/* ret. 1 if inv. exists */ +int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); /* in place bit-transp. of a(x) % x^P+1, e.g.: a3 a2 a1 a0 --> a1 a2 a3 a0 */ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]); void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block( POSITION_T *pos_ones, - const int countOnes, + int countOnes, AES_XOF_struct *seed_expander_ctx); void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], - const int countOnes, AES_XOF_struct *seed_expander_ctx); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse( int sizeR, POSITION_T Res[], - int sizeA, POSITION_T A[], - int sizeB, POSITION_T B[]); + int sizeA, const POSITION_T A[], + int sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse( int sizeA, @@ -180,12 +173,11 @@ static inline int partition(POSITION_T arr[], int lo, int hi) { return i + 1; } -static inline void quicksort(POSITION_T Res[], unsigned int sizeR) { - /* sort the result */ - int stack[sizeR]; +static inline void quicksort_sparse(POSITION_T Res[]) { + int stack[DV * M]; int hi, lo, pivot, tos = -1; stack[++tos] = 0; - stack[++tos] = sizeR - 1; + stack[++tos] = (DV * M) - 1; while (tos >= 0 ) { hi = stack[tos--]; lo = stack[tos--]; diff --git a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h index 429a3cf9..e4082a6d 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h @@ -1,58 +1,11 @@ #ifndef GF2X_LIMBS_H #define GF2X_LIMBS_H +#include "qc_ldpc_parameters.h" + #include #include #include -#include "qc_ldpc_parameters.h" - - - -/*----------------------------------------------------------------------------*/ -/* limb size definitions for the multi-precision GF(2^x) library */ -/*----------------------------------------------------------------------------*/ - -// #ifndef CPU_WORD_BITS -// typedef size_t DIGIT; -// #define DIGIT_MAX SIZE_MAX -// #else -// // gcc -DCPU_WORD_BITS=64 ... -// #define CAT(a, b, c) PRIMITIVE_CAT(a, b, c) -// #define PRIMITIVE_CAT(a, b, c) a ## b ## c -// -// typedef CAT( uint, CPU_WORD_BITS, _t ) DIGIT; -// #define DIGIT_MAX (CAT(UINT, CPU_WORD_BITS, _MAX)) -// #endif -// -// #if (DIGIT_MAX == ULLONG_MAX) -// #define DIGIT_IS_ULLONG -// #elif (DIGIT_MAX == ULONG_MAX) -// #define DIGIT_IS_ULONG -// #elif (DIGIT_MAX == UINT_MAX) -// #define DIGIT_IS_UINT -// #elif (DIGIT_MAX == UCHAR_MAX) -// #define DIGIT_IS_UCHAR -// #else -// #error "unable to find the type of CPU_WORD_BITS" -// #endif -// -// #if (DIGIT_MAX == UINT64_MAX) -// #define DIGIT_IS_UINT64 -// #define DIGIT_SIZE_B 8 -// #elif (DIGIT_MAX == UINT32_MAX) -// #define DIGIT_IS_UINT32 -// #define DIGIT_SIZE_B 4 -// #elif (DIGIT_MAX == UINT16_MAX) -// #define DIGIT_IS_UINT16 -// #define DIGIT_SIZE_B 2 -// #elif (DIGIT_MAX == UINT8_MAX) -// #define DIGIT_IS_UINT8 -// #define DIGIT_SIZE_B 1 -// #else -// #error "unable to find the bitsize of size_t" -// #endif - -// #define LITTLE_ENDIAN typedef uint64_t DIGIT; #define DIGIT_IS_UINT64 diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt12/clean/kem.c index 41f897ce..004d3fb7 100644 --- a/crypto_kem/ledakemlt12/clean/kem.c +++ b/crypto_kem/ledakemlt12/clean/kem.c @@ -27,12 +27,9 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc( unsigned char *ct, unsigned char * randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); - PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(error_vector, - NUM_ERRORS_T, - &niederreiter_encap_key_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); - HASH_FUNCTION(ss, (const uint8_t *) error_vector, // input - (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); // input length + HASH_FUNCTION(ss, (const uint8_t *) error_vector, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); return 0; @@ -46,13 +43,10 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *sk ) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; - int decode_ok = PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, - (privateKeyNiederreiter_t *)sk, - (DIGIT *)ct); - HASH_FUNCTION(ss, (const unsigned char *) decoded_error_vector, - (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); - if (decode_ok == 1) { - return 0; - } - return -1; + PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, + (privateKeyNiederreiter_t *)sk, + (DIGIT *)ct); + HASH_FUNCTION(ss, (const unsigned char *) decoded_error_vector, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); + + return 0; } diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index ab084096..4fc49796 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -1,14 +1,13 @@ -#include "niederreiter.h" #include "H_Q_matrices_generation.h" -#include "gf2x_arith_mod_xPplusOne.h" -#include "rng.h" -#include "dfr_test.h" #include "bf_decoding.h" +#include "dfr_test.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "niederreiter.h" #include "qc_ldpc_parameters.h" +#include "rng.h" #include - void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *const pk, privateKeyNiederreiter_t *const sk, AES_XOF_struct *keys_expander) { @@ -207,7 +206,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT err[], // N0 } decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); - if (!decryptOk) { + if (!decryptOk) { // TODO: not constant time memcpy(err, mockup_error_vector, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); } diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.h b/crypto_kem/ledakemlt12/clean/niederreiter.h index 527e3101..efdd848a 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.h +++ b/crypto_kem/ledakemlt12/clean/niederreiter.h @@ -1,9 +1,9 @@ #ifndef NIEDERREITER_H #define NIEDERREITER_H -#include "qc_ldpc_parameters.h" -#include "gf2x_limbs.h" #include "gf2x_arith_mod_xPplusOne.h" +#include "gf2x_limbs.h" +#include "qc_ldpc_parameters.h" #include "rng.h" typedef struct { @@ -23,18 +23,21 @@ typedef struct { -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *const pk, - privateKeyNiederreiter_t *const sk, - AES_XOF_struct *keys_expander); +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen( + publicKeyNiederreiter_t *pk, + privateKeyNiederreiter_t *sk, + AES_XOF_struct *keys_expander); -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT syndrome[], - const publicKeyNiederreiter_t *const pk, - const DIGIT err[]); +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt( + DIGIT syndrome[], + const publicKeyNiederreiter_t *pk, + const DIGIT *err); // return 1 if everything is ok -int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT err[], - const privateKeyNiederreiter_t *const sk, - const DIGIT syndrome[]); +int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt( + DIGIT *err, + const privateKeyNiederreiter_t *sk, + const DIGIT *syndrome); #endif diff --git a/crypto_kem/ledakemlt12/clean/rng.h b/crypto_kem/ledakemlt12/clean/rng.h index 40226208..646bc86b 100644 --- a/crypto_kem/ledakemlt12/clean/rng.h +++ b/crypto_kem/ledakemlt12/clean/rng.h @@ -1,13 +1,13 @@ #ifndef RNG_H #define RNG_H -#include #include +#include -#define RNG_SUCCESS 0 -#define RNG_BAD_MAXLEN -1 -#define RNG_BAD_OUTBUF -2 -#define RNG_BAD_REQ_LEN -3 +#define RNG_SUCCESS ( 0) +#define RNG_BAD_MAXLEN (-1) +#define RNG_BAD_OUTBUF (-2) +#define RNG_BAD_REQ_LEN (-3) typedef struct { unsigned char buffer[16]; From 7b9e254a8b70df5eaad14e6eebc81c3fa6603040 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 27 May 2019 20:21:05 +0200 Subject: [PATCH 12/35] fix reading outside buffer --- crypto_kem/ledakemlt12/clean/dfr_test.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt12/clean/dfr_test.c index c24e0d9a..bcea824c 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.c +++ b/crypto_kem/ledakemlt12/clean/dfr_test.c @@ -57,11 +57,9 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { intersectionval++; firstidx++; secondidx++; - } - if ( LSparse_loc[i][firstidx] > rotated_column[secondidx] ) { + } else if ( LSparse_loc[i][firstidx] > rotated_column[secondidx] ) { secondidx++; - } - if ( LSparse_loc[i][firstidx] < rotated_column[secondidx] ) { + } else { /*if ( LSparse_loc[i][firstidx] < rotated_column[secondidx] ) */ firstidx++; } } From ca6d935bbc162203b5fef664d4f0099d0a5248a8 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 27 May 2019 22:48:15 +0200 Subject: [PATCH 13/35] fix msvc warnings --- .../clean/gf2x_arith_mod_xPplusOne.c | 61 +++++++++++++------ .../clean/gf2x_arith_mod_xPplusOne.h | 44 +------------ crypto_kem/ledakemlt12/clean/niederreiter.c | 19 +++--- 3 files changed, 54 insertions(+), 70 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index 69f09833..7d2add83 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -12,12 +12,15 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memset(out, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + /* not true for parameter set if (2 * NUM_DIGITS_GF2X_ELEMENT < NUM_DIGITS_GF2X_MODULUS) { for (i = 0; i < 2 * NUM_DIGITS_GF2X_ELEMENT; i++) { out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = in[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; } return; } + */ for (i = 0; i < (2 * NUM_DIGITS_GF2X_ELEMENT) - NUM_DIGITS_GF2X_MODULUS; i += 1) { for (j = DIGIT_SIZE_b - 1; j >= 0; j--) { @@ -95,6 +98,13 @@ static uint8_t byte_reverse_with_64bitDIGIT(uint8_t b) { return b; } +/* https://stackoverflow.com/questions/2182002/convert-big-endian-to-little-endian-in-c-without-using-provided-func */ +static uint64_t swap_uint64( uint64_t val ) { + val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL ); + val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL ); + return (val << 32) | (val >> 32); +} + static DIGIT reverse_digit(const DIGIT b) { int i; union toReverse_t { @@ -107,7 +117,7 @@ static DIGIT reverse_digit(const DIGIT b) { toReverse.inByte[i] = byte_reverse_with_64bitDIGIT(toReverse.inByte[i]); } - return __builtin_bswap64(toReverse.digitValue); + return swap_uint64(toReverse.digitValue); } void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { @@ -127,9 +137,11 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { A[i] = rev2; A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; } + /* if (NUM_DIGITS_GF2X_ELEMENT % 2 == 1) { A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); - } + }*/ + A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); if (slack_bits_amount) { PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); @@ -140,24 +152,26 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { static void rotate_bit_left(DIGIT in[]) { /* equivalent to x * in(x) mod x^P+1 */ DIGIT mask, rotated_bit; - + /* if (NUM_DIGITS_GF2X_MODULUS == NUM_DIGITS_GF2X_ELEMENT) { - int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; mask = ((DIGIT)0x1) << msb_offset_in_digit; rotated_bit = !!(in[0] & mask); - in[0] &= ~mask; /* clear shifted bit */ + in[0] &= ~mask; left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); } else { - /* NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT and - * MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS == 0 - */ mask = ((DIGIT)0x1) << (DIGIT_SIZE_b - 1); rotated_bit = !!(in[0] & mask); - in[0] &= ~mask; /* clear shifted bit */ + in[0] &= ~mask; left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); - } + } */ + + int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; + mask = ((DIGIT)0x1) << msb_offset_in_digit; + rotated_bit = !!(in[0] & mask); + in[0] &= ~mask; + left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); in[NUM_DIGITS_GF2X_ELEMENT - 1] |= rotated_bit; } @@ -166,15 +180,15 @@ static void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ DIGIT rotated_bit = in[NUM_DIGITS_GF2X_ELEMENT - 1] & ((DIGIT)0x1); right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); + /* if (NUM_DIGITS_GF2X_MODULUS == NUM_DIGITS_GF2X_ELEMENT) { int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; rotated_bit = rotated_bit << msb_offset_in_digit; } else { - /* NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT and - * MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS == 0 - */ rotated_bit = rotated_bit << (DIGIT_SIZE_b - 1); - } + } */ + int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; + rotated_bit = rotated_bit << msb_offset_in_digit; in[0] |= rotated_bit; } @@ -221,11 +235,13 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { v[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x0; s[NUM_DIGITS_GF2X_MODULUS - 1] = 0x1; + /* if (MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS == 0) { mask = 0x1; } else { mask = (((DIGIT)0x1) << MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS); - } + }*/ + mask = (((DIGIT)0x1) << MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS); s[0] |= mask; for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 && in[i] == 0; i--) { }; @@ -233,14 +249,18 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { return 0; } + /* if (NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT) { for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 1 ; i--) { f[i] = in[i - 1]; } - } else { /* they are equal */ + } else { for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 0 ; i--) { f[i] = in[i]; } + }*/ + for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 0 ; i--) { + f[i] = in[i]; } for (i = 1; i <= 2 * P; i++) { @@ -467,7 +487,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, while (placedOnes < countOnes) { p = rand_range(NUM_BITS_GF2X_ELEMENT, - BITS_TO_REPRESENT(P), + P_BITS, seed_expander_ctx); duplicated = 0; for (int j = 0; j < placedOnes; j++) { @@ -483,15 +503,16 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, } /* Returns random weight-t circulant block */ -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], - AES_XOF_struct *seed_expander_ctx) { +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( + DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + AES_XOF_struct *seed_expander_ctx) { int rndPos[NUM_ERRORS_T], duplicated, counter = 0; memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); while (counter < NUM_ERRORS_T) { - int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, BITS_TO_REPRESENT(P), + int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, seed_expander_ctx); duplicated = 0; for (int j = 0; j < counter; j++) { diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h index 0d6be87b..f41eb8ef 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h @@ -7,8 +7,8 @@ #include "gf2x_arith.h" #include "rng.h" -#define NUM_BITS_GF2X_ELEMENT (P) -#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define NUM_BITS_GF2X_ELEMENT (P) // 52147 +#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) // 815 #define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ( (P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1 ) #define NUM_BITS_GF2X_MODULUS (P+1) @@ -17,45 +17,7 @@ #define INVALID_POS_VALUE (P) -#define IS_REPRESENTABLE_IN_D_BITS(D, N) \ - (((unsigned long) (N) >= (1UL << ((D) - 1)) && (unsigned long) (N) < (1UL << (D))) ? (D) : -1) - -#define BITS_TO_REPRESENT(N) \ - ((N) == 0 ? 1 : (31 \ - + IS_REPRESENTABLE_IN_D_BITS( 1, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 2, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 3, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 4, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 5, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 6, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 7, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 8, N) \ - + IS_REPRESENTABLE_IN_D_BITS( 9, N) \ - + IS_REPRESENTABLE_IN_D_BITS(10, N) \ - + IS_REPRESENTABLE_IN_D_BITS(11, N) \ - + IS_REPRESENTABLE_IN_D_BITS(12, N) \ - + IS_REPRESENTABLE_IN_D_BITS(13, N) \ - + IS_REPRESENTABLE_IN_D_BITS(14, N) \ - + IS_REPRESENTABLE_IN_D_BITS(15, N) \ - + IS_REPRESENTABLE_IN_D_BITS(16, N) \ - + IS_REPRESENTABLE_IN_D_BITS(17, N) \ - + IS_REPRESENTABLE_IN_D_BITS(18, N) \ - + IS_REPRESENTABLE_IN_D_BITS(19, N) \ - + IS_REPRESENTABLE_IN_D_BITS(20, N) \ - + IS_REPRESENTABLE_IN_D_BITS(21, N) \ - + IS_REPRESENTABLE_IN_D_BITS(22, N) \ - + IS_REPRESENTABLE_IN_D_BITS(23, N) \ - + IS_REPRESENTABLE_IN_D_BITS(24, N) \ - + IS_REPRESENTABLE_IN_D_BITS(25, N) \ - + IS_REPRESENTABLE_IN_D_BITS(26, N) \ - + IS_REPRESENTABLE_IN_D_BITS(27, N) \ - + IS_REPRESENTABLE_IN_D_BITS(28, N) \ - + IS_REPRESENTABLE_IN_D_BITS(29, N) \ - + IS_REPRESENTABLE_IN_D_BITS(30, N) \ - + IS_REPRESENTABLE_IN_D_BITS(31, N) \ - + IS_REPRESENTABLE_IN_D_BITS(32, N) \ - ) \ - ) +#define P_BITS (16) // log_2(p) = 15.6703 static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index 4fc49796..e52a7077 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -8,11 +8,11 @@ #include -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *const pk, - privateKeyNiederreiter_t *const sk, +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, + privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { - // sequence of N0 circ block matrices (p x p): Hi + // sequence of N0 circ block matrices (p x p): Hi POSITION_T HPosOnes[N0][DV]; POSITION_T HtrPosOnes[N0][DV]; /* Sparse representation of the transposed circulant matrix H, @@ -89,9 +89,10 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *co } -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT syndrome[], // 1 polynomial - const publicKeyNiederreiter_t *const pk, - const DIGIT err[]) { // N0 polynomials +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT *syndrome, // 1 polynomial + const publicKeyNiederreiter_t *pk, + const DIGIT *err) { // N0 polynomials + int i; DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; @@ -108,9 +109,9 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT syndrome[], } -int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT err[], // N0 circ poly - const privateKeyNiederreiter_t *const sk, - const DIGIT syndrome[]) { +int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, // N0 circ poly + const privateKeyNiederreiter_t *sk, + const DIGIT *syndrome) { AES_XOF_struct niederreiter_decrypt_expander; PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, From b320752f2a6e08268706103ddeed45ba126c696d Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 27 May 2019 23:58:49 +0200 Subject: [PATCH 14/35] fix more msvc warnings --- crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c | 2 +- crypto_kem/ledakemlt12/clean/niederreiter.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index 7d2add83..a5adb29e 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -94,7 +94,7 @@ static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amo } static uint8_t byte_reverse_with_64bitDIGIT(uint8_t b) { - b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023; + b = (uint8_t)((b * 0x0202020202ULL & 0x010884422010ULL) % 1023); return b; } diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index e52a7077..07da2d30 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -28,8 +28,8 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, /*Rejection-sample for a full L*/ POSITION_T LPosOnes[N0][DV * M]; - int is_L_full; - int isDFRok; + int is_L_full = 0; + int isDFRok = 0; sk->rejections = (int8_t) 0; do { PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, From 6f31e7e4c2949554bbbe029e66cc9f7387ec93e7 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 7 Jun 2019 13:02:25 +0200 Subject: [PATCH 15/35] serialize error_vector before hashing instead of pointer cast --- crypto_kem/ledakemlt12/clean/kem.c | 45 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt12/clean/kem.c index 004d3fb7..bf8413e7 100644 --- a/crypto_kem/ledakemlt12/clean/kem.c +++ b/crypto_kem/ledakemlt12/clean/kem.c @@ -8,45 +8,56 @@ /* Generates a keypair - pk is the public key and sk is the secret key. */ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { AES_XOF_struct niederreiter_keys_expander; + randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); - PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, - ((privateKeyNiederreiter_t *)sk)->prng_seed); - PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen((publicKeyNiederreiter_t *) pk, - (privateKeyNiederreiter_t *) sk, - &niederreiter_keys_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); + PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen((publicKeyNiederreiter_t *) pk, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + return 0; } +static void error_tobytes(uint8_t *error_bytes, const uint64_t *error_digits) { + size_t i, j, k; + uint64_t t; + + for (i = 0; i < N0; i++) { + for (j = 0; j < NUM_DIGITS_GF2X_ELEMENT; j++) { + t = error_digits[i * NUM_DIGITS_GF2X_ELEMENT + j]; + for (k = 0; k < DIGIT_SIZE_B; k++) { + error_bytes[(i * NUM_DIGITS_GF2X_ELEMENT + j) * DIGIT_SIZE_B + k] = (uint8_t) ((t >> (8 * k)) & 0xFF); + } + } + } +} + /* Encrypt - pk is the public key, ct is a key encapsulation message (ciphertext), ss is the shared secret.*/ -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc( unsigned char *ct, unsigned char *ss, const unsigned char *pk) { +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { AES_XOF_struct niederreiter_encap_key_expander; unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + uint8_t error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); - PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); - - HASH_FUNCTION(ss, (const uint8_t *) error_vector, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); - + error_tobytes(error_bytes, error_vector); + HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); + return 0; } /* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private key, ss is the shared secret */ -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, - const unsigned char *ct, - const unsigned char *sk ) { +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; - PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, - (privateKeyNiederreiter_t *)sk, - (DIGIT *)ct); - HASH_FUNCTION(ss, (const unsigned char *) decoded_error_vector, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); + PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, (DIGIT *)ct); + error_tobytes(decoded_error_bytes, decoded_error_vector); + HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); return 0; } From e4add578445ddbf27c8ac72b35fd12ac97cbadf6 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 7 Jun 2019 13:40:02 +0200 Subject: [PATCH 16/35] more cleaning --- .../clean/H_Q_matrices_generation.h | 12 +-- crypto_kem/ledakemlt12/clean/Makefile | 2 +- crypto_kem/ledakemlt12/clean/bf_decoding.h | 4 +- crypto_kem/ledakemlt12/clean/gf2x_arith.h | 13 ++-- .../clean/gf2x_arith_mod_xPplusOne.c | 58 +++++++------- .../clean/gf2x_arith_mod_xPplusOne.h | 75 ++++++------------- crypto_kem/ledakemlt12/clean/gf2x_limbs.h | 18 ----- crypto_kem/ledakemlt12/clean/kem.c | 17 ++--- crypto_kem/ledakemlt12/clean/niederreiter.c | 47 ++++-------- crypto_kem/ledakemlt12/clean/niederreiter.h | 21 +----- crypto_kem/ledakemlt12/clean/rng.h | 3 - 11 files changed, 85 insertions(+), 185 deletions(-) delete mode 100644 crypto_kem/ledakemlt12/clean/gf2x_limbs.h diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h index 11509bf4..8274dcd9 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h @@ -1,17 +1,11 @@ #ifndef H_Q_MATRICES_GENERATION_H #define H_Q_MATRICES_GENERATION_H -#include "gf2x_limbs.h" +#include "gf2x_arith.h" #include "qc_ldpc_parameters.h" #include "rng.h" -void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes( - POSITION_T HPosOnes[N0][DV], - POSITION_T HtrPosOnes[N0][DV], - AES_XOF_struct *niederreiter_keys_expander); - -void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse( - POSITION_T pos_ones[N0][M], - AES_XOF_struct *niederreiter_keys_expander); +void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); +void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); #endif diff --git a/crypto_kem/ledakemlt12/clean/Makefile b/crypto_kem/ledakemlt12/clean/Makefile index 08c019ee..6d6fa541 100644 --- a/crypto_kem/ledakemlt12/clean/Makefile +++ b/crypto_kem/ledakemlt12/clean/Makefile @@ -2,7 +2,7 @@ LIB=libledakemlt12_clean.a HEADERS=api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ - gf2x_arith.h gf2x_limbs.h H_Q_matrices_generation.h \ + gf2x_arith.h H_Q_matrices_generation.h \ niederreiter.h qc_ldpc_parameters.h rng.h OBJECTS=bf_decoding.o dfr_test.o gf2x_arith_mod_xPplusOne.o \ diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.h b/crypto_kem/ledakemlt12/clean/bf_decoding.h index 927a5e71..18a7dd8b 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.h @@ -1,7 +1,7 @@ #ifndef BF_DECODING_H #define BF_DECODING_H -#include "gf2x_limbs.h" +#include "gf2x_arith.h" #include "qc_ldpc_parameters.h" /* Definitions for DFR level 2^-SL with SL=128 */ @@ -11,7 +11,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], - const POSITION_T QtrPosOnes[N0][M], // N0 vectors containing exp.s of Qtr ones + const POSITION_T QtrPosOnes[N0][M], DIGIT privateSyndrome[]); #endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.h b/crypto_kem/ledakemlt12/clean/gf2x_arith.h index b9a512a9..032dfd57 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.h @@ -1,7 +1,8 @@ #ifndef GF2X_ARITH_H #define GF2X_ARITH_H -#include "gf2x_limbs.h" +#include +#include /* * Elements of GF(2)[x] are stored in compact dense binary form. @@ -42,9 +43,12 @@ * position[A_{0}] == n-1 */ +typedef uint64_t DIGIT; +#define DIGIT_SIZE_B (8) +#define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) +#define POSITION_T uint32_t #define GF2X_MUL PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb -// #define GF2X_MUL gf2x_mul_comb static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], size_t nr) { for (size_t i = 0; i < nr; i++) { @@ -52,13 +56,8 @@ static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], size_ } } -/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); - -/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); - void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); - #endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index a5adb29e..e4b0557a 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -7,7 +7,7 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { - int i, j, posTrailingBit, maskOffset; + int i, j, posTrailingBit, maskOffset, to_copy; DIGIT mask, aux[2 * NUM_DIGITS_GF2X_ELEMENT]; memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); @@ -46,7 +46,7 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { } } - int to_copy = (2 * NUM_DIGITS_GF2X_ELEMENT > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : 2 * NUM_DIGITS_GF2X_ELEMENT; + to_copy = (2 * NUM_DIGITS_GF2X_ELEMENT > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : 2 * NUM_DIGITS_GF2X_ELEMENT; for (i = 0; i < to_copy; i++) { out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; @@ -93,31 +93,19 @@ static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amo PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); } -static uint8_t byte_reverse_with_64bitDIGIT(uint8_t b) { - b = (uint8_t)((b * 0x0202020202ULL & 0x010884422010ULL) % 1023); - return b; -} - -/* https://stackoverflow.com/questions/2182002/convert-big-endian-to-little-endian-in-c-without-using-provided-func */ -static uint64_t swap_uint64( uint64_t val ) { - val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL ); - val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL ); - return (val << 32) | (val >> 32); -} - -static DIGIT reverse_digit(const DIGIT b) { - int i; - union toReverse_t { - uint8_t inByte[DIGIT_SIZE_B]; - DIGIT digitValue; - } toReverse; - - toReverse.digitValue = b; - for (i = 0; i < DIGIT_SIZE_B; i++) { - toReverse.inByte[i] = byte_reverse_with_64bitDIGIT(toReverse.inByte[i]); - } - - return swap_uint64(toReverse.digitValue); +/* Hackers delight, reverses a uint64_t */ +static DIGIT reverse_digit(DIGIT x) { + uint64_t t; + x = (x << 31) | (x >> 33); + t = (x ^ (x >> 20)) & 0x00000FFF800007FFLL; + x = (t | (t << 20)) ^ x; + t = (x ^ (x >> 8)) & 0x00F8000F80700807LL; + x = (t | (t << 8)) ^ x; + t = (x ^ (x >> 4)) & 0x0808708080807008LL; + x = (t | (t << 4)) ^ x; + t = (x ^ (x >> 2)) & 0x1111111111111111LL; + x = (t | (t << 2)) ^ x; + return x; } void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { @@ -137,11 +125,11 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { A[i] = rev2; A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; } - /* + + if (NUM_DIGITS_GF2X_ELEMENT % 2 == 1) { A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); - }*/ - A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); + } if (slack_bits_amount) { PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); @@ -220,7 +208,6 @@ static void gf2x_swap(const int length, * (Chapter 11 -- Algorithm 11.44 -- pag 223) * */ - int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ int i; @@ -533,3 +520,12 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( } } + +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { + size_t i, j; + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { + for (j = 0; j < DIGIT_SIZE_B; j++) { + bytes[i * DIGIT_SIZE_B + j] = (uint8_t) ((poly[i] >> (8 * j)) & 0xFF); + } + } +} diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h index f41eb8ef..af42422f 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h @@ -1,23 +1,19 @@ #ifndef GF2X_ARITH_MOD_XPLUSONE_H #define GF2X_ARITH_MOD_XPLUSONE_H -#include "gf2x_limbs.h" #include "qc_ldpc_parameters.h" #include "gf2x_arith.h" #include "rng.h" -#define NUM_BITS_GF2X_ELEMENT (P) // 52147 -#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) // 815 -#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ( (P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1 ) - -#define NUM_BITS_GF2X_MODULUS (P+1) -#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) -#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) - -#define INVALID_POS_VALUE (P) - -#define P_BITS (16) // log_2(p) = 15.6703 +#define NUM_BITS_GF2X_ELEMENT (P) // 52147 +#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) +#define NUM_BITS_GF2X_MODULUS (P+1) +#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) +#define INVALID_POS_VALUE (P) +#define P_BITS (16) // log_2(p) = 15.6703 static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { @@ -43,8 +39,7 @@ static inline void gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT val /* clear given coefficient */ DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); poly[digitIdx] = poly[digitIdx] & mask; - poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << - (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); } /* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ @@ -58,8 +53,8 @@ static inline void gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { poly[digitIdx] = poly[digitIdx] ^ mask; } - -/* population count for an unsigned 64-bit integer */ +/* population count for an unsigned 64-bit integer + Source: Hacker's delight, p.66 */ static int popcount_uint64t(uint64_t x) { x -= (x >> 1) & 0x5555555555555555; x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); @@ -80,42 +75,6 @@ static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); - -int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); - -/* in place bit-transp. of a(x) % x^P+1, e.g.: a3 a2 a1 a0 --> a1 a2 a3 a0 */ -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]); - -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block( - POSITION_T *pos_ones, - int countOnes, - AES_XOF_struct *seed_expander_ctx); - -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( - DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], - AES_XOF_struct *seed_expander_ctx); - -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse( - int sizeR, POSITION_T Res[], - int sizeA, const POSITION_T A[], - int sizeB, const POSITION_T B[]); - -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse( - int sizeA, - POSITION_T A[]); - -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse( - size_t sizeR, POSITION_T Res[], - size_t sizeA, const POSITION_T A[], - size_t sizeB, const POSITION_T B[]); - -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse( - DIGIT Res[], - const DIGIT dense[], - POSITION_T sparse[], - unsigned int nPos); - static inline int partition(POSITION_T arr[], int lo, int hi) { POSITION_T x = arr[hi]; POSITION_T tmp; @@ -155,4 +114,16 @@ static inline void quicksort_sparse(POSITION_T Res[]) { } } +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]); +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); + +int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); + #endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h b/crypto_kem/ledakemlt12/clean/gf2x_limbs.h deleted file mode 100644 index e4082a6d..00000000 --- a/crypto_kem/ledakemlt12/clean/gf2x_limbs.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef GF2X_LIMBS_H -#define GF2X_LIMBS_H - -#include "qc_ldpc_parameters.h" - -#include -#include -#include - -typedef uint64_t DIGIT; -#define DIGIT_IS_UINT64 -#define DIGIT_IS_ULLONG -#define DIGIT_SIZE_B (8) -#define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) - -#define POSITION_T uint32_t - -#endif diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt12/clean/kem.c index bf8413e7..8a2d1dc4 100644 --- a/crypto_kem/ledakemlt12/clean/kem.c +++ b/crypto_kem/ledakemlt12/clean/kem.c @@ -16,17 +16,10 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned cha return 0; } -static void error_tobytes(uint8_t *error_bytes, const uint64_t *error_digits) { - size_t i, j, k; - uint64_t t; - +static void pack_error(uint8_t *error_bytes, const DIGIT *error_digits) { + size_t i; for (i = 0; i < N0; i++) { - for (j = 0; j < NUM_DIGITS_GF2X_ELEMENT; j++) { - t = error_digits[i * NUM_DIGITS_GF2X_ELEMENT + j]; - for (k = 0; k < DIGIT_SIZE_B; k++) { - error_bytes[(i * NUM_DIGITS_GF2X_ELEMENT + j) * DIGIT_SIZE_B + k] = (uint8_t) ((t >> (8 * k)) & 0xFF); - } - } + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, error_digits + i * NUM_DIGITS_GF2X_ELEMENT); } } @@ -41,7 +34,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); - error_tobytes(error_bytes, error_vector); + pack_error(error_bytes, error_vector); HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); @@ -56,7 +49,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned c uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, (DIGIT *)ct); - error_tobytes(decoded_error_bytes, decoded_error_vector); + pack_error(decoded_error_bytes, decoded_error_vector); HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); return 0; diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index 07da2d30..89005c5f 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -8,9 +8,7 @@ #include -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, - privateKeyNiederreiter_t *sk, - AES_XOF_struct *keys_expander) { +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { // sequence of N0 circ block matrices (p x p): Hi POSITION_T HPosOnes[N0][DV]; @@ -32,12 +30,8 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, int isDFRok = 0; sk->rejections = (int8_t) 0; do { - PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, - HtrPosOnes, - keys_expander); - - PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, - keys_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, keys_expander); for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { LPosOnes[i][j] = INVALID_POS_VALUE; @@ -89,10 +83,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT *syndrome, // 1 polynomial - const publicKeyNiederreiter_t *pk, - const DIGIT *err) { // N0 polynomials - +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { int i; DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; @@ -101,21 +92,17 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT *syndrome, // 1 polyn for (i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(saux, pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, - err + i * NUM_DIGITS_GF2X_ELEMENT - ); + err + i * NUM_DIGITS_GF2X_ELEMENT); gf2x_mod_add(syndrome, syndrome, saux); } // end for gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } -int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, // N0 circ poly - const privateKeyNiederreiter_t *sk, - const DIGIT *syndrome) { +int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { AES_XOF_struct niederreiter_decrypt_expander; - PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, - sk->prng_seed); + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); // sequence of N0 circ block matrices (p x p): POSITION_T HPosOnes[N0][DV]; @@ -124,8 +111,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, // N0 circ poly int rejections = sk->rejections; POSITION_T LPosOnes[N0][DV * M]; do { - PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, - &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { @@ -173,28 +159,25 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, // N0 circ poly for (int i = 0; i < N0; i++) { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxSparse, - DV, HPosOnes[i], - qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1] ] - ); + DV, HPosOnes[i], + qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1]]); PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(DV * M, Ln0trSparse, DV * M, Ln0trSparse, - DV * M, auxSparse - ); + DV * M, auxSparse); } // end for i + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, - syndrome, - Ln0trSparse, - DV * M); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); /* prepare mockup error vector in case a decoding failure occurs */ DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(&niederreiter_decrypt_expander, - ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), TRNG_BYTE_LENGTH); + ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), + TRNG_BYTE_LENGTH); int decryptOk = 0; memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.h b/crypto_kem/ledakemlt12/clean/niederreiter.h index efdd848a..0692581b 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.h +++ b/crypto_kem/ledakemlt12/clean/niederreiter.h @@ -2,7 +2,6 @@ #define NIEDERREITER_H #include "gf2x_arith_mod_xPplusOne.h" -#include "gf2x_limbs.h" #include "qc_ldpc_parameters.h" #include "rng.h" @@ -21,23 +20,9 @@ typedef struct { // with P coefficients. } publicKeyNiederreiter_t; - - -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen( - publicKeyNiederreiter_t *pk, - privateKeyNiederreiter_t *sk, - AES_XOF_struct *keys_expander); - -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt( - DIGIT syndrome[], - const publicKeyNiederreiter_t *pk, - const DIGIT *err); - -// return 1 if everything is ok -int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt( - DIGIT *err, - const privateKeyNiederreiter_t *sk, - const DIGIT *syndrome); +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); +void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); +int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); #endif diff --git a/crypto_kem/ledakemlt12/clean/rng.h b/crypto_kem/ledakemlt12/clean/rng.h index 646bc86b..b34dbbc6 100644 --- a/crypto_kem/ledakemlt12/clean/rng.h +++ b/crypto_kem/ledakemlt12/clean/rng.h @@ -17,10 +17,7 @@ typedef struct { unsigned char ctr[16]; } AES_XOF_struct; - int PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); - -/* TRNG_BYTE_LENGTH wide buffer */ void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); #endif From 48912d76ffea2e232306f218f2a3912e49f45303 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 7 Jun 2019 13:57:15 +0200 Subject: [PATCH 17/35] msvc warning + removing commented code --- crypto_kem/ledakemlt12/clean/gf2x_arith.c | 282 ------------------ .../clean/gf2x_arith_mod_xPplusOne.c | 45 +-- 2 files changed, 2 insertions(+), 325 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/clean/gf2x_arith.c index 31e505f1..c2d76059 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.c @@ -98,285 +98,3 @@ static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { t >>= DIGIT_SIZE_b - 1; } } - -// #define MIN_KAR_DIGITS 20 -// -// static void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(const int nr, DIGIT Res[], -// const int na, const DIGIT A[], -// const int nb, const DIGIT B[]) { -// -// if (na < MIN_KAR_DIGITS || nb < MIN_KAR_DIGITS) { -// /* fall back to schoolbook */ -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(nr, Res, na, A, nb, B); -// return; -// } -// -// if (na % 2 == 0) { -// unsigned bih = na / 2; -// DIGIT middle[2 * bih], sumA[bih], sumB[bih]; -// gf2x_add(sumA, A, A + bih, bih); -// gf2x_add(sumB, B, B + bih, bih); -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, middle, -// bih, sumA, -// bih, sumB); -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res + 2 * bih, -// bih, A + bih, -// bih, B + bih); -// gf2x_add(middle, middle, Res + 2 * bih, 2 * bih); -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res, -// bih, A, -// bih, B); -// gf2x_add(middle, middle, Res, 2 * bih); -// gf2x_add(Res + bih, Res + bih, middle, 2 * bih); -// } else { -// unsigned bih = na / 2 + 1; -// DIGIT middle[2 * bih], sumA[bih], sumB[bih]; -// gf2x_add_asymm(bih, sumA, -// bih, A + bih - 1, -// bih - 1, A); -// gf2x_add_asymm(bih, sumB, -// bih, B + bih - 1, -// bih - 1, B); -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, middle, -// bih, sumA, -// bih, sumB); -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * bih, Res + 2 * (bih - 1), -// bih, A + bih - 1, -// bih, B + bih - 1); -// gf2x_add(middle, middle, Res + 2 * (bih - 1), 2 * bih); -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(2 * (bih - 1), Res, -// (bih - 1), A, -// (bih - 1), B); -// gf2x_add_asymm(2 * bih, middle, -// 2 * bih, middle, -// 2 * (bih - 1), Res); -// gf2x_add(Res + bih - 2, Res + bih - 2, middle, 2 * bih); -// } -// } -// -// #define MIN_TOOM_DIGITS 35 -// -// void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(const int nr, DIGIT Res[], -// const int na, const DIGIT A[], -// const int nb, const DIGIT B[]) { -// -// if (na < MIN_TOOM_DIGITS || nb < MIN_TOOM_DIGITS) { -// /* fall back to Karatsuba */ -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_Kar(nr, Res, na, A, nb, B); -// return; -// } -// -// int bih; //number of limbs for each part. -// if (na % 3 == 0) { -// bih = na / 3; -// } else { -// bih = na / 3 + 1; -// } -// -// DIGIT u2[bih], u1[bih], u0[bih]; -// -// int leading_slack = (3 - (na) % 3) % 3; -// // printf("leading slack %d",leading_slack); -// int i; -// for (i = 0; i < leading_slack ; i++) { -// u2[i] = 0; -// } -// for (; i < bih; ++i) { -// u2[i] = A[i - leading_slack]; -// } -// /* note: only u2 needs to be a copy, refactor */ -// for (; i < 2 * bih; ++i) { -// u1[i - bih] = A[i - leading_slack]; -// } -// for (; i < 3 * bih; ++i) { -// u0[i - 2 * bih] = A[i - leading_slack]; -// } -// -// DIGIT v2[bih], v1[bih], v0[bih]; /* partitioned inputs */ -// /* note: only v2 needs to be a copy, refactor */ -// for (i = 0; i < leading_slack ; i++) { -// v2[i] = 0; -// } -// for (; i < bih; ++i) { -// v2[i] = B[i - leading_slack]; -// } -// /* note , only v2 needs to be a copy */ -// for (; i < 2 * bih; ++i) { -// v1[i - bih] = B[i - leading_slack]; -// } -// for (; i < 3 * bih; ++i) { -// v0[i - 2 * bih] = B[i - leading_slack]; -// } -// -// DIGIT sum_u[bih]; /*bih digit wide*/ -// gf2x_add(sum_u, u0, u1, bih); -// gf2x_add(sum_u, sum_u, u2, bih); -// -// DIGIT sum_v[bih]; /*bih digit wide*/ -// gf2x_add(sum_v, v0, v1, bih); -// gf2x_add(sum_v, sum_v, v2, bih); -// -// -// DIGIT w1[2 * bih]; -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w1, -// bih, sum_u, -// bih, sum_v); -// -// -// DIGIT u2_x2[bih + 1]; -// u2_x2[0] = 0; -// memcpy(u2_x2 + 1, u2, bih * DIGIT_SIZE_B); -// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, u2_x2, 2); -// -// DIGIT u1_x[bih + 1]; -// u1_x[0] = 0; -// memcpy(u1_x + 1, u1, bih * DIGIT_SIZE_B); -// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, u1_x, 1); -// -// DIGIT u1_x1_u2_x2[bih + 1]; -// gf2x_add(u1_x1_u2_x2, u1_x, u2_x2, bih + 1); -// -// DIGIT temp_u_components[bih + 1]; -// gf2x_add_asymm(bih + 1, temp_u_components, -// bih + 1, u1_x1_u2_x2, -// bih, sum_u); -// -// DIGIT v2_x2[bih + 1]; -// v2_x2[0] = 0; -// memcpy(v2_x2 + 1, v2, bih * DIGIT_SIZE_B); -// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, v2_x2, 2); -// -// DIGIT v1_x[bih + 1]; -// v1_x[0] = 0; -// memcpy(v1_x + 1, v1, bih * DIGIT_SIZE_B); -// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(bih + 1, v1_x, 1); -// -// DIGIT v1_x1_v2_x2[bih + 1]; -// gf2x_add(v1_x1_v2_x2, v1_x, v2_x2, bih + 1); -// -// DIGIT temp_v_components[bih + 1]; -// gf2x_add_asymm(bih + 1, temp_v_components, -// bih + 1, v1_x1_v2_x2, -// bih, sum_v); -// -// DIGIT w3[2 * bih + 2]; -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih + 2, w3, -// bih + 1, temp_u_components, -// bih + 1, temp_v_components); -// -// gf2x_add_asymm(bih + 1, u1_x1_u2_x2, -// bih + 1, u1_x1_u2_x2, -// bih, u0); -// gf2x_add_asymm(bih + 1, v1_x1_v2_x2, -// bih + 1, v1_x1_v2_x2, -// bih, v0); -// -// DIGIT w2[2 * bih + 2]; -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih + 2, w2, -// bih + 1, u1_x1_u2_x2, -// bih + 1, v1_x1_v2_x2); -// -// DIGIT w4[2 * bih]; -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w4, -// bih, u2, -// bih, v2); -// DIGIT w0[2 * bih]; -// PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_TC3(2 * bih, w0, -// bih, u0, -// bih, v0); -// -// // Interpolation starts -// gf2x_add(w3, w2, w3, 2 * bih + 2); -// gf2x_add_asymm(2 * bih + 2, w2, -// 2 * bih + 2, w2, -// 2 * bih, w0); -// PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(2 * bih + 2, w2, 1); -// gf2x_add(w2, w2, w3, 2 * bih + 2); -// -// // w2 + (w4 * x^3+1) = w2 + w4 + w4 << 3 -// DIGIT w4_x3_plus_1[2 * bih + 1]; -// w4_x3_plus_1[0] = 0; -// memcpy(w4_x3_plus_1 + 1, w4, 2 * bih * DIGIT_SIZE_B); -// PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(2 * bih + 1, w4_x3_plus_1, 3); -// gf2x_add_asymm(2 * bih + 2, w2, -// 2 * bih + 2, w2, -// 2 * bih, w4); -// gf2x_add_asymm(2 * bih + 2, w2, -// 2 * bih + 2, w2, -// 2 * bih + 1, w4_x3_plus_1); -// -// gf2x_exact_div_x_plus_one(2 * bih + 2, w2); -// -// gf2x_add(w1, w1, w0, 2 * bih); -// gf2x_add_asymm(2 * bih + 2, w3, -// 2 * bih + 2, w3, -// 2 * bih, w1); -// -// PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(2 * bih + 2, w3, 1); -// gf2x_exact_div_x_plus_one(2 * bih + 2, w3); -// -// gf2x_add(w1, w1, w4, 2 * bih); -// -// DIGIT w1_final[2 * bih + 2]; -// gf2x_add_asymm(2 * bih + 2, w1_final, -// 2 * bih + 2, w2, -// 2 * bih, w1); -// gf2x_add(w2, w2, w3, 2 * bih + 2); -// -// // Result recombination starts here -// -// memset(Res, 0, nr * DIGIT_SIZE_B); -// /* optimization: topmost slack digits should be computed, and not addedd, -// * zeroization can be avoided altogether with a proper merge of the -// * results */ -// -// int leastSignifDigitIdx = nr - 1; -// for (int i = 0; i < 2 * bih; i++) { -// Res[leastSignifDigitIdx - i] ^= w0[2 * bih - 1 - i]; -// } -// leastSignifDigitIdx -= bih; -// for (int i = 0; i < 2 * bih + 2; i++) { -// Res[leastSignifDigitIdx - i] ^= w1_final[2 * bih + 2 - 1 - i]; -// } -// leastSignifDigitIdx -= bih; -// for (int i = 0; i < 2 * bih + 2; i++) { -// Res[leastSignifDigitIdx - i] ^= w2[2 * bih + 2 - 1 - i]; -// } -// leastSignifDigitIdx -= bih; -// for (int i = 0; i < 2 * bih + 2 ; i++) { -// Res[leastSignifDigitIdx - i] ^= w3[2 * bih + 2 - 1 - i]; -// } -// leastSignifDigitIdx -= bih; -// for (int i = 0; i < 2 * bih && (leastSignifDigitIdx - i >= 0) ; i++) { -// Res[leastSignifDigitIdx - i] ^= w4[2 * bih - 1 - i]; -// } -// } -// // Unused -// static int gf2x_cmp(const unsigned lenA, const DIGIT A[], -// const unsigned lenB, const DIGIT B[]) { -// -// int i; -// unsigned lA = lenA, lB = lenB; -// for (i = 0; i < lenA && A[i] == 0; i++) { -// lA--; -// } -// for (i = 0; i < lenB && B[i] == 0; i++) { -// lB--; -// } -// if (lA < lB) { -// return -1; -// } -// if (lA > lB) { -// return +1; -// } -// for (i = 0; i < lA; i++) { -// if (A[i] > B[i]) { -// return +1; -// } -// if (A[i] < B[i]) { -// return -1; -// } -// } -// return 0; -// -// } diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index e4b0557a..5ca7aa83 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -126,10 +126,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; } - - if (NUM_DIGITS_GF2X_ELEMENT % 2 == 1) { - A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); - } + A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // reverse middle digit if (slack_bits_amount) { PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); @@ -140,21 +137,6 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { static void rotate_bit_left(DIGIT in[]) { /* equivalent to x * in(x) mod x^P+1 */ DIGIT mask, rotated_bit; - /* - if (NUM_DIGITS_GF2X_MODULUS == NUM_DIGITS_GF2X_ELEMENT) { - int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; - mask = ((DIGIT)0x1) << msb_offset_in_digit; - rotated_bit = !!(in[0] & mask); - in[0] &= ~mask; - left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); - } else { - mask = ((DIGIT)0x1) << (DIGIT_SIZE_b - 1); - rotated_bit = !!(in[0] & mask); - in[0] &= ~mask; - left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); - - } */ - int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; mask = ((DIGIT)0x1) << msb_offset_in_digit; rotated_bit = !!(in[0] & mask); @@ -167,14 +149,6 @@ static void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ DIGIT rotated_bit = in[NUM_DIGITS_GF2X_ELEMENT - 1] & ((DIGIT)0x1); right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); - - /* - if (NUM_DIGITS_GF2X_MODULUS == NUM_DIGITS_GF2X_ELEMENT) { - int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; - rotated_bit = rotated_bit << msb_offset_in_digit; - } else { - rotated_bit = rotated_bit << (DIGIT_SIZE_b - 1); - } */ int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; rotated_bit = rotated_bit << msb_offset_in_digit; in[0] |= rotated_bit; @@ -222,12 +196,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { v[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x0; s[NUM_DIGITS_GF2X_MODULUS - 1] = 0x1; - /* - if (MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS == 0) { - mask = 0x1; - } else { - mask = (((DIGIT)0x1) << MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS); - }*/ + mask = (((DIGIT)0x1) << MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS); s[0] |= mask; @@ -236,16 +205,6 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { return 0; } - /* - if (NUM_DIGITS_GF2X_MODULUS == 1 + NUM_DIGITS_GF2X_ELEMENT) { - for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 1 ; i--) { - f[i] = in[i - 1]; - } - } else { - for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 0 ; i--) { - f[i] = in[i]; - } - }*/ for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 0 ; i--) { f[i] = in[i]; } From c0aa56018604713e588a575f0c8a12db83c5f51c Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 7 Jun 2019 15:07:22 +0200 Subject: [PATCH 18/35] remove commented code, update license --- crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c | 8 ++++---- crypto_kem/ledakemlt12/clean/LICENSE | 3 +-- crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c | 9 --------- crypto_kem/ledakemlt12/clean/niederreiter.c | 4 ++-- 4 files changed, 7 insertions(+), 17 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c index 9b603c5a..3bc2159f 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c @@ -15,11 +15,11 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes( /* Obtain directly the sparse representation of the block of H */ for (int k = 0; k < DV; k++) { HPosOnes[i][k] = (P - HtrPosOnes[i][k]) % P; /* transposes indexes */ - }// end for k + } } } -void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse( +void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse( POSITION_T pos_ones[N0][M], AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { @@ -29,6 +29,6 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse( qBlockWeights[i][j], keys_expander); placed_ones += qBlockWeights[i][j]; - } // end for j - } // end for i + } + } } diff --git a/crypto_kem/ledakemlt12/clean/LICENSE b/crypto_kem/ledakemlt12/clean/LICENSE index f6004c1a..c1761078 100644 --- a/crypto_kem/ledakemlt12/clean/LICENSE +++ b/crypto_kem/ledakemlt12/clean/LICENSE @@ -4,8 +4,7 @@ * * @version 2.0 (March 2019) * - * Reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher using GCC built-ins. - * Also contains public domain implementations of AES and Keccak + * Adapted code from reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher. * * In alphabetical order: * diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index 5ca7aa83..80c2f72f 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -13,15 +13,6 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memset(out, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - /* not true for parameter set - if (2 * NUM_DIGITS_GF2X_ELEMENT < NUM_DIGITS_GF2X_MODULUS) { - for (i = 0; i < 2 * NUM_DIGITS_GF2X_ELEMENT; i++) { - out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = in[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; - } - return; - } - */ - for (i = 0; i < (2 * NUM_DIGITS_GF2X_ELEMENT) - NUM_DIGITS_GF2X_MODULUS; i += 1) { for (j = DIGIT_SIZE_b - 1; j >= 0; j--) { mask = ((DIGIT)0x1) << j; diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index 89005c5f..58c4542f 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -164,7 +164,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(DV * M, Ln0trSparse, DV * M, Ln0trSparse, DV * M, auxSparse); - } // end for i + } PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); @@ -190,7 +190,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); - if (!decryptOk) { // TODO: not constant time + if (!decryptOk) { // TODO: not constant time, replace with cmov? memcpy(err, mockup_error_vector, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); } From 6aafab57efa7bb5434b724c2d35d8b00ffc1ac74 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 10 Jun 2019 18:57:26 +0200 Subject: [PATCH 19/35] add ledakemlt52 --- .../clean/H_Q_matrices_generation.c | 4 +- crypto_kem/ledakemlt12/clean/bf_decoding.c | 3 - crypto_kem/ledakemlt52/META.yml | 18 + .../clean/H_Q_matrices_generation.c | 32 ++ .../clean/H_Q_matrices_generation.h | 11 + crypto_kem/ledakemlt52/clean/LICENSE | 31 ++ crypto_kem/ledakemlt52/clean/Makefile | 24 + .../clean/Makefile.Microsoft_nmake | 19 + crypto_kem/ledakemlt52/clean/api.h | 18 + crypto_kem/ledakemlt52/clean/bf_decoding.c | 77 +++ crypto_kem/ledakemlt52/clean/bf_decoding.h | 17 + crypto_kem/ledakemlt52/clean/dfr_test.c | 120 +++++ crypto_kem/ledakemlt52/clean/dfr_test.h | 6 + crypto_kem/ledakemlt52/clean/gf2x_arith.c | 100 ++++ crypto_kem/ledakemlt52/clean/gf2x_arith.h | 63 +++ .../clean/gf2x_arith_mod_xPplusOne.c | 481 ++++++++++++++++++ .../clean/gf2x_arith_mod_xPplusOne.h | 129 +++++ crypto_kem/ledakemlt52/clean/kem.c | 56 ++ crypto_kem/ledakemlt52/clean/niederreiter.c | 198 +++++++ crypto_kem/ledakemlt52/clean/niederreiter.h | 28 + .../ledakemlt52/clean/qc_ldpc_parameters.h | 27 + crypto_kem/ledakemlt52/clean/rng.c | 113 ++++ crypto_kem/ledakemlt52/clean/rng.h | 23 + 23 files changed, 1592 insertions(+), 6 deletions(-) create mode 100644 crypto_kem/ledakemlt52/META.yml create mode 100644 crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.c create mode 100644 crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.h create mode 100644 crypto_kem/ledakemlt52/clean/LICENSE create mode 100644 crypto_kem/ledakemlt52/clean/Makefile create mode 100644 crypto_kem/ledakemlt52/clean/Makefile.Microsoft_nmake create mode 100644 crypto_kem/ledakemlt52/clean/api.h create mode 100644 crypto_kem/ledakemlt52/clean/bf_decoding.c create mode 100644 crypto_kem/ledakemlt52/clean/bf_decoding.h create mode 100644 crypto_kem/ledakemlt52/clean/dfr_test.c create mode 100644 crypto_kem/ledakemlt52/clean/dfr_test.h create mode 100644 crypto_kem/ledakemlt52/clean/gf2x_arith.c create mode 100644 crypto_kem/ledakemlt52/clean/gf2x_arith.h create mode 100644 crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c create mode 100644 crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h create mode 100644 crypto_kem/ledakemlt52/clean/kem.c create mode 100644 crypto_kem/ledakemlt52/clean/niederreiter.c create mode 100644 crypto_kem/ledakemlt52/clean/niederreiter.h create mode 100644 crypto_kem/ledakemlt52/clean/qc_ldpc_parameters.h create mode 100644 crypto_kem/ledakemlt52/clean/rng.c create mode 100644 crypto_kem/ledakemlt52/clean/rng.h diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c index 3bc2159f..ad9ce98c 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c +++ b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c @@ -7,9 +7,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes( AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { /* Generate a random block of Htr */ - PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(&HtrPosOnes[i][0], - DV, - keys_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); } for (int i = 0; i < N0; i++) { /* Obtain directly the sparse representation of the block of H */ diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/clean/bf_decoding.c index cd55fd83..f03deb28 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.c @@ -4,9 +4,6 @@ #include #include -#define ROTBYTE(a) ( ((a) << 8) | ((a) >> (DIGIT_SIZE_b - 8)) ) -#define ROTUPC(a) ( ((a) >> 8) | ((a) << (DIGIT_SIZE_b - 8)) ) - unsigned int thresholds[2] = {B0, (DV * M) / 2 + 1}; int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], diff --git a/crypto_kem/ledakemlt52/META.yml b/crypto_kem/ledakemlt52/META.yml new file mode 100644 index 00000000..5751adc4 --- /dev/null +++ b/crypto_kem/ledakemlt52/META.yml @@ -0,0 +1,18 @@ +name: LEDAcryptKEMLT52 +type: kem +claimed-nist-level: 5 +claimed-security: IND-CCA2 +length-public-key: 19040 +length-secret-key: 41 +length-ciphertext: 19040 +length-shared-secret: 64 +nistkat-sha256: c53eaa3c3573a4db989671994e9501dbbc080b9c86106787f438960848c71326 +principal-submitter: Marco Baldi +auxiliary-submitters: + - Alessandro Barenghi + - Franco Chiaraluce + - Gerardo Pelosi + - Paolo Santini +implementations: + - name: clean + version: 2.0 diff --git a/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.c new file mode 100644 index 00000000..ff832abf --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.c @@ -0,0 +1,32 @@ +#include "H_Q_matrices_generation.h" +#include "gf2x_arith_mod_xPplusOne.h" + +void PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes( + POSITION_T HPosOnes[N0][DV], + POSITION_T HtrPosOnes[N0][DV], + AES_XOF_struct *keys_expander) { + for (int i = 0; i < N0; i++) { + /* Generate a random block of Htr */ + PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); + } + for (int i = 0; i < N0; i++) { + /* Obtain directly the sparse representation of the block of H */ + for (int k = 0; k < DV; k++) { + HPosOnes[i][k] = (P - HtrPosOnes[i][k]) % P; /* transposes indexes */ + } + } +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse( + POSITION_T pos_ones[N0][M], + AES_XOF_struct *keys_expander) { + for (int i = 0; i < N0; i++) { + int placed_ones = 0; + for (int j = 0; j < N0; j++) { + PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(&pos_ones[i][placed_ones], + qBlockWeights[i][j], + keys_expander); + placed_ones += qBlockWeights[i][j]; + } + } +} diff --git a/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.h new file mode 100644 index 00000000..18a8cd5c --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.h @@ -0,0 +1,11 @@ +#ifndef H_Q_MATRICES_GENERATION_H +#define H_Q_MATRICES_GENERATION_H + +#include "gf2x_arith.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +void PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); +void PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); + +#endif diff --git a/crypto_kem/ledakemlt52/clean/LICENSE b/crypto_kem/ledakemlt52/clean/LICENSE new file mode 100644 index 00000000..c1761078 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/LICENSE @@ -0,0 +1,31 @@ +/** + * + * LEDAcryptKEM + * + * @version 2.0 (March 2019) + * + * Adapted code from reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ diff --git a/crypto_kem/ledakemlt52/clean/Makefile b/crypto_kem/ledakemlt52/clean/Makefile new file mode 100644 index 00000000..09000ede --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/Makefile @@ -0,0 +1,24 @@ +# This Makefile can be used with GNU Make or BSD Make + +LIB=libledakemlt52_clean.a +HEADERS=api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ + gf2x_arith.h H_Q_matrices_generation.h \ + niederreiter.h qc_ldpc_parameters.h rng.h + +OBJECTS=bf_decoding.o dfr_test.o gf2x_arith_mod_xPplusOne.o \ + gf2x_arith.o H_Q_matrices_generation.o kem.o niederreiter.o rng.o + +CFLAGS=-O3 -Wall -Werror -Wextra -Wvla -Wpedantic -Wmissing-prototypes -std=c99 \ + -I../../../common $(EXTRAFLAGS) + +all: $(LIB) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c -o $@ $< + +$(LIB): $(OBJECTS) + $(AR) -r $@ $(OBJECTS) + +clean: + $(RM) $(OBJECTS) + $(RM) $(LIB) diff --git a/crypto_kem/ledakemlt52/clean/Makefile.Microsoft_nmake b/crypto_kem/ledakemlt52/clean/Makefile.Microsoft_nmake new file mode 100644 index 00000000..3cbd9d76 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/Makefile.Microsoft_nmake @@ -0,0 +1,19 @@ +# This Makefile can be used with Microsoft Visual Studio's nmake using the command: +# nmake /f Makefile.Microsoft_nmake + +LIBRARY=libledakemlt52_clean.lib +OBJECTS=bf_decoding.obj dfr_test.obj gf2x_arith_mod_xPplusOne.obj gf2x_arith.obj H_Q_matrices_generation.obj kem.obj niederreiter.obj rng.obj + +CFLAGS=/nologo /I ..\..\..\common /W4 /WX + +all: $(LIBRARY) + +# Make sure objects are recompiled if headers change. +$(OBJECTS): *.h + +$(LIBRARY): $(OBJECTS) + LIB.EXE /NOLOGO /WX /OUT:$@ $** + +clean: + -DEL $(OBJECTS) + -DEL $(LIBRARY) diff --git a/crypto_kem/ledakemlt52/clean/api.h b/crypto_kem/ledakemlt52/clean/api.h new file mode 100644 index 00000000..05ce0565 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/api.h @@ -0,0 +1,18 @@ +#ifndef PQCLEAN_LEDAKEMLT52_CLEAN_API_H +#define PQCLEAN_LEDAKEMLT52_CLEAN_API_H + +#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_SECRETKEYBYTES 41 +#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_PUBLICKEYBYTES 19040 +#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_CIPHERTEXTBYTES 19040 +#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_BYTES 64 + +#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT52" + +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk); + +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk); + +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk); + + +#endif diff --git a/crypto_kem/ledakemlt52/clean/bf_decoding.c b/crypto_kem/ledakemlt52/clean/bf_decoding.c new file mode 100644 index 00000000..531a73d6 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/bf_decoding.c @@ -0,0 +1,77 @@ +#include "bf_decoding.h" +#include "gf2x_arith_mod_xPplusOne.h" + +#include +#include + +unsigned int thresholds[2] = {B0, (DV * M) / 2 + 1}; + +int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], + const POSITION_T HtrPosOnes[N0][DV], + const POSITION_T QtrPosOnes[N0][M], + DIGIT privateSyndrome[]) { + + uint8_t unsatParityChecks[N0 * P]; + POSITION_T currQBlkPos[M], currQBitPos[M]; + DIGIT currSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + int check; + int iteration = 0; + + do { + gf2x_copy(currSyndrome, privateSyndrome); + memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); + for (int i = 0; i < N0; i++) { + for (int valueIdx = 0; valueIdx < P; valueIdx++) { + for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { + POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); + if (gf2x_get_coeff(currSyndrome, tmp)) { + unsatParityChecks[i * P + valueIdx]++; + } + } + } + } + + /* iteration based threshold determination*/ + unsigned int corrt_syndrome_based = thresholds[iteration]; + + //Computation of correlation with a full Q matrix + for (int i = 0; i < N0; i++) { + for (int j = 0; j < P; j++) { + int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + int endQblockIdx = 0; + unsigned int correlation = 0; + + for (int blockIdx = 0; blockIdx < N0; blockIdx++) { + endQblockIdx += qBlockWeights[blockIdx][i]; + int currblockoffset = blockIdx * P; + for (; currQoneIdx < endQblockIdx; currQoneIdx++) { + uint32_t tmp = QtrPosOnes[i][currQoneIdx] + j; + tmp = tmp >= P ? tmp - P : tmp; + currQBitPos[currQoneIdx] = tmp; + currQBlkPos[currQoneIdx] = blockIdx; + correlation += unsatParityChecks[tmp + currblockoffset]; + } + } + /* Correlation based flipping */ + if (correlation >= corrt_syndrome_based) { + gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); + for (int v = 0; v < M; v++) { + unsigned syndromePosToFlip; + for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { + syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); + syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; + gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + } + } // end for v + } // end if + } // end for j + } // end for i + + iteration = iteration + 1; + check = 0; + while (check < NUM_DIGITS_GF2X_ELEMENT && privateSyndrome[check++] == 0) {}; + + } while (iteration < ITERATIONS_MAX && check < NUM_DIGITS_GF2X_ELEMENT); + + return (check == NUM_DIGITS_GF2X_ELEMENT); +} diff --git a/crypto_kem/ledakemlt52/clean/bf_decoding.h b/crypto_kem/ledakemlt52/clean/bf_decoding.h new file mode 100644 index 00000000..af006f04 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/bf_decoding.h @@ -0,0 +1,17 @@ +#ifndef BF_DECODING_H +#define BF_DECODING_H + +#include "gf2x_arith.h" +#include "qc_ldpc_parameters.h" + +/* Definitions for DFR level 2^-SL with SL=128 */ +#define ITERATIONS_MAX (2) +#define B0 (88) +#define T_BAR (6) + +int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], + const POSITION_T HtrPosOnes[N0][DV], + const POSITION_T QtrPosOnes[N0][M], + DIGIT privateSyndrome[]); + +#endif diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.c b/crypto_kem/ledakemlt52/clean/dfr_test.c new file mode 100644 index 00000000..dde49feb --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/dfr_test.c @@ -0,0 +1,120 @@ +#include "bf_decoding.h" +#include "dfr_test.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "qc_ldpc_parameters.h" + +#include + +/* Tests if the current code attains the desired DFR. If that is the case, + * computes the threshold for the second iteration of the decoder and stores + * it in the globally accessible vector */ + +extern unsigned int thresholds[2]; + +int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { + + POSITION_T LSparse_loc[N0][DV * M]; + + /* Gamma matrix: an N0 x N0 block circulant matrix with block size p + * gamma[a][b][c] stores the intersection of the first column of the a-th + * block of L with the c-th column of the b-th block of L */ + /* Gamma computation can be accelerated employing symmetry and QC properties */ + unsigned int gamma[N0][N0][P] = {{{0}}}; + unsigned int rotated_column[DV * M]; + + unsigned int firstidx, secondidx, intersectionval; + + unsigned int gammaHist[N0][DV * M + 1] = {{0}}; + + unsigned int maxMut[N0], maxMutMinusOne[N0]; + unsigned int allBlockMaxSumst, allBlockMaxSumstMinusOne; + + unsigned int toAdd, histIdx; + + /*transpose blocks of L, we need its columns */ + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + if (LSparse[i][j] != 0) { + LSparse_loc[i][j] = (P - LSparse[i][j]); + } + } + quicksort_sparse(LSparse_loc[i]); + } + + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + for (int k = 0; k < P; k++) { + /* compute the rotated sparse column needed */ + for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { + rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; + } + quicksort_sparse(rotated_column); + /* compute the intersection amount */ + firstidx = 0, secondidx = 0; + intersectionval = 0; + while ( (firstidx < DV * M) && (secondidx < DV * M) ) { + if ( LSparse_loc[i][firstidx] == rotated_column[secondidx] ) { + intersectionval++; + firstidx++; + secondidx++; + } else if ( LSparse_loc[i][firstidx] > rotated_column[secondidx] ) { + secondidx++; + } else { /*if ( LSparse_loc[i][firstidx] < rotated_column[secondidx] ) */ + firstidx++; + } + } + gamma[i][j][k] = intersectionval; + + } + } + } + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + gamma[i][j][0] = 0; + } + } + /* build histogram of values in gamma */ + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + for (int k = 0; k < P; k++) { + gammaHist[i][gamma[i][j][k]]++; + } + } + } + + + for (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0; gammaBlockRowIdx++) { + toAdd = T_BAR - 1; + maxMutMinusOne[gammaBlockRowIdx] = 0; + histIdx = DV * M; + while ( (histIdx > 0) && (toAdd > 0)) { + if (gammaHist[gammaBlockRowIdx][histIdx] > toAdd ) { + maxMutMinusOne[gammaBlockRowIdx] += histIdx * toAdd; + toAdd = 0; + } else { + maxMutMinusOne[gammaBlockRowIdx] += histIdx * gammaHist[gammaBlockRowIdx][histIdx]; + toAdd -= gammaHist[gammaBlockRowIdx][histIdx]; + histIdx--; + } + } + maxMut[gammaBlockRowIdx] = histIdx + maxMutMinusOne[gammaBlockRowIdx]; + } + + + /*seek max values across all gamma blocks */ + allBlockMaxSumst = maxMut[0]; + allBlockMaxSumstMinusOne = maxMutMinusOne[0]; + for (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0 ; gammaBlockRowIdx++) { + allBlockMaxSumst = allBlockMaxSumst < maxMut[gammaBlockRowIdx] ? + maxMut[gammaBlockRowIdx] : + allBlockMaxSumst; + allBlockMaxSumstMinusOne = allBlockMaxSumstMinusOne < maxMutMinusOne[gammaBlockRowIdx] ? + maxMutMinusOne[gammaBlockRowIdx] : + allBlockMaxSumstMinusOne; + } + if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { + thresholds[1] = allBlockMaxSumst + 1; + return 1; + } + return 0; +} diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.h b/crypto_kem/ledakemlt52/clean/dfr_test.h new file mode 100644 index 00000000..8a74e5a3 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/dfr_test.h @@ -0,0 +1,6 @@ +#ifndef DFR_TEST_H +#define DFR_TEST_H + +int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); + +#endif diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.c b/crypto_kem/ledakemlt52/clean/gf2x_arith.c new file mode 100644 index 00000000..cd0cddda --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith.c @@ -0,0 +1,100 @@ +#include "gf2x_arith.h" + +#include +#include // memset(...) + +/* allows the second operand to be shorter than the first */ +/* the result should be as large as the first operand*/ +static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], + const size_t na, const DIGIT A[], + const size_t nb, const DIGIT B[]) { + assert(nr >= na && na >= nb); + size_t i; + size_t delta = na - nb; + for (i = 0; i < delta; i++) { + Res[i] = A[i]; + } + for (i = 0; i < nb; i++) { + Res[i + delta] = A[i + delta] ^ B[i]; + } +} + +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ +void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { + assert(amount < DIGIT_SIZE_b); + if ( amount == 0 ) { + return; + } + unsigned int j; + DIGIT mask; + mask = ((DIGIT)0x01 << amount) - 1; + for (j = length - 1; j > 0 ; j--) { + in[j] >>= amount; + in[j] |= (in[j - 1] & mask) << (DIGIT_SIZE_b - amount); + } + in[j] >>= amount; +} + +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ +void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { + assert(amount < DIGIT_SIZE_b); + if ( amount == 0 ) { + return; + } + size_t j; + DIGIT mask; + mask = ~(((DIGIT)0x01 << (DIGIT_SIZE_b - amount)) - 1); + for (j = 0 ; j < length - 1 ; j++) { + in[j] <<= amount; + in[j] |= (in[j + 1] & mask) >> (DIGIT_SIZE_b - amount); + } + in[j] <<= amount; +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], + int na, const DIGIT A[], + int nb, const DIGIT B[]) { + int i, j, k; + DIGIT u, h; + + memset(Res, 0x00, nr * sizeof(DIGIT)); + + for (k = DIGIT_SIZE_b - 1; k > 0; k--) { + for (i = na - 1; i >= 0; i--) { + if ( A[i] & (((DIGIT)0x1) << k) ) { + for (j = nb - 1; j >= 0; j--) { + Res[i + j + 1] ^= B[j]; + } + } + } + + u = Res[na + nb - 1]; + Res[na + nb - 1] = u << 0x1; + for (j = 1; j < na + nb; ++j) { + h = u >> (DIGIT_SIZE_b - 1); + u = Res[na + nb - 1 - j]; + Res[na + nb - 1 - j] = h ^ (u << 0x1); + } + } + for (i = na - 1; i >= 0; i--) { + if ( A[i] & ((DIGIT)0x1) ) { + for (j = nb - 1; j >= 0; j--) { + Res[i + j + 1] ^= B[j]; + } + } + } +} + +static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { + DIGIT t = 0; + for (int i = na - 1; i >= 0; i--) { + + t ^= A[i]; + + for (int j = 1; j <= DIGIT_SIZE_b / 2; j = j * 2) { + t ^= t << (unsigned) j; + } + A[i] = t; + t >>= DIGIT_SIZE_b - 1; + } +} diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.h b/crypto_kem/ledakemlt52/clean/gf2x_arith.h new file mode 100644 index 00000000..86303150 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith.h @@ -0,0 +1,63 @@ +#ifndef GF2X_ARITH_H +#define GF2X_ARITH_H + +#include +#include + +/* + * Elements of GF(2)[x] are stored in compact dense binary form. + * + * Each bit in a byte is assumed to be the coefficient of a binary + * polynomial f(x), in Big-Endian format (i.e., reading everything from + * left to right, the most significant element is met first): + * + * byte:(0000 0000) == 0x00 ... f(x) == 0 + * byte:(0000 0001) == 0x01 ... f(x) == 1 + * byte:(0000 0010) == 0x02 ... f(x) == x + * byte:(0000 0011) == 0x03 ... f(x) == x+1 + * ... ... ... + * byte:(0000 1111) == 0x0F ... f(x) == x^{3}+x^{2}+x+1 + * ... ... ... + * byte:(1111 1111) == 0xFF ... f(x) == x^{7}+x^{6}+x^{5}+x^{4}+x^{3}+x^{2}+x+1 + * + * + * A "machine word" (A_i) is considered as a DIGIT. + * Bytes in a DIGIT are assumed in Big-Endian format: + * E.g., if sizeof(DIGIT) == 4: + * A_i: A_{i,3} A_{i,2} A_{i,1} A_{i,0}. + * A_{i,3} denotes the most significant byte, A_{i,0} the least significant one. + * f(x) == x^{31} + ... + x^{24} + + * + x^{23} + ... + x^{16} + + * + x^{15} + ... + x^{8} + + * + x^{7} + ... + x^{0} + * + * + * Multi-precision elements (i.e., with multiple DIGITs) are stored in + * Big-endian format: + * A = A_{n-1} A_{n-2} ... A_1 A_0 + * + * position[A_{n-1}] == 0 + * position[A_{n-2}] == 1 + * ... + * position[A_{1}] == n-2 + * position[A_{0}] == n-1 + */ + +typedef uint64_t DIGIT; +#define DIGIT_SIZE_B (8) +#define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) +#define POSITION_T uint32_t + +#define GF2X_MUL PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mul_comb + +static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], size_t nr) { + for (size_t i = 0; i < nr; i++) { + Res[i] = A[i] ^ B[i]; + } +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); +void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); + +#endif diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c new file mode 100644 index 00000000..a89ef1b1 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c @@ -0,0 +1,481 @@ +#include "gf2x_arith_mod_xPplusOne.h" +#include "rng.h" + +#include +#include // memcpy(...), memset(...) + + +static void gf2x_mod(DIGIT out[], const DIGIT in[]) { + + int i, j, posTrailingBit, maskOffset, to_copy; + DIGIT mask, aux[2 * NUM_DIGITS_GF2X_ELEMENT]; + + memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memset(out, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + for (i = 0; i < (2 * NUM_DIGITS_GF2X_ELEMENT) - NUM_DIGITS_GF2X_MODULUS; i += 1) { + for (j = DIGIT_SIZE_b - 1; j >= 0; j--) { + mask = ((DIGIT)0x1) << j; + if (aux[i] & mask) { + aux[i] ^= mask; + posTrailingBit = (DIGIT_SIZE_b - 1 - j) + i * DIGIT_SIZE_b + P; + maskOffset = (DIGIT_SIZE_b - 1 - (posTrailingBit % DIGIT_SIZE_b)); + mask = (DIGIT) 0x1 << maskOffset; + aux[posTrailingBit / DIGIT_SIZE_b] ^= mask; + } + } + } + + for (j = DIGIT_SIZE_b - 1; j >= MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS; j--) { + mask = ((DIGIT)0x1) << j; + if (aux[i] & mask) { + aux[i] ^= mask; + posTrailingBit = (DIGIT_SIZE_b - 1 - j) + i * DIGIT_SIZE_b + P; + maskOffset = (DIGIT_SIZE_b - 1 - (posTrailingBit % DIGIT_SIZE_b)); + mask = (DIGIT) 0x1 << maskOffset; + aux[posTrailingBit / DIGIT_SIZE_b] ^= mask; + } + } + + to_copy = (2 * NUM_DIGITS_GF2X_ELEMENT > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : 2 * NUM_DIGITS_GF2X_ELEMENT; + + for (i = 0; i < to_copy; i++) { + out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; + } + +} + +static void left_bit_shift(const int length, DIGIT in[]) { + + int j; + for (j = 0; j < length - 1; j++) { + in[j] <<= 1; /* logical shift does not need clearing */ + in[j] |= in[j + 1] >> (DIGIT_SIZE_b - 1); + } + in[j] <<= 1; +} + +static void right_bit_shift(unsigned int length, DIGIT in[]) { + + unsigned int j; + for (j = length - 1; j > 0 ; j--) { + in[j] >>= 1; + in[j] |= (in[j - 1] & (DIGIT)0x01) << (DIGIT_SIZE_b - 1); + } + in[j] >>= 1; +} + + +/* shifts by whole digits */ +static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { + unsigned int j; + for (j = 0; (j + amount) < length; j++) { + in[j] = in[j + amount]; + } + for (; j < length; j++) { + in[j] = (DIGIT)0; + } +} + + +/* may shift by an arbitrary amount*/ +static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { + left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); + PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); +} + +/* Hackers delight, reverses a uint64_t */ +static DIGIT reverse_digit(DIGIT x) { + uint64_t t; + x = (x << 31) | (x >> 33); + t = (x ^ (x >> 20)) & 0x00000FFF800007FFLL; + x = (t | (t << 20)) ^ x; + t = (x ^ (x >> 8)) & 0x00F8000F80700807LL; + x = (t | (t << 8)) ^ x; + t = (x ^ (x >> 4)) & 0x0808708080807008LL; + x = (t | (t << 4)) ^ x; + t = (x ^ (x >> 2)) & 0x1111111111111111LL; + x = (t | (t << 2)) ^ x; + return x; +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { + /* it keeps the lsb in the same position and + * inverts the sequence of the remaining bits */ + + DIGIT mask = (DIGIT)0x1; + DIGIT rev1, rev2, a00; + int i, slack_bits_amount = NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - P; + + a00 = A[NUM_DIGITS_GF2X_ELEMENT - 1] & mask; + right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, A); + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= (NUM_DIGITS_GF2X_ELEMENT + 1) / 2; i--) { + rev1 = reverse_digit(A[i]); + rev2 = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT - 1 - i]); + A[i] = rev2; + A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; + } + + // A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // no middle digit + + if (slack_bits_amount) { + PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); + } + A[NUM_DIGITS_GF2X_ELEMENT - 1] = (A[NUM_DIGITS_GF2X_ELEMENT - 1] & (~mask)) | a00; +} + +static void rotate_bit_left(DIGIT in[]) { /* equivalent to x * in(x) mod x^P+1 */ + + DIGIT mask, rotated_bit; + int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; + mask = ((DIGIT)0x1) << msb_offset_in_digit; + rotated_bit = !!(in[0] & mask); + in[0] &= ~mask; + left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); + in[NUM_DIGITS_GF2X_ELEMENT - 1] |= rotated_bit; +} + +static void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ + + DIGIT rotated_bit = in[NUM_DIGITS_GF2X_ELEMENT - 1] & ((DIGIT)0x1); + right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); + int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; + rotated_bit = rotated_bit << msb_offset_in_digit; + in[0] |= rotated_bit; +} + +static void gf2x_swap(const int length, + DIGIT f[], + DIGIT s[]) { + DIGIT t; + for (int i = length - 1; i >= 0; i--) { + t = f[i]; + f[i] = s[i]; + s[i] = t; + } +} + +/* + * Optimized extended GCD algorithm to compute the multiplicative inverse of + * a non-zero element in GF(2)[x] mod x^P+1, in polyn. representation. + * + * H. Brunner, A. Curiger, and M. Hofstetter. 1993. + * On Computing Multiplicative Inverses in GF(2^m). + * IEEE Trans. Comput. 42, 8 (August 1993), 1010-1015. + * DOI=http://dx.doi.org/10.1109/12.238496 + * + * + * Henri Cohen, Gerhard Frey, Roberto Avanzi, Christophe Doche, Tanja Lange, + * Kim Nguyen, and Frederik Vercauteren. 2012. + * Handbook of Elliptic and Hyperelliptic Curve Cryptography, + * Second Edition (2nd ed.). Chapman & Hall/CRC. + * (Chapter 11 -- Algorithm 11.44 -- pag 223) + * + */ +int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ + + int i; + long int delta = 0; + DIGIT u[NUM_DIGITS_GF2X_ELEMENT] = {0}; + DIGIT v[NUM_DIGITS_GF2X_ELEMENT] = {0}; + DIGIT s[NUM_DIGITS_GF2X_MODULUS] = {0}; + DIGIT f[NUM_DIGITS_GF2X_MODULUS] = {0}; // alignas(32)? + + DIGIT mask; + u[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x1; + v[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x0; + + s[NUM_DIGITS_GF2X_MODULUS - 1] = 0x1; + + mask = (((DIGIT)0x1) << MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS); + s[0] |= mask; + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 && in[i] == 0; i--) { }; + if (i < 0) { + return 0; + } + + for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 0 ; i--) { + f[i] = in[i]; + } + + for (i = 1; i <= 2 * P; i++) { + if ( (f[0] & mask) == 0 ) { + left_bit_shift(NUM_DIGITS_GF2X_MODULUS, f); + rotate_bit_left(u); + delta += 1; + } else { + if ( (s[0] & mask) != 0) { + gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); + gf2x_mod_add(v, v, u); + } + left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); + if ( delta == 0 ) { + gf2x_swap(NUM_DIGITS_GF2X_MODULUS, f, s); + gf2x_swap(NUM_DIGITS_GF2X_ELEMENT, u, v); + rotate_bit_left(u); + delta = 1; + } else { + rotate_bit_right(u); + delta = delta - 1; + } + } + } + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 ; i--) { + out[i] = u[i]; + } + + return (delta == 0); +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + + DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT]; + GF2X_MUL(2 * NUM_DIGITS_GF2X_ELEMENT, aux, + NUM_DIGITS_GF2X_ELEMENT, A, + NUM_DIGITS_GF2X_ELEMENT, B); + gf2x_mod(Res, aux); + +} + +/*PRE: the representation of the sparse coefficients is sorted in increasing + order of the coefficients themselves */ +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse( + DIGIT Res[], + const DIGIT dense[], + POSITION_T sparse[], unsigned int nPos) { + + DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + DIGIT resDouble[2 * NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + memcpy(aux + NUM_DIGITS_GF2X_ELEMENT, dense, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memcpy(resDouble + NUM_DIGITS_GF2X_ELEMENT, dense, + NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + if (sparse[0] != INVALID_POS_VALUE) { + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, resDouble, sparse[0]); + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, sparse[0]); + + for (unsigned int i = 1; i < nPos; i++) { + if (sparse[i] != INVALID_POS_VALUE) { + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); + gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); + } + } + } + + gf2x_mod(Res, resDouble); + +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { + + POSITION_T t; + int i = 0, j; + + if (A[i] == 0) { + i = 1; + } + j = i; + + for (; i < sizeA && A[i] != INVALID_POS_VALUE; i++) { + A[i] = P - A[i]; + } + + for (i -= 1; j < i; j++, i--) { + t = A[j]; + A[j] = A[i]; + A[i] = t; + } + +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], + size_t sizeA, const POSITION_T A[], + size_t sizeB, const POSITION_T B[]) { + + /* compute all the coefficients, filling invalid positions with P*/ + size_t lastFilledPos = 0; + for (size_t i = 0 ; i < sizeA ; i++) { + for (size_t j = 0 ; j < sizeB ; j++) { + uint32_t prod = A[i] + B[j]; + prod = ( (prod >= P) ? prod - P : prod); + if ((A[i] != INVALID_POS_VALUE) && + (B[j] != INVALID_POS_VALUE)) { + Res[lastFilledPos] = prod; + } else { + Res[lastFilledPos] = INVALID_POS_VALUE; + } + lastFilledPos++; + } + } + while (lastFilledPos < sizeR) { + Res[lastFilledPos] = INVALID_POS_VALUE; + lastFilledPos++; + } + quicksort_sparse(Res); + /* eliminate duplicates */ + POSITION_T lastReadPos = Res[0]; + int duplicateCount; + size_t write_idx = 0; + size_t read_idx = 0; + while (read_idx < sizeR && Res[read_idx] != INVALID_POS_VALUE) { + lastReadPos = Res[read_idx]; + read_idx++; + duplicateCount = 1; + while ( (Res[read_idx] == lastReadPos) && (Res[read_idx] != INVALID_POS_VALUE)) { + read_idx++; + duplicateCount++; + } + if (duplicateCount % 2) { + Res[write_idx] = lastReadPos; + write_idx++; + } + } + /* fill remaining cells with INVALID_POS_VALUE */ + for (; write_idx < sizeR; write_idx++) { + Res[write_idx] = INVALID_POS_VALUE; + } +} + +/* the implementation is safe even in case A or B alias with the result */ +/* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse( + int sizeR, POSITION_T Res[], + int sizeA, const POSITION_T A[], + int sizeB, const POSITION_T B[]) { + + POSITION_T tmpRes[DV * M]; // TODO: now function only works for adding (disjunct) DV and M positions + int idxA = 0, idxB = 0, idxR = 0; + while ( idxA < sizeA && + idxB < sizeB && + A[idxA] != INVALID_POS_VALUE && + B[idxB] != INVALID_POS_VALUE ) { + + if (A[idxA] == B[idxB]) { + idxA++; + idxB++; + } else { + if (A[idxA] < B[idxB]) { + tmpRes[idxR] = A[idxA]; + idxA++; + } else { + tmpRes[idxR] = B[idxB]; + idxB++; + } + idxR++; + } + } + + while (idxA < sizeA && A[idxA] != INVALID_POS_VALUE) { + tmpRes[idxR] = A[idxA]; + idxA++; + idxR++; + } + + while (idxB < sizeB && B[idxB] != INVALID_POS_VALUE) { + tmpRes[idxR] = B[idxB]; + idxB++; + idxR++; + } + + while (idxR < sizeR) { + tmpRes[idxR] = INVALID_POS_VALUE; + idxR++; + } + memcpy(Res, tmpRes, sizeof(POSITION_T)*sizeR); + +} + +/* Return a uniform random value in the range 0..n-1 inclusive, + * applying a rejection sampling strategy and exploiting as a random source + * the NIST seedexpander seeded with the proper key. + * Assumes that the maximum value for the range n is 2^32-1 + */ +static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct *seed_expander_ctx) { + unsigned long required_rnd_bytes = (logn + 7) / 8; + unsigned char rnd_char_buffer[4]; + uint32_t rnd_value; + uint32_t mask = ( (uint32_t)1 << logn) - 1; + + do { + PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); + /* obtain an endianness independent representation of the generated random + bytes into an unsigned integer */ + rnd_value = ((uint32_t)rnd_char_buffer[3] << 24) + + ((uint32_t)rnd_char_buffer[2] << 16) + + ((uint32_t)rnd_char_buffer[1] << 8) + + ((uint32_t)rnd_char_buffer[0] << 0) ; + rnd_value = mask & rnd_value; + } while (rnd_value >= n); + + return rnd_value; +} + +/* Obtains fresh randomness and seed-expands it until all the required positions + * for the '1's in the circulant block are obtained */ +void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, + int countOnes, + AES_XOF_struct *seed_expander_ctx) { + + int duplicated, placedOnes = 0; + uint32_t p; + + while (placedOnes < countOnes) { + p = rand_range(NUM_BITS_GF2X_ELEMENT, + P_BITS, + seed_expander_ctx); + duplicated = 0; + for (int j = 0; j < placedOnes; j++) { + if (pos_ones[j] == p) { + duplicated = 1; + } + } + if (duplicated == 0) { + pos_ones[placedOnes] = p; + placedOnes++; + } + } +} + +/* Returns random weight-t circulant block */ +void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence( + DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + AES_XOF_struct *seed_expander_ctx) { + + int rndPos[NUM_ERRORS_T], duplicated, counter = 0; + memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + + while (counter < NUM_ERRORS_T) { + int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, + seed_expander_ctx); + duplicated = 0; + for (int j = 0; j < counter; j++) { + if (rndPos[j] == p) { + duplicated = 1; + } + } + if (duplicated == 0) { + rndPos[counter] = p; + counter++; + } + } + for (int j = 0; j < counter; j++) { + int polyIndex = rndPos[j] / P; + int exponent = rndPos[j] % P; + gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + ( (DIGIT) 1)); + } + +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { + size_t i, j; + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { + for (j = 0; j < DIGIT_SIZE_B; j++) { + bytes[i * DIGIT_SIZE_B + j] = (uint8_t) ((poly[i] >> (8 * j)) & 0xFF); + } + } +} diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h new file mode 100644 index 00000000..e514c53d --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h @@ -0,0 +1,129 @@ +#ifndef GF2X_ARITH_MOD_XPLUSONE_H +#define GF2X_ARITH_MOD_XPLUSONE_H + +#include "qc_ldpc_parameters.h" + +#include "gf2x_arith.h" +#include "rng.h" + +#define NUM_BITS_GF2X_ELEMENT (P) // 152267 +#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) // 2380 +#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) +#define NUM_BITS_GF2X_MODULUS (P+1) +#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) // 2380 +#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) +#define INVALID_POS_VALUE (P) +#define P_BITS (18) // log_2(p) = 17.216243783 + + +static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + dest[i] = in[i]; + } +} + +/* returns the coefficient of the x^exponent term as the LSB of a digit */ +static inline DIGIT gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; +} + +/* sets the coefficient of the x^exponent term as the LSB of a digit */ +static inline void gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] & mask; + poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); +} + +/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ +static inline void gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] ^ mask; +} + +/* population count for an unsigned 64-bit integer + Source: Hacker's delight, p.66 */ +static int popcount_uint64t(uint64_t x) { + x -= (x >> 1) & 0x5555555555555555; + x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); + x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; + return (int)((x * 0x0101010101010101) >> 56); +} + +/* population count for a single polynomial */ +static inline int population_count(DIGIT *poly) { + int ret = 0; + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + ret += popcount_uint64t(poly[i]); + } + return ret; +} + +static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); +} + +static inline int partition(POSITION_T arr[], int lo, int hi) { + POSITION_T x = arr[hi]; + POSITION_T tmp; + int i = (lo - 1); + for (int j = lo; j <= hi - 1; j++) { + if (arr[j] <= x) { + i++; + tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + } + tmp = arr[i + 1]; + arr[i + 1] = arr[hi]; + arr[hi] = tmp; + + return i + 1; +} + +static inline void quicksort_sparse(POSITION_T Res[]) { + int stack[DV * M]; + int hi, lo, pivot, tos = -1; + stack[++tos] = 0; + stack[++tos] = (DV * M) - 1; + while (tos >= 0 ) { + hi = stack[tos--]; + lo = stack[tos--]; + pivot = partition(Res, lo, hi); + if ( (pivot - 1) > lo) { + stack[++tos] = lo; + stack[++tos] = pivot - 1; + } + if ( (pivot + 1) < hi) { + stack[++tos] = pivot + 1; + stack[++tos] = hi; + } + } +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place(DIGIT A[]); +void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); + +int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); + +#endif diff --git a/crypto_kem/ledakemlt52/clean/kem.c b/crypto_kem/ledakemlt52/clean/kem.c new file mode 100644 index 00000000..40ac222b --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/kem.c @@ -0,0 +1,56 @@ +#include "api.h" +#include "niederreiter.h" +#include "randombytes.h" +#include "rng.h" + +#include + +/* Generates a keypair - pk is the public key and sk is the secret key. */ +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { + AES_XOF_struct niederreiter_keys_expander; + + randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); + PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen((publicKeyNiederreiter_t *) pk, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + + return 0; +} + +static void pack_error(uint8_t *error_bytes, const DIGIT *error_digits) { + size_t i; + for (i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, error_digits + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + +/* Encrypt - pk is the public key, ct is a key encapsulation message + (ciphertext), ss is the shared secret.*/ +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { + AES_XOF_struct niederreiter_encap_key_expander; + unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; + DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + uint8_t error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + + randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); + PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); + pack_error(error_bytes, error_vector); + HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); + PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); + + return 0; +} + + +/* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private + key, ss is the shared secret */ +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { + DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + + PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, (DIGIT *)ct); + pack_error(decoded_error_bytes, decoded_error_vector); + HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); + + return 0; +} diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.c b/crypto_kem/ledakemlt52/clean/niederreiter.c new file mode 100644 index 00000000..8a60466a --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/niederreiter.c @@ -0,0 +1,198 @@ +#include "H_Q_matrices_generation.h" +#include "bf_decoding.h" +#include "dfr_test.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "niederreiter.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +#include + +void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { + + // sequence of N0 circ block matrices (p x p): Hi + POSITION_T HPosOnes[N0][DV]; + POSITION_T HtrPosOnes[N0][DV]; + /* Sparse representation of the transposed circulant matrix H, + with weight DV. Each index contains the position of a '1' digit in the + corresponding Htr block */ + + /* Sparse representation of the matrix (Q). + A matrix containing the positions of the ones in the circulant + blocks of Q. Each row contains the position of the + ones of all the blocks of a row of Q as exponent+ + P*block_position */ + POSITION_T QPosOnes[N0][M]; + + /*Rejection-sample for a full L*/ + POSITION_T LPosOnes[N0][DV * M]; + int is_L_full = 0; + int isDFRok = 0; + sk->rejections = (int8_t) 0; + do { + PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse(QPosOnes, keys_expander); + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + LPosOnes[i][j] = INVALID_POS_VALUE; + } + } + + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0] = {0}; + for (int colQ = 0; colQ < N0; colQ++) { + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + DV, HPosOnes[i], + qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + DV * M, LPosOnes[colQ], + DV * M, auxPosOnes); + processedQOnes[i] += qBlockWeights[i][colQ]; + } + } + is_L_full = 1; + for (int i = 0; i < N0; i++) { + is_L_full = is_L_full && (LPosOnes[i][DV * M - 1] != INVALID_POS_VALUE); + } + sk->rejections = sk->rejections + 1; + if (is_L_full) { + isDFRok = PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(LPosOnes); + } + } while (!is_L_full || !isDFRok); + sk->rejections = sk->rejections - 1; + + DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + for (int j = 0; j < DV * M; j++) { + if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { + gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + } + } + DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); + for (int i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + Ln0Inv, + LPosOnes[i], + DV * M); + } + + for (int i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + + +void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { + int i; + DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; + + memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(saux, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + err + i * NUM_DIGITS_GF2X_ELEMENT); + gf2x_mod_add(syndrome, syndrome, saux); + } // end for + gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); +} + + +int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { + + AES_XOF_struct niederreiter_decrypt_expander; + PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); + + // sequence of N0 circ block matrices (p x p): + POSITION_T HPosOnes[N0][DV]; + POSITION_T HtrPosOnes[N0][DV]; + POSITION_T QPosOnes[N0][M]; + int rejections = sk->rejections; + POSITION_T LPosOnes[N0][DV * M]; + do { + PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + LPosOnes[i][j] = INVALID_POS_VALUE; + } + } + + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0] = {0}; + for (int colQ = 0; colQ < N0; colQ++) { + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + DV, HPosOnes[i], + qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + DV * M, LPosOnes[colQ], + DV * M, auxPosOnes); + processedQOnes[i] += qBlockWeights[i][colQ]; + } + } + rejections--; + } while (rejections >= 0); + + POSITION_T QtrPosOnes[N0][M]; + unsigned transposed_ones_idx[N0] = {0x00}; + for (unsigned source_row_idx = 0; source_row_idx < N0 ; source_row_idx++) { + int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + int endQblockIdx = 0; + for (int blockIdx = 0; blockIdx < N0; blockIdx++) { + endQblockIdx += qBlockWeights[source_row_idx][blockIdx]; + for (; currQoneIdx < endQblockIdx; currQoneIdx++) { + QtrPosOnes[blockIdx][transposed_ones_idx[blockIdx]] = (P - + QPosOnes[source_row_idx][currQoneIdx]) % P; + transposed_ones_idx[blockIdx]++; + } + } + } + + POSITION_T auxSparse[DV * M]; + POSITION_T Ln0trSparse[DV * M]; + for (int i = 0; i < DV * M; i++) { + Ln0trSparse[i] = INVALID_POS_VALUE; + auxSparse[i] = INVALID_POS_VALUE; + } + + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(DV * M, auxSparse, + DV, HPosOnes[i], + qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1]]); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(DV * M, Ln0trSparse, + DV * M, Ln0trSparse, + DV * M, auxSparse); + } + + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); + + DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); + + /* prepare mockup error vector in case a decoding failure occurs */ + DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(&niederreiter_decrypt_expander, + ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), + TRNG_BYTE_LENGTH); + + int decryptOk = 0; + memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + decryptOk = PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, + (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); + + int err_weight = 0; + for (int i = 0 ; i < N0; i++) { + err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + } + decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); + + if (!decryptOk) { // TODO: not constant time, replace with cmov? + memcpy(err, mockup_error_vector, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + } + + return decryptOk; +} diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.h b/crypto_kem/ledakemlt52/clean/niederreiter.h new file mode 100644 index 00000000..eb1d69b9 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/niederreiter.h @@ -0,0 +1,28 @@ +#ifndef NIEDERREITER_H +#define NIEDERREITER_H + +#include "gf2x_arith_mod_xPplusOne.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +typedef struct { + /* raw entropy extracted from TRNG, will be deterministically expanded into + * H and Q during decryption */ + unsigned char prng_seed[TRNG_BYTE_LENGTH]; + int8_t rejections; +} privateKeyNiederreiter_t; + +typedef struct { + DIGIT Mtr[(N0 - 1)*NUM_DIGITS_GF2X_ELEMENT]; + // Dense representation of the matrix M=Ln0*L, + // An array including a sequence of (N0-1) gf2x elements; + // each gf2x element is stored as a binary polynomial(mod x^P+1) + // with P coefficients. +} publicKeyNiederreiter_t; + +void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); +void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); +int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); + + +#endif diff --git a/crypto_kem/ledakemlt52/clean/qc_ldpc_parameters.h b/crypto_kem/ledakemlt52/clean/qc_ldpc_parameters.h new file mode 100644 index 00000000..fbb3e722 --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/qc_ldpc_parameters.h @@ -0,0 +1,27 @@ +#ifndef QC_LDPC_PARAMETERS_H +#define QC_LDPC_PARAMETERS_H + +#include "fips202.h" + +#define TRNG_BYTE_LENGTH (40) +#define HASH_BYTE_LENGTH (64) +#define HASH_FUNCTION sha3_512 + +#define N0 (2) +#define P (152267) // modulus(x) = x^P-1 +#define DV (13) // odd number +#define M (13) +#define M0 (7) +#define M1 (6) +#define NUM_ERRORS_T (267) + +// Derived parameters, they are useful for QC-LDPC algorithms +#define HASH_BIT_LENGTH (HASH_BYTE_LENGTH << 3) +#define K ((N0-1)*P) +#define N (N0*P) +#define DC (N0*DV) + +#define Q_BLOCK_WEIGHTS {{M0,M1},{M1,M0}} +static const unsigned char qBlockWeights[N0][N0] = Q_BLOCK_WEIGHTS; + +#endif diff --git a/crypto_kem/ledakemlt52/clean/rng.c b/crypto_kem/ledakemlt52/clean/rng.c new file mode 100644 index 00000000..28346f3d --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/rng.c @@ -0,0 +1,113 @@ +#include "rng.h" + +#include // void *memset(void *s, int c, size_t n); + +#include "aes.h" +#include "qc_ldpc_parameters.h" + +/* + seedexpander_init() + ctx - stores the current state of an instance of the seed expander + seed - a 32 byte random value + diversifier - an 8 byte diversifier + maxlen - maximum number of bytes (less than 2**32) generated under this seed and diversifier + */ +static int seedexpander_init(AES_XOF_struct *ctx, + unsigned char *seed, + unsigned char *diversifier, + uint64_t maxlen) { + if ( maxlen >= 0x100000000 ) { + return RNG_BAD_MAXLEN; + } + + ctx->length_remaining = maxlen; + + memset(ctx->key, 0, 32); + int max_accessible_seed_len = TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH; + memcpy(ctx->key, seed, max_accessible_seed_len); + + memcpy(ctx->ctr, diversifier, 8); + ctx->ctr[11] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[10] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[9] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[8] = maxlen % 256; + memset(ctx->ctr + 12, 0x00, 4); + + ctx->buffer_pos = 16; + memset(ctx->buffer, 0x00, 16); + + return RNG_SUCCESS; +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, + const unsigned char *trng_entropy + /* TRNG_BYTE_LENGTH wide buffer */) { + + /*the NIST seedexpander will however access 32B from this buffer */ + unsigned int prng_buffer_size = TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH; + unsigned char prng_buffer[TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH] = { 0x00 }; + unsigned char *diversifier = ((unsigned char *)trng_entropy) + 32; + + memcpy(prng_buffer, + trng_entropy, + TRNG_BYTE_LENGTH < prng_buffer_size ? TRNG_BYTE_LENGTH : prng_buffer_size); + + /* the required seed expansion will be quite small, set the max number of + * bytes conservatively to 10 MiB*/ + seedexpander_init(ctx, prng_buffer, diversifier, 10 * 1024 * 1024); +} + +/* + seedexpander() + ctx - stores the current state of an instance of the seed expander + x - returns the XOF data + xlen - number of bytes to return + */ +int PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { + uint32_t offset; + aes256ctx ctx256; + + if ( x == NULL ) { + return RNG_BAD_OUTBUF; + } + if ( xlen >= ctx->length_remaining ) { + return RNG_BAD_REQ_LEN; + } + + aes256_keyexp(&ctx256, ctx->key); + ctx->length_remaining -= xlen; + + offset = 0; + while ( xlen > 0 ) { + if ( xlen <= (16 - ctx->buffer_pos) ) { // buffer has what we need + memcpy(x + offset, ctx->buffer + ctx->buffer_pos, xlen); + ctx->buffer_pos += xlen; + + return RNG_SUCCESS; + } + + // take what's in the buffer + memcpy(x + offset, ctx->buffer + ctx->buffer_pos, 16 - ctx->buffer_pos); + xlen -= 16 - ctx->buffer_pos; + offset += 16 - ctx->buffer_pos; + + aes256_ecb(ctx->buffer, ctx->ctr, 16 / AES_BLOCKBYTES, &ctx256); + ctx->buffer_pos = 0; + + //increment the counter + for (int i = 15; i >= 12; i--) { + if ( ctx->ctr[i] == 0xff ) { + ctx->ctr[i] = 0x00; + } else { + ctx->ctr[i]++; + break; + } + } + + } + + return RNG_SUCCESS; +} diff --git a/crypto_kem/ledakemlt52/clean/rng.h b/crypto_kem/ledakemlt52/clean/rng.h new file mode 100644 index 00000000..fea4032d --- /dev/null +++ b/crypto_kem/ledakemlt52/clean/rng.h @@ -0,0 +1,23 @@ +#ifndef RNG_H +#define RNG_H + +#include +#include + +#define RNG_SUCCESS ( 0) +#define RNG_BAD_MAXLEN (-1) +#define RNG_BAD_OUTBUF (-2) +#define RNG_BAD_REQ_LEN (-3) + +typedef struct { + unsigned char buffer[16]; + unsigned int buffer_pos; + uint64_t length_remaining; + unsigned char key[32]; + unsigned char ctr[16]; +} AES_XOF_struct; + +int PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); +void PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); + +#endif From 32b3a978091f2c6b318d7cebaa192b08321ead7f Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 10 Jun 2019 20:40:49 +0200 Subject: [PATCH 20/35] add sha3_384 --- common/fips202.c | 43 +++++++++++++++++++++++++++++++++++++++++++ common/fips202.h | 12 ++++++++++++ 2 files changed, 55 insertions(+) diff --git a/common/fips202.c b/common/fips202.c index 0fb29fab..794e25a0 100644 --- a/common/fips202.c +++ b/common/fips202.c @@ -718,6 +718,49 @@ void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen) { } } +void sha3_384_inc_init(sha3_384incctx *state) { + keccak_inc_init(state->ctx); +} + +void sha3_384_inc_absorb(sha3_384incctx *state, const uint8_t *input, size_t inlen) { + keccak_inc_absorb(state->ctx, SHA3_384_RATE, input, inlen); +} + +void sha3_384_inc_finalize(uint8_t *output, sha3_384incctx *state) { + uint8_t t[SHA3_384_RATE]; + keccak_inc_finalize(state->ctx, SHA3_384_RATE, 0x06); + + keccak_squeezeblocks(t, 1, state->ctx, SHA3_384_RATE); + + for (size_t i = 0; i < 48; i++) { + output[i] = t[i]; + } +} + +/************************************************* + * Name: sha3_384 + * + * Description: SHA3-256 with non-incremental API + * + * Arguments: - uint8_t *output: pointer to output + * - const uint8_t *input: pointer to input + * - size_t inlen: length of input in bytes + **************************************************/ +void sha3_384(uint8_t *output, const uint8_t *input, size_t inlen) { + uint64_t s[25]; + uint8_t t[SHA3_384_RATE]; + + /* Absorb input */ + keccak_absorb(s, SHA3_384_RATE, input, inlen, 0x06); + + /* Squeeze output */ + keccak_squeezeblocks(t, 1, s, SHA3_384_RATE); + + for (size_t i = 0; i < 48; i++) { + output[i] = t[i]; + } +} + void sha3_512_inc_init(sha3_512incctx *state) { keccak_inc_init(state->ctx); } diff --git a/common/fips202.h b/common/fips202.h index 622d736a..5d62b284 100644 --- a/common/fips202.h +++ b/common/fips202.h @@ -7,6 +7,7 @@ #define SHAKE128_RATE 168 #define SHAKE256_RATE 136 #define SHA3_256_RATE 136 +#define SHA3_384_RATE 104 #define SHA3_512_RATE 72 @@ -35,6 +36,11 @@ typedef struct { uint64_t ctx[26]; } sha3_256incctx; +// Context for incremental API +typedef struct { + uint64_t ctx[26]; +} sha3_384incctx; + // Context for incremental API typedef struct { uint64_t ctx[26]; @@ -69,6 +75,12 @@ void sha3_256_inc_finalize(uint8_t *output, sha3_256incctx *state); void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen); +void sha3_384_inc_init(sha3_384incctx *state); +void sha3_384_inc_absorb(sha3_384incctx *state, const uint8_t *input, size_t inlen); +void sha3_384_inc_finalize(uint8_t *output, sha3_384incctx *state); + +void sha3_384(uint8_t *output, const uint8_t *input, size_t inlen); + void sha3_512_inc_init(sha3_512incctx *state); void sha3_512_inc_absorb(sha3_512incctx *state, const uint8_t *input, size_t inlen); void sha3_512_inc_finalize(uint8_t *output, sha3_512incctx *state); From 737cb1bb2e91c1589c76c4862d0675191509925a Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 10 Jun 2019 20:42:31 +0200 Subject: [PATCH 21/35] add ledakemlt32 --- crypto_kem/ledakemlt32/META.yml | 18 + .../clean/H_Q_matrices_generation.c | 32 ++ .../clean/H_Q_matrices_generation.h | 11 + crypto_kem/ledakemlt32/clean/LICENSE | 31 ++ crypto_kem/ledakemlt32/clean/Makefile | 24 + .../clean/Makefile.Microsoft_nmake | 19 + crypto_kem/ledakemlt32/clean/api.h | 18 + crypto_kem/ledakemlt32/clean/bf_decoding.c | 77 +++ crypto_kem/ledakemlt32/clean/bf_decoding.h | 17 + crypto_kem/ledakemlt32/clean/dfr_test.c | 120 +++++ crypto_kem/ledakemlt32/clean/dfr_test.h | 6 + crypto_kem/ledakemlt32/clean/gf2x_arith.c | 100 ++++ crypto_kem/ledakemlt32/clean/gf2x_arith.h | 63 +++ .../clean/gf2x_arith_mod_xPplusOne.c | 481 ++++++++++++++++++ .../clean/gf2x_arith_mod_xPplusOne.h | 129 +++++ crypto_kem/ledakemlt32/clean/kem.c | 56 ++ crypto_kem/ledakemlt32/clean/niederreiter.c | 198 +++++++ crypto_kem/ledakemlt32/clean/niederreiter.h | 28 + .../ledakemlt32/clean/qc_ldpc_parameters.h | 27 + crypto_kem/ledakemlt32/clean/rng.c | 113 ++++ crypto_kem/ledakemlt32/clean/rng.h | 23 + 21 files changed, 1591 insertions(+) create mode 100644 crypto_kem/ledakemlt32/META.yml create mode 100644 crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.c create mode 100644 crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.h create mode 100644 crypto_kem/ledakemlt32/clean/LICENSE create mode 100644 crypto_kem/ledakemlt32/clean/Makefile create mode 100644 crypto_kem/ledakemlt32/clean/Makefile.Microsoft_nmake create mode 100644 crypto_kem/ledakemlt32/clean/api.h create mode 100644 crypto_kem/ledakemlt32/clean/bf_decoding.c create mode 100644 crypto_kem/ledakemlt32/clean/bf_decoding.h create mode 100644 crypto_kem/ledakemlt32/clean/dfr_test.c create mode 100644 crypto_kem/ledakemlt32/clean/dfr_test.h create mode 100644 crypto_kem/ledakemlt32/clean/gf2x_arith.c create mode 100644 crypto_kem/ledakemlt32/clean/gf2x_arith.h create mode 100644 crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c create mode 100644 crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h create mode 100644 crypto_kem/ledakemlt32/clean/kem.c create mode 100644 crypto_kem/ledakemlt32/clean/niederreiter.c create mode 100644 crypto_kem/ledakemlt32/clean/niederreiter.h create mode 100644 crypto_kem/ledakemlt32/clean/qc_ldpc_parameters.h create mode 100644 crypto_kem/ledakemlt32/clean/rng.c create mode 100644 crypto_kem/ledakemlt32/clean/rng.h diff --git a/crypto_kem/ledakemlt32/META.yml b/crypto_kem/ledakemlt32/META.yml new file mode 100644 index 00000000..fffc304a --- /dev/null +++ b/crypto_kem/ledakemlt32/META.yml @@ -0,0 +1,18 @@ +name: LEDAcryptKEMLT32 +type: kem +claimed-nist-level: 3 +claimed-security: IND-CCA2 +length-public-key: 12032 +length-secret-key: 33 +length-ciphertext: 12032 +length-shared-secret: 48 +nistkat-sha256: 5f4e10c91755d87722bc16e13c6bc5d8bcd6190589cb8924aedb8639d9f1f244 +principal-submitter: Marco Baldi +auxiliary-submitters: + - Alessandro Barenghi + - Franco Chiaraluce + - Gerardo Pelosi + - Paolo Santini +implementations: + - name: clean + version: 2.0 diff --git a/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.c new file mode 100644 index 00000000..eca47755 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.c @@ -0,0 +1,32 @@ +#include "H_Q_matrices_generation.h" +#include "gf2x_arith_mod_xPplusOne.h" + +void PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes( + POSITION_T HPosOnes[N0][DV], + POSITION_T HtrPosOnes[N0][DV], + AES_XOF_struct *keys_expander) { + for (int i = 0; i < N0; i++) { + /* Generate a random block of Htr */ + PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); + } + for (int i = 0; i < N0; i++) { + /* Obtain directly the sparse representation of the block of H */ + for (int k = 0; k < DV; k++) { + HPosOnes[i][k] = (P - HtrPosOnes[i][k]) % P; /* transposes indexes */ + } + } +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse( + POSITION_T pos_ones[N0][M], + AES_XOF_struct *keys_expander) { + for (int i = 0; i < N0; i++) { + int placed_ones = 0; + for (int j = 0; j < N0; j++) { + PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(&pos_ones[i][placed_ones], + qBlockWeights[i][j], + keys_expander); + placed_ones += qBlockWeights[i][j]; + } + } +} diff --git a/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.h new file mode 100644 index 00000000..68d4703d --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.h @@ -0,0 +1,11 @@ +#ifndef H_Q_MATRICES_GENERATION_H +#define H_Q_MATRICES_GENERATION_H + +#include "gf2x_arith.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +void PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); +void PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); + +#endif diff --git a/crypto_kem/ledakemlt32/clean/LICENSE b/crypto_kem/ledakemlt32/clean/LICENSE new file mode 100644 index 00000000..c1761078 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/LICENSE @@ -0,0 +1,31 @@ +/** + * + * LEDAcryptKEM + * + * @version 2.0 (March 2019) + * + * Adapted code from reference ISO-C11 Implementation of the LEDAcrypt KEM-LT cipher. + * + * In alphabetical order: + * + * @author Marco Baldi + * @author Alessandro Barenghi + * @author Franco Chiaraluce + * @author Gerardo Pelosi + * @author Paolo Santini + * + * This code is hereby placed in the public domain. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **/ diff --git a/crypto_kem/ledakemlt32/clean/Makefile b/crypto_kem/ledakemlt32/clean/Makefile new file mode 100644 index 00000000..4d17eb30 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/Makefile @@ -0,0 +1,24 @@ +# This Makefile can be used with GNU Make or BSD Make + +LIB=libledakemlt32_clean.a +HEADERS=api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ + gf2x_arith.h H_Q_matrices_generation.h \ + niederreiter.h qc_ldpc_parameters.h rng.h + +OBJECTS=bf_decoding.o dfr_test.o gf2x_arith_mod_xPplusOne.o \ + gf2x_arith.o H_Q_matrices_generation.o kem.o niederreiter.o rng.o + +CFLAGS=-O3 -Wall -Werror -Wextra -Wvla -Wpedantic -Wmissing-prototypes -std=c99 \ + -I../../../common $(EXTRAFLAGS) + +all: $(LIB) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c -o $@ $< + +$(LIB): $(OBJECTS) + $(AR) -r $@ $(OBJECTS) + +clean: + $(RM) $(OBJECTS) + $(RM) $(LIB) diff --git a/crypto_kem/ledakemlt32/clean/Makefile.Microsoft_nmake b/crypto_kem/ledakemlt32/clean/Makefile.Microsoft_nmake new file mode 100644 index 00000000..8b169221 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/Makefile.Microsoft_nmake @@ -0,0 +1,19 @@ +# This Makefile can be used with Microsoft Visual Studio's nmake using the command: +# nmake /f Makefile.Microsoft_nmake + +LIBRARY=libledakemlt32_clean.lib +OBJECTS=bf_decoding.obj dfr_test.obj gf2x_arith_mod_xPplusOne.obj gf2x_arith.obj H_Q_matrices_generation.obj kem.obj niederreiter.obj rng.obj + +CFLAGS=/nologo /I ..\..\..\common /W4 /WX + +all: $(LIBRARY) + +# Make sure objects are recompiled if headers change. +$(OBJECTS): *.h + +$(LIBRARY): $(OBJECTS) + LIB.EXE /NOLOGO /WX /OUT:$@ $** + +clean: + -DEL $(OBJECTS) + -DEL $(LIBRARY) diff --git a/crypto_kem/ledakemlt32/clean/api.h b/crypto_kem/ledakemlt32/clean/api.h new file mode 100644 index 00000000..afc20eef --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/api.h @@ -0,0 +1,18 @@ +#ifndef PQCLEAN_LEDAKEMLT32_CLEAN_API_H +#define PQCLEAN_LEDAKEMLT32_CLEAN_API_H + +#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_SECRETKEYBYTES 33 +#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_PUBLICKEYBYTES 12032 +#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_CIPHERTEXTBYTES 12032 +#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_BYTES 48 + +#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT32" + +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk); + +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk); + +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk); + + +#endif diff --git a/crypto_kem/ledakemlt32/clean/bf_decoding.c b/crypto_kem/ledakemlt32/clean/bf_decoding.c new file mode 100644 index 00000000..42639492 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/bf_decoding.c @@ -0,0 +1,77 @@ +#include "bf_decoding.h" +#include "gf2x_arith_mod_xPplusOne.h" + +#include +#include + +unsigned int thresholds[2] = {B0, (DV * M) / 2 + 1}; + +int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], + const POSITION_T HtrPosOnes[N0][DV], + const POSITION_T QtrPosOnes[N0][M], + DIGIT privateSyndrome[]) { + + uint8_t unsatParityChecks[N0 * P]; + POSITION_T currQBlkPos[M], currQBitPos[M]; + DIGIT currSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + int check; + int iteration = 0; + + do { + gf2x_copy(currSyndrome, privateSyndrome); + memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); + for (int i = 0; i < N0; i++) { + for (int valueIdx = 0; valueIdx < P; valueIdx++) { + for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { + POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); + if (gf2x_get_coeff(currSyndrome, tmp)) { + unsatParityChecks[i * P + valueIdx]++; + } + } + } + } + + /* iteration based threshold determination*/ + unsigned int corrt_syndrome_based = thresholds[iteration]; + + //Computation of correlation with a full Q matrix + for (int i = 0; i < N0; i++) { + for (int j = 0; j < P; j++) { + int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + int endQblockIdx = 0; + unsigned int correlation = 0; + + for (int blockIdx = 0; blockIdx < N0; blockIdx++) { + endQblockIdx += qBlockWeights[blockIdx][i]; + int currblockoffset = blockIdx * P; + for (; currQoneIdx < endQblockIdx; currQoneIdx++) { + uint32_t tmp = QtrPosOnes[i][currQoneIdx] + j; + tmp = tmp >= P ? tmp - P : tmp; + currQBitPos[currQoneIdx] = tmp; + currQBlkPos[currQoneIdx] = blockIdx; + correlation += unsatParityChecks[tmp + currblockoffset]; + } + } + /* Correlation based flipping */ + if (correlation >= corrt_syndrome_based) { + gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); + for (int v = 0; v < M; v++) { + unsigned syndromePosToFlip; + for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { + syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); + syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; + gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + } + } // end for v + } // end if + } // end for j + } // end for i + + iteration = iteration + 1; + check = 0; + while (check < NUM_DIGITS_GF2X_ELEMENT && privateSyndrome[check++] == 0) {}; + + } while (iteration < ITERATIONS_MAX && check < NUM_DIGITS_GF2X_ELEMENT); + + return (check == NUM_DIGITS_GF2X_ELEMENT); +} diff --git a/crypto_kem/ledakemlt32/clean/bf_decoding.h b/crypto_kem/ledakemlt32/clean/bf_decoding.h new file mode 100644 index 00000000..672e65a3 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/bf_decoding.h @@ -0,0 +1,17 @@ +#ifndef BF_DECODING_H +#define BF_DECODING_H + +#include "gf2x_arith.h" +#include "qc_ldpc_parameters.h" + +/* Definitions for DFR level 2^-SL with SL=128 */ +#define ITERATIONS_MAX (2) +#define B0 (64) +#define T_BAR (5) + +int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], + const POSITION_T HtrPosOnes[N0][DV], + const POSITION_T QtrPosOnes[N0][M], + DIGIT privateSyndrome[]); + +#endif diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.c b/crypto_kem/ledakemlt32/clean/dfr_test.c new file mode 100644 index 00000000..c9543ed3 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/dfr_test.c @@ -0,0 +1,120 @@ +#include "bf_decoding.h" +#include "dfr_test.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "qc_ldpc_parameters.h" + +#include + +/* Tests if the current code attains the desired DFR. If that is the case, + * computes the threshold for the second iteration of the decoder and stores + * it in the globally accessible vector */ + +extern unsigned int thresholds[2]; + +int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { + + POSITION_T LSparse_loc[N0][DV * M]; + + /* Gamma matrix: an N0 x N0 block circulant matrix with block size p + * gamma[a][b][c] stores the intersection of the first column of the a-th + * block of L with the c-th column of the b-th block of L */ + /* Gamma computation can be accelerated employing symmetry and QC properties */ + unsigned int gamma[N0][N0][P] = {{{0}}}; + unsigned int rotated_column[DV * M]; + + unsigned int firstidx, secondidx, intersectionval; + + unsigned int gammaHist[N0][DV * M + 1] = {{0}}; + + unsigned int maxMut[N0], maxMutMinusOne[N0]; + unsigned int allBlockMaxSumst, allBlockMaxSumstMinusOne; + + unsigned int toAdd, histIdx; + + /*transpose blocks of L, we need its columns */ + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + if (LSparse[i][j] != 0) { + LSparse_loc[i][j] = (P - LSparse[i][j]); + } + } + quicksort_sparse(LSparse_loc[i]); + } + + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + for (int k = 0; k < P; k++) { + /* compute the rotated sparse column needed */ + for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { + rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; + } + quicksort_sparse(rotated_column); + /* compute the intersection amount */ + firstidx = 0, secondidx = 0; + intersectionval = 0; + while ( (firstidx < DV * M) && (secondidx < DV * M) ) { + if ( LSparse_loc[i][firstidx] == rotated_column[secondidx] ) { + intersectionval++; + firstidx++; + secondidx++; + } else if ( LSparse_loc[i][firstidx] > rotated_column[secondidx] ) { + secondidx++; + } else { /*if ( LSparse_loc[i][firstidx] < rotated_column[secondidx] ) */ + firstidx++; + } + } + gamma[i][j][k] = intersectionval; + + } + } + } + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + gamma[i][j][0] = 0; + } + } + /* build histogram of values in gamma */ + for (int i = 0; i < N0; i++ ) { + for (int j = 0; j < N0; j++ ) { + for (int k = 0; k < P; k++) { + gammaHist[i][gamma[i][j][k]]++; + } + } + } + + + for (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0; gammaBlockRowIdx++) { + toAdd = T_BAR - 1; + maxMutMinusOne[gammaBlockRowIdx] = 0; + histIdx = DV * M; + while ( (histIdx > 0) && (toAdd > 0)) { + if (gammaHist[gammaBlockRowIdx][histIdx] > toAdd ) { + maxMutMinusOne[gammaBlockRowIdx] += histIdx * toAdd; + toAdd = 0; + } else { + maxMutMinusOne[gammaBlockRowIdx] += histIdx * gammaHist[gammaBlockRowIdx][histIdx]; + toAdd -= gammaHist[gammaBlockRowIdx][histIdx]; + histIdx--; + } + } + maxMut[gammaBlockRowIdx] = histIdx + maxMutMinusOne[gammaBlockRowIdx]; + } + + + /*seek max values across all gamma blocks */ + allBlockMaxSumst = maxMut[0]; + allBlockMaxSumstMinusOne = maxMutMinusOne[0]; + for (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0 ; gammaBlockRowIdx++) { + allBlockMaxSumst = allBlockMaxSumst < maxMut[gammaBlockRowIdx] ? + maxMut[gammaBlockRowIdx] : + allBlockMaxSumst; + allBlockMaxSumstMinusOne = allBlockMaxSumstMinusOne < maxMutMinusOne[gammaBlockRowIdx] ? + maxMutMinusOne[gammaBlockRowIdx] : + allBlockMaxSumstMinusOne; + } + if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { + thresholds[1] = allBlockMaxSumst + 1; + return 1; + } + return 0; +} diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.h b/crypto_kem/ledakemlt32/clean/dfr_test.h new file mode 100644 index 00000000..72d2297b --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/dfr_test.h @@ -0,0 +1,6 @@ +#ifndef DFR_TEST_H +#define DFR_TEST_H + +int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); + +#endif diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.c b/crypto_kem/ledakemlt32/clean/gf2x_arith.c new file mode 100644 index 00000000..3ef670d2 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith.c @@ -0,0 +1,100 @@ +#include "gf2x_arith.h" + +#include +#include // memset(...) + +/* allows the second operand to be shorter than the first */ +/* the result should be as large as the first operand*/ +static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], + const size_t na, const DIGIT A[], + const size_t nb, const DIGIT B[]) { + assert(nr >= na && na >= nb); + size_t i; + size_t delta = na - nb; + for (i = 0; i < delta; i++) { + Res[i] = A[i]; + } + for (i = 0; i < nb; i++) { + Res[i + delta] = A[i + delta] ^ B[i]; + } +} + +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ +void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { + assert(amount < DIGIT_SIZE_b); + if ( amount == 0 ) { + return; + } + unsigned int j; + DIGIT mask; + mask = ((DIGIT)0x01 << amount) - 1; + for (j = length - 1; j > 0 ; j--) { + in[j] >>= amount; + in[j] |= (in[j - 1] & mask) << (DIGIT_SIZE_b - amount); + } + in[j] >>= amount; +} + +/* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ +void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { + assert(amount < DIGIT_SIZE_b); + if ( amount == 0 ) { + return; + } + size_t j; + DIGIT mask; + mask = ~(((DIGIT)0x01 << (DIGIT_SIZE_b - amount)) - 1); + for (j = 0 ; j < length - 1 ; j++) { + in[j] <<= amount; + in[j] |= (in[j + 1] & mask) >> (DIGIT_SIZE_b - amount); + } + in[j] <<= amount; +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], + int na, const DIGIT A[], + int nb, const DIGIT B[]) { + int i, j, k; + DIGIT u, h; + + memset(Res, 0x00, nr * sizeof(DIGIT)); + + for (k = DIGIT_SIZE_b - 1; k > 0; k--) { + for (i = na - 1; i >= 0; i--) { + if ( A[i] & (((DIGIT)0x1) << k) ) { + for (j = nb - 1; j >= 0; j--) { + Res[i + j + 1] ^= B[j]; + } + } + } + + u = Res[na + nb - 1]; + Res[na + nb - 1] = u << 0x1; + for (j = 1; j < na + nb; ++j) { + h = u >> (DIGIT_SIZE_b - 1); + u = Res[na + nb - 1 - j]; + Res[na + nb - 1 - j] = h ^ (u << 0x1); + } + } + for (i = na - 1; i >= 0; i--) { + if ( A[i] & ((DIGIT)0x1) ) { + for (j = nb - 1; j >= 0; j--) { + Res[i + j + 1] ^= B[j]; + } + } + } +} + +static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { + DIGIT t = 0; + for (int i = na - 1; i >= 0; i--) { + + t ^= A[i]; + + for (int j = 1; j <= DIGIT_SIZE_b / 2; j = j * 2) { + t ^= t << (unsigned) j; + } + A[i] = t; + t >>= DIGIT_SIZE_b - 1; + } +} diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.h b/crypto_kem/ledakemlt32/clean/gf2x_arith.h new file mode 100644 index 00000000..051e3b52 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith.h @@ -0,0 +1,63 @@ +#ifndef GF2X_ARITH_H +#define GF2X_ARITH_H + +#include +#include + +/* + * Elements of GF(2)[x] are stored in compact dense binary form. + * + * Each bit in a byte is assumed to be the coefficient of a binary + * polynomial f(x), in Big-Endian format (i.e., reading everything from + * left to right, the most significant element is met first): + * + * byte:(0000 0000) == 0x00 ... f(x) == 0 + * byte:(0000 0001) == 0x01 ... f(x) == 1 + * byte:(0000 0010) == 0x02 ... f(x) == x + * byte:(0000 0011) == 0x03 ... f(x) == x+1 + * ... ... ... + * byte:(0000 1111) == 0x0F ... f(x) == x^{3}+x^{2}+x+1 + * ... ... ... + * byte:(1111 1111) == 0xFF ... f(x) == x^{7}+x^{6}+x^{5}+x^{4}+x^{3}+x^{2}+x+1 + * + * + * A "machine word" (A_i) is considered as a DIGIT. + * Bytes in a DIGIT are assumed in Big-Endian format: + * E.g., if sizeof(DIGIT) == 4: + * A_i: A_{i,3} A_{i,2} A_{i,1} A_{i,0}. + * A_{i,3} denotes the most significant byte, A_{i,0} the least significant one. + * f(x) == x^{31} + ... + x^{24} + + * + x^{23} + ... + x^{16} + + * + x^{15} + ... + x^{8} + + * + x^{7} + ... + x^{0} + * + * + * Multi-precision elements (i.e., with multiple DIGITs) are stored in + * Big-endian format: + * A = A_{n-1} A_{n-2} ... A_1 A_0 + * + * position[A_{n-1}] == 0 + * position[A_{n-2}] == 1 + * ... + * position[A_{1}] == n-2 + * position[A_{0}] == n-1 + */ + +typedef uint64_t DIGIT; +#define DIGIT_SIZE_B (8) +#define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) +#define POSITION_T uint32_t + +#define GF2X_MUL PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mul_comb + +static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], size_t nr) { + for (size_t i = 0; i < nr; i++) { + Res[i] = A[i] ^ B[i]; + } +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); +void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); + +#endif diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c new file mode 100644 index 00000000..5464dfea --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c @@ -0,0 +1,481 @@ +#include "gf2x_arith_mod_xPplusOne.h" +#include "rng.h" + +#include +#include // memcpy(...), memset(...) + + +static void gf2x_mod(DIGIT out[], const DIGIT in[]) { + + int i, j, posTrailingBit, maskOffset, to_copy; + DIGIT mask, aux[2 * NUM_DIGITS_GF2X_ELEMENT]; + + memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memset(out, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + for (i = 0; i < (2 * NUM_DIGITS_GF2X_ELEMENT) - NUM_DIGITS_GF2X_MODULUS; i += 1) { + for (j = DIGIT_SIZE_b - 1; j >= 0; j--) { + mask = ((DIGIT)0x1) << j; + if (aux[i] & mask) { + aux[i] ^= mask; + posTrailingBit = (DIGIT_SIZE_b - 1 - j) + i * DIGIT_SIZE_b + P; + maskOffset = (DIGIT_SIZE_b - 1 - (posTrailingBit % DIGIT_SIZE_b)); + mask = (DIGIT) 0x1 << maskOffset; + aux[posTrailingBit / DIGIT_SIZE_b] ^= mask; + } + } + } + + for (j = DIGIT_SIZE_b - 1; j >= MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS; j--) { + mask = ((DIGIT)0x1) << j; + if (aux[i] & mask) { + aux[i] ^= mask; + posTrailingBit = (DIGIT_SIZE_b - 1 - j) + i * DIGIT_SIZE_b + P; + maskOffset = (DIGIT_SIZE_b - 1 - (posTrailingBit % DIGIT_SIZE_b)); + mask = (DIGIT) 0x1 << maskOffset; + aux[posTrailingBit / DIGIT_SIZE_b] ^= mask; + } + } + + to_copy = (2 * NUM_DIGITS_GF2X_ELEMENT > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : 2 * NUM_DIGITS_GF2X_ELEMENT; + + for (i = 0; i < to_copy; i++) { + out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; + } + +} + +static void left_bit_shift(const int length, DIGIT in[]) { + + int j; + for (j = 0; j < length - 1; j++) { + in[j] <<= 1; /* logical shift does not need clearing */ + in[j] |= in[j + 1] >> (DIGIT_SIZE_b - 1); + } + in[j] <<= 1; +} + +static void right_bit_shift(unsigned int length, DIGIT in[]) { + + unsigned int j; + for (j = length - 1; j > 0 ; j--) { + in[j] >>= 1; + in[j] |= (in[j - 1] & (DIGIT)0x01) << (DIGIT_SIZE_b - 1); + } + in[j] >>= 1; +} + + +/* shifts by whole digits */ +static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { + unsigned int j; + for (j = 0; (j + amount) < length; j++) { + in[j] = in[j + amount]; + } + for (; j < length; j++) { + in[j] = (DIGIT)0; + } +} + + +/* may shift by an arbitrary amount*/ +static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { + left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); + PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); +} + +/* Hackers delight, reverses a uint64_t */ +static DIGIT reverse_digit(DIGIT x) { + uint64_t t; + x = (x << 31) | (x >> 33); + t = (x ^ (x >> 20)) & 0x00000FFF800007FFLL; + x = (t | (t << 20)) ^ x; + t = (x ^ (x >> 8)) & 0x00F8000F80700807LL; + x = (t | (t << 8)) ^ x; + t = (x ^ (x >> 4)) & 0x0808708080807008LL; + x = (t | (t << 4)) ^ x; + t = (x ^ (x >> 2)) & 0x1111111111111111LL; + x = (t | (t << 2)) ^ x; + return x; +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { + /* it keeps the lsb in the same position and + * inverts the sequence of the remaining bits */ + + DIGIT mask = (DIGIT)0x1; + DIGIT rev1, rev2, a00; + int i, slack_bits_amount = NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - P; + + a00 = A[NUM_DIGITS_GF2X_ELEMENT - 1] & mask; + right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, A); + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= (NUM_DIGITS_GF2X_ELEMENT + 1) / 2; i--) { + rev1 = reverse_digit(A[i]); + rev2 = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT - 1 - i]); + A[i] = rev2; + A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; + } + + // A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // reverse middle digit + + if (slack_bits_amount) { + PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); + } + A[NUM_DIGITS_GF2X_ELEMENT - 1] = (A[NUM_DIGITS_GF2X_ELEMENT - 1] & (~mask)) | a00; +} + +static void rotate_bit_left(DIGIT in[]) { /* equivalent to x * in(x) mod x^P+1 */ + + DIGIT mask, rotated_bit; + int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; + mask = ((DIGIT)0x1) << msb_offset_in_digit; + rotated_bit = !!(in[0] & mask); + in[0] &= ~mask; + left_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); + in[NUM_DIGITS_GF2X_ELEMENT - 1] |= rotated_bit; +} + +static void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ + + DIGIT rotated_bit = in[NUM_DIGITS_GF2X_ELEMENT - 1] & ((DIGIT)0x1); + right_bit_shift(NUM_DIGITS_GF2X_ELEMENT, in); + int msb_offset_in_digit = MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS - 1; + rotated_bit = rotated_bit << msb_offset_in_digit; + in[0] |= rotated_bit; +} + +static void gf2x_swap(const int length, + DIGIT f[], + DIGIT s[]) { + DIGIT t; + for (int i = length - 1; i >= 0; i--) { + t = f[i]; + f[i] = s[i]; + s[i] = t; + } +} + +/* + * Optimized extended GCD algorithm to compute the multiplicative inverse of + * a non-zero element in GF(2)[x] mod x^P+1, in polyn. representation. + * + * H. Brunner, A. Curiger, and M. Hofstetter. 1993. + * On Computing Multiplicative Inverses in GF(2^m). + * IEEE Trans. Comput. 42, 8 (August 1993), 1010-1015. + * DOI=http://dx.doi.org/10.1109/12.238496 + * + * + * Henri Cohen, Gerhard Frey, Roberto Avanzi, Christophe Doche, Tanja Lange, + * Kim Nguyen, and Frederik Vercauteren. 2012. + * Handbook of Elliptic and Hyperelliptic Curve Cryptography, + * Second Edition (2nd ed.). Chapman & Hall/CRC. + * (Chapter 11 -- Algorithm 11.44 -- pag 223) + * + */ +int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ + + int i; + long int delta = 0; + DIGIT u[NUM_DIGITS_GF2X_ELEMENT] = {0}; + DIGIT v[NUM_DIGITS_GF2X_ELEMENT] = {0}; + DIGIT s[NUM_DIGITS_GF2X_MODULUS] = {0}; + DIGIT f[NUM_DIGITS_GF2X_MODULUS] = {0}; // alignas(32)? + + DIGIT mask; + u[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x1; + v[NUM_DIGITS_GF2X_ELEMENT - 1] = 0x0; + + s[NUM_DIGITS_GF2X_MODULUS - 1] = 0x1; + + mask = (((DIGIT)0x1) << MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS); + s[0] |= mask; + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 && in[i] == 0; i--) { }; + if (i < 0) { + return 0; + } + + for (i = NUM_DIGITS_GF2X_MODULUS - 1; i >= 0 ; i--) { + f[i] = in[i]; + } + + for (i = 1; i <= 2 * P; i++) { + if ( (f[0] & mask) == 0 ) { + left_bit_shift(NUM_DIGITS_GF2X_MODULUS, f); + rotate_bit_left(u); + delta += 1; + } else { + if ( (s[0] & mask) != 0) { + gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); + gf2x_mod_add(v, v, u); + } + left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); + if ( delta == 0 ) { + gf2x_swap(NUM_DIGITS_GF2X_MODULUS, f, s); + gf2x_swap(NUM_DIGITS_GF2X_ELEMENT, u, v); + rotate_bit_left(u); + delta = 1; + } else { + rotate_bit_right(u); + delta = delta - 1; + } + } + } + + for (i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0 ; i--) { + out[i] = u[i]; + } + + return (delta == 0); +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + + DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT]; + GF2X_MUL(2 * NUM_DIGITS_GF2X_ELEMENT, aux, + NUM_DIGITS_GF2X_ELEMENT, A, + NUM_DIGITS_GF2X_ELEMENT, B); + gf2x_mod(Res, aux); + +} + +/*PRE: the representation of the sparse coefficients is sorted in increasing + order of the coefficients themselves */ +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse( + DIGIT Res[], + const DIGIT dense[], + POSITION_T sparse[], unsigned int nPos) { + + DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + DIGIT resDouble[2 * NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + memcpy(aux + NUM_DIGITS_GF2X_ELEMENT, dense, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memcpy(resDouble + NUM_DIGITS_GF2X_ELEMENT, dense, + NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + if (sparse[0] != INVALID_POS_VALUE) { + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, resDouble, sparse[0]); + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, sparse[0]); + + for (unsigned int i = 1; i < nPos; i++) { + if (sparse[i] != INVALID_POS_VALUE) { + left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); + gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); + } + } + } + + gf2x_mod(Res, resDouble); + +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { + + POSITION_T t; + int i = 0, j; + + if (A[i] == 0) { + i = 1; + } + j = i; + + for (; i < sizeA && A[i] != INVALID_POS_VALUE; i++) { + A[i] = P - A[i]; + } + + for (i -= 1; j < i; j++, i--) { + t = A[j]; + A[j] = A[i]; + A[i] = t; + } + +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], + size_t sizeA, const POSITION_T A[], + size_t sizeB, const POSITION_T B[]) { + + /* compute all the coefficients, filling invalid positions with P*/ + size_t lastFilledPos = 0; + for (size_t i = 0 ; i < sizeA ; i++) { + for (size_t j = 0 ; j < sizeB ; j++) { + uint32_t prod = A[i] + B[j]; + prod = ( (prod >= P) ? prod - P : prod); + if ((A[i] != INVALID_POS_VALUE) && + (B[j] != INVALID_POS_VALUE)) { + Res[lastFilledPos] = prod; + } else { + Res[lastFilledPos] = INVALID_POS_VALUE; + } + lastFilledPos++; + } + } + while (lastFilledPos < sizeR) { + Res[lastFilledPos] = INVALID_POS_VALUE; + lastFilledPos++; + } + quicksort_sparse(Res); + /* eliminate duplicates */ + POSITION_T lastReadPos = Res[0]; + int duplicateCount; + size_t write_idx = 0; + size_t read_idx = 0; + while (read_idx < sizeR && Res[read_idx] != INVALID_POS_VALUE) { + lastReadPos = Res[read_idx]; + read_idx++; + duplicateCount = 1; + while ( (Res[read_idx] == lastReadPos) && (Res[read_idx] != INVALID_POS_VALUE)) { + read_idx++; + duplicateCount++; + } + if (duplicateCount % 2) { + Res[write_idx] = lastReadPos; + write_idx++; + } + } + /* fill remaining cells with INVALID_POS_VALUE */ + for (; write_idx < sizeR; write_idx++) { + Res[write_idx] = INVALID_POS_VALUE; + } +} + +/* the implementation is safe even in case A or B alias with the result */ +/* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse( + int sizeR, POSITION_T Res[], + int sizeA, const POSITION_T A[], + int sizeB, const POSITION_T B[]) { + + POSITION_T tmpRes[DV * M]; // TODO: now function only works for adding (disjunct) DV and M positions + int idxA = 0, idxB = 0, idxR = 0; + while ( idxA < sizeA && + idxB < sizeB && + A[idxA] != INVALID_POS_VALUE && + B[idxB] != INVALID_POS_VALUE ) { + + if (A[idxA] == B[idxB]) { + idxA++; + idxB++; + } else { + if (A[idxA] < B[idxB]) { + tmpRes[idxR] = A[idxA]; + idxA++; + } else { + tmpRes[idxR] = B[idxB]; + idxB++; + } + idxR++; + } + } + + while (idxA < sizeA && A[idxA] != INVALID_POS_VALUE) { + tmpRes[idxR] = A[idxA]; + idxA++; + idxR++; + } + + while (idxB < sizeB && B[idxB] != INVALID_POS_VALUE) { + tmpRes[idxR] = B[idxB]; + idxB++; + idxR++; + } + + while (idxR < sizeR) { + tmpRes[idxR] = INVALID_POS_VALUE; + idxR++; + } + memcpy(Res, tmpRes, sizeof(POSITION_T)*sizeR); + +} + +/* Return a uniform random value in the range 0..n-1 inclusive, + * applying a rejection sampling strategy and exploiting as a random source + * the NIST seedexpander seeded with the proper key. + * Assumes that the maximum value for the range n is 2^32-1 + */ +static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct *seed_expander_ctx) { + unsigned long required_rnd_bytes = (logn + 7) / 8; + unsigned char rnd_char_buffer[4]; + uint32_t rnd_value; + uint32_t mask = ( (uint32_t)1 << logn) - 1; + + do { + PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); + /* obtain an endianness independent representation of the generated random + bytes into an unsigned integer */ + rnd_value = ((uint32_t)rnd_char_buffer[3] << 24) + + ((uint32_t)rnd_char_buffer[2] << 16) + + ((uint32_t)rnd_char_buffer[1] << 8) + + ((uint32_t)rnd_char_buffer[0] << 0) ; + rnd_value = mask & rnd_value; + } while (rnd_value >= n); + + return rnd_value; +} + +/* Obtains fresh randomness and seed-expands it until all the required positions + * for the '1's in the circulant block are obtained */ +void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, + int countOnes, + AES_XOF_struct *seed_expander_ctx) { + + int duplicated, placedOnes = 0; + uint32_t p; + + while (placedOnes < countOnes) { + p = rand_range(NUM_BITS_GF2X_ELEMENT, + P_BITS, + seed_expander_ctx); + duplicated = 0; + for (int j = 0; j < placedOnes; j++) { + if (pos_ones[j] == p) { + duplicated = 1; + } + } + if (duplicated == 0) { + pos_ones[placedOnes] = p; + placedOnes++; + } + } +} + +/* Returns random weight-t circulant block */ +void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence( + DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + AES_XOF_struct *seed_expander_ctx) { + + int rndPos[NUM_ERRORS_T], duplicated, counter = 0; + memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + + while (counter < NUM_ERRORS_T) { + int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, + seed_expander_ctx); + duplicated = 0; + for (int j = 0; j < counter; j++) { + if (rndPos[j] == p) { + duplicated = 1; + } + } + if (duplicated == 0) { + rndPos[counter] = p; + counter++; + } + } + for (int j = 0; j < counter; j++) { + int polyIndex = rndPos[j] / P; + int exponent = rndPos[j] % P; + gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + ( (DIGIT) 1)); + } + +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { + size_t i, j; + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { + for (j = 0; j < DIGIT_SIZE_B; j++) { + bytes[i * DIGIT_SIZE_B + j] = (uint8_t) ((poly[i] >> (8 * j)) & 0xFF); + } + } +} diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h new file mode 100644 index 00000000..2e1a266a --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h @@ -0,0 +1,129 @@ +#ifndef GF2X_ARITH_MOD_XPLUSONE_H +#define GF2X_ARITH_MOD_XPLUSONE_H + +#include "qc_ldpc_parameters.h" + +#include "gf2x_arith.h" +#include "rng.h" + +#define NUM_BITS_GF2X_ELEMENT (P) // 96221 +#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) +#define NUM_BITS_GF2X_MODULUS (P+1) +#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) +#define INVALID_POS_VALUE (P) +#define P_BITS (17) // log_2(p) = 16.55406417 + + +static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + dest[i] = in[i]; + } +} + +/* returns the coefficient of the x^exponent term as the LSB of a digit */ +static inline DIGIT gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; +} + +/* sets the coefficient of the x^exponent term as the LSB of a digit */ +static inline void gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] & mask; + poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); +} + +/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ +static inline void gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] ^ mask; +} + +/* population count for an unsigned 64-bit integer + Source: Hacker's delight, p.66 */ +static int popcount_uint64t(uint64_t x) { + x -= (x >> 1) & 0x5555555555555555; + x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); + x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; + return (int)((x * 0x0101010101010101) >> 56); +} + +/* population count for a single polynomial */ +static inline int population_count(DIGIT *poly) { + int ret = 0; + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + ret += popcount_uint64t(poly[i]); + } + return ret; +} + +static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); +} + +static inline int partition(POSITION_T arr[], int lo, int hi) { + POSITION_T x = arr[hi]; + POSITION_T tmp; + int i = (lo - 1); + for (int j = lo; j <= hi - 1; j++) { + if (arr[j] <= x) { + i++; + tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + } + tmp = arr[i + 1]; + arr[i + 1] = arr[hi]; + arr[hi] = tmp; + + return i + 1; +} + +static inline void quicksort_sparse(POSITION_T Res[]) { + int stack[DV * M]; + int hi, lo, pivot, tos = -1; + stack[++tos] = 0; + stack[++tos] = (DV * M) - 1; + while (tos >= 0 ) { + hi = stack[tos--]; + lo = stack[tos--]; + pivot = partition(Res, lo, hi); + if ( (pivot - 1) > lo) { + stack[++tos] = lo; + stack[++tos] = pivot - 1; + } + if ( (pivot + 1) < hi) { + stack[++tos] = pivot + 1; + stack[++tos] = hi; + } + } +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(DIGIT A[]); +void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); + +int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); + +#endif diff --git a/crypto_kem/ledakemlt32/clean/kem.c b/crypto_kem/ledakemlt32/clean/kem.c new file mode 100644 index 00000000..fb67f865 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/kem.c @@ -0,0 +1,56 @@ +#include "api.h" +#include "niederreiter.h" +#include "randombytes.h" +#include "rng.h" + +#include + +/* Generates a keypair - pk is the public key and sk is the secret key. */ +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { + AES_XOF_struct niederreiter_keys_expander; + + randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); + PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen((publicKeyNiederreiter_t *) pk, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + + return 0; +} + +static void pack_error(uint8_t *error_bytes, const DIGIT *error_digits) { + size_t i; + for (i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, error_digits + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + +/* Encrypt - pk is the public key, ct is a key encapsulation message + (ciphertext), ss is the shared secret.*/ +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { + AES_XOF_struct niederreiter_encap_key_expander; + unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; + DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + uint8_t error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + + randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); + PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); + pack_error(error_bytes, error_vector); + HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); + PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); + + return 0; +} + + +/* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private + key, ss is the shared secret */ +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { + DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + + PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, (DIGIT *)ct); + pack_error(decoded_error_bytes, decoded_error_vector); + HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); + + return 0; +} diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.c b/crypto_kem/ledakemlt32/clean/niederreiter.c new file mode 100644 index 00000000..470d5bc1 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/niederreiter.c @@ -0,0 +1,198 @@ +#include "H_Q_matrices_generation.h" +#include "bf_decoding.h" +#include "dfr_test.h" +#include "gf2x_arith_mod_xPplusOne.h" +#include "niederreiter.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +#include + +void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { + + // sequence of N0 circ block matrices (p x p): Hi + POSITION_T HPosOnes[N0][DV]; + POSITION_T HtrPosOnes[N0][DV]; + /* Sparse representation of the transposed circulant matrix H, + with weight DV. Each index contains the position of a '1' digit in the + corresponding Htr block */ + + /* Sparse representation of the matrix (Q). + A matrix containing the positions of the ones in the circulant + blocks of Q. Each row contains the position of the + ones of all the blocks of a row of Q as exponent+ + P*block_position */ + POSITION_T QPosOnes[N0][M]; + + /*Rejection-sample for a full L*/ + POSITION_T LPosOnes[N0][DV * M]; + int is_L_full = 0; + int isDFRok = 0; + sk->rejections = (int8_t) 0; + do { + PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse(QPosOnes, keys_expander); + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + LPosOnes[i][j] = INVALID_POS_VALUE; + } + } + + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0] = {0}; + for (int colQ = 0; colQ < N0; colQ++) { + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + DV, HPosOnes[i], + qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + DV * M, LPosOnes[colQ], + DV * M, auxPosOnes); + processedQOnes[i] += qBlockWeights[i][colQ]; + } + } + is_L_full = 1; + for (int i = 0; i < N0; i++) { + is_L_full = is_L_full && (LPosOnes[i][DV * M - 1] != INVALID_POS_VALUE); + } + sk->rejections = sk->rejections + 1; + if (is_L_full) { + isDFRok = PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(LPosOnes); + } + } while (!is_L_full || !isDFRok); + sk->rejections = sk->rejections - 1; + + DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + for (int j = 0; j < DV * M; j++) { + if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { + gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + } + } + DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); + for (int i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + Ln0Inv, + LPosOnes[i], + DV * M); + } + + for (int i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + + +void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { + int i; + DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; + + memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(saux, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + err + i * NUM_DIGITS_GF2X_ELEMENT); + gf2x_mod_add(syndrome, syndrome, saux); + } // end for + gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); +} + + +int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { + + AES_XOF_struct niederreiter_decrypt_expander; + PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); + + // sequence of N0 circ block matrices (p x p): + POSITION_T HPosOnes[N0][DV]; + POSITION_T HtrPosOnes[N0][DV]; + POSITION_T QPosOnes[N0][M]; + int rejections = sk->rejections; + POSITION_T LPosOnes[N0][DV * M]; + do { + PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); + for (int i = 0; i < N0; i++) { + for (int j = 0; j < DV * M; j++) { + LPosOnes[i][j] = INVALID_POS_VALUE; + } + } + + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0] = {0}; + for (int colQ = 0; colQ < N0; colQ++) { + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + DV, HPosOnes[i], + qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + DV * M, LPosOnes[colQ], + DV * M, auxPosOnes); + processedQOnes[i] += qBlockWeights[i][colQ]; + } + } + rejections--; + } while (rejections >= 0); + + POSITION_T QtrPosOnes[N0][M]; + unsigned transposed_ones_idx[N0] = {0x00}; + for (unsigned source_row_idx = 0; source_row_idx < N0 ; source_row_idx++) { + int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + int endQblockIdx = 0; + for (int blockIdx = 0; blockIdx < N0; blockIdx++) { + endQblockIdx += qBlockWeights[source_row_idx][blockIdx]; + for (; currQoneIdx < endQblockIdx; currQoneIdx++) { + QtrPosOnes[blockIdx][transposed_ones_idx[blockIdx]] = (P - + QPosOnes[source_row_idx][currQoneIdx]) % P; + transposed_ones_idx[blockIdx]++; + } + } + } + + POSITION_T auxSparse[DV * M]; + POSITION_T Ln0trSparse[DV * M]; + for (int i = 0; i < DV * M; i++) { + Ln0trSparse[i] = INVALID_POS_VALUE; + auxSparse[i] = INVALID_POS_VALUE; + } + + for (int i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(DV * M, auxSparse, + DV, HPosOnes[i], + qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1]]); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(DV * M, Ln0trSparse, + DV * M, Ln0trSparse, + DV * M, auxSparse); + } + + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); + + DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); + + /* prepare mockup error vector in case a decoding failure occurs */ + DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(&niederreiter_decrypt_expander, + ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), + TRNG_BYTE_LENGTH); + + int decryptOk = 0; + memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + decryptOk = PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, + (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); + + int err_weight = 0; + for (int i = 0 ; i < N0; i++) { + err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + } + decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); + + if (!decryptOk) { // TODO: not constant time, replace with cmov? + memcpy(err, mockup_error_vector, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + } + + return decryptOk; +} diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.h b/crypto_kem/ledakemlt32/clean/niederreiter.h new file mode 100644 index 00000000..abea8a9a --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/niederreiter.h @@ -0,0 +1,28 @@ +#ifndef NIEDERREITER_H +#define NIEDERREITER_H + +#include "gf2x_arith_mod_xPplusOne.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +typedef struct { + /* raw entropy extracted from TRNG, will be deterministically expanded into + * H and Q during decryption */ + unsigned char prng_seed[TRNG_BYTE_LENGTH]; + int8_t rejections; +} privateKeyNiederreiter_t; + +typedef struct { + DIGIT Mtr[(N0 - 1)*NUM_DIGITS_GF2X_ELEMENT]; + // Dense representation of the matrix M=Ln0*L, + // An array including a sequence of (N0-1) gf2x elements; + // each gf2x element is stored as a binary polynomial(mod x^P+1) + // with P coefficients. +} publicKeyNiederreiter_t; + +void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); +void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); +int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); + + +#endif diff --git a/crypto_kem/ledakemlt32/clean/qc_ldpc_parameters.h b/crypto_kem/ledakemlt32/clean/qc_ldpc_parameters.h new file mode 100644 index 00000000..5e58fcdc --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/qc_ldpc_parameters.h @@ -0,0 +1,27 @@ +#ifndef QC_LDPC_PARAMETERS_H +#define QC_LDPC_PARAMETERS_H + +#include "fips202.h" + +#define TRNG_BYTE_LENGTH (32) +#define HASH_BYTE_LENGTH (48) +#define HASH_FUNCTION sha3_384 + +#define N0 (2) +#define P (96221) // modulus(x) = x^P-1 +#define DV (11) // odd number +#define M (11) +#define M0 (6) +#define M1 (5) +#define NUM_ERRORS_T (199) + +// Derived parameters, they are useful for QC-LDPC algorithms +#define HASH_BIT_LENGTH (HASH_BYTE_LENGTH << 3) +#define K ((N0-1)*P) +#define N (N0*P) +#define DC (N0*DV) + +#define Q_BLOCK_WEIGHTS {{M0,M1},{M1,M0}} +static const unsigned char qBlockWeights[N0][N0] = Q_BLOCK_WEIGHTS; + +#endif diff --git a/crypto_kem/ledakemlt32/clean/rng.c b/crypto_kem/ledakemlt32/clean/rng.c new file mode 100644 index 00000000..2b300538 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/rng.c @@ -0,0 +1,113 @@ +#include "rng.h" + +#include // void *memset(void *s, int c, size_t n); + +#include "aes.h" +#include "qc_ldpc_parameters.h" + +/* + seedexpander_init() + ctx - stores the current state of an instance of the seed expander + seed - a 32 byte random value + diversifier - an 8 byte diversifier + maxlen - maximum number of bytes (less than 2**32) generated under this seed and diversifier + */ +static int seedexpander_init(AES_XOF_struct *ctx, + unsigned char *seed, + unsigned char *diversifier, + uint64_t maxlen) { + if ( maxlen >= 0x100000000 ) { + return RNG_BAD_MAXLEN; + } + + ctx->length_remaining = maxlen; + + memset(ctx->key, 0, 32); + int max_accessible_seed_len = TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH; + memcpy(ctx->key, seed, max_accessible_seed_len); + + memcpy(ctx->ctr, diversifier, 8); + ctx->ctr[11] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[10] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[9] = maxlen % 256; + maxlen >>= 8; + ctx->ctr[8] = maxlen % 256; + memset(ctx->ctr + 12, 0x00, 4); + + ctx->buffer_pos = 16; + memset(ctx->buffer, 0x00, 16); + + return RNG_SUCCESS; +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, + const unsigned char *trng_entropy + /* TRNG_BYTE_LENGTH wide buffer */) { + + /*the NIST seedexpander will however access 32B from this buffer */ + unsigned int prng_buffer_size = TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH; + unsigned char prng_buffer[TRNG_BYTE_LENGTH < 32 ? 32 : TRNG_BYTE_LENGTH] = { 0x00 }; + unsigned char diversifier[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + memcpy(prng_buffer, + trng_entropy, + TRNG_BYTE_LENGTH < prng_buffer_size ? TRNG_BYTE_LENGTH : prng_buffer_size); + + /* the required seed expansion will be quite small, set the max number of + * bytes conservatively to 10 MiB*/ + seedexpander_init(ctx, prng_buffer, diversifier, 10 * 1024 * 1024); +} + +/* + seedexpander() + ctx - stores the current state of an instance of the seed expander + x - returns the XOF data + xlen - number of bytes to return + */ +int PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { + uint32_t offset; + aes256ctx ctx256; + + if ( x == NULL ) { + return RNG_BAD_OUTBUF; + } + if ( xlen >= ctx->length_remaining ) { + return RNG_BAD_REQ_LEN; + } + + aes256_keyexp(&ctx256, ctx->key); + ctx->length_remaining -= xlen; + + offset = 0; + while ( xlen > 0 ) { + if ( xlen <= (16 - ctx->buffer_pos) ) { // buffer has what we need + memcpy(x + offset, ctx->buffer + ctx->buffer_pos, xlen); + ctx->buffer_pos += xlen; + + return RNG_SUCCESS; + } + + // take what's in the buffer + memcpy(x + offset, ctx->buffer + ctx->buffer_pos, 16 - ctx->buffer_pos); + xlen -= 16 - ctx->buffer_pos; + offset += 16 - ctx->buffer_pos; + + aes256_ecb(ctx->buffer, ctx->ctr, 16 / AES_BLOCKBYTES, &ctx256); + ctx->buffer_pos = 0; + + //increment the counter + for (int i = 15; i >= 12; i--) { + if ( ctx->ctr[i] == 0xff ) { + ctx->ctr[i] = 0x00; + } else { + ctx->ctr[i]++; + break; + } + } + + } + + return RNG_SUCCESS; +} diff --git a/crypto_kem/ledakemlt32/clean/rng.h b/crypto_kem/ledakemlt32/clean/rng.h new file mode 100644 index 00000000..0fe312a6 --- /dev/null +++ b/crypto_kem/ledakemlt32/clean/rng.h @@ -0,0 +1,23 @@ +#ifndef RNG_H +#define RNG_H + +#include +#include + +#define RNG_SUCCESS ( 0) +#define RNG_BAD_MAXLEN (-1) +#define RNG_BAD_OUTBUF (-2) +#define RNG_BAD_REQ_LEN (-3) + +typedef struct { + unsigned char buffer[16]; + unsigned int buffer_pos; + uint64_t length_remaining; + unsigned char key[32]; + unsigned char ctr[16]; +} AES_XOF_struct; + +int PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); +void PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); + +#endif From 3caad7452555aa24b6fde04d5f376d10d33c26cf Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 11 Jun 2019 14:21:49 +0200 Subject: [PATCH 22/35] variable declarations at the beginning, namespace extern variables --- crypto_kem/ledakemlt12/clean/bf_decoding.c | 4 +- crypto_kem/ledakemlt12/clean/dfr_test.c | 4 +- .../clean/gf2x_arith_mod_xPplusOne.c | 24 +++---- crypto_kem/ledakemlt12/clean/niederreiter.c | 70 +++++++++---------- crypto_kem/ledakemlt32/clean/bf_decoding.c | 4 +- crypto_kem/ledakemlt32/clean/dfr_test.c | 4 +- .../clean/gf2x_arith_mod_xPplusOne.c | 19 +++-- crypto_kem/ledakemlt32/clean/niederreiter.c | 70 +++++++++---------- crypto_kem/ledakemlt52/clean/bf_decoding.c | 4 +- crypto_kem/ledakemlt52/clean/dfr_test.c | 4 +- .../clean/gf2x_arith_mod_xPplusOne.c | 19 +++-- crypto_kem/ledakemlt52/clean/niederreiter.c | 70 +++++++++---------- 12 files changed, 137 insertions(+), 159 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/clean/bf_decoding.c index f03deb28..977eb6ea 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.c @@ -4,7 +4,7 @@ #include #include -unsigned int thresholds[2] = {B0, (DV * M) / 2 + 1}; +unsigned int PQCLEAN_LEDAKEMLT12_CLEAN_thresholds[2] = {B0, (DV * M) / 2 + 1}; int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], @@ -32,7 +32,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], } /* iteration based threshold determination*/ - unsigned int corrt_syndrome_based = thresholds[iteration]; + unsigned int corrt_syndrome_based = PQCLEAN_LEDAKEMLT12_CLEAN_thresholds[iteration]; //Computation of correlation with a full Q matrix for (int i = 0; i < N0; i++) { diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt12/clean/dfr_test.c index bcea824c..157e77d8 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.c +++ b/crypto_kem/ledakemlt12/clean/dfr_test.c @@ -9,7 +9,7 @@ * computes the threshold for the second iteration of the decoder and stores * it in the globally accessible vector */ -extern unsigned int thresholds[2]; +extern unsigned int PQCLEAN_LEDAKEMLT12_CLEAN_thresholds[2]; int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { @@ -113,7 +113,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - thresholds[1] = allBlockMaxSumst + 1; + PQCLEAN_LEDAKEMLT12_CLEAN_thresholds[1] = allBlockMaxSumst + 1; return 1; } return 0; diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index 80c2f72f..217b24f1 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -7,7 +7,7 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { - int i, j, posTrailingBit, maskOffset, to_copy; + int i, j, posTrailingBit, maskOffset; DIGIT mask, aux[2 * NUM_DIGITS_GF2X_ELEMENT]; memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); @@ -37,9 +37,7 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { } } - to_copy = (2 * NUM_DIGITS_GF2X_ELEMENT > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : 2 * NUM_DIGITS_GF2X_ELEMENT; - - for (i = 0; i < to_copy; i++) { + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; } @@ -440,17 +438,17 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, } /* Returns random weight-t circulant block */ -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( - DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], - AES_XOF_struct *seed_expander_ctx) { +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + AES_XOF_struct *seed_expander_ctx) { int rndPos[NUM_ERRORS_T], duplicated, counter = 0; + int p, polyIndex, exponent; + memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - while (counter < NUM_ERRORS_T) { - int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, - seed_expander_ctx); + p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, + seed_expander_ctx); duplicated = 0; for (int j = 0; j < counter; j++) { if (rndPos[j] == p) { @@ -463,8 +461,8 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( } } for (int j = 0; j < counter; j++) { - int polyIndex = rndPos[j] / P; - int exponent = rndPos[j] % P; + polyIndex = rndPos[j] / P; + exponent = rndPos[j] % P; gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, ( (DIGIT) 1)); } @@ -475,7 +473,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { for (j = 0; j < DIGIT_SIZE_B; j++) { - bytes[i * DIGIT_SIZE_B + j] = (uint8_t) ((poly[i] >> (8 * j)) & 0xFF); + bytes[i * DIGIT_SIZE_B + j] = (uint8_t) (poly[i] >> 8 * j); } } } diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index 58c4542f..e2326bd4 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -10,25 +10,19 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { - // sequence of N0 circ block matrices (p x p): Hi - POSITION_T HPosOnes[N0][DV]; - POSITION_T HtrPosOnes[N0][DV]; - /* Sparse representation of the transposed circulant matrix H, - with weight DV. Each index contains the position of a '1' digit in the - corresponding Htr block */ - /* Sparse representation of the matrix (Q). - A matrix containing the positions of the ones in the circulant - blocks of Q. Each row contains the position of the - ones of all the blocks of a row of Q as exponent+ - P*block_position */ - POSITION_T QPosOnes[N0][M]; - - /*Rejection-sample for a full L*/ + POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi + POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H + POSITION_T QPosOnes[N0][M]; // Sparse Q, Each row contains the position of the ones of all the blocks of a row of Q as exponent+P*block_position POSITION_T LPosOnes[N0][DV * M]; + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0]; + DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; + DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; int isDFRok = 0; sk->rejections = (int8_t) 0; + do { PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, keys_expander); @@ -38,8 +32,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } } - POSITION_T auxPosOnes[DV * M]; - unsigned char processedQOnes[N0] = {0}; + memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, @@ -62,13 +55,14 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } while (!is_L_full || !isDFRok); sk->rejections = sk->rejections - 1; - DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } - DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + + memset(Ln0Inv, 0x00, sizeof(Ln0Inv)); PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); for (int i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, @@ -88,28 +82,36 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publi DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - for (i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(saux, pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, err + i * NUM_DIGITS_GF2X_ELEMENT); gf2x_mod_add(syndrome, syndrome, saux); - } // end for + } gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { - AES_XOF_struct niederreiter_decrypt_expander; - PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); - - // sequence of N0 circ block matrices (p x p): POSITION_T HPosOnes[N0][DV]; POSITION_T HtrPosOnes[N0][DV]; POSITION_T QPosOnes[N0][M]; - int rejections = sk->rejections; + POSITION_T QtrPosOnes[N0][M]; + POSITION_T auxPosOnes[DV * M]; POSITION_T LPosOnes[N0][DV * M]; + POSITION_T auxSparse[DV * M]; + POSITION_T Ln0trSparse[DV * M]; + unsigned char processedQOnes[N0]; + unsigned transposed_ones_idx[N0]; + DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + int rejections = sk->rejections; + int currQoneIdx, endQblockIdx; + int decryptOk, err_weight; + + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); + do { PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); @@ -119,8 +121,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } } - POSITION_T auxPosOnes[DV * M]; - unsigned char processedQOnes[N0] = {0}; + memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, @@ -135,11 +136,10 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN rejections--; } while (rejections >= 0); - POSITION_T QtrPosOnes[N0][M]; - unsigned transposed_ones_idx[N0] = {0x00}; + memset(transposed_ones_idx, 0x00, sizeof(transposed_ones_idx)); for (unsigned source_row_idx = 0; source_row_idx < N0 ; source_row_idx++) { - int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] - int endQblockIdx = 0; + currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + endQblockIdx = 0; for (int blockIdx = 0; blockIdx < N0; blockIdx++) { endQblockIdx += qBlockWeights[source_row_idx][blockIdx]; for (; currQoneIdx < endQblockIdx; currQoneIdx++) { @@ -150,8 +150,6 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } } - POSITION_T auxSparse[DV * M]; - POSITION_T Ln0trSparse[DV * M]; for (int i = 0; i < DV * M; i++) { Ln0trSparse[i] = INVALID_POS_VALUE; auxSparse[i] = INVALID_POS_VALUE; @@ -167,24 +165,20 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); - - DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); /* prepare mockup error vector in case a decoding failure occurs */ - DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(&niederreiter_decrypt_expander, ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), TRNG_BYTE_LENGTH); - int decryptOk = 0; memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); decryptOk = PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); - int err_weight = 0; + err_weight = 0; for (int i = 0 ; i < N0; i++) { err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } diff --git a/crypto_kem/ledakemlt32/clean/bf_decoding.c b/crypto_kem/ledakemlt32/clean/bf_decoding.c index 42639492..3ed29fb1 100644 --- a/crypto_kem/ledakemlt32/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt32/clean/bf_decoding.c @@ -4,7 +4,7 @@ #include #include -unsigned int thresholds[2] = {B0, (DV * M) / 2 + 1}; +unsigned int PQCLEAN_LEDAKEMLT32_CLEAN_thresholds[2] = {B0, (DV * M) / 2 + 1}; int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], @@ -32,7 +32,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], } /* iteration based threshold determination*/ - unsigned int corrt_syndrome_based = thresholds[iteration]; + unsigned int corrt_syndrome_based = PQCLEAN_LEDAKEMLT32_CLEAN_thresholds[iteration]; //Computation of correlation with a full Q matrix for (int i = 0; i < N0; i++) { diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.c b/crypto_kem/ledakemlt32/clean/dfr_test.c index c9543ed3..da61675c 100644 --- a/crypto_kem/ledakemlt32/clean/dfr_test.c +++ b/crypto_kem/ledakemlt32/clean/dfr_test.c @@ -9,7 +9,7 @@ * computes the threshold for the second iteration of the decoder and stores * it in the globally accessible vector */ -extern unsigned int thresholds[2]; +extern unsigned int PQCLEAN_LEDAKEMLT32_CLEAN_thresholds[2]; int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { @@ -113,7 +113,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - thresholds[1] = allBlockMaxSumst + 1; + PQCLEAN_LEDAKEMLT32_CLEAN_thresholds[1] = allBlockMaxSumst + 1; return 1; } return 0; diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c index 5464dfea..a81d5aeb 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c @@ -7,7 +7,7 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { - int i, j, posTrailingBit, maskOffset, to_copy; + int i, j, posTrailingBit, maskOffset; DIGIT mask, aux[2 * NUM_DIGITS_GF2X_ELEMENT]; memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); @@ -37,9 +37,7 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { } } - to_copy = (2 * NUM_DIGITS_GF2X_ELEMENT > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : 2 * NUM_DIGITS_GF2X_ELEMENT; - - for (i = 0; i < to_copy; i++) { + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; } @@ -445,12 +443,13 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence( AES_XOF_struct *seed_expander_ctx) { int rndPos[NUM_ERRORS_T], duplicated, counter = 0; + int p, polyIndex, exponent; + memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - while (counter < NUM_ERRORS_T) { - int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, - seed_expander_ctx); + p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, + seed_expander_ctx); duplicated = 0; for (int j = 0; j < counter; j++) { if (rndPos[j] == p) { @@ -463,8 +462,8 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence( } } for (int j = 0; j < counter; j++) { - int polyIndex = rndPos[j] / P; - int exponent = rndPos[j] % P; + polyIndex = rndPos[j] / P; + exponent = rndPos[j] % P; gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, ( (DIGIT) 1)); } @@ -475,7 +474,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { for (j = 0; j < DIGIT_SIZE_B; j++) { - bytes[i * DIGIT_SIZE_B + j] = (uint8_t) ((poly[i] >> (8 * j)) & 0xFF); + bytes[i * DIGIT_SIZE_B + j] = (uint8_t) (poly[i] >> 8 * j); } } } diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.c b/crypto_kem/ledakemlt32/clean/niederreiter.c index 470d5bc1..9b72c104 100644 --- a/crypto_kem/ledakemlt32/clean/niederreiter.c +++ b/crypto_kem/ledakemlt32/clean/niederreiter.c @@ -10,25 +10,19 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { - // sequence of N0 circ block matrices (p x p): Hi - POSITION_T HPosOnes[N0][DV]; - POSITION_T HtrPosOnes[N0][DV]; - /* Sparse representation of the transposed circulant matrix H, - with weight DV. Each index contains the position of a '1' digit in the - corresponding Htr block */ - /* Sparse representation of the matrix (Q). - A matrix containing the positions of the ones in the circulant - blocks of Q. Each row contains the position of the - ones of all the blocks of a row of Q as exponent+ - P*block_position */ - POSITION_T QPosOnes[N0][M]; - - /*Rejection-sample for a full L*/ + POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi + POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H + POSITION_T QPosOnes[N0][M]; // Sparse Q, Each row contains the position of the ones of all the blocks of a row of Q as exponent+P*block_position POSITION_T LPosOnes[N0][DV * M]; + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0]; + DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; + DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; int isDFRok = 0; sk->rejections = (int8_t) 0; + do { PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse(QPosOnes, keys_expander); @@ -38,8 +32,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } } - POSITION_T auxPosOnes[DV * M]; - unsigned char processedQOnes[N0] = {0}; + memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, @@ -62,13 +55,14 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } while (!is_L_full || !isDFRok); sk->rejections = sk->rejections - 1; - DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } - DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + + memset(Ln0Inv, 0x00, sizeof(Ln0Inv)); PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); for (int i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, @@ -88,28 +82,36 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publi DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - for (i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(saux, pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, err + i * NUM_DIGITS_GF2X_ELEMENT); gf2x_mod_add(syndrome, syndrome, saux); - } // end for + } gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { - AES_XOF_struct niederreiter_decrypt_expander; - PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); - - // sequence of N0 circ block matrices (p x p): POSITION_T HPosOnes[N0][DV]; POSITION_T HtrPosOnes[N0][DV]; POSITION_T QPosOnes[N0][M]; - int rejections = sk->rejections; + POSITION_T QtrPosOnes[N0][M]; + POSITION_T auxPosOnes[DV * M]; POSITION_T LPosOnes[N0][DV * M]; + POSITION_T auxSparse[DV * M]; + POSITION_T Ln0trSparse[DV * M]; + unsigned char processedQOnes[N0]; + unsigned transposed_ones_idx[N0]; + DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + int rejections = sk->rejections; + int currQoneIdx, endQblockIdx; + int decryptOk, err_weight; + + PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); + do { PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); @@ -119,8 +121,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } } - POSITION_T auxPosOnes[DV * M]; - unsigned char processedQOnes[N0] = {0}; + memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, @@ -135,11 +136,10 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN rejections--; } while (rejections >= 0); - POSITION_T QtrPosOnes[N0][M]; - unsigned transposed_ones_idx[N0] = {0x00}; + memset(transposed_ones_idx, 0x00, sizeof(transposed_ones_idx)); for (unsigned source_row_idx = 0; source_row_idx < N0 ; source_row_idx++) { - int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] - int endQblockIdx = 0; + currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + endQblockIdx = 0; for (int blockIdx = 0; blockIdx < N0; blockIdx++) { endQblockIdx += qBlockWeights[source_row_idx][blockIdx]; for (; currQoneIdx < endQblockIdx; currQoneIdx++) { @@ -150,8 +150,6 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } } - POSITION_T auxSparse[DV * M]; - POSITION_T Ln0trSparse[DV * M]; for (int i = 0; i < DV * M; i++) { Ln0trSparse[i] = INVALID_POS_VALUE; auxSparse[i] = INVALID_POS_VALUE; @@ -167,24 +165,20 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); - - DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); /* prepare mockup error vector in case a decoding failure occurs */ - DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(&niederreiter_decrypt_expander, ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), TRNG_BYTE_LENGTH); - int decryptOk = 0; memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); decryptOk = PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); - int err_weight = 0; + err_weight = 0; for (int i = 0 ; i < N0; i++) { err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } diff --git a/crypto_kem/ledakemlt52/clean/bf_decoding.c b/crypto_kem/ledakemlt52/clean/bf_decoding.c index 531a73d6..f52060f8 100644 --- a/crypto_kem/ledakemlt52/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt52/clean/bf_decoding.c @@ -4,7 +4,7 @@ #include #include -unsigned int thresholds[2] = {B0, (DV * M) / 2 + 1}; +unsigned int PQCLEAN_LEDAKEMLT52_CLEAN_thresholds[2] = {B0, (DV * M) / 2 + 1}; int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], @@ -32,7 +32,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], } /* iteration based threshold determination*/ - unsigned int corrt_syndrome_based = thresholds[iteration]; + unsigned int corrt_syndrome_based = PQCLEAN_LEDAKEMLT52_CLEAN_thresholds[iteration]; //Computation of correlation with a full Q matrix for (int i = 0; i < N0; i++) { diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.c b/crypto_kem/ledakemlt52/clean/dfr_test.c index dde49feb..f5ff3a13 100644 --- a/crypto_kem/ledakemlt52/clean/dfr_test.c +++ b/crypto_kem/ledakemlt52/clean/dfr_test.c @@ -9,7 +9,7 @@ * computes the threshold for the second iteration of the decoder and stores * it in the globally accessible vector */ -extern unsigned int thresholds[2]; +extern unsigned int PQCLEAN_LEDAKEMLT52_CLEAN_thresholds[2]; int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { @@ -113,7 +113,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - thresholds[1] = allBlockMaxSumst + 1; + PQCLEAN_LEDAKEMLT52_CLEAN_thresholds[1] = allBlockMaxSumst + 1; return 1; } return 0; diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c index a89ef1b1..cade74dd 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c @@ -7,7 +7,7 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { - int i, j, posTrailingBit, maskOffset, to_copy; + int i, j, posTrailingBit, maskOffset; DIGIT mask, aux[2 * NUM_DIGITS_GF2X_ELEMENT]; memcpy(aux, in, 2 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); @@ -37,9 +37,7 @@ static void gf2x_mod(DIGIT out[], const DIGIT in[]) { } } - to_copy = (2 * NUM_DIGITS_GF2X_ELEMENT > NUM_DIGITS_GF2X_ELEMENT) ? NUM_DIGITS_GF2X_ELEMENT : 2 * NUM_DIGITS_GF2X_ELEMENT; - - for (i = 0; i < to_copy; i++) { + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { out[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = aux[2 * NUM_DIGITS_GF2X_ELEMENT - 1 - i]; } @@ -445,12 +443,13 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence( AES_XOF_struct *seed_expander_ctx) { int rndPos[NUM_ERRORS_T], duplicated, counter = 0; + int p, polyIndex, exponent; + memset(sequence, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - while (counter < NUM_ERRORS_T) { - int p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, - seed_expander_ctx); + p = rand_range(N0 * NUM_BITS_GF2X_ELEMENT, P_BITS, + seed_expander_ctx); duplicated = 0; for (int j = 0; j < counter; j++) { if (rndPos[j] == p) { @@ -463,8 +462,8 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence( } } for (int j = 0; j < counter; j++) { - int polyIndex = rndPos[j] / P; - int exponent = rndPos[j] % P; + polyIndex = rndPos[j] / P; + exponent = rndPos[j] % P; gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, ( (DIGIT) 1)); } @@ -475,7 +474,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { for (j = 0; j < DIGIT_SIZE_B; j++) { - bytes[i * DIGIT_SIZE_B + j] = (uint8_t) ((poly[i] >> (8 * j)) & 0xFF); + bytes[i * DIGIT_SIZE_B + j] = (uint8_t) (poly[i] >> 8 * j); } } } diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.c b/crypto_kem/ledakemlt52/clean/niederreiter.c index 8a60466a..6827af13 100644 --- a/crypto_kem/ledakemlt52/clean/niederreiter.c +++ b/crypto_kem/ledakemlt52/clean/niederreiter.c @@ -10,25 +10,19 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { - // sequence of N0 circ block matrices (p x p): Hi - POSITION_T HPosOnes[N0][DV]; - POSITION_T HtrPosOnes[N0][DV]; - /* Sparse representation of the transposed circulant matrix H, - with weight DV. Each index contains the position of a '1' digit in the - corresponding Htr block */ - /* Sparse representation of the matrix (Q). - A matrix containing the positions of the ones in the circulant - blocks of Q. Each row contains the position of the - ones of all the blocks of a row of Q as exponent+ - P*block_position */ - POSITION_T QPosOnes[N0][M]; - - /*Rejection-sample for a full L*/ + POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi + POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H + POSITION_T QPosOnes[N0][M]; // Sparse Q, Each row contains the position of the ones of all the blocks of a row of Q as exponent+P*block_position POSITION_T LPosOnes[N0][DV * M]; + POSITION_T auxPosOnes[DV * M]; + unsigned char processedQOnes[N0]; + DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; + DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; int isDFRok = 0; sk->rejections = (int8_t) 0; + do { PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse(QPosOnes, keys_expander); @@ -38,8 +32,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } } - POSITION_T auxPosOnes[DV * M]; - unsigned char processedQOnes[N0] = {0}; + memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, @@ -62,13 +55,14 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } while (!is_L_full || !isDFRok); sk->rejections = sk->rejections - 1; - DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } - DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT] = {0x00}; + + memset(Ln0Inv, 0x00, sizeof(Ln0Inv)); PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); for (int i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, @@ -88,28 +82,36 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publi DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - for (i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(saux, pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, err + i * NUM_DIGITS_GF2X_ELEMENT); gf2x_mod_add(syndrome, syndrome, saux); - } // end for + } gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { - AES_XOF_struct niederreiter_decrypt_expander; - PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); - - // sequence of N0 circ block matrices (p x p): POSITION_T HPosOnes[N0][DV]; POSITION_T HtrPosOnes[N0][DV]; POSITION_T QPosOnes[N0][M]; - int rejections = sk->rejections; + POSITION_T QtrPosOnes[N0][M]; + POSITION_T auxPosOnes[DV * M]; POSITION_T LPosOnes[N0][DV * M]; + POSITION_T auxSparse[DV * M]; + POSITION_T Ln0trSparse[DV * M]; + unsigned char processedQOnes[N0]; + unsigned transposed_ones_idx[N0]; + DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; + DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; + int rejections = sk->rejections; + int currQoneIdx, endQblockIdx; + int decryptOk, err_weight; + + PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); + do { PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); @@ -119,8 +121,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } } - POSITION_T auxPosOnes[DV * M]; - unsigned char processedQOnes[N0] = {0}; + memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, @@ -135,11 +136,10 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN rejections--; } while (rejections >= 0); - POSITION_T QtrPosOnes[N0][M]; - unsigned transposed_ones_idx[N0] = {0x00}; + memset(transposed_ones_idx, 0x00, sizeof(transposed_ones_idx)); for (unsigned source_row_idx = 0; source_row_idx < N0 ; source_row_idx++) { - int currQoneIdx = 0; // position in the column of QtrPosOnes[][...] - int endQblockIdx = 0; + currQoneIdx = 0; // position in the column of QtrPosOnes[][...] + endQblockIdx = 0; for (int blockIdx = 0; blockIdx < N0; blockIdx++) { endQblockIdx += qBlockWeights[source_row_idx][blockIdx]; for (; currQoneIdx < endQblockIdx; currQoneIdx++) { @@ -150,8 +150,6 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } } - POSITION_T auxSparse[DV * M]; - POSITION_T Ln0trSparse[DV * M]; for (int i = 0; i < DV * M; i++) { Ln0trSparse[i] = INVALID_POS_VALUE; auxSparse[i] = INVALID_POS_VALUE; @@ -167,24 +165,20 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); - - DIGIT privateSyndrome[NUM_DIGITS_GF2X_ELEMENT]; PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); /* prepare mockup error vector in case a decoding failure occurs */ - DIGIT mockup_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(&niederreiter_decrypt_expander, ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), TRNG_BYTE_LENGTH); - int decryptOk = 0; memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); decryptOk = PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); - int err_weight = 0; + err_weight = 0; for (int i = 0 ; i < N0; i++) { err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } From e5da5da9a6b5f5a2fb8fcb3ebac8ae7ca915a341 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 11 Jun 2019 16:18:21 +0200 Subject: [PATCH 23/35] use uint8_t in api --- crypto_kem/ledakemlt12/clean/api.h | 8 +++----- crypto_kem/ledakemlt32/clean/api.h | 8 +++----- crypto_kem/ledakemlt52/clean/api.h | 8 +++----- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/api.h b/crypto_kem/ledakemlt12/clean/api.h index 6fc42ab8..a45ae15d 100644 --- a/crypto_kem/ledakemlt12/clean/api.h +++ b/crypto_kem/ledakemlt12/clean/api.h @@ -8,11 +8,9 @@ #define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT12" -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk); - -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk); - -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk); +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t*ct, const uint8_t *sk); #endif diff --git a/crypto_kem/ledakemlt32/clean/api.h b/crypto_kem/ledakemlt32/clean/api.h index afc20eef..6b558211 100644 --- a/crypto_kem/ledakemlt32/clean/api.h +++ b/crypto_kem/ledakemlt32/clean/api.h @@ -8,11 +8,9 @@ #define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT32" -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk); - -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk); - -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk); +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t*ct, const uint8_t *sk); #endif diff --git a/crypto_kem/ledakemlt52/clean/api.h b/crypto_kem/ledakemlt52/clean/api.h index 05ce0565..f6fd9363 100644 --- a/crypto_kem/ledakemlt52/clean/api.h +++ b/crypto_kem/ledakemlt52/clean/api.h @@ -8,11 +8,9 @@ #define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT52" -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk); - -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk); - -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk); +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t*ct, const uint8_t *sk); #endif From 98e643e5c7438eb5ad80807119b5be4a95dc64d2 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 11 Jun 2019 16:20:31 +0200 Subject: [PATCH 24/35] use size_t for index in aes xof buffer and not for index of digits --- crypto_kem/ledakemlt12/clean/gf2x_arith.c | 6 +++--- crypto_kem/ledakemlt12/clean/gf2x_arith.h | 8 ++++---- crypto_kem/ledakemlt12/clean/rng.c | 2 +- crypto_kem/ledakemlt12/clean/rng.h | 4 ++-- crypto_kem/ledakemlt32/clean/gf2x_arith.c | 16 ++++++++-------- crypto_kem/ledakemlt32/clean/gf2x_arith.h | 8 ++++---- crypto_kem/ledakemlt32/clean/rng.c | 2 +- crypto_kem/ledakemlt32/clean/rng.h | 4 ++-- crypto_kem/ledakemlt52/clean/gf2x_arith.c | 6 +++--- crypto_kem/ledakemlt52/clean/gf2x_arith.h | 8 ++++---- crypto_kem/ledakemlt52/clean/rng.c | 2 +- crypto_kem/ledakemlt52/clean/rng.h | 4 ++-- 12 files changed, 35 insertions(+), 35 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/clean/gf2x_arith.c index c2d76059..ac1a1d04 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.c @@ -20,7 +20,7 @@ static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -36,12 +36,12 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsi } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } - size_t j; + int j; DIGIT mask; mask = ~(((DIGIT)0x01 << (DIGIT_SIZE_b - amount)) - 1); for (j = 0 ; j < length - 1 ; j++) { diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.h b/crypto_kem/ledakemlt12/clean/gf2x_arith.h index 032dfd57..9cb10569 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.h @@ -50,14 +50,14 @@ typedef uint64_t DIGIT; #define GF2X_MUL PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb -static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], size_t nr) { - for (size_t i = 0; i < nr; i++) { +static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { + for (int i = 0; i < nr; i++) { Res[i] = A[i] ^ B[i]; } } -void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); -void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); #endif diff --git a/crypto_kem/ledakemlt12/clean/rng.c b/crypto_kem/ledakemlt12/clean/rng.c index d9e9bc66..c79b3195 100644 --- a/crypto_kem/ledakemlt12/clean/rng.c +++ b/crypto_kem/ledakemlt12/clean/rng.c @@ -15,7 +15,7 @@ static int seedexpander_init(AES_XOF_struct *ctx, unsigned char *seed, unsigned char *diversifier, - uint64_t maxlen) { + size_t maxlen) { if ( maxlen >= 0x100000000 ) { return RNG_BAD_MAXLEN; } diff --git a/crypto_kem/ledakemlt12/clean/rng.h b/crypto_kem/ledakemlt12/clean/rng.h index b34dbbc6..7638f216 100644 --- a/crypto_kem/ledakemlt12/clean/rng.h +++ b/crypto_kem/ledakemlt12/clean/rng.h @@ -11,8 +11,8 @@ typedef struct { unsigned char buffer[16]; - unsigned int buffer_pos; - uint64_t length_remaining; + size_t buffer_pos; + size_t length_remaining; unsigned char key[32]; unsigned char ctr[16]; } AES_XOF_struct; diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.c b/crypto_kem/ledakemlt32/clean/gf2x_arith.c index 3ef670d2..cc5cf9c7 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith.c @@ -5,12 +5,12 @@ /* allows the second operand to be shorter than the first */ /* the result should be as large as the first operand*/ -static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], - const size_t na, const DIGIT A[], - const size_t nb, const DIGIT B[]) { +static inline void gf2x_add_asymm(const int nr, DIGIT Res[], + const int na, const DIGIT A[], + const int nb, const DIGIT B[]) { assert(nr >= na && na >= nb); - size_t i; - size_t delta = na - nb; + int i; + int delta = na - nb; for (i = 0; i < delta; i++) { Res[i] = A[i]; } @@ -20,7 +20,7 @@ static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -36,12 +36,12 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsi } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } - size_t j; + int j; DIGIT mask; mask = ~(((DIGIT)0x01 << (DIGIT_SIZE_b - amount)) - 1); for (j = 0 ; j < length - 1 ; j++) { diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.h b/crypto_kem/ledakemlt32/clean/gf2x_arith.h index 051e3b52..d55b4eb3 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith.h @@ -50,14 +50,14 @@ typedef uint64_t DIGIT; #define GF2X_MUL PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mul_comb -static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], size_t nr) { - for (size_t i = 0; i < nr; i++) { +static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { + for (int i = 0; i < nr; i++) { Res[i] = A[i] ^ B[i]; } } -void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); -void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); #endif diff --git a/crypto_kem/ledakemlt32/clean/rng.c b/crypto_kem/ledakemlt32/clean/rng.c index 2b300538..d62569a8 100644 --- a/crypto_kem/ledakemlt32/clean/rng.c +++ b/crypto_kem/ledakemlt32/clean/rng.c @@ -15,7 +15,7 @@ static int seedexpander_init(AES_XOF_struct *ctx, unsigned char *seed, unsigned char *diversifier, - uint64_t maxlen) { + size_t maxlen) { if ( maxlen >= 0x100000000 ) { return RNG_BAD_MAXLEN; } diff --git a/crypto_kem/ledakemlt32/clean/rng.h b/crypto_kem/ledakemlt32/clean/rng.h index 0fe312a6..c50b6daf 100644 --- a/crypto_kem/ledakemlt32/clean/rng.h +++ b/crypto_kem/ledakemlt32/clean/rng.h @@ -11,8 +11,8 @@ typedef struct { unsigned char buffer[16]; - unsigned int buffer_pos; - uint64_t length_remaining; + size_t buffer_pos; + size_t length_remaining; unsigned char key[32]; unsigned char ctr[16]; } AES_XOF_struct; diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.c b/crypto_kem/ledakemlt52/clean/gf2x_arith.c index cd0cddda..fbbd08e1 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith.c @@ -20,7 +20,7 @@ static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -36,12 +36,12 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsi } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } - size_t j; + int j; DIGIT mask; mask = ~(((DIGIT)0x01 << (DIGIT_SIZE_b - amount)) - 1); for (j = 0 ; j < length - 1 ; j++) { diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.h b/crypto_kem/ledakemlt52/clean/gf2x_arith.h index 86303150..c3b6cb4a 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith.h @@ -50,14 +50,14 @@ typedef uint64_t DIGIT; #define GF2X_MUL PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mul_comb -static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], size_t nr) { - for (size_t i = 0; i < nr; i++) { +static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { + for (int i = 0; i < nr; i++) { Res[i] = A[i] ^ B[i]; } } -void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); -void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(size_t length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); #endif diff --git a/crypto_kem/ledakemlt52/clean/rng.c b/crypto_kem/ledakemlt52/clean/rng.c index 28346f3d..3b4af455 100644 --- a/crypto_kem/ledakemlt52/clean/rng.c +++ b/crypto_kem/ledakemlt52/clean/rng.c @@ -15,7 +15,7 @@ static int seedexpander_init(AES_XOF_struct *ctx, unsigned char *seed, unsigned char *diversifier, - uint64_t maxlen) { + size_t maxlen) { if ( maxlen >= 0x100000000 ) { return RNG_BAD_MAXLEN; } diff --git a/crypto_kem/ledakemlt52/clean/rng.h b/crypto_kem/ledakemlt52/clean/rng.h index fea4032d..d6d5b302 100644 --- a/crypto_kem/ledakemlt52/clean/rng.h +++ b/crypto_kem/ledakemlt52/clean/rng.h @@ -11,8 +11,8 @@ typedef struct { unsigned char buffer[16]; - unsigned int buffer_pos; - uint64_t length_remaining; + size_t buffer_pos; + size_t length_remaining; unsigned char key[32]; unsigned char ctr[16]; } AES_XOF_struct; From 26dad0211d6478e972c1f12d0917677ada69fc07 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 11 Jun 2019 16:39:41 +0200 Subject: [PATCH 25/35] remove unused functions --- crypto_kem/ledakemlt12/clean/gf2x_arith.c | 30 ----------------------- crypto_kem/ledakemlt32/clean/gf2x_arith.c | 30 ----------------------- crypto_kem/ledakemlt52/clean/gf2x_arith.c | 30 ----------------------- 3 files changed, 90 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/clean/gf2x_arith.c index ac1a1d04..a26aa99f 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.c @@ -3,22 +3,6 @@ #include #include // memset(...) -/* allows the second operand to be shorter than the first */ -/* the result should be as large as the first operand*/ -static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], - const size_t na, const DIGIT A[], - const size_t nb, const DIGIT B[]) { - assert(nr >= na && na >= nb); - size_t i; - size_t delta = na - nb; - for (i = 0; i < delta; i++) { - Res[i] = A[i]; - } - for (i = 0; i < nb; i++) { - Res[i + delta] = A[i + delta] ^ B[i]; - } -} - /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); @@ -84,17 +68,3 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], } } } - -static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { - DIGIT t = 0; - for (int i = na - 1; i >= 0; i--) { - - t ^= A[i]; - - for (int j = 1; j <= DIGIT_SIZE_b / 2; j = j * 2) { - t ^= t << (unsigned) j; - } - A[i] = t; - t >>= DIGIT_SIZE_b - 1; - } -} diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.c b/crypto_kem/ledakemlt32/clean/gf2x_arith.c index cc5cf9c7..99caa7ec 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith.c @@ -3,22 +3,6 @@ #include #include // memset(...) -/* allows the second operand to be shorter than the first */ -/* the result should be as large as the first operand*/ -static inline void gf2x_add_asymm(const int nr, DIGIT Res[], - const int na, const DIGIT A[], - const int nb, const DIGIT B[]) { - assert(nr >= na && na >= nb); - int i; - int delta = na - nb; - for (i = 0; i < delta; i++) { - Res[i] = A[i]; - } - for (i = 0; i < nb; i++) { - Res[i + delta] = A[i + delta] ^ B[i]; - } -} - /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); @@ -84,17 +68,3 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], } } } - -static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { - DIGIT t = 0; - for (int i = na - 1; i >= 0; i--) { - - t ^= A[i]; - - for (int j = 1; j <= DIGIT_SIZE_b / 2; j = j * 2) { - t ^= t << (unsigned) j; - } - A[i] = t; - t >>= DIGIT_SIZE_b - 1; - } -} diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.c b/crypto_kem/ledakemlt52/clean/gf2x_arith.c index fbbd08e1..9e450a81 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith.c @@ -3,22 +3,6 @@ #include #include // memset(...) -/* allows the second operand to be shorter than the first */ -/* the result should be as large as the first operand*/ -static inline void gf2x_add_asymm(const size_t nr, DIGIT Res[], - const size_t na, const DIGIT A[], - const size_t nb, const DIGIT B[]) { - assert(nr >= na && na >= nb); - size_t i; - size_t delta = na - nb; - for (i = 0; i < delta; i++) { - Res[i] = A[i]; - } - for (i = 0; i < nb; i++) { - Res[i + delta] = A[i + delta] ^ B[i]; - } -} - /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); @@ -84,17 +68,3 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], } } } - -static inline void gf2x_exact_div_x_plus_one(const int na, DIGIT A[]) { - DIGIT t = 0; - for (int i = na - 1; i >= 0; i--) { - - t ^= A[i]; - - for (int j = 1; j <= DIGIT_SIZE_b / 2; j = j * 2) { - t ^= t << (unsigned) j; - } - A[i] = t; - t >>= DIGIT_SIZE_b - 1; - } -} From 9c2449387a94eed35bbd96ca960d3d18975523d1 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 11 Jun 2019 16:50:38 +0200 Subject: [PATCH 26/35] include stdint in api --- crypto_kem/ledakemlt12/clean/api.h | 4 +++- crypto_kem/ledakemlt32/clean/api.h | 4 +++- crypto_kem/ledakemlt52/clean/api.h | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/api.h b/crypto_kem/ledakemlt12/clean/api.h index a45ae15d..170022d3 100644 --- a/crypto_kem/ledakemlt12/clean/api.h +++ b/crypto_kem/ledakemlt12/clean/api.h @@ -1,6 +1,8 @@ #ifndef PQCLEAN_LEDAKEMLT12_CLEAN_API_H #define PQCLEAN_LEDAKEMLT12_CLEAN_API_H +#include + #define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_SECRETKEYBYTES 25 #define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_PUBLICKEYBYTES 6520 #define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_CIPHERTEXTBYTES 6520 @@ -10,7 +12,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t*ct, const uint8_t *sk); +int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); #endif diff --git a/crypto_kem/ledakemlt32/clean/api.h b/crypto_kem/ledakemlt32/clean/api.h index 6b558211..509108d8 100644 --- a/crypto_kem/ledakemlt32/clean/api.h +++ b/crypto_kem/ledakemlt32/clean/api.h @@ -1,6 +1,8 @@ #ifndef PQCLEAN_LEDAKEMLT32_CLEAN_API_H #define PQCLEAN_LEDAKEMLT32_CLEAN_API_H +#include + #define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_SECRETKEYBYTES 33 #define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_PUBLICKEYBYTES 12032 #define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_CIPHERTEXTBYTES 12032 @@ -10,7 +12,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t*ct, const uint8_t *sk); +int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); #endif diff --git a/crypto_kem/ledakemlt52/clean/api.h b/crypto_kem/ledakemlt52/clean/api.h index f6fd9363..9d23bb79 100644 --- a/crypto_kem/ledakemlt52/clean/api.h +++ b/crypto_kem/ledakemlt52/clean/api.h @@ -1,6 +1,8 @@ #ifndef PQCLEAN_LEDAKEMLT52_CLEAN_API_H #define PQCLEAN_LEDAKEMLT52_CLEAN_API_H +#include + #define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_SECRETKEYBYTES 41 #define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_PUBLICKEYBYTES 19040 #define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_CIPHERTEXTBYTES 19040 @@ -10,7 +12,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t*ct, const uint8_t *sk); +int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); #endif From 889a1f1e53bae31132db1663d0edbd28a2f9156f Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 11 Jun 2019 17:09:28 +0200 Subject: [PATCH 27/35] fix mvsc warning --- crypto_kem/ledakemlt12/clean/rng.c | 2 +- crypto_kem/ledakemlt32/clean/rng.c | 2 +- crypto_kem/ledakemlt52/clean/rng.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/rng.c b/crypto_kem/ledakemlt12/clean/rng.c index c79b3195..490e7c09 100644 --- a/crypto_kem/ledakemlt12/clean/rng.c +++ b/crypto_kem/ledakemlt12/clean/rng.c @@ -67,7 +67,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, xlen - number of bytes to return */ int PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { - uint32_t offset; + size_t offset; aes256ctx ctx256; if ( x == NULL ) { diff --git a/crypto_kem/ledakemlt32/clean/rng.c b/crypto_kem/ledakemlt32/clean/rng.c index d62569a8..336c65e3 100644 --- a/crypto_kem/ledakemlt32/clean/rng.c +++ b/crypto_kem/ledakemlt32/clean/rng.c @@ -67,7 +67,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, xlen - number of bytes to return */ int PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { - uint32_t offset; + size_t offset; aes256ctx ctx256; if ( x == NULL ) { diff --git a/crypto_kem/ledakemlt52/clean/rng.c b/crypto_kem/ledakemlt52/clean/rng.c index 3b4af455..fdbe2148 100644 --- a/crypto_kem/ledakemlt52/clean/rng.c +++ b/crypto_kem/ledakemlt52/clean/rng.c @@ -67,7 +67,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, xlen - number of bytes to return */ int PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { - uint32_t offset; + size_t offset; aes256ctx ctx256; if ( x == NULL ) { From 9e3f973f5665119f2447fbd87266dc334fc29be3 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 11 Jun 2019 21:45:39 +0200 Subject: [PATCH 28/35] define a constant for max number of rng bytes, remove unnecessary check --- crypto_kem/ledakemlt12/clean/rng.c | 15 +++++---------- crypto_kem/ledakemlt12/clean/rng.h | 1 + crypto_kem/ledakemlt32/clean/rng.c | 15 +++++---------- crypto_kem/ledakemlt32/clean/rng.h | 1 + crypto_kem/ledakemlt52/clean/rng.c | 15 +++++---------- crypto_kem/ledakemlt52/clean/rng.h | 1 + 6 files changed, 18 insertions(+), 30 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/rng.c b/crypto_kem/ledakemlt12/clean/rng.c index 490e7c09..913b9e67 100644 --- a/crypto_kem/ledakemlt12/clean/rng.c +++ b/crypto_kem/ledakemlt12/clean/rng.c @@ -12,13 +12,10 @@ diversifier - an 8 byte diversifier maxlen - maximum number of bytes (less than 2**32) generated under this seed and diversifier */ -static int seedexpander_init(AES_XOF_struct *ctx, - unsigned char *seed, - unsigned char *diversifier, - size_t maxlen) { - if ( maxlen >= 0x100000000 ) { - return RNG_BAD_MAXLEN; - } +static void seedexpander_init(AES_XOF_struct *ctx, + unsigned char *seed, + unsigned char *diversifier, + size_t maxlen) { ctx->length_remaining = maxlen; @@ -38,8 +35,6 @@ static int seedexpander_init(AES_XOF_struct *ctx, ctx->buffer_pos = 16; memset(ctx->buffer, 0x00, 16); - - return RNG_SUCCESS; } void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, @@ -57,7 +52,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, /* the required seed expansion will be quite small, set the max number of * bytes conservatively to 10 MiB*/ - seedexpander_init(ctx, prng_buffer, diversifier, 10 * 1024 * 1024); + seedexpander_init(ctx, prng_buffer, diversifier, RNG_MAXLEN); } /* diff --git a/crypto_kem/ledakemlt12/clean/rng.h b/crypto_kem/ledakemlt12/clean/rng.h index 7638f216..204e8f7b 100644 --- a/crypto_kem/ledakemlt12/clean/rng.h +++ b/crypto_kem/ledakemlt12/clean/rng.h @@ -8,6 +8,7 @@ #define RNG_BAD_MAXLEN (-1) #define RNG_BAD_OUTBUF (-2) #define RNG_BAD_REQ_LEN (-3) +#define RNG_MAXLEN (10 * 1024 * 1024) typedef struct { unsigned char buffer[16]; diff --git a/crypto_kem/ledakemlt32/clean/rng.c b/crypto_kem/ledakemlt32/clean/rng.c index 336c65e3..b0757cee 100644 --- a/crypto_kem/ledakemlt32/clean/rng.c +++ b/crypto_kem/ledakemlt32/clean/rng.c @@ -12,13 +12,10 @@ diversifier - an 8 byte diversifier maxlen - maximum number of bytes (less than 2**32) generated under this seed and diversifier */ -static int seedexpander_init(AES_XOF_struct *ctx, - unsigned char *seed, - unsigned char *diversifier, - size_t maxlen) { - if ( maxlen >= 0x100000000 ) { - return RNG_BAD_MAXLEN; - } +static void seedexpander_init(AES_XOF_struct *ctx, + unsigned char *seed, + unsigned char *diversifier, + size_t maxlen) { ctx->length_remaining = maxlen; @@ -38,8 +35,6 @@ static int seedexpander_init(AES_XOF_struct *ctx, ctx->buffer_pos = 16; memset(ctx->buffer, 0x00, 16); - - return RNG_SUCCESS; } void PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, @@ -57,7 +52,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, /* the required seed expansion will be quite small, set the max number of * bytes conservatively to 10 MiB*/ - seedexpander_init(ctx, prng_buffer, diversifier, 10 * 1024 * 1024); + seedexpander_init(ctx, prng_buffer, diversifier, RNG_MAXLEN); } /* diff --git a/crypto_kem/ledakemlt32/clean/rng.h b/crypto_kem/ledakemlt32/clean/rng.h index c50b6daf..8d7ee8c4 100644 --- a/crypto_kem/ledakemlt32/clean/rng.h +++ b/crypto_kem/ledakemlt32/clean/rng.h @@ -8,6 +8,7 @@ #define RNG_BAD_MAXLEN (-1) #define RNG_BAD_OUTBUF (-2) #define RNG_BAD_REQ_LEN (-3) +#define RNG_MAXLEN (10 * 1024 * 1024) typedef struct { unsigned char buffer[16]; diff --git a/crypto_kem/ledakemlt52/clean/rng.c b/crypto_kem/ledakemlt52/clean/rng.c index fdbe2148..ef1a207f 100644 --- a/crypto_kem/ledakemlt52/clean/rng.c +++ b/crypto_kem/ledakemlt52/clean/rng.c @@ -12,13 +12,10 @@ diversifier - an 8 byte diversifier maxlen - maximum number of bytes (less than 2**32) generated under this seed and diversifier */ -static int seedexpander_init(AES_XOF_struct *ctx, - unsigned char *seed, - unsigned char *diversifier, - size_t maxlen) { - if ( maxlen >= 0x100000000 ) { - return RNG_BAD_MAXLEN; - } +static void seedexpander_init(AES_XOF_struct *ctx, + unsigned char *seed, + unsigned char *diversifier, + size_t maxlen) { ctx->length_remaining = maxlen; @@ -38,8 +35,6 @@ static int seedexpander_init(AES_XOF_struct *ctx, ctx->buffer_pos = 16; memset(ctx->buffer, 0x00, 16); - - return RNG_SUCCESS; } void PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, @@ -57,7 +52,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, /* the required seed expansion will be quite small, set the max number of * bytes conservatively to 10 MiB*/ - seedexpander_init(ctx, prng_buffer, diversifier, 10 * 1024 * 1024); + seedexpander_init(ctx, prng_buffer, diversifier, RNG_MAXLEN); } /* diff --git a/crypto_kem/ledakemlt52/clean/rng.h b/crypto_kem/ledakemlt52/clean/rng.h index d6d5b302..5bfc8703 100644 --- a/crypto_kem/ledakemlt52/clean/rng.h +++ b/crypto_kem/ledakemlt52/clean/rng.h @@ -8,6 +8,7 @@ #define RNG_BAD_MAXLEN (-1) #define RNG_BAD_OUTBUF (-2) #define RNG_BAD_REQ_LEN (-3) +#define RNG_MAXLEN (10 * 1024 * 1024) typedef struct { unsigned char buffer[16]; From 6811a405270995aae342502bed653bf64cfa7dd5 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 11 Jun 2019 22:50:33 +0200 Subject: [PATCH 29/35] move implementations of functions to .c files --- crypto_kem/ledakemlt12/clean/bf_decoding.c | 8 +- crypto_kem/ledakemlt12/clean/dfr_test.c | 4 +- crypto_kem/ledakemlt12/clean/gf2x_arith.c | 6 + crypto_kem/ledakemlt12/clean/gf2x_arith.h | 7 +- .../clean/gf2x_arith_mod_xPplusOne.c | 125 +++++++++++++++--- .../clean/gf2x_arith_mod_xPplusOne.h | 107 +-------------- crypto_kem/ledakemlt12/clean/niederreiter.c | 8 +- crypto_kem/ledakemlt32/clean/bf_decoding.c | 8 +- crypto_kem/ledakemlt32/clean/dfr_test.c | 4 +- crypto_kem/ledakemlt32/clean/gf2x_arith.c | 6 + crypto_kem/ledakemlt32/clean/gf2x_arith.h | 7 +- .../clean/gf2x_arith_mod_xPplusOne.c | 120 +++++++++++++++-- .../clean/gf2x_arith_mod_xPplusOne.h | 107 +-------------- crypto_kem/ledakemlt32/clean/niederreiter.c | 8 +- crypto_kem/ledakemlt52/clean/bf_decoding.c | 8 +- crypto_kem/ledakemlt52/clean/dfr_test.c | 4 +- crypto_kem/ledakemlt52/clean/gf2x_arith.c | 6 + crypto_kem/ledakemlt52/clean/gf2x_arith.h | 7 +- .../clean/gf2x_arith_mod_xPplusOne.c | 119 +++++++++++++++-- .../clean/gf2x_arith_mod_xPplusOne.h | 107 +-------------- crypto_kem/ledakemlt52/clean/niederreiter.c | 8 +- 21 files changed, 396 insertions(+), 388 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/clean/bf_decoding.c index 977eb6ea..070771dd 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.c @@ -18,13 +18,13 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], int iteration = 0; do { - gf2x_copy(currSyndrome, privateSyndrome); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); for (int i = 0; i < N0; i++) { for (int valueIdx = 0; valueIdx < P; valueIdx++) { for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); - if (gf2x_get_coeff(currSyndrome, tmp)) { + if (PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_get_coeff(currSyndrome, tmp)) { unsatParityChecks[i * P + valueIdx]++; } } @@ -54,13 +54,13 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], } /* Correlation based flipping */ if (correlation >= corrt_syndrome_based) { - gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { unsigned syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; - gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); } } // end for v } // end if diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt12/clean/dfr_test.c index 157e77d8..522a98f3 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.c +++ b/crypto_kem/ledakemlt12/clean/dfr_test.c @@ -38,7 +38,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { LSparse_loc[i][j] = (P - LSparse[i][j]); } } - quicksort_sparse(LSparse_loc[i]); + PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(LSparse_loc[i]); } for (int i = 0; i < N0; i++ ) { @@ -48,7 +48,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; } - quicksort_sparse(rotated_column); + PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(rotated_column); /* compute the intersection amount */ firstidx = 0, secondidx = 0; intersectionval = 0; diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/clean/gf2x_arith.c index a26aa99f..980c22de 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.c @@ -3,6 +3,12 @@ #include #include // memset(...) +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { + for (int i = 0; i < nr; i++) { + Res[i] = A[i] ^ B[i]; + } +} + /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.h b/crypto_kem/ledakemlt12/clean/gf2x_arith.h index 9cb10569..8f890ab1 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith.h @@ -50,12 +50,7 @@ typedef uint64_t DIGIT; #define GF2X_MUL PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb -static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { - for (int i = 0; i < nr; i++) { - Res[i] = A[i] ^ B[i]; - } -} - +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index 217b24f1..d56b18a0 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -4,6 +4,103 @@ #include #include // memcpy(...), memset(...) +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]) { + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + dest[i] = in[i]; + } +} + +/* returns the coefficient of the x^exponent term as the LSB of a digit */ +DIGIT PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; +} + +/* sets the coefficient of the x^exponent term as the LSB of a digit */ +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] & mask; + poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); +} + +/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] ^ mask; +} + +/* population count for an unsigned 64-bit integer + Source: Hacker's delight, p.66 */ +static int popcount_uint64t(uint64_t x) { + x -= (x >> 1) & 0x5555555555555555; + x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); + x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; + return (int)((x * 0x0101010101010101) >> 56); +} + +/* population count for a single polynomial */ +int PQCLEAN_LEDAKEMLT12_CLEAN_population_count(DIGIT *poly) { + int ret = 0; + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + ret += popcount_uint64t(poly[i]); + } + return ret; +} + +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); +} + +static int partition(POSITION_T arr[], int lo, int hi) { + POSITION_T x = arr[hi]; + POSITION_T tmp; + int i = (lo - 1); + for (int j = lo; j <= hi - 1; j++) { + if (arr[j] <= x) { + i++; + tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + } + tmp = arr[i + 1]; + arr[i + 1] = arr[hi]; + arr[hi] = tmp; + + return i + 1; +} + +void PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(POSITION_T Res[]) { + int stack[DV * M]; + int hi, lo, pivot, tos = -1; + stack[++tos] = 0; + stack[++tos] = (DV * M) - 1; + while (tos >= 0 ) { + hi = stack[tos--]; + lo = stack[tos--]; + pivot = partition(Res, lo, hi); + if ( (pivot - 1) > lo) { + stack[++tos] = lo; + stack[++tos] = pivot - 1; + } + if ( (pivot + 1) < hi) { + stack[++tos] = pivot + 1; + stack[++tos] = hi; + } + } +} static void gf2x_mod(DIGIT out[], const DIGIT in[]) { @@ -65,7 +162,7 @@ static void right_bit_shift(unsigned int length, DIGIT in[]) { /* shifts by whole digits */ -static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { +static void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { unsigned int j; for (j = 0; (j + amount) < length; j++) { in[j] = in[j + amount]; @@ -75,7 +172,6 @@ static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned } } - /* may shift by an arbitrary amount*/ static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); @@ -115,7 +211,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; } - A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // reverse middle digit + A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); if (slack_bits_amount) { PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); @@ -143,9 +239,7 @@ static void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ in[0] |= rotated_bit; } -static void gf2x_swap(const int length, - DIGIT f[], - DIGIT s[]) { +static void gf2x_swap(const int length, DIGIT f[], DIGIT s[]) { DIGIT t; for (int i = length - 1; i >= 0; i--) { t = f[i]; @@ -174,7 +268,7 @@ static void gf2x_swap(const int length, int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ int i; - long int delta = 0; + int delta = 0; DIGIT u[NUM_DIGITS_GF2X_ELEMENT] = {0}; DIGIT v[NUM_DIGITS_GF2X_ELEMENT] = {0}; DIGIT s[NUM_DIGITS_GF2X_MODULUS] = {0}; @@ -205,8 +299,8 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { delta += 1; } else { if ( (s[0] & mask) != 0) { - gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); - gf2x_mod_add(v, v, u); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(v, v, u); } left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); if ( delta == 0 ) { @@ -258,7 +352,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse( for (unsigned int i = 1; i < nPos; i++) { if (sparse[i] != INVALID_POS_VALUE) { left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); - gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); } } } @@ -312,7 +406,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ Res[lastFilledPos] = INVALID_POS_VALUE; lastFilledPos++; } - quicksort_sparse(Res); + PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(Res); /* eliminate duplicates */ POSITION_T lastReadPos = Res[0]; int duplicateCount; @@ -438,8 +532,9 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, } /* Returns random weight-t circulant block */ -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], - AES_XOF_struct *seed_expander_ctx) { +void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( + DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], + AES_XOF_struct *seed_expander_ctx) { int rndPos[NUM_ERRORS_T], duplicated, counter = 0; int p, polyIndex, exponent; @@ -463,8 +558,8 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT sequence[N0 for (int j = 0; j < counter; j++) { polyIndex = rndPos[j] / P; exponent = rndPos[j] % P; - gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, - ( (DIGIT) 1)); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + ( (DIGIT) 1)); } } diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h index af42422f..cfae3dc6 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h @@ -15,105 +15,13 @@ #define INVALID_POS_VALUE (P) #define P_BITS (16) // log_2(p) = 15.6703 - -static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { - for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { - dest[i] = in[i]; - } -} - -/* returns the coefficient of the x^exponent term as the LSB of a digit */ -static inline DIGIT gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; -} - -/* sets the coefficient of the x^exponent term as the LSB of a digit */ -static inline void gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - - /* clear given coefficient */ - DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); - poly[digitIdx] = poly[digitIdx] & mask; - poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); -} - -/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ -static inline void gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - - /* clear given coefficient */ - DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); - poly[digitIdx] = poly[digitIdx] ^ mask; -} - -/* population count for an unsigned 64-bit integer - Source: Hacker's delight, p.66 */ -static int popcount_uint64t(uint64_t x) { - x -= (x >> 1) & 0x5555555555555555; - x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); - x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; - return (int)((x * 0x0101010101010101) >> 56); -} - -/* population count for a single polynomial */ -static inline int population_count(DIGIT *poly) { - int ret = 0; - for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { - ret += popcount_uint64t(poly[i]); - } - return ret; -} - -static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { - gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); -} - -static inline int partition(POSITION_T arr[], int lo, int hi) { - POSITION_T x = arr[hi]; - POSITION_T tmp; - int i = (lo - 1); - for (int j = lo; j <= hi - 1; j++) { - if (arr[j] <= x) { - i++; - tmp = arr[i]; - arr[i] = arr[j]; - arr[j] = tmp; - } - } - tmp = arr[i + 1]; - arr[i + 1] = arr[hi]; - arr[hi] = tmp; - - return i + 1; -} - -static inline void quicksort_sparse(POSITION_T Res[]) { - int stack[DV * M]; - int hi, lo, pivot, tos = -1; - stack[++tos] = 0; - stack[++tos] = (DV * M) - 1; - while (tos >= 0 ) { - hi = stack[tos--]; - lo = stack[tos--]; - pivot = partition(Res, lo, hi); - if ( (pivot - 1) > lo) { - stack[++tos] = lo; - stack[++tos] = pivot - 1; - } - if ( (pivot + 1) < hi) { - stack[++tos] = pivot + 1; - stack[++tos] = hi; - } - } -} - +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]); +DIGIT PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); +int PQCLEAN_LEDAKEMLT12_CLEAN_population_count(DIGIT *poly); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(POSITION_T Res[]); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]); void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); @@ -123,7 +31,6 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITIO void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); - int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); #endif diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index e2326bd4..1cb3ea51 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -58,7 +58,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { - gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } @@ -86,9 +86,9 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publi PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(saux, pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, err + i * NUM_DIGITS_GF2X_ELEMENT); - gf2x_mod_add(syndrome, syndrome, saux); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(syndrome, syndrome, saux); } - gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } @@ -180,7 +180,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN err_weight = 0; for (int i = 0 ; i < N0; i++) { - err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + err_weight += PQCLEAN_LEDAKEMLT12_CLEAN_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); diff --git a/crypto_kem/ledakemlt32/clean/bf_decoding.c b/crypto_kem/ledakemlt32/clean/bf_decoding.c index 3ed29fb1..f2eb85aa 100644 --- a/crypto_kem/ledakemlt32/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt32/clean/bf_decoding.c @@ -18,13 +18,13 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], int iteration = 0; do { - gf2x_copy(currSyndrome, privateSyndrome); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); for (int i = 0; i < N0; i++) { for (int valueIdx = 0; valueIdx < P; valueIdx++) { for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); - if (gf2x_get_coeff(currSyndrome, tmp)) { + if (PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_get_coeff(currSyndrome, tmp)) { unsatParityChecks[i * P + valueIdx]++; } } @@ -54,13 +54,13 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], } /* Correlation based flipping */ if (correlation >= corrt_syndrome_based) { - gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { unsigned syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; - gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); } } // end for v } // end if diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.c b/crypto_kem/ledakemlt32/clean/dfr_test.c index da61675c..99571cf5 100644 --- a/crypto_kem/ledakemlt32/clean/dfr_test.c +++ b/crypto_kem/ledakemlt32/clean/dfr_test.c @@ -38,7 +38,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { LSparse_loc[i][j] = (P - LSparse[i][j]); } } - quicksort_sparse(LSparse_loc[i]); + PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(LSparse_loc[i]); } for (int i = 0; i < N0; i++ ) { @@ -48,7 +48,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; } - quicksort_sparse(rotated_column); + PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(rotated_column); /* compute the intersection amount */ firstidx = 0, secondidx = 0; intersectionval = 0; diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.c b/crypto_kem/ledakemlt32/clean/gf2x_arith.c index 99caa7ec..713e9cc5 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith.c @@ -3,6 +3,12 @@ #include #include // memset(...) +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { + for (int i = 0; i < nr; i++) { + Res[i] = A[i] ^ B[i]; + } +} + /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.h b/crypto_kem/ledakemlt32/clean/gf2x_arith.h index d55b4eb3..720d6fde 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith.h @@ -50,12 +50,7 @@ typedef uint64_t DIGIT; #define GF2X_MUL PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mul_comb -static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { - for (int i = 0; i < nr; i++) { - Res[i] = A[i] ^ B[i]; - } -} - +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c index a81d5aeb..39067248 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c @@ -4,6 +4,103 @@ #include #include // memcpy(...), memset(...) +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]) { + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + dest[i] = in[i]; + } +} + +/* returns the coefficient of the x^exponent term as the LSB of a digit */ +DIGIT PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; +} + +/* sets the coefficient of the x^exponent term as the LSB of a digit */ +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] & mask; + poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); +} + +/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] ^ mask; +} + +/* population count for an unsigned 64-bit integer + Source: Hacker's delight, p.66 */ +static int popcount_uint64t(uint64_t x) { + x -= (x >> 1) & 0x5555555555555555; + x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); + x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; + return (int)((x * 0x0101010101010101) >> 56); +} + +/* population count for a single polynomial */ +int PQCLEAN_LEDAKEMLT32_CLEAN_population_count(DIGIT *poly) { + int ret = 0; + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + ret += popcount_uint64t(poly[i]); + } + return ret; +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); +} + +static int partition(POSITION_T arr[], int lo, int hi) { + POSITION_T x = arr[hi]; + POSITION_T tmp; + int i = (lo - 1); + for (int j = lo; j <= hi - 1; j++) { + if (arr[j] <= x) { + i++; + tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + } + tmp = arr[i + 1]; + arr[i + 1] = arr[hi]; + arr[hi] = tmp; + + return i + 1; +} + +void PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(POSITION_T Res[]) { + int stack[DV * M]; + int hi, lo, pivot, tos = -1; + stack[++tos] = 0; + stack[++tos] = (DV * M) - 1; + while (tos >= 0 ) { + hi = stack[tos--]; + lo = stack[tos--]; + pivot = partition(Res, lo, hi); + if ( (pivot - 1) > lo) { + stack[++tos] = lo; + stack[++tos] = pivot - 1; + } + if ( (pivot + 1) < hi) { + stack[++tos] = pivot + 1; + stack[++tos] = hi; + } + } +} static void gf2x_mod(DIGIT out[], const DIGIT in[]) { @@ -65,7 +162,7 @@ static void right_bit_shift(unsigned int length, DIGIT in[]) { /* shifts by whole digits */ -static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { +static void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { unsigned int j; for (j = 0; (j + amount) < length; j++) { in[j] = in[j + amount]; @@ -75,7 +172,6 @@ static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned } } - /* may shift by an arbitrary amount*/ static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); @@ -115,7 +211,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; } - // A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // reverse middle digit + // A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // no middle digit if (slack_bits_amount) { PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); @@ -143,9 +239,7 @@ static void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ in[0] |= rotated_bit; } -static void gf2x_swap(const int length, - DIGIT f[], - DIGIT s[]) { +static void gf2x_swap(const int length, DIGIT f[], DIGIT s[]) { DIGIT t; for (int i = length - 1; i >= 0; i--) { t = f[i]; @@ -174,7 +268,7 @@ static void gf2x_swap(const int length, int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ int i; - long int delta = 0; + int delta = 0; DIGIT u[NUM_DIGITS_GF2X_ELEMENT] = {0}; DIGIT v[NUM_DIGITS_GF2X_ELEMENT] = {0}; DIGIT s[NUM_DIGITS_GF2X_MODULUS] = {0}; @@ -205,8 +299,8 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { delta += 1; } else { if ( (s[0] & mask) != 0) { - gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); - gf2x_mod_add(v, v, u); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(v, v, u); } left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); if ( delta == 0 ) { @@ -258,7 +352,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse( for (unsigned int i = 1; i < nPos; i++) { if (sparse[i] != INVALID_POS_VALUE) { left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); - gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); } } } @@ -312,7 +406,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ Res[lastFilledPos] = INVALID_POS_VALUE; lastFilledPos++; } - quicksort_sparse(Res); + PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(Res); /* eliminate duplicates */ POSITION_T lastReadPos = Res[0]; int duplicateCount; @@ -464,8 +558,8 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence( for (int j = 0; j < counter; j++) { polyIndex = rndPos[j] / P; exponent = rndPos[j] % P; - gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, - ( (DIGIT) 1)); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + ( (DIGIT) 1)); } } diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h index 2e1a266a..8f7a1f53 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h @@ -15,105 +15,13 @@ #define INVALID_POS_VALUE (P) #define P_BITS (17) // log_2(p) = 16.55406417 - -static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { - for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { - dest[i] = in[i]; - } -} - -/* returns the coefficient of the x^exponent term as the LSB of a digit */ -static inline DIGIT gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; -} - -/* sets the coefficient of the x^exponent term as the LSB of a digit */ -static inline void gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - - /* clear given coefficient */ - DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); - poly[digitIdx] = poly[digitIdx] & mask; - poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); -} - -/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ -static inline void gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - - /* clear given coefficient */ - DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); - poly[digitIdx] = poly[digitIdx] ^ mask; -} - -/* population count for an unsigned 64-bit integer - Source: Hacker's delight, p.66 */ -static int popcount_uint64t(uint64_t x) { - x -= (x >> 1) & 0x5555555555555555; - x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); - x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; - return (int)((x * 0x0101010101010101) >> 56); -} - -/* population count for a single polynomial */ -static inline int population_count(DIGIT *poly) { - int ret = 0; - for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { - ret += popcount_uint64t(poly[i]); - } - return ret; -} - -static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { - gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); -} - -static inline int partition(POSITION_T arr[], int lo, int hi) { - POSITION_T x = arr[hi]; - POSITION_T tmp; - int i = (lo - 1); - for (int j = lo; j <= hi - 1; j++) { - if (arr[j] <= x) { - i++; - tmp = arr[i]; - arr[i] = arr[j]; - arr[j] = tmp; - } - } - tmp = arr[i + 1]; - arr[i + 1] = arr[hi]; - arr[hi] = tmp; - - return i + 1; -} - -static inline void quicksort_sparse(POSITION_T Res[]) { - int stack[DV * M]; - int hi, lo, pivot, tos = -1; - stack[++tos] = 0; - stack[++tos] = (DV * M) - 1; - while (tos >= 0 ) { - hi = stack[tos--]; - lo = stack[tos--]; - pivot = partition(Res, lo, hi); - if ( (pivot - 1) > lo) { - stack[++tos] = lo; - stack[++tos] = pivot - 1; - } - if ( (pivot + 1) < hi) { - stack[++tos] = pivot + 1; - stack[++tos] = hi; - } - } -} - +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]); +DIGIT PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); +int PQCLEAN_LEDAKEMLT32_CLEAN_population_count(DIGIT *poly); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(POSITION_T Res[]); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(DIGIT A[]); void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); @@ -123,7 +31,6 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITIO void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); - int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); #endif diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.c b/crypto_kem/ledakemlt32/clean/niederreiter.c index 9b72c104..3d524800 100644 --- a/crypto_kem/ledakemlt32/clean/niederreiter.c +++ b/crypto_kem/ledakemlt32/clean/niederreiter.c @@ -58,7 +58,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { - gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } @@ -86,9 +86,9 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publi PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(saux, pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, err + i * NUM_DIGITS_GF2X_ELEMENT); - gf2x_mod_add(syndrome, syndrome, saux); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(syndrome, syndrome, saux); } - gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } @@ -180,7 +180,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN err_weight = 0; for (int i = 0 ; i < N0; i++) { - err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + err_weight += PQCLEAN_LEDAKEMLT32_CLEAN_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); diff --git a/crypto_kem/ledakemlt52/clean/bf_decoding.c b/crypto_kem/ledakemlt52/clean/bf_decoding.c index f52060f8..2b90c00a 100644 --- a/crypto_kem/ledakemlt52/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt52/clean/bf_decoding.c @@ -18,13 +18,13 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], int iteration = 0; do { - gf2x_copy(currSyndrome, privateSyndrome); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); for (int i = 0; i < N0; i++) { for (int valueIdx = 0; valueIdx < P; valueIdx++) { for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); - if (gf2x_get_coeff(currSyndrome, tmp)) { + if (PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_get_coeff(currSyndrome, tmp)) { unsatParityChecks[i * P + valueIdx]++; } } @@ -54,13 +54,13 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], } /* Correlation based flipping */ if (correlation >= corrt_syndrome_based) { - gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { unsigned syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; - gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); } } // end for v } // end if diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.c b/crypto_kem/ledakemlt52/clean/dfr_test.c index f5ff3a13..e8de098e 100644 --- a/crypto_kem/ledakemlt52/clean/dfr_test.c +++ b/crypto_kem/ledakemlt52/clean/dfr_test.c @@ -38,7 +38,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { LSparse_loc[i][j] = (P - LSparse[i][j]); } } - quicksort_sparse(LSparse_loc[i]); + PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(LSparse_loc[i]); } for (int i = 0; i < N0; i++ ) { @@ -48,7 +48,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; } - quicksort_sparse(rotated_column); + PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(rotated_column); /* compute the intersection amount */ firstidx = 0, secondidx = 0; intersectionval = 0; diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.c b/crypto_kem/ledakemlt52/clean/gf2x_arith.c index 9e450a81..73652552 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith.c @@ -3,6 +3,12 @@ #include #include // memset(...) +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { + for (int i = 0; i < nr; i++) { + Res[i] = A[i] ^ B[i]; + } +} + /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.h b/crypto_kem/ledakemlt52/clean/gf2x_arith.h index c3b6cb4a..cdb6b303 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith.h @@ -50,12 +50,7 @@ typedef uint64_t DIGIT; #define GF2X_MUL PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mul_comb -static inline void gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { - for (int i = 0; i < nr; i++) { - Res[i] = A[i] ^ B[i]; - } -} - +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c index cade74dd..d48c804d 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c @@ -5,6 +5,104 @@ #include // memcpy(...), memset(...) +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]) { + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + dest[i] = in[i]; + } +} + +/* returns the coefficient of the x^exponent term as the LSB of a digit */ +DIGIT PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; +} + +/* sets the coefficient of the x^exponent term as the LSB of a digit */ +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] & mask; + poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); +} + +/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { + unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; + unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; + unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; + + /* clear given coefficient */ + DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); + poly[digitIdx] = poly[digitIdx] ^ mask; +} + +/* population count for an unsigned 64-bit integer + Source: Hacker's delight, p.66 */ +static int popcount_uint64t(uint64_t x) { + x -= (x >> 1) & 0x5555555555555555; + x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); + x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; + return (int)((x * 0x0101010101010101) >> 56); +} + +/* population count for a single polynomial */ +int PQCLEAN_LEDAKEMLT52_CLEAN_population_count(DIGIT *poly) { + int ret = 0; + for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { + ret += popcount_uint64t(poly[i]); + } + return ret; +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); +} + +static int partition(POSITION_T arr[], int lo, int hi) { + POSITION_T x = arr[hi]; + POSITION_T tmp; + int i = (lo - 1); + for (int j = lo; j <= hi - 1; j++) { + if (arr[j] <= x) { + i++; + tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } + } + tmp = arr[i + 1]; + arr[i + 1] = arr[hi]; + arr[hi] = tmp; + + return i + 1; +} + +void PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(POSITION_T Res[]) { + int stack[DV * M]; + int hi, lo, pivot, tos = -1; + stack[++tos] = 0; + stack[++tos] = (DV * M) - 1; + while (tos >= 0 ) { + hi = stack[tos--]; + lo = stack[tos--]; + pivot = partition(Res, lo, hi); + if ( (pivot - 1) > lo) { + stack[++tos] = lo; + stack[++tos] = pivot - 1; + } + if ( (pivot + 1) < hi) { + stack[++tos] = pivot + 1; + stack[++tos] = hi; + } + } +} + static void gf2x_mod(DIGIT out[], const DIGIT in[]) { int i, j, posTrailingBit, maskOffset; @@ -65,7 +163,7 @@ static void right_bit_shift(unsigned int length, DIGIT in[]) { /* shifts by whole digits */ -static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { +static void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amount) { unsigned int j; for (j = 0; (j + amount) < length; j++) { in[j] = in[j + amount]; @@ -75,7 +173,6 @@ static inline void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned } } - /* may shift by an arbitrary amount*/ static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); @@ -143,9 +240,7 @@ static void rotate_bit_right(DIGIT in[]) { /* x^{-1} * in(x) mod x^P+1 */ in[0] |= rotated_bit; } -static void gf2x_swap(const int length, - DIGIT f[], - DIGIT s[]) { +static void gf2x_swap(const int length, DIGIT f[], DIGIT s[]) { DIGIT t; for (int i = length - 1; i >= 0; i--) { t = f[i]; @@ -174,7 +269,7 @@ static void gf2x_swap(const int length, int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ int i; - long int delta = 0; + int delta = 0; DIGIT u[NUM_DIGITS_GF2X_ELEMENT] = {0}; DIGIT v[NUM_DIGITS_GF2X_ELEMENT] = {0}; DIGIT s[NUM_DIGITS_GF2X_MODULUS] = {0}; @@ -205,8 +300,8 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { delta += 1; } else { if ( (s[0] & mask) != 0) { - gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); - gf2x_mod_add(v, v, u); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(v, v, u); } left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); if ( delta == 0 ) { @@ -258,7 +353,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse( for (unsigned int i = 1; i < nPos; i++) { if (sparse[i] != INVALID_POS_VALUE) { left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); - gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); } } } @@ -312,7 +407,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ Res[lastFilledPos] = INVALID_POS_VALUE; lastFilledPos++; } - quicksort_sparse(Res); + PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(Res); /* eliminate duplicates */ POSITION_T lastReadPos = Res[0]; int duplicateCount; @@ -464,8 +559,8 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence( for (int j = 0; j < counter; j++) { polyIndex = rndPos[j] / P; exponent = rndPos[j] % P; - gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, - ( (DIGIT) 1)); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + ( (DIGIT) 1)); } } diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h index e514c53d..f730ed41 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h @@ -15,105 +15,13 @@ #define INVALID_POS_VALUE (P) #define P_BITS (18) // log_2(p) = 17.216243783 - -static inline void gf2x_copy(DIGIT dest[], const DIGIT in[]) { - for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { - dest[i] = in[i]; - } -} - -/* returns the coefficient of the x^exponent term as the LSB of a digit */ -static inline DIGIT gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - return (poly[digitIdx] >> (DIGIT_SIZE_b - 1 - inDigitIdx)) & ((DIGIT) 1) ; -} - -/* sets the coefficient of the x^exponent term as the LSB of a digit */ -static inline void gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - - /* clear given coefficient */ - DIGIT mask = ~( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); - poly[digitIdx] = poly[digitIdx] & mask; - poly[digitIdx] = poly[digitIdx] | (( value & ((DIGIT) 1)) << (DIGIT_SIZE_b - 1 - inDigitIdx)); -} - -/* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ -static inline void gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { - unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; - unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; - unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; - - /* clear given coefficient */ - DIGIT mask = ( ((DIGIT) 1) << (DIGIT_SIZE_b - 1 - inDigitIdx)); - poly[digitIdx] = poly[digitIdx] ^ mask; -} - -/* population count for an unsigned 64-bit integer - Source: Hacker's delight, p.66 */ -static int popcount_uint64t(uint64_t x) { - x -= (x >> 1) & 0x5555555555555555; - x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); - x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; - return (int)((x * 0x0101010101010101) >> 56); -} - -/* population count for a single polynomial */ -static inline int population_count(DIGIT *poly) { - int ret = 0; - for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { - ret += popcount_uint64t(poly[i]); - } - return ret; -} - -static inline void gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { - gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); -} - -static inline int partition(POSITION_T arr[], int lo, int hi) { - POSITION_T x = arr[hi]; - POSITION_T tmp; - int i = (lo - 1); - for (int j = lo; j <= hi - 1; j++) { - if (arr[j] <= x) { - i++; - tmp = arr[i]; - arr[i] = arr[j]; - arr[j] = tmp; - } - } - tmp = arr[i + 1]; - arr[i + 1] = arr[hi]; - arr[hi] = tmp; - - return i + 1; -} - -static inline void quicksort_sparse(POSITION_T Res[]) { - int stack[DV * M]; - int hi, lo, pivot, tos = -1; - stack[++tos] = 0; - stack[++tos] = (DV * M) - 1; - while (tos >= 0 ) { - hi = stack[tos--]; - lo = stack[tos--]; - pivot = partition(Res, lo, hi); - if ( (pivot - 1) > lo) { - stack[++tos] = lo; - stack[++tos] = pivot - 1; - } - if ( (pivot + 1) < hi) { - stack[++tos] = pivot + 1; - stack[++tos] = hi; - } - } -} - +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]); +DIGIT PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); +int PQCLEAN_LEDAKEMLT52_CLEAN_population_count(DIGIT *poly); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(POSITION_T Res[]); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place(DIGIT A[]); void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); @@ -123,7 +31,6 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITIO void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); - int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); #endif diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.c b/crypto_kem/ledakemlt52/clean/niederreiter.c index 6827af13..e811f1e2 100644 --- a/crypto_kem/ledakemlt52/clean/niederreiter.c +++ b/crypto_kem/ledakemlt52/clean/niederreiter.c @@ -58,7 +58,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { - gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } @@ -86,9 +86,9 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publi PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(saux, pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, err + i * NUM_DIGITS_GF2X_ELEMENT); - gf2x_mod_add(syndrome, syndrome, saux); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(syndrome, syndrome, saux); } - gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } @@ -180,7 +180,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN err_weight = 0; for (int i = 0 ; i < N0; i++) { - err_weight += population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + err_weight += PQCLEAN_LEDAKEMLT52_CLEAN_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); From 5a4b7f24a3b9e53c1c26c775284f5ee83f586fd0 Mon Sep 17 00:00:00 2001 From: Leon Date: Wed, 12 Jun 2019 15:33:20 +0200 Subject: [PATCH 30/35] (de)serialization instead of pointer casts --- .../clean/gf2x_arith_mod_xPplusOne.c | 10 ++++ .../clean/gf2x_arith_mod_xPplusOne.h | 4 +- crypto_kem/ledakemlt12/clean/kem.c | 56 +++++++++++++++---- .../clean/gf2x_arith_mod_xPplusOne.c | 10 ++++ .../clean/gf2x_arith_mod_xPplusOne.h | 4 +- crypto_kem/ledakemlt32/clean/kem.c | 56 +++++++++++++++---- .../clean/gf2x_arith_mod_xPplusOne.c | 10 ++++ .../clean/gf2x_arith_mod_xPplusOne.h | 3 +- crypto_kem/ledakemlt52/clean/kem.c | 56 +++++++++++++++---- 9 files changed, 176 insertions(+), 33 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c index d56b18a0..30dca0f9 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c @@ -572,3 +572,13 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { } } } + +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { + size_t i, j; + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { + poly[i] = (DIGIT) 0; + for (j = 0; j < DIGIT_SIZE_B; j++) { + poly[i] |= (DIGIT) poly_bytes[i * DIGIT_SIZE_B + j] << 8 * j; + } + } +} diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h index cfae3dc6..05695f67 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h @@ -28,9 +28,11 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); -int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); +void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); + #endif diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt12/clean/kem.c index 8a2d1dc4..f8c72798 100644 --- a/crypto_kem/ledakemlt12/clean/kem.c +++ b/crypto_kem/ledakemlt12/clean/kem.c @@ -5,24 +5,52 @@ #include +static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { + size_t i; + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + +static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { + size_t i; + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + } +} + +static void pack_ct(uint8_t *sk_bytes, DIGIT *ct) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(sk_bytes, ct); +} + +static void unpack_ct(DIGIT *ct, const uint8_t *ct_bytes) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(ct, ct_bytes); +} + +static void pack_error(uint8_t *error_bytes, DIGIT *error_digits) { + size_t i; + for (i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + error_digits + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + /* Generates a keypair - pk is the public key and sk is the secret key. */ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { AES_XOF_struct niederreiter_keys_expander; + publicKeyNiederreiter_t pk_nie; randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); - PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen((publicKeyNiederreiter_t *) pk, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + + pack_pk(pk, &pk_nie); return 0; } -static void pack_error(uint8_t *error_bytes, const DIGIT *error_digits) { - size_t i; - for (i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, error_digits + i * NUM_DIGITS_GF2X_ELEMENT); - } -} - /* Encrypt - pk is the public key, ct is a key encapsulation message (ciphertext), ss is the shared secret.*/ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { @@ -30,13 +58,19 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; + publicKeyNiederreiter_t pk_nie; randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); + unpack_pk(&pk_nie, pk); + PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); pack_error(error_bytes, error_vector); HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); - PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); + PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(syndrome, &pk_nie, error_vector); + + pack_ct(ct, syndrome); return 0; } @@ -47,8 +81,10 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; - PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, (DIGIT *)ct); + unpack_ct(syndrome, ct); + PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); pack_error(decoded_error_bytes, decoded_error_vector); HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c index 39067248..0d69e00e 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c @@ -572,3 +572,13 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { } } } + +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { + size_t i, j; + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { + poly[i] = (DIGIT) 0; + for (j = 0; j < DIGIT_SIZE_B; j++) { + poly[i] |= (DIGIT) poly_bytes[i * DIGIT_SIZE_B + j] << 8 * j; + } + } +} diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h index 8f7a1f53..371ae170 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h @@ -28,9 +28,11 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); -int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); +void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); + #endif diff --git a/crypto_kem/ledakemlt32/clean/kem.c b/crypto_kem/ledakemlt32/clean/kem.c index fb67f865..23b08cbd 100644 --- a/crypto_kem/ledakemlt32/clean/kem.c +++ b/crypto_kem/ledakemlt32/clean/kem.c @@ -5,24 +5,52 @@ #include +static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { + size_t i; + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + +static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { + size_t i; + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + } +} + +static void pack_ct(uint8_t *sk_bytes, DIGIT *ct) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(sk_bytes, ct); +} + +static void unpack_ct(DIGIT *ct, const uint8_t *ct_bytes) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(ct, ct_bytes); +} + +static void pack_error(uint8_t *error_bytes, DIGIT *error_digits) { + size_t i; + for (i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + error_digits + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + /* Generates a keypair - pk is the public key and sk is the secret key. */ int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { AES_XOF_struct niederreiter_keys_expander; + publicKeyNiederreiter_t pk_nie; randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); - PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen((publicKeyNiederreiter_t *) pk, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + + pack_pk(pk, &pk_nie); return 0; } -static void pack_error(uint8_t *error_bytes, const DIGIT *error_digits) { - size_t i; - for (i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, error_digits + i * NUM_DIGITS_GF2X_ELEMENT); - } -} - /* Encrypt - pk is the public key, ct is a key encapsulation message (ciphertext), ss is the shared secret.*/ int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { @@ -30,13 +58,19 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; + publicKeyNiederreiter_t pk_nie; randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); + unpack_pk(&pk_nie, pk); + PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); pack_error(error_bytes, error_vector); HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); - PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); + PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt(syndrome, &pk_nie, error_vector); + + pack_ct(ct, syndrome); return 0; } @@ -47,8 +81,10 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; - PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, (DIGIT *)ct); + unpack_ct(syndrome, ct); + PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); pack_error(decoded_error_bytes, decoded_error_vector); HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c index d48c804d..7aac95c7 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c @@ -573,3 +573,13 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { } } } + +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { + size_t i, j; + for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { + poly[i] = (DIGIT) 0; + for (j = 0; j < DIGIT_SIZE_B; j++) { + poly[i] |= (DIGIT) poly_bytes[i * DIGIT_SIZE_B + j] << 8 * j; + } + } +} diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h index f730ed41..959c645a 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h +++ b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h @@ -28,9 +28,10 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); -int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); +void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); #endif diff --git a/crypto_kem/ledakemlt52/clean/kem.c b/crypto_kem/ledakemlt52/clean/kem.c index 40ac222b..f7869550 100644 --- a/crypto_kem/ledakemlt52/clean/kem.c +++ b/crypto_kem/ledakemlt52/clean/kem.c @@ -5,24 +5,52 @@ #include +static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { + size_t i; + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + +static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { + size_t i; + for (i = 0; i < N0 - 1; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); + } +} + +static void pack_ct(uint8_t *sk_bytes, DIGIT *ct) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(sk_bytes, ct); +} + +static void unpack_ct(DIGIT *ct, const uint8_t *ct_bytes) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(ct, ct_bytes); +} + +static void pack_error(uint8_t *error_bytes, DIGIT *error_digits) { + size_t i; + for (i = 0; i < N0; i++) { + PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + error_digits + i * NUM_DIGITS_GF2X_ELEMENT); + } +} + /* Generates a keypair - pk is the public key and sk is the secret key. */ int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { AES_XOF_struct niederreiter_keys_expander; + publicKeyNiederreiter_t pk_nie; randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); - PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen((publicKeyNiederreiter_t *) pk, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + + pack_pk(pk, &pk_nie); return 0; } -static void pack_error(uint8_t *error_bytes, const DIGIT *error_digits) { - size_t i; - for (i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, error_digits + i * NUM_DIGITS_GF2X_ELEMENT); - } -} - /* Encrypt - pk is the public key, ct is a key encapsulation message (ciphertext), ss is the shared secret.*/ int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { @@ -30,13 +58,19 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; + publicKeyNiederreiter_t pk_nie; randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); + unpack_pk(&pk_nie, pk); + PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); pack_error(error_bytes, error_vector); HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); - PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt((DIGIT *) ct, (publicKeyNiederreiter_t *) pk, error_vector); + PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt(syndrome, &pk_nie, error_vector); + + pack_ct(ct, syndrome); return 0; } @@ -47,8 +81,10 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; + DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; - PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, (DIGIT *)ct); + unpack_ct(syndrome, ct); + PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); pack_error(decoded_error_bytes, decoded_error_vector); HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); From bf0aca644ec61256322106a75d8791d3e1dfa85d Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 14 Jun 2019 14:23:58 +0200 Subject: [PATCH 31/35] avoid global state by including 2nd round threshold in secret key --- crypto_kem/ledakemlt12/META.yml | 6 ++--- crypto_kem/ledakemlt12/clean/api.h | 2 +- crypto_kem/ledakemlt12/clean/bf_decoding.c | 12 +++++----- crypto_kem/ledakemlt12/clean/bf_decoding.h | 3 ++- crypto_kem/ledakemlt12/clean/dfr_test.c | 26 +++++++-------------- crypto_kem/ledakemlt12/clean/dfr_test.h | 4 +++- crypto_kem/ledakemlt12/clean/kem.c | 2 +- crypto_kem/ledakemlt12/clean/niederreiter.c | 10 ++++---- crypto_kem/ledakemlt12/clean/niederreiter.h | 1 + crypto_kem/ledakemlt32/META.yml | 6 ++--- crypto_kem/ledakemlt32/clean/api.h | 2 +- crypto_kem/ledakemlt32/clean/bf_decoding.c | 12 +++++----- crypto_kem/ledakemlt32/clean/bf_decoding.h | 3 ++- crypto_kem/ledakemlt32/clean/dfr_test.c | 26 +++++++-------------- crypto_kem/ledakemlt32/clean/dfr_test.h | 4 +++- crypto_kem/ledakemlt32/clean/kem.c | 2 +- crypto_kem/ledakemlt32/clean/niederreiter.c | 10 ++++---- crypto_kem/ledakemlt32/clean/niederreiter.h | 1 + crypto_kem/ledakemlt52/META.yml | 6 ++--- crypto_kem/ledakemlt52/clean/api.h | 2 +- crypto_kem/ledakemlt52/clean/bf_decoding.c | 12 +++++----- crypto_kem/ledakemlt52/clean/bf_decoding.h | 3 ++- crypto_kem/ledakemlt52/clean/dfr_test.c | 26 +++++++-------------- crypto_kem/ledakemlt52/clean/dfr_test.h | 4 +++- crypto_kem/ledakemlt52/clean/kem.c | 2 +- crypto_kem/ledakemlt52/clean/niederreiter.c | 10 ++++---- crypto_kem/ledakemlt52/clean/niederreiter.h | 1 + 27 files changed, 93 insertions(+), 105 deletions(-) diff --git a/crypto_kem/ledakemlt12/META.yml b/crypto_kem/ledakemlt12/META.yml index a213611a..9eda0152 100644 --- a/crypto_kem/ledakemlt12/META.yml +++ b/crypto_kem/ledakemlt12/META.yml @@ -3,10 +3,10 @@ type: kem claimed-nist-level: 1 claimed-security: IND-CCA2 length-public-key: 6520 -length-secret-key: 25 +length-secret-key: 26 length-ciphertext: 6520 length-shared-secret: 32 -nistkat-sha256: 3bb5945e0aea26f121e1d56946760e506bdfbebb07e2fb018ce737b90b1eee2b +nistkat-sha256: c49a3f0ff5f3e7d6b41995649d7003daf7c06d9539fc28cb3b93ed02dcbe09d4 principal-submitter: Marco Baldi auxiliary-submitters: - Alessandro Barenghi @@ -15,4 +15,4 @@ auxiliary-submitters: - Paolo Santini implementations: - name: clean - version: 2.0 + version: 2.? diff --git a/crypto_kem/ledakemlt12/clean/api.h b/crypto_kem/ledakemlt12/clean/api.h index 170022d3..d37a6323 100644 --- a/crypto_kem/ledakemlt12/clean/api.h +++ b/crypto_kem/ledakemlt12/clean/api.h @@ -3,7 +3,7 @@ #include -#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_SECRETKEYBYTES 25 +#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_SECRETKEYBYTES 26 #define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_PUBLICKEYBYTES 6520 #define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_CIPHERTEXTBYTES 6520 #define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_BYTES 32 diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/clean/bf_decoding.c index 070771dd..d4dca821 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.c @@ -4,18 +4,18 @@ #include #include -unsigned int PQCLEAN_LEDAKEMLT12_CLEAN_thresholds[2] = {B0, (DV * M) / 2 + 1}; - int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], - DIGIT privateSyndrome[]) { + DIGIT privateSyndrome[], + uint8_t threshold) { uint8_t unsatParityChecks[N0 * P]; POSITION_T currQBlkPos[M], currQBitPos[M]; DIGIT currSyndrome[NUM_DIGITS_GF2X_ELEMENT]; int check; int iteration = 0; + unsigned int corrt_syndrome_based; do { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); @@ -32,7 +32,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], } /* iteration based threshold determination*/ - unsigned int corrt_syndrome_based = PQCLEAN_LEDAKEMLT12_CLEAN_thresholds[iteration]; + corrt_syndrome_based = iteration ? (unsigned int) threshold : B0; //Computation of correlation with a full Q matrix for (int i = 0; i < N0; i++) { @@ -45,7 +45,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], endQblockIdx += qBlockWeights[blockIdx][i]; int currblockoffset = blockIdx * P; for (; currQoneIdx < endQblockIdx; currQoneIdx++) { - uint32_t tmp = QtrPosOnes[i][currQoneIdx] + j; + POSITION_T tmp = QtrPosOnes[i][currQoneIdx] + j; tmp = tmp >= P ? tmp - P : tmp; currQBitPos[currQoneIdx] = tmp; currQBlkPos[currQoneIdx] = blockIdx; @@ -56,7 +56,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], if (correlation >= corrt_syndrome_based) { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { - unsigned syndromePosToFlip; + POSITION_T syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.h b/crypto_kem/ledakemlt12/clean/bf_decoding.h index 18a7dd8b..3da62547 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt12/clean/bf_decoding.h @@ -12,6 +12,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], - DIGIT privateSyndrome[]); + DIGIT privateSyndrome[], + uint8_t threshold); // B2 #endif diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt12/clean/dfr_test.c index 522a98f3..b36861fa 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.c +++ b/crypto_kem/ledakemlt12/clean/dfr_test.c @@ -6,29 +6,22 @@ #include /* Tests if the current code attains the desired DFR. If that is the case, - * computes the threshold for the second iteration of the decoder and stores - * it in the globally accessible vector */ + * computes the threshold for the second iteration of the decoder and returns this values + * (max DV * M), on failure it returns 255 >> DV * M */ -extern unsigned int PQCLEAN_LEDAKEMLT12_CLEAN_thresholds[2]; - -int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { +uint8_t PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { POSITION_T LSparse_loc[N0][DV * M]; - + POSITION_T rotated_column[DV * M]; /* Gamma matrix: an N0 x N0 block circulant matrix with block size p * gamma[a][b][c] stores the intersection of the first column of the a-th - * block of L with the c-th column of the b-th block of L */ - /* Gamma computation can be accelerated employing symmetry and QC properties */ + * block of L with the c-th column of the b-th block of L. + * Gamma computation can be accelerated employing symmetry and QC properties */ unsigned int gamma[N0][N0][P] = {{{0}}}; - unsigned int rotated_column[DV * M]; - - unsigned int firstidx, secondidx, intersectionval; - unsigned int gammaHist[N0][DV * M + 1] = {{0}}; - unsigned int maxMut[N0], maxMutMinusOne[N0]; + unsigned int firstidx, secondidx, intersectionval; unsigned int allBlockMaxSumst, allBlockMaxSumstMinusOne; - unsigned int toAdd, histIdx; /*transpose blocks of L, we need its columns */ @@ -113,8 +106,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - PQCLEAN_LEDAKEMLT12_CLEAN_thresholds[1] = allBlockMaxSumst + 1; - return 1; + return allBlockMaxSumst + 1; } - return 0; + return DFR_TEST_FAIL; } diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.h b/crypto_kem/ledakemlt12/clean/dfr_test.h index da98b2f2..cbbcbd8a 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.h +++ b/crypto_kem/ledakemlt12/clean/dfr_test.h @@ -1,6 +1,8 @@ #ifndef DFR_TEST_H #define DFR_TEST_H -int PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); +#define DFR_TEST_FAIL (255) + +uint8_t PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); #endif diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt12/clean/kem.c index f8c72798..89f47145 100644 --- a/crypto_kem/ledakemlt12/clean/kem.c +++ b/crypto_kem/ledakemlt12/clean/kem.c @@ -13,7 +13,7 @@ static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { } } -static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { +static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { size_t i; for (i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index 1cb3ea51..e78ef814 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -10,7 +10,6 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { - POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H POSITION_T QPosOnes[N0][M]; // Sparse Q, Each row contains the position of the ones of all the blocks of a row of Q as exponent+P*block_position @@ -20,7 +19,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; - int isDFRok = 0; + uint8_t threshold; // threshold for round 2 sk->rejections = (int8_t) 0; do { @@ -50,10 +49,11 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } sk->rejections = sk->rejections + 1; if (is_L_full) { - isDFRok = PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(LPosOnes); + threshold = PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(LPosOnes); } - } while (!is_L_full || !isDFRok); + } while (!is_L_full || threshold == DFR_TEST_FAIL); sk->rejections = sk->rejections - 1; + sk->threshold = threshold; memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { @@ -176,7 +176,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); decryptOk = PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, - (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); + (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome, sk->threshold); err_weight = 0; for (int i = 0 ; i < N0; i++) { diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.h b/crypto_kem/ledakemlt12/clean/niederreiter.h index 0692581b..428726cf 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.h +++ b/crypto_kem/ledakemlt12/clean/niederreiter.h @@ -10,6 +10,7 @@ typedef struct { * H and Q during decryption */ unsigned char prng_seed[TRNG_BYTE_LENGTH]; int8_t rejections; + uint8_t threshold; // for round 2 } privateKeyNiederreiter_t; typedef struct { diff --git a/crypto_kem/ledakemlt32/META.yml b/crypto_kem/ledakemlt32/META.yml index fffc304a..ed52b88d 100644 --- a/crypto_kem/ledakemlt32/META.yml +++ b/crypto_kem/ledakemlt32/META.yml @@ -3,10 +3,10 @@ type: kem claimed-nist-level: 3 claimed-security: IND-CCA2 length-public-key: 12032 -length-secret-key: 33 +length-secret-key: 34 length-ciphertext: 12032 length-shared-secret: 48 -nistkat-sha256: 5f4e10c91755d87722bc16e13c6bc5d8bcd6190589cb8924aedb8639d9f1f244 +nistkat-sha256: 455dc69ee95196fe0526c3289fe46792acd55ac380b3c66be48eb3e3e10ad4e6 principal-submitter: Marco Baldi auxiliary-submitters: - Alessandro Barenghi @@ -15,4 +15,4 @@ auxiliary-submitters: - Paolo Santini implementations: - name: clean - version: 2.0 + version: 2.? diff --git a/crypto_kem/ledakemlt32/clean/api.h b/crypto_kem/ledakemlt32/clean/api.h index 509108d8..0d54b775 100644 --- a/crypto_kem/ledakemlt32/clean/api.h +++ b/crypto_kem/ledakemlt32/clean/api.h @@ -3,7 +3,7 @@ #include -#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_SECRETKEYBYTES 33 +#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_SECRETKEYBYTES 34 #define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_PUBLICKEYBYTES 12032 #define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_CIPHERTEXTBYTES 12032 #define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_BYTES 48 diff --git a/crypto_kem/ledakemlt32/clean/bf_decoding.c b/crypto_kem/ledakemlt32/clean/bf_decoding.c index f2eb85aa..f740dbc2 100644 --- a/crypto_kem/ledakemlt32/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt32/clean/bf_decoding.c @@ -4,18 +4,18 @@ #include #include -unsigned int PQCLEAN_LEDAKEMLT32_CLEAN_thresholds[2] = {B0, (DV * M) / 2 + 1}; - int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], - DIGIT privateSyndrome[]) { + DIGIT privateSyndrome[], + uint8_t threshold) { uint8_t unsatParityChecks[N0 * P]; POSITION_T currQBlkPos[M], currQBitPos[M]; DIGIT currSyndrome[NUM_DIGITS_GF2X_ELEMENT]; int check; int iteration = 0; + unsigned int corrt_syndrome_based; do { PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); @@ -32,7 +32,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], } /* iteration based threshold determination*/ - unsigned int corrt_syndrome_based = PQCLEAN_LEDAKEMLT32_CLEAN_thresholds[iteration]; + corrt_syndrome_based = iteration ? (unsigned int) threshold : B0; //Computation of correlation with a full Q matrix for (int i = 0; i < N0; i++) { @@ -45,7 +45,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], endQblockIdx += qBlockWeights[blockIdx][i]; int currblockoffset = blockIdx * P; for (; currQoneIdx < endQblockIdx; currQoneIdx++) { - uint32_t tmp = QtrPosOnes[i][currQoneIdx] + j; + POSITION_T tmp = QtrPosOnes[i][currQoneIdx] + j; tmp = tmp >= P ? tmp - P : tmp; currQBitPos[currQoneIdx] = tmp; currQBlkPos[currQoneIdx] = blockIdx; @@ -56,7 +56,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], if (correlation >= corrt_syndrome_based) { PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { - unsigned syndromePosToFlip; + POSITION_T syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; diff --git a/crypto_kem/ledakemlt32/clean/bf_decoding.h b/crypto_kem/ledakemlt32/clean/bf_decoding.h index 672e65a3..31cc369a 100644 --- a/crypto_kem/ledakemlt32/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt32/clean/bf_decoding.h @@ -12,6 +12,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], - DIGIT privateSyndrome[]); + DIGIT privateSyndrome[], + uint8_t threshold); #endif diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.c b/crypto_kem/ledakemlt32/clean/dfr_test.c index 99571cf5..c9e6fb1c 100644 --- a/crypto_kem/ledakemlt32/clean/dfr_test.c +++ b/crypto_kem/ledakemlt32/clean/dfr_test.c @@ -6,29 +6,22 @@ #include /* Tests if the current code attains the desired DFR. If that is the case, - * computes the threshold for the second iteration of the decoder and stores - * it in the globally accessible vector */ + * computes the threshold for the second iteration of the decoder and returns this values + * (max DV * M), on failure it returns 255 >> DV * M */ -extern unsigned int PQCLEAN_LEDAKEMLT32_CLEAN_thresholds[2]; - -int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { +uint8_t PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { POSITION_T LSparse_loc[N0][DV * M]; - + POSITION_T rotated_column[DV * M]; /* Gamma matrix: an N0 x N0 block circulant matrix with block size p * gamma[a][b][c] stores the intersection of the first column of the a-th - * block of L with the c-th column of the b-th block of L */ - /* Gamma computation can be accelerated employing symmetry and QC properties */ + * block of L with the c-th column of the b-th block of L. + * Gamma computation can be accelerated employing symmetry and QC properties */ unsigned int gamma[N0][N0][P] = {{{0}}}; - unsigned int rotated_column[DV * M]; - - unsigned int firstidx, secondidx, intersectionval; - unsigned int gammaHist[N0][DV * M + 1] = {{0}}; - unsigned int maxMut[N0], maxMutMinusOne[N0]; + unsigned int firstidx, secondidx, intersectionval; unsigned int allBlockMaxSumst, allBlockMaxSumstMinusOne; - unsigned int toAdd, histIdx; /*transpose blocks of L, we need its columns */ @@ -113,8 +106,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - PQCLEAN_LEDAKEMLT32_CLEAN_thresholds[1] = allBlockMaxSumst + 1; - return 1; + return allBlockMaxSumst + 1; } - return 0; + return DFR_TEST_FAIL; } diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.h b/crypto_kem/ledakemlt32/clean/dfr_test.h index 72d2297b..1dd40cd2 100644 --- a/crypto_kem/ledakemlt32/clean/dfr_test.h +++ b/crypto_kem/ledakemlt32/clean/dfr_test.h @@ -1,6 +1,8 @@ #ifndef DFR_TEST_H #define DFR_TEST_H -int PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); +#define DFR_TEST_FAIL (255) + +uint8_t PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); #endif diff --git a/crypto_kem/ledakemlt32/clean/kem.c b/crypto_kem/ledakemlt32/clean/kem.c index 23b08cbd..647f4326 100644 --- a/crypto_kem/ledakemlt32/clean/kem.c +++ b/crypto_kem/ledakemlt32/clean/kem.c @@ -13,7 +13,7 @@ static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { } } -static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { +static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { size_t i; for (i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.c b/crypto_kem/ledakemlt32/clean/niederreiter.c index 3d524800..540151f7 100644 --- a/crypto_kem/ledakemlt32/clean/niederreiter.c +++ b/crypto_kem/ledakemlt32/clean/niederreiter.c @@ -10,7 +10,6 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { - POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H POSITION_T QPosOnes[N0][M]; // Sparse Q, Each row contains the position of the ones of all the blocks of a row of Q as exponent+P*block_position @@ -20,7 +19,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; - int isDFRok = 0; + uint8_t threshold; // threshold for round 2 sk->rejections = (int8_t) 0; do { @@ -50,10 +49,11 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } sk->rejections = sk->rejections + 1; if (is_L_full) { - isDFRok = PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(LPosOnes); + threshold = PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(LPosOnes); } - } while (!is_L_full || !isDFRok); + } while (!is_L_full || threshold == DFR_TEST_FAIL); sk->rejections = sk->rejections - 1; + sk->threshold = threshold; memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { @@ -176,7 +176,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); decryptOk = PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, - (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); + (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome, sk->threshold); err_weight = 0; for (int i = 0 ; i < N0; i++) { diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.h b/crypto_kem/ledakemlt32/clean/niederreiter.h index abea8a9a..c2e09ffa 100644 --- a/crypto_kem/ledakemlt32/clean/niederreiter.h +++ b/crypto_kem/ledakemlt32/clean/niederreiter.h @@ -10,6 +10,7 @@ typedef struct { * H and Q during decryption */ unsigned char prng_seed[TRNG_BYTE_LENGTH]; int8_t rejections; + uint8_t threshold; } privateKeyNiederreiter_t; typedef struct { diff --git a/crypto_kem/ledakemlt52/META.yml b/crypto_kem/ledakemlt52/META.yml index 5751adc4..0dcac3f0 100644 --- a/crypto_kem/ledakemlt52/META.yml +++ b/crypto_kem/ledakemlt52/META.yml @@ -3,10 +3,10 @@ type: kem claimed-nist-level: 5 claimed-security: IND-CCA2 length-public-key: 19040 -length-secret-key: 41 +length-secret-key: 42 length-ciphertext: 19040 length-shared-secret: 64 -nistkat-sha256: c53eaa3c3573a4db989671994e9501dbbc080b9c86106787f438960848c71326 +nistkat-sha256: 9cd9299d20a1c8c242730d3795683a9e87c6bcd0e691dc1fd54cd6a418266c36 principal-submitter: Marco Baldi auxiliary-submitters: - Alessandro Barenghi @@ -15,4 +15,4 @@ auxiliary-submitters: - Paolo Santini implementations: - name: clean - version: 2.0 + version: 2.? diff --git a/crypto_kem/ledakemlt52/clean/api.h b/crypto_kem/ledakemlt52/clean/api.h index 9d23bb79..2ac2f10c 100644 --- a/crypto_kem/ledakemlt52/clean/api.h +++ b/crypto_kem/ledakemlt52/clean/api.h @@ -3,7 +3,7 @@ #include -#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_SECRETKEYBYTES 41 +#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_SECRETKEYBYTES 42 #define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_PUBLICKEYBYTES 19040 #define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_CIPHERTEXTBYTES 19040 #define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_BYTES 64 diff --git a/crypto_kem/ledakemlt52/clean/bf_decoding.c b/crypto_kem/ledakemlt52/clean/bf_decoding.c index 2b90c00a..5e209aa9 100644 --- a/crypto_kem/ledakemlt52/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt52/clean/bf_decoding.c @@ -4,18 +4,18 @@ #include #include -unsigned int PQCLEAN_LEDAKEMLT52_CLEAN_thresholds[2] = {B0, (DV * M) / 2 + 1}; - int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], - DIGIT privateSyndrome[]) { + DIGIT privateSyndrome[], + uint8_t threshold) { uint8_t unsatParityChecks[N0 * P]; POSITION_T currQBlkPos[M], currQBitPos[M]; DIGIT currSyndrome[NUM_DIGITS_GF2X_ELEMENT]; int check; int iteration = 0; + unsigned int corrt_syndrome_based; do { PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); @@ -32,7 +32,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], } /* iteration based threshold determination*/ - unsigned int corrt_syndrome_based = PQCLEAN_LEDAKEMLT52_CLEAN_thresholds[iteration]; + corrt_syndrome_based = iteration ? (unsigned int) threshold : B0; //Computation of correlation with a full Q matrix for (int i = 0; i < N0; i++) { @@ -45,7 +45,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], endQblockIdx += qBlockWeights[blockIdx][i]; int currblockoffset = blockIdx * P; for (; currQoneIdx < endQblockIdx; currQoneIdx++) { - uint32_t tmp = QtrPosOnes[i][currQoneIdx] + j; + POSITION_T tmp = QtrPosOnes[i][currQoneIdx] + j; tmp = tmp >= P ? tmp - P : tmp; currQBitPos[currQoneIdx] = tmp; currQBlkPos[currQoneIdx] = blockIdx; @@ -56,7 +56,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], if (correlation >= corrt_syndrome_based) { PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { - unsigned syndromePosToFlip; + POSITION_T syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; diff --git a/crypto_kem/ledakemlt52/clean/bf_decoding.h b/crypto_kem/ledakemlt52/clean/bf_decoding.h index af006f04..e3b8bb90 100644 --- a/crypto_kem/ledakemlt52/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt52/clean/bf_decoding.h @@ -12,6 +12,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], - DIGIT privateSyndrome[]); + DIGIT privateSyndrome[], + uint8_t threshold); #endif diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.c b/crypto_kem/ledakemlt52/clean/dfr_test.c index e8de098e..d82b3388 100644 --- a/crypto_kem/ledakemlt52/clean/dfr_test.c +++ b/crypto_kem/ledakemlt52/clean/dfr_test.c @@ -6,29 +6,22 @@ #include /* Tests if the current code attains the desired DFR. If that is the case, - * computes the threshold for the second iteration of the decoder and stores - * it in the globally accessible vector */ + * computes the threshold for the second iteration of the decoder and returns this values + * (max DV * M), on failure it returns 255 >> DV * M */ -extern unsigned int PQCLEAN_LEDAKEMLT52_CLEAN_thresholds[2]; - -int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { +uint8_t PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { POSITION_T LSparse_loc[N0][DV * M]; - + POSITION_T rotated_column[DV * M]; /* Gamma matrix: an N0 x N0 block circulant matrix with block size p * gamma[a][b][c] stores the intersection of the first column of the a-th - * block of L with the c-th column of the b-th block of L */ - /* Gamma computation can be accelerated employing symmetry and QC properties */ + * block of L with the c-th column of the b-th block of L. + * Gamma computation can be accelerated employing symmetry and QC properties */ unsigned int gamma[N0][N0][P] = {{{0}}}; - unsigned int rotated_column[DV * M]; - - unsigned int firstidx, secondidx, intersectionval; - unsigned int gammaHist[N0][DV * M + 1] = {{0}}; - unsigned int maxMut[N0], maxMutMinusOne[N0]; + unsigned int firstidx, secondidx, intersectionval; unsigned int allBlockMaxSumst, allBlockMaxSumstMinusOne; - unsigned int toAdd, histIdx; /*transpose blocks of L, we need its columns */ @@ -113,8 +106,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - PQCLEAN_LEDAKEMLT52_CLEAN_thresholds[1] = allBlockMaxSumst + 1; - return 1; + return allBlockMaxSumst + 1; } - return 0; + return DFR_TEST_FAIL; } diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.h b/crypto_kem/ledakemlt52/clean/dfr_test.h index 8a74e5a3..577dd3fa 100644 --- a/crypto_kem/ledakemlt52/clean/dfr_test.h +++ b/crypto_kem/ledakemlt52/clean/dfr_test.h @@ -1,6 +1,8 @@ #ifndef DFR_TEST_H #define DFR_TEST_H -int PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); +#define DFR_TEST_FAIL (255) + +uint8_t PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); #endif diff --git a/crypto_kem/ledakemlt52/clean/kem.c b/crypto_kem/ledakemlt52/clean/kem.c index f7869550..d571c73e 100644 --- a/crypto_kem/ledakemlt52/clean/kem.c +++ b/crypto_kem/ledakemlt52/clean/kem.c @@ -13,7 +13,7 @@ static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { } } -static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { +static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { size_t i; for (i = 0; i < N0 - 1; i++) { PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.c b/crypto_kem/ledakemlt52/clean/niederreiter.c index e811f1e2..03d5d84b 100644 --- a/crypto_kem/ledakemlt52/clean/niederreiter.c +++ b/crypto_kem/ledakemlt52/clean/niederreiter.c @@ -10,7 +10,6 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { - POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H POSITION_T QPosOnes[N0][M]; // Sparse Q, Each row contains the position of the ones of all the blocks of a row of Q as exponent+P*block_position @@ -20,7 +19,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; - int isDFRok = 0; + uint8_t threshold; // threshold for round 2 sk->rejections = (int8_t) 0; do { @@ -50,10 +49,11 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } sk->rejections = sk->rejections + 1; if (is_L_full) { - isDFRok = PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(LPosOnes); + threshold = PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(LPosOnes); } - } while (!is_L_full || !isDFRok); + } while (!is_L_full || threshold == DFR_TEST_FAIL); sk->rejections = sk->rejections - 1; + sk->threshold = threshold; memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { @@ -176,7 +176,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); decryptOk = PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, - (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome); + (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome, sk->threshold); err_weight = 0; for (int i = 0 ; i < N0; i++) { diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.h b/crypto_kem/ledakemlt52/clean/niederreiter.h index eb1d69b9..e6c33ecd 100644 --- a/crypto_kem/ledakemlt52/clean/niederreiter.h +++ b/crypto_kem/ledakemlt52/clean/niederreiter.h @@ -10,6 +10,7 @@ typedef struct { * H and Q during decryption */ unsigned char prng_seed[TRNG_BYTE_LENGTH]; int8_t rejections; + uint8_t threshold; } privateKeyNiederreiter_t; typedef struct { From e353081cc2775a131d7fd95facd1b3330f15f5b7 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 14 Jun 2019 15:04:30 +0200 Subject: [PATCH 32/35] fix msvc warnings --- crypto_kem/ledakemlt12/clean/dfr_test.c | 2 +- crypto_kem/ledakemlt32/clean/dfr_test.c | 2 +- crypto_kem/ledakemlt52/clean/dfr_test.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt12/clean/dfr_test.c index b36861fa..70c4b26d 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.c +++ b/crypto_kem/ledakemlt12/clean/dfr_test.c @@ -106,7 +106,7 @@ uint8_t PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - return allBlockMaxSumst + 1; + return (uint8_t) allBlockMaxSumst + 1; } return DFR_TEST_FAIL; } diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.c b/crypto_kem/ledakemlt32/clean/dfr_test.c index c9e6fb1c..f612e2a2 100644 --- a/crypto_kem/ledakemlt32/clean/dfr_test.c +++ b/crypto_kem/ledakemlt32/clean/dfr_test.c @@ -106,7 +106,7 @@ uint8_t PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - return allBlockMaxSumst + 1; + return (uint8_t) allBlockMaxSumst + 1; } return DFR_TEST_FAIL; } diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.c b/crypto_kem/ledakemlt52/clean/dfr_test.c index d82b3388..9e97cb17 100644 --- a/crypto_kem/ledakemlt52/clean/dfr_test.c +++ b/crypto_kem/ledakemlt52/clean/dfr_test.c @@ -106,7 +106,7 @@ uint8_t PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { allBlockMaxSumstMinusOne; } if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) { - return allBlockMaxSumst + 1; + return (uint8_t) allBlockMaxSumst + 1; } return DFR_TEST_FAIL; } From db99d3ec0929d080836ba50d93ba6a9775ae5678 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 14 Jun 2019 16:07:31 +0200 Subject: [PATCH 33/35] more msvc complaints --- crypto_kem/ledakemlt12/clean/niederreiter.c | 2 +- crypto_kem/ledakemlt32/clean/niederreiter.c | 2 +- crypto_kem/ledakemlt52/clean/niederreiter.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt12/clean/niederreiter.c index e78ef814..419a00c5 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/clean/niederreiter.c @@ -19,7 +19,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; - uint8_t threshold; // threshold for round 2 + uint8_t threshold = (DV * M) / 2 + 1; // threshold for round 2 sk->rejections = (int8_t) 0; do { diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.c b/crypto_kem/ledakemlt32/clean/niederreiter.c index 540151f7..1d67281c 100644 --- a/crypto_kem/ledakemlt32/clean/niederreiter.c +++ b/crypto_kem/ledakemlt32/clean/niederreiter.c @@ -19,7 +19,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; - uint8_t threshold; // threshold for round 2 + uint8_t threshold = (DV * M) / 2 + 1; // threshold for round 2 sk->rejections = (int8_t) 0; do { diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.c b/crypto_kem/ledakemlt52/clean/niederreiter.c index 03d5d84b..82fabcc5 100644 --- a/crypto_kem/ledakemlt52/clean/niederreiter.c +++ b/crypto_kem/ledakemlt52/clean/niederreiter.c @@ -19,7 +19,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, DIGIT Ln0dense[NUM_DIGITS_GF2X_ELEMENT]; DIGIT Ln0Inv[NUM_DIGITS_GF2X_ELEMENT]; int is_L_full = 0; - uint8_t threshold; // threshold for round 2 + uint8_t threshold = (DV * M) / 2 + 1; // threshold for round 2 sk->rejections = (int8_t) 0; do { From e5b9b13160c340326fae8c3258ddb5798587800c Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 16 Jun 2019 17:01:29 +0200 Subject: [PATCH 34/35] rename impl to leaktime --- crypto_kem/ledakemlt12/META.yml | 2 +- .../clean/H_Q_matrices_generation.h | 11 ---- crypto_kem/ledakemlt12/clean/api.h | 18 ------ crypto_kem/ledakemlt12/clean/dfr_test.h | 8 --- .../clean/gf2x_arith_mod_xPplusOne.h | 38 ------------ .../H_Q_matrices_generation.c | 8 +-- .../leaktime/H_Q_matrices_generation.h | 11 ++++ .../ledakemlt12/{clean => leaktime}/LICENSE | 0 .../clean => ledakemlt12/leaktime}/Makefile | 2 +- .../leaktime}/Makefile.Microsoft_nmake | 2 +- crypto_kem/ledakemlt12/leaktime/api.h | 18 ++++++ .../{clean => leaktime}/bf_decoding.c | 10 +-- .../{clean => leaktime}/bf_decoding.h | 2 +- .../clean => ledakemlt12/leaktime}/dfr_test.c | 6 +- crypto_kem/ledakemlt12/leaktime/dfr_test.h | 8 +++ .../leaktime}/gf2x_arith.c | 8 +-- .../leaktime}/gf2x_arith.h | 8 +-- .../gf2x_arith_mod_xPplusOne.c | 54 ++++++++-------- .../leaktime/gf2x_arith_mod_xPplusOne.h | 38 ++++++++++++ .../clean => ledakemlt12/leaktime}/kem.c | 32 +++++----- .../leaktime}/niederreiter.c | 62 +++++++++---------- .../{clean => leaktime}/niederreiter.h | 6 +- .../{clean => leaktime}/qc_ldpc_parameters.h | 0 .../ledakemlt12/{clean => leaktime}/rng.c | 4 +- .../ledakemlt12/{clean => leaktime}/rng.h | 4 +- crypto_kem/ledakemlt32/META.yml | 2 +- .../clean/H_Q_matrices_generation.h | 11 ---- crypto_kem/ledakemlt32/clean/api.h | 18 ------ crypto_kem/ledakemlt32/clean/dfr_test.h | 8 --- .../clean/gf2x_arith_mod_xPplusOne.h | 38 ------------ .../leaktime}/H_Q_matrices_generation.c | 8 +-- .../leaktime/H_Q_matrices_generation.h | 11 ++++ .../ledakemlt32/{clean => leaktime}/LICENSE | 0 .../ledakemlt32/{clean => leaktime}/Makefile | 2 +- .../leaktime}/Makefile.Microsoft_nmake | 2 +- crypto_kem/ledakemlt32/leaktime/api.h | 18 ++++++ .../leaktime}/bf_decoding.c | 10 +-- .../{clean => leaktime}/bf_decoding.h | 2 +- .../clean => ledakemlt32/leaktime}/dfr_test.c | 6 +- crypto_kem/ledakemlt32/leaktime/dfr_test.h | 8 +++ .../leaktime}/gf2x_arith.c | 8 +-- .../{clean => leaktime}/gf2x_arith.h | 8 +-- .../gf2x_arith_mod_xPplusOne.c | 54 ++++++++-------- .../leaktime/gf2x_arith_mod_xPplusOne.h | 38 ++++++++++++ .../clean => ledakemlt32/leaktime}/kem.c | 32 +++++----- .../leaktime}/niederreiter.c | 62 +++++++++---------- .../leaktime}/niederreiter.h | 6 +- .../{clean => leaktime}/qc_ldpc_parameters.h | 0 .../ledakemlt32/{clean => leaktime}/rng.c | 4 +- .../clean => ledakemlt32/leaktime}/rng.h | 4 +- crypto_kem/ledakemlt52/META.yml | 2 +- .../clean/H_Q_matrices_generation.h | 11 ---- crypto_kem/ledakemlt52/clean/api.h | 18 ------ crypto_kem/ledakemlt52/clean/dfr_test.h | 8 --- .../clean/gf2x_arith_mod_xPplusOne.h | 37 ----------- .../leaktime}/H_Q_matrices_generation.c | 8 +-- .../leaktime/H_Q_matrices_generation.h | 11 ++++ .../ledakemlt52/{clean => leaktime}/LICENSE | 0 .../clean => ledakemlt52/leaktime}/Makefile | 2 +- .../leaktime}/Makefile.Microsoft_nmake | 2 +- crypto_kem/ledakemlt52/leaktime/api.h | 18 ++++++ .../leaktime}/bf_decoding.c | 10 +-- .../{clean => leaktime}/bf_decoding.h | 2 +- .../{clean => leaktime}/dfr_test.c | 6 +- crypto_kem/ledakemlt52/leaktime/dfr_test.h | 8 +++ .../leaktime}/gf2x_arith.c | 8 +-- .../leaktime}/gf2x_arith.h | 8 +-- .../gf2x_arith_mod_xPplusOne.c | 54 ++++++++-------- .../leaktime/gf2x_arith_mod_xPplusOne.h | 37 +++++++++++ .../clean => ledakemlt52/leaktime}/kem.c | 32 +++++----- .../leaktime}/niederreiter.c | 62 +++++++++---------- .../leaktime}/niederreiter.h | 6 +- .../{clean => leaktime}/qc_ldpc_parameters.h | 0 .../ledakemlt52/{clean => leaktime}/rng.c | 4 +- .../clean => ledakemlt52/leaktime}/rng.h | 4 +- 75 files changed, 539 insertions(+), 539 deletions(-) delete mode 100644 crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h delete mode 100644 crypto_kem/ledakemlt12/clean/api.h delete mode 100644 crypto_kem/ledakemlt12/clean/dfr_test.h delete mode 100644 crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h rename crypto_kem/ledakemlt12/{clean => leaktime}/H_Q_matrices_generation.c (72%) create mode 100644 crypto_kem/ledakemlt12/leaktime/H_Q_matrices_generation.h rename crypto_kem/ledakemlt12/{clean => leaktime}/LICENSE (100%) rename crypto_kem/{ledakemlt52/clean => ledakemlt12/leaktime}/Makefile (95%) rename crypto_kem/{ledakemlt32/clean => ledakemlt12/leaktime}/Makefile.Microsoft_nmake (93%) create mode 100644 crypto_kem/ledakemlt12/leaktime/api.h rename crypto_kem/ledakemlt12/{clean => leaktime}/bf_decoding.c (87%) rename crypto_kem/ledakemlt12/{clean => leaktime}/bf_decoding.h (87%) rename crypto_kem/{ledakemlt32/clean => ledakemlt12/leaktime}/dfr_test.c (94%) create mode 100644 crypto_kem/ledakemlt12/leaktime/dfr_test.h rename crypto_kem/{ledakemlt52/clean => ledakemlt12/leaktime}/gf2x_arith.c (82%) rename crypto_kem/{ledakemlt52/clean => ledakemlt12/leaktime}/gf2x_arith.h (82%) rename crypto_kem/ledakemlt12/{clean => leaktime}/gf2x_arith_mod_xPplusOne.c (87%) create mode 100644 crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.h rename crypto_kem/{ledakemlt32/clean => ledakemlt12/leaktime}/kem.c (58%) rename crypto_kem/{ledakemlt52/clean => ledakemlt12/leaktime}/niederreiter.c (65%) rename crypto_kem/ledakemlt12/{clean => leaktime}/niederreiter.h (64%) rename crypto_kem/ledakemlt12/{clean => leaktime}/qc_ldpc_parameters.h (100%) rename crypto_kem/ledakemlt12/{clean => leaktime}/rng.c (94%) rename crypto_kem/ledakemlt12/{clean => leaktime}/rng.h (66%) delete mode 100644 crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.h delete mode 100644 crypto_kem/ledakemlt32/clean/api.h delete mode 100644 crypto_kem/ledakemlt32/clean/dfr_test.h delete mode 100644 crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h rename crypto_kem/{ledakemlt52/clean => ledakemlt32/leaktime}/H_Q_matrices_generation.c (72%) create mode 100644 crypto_kem/ledakemlt32/leaktime/H_Q_matrices_generation.h rename crypto_kem/ledakemlt32/{clean => leaktime}/LICENSE (100%) rename crypto_kem/ledakemlt32/{clean => leaktime}/Makefile (95%) rename crypto_kem/{ledakemlt52/clean => ledakemlt32/leaktime}/Makefile.Microsoft_nmake (93%) create mode 100644 crypto_kem/ledakemlt32/leaktime/api.h rename crypto_kem/{ledakemlt52/clean => ledakemlt32/leaktime}/bf_decoding.c (87%) rename crypto_kem/ledakemlt32/{clean => leaktime}/bf_decoding.h (87%) rename crypto_kem/{ledakemlt12/clean => ledakemlt32/leaktime}/dfr_test.c (94%) create mode 100644 crypto_kem/ledakemlt32/leaktime/dfr_test.h rename crypto_kem/{ledakemlt12/clean => ledakemlt32/leaktime}/gf2x_arith.c (82%) rename crypto_kem/ledakemlt32/{clean => leaktime}/gf2x_arith.h (82%) rename crypto_kem/ledakemlt32/{clean => leaktime}/gf2x_arith_mod_xPplusOne.c (87%) create mode 100644 crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.h rename crypto_kem/{ledakemlt52/clean => ledakemlt32/leaktime}/kem.c (58%) rename crypto_kem/{ledakemlt12/clean => ledakemlt32/leaktime}/niederreiter.c (65%) rename crypto_kem/{ledakemlt52/clean => ledakemlt32/leaktime}/niederreiter.h (63%) rename crypto_kem/ledakemlt32/{clean => leaktime}/qc_ldpc_parameters.h (100%) rename crypto_kem/ledakemlt32/{clean => leaktime}/rng.c (94%) rename crypto_kem/{ledakemlt52/clean => ledakemlt32/leaktime}/rng.h (67%) delete mode 100644 crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.h delete mode 100644 crypto_kem/ledakemlt52/clean/api.h delete mode 100644 crypto_kem/ledakemlt52/clean/dfr_test.h delete mode 100644 crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h rename crypto_kem/{ledakemlt32/clean => ledakemlt52/leaktime}/H_Q_matrices_generation.c (72%) create mode 100644 crypto_kem/ledakemlt52/leaktime/H_Q_matrices_generation.h rename crypto_kem/ledakemlt52/{clean => leaktime}/LICENSE (100%) rename crypto_kem/{ledakemlt12/clean => ledakemlt52/leaktime}/Makefile (95%) rename crypto_kem/{ledakemlt12/clean => ledakemlt52/leaktime}/Makefile.Microsoft_nmake (93%) create mode 100644 crypto_kem/ledakemlt52/leaktime/api.h rename crypto_kem/{ledakemlt32/clean => ledakemlt52/leaktime}/bf_decoding.c (87%) rename crypto_kem/ledakemlt52/{clean => leaktime}/bf_decoding.h (87%) rename crypto_kem/ledakemlt52/{clean => leaktime}/dfr_test.c (94%) create mode 100644 crypto_kem/ledakemlt52/leaktime/dfr_test.h rename crypto_kem/{ledakemlt32/clean => ledakemlt52/leaktime}/gf2x_arith.c (82%) rename crypto_kem/{ledakemlt12/clean => ledakemlt52/leaktime}/gf2x_arith.h (82%) rename crypto_kem/ledakemlt52/{clean => leaktime}/gf2x_arith_mod_xPplusOne.c (87%) create mode 100644 crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.h rename crypto_kem/{ledakemlt12/clean => ledakemlt52/leaktime}/kem.c (58%) rename crypto_kem/{ledakemlt32/clean => ledakemlt52/leaktime}/niederreiter.c (65%) rename crypto_kem/{ledakemlt32/clean => ledakemlt52/leaktime}/niederreiter.h (63%) rename crypto_kem/ledakemlt52/{clean => leaktime}/qc_ldpc_parameters.h (100%) rename crypto_kem/ledakemlt52/{clean => leaktime}/rng.c (94%) rename crypto_kem/{ledakemlt32/clean => ledakemlt52/leaktime}/rng.h (67%) diff --git a/crypto_kem/ledakemlt12/META.yml b/crypto_kem/ledakemlt12/META.yml index 9eda0152..1ec944a7 100644 --- a/crypto_kem/ledakemlt12/META.yml +++ b/crypto_kem/ledakemlt12/META.yml @@ -14,5 +14,5 @@ auxiliary-submitters: - Gerardo Pelosi - Paolo Santini implementations: - - name: clean + - name: leaktime version: 2.? diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h deleted file mode 100644 index 8274dcd9..00000000 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef H_Q_MATRICES_GENERATION_H -#define H_Q_MATRICES_GENERATION_H - -#include "gf2x_arith.h" -#include "qc_ldpc_parameters.h" -#include "rng.h" - -void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); -void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); - -#endif diff --git a/crypto_kem/ledakemlt12/clean/api.h b/crypto_kem/ledakemlt12/clean/api.h deleted file mode 100644 index d37a6323..00000000 --- a/crypto_kem/ledakemlt12/clean/api.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef PQCLEAN_LEDAKEMLT12_CLEAN_API_H -#define PQCLEAN_LEDAKEMLT12_CLEAN_API_H - -#include - -#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_SECRETKEYBYTES 26 -#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_PUBLICKEYBYTES 6520 -#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_CIPHERTEXTBYTES 6520 -#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_BYTES 32 - -#define PQCLEAN_LEDAKEMLT12_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT12" - -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); - - -#endif diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.h b/crypto_kem/ledakemlt12/clean/dfr_test.h deleted file mode 100644 index cbbcbd8a..00000000 --- a/crypto_kem/ledakemlt12/clean/dfr_test.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef DFR_TEST_H -#define DFR_TEST_H - -#define DFR_TEST_FAIL (255) - -uint8_t PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); - -#endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h deleted file mode 100644 index 05695f67..00000000 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef GF2X_ARITH_MOD_XPLUSONE_H -#define GF2X_ARITH_MOD_XPLUSONE_H - -#include "qc_ldpc_parameters.h" - -#include "gf2x_arith.h" -#include "rng.h" - -#define NUM_BITS_GF2X_ELEMENT (P) // 52147 -#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) -#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) -#define NUM_BITS_GF2X_MODULUS (P+1) -#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) -#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) -#define INVALID_POS_VALUE (P) -#define P_BITS (16) // log_2(p) = 15.6703 - -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]); -DIGIT PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); -int PQCLEAN_LEDAKEMLT12_CLEAN_population_count(DIGIT *poly); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); -void PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(POSITION_T Res[]); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]); -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); -int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); - - -#endif diff --git a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt12/leaktime/H_Q_matrices_generation.c similarity index 72% rename from crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c rename to crypto_kem/ledakemlt12/leaktime/H_Q_matrices_generation.c index ad9ce98c..70a9727a 100644 --- a/crypto_kem/ledakemlt12/clean/H_Q_matrices_generation.c +++ b/crypto_kem/ledakemlt12/leaktime/H_Q_matrices_generation.c @@ -1,13 +1,13 @@ #include "H_Q_matrices_generation.h" #include "gf2x_arith_mod_xPplusOne.h" -void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes( +void PQCLEAN_LEDAKEMLT12_LEAKTIME_generateHPosOnes_HtrPosOnes( POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { /* Generate a random block of Htr */ - PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); + PQCLEAN_LEDAKEMLT12_LEAKTIME_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); } for (int i = 0; i < N0; i++) { /* Obtain directly the sparse representation of the block of H */ @@ -17,13 +17,13 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes( } } -void PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse( +void PQCLEAN_LEDAKEMLT12_LEAKTIME_generateQsparse( POSITION_T pos_ones[N0][M], AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { int placed_ones = 0; for (int j = 0; j < N0; j++) { - PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(&pos_ones[i][placed_ones], + PQCLEAN_LEDAKEMLT12_LEAKTIME_rand_circulant_sparse_block(&pos_ones[i][placed_ones], qBlockWeights[i][j], keys_expander); placed_ones += qBlockWeights[i][j]; diff --git a/crypto_kem/ledakemlt12/leaktime/H_Q_matrices_generation.h b/crypto_kem/ledakemlt12/leaktime/H_Q_matrices_generation.h new file mode 100644 index 00000000..0bf43055 --- /dev/null +++ b/crypto_kem/ledakemlt12/leaktime/H_Q_matrices_generation.h @@ -0,0 +1,11 @@ +#ifndef H_Q_MATRICES_GENERATION_H +#define H_Q_MATRICES_GENERATION_H + +#include "gf2x_arith.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +void PQCLEAN_LEDAKEMLT12_LEAKTIME_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); + +#endif diff --git a/crypto_kem/ledakemlt12/clean/LICENSE b/crypto_kem/ledakemlt12/leaktime/LICENSE similarity index 100% rename from crypto_kem/ledakemlt12/clean/LICENSE rename to crypto_kem/ledakemlt12/leaktime/LICENSE diff --git a/crypto_kem/ledakemlt52/clean/Makefile b/crypto_kem/ledakemlt12/leaktime/Makefile similarity index 95% rename from crypto_kem/ledakemlt52/clean/Makefile rename to crypto_kem/ledakemlt12/leaktime/Makefile index 09000ede..09326171 100644 --- a/crypto_kem/ledakemlt52/clean/Makefile +++ b/crypto_kem/ledakemlt12/leaktime/Makefile @@ -1,6 +1,6 @@ # This Makefile can be used with GNU Make or BSD Make -LIB=libledakemlt52_clean.a +LIB=libledakemlt12_leaktime.a HEADERS=api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ gf2x_arith.h H_Q_matrices_generation.h \ niederreiter.h qc_ldpc_parameters.h rng.h diff --git a/crypto_kem/ledakemlt32/clean/Makefile.Microsoft_nmake b/crypto_kem/ledakemlt12/leaktime/Makefile.Microsoft_nmake similarity index 93% rename from crypto_kem/ledakemlt32/clean/Makefile.Microsoft_nmake rename to crypto_kem/ledakemlt12/leaktime/Makefile.Microsoft_nmake index 8b169221..fbad677a 100644 --- a/crypto_kem/ledakemlt32/clean/Makefile.Microsoft_nmake +++ b/crypto_kem/ledakemlt12/leaktime/Makefile.Microsoft_nmake @@ -1,7 +1,7 @@ # This Makefile can be used with Microsoft Visual Studio's nmake using the command: # nmake /f Makefile.Microsoft_nmake -LIBRARY=libledakemlt32_clean.lib +LIBRARY=libledakemlt12_leaktime.lib OBJECTS=bf_decoding.obj dfr_test.obj gf2x_arith_mod_xPplusOne.obj gf2x_arith.obj H_Q_matrices_generation.obj kem.obj niederreiter.obj rng.obj CFLAGS=/nologo /I ..\..\..\common /W4 /WX diff --git a/crypto_kem/ledakemlt12/leaktime/api.h b/crypto_kem/ledakemlt12/leaktime/api.h new file mode 100644 index 00000000..fd2f6b8b --- /dev/null +++ b/crypto_kem/ledakemlt12/leaktime/api.h @@ -0,0 +1,18 @@ +#ifndef PQCLEAN_LEDAKEMLT12_LEAKTIME_API_H +#define PQCLEAN_LEDAKEMLT12_LEAKTIME_API_H + +#include + +#define PQCLEAN_LEDAKEMLT12_LEAKTIME_CRYPTO_SECRETKEYBYTES 26 +#define PQCLEAN_LEDAKEMLT12_LEAKTIME_CRYPTO_PUBLICKEYBYTES 6520 +#define PQCLEAN_LEDAKEMLT12_LEAKTIME_CRYPTO_CIPHERTEXTBYTES 6520 +#define PQCLEAN_LEDAKEMLT12_LEAKTIME_CRYPTO_BYTES 32 + +#define PQCLEAN_LEDAKEMLT12_LEAKTIME_CRYPTO_ALGNAME "LEDAKEMLT12" + +int PQCLEAN_LEDAKEMLT12_LEAKTIME_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); +int PQCLEAN_LEDAKEMLT12_LEAKTIME_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); +int PQCLEAN_LEDAKEMLT12_LEAKTIME_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); + + +#endif diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.c b/crypto_kem/ledakemlt12/leaktime/bf_decoding.c similarity index 87% rename from crypto_kem/ledakemlt12/clean/bf_decoding.c rename to crypto_kem/ledakemlt12/leaktime/bf_decoding.c index d4dca821..0e1123ac 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt12/leaktime/bf_decoding.c @@ -4,7 +4,7 @@ #include #include -int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], +int PQCLEAN_LEDAKEMLT12_LEAKTIME_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], DIGIT privateSyndrome[], @@ -18,13 +18,13 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], unsigned int corrt_syndrome_based; do { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_copy(currSyndrome, privateSyndrome); memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); for (int i = 0; i < N0; i++) { for (int valueIdx = 0; valueIdx < P; valueIdx++) { for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); - if (PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_get_coeff(currSyndrome, tmp)) { + if (PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_get_coeff(currSyndrome, tmp)) { unsatParityChecks[i * P + valueIdx]++; } } @@ -54,13 +54,13 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], } /* Correlation based flipping */ if (correlation >= corrt_syndrome_based) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { POSITION_T syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); } } // end for v } // end if diff --git a/crypto_kem/ledakemlt12/clean/bf_decoding.h b/crypto_kem/ledakemlt12/leaktime/bf_decoding.h similarity index 87% rename from crypto_kem/ledakemlt12/clean/bf_decoding.h rename to crypto_kem/ledakemlt12/leaktime/bf_decoding.h index 3da62547..6c62f4b3 100644 --- a/crypto_kem/ledakemlt12/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt12/leaktime/bf_decoding.h @@ -9,7 +9,7 @@ #define B0 (43) #define T_BAR (4) -int PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(DIGIT err[], +int PQCLEAN_LEDAKEMLT12_LEAKTIME_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], DIGIT privateSyndrome[], diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.c b/crypto_kem/ledakemlt12/leaktime/dfr_test.c similarity index 94% rename from crypto_kem/ledakemlt32/clean/dfr_test.c rename to crypto_kem/ledakemlt12/leaktime/dfr_test.c index f612e2a2..9ea239a9 100644 --- a/crypto_kem/ledakemlt32/clean/dfr_test.c +++ b/crypto_kem/ledakemlt12/leaktime/dfr_test.c @@ -9,7 +9,7 @@ * computes the threshold for the second iteration of the decoder and returns this values * (max DV * M), on failure it returns 255 >> DV * M */ -uint8_t PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { +uint8_t PQCLEAN_LEDAKEMLT12_LEAKTIME_DFR_test(POSITION_T LSparse[N0][DV * M]) { POSITION_T LSparse_loc[N0][DV * M]; POSITION_T rotated_column[DV * M]; @@ -31,7 +31,7 @@ uint8_t PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { LSparse_loc[i][j] = (P - LSparse[i][j]); } } - PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(LSparse_loc[i]); + PQCLEAN_LEDAKEMLT12_LEAKTIME_quicksort_sparse(LSparse_loc[i]); } for (int i = 0; i < N0; i++ ) { @@ -41,7 +41,7 @@ uint8_t PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; } - PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(rotated_column); + PQCLEAN_LEDAKEMLT12_LEAKTIME_quicksort_sparse(rotated_column); /* compute the intersection amount */ firstidx = 0, secondidx = 0; intersectionval = 0; diff --git a/crypto_kem/ledakemlt12/leaktime/dfr_test.h b/crypto_kem/ledakemlt12/leaktime/dfr_test.h new file mode 100644 index 00000000..39673efb --- /dev/null +++ b/crypto_kem/ledakemlt12/leaktime/dfr_test.h @@ -0,0 +1,8 @@ +#ifndef DFR_TEST_H +#define DFR_TEST_H + +#define DFR_TEST_FAIL (255) + +uint8_t PQCLEAN_LEDAKEMLT12_LEAKTIME_DFR_test(POSITION_T LSparse[N0][DV * M]); + +#endif diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.c b/crypto_kem/ledakemlt12/leaktime/gf2x_arith.c similarity index 82% rename from crypto_kem/ledakemlt52/clean/gf2x_arith.c rename to crypto_kem/ledakemlt12/leaktime/gf2x_arith.c index 73652552..6791ed1e 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/leaktime/gf2x_arith.c @@ -3,14 +3,14 @@ #include #include // memset(...) -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { for (int i = 0; i < nr; i++) { Res[i] = A[i] ^ B[i]; } } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -26,7 +26,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigne } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -41,7 +41,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned in[j] <<= amount; } -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mul_comb(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]) { int i, j, k; diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith.h b/crypto_kem/ledakemlt12/leaktime/gf2x_arith.h similarity index 82% rename from crypto_kem/ledakemlt52/clean/gf2x_arith.h rename to crypto_kem/ledakemlt12/leaktime/gf2x_arith.h index cdb6b303..3851fb7f 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt12/leaktime/gf2x_arith.h @@ -48,11 +48,11 @@ typedef uint64_t DIGIT; #define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) #define POSITION_T uint32_t -#define GF2X_MUL PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mul_comb +#define GF2X_MUL PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mul_comb -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); -void PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); -void PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); #endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.c similarity index 87% rename from crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c rename to crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.c index 30dca0f9..aa43cd45 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.c @@ -4,14 +4,14 @@ #include #include // memcpy(...), memset(...) -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]) { for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { dest[i] = in[i]; } } /* returns the coefficient of the x^exponent term as the LSB of a digit */ -DIGIT PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { +DIGIT PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -19,7 +19,7 @@ DIGIT PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int } /* sets the coefficient of the x^exponent term as the LSB of a digit */ -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -31,7 +31,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponen } /* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -51,7 +51,7 @@ static int popcount_uint64t(uint64_t x) { } /* population count for a single polynomial */ -int PQCLEAN_LEDAKEMLT12_CLEAN_population_count(DIGIT *poly) { +int PQCLEAN_LEDAKEMLT12_LEAKTIME_population_count(DIGIT *poly) { int ret = 0; for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { ret += popcount_uint64t(poly[i]); @@ -59,8 +59,8 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_population_count(DIGIT *poly) { return ret; } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); } static int partition(POSITION_T arr[], int lo, int hi) { @@ -82,7 +82,7 @@ static int partition(POSITION_T arr[], int lo, int hi) { return i + 1; } -void PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(POSITION_T Res[]) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_quicksort_sparse(POSITION_T Res[]) { int stack[DV * M]; int hi, lo, pivot, tos = -1; stack[++tos] = 0; @@ -175,7 +175,7 @@ static void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amo /* may shift by an arbitrary amount*/ static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); - PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); + PQCLEAN_LEDAKEMLT12_LEAKTIME_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); } /* Hackers delight, reverses a uint64_t */ @@ -193,7 +193,7 @@ static DIGIT reverse_digit(DIGIT x) { return x; } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_transpose_in_place(DIGIT A[]) { /* it keeps the lsb in the same position and * inverts the sequence of the remaining bits */ @@ -214,7 +214,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); if (slack_bits_amount) { - PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); + PQCLEAN_LEDAKEMLT12_LEAKTIME_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); } A[NUM_DIGITS_GF2X_ELEMENT - 1] = (A[NUM_DIGITS_GF2X_ELEMENT - 1] & (~mask)) | a00; } @@ -265,7 +265,7 @@ static void gf2x_swap(const int length, DIGIT f[], DIGIT s[]) { * (Chapter 11 -- Algorithm 11.44 -- pag 223) * */ -int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ +int PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ int i; int delta = 0; @@ -299,8 +299,8 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { delta += 1; } else { if ( (s[0] & mask) != 0) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(v, v, u); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add(v, v, u); } left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); if ( delta == 0 ) { @@ -322,7 +322,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { return (delta == 0); } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT]; GF2X_MUL(2 * NUM_DIGITS_GF2X_ELEMENT, aux, @@ -334,7 +334,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const /*PRE: the representation of the sparse coefficients is sorted in increasing order of the coefficients themselves */ -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse( +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_dense_to_sparse( DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos) { @@ -352,7 +352,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse( for (unsigned int i = 1; i < nPos; i++) { if (sparse[i] != INVALID_POS_VALUE) { left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); } } } @@ -361,7 +361,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse( } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { POSITION_T t; int i = 0, j; @@ -383,7 +383,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITIO } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]) { @@ -406,7 +406,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ Res[lastFilledPos] = INVALID_POS_VALUE; lastFilledPos++; } - PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(Res); + PQCLEAN_LEDAKEMLT12_LEAKTIME_quicksort_sparse(Res); /* eliminate duplicates */ POSITION_T lastReadPos = Res[0]; int duplicateCount; @@ -433,7 +433,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ /* the implementation is safe even in case A or B alias with the result */ /* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse( +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add_sparse( int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]) { @@ -492,7 +492,7 @@ static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct uint32_t mask = ( (uint32_t)1 << logn) - 1; do { - PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); + PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); /* obtain an endianness independent representation of the generated random bytes into an unsigned integer */ rnd_value = ((uint32_t)rnd_char_buffer[3] << 24) + @@ -507,7 +507,7 @@ static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct /* Obtains fresh randomness and seed-expands it until all the required positions * for the '1's in the circulant block are obtained */ -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, +void PQCLEAN_LEDAKEMLT12_LEAKTIME_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx) { @@ -532,7 +532,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, } /* Returns random weight-t circulant block */ -void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( +void PQCLEAN_LEDAKEMLT12_LEAKTIME_rand_circulant_blocks_sequence( DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], AES_XOF_struct *seed_expander_ctx) { @@ -558,13 +558,13 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence( for (int j = 0; j < counter; j++) { polyIndex = rndPos[j] / P; exponent = rndPos[j] % P; - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, ( (DIGIT) 1)); } } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { for (j = 0; j < DIGIT_SIZE_B; j++) { @@ -573,7 +573,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { } } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { poly[i] = (DIGIT) 0; diff --git a/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.h new file mode 100644 index 00000000..8fa0d40a --- /dev/null +++ b/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.h @@ -0,0 +1,38 @@ +#ifndef GF2X_ARITH_MOD_XPLUSONE_H +#define GF2X_ARITH_MOD_XPLUSONE_H + +#include "qc_ldpc_parameters.h" + +#include "gf2x_arith.h" +#include "rng.h" + +#define NUM_BITS_GF2X_ELEMENT (P) // 52147 +#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) +#define NUM_BITS_GF2X_MODULUS (P+1) +#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) +#define INVALID_POS_VALUE (P) +#define P_BITS (16) // log_2(p) = 15.6703 + +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]); +DIGIT PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); +int PQCLEAN_LEDAKEMLT12_LEAKTIME_population_count(DIGIT *poly); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_quicksort_sparse(POSITION_T Res[]); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_transpose_in_place(DIGIT A[]); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +int PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); + + +#endif diff --git a/crypto_kem/ledakemlt32/clean/kem.c b/crypto_kem/ledakemlt12/leaktime/kem.c similarity index 58% rename from crypto_kem/ledakemlt32/clean/kem.c rename to crypto_kem/ledakemlt12/leaktime/kem.c index 647f4326..5a06bf0b 100644 --- a/crypto_kem/ledakemlt32/clean/kem.c +++ b/crypto_kem/ledakemlt12/leaktime/kem.c @@ -8,43 +8,43 @@ static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { size_t i; for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, - pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); } } static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { size_t i; for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); } } static void pack_ct(uint8_t *sk_bytes, DIGIT *ct) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(sk_bytes, ct); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_tobytes(sk_bytes, ct); } static void unpack_ct(DIGIT *ct, const uint8_t *ct_bytes) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(ct, ct_bytes); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_frombytes(ct, ct_bytes); } static void pack_error(uint8_t *error_bytes, DIGIT *error_digits) { size_t i; for (i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, - error_digits + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + error_digits + i * NUM_DIGITS_GF2X_ELEMENT); } } /* Generates a keypair - pk is the public key and sk is the secret key. */ -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { +int PQCLEAN_LEDAKEMLT12_LEAKTIME_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { AES_XOF_struct niederreiter_keys_expander; publicKeyNiederreiter_t pk_nie; randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); - PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); - PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); + PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); pack_pk(pk, &pk_nie); @@ -53,7 +53,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned cha /* Encrypt - pk is the public key, ct is a key encapsulation message (ciphertext), ss is the shared secret.*/ -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { +int PQCLEAN_LEDAKEMLT12_LEAKTIME_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { AES_XOF_struct niederreiter_encap_key_expander; unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; @@ -64,11 +64,11 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); unpack_pk(&pk_nie, pk); - PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); - PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); + PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); + PQCLEAN_LEDAKEMLT12_LEAKTIME_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); pack_error(error_bytes, error_vector); HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); - PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt(syndrome, &pk_nie, error_vector); + PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_encrypt(syndrome, &pk_nie, error_vector); pack_ct(ct, syndrome); @@ -78,13 +78,13 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s /* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private key, ss is the shared secret */ -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { +int PQCLEAN_LEDAKEMLT12_LEAKTIME_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; unpack_ct(syndrome, ct); - PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); + PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); pack_error(decoded_error_bytes, decoded_error_vector); HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.c b/crypto_kem/ledakemlt12/leaktime/niederreiter.c similarity index 65% rename from crypto_kem/ledakemlt52/clean/niederreiter.c rename to crypto_kem/ledakemlt12/leaktime/niederreiter.c index 82fabcc5..273f2c41 100644 --- a/crypto_kem/ledakemlt52/clean/niederreiter.c +++ b/crypto_kem/ledakemlt12/leaktime/niederreiter.c @@ -8,7 +8,7 @@ #include -void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H @@ -23,8 +23,8 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, sk->rejections = (int8_t) 0; do { - PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); - PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse(QPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT12_LEAKTIME_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT12_LEAKTIME_generateQsparse(QPosOnes, keys_expander); for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { LPosOnes[i][j] = INVALID_POS_VALUE; @@ -34,10 +34,10 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxPosOnes, DV, HPosOnes[i], qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], DV * M, LPosOnes[colQ], DV * M, auxPosOnes); processedQOnes[i] += qBlockWeights[i][colQ]; @@ -49,7 +49,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } sk->rejections = sk->rejections + 1; if (is_L_full) { - threshold = PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(LPosOnes); + threshold = PQCLEAN_LEDAKEMLT12_LEAKTIME_DFR_test(LPosOnes); } } while (!is_L_full || threshold == DFR_TEST_FAIL); sk->rejections = sk->rejections - 1; @@ -58,41 +58,41 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } memset(Ln0Inv, 0x00, sizeof(Ln0Inv)); - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_inverse(Ln0Inv, Ln0dense); for (int i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, Ln0Inv, LPosOnes[i], DV * M); } for (int i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); } } -void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { +void PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { int i; DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(saux, - pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, - err + i * NUM_DIGITS_GF2X_ELEMENT); - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(syndrome, syndrome, saux); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul(saux, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + err + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add(syndrome, syndrome, saux); } - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } -int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { +int PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { AES_XOF_struct niederreiter_decrypt_expander; POSITION_T HPosOnes[N0][DV]; POSITION_T HtrPosOnes[N0][DV]; @@ -110,11 +110,11 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN int currQoneIdx, endQblockIdx; int decryptOk, err_weight; - PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); + PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); do { - PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); - PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT12_LEAKTIME_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT12_LEAKTIME_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { LPosOnes[i][j] = INVALID_POS_VALUE; @@ -124,10 +124,10 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxPosOnes, DV, HPosOnes[i], qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], DV * M, LPosOnes[colQ], DV * M, auxPosOnes); processedQOnes[i] += qBlockWeights[i][colQ]; @@ -156,31 +156,31 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(DV * M, auxSparse, + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxSparse, DV, HPosOnes[i], qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1]]); - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(DV * M, Ln0trSparse, + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add_sparse(DV * M, Ln0trSparse, DV * M, Ln0trSparse, DV * M, auxSparse); } - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); + PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); /* prepare mockup error vector in case a decoding failure occurs */ memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(&niederreiter_decrypt_expander, - ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), - TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander(&niederreiter_decrypt_expander, + ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), + TRNG_BYTE_LENGTH); memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - decryptOk = PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, + decryptOk = PQCLEAN_LEDAKEMLT12_LEAKTIME_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome, sk->threshold); err_weight = 0; for (int i = 0 ; i < N0; i++) { - err_weight += PQCLEAN_LEDAKEMLT52_CLEAN_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + err_weight += PQCLEAN_LEDAKEMLT12_LEAKTIME_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.h b/crypto_kem/ledakemlt12/leaktime/niederreiter.h similarity index 64% rename from crypto_kem/ledakemlt12/clean/niederreiter.h rename to crypto_kem/ledakemlt12/leaktime/niederreiter.h index 428726cf..17da4c29 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.h +++ b/crypto_kem/ledakemlt12/leaktime/niederreiter.h @@ -21,9 +21,9 @@ typedef struct { // with P coefficients. } publicKeyNiederreiter_t; -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); -int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); +int PQCLEAN_LEDAKEMLT12_LEAKTIME_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); #endif diff --git a/crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h b/crypto_kem/ledakemlt12/leaktime/qc_ldpc_parameters.h similarity index 100% rename from crypto_kem/ledakemlt12/clean/qc_ldpc_parameters.h rename to crypto_kem/ledakemlt12/leaktime/qc_ldpc_parameters.h diff --git a/crypto_kem/ledakemlt12/clean/rng.c b/crypto_kem/ledakemlt12/leaktime/rng.c similarity index 94% rename from crypto_kem/ledakemlt12/clean/rng.c rename to crypto_kem/ledakemlt12/leaktime/rng.c index 913b9e67..6d7604e6 100644 --- a/crypto_kem/ledakemlt12/clean/rng.c +++ b/crypto_kem/ledakemlt12/leaktime/rng.c @@ -37,7 +37,7 @@ static void seedexpander_init(AES_XOF_struct *ctx, memset(ctx->buffer, 0x00, 16); } -void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, +void PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy /* TRNG_BYTE_LENGTH wide buffer */) { @@ -61,7 +61,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, x - returns the XOF data xlen - number of bytes to return */ -int PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { +int PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { size_t offset; aes256ctx ctx256; diff --git a/crypto_kem/ledakemlt12/clean/rng.h b/crypto_kem/ledakemlt12/leaktime/rng.h similarity index 66% rename from crypto_kem/ledakemlt12/clean/rng.h rename to crypto_kem/ledakemlt12/leaktime/rng.h index 204e8f7b..09a81a57 100644 --- a/crypto_kem/ledakemlt12/clean/rng.h +++ b/crypto_kem/ledakemlt12/leaktime/rng.h @@ -18,7 +18,7 @@ typedef struct { unsigned char ctr[16]; } AES_XOF_struct; -int PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); -void PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); +int PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); +void PQCLEAN_LEDAKEMLT12_LEAKTIME_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); #endif diff --git a/crypto_kem/ledakemlt32/META.yml b/crypto_kem/ledakemlt32/META.yml index ed52b88d..c6c040a5 100644 --- a/crypto_kem/ledakemlt32/META.yml +++ b/crypto_kem/ledakemlt32/META.yml @@ -14,5 +14,5 @@ auxiliary-submitters: - Gerardo Pelosi - Paolo Santini implementations: - - name: clean + - name: leaktime version: 2.? diff --git a/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.h deleted file mode 100644 index 68d4703d..00000000 --- a/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef H_Q_MATRICES_GENERATION_H -#define H_Q_MATRICES_GENERATION_H - -#include "gf2x_arith.h" -#include "qc_ldpc_parameters.h" -#include "rng.h" - -void PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); -void PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); - -#endif diff --git a/crypto_kem/ledakemlt32/clean/api.h b/crypto_kem/ledakemlt32/clean/api.h deleted file mode 100644 index 0d54b775..00000000 --- a/crypto_kem/ledakemlt32/clean/api.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef PQCLEAN_LEDAKEMLT32_CLEAN_API_H -#define PQCLEAN_LEDAKEMLT32_CLEAN_API_H - -#include - -#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_SECRETKEYBYTES 34 -#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_PUBLICKEYBYTES 12032 -#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_CIPHERTEXTBYTES 12032 -#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_BYTES 48 - -#define PQCLEAN_LEDAKEMLT32_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT32" - -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); -int PQCLEAN_LEDAKEMLT32_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); - - -#endif diff --git a/crypto_kem/ledakemlt32/clean/dfr_test.h b/crypto_kem/ledakemlt32/clean/dfr_test.h deleted file mode 100644 index 1dd40cd2..00000000 --- a/crypto_kem/ledakemlt32/clean/dfr_test.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef DFR_TEST_H -#define DFR_TEST_H - -#define DFR_TEST_FAIL (255) - -uint8_t PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); - -#endif diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h deleted file mode 100644 index 371ae170..00000000 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef GF2X_ARITH_MOD_XPLUSONE_H -#define GF2X_ARITH_MOD_XPLUSONE_H - -#include "qc_ldpc_parameters.h" - -#include "gf2x_arith.h" -#include "rng.h" - -#define NUM_BITS_GF2X_ELEMENT (P) // 96221 -#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) -#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) -#define NUM_BITS_GF2X_MODULUS (P+1) -#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) -#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) -#define INVALID_POS_VALUE (P) -#define P_BITS (17) // log_2(p) = 16.55406417 - -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]); -DIGIT PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); -int PQCLEAN_LEDAKEMLT32_CLEAN_population_count(DIGIT *poly); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); -void PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(POSITION_T Res[]); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(DIGIT A[]); -void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); -void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); -int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); - - -#endif diff --git a/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt32/leaktime/H_Q_matrices_generation.c similarity index 72% rename from crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.c rename to crypto_kem/ledakemlt32/leaktime/H_Q_matrices_generation.c index ff832abf..37a455b1 100644 --- a/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.c +++ b/crypto_kem/ledakemlt32/leaktime/H_Q_matrices_generation.c @@ -1,13 +1,13 @@ #include "H_Q_matrices_generation.h" #include "gf2x_arith_mod_xPplusOne.h" -void PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes( +void PQCLEAN_LEDAKEMLT32_LEAKTIME_generateHPosOnes_HtrPosOnes( POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { /* Generate a random block of Htr */ - PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); + PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); } for (int i = 0; i < N0; i++) { /* Obtain directly the sparse representation of the block of H */ @@ -17,13 +17,13 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes( } } -void PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse( +void PQCLEAN_LEDAKEMLT32_LEAKTIME_generateQsparse( POSITION_T pos_ones[N0][M], AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { int placed_ones = 0; for (int j = 0; j < N0; j++) { - PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(&pos_ones[i][placed_ones], + PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_circulant_sparse_block(&pos_ones[i][placed_ones], qBlockWeights[i][j], keys_expander); placed_ones += qBlockWeights[i][j]; diff --git a/crypto_kem/ledakemlt32/leaktime/H_Q_matrices_generation.h b/crypto_kem/ledakemlt32/leaktime/H_Q_matrices_generation.h new file mode 100644 index 00000000..23bd44f0 --- /dev/null +++ b/crypto_kem/ledakemlt32/leaktime/H_Q_matrices_generation.h @@ -0,0 +1,11 @@ +#ifndef H_Q_MATRICES_GENERATION_H +#define H_Q_MATRICES_GENERATION_H + +#include "gf2x_arith.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +void PQCLEAN_LEDAKEMLT32_LEAKTIME_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); + +#endif diff --git a/crypto_kem/ledakemlt32/clean/LICENSE b/crypto_kem/ledakemlt32/leaktime/LICENSE similarity index 100% rename from crypto_kem/ledakemlt32/clean/LICENSE rename to crypto_kem/ledakemlt32/leaktime/LICENSE diff --git a/crypto_kem/ledakemlt32/clean/Makefile b/crypto_kem/ledakemlt32/leaktime/Makefile similarity index 95% rename from crypto_kem/ledakemlt32/clean/Makefile rename to crypto_kem/ledakemlt32/leaktime/Makefile index 4d17eb30..d8d4e9cb 100644 --- a/crypto_kem/ledakemlt32/clean/Makefile +++ b/crypto_kem/ledakemlt32/leaktime/Makefile @@ -1,6 +1,6 @@ # This Makefile can be used with GNU Make or BSD Make -LIB=libledakemlt32_clean.a +LIB=libledakemlt32_leaktime.a HEADERS=api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ gf2x_arith.h H_Q_matrices_generation.h \ niederreiter.h qc_ldpc_parameters.h rng.h diff --git a/crypto_kem/ledakemlt52/clean/Makefile.Microsoft_nmake b/crypto_kem/ledakemlt32/leaktime/Makefile.Microsoft_nmake similarity index 93% rename from crypto_kem/ledakemlt52/clean/Makefile.Microsoft_nmake rename to crypto_kem/ledakemlt32/leaktime/Makefile.Microsoft_nmake index 3cbd9d76..f895e6dc 100644 --- a/crypto_kem/ledakemlt52/clean/Makefile.Microsoft_nmake +++ b/crypto_kem/ledakemlt32/leaktime/Makefile.Microsoft_nmake @@ -1,7 +1,7 @@ # This Makefile can be used with Microsoft Visual Studio's nmake using the command: # nmake /f Makefile.Microsoft_nmake -LIBRARY=libledakemlt52_clean.lib +LIBRARY=libledakemlt32_leaktime.lib OBJECTS=bf_decoding.obj dfr_test.obj gf2x_arith_mod_xPplusOne.obj gf2x_arith.obj H_Q_matrices_generation.obj kem.obj niederreiter.obj rng.obj CFLAGS=/nologo /I ..\..\..\common /W4 /WX diff --git a/crypto_kem/ledakemlt32/leaktime/api.h b/crypto_kem/ledakemlt32/leaktime/api.h new file mode 100644 index 00000000..0a048aa3 --- /dev/null +++ b/crypto_kem/ledakemlt32/leaktime/api.h @@ -0,0 +1,18 @@ +#ifndef PQCLEAN_LEDAKEMLT32_LEAKTIME_API_H +#define PQCLEAN_LEDAKEMLT32_LEAKTIME_API_H + +#include + +#define PQCLEAN_LEDAKEMLT32_LEAKTIME_CRYPTO_SECRETKEYBYTES 34 +#define PQCLEAN_LEDAKEMLT32_LEAKTIME_CRYPTO_PUBLICKEYBYTES 12032 +#define PQCLEAN_LEDAKEMLT32_LEAKTIME_CRYPTO_CIPHERTEXTBYTES 12032 +#define PQCLEAN_LEDAKEMLT32_LEAKTIME_CRYPTO_BYTES 48 + +#define PQCLEAN_LEDAKEMLT32_LEAKTIME_CRYPTO_ALGNAME "LEDAKEMLT32" + +int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); +int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); +int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); + + +#endif diff --git a/crypto_kem/ledakemlt52/clean/bf_decoding.c b/crypto_kem/ledakemlt32/leaktime/bf_decoding.c similarity index 87% rename from crypto_kem/ledakemlt52/clean/bf_decoding.c rename to crypto_kem/ledakemlt32/leaktime/bf_decoding.c index 5e209aa9..764904f1 100644 --- a/crypto_kem/ledakemlt52/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt32/leaktime/bf_decoding.c @@ -4,7 +4,7 @@ #include #include -int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], +int PQCLEAN_LEDAKEMLT32_LEAKTIME_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], DIGIT privateSyndrome[], @@ -18,13 +18,13 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], unsigned int corrt_syndrome_based; do { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_copy(currSyndrome, privateSyndrome); memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); for (int i = 0; i < N0; i++) { for (int valueIdx = 0; valueIdx < P; valueIdx++) { for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); - if (PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_get_coeff(currSyndrome, tmp)) { + if (PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_get_coeff(currSyndrome, tmp)) { unsatParityChecks[i * P + valueIdx]++; } } @@ -54,13 +54,13 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], } /* Correlation based flipping */ if (correlation >= corrt_syndrome_based) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { POSITION_T syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); } } // end for v } // end if diff --git a/crypto_kem/ledakemlt32/clean/bf_decoding.h b/crypto_kem/ledakemlt32/leaktime/bf_decoding.h similarity index 87% rename from crypto_kem/ledakemlt32/clean/bf_decoding.h rename to crypto_kem/ledakemlt32/leaktime/bf_decoding.h index 31cc369a..a55b0300 100644 --- a/crypto_kem/ledakemlt32/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt32/leaktime/bf_decoding.h @@ -9,7 +9,7 @@ #define B0 (64) #define T_BAR (5) -int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], +int PQCLEAN_LEDAKEMLT32_LEAKTIME_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], DIGIT privateSyndrome[], diff --git a/crypto_kem/ledakemlt12/clean/dfr_test.c b/crypto_kem/ledakemlt32/leaktime/dfr_test.c similarity index 94% rename from crypto_kem/ledakemlt12/clean/dfr_test.c rename to crypto_kem/ledakemlt32/leaktime/dfr_test.c index 70c4b26d..73a52f6f 100644 --- a/crypto_kem/ledakemlt12/clean/dfr_test.c +++ b/crypto_kem/ledakemlt32/leaktime/dfr_test.c @@ -9,7 +9,7 @@ * computes the threshold for the second iteration of the decoder and returns this values * (max DV * M), on failure it returns 255 >> DV * M */ -uint8_t PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { +uint8_t PQCLEAN_LEDAKEMLT32_LEAKTIME_DFR_test(POSITION_T LSparse[N0][DV * M]) { POSITION_T LSparse_loc[N0][DV * M]; POSITION_T rotated_column[DV * M]; @@ -31,7 +31,7 @@ uint8_t PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { LSparse_loc[i][j] = (P - LSparse[i][j]); } } - PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(LSparse_loc[i]); + PQCLEAN_LEDAKEMLT32_LEAKTIME_quicksort_sparse(LSparse_loc[i]); } for (int i = 0; i < N0; i++ ) { @@ -41,7 +41,7 @@ uint8_t PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; } - PQCLEAN_LEDAKEMLT12_CLEAN_quicksort_sparse(rotated_column); + PQCLEAN_LEDAKEMLT32_LEAKTIME_quicksort_sparse(rotated_column); /* compute the intersection amount */ firstidx = 0, secondidx = 0; intersectionval = 0; diff --git a/crypto_kem/ledakemlt32/leaktime/dfr_test.h b/crypto_kem/ledakemlt32/leaktime/dfr_test.h new file mode 100644 index 00000000..d1484ac9 --- /dev/null +++ b/crypto_kem/ledakemlt32/leaktime/dfr_test.h @@ -0,0 +1,8 @@ +#ifndef DFR_TEST_H +#define DFR_TEST_H + +#define DFR_TEST_FAIL (255) + +uint8_t PQCLEAN_LEDAKEMLT32_LEAKTIME_DFR_test(POSITION_T LSparse[N0][DV * M]); + +#endif diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.c b/crypto_kem/ledakemlt32/leaktime/gf2x_arith.c similarity index 82% rename from crypto_kem/ledakemlt12/clean/gf2x_arith.c rename to crypto_kem/ledakemlt32/leaktime/gf2x_arith.c index 980c22de..2ce4869f 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt32/leaktime/gf2x_arith.c @@ -3,14 +3,14 @@ #include #include // memset(...) -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { for (int i = 0; i < nr; i++) { Res[i] = A[i] ^ B[i]; } } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -26,7 +26,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigne } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -41,7 +41,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned in[j] <<= amount; } -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mul_comb(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]) { int i, j, k; diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.h b/crypto_kem/ledakemlt32/leaktime/gf2x_arith.h similarity index 82% rename from crypto_kem/ledakemlt32/clean/gf2x_arith.h rename to crypto_kem/ledakemlt32/leaktime/gf2x_arith.h index 720d6fde..ff127b2b 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt32/leaktime/gf2x_arith.h @@ -48,11 +48,11 @@ typedef uint64_t DIGIT; #define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) #define POSITION_T uint32_t -#define GF2X_MUL PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mul_comb +#define GF2X_MUL PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mul_comb -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); -void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); -void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); #endif diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.c similarity index 87% rename from crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c rename to crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.c index 0d69e00e..75109644 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.c @@ -4,14 +4,14 @@ #include #include // memcpy(...), memset(...) -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]) { for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { dest[i] = in[i]; } } /* returns the coefficient of the x^exponent term as the LSB of a digit */ -DIGIT PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { +DIGIT PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -19,7 +19,7 @@ DIGIT PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int } /* sets the coefficient of the x^exponent term as the LSB of a digit */ -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -31,7 +31,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponen } /* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -51,7 +51,7 @@ static int popcount_uint64t(uint64_t x) { } /* population count for a single polynomial */ -int PQCLEAN_LEDAKEMLT32_CLEAN_population_count(DIGIT *poly) { +int PQCLEAN_LEDAKEMLT32_LEAKTIME_population_count(DIGIT *poly) { int ret = 0; for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { ret += popcount_uint64t(poly[i]); @@ -59,8 +59,8 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_population_count(DIGIT *poly) { return ret; } -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); } static int partition(POSITION_T arr[], int lo, int hi) { @@ -82,7 +82,7 @@ static int partition(POSITION_T arr[], int lo, int hi) { return i + 1; } -void PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(POSITION_T Res[]) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_quicksort_sparse(POSITION_T Res[]) { int stack[DV * M]; int hi, lo, pivot, tos = -1; stack[++tos] = 0; @@ -175,7 +175,7 @@ static void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amo /* may shift by an arbitrary amount*/ static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); - PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); + PQCLEAN_LEDAKEMLT32_LEAKTIME_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); } /* Hackers delight, reverses a uint64_t */ @@ -193,7 +193,7 @@ static DIGIT reverse_digit(DIGIT x) { return x; } -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_transpose_in_place(DIGIT A[]) { /* it keeps the lsb in the same position and * inverts the sequence of the remaining bits */ @@ -214,7 +214,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { // A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // no middle digit if (slack_bits_amount) { - PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); + PQCLEAN_LEDAKEMLT32_LEAKTIME_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); } A[NUM_DIGITS_GF2X_ELEMENT - 1] = (A[NUM_DIGITS_GF2X_ELEMENT - 1] & (~mask)) | a00; } @@ -265,7 +265,7 @@ static void gf2x_swap(const int length, DIGIT f[], DIGIT s[]) { * (Chapter 11 -- Algorithm 11.44 -- pag 223) * */ -int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ +int PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ int i; int delta = 0; @@ -299,8 +299,8 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { delta += 1; } else { if ( (s[0] & mask) != 0) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(v, v, u); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add(v, v, u); } left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); if ( delta == 0 ) { @@ -322,7 +322,7 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { return (delta == 0); } -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT]; GF2X_MUL(2 * NUM_DIGITS_GF2X_ELEMENT, aux, @@ -334,7 +334,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const /*PRE: the representation of the sparse coefficients is sorted in increasing order of the coefficients themselves */ -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse( +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_dense_to_sparse( DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos) { @@ -352,7 +352,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse( for (unsigned int i = 1; i < nPos; i++) { if (sparse[i] != INVALID_POS_VALUE) { left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); } } } @@ -361,7 +361,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse( } -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { POSITION_T t; int i = 0, j; @@ -383,7 +383,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITIO } -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]) { @@ -406,7 +406,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ Res[lastFilledPos] = INVALID_POS_VALUE; lastFilledPos++; } - PQCLEAN_LEDAKEMLT32_CLEAN_quicksort_sparse(Res); + PQCLEAN_LEDAKEMLT32_LEAKTIME_quicksort_sparse(Res); /* eliminate duplicates */ POSITION_T lastReadPos = Res[0]; int duplicateCount; @@ -433,7 +433,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ /* the implementation is safe even in case A or B alias with the result */ /* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse( +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add_sparse( int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]) { @@ -492,7 +492,7 @@ static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct uint32_t mask = ( (uint32_t)1 << logn) - 1; do { - PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); + PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); /* obtain an endianness independent representation of the generated random bytes into an unsigned integer */ rnd_value = ((uint32_t)rnd_char_buffer[3] << 24) + @@ -507,7 +507,7 @@ static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct /* Obtains fresh randomness and seed-expands it until all the required positions * for the '1's in the circulant block are obtained */ -void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, +void PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx) { @@ -532,7 +532,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, } /* Returns random weight-t circulant block */ -void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence( +void PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_circulant_blocks_sequence( DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], AES_XOF_struct *seed_expander_ctx) { @@ -558,13 +558,13 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_blocks_sequence( for (int j = 0; j < counter; j++) { polyIndex = rndPos[j] / P; exponent = rndPos[j] % P; - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, ( (DIGIT) 1)); } } -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { for (j = 0; j < DIGIT_SIZE_B; j++) { @@ -573,7 +573,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { } } -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { poly[i] = (DIGIT) 0; diff --git a/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.h new file mode 100644 index 00000000..a43f1225 --- /dev/null +++ b/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.h @@ -0,0 +1,38 @@ +#ifndef GF2X_ARITH_MOD_XPLUSONE_H +#define GF2X_ARITH_MOD_XPLUSONE_H + +#include "qc_ldpc_parameters.h" + +#include "gf2x_arith.h" +#include "rng.h" + +#define NUM_BITS_GF2X_ELEMENT (P) // 96221 +#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) +#define NUM_BITS_GF2X_MODULUS (P+1) +#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) +#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) +#define INVALID_POS_VALUE (P) +#define P_BITS (17) // log_2(p) = 16.55406417 + +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]); +DIGIT PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); +int PQCLEAN_LEDAKEMLT32_LEAKTIME_population_count(DIGIT *poly); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_quicksort_sparse(POSITION_T Res[]); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_transpose_in_place(DIGIT A[]); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +int PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); + + +#endif diff --git a/crypto_kem/ledakemlt52/clean/kem.c b/crypto_kem/ledakemlt32/leaktime/kem.c similarity index 58% rename from crypto_kem/ledakemlt52/clean/kem.c rename to crypto_kem/ledakemlt32/leaktime/kem.c index d571c73e..d7e09732 100644 --- a/crypto_kem/ledakemlt52/clean/kem.c +++ b/crypto_kem/ledakemlt32/leaktime/kem.c @@ -8,43 +8,43 @@ static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { size_t i; for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, - pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); } } static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { size_t i; for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); } } static void pack_ct(uint8_t *sk_bytes, DIGIT *ct) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(sk_bytes, ct); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(sk_bytes, ct); } static void unpack_ct(DIGIT *ct, const uint8_t *ct_bytes) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(ct, ct_bytes); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_frombytes(ct, ct_bytes); } static void pack_error(uint8_t *error_bytes, DIGIT *error_digits) { size_t i; for (i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, - error_digits + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + error_digits + i * NUM_DIGITS_GF2X_ELEMENT); } } /* Generates a keypair - pk is the public key and sk is the secret key. */ -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { +int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { AES_XOF_struct niederreiter_keys_expander; publicKeyNiederreiter_t pk_nie; randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); - PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); - PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); + PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); pack_pk(pk, &pk_nie); @@ -53,7 +53,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned cha /* Encrypt - pk is the public key, ct is a key encapsulation message (ciphertext), ss is the shared secret.*/ -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { +int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { AES_XOF_struct niederreiter_encap_key_expander; unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; @@ -64,11 +64,11 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); unpack_pk(&pk_nie, pk); - PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); - PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); + PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); + PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); pack_error(error_bytes, error_vector); HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); - PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt(syndrome, &pk_nie, error_vector); + PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_encrypt(syndrome, &pk_nie, error_vector); pack_ct(ct, syndrome); @@ -78,13 +78,13 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s /* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private key, ss is the shared secret */ -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { +int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; unpack_ct(syndrome, ct); - PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); + PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); pack_error(decoded_error_bytes, decoded_error_vector); HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); diff --git a/crypto_kem/ledakemlt12/clean/niederreiter.c b/crypto_kem/ledakemlt32/leaktime/niederreiter.c similarity index 65% rename from crypto_kem/ledakemlt12/clean/niederreiter.c rename to crypto_kem/ledakemlt32/leaktime/niederreiter.c index 419a00c5..66bc3f80 100644 --- a/crypto_kem/ledakemlt12/clean/niederreiter.c +++ b/crypto_kem/ledakemlt32/leaktime/niederreiter.c @@ -8,7 +8,7 @@ #include -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H @@ -23,8 +23,8 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, sk->rejections = (int8_t) 0; do { - PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); - PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT32_LEAKTIME_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT32_LEAKTIME_generateQsparse(QPosOnes, keys_expander); for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { LPosOnes[i][j] = INVALID_POS_VALUE; @@ -34,10 +34,10 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxPosOnes, DV, HPosOnes[i], qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], DV * M, LPosOnes[colQ], DV * M, auxPosOnes); processedQOnes[i] += qBlockWeights[i][colQ]; @@ -49,7 +49,7 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } sk->rejections = sk->rejections + 1; if (is_L_full) { - threshold = PQCLEAN_LEDAKEMLT12_CLEAN_DFR_test(LPosOnes); + threshold = PQCLEAN_LEDAKEMLT32_LEAKTIME_DFR_test(LPosOnes); } } while (!is_L_full || threshold == DFR_TEST_FAIL); sk->rejections = sk->rejections - 1; @@ -58,41 +58,41 @@ void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } memset(Ln0Inv, 0x00, sizeof(Ln0Inv)); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_inverse(Ln0Inv, Ln0dense); for (int i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, Ln0Inv, LPosOnes[i], DV * M); } for (int i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); } } -void PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { +void PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { int i; DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul(saux, - pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, - err + i * NUM_DIGITS_GF2X_ELEMENT); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(syndrome, syndrome, saux); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul(saux, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + err + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add(syndrome, syndrome, saux); } - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } -int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { +int PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { AES_XOF_struct niederreiter_decrypt_expander; POSITION_T HPosOnes[N0][DV]; POSITION_T HtrPosOnes[N0][DV]; @@ -110,11 +110,11 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN int currQoneIdx, endQblockIdx; int decryptOk, err_weight; - PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); + PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); do { - PQCLEAN_LEDAKEMLT12_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); - PQCLEAN_LEDAKEMLT12_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT32_LEAKTIME_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT32_LEAKTIME_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { LPosOnes[i][j] = INVALID_POS_VALUE; @@ -124,10 +124,10 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxPosOnes, DV, HPosOnes[i], qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], DV * M, LPosOnes[colQ], DV * M, auxPosOnes); processedQOnes[i] += qBlockWeights[i][colQ]; @@ -156,31 +156,31 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_sparse(DV * M, auxSparse, + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxSparse, DV, HPosOnes[i], qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1]]); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_add_sparse(DV * M, Ln0trSparse, + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add_sparse(DV * M, Ln0trSparse, DV * M, Ln0trSparse, DV * M, auxSparse); } - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); + PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); /* prepare mockup error vector in case a decoding failure occurs */ memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander(&niederreiter_decrypt_expander, - ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), - TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander(&niederreiter_decrypt_expander, + ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), + TRNG_BYTE_LENGTH); memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - decryptOk = PQCLEAN_LEDAKEMLT12_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, + decryptOk = PQCLEAN_LEDAKEMLT32_LEAKTIME_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome, sk->threshold); err_weight = 0; for (int i = 0 ; i < N0; i++) { - err_weight += PQCLEAN_LEDAKEMLT12_CLEAN_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + err_weight += PQCLEAN_LEDAKEMLT32_LEAKTIME_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); diff --git a/crypto_kem/ledakemlt52/clean/niederreiter.h b/crypto_kem/ledakemlt32/leaktime/niederreiter.h similarity index 63% rename from crypto_kem/ledakemlt52/clean/niederreiter.h rename to crypto_kem/ledakemlt32/leaktime/niederreiter.h index e6c33ecd..41048034 100644 --- a/crypto_kem/ledakemlt52/clean/niederreiter.h +++ b/crypto_kem/ledakemlt32/leaktime/niederreiter.h @@ -21,9 +21,9 @@ typedef struct { // with P coefficients. } publicKeyNiederreiter_t; -void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); -void PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); -int PQCLEAN_LEDAKEMLT52_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); +int PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); #endif diff --git a/crypto_kem/ledakemlt32/clean/qc_ldpc_parameters.h b/crypto_kem/ledakemlt32/leaktime/qc_ldpc_parameters.h similarity index 100% rename from crypto_kem/ledakemlt32/clean/qc_ldpc_parameters.h rename to crypto_kem/ledakemlt32/leaktime/qc_ldpc_parameters.h diff --git a/crypto_kem/ledakemlt32/clean/rng.c b/crypto_kem/ledakemlt32/leaktime/rng.c similarity index 94% rename from crypto_kem/ledakemlt32/clean/rng.c rename to crypto_kem/ledakemlt32/leaktime/rng.c index b0757cee..4e18fa57 100644 --- a/crypto_kem/ledakemlt32/clean/rng.c +++ b/crypto_kem/ledakemlt32/leaktime/rng.c @@ -37,7 +37,7 @@ static void seedexpander_init(AES_XOF_struct *ctx, memset(ctx->buffer, 0x00, 16); } -void PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, +void PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy /* TRNG_BYTE_LENGTH wide buffer */) { @@ -61,7 +61,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, x - returns the XOF data xlen - number of bytes to return */ -int PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { +int PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { size_t offset; aes256ctx ctx256; diff --git a/crypto_kem/ledakemlt52/clean/rng.h b/crypto_kem/ledakemlt32/leaktime/rng.h similarity index 67% rename from crypto_kem/ledakemlt52/clean/rng.h rename to crypto_kem/ledakemlt32/leaktime/rng.h index 5bfc8703..dbff0366 100644 --- a/crypto_kem/ledakemlt52/clean/rng.h +++ b/crypto_kem/ledakemlt32/leaktime/rng.h @@ -18,7 +18,7 @@ typedef struct { unsigned char ctr[16]; } AES_XOF_struct; -int PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); -void PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); +int PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); +void PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); #endif diff --git a/crypto_kem/ledakemlt52/META.yml b/crypto_kem/ledakemlt52/META.yml index 0dcac3f0..5aef2b59 100644 --- a/crypto_kem/ledakemlt52/META.yml +++ b/crypto_kem/ledakemlt52/META.yml @@ -14,5 +14,5 @@ auxiliary-submitters: - Gerardo Pelosi - Paolo Santini implementations: - - name: clean + - name: leaktime version: 2.? diff --git a/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.h b/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.h deleted file mode 100644 index 18a8cd5c..00000000 --- a/crypto_kem/ledakemlt52/clean/H_Q_matrices_generation.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef H_Q_MATRICES_GENERATION_H -#define H_Q_MATRICES_GENERATION_H - -#include "gf2x_arith.h" -#include "qc_ldpc_parameters.h" -#include "rng.h" - -void PQCLEAN_LEDAKEMLT52_CLEAN_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); -void PQCLEAN_LEDAKEMLT52_CLEAN_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); - -#endif diff --git a/crypto_kem/ledakemlt52/clean/api.h b/crypto_kem/ledakemlt52/clean/api.h deleted file mode 100644 index 2ac2f10c..00000000 --- a/crypto_kem/ledakemlt52/clean/api.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef PQCLEAN_LEDAKEMLT52_CLEAN_API_H -#define PQCLEAN_LEDAKEMLT52_CLEAN_API_H - -#include - -#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_SECRETKEYBYTES 42 -#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_PUBLICKEYBYTES 19040 -#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_CIPHERTEXTBYTES 19040 -#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_BYTES 64 - -#define PQCLEAN_LEDAKEMLT52_CLEAN_CRYPTO_ALGNAME "LEDAKEMLT52" - -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); -int PQCLEAN_LEDAKEMLT52_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); - - -#endif diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.h b/crypto_kem/ledakemlt52/clean/dfr_test.h deleted file mode 100644 index 577dd3fa..00000000 --- a/crypto_kem/ledakemlt52/clean/dfr_test.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef DFR_TEST_H -#define DFR_TEST_H - -#define DFR_TEST_FAIL (255) - -uint8_t PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]); - -#endif diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h deleted file mode 100644 index 959c645a..00000000 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef GF2X_ARITH_MOD_XPLUSONE_H -#define GF2X_ARITH_MOD_XPLUSONE_H - -#include "qc_ldpc_parameters.h" - -#include "gf2x_arith.h" -#include "rng.h" - -#define NUM_BITS_GF2X_ELEMENT (P) // 152267 -#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) // 2380 -#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) -#define NUM_BITS_GF2X_MODULUS (P+1) -#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) // 2380 -#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) -#define INVALID_POS_VALUE (P) -#define P_BITS (18) // log_2(p) = 17.216243783 - -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]); -DIGIT PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); -int PQCLEAN_LEDAKEMLT52_CLEAN_population_count(DIGIT *poly); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); -void PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(POSITION_T Res[]); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place(DIGIT A[]); -void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); -void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); -int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); - -#endif diff --git a/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.c b/crypto_kem/ledakemlt52/leaktime/H_Q_matrices_generation.c similarity index 72% rename from crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.c rename to crypto_kem/ledakemlt52/leaktime/H_Q_matrices_generation.c index eca47755..8c1cdf0f 100644 --- a/crypto_kem/ledakemlt32/clean/H_Q_matrices_generation.c +++ b/crypto_kem/ledakemlt52/leaktime/H_Q_matrices_generation.c @@ -1,13 +1,13 @@ #include "H_Q_matrices_generation.h" #include "gf2x_arith_mod_xPplusOne.h" -void PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes( +void PQCLEAN_LEDAKEMLT52_LEAKTIME_generateHPosOnes_HtrPosOnes( POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { /* Generate a random block of Htr */ - PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); + PQCLEAN_LEDAKEMLT52_LEAKTIME_rand_circulant_sparse_block(&HtrPosOnes[i][0], DV, keys_expander); } for (int i = 0; i < N0; i++) { /* Obtain directly the sparse representation of the block of H */ @@ -17,13 +17,13 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes( } } -void PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse( +void PQCLEAN_LEDAKEMLT52_LEAKTIME_generateQsparse( POSITION_T pos_ones[N0][M], AES_XOF_struct *keys_expander) { for (int i = 0; i < N0; i++) { int placed_ones = 0; for (int j = 0; j < N0; j++) { - PQCLEAN_LEDAKEMLT32_CLEAN_rand_circulant_sparse_block(&pos_ones[i][placed_ones], + PQCLEAN_LEDAKEMLT52_LEAKTIME_rand_circulant_sparse_block(&pos_ones[i][placed_ones], qBlockWeights[i][j], keys_expander); placed_ones += qBlockWeights[i][j]; diff --git a/crypto_kem/ledakemlt52/leaktime/H_Q_matrices_generation.h b/crypto_kem/ledakemlt52/leaktime/H_Q_matrices_generation.h new file mode 100644 index 00000000..168fa743 --- /dev/null +++ b/crypto_kem/ledakemlt52/leaktime/H_Q_matrices_generation.h @@ -0,0 +1,11 @@ +#ifndef H_Q_MATRICES_GENERATION_H +#define H_Q_MATRICES_GENERATION_H + +#include "gf2x_arith.h" +#include "qc_ldpc_parameters.h" +#include "rng.h" + +void PQCLEAN_LEDAKEMLT52_LEAKTIME_generateHPosOnes_HtrPosOnes(POSITION_T HPosOnes[N0][DV], POSITION_T HtrPosOnes[N0][DV], AES_XOF_struct *niederreiter_keys_expander); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_generateQsparse(POSITION_T pos_ones[N0][M], AES_XOF_struct *niederreiter_keys_expander); + +#endif diff --git a/crypto_kem/ledakemlt52/clean/LICENSE b/crypto_kem/ledakemlt52/leaktime/LICENSE similarity index 100% rename from crypto_kem/ledakemlt52/clean/LICENSE rename to crypto_kem/ledakemlt52/leaktime/LICENSE diff --git a/crypto_kem/ledakemlt12/clean/Makefile b/crypto_kem/ledakemlt52/leaktime/Makefile similarity index 95% rename from crypto_kem/ledakemlt12/clean/Makefile rename to crypto_kem/ledakemlt52/leaktime/Makefile index 6d6fa541..0c3b52fa 100644 --- a/crypto_kem/ledakemlt12/clean/Makefile +++ b/crypto_kem/ledakemlt52/leaktime/Makefile @@ -1,6 +1,6 @@ # This Makefile can be used with GNU Make or BSD Make -LIB=libledakemlt12_clean.a +LIB=libledakemlt52_leaktime.a HEADERS=api.h bf_decoding.h dfr_test.h gf2x_arith_mod_xPplusOne.h \ gf2x_arith.h H_Q_matrices_generation.h \ niederreiter.h qc_ldpc_parameters.h rng.h diff --git a/crypto_kem/ledakemlt12/clean/Makefile.Microsoft_nmake b/crypto_kem/ledakemlt52/leaktime/Makefile.Microsoft_nmake similarity index 93% rename from crypto_kem/ledakemlt12/clean/Makefile.Microsoft_nmake rename to crypto_kem/ledakemlt52/leaktime/Makefile.Microsoft_nmake index e4bfc760..0bb72b8b 100644 --- a/crypto_kem/ledakemlt12/clean/Makefile.Microsoft_nmake +++ b/crypto_kem/ledakemlt52/leaktime/Makefile.Microsoft_nmake @@ -1,7 +1,7 @@ # This Makefile can be used with Microsoft Visual Studio's nmake using the command: # nmake /f Makefile.Microsoft_nmake -LIBRARY=libledakemlt12_clean.lib +LIBRARY=libledakemlt52_leaktime.lib OBJECTS=bf_decoding.obj dfr_test.obj gf2x_arith_mod_xPplusOne.obj gf2x_arith.obj H_Q_matrices_generation.obj kem.obj niederreiter.obj rng.obj CFLAGS=/nologo /I ..\..\..\common /W4 /WX diff --git a/crypto_kem/ledakemlt52/leaktime/api.h b/crypto_kem/ledakemlt52/leaktime/api.h new file mode 100644 index 00000000..11c84a4d --- /dev/null +++ b/crypto_kem/ledakemlt52/leaktime/api.h @@ -0,0 +1,18 @@ +#ifndef PQCLEAN_LEDAKEMLT52_LEAKTIME_API_H +#define PQCLEAN_LEDAKEMLT52_LEAKTIME_API_H + +#include + +#define PQCLEAN_LEDAKEMLT52_LEAKTIME_CRYPTO_SECRETKEYBYTES 42 +#define PQCLEAN_LEDAKEMLT52_LEAKTIME_CRYPTO_PUBLICKEYBYTES 19040 +#define PQCLEAN_LEDAKEMLT52_LEAKTIME_CRYPTO_CIPHERTEXTBYTES 19040 +#define PQCLEAN_LEDAKEMLT52_LEAKTIME_CRYPTO_BYTES 64 + +#define PQCLEAN_LEDAKEMLT52_LEAKTIME_CRYPTO_ALGNAME "LEDAKEMLT52" + +int PQCLEAN_LEDAKEMLT52_LEAKTIME_crypto_kem_keypair(uint8_t *pk, uint8_t *sk); +int PQCLEAN_LEDAKEMLT52_LEAKTIME_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk); +int PQCLEAN_LEDAKEMLT52_LEAKTIME_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk); + + +#endif diff --git a/crypto_kem/ledakemlt32/clean/bf_decoding.c b/crypto_kem/ledakemlt52/leaktime/bf_decoding.c similarity index 87% rename from crypto_kem/ledakemlt32/clean/bf_decoding.c rename to crypto_kem/ledakemlt52/leaktime/bf_decoding.c index f740dbc2..476bf162 100644 --- a/crypto_kem/ledakemlt32/clean/bf_decoding.c +++ b/crypto_kem/ledakemlt52/leaktime/bf_decoding.c @@ -4,7 +4,7 @@ #include #include -int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], +int PQCLEAN_LEDAKEMLT52_LEAKTIME_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], DIGIT privateSyndrome[], @@ -18,13 +18,13 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], unsigned int corrt_syndrome_based; do { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_copy(currSyndrome, privateSyndrome); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_copy(currSyndrome, privateSyndrome); memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t)); for (int i = 0; i < N0; i++) { for (int valueIdx = 0; valueIdx < P; valueIdx++) { for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx); - if (PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_get_coeff(currSyndrome, tmp)) { + if (PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_get_coeff(currSyndrome, tmp)) { unsatParityChecks[i * P + valueIdx]++; } } @@ -54,13 +54,13 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[], } /* Correlation based flipping */ if (correlation >= corrt_syndrome_based) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j); for (int v = 0; v < M; v++) { POSITION_T syndromePosToFlip; for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) { syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] ); syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip; - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip); } } // end for v } // end if diff --git a/crypto_kem/ledakemlt52/clean/bf_decoding.h b/crypto_kem/ledakemlt52/leaktime/bf_decoding.h similarity index 87% rename from crypto_kem/ledakemlt52/clean/bf_decoding.h rename to crypto_kem/ledakemlt52/leaktime/bf_decoding.h index e3b8bb90..0b391c48 100644 --- a/crypto_kem/ledakemlt52/clean/bf_decoding.h +++ b/crypto_kem/ledakemlt52/leaktime/bf_decoding.h @@ -9,7 +9,7 @@ #define B0 (88) #define T_BAR (6) -int PQCLEAN_LEDAKEMLT52_CLEAN_bf_decoding(DIGIT err[], +int PQCLEAN_LEDAKEMLT52_LEAKTIME_bf_decoding(DIGIT err[], const POSITION_T HtrPosOnes[N0][DV], const POSITION_T QtrPosOnes[N0][M], DIGIT privateSyndrome[], diff --git a/crypto_kem/ledakemlt52/clean/dfr_test.c b/crypto_kem/ledakemlt52/leaktime/dfr_test.c similarity index 94% rename from crypto_kem/ledakemlt52/clean/dfr_test.c rename to crypto_kem/ledakemlt52/leaktime/dfr_test.c index 9e97cb17..b93f68aa 100644 --- a/crypto_kem/ledakemlt52/clean/dfr_test.c +++ b/crypto_kem/ledakemlt52/leaktime/dfr_test.c @@ -9,7 +9,7 @@ * computes the threshold for the second iteration of the decoder and returns this values * (max DV * M), on failure it returns 255 >> DV * M */ -uint8_t PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { +uint8_t PQCLEAN_LEDAKEMLT52_LEAKTIME_DFR_test(POSITION_T LSparse[N0][DV * M]) { POSITION_T LSparse_loc[N0][DV * M]; POSITION_T rotated_column[DV * M]; @@ -31,7 +31,7 @@ uint8_t PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { LSparse_loc[i][j] = (P - LSparse[i][j]); } } - PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(LSparse_loc[i]); + PQCLEAN_LEDAKEMLT52_LEAKTIME_quicksort_sparse(LSparse_loc[i]); } for (int i = 0; i < N0; i++ ) { @@ -41,7 +41,7 @@ uint8_t PQCLEAN_LEDAKEMLT52_CLEAN_DFR_test(POSITION_T LSparse[N0][DV * M]) { for (int idxToRotate = 0; idxToRotate < (DV * M); idxToRotate++) { rotated_column[idxToRotate] = (LSparse_loc[j][idxToRotate] + k) % P; } - PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(rotated_column); + PQCLEAN_LEDAKEMLT52_LEAKTIME_quicksort_sparse(rotated_column); /* compute the intersection amount */ firstidx = 0, secondidx = 0; intersectionval = 0; diff --git a/crypto_kem/ledakemlt52/leaktime/dfr_test.h b/crypto_kem/ledakemlt52/leaktime/dfr_test.h new file mode 100644 index 00000000..92c49c75 --- /dev/null +++ b/crypto_kem/ledakemlt52/leaktime/dfr_test.h @@ -0,0 +1,8 @@ +#ifndef DFR_TEST_H +#define DFR_TEST_H + +#define DFR_TEST_FAIL (255) + +uint8_t PQCLEAN_LEDAKEMLT52_LEAKTIME_DFR_test(POSITION_T LSparse[N0][DV * M]); + +#endif diff --git a/crypto_kem/ledakemlt32/clean/gf2x_arith.c b/crypto_kem/ledakemlt52/leaktime/gf2x_arith.c similarity index 82% rename from crypto_kem/ledakemlt32/clean/gf2x_arith.c rename to crypto_kem/ledakemlt52/leaktime/gf2x_arith.c index 713e9cc5..b1b9df44 100644 --- a/crypto_kem/ledakemlt32/clean/gf2x_arith.c +++ b/crypto_kem/ledakemlt52/leaktime/gf2x_arith.c @@ -3,14 +3,14 @@ #include #include // memset(...) -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { for (int i = 0; i < nr; i++) { Res[i] = A[i] ^ B[i]; } } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -26,7 +26,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigne } /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ -void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; @@ -41,7 +41,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned in[j] <<= amount; } -void PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mul_comb(int nr, DIGIT Res[], +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mul_comb(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]) { int i, j, k; diff --git a/crypto_kem/ledakemlt12/clean/gf2x_arith.h b/crypto_kem/ledakemlt52/leaktime/gf2x_arith.h similarity index 82% rename from crypto_kem/ledakemlt12/clean/gf2x_arith.h rename to crypto_kem/ledakemlt52/leaktime/gf2x_arith.h index 8f890ab1..c4635592 100644 --- a/crypto_kem/ledakemlt12/clean/gf2x_arith.h +++ b/crypto_kem/ledakemlt52/leaktime/gf2x_arith.h @@ -48,11 +48,11 @@ typedef uint64_t DIGIT; #define DIGIT_SIZE_b (DIGIT_SIZE_B << 3) #define POSITION_T uint32_t -#define GF2X_MUL PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_mul_comb +#define GF2X_MUL PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mul_comb -void PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); -void PQCLEAN_LEDAKEMLT12_CLEAN_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); -void PQCLEAN_LEDAKEMLT12_CLEAN_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount); void GF2X_MUL(int nr, DIGIT Res[], int na, const DIGIT A[], int nb, const DIGIT B[]); #endif diff --git a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.c similarity index 87% rename from crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c rename to crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.c index 7aac95c7..ef7d0e57 100644 --- a/crypto_kem/ledakemlt52/clean/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.c @@ -5,14 +5,14 @@ #include // memcpy(...), memset(...) -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_copy(DIGIT dest[], const DIGIT in[]) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]) { for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { dest[i] = in[i]; } } /* returns the coefficient of the x^exponent term as the LSB of a digit */ -DIGIT PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { +DIGIT PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -20,7 +20,7 @@ DIGIT PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_get_coeff(const DIGIT poly[], unsigned int } /* sets the coefficient of the x^exponent term as the LSB of a digit */ -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -32,7 +32,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff(DIGIT poly[], unsigned int exponen } /* toggles (flips) the coefficient of the x^exponent term as the LSB of a digit */ -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent) { unsigned int straightIdx = (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_b - 1) - exponent; unsigned int digitIdx = straightIdx / DIGIT_SIZE_b; unsigned int inDigitIdx = straightIdx % DIGIT_SIZE_b; @@ -52,7 +52,7 @@ static int popcount_uint64t(uint64_t x) { } /* population count for a single polynomial */ -int PQCLEAN_LEDAKEMLT52_CLEAN_population_count(DIGIT *poly) { +int PQCLEAN_LEDAKEMLT52_LEAKTIME_population_count(DIGIT *poly) { int ret = 0; for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { ret += popcount_uint64t(poly[i]); @@ -60,8 +60,8 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_population_count(DIGIT *poly) { return ret; } -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_add(Res, A, B, NUM_DIGITS_GF2X_ELEMENT); } static int partition(POSITION_T arr[], int lo, int hi) { @@ -83,7 +83,7 @@ static int partition(POSITION_T arr[], int lo, int hi) { return i + 1; } -void PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(POSITION_T Res[]) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_quicksort_sparse(POSITION_T Res[]) { int stack[DV * M]; int hi, lo, pivot, tos = -1; stack[++tos] = 0; @@ -176,7 +176,7 @@ static void left_DIGIT_shift_n(unsigned int length, DIGIT in[], unsigned int amo /* may shift by an arbitrary amount*/ static void left_bit_shift_wide_n(const int length, DIGIT in[], unsigned int amount) { left_DIGIT_shift_n(length, in, amount / DIGIT_SIZE_b); - PQCLEAN_LEDAKEMLT52_CLEAN_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); + PQCLEAN_LEDAKEMLT52_LEAKTIME_left_bit_shift_n(length, in, amount % DIGIT_SIZE_b); } /* Hackers delight, reverses a uint64_t */ @@ -194,7 +194,7 @@ static DIGIT reverse_digit(DIGIT x) { return x; } -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_transpose_in_place(DIGIT A[]) { /* it keeps the lsb in the same position and * inverts the sequence of the remaining bits */ @@ -215,7 +215,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place(DIGIT A[]) { // A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // no middle digit if (slack_bits_amount) { - PQCLEAN_LEDAKEMLT52_CLEAN_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); + PQCLEAN_LEDAKEMLT52_LEAKTIME_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); } A[NUM_DIGITS_GF2X_ELEMENT - 1] = (A[NUM_DIGITS_GF2X_ELEMENT - 1] & (~mask)) | a00; } @@ -266,7 +266,7 @@ static void gf2x_swap(const int length, DIGIT f[], DIGIT s[]) { * (Chapter 11 -- Algorithm 11.44 -- pag 223) * */ -int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ +int PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { /* in^{-1} mod x^P-1 */ int i; int delta = 0; @@ -300,8 +300,8 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { delta += 1; } else { if ( (s[0] & mask) != 0) { - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add(v, v, u); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_add(s, s, f, NUM_DIGITS_GF2X_MODULUS); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add(v, v, u); } left_bit_shift(NUM_DIGITS_GF2X_MODULUS, s); if ( delta == 0 ) { @@ -323,7 +323,7 @@ int PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]) { return (delta == 0); } -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]) { DIGIT aux[2 * NUM_DIGITS_GF2X_ELEMENT]; GF2X_MUL(2 * NUM_DIGITS_GF2X_ELEMENT, aux, @@ -335,7 +335,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const /*PRE: the representation of the sparse coefficients is sorted in increasing order of the coefficients themselves */ -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse( +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_dense_to_sparse( DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos) { @@ -353,7 +353,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse( for (unsigned int i = 1; i < nPos; i++) { if (sparse[i] != INVALID_POS_VALUE) { left_bit_shift_wide_n(2 * NUM_DIGITS_GF2X_ELEMENT, aux, (sparse[i] - sparse[i - 1]) ); - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_add(resDouble, aux, resDouble, 2 * NUM_DIGITS_GF2X_ELEMENT); } } } @@ -362,7 +362,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_dense_to_sparse( } -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]) { POSITION_T t; int i = 0, j; @@ -384,7 +384,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_transpose_in_place_sparse(int sizeA, POSITIO } -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]) { @@ -407,7 +407,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ Res[lastFilledPos] = INVALID_POS_VALUE; lastFilledPos++; } - PQCLEAN_LEDAKEMLT52_CLEAN_quicksort_sparse(Res); + PQCLEAN_LEDAKEMLT52_LEAKTIME_quicksort_sparse(Res); /* eliminate duplicates */ POSITION_T lastReadPos = Res[0]; int duplicateCount; @@ -434,7 +434,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[ /* the implementation is safe even in case A or B alias with the result */ /* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_mod_add_sparse( +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add_sparse( int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]) { @@ -493,7 +493,7 @@ static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct uint32_t mask = ( (uint32_t)1 << logn) - 1; do { - PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); + PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander(seed_expander_ctx, rnd_char_buffer, required_rnd_bytes); /* obtain an endianness independent representation of the generated random bytes into an unsigned integer */ rnd_value = ((uint32_t)rnd_char_buffer[3] << 24) + @@ -508,7 +508,7 @@ static uint32_t rand_range(const unsigned int n, const int logn, AES_XOF_struct /* Obtains fresh randomness and seed-expands it until all the required positions * for the '1's in the circulant block are obtained */ -void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, +void PQCLEAN_LEDAKEMLT52_LEAKTIME_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx) { @@ -533,7 +533,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_sparse_block(POSITION_T *pos_ones, } /* Returns random weight-t circulant block */ -void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence( +void PQCLEAN_LEDAKEMLT52_LEAKTIME_rand_circulant_blocks_sequence( DIGIT sequence[N0 * NUM_DIGITS_GF2X_ELEMENT], AES_XOF_struct *seed_expander_ctx) { @@ -559,13 +559,13 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_rand_circulant_blocks_sequence( for (int j = 0; j < counter; j++) { polyIndex = rndPos[j] / P; exponent = rndPos[j] % P; - PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_set_coeff( sequence + NUM_DIGITS_GF2X_ELEMENT * polyIndex, exponent, ( (DIGIT) 1)); } } -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { for (j = 0; j < DIGIT_SIZE_B; j++) { @@ -574,7 +574,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly) { } } -void PQCLEAN_LEDAKEMLT52_CLEAN_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes) { size_t i, j; for (i = 0; i < NUM_DIGITS_GF2X_ELEMENT; i++) { poly[i] = (DIGIT) 0; diff --git a/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.h b/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.h new file mode 100644 index 00000000..2286c55f --- /dev/null +++ b/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.h @@ -0,0 +1,37 @@ +#ifndef GF2X_ARITH_MOD_XPLUSONE_H +#define GF2X_ARITH_MOD_XPLUSONE_H + +#include "qc_ldpc_parameters.h" + +#include "gf2x_arith.h" +#include "rng.h" + +#define NUM_BITS_GF2X_ELEMENT (P) // 152267 +#define NUM_DIGITS_GF2X_ELEMENT ((P+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) // 2380 +#define MSb_POSITION_IN_MSB_DIGIT_OF_ELEMENT ((P % DIGIT_SIZE_b) ? (P % DIGIT_SIZE_b)-1 : DIGIT_SIZE_b-1) +#define NUM_BITS_GF2X_MODULUS (P+1) +#define NUM_DIGITS_GF2X_MODULUS ((P+1+DIGIT_SIZE_b-1)/DIGIT_SIZE_b) // 2380 +#define MSb_POSITION_IN_MSB_DIGIT_OF_MODULUS (P-DIGIT_SIZE_b*(NUM_DIGITS_GF2X_MODULUS-1)) +#define INVALID_POS_VALUE (P) +#define P_BITS (18) // log_2(p) = 17.216243783 + +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]); +DIGIT PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_get_coeff(const DIGIT poly[], unsigned int exponent); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_set_coeff(DIGIT poly[], unsigned int exponent, DIGIT value); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_toggle_coeff(DIGIT poly[], unsigned int exponent); +int PQCLEAN_LEDAKEMLT52_LEAKTIME_population_count(DIGIT *poly); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_quicksort_sparse(POSITION_T Res[]); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul(DIGIT Res[], const DIGIT A[], const DIGIT B[]); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_transpose_in_place(DIGIT A[]); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_rand_circulant_sparse_block(POSITION_T *pos_ones, int countOnes, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_rand_circulant_blocks_sequence(DIGIT *sequence, AES_XOF_struct *seed_expander_ctx); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add_sparse(int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_transpose_in_place_sparse(int sizeA, POSITION_T A[]); +int PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_inverse(DIGIT out[], const DIGIT in[]); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T Res[], size_t sizeA, const POSITION_T A[], size_t sizeB, const POSITION_T B[]); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_dense_to_sparse(DIGIT Res[], const DIGIT dense[], POSITION_T sparse[], unsigned int nPos); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_tobytes(uint8_t *bytes, const DIGIT *poly); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_frombytes(DIGIT *poly, const uint8_t *poly_bytes); + +#endif diff --git a/crypto_kem/ledakemlt12/clean/kem.c b/crypto_kem/ledakemlt52/leaktime/kem.c similarity index 58% rename from crypto_kem/ledakemlt12/clean/kem.c rename to crypto_kem/ledakemlt52/leaktime/kem.c index 89f47145..316d14d0 100644 --- a/crypto_kem/ledakemlt12/clean/kem.c +++ b/crypto_kem/ledakemlt52/leaktime/kem.c @@ -8,43 +8,43 @@ static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) { size_t i; for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, - pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); } } static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) { size_t i; for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); } } static void pack_ct(uint8_t *sk_bytes, DIGIT *ct) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(sk_bytes, ct); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_tobytes(sk_bytes, ct); } static void unpack_ct(DIGIT *ct, const uint8_t *ct_bytes) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_frombytes(ct, ct_bytes); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_frombytes(ct, ct_bytes); } static void pack_error(uint8_t *error_bytes, DIGIT *error_digits) { size_t i; for (i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT12_CLEAN_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, - error_digits + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B, + error_digits + i * NUM_DIGITS_GF2X_ELEMENT); } } /* Generates a keypair - pk is the public key and sk is the secret key. */ -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { +int PQCLEAN_LEDAKEMLT52_LEAKTIME_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) { AES_XOF_struct niederreiter_keys_expander; publicKeyNiederreiter_t pk_nie; randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH); - PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); - PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); + PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed); + PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander); pack_pk(pk, &pk_nie); @@ -53,7 +53,7 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned cha /* Encrypt - pk is the public key, ct is a key encapsulation message (ciphertext), ss is the shared secret.*/ -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { +int PQCLEAN_LEDAKEMLT52_LEAKTIME_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) { AES_XOF_struct niederreiter_encap_key_expander; unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH]; DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; @@ -64,11 +64,11 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH); unpack_pk(&pk_nie, pk); - PQCLEAN_LEDAKEMLT12_CLEAN_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); - PQCLEAN_LEDAKEMLT12_CLEAN_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); + PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed); + PQCLEAN_LEDAKEMLT52_LEAKTIME_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander); pack_error(error_bytes, error_vector); HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); - PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_encrypt(syndrome, &pk_nie, error_vector); + PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_encrypt(syndrome, &pk_nie, error_vector); pack_ct(ct, syndrome); @@ -78,13 +78,13 @@ int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *s /* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private key, ss is the shared secret */ -int PQCLEAN_LEDAKEMLT12_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { +int PQCLEAN_LEDAKEMLT52_LEAKTIME_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) { DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT]; uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B]; DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT]; unpack_ct(syndrome, ct); - PQCLEAN_LEDAKEMLT12_CLEAN_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); + PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome); pack_error(decoded_error_bytes, decoded_error_vector); HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B)); diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.c b/crypto_kem/ledakemlt52/leaktime/niederreiter.c similarity index 65% rename from crypto_kem/ledakemlt32/clean/niederreiter.c rename to crypto_kem/ledakemlt52/leaktime/niederreiter.c index 1d67281c..d8a70262 100644 --- a/crypto_kem/ledakemlt32/clean/niederreiter.c +++ b/crypto_kem/ledakemlt52/leaktime/niederreiter.c @@ -8,7 +8,7 @@ #include -void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander) { POSITION_T HPosOnes[N0][DV]; // sequence of N0 circ block matrices (p x p): Hi POSITION_T HtrPosOnes[N0][DV]; // Sparse tranposed circulant H @@ -23,8 +23,8 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, sk->rejections = (int8_t) 0; do { - PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); - PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse(QPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT52_LEAKTIME_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, keys_expander); + PQCLEAN_LEDAKEMLT52_LEAKTIME_generateQsparse(QPosOnes, keys_expander); for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { LPosOnes[i][j] = INVALID_POS_VALUE; @@ -34,10 +34,10 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxPosOnes, DV, HPosOnes[i], qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], DV * M, LPosOnes[colQ], DV * M, auxPosOnes); processedQOnes[i] += qBlockWeights[i][colQ]; @@ -49,7 +49,7 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, } sk->rejections = sk->rejections + 1; if (is_L_full) { - threshold = PQCLEAN_LEDAKEMLT32_CLEAN_DFR_test(LPosOnes); + threshold = PQCLEAN_LEDAKEMLT52_LEAKTIME_DFR_test(LPosOnes); } } while (!is_L_full || threshold == DFR_TEST_FAIL); sk->rejections = sk->rejections - 1; @@ -58,41 +58,41 @@ void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, memset(Ln0dense, 0x00, sizeof(Ln0dense)); for (int j = 0; j < DV * M; j++) { if (LPosOnes[N0 - 1][j] != INVALID_POS_VALUE) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_set_coeff(Ln0dense, LPosOnes[N0 - 1][j], 1); } } memset(Ln0Inv, 0x00, sizeof(Ln0Inv)); - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_inverse(Ln0Inv, Ln0dense); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_inverse(Ln0Inv, Ln0dense); for (int i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_dense_to_sparse(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, Ln0Inv, LPosOnes[i], DV * M); } for (int i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_transpose_in_place(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT); } } -void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { +void PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_encrypt(DIGIT *syndrome, const publicKeyNiederreiter_t *pk, const DIGIT *err) { int i; DIGIT saux[NUM_DIGITS_GF2X_ELEMENT]; memset(syndrome, 0x00, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); for (i = 0; i < N0 - 1; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul(saux, - pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, - err + i * NUM_DIGITS_GF2X_ELEMENT); - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(syndrome, syndrome, saux); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul(saux, + pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT, + err + i * NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add(syndrome, syndrome, saux); } - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add(syndrome, syndrome, err + (N0 - 1)*NUM_DIGITS_GF2X_ELEMENT); } -int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { +int PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome) { AES_XOF_struct niederreiter_decrypt_expander; POSITION_T HPosOnes[N0][DV]; POSITION_T HtrPosOnes[N0][DV]; @@ -110,11 +110,11 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN int currQoneIdx, endQblockIdx; int decryptOk, err_weight; - PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); + PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander_from_trng(&niederreiter_decrypt_expander, sk->prng_seed); do { - PQCLEAN_LEDAKEMLT32_CLEAN_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); - PQCLEAN_LEDAKEMLT32_CLEAN_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT52_LEAKTIME_generateHPosOnes_HtrPosOnes(HPosOnes, HtrPosOnes, &niederreiter_decrypt_expander); + PQCLEAN_LEDAKEMLT52_LEAKTIME_generateQsparse(QPosOnes, &niederreiter_decrypt_expander); for (int i = 0; i < N0; i++) { for (int j = 0; j < DV * M; j++) { LPosOnes[i][j] = INVALID_POS_VALUE; @@ -124,10 +124,10 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN memset(processedQOnes, 0x00, sizeof(processedQOnes)); for (int colQ = 0; colQ < N0; colQ++) { for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(DV * M, auxPosOnes, + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxPosOnes, DV, HPosOnes[i], qBlockWeights[i][colQ], QPosOnes[i] + processedQOnes[i]); - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add_sparse(DV * M, LPosOnes[colQ], DV * M, LPosOnes[colQ], DV * M, auxPosOnes); processedQOnes[i] += qBlockWeights[i][colQ]; @@ -156,31 +156,31 @@ int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyN } for (int i = 0; i < N0; i++) { - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_sparse(DV * M, auxSparse, + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_sparse(DV * M, auxSparse, DV, HPosOnes[i], qBlockWeights[i][N0 - 1], &QPosOnes[i][ M - qBlockWeights[i][N0 - 1]]); - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_add_sparse(DV * M, Ln0trSparse, + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add_sparse(DV * M, Ln0trSparse, DV * M, Ln0trSparse, DV * M, auxSparse); } - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); - PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_transpose_in_place_sparse(DV * M, Ln0trSparse); + PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_dense_to_sparse(privateSyndrome, syndrome, Ln0trSparse, DV * M); /* prepare mockup error vector in case a decoding failure occurs */ memset(mockup_error_vector, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); memcpy(mockup_error_vector, syndrome, NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(&niederreiter_decrypt_expander, - ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), - TRNG_BYTE_LENGTH); + PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander(&niederreiter_decrypt_expander, + ((unsigned char *) mockup_error_vector) + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), + TRNG_BYTE_LENGTH); memset(err, 0x00, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B); - decryptOk = PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, + decryptOk = PQCLEAN_LEDAKEMLT52_LEAKTIME_bf_decoding(err, (const POSITION_T (*)[DV]) HtrPosOnes, (const POSITION_T (*)[M]) QtrPosOnes, privateSyndrome, sk->threshold); err_weight = 0; for (int i = 0 ; i < N0; i++) { - err_weight += PQCLEAN_LEDAKEMLT32_CLEAN_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); + err_weight += PQCLEAN_LEDAKEMLT52_LEAKTIME_population_count(err + (NUM_DIGITS_GF2X_ELEMENT * i)); } decryptOk = decryptOk && (err_weight == NUM_ERRORS_T); diff --git a/crypto_kem/ledakemlt32/clean/niederreiter.h b/crypto_kem/ledakemlt52/leaktime/niederreiter.h similarity index 63% rename from crypto_kem/ledakemlt32/clean/niederreiter.h rename to crypto_kem/ledakemlt52/leaktime/niederreiter.h index c2e09ffa..68a9a547 100644 --- a/crypto_kem/ledakemlt32/clean/niederreiter.h +++ b/crypto_kem/ledakemlt52/leaktime/niederreiter.h @@ -21,9 +21,9 @@ typedef struct { // with P coefficients. } publicKeyNiederreiter_t; -void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); -void PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); -int PQCLEAN_LEDAKEMLT32_CLEAN_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_keygen(publicKeyNiederreiter_t *pk, privateKeyNiederreiter_t *sk, AES_XOF_struct *keys_expander); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_encrypt(DIGIT syndrome[], const publicKeyNiederreiter_t *pk, const DIGIT *err); +int PQCLEAN_LEDAKEMLT52_LEAKTIME_niederreiter_decrypt(DIGIT *err, const privateKeyNiederreiter_t *sk, const DIGIT *syndrome); #endif diff --git a/crypto_kem/ledakemlt52/clean/qc_ldpc_parameters.h b/crypto_kem/ledakemlt52/leaktime/qc_ldpc_parameters.h similarity index 100% rename from crypto_kem/ledakemlt52/clean/qc_ldpc_parameters.h rename to crypto_kem/ledakemlt52/leaktime/qc_ldpc_parameters.h diff --git a/crypto_kem/ledakemlt52/clean/rng.c b/crypto_kem/ledakemlt52/leaktime/rng.c similarity index 94% rename from crypto_kem/ledakemlt52/clean/rng.c rename to crypto_kem/ledakemlt52/leaktime/rng.c index ef1a207f..b9f6c255 100644 --- a/crypto_kem/ledakemlt52/clean/rng.c +++ b/crypto_kem/ledakemlt52/leaktime/rng.c @@ -37,7 +37,7 @@ static void seedexpander_init(AES_XOF_struct *ctx, memset(ctx->buffer, 0x00, 16); } -void PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, +void PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy /* TRNG_BYTE_LENGTH wide buffer */) { @@ -61,7 +61,7 @@ void PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, x - returns the XOF data xlen - number of bytes to return */ -int PQCLEAN_LEDAKEMLT52_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { +int PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen) { size_t offset; aes256ctx ctx256; diff --git a/crypto_kem/ledakemlt32/clean/rng.h b/crypto_kem/ledakemlt52/leaktime/rng.h similarity index 67% rename from crypto_kem/ledakemlt32/clean/rng.h rename to crypto_kem/ledakemlt52/leaktime/rng.h index 8d7ee8c4..19470c36 100644 --- a/crypto_kem/ledakemlt32/clean/rng.h +++ b/crypto_kem/ledakemlt52/leaktime/rng.h @@ -18,7 +18,7 @@ typedef struct { unsigned char ctr[16]; } AES_XOF_struct; -int PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); -void PQCLEAN_LEDAKEMLT32_CLEAN_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); +int PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander(AES_XOF_struct *ctx, unsigned char *x, size_t xlen); +void PQCLEAN_LEDAKEMLT52_LEAKTIME_seedexpander_from_trng(AES_XOF_struct *ctx, const unsigned char *trng_entropy); #endif From 3c733b66918d6a57cd50e0f252ffaa28ed4c6026 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 17 Jun 2019 14:03:59 +0200 Subject: [PATCH 35/35] resolve todo, remove asserts, add duplicate checks and make sure they pass --- crypto_kem/ledakemlt12/leaktime/bf_decoding.c | 1 - crypto_kem/ledakemlt12/leaktime/gf2x_arith.c | 3 -- .../leaktime/gf2x_arith_mod_xPplusOne.c | 7 ++-- crypto_kem/ledakemlt12/leaktime/rng.h | 2 +- crypto_kem/ledakemlt32/leaktime/bf_decoding.c | 1 - crypto_kem/ledakemlt32/leaktime/gf2x_arith.c | 3 -- .../leaktime/gf2x_arith_mod_xPplusOne.c | 9 ++---- .../ledakemlt32/leaktime/niederreiter.h | 2 +- crypto_kem/ledakemlt52/leaktime/bf_decoding.c | 1 - crypto_kem/ledakemlt52/leaktime/gf2x_arith.c | 3 -- .../leaktime/gf2x_arith_mod_xPplusOne.c | 10 ++---- .../ledakemlt52/leaktime/niederreiter.h | 2 +- .../ledakemlt12_leaktime.yml | 32 +++++++++++++++++++ .../ledakemlt32_leaktime.yml | 32 +++++++++++++++++++ .../ledakemlt52_leaktime.yml | 32 +++++++++++++++++++ 15 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 test/duplicate_consistency/ledakemlt12_leaktime.yml create mode 100644 test/duplicate_consistency/ledakemlt32_leaktime.yml create mode 100644 test/duplicate_consistency/ledakemlt52_leaktime.yml diff --git a/crypto_kem/ledakemlt12/leaktime/bf_decoding.c b/crypto_kem/ledakemlt12/leaktime/bf_decoding.c index 0e1123ac..f095b7e7 100644 --- a/crypto_kem/ledakemlt12/leaktime/bf_decoding.c +++ b/crypto_kem/ledakemlt12/leaktime/bf_decoding.c @@ -1,7 +1,6 @@ #include "bf_decoding.h" #include "gf2x_arith_mod_xPplusOne.h" -#include #include int PQCLEAN_LEDAKEMLT12_LEAKTIME_bf_decoding(DIGIT err[], diff --git a/crypto_kem/ledakemlt12/leaktime/gf2x_arith.c b/crypto_kem/ledakemlt12/leaktime/gf2x_arith.c index 6791ed1e..a7799825 100644 --- a/crypto_kem/ledakemlt12/leaktime/gf2x_arith.c +++ b/crypto_kem/ledakemlt12/leaktime/gf2x_arith.c @@ -1,6 +1,5 @@ #include "gf2x_arith.h" -#include #include // memset(...) void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { @@ -11,7 +10,6 @@ void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const D /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT12_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { - assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } @@ -27,7 +25,6 @@ void PQCLEAN_LEDAKEMLT12_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsi /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT12_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { - assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } diff --git a/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.c index aa43cd45..b2de57d4 100644 --- a/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt12/leaktime/gf2x_arith_mod_xPplusOne.c @@ -1,7 +1,6 @@ #include "gf2x_arith_mod_xPplusOne.h" #include "rng.h" -#include #include // memcpy(...), memset(...) void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]) { @@ -431,14 +430,14 @@ void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T R } } -/* the implementation is safe even in case A or B alias with the result */ -/* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ +/* the implementation is safe even in case A or B alias with the result + * PRE: A and B should be sorted, disjunct arrays ending with INVALID_POS_VALUE */ void PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_mod_add_sparse( int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]) { - POSITION_T tmpRes[DV * M]; // TODO: now function only works for adding (disjunct) DV and M positions + POSITION_T tmpRes[DV * M]; int idxA = 0, idxB = 0, idxR = 0; while ( idxA < sizeA && idxB < sizeB && diff --git a/crypto_kem/ledakemlt12/leaktime/rng.h b/crypto_kem/ledakemlt12/leaktime/rng.h index 09a81a57..fc35f19f 100644 --- a/crypto_kem/ledakemlt12/leaktime/rng.h +++ b/crypto_kem/ledakemlt12/leaktime/rng.h @@ -8,7 +8,7 @@ #define RNG_BAD_MAXLEN (-1) #define RNG_BAD_OUTBUF (-2) #define RNG_BAD_REQ_LEN (-3) -#define RNG_MAXLEN (10 * 1024 * 1024) +#define RNG_MAXLEN (10 * 1024 * 1024) typedef struct { unsigned char buffer[16]; diff --git a/crypto_kem/ledakemlt32/leaktime/bf_decoding.c b/crypto_kem/ledakemlt32/leaktime/bf_decoding.c index 764904f1..07957b6c 100644 --- a/crypto_kem/ledakemlt32/leaktime/bf_decoding.c +++ b/crypto_kem/ledakemlt32/leaktime/bf_decoding.c @@ -1,7 +1,6 @@ #include "bf_decoding.h" #include "gf2x_arith_mod_xPplusOne.h" -#include #include int PQCLEAN_LEDAKEMLT32_LEAKTIME_bf_decoding(DIGIT err[], diff --git a/crypto_kem/ledakemlt32/leaktime/gf2x_arith.c b/crypto_kem/ledakemlt32/leaktime/gf2x_arith.c index 2ce4869f..c421b02d 100644 --- a/crypto_kem/ledakemlt32/leaktime/gf2x_arith.c +++ b/crypto_kem/ledakemlt32/leaktime/gf2x_arith.c @@ -1,6 +1,5 @@ #include "gf2x_arith.h" -#include #include // memset(...) void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { @@ -11,7 +10,6 @@ void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const D /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT32_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { - assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } @@ -27,7 +25,6 @@ void PQCLEAN_LEDAKEMLT32_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsi /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT32_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { - assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } diff --git a/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.c index 75109644..9576d55d 100644 --- a/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt32/leaktime/gf2x_arith_mod_xPplusOne.c @@ -1,7 +1,6 @@ #include "gf2x_arith_mod_xPplusOne.h" #include "rng.h" -#include #include // memcpy(...), memset(...) void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]) { @@ -211,8 +210,6 @@ void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_transpose_in_place(DIGIT A[]) { A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; } - // A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // no middle digit - if (slack_bits_amount) { PQCLEAN_LEDAKEMLT32_LEAKTIME_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); } @@ -431,14 +428,14 @@ void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T R } } -/* the implementation is safe even in case A or B alias with the result */ -/* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ +/* the implementation is safe even in case A or B alias with the result + * PRE: A and B should be sorted, disjunct arrays ending with INVALID_POS_VALUE */ void PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_mod_add_sparse( int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]) { - POSITION_T tmpRes[DV * M]; // TODO: now function only works for adding (disjunct) DV and M positions + POSITION_T tmpRes[DV * M]; int idxA = 0, idxB = 0, idxR = 0; while ( idxA < sizeA && idxB < sizeB && diff --git a/crypto_kem/ledakemlt32/leaktime/niederreiter.h b/crypto_kem/ledakemlt32/leaktime/niederreiter.h index 41048034..a954718d 100644 --- a/crypto_kem/ledakemlt32/leaktime/niederreiter.h +++ b/crypto_kem/ledakemlt32/leaktime/niederreiter.h @@ -10,7 +10,7 @@ typedef struct { * H and Q during decryption */ unsigned char prng_seed[TRNG_BYTE_LENGTH]; int8_t rejections; - uint8_t threshold; + uint8_t threshold; // for round 2 } privateKeyNiederreiter_t; typedef struct { diff --git a/crypto_kem/ledakemlt52/leaktime/bf_decoding.c b/crypto_kem/ledakemlt52/leaktime/bf_decoding.c index 476bf162..4d35a3fd 100644 --- a/crypto_kem/ledakemlt52/leaktime/bf_decoding.c +++ b/crypto_kem/ledakemlt52/leaktime/bf_decoding.c @@ -1,7 +1,6 @@ #include "bf_decoding.h" #include "gf2x_arith_mod_xPplusOne.h" -#include #include int PQCLEAN_LEDAKEMLT52_LEAKTIME_bf_decoding(DIGIT err[], diff --git a/crypto_kem/ledakemlt52/leaktime/gf2x_arith.c b/crypto_kem/ledakemlt52/leaktime/gf2x_arith.c index b1b9df44..c90b2bc8 100644 --- a/crypto_kem/ledakemlt52/leaktime/gf2x_arith.c +++ b/crypto_kem/ledakemlt52/leaktime/gf2x_arith.c @@ -1,6 +1,5 @@ #include "gf2x_arith.h" -#include #include // memset(...) void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const DIGIT B[], int nr) { @@ -11,7 +10,6 @@ void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_add(DIGIT Res[], const DIGIT A[], const D /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT52_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsigned int amount) { - assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } @@ -27,7 +25,6 @@ void PQCLEAN_LEDAKEMLT52_LEAKTIME_right_bit_shift_n(int length, DIGIT in[], unsi /* PRE: MAX ALLOWED ROTATION AMOUNT : DIGIT_SIZE_b */ void PQCLEAN_LEDAKEMLT52_LEAKTIME_left_bit_shift_n(int length, DIGIT in[], unsigned int amount) { - assert(amount < DIGIT_SIZE_b); if ( amount == 0 ) { return; } diff --git a/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.c b/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.c index ef7d0e57..389e23f6 100644 --- a/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.c +++ b/crypto_kem/ledakemlt52/leaktime/gf2x_arith_mod_xPplusOne.c @@ -1,10 +1,8 @@ #include "gf2x_arith_mod_xPplusOne.h" #include "rng.h" -#include #include // memcpy(...), memset(...) - void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_copy(DIGIT dest[], const DIGIT in[]) { for (int i = NUM_DIGITS_GF2X_ELEMENT - 1; i >= 0; i--) { dest[i] = in[i]; @@ -212,8 +210,6 @@ void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_transpose_in_place(DIGIT A[]) { A[NUM_DIGITS_GF2X_ELEMENT - 1 - i] = rev1; } - // A[NUM_DIGITS_GF2X_ELEMENT / 2] = reverse_digit(A[NUM_DIGITS_GF2X_ELEMENT / 2]); // no middle digit - if (slack_bits_amount) { PQCLEAN_LEDAKEMLT52_LEAKTIME_right_bit_shift_n(NUM_DIGITS_GF2X_ELEMENT, A, slack_bits_amount); } @@ -432,14 +428,14 @@ void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_mul_sparse(size_t sizeR, POSITION_T R } } -/* the implementation is safe even in case A or B alias with the result */ -/* PRE: A and B should be sorted and have INVALID_POS_VALUE at the end */ +/* the implementation is safe even in case A or B alias with the result + * PRE: A and B should be sorted, disjunct arrays ending with INVALID_POS_VALUE */ void PQCLEAN_LEDAKEMLT52_LEAKTIME_gf2x_mod_add_sparse( int sizeR, POSITION_T Res[], int sizeA, const POSITION_T A[], int sizeB, const POSITION_T B[]) { - POSITION_T tmpRes[DV * M]; // TODO: now function only works for adding (disjunct) DV and M positions + POSITION_T tmpRes[DV * M]; int idxA = 0, idxB = 0, idxR = 0; while ( idxA < sizeA && idxB < sizeB && diff --git a/crypto_kem/ledakemlt52/leaktime/niederreiter.h b/crypto_kem/ledakemlt52/leaktime/niederreiter.h index 68a9a547..a74e3169 100644 --- a/crypto_kem/ledakemlt52/leaktime/niederreiter.h +++ b/crypto_kem/ledakemlt52/leaktime/niederreiter.h @@ -10,7 +10,7 @@ typedef struct { * H and Q during decryption */ unsigned char prng_seed[TRNG_BYTE_LENGTH]; int8_t rejections; - uint8_t threshold; + uint8_t threshold; // for round 2 } privateKeyNiederreiter_t; typedef struct { diff --git a/test/duplicate_consistency/ledakemlt12_leaktime.yml b/test/duplicate_consistency/ledakemlt12_leaktime.yml new file mode 100644 index 00000000..e85f05ab --- /dev/null +++ b/test/duplicate_consistency/ledakemlt12_leaktime.yml @@ -0,0 +1,32 @@ +consistency_checks: +- source: + scheme: ledakemlt32 + implementation: leaktime + files: + - bf_decoding.c + - dfr_test.c + - dfr_test.h + - gf2x_arith.c + - gf2x_arith.h + - H_Q_matrices_generation.c + - H_Q_matrices_generation.h + - kem.c + - niederreiter.c + - niederreiter.h + - rng.c + - rng.h +- source: + scheme: ledakemlt52 + implementation: leaktime + files: + - bf_decoding.c + - dfr_test.c + - dfr_test.h + - gf2x_arith.c + - gf2x_arith.h + - H_Q_matrices_generation.c + - H_Q_matrices_generation.h + - kem.c + - niederreiter.c + - niederreiter.h + - rng.h diff --git a/test/duplicate_consistency/ledakemlt32_leaktime.yml b/test/duplicate_consistency/ledakemlt32_leaktime.yml new file mode 100644 index 00000000..ec6d6e50 --- /dev/null +++ b/test/duplicate_consistency/ledakemlt32_leaktime.yml @@ -0,0 +1,32 @@ +consistency_checks: +- source: + scheme: ledakemlt12 + implementation: leaktime + files: + - bf_decoding.c + - dfr_test.c + - dfr_test.h + - gf2x_arith.c + - gf2x_arith.h + - H_Q_matrices_generation.c + - H_Q_matrices_generation.h + - kem.c + - niederreiter.c + - niederreiter.h + - rng.c + - rng.h +- source: + scheme: ledakemlt52 + implementation: leaktime + files: + - bf_decoding.c + - dfr_test.c + - dfr_test.h + - gf2x_arith.c + - gf2x_arith.h + - H_Q_matrices_generation.c + - H_Q_matrices_generation.h + - kem.c + - niederreiter.c + - niederreiter.h + - rng.h diff --git a/test/duplicate_consistency/ledakemlt52_leaktime.yml b/test/duplicate_consistency/ledakemlt52_leaktime.yml new file mode 100644 index 00000000..b7602e85 --- /dev/null +++ b/test/duplicate_consistency/ledakemlt52_leaktime.yml @@ -0,0 +1,32 @@ +consistency_checks: +- source: + scheme: ledakemlt12 + implementation: leaktime + files: + - bf_decoding.c + - dfr_test.c + - dfr_test.h + - gf2x_arith.c + - gf2x_arith.h + - H_Q_matrices_generation.c + - H_Q_matrices_generation.h + - kem.c + - niederreiter.c + - niederreiter.h + - rng.h +- source: + scheme: ledakemlt32 + implementation: leaktime + files: + - bf_decoding.c + - dfr_test.c + - dfr_test.h + - gf2x_arith.c + - gf2x_arith.h + - gf2x_arith_mod_xPplusOne.c + - H_Q_matrices_generation.c + - H_Q_matrices_generation.h + - kem.c + - niederreiter.c + - niederreiter.h + - rng.h