diff --git a/src/Makefile.am b/src/Makefile.am index 09f039b..cd20f3f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -35,6 +35,7 @@ LIBVEXTA_CONSENSUS=libvexta_consensus.a LIBVEXTA_CLI=libvexta_cli.a LIBVEXTA_UTIL=libvexta_util.a LIBVEXTA_CRYPTO_BASE=crypto/libvexta_crypto_base.a +LIBVEXTA_PQ=crypto/libvexta_pq.a LIBVEXTAQT=qt/libvextaqt.a LIBSECP256K1=secp256k1/libsecp256k1.la @@ -70,6 +71,7 @@ $(LIBSECP256K1): $(wildcard secp256k1/src/*.h) $(wildcard secp256k1/src/*.c) $(w # But to build the less dependent modules first, we manually select their order here: EXTRA_LIBRARIES += \ $(LIBVEXTA_CRYPTO) \ + $(LIBVEXTA_PQ) \ $(LIBVEXTA_UTIL) \ $(LIBVEXTA_COMMON) \ $(LIBVEXTA_CONSENSUS) \ @@ -499,6 +501,36 @@ crypto_libvexta_crypto_shani_a_CXXFLAGS += $(SHANI_CXXFLAGS) crypto_libvexta_crypto_shani_a_CPPFLAGS += -DENABLE_SHANI crypto_libvexta_crypto_shani_a_SOURCES = crypto/sha256_shani.cpp +# post-quantum cryptography: ML-DSA-65 + SLH-DSA/SPHINCS+ sha2-128s +crypto_libvexta_pq_a_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -I$(srcdir) -I$(srcdir)/crypto/pq/mldsa65 -I$(srcdir)/crypto/pq/sphincsplus -DDILITHIUM_MODE=3 -DPARAMS=sphincs-sha2-128s +crypto_libvexta_pq_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) +crypto_libvexta_pq_a_CFLAGS = $(AM_CFLAGS) $(PIE_FLAGS) +crypto_libvexta_pq_a_SOURCES = \ + crypto/pq/pq.cpp \ + crypto/pq/pq.h \ + crypto/pq/pq_randombytes.cpp \ + crypto/pq/mldsa65/fips202.c \ + crypto/pq/mldsa65/ntt.c \ + crypto/pq/mldsa65/packing.c \ + crypto/pq/mldsa65/poly.c \ + crypto/pq/mldsa65/polyvec.c \ + crypto/pq/mldsa65/reduce.c \ + crypto/pq/mldsa65/rounding.c \ + crypto/pq/mldsa65/sign.c \ + crypto/pq/mldsa65/symmetric-shake.c \ + crypto/pq/sphincsplus/address.c \ + crypto/pq/sphincsplus/fips202.c \ + crypto/pq/sphincsplus/fors.c \ + crypto/pq/sphincsplus/hash_sha2.c \ + crypto/pq/sphincsplus/merkle.c \ + crypto/pq/sphincsplus/sha2.c \ + crypto/pq/sphincsplus/sign.c \ + crypto/pq/sphincsplus/thash_sha2_simple.c \ + crypto/pq/sphincsplus/utils.c \ + crypto/pq/sphincsplus/utilsx1.c \ + crypto/pq/sphincsplus/wots.c \ + crypto/pq/sphincsplus/wotsx1.c + # consensus: shared between all executables that validate any consensus rules. libvexta_consensus_a_CPPFLAGS = $(AM_CPPFLAGS) $(VEXTA_INCLUDES) libvexta_consensus_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) @@ -677,6 +709,7 @@ vexta_bin_ldadd = \ $(LIBVEXTA_UTIL) \ $(LIBVEXTA_ZMQ) \ $(LIBVEXTA_CONSENSUS) \ + $(LIBVEXTA_PQ) \ $(LIBVEXTA_CRYPTO) \ $(LIBLEVELDB) \ $(LIBLEVELDB_SSE42) \ @@ -711,6 +744,7 @@ vexta_cli_LDADD = \ $(LIBVEXTA_CLI) \ $(LIBUNIVALUE) \ $(LIBVEXTA_CONSENSUS) \ + $(LIBVEXTA_PQ) \ $(LIBVEXTA_UTIL) \ $(LIBVEXTA_CRYPTO) @@ -732,6 +766,7 @@ vexta_tx_LDADD = \ $(LIBVEXTA_COMMON) \ $(LIBVEXTA_UTIL) \ $(LIBVEXTA_CONSENSUS) \ + $(LIBVEXTA_PQ) \ $(LIBVEXTA_CRYPTO) \ $(LIBSECP256K1) @@ -765,6 +800,7 @@ vexta_util_LDADD = \ $(LIBVEXTA_UTIL) \ $(LIBUNIVALUE) \ $(LIBVEXTA_CONSENSUS) \ + $(LIBVEXTA_PQ) \ $(LIBVEXTA_CRYPTO) \ $(LIBSECP256K1) @@ -774,7 +810,32 @@ vexta_util_LDADD += $(BOOST_LIBS) # vextaconsensus library # if BUILD_VEXTA_LIBS include_HEADERS = script/vextaconsensus.h -libvextaconsensus_la_SOURCES = support/cleanse.cpp $(crypto_libvexta_crypto_base_a_SOURCES) $(libvexta_consensus_a_SOURCES) +libvextaconsensus_pq_sources = \ + crypto/pq/pq.cpp \ + crypto/pq/pq.h \ + crypto/pq/mldsa65/fips202.c \ + crypto/pq/mldsa65/ntt.c \ + crypto/pq/mldsa65/packing.c \ + crypto/pq/mldsa65/poly.c \ + crypto/pq/mldsa65/polyvec.c \ + crypto/pq/mldsa65/reduce.c \ + crypto/pq/mldsa65/rounding.c \ + crypto/pq/mldsa65/sign.c \ + crypto/pq/mldsa65/symmetric-shake.c \ + crypto/pq/sphincsplus/address.c \ + crypto/pq/sphincsplus/fips202.c \ + crypto/pq/sphincsplus/fors.c \ + crypto/pq/sphincsplus/hash_sha2.c \ + crypto/pq/sphincsplus/merkle.c \ + crypto/pq/sphincsplus/sha2.c \ + crypto/pq/sphincsplus/sign.c \ + crypto/pq/sphincsplus/thash_sha2_simple.c \ + crypto/pq/sphincsplus/utils.c \ + crypto/pq/sphincsplus/utilsx1.c \ + crypto/pq/sphincsplus/wots.c \ + crypto/pq/sphincsplus/wotsx1.c + +libvextaconsensus_la_SOURCES = support/cleanse.cpp $(crypto_libvexta_crypto_base_a_SOURCES) $(libvextaconsensus_pq_sources) $(libvexta_consensus_a_SOURCES) if GLIBC_BACK_COMPAT libvextaconsensus_la_SOURCES += compat/glibc_compat.cpp @@ -782,8 +843,9 @@ endif libvextaconsensus_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS) libvextaconsensus_la_LIBADD = $(LIBSECP256K1) -libvextaconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_VEXTA_INTERNAL +libvextaconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -I$(srcdir)/crypto/pq/mldsa65 -I$(srcdir)/crypto/pq/sphincsplus -DDILITHIUM_MODE=3 -DPARAMS=sphincs-sha2-128s -DVEXTA_PQ_VERIFY_ONLY -DBUILD_VEXTA_INTERNAL libvextaconsensus_la_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) +libvextaconsensus_la_CFLAGS = $(AM_CFLAGS) $(PIE_FLAGS) endif # diff --git a/src/Makefile.test.include b/src/Makefile.test.include index 2d3a3a7..e5287bc 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -111,6 +111,7 @@ DIGIBYTE_TESTS =\ test/policy_fee_tests.cpp \ test/policyestimator_tests.cpp \ test/pow_tests.cpp \ + test/pq_tests.cpp \ test/prevector_tests.cpp \ test/raii_event_tests.cpp \ test/random_tests.cpp \ @@ -177,8 +178,8 @@ DIGIBYTE_TEST_SUITE += \ endif test_test_digibyte_SOURCES = $(DIGIBYTE_TEST_SUITE) $(DIGIBYTE_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES) -test_test_digibyte_CPPFLAGS = $(AM_CPPFLAGS) $(DIGIBYTE_INCLUDES) $(TESTDEFS) $(EVENT_CFLAGS) -test_test_digibyte_LDADD = $(LIBTEST_UTIL) +test_test_digibyte_CPPFLAGS = $(AM_CPPFLAGS) $(DIGIBYTE_INCLUDES) $(UNIVALUE_CFLAGS) $(TESTDEFS) $(EVENT_CFLAGS) +test_test_digibyte_LDADD = $(LIBTEST_UTIL) $(LIBVEXTA_PQ) if ENABLE_WALLET test_test_digibyte_LDADD += $(LIBDIGIBYTE_WALLET) endif diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 48bec6e..15fbf01 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -103,6 +103,13 @@ public: consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT; consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay + // VEXTA quantum-resistant witness v2/v3. + // Integrated but intentionally not active yet. + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].bit = 3; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].min_activation_height = 0; + // The best chain should have at least this much work. consensus.nMinimumChainWork = uint256{}; @@ -235,6 +242,12 @@ public: consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = 1750457304; // 20th June 2025 Testnet consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay + // VEXTA quantum-resistant witness v2/v3. + // Integrated but intentionally not active yet. + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].bit = 3; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].min_activation_height = 0; // The best chain should have at least this much work. consensus.nMinimumChainWork = uint256{}; @@ -369,6 +382,13 @@ public: consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT; consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay + // VEXTA quantum-resistant witness v2/v3. + // Integrated but intentionally not active yet. + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].bit = 3; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].min_activation_height = 0; + // message start is defined as the first 4 bytes of the sha256d of the block script CHashWriter h(SER_DISK, 0); h << consensus.signet_challenge; @@ -454,6 +474,13 @@ public: consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT; consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay + // VEXTA quantum-resistant witness v2/v3. + // Integrated but intentionally not active yet. + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].bit = 3; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT; + consensus.vDeployments[Consensus::DEPLOYMENT_PQR].min_activation_height = 0; + consensus.nMinimumChainWork = uint256{}; consensus.defaultAssumeValid = uint256{}; diff --git a/src/consensus/params.h b/src/consensus/params.h index 28c42b0..f2c562e 100644 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -32,11 +32,12 @@ enum BuriedDeployment : int16_t { enum DeploymentPos : uint16_t { DEPLOYMENT_TESTDUMMY, DEPLOYMENT_TAPROOT, // Deployment of Schnorr/Taproot (BIPs 340-342) + DEPLOYMENT_PQR, // VEXTA quantum-resistant witness v2/v3 // NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp MAX_VERSION_BITS_DEPLOYMENTS }; constexpr bool ValidDeployment(BuriedDeployment dep) { return dep <= DEPLOYMENT_NVERSIONBIPS; } -constexpr bool ValidDeployment(DeploymentPos dep) { return DEPLOYMENT_TESTDUMMY <= dep && dep <= DEPLOYMENT_TAPROOT; } +constexpr bool ValidDeployment(DeploymentPos dep) { return DEPLOYMENT_TESTDUMMY <= dep && dep <= DEPLOYMENT_PQR; } /** * Struct for each individual consensus rule change using BIP9. diff --git a/src/crypto/pq/CONFORMANCE.md b/src/crypto/pq/CONFORMANCE.md new file mode 100644 index 0000000..5e29dde --- /dev/null +++ b/src/crypto/pq/CONFORMANCE.md @@ -0,0 +1,19 @@ +# Vexta PQC Conformance (b2 — full KAT) + +Validated against pinned upstream known-answer vectors. + +## ML-DSA-65 (FIPS 204, dilithium mode 3) +- Pinned commit: d35ba3fe5449bee3e6d43e1f296c3ca818bd36be +- Artifact: `_d/ref/test/test_vectors3` stdout +- SHA256: 14bf84918ee90e7afbd580191d3eb890d4557e0900b1145e39a8399ef7dd3fba +- Status: PASS + +## SPHINCS+-sha2-128s-simple (FIPS 205 / SLH-DSA) +- Pinned commit: 7ec789ace6874d875f4bb84cb61b81155398167e +- Checker: `_s/vectors.py sphincs-sha2-128s-simple ref` +- Expected SHA256: 65942fac8e225fde77dd277d297e68c94c2e25a2a4089f88be4b56fa92b18a84 +- Status: PASS (ok) + +Reproduce: + cd src/crypto/pq/_d/ref && make test/test_vectors3 && ./test/test_vectors3 | sha256sum + cd src/crypto/pq/_s && python3 vectors.py sphincs-sha2-128s-simple ref diff --git a/src/crypto/pq/README.md b/src/crypto/pq/README.md new file mode 100644 index 0000000..2b25733 --- /dev/null +++ b/src/crypto/pq/README.md @@ -0,0 +1,21 @@ +# Vexta post-quantum vendored references + +Clean-room vendored, MIT-attribution discipline. No KV5 lineage. + +## ML-DSA-65 (Dilithium mode 3, NIST FIPS 204) +- Upstream: https://github.com/pq-crystals/dilithium +- Pinned commit: d35ba3fe5449bee3e6d43e1f296c3ca818bd36be +- License: Apache-2.0 / public-domain (dual). Vendored, not relicensed. +- Param pin: DILITHIUM_MODE=3 (compile define). + +## SLH-DSA (SPHINCS+ sha2-128s-simple, NIST FIPS 205) +- Upstream: https://github.com/sphincs/sphincsplus +- Pinned commit: 7ec789ace6874d875f4bb84cb61b81155398167e +- License: CC0 public domain. Vendored, not relicensed. +- Param pin: PARAMS=sphincs-sha2-128s; sha2 backend + simple thash only. + +## Local modifications +- Deleted all test-only RNGs (randombytes.c, rng.*): insecure for wallet use. +- randombytes.h unified to a single prototype; definition in pq_randombytes.cpp + is backed by Bitcoin's GetStrongRandBytes (CSPRNG). +- Pruned shake/haraka backends and robust thash variants. diff --git a/src/crypto/pq/_d/.gitignore b/src/crypto/pq/_d/.gitignore new file mode 100644 index 0000000..17ace0a --- /dev/null +++ b/src/crypto/pq/_d/.gitignore @@ -0,0 +1,9 @@ +PQCsignKAT_Dilithium2.req +PQCsignKAT_Dilithium2.rsp +PQCsignKAT_Dilithium3.req +PQCsignKAT_Dilithium3.rsp +PQCsignKAT_Dilithium5.req +PQCsignKAT_Dilithium5.rsp +tvecs2 +tvecs3 +tvecs5 diff --git a/src/crypto/pq/_d/.travis.yml b/src/crypto/pq/_d/.travis.yml new file mode 100644 index 0000000..d389f46 --- /dev/null +++ b/src/crypto/pq/_d/.travis.yml @@ -0,0 +1,33 @@ +language: c +os: linux +dist: bionic +compiler: + - gcc + - clang +arch: + - amd64 + - arm64 + - ppc64le + - s390x +script: ./runtests.sh + +jobs: + include: + - os: osx + osx_image: xcode11.6 + compiler: clang + env: + - CFLAGS="-I/usr/local/opt/openssl@1.1/include/" + - NISTFLAGS="-I/usr/local/opt/openssl@1.1/include/" + - LDFLAGS="-L/usr/local/opt/openssl@1.1/lib/" + + - stage: coverage + os: linux + compiler: gcc + before_install: + - sudo apt-get update + - sudo apt-get install -y lcov + - gem install coveralls-lcov + script: ./runlcov.sh + after_success: + - coveralls-lcov ref/dilithium.lcov diff --git a/src/crypto/pq/_d/AUTHORS.md b/src/crypto/pq/_d/AUTHORS.md new file mode 100644 index 0000000..251f92c --- /dev/null +++ b/src/crypto/pq/_d/AUTHORS.md @@ -0,0 +1,7 @@ +Léo Ducas, +Eike Kiltz, +Tancrède Lepoint, +Vadim Lyubashevsky, +Gregor Seiler, +Peter Schwabe, +Damien Stehlé diff --git a/src/crypto/pq/_d/Common_META.yml b/src/crypto/pq/_d/Common_META.yml new file mode 100644 index 0000000..2efae86 --- /dev/null +++ b/src/crypto/pq/_d/Common_META.yml @@ -0,0 +1,15 @@ +commons: + - name: common_ref + folder_name: ref + sources: fips202.c fips202.h + - name: common_avx2 + folder_name: avx2 + sources: f1600x4.S fips202.c fips202.h fips202x4.c fips202x4.h + supported_platforms: + - architecture: x86_64 + operating_systems: + - Darwin + - Linux + required_flags: + - avx2 + - popcnt diff --git a/src/crypto/pq/_d/Dilithium2_META.yml b/src/crypto/pq/_d/Dilithium2_META.yml new file mode 100644 index 0000000..122b3ca --- /dev/null +++ b/src/crypto/pq/_d/Dilithium2_META.yml @@ -0,0 +1,44 @@ +name: Dilithium2 +type: signature +claimed-nist-level: 2 +length-public-key: 1312 +length-secret-key: 2560 +length-signature: 2420 +nistkat-sha256: 9a196e7fb32fbc93757dc2d8dc1924460eab66303c0c08aeb8b798fb8d8f8cf3 +testvectors-sha256: 5f0d135c0f7fd43f3fb9727265fcd6ec3651eb8c67c04ea5f3d8dfa1d99740d2 +principal-submitters: + - Vadim Lyubashevsky +auxiliary-submitters: + - Shi Bai + - Léo Ducas + - Eike Kiltz + - Tancrède Lepoint + - Peter Schwabe + - Gregor Seiler + - Damien Stehlé +implementations: + - name: ref + version: https://github.com/pq-crystals/dilithium/tree/master + folder_name: ref + compile_opts: -DDILITHIUM_MODE=2 -DDILITHIUM_RANDOMIZED_SIGNING + signature_keypair: pqcrystals_dilithium2_ref_keypair + signature_signature: pqcrystals_dilithium2_ref_signature + signature_verify: pqcrystals_dilithium2_ref_verify + sources: ../LICENSE api.h config.h params.h sign.c sign.h packing.c packing.h polyvec.c polyvec.h poly.c poly.h ntt.c ntt.h reduce.c reduce.h rounding.c rounding.h symmetric.h fips202.h symmetric-shake.c + common_dep: common_ref + - name: avx2 + version: https://github.com/pq-crystals/dilithium/tree/master + compile_opts: -DDILITHIUM_MODE=2 -DDILITHIUM_RANDOMIZED_SIGNING + signature_keypair: pqcrystals_dilithium2_avx2_keypair + signature_signature: pqcrystals_dilithium2_avx2_signature + signature_verify: pqcrystals_dilithium2_avx2_verify + sources: ../LICENSE api.h config.h params.h align.h sign.c sign.h packing.c packing.h polyvec.c polyvec.h poly.c poly.h ntt.S invntt.S pointwise.S ntt.h shuffle.S shuffle.inc consts.c consts.h rejsample.c rejsample.h rounding.c rounding.h symmetric.h fips202.h fips202x4.h symmetric-shake.c + common_dep: common_avx2 + supported_platforms: + - architecture: x86_64 + operating_systems: + - Darwin + - Linux + required_flags: + - avx2 + - popcnt diff --git a/src/crypto/pq/_d/Dilithium3_META.yml b/src/crypto/pq/_d/Dilithium3_META.yml new file mode 100644 index 0000000..b108b4f --- /dev/null +++ b/src/crypto/pq/_d/Dilithium3_META.yml @@ -0,0 +1,44 @@ +name: Dilithium3 +type: signature +claimed-nist-level: 3 +length-public-key: 1952 +length-secret-key: 4032 +length-signature: 3309 +nistkat-sha256: 7cb96242eac9907a55b5c84c202f0ebd552419c50b2e986dc2e28f07ecebf072 +testvectors-sha256: 14bf84918ee90e7afbd580191d3eb890d4557e0900b1145e39a8399ef7dd3fba +principal-submitters: + - Vadim Lyubashevsky +auxiliary-submitters: + - Shi Bai + - Léo Ducas + - Eike Kiltz + - Tancrède Lepoint + - Peter Schwabe + - Gregor Seiler + - Damien Stehlé +implementations: + - name: ref + version: https://github.com/pq-crystals/dilithium/tree/master + folder_name: ref + compile_opts: -DDILITHIUM_MODE=3 -DDILITHIUM_RANDOMIZED_SIGNING + signature_keypair: pqcrystals_dilithium3_ref_keypair + signature_signature: pqcrystals_dilithium3_ref_signature + signature_verify: pqcrystals_dilithium3_ref_verify + sources: ../LICENSE api.h config.h params.h sign.c sign.h packing.c packing.h polyvec.c polyvec.h poly.c poly.h ntt.c ntt.h reduce.c reduce.h rounding.c rounding.h symmetric.h fips202.h symmetric-shake.c + common_dep: common_ref + - name: avx2 + version: https://github.com/pq-crystals/dilithium/tree/master + compile_opts: -DDILITHIUM_MODE=3 -DDILITHIUM_RANDOMIZED_SIGNING + signature_keypair: pqcrystals_dilithium3_avx2_keypair + signature_signature: pqcrystals_dilithium3_avx2_signature + signature_verify: pqcrystals_dilithium3_avx2_verify + sources: ../LICENSE api.h config.h params.h align.h sign.c sign.h packing.c packing.h polyvec.c polyvec.h poly.c poly.h ntt.S invntt.S pointwise.S ntt.h shuffle.S shuffle.inc consts.c consts.h rejsample.c rejsample.h rounding.c rounding.h symmetric.h fips202.h fips202x4.h symmetric-shake.c + common_dep: common_avx2 + supported_platforms: + - architecture: x86_64 + operating_systems: + - Darwin + - Linux + required_flags: + - avx2 + - popcnt diff --git a/src/crypto/pq/_d/Dilithium5_META.yml b/src/crypto/pq/_d/Dilithium5_META.yml new file mode 100644 index 0000000..5163526 --- /dev/null +++ b/src/crypto/pq/_d/Dilithium5_META.yml @@ -0,0 +1,44 @@ +name: Dilithium5 +type: signature +claimed-nist-level: 5 +length-public-key: 2592 +length-secret-key: 4896 +length-signature: 4627 +nistkat-sha256: 4537905d2aabcf302fab2f242baed293459ecda7c230e6a67063b02c7e2840ed +testvectors-sha256: 759a3ba35210c7e27ff90a7ce5e399295533b82ef125e6ec98af158e00268e44 +principal-submitters: + - Vadim Lyubashevsky +auxiliary-submitters: + - Shi Bai + - Léo Ducas + - Eike Kiltz + - Tancrède Lepoint + - Peter Schwabe + - Gregor Seiler + - Damien Stehlé +implementations: + - name: ref + version: https://github.com/pq-crystals/dilithium/tree/master + folder_name: ref + compile_opts: -DDILITHIUM_MODE=5 -DDILITHIUM_RANDOMIZED_SIGNING + signature_keypair: pqcrystals_dilithium5_ref_keypair + signature_signature: pqcrystals_dilithium5_ref_signature + signature_verify: pqcrystals_dilithium5_ref_verify + sources: ../LICENSE api.h config.h params.h sign.c sign.h packing.c packing.h polyvec.c polyvec.h poly.c poly.h ntt.c ntt.h reduce.c reduce.h rounding.c rounding.h symmetric.h fips202.h symmetric-shake.c + common_dep: common_ref + - name: avx2 + version: https://github.com/pq-crystals/dilithium/tree/master + compile_opts: -DDILITHIUM_MODE=5 -DDILITHIUM_RANDOMIZED_SIGNING + signature_keypair: pqcrystals_dilithium5_avx2_keypair + signature_signature: pqcrystals_dilithium5_avx2_signature + signature_verify: pqcrystals_dilithium5_avx2_verify + sources: ../LICENSE api.h config.h params.h align.h sign.c sign.h packing.c packing.h polyvec.c polyvec.h poly.c poly.h ntt.S invntt.S pointwise.S ntt.h shuffle.S shuffle.inc consts.c consts.h rejsample.c rejsample.h rounding.c rounding.h symmetric.h fips202.h fips202x4.h symmetric-shake.c + common_dep: common_avx2 + supported_platforms: + - architecture: x86_64 + operating_systems: + - Darwin + - Linux + required_flags: + - avx2 + - popcnt diff --git a/src/crypto/pq/_d/LICENSE b/src/crypto/pq/_d/LICENSE new file mode 100644 index 0000000..c086379 --- /dev/null +++ b/src/crypto/pq/_d/LICENSE @@ -0,0 +1,7 @@ +Public Domain (https://creativecommons.org/share-your-work/public-domain/cc0/); +or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html) or GPL 2.0 License (http://www.gnu.org/licenses/gpl-2.0.html). + +For Keccak and the random number generator +we are using public-domain code from sources +and by authors listed in comments on top of +the respective files. diff --git a/src/crypto/pq/_d/README.md b/src/crypto/pq/_d/README.md new file mode 100644 index 0000000..9204fa6 --- /dev/null +++ b/src/crypto/pq/_d/README.md @@ -0,0 +1,81 @@ +# Dilithium + +[![Build Status](https://travis-ci.org/pq-crystals/dilithium.svg?branch=master)](https://travis-ci.org/pq-crystals/dilithium) [![Coverage Status](https://coveralls.io/repos/github/pq-crystals/dilithium/badge.svg?branch=master)](https://coveralls.io/github/pq-crystals/dilithium?branch=master) + +This repository contains the official reference implementation of the [Dilithium](https://www.pq-crystals.org/dilithium/) signature scheme, and an optimized implementation for x86 CPUs supporting the AVX2 instruction set. Dilithium is standardized as [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final). + +## Build instructions + +The implementations contain several test and benchmarking programs and a Makefile to facilitate compilation. + +### Prerequisites + +Some of the test programs require [OpenSSL](https://openssl.org). If the OpenSSL header files and/or shared libraries do not lie in one of the standard locations on your system, it is necessary to specify their location via compiler and linker flags in the environment variables `CFLAGS`, `NISTFLAGS`, and `LDFLAGS`. + +For example, on macOS you can install OpenSSL via [Homebrew](https://brew.sh) by running +```sh +brew install openssl +``` +Then, run +```sh +export CFLAGS="-I/opt/homebrew/opt/openssl@1.1/include" +export NISTFLAGS="-I/opt/homebrew/opt/openssl@1.1/include" +export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib" +``` +before compilation to add the OpenSSL header and library locations to the respective search paths. + +### Test programs + +To compile the test programs on Linux or macOS, go to the `ref/` or `avx2/` directory and run +```sh +make +``` +This produces the executables +```sh +test/test_dilithium$ALG +test/test_vectors$ALG +``` +where `$ALG` ranges over the parameter sets 2, 3, and 5. + +* `test_dilithium$ALG` tests 10000 times to generate keys, sign a random message of 59 bytes and verify the produced signature. Also, the program will try to verify wrong signatures where a single random byte of a valid signature was randomly distorted. The program will abort with an error message and return -1 if there was an error. Otherwise it will output the key and signature sizes and return 0. +* `test_vectors$ALG` performs further tests of internal functions and prints deterministically generated test vectors for several intermediate values that occur in the Dilithium algorithms. Namely, a 48 byte seed, the matrix A corresponding to the first 32 bytes of seed, a short secret vector s corresponding to the first 32 bytes of seed and nonce 0, a masking vector y corresponding to the seed and nonce 0, the high bits w1 and the low bits w0 of the vector w = Ay, the power-of-two rounding t1 of w and the corresponding low part t0, and the challenge c for the seed and w1. This program is meant to help to ensure compatibility of independent implementations. + +### Benchmarking programs + +For benchmarking the implementations, we provide speed test programs for x86 CPUs that use the Time Step Counter (TSC) or the actual cycle counter provided by the Performance Measurement Counters (PMC) to measure performance. To compile the programs run +```sh +make speed +``` +This produces the executables +```sh +test/test_speed$ALG +``` +for all parameter sets `$ALG` as above. The programs report the median and average cycle counts of 10000 executions of various internal functions and the API functions for key generation, signing and verification. By default the Time Step Counter is used. If instead you want to obtain the actual cycle counts from the Performance Measurement Counters export `CFLAGS="-DUSE_RDPMC"` before compilation. + +Please note that the reference implementation in `ref/` is not optimized for any platform, and, since it prioritises clean code, is significantly slower than a trivially optimized but still platform-independent implementation. Hence benchmarking the reference code does not provide representative results. + +Our Dilithium implementations are contained in the [SUPERCOP](https://bench.cr.yp.to) benchmarking framework. See [here](http://bench.cr.yp.to/results-sign.html#amd64-kizomba) for current cycle counts on an Intel KabyLake CPU. + +## Randomized signing + +By default our code implements Dilithium's hedged signing mode. To change this to the deterministic signing mode, undefine the `DILITHIUM_RANDOMIZED_SIGNING` preprocessor macro at compilation by either commenting the line +```sh +#define DILITHIUM_RANDOMIZED_SIGNING +``` +in config.h, or adding `-UDILITHIUM_RANDOMIZED_SIGNING` to the compiler flags in the environment variable `CFLAGS`. + +## Shared libraries + +All implementations can be compiled into shared libraries by running +```sh +make shared +``` +For example in the directory `ref/` of the reference implementation, this produces the libraries +```sh +libpqcrystals_dilithium$ALG_ref.so +``` +for all parameter sets `$ALG`, and the required symmetric crypto library +``` +libpqcrystals_fips202_ref.so +``` +All global symbols in the libraries lie in the namespaces `pqcrystals_dilithium$ALG_ref` and `libpqcrystals_fips202_ref`. Hence it is possible to link a program against all libraries simultaneously and obtain access to all implementations for all parameter sets. The corresponding API header file is `ref/api.h`, which contains prototypes for all API functions and preprocessor defines for the key and signature lengths. diff --git a/src/crypto/pq/_d/SHA256SUMS b/src/crypto/pq/_d/SHA256SUMS new file mode 100644 index 0000000..3e5914f --- /dev/null +++ b/src/crypto/pq/_d/SHA256SUMS @@ -0,0 +1,3 @@ +5f0d135c0f7fd43f3fb9727265fcd6ec3651eb8c67c04ea5f3d8dfa1d99740d2 tvecs2 +14bf84918ee90e7afbd580191d3eb890d4557e0900b1145e39a8399ef7dd3fba tvecs3 +759a3ba35210c7e27ff90a7ce5e399295533b82ef125e6ec98af158e00268e44 tvecs5 diff --git a/src/crypto/pq/_d/avx2/.gitignore b/src/crypto/pq/_d/avx2/.gitignore new file mode 100644 index 0000000..cc78368 --- /dev/null +++ b/src/crypto/pq/_d/avx2/.gitignore @@ -0,0 +1,2 @@ +*.so +*.o diff --git a/src/crypto/pq/_d/avx2/align.h b/src/crypto/pq/_d/avx2/align.h new file mode 100644 index 0000000..33fac1d --- /dev/null +++ b/src/crypto/pq/_d/avx2/align.h @@ -0,0 +1,19 @@ +#ifndef ALIGN_H +#define ALIGN_H + +#include +#include + +#define ALIGNED_UINT8(N) \ + union { \ + uint8_t coeffs[N]; \ + __m256i vec[(N+31)/32]; \ + } + +#define ALIGNED_INT32(N) \ + union { \ + int32_t coeffs[N]; \ + __m256i vec[(N+7)/8]; \ + } + +#endif diff --git a/src/crypto/pq/_d/avx2/api.h b/src/crypto/pq/_d/avx2/api.h new file mode 100644 index 0000000..36ec622 --- /dev/null +++ b/src/crypto/pq/_d/avx2/api.h @@ -0,0 +1,100 @@ +#ifndef API_H +#define API_H + +#include +#include + +#define pqcrystals_dilithium2_PUBLICKEYBYTES 1312 +#define pqcrystals_dilithium2_SECRETKEYBYTES 2560 +#define pqcrystals_dilithium2_BYTES 2420 + +#define pqcrystals_dilithium2_avx2_PUBLICKEYBYTES pqcrystals_dilithium2_PUBLICKEYBYTES +#define pqcrystals_dilithium2_avx2_SECRETKEYBYTES pqcrystals_dilithium2_SECRETKEYBYTES +#define pqcrystals_dilithium2_avx2_BYTES pqcrystals_dilithium2_BYTES + +int pqcrystals_dilithium2_avx2_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium2_avx2_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium2_avx2(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium2_avx2_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium2_avx2_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + + +#define pqcrystals_dilithium3_PUBLICKEYBYTES 1952 +#define pqcrystals_dilithium3_SECRETKEYBYTES 4032 +#define pqcrystals_dilithium3_BYTES 3309 + +#define pqcrystals_dilithium3_avx2_PUBLICKEYBYTES pqcrystals_dilithium3_PUBLICKEYBYTES +#define pqcrystals_dilithium3_avx2_SECRETKEYBYTES pqcrystals_dilithium3_SECRETKEYBYTES +#define pqcrystals_dilithium3_avx2_BYTES pqcrystals_dilithium3_BYTES + +int pqcrystals_dilithium3_avx2_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium3_avx2_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium3_avx2(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium3_avx2_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium3_avx2_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + + +#define pqcrystals_dilithium5_PUBLICKEYBYTES 2592 +#define pqcrystals_dilithium5_SECRETKEYBYTES 4896 +#define pqcrystals_dilithium5_BYTES 4627 + +#define pqcrystals_dilithium5_avx2_PUBLICKEYBYTES pqcrystals_dilithium5_PUBLICKEYBYTES +#define pqcrystals_dilithium5_avx2_SECRETKEYBYTES pqcrystals_dilithium5_SECRETKEYBYTES +#define pqcrystals_dilithium5_avx2_BYTES pqcrystals_dilithium5_BYTES + +int pqcrystals_dilithium5_avx2_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium5_avx2_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium5_avx2(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium5_avx2_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium5_avx2_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + + +#endif diff --git a/src/crypto/pq/_d/avx2/config.h b/src/crypto/pq/_d/avx2/config.h new file mode 100644 index 0000000..a9facc0 --- /dev/null +++ b/src/crypto/pq/_d/avx2/config.h @@ -0,0 +1,27 @@ +#ifndef CONFIG_H +#define CONFIG_H + +//#define DILITHIUM_MODE 2 +#define DILITHIUM_RANDOMIZED_SIGNING +//#define USE_RDPMC +//#define DBENCH + +#ifndef DILITHIUM_MODE +#define DILITHIUM_MODE 2 +#endif + +#if DILITHIUM_MODE == 2 +#define CRYPTO_ALGNAME "Dilithium2" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium2_avx2 +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium2_avx2_##s +#elif DILITHIUM_MODE == 3 +#define CRYPTO_ALGNAME "Dilithium3" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium3_avx2 +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium3_avx2_##s +#elif DILITHIUM_MODE == 5 +#define CRYPTO_ALGNAME "Dilithium5" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium5_avx2 +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium5_avx2_##s +#endif + +#endif diff --git a/src/crypto/pq/_d/avx2/consts.c b/src/crypto/pq/_d/avx2/consts.c new file mode 100644 index 0000000..414d99e --- /dev/null +++ b/src/crypto/pq/_d/avx2/consts.c @@ -0,0 +1,100 @@ +#include +#include "params.h" +#include "consts.h" + +#define QINV 58728449 // q^(-1) mod 2^32 +#define MONT -4186625 // 2^32 mod q +#define DIV 41978 // mont^2/256 +#define DIV_QINV -8395782 + +const qdata_t qdata = {{ +#define _8XQ 0 + Q, Q, Q, Q, Q, Q, Q, Q, + +#define _8XQINV 8 + QINV, QINV, QINV, QINV, QINV, QINV, QINV, QINV, + +#define _8XDIV_QINV 16 + DIV_QINV, DIV_QINV, DIV_QINV, DIV_QINV, DIV_QINV, DIV_QINV, DIV_QINV, DIV_QINV, + +#define _8XDIV 24 + DIV, DIV, DIV, DIV, DIV, DIV, DIV, DIV, + +#define _ZETAS_QINV 32 + -151046689, 1830765815, -1929875198, -1927777021, 1640767044, 1477910808, 1612161320, 1640734244, + 308362795, 308362795, 308362795, 308362795, -1815525077, -1815525077, -1815525077, -1815525077, + -1374673747, -1374673747, -1374673747, -1374673747, -1091570561, -1091570561, -1091570561, -1091570561, + -1929495947, -1929495947, -1929495947, -1929495947, 515185417, 515185417, 515185417, 515185417, + -285697463, -285697463, -285697463, -285697463, 625853735, 625853735, 625853735, 625853735, + 1727305304, 1727305304, 2082316400, 2082316400, -1364982364, -1364982364, 858240904, 858240904, + 1806278032, 1806278032, 222489248, 222489248, -346752664, -346752664, 684667771, 684667771, + 1654287830, 1654287830, -878576921, -878576921, -1257667337, -1257667337, -748618600, -748618600, + 329347125, 329347125, 1837364258, 1837364258, -1443016191, -1443016191, -1170414139, -1170414139, + -1846138265, -1631226336, -1404529459, 1838055109, 1594295555, -1076973524, -1898723372, -594436433, + -202001019, -475984260, -561427818, 1797021249, -1061813248, 2059733581, -1661512036, -1104976547, + -1750224323, -901666090, 418987550, 1831915353, -1925356481, 992097815, 879957084, 2024403852, + 1484874664, -1636082790, -285388938, -1983539117, -1495136972, -950076368, -1714807468, -952438995, + -1574918427, 1350681039, -2143979939, 1599739335, -1285853323, -993005454, -1440787840, 568627424, + -783134478, -588790216, 289871779, -1262003603, 2135294594, -1018755525, -889861155, 1665705315, + 1321868265, 1225434135, -1784632064, 666258756, 675310538, -1555941048, -1999506068, -1499481951, + -695180180, -1375177022, 1777179795, 334803717, -178766299, -518252220, 1957047970, 1146323031, + -654783359, -1974159335, 1651689966, 140455867, -1039411342, 1955560694, 1529189038, -2131021878, + -247357819, 1518161567, -86965173, 1708872713, 1787797779, 1638590967, -120646188, -1669960606, + -916321552, 1155548552, 2143745726, 1210558298, -1261461890, -318346816, 628664287, -1729304568, + 1422575624, 1424130038, -1185330464, 235321234, 168022240, 1206536194, 985155484, -894060583, + -898413, -1363460238, -605900043, 2027833504, 14253662, 1014493059, 863641633, 1819892093, + 2124962073, -1223601433, -1920467227, -1637785316, -1536588520, 694382729, 235104446, -1045062172, + 831969619, -300448763, 756955444, -260312805, 1554794072, 1339088280, -2040058690, -853476187, + -2047270596, -1723816713, -1591599803, -440824168, 1119856484, 1544891539, 155290192, -973777462, + 991903578, 912367099, -44694137, 1176904444, -421552614, -818371958, 1747917558, -325927722, + 908452108, 1851023419, -1176751719, -1354528380, -72690498, -314284737, 985022747, 963438279, + -1078959975, 604552167, -1021949428, 608791570, 173440395, -2126092136, -1316619236, -1039370342, + 6087993, -110126092, 565464272, -1758099917, -1600929361, 879867909, -1809756372, 400711272, + 1363007700, 30313375, -326425360, 1683520342, -517299994, 2027935492, -1372618620, 128353682, + -1123881663, 137583815, -635454918, -642772911, 45766801, 671509323, -2070602178, 419615363, + 1216882040, -270590488, -1276805128, 371462360, -1357098057, -384158533, 827959816, -596344473, + 702390549, -279505433, -260424530, -71875110, -1208667171, -1499603926, 2036925262, -540420426, + 746144248, -1420958686, 2032221021, 1904936414, 1257750362, 1926727420, 1931587462, 1258381762, + 885133339, 1629985060, 1967222129, 6363718, -1287922800, 1136965286, 1779436847, 1116720494, + 1042326957, 1405999311, 713994583, 940195359, -1542497137, 2061661095, -883155599, 1726753853, + -1547952704, 394851342, 283780712, 776003547, 1123958025, 201262505, 1934038751, 374860238, + +#define _ZETAS 328 + -3975713, 25847, -2608894, -518909, 237124, -777960, -876248, 466468, + 1826347, 1826347, 1826347, 1826347, 2353451, 2353451, 2353451, 2353451, + -359251, -359251, -359251, -359251, -2091905, -2091905, -2091905, -2091905, + 3119733, 3119733, 3119733, 3119733, -2884855, -2884855, -2884855, -2884855, + 3111497, 3111497, 3111497, 3111497, 2680103, 2680103, 2680103, 2680103, + 2725464, 2725464, 1024112, 1024112, -1079900, -1079900, 3585928, 3585928, + -549488, -549488, -1119584, -1119584, 2619752, 2619752, -2108549, -2108549, + -2118186, -2118186, -3859737, -3859737, -1399561, -1399561, -3277672, -3277672, + 1757237, 1757237, -19422, -19422, 4010497, 4010497, 280005, 280005, + 2706023, 95776, 3077325, 3530437, -1661693, -3592148, -2537516, 3915439, + -3861115, -3043716, 3574422, -2867647, 3539968, -300467, 2348700, -539299, + -1699267, -1643818, 3505694, -3821735, 3507263, -2140649, -1600420, 3699596, + 811944, 531354, 954230, 3881043, 3900724, -2556880, 2071892, -2797779, + -3930395, -3677745, -1452451, 2176455, -1257611, -4083598, -3190144, -3632928, + 3412210, 2147896, -2967645, -411027, -671102, -22981, -381987, 1852771, + -3343383, 508951, 44288, 904516, -3724342, 1653064, 2389356, 759969, + 189548, 3159746, -2409325, 1315589, 1285669, -812732, -3019102, -3628969, + -1528703, -3041255, 3475950, -1585221, 1939314, -1000202, -3157330, 126922, + -983419, 2715295, -3693493, -2477047, -1228525, -1308169, 1349076, -1430430, + 264944, 3097992, -1100098, 3958618, -8578, -3249728, -210977, -1316856, + -3553272, -1851402, -177440, 1341330, -1584928, -1439742, -3881060, 3839961, + 2091667, -3342478, 266997, -3520352, 900702, 495491, -655327, -3556995, + 342297, 3437287, 2842341, 4055324, -3767016, -2994039, -1333058, -451100, + -1279661, 1500165, -542412, -2584293, -2013608, 1957272, -3183426, 810149, + -3038916, 2213111, -426683, -1667432, -2939036, 183443, -554416, 3937738, + 3407706, 2244091, 2434439, -3759364, 1859098, -1613174, -3122442, -525098, + 286988, -3342277, 2691481, 1247620, 1250494, 1869119, 1237275, 1312455, + 1917081, 777191, -2831860, -3724270, 2432395, 3369112, 162844, 1652634, + 3523897, -975884, 1723600, -1104333, -2235985, -976891, 3919660, 1400424, + 2316500, -2446433, -1235728, -1197226, 909542, -43260, 2031748, -768622, + -2437823, 1735879, -2590150, 2486353, 2635921, 1903435, -3318210, 3306115, + -2546312, 2235880, -1671176, 594136, 2454455, 185531, 1616392, -3694233, + 3866901, 1717735, -1803090, -260646, -420899, 1612842, -48306, -846154, + 3817976, -3562462, 3513181, -3193378, 819034, -522500, 3207046, -3595838, + 4108315, 203044, 1265009, 1595974, -3548272, -1050970, -1430225, -1962642, + -1374803, 3406031, -1846953, -3776993, -164721, -1207385, 3014001, -1799107, + 269760, 472078, 1910376, -3833893, -2286327, -3545687, -1362209, 1976782, +}}; diff --git a/src/crypto/pq/_d/avx2/consts.h b/src/crypto/pq/_d/avx2/consts.h new file mode 100644 index 0000000..930d2f0 --- /dev/null +++ b/src/crypto/pq/_d/avx2/consts.h @@ -0,0 +1,38 @@ +#ifndef CONSTS_H +#define CONSTS_H + +#include "params.h" + +#define _8XQ 0 +#define _8XQINV 8 +#define _8XDIV_QINV 16 +#define _8XDIV 24 +#define _ZETAS_QINV 32 +#define _ZETAS 328 + +/* The C ABI on MacOS exports all symbols with a leading + * underscore. This means that any symbols we refer to from + * C files (functions) can't be found, and all symbols we + * refer to from ASM also can't be found. + * + * This define helps us get around this + */ +#if defined(__WIN32__) || defined(__APPLE__) +#define decorate(s) _##s +#define _cdecl(s) decorate(s) +#define cdecl(s) _cdecl(DILITHIUM_NAMESPACE(##s)) +#else +#define cdecl(s) DILITHIUM_NAMESPACE(##s) +#endif + +#ifndef __ASSEMBLER__ + +#include "align.h" + +typedef ALIGNED_INT32(624) qdata_t; + +#define qdata DILITHIUM_NAMESPACE(qdata) +extern const qdata_t qdata; + +#endif +#endif diff --git a/src/crypto/pq/_d/avx2/f1600x4.S b/src/crypto/pq/_d/avx2/f1600x4.S new file mode 100644 index 0000000..5455129 --- /dev/null +++ b/src/crypto/pq/_d/avx2/f1600x4.S @@ -0,0 +1,909 @@ +/* Taken from Bas Westerbaan's new 4-way SHAKE implementation + * for Sphincs+ (https://github.com/sphincs/sphincsplus/pull/14/), + * but uses vpshufb for byte-granular rotations as in the Keccak Code Package. */ + +#include "fips202x4.h" + +.data +.p2align 5 +rho8: +.byte 7,0,1,2,3,4,5,6,15,8,9,10,11,12,13,14,7,0,1,2,3,4,5,6,15,8,9,10,11,12,13,14 +rho56: +.byte 1,2,3,4,5,6,7,0,9,10,11,12,13,14,15,8,1,2,3,4,5,6,7,0,9,10,11,12,13,14,15,8 + +.text +.global cdecl(f1600x4) +cdecl(f1600x4): +vmovdqa rho8(%rip), %ymm0 +movq $6, %rax +looptop: +vmovdqa 0(%rdi), %ymm8 +vmovdqa 32(%rdi), %ymm9 +vmovdqa 64(%rdi), %ymm10 +vmovdqa 96(%rdi), %ymm11 +vmovdqa 128(%rdi), %ymm12 +vpxor 160(%rdi), %ymm8, %ymm8 +vpxor 192(%rdi), %ymm9, %ymm9 +vpxor 224(%rdi), %ymm10, %ymm10 +vpxor 256(%rdi), %ymm11, %ymm11 +vpxor 288(%rdi), %ymm12, %ymm12 +vpxor 320(%rdi), %ymm8, %ymm8 +vpxor 352(%rdi), %ymm9, %ymm9 +vpxor 384(%rdi), %ymm10, %ymm10 +vpxor 416(%rdi), %ymm11, %ymm11 +vpxor 448(%rdi), %ymm12, %ymm12 +vpxor 480(%rdi), %ymm8, %ymm8 +vpxor 512(%rdi), %ymm9, %ymm9 +vpxor 544(%rdi), %ymm10, %ymm10 +vpxor 576(%rdi), %ymm11, %ymm11 +vpxor 608(%rdi), %ymm12, %ymm12 +vpxor 640(%rdi), %ymm8, %ymm8 +vpxor 672(%rdi), %ymm9, %ymm9 +vpxor 704(%rdi), %ymm10, %ymm10 +vpxor 736(%rdi), %ymm11, %ymm11 +vpxor 768(%rdi), %ymm12, %ymm12 +vpsllq $1, %ymm9, %ymm13 +vpsllq $1, %ymm10, %ymm14 +vpsllq $1, %ymm11, %ymm15 +vpsllq $1, %ymm12, %ymm7 +vpsllq $1, %ymm8, %ymm6 +vpsrlq $63, %ymm9, %ymm5 +vpsrlq $63, %ymm10, %ymm4 +vpsrlq $63, %ymm11, %ymm3 +vpsrlq $63, %ymm12, %ymm2 +vpsrlq $63, %ymm8, %ymm1 +vpor %ymm13, %ymm5, %ymm5 +vpor %ymm14, %ymm4, %ymm4 +vpor %ymm15, %ymm3, %ymm3 +vpor %ymm7, %ymm2, %ymm2 +vpor %ymm6, %ymm1, %ymm1 +vpxor %ymm5, %ymm12, %ymm5 +vpxor %ymm4, %ymm8, %ymm4 +vpxor %ymm3, %ymm9, %ymm3 +vpxor %ymm2, %ymm10, %ymm2 +vpxor %ymm1, %ymm11, %ymm1 +vpxor 0(%rdi), %ymm5, %ymm8 +vpxor 192(%rdi), %ymm4, %ymm9 +vpxor 384(%rdi), %ymm3, %ymm10 +vpxor 576(%rdi), %ymm2, %ymm11 +vpxor 768(%rdi), %ymm1, %ymm12 +vpsllq $44, %ymm9, %ymm14 +vpsllq $43, %ymm10, %ymm15 +vpsllq $21, %ymm11, %ymm7 +vpsllq $14, %ymm12, %ymm6 +vpsrlq $20, %ymm9, %ymm9 +vpsrlq $21, %ymm10, %ymm10 +vpsrlq $43, %ymm11, %ymm11 +vpsrlq $50, %ymm12, %ymm12 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vpbroadcastq 0(%rsi), %ymm8 +vpxor %ymm8, %ymm13, %ymm13 +vmovdqa %ymm13, 0(%rdi) +vmovdqa %ymm14, 192(%rdi) +vmovdqa %ymm15, 384(%rdi) +vmovdqa %ymm7, 576(%rdi) +vmovdqa %ymm6, 768(%rdi) +vpxor 96(%rdi), %ymm2, %ymm8 +vpxor 288(%rdi), %ymm1, %ymm9 +vpxor 320(%rdi), %ymm5, %ymm10 +vpxor 512(%rdi), %ymm4, %ymm11 +vpxor 704(%rdi), %ymm3, %ymm12 +vpsllq $28, %ymm8, %ymm13 +vpsllq $20, %ymm9, %ymm14 +vpsllq $3, %ymm10, %ymm15 +vpsllq $45, %ymm11, %ymm7 +vpsllq $61, %ymm12, %ymm6 +vpsrlq $36, %ymm8, %ymm8 +vpsrlq $44, %ymm9, %ymm9 +vpsrlq $61, %ymm10, %ymm10 +vpsrlq $19, %ymm11, %ymm11 +vpsrlq $3, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 320(%rdi) +vmovdqa %ymm14, 512(%rdi) +vmovdqa %ymm15, 704(%rdi) +vmovdqa %ymm7, 96(%rdi) +vmovdqa %ymm6, 288(%rdi) +vpxor 32(%rdi), %ymm4, %ymm8 +vpxor 224(%rdi), %ymm3, %ymm9 +vpxor 416(%rdi), %ymm2, %ymm10 +vpxor 608(%rdi), %ymm1, %ymm11 +vpxor 640(%rdi), %ymm5, %ymm12 +vpsllq $1, %ymm8, %ymm13 +vpsllq $6, %ymm9, %ymm14 +vpsllq $25, %ymm10, %ymm15 +#vpsllq $8, %ymm11, %ymm7 +vpsllq $18, %ymm12, %ymm6 +vpsrlq $63, %ymm8, %ymm8 +vpsrlq $58, %ymm9, %ymm9 +vpsrlq $39, %ymm10, %ymm10 +#vpsrlq $56, %ymm11, %ymm11 +vpsrlq $46, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +#vpor %ymm7, %ymm11, %ymm11 +vpshufb %ymm0, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 640(%rdi) +vmovdqa %ymm14, 32(%rdi) +vmovdqa %ymm15, 224(%rdi) +vmovdqa %ymm7, 416(%rdi) +vmovdqa %ymm6, 608(%rdi) +vpxor 128(%rdi), %ymm1, %ymm8 +vpxor 160(%rdi), %ymm5, %ymm9 +vpxor 352(%rdi), %ymm4, %ymm10 +vpxor 544(%rdi), %ymm3, %ymm11 +vpxor 736(%rdi), %ymm2, %ymm12 +vpsllq $27, %ymm8, %ymm13 +vpsllq $36, %ymm9, %ymm14 +vpsllq $10, %ymm10, %ymm15 +vpsllq $15, %ymm11, %ymm7 +#vpsllq $56, %ymm12, %ymm6 +vpsrlq $37, %ymm8, %ymm8 +vpsrlq $28, %ymm9, %ymm9 +vpsrlq $54, %ymm10, %ymm10 +vpsrlq $49, %ymm11, %ymm11 +#vpsrlq $8, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +#vpor %ymm6, %ymm12, %ymm12 +vpshufb rho56(%rip), %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 160(%rdi) +vmovdqa %ymm14, 352(%rdi) +vmovdqa %ymm15, 544(%rdi) +vmovdqa %ymm7, 736(%rdi) +vmovdqa %ymm6, 128(%rdi) +vpxor 64(%rdi), %ymm3, %ymm8 +vpxor 256(%rdi), %ymm2, %ymm9 +vpxor 448(%rdi), %ymm1, %ymm10 +vpxor 480(%rdi), %ymm5, %ymm11 +vpxor 672(%rdi), %ymm4, %ymm12 +vpsllq $62, %ymm8, %ymm13 +vpsllq $55, %ymm9, %ymm14 +vpsllq $39, %ymm10, %ymm15 +vpsllq $41, %ymm11, %ymm7 +vpsllq $2, %ymm12, %ymm6 +vpsrlq $2, %ymm8, %ymm8 +vpsrlq $9, %ymm9, %ymm9 +vpsrlq $25, %ymm10, %ymm10 +vpsrlq $23, %ymm11, %ymm11 +vpsrlq $62, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 480(%rdi) +vmovdqa %ymm14, 672(%rdi) +vmovdqa %ymm15, 64(%rdi) +vmovdqa %ymm7, 256(%rdi) +vmovdqa %ymm6, 448(%rdi) +vmovdqa 0(%rdi), %ymm8 +vmovdqa 32(%rdi), %ymm9 +vmovdqa 64(%rdi), %ymm10 +vmovdqa 96(%rdi), %ymm11 +vmovdqa 128(%rdi), %ymm12 +vpxor 160(%rdi), %ymm8, %ymm8 +vpxor 192(%rdi), %ymm9, %ymm9 +vpxor 224(%rdi), %ymm10, %ymm10 +vpxor 256(%rdi), %ymm11, %ymm11 +vpxor 288(%rdi), %ymm12, %ymm12 +vpxor 320(%rdi), %ymm8, %ymm8 +vpxor 352(%rdi), %ymm9, %ymm9 +vpxor 384(%rdi), %ymm10, %ymm10 +vpxor 416(%rdi), %ymm11, %ymm11 +vpxor 448(%rdi), %ymm12, %ymm12 +vpxor 480(%rdi), %ymm8, %ymm8 +vpxor 512(%rdi), %ymm9, %ymm9 +vpxor 544(%rdi), %ymm10, %ymm10 +vpxor 576(%rdi), %ymm11, %ymm11 +vpxor 608(%rdi), %ymm12, %ymm12 +vpxor 640(%rdi), %ymm8, %ymm8 +vpxor 672(%rdi), %ymm9, %ymm9 +vpxor 704(%rdi), %ymm10, %ymm10 +vpxor 736(%rdi), %ymm11, %ymm11 +vpxor 768(%rdi), %ymm12, %ymm12 +vpsllq $1, %ymm9, %ymm13 +vpsllq $1, %ymm10, %ymm14 +vpsllq $1, %ymm11, %ymm15 +vpsllq $1, %ymm12, %ymm7 +vpsllq $1, %ymm8, %ymm6 +vpsrlq $63, %ymm9, %ymm5 +vpsrlq $63, %ymm10, %ymm4 +vpsrlq $63, %ymm11, %ymm3 +vpsrlq $63, %ymm12, %ymm2 +vpsrlq $63, %ymm8, %ymm1 +vpor %ymm13, %ymm5, %ymm5 +vpor %ymm14, %ymm4, %ymm4 +vpor %ymm15, %ymm3, %ymm3 +vpor %ymm7, %ymm2, %ymm2 +vpor %ymm6, %ymm1, %ymm1 +vpxor %ymm5, %ymm12, %ymm5 +vpxor %ymm4, %ymm8, %ymm4 +vpxor %ymm3, %ymm9, %ymm3 +vpxor %ymm2, %ymm10, %ymm2 +vpxor %ymm1, %ymm11, %ymm1 +vpxor 0(%rdi), %ymm5, %ymm8 +vpxor 512(%rdi), %ymm4, %ymm9 +vpxor 224(%rdi), %ymm3, %ymm10 +vpxor 736(%rdi), %ymm2, %ymm11 +vpxor 448(%rdi), %ymm1, %ymm12 +vpsllq $44, %ymm9, %ymm14 +vpsllq $43, %ymm10, %ymm15 +vpsllq $21, %ymm11, %ymm7 +vpsllq $14, %ymm12, %ymm6 +vpsrlq $20, %ymm9, %ymm9 +vpsrlq $21, %ymm10, %ymm10 +vpsrlq $43, %ymm11, %ymm11 +vpsrlq $50, %ymm12, %ymm12 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vpbroadcastq 8(%rsi), %ymm8 +vpxor %ymm8, %ymm13, %ymm13 +vmovdqa %ymm13, 0(%rdi) +vmovdqa %ymm14, 512(%rdi) +vmovdqa %ymm15, 224(%rdi) +vmovdqa %ymm7, 736(%rdi) +vmovdqa %ymm6, 448(%rdi) +vpxor 576(%rdi), %ymm2, %ymm8 +vpxor 288(%rdi), %ymm1, %ymm9 +vpxor 640(%rdi), %ymm5, %ymm10 +vpxor 352(%rdi), %ymm4, %ymm11 +vpxor 64(%rdi), %ymm3, %ymm12 +vpsllq $28, %ymm8, %ymm13 +vpsllq $20, %ymm9, %ymm14 +vpsllq $3, %ymm10, %ymm15 +vpsllq $45, %ymm11, %ymm7 +vpsllq $61, %ymm12, %ymm6 +vpsrlq $36, %ymm8, %ymm8 +vpsrlq $44, %ymm9, %ymm9 +vpsrlq $61, %ymm10, %ymm10 +vpsrlq $19, %ymm11, %ymm11 +vpsrlq $3, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 640(%rdi) +vmovdqa %ymm14, 352(%rdi) +vmovdqa %ymm15, 64(%rdi) +vmovdqa %ymm7, 576(%rdi) +vmovdqa %ymm6, 288(%rdi) +vpxor 192(%rdi), %ymm4, %ymm8 +vpxor 704(%rdi), %ymm3, %ymm9 +vpxor 416(%rdi), %ymm2, %ymm10 +vpxor 128(%rdi), %ymm1, %ymm11 +vpxor 480(%rdi), %ymm5, %ymm12 +vpsllq $1, %ymm8, %ymm13 +vpsllq $6, %ymm9, %ymm14 +vpsllq $25, %ymm10, %ymm15 +#vpsllq $8, %ymm11, %ymm7 +vpsllq $18, %ymm12, %ymm6 +vpsrlq $63, %ymm8, %ymm8 +vpsrlq $58, %ymm9, %ymm9 +vpsrlq $39, %ymm10, %ymm10 +#vpsrlq $56, %ymm11, %ymm11 +vpsrlq $46, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +#vpor %ymm7, %ymm11, %ymm11 +vpshufb %ymm0, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 480(%rdi) +vmovdqa %ymm14, 192(%rdi) +vmovdqa %ymm15, 704(%rdi) +vmovdqa %ymm7, 416(%rdi) +vmovdqa %ymm6, 128(%rdi) +vpxor 768(%rdi), %ymm1, %ymm8 +vpxor 320(%rdi), %ymm5, %ymm9 +vpxor 32(%rdi), %ymm4, %ymm10 +vpxor 544(%rdi), %ymm3, %ymm11 +vpxor 256(%rdi), %ymm2, %ymm12 +vpsllq $27, %ymm8, %ymm13 +vpsllq $36, %ymm9, %ymm14 +vpsllq $10, %ymm10, %ymm15 +vpsllq $15, %ymm11, %ymm7 +#vpsllq $56, %ymm12, %ymm6 +vpsrlq $37, %ymm8, %ymm8 +vpsrlq $28, %ymm9, %ymm9 +vpsrlq $54, %ymm10, %ymm10 +vpsrlq $49, %ymm11, %ymm11 +#vpsrlq $8, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +#vpor %ymm6, %ymm12, %ymm12 +vpshufb rho56(%rip), %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 320(%rdi) +vmovdqa %ymm14, 32(%rdi) +vmovdqa %ymm15, 544(%rdi) +vmovdqa %ymm7, 256(%rdi) +vmovdqa %ymm6, 768(%rdi) +vpxor 384(%rdi), %ymm3, %ymm8 +vpxor 96(%rdi), %ymm2, %ymm9 +vpxor 608(%rdi), %ymm1, %ymm10 +vpxor 160(%rdi), %ymm5, %ymm11 +vpxor 672(%rdi), %ymm4, %ymm12 +vpsllq $62, %ymm8, %ymm13 +vpsllq $55, %ymm9, %ymm14 +vpsllq $39, %ymm10, %ymm15 +vpsllq $41, %ymm11, %ymm7 +vpsllq $2, %ymm12, %ymm6 +vpsrlq $2, %ymm8, %ymm8 +vpsrlq $9, %ymm9, %ymm9 +vpsrlq $25, %ymm10, %ymm10 +vpsrlq $23, %ymm11, %ymm11 +vpsrlq $62, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 160(%rdi) +vmovdqa %ymm14, 672(%rdi) +vmovdqa %ymm15, 384(%rdi) +vmovdqa %ymm7, 96(%rdi) +vmovdqa %ymm6, 608(%rdi) +vmovdqa 0(%rdi), %ymm8 +vmovdqa 32(%rdi), %ymm9 +vmovdqa 64(%rdi), %ymm10 +vmovdqa 96(%rdi), %ymm11 +vmovdqa 128(%rdi), %ymm12 +vpxor 160(%rdi), %ymm8, %ymm8 +vpxor 192(%rdi), %ymm9, %ymm9 +vpxor 224(%rdi), %ymm10, %ymm10 +vpxor 256(%rdi), %ymm11, %ymm11 +vpxor 288(%rdi), %ymm12, %ymm12 +vpxor 320(%rdi), %ymm8, %ymm8 +vpxor 352(%rdi), %ymm9, %ymm9 +vpxor 384(%rdi), %ymm10, %ymm10 +vpxor 416(%rdi), %ymm11, %ymm11 +vpxor 448(%rdi), %ymm12, %ymm12 +vpxor 480(%rdi), %ymm8, %ymm8 +vpxor 512(%rdi), %ymm9, %ymm9 +vpxor 544(%rdi), %ymm10, %ymm10 +vpxor 576(%rdi), %ymm11, %ymm11 +vpxor 608(%rdi), %ymm12, %ymm12 +vpxor 640(%rdi), %ymm8, %ymm8 +vpxor 672(%rdi), %ymm9, %ymm9 +vpxor 704(%rdi), %ymm10, %ymm10 +vpxor 736(%rdi), %ymm11, %ymm11 +vpxor 768(%rdi), %ymm12, %ymm12 +vpsllq $1, %ymm9, %ymm13 +vpsllq $1, %ymm10, %ymm14 +vpsllq $1, %ymm11, %ymm15 +vpsllq $1, %ymm12, %ymm7 +vpsllq $1, %ymm8, %ymm6 +vpsrlq $63, %ymm9, %ymm5 +vpsrlq $63, %ymm10, %ymm4 +vpsrlq $63, %ymm11, %ymm3 +vpsrlq $63, %ymm12, %ymm2 +vpsrlq $63, %ymm8, %ymm1 +vpor %ymm13, %ymm5, %ymm5 +vpor %ymm14, %ymm4, %ymm4 +vpor %ymm15, %ymm3, %ymm3 +vpor %ymm7, %ymm2, %ymm2 +vpor %ymm6, %ymm1, %ymm1 +vpxor %ymm5, %ymm12, %ymm5 +vpxor %ymm4, %ymm8, %ymm4 +vpxor %ymm3, %ymm9, %ymm3 +vpxor %ymm2, %ymm10, %ymm2 +vpxor %ymm1, %ymm11, %ymm1 +vpxor 0(%rdi), %ymm5, %ymm8 +vpxor 352(%rdi), %ymm4, %ymm9 +vpxor 704(%rdi), %ymm3, %ymm10 +vpxor 256(%rdi), %ymm2, %ymm11 +vpxor 608(%rdi), %ymm1, %ymm12 +vpsllq $44, %ymm9, %ymm14 +vpsllq $43, %ymm10, %ymm15 +vpsllq $21, %ymm11, %ymm7 +vpsllq $14, %ymm12, %ymm6 +vpsrlq $20, %ymm9, %ymm9 +vpsrlq $21, %ymm10, %ymm10 +vpsrlq $43, %ymm11, %ymm11 +vpsrlq $50, %ymm12, %ymm12 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vpbroadcastq 16(%rsi), %ymm8 +vpxor %ymm8, %ymm13, %ymm13 +vmovdqa %ymm13, 0(%rdi) +vmovdqa %ymm14, 352(%rdi) +vmovdqa %ymm15, 704(%rdi) +vmovdqa %ymm7, 256(%rdi) +vmovdqa %ymm6, 608(%rdi) +vpxor 736(%rdi), %ymm2, %ymm8 +vpxor 288(%rdi), %ymm1, %ymm9 +vpxor 480(%rdi), %ymm5, %ymm10 +vpxor 32(%rdi), %ymm4, %ymm11 +vpxor 384(%rdi), %ymm3, %ymm12 +vpsllq $28, %ymm8, %ymm13 +vpsllq $20, %ymm9, %ymm14 +vpsllq $3, %ymm10, %ymm15 +vpsllq $45, %ymm11, %ymm7 +vpsllq $61, %ymm12, %ymm6 +vpsrlq $36, %ymm8, %ymm8 +vpsrlq $44, %ymm9, %ymm9 +vpsrlq $61, %ymm10, %ymm10 +vpsrlq $19, %ymm11, %ymm11 +vpsrlq $3, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 480(%rdi) +vmovdqa %ymm14, 32(%rdi) +vmovdqa %ymm15, 384(%rdi) +vmovdqa %ymm7, 736(%rdi) +vmovdqa %ymm6, 288(%rdi) +vpxor 512(%rdi), %ymm4, %ymm8 +vpxor 64(%rdi), %ymm3, %ymm9 +vpxor 416(%rdi), %ymm2, %ymm10 +vpxor 768(%rdi), %ymm1, %ymm11 +vpxor 160(%rdi), %ymm5, %ymm12 +vpsllq $1, %ymm8, %ymm13 +vpsllq $6, %ymm9, %ymm14 +vpsllq $25, %ymm10, %ymm15 +#vpsllq $8, %ymm11, %ymm7 +vpsllq $18, %ymm12, %ymm6 +vpsrlq $63, %ymm8, %ymm8 +vpsrlq $58, %ymm9, %ymm9 +vpsrlq $39, %ymm10, %ymm10 +#vpsrlq $56, %ymm11, %ymm11 +vpsrlq $46, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +#vpor %ymm7, %ymm11, %ymm11 +vpshufb %ymm0, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 160(%rdi) +vmovdqa %ymm14, 512(%rdi) +vmovdqa %ymm15, 64(%rdi) +vmovdqa %ymm7, 416(%rdi) +vmovdqa %ymm6, 768(%rdi) +vpxor 448(%rdi), %ymm1, %ymm8 +vpxor 640(%rdi), %ymm5, %ymm9 +vpxor 192(%rdi), %ymm4, %ymm10 +vpxor 544(%rdi), %ymm3, %ymm11 +vpxor 96(%rdi), %ymm2, %ymm12 +vpsllq $27, %ymm8, %ymm13 +vpsllq $36, %ymm9, %ymm14 +vpsllq $10, %ymm10, %ymm15 +vpsllq $15, %ymm11, %ymm7 +#vpsllq $56, %ymm12, %ymm6 +vpsrlq $37, %ymm8, %ymm8 +vpsrlq $28, %ymm9, %ymm9 +vpsrlq $54, %ymm10, %ymm10 +vpsrlq $49, %ymm11, %ymm11 +#vpsrlq $8, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +#vpor %ymm6, %ymm12, %ymm12 +vpshufb rho56(%rip), %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 640(%rdi) +vmovdqa %ymm14, 192(%rdi) +vmovdqa %ymm15, 544(%rdi) +vmovdqa %ymm7, 96(%rdi) +vmovdqa %ymm6, 448(%rdi) +vpxor 224(%rdi), %ymm3, %ymm8 +vpxor 576(%rdi), %ymm2, %ymm9 +vpxor 128(%rdi), %ymm1, %ymm10 +vpxor 320(%rdi), %ymm5, %ymm11 +vpxor 672(%rdi), %ymm4, %ymm12 +vpsllq $62, %ymm8, %ymm13 +vpsllq $55, %ymm9, %ymm14 +vpsllq $39, %ymm10, %ymm15 +vpsllq $41, %ymm11, %ymm7 +vpsllq $2, %ymm12, %ymm6 +vpsrlq $2, %ymm8, %ymm8 +vpsrlq $9, %ymm9, %ymm9 +vpsrlq $25, %ymm10, %ymm10 +vpsrlq $23, %ymm11, %ymm11 +vpsrlq $62, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 320(%rdi) +vmovdqa %ymm14, 672(%rdi) +vmovdqa %ymm15, 224(%rdi) +vmovdqa %ymm7, 576(%rdi) +vmovdqa %ymm6, 128(%rdi) +vmovdqa 0(%rdi), %ymm8 +vmovdqa 32(%rdi), %ymm9 +vmovdqa 64(%rdi), %ymm10 +vmovdqa 96(%rdi), %ymm11 +vmovdqa 128(%rdi), %ymm12 +vpxor 160(%rdi), %ymm8, %ymm8 +vpxor 192(%rdi), %ymm9, %ymm9 +vpxor 224(%rdi), %ymm10, %ymm10 +vpxor 256(%rdi), %ymm11, %ymm11 +vpxor 288(%rdi), %ymm12, %ymm12 +vpxor 320(%rdi), %ymm8, %ymm8 +vpxor 352(%rdi), %ymm9, %ymm9 +vpxor 384(%rdi), %ymm10, %ymm10 +vpxor 416(%rdi), %ymm11, %ymm11 +vpxor 448(%rdi), %ymm12, %ymm12 +vpxor 480(%rdi), %ymm8, %ymm8 +vpxor 512(%rdi), %ymm9, %ymm9 +vpxor 544(%rdi), %ymm10, %ymm10 +vpxor 576(%rdi), %ymm11, %ymm11 +vpxor 608(%rdi), %ymm12, %ymm12 +vpxor 640(%rdi), %ymm8, %ymm8 +vpxor 672(%rdi), %ymm9, %ymm9 +vpxor 704(%rdi), %ymm10, %ymm10 +vpxor 736(%rdi), %ymm11, %ymm11 +vpxor 768(%rdi), %ymm12, %ymm12 +vpsllq $1, %ymm9, %ymm13 +vpsllq $1, %ymm10, %ymm14 +vpsllq $1, %ymm11, %ymm15 +vpsllq $1, %ymm12, %ymm7 +vpsllq $1, %ymm8, %ymm6 +vpsrlq $63, %ymm9, %ymm5 +vpsrlq $63, %ymm10, %ymm4 +vpsrlq $63, %ymm11, %ymm3 +vpsrlq $63, %ymm12, %ymm2 +vpsrlq $63, %ymm8, %ymm1 +vpor %ymm13, %ymm5, %ymm5 +vpor %ymm14, %ymm4, %ymm4 +vpor %ymm15, %ymm3, %ymm3 +vpor %ymm7, %ymm2, %ymm2 +vpor %ymm6, %ymm1, %ymm1 +vpxor %ymm5, %ymm12, %ymm5 +vpxor %ymm4, %ymm8, %ymm4 +vpxor %ymm3, %ymm9, %ymm3 +vpxor %ymm2, %ymm10, %ymm2 +vpxor %ymm1, %ymm11, %ymm1 +vpxor 0(%rdi), %ymm5, %ymm8 +vpxor 32(%rdi), %ymm4, %ymm9 +vpxor 64(%rdi), %ymm3, %ymm10 +vpxor 96(%rdi), %ymm2, %ymm11 +vpxor 128(%rdi), %ymm1, %ymm12 +vpsllq $44, %ymm9, %ymm14 +vpsllq $43, %ymm10, %ymm15 +vpsllq $21, %ymm11, %ymm7 +vpsllq $14, %ymm12, %ymm6 +vpsrlq $20, %ymm9, %ymm9 +vpsrlq $21, %ymm10, %ymm10 +vpsrlq $43, %ymm11, %ymm11 +vpsrlq $50, %ymm12, %ymm12 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vpbroadcastq 24(%rsi), %ymm8 +vpxor %ymm8, %ymm13, %ymm13 +vmovdqa %ymm13, 0(%rdi) +vmovdqa %ymm14, 32(%rdi) +vmovdqa %ymm15, 64(%rdi) +vmovdqa %ymm7, 96(%rdi) +vmovdqa %ymm6, 128(%rdi) +vpxor 256(%rdi), %ymm2, %ymm8 +vpxor 288(%rdi), %ymm1, %ymm9 +vpxor 160(%rdi), %ymm5, %ymm10 +vpxor 192(%rdi), %ymm4, %ymm11 +vpxor 224(%rdi), %ymm3, %ymm12 +vpsllq $28, %ymm8, %ymm13 +vpsllq $20, %ymm9, %ymm14 +vpsllq $3, %ymm10, %ymm15 +vpsllq $45, %ymm11, %ymm7 +vpsllq $61, %ymm12, %ymm6 +vpsrlq $36, %ymm8, %ymm8 +vpsrlq $44, %ymm9, %ymm9 +vpsrlq $61, %ymm10, %ymm10 +vpsrlq $19, %ymm11, %ymm11 +vpsrlq $3, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 160(%rdi) +vmovdqa %ymm14, 192(%rdi) +vmovdqa %ymm15, 224(%rdi) +vmovdqa %ymm7, 256(%rdi) +vmovdqa %ymm6, 288(%rdi) +vpxor 352(%rdi), %ymm4, %ymm8 +vpxor 384(%rdi), %ymm3, %ymm9 +vpxor 416(%rdi), %ymm2, %ymm10 +vpxor 448(%rdi), %ymm1, %ymm11 +vpxor 320(%rdi), %ymm5, %ymm12 +vpsllq $1, %ymm8, %ymm13 +vpsllq $6, %ymm9, %ymm14 +vpsllq $25, %ymm10, %ymm15 +#vpsllq $8, %ymm11, %ymm7 +vpsllq $18, %ymm12, %ymm6 +vpsrlq $63, %ymm8, %ymm8 +vpsrlq $58, %ymm9, %ymm9 +vpsrlq $39, %ymm10, %ymm10 +#vpsrlq $56, %ymm11, %ymm11 +vpsrlq $46, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +#vpor %ymm7, %ymm11, %ymm11 +vpshufb %ymm0, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 320(%rdi) +vmovdqa %ymm14, 352(%rdi) +vmovdqa %ymm15, 384(%rdi) +vmovdqa %ymm7, 416(%rdi) +vmovdqa %ymm6, 448(%rdi) +vpxor 608(%rdi), %ymm1, %ymm8 +vpxor 480(%rdi), %ymm5, %ymm9 +vpxor 512(%rdi), %ymm4, %ymm10 +vpxor 544(%rdi), %ymm3, %ymm11 +vpxor 576(%rdi), %ymm2, %ymm12 +vpsllq $27, %ymm8, %ymm13 +vpsllq $36, %ymm9, %ymm14 +vpsllq $10, %ymm10, %ymm15 +vpsllq $15, %ymm11, %ymm7 +#vpsllq $56, %ymm12, %ymm6 +vpsrlq $37, %ymm8, %ymm8 +vpsrlq $28, %ymm9, %ymm9 +vpsrlq $54, %ymm10, %ymm10 +vpsrlq $49, %ymm11, %ymm11 +#vpsrlq $8, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +#vpor %ymm6, %ymm12, %ymm12 +vpshufb rho56(%rip), %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 480(%rdi) +vmovdqa %ymm14, 512(%rdi) +vmovdqa %ymm15, 544(%rdi) +vmovdqa %ymm7, 576(%rdi) +vmovdqa %ymm6, 608(%rdi) +vpxor 704(%rdi), %ymm3, %ymm8 +vpxor 736(%rdi), %ymm2, %ymm9 +vpxor 768(%rdi), %ymm1, %ymm10 +vpxor 640(%rdi), %ymm5, %ymm11 +vpxor 672(%rdi), %ymm4, %ymm12 +vpsllq $62, %ymm8, %ymm13 +vpsllq $55, %ymm9, %ymm14 +vpsllq $39, %ymm10, %ymm15 +vpsllq $41, %ymm11, %ymm7 +vpsllq $2, %ymm12, %ymm6 +vpsrlq $2, %ymm8, %ymm8 +vpsrlq $9, %ymm9, %ymm9 +vpsrlq $25, %ymm10, %ymm10 +vpsrlq $23, %ymm11, %ymm11 +vpsrlq $62, %ymm12, %ymm12 +vpor %ymm13, %ymm8, %ymm8 +vpor %ymm14, %ymm9, %ymm9 +vpor %ymm15, %ymm10, %ymm10 +vpor %ymm7, %ymm11, %ymm11 +vpor %ymm6, %ymm12, %ymm12 +vpandn %ymm10, %ymm9, %ymm13 +vpandn %ymm11, %ymm10, %ymm14 +vpandn %ymm12, %ymm11, %ymm15 +vpandn %ymm8, %ymm12, %ymm7 +vpandn %ymm9, %ymm8, %ymm6 +vpxor %ymm8, %ymm13, %ymm13 +vpxor %ymm9, %ymm14, %ymm14 +vpxor %ymm10, %ymm15, %ymm15 +vpxor %ymm11, %ymm7, %ymm7 +vpxor %ymm12, %ymm6, %ymm6 +vmovdqa %ymm13, 640(%rdi) +vmovdqa %ymm14, 672(%rdi) +vmovdqa %ymm15, 704(%rdi) +vmovdqa %ymm7, 736(%rdi) +vmovdqa %ymm6, 768(%rdi) +addq $32, %rsi +subq $1, %rax +jnz looptop +ret + +.section .note.GNU-stack,"",@progbits diff --git a/src/crypto/pq/_d/avx2/fips202.c b/src/crypto/pq/_d/avx2/fips202.c new file mode 120000 index 0000000..da2fa42 --- /dev/null +++ b/src/crypto/pq/_d/avx2/fips202.c @@ -0,0 +1 @@ +../ref/fips202.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/fips202.h b/src/crypto/pq/_d/avx2/fips202.h new file mode 100644 index 0000000..72fb2c2 --- /dev/null +++ b/src/crypto/pq/_d/avx2/fips202.h @@ -0,0 +1,57 @@ +#ifndef FIPS202_H +#define FIPS202_H + +#include +#include + +#define SHAKE128_RATE 168 +#define SHAKE256_RATE 136 +#define SHA3_256_RATE 136 +#define SHA3_512_RATE 72 + +#define FIPS202_NAMESPACE(s) pqcrystals_dilithium_fips202_avx2_##s + +typedef struct { + uint64_t s[25]; + unsigned int pos; +} keccak_state; + +#define KeccakF_RoundConstants FIPS202_NAMESPACE(KeccakF_RoundConstants) +extern const uint64_t KeccakF_RoundConstants[]; + +#define shake128_init FIPS202_NAMESPACE(shake128_init) +void shake128_init(keccak_state *state); +#define shake128_absorb FIPS202_NAMESPACE(shake128_absorb) +void shake128_absorb(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake128_finalize FIPS202_NAMESPACE(shake128_finalize) +void shake128_finalize(keccak_state *state); +#define shake128_squeeze FIPS202_NAMESPACE(shake128_squeeze) +void shake128_squeeze(uint8_t *out, size_t outlen, keccak_state *state); +#define shake128_absorb_once FIPS202_NAMESPACE(shake128_absorb_once) +void shake128_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake128_squeezeblocks FIPS202_NAMESPACE(shake128_squeezeblocks) +void shake128_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state); + +#define shake256_init FIPS202_NAMESPACE(shake256_init) +void shake256_init(keccak_state *state); +#define shake256_absorb FIPS202_NAMESPACE(shake256_absorb) +void shake256_absorb(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake256_finalize FIPS202_NAMESPACE(shake256_finalize) +void shake256_finalize(keccak_state *state); +#define shake256_squeeze FIPS202_NAMESPACE(shake256_squeeze) +void shake256_squeeze(uint8_t *out, size_t outlen, keccak_state *state); +#define shake256_absorb_once FIPS202_NAMESPACE(shake256_absorb_once) +void shake256_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake256_squeezeblocks FIPS202_NAMESPACE(shake256_squeezeblocks) +void shake256_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state); + +#define shake128 FIPS202_NAMESPACE(shake128) +void shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen); +#define shake256 FIPS202_NAMESPACE(shake256) +void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen); +#define sha3_256 FIPS202_NAMESPACE(sha3_256) +void sha3_256(uint8_t h[32], const uint8_t *in, size_t inlen); +#define sha3_512 FIPS202_NAMESPACE(sha3_512) +void sha3_512(uint8_t h[64], const uint8_t *in, size_t inlen); + +#endif diff --git a/src/crypto/pq/_d/avx2/fips202x4.c b/src/crypto/pq/_d/avx2/fips202x4.c new file mode 100644 index 0000000..2ffa691 --- /dev/null +++ b/src/crypto/pq/_d/avx2/fips202x4.c @@ -0,0 +1,196 @@ +#include +#include +#include +#include +#include "fips202.h" +#include "fips202x4.h" + +static void keccakx4_absorb_once(__m256i s[25], + unsigned int r, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen, + uint8_t p) +{ + size_t i; + uint64_t pos = 0; + __m256i t, idx; + + for(i = 0; i < 25; ++i) + s[i] = _mm256_setzero_si256(); + + idx = _mm256_set_epi64x((long long)in3, (long long)in2, (long long)in1, (long long)in0); + while(inlen >= r) { + for(i = 0; i < r/8; ++i) { + t = _mm256_i64gather_epi64((long long *)pos, idx, 1); + s[i] = _mm256_xor_si256(s[i], t); + pos += 8; + } + inlen -= r; + + f1600x4(s, KeccakF_RoundConstants); + } + + for(i = 0; i < inlen/8; ++i) { + t = _mm256_i64gather_epi64((long long *)pos, idx, 1); + s[i] = _mm256_xor_si256(s[i], t); + pos += 8; + } + inlen -= 8*i; + + if(inlen) { + t = _mm256_i64gather_epi64((long long *)pos, idx, 1); + idx = _mm256_set1_epi64x((1ULL << (8*inlen)) - 1); + t = _mm256_and_si256(t, idx); + s[i] = _mm256_xor_si256(s[i], t); + } + + t = _mm256_set1_epi64x((uint64_t)p << 8*inlen); + s[i] = _mm256_xor_si256(s[i], t); + t = _mm256_set1_epi64x(1ULL << 63); + s[r/8 - 1] = _mm256_xor_si256(s[r/8 - 1], t); +} + +static void keccakx4_squeezeblocks(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t nblocks, + unsigned int r, + __m256i s[25]) +{ + unsigned int i; + __m128d t; + + while(nblocks > 0) { + f1600x4(s, KeccakF_RoundConstants); + for(i=0; i < r/8; ++i) { + t = _mm_castsi128_pd(_mm256_castsi256_si128(s[i])); + _mm_storel_pd((__attribute__((__may_alias__)) double *)&out0[8*i], t); + _mm_storeh_pd((__attribute__((__may_alias__)) double *)&out1[8*i], t); + t = _mm_castsi128_pd(_mm256_extracti128_si256(s[i],1)); + _mm_storel_pd((__attribute__((__may_alias__)) double *)&out2[8*i], t); + _mm_storeh_pd((__attribute__((__may_alias__)) double *)&out3[8*i], t); + } + + out0 += r; + out1 += r; + out2 += r; + out3 += r; + --nblocks; + } +} + +void shake128x4_absorb_once(keccakx4_state *state, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen) +{ + keccakx4_absorb_once(state->s, SHAKE128_RATE, in0, in1, in2, in3, inlen, 0x1F); +} + +void shake128x4_squeezeblocks(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t nblocks, + keccakx4_state *state) +{ + keccakx4_squeezeblocks(out0, out1, out2, out3, nblocks, SHAKE128_RATE, state->s); +} + +void shake256x4_absorb_once(keccakx4_state *state, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen) +{ + keccakx4_absorb_once(state->s, SHAKE256_RATE, in0, in1, in2, in3, inlen, 0x1F); +} + +void shake256x4_squeezeblocks(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t nblocks, + keccakx4_state *state) +{ + keccakx4_squeezeblocks(out0, out1, out2, out3, nblocks, SHAKE256_RATE, state->s); +} + +void shake128x4(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t outlen, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen) +{ + unsigned int i; + size_t nblocks = outlen/SHAKE128_RATE; + uint8_t t[4][SHAKE128_RATE]; + keccakx4_state state; + + shake128x4_absorb_once(&state, in0, in1, in2, in3, inlen); + shake128x4_squeezeblocks(out0, out1, out2, out3, nblocks, &state); + + out0 += nblocks*SHAKE128_RATE; + out1 += nblocks*SHAKE128_RATE; + out2 += nblocks*SHAKE128_RATE; + out3 += nblocks*SHAKE128_RATE; + outlen -= nblocks*SHAKE128_RATE; + + if(outlen) { + shake128x4_squeezeblocks(t[0], t[1], t[2], t[3], 1, &state); + for(i = 0; i < outlen; ++i) { + out0[i] = t[0][i]; + out1[i] = t[1][i]; + out2[i] = t[2][i]; + out3[i] = t[3][i]; + } + } +} + +void shake256x4(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t outlen, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen) +{ + unsigned int i; + size_t nblocks = outlen/SHAKE256_RATE; + uint8_t t[4][SHAKE256_RATE]; + keccakx4_state state; + + shake256x4_absorb_once(&state, in0, in1, in2, in3, inlen); + shake256x4_squeezeblocks(out0, out1, out2, out3, nblocks, &state); + + out0 += nblocks*SHAKE256_RATE; + out1 += nblocks*SHAKE256_RATE; + out2 += nblocks*SHAKE256_RATE; + out3 += nblocks*SHAKE256_RATE; + outlen -= nblocks*SHAKE256_RATE; + + if(outlen) { + shake256x4_squeezeblocks(t[0], t[1], t[2], t[3], 1, &state); + for(i = 0; i < outlen; ++i) { + out0[i] = t[0][i]; + out1[i] = t[1][i]; + out2[i] = t[2][i]; + out3[i] = t[3][i]; + } + } +} diff --git a/src/crypto/pq/_d/avx2/fips202x4.h b/src/crypto/pq/_d/avx2/fips202x4.h new file mode 100644 index 0000000..3288a3a --- /dev/null +++ b/src/crypto/pq/_d/avx2/fips202x4.h @@ -0,0 +1,91 @@ +#ifndef FIPS202X4_H +#define FIPS202X4_H + +#define FIPS202X4_NAMESPACE(s) pqcrystals_dilithium_fips202x4_avx2_##s + +#ifdef __ASSEMBLER__ +/* The C ABI on MacOS exports all symbols with a leading + * underscore. This means that any symbols we refer to from + * C files (functions) can't be found, and all symbols we + * refer to from ASM also can't be found. + * + * This define helps us get around this + */ +#if defined(__WIN32__) || defined(__APPLE__) +#define decorate(s) _##s +#define _cdecl(s) decorate(s) +#define cdecl(s) _cdecl(FIPS202X4_NAMESPACE(##s)) +#else +#define cdecl(s) FIPS202X4_NAMESPACE(##s) +#endif + +#else +#include +#include +#include + +typedef struct { + __m256i s[25]; +} keccakx4_state; + +#define f1600x4 FIPS202X4_NAMESPACE(f1600x4) +void f1600x4(__m256i *s, const uint64_t *rc); + +#define shake128x4_absorb_once FIPS202X4_NAMESPACE(shake128x4_absorb_once) +void shake128x4_absorb_once(keccakx4_state *state, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen); + +#define shake128x4_squeezeblocks FIPS202X4_NAMESPACE(shake128x4_squeezeblocks) +void shake128x4_squeezeblocks(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t nblocks, + keccakx4_state *state); + +#define shake256x4_absorb_once FIPS202X4_NAMESPACE(shake256x4_absorb_once) +void shake256x4_absorb_once(keccakx4_state *state, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen); + +#define shake256x4_squeezeblocks FIPS202X4_NAMESPACE(shake256x4_squeezeblocks) +void shake256x4_squeezeblocks(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t nblocks, + keccakx4_state *state); + +#define shake128x4 FIPS202X4_NAMESPACE(shake128x4) +void shake128x4(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t outlen, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen); + +#define shake256x4 FIPS202X4_NAMESPACE(shake256x4) +void shake256x4(uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t outlen, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen); + +#endif +#endif diff --git a/src/crypto/pq/_d/avx2/invntt.S b/src/crypto/pq/_d/avx2/invntt.S new file mode 100644 index 0000000..d40ca13 --- /dev/null +++ b/src/crypto/pq/_d/avx2/invntt.S @@ -0,0 +1,240 @@ +#include "consts.h" +.include "shuffle.inc" + +.macro butterfly l,h,zl0=1,zl1=1,zh0=2,zh1=2 +vpsubd %ymm\l,%ymm\h,%ymm12 +vpaddd %ymm\h,%ymm\l,%ymm\l + +vpmuldq %ymm\zl0,%ymm12,%ymm13 +vmovshdup %ymm12,%ymm\h +vpmuldq %ymm\zl1,%ymm\h,%ymm14 + +vpmuldq %ymm\zh0,%ymm12,%ymm12 +vpmuldq %ymm\zh1,%ymm\h,%ymm\h + +vpmuldq %ymm0,%ymm13,%ymm13 +vpmuldq %ymm0,%ymm14,%ymm14 + +vpsubd %ymm13,%ymm12,%ymm12 +vpsubd %ymm14,%ymm\h,%ymm\h + +vmovshdup %ymm12,%ymm12 +vpblendd $0xAA,%ymm\h,%ymm12,%ymm\h +.endm + +.macro levels0t5 off +vmovdqa 256*\off+ 0(%rdi),%ymm4 +vmovdqa 256*\off+ 32(%rdi),%ymm5 +vmovdqa 256*\off+ 64(%rdi),%ymm6 +vmovdqa 256*\off+ 96(%rdi),%ymm7 +vmovdqa 256*\off+128(%rdi),%ymm8 +vmovdqa 256*\off+160(%rdi),%ymm9 +vmovdqa 256*\off+192(%rdi),%ymm10 +vmovdqa 256*\off+224(%rdi),%ymm11 + +/* level 0 */ +vpermq $0x1B,(_ZETAS_QINV+296-8*\off-8)*4(%rsi),%ymm3 +vpermq $0x1B,(_ZETAS+296-8*\off-8)*4(%rsi),%ymm15 +vmovshdup %ymm3,%ymm1 +vmovshdup %ymm15,%ymm2 +butterfly 4,5,1,3,2,15 + +vpermq $0x1B,(_ZETAS_QINV+296-8*\off-40)*4(%rsi),%ymm3 +vpermq $0x1B,(_ZETAS+296-8*\off-40)*4(%rsi),%ymm15 +vmovshdup %ymm3,%ymm1 +vmovshdup %ymm15,%ymm2 +butterfly 6,7,1,3,2,15 + +vpermq $0x1B,(_ZETAS_QINV+296-8*\off-72)*4(%rsi),%ymm3 +vpermq $0x1B,(_ZETAS+296-8*\off-72)*4(%rsi),%ymm15 +vmovshdup %ymm3,%ymm1 +vmovshdup %ymm15,%ymm2 +butterfly 8,9,1,3,2,15 + +vpermq $0x1B,(_ZETAS_QINV+296-8*\off-104)*4(%rsi),%ymm3 +vpermq $0x1B,(_ZETAS+296-8*\off-104)*4(%rsi),%ymm15 +vmovshdup %ymm3,%ymm1 +vmovshdup %ymm15,%ymm2 +butterfly 10,11,1,3,2,15 + +/* level 1 */ +vpermq $0x1B,(_ZETAS_QINV+168-8*\off-8)*4(%rsi),%ymm3 +vpermq $0x1B,(_ZETAS+168-8*\off-8)*4(%rsi),%ymm15 +vmovshdup %ymm3,%ymm1 +vmovshdup %ymm15,%ymm2 +butterfly 4,6,1,3,2,15 +butterfly 5,7,1,3,2,15 + +vpermq $0x1B,(_ZETAS_QINV+168-8*\off-40)*4(%rsi),%ymm3 +vpermq $0x1B,(_ZETAS+168-8*\off-40)*4(%rsi),%ymm15 +vmovshdup %ymm3,%ymm1 +vmovshdup %ymm15,%ymm2 +butterfly 8,10,1,3,2,15 +butterfly 9,11,1,3,2,15 + +/* level 2 */ +vpermq $0x1B,(_ZETAS_QINV+104-8*\off-8)*4(%rsi),%ymm3 +vpermq $0x1B,(_ZETAS+104-8*\off-8)*4(%rsi),%ymm15 +vmovshdup %ymm3,%ymm1 +vmovshdup %ymm15,%ymm2 +butterfly 4,8,1,3,2,15 +butterfly 5,9,1,3,2,15 +butterfly 6,10,1,3,2,15 +butterfly 7,11,1,3,2,15 + +/* level 3 */ +shuffle2 4,5,3,5 +shuffle2 6,7,4,7 +shuffle2 8,9,6,9 +shuffle2 10,11,8,11 + +vpermq $0x1B,(_ZETAS_QINV+72-8*\off-8)*4(%rsi),%ymm1 +vpermq $0x1B,(_ZETAS+72-8*\off-8)*4(%rsi),%ymm2 +butterfly 3,5 +butterfly 4,7 +butterfly 6,9 +butterfly 8,11 + +/* level 4 */ +shuffle4 3,4,10,4 +shuffle4 6,8,3,8 +shuffle4 5,7,6,7 +shuffle4 9,11,5,11 + +vpermq $0x1B,(_ZETAS_QINV+40-8*\off-8)*4(%rsi),%ymm1 +vpermq $0x1B,(_ZETAS+40-8*\off-8)*4(%rsi),%ymm2 +butterfly 10,4 +butterfly 3,8 +butterfly 6,7 +butterfly 5,11 + +/* level 5 */ +shuffle8 10,3,9,3 +shuffle8 6,5,10,5 +shuffle8 4,8,6,8 +shuffle8 7,11,4,11 + +vpbroadcastd (_ZETAS_QINV+7-\off)*4(%rsi),%ymm1 +vpbroadcastd (_ZETAS+7-\off)*4(%rsi),%ymm2 +butterfly 9,3 +butterfly 10,5 +butterfly 6,8 +butterfly 4,11 + +vmovdqa %ymm9,256*\off+ 0(%rdi) +vmovdqa %ymm10,256*\off+ 32(%rdi) +vmovdqa %ymm6,256*\off+ 64(%rdi) +vmovdqa %ymm4,256*\off+ 96(%rdi) +vmovdqa %ymm3,256*\off+128(%rdi) +vmovdqa %ymm5,256*\off+160(%rdi) +vmovdqa %ymm8,256*\off+192(%rdi) +vmovdqa %ymm11,256*\off+224(%rdi) +.endm + +.macro levels6t7 off +vmovdqa 0+32*\off(%rdi),%ymm4 +vmovdqa 128+32*\off(%rdi),%ymm5 +vmovdqa 256+32*\off(%rdi),%ymm6 +vmovdqa 384+32*\off(%rdi),%ymm7 +vmovdqa 512+32*\off(%rdi),%ymm8 +vmovdqa 640+32*\off(%rdi),%ymm9 +vmovdqa 768+32*\off(%rdi),%ymm10 +vmovdqa 896+32*\off(%rdi),%ymm11 + +/* level 6 */ +vpbroadcastd (_ZETAS_QINV+3)*4(%rsi),%ymm1 +vpbroadcastd (_ZETAS+3)*4(%rsi),%ymm2 +butterfly 4,6 +butterfly 5,7 + +vpbroadcastd (_ZETAS_QINV+2)*4(%rsi),%ymm1 +vpbroadcastd (_ZETAS+2)*4(%rsi),%ymm2 +butterfly 8,10 +butterfly 9,11 + +/* level 7 */ +vpbroadcastd (_ZETAS_QINV+0)*4(%rsi),%ymm1 +vpbroadcastd (_ZETAS+0)*4(%rsi),%ymm2 + +butterfly 4,8 +butterfly 5,9 +butterfly 6,10 +butterfly 7,11 + +vmovdqa %ymm8,512+32*\off(%rdi) +vmovdqa %ymm9,640+32*\off(%rdi) +vmovdqa %ymm10,768+32*\off(%rdi) +vmovdqa %ymm11,896+32*\off(%rdi) + +vmovdqa (_8XDIV_QINV)*4(%rsi),%ymm1 +vmovdqa (_8XDIV)*4(%rsi),%ymm2 +vpmuldq %ymm1,%ymm4,%ymm12 +vpmuldq %ymm1,%ymm5,%ymm13 +vmovshdup %ymm4,%ymm8 +vmovshdup %ymm5,%ymm9 +vpmuldq %ymm1,%ymm8,%ymm14 +vpmuldq %ymm1,%ymm9,%ymm15 +vpmuldq %ymm2,%ymm4,%ymm4 +vpmuldq %ymm2,%ymm5,%ymm5 +vpmuldq %ymm2,%ymm8,%ymm8 +vpmuldq %ymm2,%ymm9,%ymm9 +vpmuldq %ymm0,%ymm12,%ymm12 +vpmuldq %ymm0,%ymm13,%ymm13 +vpmuldq %ymm0,%ymm14,%ymm14 +vpmuldq %ymm0,%ymm15,%ymm15 +vpsubd %ymm12,%ymm4,%ymm4 +vpsubd %ymm13,%ymm5,%ymm5 +vpsubd %ymm14,%ymm8,%ymm8 +vpsubd %ymm15,%ymm9,%ymm9 +vmovshdup %ymm4,%ymm4 +vmovshdup %ymm5,%ymm5 +vpblendd $0xAA,%ymm8,%ymm4,%ymm4 +vpblendd $0xAA,%ymm9,%ymm5,%ymm5 + +vpmuldq %ymm1,%ymm6,%ymm12 +vpmuldq %ymm1,%ymm7,%ymm13 +vmovshdup %ymm6,%ymm8 +vmovshdup %ymm7,%ymm9 +vpmuldq %ymm1,%ymm8,%ymm14 +vpmuldq %ymm1,%ymm9,%ymm15 +vpmuldq %ymm2,%ymm6,%ymm6 +vpmuldq %ymm2,%ymm7,%ymm7 +vpmuldq %ymm2,%ymm8,%ymm8 +vpmuldq %ymm2,%ymm9,%ymm9 +vpmuldq %ymm0,%ymm12,%ymm12 +vpmuldq %ymm0,%ymm13,%ymm13 +vpmuldq %ymm0,%ymm14,%ymm14 +vpmuldq %ymm0,%ymm15,%ymm15 +vpsubd %ymm12,%ymm6,%ymm6 +vpsubd %ymm13,%ymm7,%ymm7 +vpsubd %ymm14,%ymm8,%ymm8 +vpsubd %ymm15,%ymm9,%ymm9 +vmovshdup %ymm6,%ymm6 +vmovshdup %ymm7,%ymm7 +vpblendd $0xAA,%ymm8,%ymm6,%ymm6 +vpblendd $0xAA,%ymm9,%ymm7,%ymm7 + +vmovdqa %ymm4, 0+32*\off(%rdi) +vmovdqa %ymm5,128+32*\off(%rdi) +vmovdqa %ymm6,256+32*\off(%rdi) +vmovdqa %ymm7,384+32*\off(%rdi) +.endm + +.text +.global cdecl(invntt_avx) +cdecl(invntt_avx): +vmovdqa _8XQ*4(%rsi),%ymm0 + +levels0t5 0 +levels0t5 1 +levels0t5 2 +levels0t5 3 + +levels6t7 0 +levels6t7 1 +levels6t7 2 +levels6t7 3 + +ret + +.section .note.GNU-stack,"",@progbits diff --git a/src/crypto/pq/_d/avx2/ntt.S b/src/crypto/pq/_d/avx2/ntt.S new file mode 100644 index 0000000..026f057 --- /dev/null +++ b/src/crypto/pq/_d/avx2/ntt.S @@ -0,0 +1,198 @@ +#include "consts.h" +.include "shuffle.inc" + +.macro butterfly l,h,zl0=1,zl1=1,zh0=2,zh1=2 +vpmuldq %ymm\zl0,%ymm\h,%ymm13 +vmovshdup %ymm\h,%ymm12 +vpmuldq %ymm\zl1,%ymm12,%ymm14 + +vpmuldq %ymm\zh0,%ymm\h,%ymm\h +vpmuldq %ymm\zh1,%ymm12,%ymm12 + +vpmuldq %ymm0,%ymm13,%ymm13 +vpmuldq %ymm0,%ymm14,%ymm14 + +vmovshdup %ymm\h,%ymm\h +vpblendd $0xAA,%ymm12,%ymm\h,%ymm\h + +vpsubd %ymm\h,%ymm\l,%ymm12 +vpaddd %ymm\h,%ymm\l,%ymm\l + +vmovshdup %ymm13,%ymm13 +vpblendd $0xAA,%ymm14,%ymm13,%ymm13 + +vpaddd %ymm13,%ymm12,%ymm\h +vpsubd %ymm13,%ymm\l,%ymm\l +.endm + +.macro levels0t1 off +/* level 0 */ +vpbroadcastd (_ZETAS_QINV+1)*4(%rsi),%ymm1 +vpbroadcastd (_ZETAS+1)*4(%rsi),%ymm2 + +vmovdqa 0+32*\off(%rdi),%ymm4 +vmovdqa 128+32*\off(%rdi),%ymm5 +vmovdqa 256+32*\off(%rdi),%ymm6 +vmovdqa 384+32*\off(%rdi),%ymm7 +vmovdqa 512+32*\off(%rdi),%ymm8 +vmovdqa 640+32*\off(%rdi),%ymm9 +vmovdqa 768+32*\off(%rdi),%ymm10 +vmovdqa 896+32*\off(%rdi),%ymm11 + +butterfly 4,8 +butterfly 5,9 +butterfly 6,10 +butterfly 7,11 + +/* level 1 */ +vpbroadcastd (_ZETAS_QINV+2)*4(%rsi),%ymm1 +vpbroadcastd (_ZETAS+2)*4(%rsi),%ymm2 +butterfly 4,6 +butterfly 5,7 + +vpbroadcastd (_ZETAS_QINV+3)*4(%rsi),%ymm1 +vpbroadcastd (_ZETAS+3)*4(%rsi),%ymm2 +butterfly 8,10 +butterfly 9,11 + +vmovdqa %ymm4, 0+32*\off(%rdi) +vmovdqa %ymm5,128+32*\off(%rdi) +vmovdqa %ymm6,256+32*\off(%rdi) +vmovdqa %ymm7,384+32*\off(%rdi) +vmovdqa %ymm8,512+32*\off(%rdi) +vmovdqa %ymm9,640+32*\off(%rdi) +vmovdqa %ymm10,768+32*\off(%rdi) +vmovdqa %ymm11,896+32*\off(%rdi) +.endm + +.macro levels2t7 off +/* level 2 */ +vmovdqa 256*\off+ 0(%rdi),%ymm4 +vmovdqa 256*\off+ 32(%rdi),%ymm5 +vmovdqa 256*\off+ 64(%rdi),%ymm6 +vmovdqa 256*\off+ 96(%rdi),%ymm7 +vmovdqa 256*\off+128(%rdi),%ymm8 +vmovdqa 256*\off+160(%rdi),%ymm9 +vmovdqa 256*\off+192(%rdi),%ymm10 +vmovdqa 256*\off+224(%rdi),%ymm11 + +vpbroadcastd (_ZETAS_QINV+4+\off)*4(%rsi),%ymm1 +vpbroadcastd (_ZETAS+4+\off)*4(%rsi),%ymm2 + +butterfly 4,8 +butterfly 5,9 +butterfly 6,10 +butterfly 7,11 + +shuffle8 4,8,3,8 +shuffle8 5,9,4,9 +shuffle8 6,10,5,10 +shuffle8 7,11,6,11 + +/* level 3 */ +vmovdqa (_ZETAS_QINV+8+8*\off)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+8+8*\off)*4(%rsi),%ymm2 + +butterfly 3,5 +butterfly 8,10 +butterfly 4,6 +butterfly 9,11 + +shuffle4 3,5,7,5 +shuffle4 8,10,3,10 +shuffle4 4,6,8,6 +shuffle4 9,11,4,11 + +/* level 4 */ +vmovdqa (_ZETAS_QINV+40+8*\off)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+40+8*\off)*4(%rsi),%ymm2 + +butterfly 7,8 +butterfly 5,6 +butterfly 3,4 +butterfly 10,11 + +shuffle2 7,8,9,8 +shuffle2 5,6,7,6 +shuffle2 3,4,5,4 +shuffle2 10,11,3,11 + +/* level 5 */ +vmovdqa (_ZETAS_QINV+72+8*\off)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+72+8*\off)*4(%rsi),%ymm2 +vpsrlq $32,%ymm1,%ymm10 +vmovshdup %ymm2,%ymm15 + +butterfly 9,5,1,10,2,15 +butterfly 8,4,1,10,2,15 +butterfly 7,3,1,10,2,15 +butterfly 6,11,1,10,2,15 + +/* level 6 */ +vmovdqa (_ZETAS_QINV+104+8*\off)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+104+8*\off)*4(%rsi),%ymm2 +vpsrlq $32,%ymm1,%ymm10 +vmovshdup %ymm2,%ymm15 +butterfly 9,7,1,10,2,15 +butterfly 8,6,1,10,2,15 + +vmovdqa (_ZETAS_QINV+104+8*\off+32)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+104+8*\off+32)*4(%rsi),%ymm2 +vpsrlq $32,%ymm1,%ymm10 +vmovshdup %ymm2,%ymm15 +butterfly 5,3,1,10,2,15 +butterfly 4,11,1,10,2,15 + +/* level 7 */ +vmovdqa (_ZETAS_QINV+168+8*\off)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+168+8*\off)*4(%rsi),%ymm2 +vpsrlq $32,%ymm1,%ymm10 +vmovshdup %ymm2,%ymm15 +butterfly 9,8,1,10,2,15 + +vmovdqa (_ZETAS_QINV+168+8*\off+32)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+168+8*\off+32)*4(%rsi),%ymm2 +vpsrlq $32,%ymm1,%ymm10 +vmovshdup %ymm2,%ymm15 +butterfly 7,6,1,10,2,15 + +vmovdqa (_ZETAS_QINV+168+8*\off+64)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+168+8*\off+64)*4(%rsi),%ymm2 +vpsrlq $32,%ymm1,%ymm10 +vmovshdup %ymm2,%ymm15 +butterfly 5,4,1,10,2,15 + +vmovdqa (_ZETAS_QINV+168+8*\off+96)*4(%rsi),%ymm1 +vmovdqa (_ZETAS+168+8*\off+96)*4(%rsi),%ymm2 +vpsrlq $32,%ymm1,%ymm10 +vmovshdup %ymm2,%ymm15 +butterfly 3,11,1,10,2,15 + +vmovdqa %ymm9,256*\off+ 0(%rdi) +vmovdqa %ymm8,256*\off+ 32(%rdi) +vmovdqa %ymm7,256*\off+ 64(%rdi) +vmovdqa %ymm6,256*\off+ 96(%rdi) +vmovdqa %ymm5,256*\off+128(%rdi) +vmovdqa %ymm4,256*\off+160(%rdi) +vmovdqa %ymm3,256*\off+192(%rdi) +vmovdqa %ymm11,256*\off+224(%rdi) +.endm + +.text +.global cdecl(ntt_avx) +cdecl(ntt_avx): +vmovdqa _8XQ*4(%rsi),%ymm0 + +levels0t1 0 +levels0t1 1 +levels0t1 2 +levels0t1 3 + +levels2t7 0 +levels2t7 1 +levels2t7 2 +levels2t7 3 + +ret + +.section .note.GNU-stack,"",@progbits diff --git a/src/crypto/pq/_d/avx2/ntt.h b/src/crypto/pq/_d/avx2/ntt.h new file mode 100644 index 0000000..0c4fbdd --- /dev/null +++ b/src/crypto/pq/_d/avx2/ntt.h @@ -0,0 +1,19 @@ +#ifndef NTT_H +#define NTT_H + +#include + +#define ntt_avx DILITHIUM_NAMESPACE(ntt_avx) +void ntt_avx(__m256i *a, const __m256i *qdata); +#define invntt_avx DILITHIUM_NAMESPACE(invntt_avx) +void invntt_avx(__m256i *a, const __m256i *qdata); + +#define nttunpack_avx DILITHIUM_NAMESPACE(nttunpack_avx) +void nttunpack_avx(__m256i *a); + +#define pointwise_avx DILITHIUM_NAMESPACE(pointwise_avx) +void pointwise_avx(__m256i *c, const __m256i *a, const __m256i *b, const __m256i *qdata); +#define pointwise_acc_avx DILITHIUM_NAMESPACE(pointwise_acc_avx) +void pointwise_acc_avx(__m256i *c, const __m256i *a, const __m256i *b, const __m256i *qdata); + +#endif diff --git a/src/crypto/pq/_d/avx2/packing.c b/src/crypto/pq/_d/avx2/packing.c new file mode 120000 index 0000000..e77fac9 --- /dev/null +++ b/src/crypto/pq/_d/avx2/packing.c @@ -0,0 +1 @@ +../ref/packing.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/packing.h b/src/crypto/pq/_d/avx2/packing.h new file mode 120000 index 0000000..d27a8e9 --- /dev/null +++ b/src/crypto/pq/_d/avx2/packing.h @@ -0,0 +1 @@ +../ref/packing.h \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/params.h b/src/crypto/pq/_d/avx2/params.h new file mode 120000 index 0000000..53133cc --- /dev/null +++ b/src/crypto/pq/_d/avx2/params.h @@ -0,0 +1 @@ +../ref/params.h \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/pointwise.S b/src/crypto/pq/_d/avx2/pointwise.S new file mode 100644 index 0000000..6b687c7 --- /dev/null +++ b/src/crypto/pq/_d/avx2/pointwise.S @@ -0,0 +1,213 @@ +#include "params.h" +#include "consts.h" + +.text +.global cdecl(pointwise_avx) +cdecl(pointwise_avx): +#consts +vmovdqa _8XQINV*4(%rcx),%ymm0 +vmovdqa _8XQ*4(%rcx),%ymm1 + +xor %eax,%eax +_looptop1: +#load +vmovdqa (%rsi),%ymm2 +vmovdqa 32(%rsi),%ymm4 +vmovdqa 64(%rsi),%ymm6 +vmovdqa (%rdx),%ymm10 +vmovdqa 32(%rdx),%ymm12 +vmovdqa 64(%rdx),%ymm14 +vpsrlq $32,%ymm2,%ymm3 +vpsrlq $32,%ymm4,%ymm5 +vmovshdup %ymm6,%ymm7 +vpsrlq $32,%ymm10,%ymm11 +vpsrlq $32,%ymm12,%ymm13 +vmovshdup %ymm14,%ymm15 + +#mul +vpmuldq %ymm2,%ymm10,%ymm2 +vpmuldq %ymm3,%ymm11,%ymm3 +vpmuldq %ymm4,%ymm12,%ymm4 +vpmuldq %ymm5,%ymm13,%ymm5 +vpmuldq %ymm6,%ymm14,%ymm6 +vpmuldq %ymm7,%ymm15,%ymm7 + +#reduce +vpmuldq %ymm0,%ymm2,%ymm10 +vpmuldq %ymm0,%ymm3,%ymm11 +vpmuldq %ymm0,%ymm4,%ymm12 +vpmuldq %ymm0,%ymm5,%ymm13 +vpmuldq %ymm0,%ymm6,%ymm14 +vpmuldq %ymm0,%ymm7,%ymm15 +vpmuldq %ymm1,%ymm10,%ymm10 +vpmuldq %ymm1,%ymm11,%ymm11 +vpmuldq %ymm1,%ymm12,%ymm12 +vpmuldq %ymm1,%ymm13,%ymm13 +vpmuldq %ymm1,%ymm14,%ymm14 +vpmuldq %ymm1,%ymm15,%ymm15 +vpsubq %ymm10,%ymm2,%ymm2 +vpsubq %ymm11,%ymm3,%ymm3 +vpsubq %ymm12,%ymm4,%ymm4 +vpsubq %ymm13,%ymm5,%ymm5 +vpsubq %ymm14,%ymm6,%ymm6 +vpsubq %ymm15,%ymm7,%ymm7 +vpsrlq $32,%ymm2,%ymm2 +vpsrlq $32,%ymm4,%ymm4 +vmovshdup %ymm6,%ymm6 + +#store +vpblendd $0xAA,%ymm3,%ymm2,%ymm2 +vpblendd $0xAA,%ymm5,%ymm4,%ymm4 +vpblendd $0xAA,%ymm7,%ymm6,%ymm6 +vmovdqa %ymm2,(%rdi) +vmovdqa %ymm4,32(%rdi) +vmovdqa %ymm6,64(%rdi) + +add $96,%rdi +add $96,%rsi +add $96,%rdx +add $1,%eax +cmp $10,%eax +jb _looptop1 + +vmovdqa (%rsi),%ymm2 +vmovdqa 32(%rsi),%ymm4 +vmovdqa (%rdx),%ymm10 +vmovdqa 32(%rdx),%ymm12 +vpsrlq $32,%ymm2,%ymm3 +vpsrlq $32,%ymm4,%ymm5 +vmovshdup %ymm10,%ymm11 +vmovshdup %ymm12,%ymm13 + +#mul +vpmuldq %ymm2,%ymm10,%ymm2 +vpmuldq %ymm3,%ymm11,%ymm3 +vpmuldq %ymm4,%ymm12,%ymm4 +vpmuldq %ymm5,%ymm13,%ymm5 + +#reduce +vpmuldq %ymm0,%ymm2,%ymm10 +vpmuldq %ymm0,%ymm3,%ymm11 +vpmuldq %ymm0,%ymm4,%ymm12 +vpmuldq %ymm0,%ymm5,%ymm13 +vpmuldq %ymm1,%ymm10,%ymm10 +vpmuldq %ymm1,%ymm11,%ymm11 +vpmuldq %ymm1,%ymm12,%ymm12 +vpmuldq %ymm1,%ymm13,%ymm13 +vpsubq %ymm10,%ymm2,%ymm2 +vpsubq %ymm11,%ymm3,%ymm3 +vpsubq %ymm12,%ymm4,%ymm4 +vpsubq %ymm13,%ymm5,%ymm5 +vpsrlq $32,%ymm2,%ymm2 +vmovshdup %ymm4,%ymm4 + +#store +vpblendd $0x55,%ymm2,%ymm3,%ymm2 +vpblendd $0x55,%ymm4,%ymm5,%ymm4 +vmovdqa %ymm2,(%rdi) +vmovdqa %ymm4,32(%rdi) + +ret + +.macro pointwise off +#load +vmovdqa \off(%rsi),%ymm6 +vmovdqa \off+32(%rsi),%ymm8 +vmovdqa \off(%rdx),%ymm10 +vmovdqa \off+32(%rdx),%ymm12 +vpsrlq $32,%ymm6,%ymm7 +vpsrlq $32,%ymm8,%ymm9 +vmovshdup %ymm10,%ymm11 +vmovshdup %ymm12,%ymm13 + +#mul +vpmuldq %ymm6,%ymm10,%ymm6 +vpmuldq %ymm7,%ymm11,%ymm7 +vpmuldq %ymm8,%ymm12,%ymm8 +vpmuldq %ymm9,%ymm13,%ymm9 +.endm + +.macro acc +vpaddq %ymm6,%ymm2,%ymm2 +vpaddq %ymm7,%ymm3,%ymm3 +vpaddq %ymm8,%ymm4,%ymm4 +vpaddq %ymm9,%ymm5,%ymm5 +.endm + +.global cdecl(pointwise_acc_avx) +cdecl(pointwise_acc_avx): +#consts +vmovdqa _8XQINV*4(%rcx),%ymm0 +vmovdqa _8XQ*4(%rcx),%ymm1 + +xor %eax,%eax +_looptop2: +pointwise 0 + +#mov +vmovdqa %ymm6,%ymm2 +vmovdqa %ymm7,%ymm3 +vmovdqa %ymm8,%ymm4 +vmovdqa %ymm9,%ymm5 + +pointwise 1024 +acc + +#if L >= 3 +pointwise 2048 +acc +#endif + +#if L >= 4 +pointwise 3072 +acc +#endif + +#if L >= 5 +pointwise 4096 +acc +#endif + +#if L >= 6 +pointwise 5120 +acc +#endif + +#if L >= 7 +pointwise 6144 +acc +#endif + +#reduce +vpmuldq %ymm0,%ymm2,%ymm6 +vpmuldq %ymm0,%ymm3,%ymm7 +vpmuldq %ymm0,%ymm4,%ymm8 +vpmuldq %ymm0,%ymm5,%ymm9 +vpmuldq %ymm1,%ymm6,%ymm6 +vpmuldq %ymm1,%ymm7,%ymm7 +vpmuldq %ymm1,%ymm8,%ymm8 +vpmuldq %ymm1,%ymm9,%ymm9 +vpsubq %ymm6,%ymm2,%ymm2 +vpsubq %ymm7,%ymm3,%ymm3 +vpsubq %ymm8,%ymm4,%ymm4 +vpsubq %ymm9,%ymm5,%ymm5 +vpsrlq $32,%ymm2,%ymm2 +vmovshdup %ymm4,%ymm4 + +#store +vpblendd $0xAA,%ymm3,%ymm2,%ymm2 +vpblendd $0xAA,%ymm5,%ymm4,%ymm4 + +vmovdqa %ymm2,(%rdi) +vmovdqa %ymm4,32(%rdi) + +add $64,%rsi +add $64,%rdx +add $64,%rdi +add $1,%eax +cmp $16,%eax +jb _looptop2 + +ret + +.section .note.GNU-stack,"",@progbits diff --git a/src/crypto/pq/_d/avx2/poly.c b/src/crypto/pq/_d/avx2/poly.c new file mode 100644 index 0000000..340e91d --- /dev/null +++ b/src/crypto/pq/_d/avx2/poly.c @@ -0,0 +1,1128 @@ +#include +#include +#include +#include "align.h" +#include "params.h" +#include "poly.h" +#include "ntt.h" +#include "rounding.h" +#include "rejsample.h" +#include "consts.h" +#include "symmetric.h" +#include "fips202x4.h" + +#ifdef DBENCH +#include "test/cpucycles.h" +extern const uint64_t timing_overhead; +extern uint64_t *tred, *tadd, *tmul, *tround, *tsample, *tpack; +#define DBENCH_START() uint64_t time = cpucycles() +#define DBENCH_STOP(t) t += cpucycles() - time - timing_overhead +#else +#define DBENCH_START() +#define DBENCH_STOP(t) +#endif + +#define _mm256_blendv_epi32(a,b,mask) \ + _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(a), \ + _mm256_castsi256_ps(b), \ + _mm256_castsi256_ps(mask))) + +/************************************************* +* Name: poly_reduce +* +* Description: Inplace reduction of all coefficients of polynomial to +* representative in [-6283009,6283008]. Assumes input +* coefficients to be at most 2^31 - 2^22 - 1 in absolute value. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_reduce(poly *a) { + unsigned int i; + __m256i f,g; + const __m256i q = _mm256_load_si256(&qdata.vec[_8XQ/8]); + const __m256i off = _mm256_set1_epi32(1<<22); + DBENCH_START(); + + for(i = 0; i < N/8; i++) { + f = _mm256_load_si256(&a->vec[i]); + g = _mm256_add_epi32(f,off); + g = _mm256_srai_epi32(g,23); + g = _mm256_mullo_epi32(g,q); + f = _mm256_sub_epi32(f,g); + _mm256_store_si256(&a->vec[i],f); + } + + DBENCH_STOP(*tred); +} + +/************************************************* +* Name: poly_addq +* +* Description: For all coefficients of in/out polynomial add Q if +* coefficient is negative. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_caddq(poly *a) { + unsigned int i; + __m256i f,g; + const __m256i q = _mm256_load_si256(&qdata.vec[_8XQ/8]); + const __m256i zero = _mm256_setzero_si256(); + DBENCH_START(); + + for(i = 0; i < N/8; i++) { + f = _mm256_load_si256(&a->vec[i]); + g = _mm256_blendv_epi32(zero,q,f); + f = _mm256_add_epi32(f,g); + _mm256_store_si256(&a->vec[i],f); + } + + DBENCH_STOP(*tred); +} + +/************************************************* +* Name: poly_add +* +* Description: Add polynomials. No modular reduction is performed. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first summand +* - const poly *b: pointer to second summand +**************************************************/ +void poly_add(poly *c, const poly *a, const poly *b) { + unsigned int i; + __m256i f,g; + DBENCH_START(); + + for(i = 0; i < N/8; i++) { + f = _mm256_load_si256(&a->vec[i]); + g = _mm256_load_si256(&b->vec[i]); + f = _mm256_add_epi32(f,g); + _mm256_store_si256(&c->vec[i],f); + } + + DBENCH_STOP(*tadd); +} + +/************************************************* +* Name: poly_sub +* +* Description: Subtract polynomials. No modular reduction is +* performed. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first input polynomial +* - const poly *b: pointer to second input polynomial to be +* subtraced from first input polynomial +**************************************************/ +void poly_sub(poly *c, const poly *a, const poly *b) { + unsigned int i; + __m256i f,g; + DBENCH_START(); + + for(i = 0; i < N/8; i++) { + f = _mm256_load_si256(&a->vec[i]); + g = _mm256_load_si256(&b->vec[i]); + f = _mm256_sub_epi32(f,g); + _mm256_store_si256(&c->vec[i],f); + } + + DBENCH_STOP(*tadd); +} + +/************************************************* +* Name: poly_shiftl +* +* Description: Multiply polynomial by 2^D without modular reduction. Assumes +* input coefficients to be less than 2^{31-D} in absolute value. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_shiftl(poly *a) { + unsigned int i; + __m256i f; + DBENCH_START(); + + for(i = 0; i < N/8; i++) { + f = _mm256_load_si256(&a->vec[i]); + f = _mm256_slli_epi32(f,D); + _mm256_store_si256(&a->vec[i],f); + } + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_ntt +* +* Description: Inplace forward NTT. Coefficients can grow by up to +* 8*Q in absolute value. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_ntt(poly *a) { + DBENCH_START(); + + ntt_avx(a->vec, qdata.vec); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_invntt_tomont +* +* Description: Inplace inverse NTT and multiplication by 2^{32}. +* Input coefficients need to be less than Q in absolute +* value and output coefficients are again bounded by Q. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_invntt_tomont(poly *a) { + DBENCH_START(); + + invntt_avx(a->vec, qdata.vec); + + DBENCH_STOP(*tmul); +} + +void poly_nttunpack(poly *a) { + DBENCH_START(); + + nttunpack_avx(a->vec); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_pointwise_montgomery +* +* Description: Pointwise multiplication of polynomials in NTT domain +* representation and multiplication of resulting polynomial +* by 2^{-32}. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first input polynomial +* - const poly *b: pointer to second input polynomial +**************************************************/ +void poly_pointwise_montgomery(poly *c, const poly *a, const poly *b) { + DBENCH_START(); + + pointwise_avx(c->vec, a->vec, b->vec, qdata.vec); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_power2round +* +* Description: For all coefficients c of the input polynomial, +* compute c0, c1 such that c mod^+ Q = c1*2^D + c0 +* with -2^{D-1} < c0 <= 2^{D-1}. Assumes coefficients to be +* positive standard representatives. +* +* Arguments: - poly *a1: pointer to output polynomial with coefficients c1 +* - poly *a0: pointer to output polynomial with coefficients c0 +* - const poly *a: pointer to input polynomial +**************************************************/ +void poly_power2round(poly *a1, poly *a0, const poly *a) +{ + DBENCH_START(); + + power2round_avx(a1->vec, a0->vec, a->vec); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_decompose +* +* Description: For all coefficients c of the input polynomial, +* compute high and low bits c0, c1 such c mod^+ Q = c1*ALPHA + c0 +* with -ALPHA/2 < c0 <= ALPHA/2 except if c1 = (Q-1)/ALPHA where we +* set c1 = 0 and -ALPHA/2 <= c0 = c mod Q - Q < 0. +* Assumes coefficients to be positive standard representatives. +* +* Arguments: - poly *a1: pointer to output polynomial with coefficients c1 +* - poly *a0: pointer to output polynomial with coefficients c0 +* - const poly *a: pointer to input polynomial +**************************************************/ +void poly_decompose(poly *a1, poly *a0, const poly *a) +{ + DBENCH_START(); + + decompose_avx(a1->vec, a0->vec, a->vec); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_make_hint +* +* Description: Compute hint array. The coefficients of which are the +* indices of the coefficients of the input polynomial +* whose low bits overflow into the high bits. +* +* Arguments: - uint8_t *h: pointer to output hint array (preallocated of length N) +* - const poly *a0: pointer to low part of input polynomial +* - const poly *a1: pointer to high part of input polynomial +* +* Returns number of hints, i.e. length of hint array. +**************************************************/ +unsigned int poly_make_hint(uint8_t hint[N], const poly *a0, const poly *a1) +{ + unsigned int r; + DBENCH_START(); + + r = make_hint_avx(hint, a0->vec, a1->vec); + + DBENCH_STOP(*tround); + return r; +} + +/************************************************* +* Name: poly_use_hint +* +* Description: Use hint polynomial to correct the high bits of a polynomial. +* +* Arguments: - poly *b: pointer to output polynomial with corrected high bits +* - const poly *a: pointer to input polynomial +* - const poly *h: pointer to input hint polynomial +**************************************************/ +void poly_use_hint(poly *b, const poly *a, const poly *h) +{ + DBENCH_START(); + + use_hint_avx(b->vec, a->vec, h->vec); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_chknorm +* +* Description: Check infinity norm of polynomial against given bound. +* Assumes input polynomial to be reduced by poly_reduce(). +* +* Arguments: - const poly *a: pointer to polynomial +* - int32_t B: norm bound +* +* Returns 0 if norm is strictly smaller than B <= (Q-1)/8 and 1 otherwise. +**************************************************/ +int poly_chknorm(const poly *a, int32_t B) { + unsigned int i; + int r; + __m256i f,t; + const __m256i bound = _mm256_set1_epi32(B-1); + DBENCH_START(); + + if(B > (Q-1)/8) + return 1; + + t = _mm256_setzero_si256(); + for(i = 0; i < N/8; i++) { + f = _mm256_load_si256(&a->vec[i]); + f = _mm256_abs_epi32(f); + f = _mm256_cmpgt_epi32(f,bound); + t = _mm256_or_si256(t,f); + } + + r = 1 - _mm256_testz_si256(t,t); + DBENCH_STOP(*tsample); + return r; +} + +/************************************************* +* Name: rej_uniform +* +* Description: Sample uniformly random coefficients in [0, Q-1] by +* performing rejection sampling on array of random bytes. +* +* Arguments: - int32_t *a: pointer to output array (allocated) +* - unsigned int len: number of coefficients to be sampled +* - const uint8_t *buf: array of random bytes +* - unsigned int buflen: length of array of random bytes +* +* Returns number of sampled coefficients. Can be smaller than len if not enough +* random bytes were given. +**************************************************/ +static unsigned int rej_uniform(int32_t *a, + unsigned int len, + const uint8_t *buf, + unsigned int buflen) +{ + unsigned int ctr, pos; + uint32_t t; + DBENCH_START(); + + ctr = pos = 0; + while(ctr < len && pos + 3 <= buflen) { + t = buf[pos++]; + t |= (uint32_t)buf[pos++] << 8; + t |= (uint32_t)buf[pos++] << 16; + t &= 0x7FFFFF; + + if(t < Q) + a[ctr++] = t; + } + + DBENCH_STOP(*tsample); + return ctr; +} + +/************************************************* +* Name: poly_uniform +* +* Description: Sample polynomial with uniformly random coefficients +* in [0,Q-1] by performing rejection sampling on the +* output stream of SHAKE256(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length SEEDBYTES +* - uint16_t nonce: 2-byte nonce +**************************************************/ +void poly_uniform_preinit(poly *a, stream128_state *state) +{ + unsigned int ctr; + /* rej_uniform_avx reads up to 8 additional bytes */ + ALIGNED_UINT8(REJ_UNIFORM_BUFLEN+8) buf; + + stream128_squeezeblocks(buf.coeffs, REJ_UNIFORM_NBLOCKS, state); + ctr = rej_uniform_avx(a->coeffs, buf.coeffs); + + while(ctr < N) { + /* length of buf is always divisible by 3; hence, no bytes left */ + stream128_squeezeblocks(buf.coeffs, 1, state); + ctr += rej_uniform(a->coeffs + ctr, N - ctr, buf.coeffs, STREAM128_BLOCKBYTES); + } +} + +void poly_uniform(poly *a, const uint8_t seed[SEEDBYTES], uint16_t nonce) +{ + stream128_state state; + stream128_init(&state, seed, nonce); + poly_uniform_preinit(a, &state); +} + +void poly_uniform_4x(poly *a0, + poly *a1, + poly *a2, + poly *a3, + const uint8_t seed[32], + uint16_t nonce0, + uint16_t nonce1, + uint16_t nonce2, + uint16_t nonce3) +{ + unsigned int ctr0, ctr1, ctr2, ctr3; + ALIGNED_UINT8(REJ_UNIFORM_BUFLEN+8) buf[4]; + keccakx4_state state; + __m256i f; + + f = _mm256_loadu_si256((__m256i *)seed); + _mm256_store_si256(buf[0].vec,f); + _mm256_store_si256(buf[1].vec,f); + _mm256_store_si256(buf[2].vec,f); + _mm256_store_si256(buf[3].vec,f); + + buf[0].coeffs[SEEDBYTES+0] = nonce0; + buf[0].coeffs[SEEDBYTES+1] = nonce0 >> 8; + buf[1].coeffs[SEEDBYTES+0] = nonce1; + buf[1].coeffs[SEEDBYTES+1] = nonce1 >> 8; + buf[2].coeffs[SEEDBYTES+0] = nonce2; + buf[2].coeffs[SEEDBYTES+1] = nonce2 >> 8; + buf[3].coeffs[SEEDBYTES+0] = nonce3; + buf[3].coeffs[SEEDBYTES+1] = nonce3 >> 8; + + shake128x4_absorb_once(&state, buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, SEEDBYTES + 2); + shake128x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, REJ_UNIFORM_NBLOCKS, &state); + + ctr0 = rej_uniform_avx(a0->coeffs, buf[0].coeffs); + ctr1 = rej_uniform_avx(a1->coeffs, buf[1].coeffs); + ctr2 = rej_uniform_avx(a2->coeffs, buf[2].coeffs); + ctr3 = rej_uniform_avx(a3->coeffs, buf[3].coeffs); + + while(ctr0 < N || ctr1 < N || ctr2 < N || ctr3 < N) { + shake128x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, 1, &state); + + ctr0 += rej_uniform(a0->coeffs + ctr0, N - ctr0, buf[0].coeffs, SHAKE128_RATE); + ctr1 += rej_uniform(a1->coeffs + ctr1, N - ctr1, buf[1].coeffs, SHAKE128_RATE); + ctr2 += rej_uniform(a2->coeffs + ctr2, N - ctr2, buf[2].coeffs, SHAKE128_RATE); + ctr3 += rej_uniform(a3->coeffs + ctr3, N - ctr3, buf[3].coeffs, SHAKE128_RATE); + } +} + +/************************************************* +* Name: rej_eta +* +* Description: Sample uniformly random coefficients in [-ETA, ETA] by +* performing rejection sampling on array of random bytes. +* +* Arguments: - int32_t *a: pointer to output array (allocated) +* - unsigned int len: number of coefficients to be sampled +* - const uint8_t *buf: array of random bytes +* - unsigned int buflen: length of array of random bytes +* +* Returns number of sampled coefficients. Can be smaller than len if not enough +* random bytes were given. +**************************************************/ +static unsigned int rej_eta(int32_t *a, + unsigned int len, + const uint8_t *buf, + unsigned int buflen) +{ + unsigned int ctr, pos; + uint32_t t0, t1; + DBENCH_START(); + + ctr = pos = 0; + while(ctr < len && pos < buflen) { + t0 = buf[pos] & 0x0F; + t1 = buf[pos++] >> 4; + +#if ETA == 2 + if(t0 < 15) { + t0 = t0 - (205*t0 >> 10)*5; + a[ctr++] = 2 - t0; + } + if(t1 < 15 && ctr < len) { + t1 = t1 - (205*t1 >> 10)*5; + a[ctr++] = 2 - t1; + } +#elif ETA == 4 + if(t0 < 9) + a[ctr++] = 4 - t0; + if(t1 < 9 && ctr < len) + a[ctr++] = 4 - t1; +#endif + } + + DBENCH_STOP(*tsample); + return ctr; +} + +/************************************************* +* Name: poly_uniform_eta +* +* Description: Sample polynomial with uniformly random coefficients +* in [-ETA,ETA] by performing rejection sampling using the +* output stream of SHAKE256(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length CRHBYTES +* - uint16_t nonce: 2-byte nonce +**************************************************/ +void poly_uniform_eta_preinit(poly *a, stream256_state *state) +{ + unsigned int ctr; + ALIGNED_UINT8(REJ_UNIFORM_ETA_BUFLEN) buf; + + stream256_squeezeblocks(buf.coeffs, REJ_UNIFORM_ETA_NBLOCKS, state); + ctr = rej_eta_avx(a->coeffs, buf.coeffs); + + while(ctr < N) { + stream256_squeezeblocks(buf.coeffs, 1, state); + ctr += rej_eta(a->coeffs + ctr, N - ctr, buf.coeffs, STREAM256_BLOCKBYTES); + } +} + +void poly_uniform_eta(poly *a, const uint8_t seed[CRHBYTES], uint16_t nonce) +{ + stream256_state state; + stream256_init(&state, seed, nonce); + poly_uniform_eta_preinit(a, &state); +} + +void poly_uniform_eta_4x(poly *a0, + poly *a1, + poly *a2, + poly *a3, + const uint8_t seed[64], + uint16_t nonce0, + uint16_t nonce1, + uint16_t nonce2, + uint16_t nonce3) +{ + unsigned int ctr0, ctr1, ctr2, ctr3; + ALIGNED_UINT8(REJ_UNIFORM_ETA_BUFLEN) buf[4]; + + __m256i f; + keccakx4_state state; + + f = _mm256_loadu_si256((__m256i *)&seed[0]); + _mm256_store_si256(&buf[0].vec[0],f); + _mm256_store_si256(&buf[1].vec[0],f); + _mm256_store_si256(&buf[2].vec[0],f); + _mm256_store_si256(&buf[3].vec[0],f); + f = _mm256_loadu_si256((__m256i *)&seed[32]); + _mm256_store_si256(&buf[0].vec[1],f); + _mm256_store_si256(&buf[1].vec[1],f); + _mm256_store_si256(&buf[2].vec[1],f); + _mm256_store_si256(&buf[3].vec[1],f); + + buf[0].coeffs[64] = nonce0; + buf[0].coeffs[65] = nonce0 >> 8; + buf[1].coeffs[64] = nonce1; + buf[1].coeffs[65] = nonce1 >> 8; + buf[2].coeffs[64] = nonce2; + buf[2].coeffs[65] = nonce2 >> 8; + buf[3].coeffs[64] = nonce3; + buf[3].coeffs[65] = nonce3 >> 8; + + shake256x4_absorb_once(&state, buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, 66); + shake256x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, REJ_UNIFORM_ETA_NBLOCKS, &state); + + ctr0 = rej_eta_avx(a0->coeffs, buf[0].coeffs); + ctr1 = rej_eta_avx(a1->coeffs, buf[1].coeffs); + ctr2 = rej_eta_avx(a2->coeffs, buf[2].coeffs); + ctr3 = rej_eta_avx(a3->coeffs, buf[3].coeffs); + + while(ctr0 < N || ctr1 < N || ctr2 < N || ctr3 < N) { + shake256x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, 1, &state); + + ctr0 += rej_eta(a0->coeffs + ctr0, N - ctr0, buf[0].coeffs, SHAKE256_RATE); + ctr1 += rej_eta(a1->coeffs + ctr1, N - ctr1, buf[1].coeffs, SHAKE256_RATE); + ctr2 += rej_eta(a2->coeffs + ctr2, N - ctr2, buf[2].coeffs, SHAKE256_RATE); + ctr3 += rej_eta(a3->coeffs + ctr3, N - ctr3, buf[3].coeffs, SHAKE256_RATE); + } +} + +/************************************************* +* Name: poly_uniform_gamma1 +* +* Description: Sample polynomial with uniformly random coefficients +* in [-(GAMMA1 - 1), GAMMA1] by unpacking output stream +* of SHAKE256(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length CRHBYTES +* - uint16_t nonce: 16-bit nonce +**************************************************/ +#define POLY_UNIFORM_GAMMA1_NBLOCKS ((POLYZ_PACKEDBYTES+STREAM256_BLOCKBYTES-1)/STREAM256_BLOCKBYTES) +void poly_uniform_gamma1_preinit(poly *a, stream256_state *state) +{ + /* polyz_unpack reads 14 additional bytes */ + ALIGNED_UINT8(POLY_UNIFORM_GAMMA1_NBLOCKS*STREAM256_BLOCKBYTES+14) buf; + stream256_squeezeblocks(buf.coeffs, POLY_UNIFORM_GAMMA1_NBLOCKS, state); + polyz_unpack(a, buf.coeffs); +} + +void poly_uniform_gamma1(poly *a, const uint8_t seed[CRHBYTES], uint16_t nonce) +{ + stream256_state state; + stream256_init(&state, seed, nonce); + poly_uniform_gamma1_preinit(a, &state); +} + +void poly_uniform_gamma1_4x(poly *a0, + poly *a1, + poly *a2, + poly *a3, + const uint8_t seed[64], + uint16_t nonce0, + uint16_t nonce1, + uint16_t nonce2, + uint16_t nonce3) +{ + ALIGNED_UINT8(POLY_UNIFORM_GAMMA1_NBLOCKS*STREAM256_BLOCKBYTES+14) buf[4]; + keccakx4_state state; + __m256i f; + + f = _mm256_loadu_si256((__m256i *)&seed[0]); + _mm256_store_si256(&buf[0].vec[0],f); + _mm256_store_si256(&buf[1].vec[0],f); + _mm256_store_si256(&buf[2].vec[0],f); + _mm256_store_si256(&buf[3].vec[0],f); + f = _mm256_loadu_si256((__m256i *)&seed[32]); + _mm256_store_si256(&buf[0].vec[1],f); + _mm256_store_si256(&buf[1].vec[1],f); + _mm256_store_si256(&buf[2].vec[1],f); + _mm256_store_si256(&buf[3].vec[1],f); + + buf[0].coeffs[64] = nonce0; + buf[0].coeffs[65] = nonce0 >> 8; + buf[1].coeffs[64] = nonce1; + buf[1].coeffs[65] = nonce1 >> 8; + buf[2].coeffs[64] = nonce2; + buf[2].coeffs[65] = nonce2 >> 8; + buf[3].coeffs[64] = nonce3; + buf[3].coeffs[65] = nonce3 >> 8; + + shake256x4_absorb_once(&state, buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, 66); + shake256x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, POLY_UNIFORM_GAMMA1_NBLOCKS, &state); + + polyz_unpack(a0, buf[0].coeffs); + polyz_unpack(a1, buf[1].coeffs); + polyz_unpack(a2, buf[2].coeffs); + polyz_unpack(a3, buf[3].coeffs); +} + +/************************************************* +* Name: challenge +* +* Description: Implementation of H. Samples polynomial with TAU nonzero +* coefficients in {-1,1} using the output stream of +* SHAKE256(seed). +* +* Arguments: - poly *c: pointer to output polynomial +* - const uint8_t mu[]: byte array containing seed of length CTILDEBYTES +**************************************************/ +void poly_challenge(poly * restrict c, const uint8_t seed[CTILDEBYTES]) { + unsigned int i, b, pos; + uint64_t signs; + ALIGNED_UINT8(SHAKE256_RATE) buf; + keccak_state state; + + shake256_init(&state); + shake256_absorb(&state, seed, CTILDEBYTES); + shake256_finalize(&state); + shake256_squeezeblocks(buf.coeffs, 1, &state); + + memcpy(&signs, buf.coeffs, 8); + pos = 8; + + memset(c->vec, 0, sizeof(poly)); + for(i = N-TAU; i < N; ++i) { + do { + if(pos >= SHAKE256_RATE) { + shake256_squeezeblocks(buf.coeffs, 1, &state); + pos = 0; + } + + b = buf.coeffs[pos++]; + } while(b > i); + + c->coeffs[i] = c->coeffs[b]; + c->coeffs[b] = 1 - 2*(signs & 1); + signs >>= 1; + } +} + +/************************************************* +* Name: polyeta_pack +* +* Description: Bit-pack polynomial with coefficients in [-ETA,ETA]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYETA_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyeta_pack(uint8_t r[POLYETA_PACKEDBYTES], const poly * restrict a) { + unsigned int i; + uint8_t t[8]; + DBENCH_START(); + +#if ETA == 2 + for(i = 0; i < N/8; ++i) { + t[0] = ETA - a->coeffs[8*i+0]; + t[1] = ETA - a->coeffs[8*i+1]; + t[2] = ETA - a->coeffs[8*i+2]; + t[3] = ETA - a->coeffs[8*i+3]; + t[4] = ETA - a->coeffs[8*i+4]; + t[5] = ETA - a->coeffs[8*i+5]; + t[6] = ETA - a->coeffs[8*i+6]; + t[7] = ETA - a->coeffs[8*i+7]; + + r[3*i+0] = (t[0] >> 0) | (t[1] << 3) | (t[2] << 6); + r[3*i+1] = (t[2] >> 2) | (t[3] << 1) | (t[4] << 4) | (t[5] << 7); + r[3*i+2] = (t[5] >> 1) | (t[6] << 2) | (t[7] << 5); + } +#elif ETA == 4 + for(i = 0; i < N/2; ++i) { + t[0] = ETA - a->coeffs[2*i+0]; + t[1] = ETA - a->coeffs[2*i+1]; + r[i] = t[0] | (t[1] << 4); + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyeta_unpack +* +* Description: Unpack polynomial with coefficients in [-ETA,ETA]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyeta_unpack(poly * restrict r, const uint8_t a[POLYETA_PACKEDBYTES]) { + unsigned int i; + DBENCH_START(); + +#if ETA == 2 + for(i = 0; i < N/8; ++i) { + r->coeffs[8*i+0] = (a[3*i+0] >> 0) & 7; + r->coeffs[8*i+1] = (a[3*i+0] >> 3) & 7; + r->coeffs[8*i+2] = ((a[3*i+0] >> 6) | (a[3*i+1] << 2)) & 7; + r->coeffs[8*i+3] = (a[3*i+1] >> 1) & 7; + r->coeffs[8*i+4] = (a[3*i+1] >> 4) & 7; + r->coeffs[8*i+5] = ((a[3*i+1] >> 7) | (a[3*i+2] << 1)) & 7; + r->coeffs[8*i+6] = (a[3*i+2] >> 2) & 7; + r->coeffs[8*i+7] = (a[3*i+2] >> 5) & 7; + + r->coeffs[8*i+0] = ETA - r->coeffs[8*i+0]; + r->coeffs[8*i+1] = ETA - r->coeffs[8*i+1]; + r->coeffs[8*i+2] = ETA - r->coeffs[8*i+2]; + r->coeffs[8*i+3] = ETA - r->coeffs[8*i+3]; + r->coeffs[8*i+4] = ETA - r->coeffs[8*i+4]; + r->coeffs[8*i+5] = ETA - r->coeffs[8*i+5]; + r->coeffs[8*i+6] = ETA - r->coeffs[8*i+6]; + r->coeffs[8*i+7] = ETA - r->coeffs[8*i+7]; + } +#elif ETA == 4 + for(i = 0; i < N/2; ++i) { + r->coeffs[2*i+0] = a[i] & 0x0F; + r->coeffs[2*i+1] = a[i] >> 4; + r->coeffs[2*i+0] = ETA - r->coeffs[2*i+0]; + r->coeffs[2*i+1] = ETA - r->coeffs[2*i+1]; + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt1_pack +* +* Description: Bit-pack polynomial t1 with coefficients fitting in 10 bits. +* Input coefficients are assumed to be positive standard representatives. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYT1_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyt1_pack(uint8_t r[POLYT1_PACKEDBYTES], const poly * restrict a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/4; ++i) { + r[5*i+0] = (a->coeffs[4*i+0] >> 0); + r[5*i+1] = (a->coeffs[4*i+0] >> 8) | (a->coeffs[4*i+1] << 2); + r[5*i+2] = (a->coeffs[4*i+1] >> 6) | (a->coeffs[4*i+2] << 4); + r[5*i+3] = (a->coeffs[4*i+2] >> 4) | (a->coeffs[4*i+3] << 6); + r[5*i+4] = (a->coeffs[4*i+3] >> 2); + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt1_unpack +* +* Description: Unpack polynomial t1 with 10-bit coefficients. +* Output coefficients are positive standard representatives. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyt1_unpack(poly * restrict r, const uint8_t a[POLYT1_PACKEDBYTES]) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/4; ++i) { + r->coeffs[4*i+0] = ((a[5*i+0] >> 0) | ((uint32_t)a[5*i+1] << 8)) & 0x3FF; + r->coeffs[4*i+1] = ((a[5*i+1] >> 2) | ((uint32_t)a[5*i+2] << 6)) & 0x3FF; + r->coeffs[4*i+2] = ((a[5*i+2] >> 4) | ((uint32_t)a[5*i+3] << 4)) & 0x3FF; + r->coeffs[4*i+3] = ((a[5*i+3] >> 6) | ((uint32_t)a[5*i+4] << 2)) & 0x3FF; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt0_pack +* +* Description: Bit-pack polynomial t0 with coefficients in ]-2^{D-1}, 2^{D-1}]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYT0_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyt0_pack(uint8_t r[POLYT0_PACKEDBYTES], const poly * restrict a) { + unsigned int i; + uint32_t t[8]; + DBENCH_START(); + + for(i = 0; i < N/8; ++i) { + t[0] = (1 << (D-1)) - a->coeffs[8*i+0]; + t[1] = (1 << (D-1)) - a->coeffs[8*i+1]; + t[2] = (1 << (D-1)) - a->coeffs[8*i+2]; + t[3] = (1 << (D-1)) - a->coeffs[8*i+3]; + t[4] = (1 << (D-1)) - a->coeffs[8*i+4]; + t[5] = (1 << (D-1)) - a->coeffs[8*i+5]; + t[6] = (1 << (D-1)) - a->coeffs[8*i+6]; + t[7] = (1 << (D-1)) - a->coeffs[8*i+7]; + + r[13*i+ 0] = t[0]; + r[13*i+ 1] = t[0] >> 8; + r[13*i+ 1] |= t[1] << 5; + r[13*i+ 2] = t[1] >> 3; + r[13*i+ 3] = t[1] >> 11; + r[13*i+ 3] |= t[2] << 2; + r[13*i+ 4] = t[2] >> 6; + r[13*i+ 4] |= t[3] << 7; + r[13*i+ 5] = t[3] >> 1; + r[13*i+ 6] = t[3] >> 9; + r[13*i+ 6] |= t[4] << 4; + r[13*i+ 7] = t[4] >> 4; + r[13*i+ 8] = t[4] >> 12; + r[13*i+ 8] |= t[5] << 1; + r[13*i+ 9] = t[5] >> 7; + r[13*i+ 9] |= t[6] << 6; + r[13*i+10] = t[6] >> 2; + r[13*i+11] = t[6] >> 10; + r[13*i+11] |= t[7] << 3; + r[13*i+12] = t[7] >> 5; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt0_unpack +* +* Description: Unpack polynomial t0 with coefficients in ]-2^{D-1}, 2^{D-1}]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyt0_unpack(poly * restrict r, const uint8_t a[POLYT0_PACKEDBYTES]) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/8; ++i) { + r->coeffs[8*i+0] = a[13*i+0]; + r->coeffs[8*i+0] |= (uint32_t)a[13*i+1] << 8; + r->coeffs[8*i+0] &= 0x1FFF; + + r->coeffs[8*i+1] = a[13*i+1] >> 5; + r->coeffs[8*i+1] |= (uint32_t)a[13*i+2] << 3; + r->coeffs[8*i+1] |= (uint32_t)a[13*i+3] << 11; + r->coeffs[8*i+1] &= 0x1FFF; + + r->coeffs[8*i+2] = a[13*i+3] >> 2; + r->coeffs[8*i+2] |= (uint32_t)a[13*i+4] << 6; + r->coeffs[8*i+2] &= 0x1FFF; + + r->coeffs[8*i+3] = a[13*i+4] >> 7; + r->coeffs[8*i+3] |= (uint32_t)a[13*i+5] << 1; + r->coeffs[8*i+3] |= (uint32_t)a[13*i+6] << 9; + r->coeffs[8*i+3] &= 0x1FFF; + + r->coeffs[8*i+4] = a[13*i+6] >> 4; + r->coeffs[8*i+4] |= (uint32_t)a[13*i+7] << 4; + r->coeffs[8*i+4] |= (uint32_t)a[13*i+8] << 12; + r->coeffs[8*i+4] &= 0x1FFF; + + r->coeffs[8*i+5] = a[13*i+8] >> 1; + r->coeffs[8*i+5] |= (uint32_t)a[13*i+9] << 7; + r->coeffs[8*i+5] &= 0x1FFF; + + r->coeffs[8*i+6] = a[13*i+9] >> 6; + r->coeffs[8*i+6] |= (uint32_t)a[13*i+10] << 2; + r->coeffs[8*i+6] |= (uint32_t)a[13*i+11] << 10; + r->coeffs[8*i+6] &= 0x1FFF; + + r->coeffs[8*i+7] = a[13*i+11] >> 3; + r->coeffs[8*i+7] |= (uint32_t)a[13*i+12] << 5; + r->coeffs[8*i+7] &= 0x1FFF; + + r->coeffs[8*i+0] = (1 << (D-1)) - r->coeffs[8*i+0]; + r->coeffs[8*i+1] = (1 << (D-1)) - r->coeffs[8*i+1]; + r->coeffs[8*i+2] = (1 << (D-1)) - r->coeffs[8*i+2]; + r->coeffs[8*i+3] = (1 << (D-1)) - r->coeffs[8*i+3]; + r->coeffs[8*i+4] = (1 << (D-1)) - r->coeffs[8*i+4]; + r->coeffs[8*i+5] = (1 << (D-1)) - r->coeffs[8*i+5]; + r->coeffs[8*i+6] = (1 << (D-1)) - r->coeffs[8*i+6]; + r->coeffs[8*i+7] = (1 << (D-1)) - r->coeffs[8*i+7]; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyz_pack +* +* Description: Bit-pack polynomial with coefficients +* in [-(GAMMA1 - 1), GAMMA1]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYZ_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyz_pack(uint8_t r[POLYZ_PACKEDBYTES], const poly * restrict a) { + unsigned int i; + uint32_t t[4]; + DBENCH_START(); + +#if GAMMA1 == (1 << 17) + for(i = 0; i < N/4; ++i) { + t[0] = GAMMA1 - a->coeffs[4*i+0]; + t[1] = GAMMA1 - a->coeffs[4*i+1]; + t[2] = GAMMA1 - a->coeffs[4*i+2]; + t[3] = GAMMA1 - a->coeffs[4*i+3]; + + r[9*i+0] = t[0]; + r[9*i+1] = t[0] >> 8; + r[9*i+2] = t[0] >> 16; + r[9*i+2] |= t[1] << 2; + r[9*i+3] = t[1] >> 6; + r[9*i+4] = t[1] >> 14; + r[9*i+4] |= t[2] << 4; + r[9*i+5] = t[2] >> 4; + r[9*i+6] = t[2] >> 12; + r[9*i+6] |= t[3] << 6; + r[9*i+7] = t[3] >> 2; + r[9*i+8] = t[3] >> 10; + } +#elif GAMMA1 == (1 << 19) + for(i = 0; i < N/2; ++i) { + t[0] = GAMMA1 - a->coeffs[2*i+0]; + t[1] = GAMMA1 - a->coeffs[2*i+1]; + + r[5*i+0] = t[0]; + r[5*i+1] = t[0] >> 8; + r[5*i+2] = t[0] >> 16; + r[5*i+2] |= t[1] << 4; + r[5*i+3] = t[1] >> 4; + r[5*i+4] = t[1] >> 12; + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyz_unpack +* +* Description: Unpack polynomial z with coefficients +* in [-(GAMMA1 - 1), GAMMA1]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +#if GAMMA1 == (1 << 17) +void polyz_unpack(poly * restrict r, const uint8_t *a) { + unsigned int i; + __m256i f; + const __m256i shufbidx = _mm256_set_epi8(-1, 9, 8, 7,-1, 7, 6, 5,-1, 5, 4, 3,-1, 3, 2, 1, + -1, 8, 7, 6,-1, 6, 5, 4,-1, 4, 3, 2,-1, 2, 1, 0); + const __m256i srlvdidx = _mm256_set_epi32(6,4,2,0,6,4,2,0); + const __m256i mask = _mm256_set1_epi32(0x3FFFF); + const __m256i gamma1 = _mm256_set1_epi32(GAMMA1); + DBENCH_START(); + + for(i = 0; i < N/8; i++) { + f = _mm256_loadu_si256((__m256i *)&a[18*i]); + f = _mm256_permute4x64_epi64(f,0x94); + f = _mm256_shuffle_epi8(f,shufbidx); + f = _mm256_srlv_epi32(f,srlvdidx); + f = _mm256_and_si256(f,mask); + f = _mm256_sub_epi32(gamma1,f); + _mm256_store_si256(&r->vec[i],f); + } + + DBENCH_STOP(*tpack); +} + +#elif GAMMA1 == (1 << 19) +void polyz_unpack(poly * restrict r, const uint8_t *a) { + unsigned int i; + __m256i f; + const __m256i shufbidx = _mm256_set_epi8(-1,11,10, 9,-1, 9, 8, 7,-1, 6, 5, 4,-1, 4, 3, 2, + -1, 9, 8, 7,-1, 7, 6, 5,-1, 4, 3, 2,-1, 2, 1, 0); + const __m256i srlvdidx = _mm256_set1_epi64x((uint64_t)4 << 32); + const __m256i mask = _mm256_set1_epi32(0xFFFFF); + const __m256i gamma1 = _mm256_set1_epi32(GAMMA1); + DBENCH_START(); + + for(i = 0; i < N/8; i++) { + f = _mm256_loadu_si256((__m256i *)&a[20*i]); + f = _mm256_permute4x64_epi64(f,0x94); + f = _mm256_shuffle_epi8(f,shufbidx); + f = _mm256_srlv_epi32(f,srlvdidx); + f = _mm256_and_si256(f,mask); + f = _mm256_sub_epi32(gamma1,f); + _mm256_store_si256(&r->vec[i],f); + } + + DBENCH_STOP(*tpack); +} +#endif + +/************************************************* +* Name: polyw1_pack +* +* Description: Bit-pack polynomial w1 with coefficients in [0,15] or [0,43]. +* Input coefficients are assumed to be positive standard representatives. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYW1_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +#if GAMMA2 == (Q-1)/88 +void polyw1_pack(uint8_t *r, const poly * restrict a) { + unsigned int i; + __m256i f0,f1,f2,f3; + const __m256i shift1 = _mm256_set1_epi16((64 << 8) + 1); + const __m256i shift2 = _mm256_set1_epi32((4096 << 16) + 1); + const __m256i shufdidx1 = _mm256_set_epi32(7,3,6,2,5,1,4,0); + const __m256i shufdidx2 = _mm256_set_epi32(-1,-1,6,5,4,2,1,0); + const __m256i shufbidx = _mm256_set_epi8(-1,-1,-1,-1,14,13,12,10, 9, 8, 6, 5, 4, 2, 1, 0, + -1,-1,-1,-1,14,13,12,10, 9, 8, 6, 5, 4, 2, 1, 0); + DBENCH_START(); + + for(i = 0; i < N/32; i++) { + f0 = _mm256_load_si256(&a->vec[4*i+0]); + f1 = _mm256_load_si256(&a->vec[4*i+1]); + f2 = _mm256_load_si256(&a->vec[4*i+2]); + f3 = _mm256_load_si256(&a->vec[4*i+3]); + f0 = _mm256_packus_epi32(f0,f1); + f1 = _mm256_packus_epi32(f2,f3); + f0 = _mm256_packus_epi16(f0,f1); + f0 = _mm256_maddubs_epi16(f0,shift1); + f0 = _mm256_madd_epi16(f0,shift2); + f0 = _mm256_permutevar8x32_epi32(f0,shufdidx1); + f0 = _mm256_shuffle_epi8(f0,shufbidx); + f0 = _mm256_permutevar8x32_epi32(f0,shufdidx2); + _mm256_storeu_si256((__m256i *)&r[24*i],f0); + } + + DBENCH_STOP(*tpack); +} + +#elif GAMMA2 == (Q-1)/32 +void polyw1_pack(uint8_t *r, const poly * restrict a) { + unsigned int i; + __m256i f0, f1, f2, f3, f4, f5, f6, f7; + const __m256i shift = _mm256_set1_epi16((16 << 8) + 1); + const __m256i shufbidx = _mm256_set_epi8(15,14, 7, 6,13,12, 5, 4,11,10, 3, 2, 9, 8, 1, 0, + 15,14, 7, 6,13,12, 5, 4,11,10, 3, 2, 9, 8, 1, 0); + DBENCH_START(); + + for(i = 0; i < N/64; ++i) { + f0 = _mm256_load_si256(&a->vec[8*i+0]); + f1 = _mm256_load_si256(&a->vec[8*i+1]); + f2 = _mm256_load_si256(&a->vec[8*i+2]); + f3 = _mm256_load_si256(&a->vec[8*i+3]); + f4 = _mm256_load_si256(&a->vec[8*i+4]); + f5 = _mm256_load_si256(&a->vec[8*i+5]); + f6 = _mm256_load_si256(&a->vec[8*i+6]); + f7 = _mm256_load_si256(&a->vec[8*i+7]); + f0 = _mm256_packus_epi32(f0,f1); + f1 = _mm256_packus_epi32(f2,f3); + f2 = _mm256_packus_epi32(f4,f5); + f3 = _mm256_packus_epi32(f6,f7); + f0 = _mm256_packus_epi16(f0,f1); + f1 = _mm256_packus_epi16(f2,f3); + f0 = _mm256_maddubs_epi16(f0,shift); + f1 = _mm256_maddubs_epi16(f1,shift); + f0 = _mm256_packus_epi16(f0,f1); + f0 = _mm256_permute4x64_epi64(f0,0xD8); + f0 = _mm256_shuffle_epi8(f0,shufbidx); + _mm256_storeu_si256((__m256i *)&r[32*i], f0); + } + + DBENCH_STOP(*tpack); +} +#endif diff --git a/src/crypto/pq/_d/avx2/poly.h b/src/crypto/pq/_d/avx2/poly.h new file mode 100644 index 0000000..7d93088 --- /dev/null +++ b/src/crypto/pq/_d/avx2/poly.h @@ -0,0 +1,112 @@ +#ifndef POLY_H +#define POLY_H + +#include +#include "align.h" +#include "params.h" +#include "symmetric.h" + +typedef ALIGNED_INT32(N) poly; + +#define poly_reduce DILITHIUM_NAMESPACE(poly_reduce) +void poly_reduce(poly *a); +#define poly_caddq DILITHIUM_NAMESPACE(poly_caddq) +void poly_caddq(poly *a); + +#define poly_add DILITHIUM_NAMESPACE(poly_add) +void poly_add(poly *c, const poly *a, const poly *b); +#define poly_sub DILITHIUM_NAMESPACE(poly_sub) +void poly_sub(poly *c, const poly *a, const poly *b); +#define poly_shiftl DILITHIUM_NAMESPACE(poly_shiftl) +void poly_shiftl(poly *a); + +#define poly_ntt DILITHIUM_NAMESPACE(poly_ntt) +void poly_ntt(poly *a); +#define poly_invntt_tomont DILITHIUM_NAMESPACE(poly_invntt_tomont) +void poly_invntt_tomont(poly *a); +#define poly_nttunpack DILITHIUM_NAMESPACE(poly_nttunpack) +void poly_nttunpack(poly *a); +#define poly_pointwise_montgomery DILITHIUM_NAMESPACE(poly_pointwise_montgomery) +void poly_pointwise_montgomery(poly *c, const poly *a, const poly *b); + +#define poly_power2round DILITHIUM_NAMESPACE(poly_power2round) +void poly_power2round(poly *a1, poly *a0, const poly *a); +#define poly_decompose DILITHIUM_NAMESPACE(poly_decompose) +void poly_decompose(poly *a1, poly *a0, const poly *a); +#define poly_make_hint DILITHIUM_NAMESPACE(poly_make_hint) +unsigned int poly_make_hint(uint8_t hint[N], const poly *a0, const poly *a1); +#define poly_use_hint DILITHIUM_NAMESPACE(poly_use_hint) +void poly_use_hint(poly *b, const poly *a, const poly *h); + +#define poly_chknorm DILITHIUM_NAMESPACE(poly_chknorm) +int poly_chknorm(const poly *a, int32_t B); +#define poly_uniform_preinit DILITHIUM_NAMESPACE(poly_uniform_preinit) +void poly_uniform_preinit(poly *a, stream128_state *state); +#define poly_uniform DILITHIUM_NAMESPACE(poly_uniform) +void poly_uniform(poly *a, const uint8_t seed[SEEDBYTES], uint16_t nonce); +#define poly_uniform_eta_preinit DILITHIUM_NAMESPACE(poly_uniform_eta_preinit) +void poly_uniform_eta_preinit(poly *a, stream256_state *state); +#define poly_uniform_eta DILITHIUM_NAMESPACE(poly_uniform_eta) +void poly_uniform_eta(poly *a, const uint8_t seed[CRHBYTES], uint16_t nonce); +#define poly_uniform_gamma1_preinit DILITHIUM_NAMESPACE(poly_uniform_gamma1_preinit) +void poly_uniform_gamma1_preinit(poly *a, stream256_state *state); +#define poly_uniform_gamma1 DILITHIUM_NAMESPACE(poly_uniform_gamma1) +void poly_uniform_gamma1(poly *a, const uint8_t seed[CRHBYTES], uint16_t nonce); +#define poly_challenge DILITHIUM_NAMESPACE(poly_challenge) +void poly_challenge(poly *c, const uint8_t seed[CTILDEBYTES]); + +#define poly_uniform_4x DILITHIUM_NAMESPACE(poly_uniform_4x) +void poly_uniform_4x(poly *a0, + poly *a1, + poly *a2, + poly *a3, + const uint8_t seed[SEEDBYTES], + uint16_t nonce0, + uint16_t nonce1, + uint16_t nonce2, + uint16_t nonce3); +#define poly_uniform_eta_4x DILITHIUM_NAMESPACE(poly_uniform_eta_4x) +void poly_uniform_eta_4x(poly *a0, + poly *a1, + poly *a2, + poly *a3, + const uint8_t seed[CRHBYTES], + uint16_t nonce0, + uint16_t nonce1, + uint16_t nonce2, + uint16_t nonce3); +#define poly_uniform_gamma1_4x DILITHIUM_NAMESPACE(poly_uniform_gamma1_4x) +void poly_uniform_gamma1_4x(poly *a0, + poly *a1, + poly *a2, + poly *a3, + const uint8_t seed[CRHBYTES], + uint16_t nonce0, + uint16_t nonce1, + uint16_t nonce2, + uint16_t nonce3); + +#define polyeta_pack DILITHIUM_NAMESPACE(polyeta_pack) +void polyeta_pack(uint8_t r[POLYETA_PACKEDBYTES], const poly *a); +#define polyeta_unpack DILITHIUM_NAMESPACE(polyeta_unpack) +void polyeta_unpack(poly *r, const uint8_t a[POLYETA_PACKEDBYTES]); + +#define polyt1_pack DILITHIUM_NAMESPACE(polyt1_pack) +void polyt1_pack(uint8_t r[POLYT1_PACKEDBYTES], const poly *a); +#define polyt1_unpack DILITHIUM_NAMESPACE(polyt1_unpack) +void polyt1_unpack(poly *r, const uint8_t a[POLYT1_PACKEDBYTES]); + +#define polyt0_pack DILITHIUM_NAMESPACE(polyt0_pack) +void polyt0_pack(uint8_t r[POLYT0_PACKEDBYTES], const poly *a); +#define polyt0_unpack DILITHIUM_NAMESPACE(polyt0_unpack) +void polyt0_unpack(poly *r, const uint8_t a[POLYT0_PACKEDBYTES]); + +#define polyz_pack DILITHIUM_NAMESPACE(polyz_pack) +void polyz_pack(uint8_t r[POLYZ_PACKEDBYTES], const poly *a); +#define polyz_unpack DILITHIUM_NAMESPACE(polyz_unpack) +void polyz_unpack(poly *r, const uint8_t *a); + +#define polyw1_pack DILITHIUM_NAMESPACE(polyw1_pack) +void polyw1_pack(uint8_t *r, const poly *a); + +#endif diff --git a/src/crypto/pq/_d/avx2/polyvec.c b/src/crypto/pq/_d/avx2/polyvec.c new file mode 100644 index 0000000..0db3514 --- /dev/null +++ b/src/crypto/pq/_d/avx2/polyvec.c @@ -0,0 +1,588 @@ +#include +#include "params.h" +#include "polyvec.h" +#include "poly.h" +#include "ntt.h" +#include "consts.h" + +/************************************************* +* Name: expand_mat +* +* Description: Implementation of ExpandA. Generates matrix A with uniformly +* random coefficients a_{i,j} by performing rejection +* sampling on the output stream of SHAKE128(rho|j|i) +* +* Arguments: - polyvecl mat[K]: output matrix +* - const uint8_t rho[]: byte array containing seed rho +**************************************************/ + +#if K == 4 && L == 4 +void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]) { + polyvec_matrix_expand_row0(&mat[0], NULL, rho); + polyvec_matrix_expand_row1(&mat[1], NULL, rho); + polyvec_matrix_expand_row2(&mat[2], NULL, rho); + polyvec_matrix_expand_row3(&mat[3], NULL, rho); +} + +void polyvec_matrix_expand_row0(polyvecl *rowa, __attribute__((unused)) polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[0], &rowa->vec[1], &rowa->vec[2], &rowa->vec[3], rho, 0, 1, 2, 3); + poly_nttunpack(&rowa->vec[0]); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); +} + +void polyvec_matrix_expand_row1(polyvecl *rowa, __attribute__((unused)) polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[0], &rowa->vec[1], &rowa->vec[2], &rowa->vec[3], rho, 256, 257, 258, 259); + poly_nttunpack(&rowa->vec[0]); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); +} + +void polyvec_matrix_expand_row2(polyvecl *rowa, __attribute__((unused)) polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[0], &rowa->vec[1], &rowa->vec[2], &rowa->vec[3], rho, 512, 513, 514, 515); + poly_nttunpack(&rowa->vec[0]); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); +} + +void polyvec_matrix_expand_row3(polyvecl *rowa, __attribute__((unused)) polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[0], &rowa->vec[1], &rowa->vec[2], &rowa->vec[3], rho, 768, 769, 770, 771); + poly_nttunpack(&rowa->vec[0]); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); +} + +#elif K == 6 && L == 5 +void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]) { + polyvecl tmp; + polyvec_matrix_expand_row0(&mat[0], &mat[1], rho); + polyvec_matrix_expand_row1(&mat[1], &mat[2], rho); + polyvec_matrix_expand_row2(&mat[2], &mat[3], rho); + polyvec_matrix_expand_row3(&mat[3], NULL, rho); + polyvec_matrix_expand_row4(&mat[4], &mat[5], rho); + polyvec_matrix_expand_row5(&mat[5], &tmp, rho); +} + +void polyvec_matrix_expand_row0(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[0], &rowa->vec[1], &rowa->vec[2], &rowa->vec[3], rho, 0, 1, 2, 3); + poly_uniform_4x(&rowa->vec[4], &rowb->vec[0], &rowb->vec[1], &rowb->vec[2], rho, 4, 256, 257, 258); + poly_nttunpack(&rowa->vec[0]); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowb->vec[0]); + poly_nttunpack(&rowb->vec[1]); + poly_nttunpack(&rowb->vec[2]); +} + +void polyvec_matrix_expand_row1(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[3], &rowa->vec[4], &rowb->vec[0], &rowb->vec[1], rho, 259, 260, 512, 513); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowb->vec[0]); + poly_nttunpack(&rowb->vec[1]); +} + +void polyvec_matrix_expand_row2(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[2], &rowa->vec[3], &rowa->vec[4], &rowb->vec[0], rho, 514, 515, 516, 768); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowb->vec[0]); +} + +void polyvec_matrix_expand_row3(polyvecl *rowa, __attribute__((unused)) polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[1], &rowa->vec[2], &rowa->vec[3], &rowa->vec[4], rho, 769, 770, 771, 772); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); +} + +void polyvec_matrix_expand_row4(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[0], &rowa->vec[1], &rowa->vec[2], &rowa->vec[3], rho, 1024, 1025, 1026, 1027); + poly_uniform_4x(&rowa->vec[4], &rowb->vec[0], &rowb->vec[1], &rowb->vec[2], rho, 1028, 1280, 1281, 1282); + poly_nttunpack(&rowa->vec[0]); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowb->vec[0]); + poly_nttunpack(&rowb->vec[1]); + poly_nttunpack(&rowb->vec[2]); +} + +void polyvec_matrix_expand_row5(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[3], &rowa->vec[4], &rowb->vec[0], &rowb->vec[1], rho, 1283, 1284, 1536, 1537); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); +} + +#elif K == 8 && L == 7 +void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]) { + polyvec_matrix_expand_row0(&mat[0], &mat[1], rho); + polyvec_matrix_expand_row1(&mat[1], &mat[2], rho); + polyvec_matrix_expand_row2(&mat[2], &mat[3], rho); + polyvec_matrix_expand_row3(&mat[3], NULL, rho); + polyvec_matrix_expand_row4(&mat[4], &mat[5], rho); + polyvec_matrix_expand_row5(&mat[5], &mat[6], rho); + polyvec_matrix_expand_row6(&mat[6], &mat[7], rho); + polyvec_matrix_expand_row7(&mat[7], NULL, rho); +} + +void polyvec_matrix_expand_row0(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[0], &rowa->vec[1], &rowa->vec[2], &rowa->vec[3], rho, 0, 1, 2, 3); + poly_uniform_4x(&rowa->vec[4], &rowa->vec[5], &rowa->vec[6], &rowb->vec[0], rho, 4, 5, 6, 256); + poly_nttunpack(&rowa->vec[0]); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowa->vec[5]); + poly_nttunpack(&rowa->vec[6]); + poly_nttunpack(&rowb->vec[0]); +} + +void polyvec_matrix_expand_row1(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[1], &rowa->vec[2], &rowa->vec[3], &rowa->vec[4], rho, 257, 258, 259, 260); + poly_uniform_4x(&rowa->vec[5], &rowa->vec[6], &rowb->vec[0], &rowb->vec[1], rho, 261, 262, 512, 513); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowa->vec[5]); + poly_nttunpack(&rowa->vec[6]); + poly_nttunpack(&rowb->vec[0]); + poly_nttunpack(&rowb->vec[1]); +} + +void polyvec_matrix_expand_row2(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[2], &rowa->vec[3], &rowa->vec[4], &rowa->vec[5], rho, 514, 515, 516, 517); + poly_uniform_4x(&rowa->vec[6], &rowb->vec[0], &rowb->vec[1], &rowb->vec[2], rho, 518, 768, 769, 770); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowa->vec[5]); + poly_nttunpack(&rowa->vec[6]); + poly_nttunpack(&rowb->vec[0]); + poly_nttunpack(&rowb->vec[1]); + poly_nttunpack(&rowb->vec[2]); +} + +void polyvec_matrix_expand_row3(polyvecl *rowa, __attribute__((unused)) polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[3], &rowa->vec[4], &rowa->vec[5], &rowa->vec[6], rho, 771, 772, 773, 774); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowa->vec[5]); + poly_nttunpack(&rowa->vec[6]); +} + +void polyvec_matrix_expand_row4(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[0], &rowa->vec[1], &rowa->vec[2], &rowa->vec[3], rho, 1024, 1025, 1026, 1027); + poly_uniform_4x(&rowa->vec[4], &rowa->vec[5], &rowa->vec[6], &rowb->vec[0], rho, 1028, 1029, 1030, 1280); + poly_nttunpack(&rowa->vec[0]); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowa->vec[5]); + poly_nttunpack(&rowa->vec[6]); + poly_nttunpack(&rowb->vec[0]); +} + +void polyvec_matrix_expand_row5(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[1], &rowa->vec[2], &rowa->vec[3], &rowa->vec[4], rho, 1281, 1282, 1283, 1284); + poly_uniform_4x(&rowa->vec[5], &rowa->vec[6], &rowb->vec[0], &rowb->vec[1], rho, 1285, 1286, 1536, 1537); + poly_nttunpack(&rowa->vec[1]); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowa->vec[5]); + poly_nttunpack(&rowa->vec[6]); + poly_nttunpack(&rowb->vec[0]); + poly_nttunpack(&rowb->vec[1]); +} + +void polyvec_matrix_expand_row6(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[2], &rowa->vec[3], &rowa->vec[4], &rowa->vec[5], rho, 1538, 1539, 1540, 1541); + poly_uniform_4x(&rowa->vec[6], &rowb->vec[0], &rowb->vec[1], &rowb->vec[2], rho, 1542, 1792, 1793, 1794); + poly_nttunpack(&rowa->vec[2]); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowa->vec[5]); + poly_nttunpack(&rowa->vec[6]); + poly_nttunpack(&rowb->vec[0]); + poly_nttunpack(&rowb->vec[1]); + poly_nttunpack(&rowb->vec[2]); +} + +void polyvec_matrix_expand_row7(polyvecl *rowa, __attribute__((unused)) polyvecl *rowb, const uint8_t rho[SEEDBYTES]) { + poly_uniform_4x(&rowa->vec[3], &rowa->vec[4], &rowa->vec[5], &rowa->vec[6], rho, 1795, 1796, 1797, 1798); + poly_nttunpack(&rowa->vec[3]); + poly_nttunpack(&rowa->vec[4]); + poly_nttunpack(&rowa->vec[5]); + poly_nttunpack(&rowa->vec[6]); +} + +#else +#error +#endif + +void polyvec_matrix_pointwise_montgomery(polyveck *t, const polyvecl mat[K], const polyvecl *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + polyvecl_pointwise_acc_montgomery(&t->vec[i], &mat[i], v); +} + +/**************************************************************/ +/************ Vectors of polynomials of length L **************/ +/**************************************************************/ + +void polyvecl_uniform_eta(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_uniform_eta(&v->vec[i], seed, nonce++); +} + +void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_uniform_gamma1(&v->vec[i], seed, L*nonce + i); +} + +void polyvecl_reduce(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_reduce(&v->vec[i]); +} + +/************************************************* +* Name: polyvecl_add +* +* Description: Add vectors of polynomials of length L. +* No modular reduction is performed. +* +* Arguments: - polyvecl *w: pointer to output vector +* - const polyvecl *u: pointer to first summand +* - const polyvecl *v: pointer to second summand +**************************************************/ +void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_add(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyvecl_ntt +* +* Description: Forward NTT of all polynomials in vector of length L. Output +* coefficients can be up to 16*Q larger than input coefficients. +* +* Arguments: - polyvecl *v: pointer to input/output vector +**************************************************/ +void polyvecl_ntt(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_ntt(&v->vec[i]); +} + +void polyvecl_invntt_tomont(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_invntt_tomont(&v->vec[i]); +} + +void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]); +} + +/************************************************* +* Name: polyvecl_pointwise_acc_montgomery +* +* Description: Pointwise multiply vectors of polynomials of length L, multiply +* resulting vector by 2^{-32} and add (accumulate) polynomials +* in it. Input/output vectors are in NTT domain representation. +* +* Arguments: - poly *w: output polynomial +* - const polyvecl *u: pointer to first input vector +* - const polyvecl *v: pointer to second input vector +**************************************************/ +void polyvecl_pointwise_acc_montgomery(poly *w, const polyvecl *u, const polyvecl *v) { + pointwise_acc_avx(w->vec, u->vec->vec, v->vec->vec, qdata.vec); +} + +/************************************************* +* Name: polyvecl_chknorm +* +* Description: Check infinity norm of polynomials in vector of length L. +* Assumes input polyvecl to be reduced by polyvecl_reduce(). +* +* Arguments: - const polyvecl *v: pointer to vector +* - int32_t B: norm bound +* +* Returns 0 if norm of all polynomials is strictly smaller than B <= (Q-1)/8 +* and 1 otherwise. +**************************************************/ +int polyvecl_chknorm(const polyvecl *v, int32_t bound) { + unsigned int i; + + for(i = 0; i < L; ++i) + if(poly_chknorm(&v->vec[i], bound)) + return 1; + + return 0; +} + +/**************************************************************/ +/************ Vectors of polynomials of length K **************/ +/**************************************************************/ + +void polyveck_uniform_eta(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_uniform_eta(&v->vec[i], seed, nonce++); +} + +/************************************************* +* Name: polyveck_reduce +* +* Description: Reduce coefficients of polynomials in vector of length K +* to representatives in [-6283009,6283008]. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_reduce(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_reduce(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_caddq +* +* Description: For all coefficients of polynomials in vector of length K +* add Q if coefficient is negative. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_caddq(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_caddq(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_add +* +* Description: Add vectors of polynomials of length K. +* No modular reduction is performed. +* +* Arguments: - polyveck *w: pointer to output vector +* - const polyveck *u: pointer to first summand +* - const polyveck *v: pointer to second summand +**************************************************/ +void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_add(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_sub +* +* Description: Subtract vectors of polynomials of length K. +* No modular reduction is performed. +* +* Arguments: - polyveck *w: pointer to output vector +* - const polyveck *u: pointer to first input vector +* - const polyveck *v: pointer to second input vector to be +* subtracted from first input vector +**************************************************/ +void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_sub(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_shiftl +* +* Description: Multiply vector of polynomials of Length K by 2^D without modular +* reduction. Assumes input coefficients to be less than 2^{31-D}. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_shiftl(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_shiftl(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_ntt +* +* Description: Forward NTT of all polynomials in vector of length K. Output +* coefficients can be up to 16*Q larger than input coefficients. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_ntt(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_ntt(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_invntt_tomont +* +* Description: Inverse NTT and multiplication by 2^{32} of polynomials +* in vector of length K. Input coefficients need to be less +* than 2*Q. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_invntt_tomont(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_invntt_tomont(&v->vec[i]); +} + +void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]); +} + +/************************************************* +* Name: polyveck_chknorm +* +* Description: Check infinity norm of polynomials in vector of length K. +* Assumes input polyveck to be reduced by polyveck_reduce(). +* +* Arguments: - const polyveck *v: pointer to vector +* - int32_t B: norm bound +* +* Returns 0 if norm of all polynomials are strictly smaller than B <= (Q-1)/8 +* and 1 otherwise. +**************************************************/ +int polyveck_chknorm(const polyveck *v, int32_t bound) { + unsigned int i; + + for(i = 0; i < K; ++i) + if(poly_chknorm(&v->vec[i], bound)) + return 1; + + return 0; +} + +/************************************************* +* Name: polyveck_power2round +* +* Description: For all coefficients a of polynomials in vector of length K, +* compute a0, a1 such that a mod^+ Q = a1*2^D + a0 +* with -2^{D-1} < a0 <= 2^{D-1}. Assumes coefficients to be +* standard representatives. +* +* Arguments: - polyveck *v1: pointer to output vector of polynomials with +* coefficients a1 +* - polyveck *v0: pointer to output vector of polynomials with +* coefficients a0 +* - const polyveck *v: pointer to input vector +**************************************************/ +void polyveck_power2round(polyveck *v1, polyveck *v0, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_power2round(&v1->vec[i], &v0->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_decompose +* +* Description: For all coefficients a of polynomials in vector of length K, +* compute high and low bits a0, a1 such a mod^+ Q = a1*ALPHA + a0 +* with -ALPHA/2 < a0 <= ALPHA/2 except a1 = (Q-1)/ALPHA where we +* set a1 = 0 and -ALPHA/2 <= a0 = a mod Q - Q < 0. +* Assumes coefficients to be standard representatives. +* +* Arguments: - polyveck *v1: pointer to output vector of polynomials with +* coefficients a1 +* - polyveck *v0: pointer to output vector of polynomials with +* coefficients a0 +* - const polyveck *v: pointer to input vector +**************************************************/ +void polyveck_decompose(polyveck *v1, polyveck *v0, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_decompose(&v1->vec[i], &v0->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_make_hint +* +* Description: Compute hint vector. +* +* Arguments: - uint8_t *hint: pointer to output hint array +* - const polyveck *v0: pointer to low part of input vector +* - const polyveck *v1: pointer to high part of input vector +* +* Returns number of 1 bits. +**************************************************/ +unsigned int polyveck_make_hint(uint8_t *hint, const polyveck *v0, const polyveck *v1) +{ + unsigned int i, n = 0; + + for(i = 0; i < K; ++i) + n += poly_make_hint(&hint[n], &v0->vec[i], &v1->vec[i]); + + return n; +} + +/************************************************* +* Name: polyveck_use_hint +* +* Description: Use hint vector to correct the high bits of input vector. +* +* Arguments: - polyveck *w: pointer to output vector of polynomials with +* corrected high bits +* - const polyveck *u: pointer to input vector +* - const polyveck *h: pointer to input hint vector +**************************************************/ +void polyveck_use_hint(polyveck *w, const polyveck *u, const polyveck *h) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_use_hint(&w->vec[i], &u->vec[i], &h->vec[i]); +} + +void polyveck_pack_w1(uint8_t r[K*POLYW1_PACKEDBYTES], const polyveck *w1) { + unsigned int i; + + for(i = 0; i < K; ++i) + polyw1_pack(&r[i*POLYW1_PACKEDBYTES], &w1->vec[i]); +} diff --git a/src/crypto/pq/_d/avx2/polyvec.h b/src/crypto/pq/_d/avx2/polyvec.h new file mode 100644 index 0000000..1b6dc87 --- /dev/null +++ b/src/crypto/pq/_d/avx2/polyvec.h @@ -0,0 +1,105 @@ +#ifndef POLYVEC_H +#define POLYVEC_H + +#include +#include "params.h" +#include "poly.h" + +/* Vectors of polynomials of length L */ +typedef struct { + poly vec[L]; +} polyvecl; + +#define polyvecl_uniform_eta DILITHIUM_NAMESPACE(polyvecl_uniform_eta) +void polyvecl_uniform_eta(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyvecl_uniform_gamma1 DILITHIUM_NAMESPACE(polyvecl_uniform_gamma1) +void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyvecl_reduce DILITHIUM_NAMESPACE(polyvecl_reduce) +void polyvecl_reduce(polyvecl *v); + +#define polyvecl_add DILITHIUM_NAMESPACE(polyvecl_add) +void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v); + +#define polyvecl_ntt DILITHIUM_NAMESPACE(polyvecl_ntt) +void polyvecl_ntt(polyvecl *v); +#define polyvecl_invntt_tomont DILITHIUM_NAMESPACE(polyvecl_invntt_tomont) +void polyvecl_invntt_tomont(polyvecl *v); +#define polyvecl_pointwise_poly_montgomery DILITHIUM_NAMESPACE(polyvecl_pointwise_poly_montgomery) +void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v); +#define polyvecl_pointwise_acc_montgomery \ + DILITHIUM_NAMESPACE(polyvecl_pointwise_acc_montgomery) +void polyvecl_pointwise_acc_montgomery(poly *w, + const polyvecl *u, + const polyvecl *v); + +#define polyvecl_chknorm DILITHIUM_NAMESPACE(polyvecl_chknorm) +int polyvecl_chknorm(const polyvecl *v, int32_t B); + +/* Vectors of polynomials of length K */ +typedef struct { + poly vec[K]; +} polyveck; + +#define polyveck_uniform_eta DILITHIUM_NAMESPACE(polyveck_uniform_eta) +void polyveck_uniform_eta(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyveck_reduce DILITHIUM_NAMESPACE(polyveck_reduce) +void polyveck_reduce(polyveck *v); +#define polyveck_caddq DILITHIUM_NAMESPACE(polyveck_caddq) +void polyveck_caddq(polyveck *v); + +#define polyveck_add DILITHIUM_NAMESPACE(polyveck_add) +void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v); +#define polyveck_sub DILITHIUM_NAMESPACE(polyveck_sub) +void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v); +#define polyveck_shiftl DILITHIUM_NAMESPACE(polyveck_shiftl) +void polyveck_shiftl(polyveck *v); + +#define polyveck_ntt DILITHIUM_NAMESPACE(polyveck_ntt) +void polyveck_ntt(polyveck *v); +#define polyveck_invntt_tomont DILITHIUM_NAMESPACE(polyveck_invntt_tomont) +void polyveck_invntt_tomont(polyveck *v); +#define polyveck_pointwise_poly_montgomery DILITHIUM_NAMESPACE(polyveck_pointwise_poly_montgomery) +void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v); + +#define polyveck_chknorm DILITHIUM_NAMESPACE(polyveck_chknorm) +int polyveck_chknorm(const polyveck *v, int32_t B); + +#define polyveck_power2round DILITHIUM_NAMESPACE(polyveck_power2round) +void polyveck_power2round(polyveck *v1, polyveck *v0, const polyveck *v); +#define polyveck_decompose DILITHIUM_NAMESPACE(polyveck_decompose) +void polyveck_decompose(polyveck *v1, polyveck *v0, const polyveck *v); +#define polyveck_make_hint DILITHIUM_NAMESPACE(polyveck_make_hint) +unsigned int polyveck_make_hint(uint8_t *hint, const polyveck *v0, const polyveck *v1); +#define polyveck_use_hint DILITHIUM_NAMESPACE(polyveck_use_hint) +void polyveck_use_hint(polyveck *w, const polyveck *v, const polyveck *h); + +#define polyveck_pack_w1 DILITHIUM_NAMESPACE(polyveck_pack_w1) +void polyveck_pack_w1(uint8_t r[K*POLYW1_PACKEDBYTES], const polyveck *w1); + +#define polyvec_matrix_expand DILITHIUM_NAMESPACE(polyvec_matrix_expand) +void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]); + +#define polyvec_matrix_expand_row0 DILITHIUM_NAMESPACE(polyvec_matrix_expand_row0) +void polyvec_matrix_expand_row0(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]); +#define polyvec_matrix_expand_row1 DILITHIUM_NAMESPACE(polyvec_matrix_expand_row1) +void polyvec_matrix_expand_row1(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]); +#define polyvec_matrix_expand_row2 DILITHIUM_NAMESPACE(polyvec_matrix_expand_row2) +void polyvec_matrix_expand_row2(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]); +#define polyvec_matrix_expand_row3 DILITHIUM_NAMESPACE(polyvec_matrix_expand_row3) +void polyvec_matrix_expand_row3(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]); +#define polyvec_matrix_expand_row4 DILITHIUM_NAMESPACE(polyvec_matrix_expand_row4) +void polyvec_matrix_expand_row4(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]); +#define polyvec_matrix_expand_row5 DILITHIUM_NAMESPACE(polyvec_matrix_expand_row5) +void polyvec_matrix_expand_row5(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]); +#define polyvec_matrix_expand_row6 DILITHIUM_NAMESPACE(polyvec_matrix_expand_row6) +void polyvec_matrix_expand_row6(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]); +#define polyvec_matrix_expand_row7 DILITHIUM_NAMESPACE(polyvec_matrix_expand_row7) +void polyvec_matrix_expand_row7(polyvecl *rowa, polyvecl *rowb, const uint8_t rho[SEEDBYTES]); + +#define polyvec_matrix_pointwise_montgomery DILITHIUM_NAMESPACE(polyvec_matrix_pointwise_montgomery) +void polyvec_matrix_pointwise_montgomery(polyveck *t, const polyvecl mat[K], const polyvecl *v); + +#endif diff --git a/src/crypto/pq/_d/avx2/randombytes.c b/src/crypto/pq/_d/avx2/randombytes.c new file mode 120000 index 0000000..59a42a5 --- /dev/null +++ b/src/crypto/pq/_d/avx2/randombytes.c @@ -0,0 +1 @@ +../ref/randombytes.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/randombytes.h b/src/crypto/pq/_d/avx2/randombytes.h new file mode 120000 index 0000000..055e443 --- /dev/null +++ b/src/crypto/pq/_d/avx2/randombytes.h @@ -0,0 +1 @@ +../ref/randombytes.h \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/rejsample.c b/src/crypto/pq/_d/avx2/rejsample.c new file mode 100644 index 0000000..8b1dde4 --- /dev/null +++ b/src/crypto/pq/_d/avx2/rejsample.c @@ -0,0 +1,476 @@ +#include +#include +#include "params.h" +#include "rejsample.h" +#include "symmetric.h" + +const uint8_t idxlut[256][8] = { + { 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0}, + { 1, 0, 0, 0, 0, 0, 0, 0}, + { 0, 1, 0, 0, 0, 0, 0, 0}, + { 2, 0, 0, 0, 0, 0, 0, 0}, + { 0, 2, 0, 0, 0, 0, 0, 0}, + { 1, 2, 0, 0, 0, 0, 0, 0}, + { 0, 1, 2, 0, 0, 0, 0, 0}, + { 3, 0, 0, 0, 0, 0, 0, 0}, + { 0, 3, 0, 0, 0, 0, 0, 0}, + { 1, 3, 0, 0, 0, 0, 0, 0}, + { 0, 1, 3, 0, 0, 0, 0, 0}, + { 2, 3, 0, 0, 0, 0, 0, 0}, + { 0, 2, 3, 0, 0, 0, 0, 0}, + { 1, 2, 3, 0, 0, 0, 0, 0}, + { 0, 1, 2, 3, 0, 0, 0, 0}, + { 4, 0, 0, 0, 0, 0, 0, 0}, + { 0, 4, 0, 0, 0, 0, 0, 0}, + { 1, 4, 0, 0, 0, 0, 0, 0}, + { 0, 1, 4, 0, 0, 0, 0, 0}, + { 2, 4, 0, 0, 0, 0, 0, 0}, + { 0, 2, 4, 0, 0, 0, 0, 0}, + { 1, 2, 4, 0, 0, 0, 0, 0}, + { 0, 1, 2, 4, 0, 0, 0, 0}, + { 3, 4, 0, 0, 0, 0, 0, 0}, + { 0, 3, 4, 0, 0, 0, 0, 0}, + { 1, 3, 4, 0, 0, 0, 0, 0}, + { 0, 1, 3, 4, 0, 0, 0, 0}, + { 2, 3, 4, 0, 0, 0, 0, 0}, + { 0, 2, 3, 4, 0, 0, 0, 0}, + { 1, 2, 3, 4, 0, 0, 0, 0}, + { 0, 1, 2, 3, 4, 0, 0, 0}, + { 5, 0, 0, 0, 0, 0, 0, 0}, + { 0, 5, 0, 0, 0, 0, 0, 0}, + { 1, 5, 0, 0, 0, 0, 0, 0}, + { 0, 1, 5, 0, 0, 0, 0, 0}, + { 2, 5, 0, 0, 0, 0, 0, 0}, + { 0, 2, 5, 0, 0, 0, 0, 0}, + { 1, 2, 5, 0, 0, 0, 0, 0}, + { 0, 1, 2, 5, 0, 0, 0, 0}, + { 3, 5, 0, 0, 0, 0, 0, 0}, + { 0, 3, 5, 0, 0, 0, 0, 0}, + { 1, 3, 5, 0, 0, 0, 0, 0}, + { 0, 1, 3, 5, 0, 0, 0, 0}, + { 2, 3, 5, 0, 0, 0, 0, 0}, + { 0, 2, 3, 5, 0, 0, 0, 0}, + { 1, 2, 3, 5, 0, 0, 0, 0}, + { 0, 1, 2, 3, 5, 0, 0, 0}, + { 4, 5, 0, 0, 0, 0, 0, 0}, + { 0, 4, 5, 0, 0, 0, 0, 0}, + { 1, 4, 5, 0, 0, 0, 0, 0}, + { 0, 1, 4, 5, 0, 0, 0, 0}, + { 2, 4, 5, 0, 0, 0, 0, 0}, + { 0, 2, 4, 5, 0, 0, 0, 0}, + { 1, 2, 4, 5, 0, 0, 0, 0}, + { 0, 1, 2, 4, 5, 0, 0, 0}, + { 3, 4, 5, 0, 0, 0, 0, 0}, + { 0, 3, 4, 5, 0, 0, 0, 0}, + { 1, 3, 4, 5, 0, 0, 0, 0}, + { 0, 1, 3, 4, 5, 0, 0, 0}, + { 2, 3, 4, 5, 0, 0, 0, 0}, + { 0, 2, 3, 4, 5, 0, 0, 0}, + { 1, 2, 3, 4, 5, 0, 0, 0}, + { 0, 1, 2, 3, 4, 5, 0, 0}, + { 6, 0, 0, 0, 0, 0, 0, 0}, + { 0, 6, 0, 0, 0, 0, 0, 0}, + { 1, 6, 0, 0, 0, 0, 0, 0}, + { 0, 1, 6, 0, 0, 0, 0, 0}, + { 2, 6, 0, 0, 0, 0, 0, 0}, + { 0, 2, 6, 0, 0, 0, 0, 0}, + { 1, 2, 6, 0, 0, 0, 0, 0}, + { 0, 1, 2, 6, 0, 0, 0, 0}, + { 3, 6, 0, 0, 0, 0, 0, 0}, + { 0, 3, 6, 0, 0, 0, 0, 0}, + { 1, 3, 6, 0, 0, 0, 0, 0}, + { 0, 1, 3, 6, 0, 0, 0, 0}, + { 2, 3, 6, 0, 0, 0, 0, 0}, + { 0, 2, 3, 6, 0, 0, 0, 0}, + { 1, 2, 3, 6, 0, 0, 0, 0}, + { 0, 1, 2, 3, 6, 0, 0, 0}, + { 4, 6, 0, 0, 0, 0, 0, 0}, + { 0, 4, 6, 0, 0, 0, 0, 0}, + { 1, 4, 6, 0, 0, 0, 0, 0}, + { 0, 1, 4, 6, 0, 0, 0, 0}, + { 2, 4, 6, 0, 0, 0, 0, 0}, + { 0, 2, 4, 6, 0, 0, 0, 0}, + { 1, 2, 4, 6, 0, 0, 0, 0}, + { 0, 1, 2, 4, 6, 0, 0, 0}, + { 3, 4, 6, 0, 0, 0, 0, 0}, + { 0, 3, 4, 6, 0, 0, 0, 0}, + { 1, 3, 4, 6, 0, 0, 0, 0}, + { 0, 1, 3, 4, 6, 0, 0, 0}, + { 2, 3, 4, 6, 0, 0, 0, 0}, + { 0, 2, 3, 4, 6, 0, 0, 0}, + { 1, 2, 3, 4, 6, 0, 0, 0}, + { 0, 1, 2, 3, 4, 6, 0, 0}, + { 5, 6, 0, 0, 0, 0, 0, 0}, + { 0, 5, 6, 0, 0, 0, 0, 0}, + { 1, 5, 6, 0, 0, 0, 0, 0}, + { 0, 1, 5, 6, 0, 0, 0, 0}, + { 2, 5, 6, 0, 0, 0, 0, 0}, + { 0, 2, 5, 6, 0, 0, 0, 0}, + { 1, 2, 5, 6, 0, 0, 0, 0}, + { 0, 1, 2, 5, 6, 0, 0, 0}, + { 3, 5, 6, 0, 0, 0, 0, 0}, + { 0, 3, 5, 6, 0, 0, 0, 0}, + { 1, 3, 5, 6, 0, 0, 0, 0}, + { 0, 1, 3, 5, 6, 0, 0, 0}, + { 2, 3, 5, 6, 0, 0, 0, 0}, + { 0, 2, 3, 5, 6, 0, 0, 0}, + { 1, 2, 3, 5, 6, 0, 0, 0}, + { 0, 1, 2, 3, 5, 6, 0, 0}, + { 4, 5, 6, 0, 0, 0, 0, 0}, + { 0, 4, 5, 6, 0, 0, 0, 0}, + { 1, 4, 5, 6, 0, 0, 0, 0}, + { 0, 1, 4, 5, 6, 0, 0, 0}, + { 2, 4, 5, 6, 0, 0, 0, 0}, + { 0, 2, 4, 5, 6, 0, 0, 0}, + { 1, 2, 4, 5, 6, 0, 0, 0}, + { 0, 1, 2, 4, 5, 6, 0, 0}, + { 3, 4, 5, 6, 0, 0, 0, 0}, + { 0, 3, 4, 5, 6, 0, 0, 0}, + { 1, 3, 4, 5, 6, 0, 0, 0}, + { 0, 1, 3, 4, 5, 6, 0, 0}, + { 2, 3, 4, 5, 6, 0, 0, 0}, + { 0, 2, 3, 4, 5, 6, 0, 0}, + { 1, 2, 3, 4, 5, 6, 0, 0}, + { 0, 1, 2, 3, 4, 5, 6, 0}, + { 7, 0, 0, 0, 0, 0, 0, 0}, + { 0, 7, 0, 0, 0, 0, 0, 0}, + { 1, 7, 0, 0, 0, 0, 0, 0}, + { 0, 1, 7, 0, 0, 0, 0, 0}, + { 2, 7, 0, 0, 0, 0, 0, 0}, + { 0, 2, 7, 0, 0, 0, 0, 0}, + { 1, 2, 7, 0, 0, 0, 0, 0}, + { 0, 1, 2, 7, 0, 0, 0, 0}, + { 3, 7, 0, 0, 0, 0, 0, 0}, + { 0, 3, 7, 0, 0, 0, 0, 0}, + { 1, 3, 7, 0, 0, 0, 0, 0}, + { 0, 1, 3, 7, 0, 0, 0, 0}, + { 2, 3, 7, 0, 0, 0, 0, 0}, + { 0, 2, 3, 7, 0, 0, 0, 0}, + { 1, 2, 3, 7, 0, 0, 0, 0}, + { 0, 1, 2, 3, 7, 0, 0, 0}, + { 4, 7, 0, 0, 0, 0, 0, 0}, + { 0, 4, 7, 0, 0, 0, 0, 0}, + { 1, 4, 7, 0, 0, 0, 0, 0}, + { 0, 1, 4, 7, 0, 0, 0, 0}, + { 2, 4, 7, 0, 0, 0, 0, 0}, + { 0, 2, 4, 7, 0, 0, 0, 0}, + { 1, 2, 4, 7, 0, 0, 0, 0}, + { 0, 1, 2, 4, 7, 0, 0, 0}, + { 3, 4, 7, 0, 0, 0, 0, 0}, + { 0, 3, 4, 7, 0, 0, 0, 0}, + { 1, 3, 4, 7, 0, 0, 0, 0}, + { 0, 1, 3, 4, 7, 0, 0, 0}, + { 2, 3, 4, 7, 0, 0, 0, 0}, + { 0, 2, 3, 4, 7, 0, 0, 0}, + { 1, 2, 3, 4, 7, 0, 0, 0}, + { 0, 1, 2, 3, 4, 7, 0, 0}, + { 5, 7, 0, 0, 0, 0, 0, 0}, + { 0, 5, 7, 0, 0, 0, 0, 0}, + { 1, 5, 7, 0, 0, 0, 0, 0}, + { 0, 1, 5, 7, 0, 0, 0, 0}, + { 2, 5, 7, 0, 0, 0, 0, 0}, + { 0, 2, 5, 7, 0, 0, 0, 0}, + { 1, 2, 5, 7, 0, 0, 0, 0}, + { 0, 1, 2, 5, 7, 0, 0, 0}, + { 3, 5, 7, 0, 0, 0, 0, 0}, + { 0, 3, 5, 7, 0, 0, 0, 0}, + { 1, 3, 5, 7, 0, 0, 0, 0}, + { 0, 1, 3, 5, 7, 0, 0, 0}, + { 2, 3, 5, 7, 0, 0, 0, 0}, + { 0, 2, 3, 5, 7, 0, 0, 0}, + { 1, 2, 3, 5, 7, 0, 0, 0}, + { 0, 1, 2, 3, 5, 7, 0, 0}, + { 4, 5, 7, 0, 0, 0, 0, 0}, + { 0, 4, 5, 7, 0, 0, 0, 0}, + { 1, 4, 5, 7, 0, 0, 0, 0}, + { 0, 1, 4, 5, 7, 0, 0, 0}, + { 2, 4, 5, 7, 0, 0, 0, 0}, + { 0, 2, 4, 5, 7, 0, 0, 0}, + { 1, 2, 4, 5, 7, 0, 0, 0}, + { 0, 1, 2, 4, 5, 7, 0, 0}, + { 3, 4, 5, 7, 0, 0, 0, 0}, + { 0, 3, 4, 5, 7, 0, 0, 0}, + { 1, 3, 4, 5, 7, 0, 0, 0}, + { 0, 1, 3, 4, 5, 7, 0, 0}, + { 2, 3, 4, 5, 7, 0, 0, 0}, + { 0, 2, 3, 4, 5, 7, 0, 0}, + { 1, 2, 3, 4, 5, 7, 0, 0}, + { 0, 1, 2, 3, 4, 5, 7, 0}, + { 6, 7, 0, 0, 0, 0, 0, 0}, + { 0, 6, 7, 0, 0, 0, 0, 0}, + { 1, 6, 7, 0, 0, 0, 0, 0}, + { 0, 1, 6, 7, 0, 0, 0, 0}, + { 2, 6, 7, 0, 0, 0, 0, 0}, + { 0, 2, 6, 7, 0, 0, 0, 0}, + { 1, 2, 6, 7, 0, 0, 0, 0}, + { 0, 1, 2, 6, 7, 0, 0, 0}, + { 3, 6, 7, 0, 0, 0, 0, 0}, + { 0, 3, 6, 7, 0, 0, 0, 0}, + { 1, 3, 6, 7, 0, 0, 0, 0}, + { 0, 1, 3, 6, 7, 0, 0, 0}, + { 2, 3, 6, 7, 0, 0, 0, 0}, + { 0, 2, 3, 6, 7, 0, 0, 0}, + { 1, 2, 3, 6, 7, 0, 0, 0}, + { 0, 1, 2, 3, 6, 7, 0, 0}, + { 4, 6, 7, 0, 0, 0, 0, 0}, + { 0, 4, 6, 7, 0, 0, 0, 0}, + { 1, 4, 6, 7, 0, 0, 0, 0}, + { 0, 1, 4, 6, 7, 0, 0, 0}, + { 2, 4, 6, 7, 0, 0, 0, 0}, + { 0, 2, 4, 6, 7, 0, 0, 0}, + { 1, 2, 4, 6, 7, 0, 0, 0}, + { 0, 1, 2, 4, 6, 7, 0, 0}, + { 3, 4, 6, 7, 0, 0, 0, 0}, + { 0, 3, 4, 6, 7, 0, 0, 0}, + { 1, 3, 4, 6, 7, 0, 0, 0}, + { 0, 1, 3, 4, 6, 7, 0, 0}, + { 2, 3, 4, 6, 7, 0, 0, 0}, + { 0, 2, 3, 4, 6, 7, 0, 0}, + { 1, 2, 3, 4, 6, 7, 0, 0}, + { 0, 1, 2, 3, 4, 6, 7, 0}, + { 5, 6, 7, 0, 0, 0, 0, 0}, + { 0, 5, 6, 7, 0, 0, 0, 0}, + { 1, 5, 6, 7, 0, 0, 0, 0}, + { 0, 1, 5, 6, 7, 0, 0, 0}, + { 2, 5, 6, 7, 0, 0, 0, 0}, + { 0, 2, 5, 6, 7, 0, 0, 0}, + { 1, 2, 5, 6, 7, 0, 0, 0}, + { 0, 1, 2, 5, 6, 7, 0, 0}, + { 3, 5, 6, 7, 0, 0, 0, 0}, + { 0, 3, 5, 6, 7, 0, 0, 0}, + { 1, 3, 5, 6, 7, 0, 0, 0}, + { 0, 1, 3, 5, 6, 7, 0, 0}, + { 2, 3, 5, 6, 7, 0, 0, 0}, + { 0, 2, 3, 5, 6, 7, 0, 0}, + { 1, 2, 3, 5, 6, 7, 0, 0}, + { 0, 1, 2, 3, 5, 6, 7, 0}, + { 4, 5, 6, 7, 0, 0, 0, 0}, + { 0, 4, 5, 6, 7, 0, 0, 0}, + { 1, 4, 5, 6, 7, 0, 0, 0}, + { 0, 1, 4, 5, 6, 7, 0, 0}, + { 2, 4, 5, 6, 7, 0, 0, 0}, + { 0, 2, 4, 5, 6, 7, 0, 0}, + { 1, 2, 4, 5, 6, 7, 0, 0}, + { 0, 1, 2, 4, 5, 6, 7, 0}, + { 3, 4, 5, 6, 7, 0, 0, 0}, + { 0, 3, 4, 5, 6, 7, 0, 0}, + { 1, 3, 4, 5, 6, 7, 0, 0}, + { 0, 1, 3, 4, 5, 6, 7, 0}, + { 2, 3, 4, 5, 6, 7, 0, 0}, + { 0, 2, 3, 4, 5, 6, 7, 0}, + { 1, 2, 3, 4, 5, 6, 7, 0}, + { 0, 1, 2, 3, 4, 5, 6, 7} +}; + +unsigned int rej_uniform_avx(int32_t * restrict r, const uint8_t buf[REJ_UNIFORM_BUFLEN+8]) +{ + unsigned int ctr, pos; + uint32_t good; + __m256i d, tmp; + const __m256i bound = _mm256_set1_epi32(Q); + const __m256i mask = _mm256_set1_epi32(0x7FFFFF); + const __m256i idx8 = _mm256_set_epi8(-1,15,14,13,-1,12,11,10, + -1, 9, 8, 7,-1, 6, 5, 4, + -1,11,10, 9,-1, 8, 7, 6, + -1, 5, 4, 3,-1, 2, 1, 0); + + ctr = pos = 0; + while(pos <= REJ_UNIFORM_BUFLEN - 24) { + d = _mm256_loadu_si256((__m256i *)&buf[pos]); + d = _mm256_permute4x64_epi64(d, 0x94); + d = _mm256_shuffle_epi8(d, idx8); + d = _mm256_and_si256(d, mask); + pos += 24; + + tmp = _mm256_sub_epi32(d, bound); + good = _mm256_movemask_ps((__m256)tmp); + tmp = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i *)&idxlut[good])); + d = _mm256_permutevar8x32_epi32(d, tmp); + + _mm256_storeu_si256((__m256i *)&r[ctr], d); + ctr += _mm_popcnt_u32(good); + + if(ctr > N - 8) break; + } + + uint32_t t; + while(ctr < N && pos <= REJ_UNIFORM_BUFLEN - 3) { + t = buf[pos++]; + t |= (uint32_t)buf[pos++] << 8; + t |= (uint32_t)buf[pos++] << 16; + t &= 0x7FFFFF; + + if(t < Q) + r[ctr++] = t; + } + + return ctr; +} + +#if ETA == 2 +unsigned int rej_eta_avx(int32_t * restrict r, const uint8_t buf[REJ_UNIFORM_ETA_BUFLEN]) { + unsigned int ctr, pos; + uint32_t good; + __m256i f0, f1, f2; + __m128i g0, g1; + const __m256i mask = _mm256_set1_epi8(15); + const __m256i eta = _mm256_set1_epi8(ETA); + const __m256i bound = mask; + const __m256i v = _mm256_set1_epi32(-6560); + const __m256i p = _mm256_set1_epi32(5); + + ctr = pos = 0; + while(ctr <= N - 8 && pos <= REJ_UNIFORM_ETA_BUFLEN - 16) { + f0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((__m128i *)&buf[pos])); + f1 = _mm256_slli_epi16(f0,4); + f0 = _mm256_or_si256(f0,f1); + f0 = _mm256_and_si256(f0,mask); + + f1 = _mm256_sub_epi8(f0,bound); + f0 = _mm256_sub_epi8(eta,f0); + good = _mm256_movemask_epi8(f1); + + g0 = _mm256_castsi256_si128(f0); + g1 = _mm_loadl_epi64((__m128i *)&idxlut[good & 0xFF]); + g1 = _mm_shuffle_epi8(g0,g1); + f1 = _mm256_cvtepi8_epi32(g1); + f2 = _mm256_mulhrs_epi16(f1,v); + f2 = _mm256_mullo_epi16(f2,p); + f1 = _mm256_add_epi32(f1,f2); + _mm256_storeu_si256((__m256i *)&r[ctr],f1); + ctr += _mm_popcnt_u32(good & 0xFF); + good >>= 8; + pos += 4; + + if(ctr > N - 8) break; + g0 = _mm_bsrli_si128(g0,8); + g1 = _mm_loadl_epi64((__m128i *)&idxlut[good & 0xFF]); + g1 = _mm_shuffle_epi8(g0,g1); + f1 = _mm256_cvtepi8_epi32(g1); + f2 = _mm256_mulhrs_epi16(f1,v); + f2 = _mm256_mullo_epi16(f2,p); + f1 = _mm256_add_epi32(f1,f2); + _mm256_storeu_si256((__m256i *)&r[ctr],f1); + ctr += _mm_popcnt_u32(good & 0xFF); + good >>= 8; + pos += 4; + + if(ctr > N - 8) break; + g0 = _mm256_extracti128_si256(f0,1); + g1 = _mm_loadl_epi64((__m128i *)&idxlut[good & 0xFF]); + g1 = _mm_shuffle_epi8(g0,g1); + f1 = _mm256_cvtepi8_epi32(g1); + f2 = _mm256_mulhrs_epi16(f1,v); + f2 = _mm256_mullo_epi16(f2,p); + f1 = _mm256_add_epi32(f1,f2); + _mm256_storeu_si256((__m256i *)&r[ctr],f1); + ctr += _mm_popcnt_u32(good & 0xFF); + good >>= 8; + pos += 4; + + if(ctr > N - 8) break; + g0 = _mm_bsrli_si128(g0,8); + g1 = _mm_loadl_epi64((__m128i *)&idxlut[good]); + g1 = _mm_shuffle_epi8(g0,g1); + f1 = _mm256_cvtepi8_epi32(g1); + f2 = _mm256_mulhrs_epi16(f1,v); + f2 = _mm256_mullo_epi16(f2,p); + f1 = _mm256_add_epi32(f1,f2); + _mm256_storeu_si256((__m256i *)&r[ctr],f1); + ctr += _mm_popcnt_u32(good); + pos += 4; + } + + uint32_t t0, t1; + while(ctr < N && pos < REJ_UNIFORM_ETA_BUFLEN) { + t0 = buf[pos] & 0x0F; + t1 = buf[pos++] >> 4; + + if(t0 < 15) { + t0 = t0 - (205*t0 >> 10)*5; + r[ctr++] = 2 - t0; + } + if(t1 < 15 && ctr < N) { + t1 = t1 - (205*t1 >> 10)*5; + r[ctr++] = 2 - t1; + } + } + + return ctr; +} + +#elif ETA == 4 +unsigned int rej_eta_avx(int32_t * restrict r, const uint8_t buf[REJ_UNIFORM_ETA_BUFLEN]) { + unsigned int ctr, pos; + uint32_t good; + __m256i f0, f1; + __m128i g0, g1; + const __m256i mask = _mm256_set1_epi8(15); + const __m256i eta = _mm256_set1_epi8(4); + const __m256i bound = _mm256_set1_epi8(9); + + ctr = pos = 0; + while(ctr <= N - 8 && pos <= REJ_UNIFORM_ETA_BUFLEN - 16) { + f0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((__m128i *)&buf[pos])); + f1 = _mm256_slli_epi16(f0,4); + f0 = _mm256_or_si256(f0,f1); + f0 = _mm256_and_si256(f0,mask); + + f1 = _mm256_sub_epi8(f0,bound); + f0 = _mm256_sub_epi8(eta,f0); + good = _mm256_movemask_epi8(f1); + + g0 = _mm256_castsi256_si128(f0); + g1 = _mm_loadl_epi64((__m128i *)&idxlut[good & 0xFF]); + g1 = _mm_shuffle_epi8(g0,g1); + f1 = _mm256_cvtepi8_epi32(g1); + _mm256_storeu_si256((__m256i *)&r[ctr],f1); + ctr += _mm_popcnt_u32(good & 0xFF); + good >>= 8; + pos += 4; + + if(ctr > N - 8) break; + g0 = _mm_bsrli_si128(g0,8); + g1 = _mm_loadl_epi64((__m128i *)&idxlut[good & 0xFF]); + g1 = _mm_shuffle_epi8(g0,g1); + f1 = _mm256_cvtepi8_epi32(g1); + _mm256_storeu_si256((__m256i *)&r[ctr],f1); + ctr += _mm_popcnt_u32(good & 0xFF); + good >>= 8; + pos += 4; + + if(ctr > N - 8) break; + g0 = _mm256_extracti128_si256(f0,1); + g1 = _mm_loadl_epi64((__m128i *)&idxlut[good & 0xFF]); + g1 = _mm_shuffle_epi8(g0,g1); + f1 = _mm256_cvtepi8_epi32(g1); + _mm256_storeu_si256((__m256i *)&r[ctr],f1); + ctr += _mm_popcnt_u32(good & 0xFF); + good >>= 8; + pos += 4; + + if(ctr > N - 8) break; + g0 = _mm_bsrli_si128(g0,8); + g1 = _mm_loadl_epi64((__m128i *)&idxlut[good]); + g1 = _mm_shuffle_epi8(g0,g1); + f1 = _mm256_cvtepi8_epi32(g1); + _mm256_storeu_si256((__m256i *)&r[ctr],f1); + ctr += _mm_popcnt_u32(good); + pos += 4; + } + + uint32_t t0, t1; + while(ctr < N && pos < REJ_UNIFORM_ETA_BUFLEN) { + t0 = buf[pos] & 0x0F; + t1 = buf[pos++] >> 4; + + if(t0 < 9) + r[ctr++] = 4 - t0; + if(t1 < 9 && ctr < N) + r[ctr++] = 4 - t1; + } + + return ctr; +} +#endif diff --git a/src/crypto/pq/_d/avx2/rejsample.h b/src/crypto/pq/_d/avx2/rejsample.h new file mode 100644 index 0000000..61f3f35 --- /dev/null +++ b/src/crypto/pq/_d/avx2/rejsample.h @@ -0,0 +1,28 @@ +#ifndef REJSAMPLE_H +#define REJSAMPLE_H + +#include +#include "params.h" +#include "symmetric.h" + +#define REJ_UNIFORM_NBLOCKS ((768+STREAM128_BLOCKBYTES-1)/STREAM128_BLOCKBYTES) +#define REJ_UNIFORM_BUFLEN (REJ_UNIFORM_NBLOCKS*STREAM128_BLOCKBYTES) + +#if ETA == 2 +#define REJ_UNIFORM_ETA_NBLOCKS ((136+STREAM256_BLOCKBYTES-1)/STREAM256_BLOCKBYTES) +#elif ETA == 4 +#define REJ_UNIFORM_ETA_NBLOCKS ((227+STREAM256_BLOCKBYTES-1)/STREAM256_BLOCKBYTES) +#endif +#define REJ_UNIFORM_ETA_BUFLEN (REJ_UNIFORM_ETA_NBLOCKS*STREAM256_BLOCKBYTES) + +#define idxlut DILITHIUM_NAMESPACE(idxlut) +extern const uint8_t idxlut[256][8]; + +#define rej_uniform_avx DILITHIUM_NAMESPACE(rej_uniform_avx) +unsigned int rej_uniform_avx(int32_t *r, const uint8_t buf[REJ_UNIFORM_BUFLEN+8]); + +#define rej_eta_avx DILITHIUM_NAMESPACE(rej_eta_avx) +unsigned int rej_eta_avx(int32_t *r, const uint8_t buf[REJ_UNIFORM_ETA_BUFLEN]); + +#endif + diff --git a/src/crypto/pq/_d/avx2/rounding.c b/src/crypto/pq/_d/avx2/rounding.c new file mode 100644 index 0000000..0aec74b --- /dev/null +++ b/src/crypto/pq/_d/avx2/rounding.c @@ -0,0 +1,201 @@ +#include +#include +#include +#include "params.h" +#include "rounding.h" +#include "rejsample.h" +#include "consts.h" + +#define _mm256_blendv_epi32(a,b,mask) \ + _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(a), \ + _mm256_castsi256_ps(b), \ + _mm256_castsi256_ps(mask))) + +/************************************************* +* Name: power2round +* +* Description: For finite field elements a, compute a0, a1 such that +* a mod^+ Q = a1*2^D + a0 with -2^{D-1} < a0 <= 2^{D-1}. +* Assumes a to be positive standard representative. +* +* Arguments: - __m256i *a1: output array of length N/8 with high bits +* - __m256i *a0: output array of length N/8 with low bits a0 +* - const __m256i *a: input array of length N/8 +* +**************************************************/ +void power2round_avx(__m256i *a1, __m256i *a0, const __m256i *a) +{ + unsigned int i; + __m256i f,f0,f1; + const __m256i mask = _mm256_set1_epi32(-(1 << D)); + const __m256i half = _mm256_set1_epi32((1 << (D-1)) - 1); + + for(i = 0; i < N/8; ++i) { + f = _mm256_load_si256(&a[i]); + f1 = _mm256_add_epi32(f,half); + f0 = _mm256_and_si256(f1,mask); + f1 = _mm256_srli_epi32(f1,D); + f0 = _mm256_sub_epi32(f,f0); + _mm256_store_si256(&a1[i],f1); + _mm256_store_si256(&a0[i],f0); + } +} + +/************************************************* +* Name: decompose +* +* Description: For finite field element a, compute high and low parts a0, a1 such +* that a mod^+ Q = a1*ALPHA + a0 with -ALPHA/2 < a0 <= ALPHA/2 except +* if a1 = (Q-1)/ALPHA where we set a1 = 0 and +* -ALPHA/2 <= a0 = a mod Q - Q < 0. Assumes a to be positive standard +* representative. +* +* Arguments: - __m256i *a1: output array of length N/8 with high parts +* - __m256i *a0: output array of length N/8 with low parts a0 +* - const __m256i *a: input array of length N/8 +* +**************************************************/ +#if GAMMA2 == (Q-1)/32 +void decompose_avx(__m256i *a1, __m256i *a0, const __m256i *a) +{ + unsigned int i; + __m256i f,f0,f1; + const __m256i q = _mm256_load_si256(&qdata.vec[_8XQ/8]); + const __m256i hq = _mm256_srli_epi32(q,1); + const __m256i v = _mm256_set1_epi32(1025); + const __m256i alpha = _mm256_set1_epi32(2*GAMMA2); + const __m256i off = _mm256_set1_epi32(127); + const __m256i shift = _mm256_set1_epi32(512); + const __m256i mask = _mm256_set1_epi32(15); + + for(i=0;i +#include +#include "params.h" + +#define power2round_avx DILITHIUM_NAMESPACE(power2round_avx) +void power2round_avx(__m256i *a1, __m256i *a0, const __m256i *a); +#define decompose_avx DILITHIUM_NAMESPACE(decompose_avx) +void decompose_avx(__m256i *a1, __m256i *a0, const __m256i *a); +#define make_hint_avx DILITHIUM_NAMESPACE(make_hint_avx) +unsigned int make_hint_avx(uint8_t hint[N], const __m256i *a0, const __m256i *a1); +#define use_hint_avx DILITHIUM_NAMESPACE(use_hint_avx) +void use_hint_avx(__m256i *b, const __m256i *a, const __m256i *hint); + +#endif diff --git a/src/crypto/pq/_d/avx2/shuffle.S b/src/crypto/pq/_d/avx2/shuffle.S new file mode 100644 index 0000000..08c757c --- /dev/null +++ b/src/crypto/pq/_d/avx2/shuffle.S @@ -0,0 +1,54 @@ +#include "consts.h" +.include "shuffle.inc" + +.text +nttunpack128_avx: +#load +vmovdqa (%rdi),%ymm4 +vmovdqa 32(%rdi),%ymm5 +vmovdqa 64(%rdi),%ymm6 +vmovdqa 96(%rdi),%ymm7 +vmovdqa 128(%rdi),%ymm8 +vmovdqa 160(%rdi),%ymm9 +vmovdqa 192(%rdi),%ymm10 +vmovdqa 224(%rdi),%ymm11 + +shuffle8 4,8,3,8 +shuffle8 5,9,4,9 +shuffle8 6,10,5,10 +shuffle8 7,11,6,11 + +shuffle4 3,5,7,5 +shuffle4 8,10,3,10 +shuffle4 4,6,8,6 +shuffle4 9,11,4,11 + +shuffle2 7,8,9,8 +shuffle2 5,6,7,6 +shuffle2 3,4,5,4 +shuffle2 10,11,3,11 + +#store +vmovdqa %ymm9,(%rdi) +vmovdqa %ymm8,32(%rdi) +vmovdqa %ymm7,64(%rdi) +vmovdqa %ymm6,96(%rdi) +vmovdqa %ymm5,128(%rdi) +vmovdqa %ymm4,160(%rdi) +vmovdqa %ymm3,192(%rdi) +vmovdqa %ymm11,224(%rdi) + +ret + +.global cdecl(nttunpack_avx) +cdecl(nttunpack_avx): +call nttunpack128_avx +add $256,%rdi +call nttunpack128_avx +add $256,%rdi +call nttunpack128_avx +add $256,%rdi +call nttunpack128_avx +ret + +.section .note.GNU-stack,"",@progbits diff --git a/src/crypto/pq/_d/avx2/shuffle.inc b/src/crypto/pq/_d/avx2/shuffle.inc new file mode 100644 index 0000000..73e9ffe --- /dev/null +++ b/src/crypto/pq/_d/avx2/shuffle.inc @@ -0,0 +1,25 @@ +.macro shuffle8 r0,r1,r2,r3 +vperm2i128 $0x20,%ymm\r1,%ymm\r0,%ymm\r2 +vperm2i128 $0x31,%ymm\r1,%ymm\r0,%ymm\r3 +.endm + +.macro shuffle4 r0,r1,r2,r3 +vpunpcklqdq %ymm\r1,%ymm\r0,%ymm\r2 +vpunpckhqdq %ymm\r1,%ymm\r0,%ymm\r3 +.endm + +.macro shuffle2 r0,r1,r2,r3 +#vpsllq $32,%ymm\r1,%ymm\r2 +vmovsldup %ymm\r1,%ymm\r2 +vpblendd $0xAA,%ymm\r2,%ymm\r0,%ymm\r2 +vpsrlq $32,%ymm\r0,%ymm\r0 +#vmovshdup %ymm\r0,%ymm\r0 +vpblendd $0xAA,%ymm\r1,%ymm\r0,%ymm\r3 +.endm + +.macro shuffle1 r0,r1,r2,r3 +vpslld $16,%ymm\r1,%ymm\r2 +vpblendw $0xAA,%ymm\r2,%ymm\r0,%ymm\r2 +vpsrld $16,%ymm\r0,%ymm\r0 +vpblendw $0xAA,%ymm\r1,%ymm\r0,%ymm\r3 +.endm diff --git a/src/crypto/pq/_d/avx2/sign.c b/src/crypto/pq/_d/avx2/sign.c new file mode 100644 index 0000000..ff01fe4 --- /dev/null +++ b/src/crypto/pq/_d/avx2/sign.c @@ -0,0 +1,532 @@ +#include +#include +#include "align.h" +#include "params.h" +#include "sign.h" +#include "packing.h" +#include "polyvec.h" +#include "poly.h" +#include "randombytes.h" +#include "symmetric.h" +#include "fips202.h" + +static inline void polyvec_matrix_expand_row(polyvecl **row, polyvecl buf[2], const uint8_t rho[SEEDBYTES], unsigned int i) { + switch(i) { + case 0: + polyvec_matrix_expand_row0(buf, buf + 1, rho); + *row = buf; + break; + case 1: + polyvec_matrix_expand_row1(buf + 1, buf, rho); + *row = buf + 1; + break; + case 2: + polyvec_matrix_expand_row2(buf, buf + 1, rho); + *row = buf; + break; + case 3: + polyvec_matrix_expand_row3(buf + 1, buf, rho); + *row = buf + 1; + break; +#if K > 4 + case 4: + polyvec_matrix_expand_row4(buf, buf + 1, rho); + *row = buf; + break; + case 5: + polyvec_matrix_expand_row5(buf + 1, buf, rho); + *row = buf + 1; + break; +#endif +#if K > 6 + case 6: + polyvec_matrix_expand_row6(buf, buf + 1, rho); + *row = buf; + break; + case 7: + polyvec_matrix_expand_row7(buf + 1, buf, rho); + *row = buf + 1; + break; +#endif + } +} + +/************************************************* +* Name: crypto_sign_keypair +* +* Description: Generates public and private key. +* +* Arguments: - uint8_t *pk: pointer to output public key (allocated +* array of CRYPTO_PUBLICKEYBYTES bytes) +* - uint8_t *sk: pointer to output private key (allocated +* array of CRYPTO_SECRETKEYBYTES bytes) +* +* Returns 0 (success) +**************************************************/ +int crypto_sign_keypair(uint8_t *pk, uint8_t *sk) { + unsigned int i; + uint8_t seedbuf[2*SEEDBYTES + CRHBYTES]; + const uint8_t *rho, *rhoprime, *key; + polyvecl rowbuf[2]; + polyvecl s1, *row = rowbuf; + polyveck s2; + poly t1, t0; + + /* Get randomness for rho, rhoprime and key */ + randombytes(seedbuf, SEEDBYTES); + seedbuf[SEEDBYTES+0] = K; + seedbuf[SEEDBYTES+1] = L; + shake256(seedbuf, 2*SEEDBYTES + CRHBYTES, seedbuf, SEEDBYTES+2); + rho = seedbuf; + rhoprime = rho + SEEDBYTES; + key = rhoprime + CRHBYTES; + + /* Store rho, key */ + memcpy(pk, rho, SEEDBYTES); + memcpy(sk, rho, SEEDBYTES); + memcpy(sk + SEEDBYTES, key, SEEDBYTES); + + /* Sample short vectors s1 and s2 */ +#if K == 4 && L == 4 + poly_uniform_eta_4x(&s1.vec[0], &s1.vec[1], &s1.vec[2], &s1.vec[3], rhoprime, 0, 1, 2, 3); + poly_uniform_eta_4x(&s2.vec[0], &s2.vec[1], &s2.vec[2], &s2.vec[3], rhoprime, 4, 5, 6, 7); +#elif K == 6 && L == 5 + poly_uniform_eta_4x(&s1.vec[0], &s1.vec[1], &s1.vec[2], &s1.vec[3], rhoprime, 0, 1, 2, 3); + poly_uniform_eta_4x(&s1.vec[4], &s2.vec[0], &s2.vec[1], &s2.vec[2], rhoprime, 4, 5, 6, 7); + poly_uniform_eta_4x(&s2.vec[3], &s2.vec[4], &s2.vec[5], &t0, rhoprime, 8, 9, 10, 11); +#elif K == 8 && L == 7 + poly_uniform_eta_4x(&s1.vec[0], &s1.vec[1], &s1.vec[2], &s1.vec[3], rhoprime, 0, 1, 2, 3); + poly_uniform_eta_4x(&s1.vec[4], &s1.vec[5], &s1.vec[6], &s2.vec[0], rhoprime, 4, 5, 6, 7); + poly_uniform_eta_4x(&s2.vec[1], &s2.vec[2], &s2.vec[3], &s2.vec[4], rhoprime, 8, 9, 10, 11); + poly_uniform_eta_4x(&s2.vec[5], &s2.vec[6], &s2.vec[7], &t0, rhoprime, 12, 13, 14, 15); +#else +#error +#endif + + /* Pack secret vectors */ + for(i = 0; i < L; i++) + polyeta_pack(sk + 2*SEEDBYTES + TRBYTES + i*POLYETA_PACKEDBYTES, &s1.vec[i]); + for(i = 0; i < K; i++) + polyeta_pack(sk + 2*SEEDBYTES + TRBYTES + (L + i)*POLYETA_PACKEDBYTES, &s2.vec[i]); + + /* Transform s1 */ + polyvecl_ntt(&s1); + + for(i = 0; i < K; i++) { + /* Expand matrix row */ + polyvec_matrix_expand_row(&row, rowbuf, rho, i); + + /* Compute inner-product */ + polyvecl_pointwise_acc_montgomery(&t1, row, &s1); + poly_invntt_tomont(&t1); + + /* Add error polynomial */ + poly_add(&t1, &t1, &s2.vec[i]); + + /* Round t and pack t1, t0 */ + poly_caddq(&t1); + poly_power2round(&t1, &t0, &t1); + polyt1_pack(pk + SEEDBYTES + i*POLYT1_PACKEDBYTES, &t1); + polyt0_pack(sk + 2*SEEDBYTES + TRBYTES + (L+K)*POLYETA_PACKEDBYTES + i*POLYT0_PACKEDBYTES, &t0); + } + + /* Compute H(rho, t1) and store in secret key */ + shake256(sk + 2*SEEDBYTES, TRBYTES, pk, CRYPTO_PUBLICKEYBYTES); + + return 0; +} + +/************************************************* +* Name: crypto_sign_signature_internal +* +* Description: Computes signature. Internal API. +* +* Arguments: - uint8_t *sig: pointer to output signature (of length CRYPTO_BYTES) +* - size_t *siglen: pointer to output length of signature +* - uint8_t *m: pointer to message to be signed +* - size_t mlen: length of message +* - uint8_t *pre: pointer to prefix string +* - size_t prelen: length of prefix string +* - uint8_t *rnd: pointer to random seed +* - uint8_t *sk: pointer to bit-packed secret key +* +* Returns 0 (success) +**************************************************/ +int crypto_sign_signature_internal(uint8_t *sig, size_t *siglen, const uint8_t *m, size_t mlen, + const uint8_t *pre, size_t prelen, const uint8_t rnd[RNDBYTES], const uint8_t *sk) +{ + unsigned int i, n, pos; + uint8_t seedbuf[2*SEEDBYTES + TRBYTES + 2*CRHBYTES]; + uint8_t *rho, *tr, *key, *mu, *rhoprime; + uint8_t hintbuf[N]; + uint8_t *hint = sig + CTILDEBYTES + L*POLYZ_PACKEDBYTES; + uint64_t nonce = 0; + polyvecl mat[K], s1, z; + polyveck t0, s2, w1; + poly c, tmp; + union { + polyvecl y; + polyveck w0; + } tmpv; + keccak_state state; + + rho = seedbuf; + tr = rho + SEEDBYTES; + key = tr + TRBYTES; + mu = key + SEEDBYTES; + rhoprime = mu + CRHBYTES; + unpack_sk(rho, tr, key, &t0, &s1, &s2, sk); + + /* Compute mu = CRH(tr, pre, msg) */ + shake256_init(&state); + shake256_absorb(&state, tr, TRBYTES); + shake256_absorb(&state, pre, prelen); + shake256_absorb(&state, m, mlen); + shake256_finalize(&state); + shake256_squeeze(mu, CRHBYTES, &state); + + /* Compute rhoprime = CRH(key, rnd, mu) */ + shake256_init(&state); + shake256_absorb(&state, key, SEEDBYTES); + shake256_absorb(&state, rnd, RNDBYTES); + shake256_absorb(&state, mu, CRHBYTES); + shake256_finalize(&state); + shake256_squeeze(rhoprime, CRHBYTES, &state); + + /* Expand matrix and transform vectors */ + polyvec_matrix_expand(mat, rho); + polyvecl_ntt(&s1); + polyveck_ntt(&s2); + polyveck_ntt(&t0); + +rej: + /* Sample intermediate vector y */ +#if L == 4 + poly_uniform_gamma1_4x(&z.vec[0], &z.vec[1], &z.vec[2], &z.vec[3], + rhoprime, nonce, nonce + 1, nonce + 2, nonce + 3); + nonce += 4; +#elif L == 5 + poly_uniform_gamma1_4x(&z.vec[0], &z.vec[1], &z.vec[2], &z.vec[3], + rhoprime, nonce, nonce + 1, nonce + 2, nonce + 3); + poly_uniform_gamma1(&z.vec[4], rhoprime, nonce + 4); + nonce += 5; +#elif L == 7 + poly_uniform_gamma1_4x(&z.vec[0], &z.vec[1], &z.vec[2], &z.vec[3], + rhoprime, nonce, nonce + 1, nonce + 2, nonce + 3); + poly_uniform_gamma1_4x(&z.vec[4], &z.vec[5], &z.vec[6], &tmp, + rhoprime, nonce + 4, nonce + 5, nonce + 6, 0); + nonce += 7; +#else +#error +#endif + + /* Matrix-vector product */ + tmpv.y = z; + polyvecl_ntt(&tmpv.y); + polyvec_matrix_pointwise_montgomery(&w1, mat, &tmpv.y); + polyveck_invntt_tomont(&w1); + + /* Decompose w and call the random oracle */ + polyveck_caddq(&w1); + polyveck_decompose(&w1, &tmpv.w0, &w1); + polyveck_pack_w1(sig, &w1); + + shake256_init(&state); + shake256_absorb(&state, mu, CRHBYTES); + shake256_absorb(&state, sig, K*POLYW1_PACKEDBYTES); + shake256_finalize(&state); + shake256_squeeze(sig, CTILDEBYTES, &state); + poly_challenge(&c, sig); + poly_ntt(&c); + + /* Compute z, reject if it reveals secret */ + for(i = 0; i < L; i++) { + poly_pointwise_montgomery(&tmp, &c, &s1.vec[i]); + poly_invntt_tomont(&tmp); + poly_add(&z.vec[i], &z.vec[i], &tmp); + poly_reduce(&z.vec[i]); + if(poly_chknorm(&z.vec[i], GAMMA1 - BETA)) + goto rej; + } + + /* Zero hint vector in signature */ + pos = 0; + memset(hint, 0, OMEGA); + + for(i = 0; i < K; i++) { + /* Check that subtracting cs2 does not change high bits of w and low bits + * do not reveal secret information */ + poly_pointwise_montgomery(&tmp, &c, &s2.vec[i]); + poly_invntt_tomont(&tmp); + poly_sub(&tmpv.w0.vec[i], &tmpv.w0.vec[i], &tmp); + poly_reduce(&tmpv.w0.vec[i]); + if(poly_chknorm(&tmpv.w0.vec[i], GAMMA2 - BETA)) + goto rej; + + /* Compute hints */ + poly_pointwise_montgomery(&tmp, &c, &t0.vec[i]); + poly_invntt_tomont(&tmp); + poly_reduce(&tmp); + if(poly_chknorm(&tmp, GAMMA2)) + goto rej; + + poly_add(&tmpv.w0.vec[i], &tmpv.w0.vec[i], &tmp); + n = poly_make_hint(hintbuf, &tmpv.w0.vec[i], &w1.vec[i]); + if(pos + n > OMEGA) + goto rej; + + /* Store hints in signature */ + memcpy(&hint[pos], hintbuf, n); + hint[OMEGA + i] = pos = pos + n; + } + + /* Pack z into signature */ + for(i = 0; i < L; i++) + polyz_pack(sig + CTILDEBYTES + i*POLYZ_PACKEDBYTES, &z.vec[i]); + + *siglen = CRYPTO_BYTES; + return 0; +} + +/************************************************* +* Name: crypto_sign_signature +* +* Description: Computes signature. +* +* Arguments: - uint8_t *sig: pointer to output signature (of length CRYPTO_BYTES) +* - size_t *siglen: pointer to output length of signature +* - uint8_t *m: pointer to message to be signed +* - size_t mlen: length of message +* - uint8_t *ctx: pointer to context string +* - size_t ctxlen: length of context string +* - uint8_t *sk: pointer to bit-packed secret key +* +* Returns 0 (success) or -1 (context string too long) +**************************************************/ +int crypto_sign_signature(uint8_t *sig, size_t *siglen, const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, const uint8_t *sk) +{ + uint8_t pre[257]; + uint8_t rnd[RNDBYTES]; + + if(ctxlen > 255) + return -1; + + /* Prepare pre = (0, ctxlen, ctx) */ + pre[0] = 0; + pre[1] = ctxlen; + memcpy(&pre[2], ctx, ctxlen); + +#ifdef DILITHIUM_RANDOMIZED_SIGNING + randombytes(rnd, RNDBYTES); +#else + memset(rnd, 0, RNDBYTES); +#endif + + crypto_sign_signature_internal(sig,siglen,m,mlen,pre,2+ctxlen,rnd,sk); + return 0; +} + +/************************************************* +* Name: crypto_sign +* +* Description: Compute signed message. +* +* Arguments: - uint8_t *sm: pointer to output signed message (allocated +* array with CRYPTO_BYTES + mlen bytes), +* can be equal to m +* - size_t *smlen: pointer to output length of signed +* message +* - const uint8_t *m: pointer to message to be signed +* - size_t mlen: length of message +* - const uint8_t *ctx: pointer to context string +* - size_t ctxlen: length of context string +* - const uint8_t *sk: pointer to bit-packed secret key +* +* Returns 0 (success) +**************************************************/ +int crypto_sign(uint8_t *sm, size_t *smlen, const uint8_t *m, size_t mlen, const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk) +{ + size_t i; + int ret; + + for(i = 0; i < mlen; ++i) + sm[CRYPTO_BYTES + mlen - 1 - i] = m[mlen - 1 - i]; + ret = crypto_sign_signature(sm, smlen, sm + CRYPTO_BYTES, mlen, ctx, ctxlen, sk); + *smlen += mlen; + return ret; +} + +/************************************************* +* Name: crypto_sign_verify_internal +* +* Description: Verifies signature. Internal API. +* +* Arguments: - uint8_t *m: pointer to input signature +* - size_t siglen: length of signature +* - const uint8_t *m: pointer to message +* - size_t mlen: length of message +* - const uint8_t *pre: pointer to prefix string +* - size_t prelen: length of prefix string +* - const uint8_t *pk: pointer to bit-packed public key +* +* Returns 0 if signature could be verified correctly and -1 otherwise +**************************************************/ +int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen, const uint8_t *m, size_t mlen, + const uint8_t *pre, size_t prelen, const uint8_t *pk) { + unsigned int i, j, pos = 0; + /* polyw1_pack writes additional 14 bytes */ + ALIGNED_UINT8(K*POLYW1_PACKEDBYTES+14) buf; + uint8_t mu[CRHBYTES]; + const uint8_t *hint = sig + CTILDEBYTES + L*POLYZ_PACKEDBYTES; + polyvecl rowbuf[2]; + polyvecl *row = rowbuf; + polyvecl z; + poly c, w1, h; + keccak_state state; + + if(siglen != CRYPTO_BYTES) + return -1; + + /* Compute CRH(H(rho, t1), pre, msg) */ + shake256(mu, TRBYTES, pk, CRYPTO_PUBLICKEYBYTES); + shake256_init(&state); + shake256_absorb(&state, mu, CRHBYTES); + shake256_absorb(&state, pre, prelen); + shake256_absorb(&state, m, mlen); + shake256_finalize(&state); + shake256_squeeze(mu, CRHBYTES, &state); + + /* Expand challenge */ + poly_challenge(&c, sig); + poly_ntt(&c); + + /* Unpack z; shortness follows from unpacking */ + for(i = 0; i < L; i++) { + polyz_unpack(&z.vec[i], sig + CTILDEBYTES + i*POLYZ_PACKEDBYTES); + if(poly_chknorm(&z.vec[i], GAMMA1 - BETA)) + return -1; + poly_ntt(&z.vec[i]); + } + + for(i = 0; i < K; i++) { + /* Expand matrix row */ + polyvec_matrix_expand_row(&row, rowbuf, pk, i); + + /* Compute i-th row of Az - c2^Dt1 */ + polyvecl_pointwise_acc_montgomery(&w1, row, &z); + + polyt1_unpack(&h, pk + SEEDBYTES + i*POLYT1_PACKEDBYTES); + poly_shiftl(&h); + poly_ntt(&h); + poly_pointwise_montgomery(&h, &c, &h); + + poly_sub(&w1, &w1, &h); + poly_reduce(&w1); + poly_invntt_tomont(&w1); + + /* Get hint polynomial and reconstruct w1 */ + memset(h.vec, 0, sizeof(poly)); + if(hint[OMEGA + i] < pos || hint[OMEGA + i] > OMEGA) + return -1; + + for(j = pos; j < hint[OMEGA + i]; ++j) { + /* Coefficients are ordered for strong unforgeability */ + if(j > pos && hint[j] <= hint[j-1]) return -1; + h.coeffs[hint[j]] = 1; + } + pos = hint[OMEGA + i]; + + poly_caddq(&w1); + poly_use_hint(&w1, &w1, &h); + polyw1_pack(buf.coeffs + i*POLYW1_PACKEDBYTES, &w1); + } + + /* Extra indices are zero for strong unforgeability */ + for(j = pos; j < OMEGA; ++j) + if(hint[j]) return -1; + + /* Call random oracle and verify challenge */ + shake256_init(&state); + shake256_absorb(&state, mu, CRHBYTES); + shake256_absorb(&state, buf.coeffs, K*POLYW1_PACKEDBYTES); + shake256_finalize(&state); + shake256_squeeze(buf.coeffs, CTILDEBYTES, &state); + for(i = 0; i < CTILDEBYTES; ++i) + if(buf.coeffs[i] != sig[i]) + return -1; + + return 0; +} + +/************************************************* +* Name: crypto_sign_verify +* +* Description: Verifies signature. +* +* Arguments: - uint8_t *m: pointer to input signature +* - size_t siglen: length of signature +* - const uint8_t *m: pointer to message +* - size_t mlen: length of message +* - const uint8_t *ctx: pointer to context string +* - size_t ctxlen: length of context string +* - const uint8_t *pk: pointer to bit-packed public key +* +* Returns 0 if signature could be verified correctly and -1 otherwise +**************************************************/ +int crypto_sign_verify(const uint8_t *sig, size_t siglen, const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, const uint8_t *pk) +{ + uint8_t pre[257]; + + if(ctxlen > 255) + return -1; + + pre[0] = 0; + pre[1] = ctxlen; + memcpy(&pre[2], ctx, ctxlen); + return crypto_sign_verify_internal(sig,siglen,m,mlen,pre,2+ctxlen,pk); +} + +/************************************************* +* Name: crypto_sign_open +* +* Description: Verify signed message. +* +* Arguments: - uint8_t *m: pointer to output message (allocated +* array with smlen bytes), can be equal to sm +* - size_t *mlen: pointer to output length of message +* - const uint8_t *sm: pointer to signed message +* - size_t smlen: length of signed message +* - const uint8_t *ctx: pointer to context string +* - size_t ctxlen: length of context string +* - const uint8_t *pk: pointer to bit-packed public key +* +* Returns 0 if signed message could be verified correctly and -1 otherwise +**************************************************/ +int crypto_sign_open(uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, const uint8_t *pk) { + size_t i; + + if(smlen < CRYPTO_BYTES) + goto badsig; + + *mlen = smlen - CRYPTO_BYTES; + if(crypto_sign_verify(sm, CRYPTO_BYTES, sm + CRYPTO_BYTES, *mlen, ctx, ctxlen, pk)) + goto badsig; + else { + /* All good, copy msg, return 0 */ + for(i = 0; i < *mlen; ++i) + m[i] = sm[CRYPTO_BYTES + i]; + return 0; + } + +badsig: + /* Signature verification failed */ + *mlen = 0; + for(i = 0; i < smlen; ++i) + m[i] = 0; + + return -1; +} diff --git a/src/crypto/pq/_d/avx2/sign.h b/src/crypto/pq/_d/avx2/sign.h new file mode 120000 index 0000000..200b72f --- /dev/null +++ b/src/crypto/pq/_d/avx2/sign.h @@ -0,0 +1 @@ +../ref/sign.h \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/symmetric-shake.c b/src/crypto/pq/_d/avx2/symmetric-shake.c new file mode 120000 index 0000000..86f8b6c --- /dev/null +++ b/src/crypto/pq/_d/avx2/symmetric-shake.c @@ -0,0 +1 @@ +../ref/symmetric-shake.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/symmetric.h b/src/crypto/pq/_d/avx2/symmetric.h new file mode 100644 index 0000000..8f3c3c5 --- /dev/null +++ b/src/crypto/pq/_d/avx2/symmetric.h @@ -0,0 +1,26 @@ +#ifndef SYMMETRIC_H +#define SYMMETRIC_H + +#include +#include "params.h" + +#include "fips202.h" + +typedef keccak_state stream128_state; +typedef keccak_state stream256_state; + +#define dilithium_shake128_stream_init DILITHIUM_NAMESPACE(dilithium_shake128_stream_init) +void dilithium_shake128_stream_init(keccak_state *state, const uint8_t seed[SEEDBYTES], uint16_t nonce); + +#define dilithium_shake256_stream_init DILITHIUM_NAMESPACE(dilithium_shake256_stream_init) +void dilithium_shake256_stream_init(keccak_state *state, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define STREAM128_BLOCKBYTES SHAKE128_RATE +#define STREAM256_BLOCKBYTES SHAKE256_RATE + +#define stream128_init(STATE, SEED, NONCE) dilithium_shake128_stream_init(STATE, SEED, NONCE) +#define stream128_squeezeblocks(OUT, OUTBLOCKS, STATE) shake128_squeezeblocks(OUT, OUTBLOCKS, STATE) +#define stream256_init(STATE, SEED, NONCE) dilithium_shake256_stream_init(STATE, SEED, NONCE) +#define stream256_squeezeblocks(OUT, OUTBLOCKS, STATE) shake256_squeezeblocks(OUT, OUTBLOCKS, STATE) + +#endif diff --git a/src/crypto/pq/_d/avx2/test/.gitignore b/src/crypto/pq/_d/avx2/test/.gitignore new file mode 100644 index 0000000..99b3f73 --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/.gitignore @@ -0,0 +1,10 @@ +test_dilithium2 +test_dilithium3 +test_dilithium5 +test_vectors2 +test_vectors3 +test_vectors5 +test_speed2 +test_speed3 +test_speed5 +test_mul diff --git a/src/crypto/pq/_d/avx2/test/cpucycles.c b/src/crypto/pq/_d/avx2/test/cpucycles.c new file mode 120000 index 0000000..4d6fc8a --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/cpucycles.c @@ -0,0 +1 @@ +../../ref/test/cpucycles.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/test/cpucycles.h b/src/crypto/pq/_d/avx2/test/cpucycles.h new file mode 120000 index 0000000..269feb3 --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/cpucycles.h @@ -0,0 +1 @@ +../../ref/test/cpucycles.h \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/test/speed_print.c b/src/crypto/pq/_d/avx2/test/speed_print.c new file mode 120000 index 0000000..98f2a46 --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/speed_print.c @@ -0,0 +1 @@ +../../ref/test/speed_print.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/test/speed_print.h b/src/crypto/pq/_d/avx2/test/speed_print.h new file mode 120000 index 0000000..8ba4e5e --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/speed_print.h @@ -0,0 +1 @@ +../../ref/test/speed_print.h \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/test/test_dilithium.c b/src/crypto/pq/_d/avx2/test/test_dilithium.c new file mode 120000 index 0000000..729cb5b --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/test_dilithium.c @@ -0,0 +1 @@ +../../ref/test/test_dilithium.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/test/test_mul.c b/src/crypto/pq/_d/avx2/test/test_mul.c new file mode 120000 index 0000000..013f7af --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/test_mul.c @@ -0,0 +1 @@ +../../ref/test/test_mul.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/test/test_speed.c b/src/crypto/pq/_d/avx2/test/test_speed.c new file mode 120000 index 0000000..7decc02 --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/test_speed.c @@ -0,0 +1 @@ +../../ref/test/test_speed.c \ No newline at end of file diff --git a/src/crypto/pq/_d/avx2/test/test_vectors.c b/src/crypto/pq/_d/avx2/test/test_vectors.c new file mode 100644 index 0000000..430ff2a --- /dev/null +++ b/src/crypto/pq/_d/avx2/test/test_vectors.c @@ -0,0 +1,275 @@ +#include +#include +#include +#include +#include "../randombytes.h" +#include "../fips202.h" +#include "../params.h" +#include "../sign.h" +#include "../poly.h" +#include "../polyvec.h" +#include "../packing.h" + +#define MLEN 32 +#define CTXLEN 13 +#define NVECTORS 10000 + +static unsigned int nttidx(unsigned int k) { + unsigned int r; + r = k/64*64; + k %= 64; + r += k/8; + k %= 8; + r += k*8; + return r; +} + +/* Initital state after absorbing empty string + * Permute before squeeze is achieved by setting pos to SHAKE128_RATE */ +static keccak_state rngstate = {{0x1F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (1ULL << 63), 0, 0, 0, 0}, SHAKE128_RATE}; + +void randombytes(uint8_t *x,size_t xlen) { + shake128_squeeze(x, xlen, &rngstate); +} + +int main(void) { + unsigned int i, j, k, l; + uint8_t pk[CRYPTO_PUBLICKEYBYTES]; + uint8_t sk[CRYPTO_SECRETKEYBYTES]; + uint8_t sig[CRYPTO_BYTES]; + uint8_t m[MLEN]; + uint8_t ctx[CTXLEN] = {0}; + uint8_t seed[CRHBYTES]; + uint8_t buf[CRYPTO_SECRETKEYBYTES]; + size_t siglen; + poly c, tmp; + polyvecl s, y, mat[K]; + polyveck w, w1, w0, t1, t0; + + snprintf((char*)ctx,CTXLEN,"test_vectors"); + + for(i = 0; i < NVECTORS; ++i) { + printf("count = %u\n", i); + + randombytes(m, MLEN); + printf("m = "); + for(j = 0; j < MLEN; ++j) + printf("%02x", m[j]); + printf("\n"); + + crypto_sign_keypair(pk, sk); + shake256(buf, 32, pk, CRYPTO_PUBLICKEYBYTES); + printf("pk = "); + for(j = 0; j < 32; ++j) + printf("%02x", buf[j]); + printf("\n"); + shake256(buf, 32, sk, CRYPTO_SECRETKEYBYTES); + printf("sk = "); + for(j = 0; j < 32; ++j) + printf("%02x", buf[j]); + printf("\n"); + + crypto_sign_signature(sig, &siglen, m, MLEN, ctx, CTXLEN, sk); + shake256(buf, 32, sig, CRYPTO_BYTES); + printf("sig = "); + for(j = 0; j < 32; ++j) + printf("%02x", buf[j]); + printf("\n"); + + if(crypto_sign_verify(sig, siglen, m, MLEN, ctx, CTXLEN, pk)) + fprintf(stderr,"Signature verification failed!\n"); + + randombytes(seed, sizeof(seed)); + printf("seed = "); + for(j = 0; j < sizeof(seed); ++j) + printf("%02X", seed[j]); + printf("\n"); + + polyvec_matrix_expand(mat, seed); + printf("A = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < L; ++k) { + for(l = 0; l < N; ++l) { + printf("%8d", mat[j].vec[k].coeffs[nttidx(l)]); + if(l < N-1) printf(", "); + else if(k < L-1) printf("], ["); + else if(j < K-1) printf("];\n ["); + else printf("])\n"); + } + } + } + + +#if L == 4 + poly_uniform_eta_4x(&s.vec[0], &s.vec[1], &s.vec[2], &s.vec[3], seed, 0, 1, 2, 3); +#elif L == 5 + poly_uniform_eta_4x(&s.vec[0], &s.vec[1], &s.vec[2], &s.vec[3], seed, 0, 1, 2, 3); + poly_uniform_eta(&s.vec[4], seed, 4); +#elif L == 7 + poly_uniform_eta_4x(&s.vec[0], &s.vec[1], &s.vec[2], &s.vec[3], seed, 0, 1, 2, 3); + poly_uniform_eta_4x(&s.vec[4], &s.vec[5], &s.vec[6], &tmp, seed, 4, 5, 6, 7); +#else +#error +#endif + + polyeta_pack(buf, &s.vec[0]); + polyeta_unpack(&tmp, buf); + for(j = 0; j < N; ++j) + if(tmp.coeffs[j] != s.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in polyeta_(un)pack!\n"); + + if(polyvecl_chknorm(&s, ETA+1)) + fprintf(stderr, "ERROR in polyvecl_chknorm(&s ,ETA+1)!\n"); + + printf("s = (["); + for(j = 0; j < L; ++j) { + for(k = 0; k < N; ++k) { + printf("%3d", s.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < L-1) printf("],\n ["); + else printf("])\n"); + } + } + +#if L == 4 + poly_uniform_gamma1_4x(&y.vec[0], &y.vec[1], &y.vec[2], &y.vec[3], seed, 0, 1, 2, 3); +#elif L == 5 + poly_uniform_gamma1_4x(&y.vec[0], &y.vec[1], &y.vec[2], &y.vec[3], seed, 0, 1, 2, 3); + poly_uniform_gamma1(&y.vec[4], seed, 4); +#elif L == 7 + poly_uniform_gamma1_4x(&y.vec[0], &y.vec[1], &y.vec[2], &y.vec[3], seed, 0, 1, 2, 3); + poly_uniform_gamma1_4x(&y.vec[4], &y.vec[5], &y.vec[6], &tmp, seed, 4, 5, 6, 7); +#else +#error +#endif + + polyz_pack(buf, &y.vec[0]); + polyz_unpack(&tmp, buf); + for(j = 0; j < N; ++j) + if(tmp.coeffs[j] != y.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in polyz_(un)pack!\n"); + + if(polyvecl_chknorm(&y, GAMMA1+1)) + fprintf(stderr, "ERROR in polyvecl_chknorm(&y, GAMMA1)!\n"); + + printf("y = (["); + for(j = 0; j < L; ++j) { + for(k = 0; k < N; ++k) { + printf("%8d", y.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < L-1) printf("],\n ["); + else printf("])\n"); + } + } + + polyvecl_ntt(&y); + polyvec_matrix_pointwise_montgomery(&w, mat, &y); + polyveck_invntt_tomont(&w); + polyveck_caddq(&w); + polyveck_decompose(&w1, &w0, &w); + + for(j = 0; j < N; ++j) { + tmp.coeffs[j] = w1.vec[0].coeffs[j]*2*GAMMA2 + w0.vec[0].coeffs[j]; + if(tmp.coeffs[j] < 0) tmp.coeffs[j] += Q; + if(tmp.coeffs[j] != w.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in poly_decompose!\n"); + } + + polyw1_pack(buf, &w1.vec[0]); +#if GAMMA2 == (Q-1)/32 + for(j = 0; j < N/2; ++j) { + tmp.coeffs[2*j+0] = buf[j] & 0xF; + tmp.coeffs[2*j+1] = buf[j] >> 4; + if(tmp.coeffs[2*j+0] != w1.vec[0].coeffs[2*j+0] + || tmp.coeffs[2*j+1] != w1.vec[0].coeffs[2*j+1]) + fprintf(stderr, "ERROR in polyw1_pack!\n"); + } +#endif + +#if GAMMA2 == (Q-1)/32 + if(polyveck_chknorm(&w1, 16)) + fprintf(stderr, "ERROR in polyveck_chknorm(&w1, 16)!\n"); +#elif GAMMA2 == (Q-1)/88 + if(polyveck_chknorm(&w1, 44)) + fprintf(stderr, "ERROR in polyveck_chknorm(&w1, 44)!\n"); +#endif + if(polyveck_chknorm(&w0, GAMMA2 + 1)) + fprintf(stderr, "ERROR in polyveck_chknorm(&w0, GAMMA2+1)!\n"); + + printf("w1 = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < N; ++k) { + printf("%2d", w1.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < K-1) printf("],\n ["); + else printf("])\n"); + } + } + printf("w0 = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < N; ++k) { + printf("%8d", w0.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < K-1) printf("],\n ["); + else printf("])\n"); + } + } + + polyveck_power2round(&t1, &t0, &w); + + for(j = 0; j < N; ++j) { + tmp.coeffs[j] = (t1.vec[0].coeffs[j] << D) + t0.vec[0].coeffs[j]; + if(tmp.coeffs[j] != w.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in poly_power2round!\n"); + } + + polyt1_pack(buf, &t1.vec[0]); + polyt1_unpack(&tmp, buf); + for(j = 0; j < N; ++j) { + if(tmp.coeffs[j] != t1.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in polyt1_(un)pack!\n"); + } + polyt0_pack(buf, &t0.vec[0]); + polyt0_unpack(&tmp, buf); + for(j = 0; j < N; ++j) { + if(tmp.coeffs[j] != t0.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in polyt0_(un)pack!\n"); + } + + if(polyveck_chknorm(&t1, 1024)) + fprintf(stderr, "ERROR in polyveck_chknorm(&t1, 1024)!\n"); + if(polyveck_chknorm(&t0, (1U << (D-1)) + 1)) + fprintf(stderr, "ERROR in polyveck_chknorm(&t0, (1 << (D-1)) + 1)!\n"); + + printf("t1 = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < N; ++k) { + printf("%3d", t1.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < K-1) printf("],\n ["); + else printf("])\n"); + } + } + printf("t0 = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < N; ++k) { + printf("%5d", t0.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < K-1) printf("],\n ["); + else printf("])\n"); + } + } + + poly_challenge(&c, seed); + printf("c = ["); + for(j = 0; j < N; ++j) { + printf("%2d", c.coeffs[j]); + if(j < N-1) printf(", "); + else printf("]\n"); + } + + printf("\n"); + } + + return 0; +} diff --git a/src/crypto/pq/_d/ref/.gitignore b/src/crypto/pq/_d/ref/.gitignore new file mode 100644 index 0000000..cc78368 --- /dev/null +++ b/src/crypto/pq/_d/ref/.gitignore @@ -0,0 +1,2 @@ +*.so +*.o diff --git a/src/crypto/pq/_d/ref/api.h b/src/crypto/pq/_d/ref/api.h new file mode 100644 index 0000000..032fa9f --- /dev/null +++ b/src/crypto/pq/_d/ref/api.h @@ -0,0 +1,98 @@ +#ifndef API_H +#define API_H + +#include +#include + +#define pqcrystals_dilithium2_PUBLICKEYBYTES 1312 +#define pqcrystals_dilithium2_SECRETKEYBYTES 2560 +#define pqcrystals_dilithium2_BYTES 2420 + +#define pqcrystals_dilithium2_ref_PUBLICKEYBYTES pqcrystals_dilithium2_PUBLICKEYBYTES +#define pqcrystals_dilithium2_ref_SECRETKEYBYTES pqcrystals_dilithium2_SECRETKEYBYTES +#define pqcrystals_dilithium2_ref_BYTES pqcrystals_dilithium2_BYTES + +int pqcrystals_dilithium2_ref_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium2_ref_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium2_ref(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium2_ref_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium2_ref_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +#define pqcrystals_dilithium3_PUBLICKEYBYTES 1952 +#define pqcrystals_dilithium3_SECRETKEYBYTES 4032 +#define pqcrystals_dilithium3_BYTES 3309 + +#define pqcrystals_dilithium3_ref_PUBLICKEYBYTES pqcrystals_dilithium3_PUBLICKEYBYTES +#define pqcrystals_dilithium3_ref_SECRETKEYBYTES pqcrystals_dilithium3_SECRETKEYBYTES +#define pqcrystals_dilithium3_ref_BYTES pqcrystals_dilithium3_BYTES + +int pqcrystals_dilithium3_ref_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium3_ref_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium3_ref(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium3_ref_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium3_ref_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +#define pqcrystals_dilithium5_PUBLICKEYBYTES 2592 +#define pqcrystals_dilithium5_SECRETKEYBYTES 4896 +#define pqcrystals_dilithium5_BYTES 4627 + +#define pqcrystals_dilithium5_ref_PUBLICKEYBYTES pqcrystals_dilithium5_PUBLICKEYBYTES +#define pqcrystals_dilithium5_ref_SECRETKEYBYTES pqcrystals_dilithium5_SECRETKEYBYTES +#define pqcrystals_dilithium5_ref_BYTES pqcrystals_dilithium5_BYTES + +int pqcrystals_dilithium5_ref_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium5_ref_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium5_ref(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium5_ref_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium5_ref_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + + +#endif diff --git a/src/crypto/pq/_d/ref/config.h b/src/crypto/pq/_d/ref/config.h new file mode 100644 index 0000000..98b8ccb --- /dev/null +++ b/src/crypto/pq/_d/ref/config.h @@ -0,0 +1,27 @@ +#ifndef CONFIG_H +#define CONFIG_H + +//#define DILITHIUM_MODE 2 +#define DILITHIUM_RANDOMIZED_SIGNING +//#define USE_RDPMC +//#define DBENCH + +#ifndef DILITHIUM_MODE +#define DILITHIUM_MODE 2 +#endif + +#if DILITHIUM_MODE == 2 +#define CRYPTO_ALGNAME "Dilithium2" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium2_ref +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium2_ref_##s +#elif DILITHIUM_MODE == 3 +#define CRYPTO_ALGNAME "Dilithium3" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium3_ref +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium3_ref_##s +#elif DILITHIUM_MODE == 5 +#define CRYPTO_ALGNAME "Dilithium5" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium5_ref +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium5_ref_##s +#endif + +#endif diff --git a/src/crypto/pq/_d/ref/fips202.c b/src/crypto/pq/_d/ref/fips202.c new file mode 100644 index 0000000..2afe799 --- /dev/null +++ b/src/crypto/pq/_d/ref/fips202.c @@ -0,0 +1,774 @@ +/* Based on the public domain implementation in crypto_hash/keccakc512/simple/ from + * http://bench.cr.yp.to/supercop.html by Ronny Van Keer and the public domain "TweetFips202" + * implementation from https://twitter.com/tweetfips202 by Gilles Van Assche, Daniel J. Bernstein, + * and Peter Schwabe */ + +#include +#include +#include "fips202.h" + +#define NROUNDS 24 +#define ROL(a, offset) ((a << offset) ^ (a >> (64-offset))) + +/************************************************* +* Name: load64 +* +* Description: Load 8 bytes into uint64_t in little-endian order +* +* Arguments: - const uint8_t *x: pointer to input byte array +* +* Returns the loaded 64-bit unsigned integer +**************************************************/ +static uint64_t load64(const uint8_t x[8]) { + unsigned int i; + uint64_t r = 0; + + for(i=0;i<8;i++) + r |= (uint64_t)x[i] << 8*i; + + return r; +} + +/************************************************* +* Name: store64 +* +* Description: Store a 64-bit integer to array of 8 bytes in little-endian order +* +* Arguments: - uint8_t *x: pointer to the output byte array (allocated) +* - uint64_t u: input 64-bit unsigned integer +**************************************************/ +static void store64(uint8_t x[8], uint64_t u) { + unsigned int i; + + for(i=0;i<8;i++) + x[i] = u >> 8*i; +} + +/* Keccak round constants */ +const uint64_t KeccakF_RoundConstants[NROUNDS] = { + (uint64_t)0x0000000000000001ULL, + (uint64_t)0x0000000000008082ULL, + (uint64_t)0x800000000000808aULL, + (uint64_t)0x8000000080008000ULL, + (uint64_t)0x000000000000808bULL, + (uint64_t)0x0000000080000001ULL, + (uint64_t)0x8000000080008081ULL, + (uint64_t)0x8000000000008009ULL, + (uint64_t)0x000000000000008aULL, + (uint64_t)0x0000000000000088ULL, + (uint64_t)0x0000000080008009ULL, + (uint64_t)0x000000008000000aULL, + (uint64_t)0x000000008000808bULL, + (uint64_t)0x800000000000008bULL, + (uint64_t)0x8000000000008089ULL, + (uint64_t)0x8000000000008003ULL, + (uint64_t)0x8000000000008002ULL, + (uint64_t)0x8000000000000080ULL, + (uint64_t)0x000000000000800aULL, + (uint64_t)0x800000008000000aULL, + (uint64_t)0x8000000080008081ULL, + (uint64_t)0x8000000000008080ULL, + (uint64_t)0x0000000080000001ULL, + (uint64_t)0x8000000080008008ULL +}; + +/************************************************* +* Name: KeccakF1600_StatePermute +* +* Description: The Keccak F1600 Permutation +* +* Arguments: - uint64_t *state: pointer to input/output Keccak state +**************************************************/ +static void KeccakF1600_StatePermute(uint64_t state[25]) +{ + int round; + + uint64_t Aba, Abe, Abi, Abo, Abu; + uint64_t Aga, Age, Agi, Ago, Agu; + uint64_t Aka, Ake, Aki, Ako, Aku; + uint64_t Ama, Ame, Ami, Amo, Amu; + uint64_t Asa, Ase, Asi, Aso, Asu; + uint64_t BCa, BCe, BCi, BCo, BCu; + uint64_t Da, De, Di, Do, Du; + uint64_t Eba, Ebe, Ebi, Ebo, Ebu; + uint64_t Ega, Ege, Egi, Ego, Egu; + uint64_t Eka, Eke, Eki, Eko, Eku; + uint64_t Ema, Eme, Emi, Emo, Emu; + uint64_t Esa, Ese, Esi, Eso, Esu; + + //copyFromState(A, state) + Aba = state[ 0]; + Abe = state[ 1]; + Abi = state[ 2]; + Abo = state[ 3]; + Abu = state[ 4]; + Aga = state[ 5]; + Age = state[ 6]; + Agi = state[ 7]; + Ago = state[ 8]; + Agu = state[ 9]; + Aka = state[10]; + Ake = state[11]; + Aki = state[12]; + Ako = state[13]; + Aku = state[14]; + Ama = state[15]; + Ame = state[16]; + Ami = state[17]; + Amo = state[18]; + Amu = state[19]; + Asa = state[20]; + Ase = state[21]; + Asi = state[22]; + Aso = state[23]; + Asu = state[24]; + + for(round = 0; round < NROUNDS; round += 2) { + // prepareTheta + BCa = Aba^Aga^Aka^Ama^Asa; + BCe = Abe^Age^Ake^Ame^Ase; + BCi = Abi^Agi^Aki^Ami^Asi; + BCo = Abo^Ago^Ako^Amo^Aso; + BCu = Abu^Agu^Aku^Amu^Asu; + + //thetaRhoPiChiIotaPrepareTheta(round, A, E) + Da = BCu^ROL(BCe, 1); + De = BCa^ROL(BCi, 1); + Di = BCe^ROL(BCo, 1); + Do = BCi^ROL(BCu, 1); + Du = BCo^ROL(BCa, 1); + + Aba ^= Da; + BCa = Aba; + Age ^= De; + BCe = ROL(Age, 44); + Aki ^= Di; + BCi = ROL(Aki, 43); + Amo ^= Do; + BCo = ROL(Amo, 21); + Asu ^= Du; + BCu = ROL(Asu, 14); + Eba = BCa ^((~BCe)& BCi ); + Eba ^= (uint64_t)KeccakF_RoundConstants[round]; + Ebe = BCe ^((~BCi)& BCo ); + Ebi = BCi ^((~BCo)& BCu ); + Ebo = BCo ^((~BCu)& BCa ); + Ebu = BCu ^((~BCa)& BCe ); + + Abo ^= Do; + BCa = ROL(Abo, 28); + Agu ^= Du; + BCe = ROL(Agu, 20); + Aka ^= Da; + BCi = ROL(Aka, 3); + Ame ^= De; + BCo = ROL(Ame, 45); + Asi ^= Di; + BCu = ROL(Asi, 61); + Ega = BCa ^((~BCe)& BCi ); + Ege = BCe ^((~BCi)& BCo ); + Egi = BCi ^((~BCo)& BCu ); + Ego = BCo ^((~BCu)& BCa ); + Egu = BCu ^((~BCa)& BCe ); + + Abe ^= De; + BCa = ROL(Abe, 1); + Agi ^= Di; + BCe = ROL(Agi, 6); + Ako ^= Do; + BCi = ROL(Ako, 25); + Amu ^= Du; + BCo = ROL(Amu, 8); + Asa ^= Da; + BCu = ROL(Asa, 18); + Eka = BCa ^((~BCe)& BCi ); + Eke = BCe ^((~BCi)& BCo ); + Eki = BCi ^((~BCo)& BCu ); + Eko = BCo ^((~BCu)& BCa ); + Eku = BCu ^((~BCa)& BCe ); + + Abu ^= Du; + BCa = ROL(Abu, 27); + Aga ^= Da; + BCe = ROL(Aga, 36); + Ake ^= De; + BCi = ROL(Ake, 10); + Ami ^= Di; + BCo = ROL(Ami, 15); + Aso ^= Do; + BCu = ROL(Aso, 56); + Ema = BCa ^((~BCe)& BCi ); + Eme = BCe ^((~BCi)& BCo ); + Emi = BCi ^((~BCo)& BCu ); + Emo = BCo ^((~BCu)& BCa ); + Emu = BCu ^((~BCa)& BCe ); + + Abi ^= Di; + BCa = ROL(Abi, 62); + Ago ^= Do; + BCe = ROL(Ago, 55); + Aku ^= Du; + BCi = ROL(Aku, 39); + Ama ^= Da; + BCo = ROL(Ama, 41); + Ase ^= De; + BCu = ROL(Ase, 2); + Esa = BCa ^((~BCe)& BCi ); + Ese = BCe ^((~BCi)& BCo ); + Esi = BCi ^((~BCo)& BCu ); + Eso = BCo ^((~BCu)& BCa ); + Esu = BCu ^((~BCa)& BCe ); + + // prepareTheta + BCa = Eba^Ega^Eka^Ema^Esa; + BCe = Ebe^Ege^Eke^Eme^Ese; + BCi = Ebi^Egi^Eki^Emi^Esi; + BCo = Ebo^Ego^Eko^Emo^Eso; + BCu = Ebu^Egu^Eku^Emu^Esu; + + //thetaRhoPiChiIotaPrepareTheta(round+1, E, A) + Da = BCu^ROL(BCe, 1); + De = BCa^ROL(BCi, 1); + Di = BCe^ROL(BCo, 1); + Do = BCi^ROL(BCu, 1); + Du = BCo^ROL(BCa, 1); + + Eba ^= Da; + BCa = Eba; + Ege ^= De; + BCe = ROL(Ege, 44); + Eki ^= Di; + BCi = ROL(Eki, 43); + Emo ^= Do; + BCo = ROL(Emo, 21); + Esu ^= Du; + BCu = ROL(Esu, 14); + Aba = BCa ^((~BCe)& BCi ); + Aba ^= (uint64_t)KeccakF_RoundConstants[round+1]; + Abe = BCe ^((~BCi)& BCo ); + Abi = BCi ^((~BCo)& BCu ); + Abo = BCo ^((~BCu)& BCa ); + Abu = BCu ^((~BCa)& BCe ); + + Ebo ^= Do; + BCa = ROL(Ebo, 28); + Egu ^= Du; + BCe = ROL(Egu, 20); + Eka ^= Da; + BCi = ROL(Eka, 3); + Eme ^= De; + BCo = ROL(Eme, 45); + Esi ^= Di; + BCu = ROL(Esi, 61); + Aga = BCa ^((~BCe)& BCi ); + Age = BCe ^((~BCi)& BCo ); + Agi = BCi ^((~BCo)& BCu ); + Ago = BCo ^((~BCu)& BCa ); + Agu = BCu ^((~BCa)& BCe ); + + Ebe ^= De; + BCa = ROL(Ebe, 1); + Egi ^= Di; + BCe = ROL(Egi, 6); + Eko ^= Do; + BCi = ROL(Eko, 25); + Emu ^= Du; + BCo = ROL(Emu, 8); + Esa ^= Da; + BCu = ROL(Esa, 18); + Aka = BCa ^((~BCe)& BCi ); + Ake = BCe ^((~BCi)& BCo ); + Aki = BCi ^((~BCo)& BCu ); + Ako = BCo ^((~BCu)& BCa ); + Aku = BCu ^((~BCa)& BCe ); + + Ebu ^= Du; + BCa = ROL(Ebu, 27); + Ega ^= Da; + BCe = ROL(Ega, 36); + Eke ^= De; + BCi = ROL(Eke, 10); + Emi ^= Di; + BCo = ROL(Emi, 15); + Eso ^= Do; + BCu = ROL(Eso, 56); + Ama = BCa ^((~BCe)& BCi ); + Ame = BCe ^((~BCi)& BCo ); + Ami = BCi ^((~BCo)& BCu ); + Amo = BCo ^((~BCu)& BCa ); + Amu = BCu ^((~BCa)& BCe ); + + Ebi ^= Di; + BCa = ROL(Ebi, 62); + Ego ^= Do; + BCe = ROL(Ego, 55); + Eku ^= Du; + BCi = ROL(Eku, 39); + Ema ^= Da; + BCo = ROL(Ema, 41); + Ese ^= De; + BCu = ROL(Ese, 2); + Asa = BCa ^((~BCe)& BCi ); + Ase = BCe ^((~BCi)& BCo ); + Asi = BCi ^((~BCo)& BCu ); + Aso = BCo ^((~BCu)& BCa ); + Asu = BCu ^((~BCa)& BCe ); + } + + //copyToState(state, A) + state[ 0] = Aba; + state[ 1] = Abe; + state[ 2] = Abi; + state[ 3] = Abo; + state[ 4] = Abu; + state[ 5] = Aga; + state[ 6] = Age; + state[ 7] = Agi; + state[ 8] = Ago; + state[ 9] = Agu; + state[10] = Aka; + state[11] = Ake; + state[12] = Aki; + state[13] = Ako; + state[14] = Aku; + state[15] = Ama; + state[16] = Ame; + state[17] = Ami; + state[18] = Amo; + state[19] = Amu; + state[20] = Asa; + state[21] = Ase; + state[22] = Asi; + state[23] = Aso; + state[24] = Asu; +} + +/************************************************* +* Name: keccak_init +* +* Description: Initializes the Keccak state. +* +* Arguments: - uint64_t *s: pointer to Keccak state +**************************************************/ +static void keccak_init(uint64_t s[25]) +{ + unsigned int i; + for(i=0;i<25;i++) + s[i] = 0; +} + +/************************************************* +* Name: keccak_absorb +* +* Description: Absorb step of Keccak; incremental. +* +* Arguments: - uint64_t *s: pointer to Keccak state +* - unsigned int pos: position in current block to be absorbed +* - unsigned int r: rate in bytes (e.g., 168 for SHAKE128) +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +* +* Returns new position pos in current block +**************************************************/ +static unsigned int keccak_absorb(uint64_t s[25], + unsigned int pos, + unsigned int r, + const uint8_t *in, + size_t inlen) +{ + unsigned int i; + + while(pos+inlen >= r) { + for(i=pos;i> 8*(i%8); + outlen -= i-pos; + pos = i; + } + + return pos; +} + + +/************************************************* +* Name: keccak_absorb_once +* +* Description: Absorb step of Keccak; +* non-incremental, starts by zeroeing the state. +* +* Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state +* - unsigned int r: rate in bytes (e.g., 168 for SHAKE128) +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +* - uint8_t p: domain-separation byte for different Keccak-derived functions +**************************************************/ +static void keccak_absorb_once(uint64_t s[25], + unsigned int r, + const uint8_t *in, + size_t inlen, + uint8_t p) +{ + unsigned int i; + + for(i=0;i<25;i++) + s[i] = 0; + + while(inlen >= r) { + for(i=0;is); + state->pos = 0; +} + +/************************************************* +* Name: shake128_absorb +* +* Description: Absorb step of the SHAKE128 XOF; incremental. +* +* Arguments: - keccak_state *state: pointer to (initialized) output Keccak state +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +**************************************************/ +void shake128_absorb(keccak_state *state, const uint8_t *in, size_t inlen) +{ + state->pos = keccak_absorb(state->s, state->pos, SHAKE128_RATE, in, inlen); +} + +/************************************************* +* Name: shake128_finalize +* +* Description: Finalize absorb step of the SHAKE128 XOF. +* +* Arguments: - keccak_state *state: pointer to Keccak state +**************************************************/ +void shake128_finalize(keccak_state *state) +{ + keccak_finalize(state->s, state->pos, SHAKE128_RATE, 0x1F); + state->pos = SHAKE128_RATE; +} + +/************************************************* +* Name: shake128_squeeze +* +* Description: Squeeze step of SHAKE128 XOF. Squeezes arbitraily many +* bytes. Can be called multiple times to keep squeezing. +* +* Arguments: - uint8_t *out: pointer to output blocks +* - size_t outlen : number of bytes to be squeezed (written to output) +* - keccak_state *s: pointer to input/output Keccak state +**************************************************/ +void shake128_squeeze(uint8_t *out, size_t outlen, keccak_state *state) +{ + state->pos = keccak_squeeze(out, outlen, state->s, state->pos, SHAKE128_RATE); +} + +/************************************************* +* Name: shake128_absorb_once +* +* Description: Initialize, absorb into and finalize SHAKE128 XOF; non-incremental. +* +* Arguments: - keccak_state *state: pointer to (uninitialized) output Keccak state +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +**************************************************/ +void shake128_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen) +{ + keccak_absorb_once(state->s, SHAKE128_RATE, in, inlen, 0x1F); + state->pos = SHAKE128_RATE; +} + +/************************************************* +* Name: shake128_squeezeblocks +* +* Description: Squeeze step of SHAKE128 XOF. Squeezes full blocks of +* SHAKE128_RATE bytes each. Can be called multiple times +* to keep squeezing. Assumes new block has not yet been +* started (state->pos = SHAKE128_RATE). +* +* Arguments: - uint8_t *out: pointer to output blocks +* - size_t nblocks: number of blocks to be squeezed (written to output) +* - keccak_state *s: pointer to input/output Keccak state +**************************************************/ +void shake128_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state) +{ + keccak_squeezeblocks(out, nblocks, state->s, SHAKE128_RATE); +} + +/************************************************* +* Name: shake256_init +* +* Description: Initilizes Keccak state for use as SHAKE256 XOF +* +* Arguments: - keccak_state *state: pointer to (uninitialized) Keccak state +**************************************************/ +void shake256_init(keccak_state *state) +{ + keccak_init(state->s); + state->pos = 0; +} + +/************************************************* +* Name: shake256_absorb +* +* Description: Absorb step of the SHAKE256 XOF; incremental. +* +* Arguments: - keccak_state *state: pointer to (initialized) output Keccak state +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +**************************************************/ +void shake256_absorb(keccak_state *state, const uint8_t *in, size_t inlen) +{ + state->pos = keccak_absorb(state->s, state->pos, SHAKE256_RATE, in, inlen); +} + +/************************************************* +* Name: shake256_finalize +* +* Description: Finalize absorb step of the SHAKE256 XOF. +* +* Arguments: - keccak_state *state: pointer to Keccak state +**************************************************/ +void shake256_finalize(keccak_state *state) +{ + keccak_finalize(state->s, state->pos, SHAKE256_RATE, 0x1F); + state->pos = SHAKE256_RATE; +} + +/************************************************* +* Name: shake256_squeeze +* +* Description: Squeeze step of SHAKE256 XOF. Squeezes arbitraily many +* bytes. Can be called multiple times to keep squeezing. +* +* Arguments: - uint8_t *out: pointer to output blocks +* - size_t outlen : number of bytes to be squeezed (written to output) +* - keccak_state *s: pointer to input/output Keccak state +**************************************************/ +void shake256_squeeze(uint8_t *out, size_t outlen, keccak_state *state) +{ + state->pos = keccak_squeeze(out, outlen, state->s, state->pos, SHAKE256_RATE); +} + +/************************************************* +* Name: shake256_absorb_once +* +* Description: Initialize, absorb into and finalize SHAKE256 XOF; non-incremental. +* +* Arguments: - keccak_state *state: pointer to (uninitialized) output Keccak state +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +**************************************************/ +void shake256_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen) +{ + keccak_absorb_once(state->s, SHAKE256_RATE, in, inlen, 0x1F); + state->pos = SHAKE256_RATE; +} + +/************************************************* +* Name: shake256_squeezeblocks +* +* Description: Squeeze step of SHAKE256 XOF. Squeezes full blocks of +* SHAKE256_RATE bytes each. Can be called multiple times +* to keep squeezing. Assumes next block has not yet been +* started (state->pos = SHAKE256_RATE). +* +* Arguments: - uint8_t *out: pointer to output blocks +* - size_t nblocks: number of blocks to be squeezed (written to output) +* - keccak_state *s: pointer to input/output Keccak state +**************************************************/ +void shake256_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state) +{ + keccak_squeezeblocks(out, nblocks, state->s, SHAKE256_RATE); +} + +/************************************************* +* Name: shake128 +* +* Description: SHAKE128 XOF with non-incremental API +* +* Arguments: - uint8_t *out: pointer to output +* - size_t outlen: requested output length in bytes +* - const uint8_t *in: pointer to input +* - size_t inlen: length of input in bytes +**************************************************/ +void shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) +{ + size_t nblocks; + keccak_state state; + + shake128_absorb_once(&state, in, inlen); + nblocks = outlen/SHAKE128_RATE; + shake128_squeezeblocks(out, nblocks, &state); + outlen -= nblocks*SHAKE128_RATE; + out += nblocks*SHAKE128_RATE; + shake128_squeeze(out, outlen, &state); +} + +/************************************************* +* Name: shake256 +* +* Description: SHAKE256 XOF with non-incremental API +* +* Arguments: - uint8_t *out: pointer to output +* - size_t outlen: requested output length in bytes +* - const uint8_t *in: pointer to input +* - size_t inlen: length of input in bytes +**************************************************/ +void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) +{ + size_t nblocks; + keccak_state state; + + shake256_absorb_once(&state, in, inlen); + nblocks = outlen/SHAKE256_RATE; + shake256_squeezeblocks(out, nblocks, &state); + outlen -= nblocks*SHAKE256_RATE; + out += nblocks*SHAKE256_RATE; + shake256_squeeze(out, outlen, &state); +} + +/************************************************* +* Name: sha3_256 +* +* Description: SHA3-256 with non-incremental API +* +* Arguments: - uint8_t *h: pointer to output (32 bytes) +* - const uint8_t *in: pointer to input +* - size_t inlen: length of input in bytes +**************************************************/ +void sha3_256(uint8_t h[32], const uint8_t *in, size_t inlen) +{ + unsigned int i; + uint64_t s[25]; + + keccak_absorb_once(s, SHA3_256_RATE, in, inlen, 0x06); + KeccakF1600_StatePermute(s); + for(i=0;i<4;i++) + store64(h+8*i,s[i]); +} + +/************************************************* +* Name: sha3_512 +* +* Description: SHA3-512 with non-incremental API +* +* Arguments: - uint8_t *h: pointer to output (64 bytes) +* - const uint8_t *in: pointer to input +* - size_t inlen: length of input in bytes +**************************************************/ +void sha3_512(uint8_t h[64], const uint8_t *in, size_t inlen) +{ + unsigned int i; + uint64_t s[25]; + + keccak_absorb_once(s, SHA3_512_RATE, in, inlen, 0x06); + KeccakF1600_StatePermute(s); + for(i=0;i<8;i++) + store64(h+8*i,s[i]); +} diff --git a/src/crypto/pq/_d/ref/fips202.h b/src/crypto/pq/_d/ref/fips202.h new file mode 100644 index 0000000..c37f535 --- /dev/null +++ b/src/crypto/pq/_d/ref/fips202.h @@ -0,0 +1,57 @@ +#ifndef FIPS202_H +#define FIPS202_H + +#include +#include + +#define SHAKE128_RATE 168 +#define SHAKE256_RATE 136 +#define SHA3_256_RATE 136 +#define SHA3_512_RATE 72 + +#define FIPS202_NAMESPACE(s) pqcrystals_dilithium_fips202_ref_##s + +typedef struct { + uint64_t s[25]; + unsigned int pos; +} keccak_state; + +#define KeccakF_RoundConstants FIPS202_NAMESPACE(KeccakF_RoundConstants) +extern const uint64_t KeccakF_RoundConstants[]; + +#define shake128_init FIPS202_NAMESPACE(shake128_init) +void shake128_init(keccak_state *state); +#define shake128_absorb FIPS202_NAMESPACE(shake128_absorb) +void shake128_absorb(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake128_finalize FIPS202_NAMESPACE(shake128_finalize) +void shake128_finalize(keccak_state *state); +#define shake128_squeeze FIPS202_NAMESPACE(shake128_squeeze) +void shake128_squeeze(uint8_t *out, size_t outlen, keccak_state *state); +#define shake128_absorb_once FIPS202_NAMESPACE(shake128_absorb_once) +void shake128_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake128_squeezeblocks FIPS202_NAMESPACE(shake128_squeezeblocks) +void shake128_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state); + +#define shake256_init FIPS202_NAMESPACE(shake256_init) +void shake256_init(keccak_state *state); +#define shake256_absorb FIPS202_NAMESPACE(shake256_absorb) +void shake256_absorb(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake256_finalize FIPS202_NAMESPACE(shake256_finalize) +void shake256_finalize(keccak_state *state); +#define shake256_squeeze FIPS202_NAMESPACE(shake256_squeeze) +void shake256_squeeze(uint8_t *out, size_t outlen, keccak_state *state); +#define shake256_absorb_once FIPS202_NAMESPACE(shake256_absorb_once) +void shake256_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake256_squeezeblocks FIPS202_NAMESPACE(shake256_squeezeblocks) +void shake256_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state); + +#define shake128 FIPS202_NAMESPACE(shake128) +void shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen); +#define shake256 FIPS202_NAMESPACE(shake256) +void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen); +#define sha3_256 FIPS202_NAMESPACE(sha3_256) +void sha3_256(uint8_t h[32], const uint8_t *in, size_t inlen); +#define sha3_512 FIPS202_NAMESPACE(sha3_512) +void sha3_512(uint8_t h[64], const uint8_t *in, size_t inlen); + +#endif diff --git a/src/crypto/pq/_d/ref/nistkat/.gitignore b/src/crypto/pq/_d/ref/nistkat/.gitignore new file mode 100644 index 0000000..0aa8057 --- /dev/null +++ b/src/crypto/pq/_d/ref/nistkat/.gitignore @@ -0,0 +1,3 @@ +PQCgenKAT_sign2 +PQCgenKAT_sign3 +PQCgenKAT_sign5 diff --git a/src/crypto/pq/_d/ref/nistkat/PQCgenKAT_sign.c b/src/crypto/pq/_d/ref/nistkat/PQCgenKAT_sign.c new file mode 100644 index 0000000..005ce41 --- /dev/null +++ b/src/crypto/pq/_d/ref/nistkat/PQCgenKAT_sign.c @@ -0,0 +1,261 @@ +// +// PQCgenKAT_sign.c +// +// Created by Bassham, Lawrence E (Fed) on 8/29/17. +// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved. +// +#include +#include +#include +#include +#include +#include "rng.h" +#include "../sign.h" + +#define MAX_MARKER_LEN 50 + +#define KAT_SUCCESS 0 +#define KAT_FILE_OPEN_ERROR -1 +#define KAT_DATA_ERROR -3 +#define KAT_CRYPTO_FAILURE -4 + +int FindMarker(FILE *infile, const char *marker); +int ReadHex(FILE *infile, unsigned char *a, int Length, char *str); +void fprintBstr(FILE *fp, char *s, unsigned char *a, unsigned long long l); + +int +main() +{ + char fn_req[32], fn_rsp[32]; + FILE *fp_req, *fp_rsp; + uint8_t seed[48]; + uint8_t msg[3300]; + uint8_t entropy_input[48]; + uint8_t *m, *sm, *m1; + size_t mlen, smlen, mlen1; + int count; + int done; + uint8_t pk[CRYPTO_PUBLICKEYBYTES], sk[CRYPTO_SECRETKEYBYTES]; + int ret_val; + + // Create the REQUEST file + sprintf(fn_req, "PQCsignKAT_%.16s.req", CRYPTO_ALGNAME); + if ( (fp_req = fopen(fn_req, "w")) == NULL ) { + printf("Couldn't open <%s> for write\n", fn_req); + return KAT_FILE_OPEN_ERROR; + } + sprintf(fn_rsp, "PQCsignKAT_%.16s.rsp", CRYPTO_ALGNAME); + if ( (fp_rsp = fopen(fn_rsp, "w")) == NULL ) { + printf("Couldn't open <%s> for write\n", fn_rsp); + return KAT_FILE_OPEN_ERROR; + } + + for (int i=0; i<48; i++) + entropy_input[i] = i; + + randombytes_init(entropy_input, NULL, 256); + for (int i=0; i<100; i++) { + fprintf(fp_req, "count = %d\n", i); + randombytes(seed, 48); + fprintBstr(fp_req, "seed = ", seed, 48); + mlen = 33*(i+1); + fprintf(fp_req, "mlen = %lu\n", mlen); + randombytes(msg, mlen); + fprintBstr(fp_req, "msg = ", msg, mlen); + fprintf(fp_req, "pk =\n"); + fprintf(fp_req, "sk =\n"); + fprintf(fp_req, "smlen =\n"); + fprintf(fp_req, "sm =\n\n"); + } + fclose(fp_req); + + //Create the RESPONSE file based on what's in the REQUEST file + if ( (fp_req = fopen(fn_req, "r")) == NULL ) { + printf("Couldn't open <%s> for read\n", fn_req); + return KAT_FILE_OPEN_ERROR; + } + + fprintf(fp_rsp, "# %s\n\n", CRYPTO_ALGNAME); + done = 0; + do { + if ( FindMarker(fp_req, "count = ") ) + fscanf(fp_req, "%d", &count); + else { + done = 1; + break; + } + fprintf(fp_rsp, "count = %d\n", count); + + if ( !ReadHex(fp_req, seed, 48, "seed = ") ) { + printf("ERROR: unable to read 'seed' from <%s>\n", fn_req); + return KAT_DATA_ERROR; + } + fprintBstr(fp_rsp, "seed = ", seed, 48); + + randombytes_init(seed, NULL, 256); + + if ( FindMarker(fp_req, "mlen = ") ) + fscanf(fp_req, "%lu", &mlen); + else { + printf("ERROR: unable to read 'mlen' from <%s>\n", fn_req); + return KAT_DATA_ERROR; + } + fprintf(fp_rsp, "mlen = %lu\n", mlen); + + m = (uint8_t *)calloc(mlen, sizeof(uint8_t)); + m1 = (uint8_t *)calloc(mlen+CRYPTO_BYTES, sizeof(uint8_t)); + sm = (uint8_t *)calloc(mlen+CRYPTO_BYTES, sizeof(uint8_t)); + + if ( !ReadHex(fp_req, m, (int)mlen, "msg = ") ) { + printf("ERROR: unable to read 'msg' from <%s>\n", fn_req); + return KAT_DATA_ERROR; + } + fprintBstr(fp_rsp, "msg = ", m, mlen); + + // Generate the public/private keypair + if ( (ret_val = crypto_sign_keypair(pk, sk)) != 0) { + printf("crypto_sign_keypair returned <%d>\n", ret_val); + return KAT_CRYPTO_FAILURE; + } + fprintBstr(fp_rsp, "pk = ", pk, CRYPTO_PUBLICKEYBYTES); + fprintBstr(fp_rsp, "sk = ", sk, CRYPTO_SECRETKEYBYTES); + + if ( (ret_val = crypto_sign(sm, &smlen, m, mlen, NULL, 0, sk)) != 0) { + printf("crypto_sign returned <%d>\n", ret_val); + return KAT_CRYPTO_FAILURE; + } + fprintf(fp_rsp, "smlen = %lu\n", smlen); + fprintBstr(fp_rsp, "sm = ", sm, smlen); + fprintf(fp_rsp, "\n"); + + if ( (ret_val = crypto_sign_open(m1, &mlen1, sm, smlen, NULL, 0, pk)) != 0) { + printf("crypto_sign_open returned <%d>\n", ret_val); + return KAT_CRYPTO_FAILURE; + } + + if ( mlen != mlen1 ) { + printf("crypto_sign_open returned bad 'mlen': Got <%lu>, expected <%lu>\n", mlen1, mlen); + return KAT_CRYPTO_FAILURE; + } + + if ( memcmp(m, m1, mlen) ) { + printf("crypto_sign_open returned bad 'm' value\n"); + return KAT_CRYPTO_FAILURE; + } + + free(m); + free(m1); + free(sm); + + } while ( !done ); + + fclose(fp_req); + fclose(fp_rsp); + + return KAT_SUCCESS; +} + +// +// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.) +// +int +FindMarker(FILE *infile, const char *marker) +{ + char line[MAX_MARKER_LEN]; + int i, len; + int curr_line; + + len = (int)strlen(marker); + if ( len > MAX_MARKER_LEN-1 ) + len = MAX_MARKER_LEN-1; + + for ( i=0; i= '0') && (ch <= '9') ) + ich = ch - '0'; + else if ( (ch >= 'A') && (ch <= 'F') ) + ich = ch - 'A' + 10; + else if ( (ch >= 'a') && (ch <= 'f') ) + ich = ch - 'a' + 10; + else // shouldn't ever get here + ich = 0; + + for ( i=0; i> 4); + a[Length-1] = (a[Length-1] << 4) | ich; + } + else + return 0; + + return 1; +} + +void +fprintBstr(FILE *fp, char *s, unsigned char *a, unsigned long long l) +{ + unsigned long long i; + + fprintf(fp, "%s", s); + + for ( i=0; i +#include "rng.h" +#include +#include +#include + +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; + + memcpy(ctx->key, seed, 32); + + 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; +} + + +void handleErrors(void) +{ + ERR_print_errors_fp(stderr); + abort(); +} + +// Use whatever AES implementation you have. This uses AES from openSSL library +// key - 256-bit AES key +// ctr - a 128-bit plaintext value +// buffer - a 128-bit ciphertext value +void +AES256_ECB(unsigned char *key, unsigned char *ctr, unsigned char *buffer) +{ + EVP_CIPHER_CTX *ctx; + + int len; + + int ciphertext_len; + + /* Create and initialise the context */ + if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); + + if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key, NULL)) + handleErrors(); + + if(1 != EVP_EncryptUpdate(ctx, buffer, &len, ctr, 16)) + handleErrors(); + ciphertext_len = len; + + /* Clean up */ + EVP_CIPHER_CTX_free(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); +} + + + + + + + + + diff --git a/src/crypto/pq/_d/ref/nistkat/rng.h b/src/crypto/pq/_d/ref/nistkat/rng.h new file mode 100644 index 0000000..577e263 --- /dev/null +++ b/src/crypto/pq/_d/ref/nistkat/rng.h @@ -0,0 +1,55 @@ +// +// rng.h +// +// Created by Bassham, Lawrence E (Fed) on 8/29/17. +// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved. +// + +#ifndef rng_h +#define rng_h + +#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); + +#endif /* rng_h */ diff --git a/src/crypto/pq/_d/ref/ntt.c b/src/crypto/pq/_d/ref/ntt.c new file mode 100644 index 0000000..5ea8b53 --- /dev/null +++ b/src/crypto/pq/_d/ref/ntt.c @@ -0,0 +1,98 @@ +#include +#include "params.h" +#include "ntt.h" +#include "reduce.h" + +static const int32_t zetas[N] = { + 0, 25847, -2608894, -518909, 237124, -777960, -876248, 466468, + 1826347, 2353451, -359251, -2091905, 3119733, -2884855, 3111497, 2680103, + 2725464, 1024112, -1079900, 3585928, -549488, -1119584, 2619752, -2108549, + -2118186, -3859737, -1399561, -3277672, 1757237, -19422, 4010497, 280005, + 2706023, 95776, 3077325, 3530437, -1661693, -3592148, -2537516, 3915439, + -3861115, -3043716, 3574422, -2867647, 3539968, -300467, 2348700, -539299, + -1699267, -1643818, 3505694, -3821735, 3507263, -2140649, -1600420, 3699596, + 811944, 531354, 954230, 3881043, 3900724, -2556880, 2071892, -2797779, + -3930395, -1528703, -3677745, -3041255, -1452451, 3475950, 2176455, -1585221, + -1257611, 1939314, -4083598, -1000202, -3190144, -3157330, -3632928, 126922, + 3412210, -983419, 2147896, 2715295, -2967645, -3693493, -411027, -2477047, + -671102, -1228525, -22981, -1308169, -381987, 1349076, 1852771, -1430430, + -3343383, 264944, 508951, 3097992, 44288, -1100098, 904516, 3958618, + -3724342, -8578, 1653064, -3249728, 2389356, -210977, 759969, -1316856, + 189548, -3553272, 3159746, -1851402, -2409325, -177440, 1315589, 1341330, + 1285669, -1584928, -812732, -1439742, -3019102, -3881060, -3628969, 3839961, + 2091667, 3407706, 2316500, 3817976, -3342478, 2244091, -2446433, -3562462, + 266997, 2434439, -1235728, 3513181, -3520352, -3759364, -1197226, -3193378, + 900702, 1859098, 909542, 819034, 495491, -1613174, -43260, -522500, + -655327, -3122442, 2031748, 3207046, -3556995, -525098, -768622, -3595838, + 342297, 286988, -2437823, 4108315, 3437287, -3342277, 1735879, 203044, + 2842341, 2691481, -2590150, 1265009, 4055324, 1247620, 2486353, 1595974, + -3767016, 1250494, 2635921, -3548272, -2994039, 1869119, 1903435, -1050970, + -1333058, 1237275, -3318210, -1430225, -451100, 1312455, 3306115, -1962642, + -1279661, 1917081, -2546312, -1374803, 1500165, 777191, 2235880, 3406031, + -542412, -2831860, -1671176, -1846953, -2584293, -3724270, 594136, -3776993, + -2013608, 2432395, 2454455, -164721, 1957272, 3369112, 185531, -1207385, + -3183426, 162844, 1616392, 3014001, 810149, 1652634, -3694233, -1799107, + -3038916, 3523897, 3866901, 269760, 2213111, -975884, 1717735, 472078, + -426683, 1723600, -1803090, 1910376, -1667432, -1104333, -260646, -3833893, + -2939036, -2235985, -420899, -2286327, 183443, -976891, 1612842, -3545687, + -554416, 3919660, -48306, -1362209, 3937738, 1400424, -846154, 1976782 +}; + +/************************************************* +* Name: ntt +* +* Description: Forward NTT, in-place. No modular reduction is performed after +* additions or subtractions. Output vector is in bitreversed order. +* +* Arguments: - uint32_t p[N]: input/output coefficient array +**************************************************/ +void ntt(int32_t a[N]) { + unsigned int len, start, j, k; + int32_t zeta, t; + + k = 0; + for(len = 128; len > 0; len >>= 1) { + for(start = 0; start < N; start = j + len) { + zeta = zetas[++k]; + for(j = start; j < start + len; ++j) { + t = montgomery_reduce((int64_t)zeta * a[j + len]); + a[j + len] = a[j] - t; + a[j] = a[j] + t; + } + } + } +} + +/************************************************* +* Name: invntt_tomont +* +* Description: Inverse NTT and multiplication by Montgomery factor 2^32. +* In-place. No modular reductions after additions or +* subtractions; input coefficients need to be smaller than +* Q in absolute value. Output coefficient are smaller than Q in +* absolute value. +* +* Arguments: - uint32_t p[N]: input/output coefficient array +**************************************************/ +void invntt_tomont(int32_t a[N]) { + unsigned int start, len, j, k; + int32_t t, zeta; + const int32_t f = 41978; // mont^2/256 + + k = 256; + for(len = 1; len < N; len <<= 1) { + for(start = 0; start < N; start = j + len) { + zeta = -zetas[--k]; + for(j = start; j < start + len; ++j) { + t = a[j]; + a[j] = t + a[j + len]; + a[j + len] = t - a[j + len]; + a[j + len] = montgomery_reduce((int64_t)zeta * a[j + len]); + } + } + } + + for(j = 0; j < N; ++j) { + a[j] = montgomery_reduce((int64_t)f * a[j]); + } +} diff --git a/src/crypto/pq/_d/ref/ntt.h b/src/crypto/pq/_d/ref/ntt.h new file mode 100644 index 0000000..731132d --- /dev/null +++ b/src/crypto/pq/_d/ref/ntt.h @@ -0,0 +1,13 @@ +#ifndef NTT_H +#define NTT_H + +#include +#include "params.h" + +#define ntt DILITHIUM_NAMESPACE(ntt) +void ntt(int32_t a[N]); + +#define invntt_tomont DILITHIUM_NAMESPACE(invntt_tomont) +void invntt_tomont(int32_t a[N]); + +#endif diff --git a/src/crypto/pq/_d/ref/packing.c b/src/crypto/pq/_d/ref/packing.c new file mode 100644 index 0000000..039a686 --- /dev/null +++ b/src/crypto/pq/_d/ref/packing.c @@ -0,0 +1,237 @@ +#include "params.h" +#include "packing.h" +#include "polyvec.h" +#include "poly.h" + +/************************************************* +* Name: pack_pk +* +* Description: Bit-pack public key pk = (rho, t1). +* +* Arguments: - uint8_t pk[]: output byte array +* - const uint8_t rho[]: byte array containing rho +* - const polyveck *t1: pointer to vector t1 +**************************************************/ +void pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES], + const uint8_t rho[SEEDBYTES], + const polyveck *t1) +{ + unsigned int i; + + for(i = 0; i < SEEDBYTES; ++i) + pk[i] = rho[i]; + pk += SEEDBYTES; + + for(i = 0; i < K; ++i) + polyt1_pack(pk + i*POLYT1_PACKEDBYTES, &t1->vec[i]); +} + +/************************************************* +* Name: unpack_pk +* +* Description: Unpack public key pk = (rho, t1). +* +* Arguments: - const uint8_t rho[]: output byte array for rho +* - const polyveck *t1: pointer to output vector t1 +* - uint8_t pk[]: byte array containing bit-packed pk +**************************************************/ +void unpack_pk(uint8_t rho[SEEDBYTES], + polyveck *t1, + const uint8_t pk[CRYPTO_PUBLICKEYBYTES]) +{ + unsigned int i; + + for(i = 0; i < SEEDBYTES; ++i) + rho[i] = pk[i]; + pk += SEEDBYTES; + + for(i = 0; i < K; ++i) + polyt1_unpack(&t1->vec[i], pk + i*POLYT1_PACKEDBYTES); +} + +/************************************************* +* Name: pack_sk +* +* Description: Bit-pack secret key sk = (rho, tr, key, t0, s1, s2). +* +* Arguments: - uint8_t sk[]: output byte array +* - const uint8_t rho[]: byte array containing rho +* - const uint8_t tr[]: byte array containing tr +* - const uint8_t key[]: byte array containing key +* - const polyveck *t0: pointer to vector t0 +* - const polyvecl *s1: pointer to vector s1 +* - const polyveck *s2: pointer to vector s2 +**************************************************/ +void pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES], + const uint8_t rho[SEEDBYTES], + const uint8_t tr[TRBYTES], + const uint8_t key[SEEDBYTES], + const polyveck *t0, + const polyvecl *s1, + const polyveck *s2) +{ + unsigned int i; + + for(i = 0; i < SEEDBYTES; ++i) + sk[i] = rho[i]; + sk += SEEDBYTES; + + for(i = 0; i < SEEDBYTES; ++i) + sk[i] = key[i]; + sk += SEEDBYTES; + + for(i = 0; i < TRBYTES; ++i) + sk[i] = tr[i]; + sk += TRBYTES; + + for(i = 0; i < L; ++i) + polyeta_pack(sk + i*POLYETA_PACKEDBYTES, &s1->vec[i]); + sk += L*POLYETA_PACKEDBYTES; + + for(i = 0; i < K; ++i) + polyeta_pack(sk + i*POLYETA_PACKEDBYTES, &s2->vec[i]); + sk += K*POLYETA_PACKEDBYTES; + + for(i = 0; i < K; ++i) + polyt0_pack(sk + i*POLYT0_PACKEDBYTES, &t0->vec[i]); +} + +/************************************************* +* Name: unpack_sk +* +* Description: Unpack secret key sk = (rho, tr, key, t0, s1, s2). +* +* Arguments: - const uint8_t rho[]: output byte array for rho +* - const uint8_t tr[]: output byte array for tr +* - const uint8_t key[]: output byte array for key +* - const polyveck *t0: pointer to output vector t0 +* - const polyvecl *s1: pointer to output vector s1 +* - const polyveck *s2: pointer to output vector s2 +* - uint8_t sk[]: byte array containing bit-packed sk +**************************************************/ +void unpack_sk(uint8_t rho[SEEDBYTES], + uint8_t tr[TRBYTES], + uint8_t key[SEEDBYTES], + polyveck *t0, + polyvecl *s1, + polyveck *s2, + const uint8_t sk[CRYPTO_SECRETKEYBYTES]) +{ + unsigned int i; + + for(i = 0; i < SEEDBYTES; ++i) + rho[i] = sk[i]; + sk += SEEDBYTES; + + for(i = 0; i < SEEDBYTES; ++i) + key[i] = sk[i]; + sk += SEEDBYTES; + + for(i = 0; i < TRBYTES; ++i) + tr[i] = sk[i]; + sk += TRBYTES; + + for(i=0; i < L; ++i) + polyeta_unpack(&s1->vec[i], sk + i*POLYETA_PACKEDBYTES); + sk += L*POLYETA_PACKEDBYTES; + + for(i=0; i < K; ++i) + polyeta_unpack(&s2->vec[i], sk + i*POLYETA_PACKEDBYTES); + sk += K*POLYETA_PACKEDBYTES; + + for(i=0; i < K; ++i) + polyt0_unpack(&t0->vec[i], sk + i*POLYT0_PACKEDBYTES); +} + +/************************************************* +* Name: pack_sig +* +* Description: Bit-pack signature sig = (c, z, h). +* +* Arguments: - uint8_t sig[]: output byte array +* - const uint8_t *c: pointer to challenge hash length SEEDBYTES +* - const polyvecl *z: pointer to vector z +* - const polyveck *h: pointer to hint vector h +**************************************************/ +void pack_sig(uint8_t sig[CRYPTO_BYTES], + const uint8_t c[CTILDEBYTES], + const polyvecl *z, + const polyveck *h) +{ + unsigned int i, j, k; + + for(i=0; i < CTILDEBYTES; ++i) + sig[i] = c[i]; + sig += CTILDEBYTES; + + for(i = 0; i < L; ++i) + polyz_pack(sig + i*POLYZ_PACKEDBYTES, &z->vec[i]); + sig += L*POLYZ_PACKEDBYTES; + + /* Encode h */ + for(i = 0; i < OMEGA + K; ++i) + sig[i] = 0; + + k = 0; + for(i = 0; i < K; ++i) { + for(j = 0; j < N; ++j) + if(h->vec[i].coeffs[j] != 0) + sig[k++] = j; + + sig[OMEGA + i] = k; + } +} + +/************************************************* +* Name: unpack_sig +* +* Description: Unpack signature sig = (c, z, h). +* +* Arguments: - uint8_t *c: pointer to output challenge hash +* - polyvecl *z: pointer to output vector z +* - polyveck *h: pointer to output hint vector h +* - const uint8_t sig[]: byte array containing +* bit-packed signature +* +* Returns 1 in case of malformed signature; otherwise 0. +**************************************************/ +int unpack_sig(uint8_t c[CTILDEBYTES], + polyvecl *z, + polyveck *h, + const uint8_t sig[CRYPTO_BYTES]) +{ + unsigned int i, j, k; + + for(i = 0; i < CTILDEBYTES; ++i) + c[i] = sig[i]; + sig += CTILDEBYTES; + + for(i = 0; i < L; ++i) + polyz_unpack(&z->vec[i], sig + i*POLYZ_PACKEDBYTES); + sig += L*POLYZ_PACKEDBYTES; + + /* Decode h */ + k = 0; + for(i = 0; i < K; ++i) { + for(j = 0; j < N; ++j) + h->vec[i].coeffs[j] = 0; + + if(sig[OMEGA + i] < k || sig[OMEGA + i] > OMEGA) + return 1; + + for(j = k; j < sig[OMEGA + i]; ++j) { + /* Coefficients are ordered for strong unforgeability */ + if(j > k && sig[j] <= sig[j-1]) return 1; + h->vec[i].coeffs[sig[j]] = 1; + } + + k = sig[OMEGA + i]; + } + + /* Extra indices are zero for strong unforgeability */ + for(j = k; j < OMEGA; ++j) + if(sig[j]) + return 1; + + return 0; +} diff --git a/src/crypto/pq/_d/ref/packing.h b/src/crypto/pq/_d/ref/packing.h new file mode 100644 index 0000000..8e47728 --- /dev/null +++ b/src/crypto/pq/_d/ref/packing.h @@ -0,0 +1,38 @@ +#ifndef PACKING_H +#define PACKING_H + +#include +#include "params.h" +#include "polyvec.h" + +#define pack_pk DILITHIUM_NAMESPACE(pack_pk) +void pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES], const uint8_t rho[SEEDBYTES], const polyveck *t1); + +#define pack_sk DILITHIUM_NAMESPACE(pack_sk) +void pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES], + const uint8_t rho[SEEDBYTES], + const uint8_t tr[TRBYTES], + const uint8_t key[SEEDBYTES], + const polyveck *t0, + const polyvecl *s1, + const polyveck *s2); + +#define pack_sig DILITHIUM_NAMESPACE(pack_sig) +void pack_sig(uint8_t sig[CRYPTO_BYTES], const uint8_t c[CTILDEBYTES], const polyvecl *z, const polyveck *h); + +#define unpack_pk DILITHIUM_NAMESPACE(unpack_pk) +void unpack_pk(uint8_t rho[SEEDBYTES], polyveck *t1, const uint8_t pk[CRYPTO_PUBLICKEYBYTES]); + +#define unpack_sk DILITHIUM_NAMESPACE(unpack_sk) +void unpack_sk(uint8_t rho[SEEDBYTES], + uint8_t tr[TRBYTES], + uint8_t key[SEEDBYTES], + polyveck *t0, + polyvecl *s1, + polyveck *s2, + const uint8_t sk[CRYPTO_SECRETKEYBYTES]); + +#define unpack_sig DILITHIUM_NAMESPACE(unpack_sig) +int unpack_sig(uint8_t c[CTILDEBYTES], polyvecl *z, polyveck *h, const uint8_t sig[CRYPTO_BYTES]); + +#endif diff --git a/src/crypto/pq/_d/ref/params.h b/src/crypto/pq/_d/ref/params.h new file mode 100644 index 0000000..1e8a7b5 --- /dev/null +++ b/src/crypto/pq/_d/ref/params.h @@ -0,0 +1,80 @@ +#ifndef PARAMS_H +#define PARAMS_H + +#include "config.h" + +#define SEEDBYTES 32 +#define CRHBYTES 64 +#define TRBYTES 64 +#define RNDBYTES 32 +#define N 256 +#define Q 8380417 +#define D 13 +#define ROOT_OF_UNITY 1753 + +#if DILITHIUM_MODE == 2 +#define K 4 +#define L 4 +#define ETA 2 +#define TAU 39 +#define BETA 78 +#define GAMMA1 (1 << 17) +#define GAMMA2 ((Q-1)/88) +#define OMEGA 80 +#define CTILDEBYTES 32 + +#elif DILITHIUM_MODE == 3 +#define K 6 +#define L 5 +#define ETA 4 +#define TAU 49 +#define BETA 196 +#define GAMMA1 (1 << 19) +#define GAMMA2 ((Q-1)/32) +#define OMEGA 55 +#define CTILDEBYTES 48 + +#elif DILITHIUM_MODE == 5 +#define K 8 +#define L 7 +#define ETA 2 +#define TAU 60 +#define BETA 120 +#define GAMMA1 (1 << 19) +#define GAMMA2 ((Q-1)/32) +#define OMEGA 75 +#define CTILDEBYTES 64 + +#endif + +#define POLYT1_PACKEDBYTES 320 +#define POLYT0_PACKEDBYTES 416 +#define POLYVECH_PACKEDBYTES (OMEGA + K) + +#if GAMMA1 == (1 << 17) +#define POLYZ_PACKEDBYTES 576 +#elif GAMMA1 == (1 << 19) +#define POLYZ_PACKEDBYTES 640 +#endif + +#if GAMMA2 == (Q-1)/88 +#define POLYW1_PACKEDBYTES 192 +#elif GAMMA2 == (Q-1)/32 +#define POLYW1_PACKEDBYTES 128 +#endif + +#if ETA == 2 +#define POLYETA_PACKEDBYTES 96 +#elif ETA == 4 +#define POLYETA_PACKEDBYTES 128 +#endif + +#define CRYPTO_PUBLICKEYBYTES (SEEDBYTES + K*POLYT1_PACKEDBYTES) +#define CRYPTO_SECRETKEYBYTES (2*SEEDBYTES \ + + TRBYTES \ + + L*POLYETA_PACKEDBYTES \ + + K*POLYETA_PACKEDBYTES \ + + K*POLYT0_PACKEDBYTES) +#define CRYPTO_BYTES (CTILDEBYTES + L*POLYZ_PACKEDBYTES + POLYVECH_PACKEDBYTES) + +#endif diff --git a/src/crypto/pq/_d/ref/poly.c b/src/crypto/pq/_d/ref/poly.c new file mode 100644 index 0000000..0db4f42 --- /dev/null +++ b/src/crypto/pq/_d/ref/poly.c @@ -0,0 +1,907 @@ +#include +#include "params.h" +#include "poly.h" +#include "ntt.h" +#include "reduce.h" +#include "rounding.h" +#include "symmetric.h" + +#ifdef DBENCH +#include "test/cpucycles.h" +extern const uint64_t timing_overhead; +extern uint64_t *tred, *tadd, *tmul, *tround, *tsample, *tpack; +#define DBENCH_START() uint64_t time = cpucycles() +#define DBENCH_STOP(t) t += cpucycles() - time - timing_overhead +#else +#define DBENCH_START() +#define DBENCH_STOP(t) +#endif + +/************************************************* +* Name: poly_reduce +* +* Description: Inplace reduction of all coefficients of polynomial to +* representative in [-6283008,6283008]. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_reduce(poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a->coeffs[i] = reduce32(a->coeffs[i]); + + DBENCH_STOP(*tred); +} + +/************************************************* +* Name: poly_caddq +* +* Description: For all coefficients of in/out polynomial add Q if +* coefficient is negative. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_caddq(poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a->coeffs[i] = caddq(a->coeffs[i]); + + DBENCH_STOP(*tred); +} + +/************************************************* +* Name: poly_add +* +* Description: Add polynomials. No modular reduction is performed. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first summand +* - const poly *b: pointer to second summand +**************************************************/ +void poly_add(poly *c, const poly *a, const poly *b) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + c->coeffs[i] = a->coeffs[i] + b->coeffs[i]; + + DBENCH_STOP(*tadd); +} + +/************************************************* +* Name: poly_sub +* +* Description: Subtract polynomials. No modular reduction is +* performed. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first input polynomial +* - const poly *b: pointer to second input polynomial to be +* subtraced from first input polynomial +**************************************************/ +void poly_sub(poly *c, const poly *a, const poly *b) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + c->coeffs[i] = a->coeffs[i] - b->coeffs[i]; + + DBENCH_STOP(*tadd); +} + +/************************************************* +* Name: poly_shiftl +* +* Description: Multiply polynomial by 2^D without modular reduction. Assumes +* input coefficients to be less than 2^{31-D} in absolute value. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_shiftl(poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a->coeffs[i] <<= D; + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_ntt +* +* Description: Inplace forward NTT. Coefficients can grow by +* 8*Q in absolute value. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_ntt(poly *a) { + DBENCH_START(); + + ntt(a->coeffs); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_invntt_tomont +* +* Description: Inplace inverse NTT and multiplication by 2^{32}. +* Input coefficients need to be less than Q in absolute +* value and output coefficients are again bounded by Q. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_invntt_tomont(poly *a) { + DBENCH_START(); + + invntt_tomont(a->coeffs); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_pointwise_montgomery +* +* Description: Pointwise multiplication of polynomials in NTT domain +* representation and multiplication of resulting polynomial +* by 2^{-32}. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first input polynomial +* - const poly *b: pointer to second input polynomial +**************************************************/ +void poly_pointwise_montgomery(poly *c, const poly *a, const poly *b) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + c->coeffs[i] = montgomery_reduce((int64_t)a->coeffs[i] * b->coeffs[i]); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_power2round +* +* Description: For all coefficients c of the input polynomial, +* compute c0, c1 such that c mod Q = c1*2^D + c0 +* with -2^{D-1} < c0 <= 2^{D-1}. Assumes coefficients to be +* standard representatives. +* +* Arguments: - poly *a1: pointer to output polynomial with coefficients c1 +* - poly *a0: pointer to output polynomial with coefficients c0 +* - const poly *a: pointer to input polynomial +**************************************************/ +void poly_power2round(poly *a1, poly *a0, const poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a1->coeffs[i] = power2round(&a0->coeffs[i], a->coeffs[i]); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_decompose +* +* Description: For all coefficients c of the input polynomial, +* compute high and low bits c0, c1 such c mod Q = c1*ALPHA + c0 +* with -ALPHA/2 < c0 <= ALPHA/2 except c1 = (Q-1)/ALPHA where we +* set c1 = 0 and -ALPHA/2 <= c0 = c mod Q - Q < 0. +* Assumes coefficients to be standard representatives. +* +* Arguments: - poly *a1: pointer to output polynomial with coefficients c1 +* - poly *a0: pointer to output polynomial with coefficients c0 +* - const poly *a: pointer to input polynomial +**************************************************/ +void poly_decompose(poly *a1, poly *a0, const poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a1->coeffs[i] = decompose(&a0->coeffs[i], a->coeffs[i]); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_make_hint +* +* Description: Compute hint polynomial. The coefficients of which indicate +* whether the low bits of the corresponding coefficient of +* the input polynomial overflow into the high bits. +* +* Arguments: - poly *h: pointer to output hint polynomial +* - const poly *a0: pointer to low part of input polynomial +* - const poly *a1: pointer to high part of input polynomial +* +* Returns number of 1 bits. +**************************************************/ +unsigned int poly_make_hint(poly *h, const poly *a0, const poly *a1) { + unsigned int i, s = 0; + DBENCH_START(); + + for(i = 0; i < N; ++i) { + h->coeffs[i] = make_hint(a0->coeffs[i], a1->coeffs[i]); + s += h->coeffs[i]; + } + + DBENCH_STOP(*tround); + return s; +} + +/************************************************* +* Name: poly_use_hint +* +* Description: Use hint polynomial to correct the high bits of a polynomial. +* +* Arguments: - poly *b: pointer to output polynomial with corrected high bits +* - const poly *a: pointer to input polynomial +* - const poly *h: pointer to input hint polynomial +**************************************************/ +void poly_use_hint(poly *b, const poly *a, const poly *h) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + b->coeffs[i] = use_hint(a->coeffs[i], h->coeffs[i]); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_chknorm +* +* Description: Check infinity norm of polynomial against given bound. +* Assumes input coefficients were reduced by reduce32(). +* +* Arguments: - const poly *a: pointer to polynomial +* - int32_t B: norm bound +* +* Returns 0 if norm is strictly smaller than B <= (Q-1)/8 and 1 otherwise. +**************************************************/ +int poly_chknorm(const poly *a, int32_t B) { + unsigned int i; + int32_t t; + DBENCH_START(); + + if(B > (Q-1)/8) + return 1; + + /* It is ok to leak which coefficient violates the bound since + the probability for each coefficient is independent of secret + data but we must not leak the sign of the centralized representative. */ + for(i = 0; i < N; ++i) { + /* Absolute value */ + t = a->coeffs[i] >> 31; + t = a->coeffs[i] - (t & 2*a->coeffs[i]); + + if(t >= B) { + DBENCH_STOP(*tsample); + return 1; + } + } + + DBENCH_STOP(*tsample); + return 0; +} + +/************************************************* +* Name: rej_uniform +* +* Description: Sample uniformly random coefficients in [0, Q-1] by +* performing rejection sampling on array of random bytes. +* +* Arguments: - int32_t *a: pointer to output array (allocated) +* - unsigned int len: number of coefficients to be sampled +* - const uint8_t *buf: array of random bytes +* - unsigned int buflen: length of array of random bytes +* +* Returns number of sampled coefficients. Can be smaller than len if not enough +* random bytes were given. +**************************************************/ +static unsigned int rej_uniform(int32_t *a, + unsigned int len, + const uint8_t *buf, + unsigned int buflen) +{ + unsigned int ctr, pos; + uint32_t t; + DBENCH_START(); + + ctr = pos = 0; + while(ctr < len && pos + 3 <= buflen) { + t = buf[pos++]; + t |= (uint32_t)buf[pos++] << 8; + t |= (uint32_t)buf[pos++] << 16; + t &= 0x7FFFFF; + + if(t < Q) + a[ctr++] = t; + } + + DBENCH_STOP(*tsample); + return ctr; +} + +/************************************************* +* Name: poly_uniform +* +* Description: Sample polynomial with uniformly random coefficients +* in [0,Q-1] by performing rejection sampling on the +* output stream of SHAKE128(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length SEEDBYTES +* - uint16_t nonce: 2-byte nonce +**************************************************/ +#define POLY_UNIFORM_NBLOCKS ((768 + STREAM128_BLOCKBYTES - 1)/STREAM128_BLOCKBYTES) +void poly_uniform(poly *a, + const uint8_t seed[SEEDBYTES], + uint16_t nonce) +{ + unsigned int i, ctr, off; + unsigned int buflen = POLY_UNIFORM_NBLOCKS*STREAM128_BLOCKBYTES; + uint8_t buf[POLY_UNIFORM_NBLOCKS*STREAM128_BLOCKBYTES + 2]; + stream128_state state; + + stream128_init(&state, seed, nonce); + stream128_squeezeblocks(buf, POLY_UNIFORM_NBLOCKS, &state); + + ctr = rej_uniform(a->coeffs, N, buf, buflen); + + while(ctr < N) { + off = buflen % 3; + for(i = 0; i < off; ++i) + buf[i] = buf[buflen - off + i]; + + stream128_squeezeblocks(buf + off, 1, &state); + buflen = STREAM128_BLOCKBYTES + off; + ctr += rej_uniform(a->coeffs + ctr, N - ctr, buf, buflen); + } +} + +/************************************************* +* Name: rej_eta +* +* Description: Sample uniformly random coefficients in [-ETA, ETA] by +* performing rejection sampling on array of random bytes. +* +* Arguments: - int32_t *a: pointer to output array (allocated) +* - unsigned int len: number of coefficients to be sampled +* - const uint8_t *buf: array of random bytes +* - unsigned int buflen: length of array of random bytes +* +* Returns number of sampled coefficients. Can be smaller than len if not enough +* random bytes were given. +**************************************************/ +static unsigned int rej_eta(int32_t *a, + unsigned int len, + const uint8_t *buf, + unsigned int buflen) +{ + unsigned int ctr, pos; + uint32_t t0, t1; + DBENCH_START(); + + ctr = pos = 0; + while(ctr < len && pos < buflen) { + t0 = buf[pos] & 0x0F; + t1 = buf[pos++] >> 4; + +#if ETA == 2 + if(t0 < 15) { + t0 = t0 - (205*t0 >> 10)*5; + a[ctr++] = 2 - t0; + } + if(t1 < 15 && ctr < len) { + t1 = t1 - (205*t1 >> 10)*5; + a[ctr++] = 2 - t1; + } +#elif ETA == 4 + if(t0 < 9) + a[ctr++] = 4 - t0; + if(t1 < 9 && ctr < len) + a[ctr++] = 4 - t1; +#endif + } + + DBENCH_STOP(*tsample); + return ctr; +} + +/************************************************* +* Name: poly_uniform_eta +* +* Description: Sample polynomial with uniformly random coefficients +* in [-ETA,ETA] by performing rejection sampling on the +* output stream from SHAKE256(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length CRHBYTES +* - uint16_t nonce: 2-byte nonce +**************************************************/ +#if ETA == 2 +#define POLY_UNIFORM_ETA_NBLOCKS ((136 + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) +#elif ETA == 4 +#define POLY_UNIFORM_ETA_NBLOCKS ((227 + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) +#endif +void poly_uniform_eta(poly *a, + const uint8_t seed[CRHBYTES], + uint16_t nonce) +{ + unsigned int ctr; + unsigned int buflen = POLY_UNIFORM_ETA_NBLOCKS*STREAM256_BLOCKBYTES; + uint8_t buf[POLY_UNIFORM_ETA_NBLOCKS*STREAM256_BLOCKBYTES]; + stream256_state state; + + stream256_init(&state, seed, nonce); + stream256_squeezeblocks(buf, POLY_UNIFORM_ETA_NBLOCKS, &state); + + ctr = rej_eta(a->coeffs, N, buf, buflen); + + while(ctr < N) { + stream256_squeezeblocks(buf, 1, &state); + ctr += rej_eta(a->coeffs + ctr, N - ctr, buf, STREAM256_BLOCKBYTES); + } +} + +/************************************************* +* Name: poly_uniform_gamma1m1 +* +* Description: Sample polynomial with uniformly random coefficients +* in [-(GAMMA1 - 1), GAMMA1] by unpacking output stream +* of SHAKE256(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length CRHBYTES +* - uint16_t nonce: 16-bit nonce +**************************************************/ +#define POLY_UNIFORM_GAMMA1_NBLOCKS ((POLYZ_PACKEDBYTES + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) +void poly_uniform_gamma1(poly *a, + const uint8_t seed[CRHBYTES], + uint16_t nonce) +{ + uint8_t buf[POLY_UNIFORM_GAMMA1_NBLOCKS*STREAM256_BLOCKBYTES]; + stream256_state state; + + stream256_init(&state, seed, nonce); + stream256_squeezeblocks(buf, POLY_UNIFORM_GAMMA1_NBLOCKS, &state); + polyz_unpack(a, buf); +} + +/************************************************* +* Name: challenge +* +* Description: Implementation of H. Samples polynomial with TAU nonzero +* coefficients in {-1,1} using the output stream of +* SHAKE256(seed). +* +* Arguments: - poly *c: pointer to output polynomial +* - const uint8_t mu[]: byte array containing seed of length CTILDEBYTES +**************************************************/ +void poly_challenge(poly *c, const uint8_t seed[CTILDEBYTES]) { + unsigned int i, b, pos; + uint64_t signs; + uint8_t buf[SHAKE256_RATE]; + keccak_state state; + + shake256_init(&state); + shake256_absorb(&state, seed, CTILDEBYTES); + shake256_finalize(&state); + shake256_squeezeblocks(buf, 1, &state); + + signs = 0; + for(i = 0; i < 8; ++i) + signs |= (uint64_t)buf[i] << 8*i; + pos = 8; + + for(i = 0; i < N; ++i) + c->coeffs[i] = 0; + for(i = N-TAU; i < N; ++i) { + do { + if(pos >= SHAKE256_RATE) { + shake256_squeezeblocks(buf, 1, &state); + pos = 0; + } + + b = buf[pos++]; + } while(b > i); + + c->coeffs[i] = c->coeffs[b]; + c->coeffs[b] = 1 - 2*(signs & 1); + signs >>= 1; + } +} + +/************************************************* +* Name: polyeta_pack +* +* Description: Bit-pack polynomial with coefficients in [-ETA,ETA]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYETA_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyeta_pack(uint8_t *r, const poly *a) { + unsigned int i; + uint8_t t[8]; + DBENCH_START(); + +#if ETA == 2 + for(i = 0; i < N/8; ++i) { + t[0] = ETA - a->coeffs[8*i+0]; + t[1] = ETA - a->coeffs[8*i+1]; + t[2] = ETA - a->coeffs[8*i+2]; + t[3] = ETA - a->coeffs[8*i+3]; + t[4] = ETA - a->coeffs[8*i+4]; + t[5] = ETA - a->coeffs[8*i+5]; + t[6] = ETA - a->coeffs[8*i+6]; + t[7] = ETA - a->coeffs[8*i+7]; + + r[3*i+0] = (t[0] >> 0) | (t[1] << 3) | (t[2] << 6); + r[3*i+1] = (t[2] >> 2) | (t[3] << 1) | (t[4] << 4) | (t[5] << 7); + r[3*i+2] = (t[5] >> 1) | (t[6] << 2) | (t[7] << 5); + } +#elif ETA == 4 + for(i = 0; i < N/2; ++i) { + t[0] = ETA - a->coeffs[2*i+0]; + t[1] = ETA - a->coeffs[2*i+1]; + r[i] = t[0] | (t[1] << 4); + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyeta_unpack +* +* Description: Unpack polynomial with coefficients in [-ETA,ETA]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyeta_unpack(poly *r, const uint8_t *a) { + unsigned int i; + DBENCH_START(); + +#if ETA == 2 + for(i = 0; i < N/8; ++i) { + r->coeffs[8*i+0] = (a[3*i+0] >> 0) & 7; + r->coeffs[8*i+1] = (a[3*i+0] >> 3) & 7; + r->coeffs[8*i+2] = ((a[3*i+0] >> 6) | (a[3*i+1] << 2)) & 7; + r->coeffs[8*i+3] = (a[3*i+1] >> 1) & 7; + r->coeffs[8*i+4] = (a[3*i+1] >> 4) & 7; + r->coeffs[8*i+5] = ((a[3*i+1] >> 7) | (a[3*i+2] << 1)) & 7; + r->coeffs[8*i+6] = (a[3*i+2] >> 2) & 7; + r->coeffs[8*i+7] = (a[3*i+2] >> 5) & 7; + + r->coeffs[8*i+0] = ETA - r->coeffs[8*i+0]; + r->coeffs[8*i+1] = ETA - r->coeffs[8*i+1]; + r->coeffs[8*i+2] = ETA - r->coeffs[8*i+2]; + r->coeffs[8*i+3] = ETA - r->coeffs[8*i+3]; + r->coeffs[8*i+4] = ETA - r->coeffs[8*i+4]; + r->coeffs[8*i+5] = ETA - r->coeffs[8*i+5]; + r->coeffs[8*i+6] = ETA - r->coeffs[8*i+6]; + r->coeffs[8*i+7] = ETA - r->coeffs[8*i+7]; + } +#elif ETA == 4 + for(i = 0; i < N/2; ++i) { + r->coeffs[2*i+0] = a[i] & 0x0F; + r->coeffs[2*i+1] = a[i] >> 4; + r->coeffs[2*i+0] = ETA - r->coeffs[2*i+0]; + r->coeffs[2*i+1] = ETA - r->coeffs[2*i+1]; + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt1_pack +* +* Description: Bit-pack polynomial t1 with coefficients fitting in 10 bits. +* Input coefficients are assumed to be standard representatives. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYT1_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyt1_pack(uint8_t *r, const poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/4; ++i) { + r[5*i+0] = (a->coeffs[4*i+0] >> 0); + r[5*i+1] = (a->coeffs[4*i+0] >> 8) | (a->coeffs[4*i+1] << 2); + r[5*i+2] = (a->coeffs[4*i+1] >> 6) | (a->coeffs[4*i+2] << 4); + r[5*i+3] = (a->coeffs[4*i+2] >> 4) | (a->coeffs[4*i+3] << 6); + r[5*i+4] = (a->coeffs[4*i+3] >> 2); + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt1_unpack +* +* Description: Unpack polynomial t1 with 10-bit coefficients. +* Output coefficients are standard representatives. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyt1_unpack(poly *r, const uint8_t *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/4; ++i) { + r->coeffs[4*i+0] = ((a[5*i+0] >> 0) | ((uint32_t)a[5*i+1] << 8)) & 0x3FF; + r->coeffs[4*i+1] = ((a[5*i+1] >> 2) | ((uint32_t)a[5*i+2] << 6)) & 0x3FF; + r->coeffs[4*i+2] = ((a[5*i+2] >> 4) | ((uint32_t)a[5*i+3] << 4)) & 0x3FF; + r->coeffs[4*i+3] = ((a[5*i+3] >> 6) | ((uint32_t)a[5*i+4] << 2)) & 0x3FF; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt0_pack +* +* Description: Bit-pack polynomial t0 with coefficients in ]-2^{D-1}, 2^{D-1}]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYT0_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyt0_pack(uint8_t *r, const poly *a) { + unsigned int i; + uint32_t t[8]; + DBENCH_START(); + + for(i = 0; i < N/8; ++i) { + t[0] = (1 << (D-1)) - a->coeffs[8*i+0]; + t[1] = (1 << (D-1)) - a->coeffs[8*i+1]; + t[2] = (1 << (D-1)) - a->coeffs[8*i+2]; + t[3] = (1 << (D-1)) - a->coeffs[8*i+3]; + t[4] = (1 << (D-1)) - a->coeffs[8*i+4]; + t[5] = (1 << (D-1)) - a->coeffs[8*i+5]; + t[6] = (1 << (D-1)) - a->coeffs[8*i+6]; + t[7] = (1 << (D-1)) - a->coeffs[8*i+7]; + + r[13*i+ 0] = t[0]; + r[13*i+ 1] = t[0] >> 8; + r[13*i+ 1] |= t[1] << 5; + r[13*i+ 2] = t[1] >> 3; + r[13*i+ 3] = t[1] >> 11; + r[13*i+ 3] |= t[2] << 2; + r[13*i+ 4] = t[2] >> 6; + r[13*i+ 4] |= t[3] << 7; + r[13*i+ 5] = t[3] >> 1; + r[13*i+ 6] = t[3] >> 9; + r[13*i+ 6] |= t[4] << 4; + r[13*i+ 7] = t[4] >> 4; + r[13*i+ 8] = t[4] >> 12; + r[13*i+ 8] |= t[5] << 1; + r[13*i+ 9] = t[5] >> 7; + r[13*i+ 9] |= t[6] << 6; + r[13*i+10] = t[6] >> 2; + r[13*i+11] = t[6] >> 10; + r[13*i+11] |= t[7] << 3; + r[13*i+12] = t[7] >> 5; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt0_unpack +* +* Description: Unpack polynomial t0 with coefficients in ]-2^{D-1}, 2^{D-1}]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyt0_unpack(poly *r, const uint8_t *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/8; ++i) { + r->coeffs[8*i+0] = a[13*i+0]; + r->coeffs[8*i+0] |= (uint32_t)a[13*i+1] << 8; + r->coeffs[8*i+0] &= 0x1FFF; + + r->coeffs[8*i+1] = a[13*i+1] >> 5; + r->coeffs[8*i+1] |= (uint32_t)a[13*i+2] << 3; + r->coeffs[8*i+1] |= (uint32_t)a[13*i+3] << 11; + r->coeffs[8*i+1] &= 0x1FFF; + + r->coeffs[8*i+2] = a[13*i+3] >> 2; + r->coeffs[8*i+2] |= (uint32_t)a[13*i+4] << 6; + r->coeffs[8*i+2] &= 0x1FFF; + + r->coeffs[8*i+3] = a[13*i+4] >> 7; + r->coeffs[8*i+3] |= (uint32_t)a[13*i+5] << 1; + r->coeffs[8*i+3] |= (uint32_t)a[13*i+6] << 9; + r->coeffs[8*i+3] &= 0x1FFF; + + r->coeffs[8*i+4] = a[13*i+6] >> 4; + r->coeffs[8*i+4] |= (uint32_t)a[13*i+7] << 4; + r->coeffs[8*i+4] |= (uint32_t)a[13*i+8] << 12; + r->coeffs[8*i+4] &= 0x1FFF; + + r->coeffs[8*i+5] = a[13*i+8] >> 1; + r->coeffs[8*i+5] |= (uint32_t)a[13*i+9] << 7; + r->coeffs[8*i+5] &= 0x1FFF; + + r->coeffs[8*i+6] = a[13*i+9] >> 6; + r->coeffs[8*i+6] |= (uint32_t)a[13*i+10] << 2; + r->coeffs[8*i+6] |= (uint32_t)a[13*i+11] << 10; + r->coeffs[8*i+6] &= 0x1FFF; + + r->coeffs[8*i+7] = a[13*i+11] >> 3; + r->coeffs[8*i+7] |= (uint32_t)a[13*i+12] << 5; + r->coeffs[8*i+7] &= 0x1FFF; + + r->coeffs[8*i+0] = (1 << (D-1)) - r->coeffs[8*i+0]; + r->coeffs[8*i+1] = (1 << (D-1)) - r->coeffs[8*i+1]; + r->coeffs[8*i+2] = (1 << (D-1)) - r->coeffs[8*i+2]; + r->coeffs[8*i+3] = (1 << (D-1)) - r->coeffs[8*i+3]; + r->coeffs[8*i+4] = (1 << (D-1)) - r->coeffs[8*i+4]; + r->coeffs[8*i+5] = (1 << (D-1)) - r->coeffs[8*i+5]; + r->coeffs[8*i+6] = (1 << (D-1)) - r->coeffs[8*i+6]; + r->coeffs[8*i+7] = (1 << (D-1)) - r->coeffs[8*i+7]; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyz_pack +* +* Description: Bit-pack polynomial with coefficients +* in [-(GAMMA1 - 1), GAMMA1]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYZ_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyz_pack(uint8_t *r, const poly *a) { + unsigned int i; + uint32_t t[4]; + DBENCH_START(); + +#if GAMMA1 == (1 << 17) + for(i = 0; i < N/4; ++i) { + t[0] = GAMMA1 - a->coeffs[4*i+0]; + t[1] = GAMMA1 - a->coeffs[4*i+1]; + t[2] = GAMMA1 - a->coeffs[4*i+2]; + t[3] = GAMMA1 - a->coeffs[4*i+3]; + + r[9*i+0] = t[0]; + r[9*i+1] = t[0] >> 8; + r[9*i+2] = t[0] >> 16; + r[9*i+2] |= t[1] << 2; + r[9*i+3] = t[1] >> 6; + r[9*i+4] = t[1] >> 14; + r[9*i+4] |= t[2] << 4; + r[9*i+5] = t[2] >> 4; + r[9*i+6] = t[2] >> 12; + r[9*i+6] |= t[3] << 6; + r[9*i+7] = t[3] >> 2; + r[9*i+8] = t[3] >> 10; + } +#elif GAMMA1 == (1 << 19) + for(i = 0; i < N/2; ++i) { + t[0] = GAMMA1 - a->coeffs[2*i+0]; + t[1] = GAMMA1 - a->coeffs[2*i+1]; + + r[5*i+0] = t[0]; + r[5*i+1] = t[0] >> 8; + r[5*i+2] = t[0] >> 16; + r[5*i+2] |= t[1] << 4; + r[5*i+3] = t[1] >> 4; + r[5*i+4] = t[1] >> 12; + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyz_unpack +* +* Description: Unpack polynomial z with coefficients +* in [-(GAMMA1 - 1), GAMMA1]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyz_unpack(poly *r, const uint8_t *a) { + unsigned int i; + DBENCH_START(); + +#if GAMMA1 == (1 << 17) + for(i = 0; i < N/4; ++i) { + r->coeffs[4*i+0] = a[9*i+0]; + r->coeffs[4*i+0] |= (uint32_t)a[9*i+1] << 8; + r->coeffs[4*i+0] |= (uint32_t)a[9*i+2] << 16; + r->coeffs[4*i+0] &= 0x3FFFF; + + r->coeffs[4*i+1] = a[9*i+2] >> 2; + r->coeffs[4*i+1] |= (uint32_t)a[9*i+3] << 6; + r->coeffs[4*i+1] |= (uint32_t)a[9*i+4] << 14; + r->coeffs[4*i+1] &= 0x3FFFF; + + r->coeffs[4*i+2] = a[9*i+4] >> 4; + r->coeffs[4*i+2] |= (uint32_t)a[9*i+5] << 4; + r->coeffs[4*i+2] |= (uint32_t)a[9*i+6] << 12; + r->coeffs[4*i+2] &= 0x3FFFF; + + r->coeffs[4*i+3] = a[9*i+6] >> 6; + r->coeffs[4*i+3] |= (uint32_t)a[9*i+7] << 2; + r->coeffs[4*i+3] |= (uint32_t)a[9*i+8] << 10; + r->coeffs[4*i+3] &= 0x3FFFF; + + r->coeffs[4*i+0] = GAMMA1 - r->coeffs[4*i+0]; + r->coeffs[4*i+1] = GAMMA1 - r->coeffs[4*i+1]; + r->coeffs[4*i+2] = GAMMA1 - r->coeffs[4*i+2]; + r->coeffs[4*i+3] = GAMMA1 - r->coeffs[4*i+3]; + } +#elif GAMMA1 == (1 << 19) + for(i = 0; i < N/2; ++i) { + r->coeffs[2*i+0] = a[5*i+0]; + r->coeffs[2*i+0] |= (uint32_t)a[5*i+1] << 8; + r->coeffs[2*i+0] |= (uint32_t)a[5*i+2] << 16; + r->coeffs[2*i+0] &= 0xFFFFF; + + r->coeffs[2*i+1] = a[5*i+2] >> 4; + r->coeffs[2*i+1] |= (uint32_t)a[5*i+3] << 4; + r->coeffs[2*i+1] |= (uint32_t)a[5*i+4] << 12; + /* r->coeffs[2*i+1] &= 0xFFFFF; */ /* No effect, since we're anyway at 20 bits */ + + r->coeffs[2*i+0] = GAMMA1 - r->coeffs[2*i+0]; + r->coeffs[2*i+1] = GAMMA1 - r->coeffs[2*i+1]; + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyw1_pack +* +* Description: Bit-pack polynomial w1 with coefficients in [0,15] or [0,43]. +* Input coefficients are assumed to be standard representatives. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYW1_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyw1_pack(uint8_t *r, const poly *a) { + unsigned int i; + DBENCH_START(); + +#if GAMMA2 == (Q-1)/88 + for(i = 0; i < N/4; ++i) { + r[3*i+0] = a->coeffs[4*i+0]; + r[3*i+0] |= a->coeffs[4*i+1] << 6; + r[3*i+1] = a->coeffs[4*i+1] >> 2; + r[3*i+1] |= a->coeffs[4*i+2] << 4; + r[3*i+2] = a->coeffs[4*i+2] >> 4; + r[3*i+2] |= a->coeffs[4*i+3] << 2; + } +#elif GAMMA2 == (Q-1)/32 + for(i = 0; i < N/2; ++i) + r[i] = a->coeffs[2*i+0] | (a->coeffs[2*i+1] << 4); +#endif + + DBENCH_STOP(*tpack); +} diff --git a/src/crypto/pq/_d/ref/poly.h b/src/crypto/pq/_d/ref/poly.h new file mode 100644 index 0000000..904baa1 --- /dev/null +++ b/src/crypto/pq/_d/ref/poly.h @@ -0,0 +1,79 @@ +#ifndef POLY_H +#define POLY_H + +#include +#include "params.h" + +typedef struct { + int32_t coeffs[N]; +} poly; + +#define poly_reduce DILITHIUM_NAMESPACE(poly_reduce) +void poly_reduce(poly *a); +#define poly_caddq DILITHIUM_NAMESPACE(poly_caddq) +void poly_caddq(poly *a); + +#define poly_add DILITHIUM_NAMESPACE(poly_add) +void poly_add(poly *c, const poly *a, const poly *b); +#define poly_sub DILITHIUM_NAMESPACE(poly_sub) +void poly_sub(poly *c, const poly *a, const poly *b); +#define poly_shiftl DILITHIUM_NAMESPACE(poly_shiftl) +void poly_shiftl(poly *a); + +#define poly_ntt DILITHIUM_NAMESPACE(poly_ntt) +void poly_ntt(poly *a); +#define poly_invntt_tomont DILITHIUM_NAMESPACE(poly_invntt_tomont) +void poly_invntt_tomont(poly *a); +#define poly_pointwise_montgomery DILITHIUM_NAMESPACE(poly_pointwise_montgomery) +void poly_pointwise_montgomery(poly *c, const poly *a, const poly *b); + +#define poly_power2round DILITHIUM_NAMESPACE(poly_power2round) +void poly_power2round(poly *a1, poly *a0, const poly *a); +#define poly_decompose DILITHIUM_NAMESPACE(poly_decompose) +void poly_decompose(poly *a1, poly *a0, const poly *a); +#define poly_make_hint DILITHIUM_NAMESPACE(poly_make_hint) +unsigned int poly_make_hint(poly *h, const poly *a0, const poly *a1); +#define poly_use_hint DILITHIUM_NAMESPACE(poly_use_hint) +void poly_use_hint(poly *b, const poly *a, const poly *h); + +#define poly_chknorm DILITHIUM_NAMESPACE(poly_chknorm) +int poly_chknorm(const poly *a, int32_t B); +#define poly_uniform DILITHIUM_NAMESPACE(poly_uniform) +void poly_uniform(poly *a, + const uint8_t seed[SEEDBYTES], + uint16_t nonce); +#define poly_uniform_eta DILITHIUM_NAMESPACE(poly_uniform_eta) +void poly_uniform_eta(poly *a, + const uint8_t seed[CRHBYTES], + uint16_t nonce); +#define poly_uniform_gamma1 DILITHIUM_NAMESPACE(poly_uniform_gamma1) +void poly_uniform_gamma1(poly *a, + const uint8_t seed[CRHBYTES], + uint16_t nonce); +#define poly_challenge DILITHIUM_NAMESPACE(poly_challenge) +void poly_challenge(poly *c, const uint8_t seed[CTILDEBYTES]); + +#define polyeta_pack DILITHIUM_NAMESPACE(polyeta_pack) +void polyeta_pack(uint8_t *r, const poly *a); +#define polyeta_unpack DILITHIUM_NAMESPACE(polyeta_unpack) +void polyeta_unpack(poly *r, const uint8_t *a); + +#define polyt1_pack DILITHIUM_NAMESPACE(polyt1_pack) +void polyt1_pack(uint8_t *r, const poly *a); +#define polyt1_unpack DILITHIUM_NAMESPACE(polyt1_unpack) +void polyt1_unpack(poly *r, const uint8_t *a); + +#define polyt0_pack DILITHIUM_NAMESPACE(polyt0_pack) +void polyt0_pack(uint8_t *r, const poly *a); +#define polyt0_unpack DILITHIUM_NAMESPACE(polyt0_unpack) +void polyt0_unpack(poly *r, const uint8_t *a); + +#define polyz_pack DILITHIUM_NAMESPACE(polyz_pack) +void polyz_pack(uint8_t *r, const poly *a); +#define polyz_unpack DILITHIUM_NAMESPACE(polyz_unpack) +void polyz_unpack(poly *r, const uint8_t *a); + +#define polyw1_pack DILITHIUM_NAMESPACE(polyw1_pack) +void polyw1_pack(uint8_t *r, const poly *a); + +#endif diff --git a/src/crypto/pq/_d/ref/polyvec.c b/src/crypto/pq/_d/ref/polyvec.c new file mode 100644 index 0000000..241f618 --- /dev/null +++ b/src/crypto/pq/_d/ref/polyvec.c @@ -0,0 +1,389 @@ +#include +#include "params.h" +#include "polyvec.h" +#include "poly.h" + +/************************************************* +* Name: expand_mat +* +* Description: Implementation of ExpandA. Generates matrix A with uniformly +* random coefficients a_{i,j} by performing rejection +* sampling on the output stream of SHAKE128(rho|j|i) +* +* Arguments: - polyvecl mat[K]: output matrix +* - const uint8_t rho[]: byte array containing seed rho +**************************************************/ +void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]) { + unsigned int i, j; + + for(i = 0; i < K; ++i) + for(j = 0; j < L; ++j) + poly_uniform(&mat[i].vec[j], rho, (i << 8) + j); +} + +void polyvec_matrix_pointwise_montgomery(polyveck *t, const polyvecl mat[K], const polyvecl *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + polyvecl_pointwise_acc_montgomery(&t->vec[i], &mat[i], v); +} + +/**************************************************************/ +/************ Vectors of polynomials of length L **************/ +/**************************************************************/ + +void polyvecl_uniform_eta(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_uniform_eta(&v->vec[i], seed, nonce++); +} + +void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_uniform_gamma1(&v->vec[i], seed, L*nonce + i); +} + +void polyvecl_reduce(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_reduce(&v->vec[i]); +} + +/************************************************* +* Name: polyvecl_add +* +* Description: Add vectors of polynomials of length L. +* No modular reduction is performed. +* +* Arguments: - polyvecl *w: pointer to output vector +* - const polyvecl *u: pointer to first summand +* - const polyvecl *v: pointer to second summand +**************************************************/ +void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_add(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyvecl_ntt +* +* Description: Forward NTT of all polynomials in vector of length L. Output +* coefficients can be up to 16*Q larger than input coefficients. +* +* Arguments: - polyvecl *v: pointer to input/output vector +**************************************************/ +void polyvecl_ntt(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_ntt(&v->vec[i]); +} + +void polyvecl_invntt_tomont(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_invntt_tomont(&v->vec[i]); +} + +void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]); +} + +/************************************************* +* Name: polyvecl_pointwise_acc_montgomery +* +* Description: Pointwise multiply vectors of polynomials of length L, multiply +* resulting vector by 2^{-32} and add (accumulate) polynomials +* in it. Input/output vectors are in NTT domain representation. +* +* Arguments: - poly *w: output polynomial +* - const polyvecl *u: pointer to first input vector +* - const polyvecl *v: pointer to second input vector +**************************************************/ +void polyvecl_pointwise_acc_montgomery(poly *w, + const polyvecl *u, + const polyvecl *v) +{ + unsigned int i; + poly t; + + poly_pointwise_montgomery(w, &u->vec[0], &v->vec[0]); + for(i = 1; i < L; ++i) { + poly_pointwise_montgomery(&t, &u->vec[i], &v->vec[i]); + poly_add(w, w, &t); + } +} + +/************************************************* +* Name: polyvecl_chknorm +* +* Description: Check infinity norm of polynomials in vector of length L. +* Assumes input polyvecl to be reduced by polyvecl_reduce(). +* +* Arguments: - const polyvecl *v: pointer to vector +* - int32_t B: norm bound +* +* Returns 0 if norm of all polynomials is strictly smaller than B <= (Q-1)/8 +* and 1 otherwise. +**************************************************/ +int polyvecl_chknorm(const polyvecl *v, int32_t bound) { + unsigned int i; + + for(i = 0; i < L; ++i) + if(poly_chknorm(&v->vec[i], bound)) + return 1; + + return 0; +} + +/**************************************************************/ +/************ Vectors of polynomials of length K **************/ +/**************************************************************/ + +void polyveck_uniform_eta(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_uniform_eta(&v->vec[i], seed, nonce++); +} + +/************************************************* +* Name: polyveck_reduce +* +* Description: Reduce coefficients of polynomials in vector of length K +* to representatives in [-6283008,6283008]. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_reduce(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_reduce(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_caddq +* +* Description: For all coefficients of polynomials in vector of length K +* add Q if coefficient is negative. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_caddq(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_caddq(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_add +* +* Description: Add vectors of polynomials of length K. +* No modular reduction is performed. +* +* Arguments: - polyveck *w: pointer to output vector +* - const polyveck *u: pointer to first summand +* - const polyveck *v: pointer to second summand +**************************************************/ +void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_add(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_sub +* +* Description: Subtract vectors of polynomials of length K. +* No modular reduction is performed. +* +* Arguments: - polyveck *w: pointer to output vector +* - const polyveck *u: pointer to first input vector +* - const polyveck *v: pointer to second input vector to be +* subtracted from first input vector +**************************************************/ +void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_sub(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_shiftl +* +* Description: Multiply vector of polynomials of Length K by 2^D without modular +* reduction. Assumes input coefficients to be less than 2^{31-D}. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_shiftl(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_shiftl(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_ntt +* +* Description: Forward NTT of all polynomials in vector of length K. Output +* coefficients can be up to 16*Q larger than input coefficients. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_ntt(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_ntt(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_invntt_tomont +* +* Description: Inverse NTT and multiplication by 2^{32} of polynomials +* in vector of length K. Input coefficients need to be less +* than 2*Q. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_invntt_tomont(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_invntt_tomont(&v->vec[i]); +} + +void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]); +} + + +/************************************************* +* Name: polyveck_chknorm +* +* Description: Check infinity norm of polynomials in vector of length K. +* Assumes input polyveck to be reduced by polyveck_reduce(). +* +* Arguments: - const polyveck *v: pointer to vector +* - int32_t B: norm bound +* +* Returns 0 if norm of all polynomials are strictly smaller than B <= (Q-1)/8 +* and 1 otherwise. +**************************************************/ +int polyveck_chknorm(const polyveck *v, int32_t bound) { + unsigned int i; + + for(i = 0; i < K; ++i) + if(poly_chknorm(&v->vec[i], bound)) + return 1; + + return 0; +} + +/************************************************* +* Name: polyveck_power2round +* +* Description: For all coefficients a of polynomials in vector of length K, +* compute a0, a1 such that a mod^+ Q = a1*2^D + a0 +* with -2^{D-1} < a0 <= 2^{D-1}. Assumes coefficients to be +* standard representatives. +* +* Arguments: - polyveck *v1: pointer to output vector of polynomials with +* coefficients a1 +* - polyveck *v0: pointer to output vector of polynomials with +* coefficients a0 +* - const polyveck *v: pointer to input vector +**************************************************/ +void polyveck_power2round(polyveck *v1, polyveck *v0, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_power2round(&v1->vec[i], &v0->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_decompose +* +* Description: For all coefficients a of polynomials in vector of length K, +* compute high and low bits a0, a1 such a mod^+ Q = a1*ALPHA + a0 +* with -ALPHA/2 < a0 <= ALPHA/2 except a1 = (Q-1)/ALPHA where we +* set a1 = 0 and -ALPHA/2 <= a0 = a mod Q - Q < 0. +* Assumes coefficients to be standard representatives. +* +* Arguments: - polyveck *v1: pointer to output vector of polynomials with +* coefficients a1 +* - polyveck *v0: pointer to output vector of polynomials with +* coefficients a0 +* - const polyveck *v: pointer to input vector +**************************************************/ +void polyveck_decompose(polyveck *v1, polyveck *v0, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_decompose(&v1->vec[i], &v0->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_make_hint +* +* Description: Compute hint vector. +* +* Arguments: - polyveck *h: pointer to output vector +* - const polyveck *v0: pointer to low part of input vector +* - const polyveck *v1: pointer to high part of input vector +* +* Returns number of 1 bits. +**************************************************/ +unsigned int polyveck_make_hint(polyveck *h, + const polyveck *v0, + const polyveck *v1) +{ + unsigned int i, s = 0; + + for(i = 0; i < K; ++i) + s += poly_make_hint(&h->vec[i], &v0->vec[i], &v1->vec[i]); + + return s; +} + +/************************************************* +* Name: polyveck_use_hint +* +* Description: Use hint vector to correct the high bits of input vector. +* +* Arguments: - polyveck *w: pointer to output vector of polynomials with +* corrected high bits +* - const polyveck *u: pointer to input vector +* - const polyveck *h: pointer to input hint vector +**************************************************/ +void polyveck_use_hint(polyveck *w, const polyveck *u, const polyveck *h) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_use_hint(&w->vec[i], &u->vec[i], &h->vec[i]); +} + +void polyveck_pack_w1(uint8_t r[K*POLYW1_PACKEDBYTES], const polyveck *w1) { + unsigned int i; + + for(i = 0; i < K; ++i) + polyw1_pack(&r[i*POLYW1_PACKEDBYTES], &w1->vec[i]); +} diff --git a/src/crypto/pq/_d/ref/polyvec.h b/src/crypto/pq/_d/ref/polyvec.h new file mode 100644 index 0000000..615ac52 --- /dev/null +++ b/src/crypto/pq/_d/ref/polyvec.h @@ -0,0 +1,93 @@ +#ifndef POLYVEC_H +#define POLYVEC_H + +#include +#include "params.h" +#include "poly.h" + +/* Vectors of polynomials of length L */ +typedef struct { + poly vec[L]; +} polyvecl; + +#define polyvecl_uniform_eta DILITHIUM_NAMESPACE(polyvecl_uniform_eta) +void polyvecl_uniform_eta(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyvecl_uniform_gamma1 DILITHIUM_NAMESPACE(polyvecl_uniform_gamma1) +void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyvecl_reduce DILITHIUM_NAMESPACE(polyvecl_reduce) +void polyvecl_reduce(polyvecl *v); + +#define polyvecl_add DILITHIUM_NAMESPACE(polyvecl_add) +void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v); + +#define polyvecl_ntt DILITHIUM_NAMESPACE(polyvecl_ntt) +void polyvecl_ntt(polyvecl *v); +#define polyvecl_invntt_tomont DILITHIUM_NAMESPACE(polyvecl_invntt_tomont) +void polyvecl_invntt_tomont(polyvecl *v); +#define polyvecl_pointwise_poly_montgomery DILITHIUM_NAMESPACE(polyvecl_pointwise_poly_montgomery) +void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v); +#define polyvecl_pointwise_acc_montgomery \ + DILITHIUM_NAMESPACE(polyvecl_pointwise_acc_montgomery) +void polyvecl_pointwise_acc_montgomery(poly *w, + const polyvecl *u, + const polyvecl *v); + + +#define polyvecl_chknorm DILITHIUM_NAMESPACE(polyvecl_chknorm) +int polyvecl_chknorm(const polyvecl *v, int32_t B); + + + +/* Vectors of polynomials of length K */ +typedef struct { + poly vec[K]; +} polyveck; + +#define polyveck_uniform_eta DILITHIUM_NAMESPACE(polyveck_uniform_eta) +void polyveck_uniform_eta(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyveck_reduce DILITHIUM_NAMESPACE(polyveck_reduce) +void polyveck_reduce(polyveck *v); +#define polyveck_caddq DILITHIUM_NAMESPACE(polyveck_caddq) +void polyveck_caddq(polyveck *v); + +#define polyveck_add DILITHIUM_NAMESPACE(polyveck_add) +void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v); +#define polyveck_sub DILITHIUM_NAMESPACE(polyveck_sub) +void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v); +#define polyveck_shiftl DILITHIUM_NAMESPACE(polyveck_shiftl) +void polyveck_shiftl(polyveck *v); + +#define polyveck_ntt DILITHIUM_NAMESPACE(polyveck_ntt) +void polyveck_ntt(polyveck *v); +#define polyveck_invntt_tomont DILITHIUM_NAMESPACE(polyveck_invntt_tomont) +void polyveck_invntt_tomont(polyveck *v); +#define polyveck_pointwise_poly_montgomery DILITHIUM_NAMESPACE(polyveck_pointwise_poly_montgomery) +void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v); + +#define polyveck_chknorm DILITHIUM_NAMESPACE(polyveck_chknorm) +int polyveck_chknorm(const polyveck *v, int32_t B); + +#define polyveck_power2round DILITHIUM_NAMESPACE(polyveck_power2round) +void polyveck_power2round(polyveck *v1, polyveck *v0, const polyveck *v); +#define polyveck_decompose DILITHIUM_NAMESPACE(polyveck_decompose) +void polyveck_decompose(polyveck *v1, polyveck *v0, const polyveck *v); +#define polyveck_make_hint DILITHIUM_NAMESPACE(polyveck_make_hint) +unsigned int polyveck_make_hint(polyveck *h, + const polyveck *v0, + const polyveck *v1); +#define polyveck_use_hint DILITHIUM_NAMESPACE(polyveck_use_hint) +void polyveck_use_hint(polyveck *w, const polyveck *v, const polyveck *h); + +#define polyveck_pack_w1 DILITHIUM_NAMESPACE(polyveck_pack_w1) +void polyveck_pack_w1(uint8_t r[K*POLYW1_PACKEDBYTES], const polyveck *w1); + +#define polyvec_matrix_expand DILITHIUM_NAMESPACE(polyvec_matrix_expand) +void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]); + +#define polyvec_matrix_pointwise_montgomery DILITHIUM_NAMESPACE(polyvec_matrix_pointwise_montgomery) +void polyvec_matrix_pointwise_montgomery(polyveck *t, const polyvecl mat[K], const polyvecl *v); + +#endif diff --git a/src/crypto/pq/_d/ref/precomp.gp b/src/crypto/pq/_d/ref/precomp.gp new file mode 100644 index 0000000..74dbdf8 --- /dev/null +++ b/src/crypto/pq/_d/ref/precomp.gp @@ -0,0 +1,12 @@ +precomp() = { + brv = [128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255]; + + q = 2^23 - 2^13 + 1; + qinv = Mod(1/q,2^32); + mont = Mod(2^32,q); + + z = 0; + for(i = 1, q-1, z = Mod(i,q); if(znorder(z) == 512, break)); + zetas = vector(255, i, centerlift(mont * z^(brv[i]))); + return(zetas); +} diff --git a/src/crypto/pq/_d/ref/randombytes.c b/src/crypto/pq/_d/ref/randombytes.c new file mode 100644 index 0000000..7f4b857 --- /dev/null +++ b/src/crypto/pq/_d/ref/randombytes.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include "randombytes.h" + +#ifdef _WIN32 +#include +#include +#else +#include +#include +#ifdef __linux__ +#define _GNU_SOURCE +#include +#include +#else +#include +#endif +#endif + +#ifdef _WIN32 +void randombytes(uint8_t *out, size_t outlen) { + HCRYPTPROV ctx; + size_t len; + + if(!CryptAcquireContext(&ctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) + abort(); + + while(outlen > 0) { + len = (outlen > 1048576) ? 1048576 : outlen; + if(!CryptGenRandom(ctx, len, (BYTE *)out)) + abort(); + + out += len; + outlen -= len; + } + + if(!CryptReleaseContext(ctx, 0)) + abort(); +} +#elif defined(__linux__) && defined(SYS_getrandom) +void randombytes(uint8_t *out, size_t outlen) { + ssize_t ret; + + while(outlen > 0) { + ret = syscall(SYS_getrandom, out, outlen, 0); + if(ret == -1 && errno == EINTR) + continue; + else if(ret == -1) + abort(); + + out += ret; + outlen -= ret; + } +} +#else +void randombytes(uint8_t *out, size_t outlen) { + static int fd = -1; + ssize_t ret; + + while(fd == -1) { + fd = open("/dev/urandom", O_RDONLY); + if(fd == -1 && errno == EINTR) + continue; + else if(fd == -1) + abort(); + } + + while(outlen > 0) { + ret = read(fd, out, outlen); + if(ret == -1 && errno == EINTR) + continue; + else if(ret == -1) + abort(); + + out += ret; + outlen -= ret; + } +} +#endif diff --git a/src/crypto/pq/_d/ref/randombytes.h b/src/crypto/pq/_d/ref/randombytes.h new file mode 100644 index 0000000..619b7f9 --- /dev/null +++ b/src/crypto/pq/_d/ref/randombytes.h @@ -0,0 +1,9 @@ +#ifndef RANDOMBYTES_H +#define RANDOMBYTES_H + +#include +#include + +void randombytes(uint8_t *out, size_t outlen); + +#endif diff --git a/src/crypto/pq/_d/ref/reduce.c b/src/crypto/pq/_d/ref/reduce.c new file mode 100644 index 0000000..8479a22 --- /dev/null +++ b/src/crypto/pq/_d/ref/reduce.c @@ -0,0 +1,69 @@ +#include +#include "params.h" +#include "reduce.h" + +/************************************************* +* Name: montgomery_reduce +* +* Description: For finite field element a with -2^{31}Q <= a <= Q*2^31, +* compute r \equiv a*2^{-32} (mod Q) such that -Q < r < Q. +* +* Arguments: - int64_t: finite field element a +* +* Returns r. +**************************************************/ +int32_t montgomery_reduce(int64_t a) { + int32_t t; + + t = (int64_t)(int32_t)a*QINV; + t = (a - (int64_t)t*Q) >> 32; + return t; +} + +/************************************************* +* Name: reduce32 +* +* Description: For finite field element a with a <= 2^{31} - 2^{22} - 1, +* compute r \equiv a (mod Q) such that -6283008 <= r <= 6283008. +* +* Arguments: - int32_t: finite field element a +* +* Returns r. +**************************************************/ +int32_t reduce32(int32_t a) { + int32_t t; + + t = (a + (1 << 22)) >> 23; + t = a - t*Q; + return t; +} + +/************************************************* +* Name: caddq +* +* Description: Add Q if input coefficient is negative. +* +* Arguments: - int32_t: finite field element a +* +* Returns r. +**************************************************/ +int32_t caddq(int32_t a) { + a += (a >> 31) & Q; + return a; +} + +/************************************************* +* Name: freeze +* +* Description: For finite field element a, compute standard +* representative r = a mod^+ Q. +* +* Arguments: - int32_t: finite field element a +* +* Returns r. +**************************************************/ +int32_t freeze(int32_t a) { + a = reduce32(a); + a = caddq(a); + return a; +} diff --git a/src/crypto/pq/_d/ref/reduce.h b/src/crypto/pq/_d/ref/reduce.h new file mode 100644 index 0000000..26d9b4e --- /dev/null +++ b/src/crypto/pq/_d/ref/reduce.h @@ -0,0 +1,22 @@ +#ifndef REDUCE_H +#define REDUCE_H + +#include +#include "params.h" + +#define MONT -4186625 // 2^32 % Q +#define QINV 58728449 // q^(-1) mod 2^32 + +#define montgomery_reduce DILITHIUM_NAMESPACE(montgomery_reduce) +int32_t montgomery_reduce(int64_t a); + +#define reduce32 DILITHIUM_NAMESPACE(reduce32) +int32_t reduce32(int32_t a); + +#define caddq DILITHIUM_NAMESPACE(caddq) +int32_t caddq(int32_t a); + +#define freeze DILITHIUM_NAMESPACE(freeze) +int32_t freeze(int32_t a); + +#endif diff --git a/src/crypto/pq/_d/ref/rounding.c b/src/crypto/pq/_d/ref/rounding.c new file mode 100644 index 0000000..889f0a2 --- /dev/null +++ b/src/crypto/pq/_d/ref/rounding.c @@ -0,0 +1,102 @@ +#include +#include "params.h" +#include "rounding.h" + +/************************************************* +* Name: power2round +* +* Description: For finite field element a, compute a0, a1 such that +* a mod^+ Q = a1*2^D + a0 with -2^{D-1} < a0 <= 2^{D-1}. +* Assumes a to be standard representative. +* +* Arguments: - int32_t a: input element +* - int32_t *a0: pointer to output element a0 +* +* Returns a1. +**************************************************/ +int32_t power2round(int32_t *a0, int32_t a) { + int32_t a1; + + a1 = (a + (1 << (D-1)) - 1) >> D; + *a0 = a - (a1 << D); + return a1; +} + +/************************************************* +* Name: decompose +* +* Description: For finite field element a, compute high and low bits a0, a1 such +* that a mod^+ Q = a1*ALPHA + a0 with -ALPHA/2 < a0 <= ALPHA/2 except +* if a1 = (Q-1)/ALPHA where we set a1 = 0 and +* -ALPHA/2 <= a0 = a mod^+ Q - Q < 0. Assumes a to be standard +* representative. +* +* Arguments: - int32_t a: input element +* - int32_t *a0: pointer to output element a0 +* +* Returns a1. +**************************************************/ +int32_t decompose(int32_t *a0, int32_t a) { + int32_t a1; + + a1 = (a + 127) >> 7; +#if GAMMA2 == (Q-1)/32 + a1 = (a1*1025 + (1 << 21)) >> 22; + a1 &= 15; +#elif GAMMA2 == (Q-1)/88 + a1 = (a1*11275 + (1 << 23)) >> 24; + a1 ^= ((43 - a1) >> 31) & a1; +#endif + + *a0 = a - a1*2*GAMMA2; + *a0 -= (((Q-1)/2 - *a0) >> 31) & Q; + return a1; +} + +/************************************************* +* Name: make_hint +* +* Description: Compute hint bit indicating whether the low bits of the +* input element overflow into the high bits. +* +* Arguments: - int32_t a0: low bits of input element +* - int32_t a1: high bits of input element +* +* Returns 1 if overflow. +**************************************************/ +unsigned int make_hint(int32_t a0, int32_t a1) { + if(a0 > GAMMA2 || a0 < -GAMMA2 || (a0 == -GAMMA2 && a1 != 0)) + return 1; + + return 0; +} + +/************************************************* +* Name: use_hint +* +* Description: Correct high bits according to hint. +* +* Arguments: - int32_t a: input element +* - unsigned int hint: hint bit +* +* Returns corrected high bits. +**************************************************/ +int32_t use_hint(int32_t a, unsigned int hint) { + int32_t a0, a1; + + a1 = decompose(&a0, a); + if(hint == 0) + return a1; + +#if GAMMA2 == (Q-1)/32 + if(a0 > 0) + return (a1 + 1) & 15; + else + return (a1 - 1) & 15; +#elif GAMMA2 == (Q-1)/88 + if(a0 > 0) + return (a1 == 43) ? 0 : a1 + 1; + else + return (a1 == 0) ? 43 : a1 - 1; +#endif +} diff --git a/src/crypto/pq/_d/ref/rounding.h b/src/crypto/pq/_d/ref/rounding.h new file mode 100644 index 0000000..b72e8e8 --- /dev/null +++ b/src/crypto/pq/_d/ref/rounding.h @@ -0,0 +1,19 @@ +#ifndef ROUNDING_H +#define ROUNDING_H + +#include +#include "params.h" + +#define power2round DILITHIUM_NAMESPACE(power2round) +int32_t power2round(int32_t *a0, int32_t a); + +#define decompose DILITHIUM_NAMESPACE(decompose) +int32_t decompose(int32_t *a0, int32_t a); + +#define make_hint DILITHIUM_NAMESPACE(make_hint) +unsigned int make_hint(int32_t a0, int32_t a1); + +#define use_hint DILITHIUM_NAMESPACE(use_hint) +int32_t use_hint(int32_t a, unsigned int hint); + +#endif diff --git a/src/crypto/pq/_d/ref/sign.c b/src/crypto/pq/_d/ref/sign.c new file mode 100644 index 0000000..7d3f882 --- /dev/null +++ b/src/crypto/pq/_d/ref/sign.c @@ -0,0 +1,443 @@ +#include +#include "params.h" +#include "sign.h" +#include "packing.h" +#include "polyvec.h" +#include "poly.h" +#include "randombytes.h" +#include "symmetric.h" +#include "fips202.h" + +/************************************************* +* Name: crypto_sign_keypair +* +* Description: Generates public and private key. +* +* Arguments: - uint8_t *pk: pointer to output public key (allocated +* array of CRYPTO_PUBLICKEYBYTES bytes) +* - uint8_t *sk: pointer to output private key (allocated +* array of CRYPTO_SECRETKEYBYTES bytes) +* +* Returns 0 (success) +**************************************************/ +int crypto_sign_keypair(uint8_t *pk, uint8_t *sk) { + uint8_t seedbuf[2*SEEDBYTES + CRHBYTES]; + uint8_t tr[TRBYTES]; + const uint8_t *rho, *rhoprime, *key; + polyvecl mat[K]; + polyvecl s1, s1hat; + polyveck s2, t1, t0; + + /* Get randomness for rho, rhoprime and key */ + randombytes(seedbuf, SEEDBYTES); + seedbuf[SEEDBYTES+0] = K; + seedbuf[SEEDBYTES+1] = L; + shake256(seedbuf, 2*SEEDBYTES + CRHBYTES, seedbuf, SEEDBYTES+2); + rho = seedbuf; + rhoprime = rho + SEEDBYTES; + key = rhoprime + CRHBYTES; + + /* Expand matrix */ + polyvec_matrix_expand(mat, rho); + + /* Sample short vectors s1 and s2 */ + polyvecl_uniform_eta(&s1, rhoprime, 0); + polyveck_uniform_eta(&s2, rhoprime, L); + + /* Matrix-vector multiplication */ + s1hat = s1; + polyvecl_ntt(&s1hat); + polyvec_matrix_pointwise_montgomery(&t1, mat, &s1hat); + polyveck_reduce(&t1); + polyveck_invntt_tomont(&t1); + + /* Add error vector s2 */ + polyveck_add(&t1, &t1, &s2); + + /* Extract t1 and write public key */ + polyveck_caddq(&t1); + polyveck_power2round(&t1, &t0, &t1); + pack_pk(pk, rho, &t1); + + /* Compute H(rho, t1) and write secret key */ + shake256(tr, TRBYTES, pk, CRYPTO_PUBLICKEYBYTES); + pack_sk(sk, rho, tr, key, &t0, &s1, &s2); + + return 0; +} + +/************************************************* +* Name: crypto_sign_signature_internal +* +* Description: Computes signature. Internal API. +* +* Arguments: - uint8_t *sig: pointer to output signature (of length CRYPTO_BYTES) +* - size_t *siglen: pointer to output length of signature +* - uint8_t *m: pointer to message to be signed +* - size_t mlen: length of message +* - uint8_t *pre: pointer to prefix string +* - size_t prelen: length of prefix string +* - uint8_t *rnd: pointer to random seed +* - uint8_t *sk: pointer to bit-packed secret key +* +* Returns 0 (success) +**************************************************/ +int crypto_sign_signature_internal(uint8_t *sig, + size_t *siglen, + const uint8_t *m, + size_t mlen, + const uint8_t *pre, + size_t prelen, + const uint8_t rnd[RNDBYTES], + const uint8_t *sk) +{ + unsigned int n; + uint8_t seedbuf[2*SEEDBYTES + TRBYTES + 2*CRHBYTES]; + uint8_t *rho, *tr, *key, *mu, *rhoprime; + uint16_t nonce = 0; + polyvecl mat[K], s1, y, z; + polyveck t0, s2, w1, w0, h; + poly cp; + keccak_state state; + + rho = seedbuf; + tr = rho + SEEDBYTES; + key = tr + TRBYTES; + mu = key + SEEDBYTES; + rhoprime = mu + CRHBYTES; + unpack_sk(rho, tr, key, &t0, &s1, &s2, sk); + + /* Compute mu = CRH(tr, pre, msg) */ + shake256_init(&state); + shake256_absorb(&state, tr, TRBYTES); + shake256_absorb(&state, pre, prelen); + shake256_absorb(&state, m, mlen); + shake256_finalize(&state); + shake256_squeeze(mu, CRHBYTES, &state); + + /* Compute rhoprime = CRH(key, rnd, mu) */ + shake256_init(&state); + shake256_absorb(&state, key, SEEDBYTES); + shake256_absorb(&state, rnd, RNDBYTES); + shake256_absorb(&state, mu, CRHBYTES); + shake256_finalize(&state); + shake256_squeeze(rhoprime, CRHBYTES, &state); + + /* Expand matrix and transform vectors */ + polyvec_matrix_expand(mat, rho); + polyvecl_ntt(&s1); + polyveck_ntt(&s2); + polyveck_ntt(&t0); + +rej: + /* Sample intermediate vector y */ + polyvecl_uniform_gamma1(&y, rhoprime, nonce++); + + /* Matrix-vector multiplication */ + z = y; + polyvecl_ntt(&z); + polyvec_matrix_pointwise_montgomery(&w1, mat, &z); + polyveck_reduce(&w1); + polyveck_invntt_tomont(&w1); + + /* Decompose w and call the random oracle */ + polyveck_caddq(&w1); + polyveck_decompose(&w1, &w0, &w1); + polyveck_pack_w1(sig, &w1); + + shake256_init(&state); + shake256_absorb(&state, mu, CRHBYTES); + shake256_absorb(&state, sig, K*POLYW1_PACKEDBYTES); + shake256_finalize(&state); + shake256_squeeze(sig, CTILDEBYTES, &state); + poly_challenge(&cp, sig); + poly_ntt(&cp); + + /* Compute z, reject if it reveals secret */ + polyvecl_pointwise_poly_montgomery(&z, &cp, &s1); + polyvecl_invntt_tomont(&z); + polyvecl_add(&z, &z, &y); + polyvecl_reduce(&z); + if(polyvecl_chknorm(&z, GAMMA1 - BETA)) + goto rej; + + /* Check that subtracting cs2 does not change high bits of w and low bits + * do not reveal secret information */ + polyveck_pointwise_poly_montgomery(&h, &cp, &s2); + polyveck_invntt_tomont(&h); + polyveck_sub(&w0, &w0, &h); + polyveck_reduce(&w0); + if(polyveck_chknorm(&w0, GAMMA2 - BETA)) + goto rej; + + /* Compute hints for w1 */ + polyveck_pointwise_poly_montgomery(&h, &cp, &t0); + polyveck_invntt_tomont(&h); + polyveck_reduce(&h); + if(polyveck_chknorm(&h, GAMMA2)) + goto rej; + + polyveck_add(&w0, &w0, &h); + n = polyveck_make_hint(&h, &w0, &w1); + if(n > OMEGA) + goto rej; + + /* Write signature */ + pack_sig(sig, sig, &z, &h); + *siglen = CRYPTO_BYTES; + return 0; +} + +/************************************************* +* Name: crypto_sign_signature +* +* Description: Computes signature. +* +* Arguments: - uint8_t *sig: pointer to output signature (of length CRYPTO_BYTES) +* - size_t *siglen: pointer to output length of signature +* - uint8_t *m: pointer to message to be signed +* - size_t mlen: length of message +* - uint8_t *ctx: pointer to contex string +* - size_t ctxlen: length of contex string +* - uint8_t *sk: pointer to bit-packed secret key +* +* Returns 0 (success) or -1 (context string too long) +**************************************************/ +int crypto_sign_signature(uint8_t *sig, + size_t *siglen, + const uint8_t *m, + size_t mlen, + const uint8_t *ctx, + size_t ctxlen, + const uint8_t *sk) +{ + size_t i; + uint8_t pre[257]; + uint8_t rnd[RNDBYTES]; + + if(ctxlen > 255) + return -1; + + /* Prepare pre = (0, ctxlen, ctx) */ + pre[0] = 0; + pre[1] = ctxlen; + for(i = 0; i < ctxlen; i++) + pre[2 + i] = ctx[i]; + +#ifdef DILITHIUM_RANDOMIZED_SIGNING + randombytes(rnd, RNDBYTES); +#else + for(i=0;i 255) + return -1; + + pre[0] = 0; + pre[1] = ctxlen; + for(i = 0; i < ctxlen; i++) + pre[2 + i] = ctx[i]; + + return crypto_sign_verify_internal(sig,siglen,m,mlen,pre,2+ctxlen,pk); +} + +/************************************************* +* Name: crypto_sign_open +* +* Description: Verify signed message. +* +* Arguments: - uint8_t *m: pointer to output message (allocated +* array with smlen bytes), can be equal to sm +* - size_t *mlen: pointer to output length of message +* - const uint8_t *sm: pointer to signed message +* - size_t smlen: length of signed message +* - const uint8_t *ctx: pointer to context tring +* - size_t ctxlen: length of context string +* - const uint8_t *pk: pointer to bit-packed public key +* +* Returns 0 if signed message could be verified correctly and -1 otherwise +**************************************************/ +int crypto_sign_open(uint8_t *m, + size_t *mlen, + const uint8_t *sm, + size_t smlen, + const uint8_t *ctx, + size_t ctxlen, + const uint8_t *pk) +{ + size_t i; + + if(smlen < CRYPTO_BYTES) + goto badsig; + + *mlen = smlen - CRYPTO_BYTES; + if(crypto_sign_verify(sm, CRYPTO_BYTES, sm + CRYPTO_BYTES, *mlen, ctx, ctxlen, pk)) + goto badsig; + else { + /* All good, copy msg, return 0 */ + for(i = 0; i < *mlen; ++i) + m[i] = sm[CRYPTO_BYTES + i]; + return 0; + } + +badsig: + /* Signature verification failed */ + *mlen = 0; + for(i = 0; i < smlen; ++i) + m[i] = 0; + + return -1; +} diff --git a/src/crypto/pq/_d/ref/sign.h b/src/crypto/pq/_d/ref/sign.h new file mode 100644 index 0000000..2741e8f --- /dev/null +++ b/src/crypto/pq/_d/ref/sign.h @@ -0,0 +1,56 @@ +#ifndef SIGN_H +#define SIGN_H + +#include +#include +#include "params.h" +#include "polyvec.h" +#include "poly.h" + +#define crypto_sign_keypair DILITHIUM_NAMESPACE(keypair) +int crypto_sign_keypair(uint8_t *pk, uint8_t *sk); + +#define crypto_sign_signature_internal DILITHIUM_NAMESPACE(signature_internal) +int crypto_sign_signature_internal(uint8_t *sig, + size_t *siglen, + const uint8_t *m, + size_t mlen, + const uint8_t *pre, + size_t prelen, + const uint8_t rnd[RNDBYTES], + const uint8_t *sk); + +#define crypto_sign_signature DILITHIUM_NAMESPACE(signature) +int crypto_sign_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +#define crypto_sign DILITHIUM_NAMESPACETOP +int crypto_sign(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +#define crypto_sign_verify_internal DILITHIUM_NAMESPACE(verify_internal) +int crypto_sign_verify_internal(const uint8_t *sig, + size_t siglen, + const uint8_t *m, + size_t mlen, + const uint8_t *pre, + size_t prelen, + const uint8_t *pk); + +#define crypto_sign_verify DILITHIUM_NAMESPACE(verify) +int crypto_sign_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +#define crypto_sign_open DILITHIUM_NAMESPACE(open) +int crypto_sign_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +#endif diff --git a/src/crypto/pq/_d/ref/symmetric-shake.c b/src/crypto/pq/_d/ref/symmetric-shake.c new file mode 100644 index 0000000..11ec09c --- /dev/null +++ b/src/crypto/pq/_d/ref/symmetric-shake.c @@ -0,0 +1,28 @@ +#include +#include "params.h" +#include "symmetric.h" +#include "fips202.h" + +void dilithium_shake128_stream_init(keccak_state *state, const uint8_t seed[SEEDBYTES], uint16_t nonce) +{ + uint8_t t[2]; + t[0] = nonce; + t[1] = nonce >> 8; + + shake128_init(state); + shake128_absorb(state, seed, SEEDBYTES); + shake128_absorb(state, t, 2); + shake128_finalize(state); +} + +void dilithium_shake256_stream_init(keccak_state *state, const uint8_t seed[CRHBYTES], uint16_t nonce) +{ + uint8_t t[2]; + t[0] = nonce; + t[1] = nonce >> 8; + + shake256_init(state); + shake256_absorb(state, seed, CRHBYTES); + shake256_absorb(state, t, 2); + shake256_finalize(state); +} diff --git a/src/crypto/pq/_d/ref/symmetric.h b/src/crypto/pq/_d/ref/symmetric.h new file mode 100644 index 0000000..cba12d1 --- /dev/null +++ b/src/crypto/pq/_d/ref/symmetric.h @@ -0,0 +1,34 @@ +#ifndef SYMMETRIC_H +#define SYMMETRIC_H + +#include +#include "params.h" + +#include "fips202.h" + +typedef keccak_state stream128_state; +typedef keccak_state stream256_state; + +#define dilithium_shake128_stream_init DILITHIUM_NAMESPACE(dilithium_shake128_stream_init) +void dilithium_shake128_stream_init(keccak_state *state, + const uint8_t seed[SEEDBYTES], + uint16_t nonce); + +#define dilithium_shake256_stream_init DILITHIUM_NAMESPACE(dilithium_shake256_stream_init) +void dilithium_shake256_stream_init(keccak_state *state, + const uint8_t seed[CRHBYTES], + uint16_t nonce); + +#define STREAM128_BLOCKBYTES SHAKE128_RATE +#define STREAM256_BLOCKBYTES SHAKE256_RATE + +#define stream128_init(STATE, SEED, NONCE) \ + dilithium_shake128_stream_init(STATE, SEED, NONCE) +#define stream128_squeezeblocks(OUT, OUTBLOCKS, STATE) \ + shake128_squeezeblocks(OUT, OUTBLOCKS, STATE) +#define stream256_init(STATE, SEED, NONCE) \ + dilithium_shake256_stream_init(STATE, SEED, NONCE) +#define stream256_squeezeblocks(OUT, OUTBLOCKS, STATE) \ + shake256_squeezeblocks(OUT, OUTBLOCKS, STATE) + +#endif diff --git a/src/crypto/pq/_d/ref/test/.gitignore b/src/crypto/pq/_d/ref/test/.gitignore new file mode 100644 index 0000000..99b3f73 --- /dev/null +++ b/src/crypto/pq/_d/ref/test/.gitignore @@ -0,0 +1,10 @@ +test_dilithium2 +test_dilithium3 +test_dilithium5 +test_vectors2 +test_vectors3 +test_vectors5 +test_speed2 +test_speed3 +test_speed5 +test_mul diff --git a/src/crypto/pq/_d/ref/test/cpucycles.c b/src/crypto/pq/_d/ref/test/cpucycles.c new file mode 100644 index 0000000..ccbf54d --- /dev/null +++ b/src/crypto/pq/_d/ref/test/cpucycles.c @@ -0,0 +1,17 @@ +#include +#include "cpucycles.h" + +uint64_t cpucycles_overhead(void) { + uint64_t t0, t1, overhead = -1LL; + unsigned int i; + + for(i=0;i<100000;i++) { + t0 = cpucycles(); + __asm__ volatile(""); + t1 = cpucycles(); + if(t1 - t0 < overhead) + overhead = t1 - t0; + } + + return overhead; +} diff --git a/src/crypto/pq/_d/ref/test/cpucycles.h b/src/crypto/pq/_d/ref/test/cpucycles.h new file mode 100644 index 0000000..7b7b9f7 --- /dev/null +++ b/src/crypto/pq/_d/ref/test/cpucycles.h @@ -0,0 +1,33 @@ +#ifndef CPUCYCLES_H +#define CPUCYCLES_H + +#include + +#ifdef USE_RDPMC /* Needs echo 2 > /sys/devices/cpu/rdpmc */ + +static inline uint64_t cpucycles(void) { + const uint32_t ecx = (1U << 30) + 1; + uint64_t result; + + __asm__ volatile ("rdpmc; shlq $32,%%rdx; orq %%rdx,%%rax" + : "=a" (result) : "c" (ecx) : "rdx"); + + return result; +} + +#else + +static inline uint64_t cpucycles(void) { + uint64_t result; + + __asm__ volatile ("rdtsc; shlq $32,%%rdx; orq %%rdx,%%rax" + : "=a" (result) : : "%rdx"); + + return result; +} + +#endif + +uint64_t cpucycles_overhead(void); + +#endif diff --git a/src/crypto/pq/_d/ref/test/speed_print.c b/src/crypto/pq/_d/ref/test/speed_print.c new file mode 100644 index 0000000..59f147d --- /dev/null +++ b/src/crypto/pq/_d/ref/test/speed_print.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include "cpucycles.h" +#include "speed_print.h" + +static int cmp_uint64(const void *a, const void *b) { + if(*(uint64_t *)a < *(uint64_t *)b) return -1; + if(*(uint64_t *)a > *(uint64_t *)b) return 1; + return 0; +} + +static uint64_t median(uint64_t *l, size_t llen) { + qsort(l,llen,sizeof(uint64_t),cmp_uint64); + + if(llen%2) return l[llen/2]; + else return (l[llen/2-1]+l[llen/2])/2; +} + +static uint64_t average(uint64_t *t, size_t tlen) { + size_t i; + uint64_t acc=0; + + for(i=0;i +#include + +void print_results(const char *s, uint64_t *t, size_t tlen); + +#endif diff --git a/src/crypto/pq/_d/ref/test/test_dilithium.c b/src/crypto/pq/_d/ref/test/test_dilithium.c new file mode 100644 index 0000000..66beb4c --- /dev/null +++ b/src/crypto/pq/_d/ref/test/test_dilithium.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "../randombytes.h" +#include "../sign.h" + +#define MLEN 59 +#define CTXLEN 14 +#define NTESTS 10000 + +int main(void) +{ + size_t i, j; + int ret; + size_t mlen, smlen; + uint8_t b; + uint8_t ctx[CTXLEN] = {0}; + uint8_t m[MLEN + CRYPTO_BYTES]; + uint8_t m2[MLEN + CRYPTO_BYTES]; + uint8_t sm[MLEN + CRYPTO_BYTES]; + uint8_t pk[CRYPTO_PUBLICKEYBYTES]; + uint8_t sk[CRYPTO_SECRETKEYBYTES]; + + snprintf((char*)ctx,CTXLEN,"test_dilitium"); + + for(i = 0; i < NTESTS; ++i) { + randombytes(m, MLEN); + + crypto_sign_keypair(pk, sk); + crypto_sign(sm, &smlen, m, MLEN, ctx, CTXLEN, sk); + ret = crypto_sign_open(m2, &mlen, sm, smlen, ctx, CTXLEN, pk); + + if(ret) { + fprintf(stderr, "Verification failed\n"); + return -1; + } + if(smlen != MLEN + CRYPTO_BYTES) { + fprintf(stderr, "Signed message lengths wrong\n"); + return -1; + } + if(mlen != MLEN) { + fprintf(stderr, "Message lengths wrong\n"); + return -1; + } + for(j = 0; j < MLEN; ++j) { + if(m2[j] != m[j]) { + fprintf(stderr, "Messages don't match\n"); + return -1; + } + } + + randombytes((uint8_t *)&j, sizeof(j)); + do { + randombytes(&b, 1); + } while(!b); + sm[j % (MLEN + CRYPTO_BYTES)] += b; + ret = crypto_sign_open(m2, &mlen, sm, smlen, ctx, CTXLEN, pk); + if(!ret) { + fprintf(stderr, "Trivial forgeries possible\n"); + return -1; + } + } + + printf("CRYPTO_PUBLICKEYBYTES = %d\n", CRYPTO_PUBLICKEYBYTES); + printf("CRYPTO_SECRETKEYBYTES = %d\n", CRYPTO_SECRETKEYBYTES); + printf("CRYPTO_BYTES = %d\n", CRYPTO_BYTES); + + return 0; +} diff --git a/src/crypto/pq/_d/ref/test/test_mul.c b/src/crypto/pq/_d/ref/test/test_mul.c new file mode 100644 index 0000000..4633c11 --- /dev/null +++ b/src/crypto/pq/_d/ref/test/test_mul.c @@ -0,0 +1,60 @@ +#include +#include +#include "../params.h" +#include "../randombytes.h" +#include "../poly.h" + +#define NTESTS 100000 + +static void poly_naivemul(poly *c, const poly *a, const poly *b) { + unsigned int i,j; + int32_t r[2*N] = {0}; + + for(i = 0; i < N; i++) + for(j = 0; j < N; j++) + r[i+j] = (r[i+j] + (int64_t)a->coeffs[i]*b->coeffs[j]) % Q; + + for(i = N; i < 2*N; i++) + r[i-N] = (r[i-N] - r[i]) % Q; + + for(i = 0; i < N; i++) + c->coeffs[i] = r[i]; +} + +int main(void) { + unsigned int i, j; + uint8_t seed[SEEDBYTES]; + uint16_t nonce = 0; + poly a, b, c, d; + + randombytes(seed, sizeof(seed)); + for(i = 0; i < NTESTS; ++i) { + poly_uniform(&a, seed, nonce++); + poly_uniform(&b, seed, nonce++); + + c = a; + poly_ntt(&c); + for(j = 0; j < N; ++j) + c.coeffs[j] = (int64_t)c.coeffs[j]*-114592 % Q; + poly_invntt_tomont(&c); + for(j = 0; j < N; ++j) { + if((c.coeffs[j] - a.coeffs[j]) % Q) + fprintf(stderr, "ERROR in ntt/invntt: c[%d] = %d != %d\n", + j, c.coeffs[j]%Q, a.coeffs[j]); + } + + poly_naivemul(&c, &a, &b); + poly_ntt(&a); + poly_ntt(&b); + poly_pointwise_montgomery(&d, &a, &b); + poly_invntt_tomont(&d); + + for(j = 0; j < N; ++j) { + if((d.coeffs[j] - c.coeffs[j]) % Q) + fprintf(stderr, "ERROR in multiplication: d[%d] = %d != %d\n", + j, d.coeffs[j], c.coeffs[j]); + } + } + + return 0; +} diff --git a/src/crypto/pq/_d/ref/test/test_speed.c b/src/crypto/pq/_d/ref/test/test_speed.c new file mode 100644 index 0000000..688d930 --- /dev/null +++ b/src/crypto/pq/_d/ref/test/test_speed.c @@ -0,0 +1,87 @@ +#include +#include "../sign.h" +#include "../poly.h" +#include "../polyvec.h" +#include "../params.h" +#include "cpucycles.h" +#include "speed_print.h" + +#define NTESTS 1000 + +uint64_t t[NTESTS]; + +int main(void) +{ + unsigned int i; + size_t siglen; + uint8_t pk[CRYPTO_PUBLICKEYBYTES]; + uint8_t sk[CRYPTO_SECRETKEYBYTES]; + uint8_t sig[CRYPTO_BYTES]; + uint8_t seed[CRHBYTES]; + polyvecl mat[K]; + poly *a = &mat[0].vec[0]; + poly *b = &mat[0].vec[1]; + poly *c = &mat[0].vec[2]; + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + polyvec_matrix_expand(mat, seed); + } + print_results("polyvec_matrix_expand:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + poly_uniform_eta(a, seed, 0); + } + print_results("poly_uniform_eta:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + poly_uniform_gamma1(a, seed, 0); + } + print_results("poly_uniform_gamma1:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + poly_ntt(a); + } + print_results("poly_ntt:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + poly_invntt_tomont(a); + } + print_results("poly_invntt_tomont:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + poly_pointwise_montgomery(c, a, b); + } + print_results("poly_pointwise_montgomery:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + poly_challenge(c, seed); + } + print_results("poly_challenge:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + crypto_sign_keypair(pk, sk); + } + print_results("Keypair:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + crypto_sign_signature(sig, &siglen, sig, CRHBYTES, NULL, 0, sk); + } + print_results("Sign:", t, NTESTS); + + for(i = 0; i < NTESTS; ++i) { + t[i] = cpucycles(); + crypto_sign_verify(sig, CRYPTO_BYTES, sig, CRHBYTES, NULL, 0, pk); + } + print_results("Verify:", t, NTESTS); + + return 0; +} diff --git a/src/crypto/pq/_d/ref/test/test_vectors.c b/src/crypto/pq/_d/ref/test/test_vectors.c new file mode 100644 index 0000000..2829286 --- /dev/null +++ b/src/crypto/pq/_d/ref/test/test_vectors.c @@ -0,0 +1,251 @@ +#include +#include +#include +#include +#include "../randombytes.h" +#include "../fips202.h" +#include "../params.h" +#include "../sign.h" +#include "../poly.h" +#include "../polyvec.h" +#include "../packing.h" + +#define MLEN 32 +#define CTXLEN 13 +#define NVECTORS 10000 + +/* Initital state after absorbing empty string + * Permute before squeeze is achieved by setting pos to SHAKE128_RATE */ +static keccak_state rngstate = {{0x1F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (1ULL << 63), 0, 0, 0, 0}, SHAKE128_RATE}; + +void randombytes(uint8_t *x,size_t xlen) { + shake128_squeeze(x, xlen, &rngstate); +} + +int main(void) { + unsigned int i, j, k, l; + uint8_t pk[CRYPTO_PUBLICKEYBYTES]; + uint8_t sk[CRYPTO_SECRETKEYBYTES]; + uint8_t sig[CRYPTO_BYTES]; + uint8_t m[MLEN]; + uint8_t ctx[CTXLEN] = {0}; + uint8_t seed[CRHBYTES]; + uint8_t buf[CRYPTO_SECRETKEYBYTES]; + size_t siglen; + poly c, tmp; + polyvecl s, y, mat[K]; + polyveck w, w1, w0, t1, t0, h; + + snprintf((char*)ctx,CTXLEN,"test_vectors"); + + for(i = 0; i < NVECTORS; ++i) { + printf("count = %u\n", i); + + randombytes(m, MLEN); + printf("m = "); + for(j = 0; j < MLEN; ++j) + printf("%02x", m[j]); + printf("\n"); + + crypto_sign_keypair(pk, sk); + shake256(buf, 32, pk, CRYPTO_PUBLICKEYBYTES); + printf("pk = "); + for(j = 0; j < 32; ++j) + printf("%02x", buf[j]); + printf("\n"); + shake256(buf, 32, sk, CRYPTO_SECRETKEYBYTES); + printf("sk = "); + for(j = 0; j < 32; ++j) + printf("%02x", buf[j]); + printf("\n"); + + crypto_sign_signature(sig, &siglen, m, MLEN, ctx, CTXLEN, sk); + shake256(buf, 32, sig, CRYPTO_BYTES); + printf("sig = "); + for(j = 0; j < 32; ++j) + printf("%02x", buf[j]); + printf("\n"); + + if(crypto_sign_verify(sig, siglen, m, MLEN, ctx, CTXLEN, pk)) + fprintf(stderr,"Signature verification failed!\n"); + + randombytes(seed, sizeof(seed)); + printf("seed = "); + for(j = 0; j < sizeof(seed); ++j) + printf("%02X", seed[j]); + printf("\n"); + + polyvec_matrix_expand(mat, seed); + printf("A = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < L; ++k) { + for(l = 0; l < N; ++l) { + printf("%8d", mat[j].vec[k].coeffs[l]); + if(l < N-1) printf(", "); + else if(k < L-1) printf("], ["); + else if(j < K-1) printf("];\n ["); + else printf("])\n"); + } + } + } + + polyvecl_uniform_eta(&s, seed, 0); + + polyeta_pack(buf, &s.vec[0]); + polyeta_unpack(&tmp, buf); + for(j = 0; j < N; ++j) + if(tmp.coeffs[j] != s.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in polyeta_(un)pack!\n"); + + if(polyvecl_chknorm(&s, ETA+1)) + fprintf(stderr, "ERROR in polyvecl_chknorm(&s ,ETA+1)!\n"); + + printf("s = (["); + for(j = 0; j < L; ++j) { + for(k = 0; k < N; ++k) { + printf("%3d", s.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < L-1) printf("],\n ["); + else printf("])\n"); + } + } + + polyvecl_uniform_gamma1(&y, seed, 0); + + polyz_pack(buf, &y.vec[0]); + polyz_unpack(&tmp, buf); + for(j = 0; j < N; ++j) + if(tmp.coeffs[j] != y.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in polyz_(un)pack!\n"); + + if(polyvecl_chknorm(&y, GAMMA1+1)) + fprintf(stderr, "ERROR in polyvecl_chknorm(&y, GAMMA1)!\n"); + + printf("y = (["); + for(j = 0; j < L; ++j) { + for(k = 0; k < N; ++k) { + printf("%8d", y.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < L-1) printf("],\n ["); + else printf("])\n"); + } + } + + polyvecl_ntt(&y); + polyvec_matrix_pointwise_montgomery(&w, mat, &y); + polyveck_reduce(&w); + polyveck_invntt_tomont(&w); + polyveck_caddq(&w); + polyveck_decompose(&w1, &w0, &w); + + for(j = 0; j < N; ++j) { + tmp.coeffs[j] = w1.vec[0].coeffs[j]*2*GAMMA2 + w0.vec[0].coeffs[j]; + if(tmp.coeffs[j] < 0) tmp.coeffs[j] += Q; + if(tmp.coeffs[j] != w.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in poly_decompose!\n"); + } + + polyw1_pack(buf, &w1.vec[0]); +#if GAMMA2 == (Q-1)/32 + for(j = 0; j < N/2; ++j) { + tmp.coeffs[2*j+0] = buf[j] & 0xF; + tmp.coeffs[2*j+1] = buf[j] >> 4; + if(tmp.coeffs[2*j+0] != w1.vec[0].coeffs[2*j+0] + || tmp.coeffs[2*j+1] != w1.vec[0].coeffs[2*j+1]) + fprintf(stderr, "ERROR in polyw1_pack!\n"); + } +#endif + +#if GAMMA2 == (Q-1)/32 + if(polyveck_chknorm(&w1, 16)) + fprintf(stderr, "ERROR in polyveck_chknorm(&w1, 16)!\n"); +#elif GAMMA2 == (Q-1)/88 + if(polyveck_chknorm(&w1, 44)) + fprintf(stderr, "ERROR in polyveck_chknorm(&w1, 44)!\n"); +#endif + if(polyveck_chknorm(&w0, GAMMA2 + 1)) + fprintf(stderr, "ERROR in polyveck_chknorm(&w0, GAMMA2+1)!\n"); + + printf("w1 = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < N; ++k) { + printf("%2d", w1.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < K-1) printf("],\n ["); + else printf("])\n"); + } + } + printf("w0 = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < N; ++k) { + printf("%8d", w0.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < K-1) printf("],\n ["); + else printf("])\n"); + } + } + + polyveck_power2round(&t1, &t0, &w); + + for(j = 0; j < N; ++j) { + tmp.coeffs[j] = (t1.vec[0].coeffs[j] << D) + t0.vec[0].coeffs[j]; + if(tmp.coeffs[j] != w.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in poly_power2round!\n"); + } + + polyt1_pack(buf, &t1.vec[0]); + polyt1_unpack(&tmp, buf); + for(j = 0; j < N; ++j) { + if(tmp.coeffs[j] != t1.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in polyt1_(un)pack!\n"); + } + polyt0_pack(buf, &t0.vec[0]); + polyt0_unpack(&tmp, buf); + for(j = 0; j < N; ++j) { + if(tmp.coeffs[j] != t0.vec[0].coeffs[j]) + fprintf(stderr, "ERROR in polyt0_(un)pack!\n"); + } + + if(polyveck_chknorm(&t1, 1024)) + fprintf(stderr, "ERROR in polyveck_chknorm(&t1, 1024)!\n"); + if(polyveck_chknorm(&t0, (1U << (D-1)) + 1)) + fprintf(stderr, "ERROR in polyveck_chknorm(&t0, (1 << (D-1)) + 1)!\n"); + + printf("t1 = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < N; ++k) { + printf("%3d", t1.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < K-1) printf("],\n ["); + else printf("])\n"); + } + } + printf("t0 = (["); + for(j = 0; j < K; ++j) { + for(k = 0; k < N; ++k) { + printf("%5d", t0.vec[j].coeffs[k]); + if(k < N-1) printf(", "); + else if(j < K-1) printf("],\n ["); + else printf("])\n"); + } + } + + poly_challenge(&c, seed); + printf("c = ["); + for(j = 0; j < N; ++j) { + printf("%2d", c.coeffs[j]); + if(j < N-1) printf(", "); + else printf("]\n"); + } + + polyveck_make_hint(&h, &w0, &w1); + pack_sig(buf, seed, &y, &h); + unpack_sig(seed, &y, &w, buf); + if(memcmp(&h,&w,sizeof(h))) + fprintf(stderr, "ERROR in (un)pack_sig!\n"); + + printf("\n"); + } + + return 0; +} diff --git a/src/crypto/pq/_d/runlcov.sh b/src/crypto/pq/_d/runlcov.sh new file mode 100755 index 0000000..a94bb8b --- /dev/null +++ b/src/crypto/pq/_d/runlcov.sh @@ -0,0 +1,21 @@ +#!/bin/sh -e + +cd ref + +for alg in 2 3 5; do + make -B test/test_dilithium$alg CFLAGS="-O0 -g --coverage" + ./test/test_dilithium$alg + lcov -c -d . -o dilithium$alg.lcov + lcov -z -d . + rm test/test_dilithium$alg +done + +lcov -o dilithium.lcov \ + -a dilithium2.lcov \ + -a dilithium3.lcov \ + -a dilithium5.lcov \ + +lcov -r dilithium.lcov -o dilithium.lcov \ + '*/test/test_dilithium.c' + +exit 0 diff --git a/src/crypto/pq/_d/runtests.sh b/src/crypto/pq/_d/runtests.sh new file mode 100755 index 0000000..f0b0bc5 --- /dev/null +++ b/src/crypto/pq/_d/runtests.sh @@ -0,0 +1,33 @@ +#!/bin/sh -e +nproc="${nproc:-2}" + +ARCH="${ARCH:-amd64}" +ARCH="${TRAVIS_CPU_ARCH:-$ARCH}" + +if [ "$ARCH" = "amd64" -a "$TRAVIS_OS_NAME" != "osx" ]; then + DIRS="ref avx2" +else + DIRS="ref" +fi + +if [ "$ARCH" = "amd64" -o "$ARCH" = "arm64" ]; then + export CC=/usr/bin/clang + export CFLAGS="-fsanitize=undefined,address ${CFLAGS}" +fi + +for dir in $DIRS; do + make -j$(nproc) -C $dir clean + make -j$(nproc) -C $dir + for alg in 2 3 5; do + #valgrind --vex-guest-max-insns=25 ./$dir/test/test_dilithium$alg + ./$dir/test/test_dilithium$alg & + PID1=$! + echo testvec$alg + ./$dir/test/test_vectors$alg > tvecs$alg & + PID2=$! + wait $PID1 $PID2 + done + shasum -a256 -c SHA256SUMS +done + +exit 0 diff --git a/src/crypto/pq/_s/.github/workflows/test-haraka-aesni.yml b/src/crypto/pq/_s/.github/workflows/test-haraka-aesni.yml new file mode 100644 index 0000000..4f0e713 --- /dev/null +++ b/src/crypto/pq/_s/.github/workflows/test-haraka-aesni.yml @@ -0,0 +1,33 @@ +name: Tests for haraka-aesni implementation + +on: + - push + - pull_request + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + size: + - 128 + - 192 + - 256 + option: + - s + - f + thash: + - simple + - robust + steps: + - uses: actions/checkout@v1 + - name: Run make + run: | + make -C haraka-aesni THASH=${{ matrix.thash }} clean + make -C haraka-aesni THASH=${{ matrix.thash }} PARAMS=sphincs-haraka-${{ matrix.size }}${{ matrix.option }} tests + make -C haraka-aesni THASH=${{ matrix.thash }} PARAMS=sphincs-haraka-${{ matrix.size }}${{ matrix.option }} test + make -C haraka-aesni THASH=${{ matrix.thash }} PARAMS=sphincs-haraka-${{ matrix.size }}${{ matrix.option }} PQCgenKAT_sign + - name: Run PQCgenKAT_sign + run: python3 vectors.py sphincs-haraka-${{ matrix.size }}${{ matrix.option }}-${{ matrix.thash }} haraka-aesni + +# vim: set ft=yaml ts=2 sw=2 et : diff --git a/src/crypto/pq/_s/.github/workflows/test-ref.yml b/src/crypto/pq/_s/.github/workflows/test-ref.yml new file mode 100644 index 0000000..b97ded2 --- /dev/null +++ b/src/crypto/pq/_s/.github/workflows/test-ref.yml @@ -0,0 +1,37 @@ +name: Tests for ref implementation + +on: + - push + - pull_request + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + hash: + - sha2 + - shake + - haraka + size: + - 128 + - 192 + - 256 + option: + - s + - f + thash: + - simple + - robust + steps: + - uses: actions/checkout@v1 + - name: Run make + run: | + make -C ref HASH=${{ matrix.hash }} THASH=${{ matrix.thash }} clean + make -C ref HASH=${{ matrix.hash }} THASH=${{ matrix.thash }} PARAMS=sphincs-${{ matrix.hash }}-${{ matrix.size }}${{ matrix.option }} tests + make -C ref HASH=${{ matrix.hash }} THASH=${{ matrix.thash }} PARAMS=sphincs-${{ matrix.hash }}-${{ matrix.size }}${{ matrix.option }} test + make -C ref THASH=${{ matrix.thash }} PQCgenKAT_sign + - name: Run PQCgenKAT_sign + run: python3 vectors.py sphincs-${{ matrix.hash }}-${{ matrix.size }}${{ matrix.option }}-${{ matrix.thash }} ref + +# vim: set ft=yaml ts=2 sw=2 et : diff --git a/src/crypto/pq/_s/.github/workflows/test-sha2-avx2.yml b/src/crypto/pq/_s/.github/workflows/test-sha2-avx2.yml new file mode 100644 index 0000000..d0dd58e --- /dev/null +++ b/src/crypto/pq/_s/.github/workflows/test-sha2-avx2.yml @@ -0,0 +1,33 @@ +name: Tests for sha2-avx2 implementation + +on: + - push + - pull_request + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + size: + - 128 + - 192 + - 256 + option: + - s + - f + thash: + - simple + - robust + steps: + - uses: actions/checkout@v1 + - name: Run make + run: | + make -C sha2-avx2 THASH=${{ matrix.thash }} clean + make -C sha2-avx2 THASH=${{ matrix.thash }} PARAMS=sphincs-sha2-${{ matrix.size }}${{ matrix.option }} tests + make -C sha2-avx2 THASH=${{ matrix.thash }} PARAMS=sphincs-sha2-${{ matrix.size }}${{ matrix.option }} test + make -C sha2-avx2 THASH=${{ matrix.thash }} PARAMS=sphincs-sha2-${{ matrix.size }}${{ matrix.option }} PQCgenKAT_sign + - name: Run PQCgenKAT_sign + run: python3 vectors.py sphincs-sha2-${{ matrix.size }}${{ matrix.option }}-${{ matrix.thash }} sha2-avx2 + +# vim: set ft=yaml ts=2 sw=2 et : diff --git a/src/crypto/pq/_s/.github/workflows/test-shake-avx2.yml b/src/crypto/pq/_s/.github/workflows/test-shake-avx2.yml new file mode 100644 index 0000000..4e6ddea --- /dev/null +++ b/src/crypto/pq/_s/.github/workflows/test-shake-avx2.yml @@ -0,0 +1,33 @@ +name: Tests for shake-avx2 implementation + +on: + - push + - pull_request + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + size: + - 128 + - 192 + - 256 + option: + - s + - f + thash: + - simple + - robust + steps: + - uses: actions/checkout@v1 + - name: Run make + run: | + make -C shake-avx2 THASH=${{ matrix.thash }} clean + make -C shake-avx2 THASH=${{ matrix.thash }} PARAMS=sphincs-shake-${{ matrix.size }}${{ matrix.option }} tests + make -C shake-avx2 THASH=${{ matrix.thash }} PARAMS=sphincs-shake-${{ matrix.size }}${{ matrix.option }} test + make -C shake-avx2 THASH=${{ matrix.thash }} PARAMS=sphincs-shake-${{ matrix.size }}${{ matrix.option }} PQCgenKAT_sign + - name: Run PQCgenKAT_sign + run: python3 vectors.py sphincs-shake-${{ matrix.size }}${{ matrix.option }}-${{ matrix.thash }} shake-avx2 + +# vim: set ft=yaml ts=2 sw=2 et : diff --git a/src/crypto/pq/_s/.reuse/dep5 b/src/crypto/pq/_s/.reuse/dep5 new file mode 100644 index 0000000..8d2def9 --- /dev/null +++ b/src/crypto/pq/_s/.reuse/dep5 @@ -0,0 +1,15 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: SPHINCS+ +Upstream-Contact: contact@sphincs.org +Souce: https://github.com/sphincs/sphincsplus + +Files: * +License: LicenseRef-SPHINCS-PLUS-Public-Domain OR CC0-1.0 OR 0BSD OR MIT-0 + +Files: ref/haraka.c +Copyright: 2016 Thomas Pornin and SPHINCS+ team +License: (LicenseRef-SPHINCS-PLUS-Public-Domain OR CC0-1.0 OR 0BSD OR MIT-0) AND MIT + +Files: ref/PQCgenKAT_sign.c ref/rng.c ref/rng.h +Copyright: 2017 Bassham, Lawrence E (Fed). +License: All rights reserved. diff --git a/src/crypto/pq/_s/LICENSE b/src/crypto/pq/_s/LICENSE new file mode 100644 index 0000000..ca88ec4 --- /dev/null +++ b/src/crypto/pq/_s/LICENSE @@ -0,0 +1,2 @@ +SPDX-License-Identifier: (LicenseRef-SPHINCS-PLUS-Public-Domain OR CC0-1.0 OR 0BSD OR MIT-0) AND MIT + diff --git a/src/crypto/pq/_s/LICENSES/0BSD.txt b/src/crypto/pq/_s/LICENSES/0BSD.txt new file mode 100644 index 0000000..c19ef1c --- /dev/null +++ b/src/crypto/pq/_s/LICENSES/0BSD.txt @@ -0,0 +1,12 @@ +BSD Zero Clause License + +Permission to use, copy, modify, and/or distribute this software for +any purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE +FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY +DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN +AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/src/crypto/pq/_s/LICENSES/CC0-1.0.txt b/src/crypto/pq/_s/LICENSES/CC0-1.0.txt new file mode 100644 index 0000000..6ca207e --- /dev/null +++ b/src/crypto/pq/_s/LICENSES/CC0-1.0.txt @@ -0,0 +1,122 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. + diff --git a/src/crypto/pq/_s/LICENSES/LicenseRef-SPHINCS-PLUS-Public-Domain.txt b/src/crypto/pq/_s/LICENSES/LicenseRef-SPHINCS-PLUS-Public-Domain.txt new file mode 100644 index 0000000..8c93d5a --- /dev/null +++ b/src/crypto/pq/_s/LICENSES/LicenseRef-SPHINCS-PLUS-Public-Domain.txt @@ -0,0 +1,2 @@ +This work is hereby placed into the public domain. + diff --git a/src/crypto/pq/_s/LICENSES/MIT-0.txt b/src/crypto/pq/_s/LICENSES/MIT-0.txt new file mode 100644 index 0000000..11f9808 --- /dev/null +++ b/src/crypto/pq/_s/LICENSES/MIT-0.txt @@ -0,0 +1,15 @@ +MIT No Attribution + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/src/crypto/pq/_s/LICENSES/MIT.txt b/src/crypto/pq/_s/LICENSES/MIT.txt new file mode 100644 index 0000000..66cb02b --- /dev/null +++ b/src/crypto/pq/_s/LICENSES/MIT.txt @@ -0,0 +1,8 @@ +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/src/crypto/pq/_s/README.md b/src/crypto/pq/_s/README.md new file mode 100644 index 0000000..5948b7d --- /dev/null +++ b/src/crypto/pq/_s/README.md @@ -0,0 +1,37 @@ +## SPHINCS+ + +This repository contains the software that accompanies the [SPHINCS+ submission](https://sphincs.org/) to [NIST's Post-Quantum Cryptography](https://csrc.nist.gov/Projects/Post-Quantum-Cryptography) project. + +![][test-ref] +![][test-sha256-avx2] +![][test-shake256-avx2] +![][test-haraka-aesni] + +### Parameters + +The [SPHINCS+ specification](https://sphincs.org/data/sphincs+-specification.pdf) proposed a set of 36 named instances, specifying hash functions and concrete parameters for the security level, tree dimensions, WOTS+ and FORS. This reference implementation allows for more flexibility, as parameters can be specified in a `params.h` file. The proposed parameter sets have been predefined in `ref/params/params-*.h`, and the hash function can be varied by linking with the different implementations of `hash.h`, i.e., `hash_haraka.c`, `hash_sha2.c` and `hash_shake.c`, as well as different implementations of `thash.h`, i.e., `*_robust.c` and `*_simple.c`. This is demonstrated in the `Makefile`. See the table below for a summary of the parameter sets. These parameters target the NIST security categories 1, 3 and 5; for each category, there is a parameter set geared towards either small signatures or fast signature generation. + +| | n | h | d | log(t) | k | w | bit security | pk bytes | sk bytes | sig bytes | +| :------------ | -: | -: | -: | -----: | -: | --: | -----------: | -------: | -------: | --------: | +| SPHINCS+-128s | 16 | 63 | 7 | 12 | 14 | 16 | 133 | 32 | 64 | 7,856 | +| SPHINCS+-128f | 16 | 66 | 22 | 6 | 33 | 16 | 128 | 32 | 64 | 17,088 | +| SPHINCS+-192s | 24 | 63 | 7 | 14 | 17 | 16 | 193 | 48 | 96 | 16,224 | +| SPHINCS+-192f | 24 | 66 | 22 | 8 | 33 | 16 | 194 | 48 | 96 | 35,664 | +| SPHINCS+-256s | 32 | 64 | 8 | 14 | 22 | 16 | 255 | 64 | 128 | 29,792 | +| SPHINCS+-256f | 32 | 68 | 17 | 9 | 35 | 16 | 255 | 64 | 128 | 49,856 | + +### License + +All included code has been placed into +[Public Domain](LICENSES/LicenseRef-SPHINCS-PLUS-Public-Domain.txt) +and is available under various open source licenses +([Creative Commons Zero v1.0 Universal (CC0-1.0)](LICENSES/CC0-1.0.txt), +[BSD Zero Clause License (0BSD)](LICENSES/0BSD.txt), and +[MIT No Attribution (MIT-0)](LICENSES/MIT-0.txt), +see the [LICENSE file](LICENSE) and the licenses in the [LICENSES folder](LICENSES)), with the exception of `rng.c`, `rng.h` and `PQCgenKAT_sign.c`, which were provided by NIST, and parts of `ref/haraka.c`, which are under +[MIT license (MIT)](LICENSES/MIT.txt). + +[test-ref]: https://github.com/sphincs/sphincsplus/actions/workflows/test-ref.yml/badge.svg +[test-sha256-avx2]: https://github.com/sphincs/sphincsplus/actions/workflows/test-sha256-avx2.yml/badge.svg +[test-shake256-avx2]: https://github.com/sphincs/sphincsplus/actions/workflows/test-shake256-avx2.yml/badge.svg +[test-haraka-aesni]: https://github.com/sphincs/sphincsplus/actions/workflows/test-haraka-aesni.yml/badge.svg diff --git a/src/crypto/pq/_s/SHA256SUMS b/src/crypto/pq/_s/SHA256SUMS new file mode 100644 index 0000000..e781c99 --- /dev/null +++ b/src/crypto/pq/_s/SHA256SUMS @@ -0,0 +1,36 @@ +9e1b3168520c917b6de676caa7a5799ec972e55caa150090e8452c80c299545e sphincs-haraka-128f-robust +c6a28dcf0667bd91c7bb46814ac7408c0375727fe5fec7d41332149006d3f9d7 sphincs-haraka-128f-simple +f93f4a554322080545a70f85ce936a12acc2fe928a243e3d13546ffe87872a9e sphincs-haraka-128s-robust +3c9b181d3d96c066039b77e9accd926745fe1ecb010039d3579140b877da6f33 sphincs-haraka-128s-simple +8876bfae8924983db27acfeaee6252d37cea86f05fcc4b16ea2c902d717e6a6e sphincs-haraka-192f-robust +df26bd02796f5ad9d6ff412793960e79ec911cbf4521656814895e6ef5a1db83 sphincs-haraka-192f-simple +6cfde6cb5f9ce93eb3f7b0845e1149f661f92000f54e9d340c0bff504920ec7e sphincs-haraka-192s-robust +64037177e1524f2b2d3ea4a79fdaf9352eb39a3aa6e68bc9d3316b7c2b835820 sphincs-haraka-192s-simple +a838509fa6ec49ade2638efc35d9e29fdb56bd9b843d5e1f48210a2cab218332 sphincs-haraka-256f-robust +e1e3258be6b4467bcea81392363f657a58278a5b99fe240f29e388b0fe72f5da sphincs-haraka-256f-simple +b5c5cc535f03789c25c018c009615ac62ba5b64188e4db5e3ede5513e3704dcc sphincs-haraka-256s-robust +9428a566a2c2ee03665fc0eb2dbf208deb1b28716dc8c2d5e7c036a9f83d31da sphincs-haraka-256s-simple +b6c82007bbce794f9fd67de708cd4d959319c744b918ddb28795fd491b713aa9 sphincs-sha2-128f-robust +708f6ab77f8026361e975f7be7b9b5d1cd8aca56e4a3604c85ef3f9fe6618549 sphincs-sha2-128f-simple +f4c2f31082fc8ad15419edc4f24c34a83d909f75eb37ea5ffe53df0fb5ef5306 sphincs-sha2-128s-robust +65942fac8e225fde77dd277d297e68c94c2e25a2a4089f88be4b56fa92b18a84 sphincs-sha2-128s-simple +b8e617db2099e617dfc372ff732eead88872aea791e2fe82628568d75dd03c78 sphincs-sha2-192f-robust +84b1a342683bcad658efb6c65f7367c6b30623e74e3a24c2238d19eaf74722ab sphincs-sha2-192f-simple +50c4b94dc788446077b48af1d8fa0170dc2114b4cb72a19f1d8c7628f9dadfd6 sphincs-sha2-192s-robust +13efa67b9297afa051b9b30e2686266350c8b4000caa49aa432516e2a86d0b68 sphincs-sha2-192s-simple +dc3330f8f19c816f45ee9a1127bf2b8a8c900e05df9a964bb760f0adf8f9b1b3 sphincs-sha2-256f-robust +46e286dc1a20012789c1bf4793a8eb2043dd0c11df729fa36d9f96b0aeffdac6 sphincs-sha2-256f-simple +1f42b407e1e351861ba23e520b1974f399e349fcb66c614d727a38fb4e646634 sphincs-sha2-256s-robust +c816ca365a667e4d6564a95ac576bc9d7be0de7e66eff93e6f05dd4f134a183f sphincs-sha2-256s-simple +4be71430814589ce7c861030c7cdce0aa73f75885b693b41fdb7c34d8f32fa79 sphincs-shake-128f-robust +5167df2ce46f33b76ccf0688f7769217d91878bd7d9b431080a3032eba51da10 sphincs-shake-128f-simple +fbe6c99d6ccc42fc9af5babbac532f28288d4164b182515dffeb1cd47f351d12 sphincs-shake-128s-robust +e7d5caee1941be99b6dfe46a95fc4535a34792f429e61d1cdc7fd3bbafe9ff02 sphincs-shake-128s-simple +243d0e25de08fea547b0beae5f778a48bd55e56066435f9cdb9afc60a722699e sphincs-shake-192f-robust +f204fd1cd5dce187441d104ae7159b64322b6a4afae708d48dc9966fe418ec4e sphincs-shake-192f-simple +cb13eaa2b1c074f53c87f1025e6bb1b356ad8de3bea9388b90a058a6460766bb sphincs-shake-192s-robust +4cc01c4a562d738ac54f5abfead35ecc4f46a1e2531fa12b4bc2819f4560c351 sphincs-shake-192s-simple +5a736aeba47f8d84e3ca47126715affcb4ce6cef13e3c9f6af220827973aa383 sphincs-shake-256f-robust +127f7ab83c740344546fe30777b221e8cb39f30fc4242d07d7608dc31a9835d4 sphincs-shake-256f-simple +4d2ca7d10f2206c3cb9a26c6b00a0361601a1fe2dddf102fbfd6d3dac0be10fe sphincs-shake-256s-robust +4ce4552e2e9b009a9016eb6dbcbefae3da2de151d61e2f392d4b9517eaeab91d sphincs-shake-256s-simple diff --git a/src/crypto/pq/_s/benchmark.py b/src/crypto/pq/_s/benchmark.py new file mode 100755 index 0000000..4ae6003 --- /dev/null +++ b/src/crypto/pq/_s/benchmark.py @@ -0,0 +1,39 @@ +#! /usr/bin/env python3 +import fileinput +import itertools +import os +import sys +from subprocess import DEVNULL, run + +implementations = [ + ('ref', ['shake', 'sha2', 'haraka']), + ('haraka-aesni', ['haraka']), + ('shake-avx2', ['shake']), + ('sha2-avx2', ['sha2']), + ] + +options = ["f", "s"] +sizes = [128, 192, 256] +thashes = ['robust', 'simple'] + +for impl, fns in implementations: + params = os.path.join(impl, "params.h") + for fn in fns: + for opt, size, thash in itertools.product(options, sizes, thashes): + paramset = "sphincs-{}-{}{}".format(fn, size, opt) + paramfile = "params-{}.h".format(paramset) + + print("Benchmarking", paramset, thash, "using", impl, flush=True) + + params = 'PARAMS={}'.format(paramset) # overrides Makefile var + thash = 'THASH={}'.format(thash) # overrides Makefile var + + run(["make", "-C", impl, "clean", thash, params], + stdout=DEVNULL, stderr=sys.stderr) + run(["make", "-C", impl, "benchmarks", thash, params], + stdout=DEVNULL, stderr=sys.stderr) + run(["make", "-C", impl, "benchmark", thash, params], + stdout=sys.stdout, stderr=sys.stderr) + + print(flush=True) + diff --git a/src/crypto/pq/_s/haraka-aesni/.gitignore b/src/crypto/pq/_s/haraka-aesni/.gitignore new file mode 100644 index 0000000..67bcef9 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/.gitignore @@ -0,0 +1,5 @@ +test/* +!test/*.c +PQCsignKAT_*.rsp +PQCsignKAT_*.req +PQCgenKAT_sign diff --git a/src/crypto/pq/_s/haraka-aesni/PQCgenKAT_sign.c b/src/crypto/pq/_s/haraka-aesni/PQCgenKAT_sign.c new file mode 120000 index 0000000..a17dbe2 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/PQCgenKAT_sign.c @@ -0,0 +1 @@ +../ref/PQCgenKAT_sign.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/address.c b/src/crypto/pq/_s/haraka-aesni/address.c new file mode 120000 index 0000000..02c52c6 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/address.c @@ -0,0 +1 @@ +../ref/address.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/address.h b/src/crypto/pq/_s/haraka-aesni/address.h new file mode 120000 index 0000000..e670da5 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/address.h @@ -0,0 +1 @@ +../ref/address.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/api.h b/src/crypto/pq/_s/haraka-aesni/api.h new file mode 120000 index 0000000..ea89e0a --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/api.h @@ -0,0 +1 @@ +../ref/api.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/context.h b/src/crypto/pq/_s/haraka-aesni/context.h new file mode 100644 index 0000000..f88445a --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/context.h @@ -0,0 +1,16 @@ +#ifndef SPX_CONTEXT_H +#define SPX_CONTEXT_H + +#include + +#include "params.h" +#include "immintrin.h" + +typedef struct { + uint8_t pub_seed[SPX_N]; + uint8_t sk_seed[SPX_N]; + + __m128i rc[40]; +} spx_ctx; + +#endif diff --git a/src/crypto/pq/_s/haraka-aesni/fors.c b/src/crypto/pq/_s/haraka-aesni/fors.c new file mode 120000 index 0000000..abd8d52 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/fors.c @@ -0,0 +1 @@ +../shake-avx2/fors.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/fors.h b/src/crypto/pq/_s/haraka-aesni/fors.h new file mode 120000 index 0000000..07156bd --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/fors.h @@ -0,0 +1 @@ +../ref/fors.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/haraka.c b/src/crypto/pq/_s/haraka-aesni/haraka.c new file mode 100644 index 0000000..2d01db8 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/haraka.c @@ -0,0 +1,720 @@ +/* +Plain C implementation of the Haraka256 and Haraka512 permutations. +*/ +#include +#include +#include +#include +#include + +#include "haraka.h" +#include "harakax4.h" +#include "utils.h" + +#define HARAKAS_RATE 32 + +#define u64 unsigned long +#define u128 __m128i + +#define LOAD(src) _mm_loadu_si128((u128 *)(src)) +#define STORE(dest,src) _mm_storeu_si128((u128 *)(dest),src) + +#define XOR128(a, b) _mm_xor_si128(a, b) + +#define AES2(s0, s1, rci) \ + s0 = _mm_aesenc_si128(s0, *(rci)); \ + s1 = _mm_aesenc_si128(s1, *(rci + 1)); \ + s0 = _mm_aesenc_si128(s0, *(rci + 2)); \ + s1 = _mm_aesenc_si128(s1, *(rci + 3)); + +#define AES2_4x(s0, s1, s2, s3, rci) \ + AES2(s0[0], s0[1], rci); \ + AES2(s1[0], s1[1], rci); \ + AES2(s2[0], s2[1], rci); \ + AES2(s3[0], s3[1], rci); + +#define AES4(s0, s1, s2, s3, rci) \ + s0 = _mm_aesenc_si128(s0, *(rci)); \ + s1 = _mm_aesenc_si128(s1, *(rci + 1)); \ + s2 = _mm_aesenc_si128(s2, *(rci + 2)); \ + s3 = _mm_aesenc_si128(s3, *(rci + 3)); \ + s0 = _mm_aesenc_si128(s0, *(rci + 4)); \ + s1 = _mm_aesenc_si128(s1, *(rci + 5)); \ + s2 = _mm_aesenc_si128(s2, *(rci + 6)); \ + s3 = _mm_aesenc_si128(s3, *(rci + 7)); + +#define AES4_4x(s0, s1, s2, s3, rci) \ + AES4(s0[0], s0[1], s0[2], s0[3], rci); \ + AES4(s1[0], s1[1], s1[2], s1[3], rci); \ + AES4(s2[0], s2[1], s2[2], s2[3], rci); \ + AES4(s3[0], s3[1], s3[2], s3[3], rci); + +#define MIX2(s0, s1) \ + tmp = _mm_unpacklo_epi32(s0, s1); \ + s1 = _mm_unpackhi_epi32(s0, s1); \ + s0 = tmp; + +#define MIX4(s0, s1, s2, s3) \ + tmp = _mm_unpacklo_epi32(s0, s1); \ + s0 = _mm_unpackhi_epi32(s0, s1); \ + s1 = _mm_unpacklo_epi32(s2, s3); \ + s2 = _mm_unpackhi_epi32(s2, s3); \ + s3 = _mm_unpacklo_epi32(s0, s2); \ + s0 = _mm_unpackhi_epi32(s0, s2); \ + s2 = _mm_unpackhi_epi32(s1, tmp); \ + s1 = _mm_unpacklo_epi32(s1, tmp); + +#define TRUNCSTORE(out, s0, s1, s2, s3) \ + _mm_storeu_si128((u128 *)out, \ + _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(s0), _mm_castsi128_pd(s1), 3))); \ + _mm_storeu_si128((u128 *)(out + 16), \ + _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(s2), _mm_castsi128_pd(s3), 0))); + +static void load_haraka_constants(u128 *rc) +{ + rc[0] = _mm_set_epi32(0x0684704c,0xe620c00a,0xb2c5fef0,0x75817b9d); + rc[1] = _mm_set_epi32(0x8b66b4e1,0x88f3a06b,0x640f6ba4,0x2f08f717); + rc[2] = _mm_set_epi32(0x3402de2d,0x53f28498,0xcf029d60,0x9f029114); + rc[3] = _mm_set_epi32(0x0ed6eae6,0x2e7b4f08,0xbbf3bcaf,0xfd5b4f79); + rc[4] = _mm_set_epi32(0xcbcfb0cb,0x4872448b,0x79eecd1c,0xbe397044); + rc[5] = _mm_set_epi32(0x7eeacdee,0x6e9032b7,0x8d5335ed,0x2b8a057b); + rc[6] = _mm_set_epi32(0x67c28f43,0x5e2e7cd0,0xe2412761,0xda4fef1b); + rc[7] = _mm_set_epi32(0x2924d9b0,0xafcacc07,0x675ffde2,0x1fc70b3b); + rc[8] = _mm_set_epi32(0xab4d63f1,0xe6867fe9,0xecdb8fca,0xb9d465ee); + rc[9] = _mm_set_epi32(0x1c30bf84,0xd4b7cd64,0x5b2a404f,0xad037e33); + rc[10] = _mm_set_epi32(0xb2cc0bb9,0x941723bf,0x69028b2e,0x8df69800); + rc[11] = _mm_set_epi32(0xfa0478a6,0xde6f5572,0x4aaa9ec8,0x5c9d2d8a); + rc[12] = _mm_set_epi32(0xdfb49f2b,0x6b772a12,0x0efa4f2e,0x29129fd4); + rc[13] = _mm_set_epi32(0x1ea10344,0xf449a236,0x32d611ae,0xbb6a12ee); + rc[14] = _mm_set_epi32(0xaf044988,0x4b050084,0x5f9600c9,0x9ca8eca6); + rc[15] = _mm_set_epi32(0x21025ed8,0x9d199c4f,0x78a2c7e3,0x27e593ec); + rc[16] = _mm_set_epi32(0xbf3aaaf8,0xa759c9b7,0xb9282ecd,0x82d40173); + rc[17] = _mm_set_epi32(0x6260700d,0x6186b017,0x37f2efd9,0x10307d6b); + rc[18] = _mm_set_epi32(0x5aca45c2,0x21300443,0x81c29153,0xf6fc9ac6); + rc[19] = _mm_set_epi32(0x9223973c,0x226b68bb,0x2caf92e8,0x36d1943a); + rc[20] = _mm_set_epi32(0xd3bf9238,0x225886eb,0x6cbab958,0xe51071b4); + rc[21] = _mm_set_epi32(0xdb863ce5,0xaef0c677,0x933dfddd,0x24e1128d); + rc[22] = _mm_set_epi32(0xbb606268,0xffeba09c,0x83e48de3,0xcb2212b1); + rc[23] = _mm_set_epi32(0x734bd3dc,0xe2e4d19c,0x2db91a4e,0xc72bf77d); + rc[24] = _mm_set_epi32(0x43bb47c3,0x61301b43,0x4b1415c4,0x2cb3924e); + rc[25] = _mm_set_epi32(0xdba775a8,0xe707eff6,0x03b231dd,0x16eb6899); + rc[26] = _mm_set_epi32(0x6df3614b,0x3c755977,0x8e5e2302,0x7eca472c); + rc[27] = _mm_set_epi32(0xcda75a17,0xd6de7d77,0x6d1be5b9,0xb88617f9); + rc[28] = _mm_set_epi32(0xec6b43f0,0x6ba8e9aa,0x9d6c069d,0xa946ee5d); + rc[29] = _mm_set_epi32(0xcb1e6950,0xf957332b,0xa2531159,0x3bf327c1); + rc[30] = _mm_set_epi32(0x2cee0c75,0x00da619c,0xe4ed0353,0x600ed0d9); + rc[31] = _mm_set_epi32(0xf0b1a5a1,0x96e90cab,0x80bbbabc,0x63a4a350); + rc[32] = _mm_set_epi32(0xae3db102,0x5e962988,0xab0dde30,0x938dca39); + rc[33] = _mm_set_epi32(0x17bb8f38,0xd554a40b,0x8814f3a8,0x2e75b442); + rc[34] = _mm_set_epi32(0x34bb8a5b,0x5f427fd7,0xaeb6b779,0x360a16f6); + rc[35] = _mm_set_epi32(0x26f65241,0xcbe55438,0x43ce5918,0xffbaafde); + rc[36] = _mm_set_epi32(0x4ce99a54,0xb9f3026a,0xa2ca9cf7,0x839ec978); + rc[37] = _mm_set_epi32(0xae51a51a,0x1bdff7be,0x40c06e28,0x22901235); + rc[38] = _mm_set_epi32(0xa0c1613c,0xba7ed22b,0xc173bc0f,0x48a659cf); + rc[39] = _mm_set_epi32(0x756acc03,0x02288288,0x4ad6bdfd,0xe9c59da1); +} + +void tweak_constants(spx_ctx *ctx) +{ + int i; + unsigned char buf[40*16]; + + /* Use the standard constants to generate tweaked ones. */ + load_haraka_constants(ctx->rc); + + /* Constants for pk.seed */ + haraka_S(buf, 40*16, ctx->pub_seed, SPX_N, ctx); + + /* Tweak constants with the pub_seed */ + for (i = 0; i < 40; i++) { + ctx->rc[i] = LOAD(buf + i*16); + } +} + +static void haraka_S_absorb(unsigned char *s, unsigned int r, + const unsigned char *m, unsigned long long mlen, + unsigned char p, const spx_ctx *ctx) +{ + unsigned long long i; + SPX_VLA(unsigned char, t, r); + + while (mlen >= r) { + // XOR block to state + STORE(s, XOR128(LOAD(s), LOAD(m))); + STORE(s + 16, XOR128(LOAD(s + 16), LOAD(m + 16))); + haraka512_perm(s, s, ctx); + mlen -= r; + m += r; + } + + for (i = 0; i < r; ++i) { + t[i] = 0; + } + for (i = 0; i < mlen; ++i) { + t[i] = m[i]; + } + t[i] = p; + t[r - 1] |= 128; + STORE(s, XOR128(LOAD(s), LOAD(t))); + STORE(s + 16, XOR128(LOAD(s + 16), LOAD(t + 16))); +} + +static void haraka_S_absorb4x(unsigned char *s, + unsigned int r, + const unsigned char *m0, + const unsigned char *m1, + const unsigned char *m2, + const unsigned char *m3, + unsigned long long int mlen, + unsigned char p, + const spx_ctx *ctx) +{ + unsigned long long i; + SPX_VLA(unsigned char, t0, r); + SPX_VLA(unsigned char, t1, r); + SPX_VLA(unsigned char, t2, r); + SPX_VLA(unsigned char, t3, r); + + while (mlen >= r) { + // XOR block to state + STORE(s, XOR128(LOAD(s), LOAD(m0))); + STORE(s + 16, XOR128(LOAD(s + 16), LOAD(m0 + 16))); + STORE(s + 64, XOR128(LOAD(s + 64), LOAD(m1))); + STORE(s + 80, XOR128(LOAD(s + 80), LOAD(m1 + 16))); + STORE(s + 128, XOR128(LOAD(s + 128), LOAD(m2))); + STORE(s + 144, XOR128(LOAD(s + 144), LOAD(m2 + 16))); + STORE(s + 192, XOR128(LOAD(s + 192), LOAD(m3))); + STORE(s + 208, XOR128(LOAD(s + 208), LOAD(m3 + 16))); + + haraka512_perm_x4(s, s, ctx); + mlen -= r; + m0 += r; + m1 += r; + m2 += r; + m3 += r; + } + + for (i = 0; i < r; ++i) { + t0[i] = 0; + t1[i] = 0; + t2[i] = 0; + t3[i] = 0; + } + for (i = 0; i < mlen; ++i) { + t0[i] = m0[i]; + t1[i] = m1[i]; + t2[i] = m2[i]; + t3[i] = m3[i]; + } + + t0[i] = p; + t1[i] = p; + t2[i] = p; + t3[i] = p; + + t0[r - 1] |= 128; + t1[r - 1] |= 128; + t2[r - 1] |= 128; + t3[r - 1] |= 128; + + STORE(s, XOR128(LOAD(s), LOAD(t0))); + STORE(s + 16, XOR128(LOAD(s + 16), LOAD(t0 + 16))); + STORE(s + 64, XOR128(LOAD(s + 64), LOAD(t1))); + STORE(s + 80, XOR128(LOAD(s + 80), LOAD(t1 + 16))); + STORE(s + 128, XOR128(LOAD(s + 128), LOAD(t2))); + STORE(s + 144, XOR128(LOAD(s + 144), LOAD(t2 + 16))); + STORE(s + 192, XOR128(LOAD(s + 192), LOAD(t3))); + STORE(s + 208, XOR128(LOAD(s + 208), LOAD(t3 + 16))); +} + +static void haraka_S_squeezeblocks(unsigned char *h, unsigned long long nblocks, + unsigned char *s, unsigned int r, + const spx_ctx *ctx) +{ + while (nblocks > 0) { + haraka512_perm(s, s, ctx); + STORE(h, LOAD(s)); + STORE(h + 16, LOAD(s + 16)); + h += r; + nblocks--; + } +} + +static void haraka_S_squeezeblocks4x(unsigned char *h0, + unsigned char *h1, + unsigned char *h2, + unsigned char *h3, + unsigned long long nblocks, + unsigned char *s, + unsigned int r, + const spx_ctx *ctx) +{ + while (nblocks > 0) { + haraka512_perm_x4(s, s, ctx); + STORE(h0, LOAD(s)); + STORE(h0 + 16, LOAD(s + 16)); + STORE(h1, LOAD(s + 64)); + STORE(h1 + 16, LOAD(s + 80)); + STORE(h2, LOAD(s + 128)); + STORE(h2 + 16, LOAD(s + 144)); + STORE(h3, LOAD(s + 192)); + STORE(h3 + 16, LOAD(s + 208)); + h0 += r; + h1 += r; + h2 += r; + h3 += r; + nblocks--; + } +} + +void haraka_S_inc_init(uint8_t *s_inc) +{ + size_t i; + + for (i = 0; i < 64; i++) { + s_inc[i] = 0; + } + s_inc[64] = 0; +} + +void haraka_S_inc_absorb(uint8_t *s_inc, const uint8_t *m, size_t mlen, + const spx_ctx *ctx) +{ + size_t i; + + /* Recall that s_inc[64] is the non-absorbed bytes xored into the state */ + while (mlen + s_inc[64] >= HARAKAS_RATE) { + for (i = 0; i < (size_t)(HARAKAS_RATE - s_inc[64]); i++) { + /* Take the i'th byte from message + xor with the s_inc[64] + i'th byte of the state */ + s_inc[s_inc[64] + i] ^= m[i]; + } + mlen -= (size_t)(HARAKAS_RATE - s_inc[64]); + m += HARAKAS_RATE - s_inc[64]; + s_inc[64] = 0; + + haraka512_perm(s_inc, s_inc, ctx); + } + + for (i = 0; i < mlen; i++) { + s_inc[s_inc[64] + i] ^= m[i]; + } + s_inc[64] += mlen; +} + +void haraka_S_inc_finalize(uint8_t *s_inc) +{ + /* After haraka_S_inc_absorb, we are guaranteed that s_inc[64] < HARAKAS_RATE, + so we can always use one more byte for p in the current state. */ + s_inc[s_inc[64]] ^= 0x1F; + s_inc[HARAKAS_RATE - 1] ^= 128; + s_inc[64] = 0; +} + +void haraka_S_inc_squeeze(uint8_t *out, size_t outlen, uint8_t *s_inc, + const spx_ctx *ctx) +{ + size_t i; + + /* First consume any bytes we still have sitting around */ + for (i = 0; i < outlen && i < s_inc[64]; i++) { + /* There are s_inc[64] bytes left, so r - s_inc[64] is the first + available byte. We consume from there, i.e., up to r. */ + out[i] = (uint8_t)s_inc[(HARAKAS_RATE - s_inc[64] + i)]; + } + out += i; + outlen -= i; + s_inc[64] -= i; + + /* Then squeeze the remaining necessary blocks */ + while (outlen > 0) { + haraka512_perm(s_inc, s_inc, ctx); + + for (i = 0; i < outlen && i < HARAKAS_RATE; i++) { + out[i] = s_inc[i]; + } + out += i; + outlen -= i; + s_inc[64] = HARAKAS_RATE - i; + } +} + +void haraka_S(unsigned char *out, unsigned long long outlen, + const unsigned char *in, unsigned long long inlen, + const spx_ctx *ctx) +{ + unsigned long long i; + unsigned char s[64]; + unsigned char d[32]; + + for (i = 0; i < 64; i++) { + s[i] = 0; + } + haraka_S_absorb(s, HARAKAS_RATE, in, inlen, 0x1F, ctx); + + haraka_S_squeezeblocks(out, outlen / HARAKAS_RATE, s, HARAKAS_RATE, ctx); + out += (outlen / HARAKAS_RATE) * HARAKAS_RATE; + + if (outlen % HARAKAS_RATE) { + haraka_S_squeezeblocks(d, 1, s, HARAKAS_RATE, ctx); + for (i = 0; i < outlen % HARAKAS_RATE; i++) { + out[i] = d[i]; + } + } +} + +void haraka_Sx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned long long outlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + unsigned long long inlen, + const spx_ctx *ctx) +{ + unsigned long long i; + unsigned char s[64 * 4]; + unsigned char d0[32]; + unsigned char d1[32]; + unsigned char d2[32]; + unsigned char d3[32]; + + for (i = 0; i < 64 * 4; i++) { + s[i] = 0; + } + haraka_S_absorb4x(s, HARAKAS_RATE, in0, in1, in2, in3, inlen, 0x1F, ctx); + + haraka_S_squeezeblocks4x(out0, out1, out2, out3, outlen / HARAKAS_RATE, s, + HARAKAS_RATE, ctx); + out0 += (outlen / HARAKAS_RATE) * HARAKAS_RATE; + out1 += (outlen / HARAKAS_RATE) * HARAKAS_RATE; + out2 += (outlen / HARAKAS_RATE) * HARAKAS_RATE; + out3 += (outlen / HARAKAS_RATE) * HARAKAS_RATE; + + if (outlen % HARAKAS_RATE) { + haraka_S_squeezeblocks4x(d0, d1, d2, d3, 1, s, HARAKAS_RATE, ctx); + for (i = 0; i < outlen % HARAKAS_RATE; i++) { + out0[i] = d0[i]; + out1[i] = d1[i]; + out2[i] = d2[i]; + out3[i] = d3[i]; + } + } +} + +void haraka512_perm(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx) +{ + u128 s[4], tmp; + + s[0] = LOAD(in); + s[1] = LOAD(in + 16); + s[2] = LOAD(in + 32); + s[3] = LOAD(in + 48); + + AES4(s[0], s[1], s[2], s[3], ctx->rc); + MIX4(s[0], s[1], s[2], s[3]); + + AES4(s[0], s[1], s[2], s[3], ctx->rc + 8); + MIX4(s[0], s[1], s[2], s[3]); + + AES4(s[0], s[1], s[2], s[3], ctx->rc + 16); + MIX4(s[0], s[1], s[2], s[3]); + + AES4(s[0], s[1], s[2], s[3], ctx->rc + 24); + MIX4(s[0], s[1], s[2], s[3]); + + AES4(s[0], s[1], s[2], s[3], ctx->rc + 32); + MIX4(s[0], s[1], s[2], s[3]); + + STORE(out, s[0]); + STORE(out + 16, s[1]); + STORE(out + 32, s[2]); + STORE(out + 48, s[3]); +} + +void haraka512_perm_x4(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx) +{ + u128 s[4][4], tmp; + + s[0][0] = LOAD(in); + s[0][1] = LOAD(in + 16); + s[0][2] = LOAD(in + 32); + s[0][3] = LOAD(in + 48); + s[1][0] = LOAD(in + 64); + s[1][1] = LOAD(in + 80); + s[1][2] = LOAD(in + 96); + s[1][3] = LOAD(in + 112); + s[2][0] = LOAD(in + 128); + s[2][1] = LOAD(in + 144); + s[2][2] = LOAD(in + 160); + s[2][3] = LOAD(in + 176); + s[3][0] = LOAD(in + 192); + s[3][1] = LOAD(in + 208); + s[3][2] = LOAD(in + 224); + s[3][3] = LOAD(in + 240); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc + 8); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc + 16); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc + 24); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc + 32); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + STORE(out, s[0][0]); + STORE(out + 16, s[0][1]); + STORE(out + 32, s[0][2]); + STORE(out + 48, s[0][3]); + STORE(out + 64, s[1][0]); + STORE(out + 80, s[1][1]); + STORE(out + 96, s[1][2]); + STORE(out + 112, s[1][3]); + STORE(out + 128, s[2][0]); + STORE(out + 144, s[2][1]); + STORE(out + 160, s[2][2]); + STORE(out + 176, s[2][3]); + STORE(out + 192, s[3][0]); + STORE(out + 208, s[3][1]); + STORE(out + 224, s[3][2]); + STORE(out + 240, s[3][3]); +} + +void haraka512(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx) +{ + u128 s[4], tmp; + + s[0] = LOAD(in); + s[1] = LOAD(in + 16); + s[2] = LOAD(in + 32); + s[3] = LOAD(in + 48); + + AES4(s[0], s[1], s[2], s[3], ctx->rc); + MIX4(s[0], s[1], s[2], s[3]); + + AES4(s[0], s[1], s[2], s[3], ctx->rc + 8); + MIX4(s[0], s[1], s[2], s[3]); + + AES4(s[0], s[1], s[2], s[3], ctx->rc + 16); + MIX4(s[0], s[1], s[2], s[3]); + + AES4(s[0], s[1], s[2], s[3], ctx->rc + 24); + MIX4(s[0], s[1], s[2], s[3]); + + AES4(s[0], s[1], s[2], s[3], ctx->rc + 32); + MIX4(s[0], s[1], s[2], s[3]); + + s[0] = XOR128(s[0], LOAD(in)); + s[1] = XOR128(s[1], LOAD(in + 16)); + s[2] = XOR128(s[2], LOAD(in + 32)); + s[3] = XOR128(s[3], LOAD(in + 48)); + + // truncate and store result + TRUNCSTORE(out, s[0], s[1], s[2], s[3]); +} + +void haraka512x4(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx) +{ + u128 s[4][4], tmp; + + s[0][0] = LOAD(in); + s[0][1] = LOAD(in + 16); + s[0][2] = LOAD(in + 32); + s[0][3] = LOAD(in + 48); + s[1][0] = LOAD(in + 64); + s[1][1] = LOAD(in + 80); + s[1][2] = LOAD(in + 96); + s[1][3] = LOAD(in + 112); + s[2][0] = LOAD(in + 128); + s[2][1] = LOAD(in + 144); + s[2][2] = LOAD(in + 160); + s[2][3] = LOAD(in + 176); + s[3][0] = LOAD(in + 192); + s[3][1] = LOAD(in + 208); + s[3][2] = LOAD(in + 224); + s[3][3] = LOAD(in + 240); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc + 8); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc + 16); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc + 24); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + AES4_4x(s[0], s[1], s[2], s[3], ctx->rc + 32); + MIX4(s[0][0], s[0][1], s[0][2], s[0][3]); + MIX4(s[1][0], s[1][1], s[1][2], s[1][3]); + MIX4(s[2][0], s[2][1], s[2][2], s[2][3]); + MIX4(s[3][0], s[3][1], s[3][2], s[3][3]); + + s[0][0] = XOR128(s[0][0], LOAD(in)); + s[0][1] = XOR128(s[0][1], LOAD(in + 16)); + s[0][2] = XOR128(s[0][2], LOAD(in + 32)); + s[0][3] = XOR128(s[0][3], LOAD(in + 48)); + s[1][0] = XOR128(s[1][0], LOAD(in + 64)); + s[1][1] = XOR128(s[1][1], LOAD(in + 80)); + s[1][2] = XOR128(s[1][2], LOAD(in + 96)); + s[1][3] = XOR128(s[1][3], LOAD(in + 112)); + s[2][0] = XOR128(s[2][0], LOAD(in + 128)); + s[2][1] = XOR128(s[2][1], LOAD(in + 144)); + s[2][2] = XOR128(s[2][2], LOAD(in + 160)); + s[2][3] = XOR128(s[2][3], LOAD(in + 176)); + s[3][0] = XOR128(s[3][0], LOAD(in + 192)); + s[3][1] = XOR128(s[3][1], LOAD(in + 208)); + s[3][2] = XOR128(s[3][2], LOAD(in + 224)); + s[3][3] = XOR128(s[3][3], LOAD(in + 240)); + + TRUNCSTORE(out, s[0][0], s[0][1], s[0][2], s[0][3]); + TRUNCSTORE((out + 32), s[1][0], s[1][1], s[1][2], s[1][3]); + TRUNCSTORE((out + 64), s[2][0], s[2][1], s[2][2], s[2][3]); + TRUNCSTORE((out + 96), s[3][0], s[3][1], s[3][2], s[3][3]); +} + +void haraka256(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx) +{ + u128 s[2], tmp; + + s[0] = LOAD(in); + s[1] = LOAD(in + 16); + + AES2(s[0], s[1], ctx->rc); + MIX2(s[0], s[1]); + + AES2(s[0], s[1], ctx->rc + 4); + MIX2(s[0], s[1]); + + AES2(s[0], s[1], ctx->rc + 8); + MIX2(s[0], s[1]); + + AES2(s[0], s[1], ctx->rc + 12); + MIX2(s[0], s[1]); + + AES2(s[0], s[1], ctx->rc + 16); + MIX2(s[0], s[1]); + + s[0] = XOR128(s[0], LOAD(in)); + s[1] = XOR128(s[1], LOAD(in + 16)); + + STORE(out, s[0]); + STORE(out + 16, s[1]); +} + +void haraka256x4(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx) +{ + u128 s[4][2], tmp; + + s[0][0] = LOAD(in); + s[0][1] = LOAD(in + 16); + s[1][0] = LOAD(in + 32); + s[1][1] = LOAD(in + 48); + s[2][0] = LOAD(in + 64); + s[2][1] = LOAD(in + 80); + s[3][0] = LOAD(in + 96); + s[3][1] = LOAD(in + 112); + + // Round 1 + AES2_4x(s[0], s[1], s[2], s[3], ctx->rc); + + MIX2(s[0][0], s[0][1]); + MIX2(s[1][0], s[1][1]); + MIX2(s[2][0], s[2][1]); + MIX2(s[3][0], s[3][1]); + + // Round 2 + AES2_4x(s[0], s[1], s[2], s[3], ctx->rc + 4); + + MIX2(s[0][0], s[0][1]); + MIX2(s[1][0], s[1][1]); + MIX2(s[2][0], s[2][1]); + MIX2(s[3][0], s[3][1]); + + // Round 3 + AES2_4x(s[0], s[1], s[2], s[3], ctx->rc + 8); + + MIX2(s[0][0], s[0][1]); + MIX2(s[1][0], s[1][1]); + MIX2(s[2][0], s[2][1]); + MIX2(s[3][0], s[3][1]); + + // Round 4 + AES2_4x(s[0], s[1], s[2], s[3], ctx->rc + 12); + + MIX2(s[0][0], s[0][1]); + MIX2(s[1][0], s[1][1]); + MIX2(s[2][0], s[2][1]); + MIX2(s[3][0], s[3][1]); + + // Round 5 + AES2_4x(s[0], s[1], s[2], s[3], ctx->rc + 16); + + MIX2(s[0][0], s[0][1]); + MIX2(s[1][0], s[1][1]); + MIX2(s[2][0], s[2][1]); + MIX2(s[3][0], s[3][1]); + + // Feed Forward + s[0][0] = _mm_xor_si128(s[0][0], LOAD(in)); + s[0][1] = _mm_xor_si128(s[0][1], LOAD(in + 16)); + s[1][0] = _mm_xor_si128(s[1][0], LOAD(in + 32)); + s[1][1] = _mm_xor_si128(s[1][1], LOAD(in + 48)); + s[2][0] = _mm_xor_si128(s[2][0], LOAD(in + 64)); + s[2][1] = _mm_xor_si128(s[2][1], LOAD(in + 80)); + s[3][0] = _mm_xor_si128(s[3][0], LOAD(in + 96)); + s[3][1] = _mm_xor_si128(s[3][1], LOAD(in + 112)); + + STORE(out, s[0][0]); + STORE(out + 16, s[0][1]); + STORE(out + 32, s[1][0]); + STORE(out + 48, s[1][1]); + STORE(out + 64, s[2][0]); + STORE(out + 80, s[2][1]); + STORE(out + 96, s[3][0]); + STORE(out + 112, s[3][1]); +} diff --git a/src/crypto/pq/_s/haraka-aesni/haraka.h b/src/crypto/pq/_s/haraka-aesni/haraka.h new file mode 120000 index 0000000..0378b1e --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/haraka.h @@ -0,0 +1 @@ +../ref/haraka.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/haraka_offsets.h b/src/crypto/pq/_s/haraka-aesni/haraka_offsets.h new file mode 120000 index 0000000..cb6abf9 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/haraka_offsets.h @@ -0,0 +1 @@ +../ref/haraka_offsets.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/harakax4.h b/src/crypto/pq/_s/haraka-aesni/harakax4.h new file mode 100644 index 0000000..2a18650 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/harakax4.h @@ -0,0 +1,36 @@ +#ifndef SPX_HARAKAX4_H +#define SPX_HARAKAX4_H + +#include "context.h" +#include "params.h" + +/* Haraka Sponge */ +#define haraka_Sx4 SPX_NAMESPACE(haraka_Sx4) +void haraka_Sx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned long long outlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + unsigned long long inlen, + const spx_ctx *ctx); + +/* Applies the 512-bit Haraka permutation x4 to in. */ +#define haraka512_perm_x4 SPX_NAMESPACE(haraka512_perm_x4) +void haraka512_perm_x4(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx); + +/* Implementation of Haraka-512 x4*/ +#define haraka512x4 SPX_NAMESPACE(haraka512x4) +void haraka512x4(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx); + +/* Implementation of Haraka-256 x4 */ +#define haraka256x4 SPX_NAMESPACE(haraka256x4) +void haraka256x4(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx); + +#endif diff --git a/src/crypto/pq/_s/haraka-aesni/hash.h b/src/crypto/pq/_s/haraka-aesni/hash.h new file mode 120000 index 0000000..cffc52b --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/hash.h @@ -0,0 +1 @@ +../ref/hash.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/hash_haraka.c b/src/crypto/pq/_s/haraka-aesni/hash_haraka.c new file mode 120000 index 0000000..6c5febe --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/hash_haraka.c @@ -0,0 +1 @@ +../ref/hash_haraka.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/hash_harakax4.c b/src/crypto/pq/_s/haraka-aesni/hash_harakax4.c new file mode 100644 index 0000000..e970078 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/hash_harakax4.c @@ -0,0 +1,36 @@ +#include +#include + +#include "address.h" +#include "params.h" +#include "harakax4.h" +#include "hashx4.h" + +/* + * 4-way parallel version of prf_addr; takes 4x as much input and output + */ +#define prf_addrx4 SPX_NAMESPACE(prf_addrx4) +void prf_addrx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const spx_ctx *ctx, + const uint32_t addrx4[4*8]) +{ + unsigned char bufx4[4 * 64] = {0}; + /* Since SPX_N may be smaller than 32, we need temporary buffers. */ + unsigned char outbuf[4 * 32]; + unsigned int i; + + for (i = 0; i < 4; i++) { + memcpy(bufx4 + i*64, addrx4 + i*8, SPX_ADDR_BYTES); + memcpy(bufx4 + i*64 + SPX_ADDR_BYTES, ctx->sk_seed, SPX_N); + } + + haraka512x4(outbuf, bufx4, ctx); + + memcpy(out0, outbuf, SPX_N); + memcpy(out1, outbuf + 32, SPX_N); + memcpy(out2, outbuf + 64, SPX_N); + memcpy(out3, outbuf + 96, SPX_N); +} diff --git a/src/crypto/pq/_s/haraka-aesni/hashx4.h b/src/crypto/pq/_s/haraka-aesni/hashx4.h new file mode 120000 index 0000000..3dc1ba4 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/hashx4.h @@ -0,0 +1 @@ +../shake-avx2/hashx4.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/merkle.c b/src/crypto/pq/_s/haraka-aesni/merkle.c new file mode 120000 index 0000000..7a8454d --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/merkle.c @@ -0,0 +1 @@ +../shake-avx2/merkle.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/merkle.h b/src/crypto/pq/_s/haraka-aesni/merkle.h new file mode 120000 index 0000000..7d167ed --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/merkle.h @@ -0,0 +1 @@ +../ref/merkle.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/params.h b/src/crypto/pq/_s/haraka-aesni/params.h new file mode 120000 index 0000000..53133cc --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/params.h @@ -0,0 +1 @@ +../ref/params.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-128f.h b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-128f.h new file mode 120000 index 0000000..4e5417f --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-128f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-haraka-128f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-128s.h b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-128s.h new file mode 120000 index 0000000..686aa8c --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-128s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-haraka-128s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-192f.h b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-192f.h new file mode 120000 index 0000000..14971bf --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-192f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-haraka-192f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-192s.h b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-192s.h new file mode 120000 index 0000000..3361225 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-192s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-haraka-192s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-256f.h b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-256f.h new file mode 120000 index 0000000..28d6862 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-256f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-haraka-256f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-256s.h b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-256s.h new file mode 120000 index 0000000..233ce46 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/params/params-sphincs-haraka-256s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-haraka-256s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/randombytes.c b/src/crypto/pq/_s/haraka-aesni/randombytes.c new file mode 120000 index 0000000..59a42a5 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/randombytes.c @@ -0,0 +1 @@ +../ref/randombytes.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/randombytes.h b/src/crypto/pq/_s/haraka-aesni/randombytes.h new file mode 120000 index 0000000..055e443 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/randombytes.h @@ -0,0 +1 @@ +../ref/randombytes.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/rng.c b/src/crypto/pq/_s/haraka-aesni/rng.c new file mode 120000 index 0000000..6e2fdac --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/rng.c @@ -0,0 +1 @@ +../ref/rng.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/rng.h b/src/crypto/pq/_s/haraka-aesni/rng.h new file mode 120000 index 0000000..d678c7c --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/rng.h @@ -0,0 +1 @@ +../ref/rng.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/sign.c b/src/crypto/pq/_s/haraka-aesni/sign.c new file mode 120000 index 0000000..42fea88 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/sign.c @@ -0,0 +1 @@ +../ref/sign.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/test/benchmark.c b/src/crypto/pq/_s/haraka-aesni/test/benchmark.c new file mode 100644 index 0000000..1c87297 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/test/benchmark.c @@ -0,0 +1,162 @@ +#define _POSIX_C_SOURCE 199309L + +#include +#include +#include + +#include "../api.h" +#include "../fors.h" +#include "../wotsx4.h" +#include "../params.h" +#include "../randombytes.h" + +#define SPX_MLEN 32 +#define NTESTS 10 + +static void wots_gen_pkx4(unsigned char* pk, const spx_ctx *ctx, + uint32_t addr[8]); + +static int cmp_llu(const void *a, const void*b) +{ + if(*(unsigned long long *)a < *(unsigned long long *)b) return -1; + if(*(unsigned long long *)a > *(unsigned long long *)b) return 1; + return 0; +} + +static unsigned long long median(unsigned long long *l, size_t llen) +{ + qsort(l,llen,sizeof(unsigned long long),cmp_llu); + + if(llen%2) return l[llen/2]; + else return (l[llen/2-1]+l[llen/2])/2; +} + +static void delta(unsigned long long *l, size_t llen) +{ + unsigned int i; + for(i = 0; i < llen - 1; i++) { + l[i] = l[i+1] - l[i]; + } +} + +static unsigned long long cpucycles(void) +{ + unsigned long long result; + __asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" + : "=a" (result) :: "%rdx"); + return result; +} + +static void printfcomma (unsigned long long n) +{ + if (n < 1000) { + printf("%llu", n); + return; + } + printfcomma(n / 1000); + printf (",%03llu", n % 1000); +} + +static void printfalignedcomma (unsigned long long n, int len) +{ + unsigned long long ncopy = n; + int i = 0; + + while (ncopy > 9) { + len -= 1; + ncopy /= 10; + i += 1; // to account for commas + } + i = i/3 - 1; // to account for commas + for (; i < len; i++) { + printf(" "); + } + printfcomma(n); +} + +static void display_result(double result, unsigned long long *l, size_t llen, unsigned long long mul) +{ + unsigned long long med; + + result /= NTESTS; + delta(l, NTESTS + 1); + med = median(l, llen); + printf("avg. %11.2lf us (%2.2lf sec); median ", result, result / 1e6); + printfalignedcomma(med, 12); + printf(" cycles, %5llux: ", mul); + printfalignedcomma(mul*med, 12); + printf(" cycles\n"); +} + +#define MEASURE(TEXT, MUL, FNCALL)\ + printf(TEXT);\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);\ + for(i = 0; i < NTESTS; i++) {\ + t[i] = cpucycles();\ + FNCALL;\ + }\ + t[NTESTS] = cpucycles();\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);\ + result = (stop.tv_sec - start.tv_sec) * 1e6 + (stop.tv_nsec - start.tv_nsec) / 1e3;\ + display_result(result, t, NTESTS, MUL); + +int main(void) +{ + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + spx_ctx ctx; + unsigned char pk[SPX_PK_BYTES]; + unsigned char sk[SPX_SK_BYTES]; + unsigned char *m = malloc(SPX_MLEN); + unsigned char *sm = malloc(SPX_BYTES + SPX_MLEN); + unsigned char *mout = malloc(SPX_BYTES + SPX_MLEN); + + unsigned char fors_pk[SPX_FORS_PK_BYTES]; + unsigned char fors_m[SPX_FORS_MSG_BYTES]; + unsigned char fors_sig[SPX_FORS_BYTES]; + unsigned char addr[SPX_ADDR_BYTES]; + + unsigned char wots_pk[SPX_WOTS_PK_BYTES]; + + unsigned long long smlen; + unsigned long long mlen; + unsigned long long t[NTESTS+1]; + struct timespec start, stop; + double result; + int i; + + randombytes(m, SPX_MLEN); + randombytes(addr, SPX_ADDR_BYTES); + + printf("Parameters: n = %d, h = %d, d = %d, b = %d, k = %d, w = %d\n", + SPX_N, SPX_FULL_HEIGHT, SPX_D, SPX_FORS_HEIGHT, SPX_FORS_TREES, + SPX_WOTS_W); + + printf("Running %d iterations.\n", NTESTS); + + MEASURE("Generating keypair.. ", 1, crypto_sign_keypair(pk, sk)); + MEASURE(" - WOTS pk gen.. ", (1 << SPX_TREE_HEIGHT), wots_gen_pkx4(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Signing.. ", 1, crypto_sign(sm, &smlen, m, SPX_MLEN, sk)); + MEASURE(" - FORS signing.. ", 1, fors_sign(fors_sig, fors_pk, fors_m, &ctx, (uint32_t *) addr)); + MEASURE(" - WOTS pk gen.. ", SPX_D * (1 << SPX_TREE_HEIGHT), wots_gen_pkx4(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Verifying.. ", 1, crypto_sign_open(mout, &mlen, sm, smlen, pk)); + + printf("Signature size: %d (%.2f KiB)\n", SPX_BYTES, SPX_BYTES / 1024.0); + printf("Public key size: %d (%.2f KiB)\n", SPX_PK_BYTES, SPX_PK_BYTES / 1024.0); + printf("Secret key size: %d (%.2f KiB)\n", SPX_SK_BYTES, SPX_SK_BYTES / 1024.0); + + free(m); + free(sm); + free(mout); + + return 0; +} + +static void wots_gen_pkx4(unsigned char *pk, const spx_ctx *ctx, + uint32_t addr[8]) { + struct leaf_info_x4 leaf; + unsigned steps[ SPX_WOTS_LEN ] = { 0 }; + INITIALIZE_LEAF_INFO_X4(leaf, addr, steps); + wots_gen_leafx4(pk, ctx, 0, &leaf); +} diff --git a/src/crypto/pq/_s/haraka-aesni/test/fors.c b/src/crypto/pq/_s/haraka-aesni/test/fors.c new file mode 120000 index 0000000..b2bccee --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/test/fors.c @@ -0,0 +1 @@ +../../ref/test/fors.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/test/spx.c b/src/crypto/pq/_s/haraka-aesni/test/spx.c new file mode 120000 index 0000000..7af26df --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/test/spx.c @@ -0,0 +1 @@ +../../ref/test/spx.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/thash.h b/src/crypto/pq/_s/haraka-aesni/thash.h new file mode 120000 index 0000000..937dd48 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/thash.h @@ -0,0 +1 @@ +../ref/thash.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/thash_haraka_robust.c b/src/crypto/pq/_s/haraka-aesni/thash_haraka_robust.c new file mode 120000 index 0000000..156c18d --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/thash_haraka_robust.c @@ -0,0 +1 @@ +../ref/thash_haraka_robust.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/thash_haraka_robustx4.c b/src/crypto/pq/_s/haraka-aesni/thash_haraka_robustx4.c new file mode 100644 index 0000000..15d19f1 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/thash_haraka_robustx4.c @@ -0,0 +1,93 @@ +#include +#include + +#include "thashx4.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "harakax4.h" + +/** + * 4-way parallel version of thash; takes 4x as much input and output + */ +#define thashx4 SPX_NAMESPACE(thashx4) +void thashx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx4[4*8]) +{ + SPX_VLA(unsigned char, buf0, SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf1, SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf2, SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf3, SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, bitmask0, inblocks * SPX_N); + SPX_VLA(unsigned char, bitmask1, inblocks * SPX_N); + SPX_VLA(unsigned char, bitmask2, inblocks * SPX_N); + SPX_VLA(unsigned char, bitmask3, inblocks * SPX_N); + unsigned char outbuf[32 * 4]; + unsigned char buf_tmp[64 * 4]; + unsigned int i; + + if (inblocks == 1) { + memset(buf_tmp, 0, 64 * 4); + + // Generate masks first in buffer + memcpy(buf_tmp, addrx4 + 0*8, 32); + memcpy(buf_tmp + 32, addrx4 + 1*8, 32); + memcpy(buf_tmp + 64, addrx4 + 2*8, 32); + memcpy(buf_tmp + 96, addrx4 + 3*8, 32); + + haraka256x4(outbuf, buf_tmp, ctx); + + /* move addresses to make room for inputs; zero old values */ + memcpy(buf_tmp + 192, buf_tmp + 96, SPX_ADDR_BYTES); + memcpy(buf_tmp + 128, buf_tmp + 64, SPX_ADDR_BYTES); + memcpy(buf_tmp + 64, buf_tmp + 32, SPX_ADDR_BYTES); + /* skip memcpy(buf_tmp, buf_tmp, SPX_ADDR_BYTES); already in place */ + + /* skip memset(buf_tmp, 0, SPX_ADDR_BYTES); remained untouched */ + memset(buf_tmp + 32, 0, SPX_ADDR_BYTES); + /* skip memset(buf_tmp + 64, 0, SPX_ADDR_BYTES); contains addr1 */ + memset(buf_tmp + 96, 0, SPX_ADDR_BYTES); + + for (i = 0; i < SPX_N; i++) { + buf_tmp[SPX_ADDR_BYTES + i] = in0[i] ^ outbuf[i]; + buf_tmp[SPX_ADDR_BYTES + i + 64] = in1[i] ^ outbuf[i + 32]; + buf_tmp[SPX_ADDR_BYTES + i + 128] = in2[i] ^ outbuf[i + 64]; + buf_tmp[SPX_ADDR_BYTES + i + 192] = in3[i] ^ outbuf[i + 96]; + } + + haraka512x4(outbuf, buf_tmp, ctx); + + memcpy(out0, outbuf, SPX_N); + memcpy(out1, outbuf + 32, SPX_N); + memcpy(out2, outbuf + 64, SPX_N); + memcpy(out3, outbuf + 96, SPX_N); + } else { + /* All other tweakable hashes*/ + memcpy(buf0, addrx4 + 0*8, 32); + memcpy(buf1, addrx4 + 1*8, 32); + memcpy(buf2, addrx4 + 2*8, 32); + memcpy(buf3, addrx4 + 3*8, 32); + + haraka_Sx4(bitmask0, bitmask1, bitmask2, bitmask3, inblocks * SPX_N, + buf0, buf1, buf2, buf3, SPX_ADDR_BYTES, ctx); + + for (i = 0; i < inblocks * SPX_N; i++) { + buf0[SPX_ADDR_BYTES + i] = in0[i] ^ bitmask0[i]; + buf1[SPX_ADDR_BYTES + i] = in1[i] ^ bitmask1[i]; + buf2[SPX_ADDR_BYTES + i] = in2[i] ^ bitmask2[i]; + buf3[SPX_ADDR_BYTES + i] = in3[i] ^ bitmask3[i]; + } + + haraka_Sx4(out0, out1, out2, out3, SPX_N, + buf0, buf1, buf2, buf3, SPX_ADDR_BYTES + inblocks*SPX_N, + ctx); + } +} diff --git a/src/crypto/pq/_s/haraka-aesni/thash_haraka_simple.c b/src/crypto/pq/_s/haraka-aesni/thash_haraka_simple.c new file mode 120000 index 0000000..4d3c767 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/thash_haraka_simple.c @@ -0,0 +1 @@ +../ref/thash_haraka_simple.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/thash_haraka_simplex4.c b/src/crypto/pq/_s/haraka-aesni/thash_haraka_simplex4.c new file mode 100644 index 0000000..841d445 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/thash_haraka_simplex4.c @@ -0,0 +1,67 @@ +#include +#include + +#include "thashx4.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "harakax4.h" + +/** + * 4-way parallel version of thash; takes 4x as much input and output + */ +#define thashx4 SPX_NAMESPACE(thashx4) +void thashx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx4[4*8]) +{ + SPX_VLA(unsigned char, buf0, SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf1, SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf2, SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf3, SPX_ADDR_BYTES + inblocks*SPX_N); + unsigned char outbuf[32 * 4]; + unsigned char buf_tmp[64 * 4]; + + if (inblocks == 1) { + memset(buf_tmp, 0, 64 * 4); + + memcpy(buf_tmp, addrx4 + 0*8, 32); + memcpy(buf_tmp + 64, addrx4 + 1*8, 32); + memcpy(buf_tmp + 128, addrx4 + 2*8, 32); + memcpy(buf_tmp + 192, addrx4 + 3*8, 32); + + memcpy(buf_tmp + SPX_ADDR_BYTES, in0, SPX_N); + memcpy(buf_tmp + SPX_ADDR_BYTES + 64, in1, SPX_N); + memcpy(buf_tmp + SPX_ADDR_BYTES + 128, in2, SPX_N); + memcpy(buf_tmp + SPX_ADDR_BYTES + 192, in3, SPX_N); + + haraka512x4(outbuf, buf_tmp, ctx); + + memcpy(out0, outbuf, SPX_N); + memcpy(out1, outbuf + 32, SPX_N); + memcpy(out2, outbuf + 64, SPX_N); + memcpy(out3, outbuf + 96, SPX_N); + } else { + /* All other tweakable hashes*/ + memcpy(buf0, addrx4 + 0*8, 32); + memcpy(buf1, addrx4 + 1*8, 32); + memcpy(buf2, addrx4 + 2*8, 32); + memcpy(buf3, addrx4 + 3*8, 32); + + memcpy(buf0 + SPX_ADDR_BYTES, in0, inblocks * SPX_N); + memcpy(buf1 + SPX_ADDR_BYTES, in1, inblocks * SPX_N); + memcpy(buf2 + SPX_ADDR_BYTES, in2, inblocks * SPX_N); + memcpy(buf3 + SPX_ADDR_BYTES, in3, inblocks * SPX_N); + + haraka_Sx4(out0, out1, out2, out3, SPX_N, + buf0, buf1, buf2, buf3, SPX_ADDR_BYTES + inblocks*SPX_N, + ctx); + } +} diff --git a/src/crypto/pq/_s/haraka-aesni/thashx4.h b/src/crypto/pq/_s/haraka-aesni/thashx4.h new file mode 120000 index 0000000..982555e --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/thashx4.h @@ -0,0 +1 @@ +../shake-avx2/thashx4.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/utils.c b/src/crypto/pq/_s/haraka-aesni/utils.c new file mode 120000 index 0000000..e8ef6eb --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/utils.c @@ -0,0 +1 @@ +../ref/utils.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/utils.h b/src/crypto/pq/_s/haraka-aesni/utils.h new file mode 120000 index 0000000..51b0d39 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/utils.h @@ -0,0 +1 @@ +../ref/utils.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/utilsx4.c b/src/crypto/pq/_s/haraka-aesni/utilsx4.c new file mode 120000 index 0000000..ea6a5cf --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/utilsx4.c @@ -0,0 +1 @@ +../shake-avx2/utilsx4.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/utilsx4.h b/src/crypto/pq/_s/haraka-aesni/utilsx4.h new file mode 120000 index 0000000..faf4c28 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/utilsx4.h @@ -0,0 +1 @@ +../shake-avx2/utilsx4.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/wots.c b/src/crypto/pq/_s/haraka-aesni/wots.c new file mode 120000 index 0000000..07845f1 --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/wots.c @@ -0,0 +1 @@ +../shake-avx2/wots.c \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/wots.h b/src/crypto/pq/_s/haraka-aesni/wots.h new file mode 120000 index 0000000..8c327ea --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/wots.h @@ -0,0 +1 @@ +../ref/wots.h \ No newline at end of file diff --git a/src/crypto/pq/_s/haraka-aesni/wotsx4.h b/src/crypto/pq/_s/haraka-aesni/wotsx4.h new file mode 120000 index 0000000..db76d0f --- /dev/null +++ b/src/crypto/pq/_s/haraka-aesni/wotsx4.h @@ -0,0 +1 @@ +../shake-avx2/wotsx4.h \ No newline at end of file diff --git a/src/crypto/pq/_s/ref/.gitignore b/src/crypto/pq/_s/ref/.gitignore new file mode 100644 index 0000000..67bcef9 --- /dev/null +++ b/src/crypto/pq/_s/ref/.gitignore @@ -0,0 +1,5 @@ +test/* +!test/*.c +PQCsignKAT_*.rsp +PQCsignKAT_*.req +PQCgenKAT_sign diff --git a/src/crypto/pq/_s/ref/PQCgenKAT_sign.c b/src/crypto/pq/_s/ref/PQCgenKAT_sign.c new file mode 100644 index 0000000..16f41c1 --- /dev/null +++ b/src/crypto/pq/_s/ref/PQCgenKAT_sign.c @@ -0,0 +1,262 @@ + +// +// PQCgenKAT_sign.c +// +// Created by Bassham, Lawrence E (Fed) on 8/29/17. +// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved. +// +#include +#include +#include +#include +#include "rng.h" +#include "api.h" + +#define MAX_MARKER_LEN 50 + +#define KAT_SUCCESS 0 +#define KAT_FILE_OPEN_ERROR -1 +#define KAT_DATA_ERROR -3 +#define KAT_CRYPTO_FAILURE -4 + +int FindMarker(FILE *infile, const char *marker); +int ReadHex(FILE *infile, unsigned char *A, int Length, char *str); +void fprintBstr(FILE *fp, char *S, unsigned char *A, unsigned long long L); + +char AlgName[] = "My Alg Name"; + +int +main(void) +{ + char fn_req[32], fn_rsp[32]; + FILE *fp_req, *fp_rsp; + unsigned char seed[48]; + unsigned char msg[3300]; + unsigned char entropy_input[48]; + unsigned char *m, *sm, *m1; + unsigned long long mlen, smlen, mlen1; + int count; + int done; + unsigned char pk[CRYPTO_PUBLICKEYBYTES], sk[CRYPTO_SECRETKEYBYTES]; + int ret_val; + + // Create the REQUEST file + sprintf(fn_req, "PQCsignKAT_%d.req", CRYPTO_SECRETKEYBYTES); + if ( (fp_req = fopen(fn_req, "w")) == NULL ) { + printf("Couldn't open <%s> for write\n", fn_req); + return KAT_FILE_OPEN_ERROR; + } + sprintf(fn_rsp, "PQCsignKAT_%d.rsp", CRYPTO_SECRETKEYBYTES); + if ( (fp_rsp = fopen(fn_rsp, "w")) == NULL ) { + printf("Couldn't open <%s> for write\n", fn_rsp); + return KAT_FILE_OPEN_ERROR; + } + + for (int i=0; i<48; i++) + entropy_input[i] = (unsigned char)i; + + randombytes_init(entropy_input, NULL); + for (int i=0; i<100; i++) { + fprintf(fp_req, "count = %d\n", i); + randombytes(seed, 48); + fprintBstr(fp_req, "seed = ", seed, 48); + mlen = (unsigned long long int)(33*(i+1)); + fprintf(fp_req, "mlen = %llu\n", mlen); + randombytes(msg, mlen); + fprintBstr(fp_req, "msg = ", msg, mlen); + fprintf(fp_req, "pk =\n"); + fprintf(fp_req, "sk =\n"); + fprintf(fp_req, "smlen =\n"); + fprintf(fp_req, "sm =\n\n"); + } + fclose(fp_req); + + //Create the RESPONSE file based on what's in the REQUEST file + if ( (fp_req = fopen(fn_req, "r")) == NULL ) { + printf("Couldn't open <%s> for read\n", fn_req); + return KAT_FILE_OPEN_ERROR; + } + + fprintf(fp_rsp, "# %s\n\n", CRYPTO_ALGNAME); + done = 0; + do { + if ( FindMarker(fp_req, "count = ") ) + ret_val = fscanf(fp_req, "%d", &count); + else { + done = 1; + break; + } + fprintf(fp_rsp, "count = %d\n", count); + + if ( !ReadHex(fp_req, seed, 48, "seed = ") ) { + printf("ERROR: unable to read 'seed' from <%s>\n", fn_req); + return KAT_DATA_ERROR; + } + fprintBstr(fp_rsp, "seed = ", seed, 48); + + randombytes_init(seed, NULL); + + if ( FindMarker(fp_req, "mlen = ") ) + ret_val = fscanf(fp_req, "%llu", &mlen); + else { + printf("ERROR: unable to read 'mlen' from <%s>\n", fn_req); + return KAT_DATA_ERROR; + } + fprintf(fp_rsp, "mlen = %llu\n", mlen); + + m = (unsigned char *)calloc(mlen, sizeof(unsigned char)); + m1 = (unsigned char *)calloc(mlen+CRYPTO_BYTES, sizeof(unsigned char)); + sm = (unsigned char *)calloc(mlen+CRYPTO_BYTES, sizeof(unsigned char)); + + if ( !ReadHex(fp_req, m, (int)mlen, "msg = ") ) { + printf("ERROR: unable to read 'msg' from <%s>\n", fn_req); + return KAT_DATA_ERROR; + } + fprintBstr(fp_rsp, "msg = ", m, mlen); + + // Generate the public/private keypair + if ( (ret_val = crypto_sign_keypair(pk, sk)) != 0) { + printf("crypto_sign_keypair returned <%d>\n", ret_val); + return KAT_CRYPTO_FAILURE; + } + fprintBstr(fp_rsp, "pk = ", pk, CRYPTO_PUBLICKEYBYTES); + fprintBstr(fp_rsp, "sk = ", sk, CRYPTO_SECRETKEYBYTES); + + if ( (ret_val = crypto_sign(sm, &smlen, m, mlen, sk)) != 0) { + printf("crypto_sign returned <%d>\n", ret_val); + return KAT_CRYPTO_FAILURE; + } + fprintf(fp_rsp, "smlen = %llu\n", smlen); + fprintBstr(fp_rsp, "sm = ", sm, smlen); + fprintf(fp_rsp, "\n"); + + if ( (ret_val = crypto_sign_open(m1, &mlen1, sm, smlen, pk)) != 0) { + printf("crypto_sign_open returned <%d>\n", ret_val); + return KAT_CRYPTO_FAILURE; + } + + if ( mlen != mlen1 ) { + printf("crypto_sign_open returned bad 'mlen': Got <%llu>, expected <%llu>\n", mlen1, mlen); + return KAT_CRYPTO_FAILURE; + } + + if ( memcmp(m, m1, mlen) ) { + printf("crypto_sign_open returned bad 'm' value\n"); + return KAT_CRYPTO_FAILURE; + } + + free(m); + free(m1); + free(sm); + + } while ( !done ); + + fclose(fp_req); + fclose(fp_rsp); + + return KAT_SUCCESS; +} + +// +// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.) +// +int +FindMarker(FILE *infile, const char *marker) +{ + char line[MAX_MARKER_LEN]; + size_t i, len; + int curr_line; + + len = strlen(marker); + if ( len > MAX_MARKER_LEN-1 ) + len = MAX_MARKER_LEN-1; + + for ( i=0; i= '0') && (ch <= '9') ) + ich = (unsigned char)(ch - '0'); + else if ( (ch >= 'A') && (ch <= 'F') ) + ich = (unsigned char)(ch - 'A' + 10); + else if ( (ch >= 'a') && (ch <= 'f') ) + ich = (unsigned char)(ch - 'a' + 10); + else // shouldn't ever get here + ich = 0; + + for ( i=0; i> 4)); + A[Length-1] = (unsigned char)((A[Length-1] << 4) | ich); + } + else + return 0; + + return 1; +} + +void +fprintBstr(FILE *fp, char *S, unsigned char *A, unsigned long long L) +{ + unsigned long long i; + + fprintf(fp, "%s", S); + + for ( i=0; i +#include + +#include "address.h" +#include "params.h" +#include "utils.h" + +/* + * Specify which level of Merkle tree (the "layer") we're working on + */ +void set_layer_addr(uint32_t addr[8], uint32_t layer) +{ + ((unsigned char *)addr)[SPX_OFFSET_LAYER] = (unsigned char)layer; +} + +/* + * Specify which Merkle tree within the level (the "tree address") we're working on + */ +void set_tree_addr(uint32_t addr[8], uint64_t tree) +{ +#if (SPX_TREE_HEIGHT * (SPX_D - 1)) > 64 + #error Subtree addressing is currently limited to at most 2^64 trees +#endif + ull_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_TREE], 8, tree ); +} + +/* + * Specify the reason we'll use this address structure for, that is, what + * hash will we compute with it. This is used so that unrelated types of + * hashes don't accidentally get the same address structure. The type will be + * one of the SPX_ADDR_TYPE constants + */ +void set_type(uint32_t addr[8], uint32_t type) +{ + ((unsigned char *)addr)[SPX_OFFSET_TYPE] = (unsigned char)type; +} + +/* + * Copy the layer and tree fields of the address structure. This is used + * when we're doing multiple types of hashes within the same Merkle tree + */ +void copy_subtree_addr(uint32_t out[8], const uint32_t in[8]) +{ + memcpy( out, in, SPX_OFFSET_TREE+8 ); +} + +/* These functions are used for OTS addresses. */ + +/* + * Specify which Merkle leaf we're working on; that is, which OTS keypair + * we're talking about. + */ +void set_keypair_addr(uint32_t addr[8], uint32_t keypair) +{ + u32_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_KP_ADDR], keypair); +} + +/* + * Copy the layer, tree and keypair fields of the address structure. This is + * used when we're doing multiple things within the same OTS keypair + */ +void copy_keypair_addr(uint32_t out[8], const uint32_t in[8]) +{ + memcpy( out, in, SPX_OFFSET_TREE+8 ); + memcpy( (unsigned char *)out + SPX_OFFSET_KP_ADDR, (unsigned char *)in + SPX_OFFSET_KP_ADDR, 4); +} + +/* + * Specify which Merkle chain within the OTS we're working with + * (the chain address) + */ +void set_chain_addr(uint32_t addr[8], uint32_t chain) +{ + ((unsigned char *)addr)[SPX_OFFSET_CHAIN_ADDR] = (unsigned char)chain; +} + +/* + * Specify where in the Merkle chain we are +* (the hash address) + */ +void set_hash_addr(uint32_t addr[8], uint32_t hash) +{ + ((unsigned char *)addr)[SPX_OFFSET_HASH_ADDR] = (unsigned char)hash; +} + +/* These functions are used for all hash tree addresses (including FORS). */ + +/* + * Specify the height of the node in the Merkle/FORS tree we are in + * (the tree height) + */ +void set_tree_height(uint32_t addr[8], uint32_t tree_height) +{ + ((unsigned char *)addr)[SPX_OFFSET_TREE_HGT] = (unsigned char)tree_height; +} + +/* + * Specify the distance from the left edge of the node in the Merkle/FORS tree + * (the tree index) + */ +void set_tree_index(uint32_t addr[8], uint32_t tree_index) +{ + u32_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_TREE_INDEX], tree_index ); +} diff --git a/src/crypto/pq/_s/ref/address.h b/src/crypto/pq/_s/ref/address.h new file mode 100644 index 0000000..49f8d66 --- /dev/null +++ b/src/crypto/pq/_s/ref/address.h @@ -0,0 +1,51 @@ +#ifndef SPX_ADDRESS_H +#define SPX_ADDRESS_H + +#include +#include "params.h" + +/* The hash types that are passed to set_type */ +#define SPX_ADDR_TYPE_WOTS 0 +#define SPX_ADDR_TYPE_WOTSPK 1 +#define SPX_ADDR_TYPE_HASHTREE 2 +#define SPX_ADDR_TYPE_FORSTREE 3 +#define SPX_ADDR_TYPE_FORSPK 4 +#define SPX_ADDR_TYPE_WOTSPRF 5 +#define SPX_ADDR_TYPE_FORSPRF 6 + +#define set_layer_addr SPX_NAMESPACE(set_layer_addr) +void set_layer_addr(uint32_t addr[8], uint32_t layer); + +#define set_tree_addr SPX_NAMESPACE(set_tree_addr) +void set_tree_addr(uint32_t addr[8], uint64_t tree); + +#define set_type SPX_NAMESPACE(set_type) +void set_type(uint32_t addr[8], uint32_t type); + +/* Copies the layer and tree part of one address into the other */ +#define copy_subtree_addr SPX_NAMESPACE(copy_subtree_addr) +void copy_subtree_addr(uint32_t out[8], const uint32_t in[8]); + +/* These functions are used for WOTS and FORS addresses. */ + +#define set_keypair_addr SPX_NAMESPACE(set_keypair_addr) +void set_keypair_addr(uint32_t addr[8], uint32_t keypair); + +#define set_chain_addr SPX_NAMESPACE(set_chain_addr) +void set_chain_addr(uint32_t addr[8], uint32_t chain); + +#define set_hash_addr SPX_NAMESPACE(set_hash_addr) +void set_hash_addr(uint32_t addr[8], uint32_t hash); + +#define copy_keypair_addr SPX_NAMESPACE(copy_keypair_addr) +void copy_keypair_addr(uint32_t out[8], const uint32_t in[8]); + +/* These functions are used for all hash tree addresses (including FORS). */ + +#define set_tree_height SPX_NAMESPACE(set_tree_height) +void set_tree_height(uint32_t addr[8], uint32_t tree_height); + +#define set_tree_index SPX_NAMESPACE(set_tree_index) +void set_tree_index(uint32_t addr[8], uint32_t tree_index); + +#endif diff --git a/src/crypto/pq/_s/ref/api.h b/src/crypto/pq/_s/ref/api.h new file mode 100644 index 0000000..d57a148 --- /dev/null +++ b/src/crypto/pq/_s/ref/api.h @@ -0,0 +1,77 @@ +#ifndef SPX_API_H +#define SPX_API_H + +#include +#include + +#include "params.h" + +#define CRYPTO_ALGNAME "SPHINCS+" + +#define CRYPTO_SECRETKEYBYTES SPX_SK_BYTES +#define CRYPTO_PUBLICKEYBYTES SPX_PK_BYTES +#define CRYPTO_BYTES SPX_BYTES +#define CRYPTO_SEEDBYTES 3*SPX_N + +/* + * Returns the length of a secret key, in bytes + */ +unsigned long long crypto_sign_secretkeybytes(void); + +/* + * Returns the length of a public key, in bytes + */ +unsigned long long crypto_sign_publickeybytes(void); + +/* + * Returns the length of a signature, in bytes + */ +unsigned long long crypto_sign_bytes(void); + +/* + * Returns the length of the seed required to generate a key pair, in bytes + */ +unsigned long long crypto_sign_seedbytes(void); + +/* + * Generates a SPHINCS+ key pair given a seed. + * Format sk: [SK_SEED || SK_PRF || PUB_SEED || root] + * Format pk: [root || PUB_SEED] + */ +int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, + const unsigned char *seed); + +/* + * Generates a SPHINCS+ key pair. + * Format sk: [SK_SEED || SK_PRF || PUB_SEED || root] + * Format pk: [root || PUB_SEED] + */ +int crypto_sign_keypair(unsigned char *pk, unsigned char *sk); + +/** + * Returns an array containing a detached signature. + */ +int crypto_sign_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, const uint8_t *sk); + +/** + * Verifies a detached signature and message under a given public key. + */ +int crypto_sign_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, const uint8_t *pk); + +/** + * Returns an array containing the signature followed by the message. + */ +int crypto_sign(unsigned char *sm, unsigned long long *smlen, + const unsigned char *m, unsigned long long mlen, + const unsigned char *sk); + +/** + * Verifies a given signature-message pair under a given public key. + */ +int crypto_sign_open(unsigned char *m, unsigned long long *mlen, + const unsigned char *sm, unsigned long long smlen, + const unsigned char *pk); + +#endif diff --git a/src/crypto/pq/_s/ref/context.h b/src/crypto/pq/_s/ref/context.h new file mode 100644 index 0000000..aded564 --- /dev/null +++ b/src/crypto/pq/_s/ref/context.h @@ -0,0 +1,28 @@ +#ifndef SPX_CONTEXT_H +#define SPX_CONTEXT_H + +#include + +#include "params.h" + +typedef struct { + uint8_t pub_seed[SPX_N]; + uint8_t sk_seed[SPX_N]; + +#ifdef SPX_SHA2 + // sha256 state that absorbed pub_seed + uint8_t state_seeded[40]; + +# if SPX_SHA512 + // sha512 state that absorbed pub_seed + uint8_t state_seeded_512[72]; +# endif +#endif + +#ifdef SPX_HARAKA + uint64_t tweaked512_rc64[10][8]; + uint32_t tweaked256_rc32[10][8]; +#endif +} spx_ctx; + +#endif diff --git a/src/crypto/pq/_s/ref/fips202.c b/src/crypto/pq/_s/ref/fips202.c new file mode 100644 index 0000000..8262d91 --- /dev/null +++ b/src/crypto/pq/_s/ref/fips202.c @@ -0,0 +1,598 @@ +/* Based on the public domain implementation in + * crypto_hash/keccakc512/simple/ from http://bench.cr.yp.to/supercop.html + * by Ronny Van Keer + * and the public domain "TweetFips202" implementation + * from https://twitter.com/tweetfips202 + * by Gilles Van Assche, Daniel J. Bernstein, and Peter Schwabe */ + +#include +#include + +#include "fips202.h" + +#define NROUNDS 24 +#define ROL(a, offset) (((a) << (offset)) ^ ((a) >> (64 - (offset)))) + +/************************************************* + * Name: load64 + * + * Description: Load 8 bytes into uint64_t in little-endian order + * + * Arguments: - const uint8_t *x: pointer to input byte array + * + * Returns the loaded 64-bit unsigned integer + **************************************************/ +static uint64_t load64(const uint8_t *x) { + uint64_t r = 0; + for (size_t i = 0; i < 8; ++i) { + r |= (uint64_t)x[i] << 8 * i; + } + + return r; +} + +/************************************************* + * Name: store64 + * + * Description: Store a 64-bit integer to a byte array in little-endian order + * + * Arguments: - uint8_t *x: pointer to the output byte array + * - uint64_t u: input 64-bit unsigned integer + **************************************************/ +static void store64(uint8_t *x, uint64_t u) { + for (size_t i = 0; i < 8; ++i) { + x[i] = (uint8_t) (u >> 8 * i); + } +} + +/* Keccak round constants */ +static const uint64_t KeccakF_RoundConstants[NROUNDS] = { + 0x0000000000000001ULL, 0x0000000000008082ULL, + 0x800000000000808aULL, 0x8000000080008000ULL, + 0x000000000000808bULL, 0x0000000080000001ULL, + 0x8000000080008081ULL, 0x8000000000008009ULL, + 0x000000000000008aULL, 0x0000000000000088ULL, + 0x0000000080008009ULL, 0x000000008000000aULL, + 0x000000008000808bULL, 0x800000000000008bULL, + 0x8000000000008089ULL, 0x8000000000008003ULL, + 0x8000000000008002ULL, 0x8000000000000080ULL, + 0x000000000000800aULL, 0x800000008000000aULL, + 0x8000000080008081ULL, 0x8000000000008080ULL, + 0x0000000080000001ULL, 0x8000000080008008ULL +}; + +/************************************************* + * Name: KeccakF1600_StatePermute + * + * Description: The Keccak F1600 Permutation + * + * Arguments: - uint64_t *state: pointer to input/output Keccak state + **************************************************/ +static void KeccakF1600_StatePermute(uint64_t *state) { + int round; + + uint64_t Aba, Abe, Abi, Abo, Abu; + uint64_t Aga, Age, Agi, Ago, Agu; + uint64_t Aka, Ake, Aki, Ako, Aku; + uint64_t Ama, Ame, Ami, Amo, Amu; + uint64_t Asa, Ase, Asi, Aso, Asu; + uint64_t BCa, BCe, BCi, BCo, BCu; + uint64_t Da, De, Di, Do, Du; + uint64_t Eba, Ebe, Ebi, Ebo, Ebu; + uint64_t Ega, Ege, Egi, Ego, Egu; + uint64_t Eka, Eke, Eki, Eko, Eku; + uint64_t Ema, Eme, Emi, Emo, Emu; + uint64_t Esa, Ese, Esi, Eso, Esu; + + // copyFromState(A, state) + Aba = state[0]; + Abe = state[1]; + Abi = state[2]; + Abo = state[3]; + Abu = state[4]; + Aga = state[5]; + Age = state[6]; + Agi = state[7]; + Ago = state[8]; + Agu = state[9]; + Aka = state[10]; + Ake = state[11]; + Aki = state[12]; + Ako = state[13]; + Aku = state[14]; + Ama = state[15]; + Ame = state[16]; + Ami = state[17]; + Amo = state[18]; + Amu = state[19]; + Asa = state[20]; + Ase = state[21]; + Asi = state[22]; + Aso = state[23]; + Asu = state[24]; + + for (round = 0; round < NROUNDS; round += 2) { + // prepareTheta + BCa = Aba ^ Aga ^ Aka ^ Ama ^ Asa; + BCe = Abe ^ Age ^ Ake ^ Ame ^ Ase; + BCi = Abi ^ Agi ^ Aki ^ Ami ^ Asi; + BCo = Abo ^ Ago ^ Ako ^ Amo ^ Aso; + BCu = Abu ^ Agu ^ Aku ^ Amu ^ Asu; + + // thetaRhoPiChiIotaPrepareTheta(round , A, E) + Da = BCu ^ ROL(BCe, 1); + De = BCa ^ ROL(BCi, 1); + Di = BCe ^ ROL(BCo, 1); + Do = BCi ^ ROL(BCu, 1); + Du = BCo ^ ROL(BCa, 1); + + Aba ^= Da; + BCa = Aba; + Age ^= De; + BCe = ROL(Age, 44); + Aki ^= Di; + BCi = ROL(Aki, 43); + Amo ^= Do; + BCo = ROL(Amo, 21); + Asu ^= Du; + BCu = ROL(Asu, 14); + Eba = BCa ^ ((~BCe) & BCi); + Eba ^= KeccakF_RoundConstants[round]; + Ebe = BCe ^ ((~BCi) & BCo); + Ebi = BCi ^ ((~BCo) & BCu); + Ebo = BCo ^ ((~BCu) & BCa); + Ebu = BCu ^ ((~BCa) & BCe); + + Abo ^= Do; + BCa = ROL(Abo, 28); + Agu ^= Du; + BCe = ROL(Agu, 20); + Aka ^= Da; + BCi = ROL(Aka, 3); + Ame ^= De; + BCo = ROL(Ame, 45); + Asi ^= Di; + BCu = ROL(Asi, 61); + Ega = BCa ^ ((~BCe) & BCi); + Ege = BCe ^ ((~BCi) & BCo); + Egi = BCi ^ ((~BCo) & BCu); + Ego = BCo ^ ((~BCu) & BCa); + Egu = BCu ^ ((~BCa) & BCe); + + Abe ^= De; + BCa = ROL(Abe, 1); + Agi ^= Di; + BCe = ROL(Agi, 6); + Ako ^= Do; + BCi = ROL(Ako, 25); + Amu ^= Du; + BCo = ROL(Amu, 8); + Asa ^= Da; + BCu = ROL(Asa, 18); + Eka = BCa ^ ((~BCe) & BCi); + Eke = BCe ^ ((~BCi) & BCo); + Eki = BCi ^ ((~BCo) & BCu); + Eko = BCo ^ ((~BCu) & BCa); + Eku = BCu ^ ((~BCa) & BCe); + + Abu ^= Du; + BCa = ROL(Abu, 27); + Aga ^= Da; + BCe = ROL(Aga, 36); + Ake ^= De; + BCi = ROL(Ake, 10); + Ami ^= Di; + BCo = ROL(Ami, 15); + Aso ^= Do; + BCu = ROL(Aso, 56); + Ema = BCa ^ ((~BCe) & BCi); + Eme = BCe ^ ((~BCi) & BCo); + Emi = BCi ^ ((~BCo) & BCu); + Emo = BCo ^ ((~BCu) & BCa); + Emu = BCu ^ ((~BCa) & BCe); + + Abi ^= Di; + BCa = ROL(Abi, 62); + Ago ^= Do; + BCe = ROL(Ago, 55); + Aku ^= Du; + BCi = ROL(Aku, 39); + Ama ^= Da; + BCo = ROL(Ama, 41); + Ase ^= De; + BCu = ROL(Ase, 2); + Esa = BCa ^ ((~BCe) & BCi); + Ese = BCe ^ ((~BCi) & BCo); + Esi = BCi ^ ((~BCo) & BCu); + Eso = BCo ^ ((~BCu) & BCa); + Esu = BCu ^ ((~BCa) & BCe); + + // prepareTheta + BCa = Eba ^ Ega ^ Eka ^ Ema ^ Esa; + BCe = Ebe ^ Ege ^ Eke ^ Eme ^ Ese; + BCi = Ebi ^ Egi ^ Eki ^ Emi ^ Esi; + BCo = Ebo ^ Ego ^ Eko ^ Emo ^ Eso; + BCu = Ebu ^ Egu ^ Eku ^ Emu ^ Esu; + + // thetaRhoPiChiIotaPrepareTheta(round+1, E, A) + Da = BCu ^ ROL(BCe, 1); + De = BCa ^ ROL(BCi, 1); + Di = BCe ^ ROL(BCo, 1); + Do = BCi ^ ROL(BCu, 1); + Du = BCo ^ ROL(BCa, 1); + + Eba ^= Da; + BCa = Eba; + Ege ^= De; + BCe = ROL(Ege, 44); + Eki ^= Di; + BCi = ROL(Eki, 43); + Emo ^= Do; + BCo = ROL(Emo, 21); + Esu ^= Du; + BCu = ROL(Esu, 14); + Aba = BCa ^ ((~BCe) & BCi); + Aba ^= KeccakF_RoundConstants[round + 1]; + Abe = BCe ^ ((~BCi) & BCo); + Abi = BCi ^ ((~BCo) & BCu); + Abo = BCo ^ ((~BCu) & BCa); + Abu = BCu ^ ((~BCa) & BCe); + + Ebo ^= Do; + BCa = ROL(Ebo, 28); + Egu ^= Du; + BCe = ROL(Egu, 20); + Eka ^= Da; + BCi = ROL(Eka, 3); + Eme ^= De; + BCo = ROL(Eme, 45); + Esi ^= Di; + BCu = ROL(Esi, 61); + Aga = BCa ^ ((~BCe) & BCi); + Age = BCe ^ ((~BCi) & BCo); + Agi = BCi ^ ((~BCo) & BCu); + Ago = BCo ^ ((~BCu) & BCa); + Agu = BCu ^ ((~BCa) & BCe); + + Ebe ^= De; + BCa = ROL(Ebe, 1); + Egi ^= Di; + BCe = ROL(Egi, 6); + Eko ^= Do; + BCi = ROL(Eko, 25); + Emu ^= Du; + BCo = ROL(Emu, 8); + Esa ^= Da; + BCu = ROL(Esa, 18); + Aka = BCa ^ ((~BCe) & BCi); + Ake = BCe ^ ((~BCi) & BCo); + Aki = BCi ^ ((~BCo) & BCu); + Ako = BCo ^ ((~BCu) & BCa); + Aku = BCu ^ ((~BCa) & BCe); + + Ebu ^= Du; + BCa = ROL(Ebu, 27); + Ega ^= Da; + BCe = ROL(Ega, 36); + Eke ^= De; + BCi = ROL(Eke, 10); + Emi ^= Di; + BCo = ROL(Emi, 15); + Eso ^= Do; + BCu = ROL(Eso, 56); + Ama = BCa ^ ((~BCe) & BCi); + Ame = BCe ^ ((~BCi) & BCo); + Ami = BCi ^ ((~BCo) & BCu); + Amo = BCo ^ ((~BCu) & BCa); + Amu = BCu ^ ((~BCa) & BCe); + + Ebi ^= Di; + BCa = ROL(Ebi, 62); + Ego ^= Do; + BCe = ROL(Ego, 55); + Eku ^= Du; + BCi = ROL(Eku, 39); + Ema ^= Da; + BCo = ROL(Ema, 41); + Ese ^= De; + BCu = ROL(Ese, 2); + Asa = BCa ^ ((~BCe) & BCi); + Ase = BCe ^ ((~BCi) & BCo); + Asi = BCi ^ ((~BCo) & BCu); + Aso = BCo ^ ((~BCu) & BCa); + Asu = BCu ^ ((~BCa) & BCe); + } + + // copyToState(state, A) + state[0] = Aba; + state[1] = Abe; + state[2] = Abi; + state[3] = Abo; + state[4] = Abu; + state[5] = Aga; + state[6] = Age; + state[7] = Agi; + state[8] = Ago; + state[9] = Agu; + state[10] = Aka; + state[11] = Ake; + state[12] = Aki; + state[13] = Ako; + state[14] = Aku; + state[15] = Ama; + state[16] = Ame; + state[17] = Ami; + state[18] = Amo; + state[19] = Amu; + state[20] = Asa; + state[21] = Ase; + state[22] = Asi; + state[23] = Aso; + state[24] = Asu; +} + +/************************************************* + * Name: keccak_absorb + * + * Description: Absorb step of Keccak; + * non-incremental, starts by zeroeing the state. + * + * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + * - const uint8_t *m: pointer to input to be absorbed into s + * - size_t mlen: length of input in bytes + * - uint8_t p: domain-separation byte for different + * Keccak-derived functions + **************************************************/ +static void keccak_absorb(uint64_t *s, uint32_t r, const uint8_t *m, + size_t mlen, uint8_t p) { + size_t i; + uint8_t t[200]; + + /* Zero state */ + for (i = 0; i < 25; ++i) { + s[i] = 0; + } + + while (mlen >= r) { + for (i = 0; i < r / 8; ++i) { + s[i] ^= load64(m + 8 * i); + } + + KeccakF1600_StatePermute(s); + mlen -= r; + m += r; + } + + for (i = 0; i < r; ++i) { + t[i] = 0; + } + for (i = 0; i < mlen; ++i) { + t[i] = m[i]; + } + t[i] = p; + t[r - 1] |= 128; + for (i = 0; i < r / 8; ++i) { + s[i] ^= load64(t + 8 * i); + } +} + +/************************************************* + * Name: keccak_squeezeblocks + * + * Description: Squeeze step of Keccak. Squeezes full blocks of r bytes each. + * Modifies the state. Can be called multiple times to keep + * squeezing, i.e., is incremental. + * + * Arguments: - uint8_t *h: pointer to output blocks + * - size_t nblocks: number of blocks to be + * squeezed (written to h) + * - uint64_t *s: pointer to input/output Keccak state + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + **************************************************/ +static void keccak_squeezeblocks(uint8_t *h, size_t nblocks, + uint64_t *s, uint32_t r) { + while (nblocks > 0) { + KeccakF1600_StatePermute(s); + for (size_t i = 0; i < (r >> 3); i++) { + store64(h + 8 * i, s[i]); + } + h += r; + nblocks--; + } +} + +/************************************************* + * Name: keccak_inc_init + * + * Description: Initializes the incremental Keccak state to zero. + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + **************************************************/ +static void keccak_inc_init(uint64_t *s_inc) { + size_t i; + + for (i = 0; i < 25; ++i) { + s_inc[i] = 0; + } + s_inc[25] = 0; +} + +/************************************************* + * Name: keccak_inc_absorb + * + * Description: Incremental keccak absorb + * Preceded by keccak_inc_init, succeeded by keccak_inc_finalize + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + * - const uint8_t *m: pointer to input to be absorbed into s + * - size_t mlen: length of input in bytes + **************************************************/ +static void keccak_inc_absorb(uint64_t *s_inc, uint32_t r, const uint8_t *m, + size_t mlen) { + size_t i; + + /* Recall that s_inc[25] is the non-absorbed bytes xored into the state */ + while (mlen + s_inc[25] >= r) { + for (i = 0; i < r - s_inc[25]; i++) { + /* Take the i'th byte from message + xor with the s_inc[25] + i'th byte of the state; little-endian */ + s_inc[(s_inc[25] + i) >> 3] ^= (uint64_t)m[i] << (8 * ((s_inc[25] + i) & 0x07)); + } + mlen -= (size_t)(r - s_inc[25]); + m += r - s_inc[25]; + s_inc[25] = 0; + + KeccakF1600_StatePermute(s_inc); + } + + for (i = 0; i < mlen; i++) { + s_inc[(s_inc[25] + i) >> 3] ^= (uint64_t)m[i] << (8 * ((s_inc[25] + i) & 0x07)); + } + s_inc[25] += mlen; +} + +/************************************************* + * Name: keccak_inc_finalize + * + * Description: Finalizes Keccak absorb phase, prepares for squeezing + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + * - uint8_t p: domain-separation byte for different + * Keccak-derived functions + **************************************************/ +static void keccak_inc_finalize(uint64_t *s_inc, uint32_t r, uint8_t p) { + /* After keccak_inc_absorb, we are guaranteed that s_inc[25] < r, + so we can always use one more byte for p in the current state. */ + s_inc[s_inc[25] >> 3] ^= (uint64_t)p << (8 * (s_inc[25] & 0x07)); + s_inc[(r - 1) >> 3] ^= (uint64_t)128 << (8 * ((r - 1) & 0x07)); + s_inc[25] = 0; +} + +/************************************************* + * Name: keccak_inc_squeeze + * + * Description: Incremental Keccak squeeze; can be called on byte-level + * + * Arguments: - uint8_t *h: pointer to output bytes + * - size_t outlen: number of bytes to be squeezed + * - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + **************************************************/ +static void keccak_inc_squeeze(uint8_t *h, size_t outlen, + uint64_t *s_inc, uint32_t r) { + size_t i; + + /* First consume any bytes we still have sitting around */ + for (i = 0; i < outlen && i < s_inc[25]; i++) { + /* There are s_inc[25] bytes left, so r - s_inc[25] is the first + available byte. We consume from there, i.e., up to r. */ + h[i] = (uint8_t)(s_inc[(r - s_inc[25] + i) >> 3] >> (8 * ((r - s_inc[25] + i) & 0x07))); + } + h += i; + outlen -= i; + s_inc[25] -= i; + + /* Then squeeze the remaining necessary blocks */ + while (outlen > 0) { + KeccakF1600_StatePermute(s_inc); + + for (i = 0; i < outlen && i < r; i++) { + h[i] = (uint8_t)(s_inc[i >> 3] >> (8 * (i & 0x07))); + } + h += i; + outlen -= i; + s_inc[25] = r - i; + } +} + +void shake256_inc_init(uint64_t *s_inc) { + keccak_inc_init(s_inc); +} + +void shake256_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen) { + keccak_inc_absorb(s_inc, SHAKE256_RATE, input, inlen); +} + +void shake256_inc_finalize(uint64_t *s_inc) { + keccak_inc_finalize(s_inc, SHAKE256_RATE, 0x1F); +} + +void shake256_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc) { + keccak_inc_squeeze(output, outlen, s_inc, SHAKE256_RATE); +} + +/************************************************* + * Name: shake256_absorb + * + * Description: Absorb step of the SHAKE256 XOF. + * non-incremental, starts by zeroeing the state. + * + * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state + * - const uint8_t *input: pointer to input to be absorbed + * into s + * - size_t inlen: length of input in bytes + **************************************************/ +void shake256_absorb(uint64_t *s, const uint8_t *input, size_t inlen) { + keccak_absorb(s, SHAKE256_RATE, input, inlen, 0x1F); +} + +/************************************************* + * Name: shake256_squeezeblocks + * + * Description: Squeeze step of SHAKE256 XOF. Squeezes full blocks of + * SHAKE256_RATE bytes each. Modifies the state. Can be called + * multiple times to keep squeezing, i.e., is incremental. + * + * Arguments: - uint8_t *output: pointer to output blocks + * - size_t nblocks: number of blocks to be squeezed + * (written to output) + * - uint64_t *s: pointer to input/output Keccak state + **************************************************/ +void shake256_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s) { + keccak_squeezeblocks(output, nblocks, s, SHAKE256_RATE); +} + +/************************************************* + * Name: shake256 + * + * Description: SHAKE256 XOF with non-incremental API + * + * Arguments: - uint8_t *output: pointer to output + * - size_t outlen: requested output length in bytes + * - const uint8_t *input: pointer to input + * - size_t inlen: length of input in bytes + **************************************************/ +void shake256(uint8_t *output, size_t outlen, + const uint8_t *input, size_t inlen) { + size_t nblocks = outlen / SHAKE256_RATE; + uint8_t t[SHAKE256_RATE]; + uint64_t s[25]; + + shake256_absorb(s, input, inlen); + shake256_squeezeblocks(output, nblocks, s); + + output += nblocks * SHAKE256_RATE; + outlen -= nblocks * SHAKE256_RATE; + + if (outlen) { + shake256_squeezeblocks(t, 1, s); + for (size_t i = 0; i < outlen; ++i) { + output[i] = t[i]; + } + } +} diff --git a/src/crypto/pq/_s/ref/fips202.h b/src/crypto/pq/_s/ref/fips202.h new file mode 100644 index 0000000..e11cb7f --- /dev/null +++ b/src/crypto/pq/_s/ref/fips202.h @@ -0,0 +1,47 @@ +#ifndef SPX_FIPS202_H +#define SPX_FIPS202_H + +#include +#include + +#define SHAKE128_RATE 168 +#define SHAKE256_RATE 136 +#define SHA3_256_RATE 136 +#define SHA3_512_RATE 72 + +void shake128_absorb(uint64_t *s, const uint8_t *input, size_t inlen); + +void shake128_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s); + +void shake128_inc_init(uint64_t *s_inc); +void shake128_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void shake128_inc_finalize(uint64_t *s_inc); +void shake128_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc); + +void shake256_absorb(uint64_t *s, const uint8_t *input, size_t inlen); +void shake256_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s); + +void shake256_inc_init(uint64_t *s_inc); +void shake256_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void shake256_inc_finalize(uint64_t *s_inc); +void shake256_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc); + +void shake128(uint8_t *output, size_t outlen, + const uint8_t *input, size_t inlen); + +void shake256(uint8_t *output, size_t outlen, + const uint8_t *input, size_t inlen); + +void sha3_256_inc_init(uint64_t *s_inc); +void sha3_256_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void sha3_256_inc_finalize(uint8_t *output, uint64_t *s_inc); + +void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen); + +void sha3_512_inc_init(uint64_t *s_inc); +void sha3_512_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void sha3_512_inc_finalize(uint8_t *output, uint64_t *s_inc); + +void sha3_512(uint8_t *output, const uint8_t *input, size_t inlen); + +#endif diff --git a/src/crypto/pq/_s/ref/fors.c b/src/crypto/pq/_s/ref/fors.c new file mode 100644 index 0000000..e6aa4b4 --- /dev/null +++ b/src/crypto/pq/_s/ref/fors.c @@ -0,0 +1,161 @@ +#include +#include +#include + +#include "fors.h" +#include "utils.h" +#include "utilsx1.h" +#include "hash.h" +#include "thash.h" +#include "address.h" + +static void fors_gen_sk(unsigned char *sk, const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + prf_addr(sk, ctx, fors_leaf_addr); +} + +static void fors_sk_to_leaf(unsigned char *leaf, const unsigned char *sk, + const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + thash(leaf, sk, 1, ctx, fors_leaf_addr); +} + +struct fors_gen_leaf_info { + uint32_t leaf_addrx[8]; +}; + +static void fors_gen_leafx1(unsigned char *leaf, + const spx_ctx *ctx, + uint32_t addr_idx, void *info) +{ + struct fors_gen_leaf_info *fors_info = info; + uint32_t *fors_leaf_addr = fors_info->leaf_addrx; + + /* Only set the parts that the caller doesn't set */ + set_tree_index(fors_leaf_addr, addr_idx); + set_type(fors_leaf_addr, SPX_ADDR_TYPE_FORSPRF); + fors_gen_sk(leaf, ctx, fors_leaf_addr); + + set_type(fors_leaf_addr, SPX_ADDR_TYPE_FORSTREE); + fors_sk_to_leaf(leaf, leaf, + ctx, fors_leaf_addr); +} + +/** + * Interprets m as SPX_FORS_HEIGHT-bit unsigned integers. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + * Assumes indices has space for SPX_FORS_TREES integers. + */ +static void message_to_indices(uint32_t *indices, const unsigned char *m) +{ + unsigned int i, j; + unsigned int offset = 0; + + for (i = 0; i < SPX_FORS_TREES; i++) { + indices[i] = 0; + for (j = 0; j < SPX_FORS_HEIGHT; j++) { + indices[i] ^= ((m[offset >> 3] >> (offset & 0x7)) & 1u) << j; + offset++; + } + } +} + +/** + * Signs a message m, deriving the secret key from sk_seed and the FTS address. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_sign(unsigned char *sig, unsigned char *pk, + const unsigned char *m, + const spx_ctx *ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + uint32_t fors_tree_addr[8] = {0}; + struct fors_gen_leaf_info fors_info = {0}; + uint32_t *fors_leaf_addr = fors_info.leaf_addrx; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + copy_keypair_addr(fors_tree_addr, fors_addr); + copy_keypair_addr(fors_leaf_addr, fors_addr); + + copy_keypair_addr(fors_pk_addr, fors_addr); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSPRF); + + /* Include the secret key part that produces the selected leaf node. */ + fors_gen_sk(sig, ctx, fors_tree_addr); + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + sig += SPX_N; + + /* Compute the authentication path for this leaf node. */ + treehashx1(roots + i*SPX_N, sig, ctx, + indices[i], idx_offset, SPX_FORS_HEIGHT, fors_gen_leafx1, + fors_tree_addr, &fors_info); + + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} + +/** + * Derives the FORS public key from a signature. + * This can be used for verification by comparing to a known public key, or to + * subsequently verify a signature on the derived public key. The latter is the + * typical use-case when used as an FTS below an OTS in a hypertree. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *m, + const spx_ctx* ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + unsigned char leaf[SPX_N]; + uint32_t fors_tree_addr[8] = {0}; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + copy_keypair_addr(fors_tree_addr, fors_addr); + copy_keypair_addr(fors_pk_addr, fors_addr); + + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + + /* Derive the leaf from the included secret key part. */ + fors_sk_to_leaf(leaf, sig, ctx, fors_tree_addr); + sig += SPX_N; + + /* Derive the corresponding root node of this tree. */ + compute_root(roots + i*SPX_N, leaf, indices[i], idx_offset, + sig, SPX_FORS_HEIGHT, ctx, fors_tree_addr); + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} diff --git a/src/crypto/pq/_s/ref/fors.h b/src/crypto/pq/_s/ref/fors.h new file mode 100644 index 0000000..8d98017 --- /dev/null +++ b/src/crypto/pq/_s/ref/fors.h @@ -0,0 +1,32 @@ +#ifndef SPX_FORS_H +#define SPX_FORS_H + +#include + +#include "params.h" +#include "context.h" + +/** + * Signs a message m, deriving the secret key from sk_seed and the FTS address. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +#define fors_sign SPX_NAMESPACE(fors_sign) +void fors_sign(unsigned char *sig, unsigned char *pk, + const unsigned char *m, + const spx_ctx* ctx, + const uint32_t fors_addr[8]); + +/** + * Derives the FORS public key from a signature. + * This can be used for verification by comparing to a known public key, or to + * subsequently verify a signature on the derived public key. The latter is the + * typical use-case when used as an FTS below an OTS in a hypertree. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +#define fors_pk_from_sig SPX_NAMESPACE(fors_pk_from_sig) +void fors_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *m, + const spx_ctx* ctx, + const uint32_t fors_addr[8]); + +#endif diff --git a/src/crypto/pq/_s/ref/haraka.c b/src/crypto/pq/_s/ref/haraka.c new file mode 100644 index 0000000..b9046f2 --- /dev/null +++ b/src/crypto/pq/_s/ref/haraka.c @@ -0,0 +1,965 @@ +/* + * Constant time implementation of the Haraka hash function. + * + * The bit-sliced implementation of the AES round functions are + * based on the AES implementation in BearSSL written + * by Thomas Pornin , licensed as follows: + * + * Copyright (c) 2016 Thomas Pornin + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include + +#include "haraka.h" +#include "utils.h" + +#define HARAKAS_RATE 32 + +static const uint64_t haraka512_rc64[10][8] = { + {0x24cf0ab9086f628b, 0xbdd6eeecc83b8382, 0xd96fb0306cdad0a7, 0xaace082ac8f95f89, 0x449d8e8870d7041f, 0x49bb2f80b2b3e2f8, 0x0569ae98d93bb258, 0x23dc9691e7d6a4b1}, + {0xd8ba10ede0fe5b6e, 0x7ecf7dbe424c7b8e, 0x6ea9949c6df62a31, 0xbf3f3c97ec9c313e, 0x241d03a196a1861e, 0xead3a51116e5a2ea, 0x77d479fcad9574e3, 0x18657a1af894b7a0}, + {0x10671e1a7f595522, 0xd9a00ff675d28c7b, 0x2f1edf0d2b9ba661, 0xb8ff58b8e3de45f9, 0xee29261da9865c02, 0xd1532aa4b50bdf43, 0x8bf858159b231bb1, 0xdf17439d22d4f599}, + {0xdd4b2f0870b918c0, 0x757a81f3b39b1bb6, 0x7a5c556898952e3f, 0x7dd70a16d915d87a, 0x3ae61971982b8301, 0xc3ab319e030412be, 0x17c0033ac094a8cb, 0x5a0630fc1a8dc4ef}, + {0x17708988c1632f73, 0xf92ddae090b44f4f, 0x11ac0285c43aa314, 0x509059941936b8ba, 0xd03e152fa2ce9b69, 0x3fbcbcb63a32998b, 0x6204696d692254f7, 0x915542ed93ec59b4}, + {0xf4ed94aa8879236e, 0xff6cb41cd38e03c0, 0x069b38602368aeab, 0x669495b820f0ddba, 0xf42013b1b8bf9e3d, 0xcf935efe6439734d, 0xbc1dcf42ca29e3f8, 0x7e6d3ed29f78ad67}, + {0xf3b0f6837ffcddaa, 0x3a76faef934ddf41, 0xcec7ae583a9c8e35, 0xe4dd18c68f0260af, 0x2c0e5df1ad398eaa, 0x478df5236ae22e8c, 0xfb944c46fe865f39, 0xaa48f82f028132ba}, + {0x231b9ae2b76aca77, 0x292a76a712db0b40, 0x5850625dc8134491, 0x73137dd469810fb5, 0x8a12a6a202a474fd, 0xd36fd9daa78bdb80, 0xb34c5e733505706f, 0xbaf1cdca818d9d96}, + {0x2e99781335e8c641, 0xbddfe5cce47d560e, 0xf74e9bf32e5e040c, 0x1d7a709d65996be9, 0x670df36a9cf66cdd, 0xd05ef84a176a2875, 0x0f888e828cb1c44e, 0x1a79e9c9727b052c}, + {0x83497348628d84de, 0x2e9387d51f22a754, 0xb000068da2f852d6, 0x378c9e1190fd6fe5, 0x870027c316de7293, 0xe51a9d4462e047bb, 0x90ecf7f8c6251195, 0x655953bfbed90a9c}, +}; + +static inline uint32_t br_dec32le(const unsigned char *src) +{ + return (uint32_t)src[0] + | ((uint32_t)src[1] << 8) + | ((uint32_t)src[2] << 16) + | ((uint32_t)src[3] << 24); +} + +static void br_range_dec32le(uint32_t *v, size_t num, const unsigned char *src) +{ + while (num-- > 0) { + *v ++ = br_dec32le(src); + src += 4; + } +} + +static inline void br_enc32le(unsigned char *dst, uint32_t x) +{ + dst[0] = (unsigned char)x; + dst[1] = (unsigned char)(x >> 8); + dst[2] = (unsigned char)(x >> 16); + dst[3] = (unsigned char)(x >> 24); +} + + +static void br_range_enc32le(unsigned char *dst, const uint32_t *v, size_t num) +{ + while (num-- > 0) { + br_enc32le(dst, *v ++); + dst += 4; + } +} + +static void br_aes_ct64_bitslice_Sbox(uint64_t *q) { + /* + * This S-box implementation is a straightforward translation of + * the circuit described by Boyar and Peralta in "A new + * combinational logic minimization technique with applications + * to cryptology" (https://eprint.iacr.org/2009/191.pdf). + * + * Note that variables x* (input) and s* (output) are numbered + * in "reverse" order (x0 is the high bit, x7 is the low bit). + */ + + uint64_t x0, x1, x2, x3, x4, x5, x6, x7; + uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9; + uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19; + uint64_t y20, y21; + uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9; + uint64_t z10, z11, z12, z13, z14, z15, z16, z17; + uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9; + uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19; + uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29; + uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39; + uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49; + uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59; + uint64_t t60, t61, t62, t63, t64, t65, t66, t67; + uint64_t s0, s1, s2, s3, s4, s5, s6, s7; + + x0 = q[7]; + x1 = q[6]; + x2 = q[5]; + x3 = q[4]; + x4 = q[3]; + x5 = q[2]; + x6 = q[1]; + x7 = q[0]; + + /* + * Top linear transformation. + */ + y14 = x3 ^ x5; + y13 = x0 ^ x6; + y9 = x0 ^ x3; + y8 = x0 ^ x5; + t0 = x1 ^ x2; + y1 = t0 ^ x7; + y4 = y1 ^ x3; + y12 = y13 ^ y14; + y2 = y1 ^ x0; + y5 = y1 ^ x6; + y3 = y5 ^ y8; + t1 = x4 ^ y12; + y15 = t1 ^ x5; + y20 = t1 ^ x1; + y6 = y15 ^ x7; + y10 = y15 ^ t0; + y11 = y20 ^ y9; + y7 = x7 ^ y11; + y17 = y10 ^ y11; + y19 = y10 ^ y8; + y16 = t0 ^ y11; + y21 = y13 ^ y16; + y18 = x0 ^ y16; + + /* + * Non-linear section. + */ + t2 = y12 & y15; + t3 = y3 & y6; + t4 = t3 ^ t2; + t5 = y4 & x7; + t6 = t5 ^ t2; + t7 = y13 & y16; + t8 = y5 & y1; + t9 = t8 ^ t7; + t10 = y2 & y7; + t11 = t10 ^ t7; + t12 = y9 & y11; + t13 = y14 & y17; + t14 = t13 ^ t12; + t15 = y8 & y10; + t16 = t15 ^ t12; + t17 = t4 ^ t14; + t18 = t6 ^ t16; + t19 = t9 ^ t14; + t20 = t11 ^ t16; + t21 = t17 ^ y20; + t22 = t18 ^ y19; + t23 = t19 ^ y21; + t24 = t20 ^ y18; + + t25 = t21 ^ t22; + t26 = t21 & t23; + t27 = t24 ^ t26; + t28 = t25 & t27; + t29 = t28 ^ t22; + t30 = t23 ^ t24; + t31 = t22 ^ t26; + t32 = t31 & t30; + t33 = t32 ^ t24; + t34 = t23 ^ t33; + t35 = t27 ^ t33; + t36 = t24 & t35; + t37 = t36 ^ t34; + t38 = t27 ^ t36; + t39 = t29 & t38; + t40 = t25 ^ t39; + + t41 = t40 ^ t37; + t42 = t29 ^ t33; + t43 = t29 ^ t40; + t44 = t33 ^ t37; + t45 = t42 ^ t41; + z0 = t44 & y15; + z1 = t37 & y6; + z2 = t33 & x7; + z3 = t43 & y16; + z4 = t40 & y1; + z5 = t29 & y7; + z6 = t42 & y11; + z7 = t45 & y17; + z8 = t41 & y10; + z9 = t44 & y12; + z10 = t37 & y3; + z11 = t33 & y4; + z12 = t43 & y13; + z13 = t40 & y5; + z14 = t29 & y2; + z15 = t42 & y9; + z16 = t45 & y14; + z17 = t41 & y8; + + /* + * Bottom linear transformation. + */ + t46 = z15 ^ z16; + t47 = z10 ^ z11; + t48 = z5 ^ z13; + t49 = z9 ^ z10; + t50 = z2 ^ z12; + t51 = z2 ^ z5; + t52 = z7 ^ z8; + t53 = z0 ^ z3; + t54 = z6 ^ z7; + t55 = z16 ^ z17; + t56 = z12 ^ t48; + t57 = t50 ^ t53; + t58 = z4 ^ t46; + t59 = z3 ^ t54; + t60 = t46 ^ t57; + t61 = z14 ^ t57; + t62 = t52 ^ t58; + t63 = t49 ^ t58; + t64 = z4 ^ t59; + t65 = t61 ^ t62; + t66 = z1 ^ t63; + s0 = t59 ^ t63; + s6 = t56 ^ ~t62; + s7 = t48 ^ ~t60; + t67 = t64 ^ t65; + s3 = t53 ^ t66; + s4 = t51 ^ t66; + s5 = t47 ^ t65; + s1 = t64 ^ ~s3; + s2 = t55 ^ ~t67; + + q[7] = s0; + q[6] = s1; + q[5] = s2; + q[4] = s3; + q[3] = s4; + q[2] = s5; + q[1] = s6; + q[0] = s7; +} + +static void br_aes_ct_bitslice_Sbox(uint32_t *q) +{ + /* + * This S-box implementation is a straightforward translation of + * the circuit described by Boyar and Peralta in "A new + * combinational logic minimization technique with applications + * to cryptology" (https://eprint.iacr.org/2009/191.pdf). + * + * Note that variables x* (input) and s* (output) are numbered + * in "reverse" order (x0 is the high bit, x7 is the low bit). + */ + + uint32_t x0, x1, x2, x3, x4, x5, x6, x7; + uint32_t y1, y2, y3, y4, y5, y6, y7, y8, y9; + uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19; + uint32_t y20, y21; + uint32_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9; + uint32_t z10, z11, z12, z13, z14, z15, z16, z17; + uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9; + uint32_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19; + uint32_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29; + uint32_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39; + uint32_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49; + uint32_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59; + uint32_t t60, t61, t62, t63, t64, t65, t66, t67; + uint32_t s0, s1, s2, s3, s4, s5, s6, s7; + + x0 = q[7]; + x1 = q[6]; + x2 = q[5]; + x3 = q[4]; + x4 = q[3]; + x5 = q[2]; + x6 = q[1]; + x7 = q[0]; + + /* + * Top linear transformation. + */ + y14 = x3 ^ x5; + y13 = x0 ^ x6; + y9 = x0 ^ x3; + y8 = x0 ^ x5; + t0 = x1 ^ x2; + y1 = t0 ^ x7; + y4 = y1 ^ x3; + y12 = y13 ^ y14; + y2 = y1 ^ x0; + y5 = y1 ^ x6; + y3 = y5 ^ y8; + t1 = x4 ^ y12; + y15 = t1 ^ x5; + y20 = t1 ^ x1; + y6 = y15 ^ x7; + y10 = y15 ^ t0; + y11 = y20 ^ y9; + y7 = x7 ^ y11; + y17 = y10 ^ y11; + y19 = y10 ^ y8; + y16 = t0 ^ y11; + y21 = y13 ^ y16; + y18 = x0 ^ y16; + + /* + * Non-linear section. + */ + t2 = y12 & y15; + t3 = y3 & y6; + t4 = t3 ^ t2; + t5 = y4 & x7; + t6 = t5 ^ t2; + t7 = y13 & y16; + t8 = y5 & y1; + t9 = t8 ^ t7; + t10 = y2 & y7; + t11 = t10 ^ t7; + t12 = y9 & y11; + t13 = y14 & y17; + t14 = t13 ^ t12; + t15 = y8 & y10; + t16 = t15 ^ t12; + t17 = t4 ^ t14; + t18 = t6 ^ t16; + t19 = t9 ^ t14; + t20 = t11 ^ t16; + t21 = t17 ^ y20; + t22 = t18 ^ y19; + t23 = t19 ^ y21; + t24 = t20 ^ y18; + + t25 = t21 ^ t22; + t26 = t21 & t23; + t27 = t24 ^ t26; + t28 = t25 & t27; + t29 = t28 ^ t22; + t30 = t23 ^ t24; + t31 = t22 ^ t26; + t32 = t31 & t30; + t33 = t32 ^ t24; + t34 = t23 ^ t33; + t35 = t27 ^ t33; + t36 = t24 & t35; + t37 = t36 ^ t34; + t38 = t27 ^ t36; + t39 = t29 & t38; + t40 = t25 ^ t39; + + t41 = t40 ^ t37; + t42 = t29 ^ t33; + t43 = t29 ^ t40; + t44 = t33 ^ t37; + t45 = t42 ^ t41; + z0 = t44 & y15; + z1 = t37 & y6; + z2 = t33 & x7; + z3 = t43 & y16; + z4 = t40 & y1; + z5 = t29 & y7; + z6 = t42 & y11; + z7 = t45 & y17; + z8 = t41 & y10; + z9 = t44 & y12; + z10 = t37 & y3; + z11 = t33 & y4; + z12 = t43 & y13; + z13 = t40 & y5; + z14 = t29 & y2; + z15 = t42 & y9; + z16 = t45 & y14; + z17 = t41 & y8; + + /* + * Bottom linear transformation. + */ + t46 = z15 ^ z16; + t47 = z10 ^ z11; + t48 = z5 ^ z13; + t49 = z9 ^ z10; + t50 = z2 ^ z12; + t51 = z2 ^ z5; + t52 = z7 ^ z8; + t53 = z0 ^ z3; + t54 = z6 ^ z7; + t55 = z16 ^ z17; + t56 = z12 ^ t48; + t57 = t50 ^ t53; + t58 = z4 ^ t46; + t59 = z3 ^ t54; + t60 = t46 ^ t57; + t61 = z14 ^ t57; + t62 = t52 ^ t58; + t63 = t49 ^ t58; + t64 = z4 ^ t59; + t65 = t61 ^ t62; + t66 = z1 ^ t63; + s0 = t59 ^ t63; + s6 = t56 ^ ~t62; + s7 = t48 ^ ~t60; + t67 = t64 ^ t65; + s3 = t53 ^ t66; + s4 = t51 ^ t66; + s5 = t47 ^ t65; + s1 = t64 ^ ~s3; + s2 = t55 ^ ~t67; + + q[7] = s0; + q[6] = s1; + q[5] = s2; + q[4] = s3; + q[3] = s4; + q[2] = s5; + q[1] = s6; + q[0] = s7; +} + +static void br_aes_ct_ortho(uint32_t *q) +{ +#define SWAPN_32(cl, ch, s, x, y) do { \ + uint32_t a, b; \ + a = (x); \ + b = (y); \ + (x) = (a & (uint32_t)cl) | ((b & (uint32_t)cl) << (s)); \ + (y) = ((a & (uint32_t)ch) >> (s)) | (b & (uint32_t)ch); \ + } while (0) + +#define SWAP2_32(x, y) SWAPN_32(0x55555555, 0xAAAAAAAA, 1, x, y) +#define SWAP4_32(x, y) SWAPN_32(0x33333333, 0xCCCCCCCC, 2, x, y) +#define SWAP8_32(x, y) SWAPN_32(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y) + + SWAP2_32(q[0], q[1]); + SWAP2_32(q[2], q[3]); + SWAP2_32(q[4], q[5]); + SWAP2_32(q[6], q[7]); + + SWAP4_32(q[0], q[2]); + SWAP4_32(q[1], q[3]); + SWAP4_32(q[4], q[6]); + SWAP4_32(q[5], q[7]); + + SWAP8_32(q[0], q[4]); + SWAP8_32(q[1], q[5]); + SWAP8_32(q[2], q[6]); + SWAP8_32(q[3], q[7]); +} + +static inline void add_round_key32(uint32_t *q, const uint32_t *sk) +{ + q[0] ^= sk[0]; + q[1] ^= sk[1]; + q[2] ^= sk[2]; + q[3] ^= sk[3]; + q[4] ^= sk[4]; + q[5] ^= sk[5]; + q[6] ^= sk[6]; + q[7] ^= sk[7]; +} + +static inline void shift_rows32(uint32_t *q) +{ + int i; + + for (i = 0; i < 8; i++) { + uint32_t x; + + x = q[i]; + q[i] = (x & 0x000000FF) + | ((x & 0x0000FC00) >> 2) | ((x & 0x00000300) << 6) + | ((x & 0x00F00000) >> 4) | ((x & 0x000F0000) << 4) + | ((x & 0xC0000000) >> 6) | ((x & 0x3F000000) << 2); + } +} + +static inline uint32_t rotr16(uint32_t x) +{ + return (x << 16) | (x >> 16); +} + +static inline void mix_columns32(uint32_t *q) +{ + uint32_t q0, q1, q2, q3, q4, q5, q6, q7; + uint32_t r0, r1, r2, r3, r4, r5, r6, r7; + + q0 = q[0]; + q1 = q[1]; + q2 = q[2]; + q3 = q[3]; + q4 = q[4]; + q5 = q[5]; + q6 = q[6]; + q7 = q[7]; + r0 = (q0 >> 8) | (q0 << 24); + r1 = (q1 >> 8) | (q1 << 24); + r2 = (q2 >> 8) | (q2 << 24); + r3 = (q3 >> 8) | (q3 << 24); + r4 = (q4 >> 8) | (q4 << 24); + r5 = (q5 >> 8) | (q5 << 24); + r6 = (q6 >> 8) | (q6 << 24); + r7 = (q7 >> 8) | (q7 << 24); + + q[0] = q7 ^ r7 ^ r0 ^ rotr16(q0 ^ r0); + q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr16(q1 ^ r1); + q[2] = q1 ^ r1 ^ r2 ^ rotr16(q2 ^ r2); + q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr16(q3 ^ r3); + q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr16(q4 ^ r4); + q[5] = q4 ^ r4 ^ r5 ^ rotr16(q5 ^ r5); + q[6] = q5 ^ r5 ^ r6 ^ rotr16(q6 ^ r6); + q[7] = q6 ^ r6 ^ r7 ^ rotr16(q7 ^ r7); +} + +static void br_aes_ct64_ortho(uint64_t *q) +{ +#define SWAPN(cl, ch, s, x, y) do { \ + uint64_t a, b; \ + a = (x); \ + b = (y); \ + (x) = (a & (uint64_t)(cl)) | ((b & (uint64_t)(cl)) << (s)); \ + (y) = ((a & (uint64_t)(ch)) >> (s)) | (b & (uint64_t)(ch)); \ + } while (0) + +#define SWAP2(x, y) SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 1, x, y) +#define SWAP4(x, y) SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC, 2, x, y) +#define SWAP8(x, y) SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0, 4, x, y) + + SWAP2(q[0], q[1]); + SWAP2(q[2], q[3]); + SWAP2(q[4], q[5]); + SWAP2(q[6], q[7]); + + SWAP4(q[0], q[2]); + SWAP4(q[1], q[3]); + SWAP4(q[4], q[6]); + SWAP4(q[5], q[7]); + + SWAP8(q[0], q[4]); + SWAP8(q[1], q[5]); + SWAP8(q[2], q[6]); + SWAP8(q[3], q[7]); +} + + +static void br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w) +{ + uint64_t x0, x1, x2, x3; + + x0 = w[0]; + x1 = w[1]; + x2 = w[2]; + x3 = w[3]; + x0 |= (x0 << 16); + x1 |= (x1 << 16); + x2 |= (x2 << 16); + x3 |= (x3 << 16); + x0 &= (uint64_t)0x0000FFFF0000FFFF; + x1 &= (uint64_t)0x0000FFFF0000FFFF; + x2 &= (uint64_t)0x0000FFFF0000FFFF; + x3 &= (uint64_t)0x0000FFFF0000FFFF; + x0 |= (x0 << 8); + x1 |= (x1 << 8); + x2 |= (x2 << 8); + x3 |= (x3 << 8); + x0 &= (uint64_t)0x00FF00FF00FF00FF; + x1 &= (uint64_t)0x00FF00FF00FF00FF; + x2 &= (uint64_t)0x00FF00FF00FF00FF; + x3 &= (uint64_t)0x00FF00FF00FF00FF; + *q0 = x0 | (x2 << 8); + *q1 = x1 | (x3 << 8); +} + + +static void br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1) +{ + uint64_t x0, x1, x2, x3; + + x0 = q0 & (uint64_t)0x00FF00FF00FF00FF; + x1 = q1 & (uint64_t)0x00FF00FF00FF00FF; + x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF; + x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF; + x0 |= (x0 >> 8); + x1 |= (x1 >> 8); + x2 |= (x2 >> 8); + x3 |= (x3 >> 8); + x0 &= (uint64_t)0x0000FFFF0000FFFF; + x1 &= (uint64_t)0x0000FFFF0000FFFF; + x2 &= (uint64_t)0x0000FFFF0000FFFF; + x3 &= (uint64_t)0x0000FFFF0000FFFF; + w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16); + w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16); + w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16); + w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16); +} + +static inline void add_round_key(uint64_t *q, const uint64_t *sk) +{ + q[0] ^= sk[0]; + q[1] ^= sk[1]; + q[2] ^= sk[2]; + q[3] ^= sk[3]; + q[4] ^= sk[4]; + q[5] ^= sk[5]; + q[6] ^= sk[6]; + q[7] ^= sk[7]; +} + +static inline void shift_rows(uint64_t *q) +{ + int i; + + for (i = 0; i < 8; i++) { + uint64_t x; + + x = q[i]; + q[i] = (x & (uint64_t)0x000000000000FFFF) + | ((x & (uint64_t)0x00000000FFF00000) >> 4) + | ((x & (uint64_t)0x00000000000F0000) << 12) + | ((x & (uint64_t)0x0000FF0000000000) >> 8) + | ((x & (uint64_t)0x000000FF00000000) << 8) + | ((x & (uint64_t)0xF000000000000000) >> 12) + | ((x & (uint64_t)0x0FFF000000000000) << 4); + } +} + +static inline uint64_t rotr32(uint64_t x) +{ + return (x << 32) | (x >> 32); +} + +static inline void mix_columns(uint64_t *q) +{ + uint64_t q0, q1, q2, q3, q4, q5, q6, q7; + uint64_t r0, r1, r2, r3, r4, r5, r6, r7; + + q0 = q[0]; + q1 = q[1]; + q2 = q[2]; + q3 = q[3]; + q4 = q[4]; + q5 = q[5]; + q6 = q[6]; + q7 = q[7]; + r0 = (q0 >> 16) | (q0 << 48); + r1 = (q1 >> 16) | (q1 << 48); + r2 = (q2 >> 16) | (q2 << 48); + r3 = (q3 >> 16) | (q3 << 48); + r4 = (q4 >> 16) | (q4 << 48); + r5 = (q5 >> 16) | (q5 << 48); + r6 = (q6 >> 16) | (q6 << 48); + r7 = (q7 >> 16) | (q7 << 48); + + q[0] = q7 ^ r7 ^ r0 ^ rotr32(q0 ^ r0); + q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr32(q1 ^ r1); + q[2] = q1 ^ r1 ^ r2 ^ rotr32(q2 ^ r2); + q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr32(q3 ^ r3); + q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr32(q4 ^ r4); + q[5] = q4 ^ r4 ^ r5 ^ rotr32(q5 ^ r5); + q[6] = q5 ^ r5 ^ r6 ^ rotr32(q6 ^ r6); + q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7); +} + +static void interleave_constant(uint64_t *out, const unsigned char *in) +{ + uint32_t tmp_32_constant[16]; + int i; + + br_range_dec32le(tmp_32_constant, 16, in); + for (i = 0; i < 4; i++) { + br_aes_ct64_interleave_in(&out[i], &out[i + 4], tmp_32_constant + (i << 2)); + } + br_aes_ct64_ortho(out); +} + +static void interleave_constant32(uint32_t *out, const unsigned char *in) +{ + int i; + for (i = 0; i < 4; i++) { + out[2*i] = br_dec32le(in + 4*i); + out[2*i + 1] = br_dec32le(in + 4*i + 16); + } + br_aes_ct_ortho(out); +} + +void tweak_constants(spx_ctx *ctx) +{ + unsigned char buf[40*16]; + int i; + + /* Use the standard constants to generate tweaked ones. */ + memcpy((uint8_t *)ctx->tweaked512_rc64, (uint8_t *)haraka512_rc64, 40*16); + + /* Constants for pk.seed */ + haraka_S(buf, 40*16, ctx->pub_seed, SPX_N, ctx); + for (i = 0; i < 10; i++) { + interleave_constant32(ctx->tweaked256_rc32[i], buf + 32*i); + interleave_constant(ctx->tweaked512_rc64[i], buf + 64*i); + } +} + +static void haraka_S_absorb(unsigned char *s, unsigned int r, + const unsigned char *m, unsigned long long mlen, + unsigned char p, const spx_ctx *ctx) +{ + unsigned long long i; + SPX_VLA(uint8_t, t, r); + + while (mlen >= r) { + /* XOR block to state */ + for (i = 0; i < r; ++i) { + s[i] ^= m[i]; + } + haraka512_perm(s, s, ctx); + mlen -= r; + m += r; + } + + for (i = 0; i < r; ++i) { + t[i] = 0; + } + for (i = 0; i < mlen; ++i) { + t[i] = m[i]; + } + t[i] = p; + t[r - 1] |= 128; + for (i = 0; i < r; ++i) { + s[i] ^= t[i]; + } +} + +static void haraka_S_squeezeblocks(unsigned char *h, unsigned long long nblocks, + unsigned char *s, unsigned int r, + const spx_ctx *ctx) +{ + while (nblocks > 0) { + haraka512_perm(s, s, ctx); + memcpy(h, s, HARAKAS_RATE); + h += r; + nblocks--; + } +} + +void haraka_S_inc_init(uint8_t *s_inc) +{ + size_t i; + + for (i = 0; i < 64; i++) { + s_inc[i] = 0; + } + s_inc[64] = 0; +} + +void haraka_S_inc_absorb(uint8_t *s_inc, const uint8_t *m, size_t mlen, + const spx_ctx *ctx) +{ + size_t i; + + /* Recall that s_inc[64] is the non-absorbed bytes xored into the state */ + while (mlen + s_inc[64] >= HARAKAS_RATE) { + for (i = 0; i < (size_t)(HARAKAS_RATE - s_inc[64]); i++) { + /* Take the i'th byte from message + xor with the s_inc[64] + i'th byte of the state */ + s_inc[s_inc[64] + i] ^= m[i]; + } + mlen -= (size_t)(HARAKAS_RATE - s_inc[64]); + m += HARAKAS_RATE - (uint8_t)s_inc[64]; + s_inc[64] = 0; + + haraka512_perm(s_inc, s_inc, ctx); + } + + for (i = 0; i < mlen; i++) { + s_inc[s_inc[64] + i] ^= m[i]; + } + s_inc[64] += (uint8_t)mlen; +} + +void haraka_S_inc_finalize(uint8_t *s_inc) +{ + /* After haraka_S_inc_absorb, we are guaranteed that s_inc[64] < HARAKAS_RATE, + so we can always use one more byte for p in the current state. */ + s_inc[s_inc[64]] ^= 0x1F; + s_inc[HARAKAS_RATE - 1] ^= 128; + s_inc[64] = 0; +} + +void haraka_S_inc_squeeze(uint8_t *out, size_t outlen, uint8_t *s_inc, + const spx_ctx *ctx) +{ + size_t i; + + /* First consume any bytes we still have sitting around */ + for (i = 0; i < outlen && i < s_inc[64]; i++) { + /* There are s_inc[64] bytes left, so r - s_inc[64] is the first + available byte. We consume from there, i.e., up to r. */ + out[i] = (uint8_t)s_inc[(HARAKAS_RATE - s_inc[64] + i)]; + } + out += i; + outlen -= i; + s_inc[64] -= (uint8_t)i; + + /* Then squeeze the remaining necessary blocks */ + while (outlen > 0) { + haraka512_perm(s_inc, s_inc, ctx); + + for (i = 0; i < outlen && i < HARAKAS_RATE; i++) { + out[i] = s_inc[i]; + } + out += i; + outlen -= i; + s_inc[64] = (uint8_t)(HARAKAS_RATE - i); + } +} + +void haraka_S(unsigned char *out, unsigned long long outlen, + const unsigned char *in, unsigned long long inlen, + const spx_ctx *ctx) +{ + unsigned long long i; + unsigned char s[64]; + unsigned char d[32]; + + for (i = 0; i < 64; i++) { + s[i] = 0; + } + haraka_S_absorb(s, 32, in, inlen, 0x1F, ctx); + + haraka_S_squeezeblocks(out, outlen / 32, s, 32, ctx); + out += (outlen / 32) * 32; + + if (outlen % 32) { + haraka_S_squeezeblocks(d, 1, s, 32, ctx); + for (i = 0; i < outlen % 32; i++) { + out[i] = d[i]; + } + } +} + +void haraka512_perm(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx) +{ + uint32_t w[16]; + uint64_t q[8], tmp_q; + unsigned int i, j; + + br_range_dec32le(w, 16, in); + for (i = 0; i < 4; i++) { + br_aes_ct64_interleave_in(&q[i], &q[i + 4], w + (i << 2)); + } + br_aes_ct64_ortho(q); + + /* AES rounds */ + for (i = 0; i < 5; i++) { + for (j = 0; j < 2; j++) { + br_aes_ct64_bitslice_Sbox(q); + shift_rows(q); + mix_columns(q); + add_round_key(q, ctx->tweaked512_rc64[2*i + j]); + } + /* Mix states */ + for (j = 0; j < 8; j++) { + tmp_q = q[j]; + q[j] = (tmp_q & 0x0001000100010001) << 5 | + (tmp_q & 0x0002000200020002) << 12 | + (tmp_q & 0x0004000400040004) >> 1 | + (tmp_q & 0x0008000800080008) << 6 | + (tmp_q & 0x0020002000200020) << 9 | + (tmp_q & 0x0040004000400040) >> 4 | + (tmp_q & 0x0080008000800080) << 3 | + (tmp_q & 0x2100210021002100) >> 5 | + (tmp_q & 0x0210021002100210) << 2 | + (tmp_q & 0x0800080008000800) << 4 | + (tmp_q & 0x1000100010001000) >> 12 | + (tmp_q & 0x4000400040004000) >> 10 | + (tmp_q & 0x8400840084008400) >> 3; + } + } + + br_aes_ct64_ortho(q); + for (i = 0; i < 4; i ++) { + br_aes_ct64_interleave_out(w + (i << 2), q[i], q[i + 4]); + } + br_range_enc32le(out, w, 16); +} + +void haraka512(unsigned char *out, const unsigned char *in, const spx_ctx *ctx) +{ + int i; + + unsigned char buf[64]; + + haraka512_perm(buf, in, ctx); + /* Feed-forward */ + for (i = 0; i < 64; i++) { + buf[i] = buf[i] ^ in[i]; + } + + /* Truncated */ + memcpy(out, buf + 8, 8); + memcpy(out + 8, buf + 24, 8); + memcpy(out + 16, buf + 32, 8); + memcpy(out + 24, buf + 48, 8); +} + + +void haraka256(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx) +{ + uint32_t q[8], tmp_q; + int i, j; + + for (i = 0; i < 4; i++) { + q[2*i] = br_dec32le(in + 4*i); + q[2*i + 1] = br_dec32le(in + 4*i + 16); + } + br_aes_ct_ortho(q); + + /* AES rounds */ + for (i = 0; i < 5; i++) { + for (j = 0; j < 2; j++) { + br_aes_ct_bitslice_Sbox(q); + shift_rows32(q); + mix_columns32(q); + add_round_key32(q, ctx->tweaked256_rc32[2*i + j]); + } + + /* Mix states */ + for (j = 0; j < 8; j++) { + tmp_q = q[j]; + q[j] = (tmp_q & 0x81818181) | + (tmp_q & 0x02020202) << 1 | + (tmp_q & 0x04040404) << 2 | + (tmp_q & 0x08080808) << 3 | + (tmp_q & 0x10101010) >> 3 | + (tmp_q & 0x20202020) >> 2 | + (tmp_q & 0x40404040) >> 1; + } + } + + br_aes_ct_ortho(q); + for (i = 0; i < 4; i++) { + br_enc32le(out + 4*i, q[2*i]); + br_enc32le(out + 4*i + 16, q[2*i + 1]); + } + + for (i = 0; i < 32; i++) { + out[i] ^= in[i]; + } +} diff --git a/src/crypto/pq/_s/ref/haraka.h b/src/crypto/pq/_s/ref/haraka.h new file mode 100644 index 0000000..7cd311c --- /dev/null +++ b/src/crypto/pq/_s/ref/haraka.h @@ -0,0 +1,41 @@ +#ifndef SPX_HARAKA_H +#define SPX_HARAKA_H + +#include "context.h" + +/* Tweak constants with seed */ +#define tweak_constants SPX_NAMESPACE(tweak_constants) +void tweak_constants(spx_ctx *ctx); + +/* Haraka Sponge */ +#define haraka_S_inc_init SPX_NAMESPACE(haraka_S_inc_init) +void haraka_S_inc_init(uint8_t *s_inc); +#define haraka_S_inc_absorb SPX_NAMESPACE(haraka_S_inc_absorb) +void haraka_S_inc_absorb(uint8_t *s_inc, const uint8_t *m, size_t mlen, + const spx_ctx *ctx); +#define haraka_S_inc_finalize SPX_NAMESPACE(haraka_S_inc_finalize) +void haraka_S_inc_finalize(uint8_t *s_inc); +#define haraka_S_inc_squeeze SPX_NAMESPACE(haraka_S_inc_squeeze) +void haraka_S_inc_squeeze(uint8_t *out, size_t outlen, uint8_t *s_inc, + const spx_ctx *ctx); +#define haraka_S SPX_NAMESPACE(haraka_S) +void haraka_S(unsigned char *out, unsigned long long outlen, + const unsigned char *in, unsigned long long inlen, + const spx_ctx *ctx); + +/* Applies the 512-bit Haraka permutation to in. */ +#define haraka512_perm SPX_NAMESPACE(haraka512_perm) +void haraka512_perm(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx); + +/* Implementation of Haraka-512 */ +#define haraka512 SPX_NAMESPACE(haraka512) +void haraka512(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx); + +/* Implementation of Haraka-256 */ +#define haraka256 SPX_NAMESPACE(haraka256) +void haraka256(unsigned char *out, const unsigned char *in, + const spx_ctx *ctx); + +#endif diff --git a/src/crypto/pq/_s/ref/haraka_offsets.h b/src/crypto/pq/_s/ref/haraka_offsets.h new file mode 100644 index 0000000..6afa5f8 --- /dev/null +++ b/src/crypto/pq/_s/ref/haraka_offsets.h @@ -0,0 +1,20 @@ +#if !defined( HARAKA_OFFSETS_H_ ) +#define HARAKA_OFFSETS_H_ + +/* + * Offsets of various fields in the address structure when we use Haraka as + * the Sphincs+ hash function + */ + +#define SPX_OFFSET_LAYER 3 /* The byte used to specify the Merkle tree layer */ +#define SPX_OFFSET_TREE 8 /* The start of the 8 byte field used to specify the tree */ +#define SPX_OFFSET_TYPE 19 /* The byte used to specify the hash type (reason) */ +#define SPX_OFFSET_KP_ADDR 20 /* The start of the 4 byte field used to specify the key pair address */ +#define SPX_OFFSET_CHAIN_ADDR 27 /* The byte used to specify the chain address (which Winternitz chain) */ +#define SPX_OFFSET_HASH_ADDR 31 /* The byte used to specify the hash address (where in the Winternitz chain) */ +#define SPX_OFFSET_TREE_HGT 27 /* The byte used to specify the height of this node in the FORS or Merkle tree */ +#define SPX_OFFSET_TREE_INDEX 28 /* The start of the 4 byte field used to specify the node in the FORS or Merkle tree */ + +#define SPX_HARAKA 1 + +#endif /* HARAKA_OFFSETS_H_ */ diff --git a/src/crypto/pq/_s/ref/hash.h b/src/crypto/pq/_s/ref/hash.h new file mode 100644 index 0000000..b141f09 --- /dev/null +++ b/src/crypto/pq/_s/ref/hash.h @@ -0,0 +1,27 @@ +#ifndef SPX_HASH_H +#define SPX_HASH_H + +#include +#include "context.h" +#include "params.h" + +#define initialize_hash_function SPX_NAMESPACE(initialize_hash_function) +void initialize_hash_function(spx_ctx *ctx); + +#define prf_addr SPX_NAMESPACE(prf_addr) +void prf_addr(unsigned char *out, const spx_ctx *ctx, + const uint32_t addr[8]); + +#define gen_message_random SPX_NAMESPACE(gen_message_random) +void gen_message_random(unsigned char *R, const unsigned char *sk_prf, + const unsigned char *optrand, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx); + +#define hash_message SPX_NAMESPACE(hash_message) +void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx, + const unsigned char *R, const unsigned char *pk, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx); + +#endif diff --git a/src/crypto/pq/_s/ref/hash_haraka.c b/src/crypto/pq/_s/ref/hash_haraka.c new file mode 100644 index 0000000..0c73525 --- /dev/null +++ b/src/crypto/pq/_s/ref/hash_haraka.c @@ -0,0 +1,96 @@ +#include +#include + +#include "address.h" +#include "utils.h" +#include "params.h" + +#include "haraka.h" +#include "hash.h" + +void initialize_hash_function(spx_ctx* ctx) +{ + tweak_constants(ctx); +} + +/* + * Computes PRF(key, addr), given a secret key of SPX_N bytes and an address + */ +void prf_addr(unsigned char *out, const spx_ctx *ctx, + const uint32_t addr[8]) +{ + /* Since SPX_N may be smaller than 32, we need temporary buffers. */ + unsigned char outbuf[32]; + unsigned char buf[64] = {0}; + + memcpy(buf, addr, SPX_ADDR_BYTES); + memcpy(buf + SPX_ADDR_BYTES, ctx->sk_seed, SPX_N); + + haraka512(outbuf, (const void *)buf, ctx); + memcpy(out, outbuf, SPX_N); +} + +/** + * Computes the message-dependent randomness R, using a secret seed and an + * optional randomization value as well as the message. + */ +void gen_message_random(unsigned char *R, const unsigned char* sk_prf, + const unsigned char *optrand, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx) +{ + uint8_t s_inc[65]; + + haraka_S_inc_init(s_inc); + haraka_S_inc_absorb(s_inc, sk_prf, SPX_N, ctx); + haraka_S_inc_absorb(s_inc, optrand, SPX_N, ctx); + haraka_S_inc_absorb(s_inc, m, mlen, ctx); + haraka_S_inc_finalize(s_inc); + haraka_S_inc_squeeze(R, SPX_N, s_inc, ctx); +} + +/** + * Computes the message hash using R, the public key, and the message. + * Outputs the message digest and the index of the leaf. The index is split in + * the tree index and the leaf index, for convenient copying to an address. + */ +void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx, + const unsigned char *R, const unsigned char *pk, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx) +{ +#define SPX_TREE_BITS (SPX_TREE_HEIGHT * (SPX_D - 1)) +#define SPX_TREE_BYTES ((SPX_TREE_BITS + 7) / 8) +#define SPX_LEAF_BITS SPX_TREE_HEIGHT +#define SPX_LEAF_BYTES ((SPX_LEAF_BITS + 7) / 8) +#define SPX_DGST_BYTES (SPX_FORS_MSG_BYTES + SPX_TREE_BYTES + SPX_LEAF_BYTES) + + unsigned char buf[SPX_DGST_BYTES]; + unsigned char *bufp = buf; + uint8_t s_inc[65]; + + haraka_S_inc_init(s_inc); + haraka_S_inc_absorb(s_inc, R, SPX_N, ctx); + haraka_S_inc_absorb(s_inc, pk + SPX_N, SPX_N, ctx); // Only absorb root part of pk + haraka_S_inc_absorb(s_inc, m, mlen, ctx); + haraka_S_inc_finalize(s_inc); + haraka_S_inc_squeeze(buf, SPX_DGST_BYTES, s_inc, ctx); + + memcpy(digest, bufp, SPX_FORS_MSG_BYTES); + bufp += SPX_FORS_MSG_BYTES; + +#if SPX_TREE_BITS > 64 + #error For given height and depth, 64 bits cannot represent all subtrees +#endif + + if (SPX_D == 1) { + *tree = 0; + } else { + *tree = bytes_to_ull(bufp, SPX_TREE_BYTES); + *tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS); + } + bufp += SPX_TREE_BYTES; + + *leaf_idx = (uint32_t)bytes_to_ull(bufp, SPX_LEAF_BYTES); + *leaf_idx &= (~(uint32_t)0) >> (32 - SPX_LEAF_BITS); +} diff --git a/src/crypto/pq/_s/ref/hash_sha2.c b/src/crypto/pq/_s/ref/hash_sha2.c new file mode 100644 index 0000000..67098e6 --- /dev/null +++ b/src/crypto/pq/_s/ref/hash_sha2.c @@ -0,0 +1,197 @@ +#include +#include + +#include "address.h" +#include "utils.h" +#include "params.h" +#include "hash.h" +#include "sha2.h" + +#if SPX_N >= 24 +#define SPX_SHAX_OUTPUT_BYTES SPX_SHA512_OUTPUT_BYTES +#define SPX_SHAX_BLOCK_BYTES SPX_SHA512_BLOCK_BYTES +#define shaX_inc_init sha512_inc_init +#define shaX_inc_blocks sha512_inc_blocks +#define shaX_inc_finalize sha512_inc_finalize +#define shaX sha512 +#define mgf1_X mgf1_512 +#else +#define SPX_SHAX_OUTPUT_BYTES SPX_SHA256_OUTPUT_BYTES +#define SPX_SHAX_BLOCK_BYTES SPX_SHA256_BLOCK_BYTES +#define shaX_inc_init sha256_inc_init +#define shaX_inc_blocks sha256_inc_blocks +#define shaX_inc_finalize sha256_inc_finalize +#define shaX sha256 +#define mgf1_X mgf1_256 +#endif + + +/* For SHA, there is no immediate reason to initialize at the start, + so this function is an empty operation. */ +void initialize_hash_function(spx_ctx *ctx) +{ + seed_state(ctx); +} + +/* + * Computes PRF(pk_seed, sk_seed, addr). + */ +void prf_addr(unsigned char *out, const spx_ctx *ctx, + const uint32_t addr[8]) +{ + uint8_t sha2_state[40]; + unsigned char buf[SPX_SHA256_ADDR_BYTES + SPX_N]; + unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES]; + + /* Retrieve precomputed state containing pub_seed */ + memcpy(sha2_state, ctx->state_seeded, 40 * sizeof(uint8_t)); + + /* Remainder: ADDR^c ‖ SK.seed */ + memcpy(buf, addr, SPX_SHA256_ADDR_BYTES); + memcpy(buf + SPX_SHA256_ADDR_BYTES, ctx->sk_seed, SPX_N); + + sha256_inc_finalize(outbuf, sha2_state, buf, SPX_SHA256_ADDR_BYTES + SPX_N); + + memcpy(out, outbuf, SPX_N); +} + +/** + * Computes the message-dependent randomness R, using a secret seed as a key + * for HMAC, and an optional randomization value prefixed to the message. + * This requires m to have at least SPX_SHAX_BLOCK_BYTES + SPX_N space + * available in front of the pointer, i.e. before the message to use for the + * prefix. This is necessary to prevent having to move the message around (and + * allocate memory for it). + */ +void gen_message_random(unsigned char *R, const unsigned char *sk_prf, + const unsigned char *optrand, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx) +{ + (void)ctx; + + unsigned char buf[SPX_SHAX_BLOCK_BYTES + SPX_SHAX_OUTPUT_BYTES]; + uint8_t state[8 + SPX_SHAX_OUTPUT_BYTES]; + int i; + +#if SPX_N > SPX_SHAX_BLOCK_BYTES + #error "Currently only supports SPX_N of at most SPX_SHAX_BLOCK_BYTES" +#endif + + /* This implements HMAC-SHA */ + for (i = 0; i < SPX_N; i++) { + buf[i] = 0x36 ^ sk_prf[i]; + } + memset(buf + SPX_N, 0x36, SPX_SHAX_BLOCK_BYTES - SPX_N); + + shaX_inc_init(state); + shaX_inc_blocks(state, buf, 1); + + memcpy(buf, optrand, SPX_N); + + /* If optrand + message cannot fill up an entire block */ + if (SPX_N + mlen < SPX_SHAX_BLOCK_BYTES) { + memcpy(buf + SPX_N, m, mlen); + shaX_inc_finalize(buf + SPX_SHAX_BLOCK_BYTES, state, + buf, mlen + SPX_N); + } + /* Otherwise first fill a block, so that finalize only uses the message */ + else { + memcpy(buf + SPX_N, m, SPX_SHAX_BLOCK_BYTES - SPX_N); + shaX_inc_blocks(state, buf, 1); + + m += SPX_SHAX_BLOCK_BYTES - SPX_N; + mlen -= SPX_SHAX_BLOCK_BYTES - SPX_N; + shaX_inc_finalize(buf + SPX_SHAX_BLOCK_BYTES, state, m, mlen); + } + + for (i = 0; i < SPX_N; i++) { + buf[i] = 0x5c ^ sk_prf[i]; + } + memset(buf + SPX_N, 0x5c, SPX_SHAX_BLOCK_BYTES - SPX_N); + + shaX(buf, buf, SPX_SHAX_BLOCK_BYTES + SPX_SHAX_OUTPUT_BYTES); + memcpy(R, buf, SPX_N); +} + +/** + * Computes the message hash using R, the public key, and the message. + * Outputs the message digest and the index of the leaf. The index is split in + * the tree index and the leaf index, for convenient copying to an address. + */ +void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx, + const unsigned char *R, const unsigned char *pk, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx) +{ + (void)ctx; +#define SPX_TREE_BITS (SPX_TREE_HEIGHT * (SPX_D - 1)) +#define SPX_TREE_BYTES ((SPX_TREE_BITS + 7) / 8) +#define SPX_LEAF_BITS SPX_TREE_HEIGHT +#define SPX_LEAF_BYTES ((SPX_LEAF_BITS + 7) / 8) +#define SPX_DGST_BYTES (SPX_FORS_MSG_BYTES + SPX_TREE_BYTES + SPX_LEAF_BYTES) + + unsigned char seed[2*SPX_N + SPX_SHAX_OUTPUT_BYTES]; + + /* Round to nearest multiple of SPX_SHAX_BLOCK_BYTES */ +#if (SPX_SHAX_BLOCK_BYTES & (SPX_SHAX_BLOCK_BYTES-1)) != 0 + #error "Assumes that SPX_SHAX_BLOCK_BYTES is a power of 2" +#endif +#define SPX_INBLOCKS (((SPX_N + SPX_PK_BYTES + SPX_SHAX_BLOCK_BYTES - 1) & \ + -SPX_SHAX_BLOCK_BYTES) / SPX_SHAX_BLOCK_BYTES) + unsigned char inbuf[SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES]; + + unsigned char buf[SPX_DGST_BYTES]; + unsigned char *bufp = buf; + uint8_t state[8 + SPX_SHAX_OUTPUT_BYTES]; + + shaX_inc_init(state); + + // seed: SHA-X(R ‖ PK.seed ‖ PK.root ‖ M) + memcpy(inbuf, R, SPX_N); + memcpy(inbuf + SPX_N, pk, SPX_PK_BYTES); + + /* If R + pk + message cannot fill up an entire block */ + if (SPX_N + SPX_PK_BYTES + mlen < SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES) { + memcpy(inbuf + SPX_N + SPX_PK_BYTES, m, mlen); + shaX_inc_finalize(seed + 2*SPX_N, state, inbuf, SPX_N + SPX_PK_BYTES + mlen); + } + /* Otherwise first fill a block, so that finalize only uses the message */ + else { + memcpy(inbuf + SPX_N + SPX_PK_BYTES, m, + SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES - SPX_N - SPX_PK_BYTES); + shaX_inc_blocks(state, inbuf, SPX_INBLOCKS); + + m += SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES - SPX_N - SPX_PK_BYTES; + mlen -= SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES - SPX_N - SPX_PK_BYTES; + shaX_inc_finalize(seed + 2*SPX_N, state, m, mlen); + } + + // H_msg: MGF1-SHA-X(R ‖ PK.seed ‖ seed) + memcpy(seed, R, SPX_N); + memcpy(seed + SPX_N, pk, SPX_N); + + /* By doing this in two steps, we prevent hashing the message twice; + otherwise each iteration in MGF1 would hash the message again. */ + mgf1_X(bufp, SPX_DGST_BYTES, seed, 2*SPX_N + SPX_SHAX_OUTPUT_BYTES); + + memcpy(digest, bufp, SPX_FORS_MSG_BYTES); + bufp += SPX_FORS_MSG_BYTES; + +#if SPX_TREE_BITS > 64 + #error For given height and depth, 64 bits cannot represent all subtrees +#endif + + if (SPX_D == 1) { + *tree = 0; + } else { + *tree = bytes_to_ull(bufp, SPX_TREE_BYTES); + *tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS); + } + bufp += SPX_TREE_BYTES; + + *leaf_idx = (uint32_t)bytes_to_ull(bufp, SPX_LEAF_BYTES); + *leaf_idx &= (~(uint32_t)0) >> (32 - SPX_LEAF_BITS); +} + + diff --git a/src/crypto/pq/_s/ref/hash_shake.c b/src/crypto/pq/_s/ref/hash_shake.c new file mode 100644 index 0000000..f0372bf --- /dev/null +++ b/src/crypto/pq/_s/ref/hash_shake.c @@ -0,0 +1,97 @@ +#include +#include + +#include "address.h" +#include "utils.h" +#include "params.h" +#include "hash.h" +#include "fips202.h" + +/* For SHAKE256, there is no immediate reason to initialize at the start, + so this function is an empty operation. */ +void initialize_hash_function(spx_ctx* ctx) +{ + (void)ctx; /* Suppress an 'unused parameter' warning. */ +} + +/* + * Computes PRF(pk_seed, sk_seed, addr) + */ +void prf_addr(unsigned char *out, const spx_ctx *ctx, + const uint32_t addr[8]) +{ + unsigned char buf[2*SPX_N + SPX_ADDR_BYTES]; + + memcpy(buf, ctx->pub_seed, SPX_N); + memcpy(buf + SPX_N, addr, SPX_ADDR_BYTES); + memcpy(buf + SPX_N + SPX_ADDR_BYTES, ctx->sk_seed, SPX_N); + + shake256(out, SPX_N, buf, 2*SPX_N + SPX_ADDR_BYTES); +} + +/** + * Computes the message-dependent randomness R, using a secret seed and an + * optional randomization value as well as the message. + */ +void gen_message_random(unsigned char *R, const unsigned char *sk_prf, + const unsigned char *optrand, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx) +{ + (void)ctx; + uint64_t s_inc[26]; + + shake256_inc_init(s_inc); + shake256_inc_absorb(s_inc, sk_prf, SPX_N); + shake256_inc_absorb(s_inc, optrand, SPX_N); + shake256_inc_absorb(s_inc, m, mlen); + shake256_inc_finalize(s_inc); + shake256_inc_squeeze(R, SPX_N, s_inc); +} + +/** + * Computes the message hash using R, the public key, and the message. + * Outputs the message digest and the index of the leaf. The index is split in + * the tree index and the leaf index, for convenient copying to an address. + */ +void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx, + const unsigned char *R, const unsigned char *pk, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx) +{ + (void)ctx; +#define SPX_TREE_BITS (SPX_TREE_HEIGHT * (SPX_D - 1)) +#define SPX_TREE_BYTES ((SPX_TREE_BITS + 7) / 8) +#define SPX_LEAF_BITS SPX_TREE_HEIGHT +#define SPX_LEAF_BYTES ((SPX_LEAF_BITS + 7) / 8) +#define SPX_DGST_BYTES (SPX_FORS_MSG_BYTES + SPX_TREE_BYTES + SPX_LEAF_BYTES) + + unsigned char buf[SPX_DGST_BYTES]; + unsigned char *bufp = buf; + uint64_t s_inc[26]; + + shake256_inc_init(s_inc); + shake256_inc_absorb(s_inc, R, SPX_N); + shake256_inc_absorb(s_inc, pk, SPX_PK_BYTES); + shake256_inc_absorb(s_inc, m, mlen); + shake256_inc_finalize(s_inc); + shake256_inc_squeeze(buf, SPX_DGST_BYTES, s_inc); + + memcpy(digest, bufp, SPX_FORS_MSG_BYTES); + bufp += SPX_FORS_MSG_BYTES; + +#if SPX_TREE_BITS > 64 + #error For given height and depth, 64 bits cannot represent all subtrees +#endif + + if (SPX_D == 1) { + *tree = 0; + } else { + *tree = bytes_to_ull(bufp, SPX_TREE_BYTES); + *tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS); + } + bufp += SPX_TREE_BYTES; + + *leaf_idx = (uint32_t)bytes_to_ull(bufp, SPX_LEAF_BYTES); + *leaf_idx &= (~(uint32_t)0) >> (32 - SPX_LEAF_BITS); +} diff --git a/src/crypto/pq/_s/ref/merkle.c b/src/crypto/pq/_s/ref/merkle.c new file mode 100644 index 0000000..414f468 --- /dev/null +++ b/src/crypto/pq/_s/ref/merkle.c @@ -0,0 +1,61 @@ +#include +#include + +#include "utils.h" +#include "utilsx1.h" +#include "wots.h" +#include "wotsx1.h" +#include "merkle.h" +#include "address.h" +#include "params.h" + +/* + * This generates a Merkle signature (WOTS signature followed by the Merkle + * authentication path). This is in this file because most of the complexity + * is involved with the WOTS signature; the Merkle authentication path logic + * is mostly hidden in treehashx4 + */ +void merkle_sign(uint8_t *sig, unsigned char *root, + const spx_ctx *ctx, + uint32_t wots_addr[8], uint32_t tree_addr[8], + uint32_t idx_leaf) +{ + unsigned char *auth_path = sig + SPX_WOTS_BYTES; + struct leaf_info_x1 info = { 0 }; + unsigned steps[ SPX_WOTS_LEN ]; + + info.wots_sig = sig; + chain_lengths(steps, root); + info.wots_steps = steps; + + set_type(&tree_addr[0], SPX_ADDR_TYPE_HASHTREE); + set_type(&info.pk_addr[0], SPX_ADDR_TYPE_WOTSPK); + copy_subtree_addr(&info.leaf_addr[0], wots_addr); + copy_subtree_addr(&info.pk_addr[0], wots_addr); + + info.wots_sign_leaf = idx_leaf; + + treehashx1(root, auth_path, ctx, + idx_leaf, 0, + SPX_TREE_HEIGHT, + wots_gen_leafx1, + tree_addr, &info); +} + +/* Compute root node of the top-most subtree. */ +void merkle_gen_root(unsigned char *root, const spx_ctx *ctx) +{ + /* We do not need the auth path in key generation, but it simplifies the + code to have just one treehash routine that computes both root and path + in one function. */ + unsigned char auth_path[SPX_TREE_HEIGHT * SPX_N + SPX_WOTS_BYTES]; + uint32_t top_tree_addr[8] = {0}; + uint32_t wots_addr[8] = {0}; + + set_layer_addr(top_tree_addr, SPX_D - 1); + set_layer_addr(wots_addr, SPX_D - 1); + + merkle_sign(auth_path, root, ctx, + wots_addr, top_tree_addr, + (uint32_t)~0 /* ~0 means "don't bother generating an auth path */ ); +} diff --git a/src/crypto/pq/_s/ref/merkle.h b/src/crypto/pq/_s/ref/merkle.h new file mode 100644 index 0000000..9ac2759 --- /dev/null +++ b/src/crypto/pq/_s/ref/merkle.h @@ -0,0 +1,18 @@ +#if !defined( MERKLE_H_ ) +#define MERKLE_H_ + +#include + +/* Generate a Merkle signature (WOTS signature followed by the Merkle */ +/* authentication path) */ +#define merkle_sign SPX_NAMESPACE(merkle_sign) +void merkle_sign(uint8_t *sig, unsigned char *root, + const spx_ctx* ctx, + uint32_t wots_addr[8], uint32_t tree_addr[8], + uint32_t idx_leaf); + +/* Compute the root node of the top-most subtree. */ +#define merkle_gen_root SPX_NAMESPACE(merkle_gen_root) +void merkle_gen_root(unsigned char *root, const spx_ctx* ctx); + +#endif /* MERKLE_H_ */ diff --git a/src/crypto/pq/_s/ref/params.h b/src/crypto/pq/_s/ref/params.h new file mode 100644 index 0000000..1d7f9c9 --- /dev/null +++ b/src/crypto/pq/_s/ref/params.h @@ -0,0 +1,5 @@ +#define str(s) #s +#define xstr(s) str(s) + +#include xstr(params/params-PARAMS.h) + diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-haraka-128f.h b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-128f.h new file mode 100644 index 0000000..a9bb1cf --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-128f.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 16 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 66 +/* Number of subtree layer. */ +#define SPX_D 22 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 6 +#define SPX_FORS_TREES 33 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../haraka_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-haraka-128s.h b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-128s.h new file mode 100644 index 0000000..73372ee --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-128s.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 16 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 63 +/* Number of subtree layer. */ +#define SPX_D 7 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 12 +#define SPX_FORS_TREES 14 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../haraka_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-haraka-192f.h b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-192f.h new file mode 100644 index 0000000..9eb356a --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-192f.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 24 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 66 +/* Number of subtree layer. */ +#define SPX_D 22 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 8 +#define SPX_FORS_TREES 33 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../haraka_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-haraka-192s.h b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-192s.h new file mode 100644 index 0000000..00b6b70 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-192s.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 24 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 63 +/* Number of subtree layer. */ +#define SPX_D 7 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 14 +#define SPX_FORS_TREES 17 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../haraka_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-haraka-256f.h b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-256f.h new file mode 100644 index 0000000..7491b66 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-256f.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 32 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 68 +/* Number of subtree layer. */ +#define SPX_D 17 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 9 +#define SPX_FORS_TREES 35 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../haraka_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-haraka-256s.h b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-256s.h new file mode 100644 index 0000000..6ef3a5c --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-haraka-256s.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 32 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 64 +/* Number of subtree layer. */ +#define SPX_D 8 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 14 +#define SPX_FORS_TREES 22 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../haraka_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-sha2-128f.h b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-128f.h new file mode 100644 index 0000000..3f7a60f --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-128f.h @@ -0,0 +1,85 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 16 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 66 +/* Number of subtree layer. */ +#define SPX_D 22 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 6 +#define SPX_FORS_TREES 33 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* This is a SHA2-based parameter set, hence whether we use SHA-256 + * exclusively or we use both SHA-256 and SHA-512 is controlled by + * the following #define */ +#define SPX_SHA512 0 /* Use SHA-256 for all hashes */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../sha2_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-sha2-128s.h b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-128s.h new file mode 100644 index 0000000..8ef8608 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-128s.h @@ -0,0 +1,85 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 16 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 63 +/* Number of subtree layer. */ +#define SPX_D 7 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 12 +#define SPX_FORS_TREES 14 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* This is a SHA2-based parameter set, hence whether we use SHA-256 + * exclusively or we use both SHA-256 and SHA-512 is controlled by + * the following #define */ +#define SPX_SHA512 0 /* Use SHA-256 for all hashes */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../sha2_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-sha2-192f.h b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-192f.h new file mode 100644 index 0000000..d4d98a2 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-192f.h @@ -0,0 +1,85 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 24 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 66 +/* Number of subtree layer. */ +#define SPX_D 22 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 8 +#define SPX_FORS_TREES 33 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* This is a SHA2-based parameter set, hence whether we use SHA-256 + * exclusively or we use both SHA-256 and SHA-512 is controlled by + * the following #define */ +#define SPX_SHA512 1 /* Use SHA-512 for H and T_l, l >= 2 */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../sha2_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-sha2-192s.h b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-192s.h new file mode 100644 index 0000000..0070e59 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-192s.h @@ -0,0 +1,85 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 24 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 63 +/* Number of subtree layer. */ +#define SPX_D 7 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 14 +#define SPX_FORS_TREES 17 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* This is a SHA2-based parameter set, hence whether we use SHA-256 + * exclusively or we use both SHA-256 and SHA-512 is controlled by + * the following #define */ +#define SPX_SHA512 1 /* Use SHA-512 for H and T_l, l >= 2 */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../sha2_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-sha2-256f.h b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-256f.h new file mode 100644 index 0000000..53c5bef --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-256f.h @@ -0,0 +1,85 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 32 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 68 +/* Number of subtree layer. */ +#define SPX_D 17 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 9 +#define SPX_FORS_TREES 35 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* This is a SHA2-based parameter set, hence whether we use SHA-256 + * exclusively or we use both SHA-256 and SHA-512 is controlled by + * the following #define */ +#define SPX_SHA512 1 /* Use SHA-512 for H and T_l, l >= 2 */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../sha2_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-sha2-256s.h b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-256s.h new file mode 100644 index 0000000..10c3663 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-sha2-256s.h @@ -0,0 +1,85 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 32 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 64 +/* Number of subtree layer. */ +#define SPX_D 8 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 14 +#define SPX_FORS_TREES 22 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* This is a SHA2-based parameter set, hence whether we use SHA-256 + * exclusively or we use both SHA-256 and SHA-512 is controlled by + * the following #define */ +#define SPX_SHA512 1 /* Use SHA-512 for H and T_l, l >= 2 */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../sha2_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-shake-128f.h b/src/crypto/pq/_s/ref/params/params-sphincs-shake-128f.h new file mode 100644 index 0000000..8f77692 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-shake-128f.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 16 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 66 +/* Number of subtree layer. */ +#define SPX_D 22 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 6 +#define SPX_FORS_TREES 33 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../shake_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-shake-128s.h b/src/crypto/pq/_s/ref/params/params-sphincs-shake-128s.h new file mode 100644 index 0000000..a4d1e13 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-shake-128s.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 16 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 63 +/* Number of subtree layer. */ +#define SPX_D 7 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 12 +#define SPX_FORS_TREES 14 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../shake_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-shake-192f.h b/src/crypto/pq/_s/ref/params/params-sphincs-shake-192f.h new file mode 100644 index 0000000..b1e73d1 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-shake-192f.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 24 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 66 +/* Number of subtree layer. */ +#define SPX_D 22 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 8 +#define SPX_FORS_TREES 33 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../shake_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-shake-192s.h b/src/crypto/pq/_s/ref/params/params-sphincs-shake-192s.h new file mode 100644 index 0000000..0882e1c --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-shake-192s.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 24 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 63 +/* Number of subtree layer. */ +#define SPX_D 7 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 14 +#define SPX_FORS_TREES 17 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../shake_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-shake-256f.h b/src/crypto/pq/_s/ref/params/params-sphincs-shake-256f.h new file mode 100644 index 0000000..e301c28 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-shake-256f.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 32 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 68 +/* Number of subtree layer. */ +#define SPX_D 17 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 9 +#define SPX_FORS_TREES 35 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../shake_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/params/params-sphincs-shake-256s.h b/src/crypto/pq/_s/ref/params/params-sphincs-shake-256s.h new file mode 100644 index 0000000..0a96894 --- /dev/null +++ b/src/crypto/pq/_s/ref/params/params-sphincs-shake-256s.h @@ -0,0 +1,80 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 32 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 64 +/* Number of subtree layer. */ +#define SPX_D 8 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 14 +#define SPX_FORS_TREES 22 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../shake_offsets.h" + +#endif diff --git a/src/crypto/pq/_s/ref/randombytes.c b/src/crypto/pq/_s/ref/randombytes.c new file mode 100644 index 0000000..cfbca17 --- /dev/null +++ b/src/crypto/pq/_s/ref/randombytes.c @@ -0,0 +1,43 @@ +/* +This code was taken from the SPHINCS reference implementation and is public domain. +*/ + +#include +#include + +#include "randombytes.h" + +static int fd = -1; + +void randombytes(unsigned char *x, unsigned long long xlen) +{ + unsigned long long i; + + if (fd == -1) { + for (;;) { + fd = open("/dev/urandom", O_RDONLY); + if (fd != -1) { + break; + } + sleep(1); + } + } + + while (xlen > 0) { + if (xlen < 1048576) { + i = xlen; + } + else { + i = 1048576; + } + + i = (unsigned long long)read(fd, x, i); + if (i < 1) { + sleep(1); + continue; + } + + x += i; + xlen -= i; + } +} diff --git a/src/crypto/pq/_s/ref/randombytes.h b/src/crypto/pq/_s/ref/randombytes.h new file mode 100644 index 0000000..671c1b1 --- /dev/null +++ b/src/crypto/pq/_s/ref/randombytes.h @@ -0,0 +1,6 @@ +#ifndef SPX_RANDOMBYTES_H +#define SPX_RANDOMBYTES_H + +extern void randombytes(unsigned char * x,unsigned long long xlen); + +#endif diff --git a/src/crypto/pq/_s/ref/rng.c b/src/crypto/pq/_s/ref/rng.c new file mode 100644 index 0000000..36f5b27 --- /dev/null +++ b/src/crypto/pq/_s/ref/rng.c @@ -0,0 +1,218 @@ +// +// rng.c +// +// Created by Bassham, Lawrence E (Fed) on 8/29/17. +// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved. +// + +#include +#include "rng.h" +#include +#include +#include + +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; + + memcpy(ctx->key, seed, 32); + + memcpy(ctx->ctr, diversifier, 8); + ctx->ctr[11] = (unsigned char)(maxlen % 256); + maxlen >>= 8; + ctx->ctr[10] = (unsigned char)(maxlen % 256); + maxlen >>= 8; + ctx->ctr[9] = (unsigned char)(maxlen % 256); + maxlen >>= 8; + ctx->ctr[8] = (unsigned char)(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; +} + + +static void handleErrors(void) +{ + ERR_print_errors_fp(stderr); + abort(); +} + +// Use whatever AES implementation you have. This uses AES from openSSL library +// key - 256-bit AES key +// ctr - a 128-bit plaintext value +// buffer - a 128-bit ciphertext value +void +AES256_ECB(unsigned char *key, unsigned char *ctr, unsigned char *buffer) +{ + EVP_CIPHER_CTX *ctx; + + int len; + + /* Create and initialise the context */ + if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); + + if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key, NULL)) + handleErrors(); + + if(1 != EVP_EncryptUpdate(ctx, buffer, &len, ctr, 16)) + handleErrors(); + + /* Clean up */ + EVP_CIPHER_CTX_free(ctx); +} + +void +randombytes_init(unsigned char *entropy_input, + unsigned char *personalization_string) +{ + 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); +} + + + + + + + + + diff --git a/src/crypto/pq/_s/ref/rng.h b/src/crypto/pq/_s/ref/rng.h new file mode 100644 index 0000000..c4f1c60 --- /dev/null +++ b/src/crypto/pq/_s/ref/rng.h @@ -0,0 +1,54 @@ +// +// rng.h +// +// Created by Bassham, Lawrence E (Fed) on 8/29/17. +// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved. +// + +#ifndef rng_h +#define rng_h + +#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 long 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 +randombytes(unsigned char *x, unsigned long long xlen); + +#endif /* rng_h */ diff --git a/src/crypto/pq/_s/ref/sha2.c b/src/crypto/pq/_s/ref/sha2.c new file mode 100644 index 0000000..ef73047 --- /dev/null +++ b/src/crypto/pq/_s/ref/sha2.c @@ -0,0 +1,700 @@ +/* Based on the public domain implementation in + * crypto_hash/sha512/ref/ from http://bench.cr.yp.to/supercop.html + * by D. J. Bernstein */ + +#include +#include +#include + +#include "utils.h" +#include "sha2.h" + +static uint32_t load_bigendian_32(const uint8_t *x) { + return (uint32_t)(x[3]) | (((uint32_t)(x[2])) << 8) | + (((uint32_t)(x[1])) << 16) | (((uint32_t)(x[0])) << 24); +} + +static uint64_t load_bigendian_64(const uint8_t *x) { + return (uint64_t)(x[7]) | (((uint64_t)(x[6])) << 8) | + (((uint64_t)(x[5])) << 16) | (((uint64_t)(x[4])) << 24) | + (((uint64_t)(x[3])) << 32) | (((uint64_t)(x[2])) << 40) | + (((uint64_t)(x[1])) << 48) | (((uint64_t)(x[0])) << 56); +} + +static void store_bigendian_32(uint8_t *x, uint64_t u) { + x[3] = (uint8_t) u; + u >>= 8; + x[2] = (uint8_t) u; + u >>= 8; + x[1] = (uint8_t) u; + u >>= 8; + x[0] = (uint8_t) u; +} + +static void store_bigendian_64(uint8_t *x, uint64_t u) { + x[7] = (uint8_t) u; + u >>= 8; + x[6] = (uint8_t) u; + u >>= 8; + x[5] = (uint8_t) u; + u >>= 8; + x[4] = (uint8_t) u; + u >>= 8; + x[3] = (uint8_t) u; + u >>= 8; + x[2] = (uint8_t) u; + u >>= 8; + x[1] = (uint8_t) u; + u >>= 8; + x[0] = (uint8_t) u; +} + +#define SHR(x, c) ((x) >> (c)) +#define ROTR_32(x, c) (((x) >> (c)) | ((x) << (32 - (c)))) +#define ROTR_64(x,c) (((x) >> (c)) | ((x) << (64 - (c)))) + +#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z))) +#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) + +#define Sigma0_32(x) (ROTR_32(x, 2) ^ ROTR_32(x,13) ^ ROTR_32(x,22)) +#define Sigma1_32(x) (ROTR_32(x, 6) ^ ROTR_32(x,11) ^ ROTR_32(x,25)) +#define sigma0_32(x) (ROTR_32(x, 7) ^ ROTR_32(x,18) ^ SHR(x, 3)) +#define sigma1_32(x) (ROTR_32(x,17) ^ ROTR_32(x,19) ^ SHR(x,10)) + +#define Sigma0_64(x) (ROTR_64(x,28) ^ ROTR_64(x,34) ^ ROTR_64(x,39)) +#define Sigma1_64(x) (ROTR_64(x,14) ^ ROTR_64(x,18) ^ ROTR_64(x,41)) +#define sigma0_64(x) (ROTR_64(x, 1) ^ ROTR_64(x, 8) ^ SHR(x,7)) +#define sigma1_64(x) (ROTR_64(x,19) ^ ROTR_64(x,61) ^ SHR(x,6)) + +#define M_32(w0, w14, w9, w1) w0 = sigma1_32(w14) + (w9) + sigma0_32(w1) + (w0); +#define M_64(w0, w14, w9, w1) w0 = sigma1_64(w14) + (w9) + sigma0_64(w1) + (w0); + +#define EXPAND_32 \ + M_32(w0, w14, w9, w1) \ + M_32(w1, w15, w10, w2) \ + M_32(w2, w0, w11, w3) \ + M_32(w3, w1, w12, w4) \ + M_32(w4, w2, w13, w5) \ + M_32(w5, w3, w14, w6) \ + M_32(w6, w4, w15, w7) \ + M_32(w7, w5, w0, w8) \ + M_32(w8, w6, w1, w9) \ + M_32(w9, w7, w2, w10) \ + M_32(w10, w8, w3, w11) \ + M_32(w11, w9, w4, w12) \ + M_32(w12, w10, w5, w13) \ + M_32(w13, w11, w6, w14) \ + M_32(w14, w12, w7, w15) \ + M_32(w15, w13, w8, w0) + +#define EXPAND_64 \ + M_64(w0 ,w14,w9 ,w1 ) \ + M_64(w1 ,w15,w10,w2 ) \ + M_64(w2 ,w0 ,w11,w3 ) \ + M_64(w3 ,w1 ,w12,w4 ) \ + M_64(w4 ,w2 ,w13,w5 ) \ + M_64(w5 ,w3 ,w14,w6 ) \ + M_64(w6 ,w4 ,w15,w7 ) \ + M_64(w7 ,w5 ,w0 ,w8 ) \ + M_64(w8 ,w6 ,w1 ,w9 ) \ + M_64(w9 ,w7 ,w2 ,w10) \ + M_64(w10,w8 ,w3 ,w11) \ + M_64(w11,w9 ,w4 ,w12) \ + M_64(w12,w10,w5 ,w13) \ + M_64(w13,w11,w6 ,w14) \ + M_64(w14,w12,w7 ,w15) \ + M_64(w15,w13,w8 ,w0 ) + +#define F_32(w, k) \ + T1 = h + Sigma1_32(e) + Ch(e, f, g) + (k) + (w); \ + T2 = Sigma0_32(a) + Maj(a, b, c); \ + h = g; \ + g = f; \ + f = e; \ + e = d + T1; \ + d = c; \ + c = b; \ + b = a; \ + a = T1 + T2; + +#define F_64(w,k) \ + T1 = h + Sigma1_64(e) + Ch(e,f,g) + k + w; \ + T2 = Sigma0_64(a) + Maj(a,b,c); \ + h = g; \ + g = f; \ + f = e; \ + e = d + T1; \ + d = c; \ + c = b; \ + b = a; \ + a = T1 + T2; + +static size_t crypto_hashblocks_sha256(uint8_t *statebytes, + const uint8_t *in, size_t inlen) { + uint32_t state[8]; + uint32_t a; + uint32_t b; + uint32_t c; + uint32_t d; + uint32_t e; + uint32_t f; + uint32_t g; + uint32_t h; + uint32_t T1; + uint32_t T2; + + a = load_bigendian_32(statebytes + 0); + state[0] = a; + b = load_bigendian_32(statebytes + 4); + state[1] = b; + c = load_bigendian_32(statebytes + 8); + state[2] = c; + d = load_bigendian_32(statebytes + 12); + state[3] = d; + e = load_bigendian_32(statebytes + 16); + state[4] = e; + f = load_bigendian_32(statebytes + 20); + state[5] = f; + g = load_bigendian_32(statebytes + 24); + state[6] = g; + h = load_bigendian_32(statebytes + 28); + state[7] = h; + + while (inlen >= 64) { + uint32_t w0 = load_bigendian_32(in + 0); + uint32_t w1 = load_bigendian_32(in + 4); + uint32_t w2 = load_bigendian_32(in + 8); + uint32_t w3 = load_bigendian_32(in + 12); + uint32_t w4 = load_bigendian_32(in + 16); + uint32_t w5 = load_bigendian_32(in + 20); + uint32_t w6 = load_bigendian_32(in + 24); + uint32_t w7 = load_bigendian_32(in + 28); + uint32_t w8 = load_bigendian_32(in + 32); + uint32_t w9 = load_bigendian_32(in + 36); + uint32_t w10 = load_bigendian_32(in + 40); + uint32_t w11 = load_bigendian_32(in + 44); + uint32_t w12 = load_bigendian_32(in + 48); + uint32_t w13 = load_bigendian_32(in + 52); + uint32_t w14 = load_bigendian_32(in + 56); + uint32_t w15 = load_bigendian_32(in + 60); + + F_32(w0, 0x428a2f98) + F_32(w1, 0x71374491) + F_32(w2, 0xb5c0fbcf) + F_32(w3, 0xe9b5dba5) + F_32(w4, 0x3956c25b) + F_32(w5, 0x59f111f1) + F_32(w6, 0x923f82a4) + F_32(w7, 0xab1c5ed5) + F_32(w8, 0xd807aa98) + F_32(w9, 0x12835b01) + F_32(w10, 0x243185be) + F_32(w11, 0x550c7dc3) + F_32(w12, 0x72be5d74) + F_32(w13, 0x80deb1fe) + F_32(w14, 0x9bdc06a7) + F_32(w15, 0xc19bf174) + + EXPAND_32 + + F_32(w0, 0xe49b69c1) + F_32(w1, 0xefbe4786) + F_32(w2, 0x0fc19dc6) + F_32(w3, 0x240ca1cc) + F_32(w4, 0x2de92c6f) + F_32(w5, 0x4a7484aa) + F_32(w6, 0x5cb0a9dc) + F_32(w7, 0x76f988da) + F_32(w8, 0x983e5152) + F_32(w9, 0xa831c66d) + F_32(w10, 0xb00327c8) + F_32(w11, 0xbf597fc7) + F_32(w12, 0xc6e00bf3) + F_32(w13, 0xd5a79147) + F_32(w14, 0x06ca6351) + F_32(w15, 0x14292967) + + EXPAND_32 + + F_32(w0, 0x27b70a85) + F_32(w1, 0x2e1b2138) + F_32(w2, 0x4d2c6dfc) + F_32(w3, 0x53380d13) + F_32(w4, 0x650a7354) + F_32(w5, 0x766a0abb) + F_32(w6, 0x81c2c92e) + F_32(w7, 0x92722c85) + F_32(w8, 0xa2bfe8a1) + F_32(w9, 0xa81a664b) + F_32(w10, 0xc24b8b70) + F_32(w11, 0xc76c51a3) + F_32(w12, 0xd192e819) + F_32(w13, 0xd6990624) + F_32(w14, 0xf40e3585) + F_32(w15, 0x106aa070) + + EXPAND_32 + + F_32(w0, 0x19a4c116) + F_32(w1, 0x1e376c08) + F_32(w2, 0x2748774c) + F_32(w3, 0x34b0bcb5) + F_32(w4, 0x391c0cb3) + F_32(w5, 0x4ed8aa4a) + F_32(w6, 0x5b9cca4f) + F_32(w7, 0x682e6ff3) + F_32(w8, 0x748f82ee) + F_32(w9, 0x78a5636f) + F_32(w10, 0x84c87814) + F_32(w11, 0x8cc70208) + F_32(w12, 0x90befffa) + F_32(w13, 0xa4506ceb) + F_32(w14, 0xbef9a3f7) + F_32(w15, 0xc67178f2) + + a += state[0]; + b += state[1]; + c += state[2]; + d += state[3]; + e += state[4]; + f += state[5]; + g += state[6]; + h += state[7]; + + state[0] = a; + state[1] = b; + state[2] = c; + state[3] = d; + state[4] = e; + state[5] = f; + state[6] = g; + state[7] = h; + + in += 64; + inlen -= 64; + } + + store_bigendian_32(statebytes + 0, state[0]); + store_bigendian_32(statebytes + 4, state[1]); + store_bigendian_32(statebytes + 8, state[2]); + store_bigendian_32(statebytes + 12, state[3]); + store_bigendian_32(statebytes + 16, state[4]); + store_bigendian_32(statebytes + 20, state[5]); + store_bigendian_32(statebytes + 24, state[6]); + store_bigendian_32(statebytes + 28, state[7]); + + return inlen; +} + +static int crypto_hashblocks_sha512(unsigned char *statebytes,const unsigned char *in,unsigned long long inlen) +{ + uint64_t state[8]; + uint64_t a; + uint64_t b; + uint64_t c; + uint64_t d; + uint64_t e; + uint64_t f; + uint64_t g; + uint64_t h; + uint64_t T1; + uint64_t T2; + + a = load_bigendian_64(statebytes + 0); state[0] = a; + b = load_bigendian_64(statebytes + 8); state[1] = b; + c = load_bigendian_64(statebytes + 16); state[2] = c; + d = load_bigendian_64(statebytes + 24); state[3] = d; + e = load_bigendian_64(statebytes + 32); state[4] = e; + f = load_bigendian_64(statebytes + 40); state[5] = f; + g = load_bigendian_64(statebytes + 48); state[6] = g; + h = load_bigendian_64(statebytes + 56); state[7] = h; + + while (inlen >= 128) { + uint64_t w0 = load_bigendian_64(in + 0); + uint64_t w1 = load_bigendian_64(in + 8); + uint64_t w2 = load_bigendian_64(in + 16); + uint64_t w3 = load_bigendian_64(in + 24); + uint64_t w4 = load_bigendian_64(in + 32); + uint64_t w5 = load_bigendian_64(in + 40); + uint64_t w6 = load_bigendian_64(in + 48); + uint64_t w7 = load_bigendian_64(in + 56); + uint64_t w8 = load_bigendian_64(in + 64); + uint64_t w9 = load_bigendian_64(in + 72); + uint64_t w10 = load_bigendian_64(in + 80); + uint64_t w11 = load_bigendian_64(in + 88); + uint64_t w12 = load_bigendian_64(in + 96); + uint64_t w13 = load_bigendian_64(in + 104); + uint64_t w14 = load_bigendian_64(in + 112); + uint64_t w15 = load_bigendian_64(in + 120); + + F_64(w0 ,0x428a2f98d728ae22ULL) + F_64(w1 ,0x7137449123ef65cdULL) + F_64(w2 ,0xb5c0fbcfec4d3b2fULL) + F_64(w3 ,0xe9b5dba58189dbbcULL) + F_64(w4 ,0x3956c25bf348b538ULL) + F_64(w5 ,0x59f111f1b605d019ULL) + F_64(w6 ,0x923f82a4af194f9bULL) + F_64(w7 ,0xab1c5ed5da6d8118ULL) + F_64(w8 ,0xd807aa98a3030242ULL) + F_64(w9 ,0x12835b0145706fbeULL) + F_64(w10,0x243185be4ee4b28cULL) + F_64(w11,0x550c7dc3d5ffb4e2ULL) + F_64(w12,0x72be5d74f27b896fULL) + F_64(w13,0x80deb1fe3b1696b1ULL) + F_64(w14,0x9bdc06a725c71235ULL) + F_64(w15,0xc19bf174cf692694ULL) + + EXPAND_64 + + F_64(w0 ,0xe49b69c19ef14ad2ULL) + F_64(w1 ,0xefbe4786384f25e3ULL) + F_64(w2 ,0x0fc19dc68b8cd5b5ULL) + F_64(w3 ,0x240ca1cc77ac9c65ULL) + F_64(w4 ,0x2de92c6f592b0275ULL) + F_64(w5 ,0x4a7484aa6ea6e483ULL) + F_64(w6 ,0x5cb0a9dcbd41fbd4ULL) + F_64(w7 ,0x76f988da831153b5ULL) + F_64(w8 ,0x983e5152ee66dfabULL) + F_64(w9 ,0xa831c66d2db43210ULL) + F_64(w10,0xb00327c898fb213fULL) + F_64(w11,0xbf597fc7beef0ee4ULL) + F_64(w12,0xc6e00bf33da88fc2ULL) + F_64(w13,0xd5a79147930aa725ULL) + F_64(w14,0x06ca6351e003826fULL) + F_64(w15,0x142929670a0e6e70ULL) + + EXPAND_64 + + F_64(w0 ,0x27b70a8546d22ffcULL) + F_64(w1 ,0x2e1b21385c26c926ULL) + F_64(w2 ,0x4d2c6dfc5ac42aedULL) + F_64(w3 ,0x53380d139d95b3dfULL) + F_64(w4 ,0x650a73548baf63deULL) + F_64(w5 ,0x766a0abb3c77b2a8ULL) + F_64(w6 ,0x81c2c92e47edaee6ULL) + F_64(w7 ,0x92722c851482353bULL) + F_64(w8 ,0xa2bfe8a14cf10364ULL) + F_64(w9 ,0xa81a664bbc423001ULL) + F_64(w10,0xc24b8b70d0f89791ULL) + F_64(w11,0xc76c51a30654be30ULL) + F_64(w12,0xd192e819d6ef5218ULL) + F_64(w13,0xd69906245565a910ULL) + F_64(w14,0xf40e35855771202aULL) + F_64(w15,0x106aa07032bbd1b8ULL) + + EXPAND_64 + + F_64(w0 ,0x19a4c116b8d2d0c8ULL) + F_64(w1 ,0x1e376c085141ab53ULL) + F_64(w2 ,0x2748774cdf8eeb99ULL) + F_64(w3 ,0x34b0bcb5e19b48a8ULL) + F_64(w4 ,0x391c0cb3c5c95a63ULL) + F_64(w5 ,0x4ed8aa4ae3418acbULL) + F_64(w6 ,0x5b9cca4f7763e373ULL) + F_64(w7 ,0x682e6ff3d6b2b8a3ULL) + F_64(w8 ,0x748f82ee5defb2fcULL) + F_64(w9 ,0x78a5636f43172f60ULL) + F_64(w10,0x84c87814a1f0ab72ULL) + F_64(w11,0x8cc702081a6439ecULL) + F_64(w12,0x90befffa23631e28ULL) + F_64(w13,0xa4506cebde82bde9ULL) + F_64(w14,0xbef9a3f7b2c67915ULL) + F_64(w15,0xc67178f2e372532bULL) + + EXPAND_64 + + F_64(w0 ,0xca273eceea26619cULL) + F_64(w1 ,0xd186b8c721c0c207ULL) + F_64(w2 ,0xeada7dd6cde0eb1eULL) + F_64(w3 ,0xf57d4f7fee6ed178ULL) + F_64(w4 ,0x06f067aa72176fbaULL) + F_64(w5 ,0x0a637dc5a2c898a6ULL) + F_64(w6 ,0x113f9804bef90daeULL) + F_64(w7 ,0x1b710b35131c471bULL) + F_64(w8 ,0x28db77f523047d84ULL) + F_64(w9 ,0x32caab7b40c72493ULL) + F_64(w10,0x3c9ebe0a15c9bebcULL) + F_64(w11,0x431d67c49c100d4cULL) + F_64(w12,0x4cc5d4becb3e42b6ULL) + F_64(w13,0x597f299cfc657e2aULL) + F_64(w14,0x5fcb6fab3ad6faecULL) + F_64(w15,0x6c44198c4a475817ULL) + + a += state[0]; + b += state[1]; + c += state[2]; + d += state[3]; + e += state[4]; + f += state[5]; + g += state[6]; + h += state[7]; + + state[0] = a; + state[1] = b; + state[2] = c; + state[3] = d; + state[4] = e; + state[5] = f; + state[6] = g; + state[7] = h; + + in += 128; + inlen -= 128; + } + + store_bigendian_64(statebytes + 0,state[0]); + store_bigendian_64(statebytes + 8,state[1]); + store_bigendian_64(statebytes + 16,state[2]); + store_bigendian_64(statebytes + 24,state[3]); + store_bigendian_64(statebytes + 32,state[4]); + store_bigendian_64(statebytes + 40,state[5]); + store_bigendian_64(statebytes + 48,state[6]); + store_bigendian_64(statebytes + 56,state[7]); + + return inlen; +} + + +static const uint8_t iv_256[32] = { + 0x6a, 0x09, 0xe6, 0x67, 0xbb, 0x67, 0xae, 0x85, + 0x3c, 0x6e, 0xf3, 0x72, 0xa5, 0x4f, 0xf5, 0x3a, + 0x51, 0x0e, 0x52, 0x7f, 0x9b, 0x05, 0x68, 0x8c, + 0x1f, 0x83, 0xd9, 0xab, 0x5b, 0xe0, 0xcd, 0x19 +}; + +static const uint8_t iv_512[64] = { + 0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08, 0xbb, 0x67, 0xae, + 0x85, 0x84, 0xca, 0xa7, 0x3b, 0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94, + 0xf8, 0x2b, 0xa5, 0x4f, 0xf5, 0x3a, 0x5f, 0x1d, 0x36, 0xf1, 0x51, + 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1, 0x9b, 0x05, 0x68, 0x8c, + 0x2b, 0x3e, 0x6c, 0x1f, 0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd, + 0x6b, 0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79 +}; + +void sha256_inc_init(uint8_t *state) { + for (size_t i = 0; i < 32; ++i) { + state[i] = iv_256[i]; + } + for (size_t i = 32; i < 40; ++i) { + state[i] = 0; + } +} + +void sha512_inc_init(uint8_t *state) { + for (size_t i = 0; i < 64; ++i) { + state[i] = iv_512[i]; + } + for (size_t i = 64; i < 72; ++i) { + state[i] = 0; + } +} + +void sha256_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks) { + uint64_t bytes = load_bigendian_64(state + 32); + + crypto_hashblocks_sha256(state, in, 64 * inblocks); + bytes += 64 * inblocks; + + store_bigendian_64(state + 32, bytes); +} + +void sha512_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks) { + uint64_t bytes = load_bigendian_64(state + 64); + + crypto_hashblocks_sha512(state, in, 128 * inblocks); + bytes += 128 * inblocks; + + store_bigendian_64(state + 64, bytes); +} + +void sha256_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen) { + uint8_t padded[128]; + uint64_t bytes = load_bigendian_64(state + 32) + inlen; + + crypto_hashblocks_sha256(state, in, inlen); + in += inlen; + inlen &= 63; + in -= inlen; + + for (size_t i = 0; i < inlen; ++i) { + padded[i] = in[i]; + } + padded[inlen] = 0x80; + + if (inlen < 56) { + for (size_t i = inlen + 1; i < 56; ++i) { + padded[i] = 0; + } + padded[56] = (uint8_t) (bytes >> 53); + padded[57] = (uint8_t) (bytes >> 45); + padded[58] = (uint8_t) (bytes >> 37); + padded[59] = (uint8_t) (bytes >> 29); + padded[60] = (uint8_t) (bytes >> 21); + padded[61] = (uint8_t) (bytes >> 13); + padded[62] = (uint8_t) (bytes >> 5); + padded[63] = (uint8_t) (bytes << 3); + crypto_hashblocks_sha256(state, padded, 64); + } else { + for (size_t i = inlen + 1; i < 120; ++i) { + padded[i] = 0; + } + padded[120] = (uint8_t) (bytes >> 53); + padded[121] = (uint8_t) (bytes >> 45); + padded[122] = (uint8_t) (bytes >> 37); + padded[123] = (uint8_t) (bytes >> 29); + padded[124] = (uint8_t) (bytes >> 21); + padded[125] = (uint8_t) (bytes >> 13); + padded[126] = (uint8_t) (bytes >> 5); + padded[127] = (uint8_t) (bytes << 3); + crypto_hashblocks_sha256(state, padded, 128); + } + + for (size_t i = 0; i < 32; ++i) { + out[i] = state[i]; + } + +} + +void sha512_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen) { + uint8_t padded[256]; + uint64_t bytes = load_bigendian_64(state + 64) + inlen; + + crypto_hashblocks_sha512(state, in, inlen); + in += inlen; + inlen &= 127; + in -= inlen; + + for (size_t i = 0; i < inlen; ++i) { + padded[i] = in[i]; + } + padded[inlen] = 0x80; + + if (inlen < 112) { + for (size_t i = inlen + 1; i < 119; ++i) { + padded[i] = 0; + } + padded[119] = (uint8_t) (bytes >> 61); + padded[120] = (uint8_t) (bytes >> 53); + padded[121] = (uint8_t) (bytes >> 45); + padded[122] = (uint8_t) (bytes >> 37); + padded[123] = (uint8_t) (bytes >> 29); + padded[124] = (uint8_t) (bytes >> 21); + padded[125] = (uint8_t) (bytes >> 13); + padded[126] = (uint8_t) (bytes >> 5); + padded[127] = (uint8_t) (bytes << 3); + crypto_hashblocks_sha512(state, padded, 128); + } else { + for (size_t i = inlen + 1; i < 247; ++i) { + padded[i] = 0; + } + padded[247] = (uint8_t) (bytes >> 61); + padded[248] = (uint8_t) (bytes >> 53); + padded[249] = (uint8_t) (bytes >> 45); + padded[250] = (uint8_t) (bytes >> 37); + padded[251] = (uint8_t) (bytes >> 29); + padded[252] = (uint8_t) (bytes >> 21); + padded[253] = (uint8_t) (bytes >> 13); + padded[254] = (uint8_t) (bytes >> 5); + padded[255] = (uint8_t) (bytes << 3); + crypto_hashblocks_sha512(state, padded, 256); + } + + for (size_t i = 0; i < 64; ++i) { + out[i] = state[i]; + } +} + +void sha256(uint8_t *out, const uint8_t *in, size_t inlen) { + uint8_t state[40]; + + sha256_inc_init(state); + sha256_inc_finalize(out, state, in, inlen); +} + +void sha512(uint8_t *out, const uint8_t *in, size_t inlen) { + uint8_t state[72]; + + sha512_inc_init(state); + sha512_inc_finalize(out, state, in, inlen); +} + +/** + * mgf1 function based on the SHA-256 hash function + * Note that inlen should be sufficiently small that it still allows for + * an array to be allocated on the stack. Typically 'in' is merely a seed. + * Outputs outlen number of bytes + */ +void mgf1_256(unsigned char *out, unsigned long outlen, + const unsigned char *in, unsigned long inlen) +{ + SPX_VLA(uint8_t, inbuf, inlen+4); + unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES]; + unsigned long i; + + memcpy(inbuf, in, inlen); + + /* While we can fit in at least another full block of SHA256 output.. */ + for (i = 0; (i+1)*SPX_SHA256_OUTPUT_BYTES <= outlen; i++) { + u32_to_bytes(inbuf + inlen, i); + sha256(out, inbuf, inlen + 4); + out += SPX_SHA256_OUTPUT_BYTES; + } + /* Until we cannot anymore, and we fill the remainder. */ + if (outlen > i*SPX_SHA256_OUTPUT_BYTES) { + u32_to_bytes(inbuf + inlen, i); + sha256(outbuf, inbuf, inlen + 4); + memcpy(out, outbuf, outlen - i*SPX_SHA256_OUTPUT_BYTES); + } +} + +/* + * mgf1 function based on the SHA-512 hash function + */ +void mgf1_512(unsigned char *out, unsigned long outlen, + const unsigned char *in, unsigned long inlen) +{ + SPX_VLA(uint8_t, inbuf, inlen+4); + unsigned char outbuf[SPX_SHA512_OUTPUT_BYTES]; + unsigned long i; + + memcpy(inbuf, in, inlen); + + /* While we can fit in at least another full block of SHA512 output.. */ + for (i = 0; (i+1)*SPX_SHA512_OUTPUT_BYTES <= outlen; i++) { + u32_to_bytes(inbuf + inlen, i); + sha512(out, inbuf, inlen + 4); + out += SPX_SHA512_OUTPUT_BYTES; + } + /* Until we cannot anymore, and we fill the remainder. */ + if (outlen > i*SPX_SHA512_OUTPUT_BYTES) { + u32_to_bytes(inbuf + inlen, i); + sha512(outbuf, inbuf, inlen + 4); + memcpy(out, outbuf, outlen - i*SPX_SHA512_OUTPUT_BYTES); + } +} + + +/** + * Absorb the constant pub_seed using one round of the compression function + * This initializes state_seeded and state_seeded_512, which can then be + * reused in thash + **/ +void seed_state(spx_ctx *ctx) { + uint8_t block[SPX_SHA512_BLOCK_BYTES]; + size_t i; + + for (i = 0; i < SPX_N; ++i) { + block[i] = ctx->pub_seed[i]; + } + for (i = SPX_N; i < SPX_SHA512_BLOCK_BYTES; ++i) { + block[i] = 0; + } + /* block has been properly initialized for both SHA-256 and SHA-512 */ + + sha256_inc_init(ctx->state_seeded); + sha256_inc_blocks(ctx->state_seeded, block, 1); +#if SPX_SHA512 + sha512_inc_init(ctx->state_seeded_512); + sha512_inc_blocks(ctx->state_seeded_512, block, 1); +#endif +} diff --git a/src/crypto/pq/_s/ref/sha2.h b/src/crypto/pq/_s/ref/sha2.h new file mode 100644 index 0000000..732ab4b --- /dev/null +++ b/src/crypto/pq/_s/ref/sha2.h @@ -0,0 +1,43 @@ +#ifndef SPX_SHA2_H +#define SPX_SHA2_H + +#include "params.h" + +#define SPX_SHA256_BLOCK_BYTES 64 +#define SPX_SHA256_OUTPUT_BYTES 32 /* This does not necessarily equal SPX_N */ + +#define SPX_SHA512_BLOCK_BYTES 128 +#define SPX_SHA512_OUTPUT_BYTES 64 + +#if SPX_SHA256_OUTPUT_BYTES < SPX_N + #error Linking against SHA-256 with N larger than 32 bytes is not supported +#endif + +#define SPX_SHA256_ADDR_BYTES 22 + +#include +#include + +void sha256_inc_init(uint8_t *state); +void sha256_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks); +void sha256_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen); +void sha256(uint8_t *out, const uint8_t *in, size_t inlen); + +void sha512_inc_init(uint8_t *state); +void sha512_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks); +void sha512_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen); +void sha512(uint8_t *out, const uint8_t *in, size_t inlen); + +#define mgf1_256 SPX_NAMESPACE(mgf1_256) +void mgf1_256(unsigned char *out, unsigned long outlen, + const unsigned char *in, unsigned long inlen); + +#define mgf1_512 SPX_NAMESPACE(mgf1_512) +void mgf1_512(unsigned char *out, unsigned long outlen, + const unsigned char *in, unsigned long inlen); + +#define seed_state SPX_NAMESPACE(seed_state) +void seed_state(spx_ctx *ctx); + + +#endif diff --git a/src/crypto/pq/_s/ref/sha2_offsets.h b/src/crypto/pq/_s/ref/sha2_offsets.h new file mode 100644 index 0000000..49f7e85 --- /dev/null +++ b/src/crypto/pq/_s/ref/sha2_offsets.h @@ -0,0 +1,20 @@ +#ifndef SHA2_OFFSETS_H_ +#define SHA2_OFFSETS_H_ + +/* + * Offsets of various fields in the address structure when we use SHA2 as + * the Sphincs+ hash function + */ + +#define SPX_OFFSET_LAYER 0 /* The byte used to specify the Merkle tree layer */ +#define SPX_OFFSET_TREE 1 /* The start of the 8 byte field used to specify the tree */ +#define SPX_OFFSET_TYPE 9 /* The byte used to specify the hash type (reason) */ +#define SPX_OFFSET_KP_ADDR 10 /* The start of the 4 byte field used to specify the key pair address */ +#define SPX_OFFSET_CHAIN_ADDR 17 /* The byte used to specify the chain address (which Winternitz chain) */ +#define SPX_OFFSET_HASH_ADDR 21 /* The byte used to specify the hash address (where in the Winternitz chain) */ +#define SPX_OFFSET_TREE_HGT 17 /* The byte used to specify the height of this node in the FORS or Merkle tree */ +#define SPX_OFFSET_TREE_INDEX 18 /* The start of the 4 byte field used to specify the node in the FORS or Merkle tree */ + +#define SPX_SHA2 1 + +#endif /* SHA2_OFFSETS_H_ */ diff --git a/src/crypto/pq/_s/ref/shake_offsets.h b/src/crypto/pq/_s/ref/shake_offsets.h new file mode 100644 index 0000000..0407bdf --- /dev/null +++ b/src/crypto/pq/_s/ref/shake_offsets.h @@ -0,0 +1,20 @@ +#if !defined( SHAKE_OFFSETS_H_ ) +#define SHAKE_OFFSETS_H_ + +/* + * Offsets of various fields in the address structure when we use SHAKE as + * the Sphincs+ hash function + */ + +#define SPX_OFFSET_LAYER 3 /* The byte used to specify the Merkle tree layer */ +#define SPX_OFFSET_TREE 8 /* The start of the 8 byte field used to specify the tree */ +#define SPX_OFFSET_TYPE 19 /* The byte used to specify the hash type (reason) */ +#define SPX_OFFSET_KP_ADDR 20 /* The start of the 4 byte field used to specify the key pair address */ +#define SPX_OFFSET_CHAIN_ADDR 27 /* The byte used to specify the chain address (which Winternitz chain) */ +#define SPX_OFFSET_HASH_ADDR 31 /* The byte used to specify the hash address (where in the Winternitz chain) */ +#define SPX_OFFSET_TREE_HGT 27 /* The byte used to specify the height of this node in the FORS or Merkle tree */ +#define SPX_OFFSET_TREE_INDEX 28 /* The start of the 4 byte field used to specify the node in the FORS or Merkle tree */ + +#define SPX_SHAKE 1 + +#endif /* SHAKE_OFFSETS_H_ */ diff --git a/src/crypto/pq/_s/ref/sign.c b/src/crypto/pq/_s/ref/sign.c new file mode 100644 index 0000000..a8e0c3c --- /dev/null +++ b/src/crypto/pq/_s/ref/sign.c @@ -0,0 +1,287 @@ +#include +#include +#include + +#include "api.h" +#include "params.h" +#include "wots.h" +#include "fors.h" +#include "hash.h" +#include "thash.h" +#include "address.h" +#include "randombytes.h" +#include "utils.h" +#include "merkle.h" + +/* + * Returns the length of a secret key, in bytes + */ +unsigned long long crypto_sign_secretkeybytes(void) +{ + return CRYPTO_SECRETKEYBYTES; +} + +/* + * Returns the length of a public key, in bytes + */ +unsigned long long crypto_sign_publickeybytes(void) +{ + return CRYPTO_PUBLICKEYBYTES; +} + +/* + * Returns the length of a signature, in bytes + */ +unsigned long long crypto_sign_bytes(void) +{ + return CRYPTO_BYTES; +} + +/* + * Returns the length of the seed required to generate a key pair, in bytes + */ +unsigned long long crypto_sign_seedbytes(void) +{ + return CRYPTO_SEEDBYTES; +} + +/* + * Generates an SPX key pair given a seed of length + * Format sk: [SK_SEED || SK_PRF || PUB_SEED || root] + * Format pk: [PUB_SEED || root] + */ +int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, + const unsigned char *seed) +{ + spx_ctx ctx; + + /* Initialize SK_SEED, SK_PRF and PUB_SEED from seed. */ + memcpy(sk, seed, CRYPTO_SEEDBYTES); + + memcpy(pk, sk + 2*SPX_N, SPX_N); + + memcpy(ctx.pub_seed, pk, SPX_N); + memcpy(ctx.sk_seed, sk, SPX_N); + + /* This hook allows the hash function instantiation to do whatever + preparation or computation it needs, based on the public seed. */ + initialize_hash_function(&ctx); + + /* Compute root node of the top-most subtree. */ + merkle_gen_root(sk + 3*SPX_N, &ctx); + + memcpy(pk + SPX_N, sk + 3*SPX_N, SPX_N); + + return 0; +} + +/* + * Generates an SPX key pair. + * Format sk: [SK_SEED || SK_PRF || PUB_SEED || root] + * Format pk: [PUB_SEED || root] + */ +int crypto_sign_keypair(unsigned char *pk, unsigned char *sk) +{ + unsigned char seed[CRYPTO_SEEDBYTES]; + randombytes(seed, CRYPTO_SEEDBYTES); + crypto_sign_seed_keypair(pk, sk, seed); + + return 0; +} + +/** + * Returns an array containing a detached signature. + */ +int crypto_sign_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, const uint8_t *sk) +{ + spx_ctx ctx; + + const unsigned char *sk_prf = sk + SPX_N; + const unsigned char *pk = sk + 2*SPX_N; + + unsigned char optrand[SPX_N]; + unsigned char mhash[SPX_FORS_MSG_BYTES]; + unsigned char root[SPX_N]; + uint32_t i; + uint64_t tree; + uint32_t idx_leaf; + uint32_t wots_addr[8] = {0}; + uint32_t tree_addr[8] = {0}; + + memcpy(ctx.sk_seed, sk, SPX_N); + memcpy(ctx.pub_seed, pk, SPX_N); + + /* This hook allows the hash function instantiation to do whatever + preparation or computation it needs, based on the public seed. */ + initialize_hash_function(&ctx); + + set_type(wots_addr, SPX_ADDR_TYPE_WOTS); + set_type(tree_addr, SPX_ADDR_TYPE_HASHTREE); + + /* Optionally, signing can be made non-deterministic using optrand. + This can help counter side-channel attacks that would benefit from + getting a large number of traces when the signer uses the same nodes. */ + randombytes(optrand, SPX_N); + /* Compute the digest randomization value. */ + gen_message_random(sig, sk_prf, optrand, m, mlen, &ctx); + + /* Derive the message digest and leaf index from R, PK and M. */ + hash_message(mhash, &tree, &idx_leaf, sig, pk, m, mlen, &ctx); + sig += SPX_N; + + set_tree_addr(wots_addr, tree); + set_keypair_addr(wots_addr, idx_leaf); + + /* Sign the message hash using FORS. */ + fors_sign(sig, root, mhash, &ctx, wots_addr); + sig += SPX_FORS_BYTES; + + for (i = 0; i < SPX_D; i++) { + set_layer_addr(tree_addr, i); + set_tree_addr(tree_addr, tree); + + copy_subtree_addr(wots_addr, tree_addr); + set_keypair_addr(wots_addr, idx_leaf); + + merkle_sign(sig, root, &ctx, wots_addr, tree_addr, idx_leaf); + sig += SPX_WOTS_BYTES + SPX_TREE_HEIGHT * SPX_N; + + /* Update the indices for the next layer. */ + idx_leaf = (tree & ((1 << SPX_TREE_HEIGHT)-1)); + tree = tree >> SPX_TREE_HEIGHT; + } + + *siglen = SPX_BYTES; + + return 0; +} + +/** + * Verifies a detached signature and message under a given public key. + */ +int crypto_sign_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, const uint8_t *pk) +{ + spx_ctx ctx; + const unsigned char *pub_root = pk + SPX_N; + unsigned char mhash[SPX_FORS_MSG_BYTES]; + unsigned char wots_pk[SPX_WOTS_BYTES]; + unsigned char root[SPX_N]; + unsigned char leaf[SPX_N]; + unsigned int i; + uint64_t tree; + uint32_t idx_leaf; + uint32_t wots_addr[8] = {0}; + uint32_t tree_addr[8] = {0}; + uint32_t wots_pk_addr[8] = {0}; + + if (siglen != SPX_BYTES) { + return -1; + } + + memcpy(ctx.pub_seed, pk, SPX_N); + + /* This hook allows the hash function instantiation to do whatever + preparation or computation it needs, based on the public seed. */ + initialize_hash_function(&ctx); + + set_type(wots_addr, SPX_ADDR_TYPE_WOTS); + set_type(tree_addr, SPX_ADDR_TYPE_HASHTREE); + set_type(wots_pk_addr, SPX_ADDR_TYPE_WOTSPK); + + /* Derive the message digest and leaf index from R || PK || M. */ + /* The additional SPX_N is a result of the hash domain separator. */ + hash_message(mhash, &tree, &idx_leaf, sig, pk, m, mlen, &ctx); + sig += SPX_N; + + /* Layer correctly defaults to 0, so no need to set_layer_addr */ + set_tree_addr(wots_addr, tree); + set_keypair_addr(wots_addr, idx_leaf); + + fors_pk_from_sig(root, sig, mhash, &ctx, wots_addr); + sig += SPX_FORS_BYTES; + + /* For each subtree.. */ + for (i = 0; i < SPX_D; i++) { + set_layer_addr(tree_addr, i); + set_tree_addr(tree_addr, tree); + + copy_subtree_addr(wots_addr, tree_addr); + set_keypair_addr(wots_addr, idx_leaf); + + copy_keypair_addr(wots_pk_addr, wots_addr); + + /* The WOTS public key is only correct if the signature was correct. */ + /* Initially, root is the FORS pk, but on subsequent iterations it is + the root of the subtree below the currently processed subtree. */ + wots_pk_from_sig(wots_pk, sig, root, &ctx, wots_addr); + sig += SPX_WOTS_BYTES; + + /* Compute the leaf node using the WOTS public key. */ + thash(leaf, wots_pk, SPX_WOTS_LEN, &ctx, wots_pk_addr); + + /* Compute the root node of this subtree. */ + compute_root(root, leaf, idx_leaf, 0, sig, SPX_TREE_HEIGHT, + &ctx, tree_addr); + sig += SPX_TREE_HEIGHT * SPX_N; + + /* Update the indices for the next layer. */ + idx_leaf = (tree & ((1 << SPX_TREE_HEIGHT)-1)); + tree = tree >> SPX_TREE_HEIGHT; + } + + /* Check if the root node equals the root node in the public key. */ + if (memcmp(root, pub_root, SPX_N)) { + return -1; + } + + return 0; +} + + +/** + * Returns an array containing the signature followed by the message. + */ +int crypto_sign(unsigned char *sm, unsigned long long *smlen, + const unsigned char *m, unsigned long long mlen, + const unsigned char *sk) +{ + size_t siglen; + + crypto_sign_signature(sm, &siglen, m, (size_t)mlen, sk); + + memmove(sm + SPX_BYTES, m, mlen); + *smlen = siglen + mlen; + + return 0; +} + +/** + * Verifies a given signature-message pair under a given public key. + */ +int crypto_sign_open(unsigned char *m, unsigned long long *mlen, + const unsigned char *sm, unsigned long long smlen, + const unsigned char *pk) +{ + /* The API caller does not necessarily know what size a signature should be + but SPHINCS+ signatures are always exactly SPX_BYTES. */ + if (smlen < SPX_BYTES) { + memset(m, 0, smlen); + *mlen = 0; + return -1; + } + + *mlen = smlen - SPX_BYTES; + + if (crypto_sign_verify(sm, SPX_BYTES, sm + SPX_BYTES, (size_t)*mlen, pk)) { + memset(m, 0, smlen); + *mlen = 0; + return -1; + } + + /* If verification was successful, move the message to the right place. */ + memmove(m, sm + SPX_BYTES, *mlen); + + return 0; +} diff --git a/src/crypto/pq/_s/ref/test/benchmark.c b/src/crypto/pq/_s/ref/test/benchmark.c new file mode 100644 index 0000000..b062545 --- /dev/null +++ b/src/crypto/pq/_s/ref/test/benchmark.c @@ -0,0 +1,173 @@ +#define _POSIX_C_SOURCE 199309L + +#include +#include +#include + +#include "../thash.h" +#include "../api.h" +#include "../fors.h" +#include "../wotsx1.h" +#include "../params.h" +#include "../randombytes.h" +#include "cycles.h" + +#define SPX_MLEN 32 +#define NTESTS 10 + +static void wots_gen_pkx1(unsigned char *pk, const spx_ctx* ctx, + uint32_t addr[8]); + +static int cmp_llu(const void *a, const void*b) +{ + if(*(unsigned long long *)a < *(unsigned long long *)b) return -1; + if(*(unsigned long long *)a > *(unsigned long long *)b) return 1; + return 0; +} + +static unsigned long long median(unsigned long long *l, size_t llen) +{ + qsort(l,llen,sizeof(unsigned long long),cmp_llu); + + if(llen%2) return l[llen/2]; + else return (l[llen/2-1]+l[llen/2])/2; +} + +static void delta(unsigned long long *l, size_t llen) +{ + unsigned int i; + for(i = 0; i < llen - 1; i++) { + l[i] = l[i+1] - l[i]; + } +} + + +static void printfcomma (unsigned long long n) +{ + if (n < 1000) { + printf("%llu", n); + return; + } + printfcomma(n / 1000); + printf (",%03llu", n % 1000); +} + +static void printfalignedcomma (unsigned long long n, int len) +{ + unsigned long long ncopy = n; + int i = 0; + + while (ncopy > 9) { + len -= 1; + ncopy /= 10; + i += 1; // to account for commas + } + i = i/3 - 1; // to account for commas + for (; i < len; i++) { + printf(" "); + } + printfcomma(n); +} + +static void display_result(double result, unsigned long long *l, size_t llen, unsigned long long mul) +{ + unsigned long long med; + + result /= NTESTS; + delta(l, NTESTS + 1); + med = median(l, llen); + printf("avg. %11.2lf us (%2.2lf sec); median ", result, result / 1e6); + printfalignedcomma(med, 12); + printf(" cycles, %5llux: ", mul); + printfalignedcomma(mul*med, 12); + printf(" cycles\n"); +} + +#define MEASURE_GENERIC(TEXT, MUL, FNCALL, CORR)\ + printf(TEXT);\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);\ + for(i = 0; i < NTESTS; i++) {\ + t[i] = cpucycles() / CORR;\ + FNCALL;\ + }\ + t[NTESTS] = cpucycles();\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);\ + result = ((stop.tv_sec - start.tv_sec) * 1e6 + \ + (stop.tv_nsec - start.tv_nsec) / 1e3) / (double)CORR;\ + display_result(result, t, NTESTS, MUL); +#define MEASURT(TEXT, MUL, FNCALL)\ + MEASURE_GENERIC(\ + TEXT, MUL,\ + do {\ + for (int j = 0; j < 1000; j++) {\ + FNCALL;\ + }\ + } while (0);,\ + 1000); +#define MEASURE(TEXT, MUL, FNCALL) MEASURE_GENERIC(TEXT, MUL, FNCALL, 1) + +int main(void) +{ + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + init_cpucycles(); + + spx_ctx ctx; + + unsigned char pk[SPX_PK_BYTES]; + unsigned char sk[SPX_SK_BYTES]; + unsigned char *m = malloc(SPX_MLEN); + unsigned char *sm = malloc(SPX_BYTES + SPX_MLEN); + unsigned char *mout = malloc(SPX_BYTES + SPX_MLEN); + + unsigned char fors_pk[SPX_FORS_PK_BYTES]; + unsigned char fors_m[SPX_FORS_MSG_BYTES]; + unsigned char fors_sig[SPX_FORS_BYTES]; + unsigned char addr[SPX_ADDR_BYTES]; + unsigned char block[SPX_N]; + + unsigned char wots_pk[SPX_WOTS_PK_BYTES]; + + unsigned long long smlen; + unsigned long long mlen; + unsigned long long t[NTESTS+1]; + struct timespec start, stop; + double result; + int i; + + randombytes(m, SPX_MLEN); + randombytes(addr, SPX_ADDR_BYTES); + + printf("Parameters: n = %d, h = %d, d = %d, b = %d, k = %d, w = %d\n", + SPX_N, SPX_FULL_HEIGHT, SPX_D, SPX_FORS_HEIGHT, SPX_FORS_TREES, + SPX_WOTS_W); + + printf("Running %d iterations.\n", NTESTS); + + MEASURT("thash ", 1, thash(block, block, 1, &ctx, (uint32_t*)addr)); + MEASURE("Generating keypair.. ", 1, crypto_sign_keypair(pk, sk)); + MEASURE(" - WOTS pk gen.. ", (1 << SPX_TREE_HEIGHT), wots_gen_pkx1(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Signing.. ", 1, crypto_sign(sm, &smlen, m, SPX_MLEN, sk)); + MEASURE(" - FORS signing.. ", 1, fors_sign(fors_sig, fors_pk, fors_m, &ctx, (uint32_t *) addr)); + MEASURE(" - WOTS pk gen.. ", SPX_D * (1 << SPX_TREE_HEIGHT), wots_gen_pkx1(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Verifying.. ", 1, crypto_sign_open(mout, &mlen, sm, smlen, pk)); + + printf("Signature size: %d (%.2f KiB)\n", SPX_BYTES, SPX_BYTES / 1024.0); + printf("Public key size: %d (%.2f KiB)\n", SPX_PK_BYTES, SPX_PK_BYTES / 1024.0); + printf("Secret key size: %d (%.2f KiB)\n", SPX_SK_BYTES, SPX_SK_BYTES / 1024.0); + + free(m); + free(sm); + free(mout); + + return 0; +} + +static void wots_gen_pkx1(unsigned char *pk, const spx_ctx *ctx, + uint32_t addr[8]) { + struct leaf_info_x1 leaf; + unsigned steps[ SPX_WOTS_LEN ] = { 0 }; + INITIALIZE_LEAF_INFO_X1(leaf, addr, steps); + wots_gen_leafx1(pk, ctx, 0, &leaf); +} + diff --git a/src/crypto/pq/_s/ref/test/cycles.c b/src/crypto/pq/_s/ref/test/cycles.c new file mode 100644 index 0000000..0fa46b5 --- /dev/null +++ b/src/crypto/pq/_s/ref/test/cycles.c @@ -0,0 +1,110 @@ +#include "cycles.h" + +#if defined(__aarch64__) && defined(__APPLE__) +// Adapted from +// https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2021/03/24/ + +#include +#include +#include +#include + +#define KPERF_LIST \ + F(int, kpc_force_all_ctrs_set, int) \ + F(int, kpc_set_counting, uint32_t) \ + F(int, kpc_set_thread_counting, uint32_t) \ + F(int, kpc_set_config, uint32_t, void *) \ + F(uint32_t, kpc_get_counter_count, uint32_t) \ + F(uint32_t, kpc_get_config_count, uint32_t) \ + F(int, kpc_get_thread_counters, int, unsigned int, void *) + +#define F(ret, name, ...) \ + typedef ret name##proc(__VA_ARGS__); \ + static name##proc *name; +KPERF_LIST +#undef F + +uint64_t g_counters[10]; +uint64_t g_config[10]; + +static void configure_rdtsc(void) { + if (kpc_set_config(3, g_config)) { + printf("kpc_set_config failed\n"); + return; + } + + if (kpc_force_all_ctrs_set(1)) { + printf("kpc_force_all_ctrs_set failed\n"); + return; + } + + if (kpc_set_counting(3)) { + printf("kpc_set_counting failed\n"); + return; + } + + if (kpc_set_thread_counting(3)) { + printf("kpc_set_thread_counting failed\n"); + return; + } +} + +void init_cpucycles(void) { + void *kperf = dlopen( + "/System/Library/PrivateFrameworks/kperf.framework/Versions/A/kperf", + RTLD_LAZY); + if (!kperf) { + printf("kperf = %p\n", kperf); + return; + } +#define F(ret, name, ...) \ + name = (name##proc *)(dlsym(kperf, #name)); \ + if (!name) { \ + printf("%s = %p\n", #name, (void *)name); \ + return; \ + } + KPERF_LIST +#undef F + + if (kpc_get_counter_count(3) != 10) { + printf("wrong fixed counters count\n"); + return; + } + + if (kpc_get_config_count(3) != 8) { + printf("wrong fixed config count\n"); + return; + } + g_config[0] = 0x02 | 0x20000; + g_config[3] = 0x8d | 0x20000; + g_config[4] = 0xcb | 0x20000; + g_config[5] = 0x8c | 0x20000; + + configure_rdtsc(); +} + +unsigned long long cpucycles(void) { + static int warned = 0; + if (kpc_get_thread_counters(0, 10, g_counters)) { + if (!warned) { + printf("kpc_get_thread_counters failed, run as sudo?\n"); + warned = 1; + } + return 1; + } + // g_counters[3 + 2] gives you the number of instructions 'decoded' + // whereas g_counters[1] might give you the number of instructions 'retired'. + return g_counters[0 + 2]; +} +#else +void init_cpucycles(void) { +} + +unsigned long long cpucycles(void) +{ + unsigned long long result; + __asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" + : "=a" (result) :: "%rdx"); + return result; +} +#endif diff --git a/src/crypto/pq/_s/ref/test/fors.c b/src/crypto/pq/_s/ref/test/fors.c new file mode 100644 index 0000000..73c828d --- /dev/null +++ b/src/crypto/pq/_s/ref/test/fors.c @@ -0,0 +1,41 @@ +#include +#include + +#include "../context.h" +#include "../hash.h" +#include "../fors.h" +#include "../randombytes.h" +#include "../params.h" + +int main(void) +{ + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + spx_ctx ctx; + + unsigned char pk1[SPX_FORS_PK_BYTES]; + unsigned char pk2[SPX_FORS_PK_BYTES]; + unsigned char sig[SPX_FORS_BYTES]; + unsigned char m[SPX_FORS_MSG_BYTES]; + uint32_t addr[8] = {0}; + + randombytes(ctx.sk_seed, SPX_N); + randombytes(ctx.pub_seed, SPX_N); + randombytes(m, SPX_FORS_MSG_BYTES); + randombytes((unsigned char *)addr, 8 * sizeof(uint32_t)); + + printf("Testing FORS signature and PK derivation.. "); + + initialize_hash_function(&ctx); + + fors_sign(sig, pk1, m, &ctx, addr); + fors_pk_from_sig(pk2, sig, m, &ctx, addr); + + if (memcmp(pk1, pk2, SPX_FORS_PK_BYTES)) { + printf("failed!\n"); + return -1; + } + printf("successful.\n"); + return 0; +} diff --git a/src/crypto/pq/_s/ref/test/haraka.c b/src/crypto/pq/_s/ref/test/haraka.c new file mode 100644 index 0000000..927d0de --- /dev/null +++ b/src/crypto/pq/_s/ref/test/haraka.c @@ -0,0 +1,155 @@ +#include +#include +#include +#include + +#include "../haraka.c" +#include "../randombytes.h" + +static int test_haraka_S_incremental(void) { + unsigned char input[521]; + unsigned char check[521]; + unsigned char output[521]; + uint8_t s_inc_absorb[65]; + uint8_t s_inc_squeeze[65]; + uint8_t s_inc_squeeze_all[65]; + uint8_t s_inc_both[65]; + uint8_t s_combined[64]; + int i; + int absorbed; + int squeezed; + int returncode = 0; + + randombytes(input, 521); + + haraka_S(check, 521, input, 521); + + haraka_S_inc_init(s_inc_absorb); + + absorbed = 0; + for (i = 0; i < 521 && absorbed + i <= 521; i++) { + haraka_S_inc_absorb(s_inc_absorb, input + absorbed, i); + absorbed += i; + } + haraka_S_inc_absorb(s_inc_absorb, input + absorbed, 521 - absorbed); + + haraka_S_inc_finalize(s_inc_absorb); + + memset(s_combined, 0, 64); + haraka_S_absorb(s_combined, HARAKAS_RATE, input, 521, 0x1F); + + if (memcmp(s_inc_absorb, s_combined, 64 * sizeof(uint8_t))) { + printf("ERROR haraka_S state after incremental absorb did not match all-at-once absorb.\n"); + printf(" Expected: "); + for (i = 0; i < 64; i++) { + printf("%02X", s_combined[i]); + } + printf("\n"); + printf(" State: "); + for (i = 0; i < 64; i++) { + printf("%02X", s_inc_absorb[i]); + } + printf("\n"); + returncode = 1; + } + + memcpy(s_inc_both, s_inc_absorb, 65 * sizeof(uint8_t)); + + haraka_S_squeezeblocks(output, 3, s_inc_absorb, HARAKAS_RATE); + + if (memcmp(check, output, 3*HARAKAS_RATE)) { + printf("ERROR haraka_S incremental absorb did not match haraka_S.\n"); + printf(" Expected: "); + for (i = 0; i < 3*HARAKAS_RATE; i++) { + printf("%02X", check[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 3*HARAKAS_RATE; i++) { + printf("%02X", output[i]); + } + printf("\n"); + returncode = 1; + } + + memset(s_inc_squeeze, 0, 65); + haraka_S_absorb(s_inc_squeeze, HARAKAS_RATE, input, 521, 0x1F); + s_inc_squeeze[64] = 0; + + memcpy(s_inc_squeeze_all, s_inc_squeeze, 65 * sizeof(uint8_t)); + + haraka_S_inc_squeeze(output, 521, s_inc_squeeze_all); + + if (memcmp(check, output, 521)) { + printf("ERROR haraka_S incremental squeeze-all did not match haraka_S.\n"); + printf(" Expected: "); + for (i = 0; i < 521; i++) { + printf("%02X", check[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 521; i++) { + printf("%02X", output[i]); + } + printf("\n"); + returncode = 1; + } + + squeezed = 0; + memset(output, 0, 521); + for (i = 0; i < 521 && squeezed + i <= 521; i++) { + haraka_S_inc_squeeze(output + squeezed, i, s_inc_squeeze); + squeezed += i; + } + haraka_S_inc_squeeze(output + squeezed, 521 - squeezed, s_inc_squeeze); + + if (memcmp(check, output, 521)) { + printf("ERROR haraka_S incremental squeeze did not match haraka_S.\n"); + printf(" Expected: "); + for (i = 0; i < 521; i++) { + printf("%02X", check[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 521; i++) { + printf("%02X", output[i]); + } + printf("\n"); + returncode = 1; + } + + squeezed = 0; + memset(output, 0, 521); + for (i = 0; i < 521 && squeezed + i <= 521; i++) { + haraka_S_inc_squeeze(output + squeezed, i, s_inc_both); + squeezed += i; + } + haraka_S_inc_squeeze(output + squeezed, 521 - squeezed, s_inc_both); + + if (memcmp(check, output, 521)) { + printf("ERROR haraka_S incremental absorb + squeeze did not match haraka_S.\n"); + printf(" Expected: "); + for (i = 0; i < 521; i++) { + printf("%02X", check[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 521; i++) { + printf("%02X", output[i]); + } + printf("\n"); + returncode = 1; + } + + return returncode; +} + +int main(void) { + int result = 0; + result += test_haraka_S_incremental(); + + if (result != 0) { + puts("Errors occurred"); + } + return result; +} diff --git a/src/crypto/pq/_s/ref/test/spx.c b/src/crypto/pq/_s/ref/test/spx.c new file mode 100644 index 0000000..2b214b9 --- /dev/null +++ b/src/crypto/pq/_s/ref/test/spx.c @@ -0,0 +1,125 @@ +#include +#include +#include + +#include "../api.h" +#include "../params.h" +#include "../randombytes.h" + +#define SPX_MLEN 32 +#define SPX_SIGNATURES 1 + +int main(void) +{ + int ret = 0; + int i; + + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + unsigned char pk[SPX_PK_BYTES]; + unsigned char sk[SPX_SK_BYTES]; + unsigned char *m = malloc(SPX_MLEN); + unsigned char *sm = malloc(SPX_BYTES + SPX_MLEN); + unsigned char *mout = malloc(SPX_BYTES + SPX_MLEN); + unsigned long long smlen; + unsigned long long mlen; + + randombytes(m, SPX_MLEN); + + printf("Generating keypair.. "); + + if (crypto_sign_keypair(pk, sk)) { + printf("failed!\n"); + return -1; + } + printf("successful.\n"); + + printf("Testing %d signatures.. \n", SPX_SIGNATURES); + + for (i = 0; i < SPX_SIGNATURES; i++) { + printf(" - iteration #%d:\n", i); + + crypto_sign(sm, &smlen, m, SPX_MLEN, sk); + + if (smlen != SPX_BYTES + SPX_MLEN) { + printf(" X smlen incorrect [%llu != %u]!\n", + smlen, SPX_BYTES); + ret = -1; + } + else { + printf(" smlen as expected [%llu].\n", smlen); + } + + /* Test if signature is valid. */ + if (crypto_sign_open(mout, &mlen, sm, smlen, pk)) { + printf(" X verification failed!\n"); + ret = -1; + } + else { + printf(" verification succeeded.\n"); + } + + /* Test if the correct message was recovered. */ + if (mlen != SPX_MLEN) { + printf(" X mlen incorrect [%llu != %u]!\n", mlen, SPX_MLEN); + ret = -1; + } + else { + printf(" mlen as expected [%llu].\n", mlen); + } + if (memcmp(m, mout, SPX_MLEN)) { + printf(" X output message incorrect!\n"); + ret = -1; + } + else { + printf(" output message as expected.\n"); + } + + /* Test if signature is valid when validating in-place. */ + if (crypto_sign_open(sm, &mlen, sm, smlen, pk)) { + printf(" X in-place verification failed!\n"); + ret = -1; + } + else { + printf(" in-place verification succeeded.\n"); + } + + /* Test if flipping bits invalidates the signature (it should). */ + + /* Flip the first bit of the message. Should invalidate. */ + sm[smlen - 1] ^= 1; + if (!crypto_sign_open(mout, &mlen, sm, smlen, pk)) { + printf(" X flipping a bit of m DID NOT invalidate signature!\n"); + ret = -1; + } + else { + printf(" flipping a bit of m invalidates signature.\n"); + } + sm[smlen - 1] ^= 1; + +#ifdef SPX_TEST_INVALIDSIG + int j; + /* Flip one bit per hash; the signature is entirely hashes. */ + for (j = 0; j < (int)(smlen - SPX_MLEN); j += SPX_N) { + sm[j] ^= 1; + if (!crypto_sign_open(mout, &mlen, sm, smlen, pk)) { + printf(" X flipping bit %d DID NOT invalidate sig + m!\n", j); + sm[j] ^= 1; + ret = -1; + break; + } + sm[j] ^= 1; + } + if (j >= (int)(smlen - SPX_MLEN)) { + printf(" changing any signature hash invalidates signature.\n"); + } +#endif + } + + free(m); + free(sm); + free(mout); + + return ret; +} diff --git a/src/crypto/pq/_s/ref/thash.h b/src/crypto/pq/_s/ref/thash.h new file mode 100644 index 0000000..8687ccf --- /dev/null +++ b/src/crypto/pq/_s/ref/thash.h @@ -0,0 +1,13 @@ +#ifndef SPX_THASH_H +#define SPX_THASH_H + +#include "context.h" +#include "params.h" + +#include + +#define thash SPX_NAMESPACE(thash) +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]); + +#endif diff --git a/src/crypto/pq/_s/ref/thash_haraka_robust.c b/src/crypto/pq/_s/ref/thash_haraka_robust.c new file mode 100644 index 0000000..005e14a --- /dev/null +++ b/src/crypto/pq/_s/ref/thash_haraka_robust.c @@ -0,0 +1,46 @@ +#include +#include + +#include "thash.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "haraka.h" + +/** + * Takes an array of inblocks concatenated arrays of SPX_N bytes. + */ +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ + SPX_VLA(uint8_t, buf, SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(uint8_t, bitmask, inblocks*SPX_N); + unsigned char outbuf[32]; + unsigned char buf_tmp[64]; + unsigned int i; + + if (inblocks == 1) { + /* F function */ + /* Since SPX_N may be smaller than 32, we need a temporary buffer. */ + memset(buf_tmp, 0, 64); + memcpy(buf_tmp, addr, 32); + + haraka256(outbuf, buf_tmp, ctx); + for (i = 0; i < inblocks * SPX_N; i++) { + buf_tmp[SPX_ADDR_BYTES + i] = in[i] ^ outbuf[i]; + } + haraka512(outbuf, buf_tmp, ctx); + memcpy(out, outbuf, SPX_N); + } else { + /* All other tweakable hashes*/ + memcpy(buf, addr, 32); + haraka_S(bitmask, inblocks * SPX_N, buf, SPX_ADDR_BYTES, ctx); + + for (i = 0; i < inblocks * SPX_N; i++) { + buf[SPX_ADDR_BYTES + i] = in[i] ^ bitmask[i]; + } + + haraka_S(out, SPX_N, buf, SPX_ADDR_BYTES + inblocks*SPX_N, ctx); + } +} diff --git a/src/crypto/pq/_s/ref/thash_haraka_simple.c b/src/crypto/pq/_s/ref/thash_haraka_simple.c new file mode 100644 index 0000000..1040297 --- /dev/null +++ b/src/crypto/pq/_s/ref/thash_haraka_simple.c @@ -0,0 +1,37 @@ +#include +#include + +#include "thash.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "haraka.h" + +/** + * Takes an array of inblocks concatenated arrays of SPX_N bytes. + */ +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ + SPX_VLA(uint8_t, buf, SPX_ADDR_BYTES + inblocks*SPX_N); + unsigned char outbuf[32]; + unsigned char buf_tmp[64]; + + if (inblocks == 1) { + /* F function */ + /* Since SPX_N may be smaller than 32, we need a temporary buffer. */ + memset(buf_tmp, 0, 64); + memcpy(buf_tmp, addr, 32); + memcpy(buf_tmp + SPX_ADDR_BYTES, in, SPX_N); + + haraka512(outbuf, buf_tmp, ctx); + memcpy(out, outbuf, SPX_N); + } else { + /* All other tweakable hashes*/ + memcpy(buf, addr, 32); + memcpy(buf + SPX_ADDR_BYTES, in, inblocks * SPX_N); + + haraka_S(out, SPX_N, buf, SPX_ADDR_BYTES + inblocks*SPX_N, ctx); + } +} diff --git a/src/crypto/pq/_s/ref/thash_sha2_robust.c b/src/crypto/pq/_s/ref/thash_sha2_robust.c new file mode 100644 index 0000000..67ca3da --- /dev/null +++ b/src/crypto/pq/_s/ref/thash_sha2_robust.c @@ -0,0 +1,74 @@ +#include +#include + +#include "thash.h" +#include "address.h" +#include "params.h" +#include "utils.h" +#include "sha2.h" + +#if SPX_SHA512 +static void thash_512(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]); +#endif + +/** + * Takes an array of inblocks concatenated arrays of SPX_N bytes. + */ +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ +#if SPX_SHA512 + if (inblocks > 1) { + thash_512(out, in, inblocks, ctx, addr); + return; + } +#endif + unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES]; + SPX_VLA(uint8_t, bitmask, inblocks * SPX_N); + SPX_VLA(uint8_t, buf, SPX_N + SPX_SHA256_OUTPUT_BYTES + inblocks*SPX_N); + uint8_t sha2_state[40]; + unsigned int i; + + memcpy(buf, ctx->pub_seed, SPX_N); + memcpy(buf + SPX_N, addr, SPX_SHA256_ADDR_BYTES); + mgf1_256(bitmask, inblocks * SPX_N, buf, SPX_N + SPX_SHA256_ADDR_BYTES); + + /* Retrieve precomputed state containing pub_seed */ + memcpy(sha2_state, ctx->state_seeded, 40 * sizeof(uint8_t)); + + for (i = 0; i < inblocks * SPX_N; i++) { + buf[SPX_N + SPX_SHA256_ADDR_BYTES + i] = in[i] ^ bitmask[i]; + } + + sha256_inc_finalize(outbuf, sha2_state, buf + SPX_N, + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + memcpy(out, outbuf, SPX_N); +} + +#if SPX_SHA512 +static void thash_512(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ + unsigned char outbuf[SPX_SHA512_OUTPUT_BYTES]; + SPX_VLA(uint8_t, bitmask, inblocks * SPX_N); + SPX_VLA(uint8_t, buf, SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + uint8_t sha2_state[72]; + unsigned int i; + + memcpy(buf, ctx->pub_seed, SPX_N); + memcpy(buf + SPX_N, addr, SPX_SHA256_ADDR_BYTES); + mgf1_512(bitmask, inblocks * SPX_N, buf, SPX_N + SPX_SHA256_ADDR_BYTES); + + /* Retrieve precomputed state containing pub_seed */ + memcpy(sha2_state, ctx->state_seeded_512, 72 * sizeof(uint8_t)); + + for (i = 0; i < inblocks * SPX_N; i++) { + buf[SPX_N + SPX_SHA256_ADDR_BYTES + i] = in[i] ^ bitmask[i]; + } + + sha512_inc_finalize(outbuf, sha2_state, buf + SPX_N, + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + memcpy(out, outbuf, SPX_N); +} +#endif diff --git a/src/crypto/pq/_s/ref/thash_sha2_simple.c b/src/crypto/pq/_s/ref/thash_sha2_simple.c new file mode 100644 index 0000000..da58896 --- /dev/null +++ b/src/crypto/pq/_s/ref/thash_sha2_simple.c @@ -0,0 +1,59 @@ +#include +#include + +#include "thash.h" +#include "address.h" +#include "params.h" +#include "utils.h" +#include "sha2.h" + +#if SPX_SHA512 +static void thash_512(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]); +#endif + +/** + * Takes an array of inblocks concatenated arrays of SPX_N bytes. + */ +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ +#if SPX_SHA512 + if (inblocks > 1) { + thash_512(out, in, inblocks, ctx, addr); + return; + } +#endif + + unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES]; + uint8_t sha2_state[40]; + SPX_VLA(uint8_t, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + + /* Retrieve precomputed state containing pub_seed */ + memcpy(sha2_state, ctx->state_seeded, 40 * sizeof(uint8_t)); + + memcpy(buf, addr, SPX_SHA256_ADDR_BYTES); + memcpy(buf + SPX_SHA256_ADDR_BYTES, in, inblocks * SPX_N); + + sha256_inc_finalize(outbuf, sha2_state, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + memcpy(out, outbuf, SPX_N); +} + +#if SPX_SHA512 +static void thash_512(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ + unsigned char outbuf[SPX_SHA512_OUTPUT_BYTES]; + uint8_t sha2_state[72]; + SPX_VLA(uint8_t, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + + /* Retrieve precomputed state containing pub_seed */ + memcpy(sha2_state, ctx->state_seeded_512, 72 * sizeof(uint8_t)); + + memcpy(buf, addr, SPX_SHA256_ADDR_BYTES); + memcpy(buf + SPX_SHA256_ADDR_BYTES, in, inblocks * SPX_N); + + sha512_inc_finalize(outbuf, sha2_state, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + memcpy(out, outbuf, SPX_N); +} +#endif diff --git a/src/crypto/pq/_s/ref/thash_shake_robust.c b/src/crypto/pq/_s/ref/thash_shake_robust.c new file mode 100644 index 0000000..3589fde --- /dev/null +++ b/src/crypto/pq/_s/ref/thash_shake_robust.c @@ -0,0 +1,31 @@ +#include +#include + +#include "thash.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "fips202.h" + +/** + * Takes an array of inblocks concatenated arrays of SPX_N bytes. + */ +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ + SPX_VLA(uint8_t, buf, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(uint8_t, bitmask, inblocks * SPX_N); + unsigned int i; + + memcpy(buf, ctx->pub_seed, SPX_N); + memcpy(buf + SPX_N, addr, SPX_ADDR_BYTES); + + shake256(bitmask, inblocks * SPX_N, buf, SPX_N + SPX_ADDR_BYTES); + + for (i = 0; i < inblocks * SPX_N; i++) { + buf[SPX_N + SPX_ADDR_BYTES + i] = in[i] ^ bitmask[i]; + } + + shake256(out, SPX_N, buf, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); +} diff --git a/src/crypto/pq/_s/ref/thash_shake_simple.c b/src/crypto/pq/_s/ref/thash_shake_simple.c new file mode 100644 index 0000000..7ce5c55 --- /dev/null +++ b/src/crypto/pq/_s/ref/thash_shake_simple.c @@ -0,0 +1,24 @@ +#include +#include + +#include "thash.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "fips202.h" + +/** + * Takes an array of inblocks concatenated arrays of SPX_N bytes. + */ +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ + SPX_VLA(uint8_t, buf, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + + memcpy(buf, ctx->pub_seed, SPX_N); + memcpy(buf + SPX_N, addr, SPX_ADDR_BYTES); + memcpy(buf + SPX_N + SPX_ADDR_BYTES, in, inblocks * SPX_N); + + shake256(out, SPX_N, buf, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); +} diff --git a/src/crypto/pq/_s/ref/utils.c b/src/crypto/pq/_s/ref/utils.c new file mode 100644 index 0000000..63d52ee --- /dev/null +++ b/src/crypto/pq/_s/ref/utils.c @@ -0,0 +1,154 @@ +#include + +#include "utils.h" +#include "params.h" +#include "hash.h" +#include "thash.h" +#include "address.h" + +/** + * Converts the value of 'in' to 'outlen' bytes in big-endian byte order. + */ +void ull_to_bytes(unsigned char *out, unsigned int outlen, + unsigned long long in) +{ + int i; + + /* Iterate over out in decreasing order, for big-endianness. */ + for (i = (signed int)outlen - 1; i >= 0; i--) { + out[i] = in & 0xff; + in = in >> 8; + } +} + +void u32_to_bytes(unsigned char *out, uint32_t in) +{ + out[0] = (unsigned char)(in >> 24); + out[1] = (unsigned char)(in >> 16); + out[2] = (unsigned char)(in >> 8); + out[3] = (unsigned char)in; +} + +/** + * Converts the inlen bytes in 'in' from big-endian byte order to an integer. + */ +unsigned long long bytes_to_ull(const unsigned char *in, unsigned int inlen) +{ + unsigned long long retval = 0; + unsigned int i; + + for (i = 0; i < inlen; i++) { + retval |= ((unsigned long long)in[i]) << (8*(inlen - 1 - i)); + } + return retval; +} + +/** + * Computes a root node given a leaf and an auth path. + * Expects address to be complete other than the tree_height and tree_index. + */ +void compute_root(unsigned char *root, const unsigned char *leaf, + uint32_t leaf_idx, uint32_t idx_offset, + const unsigned char *auth_path, uint32_t tree_height, + const spx_ctx *ctx, uint32_t addr[8]) +{ + uint32_t i; + unsigned char buffer[2 * SPX_N]; + + /* If leaf_idx is odd (last bit = 1), current path element is a right child + and auth_path has to go left. Otherwise it is the other way around. */ + if (leaf_idx & 1) { + memcpy(buffer + SPX_N, leaf, SPX_N); + memcpy(buffer, auth_path, SPX_N); + } + else { + memcpy(buffer, leaf, SPX_N); + memcpy(buffer + SPX_N, auth_path, SPX_N); + } + auth_path += SPX_N; + + for (i = 0; i < tree_height - 1; i++) { + leaf_idx >>= 1; + idx_offset >>= 1; + /* Set the address of the node we're creating. */ + set_tree_height(addr, i + 1); + set_tree_index(addr, leaf_idx + idx_offset); + + /* Pick the right or left neighbor, depending on parity of the node. */ + if (leaf_idx & 1) { + thash(buffer + SPX_N, buffer, 2, ctx, addr); + memcpy(buffer, auth_path, SPX_N); + } + else { + thash(buffer, buffer, 2, ctx, addr); + memcpy(buffer + SPX_N, auth_path, SPX_N); + } + auth_path += SPX_N; + } + + /* The last iteration is exceptional; we do not copy an auth_path node. */ + leaf_idx >>= 1; + idx_offset >>= 1; + set_tree_height(addr, tree_height); + set_tree_index(addr, leaf_idx + idx_offset); + thash(root, buffer, 2, ctx, addr); +} + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + */ +void treehash(unsigned char *root, unsigned char *auth_path, const spx_ctx* ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leaf)( + unsigned char* /* leaf */, + const spx_ctx* /* ctx */, + uint32_t /* addr_idx */, const uint32_t[8] /* tree_addr */), + uint32_t tree_addr[8]) +{ + SPX_VLA(uint8_t, stack, (tree_height+1)*SPX_N); + SPX_VLA(unsigned int, heights, tree_height+1); + unsigned int offset = 0; + uint32_t idx; + uint32_t tree_idx; + + for (idx = 0; idx < (uint32_t)(1 << tree_height); idx++) { + /* Add the next leaf node to the stack. */ + gen_leaf(stack + offset*SPX_N, ctx, idx + idx_offset, tree_addr); + offset++; + heights[offset - 1] = 0; + + /* If this is a node we need for the auth path.. */ + if ((leaf_idx ^ 0x1) == idx) { + memcpy(auth_path, stack + (offset - 1)*SPX_N, SPX_N); + } + + /* While the top-most nodes are of equal height.. */ + while (offset >= 2 && heights[offset - 1] == heights[offset - 2]) { + /* Compute index of the new node, in the next layer. */ + tree_idx = (idx >> (heights[offset - 1] + 1)); + + /* Set the address of the node we're creating. */ + set_tree_height(tree_addr, heights[offset - 1] + 1); + set_tree_index(tree_addr, + tree_idx + (idx_offset >> (heights[offset-1] + 1))); + /* Hash the top-most nodes from the stack together. */ + thash(stack + (offset - 2)*SPX_N, + stack + (offset - 2)*SPX_N, 2, ctx, tree_addr); + offset--; + /* Note that the top-most node is now one layer higher. */ + heights[offset - 1]++; + + /* If this is a node we need for the auth path.. */ + if (((leaf_idx >> heights[offset - 1]) ^ 0x1) == tree_idx) { + memcpy(auth_path + heights[offset - 1]*SPX_N, + stack + (offset - 1)*SPX_N, SPX_N); + } + } + } + memcpy(root, stack, SPX_N); +} diff --git a/src/crypto/pq/_s/ref/utils.h b/src/crypto/pq/_s/ref/utils.h new file mode 100644 index 0000000..b13502c --- /dev/null +++ b/src/crypto/pq/_s/ref/utils.h @@ -0,0 +1,64 @@ +#ifndef SPX_UTILS_H +#define SPX_UTILS_H + +#include +#include "params.h" +#include "context.h" + + +/* To support MSVC use alloca() instead of VLAs. See #20. */ +#ifdef _MSC_VER +/* MSVC defines _alloca in malloc.h */ +# include +/* Note: _malloca(), which is recommended over deprecated _alloca, + requires that you call _freea(). So we stick with _alloca */ +# define SPX_VLA(__t,__x,__s) __t *__x = (__t*)_alloca((__s)*sizeof(__t)) +#else +# define SPX_VLA(__t,__x,__s) __t __x[__s] +#endif + +/** + * Converts the value of 'in' to 'outlen' bytes in big-endian byte order. + */ +#define ull_to_bytes SPX_NAMESPACE(ull_to_bytes) +void ull_to_bytes(unsigned char *out, unsigned int outlen, + unsigned long long in); +#define u32_to_bytes SPX_NAMESPACE(u32_to_bytes) +void u32_to_bytes(unsigned char *out, uint32_t in); + +/** + * Converts the inlen bytes in 'in' from big-endian byte order to an integer. + */ +#define bytes_to_ull SPX_NAMESPACE(bytes_to_ull) +unsigned long long bytes_to_ull(const unsigned char *in, unsigned int inlen); + +/** + * Computes a root node given a leaf and an auth path. + * Expects address to be complete other than the tree_height and tree_index. + */ +#define compute_root SPX_NAMESPACE(compute_root) +void compute_root(unsigned char *root, const unsigned char *leaf, + uint32_t leaf_idx, uint32_t idx_offset, + const unsigned char *auth_path, uint32_t tree_height, + const spx_ctx *ctx, uint32_t addr[8]); + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + */ +#define treehash SPX_NAMESPACE(treehash) +void treehash(unsigned char *root, unsigned char *auth_path, + const spx_ctx* ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leaf)( + unsigned char* /* leaf */, + const spx_ctx* ctx /* ctx */, + uint32_t /* addr_idx */, const uint32_t[8] /* tree_addr */), + uint32_t tree_addr[8]); + + +#endif diff --git a/src/crypto/pq/_s/ref/utilsx1.c b/src/crypto/pq/_s/ref/utilsx1.c new file mode 100644 index 0000000..f6a6700 --- /dev/null +++ b/src/crypto/pq/_s/ref/utilsx1.c @@ -0,0 +1,100 @@ +#include + +#include "utils.h" +#include "utilsx1.h" +#include "params.h" +#include "thash.h" +#include "address.h" + +/* + * Generate the entire Merkle tree, computing the authentication path for + * leaf_idx, and the resulting root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE) + * + * This expects tree_addr to be initialized to the addr structures for the + * Merkle tree nodes + * + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + * + * This works by using the standard Merkle tree building algorithm, + */ +void treehashx1(unsigned char *root, unsigned char *auth_path, + const spx_ctx* ctx, + uint32_t leaf_idx, uint32_t idx_offset, + uint32_t tree_height, + void (*gen_leaf)( + unsigned char* /* Where to write the leaves */, + const spx_ctx* /* ctx */, + uint32_t idx, void *info), + uint32_t tree_addr[8], + void *info) +{ + /* This is where we keep the intermediate nodes */ + SPX_VLA(uint8_t, stack, tree_height*SPX_N); + + uint32_t idx; + uint32_t max_idx = (uint32_t)((1 << tree_height) - 1); + for (idx = 0;; idx++) { + unsigned char current[2*SPX_N]; /* Current logical node is at */ + /* index[SPX_N]. We do this to minimize the number of copies */ + /* needed during a thash */ + gen_leaf( ¤t[SPX_N], ctx, idx + idx_offset, + info ); + + /* Now combine the freshly generated right node with previously */ + /* generated left ones */ + uint32_t internal_idx_offset = idx_offset; + uint32_t internal_idx = idx; + uint32_t internal_leaf = leaf_idx; + uint32_t h; /* The height we are in the Merkle tree */ + for (h=0;; h++, internal_idx >>= 1, internal_leaf >>= 1) { + + /* Check if we hit the top of the tree */ + if (h == tree_height) { + /* We hit the root; return it */ + memcpy( root, ¤t[SPX_N], SPX_N ); + return; + } + + /* + * Check if the node we have is a part of the + * authentication path; if it is, write it out + */ + if ((internal_idx ^ internal_leaf) == 0x01) { + memcpy( &auth_path[ h * SPX_N ], + ¤t[SPX_N], + SPX_N ); + } + + /* + * Check if we're at a left child; if so, stop going up the stack + * Exception: if we've reached the end of the tree, keep on going + * (so we combine the last 4 nodes into the one root node in two + * more iterations) + */ + if ((internal_idx & 1) == 0 && idx < max_idx) { + break; + } + + /* Ok, we're at a right node */ + /* Now combine the left and right logical nodes together */ + + /* Set the address of the node we're creating. */ + internal_idx_offset >>= 1; + set_tree_height(tree_addr, h + 1); + set_tree_index(tree_addr, internal_idx/2 + internal_idx_offset ); + + unsigned char *left = &stack[h * SPX_N]; + memcpy( ¤t[0], left, SPX_N ); + thash( ¤t[1 * SPX_N], + ¤t[0 * SPX_N], + 2, ctx, tree_addr); + } + + /* We've hit a left child; save the current for when we get the */ + /* corresponding right right */ + memcpy( &stack[h * SPX_N], ¤t[SPX_N], SPX_N); + } +} diff --git a/src/crypto/pq/_s/ref/utilsx1.h b/src/crypto/pq/_s/ref/utilsx1.h new file mode 100644 index 0000000..a7fcf15 --- /dev/null +++ b/src/crypto/pq/_s/ref/utilsx1.h @@ -0,0 +1,26 @@ +#ifndef SPX_UTILSX4_H +#define SPX_UTILSX4_H + +#include +#include "params.h" +#include "context.h" + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + */ +#define treehashx1 SPX_NAMESPACE(treehashx1) +void treehashx1(unsigned char *root, unsigned char *auth_path, + const spx_ctx* ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leaf)( + unsigned char* /* Where to write the leaf */, + const spx_ctx* /* ctx */, + uint32_t addr_idx, void *info), + uint32_t tree_addrx4[8], void *info); + +#endif diff --git a/src/crypto/pq/_s/ref/wots.c b/src/crypto/pq/_s/ref/wots.c new file mode 100644 index 0000000..df83278 --- /dev/null +++ b/src/crypto/pq/_s/ref/wots.c @@ -0,0 +1,112 @@ +#include +#include + +#include "utils.h" +#include "utilsx1.h" +#include "hash.h" +#include "thash.h" +#include "wots.h" +#include "wotsx1.h" +#include "address.h" +#include "params.h" + +// TODO clarify address expectations, and make them more uniform. +// TODO i.e. do we expect types to be set already? +// TODO and do we expect modifications or copies? + +/** + * Computes the chaining function. + * out and in have to be n-byte arrays. + * + * Interprets in as start-th value of the chain. + * addr has to contain the address of the chain. + */ +static void gen_chain(unsigned char *out, const unsigned char *in, + unsigned int start, unsigned int steps, + const spx_ctx *ctx, uint32_t addr[8]) +{ + uint32_t i; + + /* Initialize out with the value at position 'start'. */ + memcpy(out, in, SPX_N); + + /* Iterate 'steps' calls to the hash function. */ + for (i = start; i < (start+steps) && i < SPX_WOTS_W; i++) { + set_hash_addr(addr, i); + thash(out, out, 1, ctx, addr); + } +} + +/** + * base_w algorithm as described in draft. + * Interprets an array of bytes as integers in base w. + * This only works when log_w is a divisor of 8. + */ +static void base_w(unsigned int *output, const int out_len, + const unsigned char *input) +{ + int in = 0; + int out = 0; + unsigned char total; + int bits = 0; + int consumed; + + for (consumed = 0; consumed < out_len; consumed++) { + if (bits == 0) { + total = input[in]; + in++; + bits += 8; + } + bits -= SPX_WOTS_LOGW; + output[out] = (total >> bits) & (SPX_WOTS_W - 1); + out++; + } +} + +/* Computes the WOTS+ checksum over a message (in base_w). */ +static void wots_checksum(unsigned int *csum_base_w, + const unsigned int *msg_base_w) +{ + unsigned int csum = 0; + unsigned char csum_bytes[(SPX_WOTS_LEN2 * SPX_WOTS_LOGW + 7) / 8]; + unsigned int i; + + /* Compute checksum. */ + for (i = 0; i < SPX_WOTS_LEN1; i++) { + csum += SPX_WOTS_W - 1 - msg_base_w[i]; + } + + /* Convert checksum to base_w. */ + /* Make sure expected empty zero bits are the least significant bits. */ + csum = csum << ((8 - ((SPX_WOTS_LEN2 * SPX_WOTS_LOGW) % 8)) % 8); + ull_to_bytes(csum_bytes, sizeof(csum_bytes), csum); + base_w(csum_base_w, SPX_WOTS_LEN2, csum_bytes); +} + +/* Takes a message and derives the matching chain lengths. */ +void chain_lengths(unsigned int *lengths, const unsigned char *msg) +{ + base_w(lengths, SPX_WOTS_LEN1, msg); + wots_checksum(lengths + SPX_WOTS_LEN1, lengths); +} + +/** + * Takes a WOTS signature and an n-byte message, computes a WOTS public key. + * + * Writes the computed public key to 'pk'. + */ +void wots_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *msg, + const spx_ctx *ctx, uint32_t addr[8]) +{ + unsigned int lengths[SPX_WOTS_LEN]; + uint32_t i; + + chain_lengths(lengths, msg); + + for (i = 0; i < SPX_WOTS_LEN; i++) { + set_chain_addr(addr, i); + gen_chain(pk + i*SPX_N, sig + i*SPX_N, + lengths[i], SPX_WOTS_W - 1 - lengths[i], ctx, addr); + } +} diff --git a/src/crypto/pq/_s/ref/wots.h b/src/crypto/pq/_s/ref/wots.h new file mode 100644 index 0000000..7e77056 --- /dev/null +++ b/src/crypto/pq/_s/ref/wots.h @@ -0,0 +1,25 @@ +#ifndef SPX_WOTS_H +#define SPX_WOTS_H + +#include + +#include "params.h" +#include "context.h" + +/** + * Takes a WOTS signature and an n-byte message, computes a WOTS public key. + * + * Writes the computed public key to 'pk'. + */ +#define wots_pk_from_sig SPX_NAMESPACE(wots_pk_from_sig) +void wots_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *msg, + const spx_ctx *ctx, uint32_t addr[8]); + +/* + * Compute the chain lengths needed for a given message hash + */ +#define chain_lengths SPX_NAMESPACE(chain_lengths) +void chain_lengths(unsigned int *lengths, const unsigned char *msg); + +#endif diff --git a/src/crypto/pq/_s/ref/wotsx1.c b/src/crypto/pq/_s/ref/wotsx1.c new file mode 100644 index 0000000..dfb3780 --- /dev/null +++ b/src/crypto/pq/_s/ref/wotsx1.c @@ -0,0 +1,73 @@ +#include +#include + +#include "utils.h" +#include "hash.h" +#include "thash.h" +#include "wots.h" +#include "wotsx1.h" +#include "address.h" +#include "params.h" + +/* + * This generates a WOTS public key + * It also generates the WOTS signature if leaf_info indicates + * that we're signing with this WOTS key + */ +void wots_gen_leafx1(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info) { + struct leaf_info_x1 *info = v_info; + uint32_t *leaf_addr = info->leaf_addr; + uint32_t *pk_addr = info->pk_addr; + unsigned int i, k; + unsigned char pk_buffer[ SPX_WOTS_BYTES ]; + unsigned char *buffer; + uint32_t wots_k_mask; + + if (leaf_idx == info->wots_sign_leaf) { + /* We're traversing the leaf that's signing; generate the WOTS */ + /* signature */ + wots_k_mask = 0; + } else { + /* Nope, we're just generating pk's; turn off the signature logic */ + wots_k_mask = (uint32_t)~0; + } + + set_keypair_addr( leaf_addr, leaf_idx ); + set_keypair_addr( pk_addr, leaf_idx ); + + for (i = 0, buffer = pk_buffer; i < SPX_WOTS_LEN; i++, buffer += SPX_N) { + uint32_t wots_k = info->wots_steps[i] | wots_k_mask; /* Set wots_k to */ + /* the step if we're generating a signature, ~0 if we're not */ + + /* Start with the secret seed */ + set_chain_addr(leaf_addr, i); + set_hash_addr(leaf_addr, 0); + set_type(leaf_addr, SPX_ADDR_TYPE_WOTSPRF); + + prf_addr(buffer, ctx, leaf_addr); + + set_type(leaf_addr, SPX_ADDR_TYPE_WOTS); + + /* Iterate down the WOTS chain */ + for (k=0;; k++) { + /* Check if this is the value that needs to be saved as a */ + /* part of the WOTS signature */ + if (k == wots_k) { + memcpy( info->wots_sig + i * SPX_N, buffer, SPX_N ); + } + + /* Check if we hit the top of the chain */ + if (k == SPX_WOTS_W - 1) break; + + /* Iterate one step on the chain */ + set_hash_addr(leaf_addr, k); + + thash(buffer, buffer, 1, ctx, leaf_addr); + } + } + + /* Do the final thash to generate the public keys */ + thash(dest, pk_buffer, SPX_WOTS_LEN, ctx, pk_addr); +} diff --git a/src/crypto/pq/_s/ref/wotsx1.h b/src/crypto/pq/_s/ref/wotsx1.h new file mode 100644 index 0000000..1257f81 --- /dev/null +++ b/src/crypto/pq/_s/ref/wotsx1.h @@ -0,0 +1,36 @@ +#if !defined( WOTSX1_H_ ) +#define WOTSX1_H_ + +#include + +/* + * This is here to provide an interface to the internal wots_gen_leafx1 + * routine. While this routine is not referenced in the package outside of + * wots.c, it is called from the stand-alone benchmark code to characterize + * the performance + */ +struct leaf_info_x1 { + unsigned char *wots_sig; + uint32_t wots_sign_leaf; /* The index of the WOTS we're using to sign */ + uint32_t *wots_steps; + uint32_t leaf_addr[8]; + uint32_t pk_addr[8]; +}; + +/* Macro to set the leaf_info to something 'benign', that is, it would */ +/* run with the same time as it does during the real signing process */ +/* Used only by the benchmark code */ +#define INITIALIZE_LEAF_INFO_X1(info, addr, step_buffer) { \ + info.wots_sig = 0; \ + info.wots_sign_leaf = ~0u; \ + info.wots_steps = step_buffer; \ + memcpy( &info.leaf_addr[0], addr, 32 ); \ + memcpy( &info.pk_addr[0], addr, 32 ); \ +} + +#define wots_gen_leafx1 SPX_NAMESPACE(wots_gen_leafx1) +void wots_gen_leafx1(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info); + +#endif /* WOTSX1_H_ */ diff --git a/src/crypto/pq/_s/sha2-avx2/.gitignore b/src/crypto/pq/_s/sha2-avx2/.gitignore new file mode 100644 index 0000000..20d83ac --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/.gitignore @@ -0,0 +1,6 @@ +test/* +!test/*.c +PQCsignKAT_*.rsp +PQCsignKAT_*.req +PQCgenKAT_sign +keccak4x/KeccakP-1600-times4-SIMD256.o \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/PQCgenKAT_sign.c b/src/crypto/pq/_s/sha2-avx2/PQCgenKAT_sign.c new file mode 120000 index 0000000..a17dbe2 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/PQCgenKAT_sign.c @@ -0,0 +1 @@ +../ref/PQCgenKAT_sign.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/address.c b/src/crypto/pq/_s/sha2-avx2/address.c new file mode 120000 index 0000000..02c52c6 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/address.c @@ -0,0 +1 @@ +../ref/address.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/address.h b/src/crypto/pq/_s/sha2-avx2/address.h new file mode 120000 index 0000000..e670da5 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/address.h @@ -0,0 +1 @@ +../ref/address.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/api.h b/src/crypto/pq/_s/sha2-avx2/api.h new file mode 120000 index 0000000..ea89e0a --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/api.h @@ -0,0 +1 @@ +../ref/api.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/context.h b/src/crypto/pq/_s/sha2-avx2/context.h new file mode 100644 index 0000000..05ffbe8 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/context.h @@ -0,0 +1,18 @@ +#ifndef SPX_CONTEXT_H +#define SPX_CONTEXT_H + +#include + +#include "params.h" + +typedef struct { + uint8_t pub_seed[SPX_N]; + uint8_t sk_seed[SPX_N]; + + uint8_t state_seeded[40]; +#if SPX_SHA512 + uint8_t state_seeded_512[72]; +#endif +} spx_ctx; + +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/fors.c b/src/crypto/pq/_s/sha2-avx2/fors.c new file mode 100644 index 0000000..67ad322 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/fors.c @@ -0,0 +1,233 @@ +#include +#include +#include + +#include "fors.h" +#include "utils.h" +#include "utilsx8.h" +#include "hash.h" +#include "hashx8.h" +#include "thash.h" +#include "thashx8.h" +#include "address.h" + +static void fors_gen_sk(unsigned char *sk, const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + prf_addr(sk, ctx, fors_leaf_addr); +} + +static void fors_gen_skx8(unsigned char *sk0, + unsigned char *sk1, + unsigned char *sk2, + unsigned char *sk3, + unsigned char *sk4, + unsigned char *sk5, + unsigned char *sk6, + unsigned char *sk7, const spx_ctx *ctx, + uint32_t fors_leaf_addrx8[8*8]) +{ + prf_addrx8(sk0, sk1, sk2, sk3, sk4, sk5, sk6, sk7, + ctx, fors_leaf_addrx8); +} + +static void fors_sk_to_leaf(unsigned char *leaf, const unsigned char *sk, + const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + thash(leaf, sk, 1, ctx, fors_leaf_addr); +} + +static void fors_sk_to_leafx8(unsigned char *leaf0, + unsigned char *leaf1, + unsigned char *leaf2, + unsigned char *leaf3, + unsigned char *leaf4, + unsigned char *leaf5, + unsigned char *leaf6, + unsigned char *leaf7, + const unsigned char *sk0, + const unsigned char *sk1, + const unsigned char *sk2, + const unsigned char *sk3, + const unsigned char *sk4, + const unsigned char *sk5, + const unsigned char *sk6, + const unsigned char *sk7, + const spx_ctx *ctx, + uint32_t fors_leaf_addrx8[8*8]) +{ + thashx8(leaf0, leaf1, leaf2, leaf3, leaf4, leaf5, leaf6, leaf7, + sk0, sk1, sk2, sk3, sk4, sk5, sk6, sk7, + 1, ctx, fors_leaf_addrx8); +} + +struct fors_gen_leaf_info { + uint32_t leaf_addrx[8*8]; +}; + +static void fors_gen_leafx8(unsigned char *leaf, + const spx_ctx *ctx, + uint32_t addr_idx, void *info) +{ + struct fors_gen_leaf_info *fors_info = info; + uint32_t *fors_leaf_addrx8 = fors_info->leaf_addrx; + unsigned int j; + + /* Only set the parts that the caller doesn't set */ + for (j = 0; j < 8; j++) { + set_tree_index(fors_leaf_addrx8 + j*8, addr_idx + j); + set_type(fors_leaf_addrx8 + j*8, SPX_ADDR_TYPE_FORSPRF); + } + + fors_gen_skx8(leaf + 0*SPX_N, + leaf + 1*SPX_N, + leaf + 2*SPX_N, + leaf + 3*SPX_N, + leaf + 4*SPX_N, + leaf + 5*SPX_N, + leaf + 6*SPX_N, + leaf + 7*SPX_N, + ctx, fors_leaf_addrx8); + + for (j = 0; j < 8; j++) { + set_type(fors_leaf_addrx8 + j*8, SPX_ADDR_TYPE_FORSTREE); + } + + fors_sk_to_leafx8(leaf + 0*SPX_N, + leaf + 1*SPX_N, + leaf + 2*SPX_N, + leaf + 3*SPX_N, + leaf + 4*SPX_N, + leaf + 5*SPX_N, + leaf + 6*SPX_N, + leaf + 7*SPX_N, + leaf + 0*SPX_N, + leaf + 1*SPX_N, + leaf + 2*SPX_N, + leaf + 3*SPX_N, + leaf + 4*SPX_N, + leaf + 5*SPX_N, + leaf + 6*SPX_N, + leaf + 7*SPX_N, + ctx, fors_leaf_addrx8); +} + +/** + * Interprets m as SPX_FORS_HEIGHT-bit unsigned integers. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + * Assumes indices has space for SPX_FORS_TREES integers. + */ +static void message_to_indices(uint32_t *indices, const unsigned char *m) +{ + unsigned int i, j; + unsigned int offset = 0; + + for (i = 0; i < SPX_FORS_TREES; i++) { + indices[i] = 0; + for (j = 0; j < SPX_FORS_HEIGHT; j++) { + indices[i] ^= ((m[offset >> 3] >> (offset & 0x7)) & 0x1) << j; + offset++; + } + } +} + +/** + * Signs a message m, deriving the secret key from sk_seed and the FTS address. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_sign(unsigned char *sig, unsigned char *pk, + const unsigned char *m, + const spx_ctx *ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + uint32_t fors_tree_addr[8*8] = {0}; + struct fors_gen_leaf_info fors_info = {0}; + uint32_t *fors_leaf_addr = fors_info.leaf_addrx; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + for (i=0; i<8; i++) { + copy_keypair_addr(fors_tree_addr + 8*i, fors_addr); + set_type(fors_tree_addr + 8*i, SPX_ADDR_TYPE_FORSTREE); + copy_keypair_addr(fors_leaf_addr + 8*i, fors_addr); + } + copy_keypair_addr(fors_pk_addr, fors_addr); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + + /* Include the secret key part that produces the selected leaf node. */ + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSPRF); + fors_gen_sk(sig, ctx, fors_tree_addr); + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + sig += SPX_N; + + /* Compute the authentication path for this leaf node. */ + treehashx8(roots + i*SPX_N, sig, ctx, + indices[i], idx_offset, SPX_FORS_HEIGHT, fors_gen_leafx8, + fors_tree_addr, &fors_info); + + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} + +/** + * Derives the FORS public key from a signature. + * This can be used for verification by comparing to a known public key, or to + * subsequently verify a signature on the derived public key. The latter is the + * typical use-case when used as an FTS below an OTS in a hypertree. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *m, + const spx_ctx *ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + unsigned char leaf[SPX_N]; + uint32_t fors_tree_addr[8] = {0}; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + copy_keypair_addr(fors_tree_addr, fors_addr); + copy_keypair_addr(fors_pk_addr, fors_addr); + + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + + /* Derive the leaf from the included secret key part. */ + fors_sk_to_leaf(leaf, sig, ctx, fors_tree_addr); + sig += SPX_N; + + /* Derive the corresponding root node of this tree. */ + compute_root(roots + i*SPX_N, leaf, indices[i], idx_offset, + sig, SPX_FORS_HEIGHT, ctx, fors_tree_addr); + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} diff --git a/src/crypto/pq/_s/sha2-avx2/fors.h b/src/crypto/pq/_s/sha2-avx2/fors.h new file mode 120000 index 0000000..07156bd --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/fors.h @@ -0,0 +1 @@ +../ref/fors.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/hash.h b/src/crypto/pq/_s/sha2-avx2/hash.h new file mode 120000 index 0000000..cffc52b --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/hash.h @@ -0,0 +1 @@ +../ref/hash.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/hash_sha2.c b/src/crypto/pq/_s/sha2-avx2/hash_sha2.c new file mode 120000 index 0000000..cfd85d4 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/hash_sha2.c @@ -0,0 +1 @@ +../ref/hash_sha2.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/hash_sha2x8.c b/src/crypto/pq/_s/sha2-avx2/hash_sha2x8.c new file mode 100644 index 0000000..3f59b9a --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/hash_sha2x8.c @@ -0,0 +1,74 @@ +#include +#include + +#include "address.h" +#include "utils.h" +#include "params.h" +#include "hashx8.h" +#include "sha2.h" +#include "sha256x8.h" +#include "sha256avx.h" + +/* + * 8-way parallel version of prf_addr; takes 8x as much input and output + */ +void prf_addrx8(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const spx_ctx *ctx, + const uint32_t addrx8[8*8]) +{ + unsigned char bufx8[8 * (SPX_N + SPX_SHA256_ADDR_BYTES)]; + unsigned char outbufx8[8 * SPX_SHA256_OUTPUT_BYTES]; + unsigned int j; + + for (j = 0; j < 8; j++) { + memcpy(bufx8 + j*(SPX_N + SPX_SHA256_ADDR_BYTES), + addrx8 + j*8, SPX_SHA256_ADDR_BYTES); + memcpy( + bufx8 + j*(SPX_N + SPX_SHA256_ADDR_BYTES) + SPX_SHA256_ADDR_BYTES, + ctx->sk_seed, + SPX_N + ); + } + + sha256x8_seeded( + /* out */ + outbufx8 + 0*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 1*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 2*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 3*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 4*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 5*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 6*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 7*SPX_SHA256_OUTPUT_BYTES, + + /* seed */ + ctx->state_seeded, 512, + + /* in */ + bufx8 + 0*(SPX_SHA256_ADDR_BYTES + SPX_N), + bufx8 + 1*(SPX_SHA256_ADDR_BYTES + SPX_N), + bufx8 + 2*(SPX_SHA256_ADDR_BYTES + SPX_N), + bufx8 + 3*(SPX_SHA256_ADDR_BYTES + SPX_N), + bufx8 + 4*(SPX_SHA256_ADDR_BYTES + SPX_N), + bufx8 + 5*(SPX_SHA256_ADDR_BYTES + SPX_N), + bufx8 + 6*(SPX_SHA256_ADDR_BYTES + SPX_N), + bufx8 + 7*(SPX_SHA256_ADDR_BYTES + SPX_N), + SPX_SHA256_ADDR_BYTES + SPX_N /* len */ + ); + + memcpy(out0, outbufx8 + 0*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out1, outbufx8 + 1*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out2, outbufx8 + 2*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out3, outbufx8 + 3*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out4, outbufx8 + 4*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out5, outbufx8 + 5*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out6, outbufx8 + 6*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out7, outbufx8 + 7*SPX_SHA256_OUTPUT_BYTES, SPX_N); +} diff --git a/src/crypto/pq/_s/sha2-avx2/hashx8.h b/src/crypto/pq/_s/sha2-avx2/hashx8.h new file mode 100644 index 0000000..dc924b4 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/hashx8.h @@ -0,0 +1,19 @@ +#ifndef SPX_HASHX8_H +#define SPX_HASHX8_H + +#include +#include "params.h" + +#define prf_addrx8 SPX_NAMESPACE(prf_addrx8) +void prf_addrx8(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const spx_ctx *ctx, + const uint32_t addrx8[8*8]); + +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/merkle.c b/src/crypto/pq/_s/sha2-avx2/merkle.c new file mode 100644 index 0000000..0b3f666 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/merkle.c @@ -0,0 +1,67 @@ +#include +#include + +#include "utils.h" +#include "utilsx8.h" +#include "wots.h" +#include "wotsx8.h" +#include "wotsx8.h" +#include "merkle.h" +#include "address.h" +#include "params.h" + +/* + * This generates a Merkle signature (WOTS signature followed by the Merkle + * authentication path). + */ +void merkle_sign(uint8_t *sig, unsigned char *root, + const spx_ctx *ctx, + uint32_t wots_addr[8], uint32_t tree_addr[8], + uint32_t idx_leaf) +{ + unsigned char *auth_path = sig + SPX_WOTS_BYTES; + uint32_t tree_addrx8[8*8] = { 0 }; + int j; + struct leaf_info_x8 info = { 0 }; + unsigned steps[ SPX_WOTS_LEN ]; + + info.wots_sig = sig; + chain_lengths(steps, root); + info.wots_steps = steps; + + for (j=0; j<8; j++) { + set_type(&tree_addrx8[8*j], SPX_ADDR_TYPE_HASHTREE); + set_type(&info.leaf_addr[8*j], SPX_ADDR_TYPE_WOTS); + set_type(&info.pk_addr[8*j], SPX_ADDR_TYPE_WOTSPK); + copy_subtree_addr(&tree_addrx8[8*j], tree_addr); + copy_subtree_addr(&info.leaf_addr[8*j], wots_addr); + copy_subtree_addr(&info.pk_addr[8*j], wots_addr); + } + + info.wots_sign_leaf = idx_leaf; + + treehashx8(root, auth_path, ctx, + idx_leaf, 0, + SPX_TREE_HEIGHT, + wots_gen_leafx8, + tree_addrx8, &info); +} + +/* Compute root node of the top-most subtree. */ +/* Again, in this file because wots_gen_leaf is most of the work */ +void merkle_gen_root(unsigned char *root, const spx_ctx *ctx) +{ + /* We do not need the auth path in key generation, but it simplifies the + code to have just one treehash routine that computes both root and path + in one function. */ + unsigned char auth_path[SPX_TREE_HEIGHT * SPX_N + SPX_WOTS_BYTES]; + uint32_t top_tree_addr[8] = {0}; + uint32_t wots_addr[8] = {0}; + + set_layer_addr(top_tree_addr, SPX_D - 1); + set_layer_addr(wots_addr, SPX_D - 1); + + merkle_sign(auth_path, root, ctx, + wots_addr, top_tree_addr, + ~0 /* ~0 means "don't bother generating an auth path */ ); +} diff --git a/src/crypto/pq/_s/sha2-avx2/merkle.h b/src/crypto/pq/_s/sha2-avx2/merkle.h new file mode 120000 index 0000000..7d167ed --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/merkle.h @@ -0,0 +1 @@ +../ref/merkle.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/params.h b/src/crypto/pq/_s/sha2-avx2/params.h new file mode 120000 index 0000000..53133cc --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/params.h @@ -0,0 +1 @@ +../ref/params.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-128f.h b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-128f.h new file mode 120000 index 0000000..d901a75 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-128f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-sha2-128f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-128s.h b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-128s.h new file mode 120000 index 0000000..a9955de --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-128s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-sha2-128s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-192f.h b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-192f.h new file mode 120000 index 0000000..ecbc980 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-192f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-sha2-192f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-192s.h b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-192s.h new file mode 120000 index 0000000..c2db1ba --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-192s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-sha2-192s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-256f.h b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-256f.h new file mode 120000 index 0000000..b388615 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-256f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-sha2-256f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-256s.h b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-256s.h new file mode 120000 index 0000000..420ffec --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/params/params-sphincs-sha2-256s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-sha2-256s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/randombytes.c b/src/crypto/pq/_s/sha2-avx2/randombytes.c new file mode 120000 index 0000000..59a42a5 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/randombytes.c @@ -0,0 +1 @@ +../ref/randombytes.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/randombytes.h b/src/crypto/pq/_s/sha2-avx2/randombytes.h new file mode 120000 index 0000000..055e443 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/randombytes.h @@ -0,0 +1 @@ +../ref/randombytes.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/rng.c b/src/crypto/pq/_s/sha2-avx2/rng.c new file mode 120000 index 0000000..6e2fdac --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/rng.c @@ -0,0 +1 @@ +../ref/rng.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/rng.h b/src/crypto/pq/_s/sha2-avx2/rng.h new file mode 120000 index 0000000..d678c7c --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/rng.h @@ -0,0 +1 @@ +../ref/rng.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/sha2.c b/src/crypto/pq/_s/sha2-avx2/sha2.c new file mode 120000 index 0000000..7ba32d4 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha2.c @@ -0,0 +1 @@ +../ref/sha2.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/sha2.h b/src/crypto/pq/_s/sha2-avx2/sha2.h new file mode 120000 index 0000000..8c1ff67 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha2.h @@ -0,0 +1 @@ +../ref/sha2.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/sha256avx.c b/src/crypto/pq/_s/sha2-avx2/sha256avx.c new file mode 100644 index 0000000..7fc1091 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha256avx.c @@ -0,0 +1,294 @@ +#include +#include +#include + +#include "sha256avx.h" + +// Transpose 8 vectors containing 32-bit values +void transpose(u256 s[8]) { + u256 tmp0[8]; + u256 tmp1[8]; + tmp0[0] = _mm256_unpacklo_epi32(s[0], s[1]); + tmp0[1] = _mm256_unpackhi_epi32(s[0], s[1]); + tmp0[2] = _mm256_unpacklo_epi32(s[2], s[3]); + tmp0[3] = _mm256_unpackhi_epi32(s[2], s[3]); + tmp0[4] = _mm256_unpacklo_epi32(s[4], s[5]); + tmp0[5] = _mm256_unpackhi_epi32(s[4], s[5]); + tmp0[6] = _mm256_unpacklo_epi32(s[6], s[7]); + tmp0[7] = _mm256_unpackhi_epi32(s[6], s[7]); + tmp1[0] = _mm256_unpacklo_epi64(tmp0[0], tmp0[2]); + tmp1[1] = _mm256_unpackhi_epi64(tmp0[0], tmp0[2]); + tmp1[2] = _mm256_unpacklo_epi64(tmp0[1], tmp0[3]); + tmp1[3] = _mm256_unpackhi_epi64(tmp0[1], tmp0[3]); + tmp1[4] = _mm256_unpacklo_epi64(tmp0[4], tmp0[6]); + tmp1[5] = _mm256_unpackhi_epi64(tmp0[4], tmp0[6]); + tmp1[6] = _mm256_unpacklo_epi64(tmp0[5], tmp0[7]); + tmp1[7] = _mm256_unpackhi_epi64(tmp0[5], tmp0[7]); + s[0] = _mm256_permute2x128_si256(tmp1[0], tmp1[4], 0x20); + s[1] = _mm256_permute2x128_si256(tmp1[1], tmp1[5], 0x20); + s[2] = _mm256_permute2x128_si256(tmp1[2], tmp1[6], 0x20); + s[3] = _mm256_permute2x128_si256(tmp1[3], tmp1[7], 0x20); + s[4] = _mm256_permute2x128_si256(tmp1[0], tmp1[4], 0x31); + s[5] = _mm256_permute2x128_si256(tmp1[1], tmp1[5], 0x31); + s[6] = _mm256_permute2x128_si256(tmp1[2], tmp1[6], 0x31); + s[7] = _mm256_permute2x128_si256(tmp1[3], tmp1[7], 0x31); +} + +void sha256_init8x(sha256ctx *ctx) { + ctx->s[0] = _mm256_set_epi32(0x6a09e667,0x6a09e667,0x6a09e667,0x6a09e667,0x6a09e667,0x6a09e667,0x6a09e667,0x6a09e667); + ctx->s[1] = _mm256_set_epi32(0xbb67ae85,0xbb67ae85,0xbb67ae85,0xbb67ae85,0xbb67ae85,0xbb67ae85,0xbb67ae85,0xbb67ae85); + ctx->s[2] = _mm256_set_epi32(0x3c6ef372,0x3c6ef372,0x3c6ef372,0x3c6ef372,0x3c6ef372,0x3c6ef372,0x3c6ef372,0x3c6ef372); + ctx->s[3] = _mm256_set_epi32(0xa54ff53a,0xa54ff53a,0xa54ff53a,0xa54ff53a,0xa54ff53a,0xa54ff53a,0xa54ff53a,0xa54ff53a); + ctx->s[4] = _mm256_set_epi32(0x510e527f,0x510e527f,0x510e527f,0x510e527f,0x510e527f,0x510e527f,0x510e527f,0x510e527f); + ctx->s[5] = _mm256_set_epi32(0x9b05688c,0x9b05688c,0x9b05688c,0x9b05688c,0x9b05688c,0x9b05688c,0x9b05688c,0x9b05688c); + ctx->s[6] = _mm256_set_epi32(0x1f83d9ab,0x1f83d9ab,0x1f83d9ab,0x1f83d9ab,0x1f83d9ab,0x1f83d9ab,0x1f83d9ab,0x1f83d9ab); + ctx->s[7] = _mm256_set_epi32(0x5be0cd19,0x5be0cd19,0x5be0cd19,0x5be0cd19,0x5be0cd19,0x5be0cd19,0x5be0cd19,0x5be0cd19); + + ctx->datalen = 0; + ctx->msglen = 0; +} + +void sha256_final8x(sha256ctx *ctx, + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7) +{ + unsigned int i, curlen; + + // Padding + if (ctx->datalen < 56) { + for (i = 0; i < 8; ++i) { + curlen = ctx->datalen; + ctx->msgblocks[64*i + curlen++] = 0x80; + while(curlen < 64) { + ctx->msgblocks[64*i + curlen++] = 0x00; + } + } + } else { + for (i = 0; i < 8; ++i) { + curlen = ctx->datalen; + ctx->msgblocks[64*i + curlen++] = 0x80; + while(curlen < 64) { + ctx->msgblocks[64*i + curlen++] = 0x00; + } + } + sha256_transform8x(ctx, + &ctx->msgblocks[64*0], + &ctx->msgblocks[64*1], + &ctx->msgblocks[64*2], + &ctx->msgblocks[64*3], + &ctx->msgblocks[64*4], + &ctx->msgblocks[64*5], + &ctx->msgblocks[64*6], + &ctx->msgblocks[64*7] + ); + memset(ctx->msgblocks, 0, 8 * 64); + } + + // Add length of the message to each block + ctx->msglen += ctx->datalen * 8; + for (i = 0; i < 8; i++) { + ctx->msgblocks[64*i + 63] = ctx->msglen; + ctx->msgblocks[64*i + 62] = ctx->msglen >> 8; + ctx->msgblocks[64*i + 61] = ctx->msglen >> 16; + ctx->msgblocks[64*i + 60] = ctx->msglen >> 24; + ctx->msgblocks[64*i + 59] = ctx->msglen >> 32; + ctx->msgblocks[64*i + 58] = ctx->msglen >> 40; + ctx->msgblocks[64*i + 57] = ctx->msglen >> 48; + ctx->msgblocks[64*i + 56] = ctx->msglen >> 56; + } + sha256_transform8x(ctx, + &ctx->msgblocks[64*0], + &ctx->msgblocks[64*1], + &ctx->msgblocks[64*2], + &ctx->msgblocks[64*3], + &ctx->msgblocks[64*4], + &ctx->msgblocks[64*5], + &ctx->msgblocks[64*6], + &ctx->msgblocks[64*7] + ); + + // Compute final hash output + transpose(ctx->s); + + // Store Hash value + STORE(out0, BYTESWAP(ctx->s[0])); + STORE(out1, BYTESWAP(ctx->s[1])); + STORE(out2, BYTESWAP(ctx->s[2])); + STORE(out3, BYTESWAP(ctx->s[3])); + STORE(out4, BYTESWAP(ctx->s[4])); + STORE(out5, BYTESWAP(ctx->s[5])); + STORE(out6, BYTESWAP(ctx->s[6])); + STORE(out7, BYTESWAP(ctx->s[7])); +} + +void sha256_transform8x(sha256ctx *ctx, + const unsigned char* data0, + const unsigned char* data1, + const unsigned char* data2, + const unsigned char* data3, + const unsigned char* data4, + const unsigned char* data5, + const unsigned char* data6, + const unsigned char* data7) { + u256 s[8], w[64], T0, T1; + + // Load words and transform data correctly + w[0] = BYTESWAP(LOAD(data0)); + w[0 + 8] = BYTESWAP(LOAD(data0 + 32)); + w[1] = BYTESWAP(LOAD(data1)); + w[1 + 8] = BYTESWAP(LOAD(data1 + 32)); + w[2] = BYTESWAP(LOAD(data2)); + w[2 + 8] = BYTESWAP(LOAD(data2 + 32)); + w[3] = BYTESWAP(LOAD(data3)); + w[3 + 8] = BYTESWAP(LOAD(data3 + 32)); + w[4] = BYTESWAP(LOAD(data4)); + w[4 + 8] = BYTESWAP(LOAD(data4 + 32)); + w[5] = BYTESWAP(LOAD(data5)); + w[5 + 8] = BYTESWAP(LOAD(data5 + 32)); + w[6] = BYTESWAP(LOAD(data6)); + w[6 + 8] = BYTESWAP(LOAD(data6 + 32)); + w[7] = BYTESWAP(LOAD(data7)); + w[7 + 8] = BYTESWAP(LOAD(data7 + 32)); + + transpose(w); + transpose(w + 8); + + // Initial State + s[0] = ctx->s[0]; + s[1] = ctx->s[1]; + s[2] = ctx->s[2]; + s[3] = ctx->s[3]; + s[4] = ctx->s[4]; + s[5] = ctx->s[5]; + s[6] = ctx->s[6]; + s[7] = ctx->s[7]; + + SHA256ROUND_AVX(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 0, w[0]); + SHA256ROUND_AVX(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 1, w[1]); + SHA256ROUND_AVX(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 2, w[2]); + SHA256ROUND_AVX(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 3, w[3]); + SHA256ROUND_AVX(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 4, w[4]); + SHA256ROUND_AVX(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 5, w[5]); + SHA256ROUND_AVX(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 6, w[6]); + SHA256ROUND_AVX(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 7, w[7]); + SHA256ROUND_AVX(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 8, w[8]); + SHA256ROUND_AVX(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 9, w[9]); + SHA256ROUND_AVX(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 10, w[10]); + SHA256ROUND_AVX(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 11, w[11]); + SHA256ROUND_AVX(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 12, w[12]); + SHA256ROUND_AVX(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 13, w[13]); + SHA256ROUND_AVX(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 14, w[14]); + SHA256ROUND_AVX(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 15, w[15]); + w[16] = ADD4_32(WSIGMA1_AVX(w[14]), w[0], w[9], WSIGMA0_AVX(w[1])); + SHA256ROUND_AVX(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 16, w[16]); + w[17] = ADD4_32(WSIGMA1_AVX(w[15]), w[1], w[10], WSIGMA0_AVX(w[2])); + SHA256ROUND_AVX(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 17, w[17]); + w[18] = ADD4_32(WSIGMA1_AVX(w[16]), w[2], w[11], WSIGMA0_AVX(w[3])); + SHA256ROUND_AVX(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 18, w[18]); + w[19] = ADD4_32(WSIGMA1_AVX(w[17]), w[3], w[12], WSIGMA0_AVX(w[4])); + SHA256ROUND_AVX(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 19, w[19]); + w[20] = ADD4_32(WSIGMA1_AVX(w[18]), w[4], w[13], WSIGMA0_AVX(w[5])); + SHA256ROUND_AVX(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 20, w[20]); + w[21] = ADD4_32(WSIGMA1_AVX(w[19]), w[5], w[14], WSIGMA0_AVX(w[6])); + SHA256ROUND_AVX(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 21, w[21]); + w[22] = ADD4_32(WSIGMA1_AVX(w[20]), w[6], w[15], WSIGMA0_AVX(w[7])); + SHA256ROUND_AVX(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 22, w[22]); + w[23] = ADD4_32(WSIGMA1_AVX(w[21]), w[7], w[16], WSIGMA0_AVX(w[8])); + SHA256ROUND_AVX(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 23, w[23]); + w[24] = ADD4_32(WSIGMA1_AVX(w[22]), w[8], w[17], WSIGMA0_AVX(w[9])); + SHA256ROUND_AVX(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 24, w[24]); + w[25] = ADD4_32(WSIGMA1_AVX(w[23]), w[9], w[18], WSIGMA0_AVX(w[10])); + SHA256ROUND_AVX(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 25, w[25]); + w[26] = ADD4_32(WSIGMA1_AVX(w[24]), w[10], w[19], WSIGMA0_AVX(w[11])); + SHA256ROUND_AVX(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 26, w[26]); + w[27] = ADD4_32(WSIGMA1_AVX(w[25]), w[11], w[20], WSIGMA0_AVX(w[12])); + SHA256ROUND_AVX(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 27, w[27]); + w[28] = ADD4_32(WSIGMA1_AVX(w[26]), w[12], w[21], WSIGMA0_AVX(w[13])); + SHA256ROUND_AVX(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 28, w[28]); + w[29] = ADD4_32(WSIGMA1_AVX(w[27]), w[13], w[22], WSIGMA0_AVX(w[14])); + SHA256ROUND_AVX(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 29, w[29]); + w[30] = ADD4_32(WSIGMA1_AVX(w[28]), w[14], w[23], WSIGMA0_AVX(w[15])); + SHA256ROUND_AVX(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 30, w[30]); + w[31] = ADD4_32(WSIGMA1_AVX(w[29]), w[15], w[24], WSIGMA0_AVX(w[16])); + SHA256ROUND_AVX(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 31, w[31]); + w[32] = ADD4_32(WSIGMA1_AVX(w[30]), w[16], w[25], WSIGMA0_AVX(w[17])); + SHA256ROUND_AVX(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 32, w[32]); + w[33] = ADD4_32(WSIGMA1_AVX(w[31]), w[17], w[26], WSIGMA0_AVX(w[18])); + SHA256ROUND_AVX(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 33, w[33]); + w[34] = ADD4_32(WSIGMA1_AVX(w[32]), w[18], w[27], WSIGMA0_AVX(w[19])); + SHA256ROUND_AVX(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 34, w[34]); + w[35] = ADD4_32(WSIGMA1_AVX(w[33]), w[19], w[28], WSIGMA0_AVX(w[20])); + SHA256ROUND_AVX(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 35, w[35]); + w[36] = ADD4_32(WSIGMA1_AVX(w[34]), w[20], w[29], WSIGMA0_AVX(w[21])); + SHA256ROUND_AVX(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 36, w[36]); + w[37] = ADD4_32(WSIGMA1_AVX(w[35]), w[21], w[30], WSIGMA0_AVX(w[22])); + SHA256ROUND_AVX(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 37, w[37]); + w[38] = ADD4_32(WSIGMA1_AVX(w[36]), w[22], w[31], WSIGMA0_AVX(w[23])); + SHA256ROUND_AVX(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 38, w[38]); + w[39] = ADD4_32(WSIGMA1_AVX(w[37]), w[23], w[32], WSIGMA0_AVX(w[24])); + SHA256ROUND_AVX(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 39, w[39]); + w[40] = ADD4_32(WSIGMA1_AVX(w[38]), w[24], w[33], WSIGMA0_AVX(w[25])); + SHA256ROUND_AVX(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 40, w[40]); + w[41] = ADD4_32(WSIGMA1_AVX(w[39]), w[25], w[34], WSIGMA0_AVX(w[26])); + SHA256ROUND_AVX(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 41, w[41]); + w[42] = ADD4_32(WSIGMA1_AVX(w[40]), w[26], w[35], WSIGMA0_AVX(w[27])); + SHA256ROUND_AVX(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 42, w[42]); + w[43] = ADD4_32(WSIGMA1_AVX(w[41]), w[27], w[36], WSIGMA0_AVX(w[28])); + SHA256ROUND_AVX(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 43, w[43]); + w[44] = ADD4_32(WSIGMA1_AVX(w[42]), w[28], w[37], WSIGMA0_AVX(w[29])); + SHA256ROUND_AVX(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 44, w[44]); + w[45] = ADD4_32(WSIGMA1_AVX(w[43]), w[29], w[38], WSIGMA0_AVX(w[30])); + SHA256ROUND_AVX(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 45, w[45]); + w[46] = ADD4_32(WSIGMA1_AVX(w[44]), w[30], w[39], WSIGMA0_AVX(w[31])); + SHA256ROUND_AVX(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 46, w[46]); + w[47] = ADD4_32(WSIGMA1_AVX(w[45]), w[31], w[40], WSIGMA0_AVX(w[32])); + SHA256ROUND_AVX(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 47, w[47]); + w[48] = ADD4_32(WSIGMA1_AVX(w[46]), w[32], w[41], WSIGMA0_AVX(w[33])); + SHA256ROUND_AVX(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 48, w[48]); + w[49] = ADD4_32(WSIGMA1_AVX(w[47]), w[33], w[42], WSIGMA0_AVX(w[34])); + SHA256ROUND_AVX(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 49, w[49]); + w[50] = ADD4_32(WSIGMA1_AVX(w[48]), w[34], w[43], WSIGMA0_AVX(w[35])); + SHA256ROUND_AVX(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 50, w[50]); + w[51] = ADD4_32(WSIGMA1_AVX(w[49]), w[35], w[44], WSIGMA0_AVX(w[36])); + SHA256ROUND_AVX(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 51, w[51]); + w[52] = ADD4_32(WSIGMA1_AVX(w[50]), w[36], w[45], WSIGMA0_AVX(w[37])); + SHA256ROUND_AVX(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 52, w[52]); + w[53] = ADD4_32(WSIGMA1_AVX(w[51]), w[37], w[46], WSIGMA0_AVX(w[38])); + SHA256ROUND_AVX(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 53, w[53]); + w[54] = ADD4_32(WSIGMA1_AVX(w[52]), w[38], w[47], WSIGMA0_AVX(w[39])); + SHA256ROUND_AVX(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 54, w[54]); + w[55] = ADD4_32(WSIGMA1_AVX(w[53]), w[39], w[48], WSIGMA0_AVX(w[40])); + SHA256ROUND_AVX(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 55, w[55]); + w[56] = ADD4_32(WSIGMA1_AVX(w[54]), w[40], w[49], WSIGMA0_AVX(w[41])); + SHA256ROUND_AVX(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 56, w[56]); + w[57] = ADD4_32(WSIGMA1_AVX(w[55]), w[41], w[50], WSIGMA0_AVX(w[42])); + SHA256ROUND_AVX(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 57, w[57]); + w[58] = ADD4_32(WSIGMA1_AVX(w[56]), w[42], w[51], WSIGMA0_AVX(w[43])); + SHA256ROUND_AVX(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 58, w[58]); + w[59] = ADD4_32(WSIGMA1_AVX(w[57]), w[43], w[52], WSIGMA0_AVX(w[44])); + SHA256ROUND_AVX(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 59, w[59]); + w[60] = ADD4_32(WSIGMA1_AVX(w[58]), w[44], w[53], WSIGMA0_AVX(w[45])); + SHA256ROUND_AVX(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 60, w[60]); + w[61] = ADD4_32(WSIGMA1_AVX(w[59]), w[45], w[54], WSIGMA0_AVX(w[46])); + SHA256ROUND_AVX(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 61, w[61]); + w[62] = ADD4_32(WSIGMA1_AVX(w[60]), w[46], w[55], WSIGMA0_AVX(w[47])); + SHA256ROUND_AVX(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 62, w[62]); + w[63] = ADD4_32(WSIGMA1_AVX(w[61]), w[47], w[56], WSIGMA0_AVX(w[48])); + SHA256ROUND_AVX(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 63, w[63]); + + // Feed Forward + ctx->s[0] = ADD32(s[0], ctx->s[0]); + ctx->s[1] = ADD32(s[1], ctx->s[1]); + ctx->s[2] = ADD32(s[2], ctx->s[2]); + ctx->s[3] = ADD32(s[3], ctx->s[3]); + ctx->s[4] = ADD32(s[4], ctx->s[4]); + ctx->s[5] = ADD32(s[5], ctx->s[5]); + ctx->s[6] = ADD32(s[6], ctx->s[6]); + ctx->s[7] = ADD32(s[7], ctx->s[7]); +} diff --git a/src/crypto/pq/_s/sha2-avx2/sha256avx.h b/src/crypto/pq/_s/sha2-avx2/sha256avx.h new file mode 100644 index 0000000..e887063 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha256avx.h @@ -0,0 +1,96 @@ +#ifndef SHA256AVX_H +#define SHA256AVX_H +#include "immintrin.h" +#include + +static const unsigned int RC[] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; + +#define u32 uint32_t +#define u256 __m256i + +#define XOR _mm256_xor_si256 +#define OR _mm256_or_si256 +#define AND _mm256_and_si256 +#define ADD32 _mm256_add_epi32 +#define NOT(x) _mm256_xor_si256(x, _mm256_set_epi32(-1, -1, -1, -1, -1, -1, -1, -1)) + +#define LOAD(src) _mm256_loadu_si256((__m256i *)(src)) +#define STORE(dest,src) _mm256_storeu_si256((__m256i *)(dest),src) + +#define BYTESWAP(x) _mm256_shuffle_epi8(x, _mm256_set_epi8(0xc,0xd,0xe,0xf,0x8,0x9,0xa,0xb,0x4,0x5,0x6,0x7,0x0,0x1,0x2,0x3,0xc,0xd,0xe,0xf,0x8,0x9,0xa,0xb,0x4,0x5,0x6,0x7,0x0,0x1,0x2,0x3)) + +#define SHIFTR32(x, y) _mm256_srli_epi32(x, y) +#define SHIFTL32(x, y) _mm256_slli_epi32(x, y) + +#define ROTR32(x, y) OR(SHIFTR32(x, y), SHIFTL32(x, 32 - y)) +#define ROTL32(x, y) OR(SHIFTL32(x, y), SHIFTR32(x, 32 - y)) + +#define XOR3(a, b, c) XOR(XOR(a, b), c) + +#define ADD3_32(a, b, c) ADD32(ADD32(a, b), c) +#define ADD4_32(a, b, c, d) ADD32(ADD32(ADD32(a, b), c), d) +#define ADD5_32(a, b, c, d, e) ADD32(ADD32(ADD32(ADD32(a, b), c), d), e) + +#define MAJ_AVX(a, b, c) XOR3(AND(a, b), AND(a, c), AND(b, c)) +#define CH_AVX(a, b, c) XOR(AND(a, b), AND(NOT(a), c)) + +#define SIGMA1_AVX(x) XOR3(ROTR32(x, 6), ROTR32(x, 11), ROTR32(x, 25)) +#define SIGMA0_AVX(x) XOR3(ROTR32(x, 2), ROTR32(x, 13), ROTR32(x, 22)) + +#define WSIGMA1_AVX(x) XOR3(ROTR32(x, 17), ROTR32(x, 19), SHIFTR32(x, 10)) +#define WSIGMA0_AVX(x) XOR3(ROTR32(x, 7), ROTR32(x, 18), SHIFTR32(x, 3)) + +#define SHA256ROUND_AVX(a, b, c, d, e, f, g, h, rc, w) \ + T0 = ADD5_32(h, SIGMA1_AVX(e), CH_AVX(e, f, g), _mm256_set1_epi32(RC[rc]), w); \ + d = ADD32(d, T0); \ + T1 = ADD32(SIGMA0_AVX(a), MAJ_AVX(a, b, c)); \ + h = ADD32(T0, T1); + +typedef struct SHA256state { + u256 s[8]; + unsigned char msgblocks[8*64]; + int datalen; + unsigned long long msglen; +} sha256ctx; + + +void transpose(u256 s[8]); +void sha256_init8x(sha256ctx *ctx); +void sha256_final8x(sha256ctx *ctx, + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7); + +void sha256_transform8x(sha256ctx *ctx, + const unsigned char *data0, + const unsigned char *data1, + const unsigned char *data2, + const unsigned char *data3, + const unsigned char *data4, + const unsigned char *data5, + const unsigned char *data6, + const unsigned char *data7); + +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/sha256x8.c b/src/crypto/pq/_s/sha2-avx2/sha256x8.c new file mode 100644 index 0000000..6f4f965 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha256x8.c @@ -0,0 +1,201 @@ +#include + +#include "sha256x8.h" +#include "sha256avx.h" +#include "utils.h" + +static uint32_t load_bigendian_32(const uint8_t *x) { + return (uint32_t)(x[3]) | (((uint32_t)(x[2])) << 8) | + (((uint32_t)(x[1])) << 16) | (((uint32_t)(x[0])) << 24); +} + +// Performs sha256x8 on an initialized (and perhaps seeded) state. +static void _sha256x8( + sha256ctx *ctx, + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, unsigned long long inlen) { + unsigned long long i = 0; + while(inlen - i >= 64) { + sha256_transform8x(ctx, + in0 + i, + in1 + i, + in2 + i, + in3 + i, + in4 + i, + in5 + i, + in6 + i, + in7 + i + ); + i += 64; + ctx->msglen += 512; + } + + int bytes_to_copy = inlen - i; + memcpy(&ctx->msgblocks[64*0], in0 + i, bytes_to_copy); + memcpy(&ctx->msgblocks[64*1], in1 + i, bytes_to_copy); + memcpy(&ctx->msgblocks[64*2], in2 + i, bytes_to_copy); + memcpy(&ctx->msgblocks[64*3], in3 + i, bytes_to_copy); + memcpy(&ctx->msgblocks[64*4], in4 + i, bytes_to_copy); + memcpy(&ctx->msgblocks[64*5], in5 + i, bytes_to_copy); + memcpy(&ctx->msgblocks[64*6], in6 + i, bytes_to_copy); + memcpy(&ctx->msgblocks[64*7], in7 + i, bytes_to_copy); + ctx->datalen = bytes_to_copy; + + sha256_final8x(ctx, out0, out1, out2, out3, out4, out5, out6, out7); +} + +void sha256x8_seeded( + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *seed, + unsigned long long seedlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, unsigned long long inlen) { + uint32_t t; + + sha256ctx ctx; + + for (size_t i = 0; i < 8; i++) { + t = load_bigendian_32(seed + 4*i); + ctx.s[i] = _mm256_set_epi32(t, t, t, t, t, t, t, t); + } + + ctx.datalen = 0; + ctx.msglen = seedlen; + + _sha256x8(&ctx, out0, out1, out2, out3, out4, out5, out6, out7, + in0, in1, in2, in3, in4, in5, in6, in7, inlen); +} + +/* This provides a wrapper around the internals of 8x parallel SHA256 */ +void sha256x8(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, unsigned long long inlen) +{ + sha256ctx ctx; + sha256_init8x(&ctx); + + _sha256x8(&ctx, out0, out1, out2, out3, out4, out5, out6, out7, + in0, in1, in2, in3, in4, in5, in6, in7, inlen); +} + +/** + * Note that inlen should be sufficiently small that it still allows for + * an array to be allocated on the stack. Typically 'in' is merely a seed. + * Outputs outlen number of bytes + */ +void mgf1x8(unsigned char *outx8, unsigned long outlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, + unsigned long inlen) +{ + SPX_VLA(unsigned char, inbufx8, 8 * (inlen + 4)); + unsigned char outbufx8[8*SPX_SHA256_OUTPUT_BYTES]; + unsigned long i; + unsigned int j; + + memcpy(inbufx8 + 0*(inlen + 4), in0, inlen); + memcpy(inbufx8 + 1*(inlen + 4), in1, inlen); + memcpy(inbufx8 + 2*(inlen + 4), in2, inlen); + memcpy(inbufx8 + 3*(inlen + 4), in3, inlen); + memcpy(inbufx8 + 4*(inlen + 4), in4, inlen); + memcpy(inbufx8 + 5*(inlen + 4), in5, inlen); + memcpy(inbufx8 + 6*(inlen + 4), in6, inlen); + memcpy(inbufx8 + 7*(inlen + 4), in7, inlen); + + /* While we can fit in at least another full block of SHA256 output.. */ + for (i = 0; (i+1)*SPX_SHA256_OUTPUT_BYTES <= outlen; i++) { + for (j = 0; j < 8; j++) { + u32_to_bytes(inbufx8 + inlen + j*(inlen + 4), i); + } + + sha256x8(outx8 + 0*outlen, + outx8 + 1*outlen, + outx8 + 2*outlen, + outx8 + 3*outlen, + outx8 + 4*outlen, + outx8 + 5*outlen, + outx8 + 6*outlen, + outx8 + 7*outlen, + inbufx8 + 0*(inlen + 4), + inbufx8 + 1*(inlen + 4), + inbufx8 + 2*(inlen + 4), + inbufx8 + 3*(inlen + 4), + inbufx8 + 4*(inlen + 4), + inbufx8 + 5*(inlen + 4), + inbufx8 + 6*(inlen + 4), + inbufx8 + 7*(inlen + 4), inlen + 4); + outx8 += SPX_SHA256_OUTPUT_BYTES; + } + /* Until we cannot anymore, and we fill the remainder. */ + for (j = 0; j < 8; j++) { + u32_to_bytes(inbufx8 + inlen + j*(inlen + 4), i); + } + sha256x8(outbufx8 + 0*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 1*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 2*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 3*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 4*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 5*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 6*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 7*SPX_SHA256_OUTPUT_BYTES, + inbufx8 + 0*(inlen + 4), + inbufx8 + 1*(inlen + 4), + inbufx8 + 2*(inlen + 4), + inbufx8 + 3*(inlen + 4), + inbufx8 + 4*(inlen + 4), + inbufx8 + 5*(inlen + 4), + inbufx8 + 6*(inlen + 4), + inbufx8 + 7*(inlen + 4), inlen + 4); + + for (j = 0; j < 8; j++) { + memcpy(outx8 + j*outlen, + outbufx8 + j*SPX_SHA256_OUTPUT_BYTES, + outlen - i*SPX_SHA256_OUTPUT_BYTES); + } +} diff --git a/src/crypto/pq/_s/sha2-avx2/sha256x8.h b/src/crypto/pq/_s/sha2-avx2/sha256x8.h new file mode 100644 index 0000000..0060de5 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha256x8.h @@ -0,0 +1,69 @@ +#ifndef SPX_SHA256X8_H +#define SPX_SHA256X8_H + +#include "params.h" + +#define SPX_SHA256_BLOCK_BYTES 64 +#define SPX_SHA256_OUTPUT_BYTES 32 /* This does not necessarily equal SPX_N */ + +#if SPX_SHA256_OUTPUT_BYTES < SPX_N + #error Linking against SHA-256 with N larger than 32 bytes is not supported +#endif + +#define sha256x8_seeded SPX_NAMESPACE(sha256x8_seeded) +void sha256x8_seeded( + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *seed, + unsigned long long seedlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, unsigned long long inlen); + +/* This provides a wrapper around the internals of 8x parallel SHA256 */ +#define sha256x8 SPX_NAMESPACE(sha256x8) +void sha256x8(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, unsigned long long inlen); + +/** + * Note that inlen should be sufficiently small that it still allows for + * an array to be allocated on the stack. Typically 'in' is merely a seed. + * Outputs outlen number of bytes + */ +#define mgf1x8 SPX_NAMESPACE(mgf1x8) +void mgf1x8(unsigned char *outx8, unsigned long outlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, + unsigned long inlen); +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/sha2_offsets.h b/src/crypto/pq/_s/sha2-avx2/sha2_offsets.h new file mode 120000 index 0000000..ca63278 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha2_offsets.h @@ -0,0 +1 @@ +../ref/sha2_offsets.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/sha512x4.c b/src/crypto/pq/_s/sha2-avx2/sha512x4.c new file mode 100644 index 0000000..3eaa0f6 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha512x4.c @@ -0,0 +1,462 @@ +#include +#include +#include + +#define SPX_SHA512_OUTPUT_BYTES 64 /* In sha256.h, but we don't want to */ + /* pull in the entire thing */ +#include "sha512x4.h" +#include "utils.h" + +typedef uint64_t u64; +typedef __m256i u256; + +static void sha512_transform4x( + sha512ctx4x *ctx, + const unsigned char *d0, + const unsigned char *d1, + const unsigned char *d2, + const unsigned char *d3 +); + +#define BYTESWAP(x) _mm256_shuffle_epi8(x, _mm256_set_epi8(0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf,0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf,0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7)) +#define STORE(dest,src) _mm256_storeu_si256((__m256i *)(dest),src) + +// Transpose 4 vectors containing 64-bit values +// That is, it rearranges the array: +// A B C D +// E F G H +// I J K L +// M N O P +// into +// A E I M +// B F J N +// C G K O +// D H L P +// where each letter stands for 64 bits (and lsbits on the left) +static void transpose(u256 s[4]) { + u256 tmp[4]; + tmp[0] = _mm256_unpacklo_epi64(s[0], s[1]); + tmp[1] = _mm256_unpackhi_epi64(s[0], s[1]); + tmp[2] = _mm256_unpacklo_epi64(s[2], s[3]); + tmp[3] = _mm256_unpackhi_epi64(s[2], s[3]); + // tmp is in the order of + // A E C G + // B F D H + // I M K O + // J N L P + s[0] = _mm256_permute2x128_si256(tmp[0], tmp[2], 0x20); + s[1] = _mm256_permute2x128_si256(tmp[1], tmp[3], 0x20); + s[2] = _mm256_permute2x128_si256(tmp[0], tmp[2], 0x31); + s[3] = _mm256_permute2x128_si256(tmp[1], tmp[3], 0x31); +} + + +static void sha512_init4x(sha512ctx4x *ctx) { +#define SET4(x) _mm256_set_epi64x(x, x, x, x) + ctx->s[0] = SET4(0x6a09e667f3bcc908ULL); + ctx->s[1] = SET4(0xbb67ae8584caa73bULL); + ctx->s[2] = SET4(0x3c6ef372fe94f82bULL); + ctx->s[3] = SET4(0xa54ff53a5f1d36f1ULL); + ctx->s[4] = SET4(0x510e527fade682d1ULL); + ctx->s[5] = SET4(0x9b05688c2b3e6c1fULL); + ctx->s[6] = SET4(0x1f83d9abfb41bd6bULL); + ctx->s[7] = SET4(0x5be0cd19137e2179ULL); +#undef SET4 + + ctx->datalen = 0; + ctx->msglen = 0; +} + +#define XOR _mm256_xor_si256 +#define OR _mm256_or_si256 +#define AND _mm256_and_si256 +#define ADD64 _mm256_add_epi64 + +#define LOAD(src) _mm256_loadu_si256((__m256i *)(src)) + +#define SHIFTR64(x, y) _mm256_srli_epi64(x, y) +#define SHIFTL64(x, y) _mm256_slli_epi64(x, y) + +#define ROTR64(x, y) OR(SHIFTR64(x, y), SHIFTL64(x, 64 - y)) + +static u256 XOR3(u256 a, u256 b, u256 c) { + return XOR(XOR(a, b), c); +} + +#define ADD3_64(a, b, c) ADD64(ADD64(a, b), c) +#define ADD4_64(a, b, c, d) ADD64(ADD64(ADD64(a, b), c), d) +#define ADD5_64(a, b, c, d, e) ADD64(ADD64(ADD64(ADD64(a, b), c), d), e) + +static u256 MAJ_AVX(u256 a, u256 b, u256 c) { + return XOR(c, AND(XOR(a, c), XOR(b, c))); +} +static u256 CH_AVX(u256 a, u256 b, u256 c) { + return XOR(c, AND(a, XOR(b, c))); +} +static u256 SIGMA0_AVX(u256 x) { + return XOR3(ROTR64(x, 28), ROTR64(x, 34), ROTR64(x, 39)); +} +static u256 SIGMA1_AVX(u256 x) { + return XOR3(ROTR64(x, 14), ROTR64(x, 18), ROTR64(x, 41)); +} +static u256 GAMMA0_AVX(u256 x) { + return XOR3(ROTR64(x, 1), ROTR64(x, 8), SHIFTR64(x, 7)); +} +static u256 GAMMA1_AVX(u256 x) { + return XOR3(ROTR64(x, 19), ROTR64(x, 61), SHIFTR64(x, 6)); +} + +#define SHA512ROUND_AVX(a, b, c, d, e, f, g, h, rc, w) \ + T0 = ADD5_64(h, w, SIGMA1_AVX(e), CH_AVX(e, f, g), _mm256_set1_epi64x(RC[rc])); \ + T1 = ADD64(SIGMA0_AVX(a), MAJ_AVX(a, b, c)); \ + d = ADD64(d, T0); \ + h = ADD64(T0, T1); + +static const unsigned long long RC[80] = { + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, + 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, + 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, + 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, + 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, + 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, + 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, + 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, + 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, + 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, + 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, + 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, + 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, + 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, + 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL, +}; + +static void sha512_transform4x( + sha512ctx4x *ctx, + const unsigned char *d0, + const unsigned char *d1, + const unsigned char *d2, + const unsigned char *d3) { + u256 s0, s1, s2, s3, s4, s5, s6, s7, w[16], T0, T1, nw; + + // Load words and transform data correctly + w[0 ] = BYTESWAP(LOAD(d0 )); + w[0 + 4] = BYTESWAP(LOAD(d0 + 32)); + w[0 + 8] = BYTESWAP(LOAD(d0 + 64)); + w[0 + 12] = BYTESWAP(LOAD(d0 + 96)); + + w[1 ] = BYTESWAP(LOAD(d1 )); + w[1 + 4] = BYTESWAP(LOAD(d1 + 32)); + w[1 + 8] = BYTESWAP(LOAD(d1 + 64)); + w[1 + 12] = BYTESWAP(LOAD(d1 + 96)); + + w[2 ] = BYTESWAP(LOAD(d2 )); + w[2 + 4] = BYTESWAP(LOAD(d2 + 32)); + w[2 + 8] = BYTESWAP(LOAD(d2 + 64)); + w[2 + 12] = BYTESWAP(LOAD(d2 + 96)); + + w[3 ] = BYTESWAP(LOAD(d3 )); + w[3 + 4] = BYTESWAP(LOAD(d3 + 32)); + w[3 + 8] = BYTESWAP(LOAD(d3 + 64)); + w[3 + 12] = BYTESWAP(LOAD(d3 + 96)); + + transpose(w); + transpose(w + 4); + transpose(w + 8); + transpose(w + 12); + + // Initial State + s0 = ctx->s[0]; + s1 = ctx->s[1]; + s2 = ctx->s[2]; + s3 = ctx->s[3]; + s4 = ctx->s[4]; + s5 = ctx->s[5]; + s6 = ctx->s[6]; + s7 = ctx->s[7]; + + // The first 16 rounds (where the w inputs are directly from the data) + SHA512ROUND_AVX(s0, s1, s2, s3, s4, s5, s6, s7, 0, w[0]); + SHA512ROUND_AVX(s7, s0, s1, s2, s3, s4, s5, s6, 1, w[1]); + SHA512ROUND_AVX(s6, s7, s0, s1, s2, s3, s4, s5, 2, w[2]); + SHA512ROUND_AVX(s5, s6, s7, s0, s1, s2, s3, s4, 3, w[3]); + SHA512ROUND_AVX(s4, s5, s6, s7, s0, s1, s2, s3, 4, w[4]); + SHA512ROUND_AVX(s3, s4, s5, s6, s7, s0, s1, s2, 5, w[5]); + SHA512ROUND_AVX(s2, s3, s4, s5, s6, s7, s0, s1, 6, w[6]); + SHA512ROUND_AVX(s1, s2, s3, s4, s5, s6, s7, s0, 7, w[7]); + SHA512ROUND_AVX(s0, s1, s2, s3, s4, s5, s6, s7, 8, w[8]); + SHA512ROUND_AVX(s7, s0, s1, s2, s3, s4, s5, s6, 9, w[9]); + SHA512ROUND_AVX(s6, s7, s0, s1, s2, s3, s4, s5, 10, w[10]); + SHA512ROUND_AVX(s5, s6, s7, s0, s1, s2, s3, s4, 11, w[11]); + SHA512ROUND_AVX(s4, s5, s6, s7, s0, s1, s2, s3, 12, w[12]); + SHA512ROUND_AVX(s3, s4, s5, s6, s7, s0, s1, s2, 13, w[13]); + SHA512ROUND_AVX(s2, s3, s4, s5, s6, s7, s0, s1, 14, w[14]); + SHA512ROUND_AVX(s1, s2, s3, s4, s5, s6, s7, s0, 15, w[15]); + +#define M(i) (((i)+16) & 0xf) +#define NextW(i) \ + w[M(i)] = ADD4_64(GAMMA1_AVX(w[M((i)-2)]), w[M((i)-7)], GAMMA0_AVX(w[M((i)-15)]), w[M((i)-16)]); + + // The remaining 64 rounds (where the w inputs are a linear fix of the data) + for (unsigned i = 16; i<80; i+=16) { + nw = NextW(0); + SHA512ROUND_AVX(s0, s1, s2, s3, s4, s5, s6, s7, i+0, nw); + nw = NextW(1); + SHA512ROUND_AVX(s7, s0, s1, s2, s3, s4, s5, s6, i+1, nw); + nw = NextW(2); + SHA512ROUND_AVX(s6, s7, s0, s1, s2, s3, s4, s5, i+2, nw); + nw = NextW(3); + SHA512ROUND_AVX(s5, s6, s7, s0, s1, s2, s3, s4, i+3, nw); + nw = NextW(4); + SHA512ROUND_AVX(s4, s5, s6, s7, s0, s1, s2, s3, i+4, nw); + nw = NextW(5); + SHA512ROUND_AVX(s3, s4, s5, s6, s7, s0, s1, s2, i+5, nw); + nw = NextW(6); + SHA512ROUND_AVX(s2, s3, s4, s5, s6, s7, s0, s1, i+6, nw); + nw = NextW(7); + SHA512ROUND_AVX(s1, s2, s3, s4, s5, s6, s7, s0, i+7, nw); + nw = NextW(8); + SHA512ROUND_AVX(s0, s1, s2, s3, s4, s5, s6, s7, i+8, nw); + nw = NextW(9); + SHA512ROUND_AVX(s7, s0, s1, s2, s3, s4, s5, s6, i+9, nw); + nw = NextW(10); + SHA512ROUND_AVX(s6, s7, s0, s1, s2, s3, s4, s5, i+10, nw); + nw = NextW(11); + SHA512ROUND_AVX(s5, s6, s7, s0, s1, s2, s3, s4, i+11, nw); + nw = NextW(12); + SHA512ROUND_AVX(s4, s5, s6, s7, s0, s1, s2, s3, i+12, nw); + nw = NextW(13); + SHA512ROUND_AVX(s3, s4, s5, s6, s7, s0, s1, s2, i+13, nw); + nw = NextW(14); + SHA512ROUND_AVX(s2, s3, s4, s5, s6, s7, s0, s1, i+14, nw); + nw = NextW(15); + SHA512ROUND_AVX(s1, s2, s3, s4, s5, s6, s7, s0, i+15, nw); + } + + // Feed Forward + ctx->s[0] = ADD64(s0, ctx->s[0]); + ctx->s[1] = ADD64(s1, ctx->s[1]); + ctx->s[2] = ADD64(s2, ctx->s[2]); + ctx->s[3] = ADD64(s3, ctx->s[3]); + ctx->s[4] = ADD64(s4, ctx->s[4]); + ctx->s[5] = ADD64(s5, ctx->s[5]); + ctx->s[6] = ADD64(s6, ctx->s[6]); + ctx->s[7] = ADD64(s7, ctx->s[7]); +} + +static void _sha512x4( + sha512ctx4x* ctx, + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + unsigned long long inlen) { + unsigned int i = 0; + + while(inlen - i >= 128) { + sha512_transform4x( + ctx, + in0 + i, + in1 + i, + in2 + i, + in3 + i + ); + ctx->msglen += 1024; + i += 128; + } + + ctx->datalen = inlen - i; + memcpy(&ctx->msgblocks[128*0], in0 + i, ctx->datalen); + memcpy(&ctx->msgblocks[128*1], in1 + i, ctx->datalen); + memcpy(&ctx->msgblocks[128*2], in2 + i, ctx->datalen); + memcpy(&ctx->msgblocks[128*3], in3 + i, ctx->datalen); + + // Padding + unsigned long curlen; + if (ctx->datalen < 112) { + for (i = 0; i < 4; ++i) { + curlen = ctx->datalen; + ctx->msgblocks[128*i + curlen++] = 0x80; + while(curlen < 128) { + ctx->msgblocks[128*i + curlen++] = 0x00; + } + } + } else { + for (i = 0; i < 4; ++i) { + curlen = ctx->datalen; + ctx->msgblocks[128*i + curlen++] = 0x80; + while(curlen < 128) { + ctx->msgblocks[128*i + curlen++] = 0x00; + } + } + sha512_transform4x( + ctx, + ctx->msgblocks, + ctx->msgblocks + 128, + ctx->msgblocks + 256, + ctx->msgblocks + 384 + ); + memset(ctx->msgblocks, 0, 4 * 128); + } + + // Add length of the message to each block + ctx->msglen += ctx->datalen * 8; + for (i = 0; i < 4; i++) { + ctx->msgblocks[128*i + 127] = ctx->msglen; + ctx->msgblocks[128*i + 126] = ctx->msglen >> 8; + ctx->msgblocks[128*i + 125] = ctx->msglen >> 16; + ctx->msgblocks[128*i + 124] = ctx->msglen >> 24; + ctx->msgblocks[128*i + 123] = ctx->msglen >> 32; + ctx->msgblocks[128*i + 122] = ctx->msglen >> 40; + ctx->msgblocks[128*i + 121] = ctx->msglen >> 48; + ctx->msgblocks[128*i + 120] = ctx->msglen >> 56; + memset( &ctx->msgblocks[128*i + 112], 0, 8 ); + } + sha512_transform4x( + ctx, + ctx->msgblocks, + ctx->msgblocks + 128, + ctx->msgblocks + 256, + ctx->msgblocks + 384 + ); + + // Compute final hash output + transpose(ctx->s); + transpose(ctx->s+4); + + // Store Hash value + __m256i out[2]; + STORE(out, BYTESWAP(ctx->s[0])); + STORE(out+1, BYTESWAP(ctx->s[4])); + memcpy(out0, out, 64); + + STORE(out, BYTESWAP(ctx->s[1])); + STORE(out+1, BYTESWAP(ctx->s[5])); + memcpy(out1, out, 64); + + STORE(out, BYTESWAP(ctx->s[2])); + STORE(out+1, BYTESWAP(ctx->s[6])); + memcpy(out2, out, 64); + + STORE(out, BYTESWAP(ctx->s[3])); + STORE(out+1, BYTESWAP(ctx->s[7])); + memcpy(out3, out, 64); +} + + +/** + * Note that inlen should be sufficiently small that it still allows for + * an array to be allocated on the stack. Typically 'in' is merely a seed. + * Outputs outlen number of bytes + */ +void mgf1x4_512(unsigned char *outx4, unsigned long outlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + unsigned long inlen) +{ + SPX_VLA(unsigned char, inbufx4, 4*(inlen + 4)); + unsigned char outbuf[4*64]; + unsigned long i; + unsigned int j; + + memcpy(inbufx4 + 0*(inlen + 4), in0, inlen); + memcpy(inbufx4 + 1*(inlen + 4), in1, inlen); + memcpy(inbufx4 + 2*(inlen + 4), in2, inlen); + memcpy(inbufx4 + 3*(inlen + 4), in3, inlen); + + /* While we can fit in at least another full block of SHA512 output.. */ + unsigned long remaining = outlen; + for (i = 0; remaining > 0; i++) { + unsigned this_step = SPX_SHA512_OUTPUT_BYTES; + if (this_step > remaining) this_step = remaining; + remaining -= this_step; + for (j = 0; j < 4; j++) { + u32_to_bytes(inbufx4 + inlen + j*(inlen + 4), i); + } + + sha512ctx4x ctx; + sha512_init4x(&ctx); + + _sha512x4( + &ctx, + outbuf + 0*64, + outbuf + 1*64, + outbuf + 2*64, + outbuf + 3*64, + inbufx4 + 0*(inlen + 4), + inbufx4 + 1*(inlen + 4), + inbufx4 + 2*(inlen + 4), + inbufx4 + 3*(inlen + 4), + inlen+4 + ); + + memcpy(outx4 + 0*outlen, outbuf+0*64, this_step); + memcpy(outx4 + 1*outlen, outbuf+1*64, this_step); + memcpy(outx4 + 2*outlen, outbuf+2*64, this_step); + memcpy(outx4 + 3*outlen, outbuf+3*64, this_step); + outx4 += this_step; + } +} + +void sha512x4_seeded( + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const unsigned char *seed, + unsigned long long seedlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + unsigned long long inlen) { + sha512ctx4x ctx; + unsigned long i; + + for (i = 0; i < 8; i++) { + uint64_t t = (uint64_t)(seed[7]) | (((uint64_t)(seed[6])) << 8) | + (((uint64_t)(seed[5])) << 16) | (((uint64_t)(seed[4])) << 24) | + (((uint64_t)(seed[3])) << 32) | (((uint64_t)(seed[2])) << 40) | + (((uint64_t)(seed[1])) << 48) | (((uint64_t)(seed[0])) << 56); + ctx.s[i] = _mm256_set_epi64x(t, t, t, t); + seed += 8; + } + + ctx.msglen = seedlen; + _sha512x4( + &ctx, + out0, out1, out2, out3, + in0, in1, in2, in3, + inlen + ); +} diff --git a/src/crypto/pq/_s/sha2-avx2/sha512x4.h b/src/crypto/pq/_s/sha2-avx2/sha512x4.h new file mode 100644 index 0000000..013459f --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sha512x4.h @@ -0,0 +1,44 @@ +#ifndef SHA512AVX_H +#define SHA512AVX_H +#include +#include "immintrin.h" + +#include "params.h" + +typedef struct SHA512state4x { + __m256i s[8]; + unsigned char msgblocks[4*128]; + int datalen; + unsigned long long msglen; +} sha512ctx4x; + + +#define sha512x4_seeded SPX_NAMESPACE(sha512x4_seeded) +void sha512x4_seeded( + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const unsigned char *seed, + unsigned long long seedlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + unsigned long long inlen); + + +/** + * Note that inlen should be sufficiently small that it still allows for + * an array to be allocated on the stack. Typically 'in' is merely a seed. + * Outputs outlen number of bytes + */ +#define mgf1x4_512 SPX_NAMESPACE(mgf1x4_512) +void mgf1x4_512(unsigned char *outx4, unsigned long outlen, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + unsigned long inlen); + +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/sign.c b/src/crypto/pq/_s/sha2-avx2/sign.c new file mode 120000 index 0000000..42fea88 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/sign.c @@ -0,0 +1 @@ +../ref/sign.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/test/benchmark.c b/src/crypto/pq/_s/sha2-avx2/test/benchmark.c new file mode 100644 index 0000000..a65bd62 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/test/benchmark.c @@ -0,0 +1,162 @@ +#define _POSIX_C_SOURCE 199309L + +#include +#include +#include + +#include "../api.h" +#include "../fors.h" +#include "../wots.h" +#include "../wotsx8.h" +#include "../params.h" +#include "../randombytes.h" + +#define SPX_MLEN 32 +#define NTESTS 10 + +static void wots_gen_pkx8(unsigned char *pk, const spx_ctx *ctx, + uint32_t addr[8]); + +static int cmp_llu(const void *a, const void*b) +{ + if(*(unsigned long long *)a < *(unsigned long long *)b) return -1; + if(*(unsigned long long *)a > *(unsigned long long *)b) return 1; + return 0; +} + +static unsigned long long median(unsigned long long *l, size_t llen) +{ + qsort(l,llen,sizeof(unsigned long long),cmp_llu); + + if(llen%2) return l[llen/2]; + else return (l[llen/2-1]+l[llen/2])/2; +} + +static void delta(unsigned long long *l, size_t llen) +{ + unsigned int i; + for(i = 0; i < llen - 1; i++) { + l[i] = l[i+1] - l[i]; + } +} + +static unsigned long long cpucycles(void) +{ + unsigned long long result; + __asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" + : "=a" (result) :: "%rdx"); + return result; +} + +static void printfcomma (unsigned long long n) +{ + if (n < 1000) { + printf("%llu", n); + return; + } + printfcomma(n / 1000); + printf (",%03llu", n % 1000); +} + +static void printfalignedcomma (unsigned long long n, int len) +{ + unsigned long long ncopy = n; + int i = 0; + + while (ncopy > 9) { + len -= 1; + ncopy /= 10; + i += 1; // to account for commas + } + i = i/3 - 1; // to account for commas + for (; i < len; i++) { + printf(" "); + } + printfcomma(n); +} + +static void display_result(double result, unsigned long long *l, size_t llen, unsigned long long mul) +{ + unsigned long long med; + + result /= NTESTS; + delta(l, NTESTS + 1); + med = median(l, llen); + printf("avg. %11.2lf us (%2.2lf sec); median ", result, result / 1e6); + printfalignedcomma(med, 12); + printf(" cycles, %5llux: ", mul); + printfalignedcomma(mul*med, 12); + printf(" cycles\n"); +} + +#define MEASURE(TEXT, MUL, FNCALL)\ + printf(TEXT);\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);\ + for(i = 0; i < NTESTS; i++) {\ + t[i] = cpucycles();\ + FNCALL;\ + }\ + t[NTESTS] = cpucycles();\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);\ + result = (stop.tv_sec - start.tv_sec) * 1e6 + (stop.tv_nsec - start.tv_nsec) / 1e3;\ + display_result(result, t, NTESTS, MUL); + +int main(void) +{ + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + spx_ctx ctx; + unsigned char pk[SPX_PK_BYTES]; + unsigned char sk[SPX_SK_BYTES]; + unsigned char *m = malloc(SPX_MLEN); + unsigned char *sm = malloc(SPX_BYTES + SPX_MLEN); + unsigned char *mout = malloc(SPX_BYTES + SPX_MLEN); + + unsigned char fors_pk[SPX_FORS_PK_BYTES]; + unsigned char fors_m[SPX_FORS_MSG_BYTES]; + unsigned char fors_sig[SPX_FORS_BYTES]; + unsigned char addr[SPX_ADDR_BYTES]; + unsigned char wots_pk[8*SPX_WOTS_PK_BYTES]; + + unsigned long long smlen; + unsigned long long mlen; + unsigned long long t[NTESTS+1]; + struct timespec start, stop; + double result; + int i; + + randombytes(m, SPX_MLEN); + randombytes(addr, SPX_ADDR_BYTES); + + printf("Parameters: n = %d, h = %d, d = %d, b = %d, k = %d, w = %d\n", + SPX_N, SPX_FULL_HEIGHT, SPX_D, SPX_FORS_HEIGHT, SPX_FORS_TREES, + SPX_WOTS_W); + + printf("Running %d iterations.\n", NTESTS); + + MEASURE("Generating keypair.. ", 1, crypto_sign_keypair(pk, sk)); + MEASURE(" - WOTS pk gen 8x.. ", (1 << SPX_TREE_HEIGHT) / 8, wots_gen_pkx8(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Signing.. ", 1, crypto_sign(sm, &smlen, m, SPX_MLEN, sk)); + MEASURE(" - FORS signing.. ", 1, fors_sign(fors_sig, fors_pk, fors_m, &ctx, (uint32_t *) addr)); + MEASURE(" - WOTS pk gen x8.. ", SPX_D * (1 << SPX_TREE_HEIGHT) / 8, wots_gen_pkx8(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Verifying.. ", 1, crypto_sign_open(mout, &mlen, sm, smlen, pk)); + + printf("Signature size: %d (%.2f KiB)\n", SPX_BYTES, SPX_BYTES / 1024.0); + printf("Public key size: %d (%.2f KiB)\n", SPX_PK_BYTES, SPX_PK_BYTES / 1024.0); + printf("Secret key size: %d (%.2f KiB)\n", SPX_SK_BYTES, SPX_SK_BYTES / 1024.0); + + free(m); + free(sm); + free(mout); + + return 0; +} + +static void wots_gen_pkx8(unsigned char *pk, const spx_ctx *ctx, + uint32_t addr[8]) { + struct leaf_info_x8 leaf; + unsigned steps[ SPX_WOTS_LEN ] = { 0 }; + INITIALIZE_LEAF_INFO_X8(leaf, addr, steps); + wots_gen_leafx8(pk, ctx, 0, &leaf); +} diff --git a/src/crypto/pq/_s/sha2-avx2/test/fors.c b/src/crypto/pq/_s/sha2-avx2/test/fors.c new file mode 120000 index 0000000..b2bccee --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/test/fors.c @@ -0,0 +1 @@ +../../ref/test/fors.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/test/spx.c b/src/crypto/pq/_s/sha2-avx2/test/spx.c new file mode 120000 index 0000000..7af26df --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/test/spx.c @@ -0,0 +1 @@ +../../ref/test/spx.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/test/thashx8.c b/src/crypto/pq/_s/sha2-avx2/test/thashx8.c new file mode 100644 index 0000000..37d27c3 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/test/thashx8.c @@ -0,0 +1,94 @@ +#include +#include + +#include "../thashx8.h" +#include "../thash.h" +#include "../randombytes.h" +#include "../params.h" +#include "../hash.h" + +#if SPX_SHA512 +#include "../sha2.h" +#include "../sha512x4.h" +#endif + + +int main(void) +{ + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + unsigned char input[16*SPX_N]; + spx_ctx ctx; + unsigned char output[8*SPX_N]; + unsigned char out8[8*SPX_N]; + uint32_t addr[8*8] = {0}; + unsigned int j; + + randombytes(ctx.pub_seed, SPX_N); + randombytes(input, 16*SPX_N); + randombytes((unsigned char *)addr, 8 * 8 * sizeof(uint32_t)); + + initialize_hash_function(&ctx); + + printf("Testing if thash matches thashx8 on one block ... "); + + for (j = 0; j < 8; j++) { + thash(out8 + j * SPX_N, input + j * SPX_N, 1, &ctx, addr + j*8); + } + + thashx8(output + 0*SPX_N, + output + 1*SPX_N, + output + 2*SPX_N, + output + 3*SPX_N, + output + 4*SPX_N, + output + 5*SPX_N, + output + 6*SPX_N, + output + 7*SPX_N, + input + 0*SPX_N, + input + 1*SPX_N, + input + 2*SPX_N, + input + 3*SPX_N, + input + 4*SPX_N, + input + 5*SPX_N, + input + 6*SPX_N, + input + 7*SPX_N, + 1, &ctx, addr); + + if (memcmp(out8, output, 8 * SPX_N)) { + printf("failed!\n"); + return -1; + } + printf("successful.\n"); + + printf("Testing if thash matches thashx8 on two blocks ... "); + + for (j = 0; j < 8; j++) { + thash(out8 + j * SPX_N, input + (2*j) * SPX_N, 2, &ctx, addr + j*8); + } + + thashx8(output + 0*SPX_N, + output + 1*SPX_N, + output + 2*SPX_N, + output + 3*SPX_N, + output + 4*SPX_N, + output + 5*SPX_N, + output + 6*SPX_N, + output + 7*SPX_N, + input + 0*SPX_N, + input + 2*SPX_N, + input + 4*SPX_N, + input + 6*SPX_N, + input + 8*SPX_N, + input + 10*SPX_N, + input + 12*SPX_N, + input + 14*SPX_N, + 2, &ctx, addr); + + if (memcmp(out8, output, 8 * SPX_N)) { + printf("failed!\n"); + return -1; + } + printf("successful.\n"); + return 0; +} diff --git a/src/crypto/pq/_s/sha2-avx2/thash.h b/src/crypto/pq/_s/sha2-avx2/thash.h new file mode 120000 index 0000000..937dd48 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/thash.h @@ -0,0 +1 @@ +../ref/thash.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/thash_sha2_robust.c b/src/crypto/pq/_s/sha2-avx2/thash_sha2_robust.c new file mode 120000 index 0000000..60e5139 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/thash_sha2_robust.c @@ -0,0 +1 @@ +../ref/thash_sha2_robust.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/thash_sha2_robustx8.c b/src/crypto/pq/_s/sha2-avx2/thash_sha2_robustx8.c new file mode 100644 index 0000000..8700d90 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/thash_sha2_robustx8.c @@ -0,0 +1,276 @@ +#include +#include + +#include "address.h" +#include "utils.h" +#include "params.h" +#include "thashx8.h" +#include "sha2.h" +#include "sha256x8.h" +#include "sha256avx.h" + +#if SPX_SHA512 +#include "sha512x4.h" + +static void thashx8_512( + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, + unsigned int inblocks, + const spx_ctx *ctx, + uint32_t addrx8[8*8] +); +#endif + +/** + * 8-way parallel version of thash; takes 8x as much input and output + */ +void thashx8(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx8[8*8]) +{ +#if SPX_SHA512 + if (inblocks > 1) { + thashx8_512( + out0, out1, out2, out3, out4, out5, out6, out7, + in0, in1, in2, in3, in4, in5, in6, in7, + inblocks, ctx, addrx8); + return; + } +#endif + SPX_VLA(unsigned char, bufx8, 8 * (SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)); + SPX_VLA(unsigned char, outbufx8, 8 * SPX_SHA256_OUTPUT_BYTES); + SPX_VLA(unsigned char, bitmaskx8, 8 * (inblocks * SPX_N)); + unsigned int i; + + for (i = 0; i < 8; i++) { + memcpy(bufx8 + i*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + ctx->pub_seed, SPX_N); + memcpy(bufx8 + SPX_N + + i*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + addrx8 + i*8, SPX_SHA256_ADDR_BYTES); + } + + mgf1x8(bitmaskx8, inblocks * SPX_N, + bufx8 + 0*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 1*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 2*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 3*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 4*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 5*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 6*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 7*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_N + SPX_SHA256_ADDR_BYTES); + + for (i = 0; i < inblocks * SPX_N; i++) { + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 0*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in0[i] ^ bitmaskx8[i + 0*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 1*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in1[i] ^ bitmaskx8[i + 1*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 2*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in2[i] ^ bitmaskx8[i + 2*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 3*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in3[i] ^ bitmaskx8[i + 3*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 4*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in4[i] ^ bitmaskx8[i + 4*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 5*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in5[i] ^ bitmaskx8[i + 5*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 6*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in6[i] ^ bitmaskx8[i + 6*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 7*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in7[i] ^ bitmaskx8[i + 7*(inblocks * SPX_N)]; + } + + sha256x8_seeded( + /* out */ + outbufx8 + 0*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 1*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 2*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 3*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 4*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 5*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 6*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 7*SPX_SHA256_OUTPUT_BYTES, + + /* seed */ + ctx->state_seeded, 512, + + /* in */ + bufx8 + SPX_N + 0*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 1*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 2*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 3*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 4*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 5*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 6*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 7*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N /* len */ + ); + + memcpy(out0, outbufx8 + 0*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out1, outbufx8 + 1*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out2, outbufx8 + 2*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out3, outbufx8 + 3*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out4, outbufx8 + 4*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out5, outbufx8 + 5*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out6, outbufx8 + 6*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out7, outbufx8 + 7*SPX_SHA256_OUTPUT_BYTES, SPX_N); +} + +#if SPX_SHA512 +/** + * 2x4-way parallel version of thash; this is for the uses of thash that are + * based on SHA-512 + */ +static void thashx8_512( + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, + unsigned int inblocks, + const spx_ctx *ctx, + uint32_t addrx8[8*8]) +{ + SPX_VLA(unsigned char, bufx8, 8 * (SPX_N + SPX_SHA256_ADDR_BYTES + inblocks * SPX_N)); + SPX_VLA(unsigned char, outbuf, 4 * SPX_SHA512_OUTPUT_BYTES); + SPX_VLA(unsigned char, bitmaskx4, 4 * (inblocks * SPX_N)); + unsigned int i; + + for (i = 0; i < 8; i++) { + memcpy(bufx8 + i*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + ctx->pub_seed, SPX_N); + memcpy(bufx8 + SPX_N + + i*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + addrx8 + i*8, SPX_SHA256_ADDR_BYTES); + } + + mgf1x4_512(bitmaskx4, inblocks * SPX_N, + bufx8 + 0*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 1*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 2*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 3*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_N + SPX_SHA256_ADDR_BYTES); + + for (i = 0; i < inblocks * SPX_N; i++) { + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 0*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in0[i] ^ bitmaskx4[i + 0*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 1*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in1[i] ^ bitmaskx4[i + 1*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 2*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in2[i] ^ bitmaskx4[i + 2*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 3*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in3[i] ^ bitmaskx4[i + 3*(inblocks * SPX_N)]; + } + + mgf1x4_512(bitmaskx4, inblocks * SPX_N, + bufx8 + 4*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 5*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 6*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 7*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_N + SPX_SHA256_ADDR_BYTES); + + for (i = 0; i < inblocks * SPX_N; i++) { + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 4*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in4[i] ^ bitmaskx4[i + 0*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 5*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in5[i] ^ bitmaskx4[i + 1*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 6*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in6[i] ^ bitmaskx4[i + 2*(inblocks * SPX_N)]; + bufx8[SPX_N + SPX_SHA256_ADDR_BYTES + i + + 7*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)] = + in7[i] ^ bitmaskx4[i + 3*(inblocks * SPX_N)]; + } + + sha512x4_seeded( + outbuf + 0*SPX_SHA512_OUTPUT_BYTES, + outbuf + 1*SPX_SHA512_OUTPUT_BYTES, + outbuf + 2*SPX_SHA512_OUTPUT_BYTES, + outbuf + 3*SPX_SHA512_OUTPUT_BYTES, + ctx->state_seeded_512, /* seed */ + 1024, /* seed length */ + bufx8 + SPX_N + 0*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 1*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 2*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 3*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N /* len */ + ); + + memcpy(out0, outbuf + 0*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out1, outbuf + 1*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out2, outbuf + 2*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out3, outbuf + 3*SPX_SHA512_OUTPUT_BYTES, SPX_N); + + sha512x4_seeded( + outbuf + 0*SPX_SHA512_OUTPUT_BYTES, + outbuf + 1*SPX_SHA512_OUTPUT_BYTES, + outbuf + 2*SPX_SHA512_OUTPUT_BYTES, + outbuf + 3*SPX_SHA512_OUTPUT_BYTES, + ctx->state_seeded_512, /* seed */ + 1024, /* seed length */ + bufx8 + SPX_N + 4*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 5*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 6*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + SPX_N + 7*(SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N /* len */ + ); + + memcpy(out4, outbuf + 0*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out5, outbuf + 1*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out6, outbuf + 2*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out7, outbuf + 3*SPX_SHA512_OUTPUT_BYTES, SPX_N); +} +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/thash_sha2_simple.c b/src/crypto/pq/_s/sha2-avx2/thash_sha2_simple.c new file mode 120000 index 0000000..c87305c --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/thash_sha2_simple.c @@ -0,0 +1 @@ +../ref/thash_sha2_simple.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/thash_sha2_simplex8.c b/src/crypto/pq/_s/sha2-avx2/thash_sha2_simplex8.c new file mode 100644 index 0000000..2b40d02 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/thash_sha2_simplex8.c @@ -0,0 +1,220 @@ +#include +#include + +#include "address.h" +#include "utils.h" +#include "params.h" +#include "thashx8.h" +#include "sha2.h" +#include "sha256x8.h" +#include "sha256avx.h" + +#if SPX_SHA512 +#include "sha512x4.h" + +static void thashx8_512( + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, + unsigned int inblocks, + const spx_ctx *ctx, + uint32_t addrx8[8*8] +); +#endif + +/** + * 8-way parallel version of thash; takes 8x as much input and output + */ +void thashx8(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx8[8*8]) +{ +#if SPX_SHA512 + if (inblocks > 1) { + thashx8_512( + out0, out1, out2, out3, out4, out5, out6, out7, + in0, in1, in2, in3, in4, in5, in6, in7, + inblocks, ctx, addrx8); + return; + } +#endif + unsigned char bufx8[8*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)]; + unsigned char outbufx8[8*SPX_SHA256_OUTPUT_BYTES]; + unsigned int i; + + for (i = 0; i < 8; i++) { + memcpy(bufx8 + i*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + addrx8 + i*8, SPX_SHA256_ADDR_BYTES); + } + + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 0*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in0, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 1*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in1, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 2*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in2, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 3*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in3, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 4*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in4, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 5*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in5, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 6*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in6, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 7*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in7, inblocks * SPX_N); + + sha256x8_seeded( + /* out */ + outbufx8 + 0*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 1*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 2*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 3*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 4*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 5*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 6*SPX_SHA256_OUTPUT_BYTES, + outbufx8 + 7*SPX_SHA256_OUTPUT_BYTES, + + /* seed */ + ctx->state_seeded, 512, + + /* in */ + bufx8 + 0*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 1*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 2*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 3*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 4*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 5*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 6*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 7*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N /* len */ + ); + + memcpy(out0, outbufx8 + 0*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out1, outbufx8 + 1*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out2, outbufx8 + 2*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out3, outbufx8 + 3*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out4, outbufx8 + 4*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out5, outbufx8 + 5*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out6, outbufx8 + 6*SPX_SHA256_OUTPUT_BYTES, SPX_N); + memcpy(out7, outbufx8 + 7*SPX_SHA256_OUTPUT_BYTES, SPX_N); +} + +#if SPX_SHA512 +/** + * 2x4-way parallel version of thash; this is for the uses of thash that are + * based on SHA-512 + */ +static void thashx8_512( + unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, + unsigned int inblocks, + const spx_ctx *ctx, + uint32_t addrx8[8*8]) +{ + unsigned char bufx8[8*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N)]; + unsigned char outbuf[4*SPX_SHA512_OUTPUT_BYTES]; + unsigned int i; + + for (i = 0; i < 8; i++) { + memcpy(bufx8 + i*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + addrx8 + i*8, SPX_SHA256_ADDR_BYTES); + } + + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 0*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in0, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 1*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in1, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 2*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in2, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 3*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in3, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 4*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in4, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 5*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in5, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 6*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in6, inblocks * SPX_N); + memcpy(bufx8 + SPX_SHA256_ADDR_BYTES + + 7*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), in7, inblocks * SPX_N); + + sha512x4_seeded( + outbuf + 0*SPX_SHA512_OUTPUT_BYTES, + outbuf + 1*SPX_SHA512_OUTPUT_BYTES, + outbuf + 2*SPX_SHA512_OUTPUT_BYTES, + outbuf + 3*SPX_SHA512_OUTPUT_BYTES, + ctx->state_seeded_512, /* seed */ + 1024, /* seed length */ + bufx8 + 0*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), /* in */ + bufx8 + 1*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 2*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 3*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N /* len */ + ); + + memcpy(out0, outbuf + 0*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out1, outbuf + 1*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out2, outbuf + 2*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out3, outbuf + 3*SPX_SHA512_OUTPUT_BYTES, SPX_N); + + sha512x4_seeded( + outbuf + 0*SPX_SHA512_OUTPUT_BYTES, + outbuf + 1*SPX_SHA512_OUTPUT_BYTES, + outbuf + 2*SPX_SHA512_OUTPUT_BYTES, + outbuf + 3*SPX_SHA512_OUTPUT_BYTES, + ctx->state_seeded_512, /* seed */ + 1024, /* seed length */ + bufx8 + 4*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), /* in */ + bufx8 + 5*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 6*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + bufx8 + 7*(SPX_SHA256_ADDR_BYTES + inblocks*SPX_N), + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N /* len */ + ); + + memcpy(out4, outbuf + 0*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out5, outbuf + 1*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out6, outbuf + 2*SPX_SHA512_OUTPUT_BYTES, SPX_N); + memcpy(out7, outbuf + 3*SPX_SHA512_OUTPUT_BYTES, SPX_N); +} +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/thashx8.h b/src/crypto/pq/_s/sha2-avx2/thashx8.h new file mode 100644 index 0000000..d6bf618 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/thashx8.h @@ -0,0 +1,27 @@ +#ifndef SPX_THASHX8_H +#define SPX_THASHX8_H + +#include +#include "context.h" +#include "params.h" + +#define thashx8 SPX_NAMESPACE(thashx8) +void thashx8(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + unsigned char *out4, + unsigned char *out5, + unsigned char *out6, + unsigned char *out7, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, + const unsigned char *in4, + const unsigned char *in5, + const unsigned char *in6, + const unsigned char *in7, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx8[8*8]); + +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/utils.c b/src/crypto/pq/_s/sha2-avx2/utils.c new file mode 120000 index 0000000..e8ef6eb --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/utils.c @@ -0,0 +1 @@ +../ref/utils.c \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/utils.h b/src/crypto/pq/_s/sha2-avx2/utils.h new file mode 120000 index 0000000..51b0d39 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/utils.h @@ -0,0 +1 @@ +../ref/utils.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/utilsx8.c b/src/crypto/pq/_s/sha2-avx2/utilsx8.c new file mode 100644 index 0000000..af21cf5 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/utilsx8.c @@ -0,0 +1,146 @@ +#include + +#include "utils.h" +#include "utilsx8.h" +#include "params.h" +#include "thashx8.h" +#include "address.h" + +/* + * Generate the entire Merkle tree, computing the authentication path for leaf_idx, + * and the resulting root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE) + * + * This expects tree_addrx8 to be initialized to 8 parallel addr structures for + * the Merkle tree nodes + * + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + * + * This works by using the standard Merkle tree building algorithm, except + * that each 'node' tracked is actually 8 consecutive nodes in the real tree. + * When we combine two logical nodes ABCDEFGH and STUVWXYZ, we perform the H + * operation on adjacent real nodes, forming the parent logical node + * (AB)(CD)(EF)(GH)(ST)(UV)(WX)(YZ) + * + * When we get to the top three levels of the real tree (where there is only + * one logical node), we continue this operation three more times; the right + * most real node will by the actual root (and the other 7 nodes will be + * garbage). We follow the same thashx8 logic so that the 'extract + * authentication path components' part of the loop is still executed (and + * to simplify the code somewhat) + * + * This currently assumes tree_height >= 3; I suspect that doing an adjusting + * idx, addr_idx on the gen_leafx8 call if tree_height < 3 would fix it; since + * we don't actually use such short trees, I haven't bothered + */ +void treehashx8(unsigned char *root, unsigned char *auth_path, + const spx_ctx *ctx, + uint32_t leaf_idx, uint32_t idx_offset, + uint32_t tree_height, + void (*gen_leafx8)( + unsigned char* /* Where to write the leaves */, + const spx_ctx*, + uint32_t idx, void *info), + uint32_t tree_addrx8[8*8], + void *info) +{ + /* This is where we keep the intermediate nodes */ + SPX_VLA(unsigned char, stackx8, 8 * tree_height * SPX_N); + uint32_t left_adj = 0, prev_left_adj = 0; /* When we're doing the top 3 */ + /* levels, the left-most part of the tree isn't at the beginning */ + /* of current[]. These give the offset of the actual start */ + + uint32_t idx; + uint32_t max_idx = (1 << (tree_height-3)) - 1; + for (idx = 0;; idx++) { + unsigned char current[8*SPX_N]; /* Current logical node */ + gen_leafx8( current, ctx, 8*idx + idx_offset, + info ); + + /* Now combine the freshly generated right node with previously */ + /* generated left ones */ + uint32_t internal_idx_offset = idx_offset; + uint32_t internal_idx = idx; + uint32_t internal_leaf = leaf_idx; + uint32_t h; /* The height we are in the Merkle tree */ + for (h=0;; h++, internal_idx >>= 1, internal_leaf >>= 1) { + + /* Special processing if we're at the top of the tree */ + if (h >= tree_height - 3) { + if (h == tree_height) { + /* We hit the root; return it */ + memcpy( root, ¤t[7*SPX_N], SPX_N ); + return; + } + /* The tree indexing logic is a bit off in this case */ + /* Adjust it so that the left-most node of the part of */ + /* the tree that we're processing has index 0 */ + prev_left_adj = left_adj; + left_adj = 8 - (1 << (tree_height - h - 1)); + } + + /* Check if we hit the top of the tree */ + if (h == tree_height) { + /* We hit the root; return it */ + memcpy( root, ¤t[7*SPX_N], SPX_N ); + return; + } + + /* + * Check if one of the nodes we have is a part of the + * authentication path; if it is, write it out + */ + if ((((internal_idx << 3) ^ internal_leaf) & ~0x7) == 0) { + memcpy( &auth_path[ h * SPX_N ], + ¤t[(((internal_leaf&7)^1) + prev_left_adj) * SPX_N], + SPX_N ); + } + + /* + * Check if we're at a left child; if so, stop going up the stack + * Exception: if we've reached the end of the tree, keep on going + * (so we combine the last 8 nodes into the one root node in three + * more iterations) + */ + if ((internal_idx & 1) == 0 && idx < max_idx) { + break; + } + + /* Ok, we're at a right node (or doing the top 3 levels) */ + /* Now combine the left and right logical nodes together */ + + /* Set the address of the node we're creating. */ + int j; + internal_idx_offset >>= 1; + for (j = 0; j < 8; j++) { + set_tree_height(tree_addrx8 + j*8, h + 1); + set_tree_index(tree_addrx8 + j*8, + (8/2) * (internal_idx&~1) + j - left_adj + internal_idx_offset ); + } + unsigned char *left = &stackx8[h * 8 * SPX_N]; + thashx8( ¤t[0 * SPX_N], + ¤t[1 * SPX_N], + ¤t[2 * SPX_N], + ¤t[3 * SPX_N], + ¤t[4 * SPX_N], + ¤t[5 * SPX_N], + ¤t[6 * SPX_N], + ¤t[7 * SPX_N], + &left [0 * SPX_N], + &left [2 * SPX_N], + &left [4 * SPX_N], + &left [6 * SPX_N], + ¤t[0 * SPX_N], + ¤t[2 * SPX_N], + ¤t[4 * SPX_N], + ¤t[6 * SPX_N], + 2, ctx, tree_addrx8); + } + + /* We've hit a left child; save the current for when we get the */ + /* corresponding right right */ + memcpy( &stackx8[h * 8 * SPX_N], current, 8 * SPX_N); + } +} diff --git a/src/crypto/pq/_s/sha2-avx2/utilsx8.h b/src/crypto/pq/_s/sha2-avx2/utilsx8.h new file mode 100644 index 0000000..ca42146 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/utilsx8.h @@ -0,0 +1,28 @@ +#ifndef SPX_UTILSX8_H +#define SPX_UTILSX8_H + +#include +#include "params.h" + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + * + * This implementation uses AVX to compute internal nodes 8 at a time (in + * parallel) + */ +#define treehashx8 SPX_NAMESPACE(treehashx8) +void treehashx8(unsigned char *root, unsigned char *auth_path, + const spx_ctx *ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leafx8)( + unsigned char* /* Where to write the leaves */, + const spx_ctx* /* ctx */, + uint32_t addr_idx, void *info), + uint32_t tree_addrx8[8*8], void *info); + +#endif diff --git a/src/crypto/pq/_s/sha2-avx2/wots.c b/src/crypto/pq/_s/sha2-avx2/wots.c new file mode 100644 index 0000000..779a868 --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/wots.c @@ -0,0 +1,293 @@ +#include +#include + +#include "utils.h" +#include "utilsx8.h" +#include "hash.h" +#include "hashx8.h" +#include "thash.h" +#include "thashx8.h" +#include "wots.h" +#include "wotsx8.h" +#include "address.h" +#include "params.h" + +// TODO clarify address expectations, and make them more uniform. +// TODO i.e. do we expect types to be set already? +// TODO and do we expect modifications or copies? + +/** + * Computes up the chains + */ +static void gen_chains( + unsigned char *out, + const unsigned char *in, + unsigned int start[SPX_WOTS_LEN], + unsigned int steps[SPX_WOTS_LEN], + const spx_ctx *ctx, + uint32_t addr[8]) +{ + uint32_t i, j, k, idx, watching; + int done; + unsigned char empty[SPX_N]; + unsigned char *bufs[8]; + uint32_t addrs[8*8]; + + int l; + uint16_t counts[SPX_WOTS_W] = { 0 }; + uint16_t idxs[SPX_WOTS_LEN]; + uint16_t total, newTotal; + + /* set addrs = {addr, addr, ..., addr} */ + for (j = 0; j < 8; j++) { + memcpy(addrs+j*8, addr, sizeof(uint32_t) * 8); + } + + /* Initialize out with the value at position 'start'. */ + memcpy(out, in, SPX_WOTS_LEN*SPX_N); + + /* Sort the chains in reverse order by steps using counting sort. */ + for (i = 0; i < SPX_WOTS_LEN; i++) { + counts[steps[i]]++; + } + total = 0; + for (l = SPX_WOTS_W - 1; l >= 0; l--) { + newTotal = counts[l] + total; + counts[l] = total; + total = newTotal; + } + for (i = 0; i < SPX_WOTS_LEN; i++) { + idxs[counts[steps[i]]] = i; + counts[steps[i]]++; + } + + /* We got our work cut out for us: do it! */ + for (i = 0; i < SPX_WOTS_LEN; i += 8) { + for (j = 0; j < 8 && i+j < SPX_WOTS_LEN; j++) { + idx = idxs[i+j]; + set_chain_addr(addrs+j*8, idx); + bufs[j] = out + SPX_N * idx; + } + + /* As the chains are sorted in reverse order, we know that the first + * chain is the longest and the last one is the shortest. We keep + * an eye on whether the last chain is done and then on the one before, + * et cetera. */ + watching = 7; + done = 0; + while (i + watching >= SPX_WOTS_LEN) { + bufs[watching] = &empty[0]; + watching--; + } + + for (k = 0;; k++) { + while (k == steps[idxs[i+watching]]) { + bufs[watching] = &empty[0]; + if (watching == 0) { + done = 1; + break; + } + watching--; + } + if (done) { + break; + } + for (j = 0; j < watching + 1; j++) { + set_hash_addr(addrs+j*8, k + start[idxs[i+j]]); + } + + thashx8(bufs[0], bufs[1], bufs[2], bufs[3], + bufs[4], bufs[5], bufs[6], bufs[7], + bufs[0], bufs[1], bufs[2], bufs[3], + bufs[4], bufs[5], bufs[6], bufs[7], 1, ctx, addrs); + } + } +} + +/** + * base_w algorithm as described in draft. + * Interprets an array of bytes as integers in base w. + * This only works when log_w is a divisor of 8. + */ +static void base_w(unsigned int *output, const int out_len, + const unsigned char *input) +{ + int in = 0; + int out = 0; + unsigned char total; + int bits = 0; + int consumed; + + for (consumed = 0; consumed < out_len; consumed++) { + if (bits == 0) { + total = input[in]; + in++; + bits += 8; + } + bits -= SPX_WOTS_LOGW; + output[out] = (total >> bits) & (SPX_WOTS_W - 1); + out++; + } +} + +/* Computes the WOTS+ checksum over a message (in base_w). */ +static void wots_checksum(unsigned int *csum_base_w, + const unsigned int *msg_base_w) +{ + unsigned int csum = 0; + unsigned char csum_bytes[(SPX_WOTS_LEN2 * SPX_WOTS_LOGW + 7) / 8]; + unsigned int i; + + /* Compute checksum. */ + for (i = 0; i < SPX_WOTS_LEN1; i++) { + csum += SPX_WOTS_W - 1 - msg_base_w[i]; + } + + /* Convert checksum to base_w. */ + /* Make sure expected empty zero bits are the least significant bits. */ + csum = csum << ((8 - ((SPX_WOTS_LEN2 * SPX_WOTS_LOGW) % 8)) % 8); + ull_to_bytes(csum_bytes, sizeof(csum_bytes), csum); + base_w(csum_base_w, SPX_WOTS_LEN2, csum_bytes); +} + +/* Takes a message and derives the matching chain lengths. */ +void chain_lengths(unsigned int *lengths, const unsigned char *msg) +{ + base_w(lengths, SPX_WOTS_LEN1, msg); + wots_checksum(lengths + SPX_WOTS_LEN1, lengths); +} + +/** + * Takes a WOTS signature and an n-byte message, computes a WOTS public key. + * + * Writes the computed public key to 'pk'. + */ +void wots_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *msg, + const spx_ctx *ctx, uint32_t addr[8]) +{ + unsigned int steps[SPX_WOTS_LEN]; + unsigned int start[SPX_WOTS_LEN]; + uint32_t i; + + chain_lengths(start, msg); + + for (i = 0; i < SPX_WOTS_LEN; i++) { + steps[i] = SPX_WOTS_W - 1 - start[i]; + } + + gen_chains(pk, sig, start, steps, ctx, addr); +} + +/* + * This generates 8 sequential WOTS public keys + * It also generates the WOTS signature if leaf_info indicates + * that we're signing with one of these WOTS keys + */ +void wots_gen_leafx8(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info) { + struct leaf_info_x8 *info = v_info; + uint32_t *leaf_addr = info->leaf_addr; + uint32_t *pk_addr = info->pk_addr; + unsigned int i, j, k; + unsigned char pk_buffer[ 8 * SPX_WOTS_BYTES ]; + unsigned wots_offset = SPX_WOTS_BYTES; + unsigned char *buffer; + uint32_t wots_k_mask; + unsigned wots_sign_index; + + if (((leaf_idx ^ info->wots_sign_leaf) & ~7) == 0) { + /* We're traversing the leaf that's signing; generate the WOTS */ + /* signature */ + wots_k_mask = 0; + wots_sign_index = info->wots_sign_leaf & 7; /* Which of of the 8 */ + /* slots do the signatures come from */ + } else { + /* Nope, we're just generating pk's; turn off the signature logic */ + wots_k_mask = ~0; + wots_sign_index = 0; + } + + for (j = 0; j < 8; j++) { + set_keypair_addr( leaf_addr + j*8, leaf_idx + j ); + set_keypair_addr( pk_addr + j*8, leaf_idx + j ); + } + + for (i = 0, buffer = pk_buffer; i < SPX_WOTS_LEN; i++, buffer += SPX_N) { + uint32_t wots_k = info->wots_steps[i] | wots_k_mask; /* Set wots_k */ + /* to the step if we're generating a signature, ~0 if we're not */ + + /* Start with the secret seed */ + for (j = 0; j < 8; j++) { + set_chain_addr(leaf_addr + j*8, i); + set_hash_addr(leaf_addr + j*8, 0); + set_type(leaf_addr + j*8, SPX_ADDR_TYPE_WOTSPRF); + } + prf_addrx8(buffer + 0*wots_offset, + buffer + 1*wots_offset, + buffer + 2*wots_offset, + buffer + 3*wots_offset, + buffer + 4*wots_offset, + buffer + 5*wots_offset, + buffer + 6*wots_offset, + buffer + 7*wots_offset, + ctx, leaf_addr); + + for (j = 0; j < 8; j++) { + set_type(leaf_addr + j*8, SPX_ADDR_TYPE_WOTS); + } + + /* Iterate down the WOTS chain */ + for (k=0;; k++) { + /* Check if one of the values we have needs to be saved as a */ + /* part of the WOTS signature */ + if (k == wots_k) { + memcpy( info->wots_sig + i * SPX_N, + buffer + wots_sign_index*wots_offset, SPX_N ); + } + + /* Check if we hit the top of the chain */ + if (k == SPX_WOTS_W - 1) break; + + /* Iterate one step on all 8 chains */ + for (j = 0; j < 8; j++) { + set_hash_addr(leaf_addr + j*8, k); + } + thashx8(buffer + 0*wots_offset, + buffer + 1*wots_offset, + buffer + 2*wots_offset, + buffer + 3*wots_offset, + buffer + 4*wots_offset, + buffer + 5*wots_offset, + buffer + 6*wots_offset, + buffer + 7*wots_offset, + buffer + 0*wots_offset, + buffer + 1*wots_offset, + buffer + 2*wots_offset, + buffer + 3*wots_offset, + buffer + 4*wots_offset, + buffer + 5*wots_offset, + buffer + 6*wots_offset, + buffer + 7*wots_offset, 1, ctx, leaf_addr); + } + } + + /* Do the final thash to generate the public keys */ + thashx8(dest + 0*SPX_N, + dest + 1*SPX_N, + dest + 2*SPX_N, + dest + 3*SPX_N, + dest + 4*SPX_N, + dest + 5*SPX_N, + dest + 6*SPX_N, + dest + 7*SPX_N, + pk_buffer + 0*wots_offset, + pk_buffer + 1*wots_offset, + pk_buffer + 2*wots_offset, + pk_buffer + 3*wots_offset, + pk_buffer + 4*wots_offset, + pk_buffer + 5*wots_offset, + pk_buffer + 6*wots_offset, + pk_buffer + 7*wots_offset, SPX_WOTS_LEN, ctx, pk_addr); +} diff --git a/src/crypto/pq/_s/sha2-avx2/wots.h b/src/crypto/pq/_s/sha2-avx2/wots.h new file mode 120000 index 0000000..8c327ea --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/wots.h @@ -0,0 +1 @@ +../ref/wots.h \ No newline at end of file diff --git a/src/crypto/pq/_s/sha2-avx2/wotsx8.h b/src/crypto/pq/_s/sha2-avx2/wotsx8.h new file mode 100644 index 0000000..10f72ad --- /dev/null +++ b/src/crypto/pq/_s/sha2-avx2/wotsx8.h @@ -0,0 +1,40 @@ +#if !defined( WOTSX8_H_ ) +#define WOTSX8_H_ + +#include +#include "params.h" + +/* + * This is here to provide an interface to the internal wots_gen_leafx8 + * routine. While this routine is not referenced in the package outside of + * wots.c, it is called from the stand-alone benchmark code to characterize + * the performance + */ +struct leaf_info_x8 { + unsigned char *wots_sig; + uint32_t wots_sign_leaf; /* The index of the WOTS we're using to sign */ + uint32_t *wots_steps; + uint32_t leaf_addr[8*8]; + uint32_t pk_addr[8*8]; +}; + +/* Macro to set the leaf_info to something 'benign', that is, it would */ +/* run with the same time as it does during the real signing process */ +/* Used only by the benchmark code */ +#define INITIALIZE_LEAF_INFO_X8(info, addr, step_buffer) { \ + info.wots_sig = 0; \ + info.wots_sign_leaf = ~0; \ + info.wots_steps = step_buffer; \ + int i; \ + for (i=0; i<8; i++) { \ + memcpy( &info.leaf_addr[8*i], addr, 32 ); \ + memcpy( &info.pk_addr[8*i], addr, 32 ); \ + } \ +} + +#define wots_gen_leafx8 SPX_NAMESPACE(wots_gen_leafx8) +void wots_gen_leafx8(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info); + +#endif /* WOTSX8_H_ */ diff --git a/src/crypto/pq/_s/shake-a64/.gitignore b/src/crypto/pq/_s/shake-a64/.gitignore new file mode 100644 index 0000000..67bcef9 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/.gitignore @@ -0,0 +1,5 @@ +test/* +!test/*.c +PQCsignKAT_*.rsp +PQCsignKAT_*.req +PQCgenKAT_sign diff --git a/src/crypto/pq/_s/shake-a64/PQCgenKAT_sign.c b/src/crypto/pq/_s/shake-a64/PQCgenKAT_sign.c new file mode 120000 index 0000000..a17dbe2 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/PQCgenKAT_sign.c @@ -0,0 +1 @@ +../ref/PQCgenKAT_sign.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/address.c b/src/crypto/pq/_s/shake-a64/address.c new file mode 120000 index 0000000..02c52c6 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/address.c @@ -0,0 +1 @@ +../ref/address.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/address.h b/src/crypto/pq/_s/shake-a64/address.h new file mode 120000 index 0000000..e670da5 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/address.h @@ -0,0 +1 @@ +../ref/address.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/api.h b/src/crypto/pq/_s/shake-a64/api.h new file mode 120000 index 0000000..ea89e0a --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/api.h @@ -0,0 +1 @@ +../ref/api.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/context.h b/src/crypto/pq/_s/shake-a64/context.h new file mode 100644 index 0000000..993c9ce --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/context.h @@ -0,0 +1,13 @@ +#ifndef SPX_CONTEXT_H +#define SPX_CONTEXT_H + +#include + +#include "params.h" + +typedef struct { + uint8_t pub_seed[SPX_N]; + uint8_t sk_seed[SPX_N]; +} spx_ctx; + +#endif diff --git a/src/crypto/pq/_s/shake-a64/f1600x2.h b/src/crypto/pq/_s/shake-a64/f1600x2.h new file mode 100644 index 0000000..70c5a8e --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/f1600x2.h @@ -0,0 +1,11 @@ +#ifndef SPX_F1600X2_H +#define SPX_F1600X2_H + +#include + +extern uint64_t f1600_RC[24]; +extern void _f1600x2(uint64_t* a, uint64_t* rc); + +#define f1600x2(s) do {_f1600x2((s), f1600_RC);} while(0) + +#endif diff --git a/src/crypto/pq/_s/shake-a64/f1600x2.s b/src/crypto/pq/_s/shake-a64/f1600x2.s new file mode 100644 index 0000000..640ed79 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/f1600x2.s @@ -0,0 +1,143 @@ +# From https://github.com/bwesterb/armed-keccak + +.macro round + # Execute theta, but without xoring into the state yet. + # Compute parities p[i] = a[i] ^ a[5+i] ^ ... ^ a[20+i]. + eor3.16b v25, v0, v5, v10 + eor3.16b v26, v1, v6, v11 + eor3.16b v27, v2, v7, v12 + eor3.16b v28, v3, v8, v13 + eor3.16b v29, v4, v9, v14 + + eor3.16b v25, v25, v15, v20 + eor3.16b v26, v26, v16, v21 + eor3.16b v27, v27, v17, v22 + eor3.16b v28, v28, v18, v23 + eor3.16b v29, v29, v19, v24 + + # d[0] = rotl(p[1], 1) ^ p[4] + rax1.2d v30, v29, v26 + # d[3] = rotl(p[4], 1) ^ p[2] + rax1.2d v29, v27, v29 + # d[1] = rotl(p[2], 1) ^ p[0] + rax1.2d v27, v25, v27 + # d[4] = rotl(p[0], 1) ^ p[3] + rax1.2d v25, v28, v25 + # d[2] = rotl(p[3], 1) ^ p[1] + rax1.2d v28, v26, v28 + + # Xor parities from step theta into the state at the same time + # as executing rho and pi. + eor.16b v0, v0, v30 + mov.16b v31, v1 + xar.2d v1, v6, v27, 20 + xar.2d v6, v9, v25, 44 + xar.2d v9, v22, v28, 3 + xar.2d v22, v14, v25, 25 + xar.2d v14, v20, v30, 46 + xar.2d v20, v2, v28, 2 + xar.2d v2, v12, v28, 21 + xar.2d v12, v13, v29, 39 + xar.2d v13, v19, v25, 56 + xar.2d v19, v23, v29, 8 + xar.2d v23, v15, v30, 23 + xar.2d v15, v4, v25, 37 + xar.2d v4, v24, v25, 50 + xar.2d v24, v21, v27, 62 + xar.2d v21, v8, v29, 9 + xar.2d v8, v16, v27, 19 + xar.2d v16, v5, v30, 28 + xar.2d v5, v3, v29, 36 + xar.2d v3, v18, v29, 43 + xar.2d v18, v17, v28, 49 + xar.2d v17, v11, v27, 54 + xar.2d v11, v7, v28, 58 + xar.2d v7, v10, v30, 61 + xar.2d v10, v31, v27, 63 + + # Chi + bcax.16b v25, v0, v2, v1 + bcax.16b v26, v1, v3, v2 + bcax.16b v2, v2, v4, v3 + bcax.16b v3, v3, v0, v4 + bcax.16b v4, v4, v1, v0 + mov.16b v0, v25 + mov.16b v1, v26 + + bcax.16b v25, v5, v7, v6 + bcax.16b v26, v6, v8, v7 + bcax.16b v7, v7, v9, v8 + bcax.16b v8, v8, v5, v9 + bcax.16b v9, v9, v6, v5 + mov.16b v5, v25 + mov.16b v6, v26 + + bcax.16b v25, v10, v12, v11 + bcax.16b v26, v11, v13, v12 + bcax.16b v12, v12, v14, v13 + bcax.16b v13, v13, v10, v14 + bcax.16b v14, v14, v11, v10 + mov.16b v10, v25 + mov.16b v11, v26 + + bcax.16b v25, v15, v17, v16 + bcax.16b v26, v16, v18, v17 + bcax.16b v17, v17, v19, v18 + bcax.16b v18, v18, v15, v19 + bcax.16b v19, v19, v16, v15 + mov.16b v15, v25 + mov.16b v16, v26 + + bcax.16b v25, v20, v22, v21 + bcax.16b v26, v21, v23, v22 + bcax.16b v22, v22, v24, v23 + bcax.16b v23, v23, v20, v24 + bcax.16b v24, v24, v21, v20 + mov.16b v20, v25 + mov.16b v21, v26 + + # iota + ld1r {v25.2d}, [x1], #8 + eor.16b v0, v0, v25 +.endm + +.align 4 +.global __f1600x2 +__f1600x2: + stp d8, d9, [sp,#-16]! + stp d10, d11, [sp,#-16]! + stp d12, d13, [sp,#-16]! + stp d14, d15, [sp,#-16]! + + mov x2, x0 + mov x3, #24 + + ld1.2d {v0, v1, v2, v3}, [x0], #64 + ld1.2d {v4, v5, v6, v7}, [x0], #64 + ld1.2d {v8, v9, v10, v11}, [x0], #64 + ld1.2d {v12, v13, v14, v15}, [x0], #64 + ld1.2d {v16, v17, v18, v19}, [x0], #64 + ld1.2d {v20, v21, v22, v23}, [x0], #64 + ld1.2d {v24}, [x0] + +loop: + round + + subs x3, x3, #1 + cbnz x3, loop + + mov x0, x2 + st1.2d {v0, v1, v2, v3}, [x0], #64 + st1.2d {v4, v5, v6, v7}, [x0], #64 + st1.2d {v8, v9, v10, v11}, [x0], #64 + st1.2d {v12, v13, v14, v15}, [x0], #64 + st1.2d {v16, v17, v18, v19}, [x0], #64 + st1.2d {v20, v21, v22, v23}, [x0], #64 + st1.2d {v24}, [x0] + + ldp d14, d15, [sp], #16 + ldp d12, d13, [sp], #16 + ldp d10, d11, [sp], #16 + ldp d8, d9, [sp], #16 + + ret lr diff --git a/src/crypto/pq/_s/shake-a64/f1600x2_const.c b/src/crypto/pq/_s/shake-a64/f1600x2_const.c new file mode 100644 index 0000000..6880176 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/f1600x2_const.c @@ -0,0 +1,30 @@ +#include "f1600x2.h" + +uint64_t f1600_RC[24] = { + 0x0000000000000001, + 0x0000000000008082, + 0x800000000000808A, + 0x8000000080008000, + 0x000000000000808B, + 0x0000000080000001, + 0x8000000080008081, + 0x8000000000008009, + 0x000000000000008A, + 0x0000000000000088, + 0x0000000080008009, + 0x000000008000000A, + 0x000000008000808B, + 0x800000000000008B, + 0x8000000000008089, + 0x8000000000008003, + 0x8000000000008002, + 0x8000000000000080, + 0x000000000000800A, + 0x800000008000000A, + 0x8000000080008081, + 0x8000000000008080, + 0x0000000080000001, + 0x8000000080008008, +}; + + diff --git a/src/crypto/pq/_s/shake-a64/fips202.c b/src/crypto/pq/_s/shake-a64/fips202.c new file mode 120000 index 0000000..da2fa42 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/fips202.c @@ -0,0 +1 @@ +../ref/fips202.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/fips202.h b/src/crypto/pq/_s/shake-a64/fips202.h new file mode 120000 index 0000000..c759415 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/fips202.h @@ -0,0 +1 @@ +../ref/fips202.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/fips202x2.c b/src/crypto/pq/_s/shake-a64/fips202x2.c new file mode 100644 index 0000000..87c0df8 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/fips202x2.c @@ -0,0 +1,165 @@ +#include +#include + +#include "fips202x2.h" +#include "fips202.h" +#include "f1600x2.h" + +uint64_t load64(const unsigned char *x) +{ + unsigned long long r = 0, i; + + for (i = 0; i < 8; ++i) { + r |= (unsigned long long)x[i] << 8 * i; + } + return r; +} + +void store64(uint8_t *x, uint64_t u) +{ + unsigned int i; + + for(i=0; i<8; ++i) { + x[i] = u; + u >>= 8; + } +} + +static void keccak_absorb2x(uint64_t *s, + unsigned int r, + const unsigned char *m0, + const unsigned char *m1, + unsigned long long int mlen, + unsigned char p) +{ + unsigned long long i; + unsigned char t0[200]; + unsigned char t1[200]; + + while (mlen >= r) + { + for (i = 0; i < r / 8; ++i) + { + s[2*i+0] ^= load64(m0 + 8 * i); + s[2*i+1] ^= load64(m1 + 8 * i); + } + + f1600x2(s); + mlen -= r; + m0 += r; + m1 += r; + } + + for (i = 0; i < r; ++i) + { + t0[i] = 0; + t1[i] = 0; + } + for (i = 0; i < mlen; ++i) + { + t0[i] = m0[i]; + t1[i] = m1[i]; + } + + t0[i] = p; + t1[i] = p; + + t0[r - 1] |= 128; + t1[r - 1] |= 128; + + for (i = 0; i < r / 8; ++i) + { + s[2*i+0] ^= load64(t0 + 8 * i); + s[2*i+1] ^= load64(t1 + 8 * i); + } +} + + +static void keccak_squeezeblocks2x(unsigned char *h0, + unsigned char *h1, + unsigned long long int nblocks, + uint64_t *s, + unsigned int r) +{ + unsigned int i; + + while(nblocks > 0) + { + f1600x2(s); + for(i=0;i<(r>>3);i++) + { + store64(h0+8*i, s[2*i+0]); + store64(h1+8*i, s[2*i+1]); + } + h0 += r; + h1 += r; + nblocks--; + } +} + + + +void shake128x2(unsigned char *out0, + unsigned char *out1, + unsigned long long outlen, + unsigned char *in0, + unsigned char *in1, + unsigned long long inlen) +{ + uint64_t s[50] = {0}; + unsigned char t0[SHAKE128_RATE]; + unsigned char t1[SHAKE128_RATE]; + unsigned int i; + + /* absorb 4 message of identical length in parallel */ + keccak_absorb2x(s, SHAKE128_RATE, in0, in1, inlen, 0x1F); + + /* Squeeze output */ + keccak_squeezeblocks2x(out0, out1, outlen/SHAKE128_RATE, s, SHAKE128_RATE); + + out0 += (outlen/SHAKE128_RATE)*SHAKE128_RATE; + out1 += (outlen/SHAKE128_RATE)*SHAKE128_RATE; + + if(outlen%SHAKE128_RATE) + { + keccak_squeezeblocks2x(t0, t1, 1, s, SHAKE128_RATE); + for(i=0;i + +uint64_t load64(const unsigned char *x); +void store64(uint8_t *x, uint64_t u); + + +void shake128x2(unsigned char *out0, + unsigned char *out1, + unsigned long long outlen, + unsigned char *in0, + unsigned char *in1, + unsigned long long inlen); + +void shake256x2(unsigned char *out0, + unsigned char *out1, + unsigned long long outlen, + unsigned char *in0, + unsigned char *in1, + unsigned long long inlen); + +#endif diff --git a/src/crypto/pq/_s/shake-a64/fors.c b/src/crypto/pq/_s/shake-a64/fors.c new file mode 100644 index 0000000..a19fc7e --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/fors.c @@ -0,0 +1,198 @@ +#include +#include +#include + +#include "thash.h" +#include "fors.h" +#include "utils.h" +#include "utilsx2.h" +#include "hash.h" +#include "hashx2.h" +#include "thashx2.h" +#include "address.h" + +static void fors_gen_sk(unsigned char *sk, const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + prf_addr(sk, ctx, fors_leaf_addr); +} + +static void fors_gen_skx2(unsigned char *sk0, + unsigned char *sk1, + const spx_ctx *ctx, + uint32_t fors_leaf_addrx2[2*8]) +{ + prf_addrx2(sk0, sk1, + ctx, fors_leaf_addrx2); +} + +static void fors_sk_to_leaf(unsigned char *leaf, const unsigned char *sk, + const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + thash(leaf, sk, 1, ctx, fors_leaf_addr); +} + +static void fors_sk_to_leafx2(unsigned char *leaf0, + unsigned char *leaf1, + const unsigned char *sk0, + const unsigned char *sk1, + const spx_ctx *ctx, + uint32_t fors_leaf_addrx2[2*8]) +{ + thashx2(leaf0, leaf1, + sk0, sk1, + 1, ctx, fors_leaf_addrx2); +} + +struct fors_gen_leaf_info { + uint32_t leaf_addrx[2*8]; +}; + +static void fors_gen_leafx2(unsigned char *leaf, + const spx_ctx *ctx, + uint32_t addr_idx, void *info) +{ + struct fors_gen_leaf_info *fors_info = info; + uint32_t *fors_leaf_addrx2 = fors_info->leaf_addrx; + unsigned int j; + + /* Only set the parts that the caller doesn't set */ + for (j = 0; j < 2; j++) { + set_tree_index(fors_leaf_addrx2 + j*8, addr_idx + j); + set_type(fors_leaf_addrx2 + j*8, SPX_ADDR_TYPE_FORSPRF); + } + + fors_gen_skx2(leaf + 0*SPX_N, + leaf + 1*SPX_N, + ctx, fors_leaf_addrx2); + + for (j = 0; j < 2; j++) { + set_type(fors_leaf_addrx2 + j*8, SPX_ADDR_TYPE_FORSTREE); + } + + fors_sk_to_leafx2(leaf + 0*SPX_N, + leaf + 1*SPX_N, + leaf + 0*SPX_N, + leaf + 1*SPX_N, + ctx, fors_leaf_addrx2); +} + +/** + * Interprets m as SPX_FORS_HEIGHT-bit unsigned integers. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + * Assumes indices has space for SPX_FORS_TREES integers. + */ +static void message_to_indices(uint32_t *indices, const unsigned char *m) +{ + unsigned int i, j; + unsigned int offset = 0; + + for (i = 0; i < SPX_FORS_TREES; i++) { + indices[i] = 0; + for (j = 0; j < SPX_FORS_HEIGHT; j++) { + indices[i] ^= ((m[offset >> 3] >> (offset & 0x7)) & 0x1) << j; + offset++; + } + } +} + +/** + * Signs a message m, deriving the secret key from sk_seed and the FTS address. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_sign(unsigned char *sig, unsigned char *pk, + const unsigned char *m, + const spx_ctx *ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + uint32_t fors_tree_addr[2*8] = {0}; + struct fors_gen_leaf_info fors_info = {0}; + uint32_t *fors_leaf_addr = fors_info.leaf_addrx; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + for (i=0; i<2; i++) { + copy_keypair_addr(fors_tree_addr + 8*i, fors_addr); + set_type(fors_tree_addr + 8*i, SPX_ADDR_TYPE_FORSTREE); + copy_keypair_addr(fors_leaf_addr + 8*i, fors_addr); + } + copy_keypair_addr(fors_pk_addr, fors_addr); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + + /* Include the secret key part that produces the selected leaf node. */ + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSPRF); + fors_gen_sk(sig, ctx, fors_tree_addr); + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + sig += SPX_N; + + /* Compute the authentication path for this leaf node. */ + treehashx2(roots + i*SPX_N, sig, ctx, + indices[i], idx_offset, SPX_FORS_HEIGHT, fors_gen_leafx2, + fors_tree_addr, &fors_info); + + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} + +/** + * Derives the FORS public key from a signature. + * This can be used for verification by comparing to a known public key, or to + * subsequently verify a signature on the derived public key. The latter is the + * typical use-case when used as an FTS below an OTS in a hypertree. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *m, + const spx_ctx *ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + unsigned char leaf[SPX_N]; + uint32_t fors_tree_addr[8] = {0}; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + copy_keypair_addr(fors_tree_addr, fors_addr); + copy_keypair_addr(fors_pk_addr, fors_addr); + + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + + /* Derive the leaf from the included secret key part. */ + fors_sk_to_leaf(leaf, sig, ctx, fors_tree_addr); + sig += SPX_N; + + /* Derive the corresponding root node of this tree. */ + compute_root(roots + i*SPX_N, leaf, indices[i], idx_offset, + sig, SPX_FORS_HEIGHT, ctx, fors_tree_addr); + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} diff --git a/src/crypto/pq/_s/shake-a64/fors.h b/src/crypto/pq/_s/shake-a64/fors.h new file mode 120000 index 0000000..07156bd --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/fors.h @@ -0,0 +1 @@ +../ref/fors.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/hash.h b/src/crypto/pq/_s/shake-a64/hash.h new file mode 120000 index 0000000..cffc52b --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/hash.h @@ -0,0 +1 @@ +../ref/hash.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/hash_shake.c b/src/crypto/pq/_s/shake-a64/hash_shake.c new file mode 120000 index 0000000..1d9d0cd --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/hash_shake.c @@ -0,0 +1 @@ +../ref/hash_shake.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/hash_shakex2.c b/src/crypto/pq/_s/shake-a64/hash_shakex2.c new file mode 100644 index 0000000..d6f36c2 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/hash_shakex2.c @@ -0,0 +1,51 @@ +#include +#include + +#include "address.h" +#include "params.h" +#include "fips202x2.h" +#include "f1600x2.h" +#include "hashx2.h" + +/* + * 2-way parallel version of prf_addr; takes 2x as much input and output + */ +void prf_addrx2(unsigned char *out0, + unsigned char *out1, + const spx_ctx *ctx, + const uint32_t addrx2[2*8]) { + /* As we write and read only a few quadwords, it is more efficient to + * build and extract from the fourway SHAKE256 state by hand. */ + uint64_t state[50] = {0}; + + for (int i = 0; i < SPX_N/8; i++) { + uint64_t x = load64(ctx->pub_seed + 8*i); + state[2*i] = x; + state[2*i+1] = x; + } + for (int i = 0; i < 4; i++) { + state[2*(SPX_N/8 + i)] = (((uint64_t)addrx2[1+2*i]) << 32) + | (uint64_t)addrx2[2*i]; + state[2*(SPX_N/8 + i) + 1] = (((uint64_t)addrx2[8+1+2*i]) << 32) + | (uint64_t)addrx2[8+2*i]; + } + for (int i = 0; i < SPX_N/8; i++) { + uint64_t x = load64(ctx->sk_seed + 8*i); + state[2*(SPX_N/8+i+4)] = x; + state[2*(SPX_N/8+i+4)+1] = x; + } + + /* SHAKE domain separator and padding. */ + state[2*(SPX_N/4+4)] = 0x1f; + state[2*(SPX_N/4+4)+1] = 0x1f; + + state[2*16] = 0x80ULL << 56; + state[2*16+1] = 0x80ULL << 56; + + f1600x2(state); + + for (int i = 0; i < SPX_N/8; i++) { + store64(out0 + 8*i, state[2*i]); + store64(out1 + 8*i, state[2*i+1]); + } +} diff --git a/src/crypto/pq/_s/shake-a64/hashx2.h b/src/crypto/pq/_s/shake-a64/hashx2.h new file mode 100644 index 0000000..25ce94d --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/hashx2.h @@ -0,0 +1,14 @@ +#ifndef SPX_HASHX2_H +#define SPX_HASHX2_H + +#include +#include "context.h" +#include "params.h" + +#define prf_addrx2 SPX_NAMESPACE(prf_addrx2) +void prf_addrx2(unsigned char *out0, + unsigned char *out1, + const spx_ctx *ctx, + const uint32_t addrx2[2*8]); + +#endif diff --git a/src/crypto/pq/_s/shake-a64/merkle.c b/src/crypto/pq/_s/shake-a64/merkle.c new file mode 100644 index 0000000..b2791d1 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/merkle.c @@ -0,0 +1,65 @@ +#include +#include + +#include "utils.h" +#include "utilsx2.h" +#include "wots.h" +#include "wotsx2.h" +#include "merkle.h" +#include "address.h" +#include "params.h" + +/* + * This generates a Merkle signature (WOTS signature followed by the Merkle + * authentication path). + */ +void merkle_sign(uint8_t *sig, unsigned char *root, + const spx_ctx* ctx, + uint32_t wots_addr[8], uint32_t tree_addr[8], + uint32_t idx_leaf) +{ + unsigned char *auth_path = sig + SPX_WOTS_BYTES; + uint32_t tree_addrx2[2*8] = { 0 }; + int j; + struct leaf_info_x2 info = { 0 }; + unsigned steps[ SPX_WOTS_LEN ]; + + info.wots_sig = sig; + chain_lengths(steps, root); + info.wots_steps = steps; + + for (j=0; j<2; j++) { + set_type(&tree_addrx2[8*j], SPX_ADDR_TYPE_HASHTREE); + set_type(&info.leaf_addr[8*j], SPX_ADDR_TYPE_WOTS); + set_type(&info.pk_addr[8*j], SPX_ADDR_TYPE_WOTSPK); + copy_subtree_addr(&tree_addrx2[8*j], tree_addr); + copy_subtree_addr(&info.leaf_addr[8*j], wots_addr); + copy_subtree_addr(&info.pk_addr[8*j], wots_addr); + } + + info.wots_sign_leaf = idx_leaf; + + treehashx2(root, auth_path, ctx, + idx_leaf, 0, + SPX_TREE_HEIGHT, + wots_gen_leafx2, + tree_addrx2, &info); +} + +/* Compute root node of the top-most subtree. */ +void merkle_gen_root(unsigned char *root, const spx_ctx *ctx) +{ + /* We do not need the auth path in key generation, but it simplifies the + code to have just one treehash routine that computes both root and path + in one function. */ + unsigned char auth_path[SPX_TREE_HEIGHT * SPX_N + SPX_WOTS_BYTES]; + uint32_t top_tree_addr[8] = {0}; + uint32_t wots_addr[8] = {0}; + + set_layer_addr(top_tree_addr, SPX_D - 1); + set_layer_addr(wots_addr, SPX_D - 1); + + merkle_sign(auth_path, root, ctx, + wots_addr, top_tree_addr, + ~0 /* ~0 means "don't bother generating an auth path */ ); +} diff --git a/src/crypto/pq/_s/shake-a64/merkle.h b/src/crypto/pq/_s/shake-a64/merkle.h new file mode 120000 index 0000000..7d167ed --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/merkle.h @@ -0,0 +1 @@ +../ref/merkle.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/params.h b/src/crypto/pq/_s/shake-a64/params.h new file mode 120000 index 0000000..53133cc --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/params.h @@ -0,0 +1 @@ +../ref/params.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-128f.h b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-128f.h new file mode 120000 index 0000000..c65db98 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-128f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-128f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-128s.h b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-128s.h new file mode 120000 index 0000000..18671f7 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-128s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-128s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-192f.h b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-192f.h new file mode 120000 index 0000000..d071e35 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-192f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-192f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-192s.h b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-192s.h new file mode 120000 index 0000000..267e2c8 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-192s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-192s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-256f.h b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-256f.h new file mode 120000 index 0000000..3b4743b --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-256f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-256f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-256s.h b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-256s.h new file mode 120000 index 0000000..0795ee1 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/params/params-sphincs-shake-256s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-256s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/randombytes.c b/src/crypto/pq/_s/shake-a64/randombytes.c new file mode 120000 index 0000000..59a42a5 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/randombytes.c @@ -0,0 +1 @@ +../ref/randombytes.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/randombytes.h b/src/crypto/pq/_s/shake-a64/randombytes.h new file mode 120000 index 0000000..055e443 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/randombytes.h @@ -0,0 +1 @@ +../ref/randombytes.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/rng.c b/src/crypto/pq/_s/shake-a64/rng.c new file mode 120000 index 0000000..6e2fdac --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/rng.c @@ -0,0 +1 @@ +../ref/rng.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/rng.h b/src/crypto/pq/_s/shake-a64/rng.h new file mode 120000 index 0000000..d678c7c --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/rng.h @@ -0,0 +1 @@ +../ref/rng.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/shake_offsets.h b/src/crypto/pq/_s/shake-a64/shake_offsets.h new file mode 120000 index 0000000..8cfe4c0 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/shake_offsets.h @@ -0,0 +1 @@ +../ref/shake_offsets.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/sign.c b/src/crypto/pq/_s/shake-a64/sign.c new file mode 120000 index 0000000..42fea88 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/sign.c @@ -0,0 +1 @@ +../ref/sign.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/test/benchmark.c b/src/crypto/pq/_s/shake-a64/test/benchmark.c new file mode 100644 index 0000000..a1affb0 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/test/benchmark.c @@ -0,0 +1,175 @@ +#define _POSIX_C_SOURCE 199309L + +#include +#include +#include + +#include "../thash.h" +#include "../thashx2.h" +#include "../api.h" +#include "../f1600x2.h" +#include "../fors.h" +#include "../wots.h" +#include "../wotsx2.h" +#include "../params.h" +#include "../randombytes.h" + +#include "cycles.h" + +#define SPX_MLEN 32 +#define NTESTS 10 + +static void wots_gen_pkx2(unsigned char *pk, const spx_ctx *ctx, + uint32_t addr[8]); + +static int cmp_llu(const void *a, const void*b) +{ + if(*(unsigned long long *)a < *(unsigned long long *)b) return -1; + if(*(unsigned long long *)a > *(unsigned long long *)b) return 1; + return 0; +} + +static unsigned long long median(unsigned long long *l, size_t llen) +{ + qsort(l,llen,sizeof(unsigned long long),cmp_llu); + + if(llen%2) return l[llen/2]; + else return (l[llen/2-1]+l[llen/2])/2; +} + +static void delta(unsigned long long *l, size_t llen) +{ + unsigned int i; + for(i = 0; i < llen - 1; i++) { + l[i] = l[i+1] - l[i]; + } +} + +static void printfcomma (unsigned long long n) +{ + if (n < 1000) { + printf("%llu", n); + return; + } + printfcomma(n / 1000); + printf (",%03llu", n % 1000); +} + +static void printfalignedcomma (unsigned long long n, int len) +{ + unsigned long long ncopy = n; + int i = 0; + + while (ncopy > 9) { + len -= 1; + ncopy /= 10; + i += 1; // to account for commas + } + i = i/3 - 1; // to account for commas + for (; i < len; i++) { + printf(" "); + } + printfcomma(n); +} + +static void display_result(double result, unsigned long long *l, size_t llen, unsigned long long mul) +{ + unsigned long long med; + + result /= NTESTS; + delta(l, NTESTS + 1); + med = median(l, llen); + printf("avg. %11.2lf us (%2.2lf sec); median ", result, result / 1e6); + printfalignedcomma(med, 12); + printf(" cycles, %5llux: ", mul); + printfalignedcomma(mul*med, 12); + printf(" cycles\n"); +} + +#define MEASURE_GENERIC(TEXT, MUL, FNCALL, CORR)\ + printf(TEXT);\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);\ + for(i = 0; i < NTESTS; i++) {\ + t[i] = cpucycles() / CORR;\ + FNCALL;\ + }\ + t[NTESTS] = cpucycles();\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);\ + result = ((stop.tv_sec - start.tv_sec) * 1e6 + \ + (stop.tv_nsec - start.tv_nsec) / 1e3) / (double)CORR;\ + display_result(result, t, NTESTS, MUL); +#define MEASURT(TEXT, MUL, FNCALL)\ + MEASURE_GENERIC(\ + TEXT, MUL,\ + do {\ + for (int j = 0; j < 1000; j++) {\ + FNCALL;\ + }\ + } while (0);,\ + 1000); +#define MEASURE(TEXT, MUL, FNCALL) MEASURE_GENERIC(TEXT, MUL, FNCALL, 1) + +int main(void) +{ + init_cpucycles(); + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + spx_ctx ctx; + unsigned char pk[SPX_PK_BYTES]; + unsigned char sk[SPX_SK_BYTES]; + unsigned char *m = malloc(SPX_MLEN); + unsigned char *sm = malloc(SPX_BYTES + SPX_MLEN); + unsigned char *mout = malloc(SPX_BYTES + SPX_MLEN); + + unsigned char fors_pk[SPX_FORS_PK_BYTES]; + unsigned char fors_m[SPX_FORS_MSG_BYTES]; + unsigned char fors_sig[SPX_FORS_BYTES]; + unsigned char addr[SPX_ADDR_BYTES*2]; + unsigned char wots_pk[4*SPX_WOTS_PK_BYTES]; + unsigned char block[SPX_N]; + + unsigned long long smlen; + unsigned long long mlen; + unsigned long long t[NTESTS+1]; + struct timespec start, stop; + double result; + int i; + uint64_t statex2[50]; + + randombytes(m, SPX_MLEN); + randombytes(addr, SPX_ADDR_BYTES*2); + + printf("Parameters: n = %d, h = %d, d = %d, b = %d, k = %d, w = %d\n", + SPX_N, SPX_FULL_HEIGHT, SPX_D, SPX_FORS_HEIGHT, SPX_FORS_TREES, + SPX_WOTS_W); + + printf("Running %d iterations.\n", NTESTS); + + MEASURT("thash ", 1, thash(block, block, 1, &ctx, (uint32_t*)addr)); + MEASURT("f1600x2 ", 1, f1600x2(statex2)); + MEASURT("thashx2 ", 1, thashx2(block, block, block, block, 1, &ctx, (uint32_t*)addr)); + MEASURE("Generating keypair.. ", 1, crypto_sign_keypair(pk, sk)); + MEASURE(" - WOTS pk gen 2x.. ", (1 << SPX_TREE_HEIGHT) / 2, wots_gen_pkx2(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Signing.. ", 1, crypto_sign(sm, &smlen, m, SPX_MLEN, sk)); + MEASURE(" - FORS signing.. ", 1, fors_sign(fors_sig, fors_pk, fors_m, &ctx, (uint32_t *) addr)); + MEASURE(" - WOTS pk gen x2.. ", SPX_D * (1 << SPX_TREE_HEIGHT) / 2, wots_gen_pkx2(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Verifying.. ", 1, crypto_sign_open(mout, &mlen, sm, smlen, pk)); + + printf("Signature size: %d (%.2f KiB)\n", SPX_BYTES, SPX_BYTES / 1024.0); + printf("Public key size: %d (%.2f KiB)\n", SPX_PK_BYTES, SPX_PK_BYTES / 1024.0); + printf("Secret key size: %d (%.2f KiB)\n", SPX_SK_BYTES, SPX_SK_BYTES / 1024.0); + + free(m); + free(sm); + free(mout); + + return 0; +} + +static void wots_gen_pkx2(unsigned char *pk, const spx_ctx *ctx, uint32_t addr[8]) { + struct leaf_info_x2 leaf; + unsigned steps[ SPX_WOTS_LEN ] = { 0 }; + INITIALIZE_LEAF_INFO_X2(leaf, addr, steps); + wots_gen_leafx2(pk, ctx, 0, &leaf); +} diff --git a/src/crypto/pq/_s/shake-a64/test/cycles.c b/src/crypto/pq/_s/shake-a64/test/cycles.c new file mode 120000 index 0000000..513f008 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/test/cycles.c @@ -0,0 +1 @@ +../../ref/test/cycles.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/test/fors.c b/src/crypto/pq/_s/shake-a64/test/fors.c new file mode 120000 index 0000000..b2bccee --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/test/fors.c @@ -0,0 +1 @@ +../../ref/test/fors.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/test/spx.c b/src/crypto/pq/_s/shake-a64/test/spx.c new file mode 120000 index 0000000..7af26df --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/test/spx.c @@ -0,0 +1 @@ +../../ref/test/spx.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/test/thashx2.c b/src/crypto/pq/_s/shake-a64/test/thashx2.c new file mode 100644 index 0000000..0b55b13 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/test/thashx2.c @@ -0,0 +1,43 @@ +#include +#include + +#include "../thashx2.h" +#include "../thash.h" +#include "../randombytes.h" +#include "../params.h" + +int main(void) +{ + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + unsigned char input[2*SPX_N]; + unsigned char output[2*SPX_N]; + unsigned char out2[2*SPX_N]; + uint32_t addr[2*8] = {0}; + unsigned int j; + spx_ctx ctx; + + randombytes(ctx.pub_seed, SPX_N); + randombytes(input, 4*SPX_N); + randombytes((unsigned char *)addr, 2 * 8 * sizeof(uint32_t)); + + printf("Testing if thash matches thashx2.. "); + + for (j = 0; j < 2; j++) { + thash(out2 + j * SPX_N, input + j * SPX_N, 1, &ctx, addr + j*8); + } + + thashx2(output + 0*SPX_N, + output + 1*SPX_N, + input + 0*SPX_N, + input + 1*SPX_N, + 1, &ctx, addr); + + if (memcmp(out2, output, 2 * SPX_N)) { + printf("failed!\n"); + return -1; + } + printf("successful.\n"); + return 0; +} diff --git a/src/crypto/pq/_s/shake-a64/thash.h b/src/crypto/pq/_s/shake-a64/thash.h new file mode 100644 index 0000000..ec9222c --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/thash.h @@ -0,0 +1,12 @@ +#ifndef SPX_THASHX2_AS_ONE +#define SPX_THASHX2_AS_ONE + +#include +#include "context.h" + +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]); + + +#endif + diff --git a/src/crypto/pq/_s/shake-a64/thash_shake_robustx2.c b/src/crypto/pq/_s/shake-a64/thash_shake_robustx2.c new file mode 100644 index 0000000..77e09ff --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/thash_shake_robustx2.c @@ -0,0 +1,112 @@ +#include +#include + +#include "thash.h" +#include "thashx2.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "f1600x2.h" +#include "fips202x2.h" + + +void thash(unsigned char *out, + const unsigned char *in, + unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) { + uint32_t addrx2 [2*8] = { + addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], + addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7] + }; + thashx2(out, out, in, in, inblocks, ctx, addrx2); +} + +/** + * 2-way parallel version of thash; takes 2x as much input and output + */ +void thashx2(unsigned char *out0, + unsigned char *out1, + const unsigned char *in0, + const unsigned char *in1, + unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx2[2*8]) +{ + if (inblocks == 1 || inblocks == 2) { + /* As we write and read only a few quadwords, it is more efficient to + * build and extract from the twoway SHAKE256 state by hand. */ + uint64_t state[50] = {0}; + uint64_t state2[50]; + + for (int i = 0; i < SPX_N/8; i++) { + uint64_t x = load64(ctx->pub_seed + 8*i); + state[2*i] = x; + state[2*i+1] = x; + } + for (int i = 0; i < 4; i++) { + state[2*(SPX_N/8 + i)] = (((uint64_t)addrx2[1+2*i]) << 32) + | (uint64_t)addrx2[2*i]; + state[2*(SPX_N/8 + i) + 1] = (((uint64_t)addrx2[8+1+2*i]) << 32) + | (uint64_t)addrx2[8+2*i]; + } + + /* Domain separator and padding. */ + state[2*16] = 0x80ULL << 56; + state[2*16+1] = 0x80ULL << 56; + + state[2*((SPX_N/8)+4)] ^= 0x1f; + state[2*((SPX_N/8)+4)+1] ^= 0x1f; + + /* We will permutate state2 with f1600x2 to compute the bitmask, + * but first we'll copy it to state2 which will be used to compute + * the final output, as its input is almost identical. */ + memcpy(state2, state, 400); + + f1600x2(state); + + /* By copying from state, state2 already contains the pub_seed + * and address. We just need to copy in the input blocks xorred with + * the bitmask we just computed. */ + for (unsigned int i = 0; i < (SPX_N/8) * inblocks; i++) { + state2[2*(SPX_N/8+4+i)] = state[2*i] ^ load64(in0 + 8*i); + state2[2*(SPX_N/8+4+i)+1] = state[2*i+1] ^ load64(in1 + 8*i); + } + + /* Domain separator and start of padding. Note that the quadwords + * around are already zeroed for state from which we copied. + * We do a XOR instead of a set as this might be the 16th quadword + * when N=32 and inblocks=2, which already contains the end + * of the padding. */ + state2[2*((SPX_N/8)*(1+inblocks)+4)] ^= 0x1f; + state2[2*((SPX_N/8)*(1+inblocks)+4)+1] ^= 0x1f; + + f1600x2(state2); + + for (int i = 0; i < SPX_N/8; i++) { + store64(out0 + 8*i, state2[2*i]); + store64(out1 + 8*i, state2[2*i+1]); + } + } else { + SPX_VLA(unsigned char, buf0, SPX_N + SPX_ADDR_BYTES + inblocks * SPX_N); + SPX_VLA(unsigned char, buf1, SPX_N + SPX_ADDR_BYTES + inblocks * SPX_N); + SPX_VLA(unsigned char, bitmask0, inblocks * SPX_N); + SPX_VLA(unsigned char, bitmask1, inblocks * SPX_N); + unsigned int i; + + memcpy(buf0, ctx->pub_seed, SPX_N); + memcpy(buf1, ctx->pub_seed, SPX_N); + memcpy(buf0 + SPX_N, addrx2 + 0*8, SPX_ADDR_BYTES); + memcpy(buf1 + SPX_N, addrx2 + 1*8, SPX_ADDR_BYTES); + + shake256x2(bitmask0, bitmask1, inblocks * SPX_N, + buf0, buf1, SPX_N + SPX_ADDR_BYTES); + + for (i = 0; i < inblocks * SPX_N; i++) { + buf0[SPX_N + SPX_ADDR_BYTES + i] = in0[i] ^ bitmask0[i]; + buf1[SPX_N + SPX_ADDR_BYTES + i] = in1[i] ^ bitmask1[i]; + } + + shake256x2(out0, out1, SPX_N, + buf0, buf1, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + } +} diff --git a/src/crypto/pq/_s/shake-a64/thash_shake_simplex2.c b/src/crypto/pq/_s/shake-a64/thash_shake_simplex2.c new file mode 100644 index 0000000..0bd7130 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/thash_shake_simplex2.c @@ -0,0 +1,83 @@ +#include +#include + +#include "thash.h" +#include "thashx2.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "f1600x2.h" +#include "fips202x2.h" + + +void thash(unsigned char *out, + const unsigned char *in, + unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) { + uint32_t addrx2 [2*8] = { + addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], + addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7] + }; + thashx2(out, out, in, in, inblocks, ctx, addrx2); +} + +/** + * 2-way parallel version of thash; takes 2x as much input and output + */ +void thashx2(unsigned char *out0, + unsigned char *out1, + const unsigned char *in0, + const unsigned char *in1, + unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx2[2*8]) +{ + if (inblocks == 1 || inblocks == 2) { + /* As we write and read only a few quadwords, it is more efficient to + * build and extract from the twoway SHAKE256 state by hand. */ + uint64_t state[50] = {0}; + for (int i = 0; i < SPX_N/8; i++) { + uint64_t x = load64(ctx->pub_seed + 8*i); + state[2*i] = x; + state[2*i+1] = x; + } + for (int i = 0; i < 4; i++) { + state[2*(SPX_N/8 + i)] = (((uint64_t)addrx2[1+2*i]) << 32) + | (uint64_t)addrx2[2*i]; + state[2*(SPX_N/8 + i) + 1] = (((uint64_t)addrx2[8+1+2*i]) << 32) + | (uint64_t)addrx2[8+2*i]; + } + + for (unsigned int i = 0; i < (SPX_N/8) * inblocks; i++) { + state[2*(SPX_N/8+4+i)] = load64(in0+8*i); + state[2*(SPX_N/8+4+i)+1] = load64(in1+8*i); + } + + /* Domain separator and padding. */ + state[2*16] = 0x80ULL << 56; + state[2*16+1] = 0x80ULL << 56; + + state[2*((SPX_N/8)*(1+inblocks)+4)] ^= 0x1f; + state[2*((SPX_N/8)*(1+inblocks)+4)+1] ^= 0x1f; + + f1600x2(state); + + for (int i = 0; i < SPX_N/8; i++) { + store64(out0 + 8*i, state[2*i]); + store64(out1 + 8*i, state[2*i+1]); + } + } else { + SPX_VLA(unsigned char, buf0, SPX_N + SPX_ADDR_BYTES + inblocks * SPX_N); + SPX_VLA(unsigned char, buf1, SPX_N + SPX_ADDR_BYTES + inblocks * SPX_N); + + memcpy(buf0, ctx->pub_seed, SPX_N); + memcpy(buf1, ctx->pub_seed, SPX_N); + memcpy(buf0 + SPX_N, addrx2 + 0*8, SPX_ADDR_BYTES); + memcpy(buf1 + SPX_N, addrx2 + 1*8, SPX_ADDR_BYTES); + memcpy(buf0 + SPX_N + SPX_ADDR_BYTES, in0, inblocks * SPX_N); + memcpy(buf1 + SPX_N + SPX_ADDR_BYTES, in1, inblocks * SPX_N); + + shake256x2(out0, out1, SPX_N, + buf0, buf1, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + } +} diff --git a/src/crypto/pq/_s/shake-a64/thashx2.h b/src/crypto/pq/_s/shake-a64/thashx2.h new file mode 100644 index 0000000..bde4f59 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/thashx2.h @@ -0,0 +1,16 @@ +#ifndef SPX_THASHX2_H +#define SPX_THASHX2_H + +#include +#include "context.h" +#include "params.h" + +#define thashx2 SPX_NAMESPACE(thashx2) +void thashx2(unsigned char *out0, + unsigned char *out1, + const unsigned char *in0, + const unsigned char *in1, + unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx2[2*8]); + +#endif diff --git a/src/crypto/pq/_s/shake-a64/utils.c b/src/crypto/pq/_s/shake-a64/utils.c new file mode 120000 index 0000000..e8ef6eb --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/utils.c @@ -0,0 +1 @@ +../ref/utils.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/utils.h b/src/crypto/pq/_s/shake-a64/utils.h new file mode 120000 index 0000000..51b0d39 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/utils.h @@ -0,0 +1 @@ +../ref/utils.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/utilsx2.c b/src/crypto/pq/_s/shake-a64/utilsx2.c new file mode 100644 index 0000000..7b75fb4 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/utilsx2.c @@ -0,0 +1,130 @@ +#include + +#include "utils.h" +#include "utilsx2.h" +#include "params.h" +#include "thashx2.h" +#include "address.h" + +/* + * Generate the entire Merkle tree, computing the authentication path for leaf_idx, + * and the resulting root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE) + * + * This expects tree_addrx2 to be initialized to 2 parallel addr structures for + * the Merkle tree nodes + * + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + * + * This works by using the standard Merkle tree building algorithm, except + * that each 'node' tracked is actually 2 consecutive nodes in the real tree. + * When we combine two logical nodes AB and WX, we perform the H + * operation on adjacent real nodes, forming the parent logical node + * (AB)(WX) + * + * When we get to the top level of the real tree (where there is only + * one logical node), we continue this operation one more time; the right + * most real node will by the actual root (and the other node will be + * garbage). We follow the same thashx2 logic so that the 'extract + * authentication path components' part of the loop is still executed (and + * to simplify the code somewhat) + */ +void treehashx2(unsigned char *root, unsigned char *auth_path, + const spx_ctx *ctx, + uint32_t leaf_idx, uint32_t idx_offset, + uint32_t tree_height, + void (*gen_leafx2)( + unsigned char* /* Where to write the leaves */, + const spx_ctx*, + uint32_t idx, void *info), + uint32_t tree_addrx2[2*8], + void *info) +{ + /* This is where we keep the intermediate nodes */ + SPX_VLA(unsigned char, stackx2, 2 * tree_height * SPX_N); + uint32_t left_adj = 0, prev_left_adj = 0; /* When we're doing the top */ + /* level, the left-most part of the tree isn't at the beginning */ + /* of current[]. These give the offset of the actual start */ + + uint32_t idx; + uint32_t max_idx = (1 << (tree_height-1)) - 1; + for (idx = 0;; idx++) { + unsigned char current[2*SPX_N]; /* Current logical node */ + gen_leafx2( current, ctx, 2*idx + idx_offset, + info ); + + /* Now combine the freshly generated right node with previously */ + /* generated left ones */ + uint32_t internal_idx_offset = idx_offset; + uint32_t internal_idx = idx; + uint32_t internal_leaf = leaf_idx; + uint32_t h; /* The height we are in the Merkle tree */ + for (h=0;; h++, internal_idx >>= 1, internal_leaf >>= 1) { + + /* Special processing if we're at the top of the tree */ + if (h >= tree_height - 1) { + if (h == tree_height) { + /* We hit the root; return it */ + memcpy( root, ¤t[1*SPX_N], SPX_N ); + return; + } + /* The tree indexing logic is a bit off in this case */ + /* Adjust it so that the left-most node of the part of */ + /* the tree that we're processing has index 0 */ + prev_left_adj = left_adj; + left_adj = 2 - (1 << (tree_height - h - 1)); + } + + /* Check if we hit the top of the tree */ + if (h == tree_height) { + /* We hit the root; return it */ + memcpy( root, ¤t[1*SPX_N], SPX_N ); + return; + } + + /* + * Check if one of the nodes we have is a part of the + * authentication path; if it is, write it out + */ + if ((((internal_idx << 1) ^ internal_leaf) & ~0x1) == 0) { + memcpy( &auth_path[ h * SPX_N ], + ¤t[(((internal_leaf&1)^1) + prev_left_adj) * SPX_N], + SPX_N ); + } + + /* + * Check if we're at a left child; if so, stop going up the stack + * Exception: if we've reached the end of the tree, keep on going + * (so we combine the last 2 nodes into the one root node in two + * more iterations) + */ + if ((internal_idx & 1) == 0 && idx < max_idx) { + break; + } + + /* Ok, we're at a right node (or doing the top 3 levels) */ + /* Now combine the left and right logical nodes together */ + + /* Set the address of the node we're creating. */ + int j; + internal_idx_offset >>= 1; + for (j = 0; j < 2; j++) { + set_tree_height(tree_addrx2 + j*8, h + 1); + set_tree_index(tree_addrx2 + j*8, + (2/2) * (internal_idx&~1) + j - left_adj + internal_idx_offset ); + } + unsigned char *left = &stackx2[h * 2 * SPX_N]; + thashx2( ¤t[0 * SPX_N], + ¤t[1 * SPX_N], + &left [0 * SPX_N], + ¤t[0 * SPX_N], + 2, ctx, tree_addrx2); + } + + /* We've hit a left child; save the current for when we get the */ + /* corresponding right right */ + memcpy( &stackx2[h * 2 * SPX_N], current, 2 * SPX_N); + } +} diff --git a/src/crypto/pq/_s/shake-a64/utilsx2.h b/src/crypto/pq/_s/shake-a64/utilsx2.h new file mode 100644 index 0000000..3fcfb9d --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/utilsx2.h @@ -0,0 +1,28 @@ +#ifndef SPX_UTILSX2_H +#define SPX_UTILSX2_H + +#include +#include "params.h" + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + * + * This implementation uses SIMD to compute internal nodes 2 at a time (in + * parallel) + */ +#define treehashx2 SPX_NAMESPACE(treehashx2) +void treehashx2(unsigned char *root, unsigned char *auth_path, + const spx_ctx *ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leafx2)( + unsigned char* /* Where to write the leaves */, + const spx_ctx* /* ctx */, + uint32_t addr_idx, void *info), + uint32_t tree_addrx2[2*8], void *info); + +#endif diff --git a/src/crypto/pq/_s/shake-a64/wots.c b/src/crypto/pq/_s/shake-a64/wots.c new file mode 100644 index 0000000..ef0235b --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/wots.c @@ -0,0 +1,261 @@ +#include +#include + +#include "utils.h" +#include "utilsx2.h" +#include "hash.h" +#include "hashx2.h" +#include "thashx2.h" +#include "wots.h" +#include "wotsx2.h" +#include "address.h" +#include "params.h" + +// TODO clarify address expectations, and make them more uniform. +// TODO i.e. do we expect types to be set already? +// TODO and do we expect modifications or copies? + +/** + * Computes up the chains + */ +static void gen_chains( + unsigned char *out, + const unsigned char *in, + unsigned int start[SPX_WOTS_LEN], + unsigned int steps[SPX_WOTS_LEN], + const spx_ctx *ctx, + uint32_t addr[8]) +{ + uint32_t i, j, k, idx, watching; + int done; + unsigned char empty[SPX_N]; + unsigned char *bufs[4]; + uint32_t addrs[8*2]; + + int l; + uint16_t counts[SPX_WOTS_W] = { 0 }; + uint16_t idxs[SPX_WOTS_LEN]; + uint16_t total, newTotal; + + /* set addrs = {addr, addr} */ + for (j = 0; j < 2; j++) { + memcpy(addrs+j*8, addr, sizeof(uint32_t) * 8); + } + + /* Initialize out with the value at position 'start'. */ + memcpy(out, in, SPX_WOTS_LEN*SPX_N); + + /* Sort the chains in reverse order by steps using counting sort. */ + for (i = 0; i < SPX_WOTS_LEN; i++) { + counts[steps[i]]++; + } + total = 0; + for (l = SPX_WOTS_W - 1; l >= 0; l--) { + newTotal = counts[l] + total; + counts[l] = total; + total = newTotal; + } + for (i = 0; i < SPX_WOTS_LEN; i++) { + idxs[counts[steps[i]]] = i; + counts[steps[i]]++; + } + + /* We got our work cut out for us: do it! */ + for (i = 0; i < SPX_WOTS_LEN; i += 2) { + for (j = 0; j < 2 && i+j < SPX_WOTS_LEN; j++) { + idx = idxs[i+j]; + set_chain_addr(addrs+j*8, idx); + bufs[j] = out + SPX_N * idx; + } + + /* As the chains are sorted in reverse order, we know that the first + * chain is the longest and the last one is the shortest. We keep + * an eye on whether the last chain is done and then on the one before, + * et cetera. */ + watching = 1; + done = 0; + while (i + watching >= SPX_WOTS_LEN) { + bufs[watching] = &empty[0]; + watching--; + } + + for (k = 0;; k++) { + while (k == steps[idxs[i+watching]]) { + bufs[watching] = &empty[0]; + if (watching == 0) { + done = 1; + break; + } + watching--; + } + if (done) { + break; + } + for (j = 0; j < watching + 1; j++) { + set_hash_addr(addrs+j*8, k + start[idxs[i+j]]); + } + + thashx2(bufs[0], bufs[1], + bufs[0], bufs[1], 1, ctx, addrs); + } + } +} + +/** + * base_w algorithm as described in draft. + * Interprets an array of bytes as integers in base w. + * This only works when log_w is a divisor of 8. + */ +static void base_w(unsigned int *output, const int out_len, + const unsigned char *input) +{ + int in = 0; + int out = 0; + unsigned char total; + int bits = 0; + int consumed; + + for (consumed = 0; consumed < out_len; consumed++) { + if (bits == 0) { + total = input[in]; + in++; + bits += 8; + } + bits -= SPX_WOTS_LOGW; + output[out] = (total >> bits) & (SPX_WOTS_W - 1); + out++; + } +} + +/* Computes the WOTS+ checksum over a message (in base_w). */ +static void wots_checksum(unsigned int *csum_base_w, + const unsigned int *msg_base_w) +{ + unsigned int csum = 0; + unsigned char csum_bytes[(SPX_WOTS_LEN2 * SPX_WOTS_LOGW + 7) / 8]; + unsigned int i; + + /* Compute checksum. */ + for (i = 0; i < SPX_WOTS_LEN1; i++) { + csum += SPX_WOTS_W - 1 - msg_base_w[i]; + } + + /* Convert checksum to base_w. */ + /* Make sure expected empty zero bits are the least significant bits. */ + csum = csum << ((8 - ((SPX_WOTS_LEN2 * SPX_WOTS_LOGW) % 8)) % 8); + ull_to_bytes(csum_bytes, sizeof(csum_bytes), csum); + base_w(csum_base_w, SPX_WOTS_LEN2, csum_bytes); +} + +/* Takes a message and derives the matching chain lengths. */ +void chain_lengths(unsigned int *lengths, const unsigned char *msg) +{ + base_w(lengths, SPX_WOTS_LEN1, msg); + wots_checksum(lengths + SPX_WOTS_LEN1, lengths); +} + +/** + * Takes a WOTS signature and an n-byte message, computes a WOTS public key. + * + * Writes the computed public key to 'pk'. + */ +void wots_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *msg, + const spx_ctx *ctx, uint32_t addr[8]) +{ + unsigned int steps[SPX_WOTS_LEN]; + unsigned int start[SPX_WOTS_LEN]; + uint32_t i; + + chain_lengths(start, msg); + + for (i = 0; i < SPX_WOTS_LEN; i++) { + steps[i] = SPX_WOTS_W - 1 - start[i]; + } + + gen_chains(pk, sig, start, steps, ctx, addr); +} + +/* + * This generates 2 sequential WOTS public keys + * It also generates the WOTS signature if leaf_info indicates + * that we're signing with one of these WOTS keys + */ +void wots_gen_leafx2(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info) { + struct leaf_info_x2 *info = v_info; + uint32_t *leaf_addr = info->leaf_addr; + uint32_t *pk_addr = info->pk_addr; + unsigned int i, j, k; + unsigned char pk_buffer[ 2 * SPX_WOTS_BYTES ]; + unsigned wots_offset = SPX_WOTS_BYTES; + unsigned char *buffer; + uint32_t wots_k_mask; + unsigned wots_sign_index; + + if (((leaf_idx ^ info->wots_sign_leaf) & ~1) == 0) { + /* We're traversing the leaf that's signing; generate the WOTS */ + /* signature */ + wots_k_mask = 0; + wots_sign_index = info->wots_sign_leaf & 1; /* Which of of the 2 */ + /* slots do the signatures come from */ + } else { + /* Nope, we're just generating pk's; turn off the signature logic */ + wots_k_mask = ~0; + wots_sign_index = 0; + } + + for (j = 0; j < 2; j++) { + set_keypair_addr( leaf_addr + j*8, leaf_idx + j ); + set_keypair_addr( pk_addr + j*8, leaf_idx + j ); + } + + for (i = 0, buffer = pk_buffer; i < SPX_WOTS_LEN; i++, buffer += SPX_N) { + uint32_t wots_k = info->wots_steps[i] | wots_k_mask; /* Set wots_k to */ + /* the step if we're generating a signature, ~0 if we're not */ + + /* Start with the secret seed */ + for (j = 0; j < 2; j++) { + set_chain_addr(leaf_addr + j*8, i); + set_hash_addr(leaf_addr + j*8, 0); + set_type(leaf_addr + j*8, SPX_ADDR_TYPE_WOTSPRF); + } + prf_addrx2(buffer + 0*wots_offset, + buffer + 1*wots_offset, + ctx, leaf_addr); + for (j = 0; j < 2; j++) { + set_type(leaf_addr + j*8, SPX_ADDR_TYPE_WOTS); + } + + /* Iterate down the WOTS chain */ + for (k=0;; k++) { + /* Check if one of the values we have needs to be saved as a */ + /* part of the WOTS signature */ + if (k == wots_k) { + memcpy( info->wots_sig + i * SPX_N, + buffer + wots_sign_index*wots_offset, SPX_N ); + } + + /* Check if we hit the top of the chain */ + if (k == SPX_WOTS_W - 1) break; + + /* Iterate one step on all 4 chains */ + for (j = 0; j < 2; j++) { + set_hash_addr(leaf_addr + j*8, k); + } + thashx2(buffer + 0*wots_offset, + buffer + 1*wots_offset, + buffer + 0*wots_offset, + buffer + 1*wots_offset, + 1, ctx, leaf_addr); + } + } + + /* Do the final thash to generate the public keys */ + thashx2(dest + 0*SPX_N, + dest + 1*SPX_N, + pk_buffer + 0*wots_offset, + pk_buffer + 1*wots_offset, + SPX_WOTS_LEN, ctx, pk_addr); +} diff --git a/src/crypto/pq/_s/shake-a64/wots.h b/src/crypto/pq/_s/shake-a64/wots.h new file mode 120000 index 0000000..8c327ea --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/wots.h @@ -0,0 +1 @@ +../ref/wots.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-a64/wotsx2.h b/src/crypto/pq/_s/shake-a64/wotsx2.h new file mode 100644 index 0000000..6237737 --- /dev/null +++ b/src/crypto/pq/_s/shake-a64/wotsx2.h @@ -0,0 +1,40 @@ +#if !defined( WOTSX2_H_ ) +#define WOTSX2_H_ + +#include +#include "params.h" + +/* + * This is here to provide an interface to the internal wots_gen_leafx2 + * routine. While this routine is not referenced in the package outside of + * wots.c, it is called from the stand-alone benchmark code to characterize + * the performance + */ +struct leaf_info_x2 { + unsigned char *wots_sig; + uint32_t wots_sign_leaf; /* The index of the WOTS we're using to sign */ + uint32_t *wots_steps; + uint32_t leaf_addr[2*8]; + uint32_t pk_addr[2*8]; +}; + +/* Macro to set the leaf_info to something 'benign', that is, it would */ +/* run with the same time as it does during the real signing process */ +/* Used only by the benchmark code */ +#define INITIALIZE_LEAF_INFO_X2(info, addr, step_buffer) { \ + info.wots_sig = 0; \ + info.wots_sign_leaf = ~0; \ + info.wots_steps = step_buffer; \ + int i; \ + for (i=0; i<2; i++) { \ + memcpy( &info.leaf_addr[8*i], addr, 32 ); \ + memcpy( &info.pk_addr[8*i], addr, 32 ); \ + } \ +} + +#define wots_gen_leafx2 SPX_NAMESPACE(wots_gen_leafx2) +void wots_gen_leafx2(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info); + +#endif /* WOTSX2_H_ */ diff --git a/src/crypto/pq/_s/shake-avx2/.gitignore b/src/crypto/pq/_s/shake-avx2/.gitignore new file mode 100644 index 0000000..20d83ac --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/.gitignore @@ -0,0 +1,6 @@ +test/* +!test/*.c +PQCsignKAT_*.rsp +PQCsignKAT_*.req +PQCgenKAT_sign +keccak4x/KeccakP-1600-times4-SIMD256.o \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/PQCgenKAT_sign.c b/src/crypto/pq/_s/shake-avx2/PQCgenKAT_sign.c new file mode 120000 index 0000000..a17dbe2 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/PQCgenKAT_sign.c @@ -0,0 +1 @@ +../ref/PQCgenKAT_sign.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/address.c b/src/crypto/pq/_s/shake-avx2/address.c new file mode 120000 index 0000000..02c52c6 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/address.c @@ -0,0 +1 @@ +../ref/address.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/address.h b/src/crypto/pq/_s/shake-avx2/address.h new file mode 120000 index 0000000..e670da5 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/address.h @@ -0,0 +1 @@ +../ref/address.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/api.h b/src/crypto/pq/_s/shake-avx2/api.h new file mode 120000 index 0000000..ea89e0a --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/api.h @@ -0,0 +1 @@ +../ref/api.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/context.h b/src/crypto/pq/_s/shake-avx2/context.h new file mode 100644 index 0000000..993c9ce --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/context.h @@ -0,0 +1,13 @@ +#ifndef SPX_CONTEXT_H +#define SPX_CONTEXT_H + +#include + +#include "params.h" + +typedef struct { + uint8_t pub_seed[SPX_N]; + uint8_t sk_seed[SPX_N]; +} spx_ctx; + +#endif diff --git a/src/crypto/pq/_s/shake-avx2/fips202.c b/src/crypto/pq/_s/shake-avx2/fips202.c new file mode 120000 index 0000000..da2fa42 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/fips202.c @@ -0,0 +1 @@ +../ref/fips202.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/fips202.h b/src/crypto/pq/_s/shake-avx2/fips202.h new file mode 120000 index 0000000..c759415 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/fips202.h @@ -0,0 +1 @@ +../ref/fips202.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/fips202x4.c b/src/crypto/pq/_s/shake-avx2/fips202x4.c new file mode 100644 index 0000000..b875467 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/fips202x4.c @@ -0,0 +1,225 @@ +#include +#include +#include + +#include "fips202.h" +#include "fips202x4.h" + +#define NROUNDS 24 +#define ROL(a, offset) ((a << offset) ^ (a >> (64-offset))) + +static uint64_t load64(const unsigned char *x) +{ + unsigned long long r = 0, i; + + for (i = 0; i < 8; ++i) { + r |= (unsigned long long)x[i] << 8 * i; + } + return r; +} + +static void store64(uint8_t *x, uint64_t u) +{ + unsigned int i; + + for(i=0; i<8; ++i) { + x[i] = u; + u >>= 8; + } +} + +/* Use implementation from the Keccak Code Package */ +extern void KeccakP1600times4_PermuteAll_24rounds(__m256i *s); +#define KeccakF1600_StatePermute4x KeccakP1600times4_PermuteAll_24rounds + +static void keccak_absorb4x(__m256i *s, + unsigned int r, + const unsigned char *m0, + const unsigned char *m1, + const unsigned char *m2, + const unsigned char *m3, + unsigned long long int mlen, + unsigned char p) +{ + unsigned long long i; + unsigned char t0[200]; + unsigned char t1[200]; + unsigned char t2[200]; + unsigned char t3[200]; + + unsigned long long *ss = (unsigned long long *)s; + + + while (mlen >= r) + { + for (i = 0; i < r / 8; ++i) + { + ss[4*i+0] ^= load64(m0 + 8 * i); + ss[4*i+1] ^= load64(m1 + 8 * i); + ss[4*i+2] ^= load64(m2 + 8 * i); + ss[4*i+3] ^= load64(m3 + 8 * i); + } + + KeccakF1600_StatePermute4x(s); + mlen -= r; + m0 += r; + m1 += r; + m2 += r; + m3 += r; + } + + for (i = 0; i < r; ++i) + { + t0[i] = 0; + t1[i] = 0; + t2[i] = 0; + t3[i] = 0; + } + for (i = 0; i < mlen; ++i) + { + t0[i] = m0[i]; + t1[i] = m1[i]; + t2[i] = m2[i]; + t3[i] = m3[i]; + } + + t0[i] = p; + t1[i] = p; + t2[i] = p; + t3[i] = p; + + t0[r - 1] |= 128; + t1[r - 1] |= 128; + t2[r - 1] |= 128; + t3[r - 1] |= 128; + + for (i = 0; i < r / 8; ++i) + { + ss[4*i+0] ^= load64(t0 + 8 * i); + ss[4*i+1] ^= load64(t1 + 8 * i); + ss[4*i+2] ^= load64(t2 + 8 * i); + ss[4*i+3] ^= load64(t3 + 8 * i); + } +} + + +static void keccak_squeezeblocks4x(unsigned char *h0, + unsigned char *h1, + unsigned char *h2, + unsigned char *h3, + unsigned long long int nblocks, + __m256i *s, + unsigned int r) +{ + unsigned int i; + + unsigned long long *ss = (unsigned long long *)s; + + while(nblocks > 0) + { + KeccakF1600_StatePermute4x(s); + for(i=0;i<(r>>3);i++) + { + store64(h0+8*i, ss[4*i+0]); + store64(h1+8*i, ss[4*i+1]); + store64(h2+8*i, ss[4*i+2]); + store64(h3+8*i, ss[4*i+3]); + } + h0 += r; + h1 += r; + h2 += r; + h3 += r; + nblocks--; + } +} + + + +void shake128x4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, unsigned long long outlen, + unsigned char *in0, + unsigned char *in1, + unsigned char *in2, + unsigned char *in3, unsigned long long inlen) +{ + __m256i s[25]; + unsigned char t0[SHAKE128_RATE]; + unsigned char t1[SHAKE128_RATE]; + unsigned char t2[SHAKE128_RATE]; + unsigned char t3[SHAKE128_RATE]; + unsigned int i; + + /* zero state */ + for(i=0;i<25;i++) + s[i] = _mm256_xor_si256(s[i], s[i]); + + /* absorb 4 message of identical length in parallel */ + keccak_absorb4x(s, SHAKE128_RATE, in0, in1, in2, in3, inlen, 0x1F); + + /* Squeeze output */ + keccak_squeezeblocks4x(out0, out1, out2, out3, outlen/SHAKE128_RATE, s, SHAKE128_RATE); + + out0 += (outlen/SHAKE128_RATE)*SHAKE128_RATE; + out1 += (outlen/SHAKE128_RATE)*SHAKE128_RATE; + out2 += (outlen/SHAKE128_RATE)*SHAKE128_RATE; + out3 += (outlen/SHAKE128_RATE)*SHAKE128_RATE; + + if(outlen%SHAKE128_RATE) + { + keccak_squeezeblocks4x(t0, t1, t2, t3, 1, s, SHAKE128_RATE); + for(i=0;i + +void shake128x4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, unsigned long long outlen, + unsigned char *in0, + unsigned char *in1, + unsigned char *in2, + unsigned char *in3, unsigned long long inlen); + +void shake256x4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, unsigned long long outlen, + unsigned char *in0, + unsigned char *in1, + unsigned char *in2, + unsigned char *in3, unsigned long long inlen); + +#endif diff --git a/src/crypto/pq/_s/shake-avx2/fors.c b/src/crypto/pq/_s/shake-avx2/fors.c new file mode 100644 index 0000000..8b634f4 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/fors.c @@ -0,0 +1,209 @@ +#include +#include +#include + +#include "fors.h" +#include "utils.h" +#include "utilsx4.h" +#include "hash.h" +#include "hashx4.h" +#include "thash.h" +#include "thashx4.h" +#include "address.h" + +static void fors_gen_sk(unsigned char *sk, const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + prf_addr(sk, ctx, fors_leaf_addr); +} + +static void fors_gen_skx4(unsigned char *sk0, + unsigned char *sk1, + unsigned char *sk2, + unsigned char *sk3, const spx_ctx *ctx, + uint32_t fors_leaf_addrx4[4*8]) +{ + prf_addrx4(sk0, sk1, sk2, sk3, + ctx, fors_leaf_addrx4); +} + +static void fors_sk_to_leaf(unsigned char *leaf, const unsigned char *sk, + const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + thash(leaf, sk, 1, ctx, fors_leaf_addr); +} + +static void fors_sk_to_leafx4(unsigned char *leaf0, + unsigned char *leaf1, + unsigned char *leaf2, + unsigned char *leaf3, + const unsigned char *sk0, + const unsigned char *sk1, + const unsigned char *sk2, + const unsigned char *sk3, + const spx_ctx *ctx, + uint32_t fors_leaf_addrx4[4*8]) +{ + thashx4(leaf0, leaf1, leaf2, leaf3, + sk0, sk1, sk2, sk3, + 1, ctx, fors_leaf_addrx4); +} + +struct fors_gen_leaf_info { + uint32_t leaf_addrx[4*8]; +}; + +static void fors_gen_leafx4(unsigned char *leaf, + const spx_ctx *ctx, + uint32_t addr_idx, void *info) +{ + struct fors_gen_leaf_info *fors_info = info; + uint32_t *fors_leaf_addrx4 = fors_info->leaf_addrx; + unsigned int j; + + /* Only set the parts that the caller doesn't set */ + for (j = 0; j < 4; j++) { + set_tree_index(fors_leaf_addrx4 + j*8, addr_idx + j); + set_type(fors_leaf_addrx4 + j*8, SPX_ADDR_TYPE_FORSPRF); + } + + fors_gen_skx4(leaf + 0*SPX_N, + leaf + 1*SPX_N, + leaf + 2*SPX_N, + leaf + 3*SPX_N, + ctx, fors_leaf_addrx4); + + for (j = 0; j < 4; j++) { + set_type(fors_leaf_addrx4 + j*8, SPX_ADDR_TYPE_FORSTREE); + } + + fors_sk_to_leafx4(leaf + 0*SPX_N, + leaf + 1*SPX_N, + leaf + 2*SPX_N, + leaf + 3*SPX_N, + leaf + 0*SPX_N, + leaf + 1*SPX_N, + leaf + 2*SPX_N, + leaf + 3*SPX_N, + ctx, fors_leaf_addrx4); +} + +/** + * Interprets m as SPX_FORS_HEIGHT-bit unsigned integers. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + * Assumes indices has space for SPX_FORS_TREES integers. + */ +static void message_to_indices(uint32_t *indices, const unsigned char *m) +{ + unsigned int i, j; + unsigned int offset = 0; + + for (i = 0; i < SPX_FORS_TREES; i++) { + indices[i] = 0; + for (j = 0; j < SPX_FORS_HEIGHT; j++) { + indices[i] ^= ((m[offset >> 3] >> (offset & 0x7)) & 0x1) << j; + offset++; + } + } +} + +/** + * Signs a message m, deriving the secret key from sk_seed and the FTS address. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_sign(unsigned char *sig, unsigned char *pk, + const unsigned char *m, + const spx_ctx *ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + uint32_t fors_tree_addr[4*8] = {0}; + struct fors_gen_leaf_info fors_info = {0}; + uint32_t *fors_leaf_addr = fors_info.leaf_addrx; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + for (i=0; i<4; i++) { + copy_keypair_addr(fors_tree_addr + 8*i, fors_addr); + set_type(fors_tree_addr + 8*i, SPX_ADDR_TYPE_FORSTREE); + copy_keypair_addr(fors_leaf_addr + 8*i, fors_addr); + } + copy_keypair_addr(fors_pk_addr, fors_addr); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + + /* Include the secret key part that produces the selected leaf node. */ + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSPRF); + fors_gen_sk(sig, ctx, fors_tree_addr); + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + sig += SPX_N; + + /* Compute the authentication path for this leaf node. */ + treehashx4(roots + i*SPX_N, sig, ctx, + indices[i], idx_offset, SPX_FORS_HEIGHT, fors_gen_leafx4, + fors_tree_addr, &fors_info); + + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} + +/** + * Derives the FORS public key from a signature. + * This can be used for verification by comparing to a known public key, or to + * subsequently verify a signature on the derived public key. The latter is the + * typical use-case when used as an FTS below an OTS in a hypertree. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *m, + const spx_ctx *ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + unsigned char leaf[SPX_N]; + uint32_t fors_tree_addr[8] = {0}; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + copy_keypair_addr(fors_tree_addr, fors_addr); + copy_keypair_addr(fors_pk_addr, fors_addr); + + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + + /* Derive the leaf from the included secret key part. */ + fors_sk_to_leaf(leaf, sig, ctx, fors_tree_addr); + sig += SPX_N; + + /* Derive the corresponding root node of this tree. */ + compute_root(roots + i*SPX_N, leaf, indices[i], idx_offset, + sig, SPX_FORS_HEIGHT, ctx, fors_tree_addr); + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} diff --git a/src/crypto/pq/_s/shake-avx2/fors.h b/src/crypto/pq/_s/shake-avx2/fors.h new file mode 120000 index 0000000..07156bd --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/fors.h @@ -0,0 +1 @@ +../ref/fors.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/hash.h b/src/crypto/pq/_s/shake-avx2/hash.h new file mode 120000 index 0000000..cffc52b --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/hash.h @@ -0,0 +1 @@ +../ref/hash.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/hash_shake.c b/src/crypto/pq/_s/shake-avx2/hash_shake.c new file mode 120000 index 0000000..1d9d0cd --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/hash_shake.c @@ -0,0 +1 @@ +../ref/hash_shake.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/hash_shakex4.c b/src/crypto/pq/_s/shake-avx2/hash_shakex4.c new file mode 100644 index 0000000..93ee3cc --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/hash_shakex4.c @@ -0,0 +1,63 @@ +#include +#include + +#include "address.h" +#include "params.h" +#include "fips202x4.h" +#include "hashx4.h" + +extern void KeccakP1600times4_PermuteAll_24rounds(__m256i *s); + +/* + * 4-way parallel version of prf_addr; takes 4x as much input and output + */ +void prf_addrx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const spx_ctx *ctx, + const uint32_t addrx4[4*8]) { + /* As we write and read only a few quadwords, it is more efficient to + * build and extract from the fourway SHAKE256 state by hand. */ + __m256i state[25]; + + for (int i = 0; i < SPX_N/8; i++) { + state[i] = _mm256_set1_epi64x(((int64_t*)ctx->pub_seed)[i]); + } + for (int i = 0; i < 4; i++) { + state[SPX_N/8+i] = _mm256_set_epi32( + addrx4[3*8+1+2*i], + addrx4[3*8+2*i], + addrx4[2*8+1+2*i], + addrx4[2*8+2*i], + addrx4[8+1+2*i], + addrx4[8+2*i], + addrx4[1+2*i], + addrx4[2*i] + ); + } + for (int i = 0; i < SPX_N/8; i++) { + state[SPX_N/8+i+4] = _mm256_set1_epi64x(((int64_t*)ctx->sk_seed)[i]); + } + + /* SHAKE domain separator and padding. */ + state[SPX_N/4+4] = _mm256_set1_epi64x(0x1f); + for (int i = SPX_N/4+5; i < 16; i++) { + state[i] = _mm256_set1_epi64x(0); + } + // shift unsigned and then cast to avoid UB + state[16] = _mm256_set1_epi64x((long long)(0x80ULL << 56)); + + for (int i = 17; i < 25; i++) { + state[i] = _mm256_set1_epi64x(0); + } + + KeccakP1600times4_PermuteAll_24rounds(&state[0]); + + for (int i = 0; i < SPX_N/8; i++) { + ((int64_t*)out0)[i] = _mm256_extract_epi64(state[i], 0); + ((int64_t*)out1)[i] = _mm256_extract_epi64(state[i], 1); + ((int64_t*)out2)[i] = _mm256_extract_epi64(state[i], 2); + ((int64_t*)out3)[i] = _mm256_extract_epi64(state[i], 3); + } +} diff --git a/src/crypto/pq/_s/shake-avx2/hashx4.h b/src/crypto/pq/_s/shake-avx2/hashx4.h new file mode 100644 index 0000000..1c19618 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/hashx4.h @@ -0,0 +1,16 @@ +#ifndef SPX_HASHX4_H +#define SPX_HASHX4_H + +#include +#include "context.h" +#include "params.h" + +#define prf_addrx4 SPX_NAMESPACE(prf_addrx4) +void prf_addrx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const spx_ctx *ctx, + const uint32_t addrx4[4*8]); + +#endif diff --git a/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-times4-SIMD256.c b/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-times4-SIMD256.c new file mode 100644 index 0000000..7a0428f --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-times4-SIMD256.c @@ -0,0 +1,1030 @@ +/* +Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni, +Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby +denoted as "the implementer". + +For more information, feedback or questions, please refer to our websites: +http://keccak.noekeon.org/ +http://keyak.noekeon.org/ +http://ketje.noekeon.org/ + +To the extent possible under law, the implementer has waived all copyright +and related or neighboring rights to the source code in this file. +http://creativecommons.org/publicdomain/zero/1.0/ +*/ + +#include +#include +#include +#include +#include +#include +#include +#include "align.h" +#include "KeccakP-1600-times4-SnP.h" +#include "SIMD256-config.h" + +#include "brg_endian.h" +#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN) +#error Expecting a little-endian platform +#endif + +typedef unsigned char UINT8; +typedef unsigned long long int UINT64; +typedef __m128i V128; +typedef __m256i V256; + +#define laneIndex(instanceIndex, lanePosition) ((lanePosition)*4 + instanceIndex) + +#if defined(KeccakP1600times4_useAVX2) + #define ANDnu256(a, b) _mm256_andnot_si256(a, b) + #define CONST256(a) _mm256_load_si256((const V256 *)&(a)) + #define CONST256_64(a) (V256)_mm256_broadcast_sd((const double*)(&a)) + #define LOAD256(a) _mm256_load_si256((const V256 *)&(a)) + #define LOAD256u(a) _mm256_loadu_si256((const V256 *)&(a)) + #define LOAD4_64(a, b, c, d) _mm256_set_epi64x((UINT64)(a), (UINT64)(b), (UINT64)(c), (UINT64)(d)) + #define ROL64in256(d, a, o) d = _mm256_or_si256(_mm256_slli_epi64(a, o), _mm256_srli_epi64(a, 64-(o))) + #define ROL64in256_8(d, a) d = _mm256_shuffle_epi8(a, CONST256(rho8)) + #define ROL64in256_56(d, a) d = _mm256_shuffle_epi8(a, CONST256(rho56)) +static const UINT64 rho8[4] = {0x0605040302010007, 0x0E0D0C0B0A09080F, 0x1615141312111017, 0x1E1D1C1B1A19181F}; +static const UINT64 rho56[4] = {0x0007060504030201, 0x080F0E0D0C0B0A09, 0x1017161514131211, 0x181F1E1D1C1B1A19}; + #define STORE256(a, b) _mm256_store_si256((V256 *)&(a), b) + #define STORE256u(a, b) _mm256_storeu_si256((V256 *)&(a), b) + #define STORE2_128(ah, al, v) _mm256_storeu2_m128d((V128*)&(ah), (V128*)&(al), v) + #define XOR256(a, b) _mm256_xor_si256(a, b) + #define XOReq256(a, b) a = _mm256_xor_si256(a, b) + #define UNPACKL( a, b ) _mm256_unpacklo_epi64((a), (b)) + #define UNPACKH( a, b ) _mm256_unpackhi_epi64((a), (b)) + #define PERM128( a, b, c ) (V256)_mm256_permute2f128_ps((__m256)(a), (__m256)(b), c) + #define SHUFFLE64( a, b, c ) (V256)_mm256_shuffle_pd((__m256d)(a), (__m256d)(b), c) + + #define UNINTLEAVE() lanesL01 = UNPACKL( lanes0, lanes1 ), \ + lanesH01 = UNPACKH( lanes0, lanes1 ), \ + lanesL23 = UNPACKL( lanes2, lanes3 ), \ + lanesH23 = UNPACKH( lanes2, lanes3 ), \ + lanes0 = PERM128( lanesL01, lanesL23, 0x20 ), \ + lanes2 = PERM128( lanesL01, lanesL23, 0x31 ), \ + lanes1 = PERM128( lanesH01, lanesH23, 0x20 ), \ + lanes3 = PERM128( lanesH01, lanesH23, 0x31 ) + + #define INTLEAVE() lanesL01 = PERM128( lanes0, lanes2, 0x20 ), \ + lanesH01 = PERM128( lanes1, lanes3, 0x20 ), \ + lanesL23 = PERM128( lanes0, lanes2, 0x31 ), \ + lanesH23 = PERM128( lanes1, lanes3, 0x31 ), \ + lanes0 = SHUFFLE64( lanesL01, lanesH01, 0x00 ), \ + lanes1 = SHUFFLE64( lanesL01, lanesH01, 0x0F ), \ + lanes2 = SHUFFLE64( lanesL23, lanesH23, 0x00 ), \ + lanes3 = SHUFFLE64( lanesL23, lanesH23, 0x0F ) + +#endif + +#define SnP_laneLengthInBytes 8 + +void KeccakP1600times4_InitializeAll(void *states) +{ + memset(states, 0, KeccakP1600times4_statesSizeInBytes); +} + +void KeccakP1600times4_AddBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length) +{ + unsigned int sizeLeft = length; + unsigned int lanePosition = offset/SnP_laneLengthInBytes; + unsigned int offsetInLane = offset%SnP_laneLengthInBytes; + const unsigned char *curData = data; + UINT64 *statesAsLanes = (UINT64 *)states; + + if ((sizeLeft > 0) && (offsetInLane != 0)) { + unsigned int bytesInLane = SnP_laneLengthInBytes - offsetInLane; + UINT64 lane = 0; + if (bytesInLane > sizeLeft) + bytesInLane = sizeLeft; + memcpy((unsigned char*)&lane + offsetInLane, curData, bytesInLane); + statesAsLanes[laneIndex(instanceIndex, lanePosition)] ^= lane; + sizeLeft -= bytesInLane; + lanePosition++; + curData += bytesInLane; + } + + while(sizeLeft >= SnP_laneLengthInBytes) { + UINT64 lane = *((const UINT64*)curData); + statesAsLanes[laneIndex(instanceIndex, lanePosition)] ^= lane; + sizeLeft -= SnP_laneLengthInBytes; + lanePosition++; + curData += SnP_laneLengthInBytes; + } + + if (sizeLeft > 0) { + UINT64 lane = 0; + memcpy(&lane, curData, sizeLeft); + statesAsLanes[laneIndex(instanceIndex, lanePosition)] ^= lane; + } +} + +void KeccakP1600times4_AddLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset) +{ + V256 *stateAsLanes = (V256 *)states; + unsigned int i; + const UINT64 *curData0 = (const UINT64 *)data; + const UINT64 *curData1 = (const UINT64 *)(data+laneOffset*SnP_laneLengthInBytes); + const UINT64 *curData2 = (const UINT64 *)(data+laneOffset*2*SnP_laneLengthInBytes); + const UINT64 *curData3 = (const UINT64 *)(data+laneOffset*3*SnP_laneLengthInBytes); + V256 lanes0, lanes1, lanes2, lanes3, lanesL01, lanesL23, lanesH01, lanesH23; + + #define Xor_In( argIndex ) XOReq256(stateAsLanes[argIndex], LOAD4_64(curData3[argIndex], curData2[argIndex], curData1[argIndex], curData0[argIndex])) + + #define Xor_In4( argIndex ) lanes0 = LOAD256u( curData0[argIndex]),\ + lanes1 = LOAD256u( curData1[argIndex]),\ + lanes2 = LOAD256u( curData2[argIndex]),\ + lanes3 = LOAD256u( curData3[argIndex]),\ + INTLEAVE(),\ + XOReq256( stateAsLanes[argIndex+0], lanes0 ),\ + XOReq256( stateAsLanes[argIndex+1], lanes1 ),\ + XOReq256( stateAsLanes[argIndex+2], lanes2 ),\ + XOReq256( stateAsLanes[argIndex+3], lanes3 ) + + if ( laneCount >= 16 ) { + Xor_In4( 0 ); + Xor_In4( 4 ); + Xor_In4( 8 ); + Xor_In4( 12 ); + if ( laneCount >= 20 ) { + Xor_In4( 16 ); + for(i=20; i 0) && (offsetInLane != 0)) { + unsigned int bytesInLane = SnP_laneLengthInBytes - offsetInLane; + if (bytesInLane > sizeLeft) + bytesInLane = sizeLeft; + memcpy( ((unsigned char *)&statesAsLanes[laneIndex(instanceIndex, lanePosition)]) + offsetInLane, curData, bytesInLane); + sizeLeft -= bytesInLane; + lanePosition++; + curData += bytesInLane; + } + + while(sizeLeft >= SnP_laneLengthInBytes) { + UINT64 lane = *((const UINT64*)curData); + statesAsLanes[laneIndex(instanceIndex, lanePosition)] = lane; + sizeLeft -= SnP_laneLengthInBytes; + lanePosition++; + curData += SnP_laneLengthInBytes; + } + + if (sizeLeft > 0) { + memcpy(&statesAsLanes[laneIndex(instanceIndex, lanePosition)], curData, sizeLeft); + } +} + +void KeccakP1600times4_OverwriteLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset) +{ + V256 *stateAsLanes = (V256 *)states; + unsigned int i; + const UINT64 *curData0 = (const UINT64 *)data; + const UINT64 *curData1 = (const UINT64 *)(data+laneOffset*SnP_laneLengthInBytes); + const UINT64 *curData2 = (const UINT64 *)(data+laneOffset*2*SnP_laneLengthInBytes); + const UINT64 *curData3 = (const UINT64 *)(data+laneOffset*3*SnP_laneLengthInBytes); + V256 lanes0, lanes1, lanes2, lanes3, lanesL01, lanesL23, lanesH01, lanesH23; + + #define OverWr( argIndex ) STORE256(stateAsLanes[argIndex], LOAD4_64(curData3[argIndex], curData2[argIndex], curData1[argIndex], curData0[argIndex])) + + #define OverWr4( argIndex ) lanes0 = LOAD256u( curData0[argIndex]),\ + lanes1 = LOAD256u( curData1[argIndex]),\ + lanes2 = LOAD256u( curData2[argIndex]),\ + lanes3 = LOAD256u( curData3[argIndex]),\ + INTLEAVE(),\ + STORE256( stateAsLanes[argIndex+0], lanes0 ),\ + STORE256( stateAsLanes[argIndex+1], lanes1 ),\ + STORE256( stateAsLanes[argIndex+2], lanes2 ),\ + STORE256( stateAsLanes[argIndex+3], lanes3 ) + + if ( laneCount >= 16 ) { + OverWr4( 0 ); + OverWr4( 4 ); + OverWr4( 8 ); + OverWr4( 12 ); + if ( laneCount >= 20 ) { + OverWr4( 16 ); + for(i=20; i= SnP_laneLengthInBytes) { + statesAsLanes[laneIndex(instanceIndex, lanePosition)] = 0; + sizeLeft -= SnP_laneLengthInBytes; + lanePosition++; + } + + if (sizeLeft > 0) { + memset(&statesAsLanes[laneIndex(instanceIndex, lanePosition)], 0, sizeLeft); + } +} + +void KeccakP1600times4_ExtractBytes(const void *states, unsigned int instanceIndex, unsigned char *data, unsigned int offset, unsigned int length) +{ + unsigned int sizeLeft = length; + unsigned int lanePosition = offset/SnP_laneLengthInBytes; + unsigned int offsetInLane = offset%SnP_laneLengthInBytes; + unsigned char *curData = data; + const UINT64 *statesAsLanes = (const UINT64 *)states; + + if ((sizeLeft > 0) && (offsetInLane != 0)) { + unsigned int bytesInLane = SnP_laneLengthInBytes - offsetInLane; + if (bytesInLane > sizeLeft) + bytesInLane = sizeLeft; + memcpy( curData, ((unsigned char *)&statesAsLanes[laneIndex(instanceIndex, lanePosition)]) + offsetInLane, bytesInLane); + sizeLeft -= bytesInLane; + lanePosition++; + curData += bytesInLane; + } + + while(sizeLeft >= SnP_laneLengthInBytes) { + *(UINT64*)curData = statesAsLanes[laneIndex(instanceIndex, lanePosition)]; + sizeLeft -= SnP_laneLengthInBytes; + lanePosition++; + curData += SnP_laneLengthInBytes; + } + + if (sizeLeft > 0) { + memcpy( curData, &statesAsLanes[laneIndex(instanceIndex, lanePosition)], sizeLeft); + } +} + +void KeccakP1600times4_ExtractLanesAll(const void *states, unsigned char *data, unsigned int laneCount, unsigned int laneOffset) +{ + UINT64 *curData0 = (UINT64 *)data; + UINT64 *curData1 = (UINT64 *)(data+laneOffset*1*SnP_laneLengthInBytes); + UINT64 *curData2 = (UINT64 *)(data+laneOffset*2*SnP_laneLengthInBytes); + UINT64 *curData3 = (UINT64 *)(data+laneOffset*3*SnP_laneLengthInBytes); + + const V256 *stateAsLanes = (const V256 *)states; + const UINT64 *stateAsLanes64 = (const UINT64*)states; + V256 lanes0, lanes1, lanes2, lanes3, lanesL01, lanesL23, lanesH01, lanesH23; + unsigned int i; + + #define Extr( argIndex ) curData0[argIndex] = stateAsLanes64[4*(argIndex)], \ + curData1[argIndex] = stateAsLanes64[4*(argIndex)+1], \ + curData2[argIndex] = stateAsLanes64[4*(argIndex)+2], \ + curData3[argIndex] = stateAsLanes64[4*(argIndex)+3] + + #define Extr4( argIndex ) lanes0 = LOAD256( stateAsLanes[argIndex+0] ), \ + lanes1 = LOAD256( stateAsLanes[argIndex+1] ), \ + lanes2 = LOAD256( stateAsLanes[argIndex+2] ), \ + lanes3 = LOAD256( stateAsLanes[argIndex+3] ), \ + UNINTLEAVE(), \ + STORE256u( curData0[argIndex], lanes0 ), \ + STORE256u( curData1[argIndex], lanes1 ), \ + STORE256u( curData2[argIndex], lanes2 ), \ + STORE256u( curData3[argIndex], lanes3 ) + + if ( laneCount >= 16 ) { + Extr4( 0 ); + Extr4( 4 ); + Extr4( 8 ); + Extr4( 12 ); + if ( laneCount >= 20 ) { + Extr4( 16 ); + for(i=20; i 0) && (offsetInLane != 0)) { + unsigned int bytesInLane = SnP_laneLengthInBytes - offsetInLane; + UINT64 lane = statesAsLanes[laneIndex(instanceIndex, lanePosition)] >> (8 * offsetInLane); + if (bytesInLane > sizeLeft) + bytesInLane = sizeLeft; + sizeLeft -= bytesInLane; + do { + *(curOutput++) = *(curInput++) ^ (unsigned char)lane; + lane >>= 8; + } while ( --bytesInLane != 0); + lanePosition++; + } + + while(sizeLeft >= SnP_laneLengthInBytes) { + *((UINT64*)curOutput) = *((UINT64*)curInput) ^ statesAsLanes[laneIndex(instanceIndex, lanePosition)]; + sizeLeft -= SnP_laneLengthInBytes; + lanePosition++; + curInput += SnP_laneLengthInBytes; + curOutput += SnP_laneLengthInBytes; + } + + if (sizeLeft != 0) { + UINT64 lane = statesAsLanes[laneIndex(instanceIndex, lanePosition)]; + do { + *(curOutput++) = *(curInput++) ^ (unsigned char)lane; + lane >>= 8; + } while ( --sizeLeft != 0); + } +} + +void KeccakP1600times4_ExtractAndAddLanesAll(const void *states, const unsigned char *input, unsigned char *output, unsigned int laneCount, unsigned int laneOffset) +{ + const UINT64 *curInput0 = (UINT64 *)input; + const UINT64 *curInput1 = (UINT64 *)(input+laneOffset*1*SnP_laneLengthInBytes); + const UINT64 *curInput2 = (UINT64 *)(input+laneOffset*2*SnP_laneLengthInBytes); + const UINT64 *curInput3 = (UINT64 *)(input+laneOffset*3*SnP_laneLengthInBytes); + UINT64 *curOutput0 = (UINT64 *)output; + UINT64 *curOutput1 = (UINT64 *)(output+laneOffset*1*SnP_laneLengthInBytes); + UINT64 *curOutput2 = (UINT64 *)(output+laneOffset*2*SnP_laneLengthInBytes); + UINT64 *curOutput3 = (UINT64 *)(output+laneOffset*3*SnP_laneLengthInBytes); + + const V256 *stateAsLanes = (const V256 *)states; + const UINT64 *stateAsLanes64 = (const UINT64*)states; + V256 lanes0, lanes1, lanes2, lanes3, lanesL01, lanesL23, lanesH01, lanesH23; + unsigned int i; + + #define ExtrXor( argIndex ) \ + curOutput0[argIndex] = curInput0[argIndex] ^ stateAsLanes64[4*(argIndex)],\ + curOutput1[argIndex] = curInput1[argIndex] ^ stateAsLanes64[4*(argIndex)+1],\ + curOutput2[argIndex] = curInput2[argIndex] ^ stateAsLanes64[4*(argIndex)+2],\ + curOutput3[argIndex] = curInput3[argIndex] ^ stateAsLanes64[4*(argIndex)+3] + + #define ExtrXor4( argIndex ) \ + lanes0 = LOAD256( stateAsLanes[argIndex+0] ),\ + lanes1 = LOAD256( stateAsLanes[argIndex+1] ),\ + lanes2 = LOAD256( stateAsLanes[argIndex+2] ),\ + lanes3 = LOAD256( stateAsLanes[argIndex+3] ),\ + UNINTLEAVE(),\ + lanesL01 = LOAD256u( curInput0[argIndex]),\ + lanesH01 = LOAD256u( curInput1[argIndex]),\ + lanesL23 = LOAD256u( curInput2[argIndex]),\ + lanesH23 = LOAD256u( curInput3[argIndex]),\ + XOReq256( lanes0, lanesL01 ),\ + XOReq256( lanes1, lanesH01 ),\ + XOReq256( lanes2, lanesL23 ),\ + XOReq256( lanes3, lanesH23 ),\ + STORE256u( curOutput0[argIndex], lanes0 ),\ + STORE256u( curOutput1[argIndex], lanes1 ),\ + STORE256u( curOutput2[argIndex], lanes2 ),\ + STORE256u( curOutput3[argIndex], lanes3 ) + + if ( laneCount >= 16 ) { + ExtrXor4( 0 ); + ExtrXor4( 4 ); + ExtrXor4( 8 ); + ExtrXor4( 12 ); + if ( laneCount >= 20 ) { + ExtrXor4( 16 ); + for(i=20; i= (laneOffsetParallel*3 + laneCount)*8) { + V256 *stateAsLanes = (V256 *)states; + V256 lanes0, lanes1, lanes2, lanes3, lanesL01, lanesL23, lanesH01, lanesH23; + #define Xor_In( argIndex ) \ + XOReq256(stateAsLanes[argIndex], LOAD4_64(curData3[argIndex], curData2[argIndex], curData1[argIndex], curData0[argIndex])) + #define Xor_In4( argIndex ) \ + lanes0 = LOAD256u( curData0[argIndex]),\ + lanes1 = LOAD256u( curData1[argIndex]),\ + lanes2 = LOAD256u( curData2[argIndex]),\ + lanes3 = LOAD256u( curData3[argIndex]),\ + INTLEAVE(),\ + XOReq256( stateAsLanes[argIndex+0], lanes0 ),\ + XOReq256( stateAsLanes[argIndex+1], lanes1 ),\ + XOReq256( stateAsLanes[argIndex+2], lanes2 ),\ + XOReq256( stateAsLanes[argIndex+3], lanes3 ) + Xor_In4( 0 ); + Xor_In4( 4 ); + Xor_In4( 8 ); + Xor_In4( 12 ); + Xor_In4( 16 ); + Xor_In( 20 ); + #undef Xor_In + #undef Xor_In4 + KeccakP1600times4_PermuteAll_24rounds(states); + curData0 += laneOffsetSerial; + curData1 += laneOffsetSerial; + curData2 += laneOffsetSerial; + curData3 += laneOffsetSerial; + dataByteLen -= laneOffsetSerial*8; + } + return (const unsigned char *)curData0 - dataStart; +#else +// unsigned int i; + const unsigned char *dataStart = data; + const UINT64 *curData0 = (const UINT64 *)data; + const UINT64 *curData1 = (const UINT64 *)(data+laneOffsetParallel*1*SnP_laneLengthInBytes); + const UINT64 *curData2 = (const UINT64 *)(data+laneOffsetParallel*2*SnP_laneLengthInBytes); + const UINT64 *curData3 = (const UINT64 *)(data+laneOffsetParallel*3*SnP_laneLengthInBytes); + V256 *statesAsLanes = (V256 *)states; + declareABCDE + + copyFromState(A, statesAsLanes) + while(dataByteLen >= (laneOffsetParallel*3 + laneCount)*8) { + #define XOR_In( Xxx, argIndex ) \ + XOReq256(Xxx, LOAD4_64(curData3[argIndex], curData2[argIndex], curData1[argIndex], curData0[argIndex])) + XOR_In( Aba, 0 ); + XOR_In( Abe, 1 ); + XOR_In( Abi, 2 ); + XOR_In( Abo, 3 ); + XOR_In( Abu, 4 ); + XOR_In( Aga, 5 ); + XOR_In( Age, 6 ); + XOR_In( Agi, 7 ); + XOR_In( Ago, 8 ); + XOR_In( Agu, 9 ); + XOR_In( Aka, 10 ); + XOR_In( Ake, 11 ); + XOR_In( Aki, 12 ); + XOR_In( Ako, 13 ); + XOR_In( Aku, 14 ); + XOR_In( Ama, 15 ); + XOR_In( Ame, 16 ); + XOR_In( Ami, 17 ); + XOR_In( Amo, 18 ); + XOR_In( Amu, 19 ); + XOR_In( Asa, 20 ); + #undef XOR_In + rounds24 + curData0 += laneOffsetSerial; + curData1 += laneOffsetSerial; + curData2 += laneOffsetSerial; + curData3 += laneOffsetSerial; + dataByteLen -= laneOffsetSerial*8; + } + copyToState(statesAsLanes, A) + return (const unsigned char *)curData0 - dataStart; +#endif + } + else { +// unsigned int i; + const unsigned char *dataStart = data; + + while(dataByteLen >= (laneOffsetParallel*3 + laneCount)*8) { + KeccakP1600times4_AddLanesAll(states, data, laneCount, laneOffsetParallel); + KeccakP1600times4_PermuteAll_24rounds(states); + data += laneOffsetSerial*8; + dataByteLen -= laneOffsetSerial*8; + } + return data - dataStart; + } +} + +size_t KeccakP1600times4_12rounds_FastLoop_Absorb(void *states, unsigned int laneCount, unsigned int laneOffsetParallel, unsigned int laneOffsetSerial, const unsigned char *data, size_t dataByteLen) +{ + if (laneCount == 21) { +#if 0 + const unsigned char *dataStart = data; + const UINT64 *curData0 = (const UINT64 *)data; + const UINT64 *curData1 = (const UINT64 *)(data+laneOffsetParallel*1*SnP_laneLengthInBytes); + const UINT64 *curData2 = (const UINT64 *)(data+laneOffsetParallel*2*SnP_laneLengthInBytes); + const UINT64 *curData3 = (const UINT64 *)(data+laneOffsetParallel*3*SnP_laneLengthInBytes); + + while(dataByteLen >= (laneOffsetParallel*3 + laneCount)*8) { + V256 *stateAsLanes = states; + V256 lanes0, lanes1, lanes2, lanes3, lanesL01, lanesL23, lanesH01, lanesH23; + #define Xor_In( argIndex ) \ + XOReq256(stateAsLanes[argIndex], LOAD4_64(curData3[argIndex], curData2[argIndex], curData1[argIndex], curData0[argIndex])) + #define Xor_In4( argIndex ) \ + lanes0 = LOAD256u( curData0[argIndex]),\ + lanes1 = LOAD256u( curData1[argIndex]),\ + lanes2 = LOAD256u( curData2[argIndex]),\ + lanes3 = LOAD256u( curData3[argIndex]),\ + INTLEAVE(),\ + XOReq256( stateAsLanes[argIndex+0], lanes0 ),\ + XOReq256( stateAsLanes[argIndex+1], lanes1 ),\ + XOReq256( stateAsLanes[argIndex+2], lanes2 ),\ + XOReq256( stateAsLanes[argIndex+3], lanes3 ) + Xor_In4( 0 ); + Xor_In4( 4 ); + Xor_In4( 8 ); + Xor_In4( 12 ); + Xor_In4( 16 ); + Xor_In( 20 ); + #undef Xor_In + #undef Xor_In4 + KeccakP1600times4_PermuteAll_12rounds(states); + curData0 += laneOffsetSerial; + curData1 += laneOffsetSerial; + curData2 += laneOffsetSerial; + curData3 += laneOffsetSerial; + dataByteLen -= laneOffsetSerial*8; + } + return (const unsigned char *)curData0 - dataStart; +#else +// unsigned int i; + const unsigned char *dataStart = data; + const UINT64 *curData0 = (const UINT64 *)data; + const UINT64 *curData1 = (const UINT64 *)(data+laneOffsetParallel*1*SnP_laneLengthInBytes); + const UINT64 *curData2 = (const UINT64 *)(data+laneOffsetParallel*2*SnP_laneLengthInBytes); + const UINT64 *curData3 = (const UINT64 *)(data+laneOffsetParallel*3*SnP_laneLengthInBytes); + V256 *statesAsLanes = states; + declareABCDE + + copyFromState(A, statesAsLanes) + while(dataByteLen >= (laneOffsetParallel*3 + laneCount)*8) { + #define XOR_In( Xxx, argIndex ) \ + XOReq256(Xxx, LOAD4_64(curData3[argIndex], curData2[argIndex], curData1[argIndex], curData0[argIndex])) + XOR_In( Aba, 0 ); + XOR_In( Abe, 1 ); + XOR_In( Abi, 2 ); + XOR_In( Abo, 3 ); + XOR_In( Abu, 4 ); + XOR_In( Aga, 5 ); + XOR_In( Age, 6 ); + XOR_In( Agi, 7 ); + XOR_In( Ago, 8 ); + XOR_In( Agu, 9 ); + XOR_In( Aka, 10 ); + XOR_In( Ake, 11 ); + XOR_In( Aki, 12 ); + XOR_In( Ako, 13 ); + XOR_In( Aku, 14 ); + XOR_In( Ama, 15 ); + XOR_In( Ame, 16 ); + XOR_In( Ami, 17 ); + XOR_In( Amo, 18 ); + XOR_In( Amu, 19 ); + XOR_In( Asa, 20 ); + #undef XOR_In + rounds12 + curData0 += laneOffsetSerial; + curData1 += laneOffsetSerial; + curData2 += laneOffsetSerial; + curData3 += laneOffsetSerial; + dataByteLen -= laneOffsetSerial*8; + } + copyToState(statesAsLanes, A) + return (const unsigned char *)curData0 - dataStart; +#endif + } + else { +// unsigned int i; + const unsigned char *dataStart = data; + + while(dataByteLen >= (laneOffsetParallel*3 + laneCount)*8) { + KeccakP1600times4_AddLanesAll(states, data, laneCount, laneOffsetParallel); + KeccakP1600times4_PermuteAll_12rounds(states); + data += laneOffsetSerial*8; + dataByteLen -= laneOffsetSerial*8; + } + return data - dataStart; + } +} diff --git a/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-times4-SnP.h b/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-times4-SnP.h new file mode 100644 index 0000000..6033848 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-times4-SnP.h @@ -0,0 +1,50 @@ +/* +Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni, +Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby +denoted as "the implementer". + +For more information, feedback or questions, please refer to our websites: +http://keccak.noekeon.org/ +http://keyak.noekeon.org/ +http://ketje.noekeon.org/ + +To the extent possible under law, the implementer has waived all copyright +and related or neighboring rights to the source code in this file. +http://creativecommons.org/publicdomain/zero/1.0/ +*/ + +#ifndef _KeccakP_1600_times4_SnP_h_ +#define _KeccakP_1600_times4_SnP_h_ + +/** For the documentation, see PlSnP-documentation.h. + */ + +#include "SIMD256-config.h" + +#define KeccakP1600times4_implementation "256-bit SIMD implementation (" KeccakP1600times4_implementation_config ")" +#define KeccakP1600times4_statesSizeInBytes 800 +#define KeccakP1600times4_statesAlignment 32 +#define KeccakF1600times4_FastLoop_supported +#define KeccakP1600times4_12rounds_FastLoop_supported + +#include + +#define KeccakP1600times4_StaticInitialize() +void KeccakP1600times4_InitializeAll(void *states); +#define KeccakP1600times4_AddByte(states, instanceIndex, byte, offset) \ + ((unsigned char*)(states))[(instanceIndex)*8 + ((offset)/8)*4*8 + (offset)%8] ^= (byte) +void KeccakP1600times4_AddBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length); +void KeccakP1600times4_AddLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset); +void KeccakP1600times4_OverwriteBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length); +void KeccakP1600times4_OverwriteLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset); +void KeccakP1600times4_OverwriteWithZeroes(void *states, unsigned int instanceIndex, unsigned int byteCount); +void KeccakP1600times4_PermuteAll_12rounds(void *states); +void KeccakP1600times4_PermuteAll_24rounds(void *states); +void KeccakP1600times4_ExtractBytes(const void *states, unsigned int instanceIndex, unsigned char *data, unsigned int offset, unsigned int length); +void KeccakP1600times4_ExtractLanesAll(const void *states, unsigned char *data, unsigned int laneCount, unsigned int laneOffset); +void KeccakP1600times4_ExtractAndAddBytes(const void *states, unsigned int instanceIndex, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length); +void KeccakP1600times4_ExtractAndAddLanesAll(const void *states, const unsigned char *input, unsigned char *output, unsigned int laneCount, unsigned int laneOffset); +size_t KeccakF1600times4_FastLoop_Absorb(void *states, unsigned int laneCount, unsigned int laneOffsetParallel, unsigned int laneOffsetSerial, const unsigned char *data, size_t dataByteLen); +size_t KeccakP1600times4_12rounds_FastLoop_Absorb(void *states, unsigned int laneCount, unsigned int laneOffsetParallel, unsigned int laneOffsetSerial, const unsigned char *data, size_t dataByteLen); + +#endif diff --git a/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-unrolling.macros b/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-unrolling.macros new file mode 100644 index 0000000..3180bb0 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/keccak4x/KeccakP-1600-unrolling.macros @@ -0,0 +1,198 @@ +/* +Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni, +Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby +denoted as "the implementer". + +For more information, feedback or questions, please refer to our websites: +http://keccak.noekeon.org/ +http://keyak.noekeon.org/ +http://ketje.noekeon.org/ + +To the extent possible under law, the implementer has waived all copyright +and related or neighboring rights to the source code in this file. +http://creativecommons.org/publicdomain/zero/1.0/ +*/ + +#if (defined(FullUnrolling)) +#define rounds24 \ + prepareTheta \ + thetaRhoPiChiIotaPrepareTheta( 0, A, E) \ + thetaRhoPiChiIotaPrepareTheta( 1, E, A) \ + thetaRhoPiChiIotaPrepareTheta( 2, A, E) \ + thetaRhoPiChiIotaPrepareTheta( 3, E, A) \ + thetaRhoPiChiIotaPrepareTheta( 4, A, E) \ + thetaRhoPiChiIotaPrepareTheta( 5, E, A) \ + thetaRhoPiChiIotaPrepareTheta( 6, A, E) \ + thetaRhoPiChiIotaPrepareTheta( 7, E, A) \ + thetaRhoPiChiIotaPrepareTheta( 8, A, E) \ + thetaRhoPiChiIotaPrepareTheta( 9, E, A) \ + thetaRhoPiChiIotaPrepareTheta(10, A, E) \ + thetaRhoPiChiIotaPrepareTheta(11, E, A) \ + thetaRhoPiChiIotaPrepareTheta(12, A, E) \ + thetaRhoPiChiIotaPrepareTheta(13, E, A) \ + thetaRhoPiChiIotaPrepareTheta(14, A, E) \ + thetaRhoPiChiIotaPrepareTheta(15, E, A) \ + thetaRhoPiChiIotaPrepareTheta(16, A, E) \ + thetaRhoPiChiIotaPrepareTheta(17, E, A) \ + thetaRhoPiChiIotaPrepareTheta(18, A, E) \ + thetaRhoPiChiIotaPrepareTheta(19, E, A) \ + thetaRhoPiChiIotaPrepareTheta(20, A, E) \ + thetaRhoPiChiIotaPrepareTheta(21, E, A) \ + thetaRhoPiChiIotaPrepareTheta(22, A, E) \ + thetaRhoPiChiIota(23, E, A) \ + +#define rounds12 \ + prepareTheta \ + thetaRhoPiChiIotaPrepareTheta(12, A, E) \ + thetaRhoPiChiIotaPrepareTheta(13, E, A) \ + thetaRhoPiChiIotaPrepareTheta(14, A, E) \ + thetaRhoPiChiIotaPrepareTheta(15, E, A) \ + thetaRhoPiChiIotaPrepareTheta(16, A, E) \ + thetaRhoPiChiIotaPrepareTheta(17, E, A) \ + thetaRhoPiChiIotaPrepareTheta(18, A, E) \ + thetaRhoPiChiIotaPrepareTheta(19, E, A) \ + thetaRhoPiChiIotaPrepareTheta(20, A, E) \ + thetaRhoPiChiIotaPrepareTheta(21, E, A) \ + thetaRhoPiChiIotaPrepareTheta(22, A, E) \ + thetaRhoPiChiIota(23, E, A) \ + +#elif (Unrolling == 12) +#define rounds24 \ + prepareTheta \ + for(i=0; i<24; i+=12) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+ 1, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+ 2, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+ 3, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+ 4, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+ 5, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+ 6, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+ 7, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+ 8, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+ 9, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+10, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+11, E, A) \ + } \ + +#define rounds12 \ + prepareTheta \ + thetaRhoPiChiIotaPrepareTheta(12, A, E) \ + thetaRhoPiChiIotaPrepareTheta(13, E, A) \ + thetaRhoPiChiIotaPrepareTheta(14, A, E) \ + thetaRhoPiChiIotaPrepareTheta(15, E, A) \ + thetaRhoPiChiIotaPrepareTheta(16, A, E) \ + thetaRhoPiChiIotaPrepareTheta(17, E, A) \ + thetaRhoPiChiIotaPrepareTheta(18, A, E) \ + thetaRhoPiChiIotaPrepareTheta(19, E, A) \ + thetaRhoPiChiIotaPrepareTheta(20, A, E) \ + thetaRhoPiChiIotaPrepareTheta(21, E, A) \ + thetaRhoPiChiIotaPrepareTheta(22, A, E) \ + thetaRhoPiChiIota(23, E, A) \ + +#elif (Unrolling == 6) +#define rounds24 \ + prepareTheta \ + for(i=0; i<24; i+=6) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+3, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+4, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+5, E, A) \ + } \ + +#define rounds12 \ + prepareTheta \ + for(i=12; i<24; i+=6) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+3, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+4, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+5, E, A) \ + } \ + +#elif (Unrolling == 4) +#define rounds24 \ + prepareTheta \ + for(i=0; i<24; i+=4) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+3, E, A) \ + } \ + +#define rounds12 \ + prepareTheta \ + for(i=12; i<24; i+=4) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+3, E, A) \ + } \ + +#elif (Unrolling == 3) +#define rounds24 \ + prepareTheta \ + for(i=0; i<24; i+=3) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \ + copyStateVariables(A, E) \ + } \ + +#define rounds12 \ + prepareTheta \ + for(i=12; i<24; i+=3) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \ + copyStateVariables(A, E) \ + } \ + +#elif (Unrolling == 2) +#define rounds24 \ + prepareTheta \ + for(i=0; i<24; i+=2) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + } \ + +#define rounds12 \ + prepareTheta \ + for(i=12; i<24; i+=2) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + } \ + +#elif (Unrolling == 1) +#define rounds24 \ + prepareTheta \ + for(i=0; i<24; i++) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + copyStateVariables(A, E) \ + } \ + +#define rounds12 \ + prepareTheta \ + for(i=12; i<24; i++) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + copyStateVariables(A, E) \ + } \ + +#else +#error "Unrolling is not correctly specified!" +#endif + +#define roundsN(__nrounds) \ + prepareTheta \ + i = 24 - (__nrounds); \ + if ((i&1) != 0) { \ + thetaRhoPiChiIotaPrepareTheta(i, A, E) \ + copyStateVariables(A, E) \ + ++i; \ + } \ + for( /* empty */; i<24; i+=2) { \ + thetaRhoPiChiIotaPrepareTheta(i , A, E) \ + thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \ + } diff --git a/src/crypto/pq/_s/shake-avx2/keccak4x/SIMD256-config.h b/src/crypto/pq/_s/shake-avx2/keccak4x/SIMD256-config.h new file mode 100644 index 0000000..1c65fe2 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/keccak4x/SIMD256-config.h @@ -0,0 +1,3 @@ +#define KeccakP1600times4_implementation_config "AVX2, all rounds unrolled" +#define KeccakP1600times4_fullUnrolling +#define KeccakP1600times4_useAVX2 diff --git a/src/crypto/pq/_s/shake-avx2/keccak4x/align.h b/src/crypto/pq/_s/shake-avx2/keccak4x/align.h new file mode 100644 index 0000000..e29771e --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/keccak4x/align.h @@ -0,0 +1,34 @@ +/* +Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni, +Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby +denoted as "the implementer". + +For more information, feedback or questions, please refer to our websites: +http://keccak.noekeon.org/ +http://keyak.noekeon.org/ +http://ketje.noekeon.org/ + +To the extent possible under law, the implementer has waived all copyright +and related or neighboring rights to the source code in this file. +http://creativecommons.org/publicdomain/zero/1.0/ +*/ + +#ifndef _align_h_ +#define _align_h_ + +/* on Mac OS-X and possibly others, ALIGN(x) is defined in param.h, and -Werror chokes on the redef. */ +#ifdef ALIGN +#undef ALIGN +#endif + +#if defined(__GNUC__) +#define ALIGN(x) __attribute__ ((aligned(x))) +#elif defined(_MSC_VER) +#define ALIGN(x) __declspec(align(x)) +#elif defined(__ARMCC_VERSION) +#define ALIGN(x) __align(x) +#else +#define ALIGN(x) +#endif + +#endif diff --git a/src/crypto/pq/_s/shake-avx2/keccak4x/brg_endian.h b/src/crypto/pq/_s/shake-avx2/keccak4x/brg_endian.h new file mode 100644 index 0000000..7226eb3 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/keccak4x/brg_endian.h @@ -0,0 +1,142 @@ +/* + --------------------------------------------------------------------------- + Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The redistribution and use of this software (with or without changes) + is allowed without the payment of fees or royalties provided that: + + 1. source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + 2. binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation; + + 3. the name of the copyright holder is not used to endorse products + built using this software without specific written permission. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 20/12/2007 + Changes for ARM 9/9/2010 +*/ + +#ifndef _BRG_ENDIAN_H +#define _BRG_ENDIAN_H + +#define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ +#define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ + +#if 0 +/* Include files where endian defines and byteswap functions may reside */ +#if defined( __sun ) +# include +#elif defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __NetBSD__ ) +# include +#elif defined( BSD ) && ( BSD >= 199103 ) || defined( __APPLE__ ) || \ + defined( __CYGWIN32__ ) || defined( __DJGPP__ ) || defined( __osf__ ) +# include +#elif defined( __linux__ ) || defined( __GNUC__ ) || defined( __GNU_LIBRARY__ ) +# if !defined( __MINGW32__ ) && !defined( _AIX ) +# include +# if !defined( __BEOS__ ) +# include +# endif +# endif +#endif +#endif + +/* Now attempt to set the define for platform byte order using any */ +/* of the four forms SYMBOL, _SYMBOL, __SYMBOL & __SYMBOL__, which */ +/* seem to encompass most endian symbol definitions */ + +#if defined( BIG_ENDIAN ) && defined( LITTLE_ENDIAN ) +# if defined( BYTE_ORDER ) && BYTE_ORDER == BIG_ENDIAN +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# elif defined( BYTE_ORDER ) && BYTE_ORDER == LITTLE_ENDIAN +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif defined( BIG_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#elif defined( LITTLE_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#endif + +#if defined( _BIG_ENDIAN ) && defined( _LITTLE_ENDIAN ) +# if defined( _BYTE_ORDER ) && _BYTE_ORDER == _BIG_ENDIAN +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# elif defined( _BYTE_ORDER ) && _BYTE_ORDER == _LITTLE_ENDIAN +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif defined( _BIG_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#elif defined( _LITTLE_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#endif + +#if defined( __BIG_ENDIAN ) && defined( __LITTLE_ENDIAN ) +# if defined( __BYTE_ORDER ) && __BYTE_ORDER == __BIG_ENDIAN +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# elif defined( __BYTE_ORDER ) && __BYTE_ORDER == __LITTLE_ENDIAN +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif defined( __BIG_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#elif defined( __LITTLE_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#endif + +#if defined( __BIG_ENDIAN__ ) && defined( __LITTLE_ENDIAN__ ) +# if defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __BIG_ENDIAN__ +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# elif defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __LITTLE_ENDIAN__ +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif defined( __BIG_ENDIAN__ ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#elif defined( __LITTLE_ENDIAN__ ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#endif + +/* if the platform byte order could not be determined, then try to */ +/* set this define using common machine defines */ +#if !defined(PLATFORM_BYTE_ORDER) + +#if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \ + defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \ + defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \ + defined( vax ) || defined( vms ) || defined( VMS ) || \ + defined( __VMS ) || defined( _M_X64 ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN + +#elif defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \ + defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \ + defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \ + defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \ + defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \ + defined( __VOS__ ) || defined( __TIGCC__ ) || defined( __TANDEM ) || \ + defined( THINK_C ) || defined( __VMCMS__ ) || defined( _AIX ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN + +#elif defined(__arm__) +# ifdef __BIG_ENDIAN +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# else +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif 1 /* **** EDIT HERE IF NECESSARY **** */ +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#elif 0 /* **** EDIT HERE IF NECESSARY **** */ +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#else +# error Please edit lines 132 or 134 in brg_endian.h to set the platform byte order +#endif + +#endif + +#endif diff --git a/src/crypto/pq/_s/shake-avx2/merkle.c b/src/crypto/pq/_s/shake-avx2/merkle.c new file mode 100644 index 0000000..e3929d8 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/merkle.c @@ -0,0 +1,65 @@ +#include +#include + +#include "utils.h" +#include "utilsx4.h" +#include "wots.h" +#include "wotsx4.h" +#include "merkle.h" +#include "address.h" +#include "params.h" + +/* + * This generates a Merkle signature (WOTS signature followed by the Merkle + * authentication path). + */ +void merkle_sign(uint8_t *sig, unsigned char *root, + const spx_ctx* ctx, + uint32_t wots_addr[8], uint32_t tree_addr[8], + uint32_t idx_leaf) +{ + unsigned char *auth_path = sig + SPX_WOTS_BYTES; + uint32_t tree_addrx4[4*8] = { 0 }; + int j; + struct leaf_info_x4 info = { 0 }; + unsigned steps[ SPX_WOTS_LEN ]; + + info.wots_sig = sig; + chain_lengths(steps, root); + info.wots_steps = steps; + + for (j=0; j<4; j++) { + set_type(&tree_addrx4[8*j], SPX_ADDR_TYPE_HASHTREE); + set_type(&info.leaf_addr[8*j], SPX_ADDR_TYPE_WOTS); + set_type(&info.pk_addr[8*j], SPX_ADDR_TYPE_WOTSPK); + copy_subtree_addr(&tree_addrx4[8*j], tree_addr); + copy_subtree_addr(&info.leaf_addr[8*j], wots_addr); + copy_subtree_addr(&info.pk_addr[8*j], wots_addr); + } + + info.wots_sign_leaf = idx_leaf; + + treehashx4(root, auth_path, ctx, + idx_leaf, 0, + SPX_TREE_HEIGHT, + wots_gen_leafx4, + tree_addrx4, &info); +} + +/* Compute root node of the top-most subtree. */ +void merkle_gen_root(unsigned char *root, const spx_ctx *ctx) +{ + /* We do not need the auth path in key generation, but it simplifies the + code to have just one treehash routine that computes both root and path + in one function. */ + unsigned char auth_path[SPX_TREE_HEIGHT * SPX_N + SPX_WOTS_BYTES]; + uint32_t top_tree_addr[8] = {0}; + uint32_t wots_addr[8] = {0}; + + set_layer_addr(top_tree_addr, SPX_D - 1); + set_layer_addr(wots_addr, SPX_D - 1); + + merkle_sign(auth_path, root, ctx, + wots_addr, top_tree_addr, + ~0 /* ~0 means "don't bother generating an auth path */ ); +} diff --git a/src/crypto/pq/_s/shake-avx2/merkle.h b/src/crypto/pq/_s/shake-avx2/merkle.h new file mode 120000 index 0000000..7d167ed --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/merkle.h @@ -0,0 +1 @@ +../ref/merkle.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/params.h b/src/crypto/pq/_s/shake-avx2/params.h new file mode 120000 index 0000000..53133cc --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/params.h @@ -0,0 +1 @@ +../ref/params.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-128f.h b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-128f.h new file mode 120000 index 0000000..c65db98 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-128f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-128f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-128s.h b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-128s.h new file mode 120000 index 0000000..18671f7 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-128s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-128s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-192f.h b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-192f.h new file mode 120000 index 0000000..d071e35 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-192f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-192f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-192s.h b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-192s.h new file mode 120000 index 0000000..267e2c8 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-192s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-192s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-256f.h b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-256f.h new file mode 120000 index 0000000..3b4743b --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-256f.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-256f.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-256s.h b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-256s.h new file mode 120000 index 0000000..0795ee1 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/params/params-sphincs-shake-256s.h @@ -0,0 +1 @@ +../../ref/params/params-sphincs-shake-256s.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/randombytes.c b/src/crypto/pq/_s/shake-avx2/randombytes.c new file mode 120000 index 0000000..59a42a5 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/randombytes.c @@ -0,0 +1 @@ +../ref/randombytes.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/randombytes.h b/src/crypto/pq/_s/shake-avx2/randombytes.h new file mode 120000 index 0000000..055e443 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/randombytes.h @@ -0,0 +1 @@ +../ref/randombytes.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/rng.c b/src/crypto/pq/_s/shake-avx2/rng.c new file mode 120000 index 0000000..6e2fdac --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/rng.c @@ -0,0 +1 @@ +../ref/rng.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/rng.h b/src/crypto/pq/_s/shake-avx2/rng.h new file mode 120000 index 0000000..d678c7c --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/rng.h @@ -0,0 +1 @@ +../ref/rng.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/shake_offsets.h b/src/crypto/pq/_s/shake-avx2/shake_offsets.h new file mode 120000 index 0000000..8cfe4c0 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/shake_offsets.h @@ -0,0 +1 @@ +../ref/shake_offsets.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/sign.c b/src/crypto/pq/_s/shake-avx2/sign.c new file mode 120000 index 0000000..42fea88 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/sign.c @@ -0,0 +1 @@ +../ref/sign.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/test/benchmark.c b/src/crypto/pq/_s/shake-avx2/test/benchmark.c new file mode 100644 index 0000000..a5767a4 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/test/benchmark.c @@ -0,0 +1,161 @@ +#define _POSIX_C_SOURCE 199309L + +#include +#include +#include + +#include "../api.h" +#include "../fors.h" +#include "../wots.h" +#include "../wotsx4.h" +#include "../params.h" +#include "../randombytes.h" + +#define SPX_MLEN 32 +#define NTESTS 10 + +static void wots_gen_pkx4(unsigned char *pk, const spx_ctx *ctx, + uint32_t addr[8]); + +static int cmp_llu(const void *a, const void*b) +{ + if(*(unsigned long long *)a < *(unsigned long long *)b) return -1; + if(*(unsigned long long *)a > *(unsigned long long *)b) return 1; + return 0; +} + +static unsigned long long median(unsigned long long *l, size_t llen) +{ + qsort(l,llen,sizeof(unsigned long long),cmp_llu); + + if(llen%2) return l[llen/2]; + else return (l[llen/2-1]+l[llen/2])/2; +} + +static void delta(unsigned long long *l, size_t llen) +{ + unsigned int i; + for(i = 0; i < llen - 1; i++) { + l[i] = l[i+1] - l[i]; + } +} + +static unsigned long long cpucycles(void) +{ + unsigned long long result; + __asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" + : "=a" (result) :: "%rdx"); + return result; +} + +static void printfcomma (unsigned long long n) +{ + if (n < 1000) { + printf("%llu", n); + return; + } + printfcomma(n / 1000); + printf (",%03llu", n % 1000); +} + +static void printfalignedcomma (unsigned long long n, int len) +{ + unsigned long long ncopy = n; + int i = 0; + + while (ncopy > 9) { + len -= 1; + ncopy /= 10; + i += 1; // to account for commas + } + i = i/3 - 1; // to account for commas + for (; i < len; i++) { + printf(" "); + } + printfcomma(n); +} + +static void display_result(double result, unsigned long long *l, size_t llen, unsigned long long mul) +{ + unsigned long long med; + + result /= NTESTS; + delta(l, NTESTS + 1); + med = median(l, llen); + printf("avg. %11.2lf us (%2.2lf sec); median ", result, result / 1e6); + printfalignedcomma(med, 12); + printf(" cycles, %5llux: ", mul); + printfalignedcomma(mul*med, 12); + printf(" cycles\n"); +} + +#define MEASURE(TEXT, MUL, FNCALL)\ + printf(TEXT);\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);\ + for(i = 0; i < NTESTS; i++) {\ + t[i] = cpucycles();\ + FNCALL;\ + }\ + t[NTESTS] = cpucycles();\ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);\ + result = (stop.tv_sec - start.tv_sec) * 1e6 + (stop.tv_nsec - start.tv_nsec) / 1e3;\ + display_result(result, t, NTESTS, MUL); + +int main(void) +{ + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + spx_ctx ctx; + unsigned char pk[SPX_PK_BYTES]; + unsigned char sk[SPX_SK_BYTES]; + unsigned char *m = malloc(SPX_MLEN); + unsigned char *sm = malloc(SPX_BYTES + SPX_MLEN); + unsigned char *mout = malloc(SPX_BYTES + SPX_MLEN); + + unsigned char fors_pk[SPX_FORS_PK_BYTES]; + unsigned char fors_m[SPX_FORS_MSG_BYTES]; + unsigned char fors_sig[SPX_FORS_BYTES]; + unsigned char addr[SPX_ADDR_BYTES]; + unsigned char wots_pk[4*SPX_WOTS_PK_BYTES]; + + unsigned long long smlen; + unsigned long long mlen; + unsigned long long t[NTESTS+1]; + struct timespec start, stop; + double result; + int i; + + randombytes(m, SPX_MLEN); + randombytes(addr, SPX_ADDR_BYTES); + + printf("Parameters: n = %d, h = %d, d = %d, b = %d, k = %d, w = %d\n", + SPX_N, SPX_FULL_HEIGHT, SPX_D, SPX_FORS_HEIGHT, SPX_FORS_TREES, + SPX_WOTS_W); + + printf("Running %d iterations.\n", NTESTS); + + MEASURE("Generating keypair.. ", 1, crypto_sign_keypair(pk, sk)); + MEASURE(" - WOTS pk gen 4x.. ", (1 << SPX_TREE_HEIGHT) / 4, wots_gen_pkx4(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Signing.. ", 1, crypto_sign(sm, &smlen, m, SPX_MLEN, sk)); + MEASURE(" - FORS signing.. ", 1, fors_sign(fors_sig, fors_pk, fors_m, &ctx, (uint32_t *) addr)); + MEASURE(" - WOTS pk gen x4.. ", SPX_D * (1 << SPX_TREE_HEIGHT) / 4, wots_gen_pkx4(wots_pk, &ctx, (uint32_t *) addr)); + MEASURE("Verifying.. ", 1, crypto_sign_open(mout, &mlen, sm, smlen, pk)); + + printf("Signature size: %d (%.2f KiB)\n", SPX_BYTES, SPX_BYTES / 1024.0); + printf("Public key size: %d (%.2f KiB)\n", SPX_PK_BYTES, SPX_PK_BYTES / 1024.0); + printf("Secret key size: %d (%.2f KiB)\n", SPX_SK_BYTES, SPX_SK_BYTES / 1024.0); + + free(m); + free(sm); + free(mout); + + return 0; +} + +static void wots_gen_pkx4(unsigned char *pk, const spx_ctx *ctx, uint32_t addr[8]) { + struct leaf_info_x4 leaf; + unsigned steps[ SPX_WOTS_LEN ] = { 0 }; + INITIALIZE_LEAF_INFO_X4(leaf, addr, steps); + wots_gen_leafx4(pk, ctx, 0, &leaf); +} diff --git a/src/crypto/pq/_s/shake-avx2/test/fors.c b/src/crypto/pq/_s/shake-avx2/test/fors.c new file mode 120000 index 0000000..b2bccee --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/test/fors.c @@ -0,0 +1 @@ +../../ref/test/fors.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/test/spx.c b/src/crypto/pq/_s/shake-avx2/test/spx.c new file mode 120000 index 0000000..7af26df --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/test/spx.c @@ -0,0 +1 @@ +../../ref/test/spx.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/test/thashx4.c b/src/crypto/pq/_s/shake-avx2/test/thashx4.c new file mode 100644 index 0000000..54e96f9 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/test/thashx4.c @@ -0,0 +1,47 @@ +#include +#include + +#include "../thashx4.h" +#include "../thash.h" +#include "../randombytes.h" +#include "../params.h" + +int main(void) +{ + /* Make stdout buffer more responsive. */ + setbuf(stdout, NULL); + + unsigned char input[4*SPX_N]; + unsigned char output[4*SPX_N]; + unsigned char out4[4*SPX_N]; + uint32_t addr[4*8] = {0}; + unsigned int j; + spx_ctx ctx; + + randombytes(ctx.pub_seed, SPX_N); + randombytes(input, 4*SPX_N); + randombytes((unsigned char *)addr, 4 * 8 * sizeof(uint32_t)); + + printf("Testing if thash matches thashx4.. "); + + for (j = 0; j < 4; j++) { + thash(out4 + j * SPX_N, input + j * SPX_N, 1, &ctx, addr + j*8); + } + + thashx4(output + 0*SPX_N, + output + 1*SPX_N, + output + 2*SPX_N, + output + 3*SPX_N, + input + 0*SPX_N, + input + 1*SPX_N, + input + 2*SPX_N, + input + 3*SPX_N, + 1, &ctx, addr); + + if (memcmp(out4, output, 4 * SPX_N)) { + printf("failed!\n"); + return -1; + } + printf("successful.\n"); + return 0; +} diff --git a/src/crypto/pq/_s/shake-avx2/thash.h b/src/crypto/pq/_s/shake-avx2/thash.h new file mode 120000 index 0000000..937dd48 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/thash.h @@ -0,0 +1 @@ +../ref/thash.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/thash_shake_robust.c b/src/crypto/pq/_s/shake-avx2/thash_shake_robust.c new file mode 120000 index 0000000..3be16c6 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/thash_shake_robust.c @@ -0,0 +1 @@ +../ref/thash_shake_robust.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/thash_shake_robustx4.c b/src/crypto/pq/_s/shake-avx2/thash_shake_robustx4.c new file mode 100644 index 0000000..ba136a6 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/thash_shake_robustx4.c @@ -0,0 +1,131 @@ +#include +#include + +#include "thashx4.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "fips202x4.h" + +extern void KeccakP1600times4_PermuteAll_24rounds(__m256i *s); + +/** + * 4-way parallel version of thash; takes 4x as much input and output + */ +void thashx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx4[4*8]) +{ + if (inblocks == 1 || inblocks == 2) { + /* As we write and read only a few quadwords, it is more efficient to + * build and extract from the fourway SHAKE256 state by hand. */ + __m256i state[25]; + for (int i = 0; i < SPX_N/8; i++) { + state[i] = _mm256_set1_epi64x(((int64_t*)ctx->pub_seed)[i]); + } + for (int i = 0; i < 4; i++) { + state[SPX_N/8+i] = _mm256_set_epi32( + addrx4[3*8+1+2*i], + addrx4[3*8+2*i], + addrx4[2*8+1+2*i], + addrx4[2*8+2*i], + addrx4[8+1+2*i], + addrx4[8+2*i], + addrx4[1+2*i], + addrx4[2*i] + ); + } + + /* SHAKE domain separator and padding */ + state[SPX_N/8+4] = _mm256_set1_epi64x(0x1f); + for (int i = SPX_N/8+5; i < 16; i++) { + state[i] = _mm256_set1_epi64x(0); + } + state[16] = _mm256_set1_epi64x((long long)(0x80ULL << 56)); + + for (int i = 17; i < 25; i++) { + state[i] = _mm256_set1_epi64x(0); + } + + /* We will permutate state2 with f1600x4 to compute the bitmask, + * but first we'll copy it to state2 which will be used to compute + * the final output, as its input is alsmost identical. */ + __m256i state2[25]; + memcpy(state2, state, 800); + + KeccakP1600times4_PermuteAll_24rounds(&state[0]); + + /* By copying from state, state2 already contains the pub_seed + * and addres. We just need to copy in the input blocks xorred with + * the bitmask we just computed. */ + for (unsigned int i = 0; i < (SPX_N/8) * inblocks; i++) { + state2[SPX_N/8+4+i] = _mm256_xor_si256( + state[i], + _mm256_set_epi64x( + ((int64_t*)in3)[i], + ((int64_t*)in2)[i], + ((int64_t*)in1)[i], + ((int64_t*)in0)[i] + ) + ); + } + + /* Domain separator and start of padding. Note that the quadwords + * around are already zeroed for state from which we copied. + * We do a XOR instead of a set as this might be the 16th quadword + * when N=32 and inblocks=2, which already contains the end + * of the padding. */ + state2[(SPX_N/8)*(1+inblocks)+4] = _mm256_xor_si256( + state2[(SPX_N/8)*(1+inblocks)+4], + _mm256_set1_epi64x(0x1f) + ); + + KeccakP1600times4_PermuteAll_24rounds(&state2[0]); + + for (int i = 0; i < SPX_N/8; i++) { + ((int64_t*)out0)[i] = _mm256_extract_epi64(state2[i], 0); + ((int64_t*)out1)[i] = _mm256_extract_epi64(state2[i], 1); + ((int64_t*)out2)[i] = _mm256_extract_epi64(state2[i], 2); + ((int64_t*)out3)[i] = _mm256_extract_epi64(state2[i], 3); + } + } else { + SPX_VLA(unsigned char, buf0, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf1, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf2, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf3, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, bitmask0, inblocks * SPX_N); + SPX_VLA(unsigned char, bitmask1, inblocks * SPX_N); + SPX_VLA(unsigned char, bitmask2, inblocks * SPX_N); + SPX_VLA(unsigned char, bitmask3, inblocks * SPX_N); + unsigned int i; + + memcpy(buf0, ctx->pub_seed, SPX_N); + memcpy(buf1, ctx->pub_seed, SPX_N); + memcpy(buf2, ctx->pub_seed, SPX_N); + memcpy(buf3, ctx->pub_seed, SPX_N); + memcpy(buf0 + SPX_N, addrx4 + 0*8, SPX_ADDR_BYTES); + memcpy(buf1 + SPX_N, addrx4 + 1*8, SPX_ADDR_BYTES); + memcpy(buf2 + SPX_N, addrx4 + 2*8, SPX_ADDR_BYTES); + memcpy(buf3 + SPX_N, addrx4 + 3*8, SPX_ADDR_BYTES); + + shake256x4(bitmask0, bitmask1, bitmask2, bitmask3, inblocks * SPX_N, + buf0, buf1, buf2, buf3, SPX_N + SPX_ADDR_BYTES); + + for (i = 0; i < inblocks * SPX_N; i++) { + buf0[SPX_N + SPX_ADDR_BYTES + i] = in0[i] ^ bitmask0[i]; + buf1[SPX_N + SPX_ADDR_BYTES + i] = in1[i] ^ bitmask1[i]; + buf2[SPX_N + SPX_ADDR_BYTES + i] = in2[i] ^ bitmask2[i]; + buf3[SPX_N + SPX_ADDR_BYTES + i] = in3[i] ^ bitmask3[i]; + } + + shake256x4(out0, out1, out2, out3, SPX_N, + buf0, buf1, buf2, buf3, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + } +} diff --git a/src/crypto/pq/_s/shake-avx2/thash_shake_simple.c b/src/crypto/pq/_s/shake-avx2/thash_shake_simple.c new file mode 120000 index 0000000..1b5a5a2 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/thash_shake_simple.c @@ -0,0 +1 @@ +../ref/thash_shake_simple.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/thash_shake_simplex4.c b/src/crypto/pq/_s/shake-avx2/thash_shake_simplex4.c new file mode 100644 index 0000000..3763ed1 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/thash_shake_simplex4.c @@ -0,0 +1,98 @@ +#include +#include + +#include "thashx4.h" +#include "address.h" +#include "params.h" +#include "utils.h" + +#include "fips202x4.h" + +extern void KeccakP1600times4_PermuteAll_24rounds(__m256i *s); + +/** + * 4-way parallel version of thash; takes 4x as much input and output + */ +void thashx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx4[4*8]) +{ + if (inblocks == 1 || inblocks == 2) { + /* As we write and read only a few quadwords, it is more efficient to + * build and extract from the fourway SHAKE256 state by hand. */ + __m256i state[25]; + for (int i = 0; i < SPX_N/8; i++) { + state[i] = _mm256_set1_epi64x(((int64_t*)ctx->pub_seed)[i]); + } + for (int i = 0; i < 4; i++) { + state[SPX_N/8+i] = _mm256_set_epi32( + addrx4[3*8+1+2*i], + addrx4[3*8+2*i], + addrx4[2*8+1+2*i], + addrx4[2*8+2*i], + addrx4[8+1+2*i], + addrx4[8+2*i], + addrx4[1+2*i], + addrx4[2*i] + ); + } + + for (unsigned int i = 0; i < (SPX_N/8) * inblocks; i++) { + state[SPX_N/8+4+i] = _mm256_set_epi64x( + ((int64_t*)in3)[i], + ((int64_t*)in2)[i], + ((int64_t*)in1)[i], + ((int64_t*)in0)[i] + ); + } + + /* Domain separator and padding. */ + for (int i = (SPX_N/8)*(1+inblocks)+4; i < 16; i++) { + state[i] = _mm256_set1_epi64x(0); + } + state[16] = _mm256_set1_epi64x((long long)(0x80ULL << 56)); + state[(SPX_N/8)*(1+inblocks)+4] = _mm256_xor_si256( + state[(SPX_N/8)*(1+inblocks)+4], + _mm256_set1_epi64x(0x1f) + ); + for (int i = 17; i < 25; i++) { + state[i] = _mm256_set1_epi64x(0); + } + + KeccakP1600times4_PermuteAll_24rounds(&state[0]); + + for (int i = 0; i < SPX_N/8; i++) { + ((int64_t*)out0)[i] = _mm256_extract_epi64(state[i], 0); + ((int64_t*)out1)[i] = _mm256_extract_epi64(state[i], 1); + ((int64_t*)out2)[i] = _mm256_extract_epi64(state[i], 2); + ((int64_t*)out3)[i] = _mm256_extract_epi64(state[i], 3); + } + } else { + SPX_VLA(unsigned char, buf0, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf1, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf2, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + SPX_VLA(unsigned char, buf3, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + + memcpy(buf0, ctx->pub_seed, SPX_N); + memcpy(buf1, ctx->pub_seed, SPX_N); + memcpy(buf2, ctx->pub_seed, SPX_N); + memcpy(buf3, ctx->pub_seed, SPX_N); + memcpy(buf0 + SPX_N, addrx4 + 0*8, SPX_ADDR_BYTES); + memcpy(buf1 + SPX_N, addrx4 + 1*8, SPX_ADDR_BYTES); + memcpy(buf2 + SPX_N, addrx4 + 2*8, SPX_ADDR_BYTES); + memcpy(buf3 + SPX_N, addrx4 + 3*8, SPX_ADDR_BYTES); + memcpy(buf0 + SPX_N + SPX_ADDR_BYTES, in0, inblocks * SPX_N); + memcpy(buf1 + SPX_N + SPX_ADDR_BYTES, in1, inblocks * SPX_N); + memcpy(buf2 + SPX_N + SPX_ADDR_BYTES, in2, inblocks * SPX_N); + memcpy(buf3 + SPX_N + SPX_ADDR_BYTES, in3, inblocks * SPX_N); + + shake256x4(out0, out1, out2, out3, SPX_N, + buf0, buf1, buf2, buf3, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N); + } +} diff --git a/src/crypto/pq/_s/shake-avx2/thashx4.h b/src/crypto/pq/_s/shake-avx2/thashx4.h new file mode 100644 index 0000000..045237e --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/thashx4.h @@ -0,0 +1,19 @@ +#ifndef SPX_THASHX4_H +#define SPX_THASHX4_H + +#include +#include "context.h" +#include "params.h" + +#define thashx4 SPX_NAMESPACE(thashx4) +void thashx4(unsigned char *out0, + unsigned char *out1, + unsigned char *out2, + unsigned char *out3, + const unsigned char *in0, + const unsigned char *in1, + const unsigned char *in2, + const unsigned char *in3, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addrx4[4*8]); + +#endif diff --git a/src/crypto/pq/_s/shake-avx2/utils.c b/src/crypto/pq/_s/shake-avx2/utils.c new file mode 120000 index 0000000..e8ef6eb --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/utils.c @@ -0,0 +1 @@ +../ref/utils.c \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/utils.h b/src/crypto/pq/_s/shake-avx2/utils.h new file mode 120000 index 0000000..51b0d39 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/utils.h @@ -0,0 +1 @@ +../ref/utils.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/utilsx4.c b/src/crypto/pq/_s/shake-avx2/utilsx4.c new file mode 100644 index 0000000..4d69825 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/utilsx4.c @@ -0,0 +1,138 @@ +#include + +#include "utils.h" +#include "utilsx4.h" +#include "params.h" +#include "thashx4.h" +#include "address.h" + +/* + * Generate the entire Merkle tree, computing the authentication path for leaf_idx, + * and the resulting root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE) + * + * This expects tree_addrx4 to be initialized to 4 parallel addr structures for + * the Merkle tree nodes + * + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + * + * This works by using the standard Merkle tree building algorithm, except + * that each 'node' tracked is actually 4 consecutive nodes in the real tree. + * When we combine two logical nodes ABCD and WXYZ, we perform the H + * operation on adjacent real nodes, forming the parent logical node + * (AB)(CD)(WX)(YZ) + * + * When we get to the top two levels of the real tree (where there is only + * one logical node), we continue this operation two more times; the right + * most real node will by the actual root (and the other 3 nodes will be + * garbage). We follow the same thashx4 logic so that the 'extract + * authentication path components' part of the loop is still executed (and + * to simplify the code somewhat) + * + * This currently assumes tree_height >= 2; I suspect that doing an adjusting + * idx, addr_idx on the gen_leafx4 call if tree_height < 2 would fix it; since + * we don't actually use such short trees, I haven't bothered + */ +void treehashx4(unsigned char *root, unsigned char *auth_path, + const spx_ctx *ctx, + uint32_t leaf_idx, uint32_t idx_offset, + uint32_t tree_height, + void (*gen_leafx4)( + unsigned char* /* Where to write the leaves */, + const spx_ctx*, + uint32_t idx, void *info), + uint32_t tree_addrx4[4*8], + void *info) +{ + /* This is where we keep the intermediate nodes */ + SPX_VLA(unsigned char, stackx4, tree_height * 4 * SPX_N); + uint32_t left_adj = 0, prev_left_adj = 0; /* When we're doing the top 3 */ + /* levels, the left-most part of the tree isn't at the beginning */ + /* of current[]. These give the offset of the actual start */ + + uint32_t idx; + uint32_t max_idx = (1 << (tree_height-2)) - 1; + for (idx = 0;; idx++) { + unsigned char current[4*SPX_N]; /* Current logical node */ + gen_leafx4( current, ctx, 4*idx + idx_offset, + info ); + + /* Now combine the freshly generated right node with previously */ + /* generated left ones */ + uint32_t internal_idx_offset = idx_offset; + uint32_t internal_idx = idx; + uint32_t internal_leaf = leaf_idx; + uint32_t h; /* The height we are in the Merkle tree */ + for (h=0;; h++, internal_idx >>= 1, internal_leaf >>= 1) { + + /* Special processing if we're at the top of the tree */ + if (h >= tree_height - 2) { + if (h == tree_height) { + /* We hit the root; return it */ + memcpy( root, ¤t[3*SPX_N], SPX_N ); + return; + } + /* The tree indexing logic is a bit off in this case */ + /* Adjust it so that the left-most node of the part of */ + /* the tree that we're processing has index 0 */ + prev_left_adj = left_adj; + left_adj = 4 - (1 << (tree_height - h - 1)); + } + + /* Check if we hit the top of the tree */ + if (h == tree_height) { + /* We hit the root; return it */ + memcpy( root, ¤t[3*SPX_N], SPX_N ); + return; + } + + /* + * Check if one of the nodes we have is a part of the + * authentication path; if it is, write it out + */ + if ((((internal_idx << 2) ^ internal_leaf) & ~0x3) == 0) { + memcpy( &auth_path[ h * SPX_N ], + ¤t[(((internal_leaf&3)^1) + prev_left_adj) * SPX_N], + SPX_N ); + } + + /* + * Check if we're at a left child; if so, stop going up the stack + * Exception: if we've reached the end of the tree, keep on going + * (so we combine the last 4 nodes into the one root node in two + * more iterations) + */ + if ((internal_idx & 1) == 0 && idx < max_idx) { + break; + } + + /* Ok, we're at a right node (or doing the top 3 levels) */ + /* Now combine the left and right logical nodes together */ + + /* Set the address of the node we're creating. */ + int j; + internal_idx_offset >>= 1; + for (j = 0; j < 4; j++) { + set_tree_height(tree_addrx4 + j*8, h + 1); + set_tree_index(tree_addrx4 + j*8, + (4/2) * (internal_idx&~1) + j - left_adj + internal_idx_offset ); + } + unsigned char *left = &stackx4[h * 4 * SPX_N]; + thashx4( ¤t[0 * SPX_N], + ¤t[1 * SPX_N], + ¤t[2 * SPX_N], + ¤t[3 * SPX_N], + &left [0 * SPX_N], + &left [2 * SPX_N], + ¤t[0 * SPX_N], + ¤t[2 * SPX_N], + 2, ctx, tree_addrx4); + } + + /* We've hit a left child; save the current for when we get the */ + /* corresponding right right */ + memcpy( &stackx4[h * 4 * SPX_N], current, 4 * SPX_N); + } +} diff --git a/src/crypto/pq/_s/shake-avx2/utilsx4.h b/src/crypto/pq/_s/shake-avx2/utilsx4.h new file mode 100644 index 0000000..92e0a4a --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/utilsx4.h @@ -0,0 +1,28 @@ +#ifndef SPX_UTILSX4_H +#define SPX_UTILSX4_H + +#include +#include "params.h" + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + * + * This implementation uses AVX to compute internal nodes 4 at a time (in + * parallel) + */ +#define treehashx4 SPX_NAMESPACE(treehashx4) +void treehashx4(unsigned char *root, unsigned char *auth_path, + const spx_ctx *ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leafx4)( + unsigned char* /* Where to write the leaves */, + const spx_ctx* /* ctx */, + uint32_t addr_idx, void *info), + uint32_t tree_addrx4[4*8], void *info); + +#endif diff --git a/src/crypto/pq/_s/shake-avx2/wots.c b/src/crypto/pq/_s/shake-avx2/wots.c new file mode 100644 index 0000000..0788b80 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/wots.c @@ -0,0 +1,271 @@ +#include +#include + +#include "utils.h" +#include "utilsx4.h" +#include "hash.h" +#include "hashx4.h" +#include "thash.h" +#include "thashx4.h" +#include "wots.h" +#include "wotsx4.h" +#include "address.h" +#include "params.h" + +// TODO clarify address expectations, and make them more uniform. +// TODO i.e. do we expect types to be set already? +// TODO and do we expect modifications or copies? + +/** + * Computes up the chains + */ +static void gen_chains( + unsigned char *out, + const unsigned char *in, + unsigned int start[SPX_WOTS_LEN], + unsigned int steps[SPX_WOTS_LEN], + const spx_ctx *ctx, + uint32_t addr[8]) +{ + uint32_t i, j, k, idx, watching; + int done; + unsigned char empty[SPX_N]; + unsigned char *bufs[4]; + uint32_t addrs[8*4]; + + int l; + uint16_t counts[SPX_WOTS_W] = { 0 }; + uint16_t idxs[SPX_WOTS_LEN]; + uint16_t total, newTotal; + + /* set addrs = {addr, addr, addr, addr} */ + for (j = 0; j < 4; j++) { + memcpy(addrs+j*8, addr, sizeof(uint32_t) * 8); + } + + /* Initialize out with the value at position 'start'. */ + memcpy(out, in, SPX_WOTS_LEN*SPX_N); + + /* Sort the chains in reverse order by steps using counting sort. */ + for (i = 0; i < SPX_WOTS_LEN; i++) { + counts[steps[i]]++; + } + total = 0; + for (l = SPX_WOTS_W - 1; l >= 0; l--) { + newTotal = counts[l] + total; + counts[l] = total; + total = newTotal; + } + for (i = 0; i < SPX_WOTS_LEN; i++) { + idxs[counts[steps[i]]] = i; + counts[steps[i]]++; + } + + /* We got our work cut out for us: do it! */ + for (i = 0; i < SPX_WOTS_LEN; i += 4) { + for (j = 0; j < 4 && i+j < SPX_WOTS_LEN; j++) { + idx = idxs[i+j]; + set_chain_addr(addrs+j*8, idx); + bufs[j] = out + SPX_N * idx; + } + + /* As the chains are sorted in reverse order, we know that the first + * chain is the longest and the last one is the shortest. We keep + * an eye on whether the last chain is done and then on the one before, + * et cetera. */ + watching = 3; + done = 0; + while (i + watching >= SPX_WOTS_LEN) { + bufs[watching] = &empty[0]; + watching--; + } + + for (k = 0;; k++) { + while (k == steps[idxs[i+watching]]) { + bufs[watching] = &empty[0]; + if (watching == 0) { + done = 1; + break; + } + watching--; + } + if (done) { + break; + } + for (j = 0; j < watching + 1; j++) { + set_hash_addr(addrs+j*8, k + start[idxs[i+j]]); + } + + thashx4(bufs[0], bufs[1], bufs[2], bufs[3], + bufs[0], bufs[1], bufs[2], bufs[3], 1, ctx, addrs); + } + } +} + +/** + * base_w algorithm as described in draft. + * Interprets an array of bytes as integers in base w. + * This only works when log_w is a divisor of 8. + */ +static void base_w(unsigned int *output, const int out_len, + const unsigned char *input) +{ + int in = 0; + int out = 0; + unsigned char total; + int bits = 0; + int consumed; + + for (consumed = 0; consumed < out_len; consumed++) { + if (bits == 0) { + total = input[in]; + in++; + bits += 8; + } + bits -= SPX_WOTS_LOGW; + output[out] = (total >> bits) & (SPX_WOTS_W - 1); + out++; + } +} + +/* Computes the WOTS+ checksum over a message (in base_w). */ +static void wots_checksum(unsigned int *csum_base_w, + const unsigned int *msg_base_w) +{ + unsigned int csum = 0; + unsigned char csum_bytes[(SPX_WOTS_LEN2 * SPX_WOTS_LOGW + 7) / 8]; + unsigned int i; + + /* Compute checksum. */ + for (i = 0; i < SPX_WOTS_LEN1; i++) { + csum += SPX_WOTS_W - 1 - msg_base_w[i]; + } + + /* Convert checksum to base_w. */ + /* Make sure expected empty zero bits are the least significant bits. */ + csum = csum << ((8 - ((SPX_WOTS_LEN2 * SPX_WOTS_LOGW) % 8)) % 8); + ull_to_bytes(csum_bytes, sizeof(csum_bytes), csum); + base_w(csum_base_w, SPX_WOTS_LEN2, csum_bytes); +} + +/* Takes a message and derives the matching chain lengths. */ +void chain_lengths(unsigned int *lengths, const unsigned char *msg) +{ + base_w(lengths, SPX_WOTS_LEN1, msg); + wots_checksum(lengths + SPX_WOTS_LEN1, lengths); +} + +/** + * Takes a WOTS signature and an n-byte message, computes a WOTS public key. + * + * Writes the computed public key to 'pk'. + */ +void wots_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *msg, + const spx_ctx *ctx, uint32_t addr[8]) +{ + unsigned int steps[SPX_WOTS_LEN]; + unsigned int start[SPX_WOTS_LEN]; + uint32_t i; + + chain_lengths(start, msg); + + for (i = 0; i < SPX_WOTS_LEN; i++) { + steps[i] = SPX_WOTS_W - 1 - start[i]; + } + + gen_chains(pk, sig, start, steps, ctx, addr); +} + +/* + * This generates 4 sequential WOTS public keys + * It also generates the WOTS signature if leaf_info indicates + * that we're signing with one of these WOTS keys + */ +void wots_gen_leafx4(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info) { + struct leaf_info_x4 *info = v_info; + uint32_t *leaf_addr = info->leaf_addr; + uint32_t *pk_addr = info->pk_addr; + unsigned int i, j, k; + unsigned char pk_buffer[ 4 * SPX_WOTS_BYTES ]; + unsigned wots_offset = SPX_WOTS_BYTES; + unsigned char *buffer; + uint32_t wots_k_mask; + unsigned wots_sign_index; + + if (((leaf_idx ^ info->wots_sign_leaf) & ~3) == 0) { + /* We're traversing the leaf that's signing; generate the WOTS */ + /* signature */ + wots_k_mask = 0; + wots_sign_index = info->wots_sign_leaf & 3; /* Which of of the 4 */ + /* 4 slots do the signatures come from */ + } else { + /* Nope, we're just generating pk's; turn off the signature logic */ + wots_k_mask = (uint32_t)~0; + wots_sign_index = 0; + } + + for (j = 0; j < 4; j++) { + set_keypair_addr( leaf_addr + j*8, leaf_idx + j ); + set_keypair_addr( pk_addr + j*8, leaf_idx + j ); + } + + for (i = 0, buffer = pk_buffer; i < SPX_WOTS_LEN; i++, buffer += SPX_N) { + uint32_t wots_k = info->wots_steps[i] | wots_k_mask; /* Set wots_k to */ + /* the step if we're generating a signature, ~0 if we're not */ + + /* Start with the secret seed */ + for (j = 0; j < 4; j++) { + set_chain_addr(leaf_addr + j*8, i); + set_hash_addr(leaf_addr + j*8, 0); + set_type(leaf_addr + j*8, SPX_ADDR_TYPE_WOTSPRF); + } + prf_addrx4(buffer + 0*wots_offset, + buffer + 1*wots_offset, + buffer + 2*wots_offset, + buffer + 3*wots_offset, + ctx, leaf_addr); + + for (j = 0; j < 4; j++) { + set_type(leaf_addr + j*8, SPX_ADDR_TYPE_WOTS); + } + + /* Iterate down the WOTS chain */ + for (k=0;; k++) { + /* Check if one of the values we have needs to be saved as a */ + /* part of the WOTS signature */ + if (k == wots_k) { + memcpy( info->wots_sig + i * SPX_N, + buffer + wots_sign_index*wots_offset, SPX_N ); + } + + /* Check if we hit the top of the chain */ + if (k == SPX_WOTS_W - 1) break; + + /* Iterate one step on all 4 chains */ + for (j = 0; j < 4; j++) { + set_hash_addr(leaf_addr + j*8, k); + } + thashx4(buffer + 0*wots_offset, + buffer + 1*wots_offset, + buffer + 2*wots_offset, + buffer + 3*wots_offset, + buffer + 0*wots_offset, + buffer + 1*wots_offset, + buffer + 2*wots_offset, + buffer + 3*wots_offset, 1, ctx, leaf_addr); + } + } + + /* Do the final thash to generate the public keys */ + thashx4(dest + 0*SPX_N, + dest + 1*SPX_N, + dest + 2*SPX_N, + dest + 3*SPX_N, + pk_buffer + 0*wots_offset, + pk_buffer + 1*wots_offset, + pk_buffer + 2*wots_offset, + pk_buffer + 3*wots_offset, SPX_WOTS_LEN, ctx, pk_addr); +} diff --git a/src/crypto/pq/_s/shake-avx2/wots.h b/src/crypto/pq/_s/shake-avx2/wots.h new file mode 120000 index 0000000..8c327ea --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/wots.h @@ -0,0 +1 @@ +../ref/wots.h \ No newline at end of file diff --git a/src/crypto/pq/_s/shake-avx2/wotsx4.h b/src/crypto/pq/_s/shake-avx2/wotsx4.h new file mode 100644 index 0000000..2a311c5 --- /dev/null +++ b/src/crypto/pq/_s/shake-avx2/wotsx4.h @@ -0,0 +1,40 @@ +#if !defined( WOTSX4_H_ ) +#define WOTSX4_H_ + +#include +#include "params.h" + +/* + * This is here to provide an interface to the internal wots_gen_leafx4 + * routine. While this routine is not referenced in the package outside of + * wots.c, it is called from the stand-alone benchmark code to characterize + * the performance + */ +struct leaf_info_x4 { + unsigned char *wots_sig; + uint32_t wots_sign_leaf; /* The index of the WOTS we're using to sign */ + uint32_t *wots_steps; + uint32_t leaf_addr[4*8]; + uint32_t pk_addr[4*8]; +}; + +/* Macro to set the leaf_info to something 'benign', that is, it would */ +/* run with the same time as it does during the real signing process */ +/* Used only by the benchmark code */ +#define INITIALIZE_LEAF_INFO_X4(info, addr, step_buffer) { \ + info.wots_sig = 0; \ + info.wots_sign_leaf = ~0; \ + info.wots_steps = step_buffer; \ + int i; \ + for (i=0; i<4; i++) { \ + memcpy( &info.leaf_addr[8*i], addr, 32 ); \ + memcpy( &info.pk_addr[8*i], addr, 32 ); \ + } \ +} + +#define wots_gen_leafx4 SPX_NAMESPACE(wots_gen_leafx4) +void wots_gen_leafx4(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info); + +#endif /* WOTSX4_H_ */ diff --git a/src/crypto/pq/_s/vectors.py b/src/crypto/pq/_s/vectors.py new file mode 100755 index 0000000..0e0b0df --- /dev/null +++ b/src/crypto/pq/_s/vectors.py @@ -0,0 +1,101 @@ +#! /usr/bin/env python3 + +# Without arguments, generates sha256 sums of NIST KAT response files +# for each of the instances (which should match SHA256SUMS.) +# +# With two arguments, checks whether the sha256 sum of the given +# generated NIST KAT response file is correct, e.g.: +# +# ./vectors.py sphincs-shake-128s-simple shake-avx2 + +import multiprocessing +import subprocess +import itertools +import tempfile +import hashlib +import shutil +import os +import sys + +fns = ['shake', 'sha2', 'haraka'] +options = ["f", "s"] +sizes = [128, 192, 256] +thashes = ['robust', 'simple'] + +def nameFor(fn, opt, size, thash): + return f"sphincs-{fn}-{size}{opt}-{thash}" + +def make(fn, opt, size, thash, bindir, impl): + name = nameFor(fn, opt, size, thash) + overrides = [f'PARAMS=sphincs-{fn}-{size}{opt}', 'THASH='+thash] + + sys.stderr.write(f"Compiling {name} …\n") + sys.stderr.flush() + + subprocess.run(["make", "-C", impl, "clean"] + overrides, + stdout=subprocess.DEVNULL, stderr=sys.stderr, check=True) + subprocess.run(["make", '-j', "-C", impl, "PQCgenKAT_sign"] + overrides, + stdout=subprocess.DEVNULL, stderr=sys.stderr, check=True) + + shutil.move( + os.path.join(impl, 'PQCgenKAT_sign'), + os.path.join(bindir, name), + ) + + return (name, size) + +def run(name_size, bindir): + name, size = name_size + rsp = f'PQCsignKAT_{size//2}.rsp' + req = f'PQCsignKAT_{size//2}.req' + + with tempfile.TemporaryDirectory() as rundir: + sys.stderr.write(f"Running {name} …\n") + sys.stderr.flush() + + subprocess.run([os.path.join(bindir, name)], + stdout=subprocess.DEVNULL, stderr=sys.stderr, cwd=rundir, check=True) + with open(os.path.join(rundir, rsp), 'rb') as f: + h = hashlib.sha256(f.read()).hexdigest() + return f"{h} {name}" + +def generate_sums(): + with tempfile.TemporaryDirectory() as bindir: + with multiprocessing.Pool() as pool: + name_sizes = [] + for fn in fns: + for opt, size, thash in itertools.product(options, sizes, thashes): + name_sizes.append(make(fn, opt, size, thash, bindir, 'ref')) + + res = pool.starmap(run, zip(name_sizes, [bindir]*len(name_sizes))) + res.sort() + print('\n'.join(res)) + +def check_sum(name, impl): + line = None + with tempfile.TemporaryDirectory() as bindir: + for fn in fns: + for opt, size, thash in itertools.product( + options, sizes, thashes): + if nameFor(fn, opt, size, thash) != name: + continue + name_size = make(fn, opt, size, thash, bindir, impl) + line = run(name_size, bindir) + break + if not line: + sys.stderr.write("No such instance\n") + sys.exit(1) + with open('SHA256SUMS', 'r') as f: + if f.read().find(line + '\n') == -1: + sys.stderr.write(f"Test vector mismatch: {line}\n") + sys.exit(2) + sys.stderr.write("ok\n") + +if __name__ == '__main__': + if len(sys.argv) <= 1: + generate_sums() + elif len(sys.argv) == 3: + check_sum(sys.argv[1], sys.argv[2]) + else: + sys.stderr.write("Expect two or no arguments\n") + sys.exit(3) diff --git a/src/crypto/pq/mldsa65.commit b/src/crypto/pq/mldsa65.commit new file mode 100644 index 0000000..658580a --- /dev/null +++ b/src/crypto/pq/mldsa65.commit @@ -0,0 +1,2 @@ +d35ba3fe5449bee3e6d43e1f296c3ca818bd36be +2026-06-03 22:03:07 +0200 diff --git a/src/crypto/pq/mldsa65/LICENSE b/src/crypto/pq/mldsa65/LICENSE new file mode 100644 index 0000000..c086379 --- /dev/null +++ b/src/crypto/pq/mldsa65/LICENSE @@ -0,0 +1,7 @@ +Public Domain (https://creativecommons.org/share-your-work/public-domain/cc0/); +or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html) or GPL 2.0 License (http://www.gnu.org/licenses/gpl-2.0.html). + +For Keccak and the random number generator +we are using public-domain code from sources +and by authors listed in comments on top of +the respective files. diff --git a/src/crypto/pq/mldsa65/api.h b/src/crypto/pq/mldsa65/api.h new file mode 100644 index 0000000..032fa9f --- /dev/null +++ b/src/crypto/pq/mldsa65/api.h @@ -0,0 +1,98 @@ +#ifndef API_H +#define API_H + +#include +#include + +#define pqcrystals_dilithium2_PUBLICKEYBYTES 1312 +#define pqcrystals_dilithium2_SECRETKEYBYTES 2560 +#define pqcrystals_dilithium2_BYTES 2420 + +#define pqcrystals_dilithium2_ref_PUBLICKEYBYTES pqcrystals_dilithium2_PUBLICKEYBYTES +#define pqcrystals_dilithium2_ref_SECRETKEYBYTES pqcrystals_dilithium2_SECRETKEYBYTES +#define pqcrystals_dilithium2_ref_BYTES pqcrystals_dilithium2_BYTES + +int pqcrystals_dilithium2_ref_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium2_ref_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium2_ref(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium2_ref_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium2_ref_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +#define pqcrystals_dilithium3_PUBLICKEYBYTES 1952 +#define pqcrystals_dilithium3_SECRETKEYBYTES 4032 +#define pqcrystals_dilithium3_BYTES 3309 + +#define pqcrystals_dilithium3_ref_PUBLICKEYBYTES pqcrystals_dilithium3_PUBLICKEYBYTES +#define pqcrystals_dilithium3_ref_SECRETKEYBYTES pqcrystals_dilithium3_SECRETKEYBYTES +#define pqcrystals_dilithium3_ref_BYTES pqcrystals_dilithium3_BYTES + +int pqcrystals_dilithium3_ref_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium3_ref_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium3_ref(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium3_ref_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium3_ref_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +#define pqcrystals_dilithium5_PUBLICKEYBYTES 2592 +#define pqcrystals_dilithium5_SECRETKEYBYTES 4896 +#define pqcrystals_dilithium5_BYTES 4627 + +#define pqcrystals_dilithium5_ref_PUBLICKEYBYTES pqcrystals_dilithium5_PUBLICKEYBYTES +#define pqcrystals_dilithium5_ref_SECRETKEYBYTES pqcrystals_dilithium5_SECRETKEYBYTES +#define pqcrystals_dilithium5_ref_BYTES pqcrystals_dilithium5_BYTES + +int pqcrystals_dilithium5_ref_keypair(uint8_t *pk, uint8_t *sk); + +int pqcrystals_dilithium5_ref_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium5_ref(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +int pqcrystals_dilithium5_ref_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +int pqcrystals_dilithium5_ref_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + + +#endif diff --git a/src/crypto/pq/mldsa65/config.h b/src/crypto/pq/mldsa65/config.h new file mode 100644 index 0000000..98b8ccb --- /dev/null +++ b/src/crypto/pq/mldsa65/config.h @@ -0,0 +1,27 @@ +#ifndef CONFIG_H +#define CONFIG_H + +//#define DILITHIUM_MODE 2 +#define DILITHIUM_RANDOMIZED_SIGNING +//#define USE_RDPMC +//#define DBENCH + +#ifndef DILITHIUM_MODE +#define DILITHIUM_MODE 2 +#endif + +#if DILITHIUM_MODE == 2 +#define CRYPTO_ALGNAME "Dilithium2" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium2_ref +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium2_ref_##s +#elif DILITHIUM_MODE == 3 +#define CRYPTO_ALGNAME "Dilithium3" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium3_ref +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium3_ref_##s +#elif DILITHIUM_MODE == 5 +#define CRYPTO_ALGNAME "Dilithium5" +#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium5_ref +#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium5_ref_##s +#endif + +#endif diff --git a/src/crypto/pq/mldsa65/fips202.c b/src/crypto/pq/mldsa65/fips202.c new file mode 100644 index 0000000..2afe799 --- /dev/null +++ b/src/crypto/pq/mldsa65/fips202.c @@ -0,0 +1,774 @@ +/* Based on the public domain implementation in crypto_hash/keccakc512/simple/ from + * http://bench.cr.yp.to/supercop.html by Ronny Van Keer and the public domain "TweetFips202" + * implementation from https://twitter.com/tweetfips202 by Gilles Van Assche, Daniel J. Bernstein, + * and Peter Schwabe */ + +#include +#include +#include "fips202.h" + +#define NROUNDS 24 +#define ROL(a, offset) ((a << offset) ^ (a >> (64-offset))) + +/************************************************* +* Name: load64 +* +* Description: Load 8 bytes into uint64_t in little-endian order +* +* Arguments: - const uint8_t *x: pointer to input byte array +* +* Returns the loaded 64-bit unsigned integer +**************************************************/ +static uint64_t load64(const uint8_t x[8]) { + unsigned int i; + uint64_t r = 0; + + for(i=0;i<8;i++) + r |= (uint64_t)x[i] << 8*i; + + return r; +} + +/************************************************* +* Name: store64 +* +* Description: Store a 64-bit integer to array of 8 bytes in little-endian order +* +* Arguments: - uint8_t *x: pointer to the output byte array (allocated) +* - uint64_t u: input 64-bit unsigned integer +**************************************************/ +static void store64(uint8_t x[8], uint64_t u) { + unsigned int i; + + for(i=0;i<8;i++) + x[i] = u >> 8*i; +} + +/* Keccak round constants */ +const uint64_t KeccakF_RoundConstants[NROUNDS] = { + (uint64_t)0x0000000000000001ULL, + (uint64_t)0x0000000000008082ULL, + (uint64_t)0x800000000000808aULL, + (uint64_t)0x8000000080008000ULL, + (uint64_t)0x000000000000808bULL, + (uint64_t)0x0000000080000001ULL, + (uint64_t)0x8000000080008081ULL, + (uint64_t)0x8000000000008009ULL, + (uint64_t)0x000000000000008aULL, + (uint64_t)0x0000000000000088ULL, + (uint64_t)0x0000000080008009ULL, + (uint64_t)0x000000008000000aULL, + (uint64_t)0x000000008000808bULL, + (uint64_t)0x800000000000008bULL, + (uint64_t)0x8000000000008089ULL, + (uint64_t)0x8000000000008003ULL, + (uint64_t)0x8000000000008002ULL, + (uint64_t)0x8000000000000080ULL, + (uint64_t)0x000000000000800aULL, + (uint64_t)0x800000008000000aULL, + (uint64_t)0x8000000080008081ULL, + (uint64_t)0x8000000000008080ULL, + (uint64_t)0x0000000080000001ULL, + (uint64_t)0x8000000080008008ULL +}; + +/************************************************* +* Name: KeccakF1600_StatePermute +* +* Description: The Keccak F1600 Permutation +* +* Arguments: - uint64_t *state: pointer to input/output Keccak state +**************************************************/ +static void KeccakF1600_StatePermute(uint64_t state[25]) +{ + int round; + + uint64_t Aba, Abe, Abi, Abo, Abu; + uint64_t Aga, Age, Agi, Ago, Agu; + uint64_t Aka, Ake, Aki, Ako, Aku; + uint64_t Ama, Ame, Ami, Amo, Amu; + uint64_t Asa, Ase, Asi, Aso, Asu; + uint64_t BCa, BCe, BCi, BCo, BCu; + uint64_t Da, De, Di, Do, Du; + uint64_t Eba, Ebe, Ebi, Ebo, Ebu; + uint64_t Ega, Ege, Egi, Ego, Egu; + uint64_t Eka, Eke, Eki, Eko, Eku; + uint64_t Ema, Eme, Emi, Emo, Emu; + uint64_t Esa, Ese, Esi, Eso, Esu; + + //copyFromState(A, state) + Aba = state[ 0]; + Abe = state[ 1]; + Abi = state[ 2]; + Abo = state[ 3]; + Abu = state[ 4]; + Aga = state[ 5]; + Age = state[ 6]; + Agi = state[ 7]; + Ago = state[ 8]; + Agu = state[ 9]; + Aka = state[10]; + Ake = state[11]; + Aki = state[12]; + Ako = state[13]; + Aku = state[14]; + Ama = state[15]; + Ame = state[16]; + Ami = state[17]; + Amo = state[18]; + Amu = state[19]; + Asa = state[20]; + Ase = state[21]; + Asi = state[22]; + Aso = state[23]; + Asu = state[24]; + + for(round = 0; round < NROUNDS; round += 2) { + // prepareTheta + BCa = Aba^Aga^Aka^Ama^Asa; + BCe = Abe^Age^Ake^Ame^Ase; + BCi = Abi^Agi^Aki^Ami^Asi; + BCo = Abo^Ago^Ako^Amo^Aso; + BCu = Abu^Agu^Aku^Amu^Asu; + + //thetaRhoPiChiIotaPrepareTheta(round, A, E) + Da = BCu^ROL(BCe, 1); + De = BCa^ROL(BCi, 1); + Di = BCe^ROL(BCo, 1); + Do = BCi^ROL(BCu, 1); + Du = BCo^ROL(BCa, 1); + + Aba ^= Da; + BCa = Aba; + Age ^= De; + BCe = ROL(Age, 44); + Aki ^= Di; + BCi = ROL(Aki, 43); + Amo ^= Do; + BCo = ROL(Amo, 21); + Asu ^= Du; + BCu = ROL(Asu, 14); + Eba = BCa ^((~BCe)& BCi ); + Eba ^= (uint64_t)KeccakF_RoundConstants[round]; + Ebe = BCe ^((~BCi)& BCo ); + Ebi = BCi ^((~BCo)& BCu ); + Ebo = BCo ^((~BCu)& BCa ); + Ebu = BCu ^((~BCa)& BCe ); + + Abo ^= Do; + BCa = ROL(Abo, 28); + Agu ^= Du; + BCe = ROL(Agu, 20); + Aka ^= Da; + BCi = ROL(Aka, 3); + Ame ^= De; + BCo = ROL(Ame, 45); + Asi ^= Di; + BCu = ROL(Asi, 61); + Ega = BCa ^((~BCe)& BCi ); + Ege = BCe ^((~BCi)& BCo ); + Egi = BCi ^((~BCo)& BCu ); + Ego = BCo ^((~BCu)& BCa ); + Egu = BCu ^((~BCa)& BCe ); + + Abe ^= De; + BCa = ROL(Abe, 1); + Agi ^= Di; + BCe = ROL(Agi, 6); + Ako ^= Do; + BCi = ROL(Ako, 25); + Amu ^= Du; + BCo = ROL(Amu, 8); + Asa ^= Da; + BCu = ROL(Asa, 18); + Eka = BCa ^((~BCe)& BCi ); + Eke = BCe ^((~BCi)& BCo ); + Eki = BCi ^((~BCo)& BCu ); + Eko = BCo ^((~BCu)& BCa ); + Eku = BCu ^((~BCa)& BCe ); + + Abu ^= Du; + BCa = ROL(Abu, 27); + Aga ^= Da; + BCe = ROL(Aga, 36); + Ake ^= De; + BCi = ROL(Ake, 10); + Ami ^= Di; + BCo = ROL(Ami, 15); + Aso ^= Do; + BCu = ROL(Aso, 56); + Ema = BCa ^((~BCe)& BCi ); + Eme = BCe ^((~BCi)& BCo ); + Emi = BCi ^((~BCo)& BCu ); + Emo = BCo ^((~BCu)& BCa ); + Emu = BCu ^((~BCa)& BCe ); + + Abi ^= Di; + BCa = ROL(Abi, 62); + Ago ^= Do; + BCe = ROL(Ago, 55); + Aku ^= Du; + BCi = ROL(Aku, 39); + Ama ^= Da; + BCo = ROL(Ama, 41); + Ase ^= De; + BCu = ROL(Ase, 2); + Esa = BCa ^((~BCe)& BCi ); + Ese = BCe ^((~BCi)& BCo ); + Esi = BCi ^((~BCo)& BCu ); + Eso = BCo ^((~BCu)& BCa ); + Esu = BCu ^((~BCa)& BCe ); + + // prepareTheta + BCa = Eba^Ega^Eka^Ema^Esa; + BCe = Ebe^Ege^Eke^Eme^Ese; + BCi = Ebi^Egi^Eki^Emi^Esi; + BCo = Ebo^Ego^Eko^Emo^Eso; + BCu = Ebu^Egu^Eku^Emu^Esu; + + //thetaRhoPiChiIotaPrepareTheta(round+1, E, A) + Da = BCu^ROL(BCe, 1); + De = BCa^ROL(BCi, 1); + Di = BCe^ROL(BCo, 1); + Do = BCi^ROL(BCu, 1); + Du = BCo^ROL(BCa, 1); + + Eba ^= Da; + BCa = Eba; + Ege ^= De; + BCe = ROL(Ege, 44); + Eki ^= Di; + BCi = ROL(Eki, 43); + Emo ^= Do; + BCo = ROL(Emo, 21); + Esu ^= Du; + BCu = ROL(Esu, 14); + Aba = BCa ^((~BCe)& BCi ); + Aba ^= (uint64_t)KeccakF_RoundConstants[round+1]; + Abe = BCe ^((~BCi)& BCo ); + Abi = BCi ^((~BCo)& BCu ); + Abo = BCo ^((~BCu)& BCa ); + Abu = BCu ^((~BCa)& BCe ); + + Ebo ^= Do; + BCa = ROL(Ebo, 28); + Egu ^= Du; + BCe = ROL(Egu, 20); + Eka ^= Da; + BCi = ROL(Eka, 3); + Eme ^= De; + BCo = ROL(Eme, 45); + Esi ^= Di; + BCu = ROL(Esi, 61); + Aga = BCa ^((~BCe)& BCi ); + Age = BCe ^((~BCi)& BCo ); + Agi = BCi ^((~BCo)& BCu ); + Ago = BCo ^((~BCu)& BCa ); + Agu = BCu ^((~BCa)& BCe ); + + Ebe ^= De; + BCa = ROL(Ebe, 1); + Egi ^= Di; + BCe = ROL(Egi, 6); + Eko ^= Do; + BCi = ROL(Eko, 25); + Emu ^= Du; + BCo = ROL(Emu, 8); + Esa ^= Da; + BCu = ROL(Esa, 18); + Aka = BCa ^((~BCe)& BCi ); + Ake = BCe ^((~BCi)& BCo ); + Aki = BCi ^((~BCo)& BCu ); + Ako = BCo ^((~BCu)& BCa ); + Aku = BCu ^((~BCa)& BCe ); + + Ebu ^= Du; + BCa = ROL(Ebu, 27); + Ega ^= Da; + BCe = ROL(Ega, 36); + Eke ^= De; + BCi = ROL(Eke, 10); + Emi ^= Di; + BCo = ROL(Emi, 15); + Eso ^= Do; + BCu = ROL(Eso, 56); + Ama = BCa ^((~BCe)& BCi ); + Ame = BCe ^((~BCi)& BCo ); + Ami = BCi ^((~BCo)& BCu ); + Amo = BCo ^((~BCu)& BCa ); + Amu = BCu ^((~BCa)& BCe ); + + Ebi ^= Di; + BCa = ROL(Ebi, 62); + Ego ^= Do; + BCe = ROL(Ego, 55); + Eku ^= Du; + BCi = ROL(Eku, 39); + Ema ^= Da; + BCo = ROL(Ema, 41); + Ese ^= De; + BCu = ROL(Ese, 2); + Asa = BCa ^((~BCe)& BCi ); + Ase = BCe ^((~BCi)& BCo ); + Asi = BCi ^((~BCo)& BCu ); + Aso = BCo ^((~BCu)& BCa ); + Asu = BCu ^((~BCa)& BCe ); + } + + //copyToState(state, A) + state[ 0] = Aba; + state[ 1] = Abe; + state[ 2] = Abi; + state[ 3] = Abo; + state[ 4] = Abu; + state[ 5] = Aga; + state[ 6] = Age; + state[ 7] = Agi; + state[ 8] = Ago; + state[ 9] = Agu; + state[10] = Aka; + state[11] = Ake; + state[12] = Aki; + state[13] = Ako; + state[14] = Aku; + state[15] = Ama; + state[16] = Ame; + state[17] = Ami; + state[18] = Amo; + state[19] = Amu; + state[20] = Asa; + state[21] = Ase; + state[22] = Asi; + state[23] = Aso; + state[24] = Asu; +} + +/************************************************* +* Name: keccak_init +* +* Description: Initializes the Keccak state. +* +* Arguments: - uint64_t *s: pointer to Keccak state +**************************************************/ +static void keccak_init(uint64_t s[25]) +{ + unsigned int i; + for(i=0;i<25;i++) + s[i] = 0; +} + +/************************************************* +* Name: keccak_absorb +* +* Description: Absorb step of Keccak; incremental. +* +* Arguments: - uint64_t *s: pointer to Keccak state +* - unsigned int pos: position in current block to be absorbed +* - unsigned int r: rate in bytes (e.g., 168 for SHAKE128) +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +* +* Returns new position pos in current block +**************************************************/ +static unsigned int keccak_absorb(uint64_t s[25], + unsigned int pos, + unsigned int r, + const uint8_t *in, + size_t inlen) +{ + unsigned int i; + + while(pos+inlen >= r) { + for(i=pos;i> 8*(i%8); + outlen -= i-pos; + pos = i; + } + + return pos; +} + + +/************************************************* +* Name: keccak_absorb_once +* +* Description: Absorb step of Keccak; +* non-incremental, starts by zeroeing the state. +* +* Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state +* - unsigned int r: rate in bytes (e.g., 168 for SHAKE128) +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +* - uint8_t p: domain-separation byte for different Keccak-derived functions +**************************************************/ +static void keccak_absorb_once(uint64_t s[25], + unsigned int r, + const uint8_t *in, + size_t inlen, + uint8_t p) +{ + unsigned int i; + + for(i=0;i<25;i++) + s[i] = 0; + + while(inlen >= r) { + for(i=0;is); + state->pos = 0; +} + +/************************************************* +* Name: shake128_absorb +* +* Description: Absorb step of the SHAKE128 XOF; incremental. +* +* Arguments: - keccak_state *state: pointer to (initialized) output Keccak state +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +**************************************************/ +void shake128_absorb(keccak_state *state, const uint8_t *in, size_t inlen) +{ + state->pos = keccak_absorb(state->s, state->pos, SHAKE128_RATE, in, inlen); +} + +/************************************************* +* Name: shake128_finalize +* +* Description: Finalize absorb step of the SHAKE128 XOF. +* +* Arguments: - keccak_state *state: pointer to Keccak state +**************************************************/ +void shake128_finalize(keccak_state *state) +{ + keccak_finalize(state->s, state->pos, SHAKE128_RATE, 0x1F); + state->pos = SHAKE128_RATE; +} + +/************************************************* +* Name: shake128_squeeze +* +* Description: Squeeze step of SHAKE128 XOF. Squeezes arbitraily many +* bytes. Can be called multiple times to keep squeezing. +* +* Arguments: - uint8_t *out: pointer to output blocks +* - size_t outlen : number of bytes to be squeezed (written to output) +* - keccak_state *s: pointer to input/output Keccak state +**************************************************/ +void shake128_squeeze(uint8_t *out, size_t outlen, keccak_state *state) +{ + state->pos = keccak_squeeze(out, outlen, state->s, state->pos, SHAKE128_RATE); +} + +/************************************************* +* Name: shake128_absorb_once +* +* Description: Initialize, absorb into and finalize SHAKE128 XOF; non-incremental. +* +* Arguments: - keccak_state *state: pointer to (uninitialized) output Keccak state +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +**************************************************/ +void shake128_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen) +{ + keccak_absorb_once(state->s, SHAKE128_RATE, in, inlen, 0x1F); + state->pos = SHAKE128_RATE; +} + +/************************************************* +* Name: shake128_squeezeblocks +* +* Description: Squeeze step of SHAKE128 XOF. Squeezes full blocks of +* SHAKE128_RATE bytes each. Can be called multiple times +* to keep squeezing. Assumes new block has not yet been +* started (state->pos = SHAKE128_RATE). +* +* Arguments: - uint8_t *out: pointer to output blocks +* - size_t nblocks: number of blocks to be squeezed (written to output) +* - keccak_state *s: pointer to input/output Keccak state +**************************************************/ +void shake128_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state) +{ + keccak_squeezeblocks(out, nblocks, state->s, SHAKE128_RATE); +} + +/************************************************* +* Name: shake256_init +* +* Description: Initilizes Keccak state for use as SHAKE256 XOF +* +* Arguments: - keccak_state *state: pointer to (uninitialized) Keccak state +**************************************************/ +void shake256_init(keccak_state *state) +{ + keccak_init(state->s); + state->pos = 0; +} + +/************************************************* +* Name: shake256_absorb +* +* Description: Absorb step of the SHAKE256 XOF; incremental. +* +* Arguments: - keccak_state *state: pointer to (initialized) output Keccak state +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +**************************************************/ +void shake256_absorb(keccak_state *state, const uint8_t *in, size_t inlen) +{ + state->pos = keccak_absorb(state->s, state->pos, SHAKE256_RATE, in, inlen); +} + +/************************************************* +* Name: shake256_finalize +* +* Description: Finalize absorb step of the SHAKE256 XOF. +* +* Arguments: - keccak_state *state: pointer to Keccak state +**************************************************/ +void shake256_finalize(keccak_state *state) +{ + keccak_finalize(state->s, state->pos, SHAKE256_RATE, 0x1F); + state->pos = SHAKE256_RATE; +} + +/************************************************* +* Name: shake256_squeeze +* +* Description: Squeeze step of SHAKE256 XOF. Squeezes arbitraily many +* bytes. Can be called multiple times to keep squeezing. +* +* Arguments: - uint8_t *out: pointer to output blocks +* - size_t outlen : number of bytes to be squeezed (written to output) +* - keccak_state *s: pointer to input/output Keccak state +**************************************************/ +void shake256_squeeze(uint8_t *out, size_t outlen, keccak_state *state) +{ + state->pos = keccak_squeeze(out, outlen, state->s, state->pos, SHAKE256_RATE); +} + +/************************************************* +* Name: shake256_absorb_once +* +* Description: Initialize, absorb into and finalize SHAKE256 XOF; non-incremental. +* +* Arguments: - keccak_state *state: pointer to (uninitialized) output Keccak state +* - const uint8_t *in: pointer to input to be absorbed into s +* - size_t inlen: length of input in bytes +**************************************************/ +void shake256_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen) +{ + keccak_absorb_once(state->s, SHAKE256_RATE, in, inlen, 0x1F); + state->pos = SHAKE256_RATE; +} + +/************************************************* +* Name: shake256_squeezeblocks +* +* Description: Squeeze step of SHAKE256 XOF. Squeezes full blocks of +* SHAKE256_RATE bytes each. Can be called multiple times +* to keep squeezing. Assumes next block has not yet been +* started (state->pos = SHAKE256_RATE). +* +* Arguments: - uint8_t *out: pointer to output blocks +* - size_t nblocks: number of blocks to be squeezed (written to output) +* - keccak_state *s: pointer to input/output Keccak state +**************************************************/ +void shake256_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state) +{ + keccak_squeezeblocks(out, nblocks, state->s, SHAKE256_RATE); +} + +/************************************************* +* Name: shake128 +* +* Description: SHAKE128 XOF with non-incremental API +* +* Arguments: - uint8_t *out: pointer to output +* - size_t outlen: requested output length in bytes +* - const uint8_t *in: pointer to input +* - size_t inlen: length of input in bytes +**************************************************/ +void shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) +{ + size_t nblocks; + keccak_state state; + + shake128_absorb_once(&state, in, inlen); + nblocks = outlen/SHAKE128_RATE; + shake128_squeezeblocks(out, nblocks, &state); + outlen -= nblocks*SHAKE128_RATE; + out += nblocks*SHAKE128_RATE; + shake128_squeeze(out, outlen, &state); +} + +/************************************************* +* Name: shake256 +* +* Description: SHAKE256 XOF with non-incremental API +* +* Arguments: - uint8_t *out: pointer to output +* - size_t outlen: requested output length in bytes +* - const uint8_t *in: pointer to input +* - size_t inlen: length of input in bytes +**************************************************/ +void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) +{ + size_t nblocks; + keccak_state state; + + shake256_absorb_once(&state, in, inlen); + nblocks = outlen/SHAKE256_RATE; + shake256_squeezeblocks(out, nblocks, &state); + outlen -= nblocks*SHAKE256_RATE; + out += nblocks*SHAKE256_RATE; + shake256_squeeze(out, outlen, &state); +} + +/************************************************* +* Name: sha3_256 +* +* Description: SHA3-256 with non-incremental API +* +* Arguments: - uint8_t *h: pointer to output (32 bytes) +* - const uint8_t *in: pointer to input +* - size_t inlen: length of input in bytes +**************************************************/ +void sha3_256(uint8_t h[32], const uint8_t *in, size_t inlen) +{ + unsigned int i; + uint64_t s[25]; + + keccak_absorb_once(s, SHA3_256_RATE, in, inlen, 0x06); + KeccakF1600_StatePermute(s); + for(i=0;i<4;i++) + store64(h+8*i,s[i]); +} + +/************************************************* +* Name: sha3_512 +* +* Description: SHA3-512 with non-incremental API +* +* Arguments: - uint8_t *h: pointer to output (64 bytes) +* - const uint8_t *in: pointer to input +* - size_t inlen: length of input in bytes +**************************************************/ +void sha3_512(uint8_t h[64], const uint8_t *in, size_t inlen) +{ + unsigned int i; + uint64_t s[25]; + + keccak_absorb_once(s, SHA3_512_RATE, in, inlen, 0x06); + KeccakF1600_StatePermute(s); + for(i=0;i<8;i++) + store64(h+8*i,s[i]); +} diff --git a/src/crypto/pq/mldsa65/fips202.h b/src/crypto/pq/mldsa65/fips202.h new file mode 100644 index 0000000..c37f535 --- /dev/null +++ b/src/crypto/pq/mldsa65/fips202.h @@ -0,0 +1,57 @@ +#ifndef FIPS202_H +#define FIPS202_H + +#include +#include + +#define SHAKE128_RATE 168 +#define SHAKE256_RATE 136 +#define SHA3_256_RATE 136 +#define SHA3_512_RATE 72 + +#define FIPS202_NAMESPACE(s) pqcrystals_dilithium_fips202_ref_##s + +typedef struct { + uint64_t s[25]; + unsigned int pos; +} keccak_state; + +#define KeccakF_RoundConstants FIPS202_NAMESPACE(KeccakF_RoundConstants) +extern const uint64_t KeccakF_RoundConstants[]; + +#define shake128_init FIPS202_NAMESPACE(shake128_init) +void shake128_init(keccak_state *state); +#define shake128_absorb FIPS202_NAMESPACE(shake128_absorb) +void shake128_absorb(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake128_finalize FIPS202_NAMESPACE(shake128_finalize) +void shake128_finalize(keccak_state *state); +#define shake128_squeeze FIPS202_NAMESPACE(shake128_squeeze) +void shake128_squeeze(uint8_t *out, size_t outlen, keccak_state *state); +#define shake128_absorb_once FIPS202_NAMESPACE(shake128_absorb_once) +void shake128_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake128_squeezeblocks FIPS202_NAMESPACE(shake128_squeezeblocks) +void shake128_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state); + +#define shake256_init FIPS202_NAMESPACE(shake256_init) +void shake256_init(keccak_state *state); +#define shake256_absorb FIPS202_NAMESPACE(shake256_absorb) +void shake256_absorb(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake256_finalize FIPS202_NAMESPACE(shake256_finalize) +void shake256_finalize(keccak_state *state); +#define shake256_squeeze FIPS202_NAMESPACE(shake256_squeeze) +void shake256_squeeze(uint8_t *out, size_t outlen, keccak_state *state); +#define shake256_absorb_once FIPS202_NAMESPACE(shake256_absorb_once) +void shake256_absorb_once(keccak_state *state, const uint8_t *in, size_t inlen); +#define shake256_squeezeblocks FIPS202_NAMESPACE(shake256_squeezeblocks) +void shake256_squeezeblocks(uint8_t *out, size_t nblocks, keccak_state *state); + +#define shake128 FIPS202_NAMESPACE(shake128) +void shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen); +#define shake256 FIPS202_NAMESPACE(shake256) +void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen); +#define sha3_256 FIPS202_NAMESPACE(sha3_256) +void sha3_256(uint8_t h[32], const uint8_t *in, size_t inlen); +#define sha3_512 FIPS202_NAMESPACE(sha3_512) +void sha3_512(uint8_t h[64], const uint8_t *in, size_t inlen); + +#endif diff --git a/src/crypto/pq/mldsa65/ntt.c b/src/crypto/pq/mldsa65/ntt.c new file mode 100644 index 0000000..5ea8b53 --- /dev/null +++ b/src/crypto/pq/mldsa65/ntt.c @@ -0,0 +1,98 @@ +#include +#include "params.h" +#include "ntt.h" +#include "reduce.h" + +static const int32_t zetas[N] = { + 0, 25847, -2608894, -518909, 237124, -777960, -876248, 466468, + 1826347, 2353451, -359251, -2091905, 3119733, -2884855, 3111497, 2680103, + 2725464, 1024112, -1079900, 3585928, -549488, -1119584, 2619752, -2108549, + -2118186, -3859737, -1399561, -3277672, 1757237, -19422, 4010497, 280005, + 2706023, 95776, 3077325, 3530437, -1661693, -3592148, -2537516, 3915439, + -3861115, -3043716, 3574422, -2867647, 3539968, -300467, 2348700, -539299, + -1699267, -1643818, 3505694, -3821735, 3507263, -2140649, -1600420, 3699596, + 811944, 531354, 954230, 3881043, 3900724, -2556880, 2071892, -2797779, + -3930395, -1528703, -3677745, -3041255, -1452451, 3475950, 2176455, -1585221, + -1257611, 1939314, -4083598, -1000202, -3190144, -3157330, -3632928, 126922, + 3412210, -983419, 2147896, 2715295, -2967645, -3693493, -411027, -2477047, + -671102, -1228525, -22981, -1308169, -381987, 1349076, 1852771, -1430430, + -3343383, 264944, 508951, 3097992, 44288, -1100098, 904516, 3958618, + -3724342, -8578, 1653064, -3249728, 2389356, -210977, 759969, -1316856, + 189548, -3553272, 3159746, -1851402, -2409325, -177440, 1315589, 1341330, + 1285669, -1584928, -812732, -1439742, -3019102, -3881060, -3628969, 3839961, + 2091667, 3407706, 2316500, 3817976, -3342478, 2244091, -2446433, -3562462, + 266997, 2434439, -1235728, 3513181, -3520352, -3759364, -1197226, -3193378, + 900702, 1859098, 909542, 819034, 495491, -1613174, -43260, -522500, + -655327, -3122442, 2031748, 3207046, -3556995, -525098, -768622, -3595838, + 342297, 286988, -2437823, 4108315, 3437287, -3342277, 1735879, 203044, + 2842341, 2691481, -2590150, 1265009, 4055324, 1247620, 2486353, 1595974, + -3767016, 1250494, 2635921, -3548272, -2994039, 1869119, 1903435, -1050970, + -1333058, 1237275, -3318210, -1430225, -451100, 1312455, 3306115, -1962642, + -1279661, 1917081, -2546312, -1374803, 1500165, 777191, 2235880, 3406031, + -542412, -2831860, -1671176, -1846953, -2584293, -3724270, 594136, -3776993, + -2013608, 2432395, 2454455, -164721, 1957272, 3369112, 185531, -1207385, + -3183426, 162844, 1616392, 3014001, 810149, 1652634, -3694233, -1799107, + -3038916, 3523897, 3866901, 269760, 2213111, -975884, 1717735, 472078, + -426683, 1723600, -1803090, 1910376, -1667432, -1104333, -260646, -3833893, + -2939036, -2235985, -420899, -2286327, 183443, -976891, 1612842, -3545687, + -554416, 3919660, -48306, -1362209, 3937738, 1400424, -846154, 1976782 +}; + +/************************************************* +* Name: ntt +* +* Description: Forward NTT, in-place. No modular reduction is performed after +* additions or subtractions. Output vector is in bitreversed order. +* +* Arguments: - uint32_t p[N]: input/output coefficient array +**************************************************/ +void ntt(int32_t a[N]) { + unsigned int len, start, j, k; + int32_t zeta, t; + + k = 0; + for(len = 128; len > 0; len >>= 1) { + for(start = 0; start < N; start = j + len) { + zeta = zetas[++k]; + for(j = start; j < start + len; ++j) { + t = montgomery_reduce((int64_t)zeta * a[j + len]); + a[j + len] = a[j] - t; + a[j] = a[j] + t; + } + } + } +} + +/************************************************* +* Name: invntt_tomont +* +* Description: Inverse NTT and multiplication by Montgomery factor 2^32. +* In-place. No modular reductions after additions or +* subtractions; input coefficients need to be smaller than +* Q in absolute value. Output coefficient are smaller than Q in +* absolute value. +* +* Arguments: - uint32_t p[N]: input/output coefficient array +**************************************************/ +void invntt_tomont(int32_t a[N]) { + unsigned int start, len, j, k; + int32_t t, zeta; + const int32_t f = 41978; // mont^2/256 + + k = 256; + for(len = 1; len < N; len <<= 1) { + for(start = 0; start < N; start = j + len) { + zeta = -zetas[--k]; + for(j = start; j < start + len; ++j) { + t = a[j]; + a[j] = t + a[j + len]; + a[j + len] = t - a[j + len]; + a[j + len] = montgomery_reduce((int64_t)zeta * a[j + len]); + } + } + } + + for(j = 0; j < N; ++j) { + a[j] = montgomery_reduce((int64_t)f * a[j]); + } +} diff --git a/src/crypto/pq/mldsa65/ntt.h b/src/crypto/pq/mldsa65/ntt.h new file mode 100644 index 0000000..731132d --- /dev/null +++ b/src/crypto/pq/mldsa65/ntt.h @@ -0,0 +1,13 @@ +#ifndef NTT_H +#define NTT_H + +#include +#include "params.h" + +#define ntt DILITHIUM_NAMESPACE(ntt) +void ntt(int32_t a[N]); + +#define invntt_tomont DILITHIUM_NAMESPACE(invntt_tomont) +void invntt_tomont(int32_t a[N]); + +#endif diff --git a/src/crypto/pq/mldsa65/packing.c b/src/crypto/pq/mldsa65/packing.c new file mode 100644 index 0000000..039a686 --- /dev/null +++ b/src/crypto/pq/mldsa65/packing.c @@ -0,0 +1,237 @@ +#include "params.h" +#include "packing.h" +#include "polyvec.h" +#include "poly.h" + +/************************************************* +* Name: pack_pk +* +* Description: Bit-pack public key pk = (rho, t1). +* +* Arguments: - uint8_t pk[]: output byte array +* - const uint8_t rho[]: byte array containing rho +* - const polyveck *t1: pointer to vector t1 +**************************************************/ +void pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES], + const uint8_t rho[SEEDBYTES], + const polyveck *t1) +{ + unsigned int i; + + for(i = 0; i < SEEDBYTES; ++i) + pk[i] = rho[i]; + pk += SEEDBYTES; + + for(i = 0; i < K; ++i) + polyt1_pack(pk + i*POLYT1_PACKEDBYTES, &t1->vec[i]); +} + +/************************************************* +* Name: unpack_pk +* +* Description: Unpack public key pk = (rho, t1). +* +* Arguments: - const uint8_t rho[]: output byte array for rho +* - const polyveck *t1: pointer to output vector t1 +* - uint8_t pk[]: byte array containing bit-packed pk +**************************************************/ +void unpack_pk(uint8_t rho[SEEDBYTES], + polyveck *t1, + const uint8_t pk[CRYPTO_PUBLICKEYBYTES]) +{ + unsigned int i; + + for(i = 0; i < SEEDBYTES; ++i) + rho[i] = pk[i]; + pk += SEEDBYTES; + + for(i = 0; i < K; ++i) + polyt1_unpack(&t1->vec[i], pk + i*POLYT1_PACKEDBYTES); +} + +/************************************************* +* Name: pack_sk +* +* Description: Bit-pack secret key sk = (rho, tr, key, t0, s1, s2). +* +* Arguments: - uint8_t sk[]: output byte array +* - const uint8_t rho[]: byte array containing rho +* - const uint8_t tr[]: byte array containing tr +* - const uint8_t key[]: byte array containing key +* - const polyveck *t0: pointer to vector t0 +* - const polyvecl *s1: pointer to vector s1 +* - const polyveck *s2: pointer to vector s2 +**************************************************/ +void pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES], + const uint8_t rho[SEEDBYTES], + const uint8_t tr[TRBYTES], + const uint8_t key[SEEDBYTES], + const polyveck *t0, + const polyvecl *s1, + const polyveck *s2) +{ + unsigned int i; + + for(i = 0; i < SEEDBYTES; ++i) + sk[i] = rho[i]; + sk += SEEDBYTES; + + for(i = 0; i < SEEDBYTES; ++i) + sk[i] = key[i]; + sk += SEEDBYTES; + + for(i = 0; i < TRBYTES; ++i) + sk[i] = tr[i]; + sk += TRBYTES; + + for(i = 0; i < L; ++i) + polyeta_pack(sk + i*POLYETA_PACKEDBYTES, &s1->vec[i]); + sk += L*POLYETA_PACKEDBYTES; + + for(i = 0; i < K; ++i) + polyeta_pack(sk + i*POLYETA_PACKEDBYTES, &s2->vec[i]); + sk += K*POLYETA_PACKEDBYTES; + + for(i = 0; i < K; ++i) + polyt0_pack(sk + i*POLYT0_PACKEDBYTES, &t0->vec[i]); +} + +/************************************************* +* Name: unpack_sk +* +* Description: Unpack secret key sk = (rho, tr, key, t0, s1, s2). +* +* Arguments: - const uint8_t rho[]: output byte array for rho +* - const uint8_t tr[]: output byte array for tr +* - const uint8_t key[]: output byte array for key +* - const polyveck *t0: pointer to output vector t0 +* - const polyvecl *s1: pointer to output vector s1 +* - const polyveck *s2: pointer to output vector s2 +* - uint8_t sk[]: byte array containing bit-packed sk +**************************************************/ +void unpack_sk(uint8_t rho[SEEDBYTES], + uint8_t tr[TRBYTES], + uint8_t key[SEEDBYTES], + polyveck *t0, + polyvecl *s1, + polyveck *s2, + const uint8_t sk[CRYPTO_SECRETKEYBYTES]) +{ + unsigned int i; + + for(i = 0; i < SEEDBYTES; ++i) + rho[i] = sk[i]; + sk += SEEDBYTES; + + for(i = 0; i < SEEDBYTES; ++i) + key[i] = sk[i]; + sk += SEEDBYTES; + + for(i = 0; i < TRBYTES; ++i) + tr[i] = sk[i]; + sk += TRBYTES; + + for(i=0; i < L; ++i) + polyeta_unpack(&s1->vec[i], sk + i*POLYETA_PACKEDBYTES); + sk += L*POLYETA_PACKEDBYTES; + + for(i=0; i < K; ++i) + polyeta_unpack(&s2->vec[i], sk + i*POLYETA_PACKEDBYTES); + sk += K*POLYETA_PACKEDBYTES; + + for(i=0; i < K; ++i) + polyt0_unpack(&t0->vec[i], sk + i*POLYT0_PACKEDBYTES); +} + +/************************************************* +* Name: pack_sig +* +* Description: Bit-pack signature sig = (c, z, h). +* +* Arguments: - uint8_t sig[]: output byte array +* - const uint8_t *c: pointer to challenge hash length SEEDBYTES +* - const polyvecl *z: pointer to vector z +* - const polyveck *h: pointer to hint vector h +**************************************************/ +void pack_sig(uint8_t sig[CRYPTO_BYTES], + const uint8_t c[CTILDEBYTES], + const polyvecl *z, + const polyveck *h) +{ + unsigned int i, j, k; + + for(i=0; i < CTILDEBYTES; ++i) + sig[i] = c[i]; + sig += CTILDEBYTES; + + for(i = 0; i < L; ++i) + polyz_pack(sig + i*POLYZ_PACKEDBYTES, &z->vec[i]); + sig += L*POLYZ_PACKEDBYTES; + + /* Encode h */ + for(i = 0; i < OMEGA + K; ++i) + sig[i] = 0; + + k = 0; + for(i = 0; i < K; ++i) { + for(j = 0; j < N; ++j) + if(h->vec[i].coeffs[j] != 0) + sig[k++] = j; + + sig[OMEGA + i] = k; + } +} + +/************************************************* +* Name: unpack_sig +* +* Description: Unpack signature sig = (c, z, h). +* +* Arguments: - uint8_t *c: pointer to output challenge hash +* - polyvecl *z: pointer to output vector z +* - polyveck *h: pointer to output hint vector h +* - const uint8_t sig[]: byte array containing +* bit-packed signature +* +* Returns 1 in case of malformed signature; otherwise 0. +**************************************************/ +int unpack_sig(uint8_t c[CTILDEBYTES], + polyvecl *z, + polyveck *h, + const uint8_t sig[CRYPTO_BYTES]) +{ + unsigned int i, j, k; + + for(i = 0; i < CTILDEBYTES; ++i) + c[i] = sig[i]; + sig += CTILDEBYTES; + + for(i = 0; i < L; ++i) + polyz_unpack(&z->vec[i], sig + i*POLYZ_PACKEDBYTES); + sig += L*POLYZ_PACKEDBYTES; + + /* Decode h */ + k = 0; + for(i = 0; i < K; ++i) { + for(j = 0; j < N; ++j) + h->vec[i].coeffs[j] = 0; + + if(sig[OMEGA + i] < k || sig[OMEGA + i] > OMEGA) + return 1; + + for(j = k; j < sig[OMEGA + i]; ++j) { + /* Coefficients are ordered for strong unforgeability */ + if(j > k && sig[j] <= sig[j-1]) return 1; + h->vec[i].coeffs[sig[j]] = 1; + } + + k = sig[OMEGA + i]; + } + + /* Extra indices are zero for strong unforgeability */ + for(j = k; j < OMEGA; ++j) + if(sig[j]) + return 1; + + return 0; +} diff --git a/src/crypto/pq/mldsa65/packing.h b/src/crypto/pq/mldsa65/packing.h new file mode 100644 index 0000000..8e47728 --- /dev/null +++ b/src/crypto/pq/mldsa65/packing.h @@ -0,0 +1,38 @@ +#ifndef PACKING_H +#define PACKING_H + +#include +#include "params.h" +#include "polyvec.h" + +#define pack_pk DILITHIUM_NAMESPACE(pack_pk) +void pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES], const uint8_t rho[SEEDBYTES], const polyveck *t1); + +#define pack_sk DILITHIUM_NAMESPACE(pack_sk) +void pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES], + const uint8_t rho[SEEDBYTES], + const uint8_t tr[TRBYTES], + const uint8_t key[SEEDBYTES], + const polyveck *t0, + const polyvecl *s1, + const polyveck *s2); + +#define pack_sig DILITHIUM_NAMESPACE(pack_sig) +void pack_sig(uint8_t sig[CRYPTO_BYTES], const uint8_t c[CTILDEBYTES], const polyvecl *z, const polyveck *h); + +#define unpack_pk DILITHIUM_NAMESPACE(unpack_pk) +void unpack_pk(uint8_t rho[SEEDBYTES], polyveck *t1, const uint8_t pk[CRYPTO_PUBLICKEYBYTES]); + +#define unpack_sk DILITHIUM_NAMESPACE(unpack_sk) +void unpack_sk(uint8_t rho[SEEDBYTES], + uint8_t tr[TRBYTES], + uint8_t key[SEEDBYTES], + polyveck *t0, + polyvecl *s1, + polyveck *s2, + const uint8_t sk[CRYPTO_SECRETKEYBYTES]); + +#define unpack_sig DILITHIUM_NAMESPACE(unpack_sig) +int unpack_sig(uint8_t c[CTILDEBYTES], polyvecl *z, polyveck *h, const uint8_t sig[CRYPTO_BYTES]); + +#endif diff --git a/src/crypto/pq/mldsa65/params.h b/src/crypto/pq/mldsa65/params.h new file mode 100644 index 0000000..1e8a7b5 --- /dev/null +++ b/src/crypto/pq/mldsa65/params.h @@ -0,0 +1,80 @@ +#ifndef PARAMS_H +#define PARAMS_H + +#include "config.h" + +#define SEEDBYTES 32 +#define CRHBYTES 64 +#define TRBYTES 64 +#define RNDBYTES 32 +#define N 256 +#define Q 8380417 +#define D 13 +#define ROOT_OF_UNITY 1753 + +#if DILITHIUM_MODE == 2 +#define K 4 +#define L 4 +#define ETA 2 +#define TAU 39 +#define BETA 78 +#define GAMMA1 (1 << 17) +#define GAMMA2 ((Q-1)/88) +#define OMEGA 80 +#define CTILDEBYTES 32 + +#elif DILITHIUM_MODE == 3 +#define K 6 +#define L 5 +#define ETA 4 +#define TAU 49 +#define BETA 196 +#define GAMMA1 (1 << 19) +#define GAMMA2 ((Q-1)/32) +#define OMEGA 55 +#define CTILDEBYTES 48 + +#elif DILITHIUM_MODE == 5 +#define K 8 +#define L 7 +#define ETA 2 +#define TAU 60 +#define BETA 120 +#define GAMMA1 (1 << 19) +#define GAMMA2 ((Q-1)/32) +#define OMEGA 75 +#define CTILDEBYTES 64 + +#endif + +#define POLYT1_PACKEDBYTES 320 +#define POLYT0_PACKEDBYTES 416 +#define POLYVECH_PACKEDBYTES (OMEGA + K) + +#if GAMMA1 == (1 << 17) +#define POLYZ_PACKEDBYTES 576 +#elif GAMMA1 == (1 << 19) +#define POLYZ_PACKEDBYTES 640 +#endif + +#if GAMMA2 == (Q-1)/88 +#define POLYW1_PACKEDBYTES 192 +#elif GAMMA2 == (Q-1)/32 +#define POLYW1_PACKEDBYTES 128 +#endif + +#if ETA == 2 +#define POLYETA_PACKEDBYTES 96 +#elif ETA == 4 +#define POLYETA_PACKEDBYTES 128 +#endif + +#define CRYPTO_PUBLICKEYBYTES (SEEDBYTES + K*POLYT1_PACKEDBYTES) +#define CRYPTO_SECRETKEYBYTES (2*SEEDBYTES \ + + TRBYTES \ + + L*POLYETA_PACKEDBYTES \ + + K*POLYETA_PACKEDBYTES \ + + K*POLYT0_PACKEDBYTES) +#define CRYPTO_BYTES (CTILDEBYTES + L*POLYZ_PACKEDBYTES + POLYVECH_PACKEDBYTES) + +#endif diff --git a/src/crypto/pq/mldsa65/poly.c b/src/crypto/pq/mldsa65/poly.c new file mode 100644 index 0000000..0db4f42 --- /dev/null +++ b/src/crypto/pq/mldsa65/poly.c @@ -0,0 +1,907 @@ +#include +#include "params.h" +#include "poly.h" +#include "ntt.h" +#include "reduce.h" +#include "rounding.h" +#include "symmetric.h" + +#ifdef DBENCH +#include "test/cpucycles.h" +extern const uint64_t timing_overhead; +extern uint64_t *tred, *tadd, *tmul, *tround, *tsample, *tpack; +#define DBENCH_START() uint64_t time = cpucycles() +#define DBENCH_STOP(t) t += cpucycles() - time - timing_overhead +#else +#define DBENCH_START() +#define DBENCH_STOP(t) +#endif + +/************************************************* +* Name: poly_reduce +* +* Description: Inplace reduction of all coefficients of polynomial to +* representative in [-6283008,6283008]. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_reduce(poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a->coeffs[i] = reduce32(a->coeffs[i]); + + DBENCH_STOP(*tred); +} + +/************************************************* +* Name: poly_caddq +* +* Description: For all coefficients of in/out polynomial add Q if +* coefficient is negative. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_caddq(poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a->coeffs[i] = caddq(a->coeffs[i]); + + DBENCH_STOP(*tred); +} + +/************************************************* +* Name: poly_add +* +* Description: Add polynomials. No modular reduction is performed. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first summand +* - const poly *b: pointer to second summand +**************************************************/ +void poly_add(poly *c, const poly *a, const poly *b) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + c->coeffs[i] = a->coeffs[i] + b->coeffs[i]; + + DBENCH_STOP(*tadd); +} + +/************************************************* +* Name: poly_sub +* +* Description: Subtract polynomials. No modular reduction is +* performed. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first input polynomial +* - const poly *b: pointer to second input polynomial to be +* subtraced from first input polynomial +**************************************************/ +void poly_sub(poly *c, const poly *a, const poly *b) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + c->coeffs[i] = a->coeffs[i] - b->coeffs[i]; + + DBENCH_STOP(*tadd); +} + +/************************************************* +* Name: poly_shiftl +* +* Description: Multiply polynomial by 2^D without modular reduction. Assumes +* input coefficients to be less than 2^{31-D} in absolute value. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_shiftl(poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a->coeffs[i] <<= D; + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_ntt +* +* Description: Inplace forward NTT. Coefficients can grow by +* 8*Q in absolute value. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_ntt(poly *a) { + DBENCH_START(); + + ntt(a->coeffs); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_invntt_tomont +* +* Description: Inplace inverse NTT and multiplication by 2^{32}. +* Input coefficients need to be less than Q in absolute +* value and output coefficients are again bounded by Q. +* +* Arguments: - poly *a: pointer to input/output polynomial +**************************************************/ +void poly_invntt_tomont(poly *a) { + DBENCH_START(); + + invntt_tomont(a->coeffs); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_pointwise_montgomery +* +* Description: Pointwise multiplication of polynomials in NTT domain +* representation and multiplication of resulting polynomial +* by 2^{-32}. +* +* Arguments: - poly *c: pointer to output polynomial +* - const poly *a: pointer to first input polynomial +* - const poly *b: pointer to second input polynomial +**************************************************/ +void poly_pointwise_montgomery(poly *c, const poly *a, const poly *b) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + c->coeffs[i] = montgomery_reduce((int64_t)a->coeffs[i] * b->coeffs[i]); + + DBENCH_STOP(*tmul); +} + +/************************************************* +* Name: poly_power2round +* +* Description: For all coefficients c of the input polynomial, +* compute c0, c1 such that c mod Q = c1*2^D + c0 +* with -2^{D-1} < c0 <= 2^{D-1}. Assumes coefficients to be +* standard representatives. +* +* Arguments: - poly *a1: pointer to output polynomial with coefficients c1 +* - poly *a0: pointer to output polynomial with coefficients c0 +* - const poly *a: pointer to input polynomial +**************************************************/ +void poly_power2round(poly *a1, poly *a0, const poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a1->coeffs[i] = power2round(&a0->coeffs[i], a->coeffs[i]); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_decompose +* +* Description: For all coefficients c of the input polynomial, +* compute high and low bits c0, c1 such c mod Q = c1*ALPHA + c0 +* with -ALPHA/2 < c0 <= ALPHA/2 except c1 = (Q-1)/ALPHA where we +* set c1 = 0 and -ALPHA/2 <= c0 = c mod Q - Q < 0. +* Assumes coefficients to be standard representatives. +* +* Arguments: - poly *a1: pointer to output polynomial with coefficients c1 +* - poly *a0: pointer to output polynomial with coefficients c0 +* - const poly *a: pointer to input polynomial +**************************************************/ +void poly_decompose(poly *a1, poly *a0, const poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + a1->coeffs[i] = decompose(&a0->coeffs[i], a->coeffs[i]); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_make_hint +* +* Description: Compute hint polynomial. The coefficients of which indicate +* whether the low bits of the corresponding coefficient of +* the input polynomial overflow into the high bits. +* +* Arguments: - poly *h: pointer to output hint polynomial +* - const poly *a0: pointer to low part of input polynomial +* - const poly *a1: pointer to high part of input polynomial +* +* Returns number of 1 bits. +**************************************************/ +unsigned int poly_make_hint(poly *h, const poly *a0, const poly *a1) { + unsigned int i, s = 0; + DBENCH_START(); + + for(i = 0; i < N; ++i) { + h->coeffs[i] = make_hint(a0->coeffs[i], a1->coeffs[i]); + s += h->coeffs[i]; + } + + DBENCH_STOP(*tround); + return s; +} + +/************************************************* +* Name: poly_use_hint +* +* Description: Use hint polynomial to correct the high bits of a polynomial. +* +* Arguments: - poly *b: pointer to output polynomial with corrected high bits +* - const poly *a: pointer to input polynomial +* - const poly *h: pointer to input hint polynomial +**************************************************/ +void poly_use_hint(poly *b, const poly *a, const poly *h) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N; ++i) + b->coeffs[i] = use_hint(a->coeffs[i], h->coeffs[i]); + + DBENCH_STOP(*tround); +} + +/************************************************* +* Name: poly_chknorm +* +* Description: Check infinity norm of polynomial against given bound. +* Assumes input coefficients were reduced by reduce32(). +* +* Arguments: - const poly *a: pointer to polynomial +* - int32_t B: norm bound +* +* Returns 0 if norm is strictly smaller than B <= (Q-1)/8 and 1 otherwise. +**************************************************/ +int poly_chknorm(const poly *a, int32_t B) { + unsigned int i; + int32_t t; + DBENCH_START(); + + if(B > (Q-1)/8) + return 1; + + /* It is ok to leak which coefficient violates the bound since + the probability for each coefficient is independent of secret + data but we must not leak the sign of the centralized representative. */ + for(i = 0; i < N; ++i) { + /* Absolute value */ + t = a->coeffs[i] >> 31; + t = a->coeffs[i] - (t & 2*a->coeffs[i]); + + if(t >= B) { + DBENCH_STOP(*tsample); + return 1; + } + } + + DBENCH_STOP(*tsample); + return 0; +} + +/************************************************* +* Name: rej_uniform +* +* Description: Sample uniformly random coefficients in [0, Q-1] by +* performing rejection sampling on array of random bytes. +* +* Arguments: - int32_t *a: pointer to output array (allocated) +* - unsigned int len: number of coefficients to be sampled +* - const uint8_t *buf: array of random bytes +* - unsigned int buflen: length of array of random bytes +* +* Returns number of sampled coefficients. Can be smaller than len if not enough +* random bytes were given. +**************************************************/ +static unsigned int rej_uniform(int32_t *a, + unsigned int len, + const uint8_t *buf, + unsigned int buflen) +{ + unsigned int ctr, pos; + uint32_t t; + DBENCH_START(); + + ctr = pos = 0; + while(ctr < len && pos + 3 <= buflen) { + t = buf[pos++]; + t |= (uint32_t)buf[pos++] << 8; + t |= (uint32_t)buf[pos++] << 16; + t &= 0x7FFFFF; + + if(t < Q) + a[ctr++] = t; + } + + DBENCH_STOP(*tsample); + return ctr; +} + +/************************************************* +* Name: poly_uniform +* +* Description: Sample polynomial with uniformly random coefficients +* in [0,Q-1] by performing rejection sampling on the +* output stream of SHAKE128(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length SEEDBYTES +* - uint16_t nonce: 2-byte nonce +**************************************************/ +#define POLY_UNIFORM_NBLOCKS ((768 + STREAM128_BLOCKBYTES - 1)/STREAM128_BLOCKBYTES) +void poly_uniform(poly *a, + const uint8_t seed[SEEDBYTES], + uint16_t nonce) +{ + unsigned int i, ctr, off; + unsigned int buflen = POLY_UNIFORM_NBLOCKS*STREAM128_BLOCKBYTES; + uint8_t buf[POLY_UNIFORM_NBLOCKS*STREAM128_BLOCKBYTES + 2]; + stream128_state state; + + stream128_init(&state, seed, nonce); + stream128_squeezeblocks(buf, POLY_UNIFORM_NBLOCKS, &state); + + ctr = rej_uniform(a->coeffs, N, buf, buflen); + + while(ctr < N) { + off = buflen % 3; + for(i = 0; i < off; ++i) + buf[i] = buf[buflen - off + i]; + + stream128_squeezeblocks(buf + off, 1, &state); + buflen = STREAM128_BLOCKBYTES + off; + ctr += rej_uniform(a->coeffs + ctr, N - ctr, buf, buflen); + } +} + +/************************************************* +* Name: rej_eta +* +* Description: Sample uniformly random coefficients in [-ETA, ETA] by +* performing rejection sampling on array of random bytes. +* +* Arguments: - int32_t *a: pointer to output array (allocated) +* - unsigned int len: number of coefficients to be sampled +* - const uint8_t *buf: array of random bytes +* - unsigned int buflen: length of array of random bytes +* +* Returns number of sampled coefficients. Can be smaller than len if not enough +* random bytes were given. +**************************************************/ +static unsigned int rej_eta(int32_t *a, + unsigned int len, + const uint8_t *buf, + unsigned int buflen) +{ + unsigned int ctr, pos; + uint32_t t0, t1; + DBENCH_START(); + + ctr = pos = 0; + while(ctr < len && pos < buflen) { + t0 = buf[pos] & 0x0F; + t1 = buf[pos++] >> 4; + +#if ETA == 2 + if(t0 < 15) { + t0 = t0 - (205*t0 >> 10)*5; + a[ctr++] = 2 - t0; + } + if(t1 < 15 && ctr < len) { + t1 = t1 - (205*t1 >> 10)*5; + a[ctr++] = 2 - t1; + } +#elif ETA == 4 + if(t0 < 9) + a[ctr++] = 4 - t0; + if(t1 < 9 && ctr < len) + a[ctr++] = 4 - t1; +#endif + } + + DBENCH_STOP(*tsample); + return ctr; +} + +/************************************************* +* Name: poly_uniform_eta +* +* Description: Sample polynomial with uniformly random coefficients +* in [-ETA,ETA] by performing rejection sampling on the +* output stream from SHAKE256(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length CRHBYTES +* - uint16_t nonce: 2-byte nonce +**************************************************/ +#if ETA == 2 +#define POLY_UNIFORM_ETA_NBLOCKS ((136 + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) +#elif ETA == 4 +#define POLY_UNIFORM_ETA_NBLOCKS ((227 + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) +#endif +void poly_uniform_eta(poly *a, + const uint8_t seed[CRHBYTES], + uint16_t nonce) +{ + unsigned int ctr; + unsigned int buflen = POLY_UNIFORM_ETA_NBLOCKS*STREAM256_BLOCKBYTES; + uint8_t buf[POLY_UNIFORM_ETA_NBLOCKS*STREAM256_BLOCKBYTES]; + stream256_state state; + + stream256_init(&state, seed, nonce); + stream256_squeezeblocks(buf, POLY_UNIFORM_ETA_NBLOCKS, &state); + + ctr = rej_eta(a->coeffs, N, buf, buflen); + + while(ctr < N) { + stream256_squeezeblocks(buf, 1, &state); + ctr += rej_eta(a->coeffs + ctr, N - ctr, buf, STREAM256_BLOCKBYTES); + } +} + +/************************************************* +* Name: poly_uniform_gamma1m1 +* +* Description: Sample polynomial with uniformly random coefficients +* in [-(GAMMA1 - 1), GAMMA1] by unpacking output stream +* of SHAKE256(seed|nonce) +* +* Arguments: - poly *a: pointer to output polynomial +* - const uint8_t seed[]: byte array with seed of length CRHBYTES +* - uint16_t nonce: 16-bit nonce +**************************************************/ +#define POLY_UNIFORM_GAMMA1_NBLOCKS ((POLYZ_PACKEDBYTES + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) +void poly_uniform_gamma1(poly *a, + const uint8_t seed[CRHBYTES], + uint16_t nonce) +{ + uint8_t buf[POLY_UNIFORM_GAMMA1_NBLOCKS*STREAM256_BLOCKBYTES]; + stream256_state state; + + stream256_init(&state, seed, nonce); + stream256_squeezeblocks(buf, POLY_UNIFORM_GAMMA1_NBLOCKS, &state); + polyz_unpack(a, buf); +} + +/************************************************* +* Name: challenge +* +* Description: Implementation of H. Samples polynomial with TAU nonzero +* coefficients in {-1,1} using the output stream of +* SHAKE256(seed). +* +* Arguments: - poly *c: pointer to output polynomial +* - const uint8_t mu[]: byte array containing seed of length CTILDEBYTES +**************************************************/ +void poly_challenge(poly *c, const uint8_t seed[CTILDEBYTES]) { + unsigned int i, b, pos; + uint64_t signs; + uint8_t buf[SHAKE256_RATE]; + keccak_state state; + + shake256_init(&state); + shake256_absorb(&state, seed, CTILDEBYTES); + shake256_finalize(&state); + shake256_squeezeblocks(buf, 1, &state); + + signs = 0; + for(i = 0; i < 8; ++i) + signs |= (uint64_t)buf[i] << 8*i; + pos = 8; + + for(i = 0; i < N; ++i) + c->coeffs[i] = 0; + for(i = N-TAU; i < N; ++i) { + do { + if(pos >= SHAKE256_RATE) { + shake256_squeezeblocks(buf, 1, &state); + pos = 0; + } + + b = buf[pos++]; + } while(b > i); + + c->coeffs[i] = c->coeffs[b]; + c->coeffs[b] = 1 - 2*(signs & 1); + signs >>= 1; + } +} + +/************************************************* +* Name: polyeta_pack +* +* Description: Bit-pack polynomial with coefficients in [-ETA,ETA]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYETA_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyeta_pack(uint8_t *r, const poly *a) { + unsigned int i; + uint8_t t[8]; + DBENCH_START(); + +#if ETA == 2 + for(i = 0; i < N/8; ++i) { + t[0] = ETA - a->coeffs[8*i+0]; + t[1] = ETA - a->coeffs[8*i+1]; + t[2] = ETA - a->coeffs[8*i+2]; + t[3] = ETA - a->coeffs[8*i+3]; + t[4] = ETA - a->coeffs[8*i+4]; + t[5] = ETA - a->coeffs[8*i+5]; + t[6] = ETA - a->coeffs[8*i+6]; + t[7] = ETA - a->coeffs[8*i+7]; + + r[3*i+0] = (t[0] >> 0) | (t[1] << 3) | (t[2] << 6); + r[3*i+1] = (t[2] >> 2) | (t[3] << 1) | (t[4] << 4) | (t[5] << 7); + r[3*i+2] = (t[5] >> 1) | (t[6] << 2) | (t[7] << 5); + } +#elif ETA == 4 + for(i = 0; i < N/2; ++i) { + t[0] = ETA - a->coeffs[2*i+0]; + t[1] = ETA - a->coeffs[2*i+1]; + r[i] = t[0] | (t[1] << 4); + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyeta_unpack +* +* Description: Unpack polynomial with coefficients in [-ETA,ETA]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyeta_unpack(poly *r, const uint8_t *a) { + unsigned int i; + DBENCH_START(); + +#if ETA == 2 + for(i = 0; i < N/8; ++i) { + r->coeffs[8*i+0] = (a[3*i+0] >> 0) & 7; + r->coeffs[8*i+1] = (a[3*i+0] >> 3) & 7; + r->coeffs[8*i+2] = ((a[3*i+0] >> 6) | (a[3*i+1] << 2)) & 7; + r->coeffs[8*i+3] = (a[3*i+1] >> 1) & 7; + r->coeffs[8*i+4] = (a[3*i+1] >> 4) & 7; + r->coeffs[8*i+5] = ((a[3*i+1] >> 7) | (a[3*i+2] << 1)) & 7; + r->coeffs[8*i+6] = (a[3*i+2] >> 2) & 7; + r->coeffs[8*i+7] = (a[3*i+2] >> 5) & 7; + + r->coeffs[8*i+0] = ETA - r->coeffs[8*i+0]; + r->coeffs[8*i+1] = ETA - r->coeffs[8*i+1]; + r->coeffs[8*i+2] = ETA - r->coeffs[8*i+2]; + r->coeffs[8*i+3] = ETA - r->coeffs[8*i+3]; + r->coeffs[8*i+4] = ETA - r->coeffs[8*i+4]; + r->coeffs[8*i+5] = ETA - r->coeffs[8*i+5]; + r->coeffs[8*i+6] = ETA - r->coeffs[8*i+6]; + r->coeffs[8*i+7] = ETA - r->coeffs[8*i+7]; + } +#elif ETA == 4 + for(i = 0; i < N/2; ++i) { + r->coeffs[2*i+0] = a[i] & 0x0F; + r->coeffs[2*i+1] = a[i] >> 4; + r->coeffs[2*i+0] = ETA - r->coeffs[2*i+0]; + r->coeffs[2*i+1] = ETA - r->coeffs[2*i+1]; + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt1_pack +* +* Description: Bit-pack polynomial t1 with coefficients fitting in 10 bits. +* Input coefficients are assumed to be standard representatives. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYT1_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyt1_pack(uint8_t *r, const poly *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/4; ++i) { + r[5*i+0] = (a->coeffs[4*i+0] >> 0); + r[5*i+1] = (a->coeffs[4*i+0] >> 8) | (a->coeffs[4*i+1] << 2); + r[5*i+2] = (a->coeffs[4*i+1] >> 6) | (a->coeffs[4*i+2] << 4); + r[5*i+3] = (a->coeffs[4*i+2] >> 4) | (a->coeffs[4*i+3] << 6); + r[5*i+4] = (a->coeffs[4*i+3] >> 2); + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt1_unpack +* +* Description: Unpack polynomial t1 with 10-bit coefficients. +* Output coefficients are standard representatives. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyt1_unpack(poly *r, const uint8_t *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/4; ++i) { + r->coeffs[4*i+0] = ((a[5*i+0] >> 0) | ((uint32_t)a[5*i+1] << 8)) & 0x3FF; + r->coeffs[4*i+1] = ((a[5*i+1] >> 2) | ((uint32_t)a[5*i+2] << 6)) & 0x3FF; + r->coeffs[4*i+2] = ((a[5*i+2] >> 4) | ((uint32_t)a[5*i+3] << 4)) & 0x3FF; + r->coeffs[4*i+3] = ((a[5*i+3] >> 6) | ((uint32_t)a[5*i+4] << 2)) & 0x3FF; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt0_pack +* +* Description: Bit-pack polynomial t0 with coefficients in ]-2^{D-1}, 2^{D-1}]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYT0_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyt0_pack(uint8_t *r, const poly *a) { + unsigned int i; + uint32_t t[8]; + DBENCH_START(); + + for(i = 0; i < N/8; ++i) { + t[0] = (1 << (D-1)) - a->coeffs[8*i+0]; + t[1] = (1 << (D-1)) - a->coeffs[8*i+1]; + t[2] = (1 << (D-1)) - a->coeffs[8*i+2]; + t[3] = (1 << (D-1)) - a->coeffs[8*i+3]; + t[4] = (1 << (D-1)) - a->coeffs[8*i+4]; + t[5] = (1 << (D-1)) - a->coeffs[8*i+5]; + t[6] = (1 << (D-1)) - a->coeffs[8*i+6]; + t[7] = (1 << (D-1)) - a->coeffs[8*i+7]; + + r[13*i+ 0] = t[0]; + r[13*i+ 1] = t[0] >> 8; + r[13*i+ 1] |= t[1] << 5; + r[13*i+ 2] = t[1] >> 3; + r[13*i+ 3] = t[1] >> 11; + r[13*i+ 3] |= t[2] << 2; + r[13*i+ 4] = t[2] >> 6; + r[13*i+ 4] |= t[3] << 7; + r[13*i+ 5] = t[3] >> 1; + r[13*i+ 6] = t[3] >> 9; + r[13*i+ 6] |= t[4] << 4; + r[13*i+ 7] = t[4] >> 4; + r[13*i+ 8] = t[4] >> 12; + r[13*i+ 8] |= t[5] << 1; + r[13*i+ 9] = t[5] >> 7; + r[13*i+ 9] |= t[6] << 6; + r[13*i+10] = t[6] >> 2; + r[13*i+11] = t[6] >> 10; + r[13*i+11] |= t[7] << 3; + r[13*i+12] = t[7] >> 5; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyt0_unpack +* +* Description: Unpack polynomial t0 with coefficients in ]-2^{D-1}, 2^{D-1}]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyt0_unpack(poly *r, const uint8_t *a) { + unsigned int i; + DBENCH_START(); + + for(i = 0; i < N/8; ++i) { + r->coeffs[8*i+0] = a[13*i+0]; + r->coeffs[8*i+0] |= (uint32_t)a[13*i+1] << 8; + r->coeffs[8*i+0] &= 0x1FFF; + + r->coeffs[8*i+1] = a[13*i+1] >> 5; + r->coeffs[8*i+1] |= (uint32_t)a[13*i+2] << 3; + r->coeffs[8*i+1] |= (uint32_t)a[13*i+3] << 11; + r->coeffs[8*i+1] &= 0x1FFF; + + r->coeffs[8*i+2] = a[13*i+3] >> 2; + r->coeffs[8*i+2] |= (uint32_t)a[13*i+4] << 6; + r->coeffs[8*i+2] &= 0x1FFF; + + r->coeffs[8*i+3] = a[13*i+4] >> 7; + r->coeffs[8*i+3] |= (uint32_t)a[13*i+5] << 1; + r->coeffs[8*i+3] |= (uint32_t)a[13*i+6] << 9; + r->coeffs[8*i+3] &= 0x1FFF; + + r->coeffs[8*i+4] = a[13*i+6] >> 4; + r->coeffs[8*i+4] |= (uint32_t)a[13*i+7] << 4; + r->coeffs[8*i+4] |= (uint32_t)a[13*i+8] << 12; + r->coeffs[8*i+4] &= 0x1FFF; + + r->coeffs[8*i+5] = a[13*i+8] >> 1; + r->coeffs[8*i+5] |= (uint32_t)a[13*i+9] << 7; + r->coeffs[8*i+5] &= 0x1FFF; + + r->coeffs[8*i+6] = a[13*i+9] >> 6; + r->coeffs[8*i+6] |= (uint32_t)a[13*i+10] << 2; + r->coeffs[8*i+6] |= (uint32_t)a[13*i+11] << 10; + r->coeffs[8*i+6] &= 0x1FFF; + + r->coeffs[8*i+7] = a[13*i+11] >> 3; + r->coeffs[8*i+7] |= (uint32_t)a[13*i+12] << 5; + r->coeffs[8*i+7] &= 0x1FFF; + + r->coeffs[8*i+0] = (1 << (D-1)) - r->coeffs[8*i+0]; + r->coeffs[8*i+1] = (1 << (D-1)) - r->coeffs[8*i+1]; + r->coeffs[8*i+2] = (1 << (D-1)) - r->coeffs[8*i+2]; + r->coeffs[8*i+3] = (1 << (D-1)) - r->coeffs[8*i+3]; + r->coeffs[8*i+4] = (1 << (D-1)) - r->coeffs[8*i+4]; + r->coeffs[8*i+5] = (1 << (D-1)) - r->coeffs[8*i+5]; + r->coeffs[8*i+6] = (1 << (D-1)) - r->coeffs[8*i+6]; + r->coeffs[8*i+7] = (1 << (D-1)) - r->coeffs[8*i+7]; + } + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyz_pack +* +* Description: Bit-pack polynomial with coefficients +* in [-(GAMMA1 - 1), GAMMA1]. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYZ_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyz_pack(uint8_t *r, const poly *a) { + unsigned int i; + uint32_t t[4]; + DBENCH_START(); + +#if GAMMA1 == (1 << 17) + for(i = 0; i < N/4; ++i) { + t[0] = GAMMA1 - a->coeffs[4*i+0]; + t[1] = GAMMA1 - a->coeffs[4*i+1]; + t[2] = GAMMA1 - a->coeffs[4*i+2]; + t[3] = GAMMA1 - a->coeffs[4*i+3]; + + r[9*i+0] = t[0]; + r[9*i+1] = t[0] >> 8; + r[9*i+2] = t[0] >> 16; + r[9*i+2] |= t[1] << 2; + r[9*i+3] = t[1] >> 6; + r[9*i+4] = t[1] >> 14; + r[9*i+4] |= t[2] << 4; + r[9*i+5] = t[2] >> 4; + r[9*i+6] = t[2] >> 12; + r[9*i+6] |= t[3] << 6; + r[9*i+7] = t[3] >> 2; + r[9*i+8] = t[3] >> 10; + } +#elif GAMMA1 == (1 << 19) + for(i = 0; i < N/2; ++i) { + t[0] = GAMMA1 - a->coeffs[2*i+0]; + t[1] = GAMMA1 - a->coeffs[2*i+1]; + + r[5*i+0] = t[0]; + r[5*i+1] = t[0] >> 8; + r[5*i+2] = t[0] >> 16; + r[5*i+2] |= t[1] << 4; + r[5*i+3] = t[1] >> 4; + r[5*i+4] = t[1] >> 12; + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyz_unpack +* +* Description: Unpack polynomial z with coefficients +* in [-(GAMMA1 - 1), GAMMA1]. +* +* Arguments: - poly *r: pointer to output polynomial +* - const uint8_t *a: byte array with bit-packed polynomial +**************************************************/ +void polyz_unpack(poly *r, const uint8_t *a) { + unsigned int i; + DBENCH_START(); + +#if GAMMA1 == (1 << 17) + for(i = 0; i < N/4; ++i) { + r->coeffs[4*i+0] = a[9*i+0]; + r->coeffs[4*i+0] |= (uint32_t)a[9*i+1] << 8; + r->coeffs[4*i+0] |= (uint32_t)a[9*i+2] << 16; + r->coeffs[4*i+0] &= 0x3FFFF; + + r->coeffs[4*i+1] = a[9*i+2] >> 2; + r->coeffs[4*i+1] |= (uint32_t)a[9*i+3] << 6; + r->coeffs[4*i+1] |= (uint32_t)a[9*i+4] << 14; + r->coeffs[4*i+1] &= 0x3FFFF; + + r->coeffs[4*i+2] = a[9*i+4] >> 4; + r->coeffs[4*i+2] |= (uint32_t)a[9*i+5] << 4; + r->coeffs[4*i+2] |= (uint32_t)a[9*i+6] << 12; + r->coeffs[4*i+2] &= 0x3FFFF; + + r->coeffs[4*i+3] = a[9*i+6] >> 6; + r->coeffs[4*i+3] |= (uint32_t)a[9*i+7] << 2; + r->coeffs[4*i+3] |= (uint32_t)a[9*i+8] << 10; + r->coeffs[4*i+3] &= 0x3FFFF; + + r->coeffs[4*i+0] = GAMMA1 - r->coeffs[4*i+0]; + r->coeffs[4*i+1] = GAMMA1 - r->coeffs[4*i+1]; + r->coeffs[4*i+2] = GAMMA1 - r->coeffs[4*i+2]; + r->coeffs[4*i+3] = GAMMA1 - r->coeffs[4*i+3]; + } +#elif GAMMA1 == (1 << 19) + for(i = 0; i < N/2; ++i) { + r->coeffs[2*i+0] = a[5*i+0]; + r->coeffs[2*i+0] |= (uint32_t)a[5*i+1] << 8; + r->coeffs[2*i+0] |= (uint32_t)a[5*i+2] << 16; + r->coeffs[2*i+0] &= 0xFFFFF; + + r->coeffs[2*i+1] = a[5*i+2] >> 4; + r->coeffs[2*i+1] |= (uint32_t)a[5*i+3] << 4; + r->coeffs[2*i+1] |= (uint32_t)a[5*i+4] << 12; + /* r->coeffs[2*i+1] &= 0xFFFFF; */ /* No effect, since we're anyway at 20 bits */ + + r->coeffs[2*i+0] = GAMMA1 - r->coeffs[2*i+0]; + r->coeffs[2*i+1] = GAMMA1 - r->coeffs[2*i+1]; + } +#endif + + DBENCH_STOP(*tpack); +} + +/************************************************* +* Name: polyw1_pack +* +* Description: Bit-pack polynomial w1 with coefficients in [0,15] or [0,43]. +* Input coefficients are assumed to be standard representatives. +* +* Arguments: - uint8_t *r: pointer to output byte array with at least +* POLYW1_PACKEDBYTES bytes +* - const poly *a: pointer to input polynomial +**************************************************/ +void polyw1_pack(uint8_t *r, const poly *a) { + unsigned int i; + DBENCH_START(); + +#if GAMMA2 == (Q-1)/88 + for(i = 0; i < N/4; ++i) { + r[3*i+0] = a->coeffs[4*i+0]; + r[3*i+0] |= a->coeffs[4*i+1] << 6; + r[3*i+1] = a->coeffs[4*i+1] >> 2; + r[3*i+1] |= a->coeffs[4*i+2] << 4; + r[3*i+2] = a->coeffs[4*i+2] >> 4; + r[3*i+2] |= a->coeffs[4*i+3] << 2; + } +#elif GAMMA2 == (Q-1)/32 + for(i = 0; i < N/2; ++i) + r[i] = a->coeffs[2*i+0] | (a->coeffs[2*i+1] << 4); +#endif + + DBENCH_STOP(*tpack); +} diff --git a/src/crypto/pq/mldsa65/poly.h b/src/crypto/pq/mldsa65/poly.h new file mode 100644 index 0000000..904baa1 --- /dev/null +++ b/src/crypto/pq/mldsa65/poly.h @@ -0,0 +1,79 @@ +#ifndef POLY_H +#define POLY_H + +#include +#include "params.h" + +typedef struct { + int32_t coeffs[N]; +} poly; + +#define poly_reduce DILITHIUM_NAMESPACE(poly_reduce) +void poly_reduce(poly *a); +#define poly_caddq DILITHIUM_NAMESPACE(poly_caddq) +void poly_caddq(poly *a); + +#define poly_add DILITHIUM_NAMESPACE(poly_add) +void poly_add(poly *c, const poly *a, const poly *b); +#define poly_sub DILITHIUM_NAMESPACE(poly_sub) +void poly_sub(poly *c, const poly *a, const poly *b); +#define poly_shiftl DILITHIUM_NAMESPACE(poly_shiftl) +void poly_shiftl(poly *a); + +#define poly_ntt DILITHIUM_NAMESPACE(poly_ntt) +void poly_ntt(poly *a); +#define poly_invntt_tomont DILITHIUM_NAMESPACE(poly_invntt_tomont) +void poly_invntt_tomont(poly *a); +#define poly_pointwise_montgomery DILITHIUM_NAMESPACE(poly_pointwise_montgomery) +void poly_pointwise_montgomery(poly *c, const poly *a, const poly *b); + +#define poly_power2round DILITHIUM_NAMESPACE(poly_power2round) +void poly_power2round(poly *a1, poly *a0, const poly *a); +#define poly_decompose DILITHIUM_NAMESPACE(poly_decompose) +void poly_decompose(poly *a1, poly *a0, const poly *a); +#define poly_make_hint DILITHIUM_NAMESPACE(poly_make_hint) +unsigned int poly_make_hint(poly *h, const poly *a0, const poly *a1); +#define poly_use_hint DILITHIUM_NAMESPACE(poly_use_hint) +void poly_use_hint(poly *b, const poly *a, const poly *h); + +#define poly_chknorm DILITHIUM_NAMESPACE(poly_chknorm) +int poly_chknorm(const poly *a, int32_t B); +#define poly_uniform DILITHIUM_NAMESPACE(poly_uniform) +void poly_uniform(poly *a, + const uint8_t seed[SEEDBYTES], + uint16_t nonce); +#define poly_uniform_eta DILITHIUM_NAMESPACE(poly_uniform_eta) +void poly_uniform_eta(poly *a, + const uint8_t seed[CRHBYTES], + uint16_t nonce); +#define poly_uniform_gamma1 DILITHIUM_NAMESPACE(poly_uniform_gamma1) +void poly_uniform_gamma1(poly *a, + const uint8_t seed[CRHBYTES], + uint16_t nonce); +#define poly_challenge DILITHIUM_NAMESPACE(poly_challenge) +void poly_challenge(poly *c, const uint8_t seed[CTILDEBYTES]); + +#define polyeta_pack DILITHIUM_NAMESPACE(polyeta_pack) +void polyeta_pack(uint8_t *r, const poly *a); +#define polyeta_unpack DILITHIUM_NAMESPACE(polyeta_unpack) +void polyeta_unpack(poly *r, const uint8_t *a); + +#define polyt1_pack DILITHIUM_NAMESPACE(polyt1_pack) +void polyt1_pack(uint8_t *r, const poly *a); +#define polyt1_unpack DILITHIUM_NAMESPACE(polyt1_unpack) +void polyt1_unpack(poly *r, const uint8_t *a); + +#define polyt0_pack DILITHIUM_NAMESPACE(polyt0_pack) +void polyt0_pack(uint8_t *r, const poly *a); +#define polyt0_unpack DILITHIUM_NAMESPACE(polyt0_unpack) +void polyt0_unpack(poly *r, const uint8_t *a); + +#define polyz_pack DILITHIUM_NAMESPACE(polyz_pack) +void polyz_pack(uint8_t *r, const poly *a); +#define polyz_unpack DILITHIUM_NAMESPACE(polyz_unpack) +void polyz_unpack(poly *r, const uint8_t *a); + +#define polyw1_pack DILITHIUM_NAMESPACE(polyw1_pack) +void polyw1_pack(uint8_t *r, const poly *a); + +#endif diff --git a/src/crypto/pq/mldsa65/polyvec.c b/src/crypto/pq/mldsa65/polyvec.c new file mode 100644 index 0000000..241f618 --- /dev/null +++ b/src/crypto/pq/mldsa65/polyvec.c @@ -0,0 +1,389 @@ +#include +#include "params.h" +#include "polyvec.h" +#include "poly.h" + +/************************************************* +* Name: expand_mat +* +* Description: Implementation of ExpandA. Generates matrix A with uniformly +* random coefficients a_{i,j} by performing rejection +* sampling on the output stream of SHAKE128(rho|j|i) +* +* Arguments: - polyvecl mat[K]: output matrix +* - const uint8_t rho[]: byte array containing seed rho +**************************************************/ +void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]) { + unsigned int i, j; + + for(i = 0; i < K; ++i) + for(j = 0; j < L; ++j) + poly_uniform(&mat[i].vec[j], rho, (i << 8) + j); +} + +void polyvec_matrix_pointwise_montgomery(polyveck *t, const polyvecl mat[K], const polyvecl *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + polyvecl_pointwise_acc_montgomery(&t->vec[i], &mat[i], v); +} + +/**************************************************************/ +/************ Vectors of polynomials of length L **************/ +/**************************************************************/ + +void polyvecl_uniform_eta(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_uniform_eta(&v->vec[i], seed, nonce++); +} + +void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_uniform_gamma1(&v->vec[i], seed, L*nonce + i); +} + +void polyvecl_reduce(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_reduce(&v->vec[i]); +} + +/************************************************* +* Name: polyvecl_add +* +* Description: Add vectors of polynomials of length L. +* No modular reduction is performed. +* +* Arguments: - polyvecl *w: pointer to output vector +* - const polyvecl *u: pointer to first summand +* - const polyvecl *v: pointer to second summand +**************************************************/ +void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_add(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyvecl_ntt +* +* Description: Forward NTT of all polynomials in vector of length L. Output +* coefficients can be up to 16*Q larger than input coefficients. +* +* Arguments: - polyvecl *v: pointer to input/output vector +**************************************************/ +void polyvecl_ntt(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_ntt(&v->vec[i]); +} + +void polyvecl_invntt_tomont(polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_invntt_tomont(&v->vec[i]); +} + +void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v) { + unsigned int i; + + for(i = 0; i < L; ++i) + poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]); +} + +/************************************************* +* Name: polyvecl_pointwise_acc_montgomery +* +* Description: Pointwise multiply vectors of polynomials of length L, multiply +* resulting vector by 2^{-32} and add (accumulate) polynomials +* in it. Input/output vectors are in NTT domain representation. +* +* Arguments: - poly *w: output polynomial +* - const polyvecl *u: pointer to first input vector +* - const polyvecl *v: pointer to second input vector +**************************************************/ +void polyvecl_pointwise_acc_montgomery(poly *w, + const polyvecl *u, + const polyvecl *v) +{ + unsigned int i; + poly t; + + poly_pointwise_montgomery(w, &u->vec[0], &v->vec[0]); + for(i = 1; i < L; ++i) { + poly_pointwise_montgomery(&t, &u->vec[i], &v->vec[i]); + poly_add(w, w, &t); + } +} + +/************************************************* +* Name: polyvecl_chknorm +* +* Description: Check infinity norm of polynomials in vector of length L. +* Assumes input polyvecl to be reduced by polyvecl_reduce(). +* +* Arguments: - const polyvecl *v: pointer to vector +* - int32_t B: norm bound +* +* Returns 0 if norm of all polynomials is strictly smaller than B <= (Q-1)/8 +* and 1 otherwise. +**************************************************/ +int polyvecl_chknorm(const polyvecl *v, int32_t bound) { + unsigned int i; + + for(i = 0; i < L; ++i) + if(poly_chknorm(&v->vec[i], bound)) + return 1; + + return 0; +} + +/**************************************************************/ +/************ Vectors of polynomials of length K **************/ +/**************************************************************/ + +void polyveck_uniform_eta(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_uniform_eta(&v->vec[i], seed, nonce++); +} + +/************************************************* +* Name: polyveck_reduce +* +* Description: Reduce coefficients of polynomials in vector of length K +* to representatives in [-6283008,6283008]. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_reduce(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_reduce(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_caddq +* +* Description: For all coefficients of polynomials in vector of length K +* add Q if coefficient is negative. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_caddq(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_caddq(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_add +* +* Description: Add vectors of polynomials of length K. +* No modular reduction is performed. +* +* Arguments: - polyveck *w: pointer to output vector +* - const polyveck *u: pointer to first summand +* - const polyveck *v: pointer to second summand +**************************************************/ +void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_add(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_sub +* +* Description: Subtract vectors of polynomials of length K. +* No modular reduction is performed. +* +* Arguments: - polyveck *w: pointer to output vector +* - const polyveck *u: pointer to first input vector +* - const polyveck *v: pointer to second input vector to be +* subtracted from first input vector +**************************************************/ +void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_sub(&w->vec[i], &u->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_shiftl +* +* Description: Multiply vector of polynomials of Length K by 2^D without modular +* reduction. Assumes input coefficients to be less than 2^{31-D}. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_shiftl(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_shiftl(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_ntt +* +* Description: Forward NTT of all polynomials in vector of length K. Output +* coefficients can be up to 16*Q larger than input coefficients. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_ntt(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_ntt(&v->vec[i]); +} + +/************************************************* +* Name: polyveck_invntt_tomont +* +* Description: Inverse NTT and multiplication by 2^{32} of polynomials +* in vector of length K. Input coefficients need to be less +* than 2*Q. +* +* Arguments: - polyveck *v: pointer to input/output vector +**************************************************/ +void polyveck_invntt_tomont(polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_invntt_tomont(&v->vec[i]); +} + +void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]); +} + + +/************************************************* +* Name: polyveck_chknorm +* +* Description: Check infinity norm of polynomials in vector of length K. +* Assumes input polyveck to be reduced by polyveck_reduce(). +* +* Arguments: - const polyveck *v: pointer to vector +* - int32_t B: norm bound +* +* Returns 0 if norm of all polynomials are strictly smaller than B <= (Q-1)/8 +* and 1 otherwise. +**************************************************/ +int polyveck_chknorm(const polyveck *v, int32_t bound) { + unsigned int i; + + for(i = 0; i < K; ++i) + if(poly_chknorm(&v->vec[i], bound)) + return 1; + + return 0; +} + +/************************************************* +* Name: polyveck_power2round +* +* Description: For all coefficients a of polynomials in vector of length K, +* compute a0, a1 such that a mod^+ Q = a1*2^D + a0 +* with -2^{D-1} < a0 <= 2^{D-1}. Assumes coefficients to be +* standard representatives. +* +* Arguments: - polyveck *v1: pointer to output vector of polynomials with +* coefficients a1 +* - polyveck *v0: pointer to output vector of polynomials with +* coefficients a0 +* - const polyveck *v: pointer to input vector +**************************************************/ +void polyveck_power2round(polyveck *v1, polyveck *v0, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_power2round(&v1->vec[i], &v0->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_decompose +* +* Description: For all coefficients a of polynomials in vector of length K, +* compute high and low bits a0, a1 such a mod^+ Q = a1*ALPHA + a0 +* with -ALPHA/2 < a0 <= ALPHA/2 except a1 = (Q-1)/ALPHA where we +* set a1 = 0 and -ALPHA/2 <= a0 = a mod Q - Q < 0. +* Assumes coefficients to be standard representatives. +* +* Arguments: - polyveck *v1: pointer to output vector of polynomials with +* coefficients a1 +* - polyveck *v0: pointer to output vector of polynomials with +* coefficients a0 +* - const polyveck *v: pointer to input vector +**************************************************/ +void polyveck_decompose(polyveck *v1, polyveck *v0, const polyveck *v) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_decompose(&v1->vec[i], &v0->vec[i], &v->vec[i]); +} + +/************************************************* +* Name: polyveck_make_hint +* +* Description: Compute hint vector. +* +* Arguments: - polyveck *h: pointer to output vector +* - const polyveck *v0: pointer to low part of input vector +* - const polyveck *v1: pointer to high part of input vector +* +* Returns number of 1 bits. +**************************************************/ +unsigned int polyveck_make_hint(polyveck *h, + const polyveck *v0, + const polyveck *v1) +{ + unsigned int i, s = 0; + + for(i = 0; i < K; ++i) + s += poly_make_hint(&h->vec[i], &v0->vec[i], &v1->vec[i]); + + return s; +} + +/************************************************* +* Name: polyveck_use_hint +* +* Description: Use hint vector to correct the high bits of input vector. +* +* Arguments: - polyveck *w: pointer to output vector of polynomials with +* corrected high bits +* - const polyveck *u: pointer to input vector +* - const polyveck *h: pointer to input hint vector +**************************************************/ +void polyveck_use_hint(polyveck *w, const polyveck *u, const polyveck *h) { + unsigned int i; + + for(i = 0; i < K; ++i) + poly_use_hint(&w->vec[i], &u->vec[i], &h->vec[i]); +} + +void polyveck_pack_w1(uint8_t r[K*POLYW1_PACKEDBYTES], const polyveck *w1) { + unsigned int i; + + for(i = 0; i < K; ++i) + polyw1_pack(&r[i*POLYW1_PACKEDBYTES], &w1->vec[i]); +} diff --git a/src/crypto/pq/mldsa65/polyvec.h b/src/crypto/pq/mldsa65/polyvec.h new file mode 100644 index 0000000..615ac52 --- /dev/null +++ b/src/crypto/pq/mldsa65/polyvec.h @@ -0,0 +1,93 @@ +#ifndef POLYVEC_H +#define POLYVEC_H + +#include +#include "params.h" +#include "poly.h" + +/* Vectors of polynomials of length L */ +typedef struct { + poly vec[L]; +} polyvecl; + +#define polyvecl_uniform_eta DILITHIUM_NAMESPACE(polyvecl_uniform_eta) +void polyvecl_uniform_eta(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyvecl_uniform_gamma1 DILITHIUM_NAMESPACE(polyvecl_uniform_gamma1) +void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyvecl_reduce DILITHIUM_NAMESPACE(polyvecl_reduce) +void polyvecl_reduce(polyvecl *v); + +#define polyvecl_add DILITHIUM_NAMESPACE(polyvecl_add) +void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v); + +#define polyvecl_ntt DILITHIUM_NAMESPACE(polyvecl_ntt) +void polyvecl_ntt(polyvecl *v); +#define polyvecl_invntt_tomont DILITHIUM_NAMESPACE(polyvecl_invntt_tomont) +void polyvecl_invntt_tomont(polyvecl *v); +#define polyvecl_pointwise_poly_montgomery DILITHIUM_NAMESPACE(polyvecl_pointwise_poly_montgomery) +void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v); +#define polyvecl_pointwise_acc_montgomery \ + DILITHIUM_NAMESPACE(polyvecl_pointwise_acc_montgomery) +void polyvecl_pointwise_acc_montgomery(poly *w, + const polyvecl *u, + const polyvecl *v); + + +#define polyvecl_chknorm DILITHIUM_NAMESPACE(polyvecl_chknorm) +int polyvecl_chknorm(const polyvecl *v, int32_t B); + + + +/* Vectors of polynomials of length K */ +typedef struct { + poly vec[K]; +} polyveck; + +#define polyveck_uniform_eta DILITHIUM_NAMESPACE(polyveck_uniform_eta) +void polyveck_uniform_eta(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t nonce); + +#define polyveck_reduce DILITHIUM_NAMESPACE(polyveck_reduce) +void polyveck_reduce(polyveck *v); +#define polyveck_caddq DILITHIUM_NAMESPACE(polyveck_caddq) +void polyveck_caddq(polyveck *v); + +#define polyveck_add DILITHIUM_NAMESPACE(polyveck_add) +void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v); +#define polyveck_sub DILITHIUM_NAMESPACE(polyveck_sub) +void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v); +#define polyveck_shiftl DILITHIUM_NAMESPACE(polyveck_shiftl) +void polyveck_shiftl(polyveck *v); + +#define polyveck_ntt DILITHIUM_NAMESPACE(polyveck_ntt) +void polyveck_ntt(polyveck *v); +#define polyveck_invntt_tomont DILITHIUM_NAMESPACE(polyveck_invntt_tomont) +void polyveck_invntt_tomont(polyveck *v); +#define polyveck_pointwise_poly_montgomery DILITHIUM_NAMESPACE(polyveck_pointwise_poly_montgomery) +void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v); + +#define polyveck_chknorm DILITHIUM_NAMESPACE(polyveck_chknorm) +int polyveck_chknorm(const polyveck *v, int32_t B); + +#define polyveck_power2round DILITHIUM_NAMESPACE(polyveck_power2round) +void polyveck_power2round(polyveck *v1, polyveck *v0, const polyveck *v); +#define polyveck_decompose DILITHIUM_NAMESPACE(polyveck_decompose) +void polyveck_decompose(polyveck *v1, polyveck *v0, const polyveck *v); +#define polyveck_make_hint DILITHIUM_NAMESPACE(polyveck_make_hint) +unsigned int polyveck_make_hint(polyveck *h, + const polyveck *v0, + const polyveck *v1); +#define polyveck_use_hint DILITHIUM_NAMESPACE(polyveck_use_hint) +void polyveck_use_hint(polyveck *w, const polyveck *v, const polyveck *h); + +#define polyveck_pack_w1 DILITHIUM_NAMESPACE(polyveck_pack_w1) +void polyveck_pack_w1(uint8_t r[K*POLYW1_PACKEDBYTES], const polyveck *w1); + +#define polyvec_matrix_expand DILITHIUM_NAMESPACE(polyvec_matrix_expand) +void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]); + +#define polyvec_matrix_pointwise_montgomery DILITHIUM_NAMESPACE(polyvec_matrix_pointwise_montgomery) +void polyvec_matrix_pointwise_montgomery(polyveck *t, const polyvecl mat[K], const polyvecl *v); + +#endif diff --git a/src/crypto/pq/mldsa65/randombytes.h b/src/crypto/pq/mldsa65/randombytes.h new file mode 100644 index 0000000..3308bdc --- /dev/null +++ b/src/crypto/pq/mldsa65/randombytes.h @@ -0,0 +1,15 @@ +#ifndef VEXTA_PQ_RANDOMBYTES_H +#define VEXTA_PQ_RANDOMBYTES_H +#include +#include +#ifdef __cplusplus +extern "C" { +#endif +/* VEXTA: unified prototype. Definition lives in pq_randombytes.cpp and is + backed by Bitcoin's CSPRNG (GetStrongRandBytes). The upstream test-only + randombytes.c files were deleted — they must never ship in a wallet. */ +void randombytes(uint8_t *out, size_t outlen); +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/crypto/pq/mldsa65/reduce.c b/src/crypto/pq/mldsa65/reduce.c new file mode 100644 index 0000000..8479a22 --- /dev/null +++ b/src/crypto/pq/mldsa65/reduce.c @@ -0,0 +1,69 @@ +#include +#include "params.h" +#include "reduce.h" + +/************************************************* +* Name: montgomery_reduce +* +* Description: For finite field element a with -2^{31}Q <= a <= Q*2^31, +* compute r \equiv a*2^{-32} (mod Q) such that -Q < r < Q. +* +* Arguments: - int64_t: finite field element a +* +* Returns r. +**************************************************/ +int32_t montgomery_reduce(int64_t a) { + int32_t t; + + t = (int64_t)(int32_t)a*QINV; + t = (a - (int64_t)t*Q) >> 32; + return t; +} + +/************************************************* +* Name: reduce32 +* +* Description: For finite field element a with a <= 2^{31} - 2^{22} - 1, +* compute r \equiv a (mod Q) such that -6283008 <= r <= 6283008. +* +* Arguments: - int32_t: finite field element a +* +* Returns r. +**************************************************/ +int32_t reduce32(int32_t a) { + int32_t t; + + t = (a + (1 << 22)) >> 23; + t = a - t*Q; + return t; +} + +/************************************************* +* Name: caddq +* +* Description: Add Q if input coefficient is negative. +* +* Arguments: - int32_t: finite field element a +* +* Returns r. +**************************************************/ +int32_t caddq(int32_t a) { + a += (a >> 31) & Q; + return a; +} + +/************************************************* +* Name: freeze +* +* Description: For finite field element a, compute standard +* representative r = a mod^+ Q. +* +* Arguments: - int32_t: finite field element a +* +* Returns r. +**************************************************/ +int32_t freeze(int32_t a) { + a = reduce32(a); + a = caddq(a); + return a; +} diff --git a/src/crypto/pq/mldsa65/reduce.h b/src/crypto/pq/mldsa65/reduce.h new file mode 100644 index 0000000..26d9b4e --- /dev/null +++ b/src/crypto/pq/mldsa65/reduce.h @@ -0,0 +1,22 @@ +#ifndef REDUCE_H +#define REDUCE_H + +#include +#include "params.h" + +#define MONT -4186625 // 2^32 % Q +#define QINV 58728449 // q^(-1) mod 2^32 + +#define montgomery_reduce DILITHIUM_NAMESPACE(montgomery_reduce) +int32_t montgomery_reduce(int64_t a); + +#define reduce32 DILITHIUM_NAMESPACE(reduce32) +int32_t reduce32(int32_t a); + +#define caddq DILITHIUM_NAMESPACE(caddq) +int32_t caddq(int32_t a); + +#define freeze DILITHIUM_NAMESPACE(freeze) +int32_t freeze(int32_t a); + +#endif diff --git a/src/crypto/pq/mldsa65/rounding.c b/src/crypto/pq/mldsa65/rounding.c new file mode 100644 index 0000000..889f0a2 --- /dev/null +++ b/src/crypto/pq/mldsa65/rounding.c @@ -0,0 +1,102 @@ +#include +#include "params.h" +#include "rounding.h" + +/************************************************* +* Name: power2round +* +* Description: For finite field element a, compute a0, a1 such that +* a mod^+ Q = a1*2^D + a0 with -2^{D-1} < a0 <= 2^{D-1}. +* Assumes a to be standard representative. +* +* Arguments: - int32_t a: input element +* - int32_t *a0: pointer to output element a0 +* +* Returns a1. +**************************************************/ +int32_t power2round(int32_t *a0, int32_t a) { + int32_t a1; + + a1 = (a + (1 << (D-1)) - 1) >> D; + *a0 = a - (a1 << D); + return a1; +} + +/************************************************* +* Name: decompose +* +* Description: For finite field element a, compute high and low bits a0, a1 such +* that a mod^+ Q = a1*ALPHA + a0 with -ALPHA/2 < a0 <= ALPHA/2 except +* if a1 = (Q-1)/ALPHA where we set a1 = 0 and +* -ALPHA/2 <= a0 = a mod^+ Q - Q < 0. Assumes a to be standard +* representative. +* +* Arguments: - int32_t a: input element +* - int32_t *a0: pointer to output element a0 +* +* Returns a1. +**************************************************/ +int32_t decompose(int32_t *a0, int32_t a) { + int32_t a1; + + a1 = (a + 127) >> 7; +#if GAMMA2 == (Q-1)/32 + a1 = (a1*1025 + (1 << 21)) >> 22; + a1 &= 15; +#elif GAMMA2 == (Q-1)/88 + a1 = (a1*11275 + (1 << 23)) >> 24; + a1 ^= ((43 - a1) >> 31) & a1; +#endif + + *a0 = a - a1*2*GAMMA2; + *a0 -= (((Q-1)/2 - *a0) >> 31) & Q; + return a1; +} + +/************************************************* +* Name: make_hint +* +* Description: Compute hint bit indicating whether the low bits of the +* input element overflow into the high bits. +* +* Arguments: - int32_t a0: low bits of input element +* - int32_t a1: high bits of input element +* +* Returns 1 if overflow. +**************************************************/ +unsigned int make_hint(int32_t a0, int32_t a1) { + if(a0 > GAMMA2 || a0 < -GAMMA2 || (a0 == -GAMMA2 && a1 != 0)) + return 1; + + return 0; +} + +/************************************************* +* Name: use_hint +* +* Description: Correct high bits according to hint. +* +* Arguments: - int32_t a: input element +* - unsigned int hint: hint bit +* +* Returns corrected high bits. +**************************************************/ +int32_t use_hint(int32_t a, unsigned int hint) { + int32_t a0, a1; + + a1 = decompose(&a0, a); + if(hint == 0) + return a1; + +#if GAMMA2 == (Q-1)/32 + if(a0 > 0) + return (a1 + 1) & 15; + else + return (a1 - 1) & 15; +#elif GAMMA2 == (Q-1)/88 + if(a0 > 0) + return (a1 == 43) ? 0 : a1 + 1; + else + return (a1 == 0) ? 43 : a1 - 1; +#endif +} diff --git a/src/crypto/pq/mldsa65/rounding.h b/src/crypto/pq/mldsa65/rounding.h new file mode 100644 index 0000000..b72e8e8 --- /dev/null +++ b/src/crypto/pq/mldsa65/rounding.h @@ -0,0 +1,19 @@ +#ifndef ROUNDING_H +#define ROUNDING_H + +#include +#include "params.h" + +#define power2round DILITHIUM_NAMESPACE(power2round) +int32_t power2round(int32_t *a0, int32_t a); + +#define decompose DILITHIUM_NAMESPACE(decompose) +int32_t decompose(int32_t *a0, int32_t a); + +#define make_hint DILITHIUM_NAMESPACE(make_hint) +unsigned int make_hint(int32_t a0, int32_t a1); + +#define use_hint DILITHIUM_NAMESPACE(use_hint) +int32_t use_hint(int32_t a, unsigned int hint); + +#endif diff --git a/src/crypto/pq/mldsa65/sign.c b/src/crypto/pq/mldsa65/sign.c new file mode 100644 index 0000000..3429797 --- /dev/null +++ b/src/crypto/pq/mldsa65/sign.c @@ -0,0 +1,477 @@ +#include +#include "params.h" +#include "sign.h" +#include "packing.h" +#include "polyvec.h" +#include "poly.h" +#include "randombytes.h" +#include "symmetric.h" +#include "fips202.h" + +#ifndef VEXTA_PQ_VERIFY_ONLY +/************************************************* +* Name: crypto_sign_keypair +* +* Description: Generates public and private key. +* +* Arguments: - uint8_t *pk: pointer to output public key (allocated +* array of CRYPTO_PUBLICKEYBYTES bytes) +* - uint8_t *sk: pointer to output private key (allocated +* array of CRYPTO_SECRETKEYBYTES bytes) +* +* Returns 0 (success) +**************************************************/ +int crypto_sign_keypair_from_seed(uint8_t *pk, uint8_t *sk, const uint8_t *seed) { + uint8_t seedbuf[2*SEEDBYTES + CRHBYTES]; + uint8_t tr[TRBYTES]; + const uint8_t *rho, *rhoprime, *key; + polyvecl mat[K]; + polyvecl s1, s1hat; + polyveck s2, t1, t0; + for (int _i=0;_i OMEGA) + goto rej; + + /* Write signature */ + pack_sig(sig, sig, &z, &h); + *siglen = CRYPTO_BYTES; + return 0; +} + +/************************************************* +* Name: crypto_sign_signature +* +* Description: Computes signature. +* +* Arguments: - uint8_t *sig: pointer to output signature (of length CRYPTO_BYTES) +* - size_t *siglen: pointer to output length of signature +* - uint8_t *m: pointer to message to be signed +* - size_t mlen: length of message +* - uint8_t *ctx: pointer to contex string +* - size_t ctxlen: length of contex string +* - uint8_t *sk: pointer to bit-packed secret key +* +* Returns 0 (success) or -1 (context string too long) +**************************************************/ +int crypto_sign_signature(uint8_t *sig, + size_t *siglen, + const uint8_t *m, + size_t mlen, + const uint8_t *ctx, + size_t ctxlen, + const uint8_t *sk) +{ + size_t i; + uint8_t pre[257]; + uint8_t rnd[RNDBYTES]; + + if(ctxlen > 255) + return -1; + + /* Prepare pre = (0, ctxlen, ctx) */ + pre[0] = 0; + pre[1] = ctxlen; + for(i = 0; i < ctxlen; i++) + pre[2 + i] = ctx[i]; + +#ifdef DILITHIUM_RANDOMIZED_SIGNING + randombytes(rnd, RNDBYTES); +#else + for(i=0;i 255) + return -1; + + pre[0] = 0; + pre[1] = ctxlen; + for(i = 0; i < ctxlen; i++) + pre[2 + i] = ctx[i]; + + return crypto_sign_verify_internal(sig,siglen,m,mlen,pre,2+ctxlen,pk); +} + +/************************************************* +* Name: crypto_sign_open +* +* Description: Verify signed message. +* +* Arguments: - uint8_t *m: pointer to output message (allocated +* array with smlen bytes), can be equal to sm +* - size_t *mlen: pointer to output length of message +* - const uint8_t *sm: pointer to signed message +* - size_t smlen: length of signed message +* - const uint8_t *ctx: pointer to context tring +* - size_t ctxlen: length of context string +* - const uint8_t *pk: pointer to bit-packed public key +* +* Returns 0 if signed message could be verified correctly and -1 otherwise +**************************************************/ +int crypto_sign_open(uint8_t *m, + size_t *mlen, + const uint8_t *sm, + size_t smlen, + const uint8_t *ctx, + size_t ctxlen, + const uint8_t *pk) +{ + size_t i; + + if(smlen < CRYPTO_BYTES) + goto badsig; + + *mlen = smlen - CRYPTO_BYTES; + if(crypto_sign_verify(sm, CRYPTO_BYTES, sm + CRYPTO_BYTES, *mlen, ctx, ctxlen, pk)) + goto badsig; + else { + /* All good, copy msg, return 0 */ + for(i = 0; i < *mlen; ++i) + m[i] = sm[CRYPTO_BYTES + i]; + return 0; + } + +badsig: + /* Signature verification failed */ + *mlen = 0; + for(i = 0; i < smlen; ++i) + m[i] = 0; + + return -1; +} diff --git a/src/crypto/pq/mldsa65/sign.h b/src/crypto/pq/mldsa65/sign.h new file mode 100644 index 0000000..2741e8f --- /dev/null +++ b/src/crypto/pq/mldsa65/sign.h @@ -0,0 +1,56 @@ +#ifndef SIGN_H +#define SIGN_H + +#include +#include +#include "params.h" +#include "polyvec.h" +#include "poly.h" + +#define crypto_sign_keypair DILITHIUM_NAMESPACE(keypair) +int crypto_sign_keypair(uint8_t *pk, uint8_t *sk); + +#define crypto_sign_signature_internal DILITHIUM_NAMESPACE(signature_internal) +int crypto_sign_signature_internal(uint8_t *sig, + size_t *siglen, + const uint8_t *m, + size_t mlen, + const uint8_t *pre, + size_t prelen, + const uint8_t rnd[RNDBYTES], + const uint8_t *sk); + +#define crypto_sign_signature DILITHIUM_NAMESPACE(signature) +int crypto_sign_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +#define crypto_sign DILITHIUM_NAMESPACETOP +int crypto_sign(uint8_t *sm, size_t *smlen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *sk); + +#define crypto_sign_verify_internal DILITHIUM_NAMESPACE(verify_internal) +int crypto_sign_verify_internal(const uint8_t *sig, + size_t siglen, + const uint8_t *m, + size_t mlen, + const uint8_t *pre, + size_t prelen, + const uint8_t *pk); + +#define crypto_sign_verify DILITHIUM_NAMESPACE(verify) +int crypto_sign_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +#define crypto_sign_open DILITHIUM_NAMESPACE(open) +int crypto_sign_open(uint8_t *m, size_t *mlen, + const uint8_t *sm, size_t smlen, + const uint8_t *ctx, size_t ctxlen, + const uint8_t *pk); + +#endif diff --git a/src/crypto/pq/mldsa65/symmetric-shake.c b/src/crypto/pq/mldsa65/symmetric-shake.c new file mode 100644 index 0000000..11ec09c --- /dev/null +++ b/src/crypto/pq/mldsa65/symmetric-shake.c @@ -0,0 +1,28 @@ +#include +#include "params.h" +#include "symmetric.h" +#include "fips202.h" + +void dilithium_shake128_stream_init(keccak_state *state, const uint8_t seed[SEEDBYTES], uint16_t nonce) +{ + uint8_t t[2]; + t[0] = nonce; + t[1] = nonce >> 8; + + shake128_init(state); + shake128_absorb(state, seed, SEEDBYTES); + shake128_absorb(state, t, 2); + shake128_finalize(state); +} + +void dilithium_shake256_stream_init(keccak_state *state, const uint8_t seed[CRHBYTES], uint16_t nonce) +{ + uint8_t t[2]; + t[0] = nonce; + t[1] = nonce >> 8; + + shake256_init(state); + shake256_absorb(state, seed, CRHBYTES); + shake256_absorb(state, t, 2); + shake256_finalize(state); +} diff --git a/src/crypto/pq/mldsa65/symmetric.h b/src/crypto/pq/mldsa65/symmetric.h new file mode 100644 index 0000000..cba12d1 --- /dev/null +++ b/src/crypto/pq/mldsa65/symmetric.h @@ -0,0 +1,34 @@ +#ifndef SYMMETRIC_H +#define SYMMETRIC_H + +#include +#include "params.h" + +#include "fips202.h" + +typedef keccak_state stream128_state; +typedef keccak_state stream256_state; + +#define dilithium_shake128_stream_init DILITHIUM_NAMESPACE(dilithium_shake128_stream_init) +void dilithium_shake128_stream_init(keccak_state *state, + const uint8_t seed[SEEDBYTES], + uint16_t nonce); + +#define dilithium_shake256_stream_init DILITHIUM_NAMESPACE(dilithium_shake256_stream_init) +void dilithium_shake256_stream_init(keccak_state *state, + const uint8_t seed[CRHBYTES], + uint16_t nonce); + +#define STREAM128_BLOCKBYTES SHAKE128_RATE +#define STREAM256_BLOCKBYTES SHAKE256_RATE + +#define stream128_init(STATE, SEED, NONCE) \ + dilithium_shake128_stream_init(STATE, SEED, NONCE) +#define stream128_squeezeblocks(OUT, OUTBLOCKS, STATE) \ + shake128_squeezeblocks(OUT, OUTBLOCKS, STATE) +#define stream256_init(STATE, SEED, NONCE) \ + dilithium_shake256_stream_init(STATE, SEED, NONCE) +#define stream256_squeezeblocks(OUT, OUTBLOCKS, STATE) \ + shake256_squeezeblocks(OUT, OUTBLOCKS, STATE) + +#endif diff --git a/src/crypto/pq/pq.cpp b/src/crypto/pq/pq.cpp new file mode 100644 index 0000000..ccdaac1 --- /dev/null +++ b/src/crypto/pq/pq.cpp @@ -0,0 +1,37 @@ +// VEXTA: wrapper impl. Bridges to the namespaced C reference symbols. +#include +extern "C" { +// ML-DSA-65 (DILITHIUM_MODE=3 -> pqcrystals_dilithium3_ref_*) +int pqcrystals_dilithium3_ref_keypair(uint8_t* pk, uint8_t* sk); +int crypto_sign_keypair_from_seed(uint8_t* pk, uint8_t* sk, const uint8_t* seed); +int pqcrystals_dilithium3_ref_signature(uint8_t* sig, size_t* siglen, + const uint8_t* m, size_t mlen, const uint8_t* ctx, size_t ctxlen, const uint8_t* sk); +int pqcrystals_dilithium3_ref_verify(const uint8_t* sig, size_t siglen, + const uint8_t* m, size_t mlen, const uint8_t* ctx, size_t ctxlen, const uint8_t* pk); +// SPHINCS+ (bare crypto_sign_* in ref) +int crypto_sign_keypair(uint8_t* pk, uint8_t* sk); +int crypto_sign_seed_keypair(uint8_t* pk, uint8_t* sk, const uint8_t* seed); +int crypto_sign_signature(uint8_t* sig, size_t* siglen, const uint8_t* m, size_t mlen, const uint8_t* sk); +int crypto_sign_verify(const uint8_t* sig, size_t siglen, const uint8_t* m, size_t mlen, const uint8_t* pk); +} +namespace pq { +#ifndef VEXTA_PQ_VERIFY_ONLY +bool MLDSA65::keygen(uint8_t* pk, uint8_t* sk){ return pqcrystals_dilithium3_ref_keypair(pk,sk)==0; } +bool MLDSA65::keygen_from_seed(uint8_t* pk, uint8_t* sk, const uint8_t* seed){ return crypto_sign_keypair_from_seed(pk,sk,seed)==0; } +bool MLDSA65::sign(uint8_t* sig,size_t* sl,const uint8_t* m,size_t ml,const uint8_t* sk){ + return pqcrystals_dilithium3_ref_signature(sig,sl,m,ml,nullptr,0,sk)==0; } +#endif + +bool MLDSA65::verify(const uint8_t* sig,size_t sl,const uint8_t* m,size_t ml,const uint8_t* pk){ + return pqcrystals_dilithium3_ref_verify(sig,sl,m,ml,nullptr,0,pk)==0; } + +#ifndef VEXTA_PQ_VERIFY_ONLY +bool SPHINCS128s::keygen(uint8_t* pk,uint8_t* sk){ return crypto_sign_keypair(pk,sk)==0; } +bool SPHINCS128s::keygen_from_seed(uint8_t* pk,uint8_t* sk,const uint8_t* seed){ return crypto_sign_seed_keypair(pk,sk,seed)==0; } +bool SPHINCS128s::sign(uint8_t* sig,size_t* sl,const uint8_t* m,size_t ml,const uint8_t* sk){ + return crypto_sign_signature(sig,sl,m,ml,sk)==0; } +#endif + +bool SPHINCS128s::verify(const uint8_t* sig,size_t sl,const uint8_t* m,size_t ml,const uint8_t* pk){ + return crypto_sign_verify(sig,sl,m,ml,pk)==0; } +} diff --git a/src/crypto/pq/pq.h b/src/crypto/pq/pq.h new file mode 100644 index 0000000..96b7a5f --- /dev/null +++ b/src/crypto/pq/pq.h @@ -0,0 +1,30 @@ +// VEXTA: uniform post-quantum signature interface over the vendored C references. +#ifndef VEXTA_CRYPTO_PQ_H +#define VEXTA_CRYPTO_PQ_H +#include +#include +#include +namespace pq { + +struct MLDSA65 { + static constexpr size_t PUBKEY_BYTES = 1952; // ML-DSA-65 (FIPS 204) + static constexpr size_t SECKEY_BYTES = 4032; + static constexpr size_t SIG_BYTES = 3309; + static bool keygen(uint8_t* pk, uint8_t* sk); + static bool keygen_from_seed(uint8_t* pk, uint8_t* sk, const uint8_t* seed); + static bool sign(uint8_t* sig, size_t* siglen, const uint8_t* msg, size_t msglen, const uint8_t* sk); + static bool verify(const uint8_t* sig, size_t siglen, const uint8_t* msg, size_t msglen, const uint8_t* pk); +}; + +struct SPHINCS128s { + static constexpr size_t PUBKEY_BYTES = 32; // SLH-DSA sha2-128s (FIPS 205) + static constexpr size_t SECKEY_BYTES = 64; + static constexpr size_t SIG_BYTES = 7856; + static bool keygen(uint8_t* pk, uint8_t* sk); + static bool keygen_from_seed(uint8_t* pk, uint8_t* sk, const uint8_t* seed); + static bool sign(uint8_t* sig, size_t* siglen, const uint8_t* msg, size_t msglen, const uint8_t* sk); + static bool verify(const uint8_t* sig, size_t siglen, const uint8_t* msg, size_t msglen, const uint8_t* pk); +}; + +} // namespace pq +#endif diff --git a/src/crypto/pq/pq_randombytes.cpp b/src/crypto/pq/pq_randombytes.cpp new file mode 100644 index 0000000..2e36ffd --- /dev/null +++ b/src/crypto/pq/pq_randombytes.cpp @@ -0,0 +1,18 @@ +// VEXTA: post-quantum RNG shim. Backs the vendored references' randombytes() +// with Vexta Core's strong CSPRNG (GetStrongRandBytes). Chunked to <=32 bytes +// per call because the internal RNG path expects small requests. + +#include + +#include +#include + +extern "C" void randombytes(uint8_t* out, size_t outlen) +{ + while (outlen > 0) { + const size_t chunk = outlen < 32 ? outlen : 32; + GetStrongRandBytes(out, static_cast(chunk)); + out += chunk; + outlen -= chunk; + } +} diff --git a/src/crypto/pq/sphincsplus.commit b/src/crypto/pq/sphincsplus.commit new file mode 100644 index 0000000..8b948fd --- /dev/null +++ b/src/crypto/pq/sphincsplus.commit @@ -0,0 +1,2 @@ +7ec789ace6874d875f4bb84cb61b81155398167e +2024-04-15 18:30:51 +0200 diff --git a/src/crypto/pq/sphincsplus/LICENSE b/src/crypto/pq/sphincsplus/LICENSE new file mode 100644 index 0000000..ca88ec4 --- /dev/null +++ b/src/crypto/pq/sphincsplus/LICENSE @@ -0,0 +1,2 @@ +SPDX-License-Identifier: (LicenseRef-SPHINCS-PLUS-Public-Domain OR CC0-1.0 OR 0BSD OR MIT-0) AND MIT + diff --git a/src/crypto/pq/sphincsplus/address.c b/src/crypto/pq/sphincsplus/address.c new file mode 100644 index 0000000..b136af8 --- /dev/null +++ b/src/crypto/pq/sphincsplus/address.c @@ -0,0 +1,104 @@ +#include +#include + +#include "address.h" +#include "params.h" +#include "utils.h" + +/* + * Specify which level of Merkle tree (the "layer") we're working on + */ +void set_layer_addr(uint32_t addr[8], uint32_t layer) +{ + ((unsigned char *)addr)[SPX_OFFSET_LAYER] = (unsigned char)layer; +} + +/* + * Specify which Merkle tree within the level (the "tree address") we're working on + */ +void set_tree_addr(uint32_t addr[8], uint64_t tree) +{ +#if (SPX_TREE_HEIGHT * (SPX_D - 1)) > 64 + #error Subtree addressing is currently limited to at most 2^64 trees +#endif + ull_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_TREE], 8, tree ); +} + +/* + * Specify the reason we'll use this address structure for, that is, what + * hash will we compute with it. This is used so that unrelated types of + * hashes don't accidentally get the same address structure. The type will be + * one of the SPX_ADDR_TYPE constants + */ +void set_type(uint32_t addr[8], uint32_t type) +{ + ((unsigned char *)addr)[SPX_OFFSET_TYPE] = (unsigned char)type; +} + +/* + * Copy the layer and tree fields of the address structure. This is used + * when we're doing multiple types of hashes within the same Merkle tree + */ +void copy_subtree_addr(uint32_t out[8], const uint32_t in[8]) +{ + memcpy( out, in, SPX_OFFSET_TREE+8 ); +} + +/* These functions are used for OTS addresses. */ + +/* + * Specify which Merkle leaf we're working on; that is, which OTS keypair + * we're talking about. + */ +void set_keypair_addr(uint32_t addr[8], uint32_t keypair) +{ + u32_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_KP_ADDR], keypair); +} + +/* + * Copy the layer, tree and keypair fields of the address structure. This is + * used when we're doing multiple things within the same OTS keypair + */ +void copy_keypair_addr(uint32_t out[8], const uint32_t in[8]) +{ + memcpy( out, in, SPX_OFFSET_TREE+8 ); + memcpy( (unsigned char *)out + SPX_OFFSET_KP_ADDR, (unsigned char *)in + SPX_OFFSET_KP_ADDR, 4); +} + +/* + * Specify which Merkle chain within the OTS we're working with + * (the chain address) + */ +void set_chain_addr(uint32_t addr[8], uint32_t chain) +{ + ((unsigned char *)addr)[SPX_OFFSET_CHAIN_ADDR] = (unsigned char)chain; +} + +/* + * Specify where in the Merkle chain we are +* (the hash address) + */ +void set_hash_addr(uint32_t addr[8], uint32_t hash) +{ + ((unsigned char *)addr)[SPX_OFFSET_HASH_ADDR] = (unsigned char)hash; +} + +/* These functions are used for all hash tree addresses (including FORS). */ + +/* + * Specify the height of the node in the Merkle/FORS tree we are in + * (the tree height) + */ +void set_tree_height(uint32_t addr[8], uint32_t tree_height) +{ + ((unsigned char *)addr)[SPX_OFFSET_TREE_HGT] = (unsigned char)tree_height; +} + +/* + * Specify the distance from the left edge of the node in the Merkle/FORS tree + * (the tree index) + */ +void set_tree_index(uint32_t addr[8], uint32_t tree_index) +{ + u32_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_TREE_INDEX], tree_index ); +} diff --git a/src/crypto/pq/sphincsplus/address.h b/src/crypto/pq/sphincsplus/address.h new file mode 100644 index 0000000..49f8d66 --- /dev/null +++ b/src/crypto/pq/sphincsplus/address.h @@ -0,0 +1,51 @@ +#ifndef SPX_ADDRESS_H +#define SPX_ADDRESS_H + +#include +#include "params.h" + +/* The hash types that are passed to set_type */ +#define SPX_ADDR_TYPE_WOTS 0 +#define SPX_ADDR_TYPE_WOTSPK 1 +#define SPX_ADDR_TYPE_HASHTREE 2 +#define SPX_ADDR_TYPE_FORSTREE 3 +#define SPX_ADDR_TYPE_FORSPK 4 +#define SPX_ADDR_TYPE_WOTSPRF 5 +#define SPX_ADDR_TYPE_FORSPRF 6 + +#define set_layer_addr SPX_NAMESPACE(set_layer_addr) +void set_layer_addr(uint32_t addr[8], uint32_t layer); + +#define set_tree_addr SPX_NAMESPACE(set_tree_addr) +void set_tree_addr(uint32_t addr[8], uint64_t tree); + +#define set_type SPX_NAMESPACE(set_type) +void set_type(uint32_t addr[8], uint32_t type); + +/* Copies the layer and tree part of one address into the other */ +#define copy_subtree_addr SPX_NAMESPACE(copy_subtree_addr) +void copy_subtree_addr(uint32_t out[8], const uint32_t in[8]); + +/* These functions are used for WOTS and FORS addresses. */ + +#define set_keypair_addr SPX_NAMESPACE(set_keypair_addr) +void set_keypair_addr(uint32_t addr[8], uint32_t keypair); + +#define set_chain_addr SPX_NAMESPACE(set_chain_addr) +void set_chain_addr(uint32_t addr[8], uint32_t chain); + +#define set_hash_addr SPX_NAMESPACE(set_hash_addr) +void set_hash_addr(uint32_t addr[8], uint32_t hash); + +#define copy_keypair_addr SPX_NAMESPACE(copy_keypair_addr) +void copy_keypair_addr(uint32_t out[8], const uint32_t in[8]); + +/* These functions are used for all hash tree addresses (including FORS). */ + +#define set_tree_height SPX_NAMESPACE(set_tree_height) +void set_tree_height(uint32_t addr[8], uint32_t tree_height); + +#define set_tree_index SPX_NAMESPACE(set_tree_index) +void set_tree_index(uint32_t addr[8], uint32_t tree_index); + +#endif diff --git a/src/crypto/pq/sphincsplus/api.h b/src/crypto/pq/sphincsplus/api.h new file mode 100644 index 0000000..d57a148 --- /dev/null +++ b/src/crypto/pq/sphincsplus/api.h @@ -0,0 +1,77 @@ +#ifndef SPX_API_H +#define SPX_API_H + +#include +#include + +#include "params.h" + +#define CRYPTO_ALGNAME "SPHINCS+" + +#define CRYPTO_SECRETKEYBYTES SPX_SK_BYTES +#define CRYPTO_PUBLICKEYBYTES SPX_PK_BYTES +#define CRYPTO_BYTES SPX_BYTES +#define CRYPTO_SEEDBYTES 3*SPX_N + +/* + * Returns the length of a secret key, in bytes + */ +unsigned long long crypto_sign_secretkeybytes(void); + +/* + * Returns the length of a public key, in bytes + */ +unsigned long long crypto_sign_publickeybytes(void); + +/* + * Returns the length of a signature, in bytes + */ +unsigned long long crypto_sign_bytes(void); + +/* + * Returns the length of the seed required to generate a key pair, in bytes + */ +unsigned long long crypto_sign_seedbytes(void); + +/* + * Generates a SPHINCS+ key pair given a seed. + * Format sk: [SK_SEED || SK_PRF || PUB_SEED || root] + * Format pk: [root || PUB_SEED] + */ +int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, + const unsigned char *seed); + +/* + * Generates a SPHINCS+ key pair. + * Format sk: [SK_SEED || SK_PRF || PUB_SEED || root] + * Format pk: [root || PUB_SEED] + */ +int crypto_sign_keypair(unsigned char *pk, unsigned char *sk); + +/** + * Returns an array containing a detached signature. + */ +int crypto_sign_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, const uint8_t *sk); + +/** + * Verifies a detached signature and message under a given public key. + */ +int crypto_sign_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, const uint8_t *pk); + +/** + * Returns an array containing the signature followed by the message. + */ +int crypto_sign(unsigned char *sm, unsigned long long *smlen, + const unsigned char *m, unsigned long long mlen, + const unsigned char *sk); + +/** + * Verifies a given signature-message pair under a given public key. + */ +int crypto_sign_open(unsigned char *m, unsigned long long *mlen, + const unsigned char *sm, unsigned long long smlen, + const unsigned char *pk); + +#endif diff --git a/src/crypto/pq/sphincsplus/context.h b/src/crypto/pq/sphincsplus/context.h new file mode 100644 index 0000000..aded564 --- /dev/null +++ b/src/crypto/pq/sphincsplus/context.h @@ -0,0 +1,28 @@ +#ifndef SPX_CONTEXT_H +#define SPX_CONTEXT_H + +#include + +#include "params.h" + +typedef struct { + uint8_t pub_seed[SPX_N]; + uint8_t sk_seed[SPX_N]; + +#ifdef SPX_SHA2 + // sha256 state that absorbed pub_seed + uint8_t state_seeded[40]; + +# if SPX_SHA512 + // sha512 state that absorbed pub_seed + uint8_t state_seeded_512[72]; +# endif +#endif + +#ifdef SPX_HARAKA + uint64_t tweaked512_rc64[10][8]; + uint32_t tweaked256_rc32[10][8]; +#endif +} spx_ctx; + +#endif diff --git a/src/crypto/pq/sphincsplus/fips202.c b/src/crypto/pq/sphincsplus/fips202.c new file mode 100644 index 0000000..8262d91 --- /dev/null +++ b/src/crypto/pq/sphincsplus/fips202.c @@ -0,0 +1,598 @@ +/* Based on the public domain implementation in + * crypto_hash/keccakc512/simple/ from http://bench.cr.yp.to/supercop.html + * by Ronny Van Keer + * and the public domain "TweetFips202" implementation + * from https://twitter.com/tweetfips202 + * by Gilles Van Assche, Daniel J. Bernstein, and Peter Schwabe */ + +#include +#include + +#include "fips202.h" + +#define NROUNDS 24 +#define ROL(a, offset) (((a) << (offset)) ^ ((a) >> (64 - (offset)))) + +/************************************************* + * Name: load64 + * + * Description: Load 8 bytes into uint64_t in little-endian order + * + * Arguments: - const uint8_t *x: pointer to input byte array + * + * Returns the loaded 64-bit unsigned integer + **************************************************/ +static uint64_t load64(const uint8_t *x) { + uint64_t r = 0; + for (size_t i = 0; i < 8; ++i) { + r |= (uint64_t)x[i] << 8 * i; + } + + return r; +} + +/************************************************* + * Name: store64 + * + * Description: Store a 64-bit integer to a byte array in little-endian order + * + * Arguments: - uint8_t *x: pointer to the output byte array + * - uint64_t u: input 64-bit unsigned integer + **************************************************/ +static void store64(uint8_t *x, uint64_t u) { + for (size_t i = 0; i < 8; ++i) { + x[i] = (uint8_t) (u >> 8 * i); + } +} + +/* Keccak round constants */ +static const uint64_t KeccakF_RoundConstants[NROUNDS] = { + 0x0000000000000001ULL, 0x0000000000008082ULL, + 0x800000000000808aULL, 0x8000000080008000ULL, + 0x000000000000808bULL, 0x0000000080000001ULL, + 0x8000000080008081ULL, 0x8000000000008009ULL, + 0x000000000000008aULL, 0x0000000000000088ULL, + 0x0000000080008009ULL, 0x000000008000000aULL, + 0x000000008000808bULL, 0x800000000000008bULL, + 0x8000000000008089ULL, 0x8000000000008003ULL, + 0x8000000000008002ULL, 0x8000000000000080ULL, + 0x000000000000800aULL, 0x800000008000000aULL, + 0x8000000080008081ULL, 0x8000000000008080ULL, + 0x0000000080000001ULL, 0x8000000080008008ULL +}; + +/************************************************* + * Name: KeccakF1600_StatePermute + * + * Description: The Keccak F1600 Permutation + * + * Arguments: - uint64_t *state: pointer to input/output Keccak state + **************************************************/ +static void KeccakF1600_StatePermute(uint64_t *state) { + int round; + + uint64_t Aba, Abe, Abi, Abo, Abu; + uint64_t Aga, Age, Agi, Ago, Agu; + uint64_t Aka, Ake, Aki, Ako, Aku; + uint64_t Ama, Ame, Ami, Amo, Amu; + uint64_t Asa, Ase, Asi, Aso, Asu; + uint64_t BCa, BCe, BCi, BCo, BCu; + uint64_t Da, De, Di, Do, Du; + uint64_t Eba, Ebe, Ebi, Ebo, Ebu; + uint64_t Ega, Ege, Egi, Ego, Egu; + uint64_t Eka, Eke, Eki, Eko, Eku; + uint64_t Ema, Eme, Emi, Emo, Emu; + uint64_t Esa, Ese, Esi, Eso, Esu; + + // copyFromState(A, state) + Aba = state[0]; + Abe = state[1]; + Abi = state[2]; + Abo = state[3]; + Abu = state[4]; + Aga = state[5]; + Age = state[6]; + Agi = state[7]; + Ago = state[8]; + Agu = state[9]; + Aka = state[10]; + Ake = state[11]; + Aki = state[12]; + Ako = state[13]; + Aku = state[14]; + Ama = state[15]; + Ame = state[16]; + Ami = state[17]; + Amo = state[18]; + Amu = state[19]; + Asa = state[20]; + Ase = state[21]; + Asi = state[22]; + Aso = state[23]; + Asu = state[24]; + + for (round = 0; round < NROUNDS; round += 2) { + // prepareTheta + BCa = Aba ^ Aga ^ Aka ^ Ama ^ Asa; + BCe = Abe ^ Age ^ Ake ^ Ame ^ Ase; + BCi = Abi ^ Agi ^ Aki ^ Ami ^ Asi; + BCo = Abo ^ Ago ^ Ako ^ Amo ^ Aso; + BCu = Abu ^ Agu ^ Aku ^ Amu ^ Asu; + + // thetaRhoPiChiIotaPrepareTheta(round , A, E) + Da = BCu ^ ROL(BCe, 1); + De = BCa ^ ROL(BCi, 1); + Di = BCe ^ ROL(BCo, 1); + Do = BCi ^ ROL(BCu, 1); + Du = BCo ^ ROL(BCa, 1); + + Aba ^= Da; + BCa = Aba; + Age ^= De; + BCe = ROL(Age, 44); + Aki ^= Di; + BCi = ROL(Aki, 43); + Amo ^= Do; + BCo = ROL(Amo, 21); + Asu ^= Du; + BCu = ROL(Asu, 14); + Eba = BCa ^ ((~BCe) & BCi); + Eba ^= KeccakF_RoundConstants[round]; + Ebe = BCe ^ ((~BCi) & BCo); + Ebi = BCi ^ ((~BCo) & BCu); + Ebo = BCo ^ ((~BCu) & BCa); + Ebu = BCu ^ ((~BCa) & BCe); + + Abo ^= Do; + BCa = ROL(Abo, 28); + Agu ^= Du; + BCe = ROL(Agu, 20); + Aka ^= Da; + BCi = ROL(Aka, 3); + Ame ^= De; + BCo = ROL(Ame, 45); + Asi ^= Di; + BCu = ROL(Asi, 61); + Ega = BCa ^ ((~BCe) & BCi); + Ege = BCe ^ ((~BCi) & BCo); + Egi = BCi ^ ((~BCo) & BCu); + Ego = BCo ^ ((~BCu) & BCa); + Egu = BCu ^ ((~BCa) & BCe); + + Abe ^= De; + BCa = ROL(Abe, 1); + Agi ^= Di; + BCe = ROL(Agi, 6); + Ako ^= Do; + BCi = ROL(Ako, 25); + Amu ^= Du; + BCo = ROL(Amu, 8); + Asa ^= Da; + BCu = ROL(Asa, 18); + Eka = BCa ^ ((~BCe) & BCi); + Eke = BCe ^ ((~BCi) & BCo); + Eki = BCi ^ ((~BCo) & BCu); + Eko = BCo ^ ((~BCu) & BCa); + Eku = BCu ^ ((~BCa) & BCe); + + Abu ^= Du; + BCa = ROL(Abu, 27); + Aga ^= Da; + BCe = ROL(Aga, 36); + Ake ^= De; + BCi = ROL(Ake, 10); + Ami ^= Di; + BCo = ROL(Ami, 15); + Aso ^= Do; + BCu = ROL(Aso, 56); + Ema = BCa ^ ((~BCe) & BCi); + Eme = BCe ^ ((~BCi) & BCo); + Emi = BCi ^ ((~BCo) & BCu); + Emo = BCo ^ ((~BCu) & BCa); + Emu = BCu ^ ((~BCa) & BCe); + + Abi ^= Di; + BCa = ROL(Abi, 62); + Ago ^= Do; + BCe = ROL(Ago, 55); + Aku ^= Du; + BCi = ROL(Aku, 39); + Ama ^= Da; + BCo = ROL(Ama, 41); + Ase ^= De; + BCu = ROL(Ase, 2); + Esa = BCa ^ ((~BCe) & BCi); + Ese = BCe ^ ((~BCi) & BCo); + Esi = BCi ^ ((~BCo) & BCu); + Eso = BCo ^ ((~BCu) & BCa); + Esu = BCu ^ ((~BCa) & BCe); + + // prepareTheta + BCa = Eba ^ Ega ^ Eka ^ Ema ^ Esa; + BCe = Ebe ^ Ege ^ Eke ^ Eme ^ Ese; + BCi = Ebi ^ Egi ^ Eki ^ Emi ^ Esi; + BCo = Ebo ^ Ego ^ Eko ^ Emo ^ Eso; + BCu = Ebu ^ Egu ^ Eku ^ Emu ^ Esu; + + // thetaRhoPiChiIotaPrepareTheta(round+1, E, A) + Da = BCu ^ ROL(BCe, 1); + De = BCa ^ ROL(BCi, 1); + Di = BCe ^ ROL(BCo, 1); + Do = BCi ^ ROL(BCu, 1); + Du = BCo ^ ROL(BCa, 1); + + Eba ^= Da; + BCa = Eba; + Ege ^= De; + BCe = ROL(Ege, 44); + Eki ^= Di; + BCi = ROL(Eki, 43); + Emo ^= Do; + BCo = ROL(Emo, 21); + Esu ^= Du; + BCu = ROL(Esu, 14); + Aba = BCa ^ ((~BCe) & BCi); + Aba ^= KeccakF_RoundConstants[round + 1]; + Abe = BCe ^ ((~BCi) & BCo); + Abi = BCi ^ ((~BCo) & BCu); + Abo = BCo ^ ((~BCu) & BCa); + Abu = BCu ^ ((~BCa) & BCe); + + Ebo ^= Do; + BCa = ROL(Ebo, 28); + Egu ^= Du; + BCe = ROL(Egu, 20); + Eka ^= Da; + BCi = ROL(Eka, 3); + Eme ^= De; + BCo = ROL(Eme, 45); + Esi ^= Di; + BCu = ROL(Esi, 61); + Aga = BCa ^ ((~BCe) & BCi); + Age = BCe ^ ((~BCi) & BCo); + Agi = BCi ^ ((~BCo) & BCu); + Ago = BCo ^ ((~BCu) & BCa); + Agu = BCu ^ ((~BCa) & BCe); + + Ebe ^= De; + BCa = ROL(Ebe, 1); + Egi ^= Di; + BCe = ROL(Egi, 6); + Eko ^= Do; + BCi = ROL(Eko, 25); + Emu ^= Du; + BCo = ROL(Emu, 8); + Esa ^= Da; + BCu = ROL(Esa, 18); + Aka = BCa ^ ((~BCe) & BCi); + Ake = BCe ^ ((~BCi) & BCo); + Aki = BCi ^ ((~BCo) & BCu); + Ako = BCo ^ ((~BCu) & BCa); + Aku = BCu ^ ((~BCa) & BCe); + + Ebu ^= Du; + BCa = ROL(Ebu, 27); + Ega ^= Da; + BCe = ROL(Ega, 36); + Eke ^= De; + BCi = ROL(Eke, 10); + Emi ^= Di; + BCo = ROL(Emi, 15); + Eso ^= Do; + BCu = ROL(Eso, 56); + Ama = BCa ^ ((~BCe) & BCi); + Ame = BCe ^ ((~BCi) & BCo); + Ami = BCi ^ ((~BCo) & BCu); + Amo = BCo ^ ((~BCu) & BCa); + Amu = BCu ^ ((~BCa) & BCe); + + Ebi ^= Di; + BCa = ROL(Ebi, 62); + Ego ^= Do; + BCe = ROL(Ego, 55); + Eku ^= Du; + BCi = ROL(Eku, 39); + Ema ^= Da; + BCo = ROL(Ema, 41); + Ese ^= De; + BCu = ROL(Ese, 2); + Asa = BCa ^ ((~BCe) & BCi); + Ase = BCe ^ ((~BCi) & BCo); + Asi = BCi ^ ((~BCo) & BCu); + Aso = BCo ^ ((~BCu) & BCa); + Asu = BCu ^ ((~BCa) & BCe); + } + + // copyToState(state, A) + state[0] = Aba; + state[1] = Abe; + state[2] = Abi; + state[3] = Abo; + state[4] = Abu; + state[5] = Aga; + state[6] = Age; + state[7] = Agi; + state[8] = Ago; + state[9] = Agu; + state[10] = Aka; + state[11] = Ake; + state[12] = Aki; + state[13] = Ako; + state[14] = Aku; + state[15] = Ama; + state[16] = Ame; + state[17] = Ami; + state[18] = Amo; + state[19] = Amu; + state[20] = Asa; + state[21] = Ase; + state[22] = Asi; + state[23] = Aso; + state[24] = Asu; +} + +/************************************************* + * Name: keccak_absorb + * + * Description: Absorb step of Keccak; + * non-incremental, starts by zeroeing the state. + * + * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + * - const uint8_t *m: pointer to input to be absorbed into s + * - size_t mlen: length of input in bytes + * - uint8_t p: domain-separation byte for different + * Keccak-derived functions + **************************************************/ +static void keccak_absorb(uint64_t *s, uint32_t r, const uint8_t *m, + size_t mlen, uint8_t p) { + size_t i; + uint8_t t[200]; + + /* Zero state */ + for (i = 0; i < 25; ++i) { + s[i] = 0; + } + + while (mlen >= r) { + for (i = 0; i < r / 8; ++i) { + s[i] ^= load64(m + 8 * i); + } + + KeccakF1600_StatePermute(s); + mlen -= r; + m += r; + } + + for (i = 0; i < r; ++i) { + t[i] = 0; + } + for (i = 0; i < mlen; ++i) { + t[i] = m[i]; + } + t[i] = p; + t[r - 1] |= 128; + for (i = 0; i < r / 8; ++i) { + s[i] ^= load64(t + 8 * i); + } +} + +/************************************************* + * Name: keccak_squeezeblocks + * + * Description: Squeeze step of Keccak. Squeezes full blocks of r bytes each. + * Modifies the state. Can be called multiple times to keep + * squeezing, i.e., is incremental. + * + * Arguments: - uint8_t *h: pointer to output blocks + * - size_t nblocks: number of blocks to be + * squeezed (written to h) + * - uint64_t *s: pointer to input/output Keccak state + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + **************************************************/ +static void keccak_squeezeblocks(uint8_t *h, size_t nblocks, + uint64_t *s, uint32_t r) { + while (nblocks > 0) { + KeccakF1600_StatePermute(s); + for (size_t i = 0; i < (r >> 3); i++) { + store64(h + 8 * i, s[i]); + } + h += r; + nblocks--; + } +} + +/************************************************* + * Name: keccak_inc_init + * + * Description: Initializes the incremental Keccak state to zero. + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + **************************************************/ +static void keccak_inc_init(uint64_t *s_inc) { + size_t i; + + for (i = 0; i < 25; ++i) { + s_inc[i] = 0; + } + s_inc[25] = 0; +} + +/************************************************* + * Name: keccak_inc_absorb + * + * Description: Incremental keccak absorb + * Preceded by keccak_inc_init, succeeded by keccak_inc_finalize + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + * - const uint8_t *m: pointer to input to be absorbed into s + * - size_t mlen: length of input in bytes + **************************************************/ +static void keccak_inc_absorb(uint64_t *s_inc, uint32_t r, const uint8_t *m, + size_t mlen) { + size_t i; + + /* Recall that s_inc[25] is the non-absorbed bytes xored into the state */ + while (mlen + s_inc[25] >= r) { + for (i = 0; i < r - s_inc[25]; i++) { + /* Take the i'th byte from message + xor with the s_inc[25] + i'th byte of the state; little-endian */ + s_inc[(s_inc[25] + i) >> 3] ^= (uint64_t)m[i] << (8 * ((s_inc[25] + i) & 0x07)); + } + mlen -= (size_t)(r - s_inc[25]); + m += r - s_inc[25]; + s_inc[25] = 0; + + KeccakF1600_StatePermute(s_inc); + } + + for (i = 0; i < mlen; i++) { + s_inc[(s_inc[25] + i) >> 3] ^= (uint64_t)m[i] << (8 * ((s_inc[25] + i) & 0x07)); + } + s_inc[25] += mlen; +} + +/************************************************* + * Name: keccak_inc_finalize + * + * Description: Finalizes Keccak absorb phase, prepares for squeezing + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + * - uint8_t p: domain-separation byte for different + * Keccak-derived functions + **************************************************/ +static void keccak_inc_finalize(uint64_t *s_inc, uint32_t r, uint8_t p) { + /* After keccak_inc_absorb, we are guaranteed that s_inc[25] < r, + so we can always use one more byte for p in the current state. */ + s_inc[s_inc[25] >> 3] ^= (uint64_t)p << (8 * (s_inc[25] & 0x07)); + s_inc[(r - 1) >> 3] ^= (uint64_t)128 << (8 * ((r - 1) & 0x07)); + s_inc[25] = 0; +} + +/************************************************* + * Name: keccak_inc_squeeze + * + * Description: Incremental Keccak squeeze; can be called on byte-level + * + * Arguments: - uint8_t *h: pointer to output bytes + * - size_t outlen: number of bytes to be squeezed + * - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + **************************************************/ +static void keccak_inc_squeeze(uint8_t *h, size_t outlen, + uint64_t *s_inc, uint32_t r) { + size_t i; + + /* First consume any bytes we still have sitting around */ + for (i = 0; i < outlen && i < s_inc[25]; i++) { + /* There are s_inc[25] bytes left, so r - s_inc[25] is the first + available byte. We consume from there, i.e., up to r. */ + h[i] = (uint8_t)(s_inc[(r - s_inc[25] + i) >> 3] >> (8 * ((r - s_inc[25] + i) & 0x07))); + } + h += i; + outlen -= i; + s_inc[25] -= i; + + /* Then squeeze the remaining necessary blocks */ + while (outlen > 0) { + KeccakF1600_StatePermute(s_inc); + + for (i = 0; i < outlen && i < r; i++) { + h[i] = (uint8_t)(s_inc[i >> 3] >> (8 * (i & 0x07))); + } + h += i; + outlen -= i; + s_inc[25] = r - i; + } +} + +void shake256_inc_init(uint64_t *s_inc) { + keccak_inc_init(s_inc); +} + +void shake256_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen) { + keccak_inc_absorb(s_inc, SHAKE256_RATE, input, inlen); +} + +void shake256_inc_finalize(uint64_t *s_inc) { + keccak_inc_finalize(s_inc, SHAKE256_RATE, 0x1F); +} + +void shake256_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc) { + keccak_inc_squeeze(output, outlen, s_inc, SHAKE256_RATE); +} + +/************************************************* + * Name: shake256_absorb + * + * Description: Absorb step of the SHAKE256 XOF. + * non-incremental, starts by zeroeing the state. + * + * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state + * - const uint8_t *input: pointer to input to be absorbed + * into s + * - size_t inlen: length of input in bytes + **************************************************/ +void shake256_absorb(uint64_t *s, const uint8_t *input, size_t inlen) { + keccak_absorb(s, SHAKE256_RATE, input, inlen, 0x1F); +} + +/************************************************* + * Name: shake256_squeezeblocks + * + * Description: Squeeze step of SHAKE256 XOF. Squeezes full blocks of + * SHAKE256_RATE bytes each. Modifies the state. Can be called + * multiple times to keep squeezing, i.e., is incremental. + * + * Arguments: - uint8_t *output: pointer to output blocks + * - size_t nblocks: number of blocks to be squeezed + * (written to output) + * - uint64_t *s: pointer to input/output Keccak state + **************************************************/ +void shake256_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s) { + keccak_squeezeblocks(output, nblocks, s, SHAKE256_RATE); +} + +/************************************************* + * Name: shake256 + * + * Description: SHAKE256 XOF with non-incremental API + * + * Arguments: - uint8_t *output: pointer to output + * - size_t outlen: requested output length in bytes + * - const uint8_t *input: pointer to input + * - size_t inlen: length of input in bytes + **************************************************/ +void shake256(uint8_t *output, size_t outlen, + const uint8_t *input, size_t inlen) { + size_t nblocks = outlen / SHAKE256_RATE; + uint8_t t[SHAKE256_RATE]; + uint64_t s[25]; + + shake256_absorb(s, input, inlen); + shake256_squeezeblocks(output, nblocks, s); + + output += nblocks * SHAKE256_RATE; + outlen -= nblocks * SHAKE256_RATE; + + if (outlen) { + shake256_squeezeblocks(t, 1, s); + for (size_t i = 0; i < outlen; ++i) { + output[i] = t[i]; + } + } +} diff --git a/src/crypto/pq/sphincsplus/fips202.h b/src/crypto/pq/sphincsplus/fips202.h new file mode 100644 index 0000000..e11cb7f --- /dev/null +++ b/src/crypto/pq/sphincsplus/fips202.h @@ -0,0 +1,47 @@ +#ifndef SPX_FIPS202_H +#define SPX_FIPS202_H + +#include +#include + +#define SHAKE128_RATE 168 +#define SHAKE256_RATE 136 +#define SHA3_256_RATE 136 +#define SHA3_512_RATE 72 + +void shake128_absorb(uint64_t *s, const uint8_t *input, size_t inlen); + +void shake128_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s); + +void shake128_inc_init(uint64_t *s_inc); +void shake128_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void shake128_inc_finalize(uint64_t *s_inc); +void shake128_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc); + +void shake256_absorb(uint64_t *s, const uint8_t *input, size_t inlen); +void shake256_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s); + +void shake256_inc_init(uint64_t *s_inc); +void shake256_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void shake256_inc_finalize(uint64_t *s_inc); +void shake256_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc); + +void shake128(uint8_t *output, size_t outlen, + const uint8_t *input, size_t inlen); + +void shake256(uint8_t *output, size_t outlen, + const uint8_t *input, size_t inlen); + +void sha3_256_inc_init(uint64_t *s_inc); +void sha3_256_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void sha3_256_inc_finalize(uint8_t *output, uint64_t *s_inc); + +void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen); + +void sha3_512_inc_init(uint64_t *s_inc); +void sha3_512_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void sha3_512_inc_finalize(uint8_t *output, uint64_t *s_inc); + +void sha3_512(uint8_t *output, const uint8_t *input, size_t inlen); + +#endif diff --git a/src/crypto/pq/sphincsplus/fors.c b/src/crypto/pq/sphincsplus/fors.c new file mode 100644 index 0000000..e6aa4b4 --- /dev/null +++ b/src/crypto/pq/sphincsplus/fors.c @@ -0,0 +1,161 @@ +#include +#include +#include + +#include "fors.h" +#include "utils.h" +#include "utilsx1.h" +#include "hash.h" +#include "thash.h" +#include "address.h" + +static void fors_gen_sk(unsigned char *sk, const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + prf_addr(sk, ctx, fors_leaf_addr); +} + +static void fors_sk_to_leaf(unsigned char *leaf, const unsigned char *sk, + const spx_ctx *ctx, + uint32_t fors_leaf_addr[8]) +{ + thash(leaf, sk, 1, ctx, fors_leaf_addr); +} + +struct fors_gen_leaf_info { + uint32_t leaf_addrx[8]; +}; + +static void fors_gen_leafx1(unsigned char *leaf, + const spx_ctx *ctx, + uint32_t addr_idx, void *info) +{ + struct fors_gen_leaf_info *fors_info = info; + uint32_t *fors_leaf_addr = fors_info->leaf_addrx; + + /* Only set the parts that the caller doesn't set */ + set_tree_index(fors_leaf_addr, addr_idx); + set_type(fors_leaf_addr, SPX_ADDR_TYPE_FORSPRF); + fors_gen_sk(leaf, ctx, fors_leaf_addr); + + set_type(fors_leaf_addr, SPX_ADDR_TYPE_FORSTREE); + fors_sk_to_leaf(leaf, leaf, + ctx, fors_leaf_addr); +} + +/** + * Interprets m as SPX_FORS_HEIGHT-bit unsigned integers. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + * Assumes indices has space for SPX_FORS_TREES integers. + */ +static void message_to_indices(uint32_t *indices, const unsigned char *m) +{ + unsigned int i, j; + unsigned int offset = 0; + + for (i = 0; i < SPX_FORS_TREES; i++) { + indices[i] = 0; + for (j = 0; j < SPX_FORS_HEIGHT; j++) { + indices[i] ^= ((m[offset >> 3] >> (offset & 0x7)) & 1u) << j; + offset++; + } + } +} + +/** + * Signs a message m, deriving the secret key from sk_seed and the FTS address. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_sign(unsigned char *sig, unsigned char *pk, + const unsigned char *m, + const spx_ctx *ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + uint32_t fors_tree_addr[8] = {0}; + struct fors_gen_leaf_info fors_info = {0}; + uint32_t *fors_leaf_addr = fors_info.leaf_addrx; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + copy_keypair_addr(fors_tree_addr, fors_addr); + copy_keypair_addr(fors_leaf_addr, fors_addr); + + copy_keypair_addr(fors_pk_addr, fors_addr); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSPRF); + + /* Include the secret key part that produces the selected leaf node. */ + fors_gen_sk(sig, ctx, fors_tree_addr); + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + sig += SPX_N; + + /* Compute the authentication path for this leaf node. */ + treehashx1(roots + i*SPX_N, sig, ctx, + indices[i], idx_offset, SPX_FORS_HEIGHT, fors_gen_leafx1, + fors_tree_addr, &fors_info); + + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} + +/** + * Derives the FORS public key from a signature. + * This can be used for verification by comparing to a known public key, or to + * subsequently verify a signature on the derived public key. The latter is the + * typical use-case when used as an FTS below an OTS in a hypertree. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +void fors_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *m, + const spx_ctx* ctx, + const uint32_t fors_addr[8]) +{ + uint32_t indices[SPX_FORS_TREES]; + unsigned char roots[SPX_FORS_TREES * SPX_N]; + unsigned char leaf[SPX_N]; + uint32_t fors_tree_addr[8] = {0}; + uint32_t fors_pk_addr[8] = {0}; + uint32_t idx_offset; + unsigned int i; + + copy_keypair_addr(fors_tree_addr, fors_addr); + copy_keypair_addr(fors_pk_addr, fors_addr); + + set_type(fors_tree_addr, SPX_ADDR_TYPE_FORSTREE); + set_type(fors_pk_addr, SPX_ADDR_TYPE_FORSPK); + + message_to_indices(indices, m); + + for (i = 0; i < SPX_FORS_TREES; i++) { + idx_offset = i * (1 << SPX_FORS_HEIGHT); + + set_tree_height(fors_tree_addr, 0); + set_tree_index(fors_tree_addr, indices[i] + idx_offset); + + /* Derive the leaf from the included secret key part. */ + fors_sk_to_leaf(leaf, sig, ctx, fors_tree_addr); + sig += SPX_N; + + /* Derive the corresponding root node of this tree. */ + compute_root(roots + i*SPX_N, leaf, indices[i], idx_offset, + sig, SPX_FORS_HEIGHT, ctx, fors_tree_addr); + sig += SPX_N * SPX_FORS_HEIGHT; + } + + /* Hash horizontally across all tree roots to derive the public key. */ + thash(pk, roots, SPX_FORS_TREES, ctx, fors_pk_addr); +} diff --git a/src/crypto/pq/sphincsplus/fors.h b/src/crypto/pq/sphincsplus/fors.h new file mode 100644 index 0000000..8d98017 --- /dev/null +++ b/src/crypto/pq/sphincsplus/fors.h @@ -0,0 +1,32 @@ +#ifndef SPX_FORS_H +#define SPX_FORS_H + +#include + +#include "params.h" +#include "context.h" + +/** + * Signs a message m, deriving the secret key from sk_seed and the FTS address. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +#define fors_sign SPX_NAMESPACE(fors_sign) +void fors_sign(unsigned char *sig, unsigned char *pk, + const unsigned char *m, + const spx_ctx* ctx, + const uint32_t fors_addr[8]); + +/** + * Derives the FORS public key from a signature. + * This can be used for verification by comparing to a known public key, or to + * subsequently verify a signature on the derived public key. The latter is the + * typical use-case when used as an FTS below an OTS in a hypertree. + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. + */ +#define fors_pk_from_sig SPX_NAMESPACE(fors_pk_from_sig) +void fors_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *m, + const spx_ctx* ctx, + const uint32_t fors_addr[8]); + +#endif diff --git a/src/crypto/pq/sphincsplus/hash.h b/src/crypto/pq/sphincsplus/hash.h new file mode 100644 index 0000000..b141f09 --- /dev/null +++ b/src/crypto/pq/sphincsplus/hash.h @@ -0,0 +1,27 @@ +#ifndef SPX_HASH_H +#define SPX_HASH_H + +#include +#include "context.h" +#include "params.h" + +#define initialize_hash_function SPX_NAMESPACE(initialize_hash_function) +void initialize_hash_function(spx_ctx *ctx); + +#define prf_addr SPX_NAMESPACE(prf_addr) +void prf_addr(unsigned char *out, const spx_ctx *ctx, + const uint32_t addr[8]); + +#define gen_message_random SPX_NAMESPACE(gen_message_random) +void gen_message_random(unsigned char *R, const unsigned char *sk_prf, + const unsigned char *optrand, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx); + +#define hash_message SPX_NAMESPACE(hash_message) +void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx, + const unsigned char *R, const unsigned char *pk, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx); + +#endif diff --git a/src/crypto/pq/sphincsplus/hash_sha2.c b/src/crypto/pq/sphincsplus/hash_sha2.c new file mode 100644 index 0000000..67098e6 --- /dev/null +++ b/src/crypto/pq/sphincsplus/hash_sha2.c @@ -0,0 +1,197 @@ +#include +#include + +#include "address.h" +#include "utils.h" +#include "params.h" +#include "hash.h" +#include "sha2.h" + +#if SPX_N >= 24 +#define SPX_SHAX_OUTPUT_BYTES SPX_SHA512_OUTPUT_BYTES +#define SPX_SHAX_BLOCK_BYTES SPX_SHA512_BLOCK_BYTES +#define shaX_inc_init sha512_inc_init +#define shaX_inc_blocks sha512_inc_blocks +#define shaX_inc_finalize sha512_inc_finalize +#define shaX sha512 +#define mgf1_X mgf1_512 +#else +#define SPX_SHAX_OUTPUT_BYTES SPX_SHA256_OUTPUT_BYTES +#define SPX_SHAX_BLOCK_BYTES SPX_SHA256_BLOCK_BYTES +#define shaX_inc_init sha256_inc_init +#define shaX_inc_blocks sha256_inc_blocks +#define shaX_inc_finalize sha256_inc_finalize +#define shaX sha256 +#define mgf1_X mgf1_256 +#endif + + +/* For SHA, there is no immediate reason to initialize at the start, + so this function is an empty operation. */ +void initialize_hash_function(spx_ctx *ctx) +{ + seed_state(ctx); +} + +/* + * Computes PRF(pk_seed, sk_seed, addr). + */ +void prf_addr(unsigned char *out, const spx_ctx *ctx, + const uint32_t addr[8]) +{ + uint8_t sha2_state[40]; + unsigned char buf[SPX_SHA256_ADDR_BYTES + SPX_N]; + unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES]; + + /* Retrieve precomputed state containing pub_seed */ + memcpy(sha2_state, ctx->state_seeded, 40 * sizeof(uint8_t)); + + /* Remainder: ADDR^c ‖ SK.seed */ + memcpy(buf, addr, SPX_SHA256_ADDR_BYTES); + memcpy(buf + SPX_SHA256_ADDR_BYTES, ctx->sk_seed, SPX_N); + + sha256_inc_finalize(outbuf, sha2_state, buf, SPX_SHA256_ADDR_BYTES + SPX_N); + + memcpy(out, outbuf, SPX_N); +} + +/** + * Computes the message-dependent randomness R, using a secret seed as a key + * for HMAC, and an optional randomization value prefixed to the message. + * This requires m to have at least SPX_SHAX_BLOCK_BYTES + SPX_N space + * available in front of the pointer, i.e. before the message to use for the + * prefix. This is necessary to prevent having to move the message around (and + * allocate memory for it). + */ +void gen_message_random(unsigned char *R, const unsigned char *sk_prf, + const unsigned char *optrand, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx) +{ + (void)ctx; + + unsigned char buf[SPX_SHAX_BLOCK_BYTES + SPX_SHAX_OUTPUT_BYTES]; + uint8_t state[8 + SPX_SHAX_OUTPUT_BYTES]; + int i; + +#if SPX_N > SPX_SHAX_BLOCK_BYTES + #error "Currently only supports SPX_N of at most SPX_SHAX_BLOCK_BYTES" +#endif + + /* This implements HMAC-SHA */ + for (i = 0; i < SPX_N; i++) { + buf[i] = 0x36 ^ sk_prf[i]; + } + memset(buf + SPX_N, 0x36, SPX_SHAX_BLOCK_BYTES - SPX_N); + + shaX_inc_init(state); + shaX_inc_blocks(state, buf, 1); + + memcpy(buf, optrand, SPX_N); + + /* If optrand + message cannot fill up an entire block */ + if (SPX_N + mlen < SPX_SHAX_BLOCK_BYTES) { + memcpy(buf + SPX_N, m, mlen); + shaX_inc_finalize(buf + SPX_SHAX_BLOCK_BYTES, state, + buf, mlen + SPX_N); + } + /* Otherwise first fill a block, so that finalize only uses the message */ + else { + memcpy(buf + SPX_N, m, SPX_SHAX_BLOCK_BYTES - SPX_N); + shaX_inc_blocks(state, buf, 1); + + m += SPX_SHAX_BLOCK_BYTES - SPX_N; + mlen -= SPX_SHAX_BLOCK_BYTES - SPX_N; + shaX_inc_finalize(buf + SPX_SHAX_BLOCK_BYTES, state, m, mlen); + } + + for (i = 0; i < SPX_N; i++) { + buf[i] = 0x5c ^ sk_prf[i]; + } + memset(buf + SPX_N, 0x5c, SPX_SHAX_BLOCK_BYTES - SPX_N); + + shaX(buf, buf, SPX_SHAX_BLOCK_BYTES + SPX_SHAX_OUTPUT_BYTES); + memcpy(R, buf, SPX_N); +} + +/** + * Computes the message hash using R, the public key, and the message. + * Outputs the message digest and the index of the leaf. The index is split in + * the tree index and the leaf index, for convenient copying to an address. + */ +void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx, + const unsigned char *R, const unsigned char *pk, + const unsigned char *m, unsigned long long mlen, + const spx_ctx *ctx) +{ + (void)ctx; +#define SPX_TREE_BITS (SPX_TREE_HEIGHT * (SPX_D - 1)) +#define SPX_TREE_BYTES ((SPX_TREE_BITS + 7) / 8) +#define SPX_LEAF_BITS SPX_TREE_HEIGHT +#define SPX_LEAF_BYTES ((SPX_LEAF_BITS + 7) / 8) +#define SPX_DGST_BYTES (SPX_FORS_MSG_BYTES + SPX_TREE_BYTES + SPX_LEAF_BYTES) + + unsigned char seed[2*SPX_N + SPX_SHAX_OUTPUT_BYTES]; + + /* Round to nearest multiple of SPX_SHAX_BLOCK_BYTES */ +#if (SPX_SHAX_BLOCK_BYTES & (SPX_SHAX_BLOCK_BYTES-1)) != 0 + #error "Assumes that SPX_SHAX_BLOCK_BYTES is a power of 2" +#endif +#define SPX_INBLOCKS (((SPX_N + SPX_PK_BYTES + SPX_SHAX_BLOCK_BYTES - 1) & \ + -SPX_SHAX_BLOCK_BYTES) / SPX_SHAX_BLOCK_BYTES) + unsigned char inbuf[SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES]; + + unsigned char buf[SPX_DGST_BYTES]; + unsigned char *bufp = buf; + uint8_t state[8 + SPX_SHAX_OUTPUT_BYTES]; + + shaX_inc_init(state); + + // seed: SHA-X(R ‖ PK.seed ‖ PK.root ‖ M) + memcpy(inbuf, R, SPX_N); + memcpy(inbuf + SPX_N, pk, SPX_PK_BYTES); + + /* If R + pk + message cannot fill up an entire block */ + if (SPX_N + SPX_PK_BYTES + mlen < SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES) { + memcpy(inbuf + SPX_N + SPX_PK_BYTES, m, mlen); + shaX_inc_finalize(seed + 2*SPX_N, state, inbuf, SPX_N + SPX_PK_BYTES + mlen); + } + /* Otherwise first fill a block, so that finalize only uses the message */ + else { + memcpy(inbuf + SPX_N + SPX_PK_BYTES, m, + SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES - SPX_N - SPX_PK_BYTES); + shaX_inc_blocks(state, inbuf, SPX_INBLOCKS); + + m += SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES - SPX_N - SPX_PK_BYTES; + mlen -= SPX_INBLOCKS * SPX_SHAX_BLOCK_BYTES - SPX_N - SPX_PK_BYTES; + shaX_inc_finalize(seed + 2*SPX_N, state, m, mlen); + } + + // H_msg: MGF1-SHA-X(R ‖ PK.seed ‖ seed) + memcpy(seed, R, SPX_N); + memcpy(seed + SPX_N, pk, SPX_N); + + /* By doing this in two steps, we prevent hashing the message twice; + otherwise each iteration in MGF1 would hash the message again. */ + mgf1_X(bufp, SPX_DGST_BYTES, seed, 2*SPX_N + SPX_SHAX_OUTPUT_BYTES); + + memcpy(digest, bufp, SPX_FORS_MSG_BYTES); + bufp += SPX_FORS_MSG_BYTES; + +#if SPX_TREE_BITS > 64 + #error For given height and depth, 64 bits cannot represent all subtrees +#endif + + if (SPX_D == 1) { + *tree = 0; + } else { + *tree = bytes_to_ull(bufp, SPX_TREE_BYTES); + *tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS); + } + bufp += SPX_TREE_BYTES; + + *leaf_idx = (uint32_t)bytes_to_ull(bufp, SPX_LEAF_BYTES); + *leaf_idx &= (~(uint32_t)0) >> (32 - SPX_LEAF_BITS); +} + + diff --git a/src/crypto/pq/sphincsplus/merkle.c b/src/crypto/pq/sphincsplus/merkle.c new file mode 100644 index 0000000..414f468 --- /dev/null +++ b/src/crypto/pq/sphincsplus/merkle.c @@ -0,0 +1,61 @@ +#include +#include + +#include "utils.h" +#include "utilsx1.h" +#include "wots.h" +#include "wotsx1.h" +#include "merkle.h" +#include "address.h" +#include "params.h" + +/* + * This generates a Merkle signature (WOTS signature followed by the Merkle + * authentication path). This is in this file because most of the complexity + * is involved with the WOTS signature; the Merkle authentication path logic + * is mostly hidden in treehashx4 + */ +void merkle_sign(uint8_t *sig, unsigned char *root, + const spx_ctx *ctx, + uint32_t wots_addr[8], uint32_t tree_addr[8], + uint32_t idx_leaf) +{ + unsigned char *auth_path = sig + SPX_WOTS_BYTES; + struct leaf_info_x1 info = { 0 }; + unsigned steps[ SPX_WOTS_LEN ]; + + info.wots_sig = sig; + chain_lengths(steps, root); + info.wots_steps = steps; + + set_type(&tree_addr[0], SPX_ADDR_TYPE_HASHTREE); + set_type(&info.pk_addr[0], SPX_ADDR_TYPE_WOTSPK); + copy_subtree_addr(&info.leaf_addr[0], wots_addr); + copy_subtree_addr(&info.pk_addr[0], wots_addr); + + info.wots_sign_leaf = idx_leaf; + + treehashx1(root, auth_path, ctx, + idx_leaf, 0, + SPX_TREE_HEIGHT, + wots_gen_leafx1, + tree_addr, &info); +} + +/* Compute root node of the top-most subtree. */ +void merkle_gen_root(unsigned char *root, const spx_ctx *ctx) +{ + /* We do not need the auth path in key generation, but it simplifies the + code to have just one treehash routine that computes both root and path + in one function. */ + unsigned char auth_path[SPX_TREE_HEIGHT * SPX_N + SPX_WOTS_BYTES]; + uint32_t top_tree_addr[8] = {0}; + uint32_t wots_addr[8] = {0}; + + set_layer_addr(top_tree_addr, SPX_D - 1); + set_layer_addr(wots_addr, SPX_D - 1); + + merkle_sign(auth_path, root, ctx, + wots_addr, top_tree_addr, + (uint32_t)~0 /* ~0 means "don't bother generating an auth path */ ); +} diff --git a/src/crypto/pq/sphincsplus/merkle.h b/src/crypto/pq/sphincsplus/merkle.h new file mode 100644 index 0000000..9ac2759 --- /dev/null +++ b/src/crypto/pq/sphincsplus/merkle.h @@ -0,0 +1,18 @@ +#if !defined( MERKLE_H_ ) +#define MERKLE_H_ + +#include + +/* Generate a Merkle signature (WOTS signature followed by the Merkle */ +/* authentication path) */ +#define merkle_sign SPX_NAMESPACE(merkle_sign) +void merkle_sign(uint8_t *sig, unsigned char *root, + const spx_ctx* ctx, + uint32_t wots_addr[8], uint32_t tree_addr[8], + uint32_t idx_leaf); + +/* Compute the root node of the top-most subtree. */ +#define merkle_gen_root SPX_NAMESPACE(merkle_gen_root) +void merkle_gen_root(unsigned char *root, const spx_ctx* ctx); + +#endif /* MERKLE_H_ */ diff --git a/src/crypto/pq/sphincsplus/params.h b/src/crypto/pq/sphincsplus/params.h new file mode 100644 index 0000000..1d7f9c9 --- /dev/null +++ b/src/crypto/pq/sphincsplus/params.h @@ -0,0 +1,5 @@ +#define str(s) #s +#define xstr(s) str(s) + +#include xstr(params/params-PARAMS.h) + diff --git a/src/crypto/pq/sphincsplus/params/params-sphincs-sha2-128s.h b/src/crypto/pq/sphincsplus/params/params-sphincs-sha2-128s.h new file mode 100644 index 0000000..8ef8608 --- /dev/null +++ b/src/crypto/pq/sphincsplus/params/params-sphincs-sha2-128s.h @@ -0,0 +1,85 @@ +#ifndef SPX_PARAMS_H +#define SPX_PARAMS_H + +#define SPX_NAMESPACE(s) SPX_##s + +/* Hash output length in bytes. */ +#define SPX_N 16 +/* Height of the hypertree. */ +#define SPX_FULL_HEIGHT 63 +/* Number of subtree layer. */ +#define SPX_D 7 +/* FORS tree dimensions. */ +#define SPX_FORS_HEIGHT 12 +#define SPX_FORS_TREES 14 +/* Winternitz parameter, */ +#define SPX_WOTS_W 16 + +/* The hash function is defined by linking a different hash.c file, as opposed + to setting a #define constant. */ + +/* This is a SHA2-based parameter set, hence whether we use SHA-256 + * exclusively or we use both SHA-256 and SHA-512 is controlled by + * the following #define */ +#define SPX_SHA512 0 /* Use SHA-256 for all hashes */ + +/* For clarity */ +#define SPX_ADDR_BYTES 32 + +/* WOTS parameters. */ +#if SPX_WOTS_W == 256 + #define SPX_WOTS_LOGW 8 +#elif SPX_WOTS_W == 16 + #define SPX_WOTS_LOGW 4 +#else + #error SPX_WOTS_W assumed 16 or 256 +#endif + +#define SPX_WOTS_LEN1 (8 * SPX_N / SPX_WOTS_LOGW) + +/* SPX_WOTS_LEN2 is floor(log(len_1 * (w - 1)) / log(w)) + 1; we precompute */ +#if SPX_WOTS_W == 256 + #if SPX_N <= 1 + #define SPX_WOTS_LEN2 1 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 2 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#elif SPX_WOTS_W == 16 + #if SPX_N <= 8 + #define SPX_WOTS_LEN2 2 + #elif SPX_N <= 136 + #define SPX_WOTS_LEN2 3 + #elif SPX_N <= 256 + #define SPX_WOTS_LEN2 4 + #else + #error Did not precompute SPX_WOTS_LEN2 for n outside {2, .., 256} + #endif +#endif + +#define SPX_WOTS_LEN (SPX_WOTS_LEN1 + SPX_WOTS_LEN2) +#define SPX_WOTS_BYTES (SPX_WOTS_LEN * SPX_N) +#define SPX_WOTS_PK_BYTES SPX_WOTS_BYTES + +/* Subtree size. */ +#define SPX_TREE_HEIGHT (SPX_FULL_HEIGHT / SPX_D) + +#if SPX_TREE_HEIGHT * SPX_D != SPX_FULL_HEIGHT + #error SPX_D should always divide SPX_FULL_HEIGHT +#endif + +/* FORS parameters. */ +#define SPX_FORS_MSG_BYTES ((SPX_FORS_HEIGHT * SPX_FORS_TREES + 7) / 8) +#define SPX_FORS_BYTES ((SPX_FORS_HEIGHT + 1) * SPX_FORS_TREES * SPX_N) +#define SPX_FORS_PK_BYTES SPX_N + +/* Resulting SPX sizes. */ +#define SPX_BYTES (SPX_N + SPX_FORS_BYTES + SPX_D * SPX_WOTS_BYTES +\ + SPX_FULL_HEIGHT * SPX_N) +#define SPX_PK_BYTES (2 * SPX_N) +#define SPX_SK_BYTES (2 * SPX_N + SPX_PK_BYTES) + +#include "../sha2_offsets.h" + +#endif diff --git a/src/crypto/pq/sphincsplus/randombytes.h b/src/crypto/pq/sphincsplus/randombytes.h new file mode 100644 index 0000000..3308bdc --- /dev/null +++ b/src/crypto/pq/sphincsplus/randombytes.h @@ -0,0 +1,15 @@ +#ifndef VEXTA_PQ_RANDOMBYTES_H +#define VEXTA_PQ_RANDOMBYTES_H +#include +#include +#ifdef __cplusplus +extern "C" { +#endif +/* VEXTA: unified prototype. Definition lives in pq_randombytes.cpp and is + backed by Bitcoin's CSPRNG (GetStrongRandBytes). The upstream test-only + randombytes.c files were deleted — they must never ship in a wallet. */ +void randombytes(uint8_t *out, size_t outlen); +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/crypto/pq/sphincsplus/sha2.c b/src/crypto/pq/sphincsplus/sha2.c new file mode 100644 index 0000000..ef73047 --- /dev/null +++ b/src/crypto/pq/sphincsplus/sha2.c @@ -0,0 +1,700 @@ +/* Based on the public domain implementation in + * crypto_hash/sha512/ref/ from http://bench.cr.yp.to/supercop.html + * by D. J. Bernstein */ + +#include +#include +#include + +#include "utils.h" +#include "sha2.h" + +static uint32_t load_bigendian_32(const uint8_t *x) { + return (uint32_t)(x[3]) | (((uint32_t)(x[2])) << 8) | + (((uint32_t)(x[1])) << 16) | (((uint32_t)(x[0])) << 24); +} + +static uint64_t load_bigendian_64(const uint8_t *x) { + return (uint64_t)(x[7]) | (((uint64_t)(x[6])) << 8) | + (((uint64_t)(x[5])) << 16) | (((uint64_t)(x[4])) << 24) | + (((uint64_t)(x[3])) << 32) | (((uint64_t)(x[2])) << 40) | + (((uint64_t)(x[1])) << 48) | (((uint64_t)(x[0])) << 56); +} + +static void store_bigendian_32(uint8_t *x, uint64_t u) { + x[3] = (uint8_t) u; + u >>= 8; + x[2] = (uint8_t) u; + u >>= 8; + x[1] = (uint8_t) u; + u >>= 8; + x[0] = (uint8_t) u; +} + +static void store_bigendian_64(uint8_t *x, uint64_t u) { + x[7] = (uint8_t) u; + u >>= 8; + x[6] = (uint8_t) u; + u >>= 8; + x[5] = (uint8_t) u; + u >>= 8; + x[4] = (uint8_t) u; + u >>= 8; + x[3] = (uint8_t) u; + u >>= 8; + x[2] = (uint8_t) u; + u >>= 8; + x[1] = (uint8_t) u; + u >>= 8; + x[0] = (uint8_t) u; +} + +#define SHR(x, c) ((x) >> (c)) +#define ROTR_32(x, c) (((x) >> (c)) | ((x) << (32 - (c)))) +#define ROTR_64(x,c) (((x) >> (c)) | ((x) << (64 - (c)))) + +#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z))) +#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) + +#define Sigma0_32(x) (ROTR_32(x, 2) ^ ROTR_32(x,13) ^ ROTR_32(x,22)) +#define Sigma1_32(x) (ROTR_32(x, 6) ^ ROTR_32(x,11) ^ ROTR_32(x,25)) +#define sigma0_32(x) (ROTR_32(x, 7) ^ ROTR_32(x,18) ^ SHR(x, 3)) +#define sigma1_32(x) (ROTR_32(x,17) ^ ROTR_32(x,19) ^ SHR(x,10)) + +#define Sigma0_64(x) (ROTR_64(x,28) ^ ROTR_64(x,34) ^ ROTR_64(x,39)) +#define Sigma1_64(x) (ROTR_64(x,14) ^ ROTR_64(x,18) ^ ROTR_64(x,41)) +#define sigma0_64(x) (ROTR_64(x, 1) ^ ROTR_64(x, 8) ^ SHR(x,7)) +#define sigma1_64(x) (ROTR_64(x,19) ^ ROTR_64(x,61) ^ SHR(x,6)) + +#define M_32(w0, w14, w9, w1) w0 = sigma1_32(w14) + (w9) + sigma0_32(w1) + (w0); +#define M_64(w0, w14, w9, w1) w0 = sigma1_64(w14) + (w9) + sigma0_64(w1) + (w0); + +#define EXPAND_32 \ + M_32(w0, w14, w9, w1) \ + M_32(w1, w15, w10, w2) \ + M_32(w2, w0, w11, w3) \ + M_32(w3, w1, w12, w4) \ + M_32(w4, w2, w13, w5) \ + M_32(w5, w3, w14, w6) \ + M_32(w6, w4, w15, w7) \ + M_32(w7, w5, w0, w8) \ + M_32(w8, w6, w1, w9) \ + M_32(w9, w7, w2, w10) \ + M_32(w10, w8, w3, w11) \ + M_32(w11, w9, w4, w12) \ + M_32(w12, w10, w5, w13) \ + M_32(w13, w11, w6, w14) \ + M_32(w14, w12, w7, w15) \ + M_32(w15, w13, w8, w0) + +#define EXPAND_64 \ + M_64(w0 ,w14,w9 ,w1 ) \ + M_64(w1 ,w15,w10,w2 ) \ + M_64(w2 ,w0 ,w11,w3 ) \ + M_64(w3 ,w1 ,w12,w4 ) \ + M_64(w4 ,w2 ,w13,w5 ) \ + M_64(w5 ,w3 ,w14,w6 ) \ + M_64(w6 ,w4 ,w15,w7 ) \ + M_64(w7 ,w5 ,w0 ,w8 ) \ + M_64(w8 ,w6 ,w1 ,w9 ) \ + M_64(w9 ,w7 ,w2 ,w10) \ + M_64(w10,w8 ,w3 ,w11) \ + M_64(w11,w9 ,w4 ,w12) \ + M_64(w12,w10,w5 ,w13) \ + M_64(w13,w11,w6 ,w14) \ + M_64(w14,w12,w7 ,w15) \ + M_64(w15,w13,w8 ,w0 ) + +#define F_32(w, k) \ + T1 = h + Sigma1_32(e) + Ch(e, f, g) + (k) + (w); \ + T2 = Sigma0_32(a) + Maj(a, b, c); \ + h = g; \ + g = f; \ + f = e; \ + e = d + T1; \ + d = c; \ + c = b; \ + b = a; \ + a = T1 + T2; + +#define F_64(w,k) \ + T1 = h + Sigma1_64(e) + Ch(e,f,g) + k + w; \ + T2 = Sigma0_64(a) + Maj(a,b,c); \ + h = g; \ + g = f; \ + f = e; \ + e = d + T1; \ + d = c; \ + c = b; \ + b = a; \ + a = T1 + T2; + +static size_t crypto_hashblocks_sha256(uint8_t *statebytes, + const uint8_t *in, size_t inlen) { + uint32_t state[8]; + uint32_t a; + uint32_t b; + uint32_t c; + uint32_t d; + uint32_t e; + uint32_t f; + uint32_t g; + uint32_t h; + uint32_t T1; + uint32_t T2; + + a = load_bigendian_32(statebytes + 0); + state[0] = a; + b = load_bigendian_32(statebytes + 4); + state[1] = b; + c = load_bigendian_32(statebytes + 8); + state[2] = c; + d = load_bigendian_32(statebytes + 12); + state[3] = d; + e = load_bigendian_32(statebytes + 16); + state[4] = e; + f = load_bigendian_32(statebytes + 20); + state[5] = f; + g = load_bigendian_32(statebytes + 24); + state[6] = g; + h = load_bigendian_32(statebytes + 28); + state[7] = h; + + while (inlen >= 64) { + uint32_t w0 = load_bigendian_32(in + 0); + uint32_t w1 = load_bigendian_32(in + 4); + uint32_t w2 = load_bigendian_32(in + 8); + uint32_t w3 = load_bigendian_32(in + 12); + uint32_t w4 = load_bigendian_32(in + 16); + uint32_t w5 = load_bigendian_32(in + 20); + uint32_t w6 = load_bigendian_32(in + 24); + uint32_t w7 = load_bigendian_32(in + 28); + uint32_t w8 = load_bigendian_32(in + 32); + uint32_t w9 = load_bigendian_32(in + 36); + uint32_t w10 = load_bigendian_32(in + 40); + uint32_t w11 = load_bigendian_32(in + 44); + uint32_t w12 = load_bigendian_32(in + 48); + uint32_t w13 = load_bigendian_32(in + 52); + uint32_t w14 = load_bigendian_32(in + 56); + uint32_t w15 = load_bigendian_32(in + 60); + + F_32(w0, 0x428a2f98) + F_32(w1, 0x71374491) + F_32(w2, 0xb5c0fbcf) + F_32(w3, 0xe9b5dba5) + F_32(w4, 0x3956c25b) + F_32(w5, 0x59f111f1) + F_32(w6, 0x923f82a4) + F_32(w7, 0xab1c5ed5) + F_32(w8, 0xd807aa98) + F_32(w9, 0x12835b01) + F_32(w10, 0x243185be) + F_32(w11, 0x550c7dc3) + F_32(w12, 0x72be5d74) + F_32(w13, 0x80deb1fe) + F_32(w14, 0x9bdc06a7) + F_32(w15, 0xc19bf174) + + EXPAND_32 + + F_32(w0, 0xe49b69c1) + F_32(w1, 0xefbe4786) + F_32(w2, 0x0fc19dc6) + F_32(w3, 0x240ca1cc) + F_32(w4, 0x2de92c6f) + F_32(w5, 0x4a7484aa) + F_32(w6, 0x5cb0a9dc) + F_32(w7, 0x76f988da) + F_32(w8, 0x983e5152) + F_32(w9, 0xa831c66d) + F_32(w10, 0xb00327c8) + F_32(w11, 0xbf597fc7) + F_32(w12, 0xc6e00bf3) + F_32(w13, 0xd5a79147) + F_32(w14, 0x06ca6351) + F_32(w15, 0x14292967) + + EXPAND_32 + + F_32(w0, 0x27b70a85) + F_32(w1, 0x2e1b2138) + F_32(w2, 0x4d2c6dfc) + F_32(w3, 0x53380d13) + F_32(w4, 0x650a7354) + F_32(w5, 0x766a0abb) + F_32(w6, 0x81c2c92e) + F_32(w7, 0x92722c85) + F_32(w8, 0xa2bfe8a1) + F_32(w9, 0xa81a664b) + F_32(w10, 0xc24b8b70) + F_32(w11, 0xc76c51a3) + F_32(w12, 0xd192e819) + F_32(w13, 0xd6990624) + F_32(w14, 0xf40e3585) + F_32(w15, 0x106aa070) + + EXPAND_32 + + F_32(w0, 0x19a4c116) + F_32(w1, 0x1e376c08) + F_32(w2, 0x2748774c) + F_32(w3, 0x34b0bcb5) + F_32(w4, 0x391c0cb3) + F_32(w5, 0x4ed8aa4a) + F_32(w6, 0x5b9cca4f) + F_32(w7, 0x682e6ff3) + F_32(w8, 0x748f82ee) + F_32(w9, 0x78a5636f) + F_32(w10, 0x84c87814) + F_32(w11, 0x8cc70208) + F_32(w12, 0x90befffa) + F_32(w13, 0xa4506ceb) + F_32(w14, 0xbef9a3f7) + F_32(w15, 0xc67178f2) + + a += state[0]; + b += state[1]; + c += state[2]; + d += state[3]; + e += state[4]; + f += state[5]; + g += state[6]; + h += state[7]; + + state[0] = a; + state[1] = b; + state[2] = c; + state[3] = d; + state[4] = e; + state[5] = f; + state[6] = g; + state[7] = h; + + in += 64; + inlen -= 64; + } + + store_bigendian_32(statebytes + 0, state[0]); + store_bigendian_32(statebytes + 4, state[1]); + store_bigendian_32(statebytes + 8, state[2]); + store_bigendian_32(statebytes + 12, state[3]); + store_bigendian_32(statebytes + 16, state[4]); + store_bigendian_32(statebytes + 20, state[5]); + store_bigendian_32(statebytes + 24, state[6]); + store_bigendian_32(statebytes + 28, state[7]); + + return inlen; +} + +static int crypto_hashblocks_sha512(unsigned char *statebytes,const unsigned char *in,unsigned long long inlen) +{ + uint64_t state[8]; + uint64_t a; + uint64_t b; + uint64_t c; + uint64_t d; + uint64_t e; + uint64_t f; + uint64_t g; + uint64_t h; + uint64_t T1; + uint64_t T2; + + a = load_bigendian_64(statebytes + 0); state[0] = a; + b = load_bigendian_64(statebytes + 8); state[1] = b; + c = load_bigendian_64(statebytes + 16); state[2] = c; + d = load_bigendian_64(statebytes + 24); state[3] = d; + e = load_bigendian_64(statebytes + 32); state[4] = e; + f = load_bigendian_64(statebytes + 40); state[5] = f; + g = load_bigendian_64(statebytes + 48); state[6] = g; + h = load_bigendian_64(statebytes + 56); state[7] = h; + + while (inlen >= 128) { + uint64_t w0 = load_bigendian_64(in + 0); + uint64_t w1 = load_bigendian_64(in + 8); + uint64_t w2 = load_bigendian_64(in + 16); + uint64_t w3 = load_bigendian_64(in + 24); + uint64_t w4 = load_bigendian_64(in + 32); + uint64_t w5 = load_bigendian_64(in + 40); + uint64_t w6 = load_bigendian_64(in + 48); + uint64_t w7 = load_bigendian_64(in + 56); + uint64_t w8 = load_bigendian_64(in + 64); + uint64_t w9 = load_bigendian_64(in + 72); + uint64_t w10 = load_bigendian_64(in + 80); + uint64_t w11 = load_bigendian_64(in + 88); + uint64_t w12 = load_bigendian_64(in + 96); + uint64_t w13 = load_bigendian_64(in + 104); + uint64_t w14 = load_bigendian_64(in + 112); + uint64_t w15 = load_bigendian_64(in + 120); + + F_64(w0 ,0x428a2f98d728ae22ULL) + F_64(w1 ,0x7137449123ef65cdULL) + F_64(w2 ,0xb5c0fbcfec4d3b2fULL) + F_64(w3 ,0xe9b5dba58189dbbcULL) + F_64(w4 ,0x3956c25bf348b538ULL) + F_64(w5 ,0x59f111f1b605d019ULL) + F_64(w6 ,0x923f82a4af194f9bULL) + F_64(w7 ,0xab1c5ed5da6d8118ULL) + F_64(w8 ,0xd807aa98a3030242ULL) + F_64(w9 ,0x12835b0145706fbeULL) + F_64(w10,0x243185be4ee4b28cULL) + F_64(w11,0x550c7dc3d5ffb4e2ULL) + F_64(w12,0x72be5d74f27b896fULL) + F_64(w13,0x80deb1fe3b1696b1ULL) + F_64(w14,0x9bdc06a725c71235ULL) + F_64(w15,0xc19bf174cf692694ULL) + + EXPAND_64 + + F_64(w0 ,0xe49b69c19ef14ad2ULL) + F_64(w1 ,0xefbe4786384f25e3ULL) + F_64(w2 ,0x0fc19dc68b8cd5b5ULL) + F_64(w3 ,0x240ca1cc77ac9c65ULL) + F_64(w4 ,0x2de92c6f592b0275ULL) + F_64(w5 ,0x4a7484aa6ea6e483ULL) + F_64(w6 ,0x5cb0a9dcbd41fbd4ULL) + F_64(w7 ,0x76f988da831153b5ULL) + F_64(w8 ,0x983e5152ee66dfabULL) + F_64(w9 ,0xa831c66d2db43210ULL) + F_64(w10,0xb00327c898fb213fULL) + F_64(w11,0xbf597fc7beef0ee4ULL) + F_64(w12,0xc6e00bf33da88fc2ULL) + F_64(w13,0xd5a79147930aa725ULL) + F_64(w14,0x06ca6351e003826fULL) + F_64(w15,0x142929670a0e6e70ULL) + + EXPAND_64 + + F_64(w0 ,0x27b70a8546d22ffcULL) + F_64(w1 ,0x2e1b21385c26c926ULL) + F_64(w2 ,0x4d2c6dfc5ac42aedULL) + F_64(w3 ,0x53380d139d95b3dfULL) + F_64(w4 ,0x650a73548baf63deULL) + F_64(w5 ,0x766a0abb3c77b2a8ULL) + F_64(w6 ,0x81c2c92e47edaee6ULL) + F_64(w7 ,0x92722c851482353bULL) + F_64(w8 ,0xa2bfe8a14cf10364ULL) + F_64(w9 ,0xa81a664bbc423001ULL) + F_64(w10,0xc24b8b70d0f89791ULL) + F_64(w11,0xc76c51a30654be30ULL) + F_64(w12,0xd192e819d6ef5218ULL) + F_64(w13,0xd69906245565a910ULL) + F_64(w14,0xf40e35855771202aULL) + F_64(w15,0x106aa07032bbd1b8ULL) + + EXPAND_64 + + F_64(w0 ,0x19a4c116b8d2d0c8ULL) + F_64(w1 ,0x1e376c085141ab53ULL) + F_64(w2 ,0x2748774cdf8eeb99ULL) + F_64(w3 ,0x34b0bcb5e19b48a8ULL) + F_64(w4 ,0x391c0cb3c5c95a63ULL) + F_64(w5 ,0x4ed8aa4ae3418acbULL) + F_64(w6 ,0x5b9cca4f7763e373ULL) + F_64(w7 ,0x682e6ff3d6b2b8a3ULL) + F_64(w8 ,0x748f82ee5defb2fcULL) + F_64(w9 ,0x78a5636f43172f60ULL) + F_64(w10,0x84c87814a1f0ab72ULL) + F_64(w11,0x8cc702081a6439ecULL) + F_64(w12,0x90befffa23631e28ULL) + F_64(w13,0xa4506cebde82bde9ULL) + F_64(w14,0xbef9a3f7b2c67915ULL) + F_64(w15,0xc67178f2e372532bULL) + + EXPAND_64 + + F_64(w0 ,0xca273eceea26619cULL) + F_64(w1 ,0xd186b8c721c0c207ULL) + F_64(w2 ,0xeada7dd6cde0eb1eULL) + F_64(w3 ,0xf57d4f7fee6ed178ULL) + F_64(w4 ,0x06f067aa72176fbaULL) + F_64(w5 ,0x0a637dc5a2c898a6ULL) + F_64(w6 ,0x113f9804bef90daeULL) + F_64(w7 ,0x1b710b35131c471bULL) + F_64(w8 ,0x28db77f523047d84ULL) + F_64(w9 ,0x32caab7b40c72493ULL) + F_64(w10,0x3c9ebe0a15c9bebcULL) + F_64(w11,0x431d67c49c100d4cULL) + F_64(w12,0x4cc5d4becb3e42b6ULL) + F_64(w13,0x597f299cfc657e2aULL) + F_64(w14,0x5fcb6fab3ad6faecULL) + F_64(w15,0x6c44198c4a475817ULL) + + a += state[0]; + b += state[1]; + c += state[2]; + d += state[3]; + e += state[4]; + f += state[5]; + g += state[6]; + h += state[7]; + + state[0] = a; + state[1] = b; + state[2] = c; + state[3] = d; + state[4] = e; + state[5] = f; + state[6] = g; + state[7] = h; + + in += 128; + inlen -= 128; + } + + store_bigendian_64(statebytes + 0,state[0]); + store_bigendian_64(statebytes + 8,state[1]); + store_bigendian_64(statebytes + 16,state[2]); + store_bigendian_64(statebytes + 24,state[3]); + store_bigendian_64(statebytes + 32,state[4]); + store_bigendian_64(statebytes + 40,state[5]); + store_bigendian_64(statebytes + 48,state[6]); + store_bigendian_64(statebytes + 56,state[7]); + + return inlen; +} + + +static const uint8_t iv_256[32] = { + 0x6a, 0x09, 0xe6, 0x67, 0xbb, 0x67, 0xae, 0x85, + 0x3c, 0x6e, 0xf3, 0x72, 0xa5, 0x4f, 0xf5, 0x3a, + 0x51, 0x0e, 0x52, 0x7f, 0x9b, 0x05, 0x68, 0x8c, + 0x1f, 0x83, 0xd9, 0xab, 0x5b, 0xe0, 0xcd, 0x19 +}; + +static const uint8_t iv_512[64] = { + 0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08, 0xbb, 0x67, 0xae, + 0x85, 0x84, 0xca, 0xa7, 0x3b, 0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94, + 0xf8, 0x2b, 0xa5, 0x4f, 0xf5, 0x3a, 0x5f, 0x1d, 0x36, 0xf1, 0x51, + 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1, 0x9b, 0x05, 0x68, 0x8c, + 0x2b, 0x3e, 0x6c, 0x1f, 0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd, + 0x6b, 0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79 +}; + +void sha256_inc_init(uint8_t *state) { + for (size_t i = 0; i < 32; ++i) { + state[i] = iv_256[i]; + } + for (size_t i = 32; i < 40; ++i) { + state[i] = 0; + } +} + +void sha512_inc_init(uint8_t *state) { + for (size_t i = 0; i < 64; ++i) { + state[i] = iv_512[i]; + } + for (size_t i = 64; i < 72; ++i) { + state[i] = 0; + } +} + +void sha256_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks) { + uint64_t bytes = load_bigendian_64(state + 32); + + crypto_hashblocks_sha256(state, in, 64 * inblocks); + bytes += 64 * inblocks; + + store_bigendian_64(state + 32, bytes); +} + +void sha512_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks) { + uint64_t bytes = load_bigendian_64(state + 64); + + crypto_hashblocks_sha512(state, in, 128 * inblocks); + bytes += 128 * inblocks; + + store_bigendian_64(state + 64, bytes); +} + +void sha256_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen) { + uint8_t padded[128]; + uint64_t bytes = load_bigendian_64(state + 32) + inlen; + + crypto_hashblocks_sha256(state, in, inlen); + in += inlen; + inlen &= 63; + in -= inlen; + + for (size_t i = 0; i < inlen; ++i) { + padded[i] = in[i]; + } + padded[inlen] = 0x80; + + if (inlen < 56) { + for (size_t i = inlen + 1; i < 56; ++i) { + padded[i] = 0; + } + padded[56] = (uint8_t) (bytes >> 53); + padded[57] = (uint8_t) (bytes >> 45); + padded[58] = (uint8_t) (bytes >> 37); + padded[59] = (uint8_t) (bytes >> 29); + padded[60] = (uint8_t) (bytes >> 21); + padded[61] = (uint8_t) (bytes >> 13); + padded[62] = (uint8_t) (bytes >> 5); + padded[63] = (uint8_t) (bytes << 3); + crypto_hashblocks_sha256(state, padded, 64); + } else { + for (size_t i = inlen + 1; i < 120; ++i) { + padded[i] = 0; + } + padded[120] = (uint8_t) (bytes >> 53); + padded[121] = (uint8_t) (bytes >> 45); + padded[122] = (uint8_t) (bytes >> 37); + padded[123] = (uint8_t) (bytes >> 29); + padded[124] = (uint8_t) (bytes >> 21); + padded[125] = (uint8_t) (bytes >> 13); + padded[126] = (uint8_t) (bytes >> 5); + padded[127] = (uint8_t) (bytes << 3); + crypto_hashblocks_sha256(state, padded, 128); + } + + for (size_t i = 0; i < 32; ++i) { + out[i] = state[i]; + } + +} + +void sha512_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen) { + uint8_t padded[256]; + uint64_t bytes = load_bigendian_64(state + 64) + inlen; + + crypto_hashblocks_sha512(state, in, inlen); + in += inlen; + inlen &= 127; + in -= inlen; + + for (size_t i = 0; i < inlen; ++i) { + padded[i] = in[i]; + } + padded[inlen] = 0x80; + + if (inlen < 112) { + for (size_t i = inlen + 1; i < 119; ++i) { + padded[i] = 0; + } + padded[119] = (uint8_t) (bytes >> 61); + padded[120] = (uint8_t) (bytes >> 53); + padded[121] = (uint8_t) (bytes >> 45); + padded[122] = (uint8_t) (bytes >> 37); + padded[123] = (uint8_t) (bytes >> 29); + padded[124] = (uint8_t) (bytes >> 21); + padded[125] = (uint8_t) (bytes >> 13); + padded[126] = (uint8_t) (bytes >> 5); + padded[127] = (uint8_t) (bytes << 3); + crypto_hashblocks_sha512(state, padded, 128); + } else { + for (size_t i = inlen + 1; i < 247; ++i) { + padded[i] = 0; + } + padded[247] = (uint8_t) (bytes >> 61); + padded[248] = (uint8_t) (bytes >> 53); + padded[249] = (uint8_t) (bytes >> 45); + padded[250] = (uint8_t) (bytes >> 37); + padded[251] = (uint8_t) (bytes >> 29); + padded[252] = (uint8_t) (bytes >> 21); + padded[253] = (uint8_t) (bytes >> 13); + padded[254] = (uint8_t) (bytes >> 5); + padded[255] = (uint8_t) (bytes << 3); + crypto_hashblocks_sha512(state, padded, 256); + } + + for (size_t i = 0; i < 64; ++i) { + out[i] = state[i]; + } +} + +void sha256(uint8_t *out, const uint8_t *in, size_t inlen) { + uint8_t state[40]; + + sha256_inc_init(state); + sha256_inc_finalize(out, state, in, inlen); +} + +void sha512(uint8_t *out, const uint8_t *in, size_t inlen) { + uint8_t state[72]; + + sha512_inc_init(state); + sha512_inc_finalize(out, state, in, inlen); +} + +/** + * mgf1 function based on the SHA-256 hash function + * Note that inlen should be sufficiently small that it still allows for + * an array to be allocated on the stack. Typically 'in' is merely a seed. + * Outputs outlen number of bytes + */ +void mgf1_256(unsigned char *out, unsigned long outlen, + const unsigned char *in, unsigned long inlen) +{ + SPX_VLA(uint8_t, inbuf, inlen+4); + unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES]; + unsigned long i; + + memcpy(inbuf, in, inlen); + + /* While we can fit in at least another full block of SHA256 output.. */ + for (i = 0; (i+1)*SPX_SHA256_OUTPUT_BYTES <= outlen; i++) { + u32_to_bytes(inbuf + inlen, i); + sha256(out, inbuf, inlen + 4); + out += SPX_SHA256_OUTPUT_BYTES; + } + /* Until we cannot anymore, and we fill the remainder. */ + if (outlen > i*SPX_SHA256_OUTPUT_BYTES) { + u32_to_bytes(inbuf + inlen, i); + sha256(outbuf, inbuf, inlen + 4); + memcpy(out, outbuf, outlen - i*SPX_SHA256_OUTPUT_BYTES); + } +} + +/* + * mgf1 function based on the SHA-512 hash function + */ +void mgf1_512(unsigned char *out, unsigned long outlen, + const unsigned char *in, unsigned long inlen) +{ + SPX_VLA(uint8_t, inbuf, inlen+4); + unsigned char outbuf[SPX_SHA512_OUTPUT_BYTES]; + unsigned long i; + + memcpy(inbuf, in, inlen); + + /* While we can fit in at least another full block of SHA512 output.. */ + for (i = 0; (i+1)*SPX_SHA512_OUTPUT_BYTES <= outlen; i++) { + u32_to_bytes(inbuf + inlen, i); + sha512(out, inbuf, inlen + 4); + out += SPX_SHA512_OUTPUT_BYTES; + } + /* Until we cannot anymore, and we fill the remainder. */ + if (outlen > i*SPX_SHA512_OUTPUT_BYTES) { + u32_to_bytes(inbuf + inlen, i); + sha512(outbuf, inbuf, inlen + 4); + memcpy(out, outbuf, outlen - i*SPX_SHA512_OUTPUT_BYTES); + } +} + + +/** + * Absorb the constant pub_seed using one round of the compression function + * This initializes state_seeded and state_seeded_512, which can then be + * reused in thash + **/ +void seed_state(spx_ctx *ctx) { + uint8_t block[SPX_SHA512_BLOCK_BYTES]; + size_t i; + + for (i = 0; i < SPX_N; ++i) { + block[i] = ctx->pub_seed[i]; + } + for (i = SPX_N; i < SPX_SHA512_BLOCK_BYTES; ++i) { + block[i] = 0; + } + /* block has been properly initialized for both SHA-256 and SHA-512 */ + + sha256_inc_init(ctx->state_seeded); + sha256_inc_blocks(ctx->state_seeded, block, 1); +#if SPX_SHA512 + sha512_inc_init(ctx->state_seeded_512); + sha512_inc_blocks(ctx->state_seeded_512, block, 1); +#endif +} diff --git a/src/crypto/pq/sphincsplus/sha2.h b/src/crypto/pq/sphincsplus/sha2.h new file mode 100644 index 0000000..732ab4b --- /dev/null +++ b/src/crypto/pq/sphincsplus/sha2.h @@ -0,0 +1,43 @@ +#ifndef SPX_SHA2_H +#define SPX_SHA2_H + +#include "params.h" + +#define SPX_SHA256_BLOCK_BYTES 64 +#define SPX_SHA256_OUTPUT_BYTES 32 /* This does not necessarily equal SPX_N */ + +#define SPX_SHA512_BLOCK_BYTES 128 +#define SPX_SHA512_OUTPUT_BYTES 64 + +#if SPX_SHA256_OUTPUT_BYTES < SPX_N + #error Linking against SHA-256 with N larger than 32 bytes is not supported +#endif + +#define SPX_SHA256_ADDR_BYTES 22 + +#include +#include + +void sha256_inc_init(uint8_t *state); +void sha256_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks); +void sha256_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen); +void sha256(uint8_t *out, const uint8_t *in, size_t inlen); + +void sha512_inc_init(uint8_t *state); +void sha512_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks); +void sha512_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen); +void sha512(uint8_t *out, const uint8_t *in, size_t inlen); + +#define mgf1_256 SPX_NAMESPACE(mgf1_256) +void mgf1_256(unsigned char *out, unsigned long outlen, + const unsigned char *in, unsigned long inlen); + +#define mgf1_512 SPX_NAMESPACE(mgf1_512) +void mgf1_512(unsigned char *out, unsigned long outlen, + const unsigned char *in, unsigned long inlen); + +#define seed_state SPX_NAMESPACE(seed_state) +void seed_state(spx_ctx *ctx); + + +#endif diff --git a/src/crypto/pq/sphincsplus/sha2_offsets.h b/src/crypto/pq/sphincsplus/sha2_offsets.h new file mode 100644 index 0000000..49f7e85 --- /dev/null +++ b/src/crypto/pq/sphincsplus/sha2_offsets.h @@ -0,0 +1,20 @@ +#ifndef SHA2_OFFSETS_H_ +#define SHA2_OFFSETS_H_ + +/* + * Offsets of various fields in the address structure when we use SHA2 as + * the Sphincs+ hash function + */ + +#define SPX_OFFSET_LAYER 0 /* The byte used to specify the Merkle tree layer */ +#define SPX_OFFSET_TREE 1 /* The start of the 8 byte field used to specify the tree */ +#define SPX_OFFSET_TYPE 9 /* The byte used to specify the hash type (reason) */ +#define SPX_OFFSET_KP_ADDR 10 /* The start of the 4 byte field used to specify the key pair address */ +#define SPX_OFFSET_CHAIN_ADDR 17 /* The byte used to specify the chain address (which Winternitz chain) */ +#define SPX_OFFSET_HASH_ADDR 21 /* The byte used to specify the hash address (where in the Winternitz chain) */ +#define SPX_OFFSET_TREE_HGT 17 /* The byte used to specify the height of this node in the FORS or Merkle tree */ +#define SPX_OFFSET_TREE_INDEX 18 /* The start of the 4 byte field used to specify the node in the FORS or Merkle tree */ + +#define SPX_SHA2 1 + +#endif /* SHA2_OFFSETS_H_ */ diff --git a/src/crypto/pq/sphincsplus/sign.c b/src/crypto/pq/sphincsplus/sign.c new file mode 100644 index 0000000..a1ee78a --- /dev/null +++ b/src/crypto/pq/sphincsplus/sign.c @@ -0,0 +1,293 @@ +#include +#include +#include + +#include "api.h" +#include "params.h" +#include "wots.h" +#include "fors.h" +#include "hash.h" +#include "thash.h" +#include "address.h" +#include "randombytes.h" +#include "utils.h" +#include "merkle.h" + +#ifndef VEXTA_PQ_VERIFY_ONLY +/* + * Returns the length of a secret key, in bytes + */ +unsigned long long crypto_sign_secretkeybytes(void) +{ + return CRYPTO_SECRETKEYBYTES; +} + +/* + * Returns the length of a public key, in bytes + */ +unsigned long long crypto_sign_publickeybytes(void) +{ + return CRYPTO_PUBLICKEYBYTES; +} + +/* + * Returns the length of a signature, in bytes + */ +unsigned long long crypto_sign_bytes(void) +{ + return CRYPTO_BYTES; +} + +/* + * Returns the length of the seed required to generate a key pair, in bytes + */ +unsigned long long crypto_sign_seedbytes(void) +{ + return CRYPTO_SEEDBYTES; +} + +/* + * Generates an SPX key pair given a seed of length + * Format sk: [SK_SEED || SK_PRF || PUB_SEED || root] + * Format pk: [PUB_SEED || root] + */ +int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, + const unsigned char *seed) +{ + spx_ctx ctx; + + /* Initialize SK_SEED, SK_PRF and PUB_SEED from seed. */ + memcpy(sk, seed, CRYPTO_SEEDBYTES); + + memcpy(pk, sk + 2*SPX_N, SPX_N); + + memcpy(ctx.pub_seed, pk, SPX_N); + memcpy(ctx.sk_seed, sk, SPX_N); + + /* This hook allows the hash function instantiation to do whatever + preparation or computation it needs, based on the public seed. */ + initialize_hash_function(&ctx); + + /* Compute root node of the top-most subtree. */ + merkle_gen_root(sk + 3*SPX_N, &ctx); + + memcpy(pk + SPX_N, sk + 3*SPX_N, SPX_N); + + return 0; +} + +/* + * Generates an SPX key pair. + * Format sk: [SK_SEED || SK_PRF || PUB_SEED || root] + * Format pk: [PUB_SEED || root] + */ +int crypto_sign_keypair(unsigned char *pk, unsigned char *sk) +{ + unsigned char seed[CRYPTO_SEEDBYTES]; + randombytes(seed, CRYPTO_SEEDBYTES); + crypto_sign_seed_keypair(pk, sk, seed); + + return 0; +} + +/** + * Returns an array containing a detached signature. + */ +int crypto_sign_signature(uint8_t *sig, size_t *siglen, + const uint8_t *m, size_t mlen, const uint8_t *sk) +{ + spx_ctx ctx; + + const unsigned char *sk_prf = sk + SPX_N; + const unsigned char *pk = sk + 2*SPX_N; + + unsigned char optrand[SPX_N]; + unsigned char mhash[SPX_FORS_MSG_BYTES]; + unsigned char root[SPX_N]; + uint32_t i; + uint64_t tree; + uint32_t idx_leaf; + uint32_t wots_addr[8] = {0}; + uint32_t tree_addr[8] = {0}; + + memcpy(ctx.sk_seed, sk, SPX_N); + memcpy(ctx.pub_seed, pk, SPX_N); + + /* This hook allows the hash function instantiation to do whatever + preparation or computation it needs, based on the public seed. */ + initialize_hash_function(&ctx); + + set_type(wots_addr, SPX_ADDR_TYPE_WOTS); + set_type(tree_addr, SPX_ADDR_TYPE_HASHTREE); + + /* Optionally, signing can be made non-deterministic using optrand. + This can help counter side-channel attacks that would benefit from + getting a large number of traces when the signer uses the same nodes. */ + randombytes(optrand, SPX_N); + /* Compute the digest randomization value. */ + gen_message_random(sig, sk_prf, optrand, m, mlen, &ctx); + + /* Derive the message digest and leaf index from R, PK and M. */ + hash_message(mhash, &tree, &idx_leaf, sig, pk, m, mlen, &ctx); + sig += SPX_N; + + set_tree_addr(wots_addr, tree); + set_keypair_addr(wots_addr, idx_leaf); + + /* Sign the message hash using FORS. */ + fors_sign(sig, root, mhash, &ctx, wots_addr); + sig += SPX_FORS_BYTES; + + for (i = 0; i < SPX_D; i++) { + set_layer_addr(tree_addr, i); + set_tree_addr(tree_addr, tree); + + copy_subtree_addr(wots_addr, tree_addr); + set_keypair_addr(wots_addr, idx_leaf); + + merkle_sign(sig, root, &ctx, wots_addr, tree_addr, idx_leaf); + sig += SPX_WOTS_BYTES + SPX_TREE_HEIGHT * SPX_N; + + /* Update the indices for the next layer. */ + idx_leaf = (tree & ((1 << SPX_TREE_HEIGHT)-1)); + tree = tree >> SPX_TREE_HEIGHT; + } + + *siglen = SPX_BYTES; + + return 0; +} + +#endif // !VEXTA_PQ_VERIFY_ONLY + +/** + * Verifies a detached signature and message under a given public key. + */ +int crypto_sign_verify(const uint8_t *sig, size_t siglen, + const uint8_t *m, size_t mlen, const uint8_t *pk) +{ + spx_ctx ctx; + const unsigned char *pub_root = pk + SPX_N; + unsigned char mhash[SPX_FORS_MSG_BYTES]; + unsigned char wots_pk[SPX_WOTS_BYTES]; + unsigned char root[SPX_N]; + unsigned char leaf[SPX_N]; + unsigned int i; + uint64_t tree; + uint32_t idx_leaf; + uint32_t wots_addr[8] = {0}; + uint32_t tree_addr[8] = {0}; + uint32_t wots_pk_addr[8] = {0}; + + if (siglen != SPX_BYTES) { + return -1; + } + + memcpy(ctx.pub_seed, pk, SPX_N); + + /* This hook allows the hash function instantiation to do whatever + preparation or computation it needs, based on the public seed. */ + initialize_hash_function(&ctx); + + set_type(wots_addr, SPX_ADDR_TYPE_WOTS); + set_type(tree_addr, SPX_ADDR_TYPE_HASHTREE); + set_type(wots_pk_addr, SPX_ADDR_TYPE_WOTSPK); + + /* Derive the message digest and leaf index from R || PK || M. */ + /* The additional SPX_N is a result of the hash domain separator. */ + hash_message(mhash, &tree, &idx_leaf, sig, pk, m, mlen, &ctx); + sig += SPX_N; + + /* Layer correctly defaults to 0, so no need to set_layer_addr */ + set_tree_addr(wots_addr, tree); + set_keypair_addr(wots_addr, idx_leaf); + + fors_pk_from_sig(root, sig, mhash, &ctx, wots_addr); + sig += SPX_FORS_BYTES; + + /* For each subtree.. */ + for (i = 0; i < SPX_D; i++) { + set_layer_addr(tree_addr, i); + set_tree_addr(tree_addr, tree); + + copy_subtree_addr(wots_addr, tree_addr); + set_keypair_addr(wots_addr, idx_leaf); + + copy_keypair_addr(wots_pk_addr, wots_addr); + + /* The WOTS public key is only correct if the signature was correct. */ + /* Initially, root is the FORS pk, but on subsequent iterations it is + the root of the subtree below the currently processed subtree. */ + wots_pk_from_sig(wots_pk, sig, root, &ctx, wots_addr); + sig += SPX_WOTS_BYTES; + + /* Compute the leaf node using the WOTS public key. */ + thash(leaf, wots_pk, SPX_WOTS_LEN, &ctx, wots_pk_addr); + + /* Compute the root node of this subtree. */ + compute_root(root, leaf, idx_leaf, 0, sig, SPX_TREE_HEIGHT, + &ctx, tree_addr); + sig += SPX_TREE_HEIGHT * SPX_N; + + /* Update the indices for the next layer. */ + idx_leaf = (tree & ((1 << SPX_TREE_HEIGHT)-1)); + tree = tree >> SPX_TREE_HEIGHT; + } + + /* Check if the root node equals the root node in the public key. */ + if (memcmp(root, pub_root, SPX_N)) { + return -1; + } + + return 0; +} + + +#ifndef VEXTA_PQ_VERIFY_ONLY +/** + * Returns an array containing the signature followed by the message. + */ +int crypto_sign(unsigned char *sm, unsigned long long *smlen, + const unsigned char *m, unsigned long long mlen, + const unsigned char *sk) +{ + size_t siglen; + + crypto_sign_signature(sm, &siglen, m, (size_t)mlen, sk); + + memmove(sm + SPX_BYTES, m, mlen); + *smlen = siglen + mlen; + + return 0; +} + +#endif // !VEXTA_PQ_VERIFY_ONLY + +/** + * Verifies a given signature-message pair under a given public key. + */ +int crypto_sign_open(unsigned char *m, unsigned long long *mlen, + const unsigned char *sm, unsigned long long smlen, + const unsigned char *pk) +{ + /* The API caller does not necessarily know what size a signature should be + but SPHINCS+ signatures are always exactly SPX_BYTES. */ + if (smlen < SPX_BYTES) { + memset(m, 0, smlen); + *mlen = 0; + return -1; + } + + *mlen = smlen - SPX_BYTES; + + if (crypto_sign_verify(sm, SPX_BYTES, sm + SPX_BYTES, (size_t)*mlen, pk)) { + memset(m, 0, smlen); + *mlen = 0; + return -1; + } + + /* If verification was successful, move the message to the right place. */ + memmove(m, sm + SPX_BYTES, *mlen); + + return 0; +} diff --git a/src/crypto/pq/sphincsplus/thash.h b/src/crypto/pq/sphincsplus/thash.h new file mode 100644 index 0000000..8687ccf --- /dev/null +++ b/src/crypto/pq/sphincsplus/thash.h @@ -0,0 +1,13 @@ +#ifndef SPX_THASH_H +#define SPX_THASH_H + +#include "context.h" +#include "params.h" + +#include + +#define thash SPX_NAMESPACE(thash) +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]); + +#endif diff --git a/src/crypto/pq/sphincsplus/thash_sha2_simple.c b/src/crypto/pq/sphincsplus/thash_sha2_simple.c new file mode 100644 index 0000000..da58896 --- /dev/null +++ b/src/crypto/pq/sphincsplus/thash_sha2_simple.c @@ -0,0 +1,59 @@ +#include +#include + +#include "thash.h" +#include "address.h" +#include "params.h" +#include "utils.h" +#include "sha2.h" + +#if SPX_SHA512 +static void thash_512(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]); +#endif + +/** + * Takes an array of inblocks concatenated arrays of SPX_N bytes. + */ +void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ +#if SPX_SHA512 + if (inblocks > 1) { + thash_512(out, in, inblocks, ctx, addr); + return; + } +#endif + + unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES]; + uint8_t sha2_state[40]; + SPX_VLA(uint8_t, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + + /* Retrieve precomputed state containing pub_seed */ + memcpy(sha2_state, ctx->state_seeded, 40 * sizeof(uint8_t)); + + memcpy(buf, addr, SPX_SHA256_ADDR_BYTES); + memcpy(buf + SPX_SHA256_ADDR_BYTES, in, inblocks * SPX_N); + + sha256_inc_finalize(outbuf, sha2_state, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + memcpy(out, outbuf, SPX_N); +} + +#if SPX_SHA512 +static void thash_512(unsigned char *out, const unsigned char *in, unsigned int inblocks, + const spx_ctx *ctx, uint32_t addr[8]) +{ + unsigned char outbuf[SPX_SHA512_OUTPUT_BYTES]; + uint8_t sha2_state[72]; + SPX_VLA(uint8_t, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + + /* Retrieve precomputed state containing pub_seed */ + memcpy(sha2_state, ctx->state_seeded_512, 72 * sizeof(uint8_t)); + + memcpy(buf, addr, SPX_SHA256_ADDR_BYTES); + memcpy(buf + SPX_SHA256_ADDR_BYTES, in, inblocks * SPX_N); + + sha512_inc_finalize(outbuf, sha2_state, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N); + memcpy(out, outbuf, SPX_N); +} +#endif diff --git a/src/crypto/pq/sphincsplus/utils.c b/src/crypto/pq/sphincsplus/utils.c new file mode 100644 index 0000000..63d52ee --- /dev/null +++ b/src/crypto/pq/sphincsplus/utils.c @@ -0,0 +1,154 @@ +#include + +#include "utils.h" +#include "params.h" +#include "hash.h" +#include "thash.h" +#include "address.h" + +/** + * Converts the value of 'in' to 'outlen' bytes in big-endian byte order. + */ +void ull_to_bytes(unsigned char *out, unsigned int outlen, + unsigned long long in) +{ + int i; + + /* Iterate over out in decreasing order, for big-endianness. */ + for (i = (signed int)outlen - 1; i >= 0; i--) { + out[i] = in & 0xff; + in = in >> 8; + } +} + +void u32_to_bytes(unsigned char *out, uint32_t in) +{ + out[0] = (unsigned char)(in >> 24); + out[1] = (unsigned char)(in >> 16); + out[2] = (unsigned char)(in >> 8); + out[3] = (unsigned char)in; +} + +/** + * Converts the inlen bytes in 'in' from big-endian byte order to an integer. + */ +unsigned long long bytes_to_ull(const unsigned char *in, unsigned int inlen) +{ + unsigned long long retval = 0; + unsigned int i; + + for (i = 0; i < inlen; i++) { + retval |= ((unsigned long long)in[i]) << (8*(inlen - 1 - i)); + } + return retval; +} + +/** + * Computes a root node given a leaf and an auth path. + * Expects address to be complete other than the tree_height and tree_index. + */ +void compute_root(unsigned char *root, const unsigned char *leaf, + uint32_t leaf_idx, uint32_t idx_offset, + const unsigned char *auth_path, uint32_t tree_height, + const spx_ctx *ctx, uint32_t addr[8]) +{ + uint32_t i; + unsigned char buffer[2 * SPX_N]; + + /* If leaf_idx is odd (last bit = 1), current path element is a right child + and auth_path has to go left. Otherwise it is the other way around. */ + if (leaf_idx & 1) { + memcpy(buffer + SPX_N, leaf, SPX_N); + memcpy(buffer, auth_path, SPX_N); + } + else { + memcpy(buffer, leaf, SPX_N); + memcpy(buffer + SPX_N, auth_path, SPX_N); + } + auth_path += SPX_N; + + for (i = 0; i < tree_height - 1; i++) { + leaf_idx >>= 1; + idx_offset >>= 1; + /* Set the address of the node we're creating. */ + set_tree_height(addr, i + 1); + set_tree_index(addr, leaf_idx + idx_offset); + + /* Pick the right or left neighbor, depending on parity of the node. */ + if (leaf_idx & 1) { + thash(buffer + SPX_N, buffer, 2, ctx, addr); + memcpy(buffer, auth_path, SPX_N); + } + else { + thash(buffer, buffer, 2, ctx, addr); + memcpy(buffer + SPX_N, auth_path, SPX_N); + } + auth_path += SPX_N; + } + + /* The last iteration is exceptional; we do not copy an auth_path node. */ + leaf_idx >>= 1; + idx_offset >>= 1; + set_tree_height(addr, tree_height); + set_tree_index(addr, leaf_idx + idx_offset); + thash(root, buffer, 2, ctx, addr); +} + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + */ +void treehash(unsigned char *root, unsigned char *auth_path, const spx_ctx* ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leaf)( + unsigned char* /* leaf */, + const spx_ctx* /* ctx */, + uint32_t /* addr_idx */, const uint32_t[8] /* tree_addr */), + uint32_t tree_addr[8]) +{ + SPX_VLA(uint8_t, stack, (tree_height+1)*SPX_N); + SPX_VLA(unsigned int, heights, tree_height+1); + unsigned int offset = 0; + uint32_t idx; + uint32_t tree_idx; + + for (idx = 0; idx < (uint32_t)(1 << tree_height); idx++) { + /* Add the next leaf node to the stack. */ + gen_leaf(stack + offset*SPX_N, ctx, idx + idx_offset, tree_addr); + offset++; + heights[offset - 1] = 0; + + /* If this is a node we need for the auth path.. */ + if ((leaf_idx ^ 0x1) == idx) { + memcpy(auth_path, stack + (offset - 1)*SPX_N, SPX_N); + } + + /* While the top-most nodes are of equal height.. */ + while (offset >= 2 && heights[offset - 1] == heights[offset - 2]) { + /* Compute index of the new node, in the next layer. */ + tree_idx = (idx >> (heights[offset - 1] + 1)); + + /* Set the address of the node we're creating. */ + set_tree_height(tree_addr, heights[offset - 1] + 1); + set_tree_index(tree_addr, + tree_idx + (idx_offset >> (heights[offset-1] + 1))); + /* Hash the top-most nodes from the stack together. */ + thash(stack + (offset - 2)*SPX_N, + stack + (offset - 2)*SPX_N, 2, ctx, tree_addr); + offset--; + /* Note that the top-most node is now one layer higher. */ + heights[offset - 1]++; + + /* If this is a node we need for the auth path.. */ + if (((leaf_idx >> heights[offset - 1]) ^ 0x1) == tree_idx) { + memcpy(auth_path + heights[offset - 1]*SPX_N, + stack + (offset - 1)*SPX_N, SPX_N); + } + } + } + memcpy(root, stack, SPX_N); +} diff --git a/src/crypto/pq/sphincsplus/utils.h b/src/crypto/pq/sphincsplus/utils.h new file mode 100644 index 0000000..b13502c --- /dev/null +++ b/src/crypto/pq/sphincsplus/utils.h @@ -0,0 +1,64 @@ +#ifndef SPX_UTILS_H +#define SPX_UTILS_H + +#include +#include "params.h" +#include "context.h" + + +/* To support MSVC use alloca() instead of VLAs. See #20. */ +#ifdef _MSC_VER +/* MSVC defines _alloca in malloc.h */ +# include +/* Note: _malloca(), which is recommended over deprecated _alloca, + requires that you call _freea(). So we stick with _alloca */ +# define SPX_VLA(__t,__x,__s) __t *__x = (__t*)_alloca((__s)*sizeof(__t)) +#else +# define SPX_VLA(__t,__x,__s) __t __x[__s] +#endif + +/** + * Converts the value of 'in' to 'outlen' bytes in big-endian byte order. + */ +#define ull_to_bytes SPX_NAMESPACE(ull_to_bytes) +void ull_to_bytes(unsigned char *out, unsigned int outlen, + unsigned long long in); +#define u32_to_bytes SPX_NAMESPACE(u32_to_bytes) +void u32_to_bytes(unsigned char *out, uint32_t in); + +/** + * Converts the inlen bytes in 'in' from big-endian byte order to an integer. + */ +#define bytes_to_ull SPX_NAMESPACE(bytes_to_ull) +unsigned long long bytes_to_ull(const unsigned char *in, unsigned int inlen); + +/** + * Computes a root node given a leaf and an auth path. + * Expects address to be complete other than the tree_height and tree_index. + */ +#define compute_root SPX_NAMESPACE(compute_root) +void compute_root(unsigned char *root, const unsigned char *leaf, + uint32_t leaf_idx, uint32_t idx_offset, + const unsigned char *auth_path, uint32_t tree_height, + const spx_ctx *ctx, uint32_t addr[8]); + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + */ +#define treehash SPX_NAMESPACE(treehash) +void treehash(unsigned char *root, unsigned char *auth_path, + const spx_ctx* ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leaf)( + unsigned char* /* leaf */, + const spx_ctx* ctx /* ctx */, + uint32_t /* addr_idx */, const uint32_t[8] /* tree_addr */), + uint32_t tree_addr[8]); + + +#endif diff --git a/src/crypto/pq/sphincsplus/utilsx1.c b/src/crypto/pq/sphincsplus/utilsx1.c new file mode 100644 index 0000000..f6a6700 --- /dev/null +++ b/src/crypto/pq/sphincsplus/utilsx1.c @@ -0,0 +1,100 @@ +#include + +#include "utils.h" +#include "utilsx1.h" +#include "params.h" +#include "thash.h" +#include "address.h" + +/* + * Generate the entire Merkle tree, computing the authentication path for + * leaf_idx, and the resulting root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE) + * + * This expects tree_addr to be initialized to the addr structures for the + * Merkle tree nodes + * + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + * + * This works by using the standard Merkle tree building algorithm, + */ +void treehashx1(unsigned char *root, unsigned char *auth_path, + const spx_ctx* ctx, + uint32_t leaf_idx, uint32_t idx_offset, + uint32_t tree_height, + void (*gen_leaf)( + unsigned char* /* Where to write the leaves */, + const spx_ctx* /* ctx */, + uint32_t idx, void *info), + uint32_t tree_addr[8], + void *info) +{ + /* This is where we keep the intermediate nodes */ + SPX_VLA(uint8_t, stack, tree_height*SPX_N); + + uint32_t idx; + uint32_t max_idx = (uint32_t)((1 << tree_height) - 1); + for (idx = 0;; idx++) { + unsigned char current[2*SPX_N]; /* Current logical node is at */ + /* index[SPX_N]. We do this to minimize the number of copies */ + /* needed during a thash */ + gen_leaf( ¤t[SPX_N], ctx, idx + idx_offset, + info ); + + /* Now combine the freshly generated right node with previously */ + /* generated left ones */ + uint32_t internal_idx_offset = idx_offset; + uint32_t internal_idx = idx; + uint32_t internal_leaf = leaf_idx; + uint32_t h; /* The height we are in the Merkle tree */ + for (h=0;; h++, internal_idx >>= 1, internal_leaf >>= 1) { + + /* Check if we hit the top of the tree */ + if (h == tree_height) { + /* We hit the root; return it */ + memcpy( root, ¤t[SPX_N], SPX_N ); + return; + } + + /* + * Check if the node we have is a part of the + * authentication path; if it is, write it out + */ + if ((internal_idx ^ internal_leaf) == 0x01) { + memcpy( &auth_path[ h * SPX_N ], + ¤t[SPX_N], + SPX_N ); + } + + /* + * Check if we're at a left child; if so, stop going up the stack + * Exception: if we've reached the end of the tree, keep on going + * (so we combine the last 4 nodes into the one root node in two + * more iterations) + */ + if ((internal_idx & 1) == 0 && idx < max_idx) { + break; + } + + /* Ok, we're at a right node */ + /* Now combine the left and right logical nodes together */ + + /* Set the address of the node we're creating. */ + internal_idx_offset >>= 1; + set_tree_height(tree_addr, h + 1); + set_tree_index(tree_addr, internal_idx/2 + internal_idx_offset ); + + unsigned char *left = &stack[h * SPX_N]; + memcpy( ¤t[0], left, SPX_N ); + thash( ¤t[1 * SPX_N], + ¤t[0 * SPX_N], + 2, ctx, tree_addr); + } + + /* We've hit a left child; save the current for when we get the */ + /* corresponding right right */ + memcpy( &stack[h * SPX_N], ¤t[SPX_N], SPX_N); + } +} diff --git a/src/crypto/pq/sphincsplus/utilsx1.h b/src/crypto/pq/sphincsplus/utilsx1.h new file mode 100644 index 0000000..a7fcf15 --- /dev/null +++ b/src/crypto/pq/sphincsplus/utilsx1.h @@ -0,0 +1,26 @@ +#ifndef SPX_UTILSX4_H +#define SPX_UTILSX4_H + +#include +#include "params.h" +#include "context.h" + +/** + * For a given leaf index, computes the authentication path and the resulting + * root node using Merkle's TreeHash algorithm. + * Expects the layer and tree parts of the tree_addr to be set, as well as the + * tree type (i.e. SPX_ADDR_TYPE_HASHTREE or SPX_ADDR_TYPE_FORSTREE). + * Applies the offset idx_offset to indices before building addresses, so that + * it is possible to continue counting indices across trees. + */ +#define treehashx1 SPX_NAMESPACE(treehashx1) +void treehashx1(unsigned char *root, unsigned char *auth_path, + const spx_ctx* ctx, + uint32_t leaf_idx, uint32_t idx_offset, uint32_t tree_height, + void (*gen_leaf)( + unsigned char* /* Where to write the leaf */, + const spx_ctx* /* ctx */, + uint32_t addr_idx, void *info), + uint32_t tree_addrx4[8], void *info); + +#endif diff --git a/src/crypto/pq/sphincsplus/wots.c b/src/crypto/pq/sphincsplus/wots.c new file mode 100644 index 0000000..df83278 --- /dev/null +++ b/src/crypto/pq/sphincsplus/wots.c @@ -0,0 +1,112 @@ +#include +#include + +#include "utils.h" +#include "utilsx1.h" +#include "hash.h" +#include "thash.h" +#include "wots.h" +#include "wotsx1.h" +#include "address.h" +#include "params.h" + +// TODO clarify address expectations, and make them more uniform. +// TODO i.e. do we expect types to be set already? +// TODO and do we expect modifications or copies? + +/** + * Computes the chaining function. + * out and in have to be n-byte arrays. + * + * Interprets in as start-th value of the chain. + * addr has to contain the address of the chain. + */ +static void gen_chain(unsigned char *out, const unsigned char *in, + unsigned int start, unsigned int steps, + const spx_ctx *ctx, uint32_t addr[8]) +{ + uint32_t i; + + /* Initialize out with the value at position 'start'. */ + memcpy(out, in, SPX_N); + + /* Iterate 'steps' calls to the hash function. */ + for (i = start; i < (start+steps) && i < SPX_WOTS_W; i++) { + set_hash_addr(addr, i); + thash(out, out, 1, ctx, addr); + } +} + +/** + * base_w algorithm as described in draft. + * Interprets an array of bytes as integers in base w. + * This only works when log_w is a divisor of 8. + */ +static void base_w(unsigned int *output, const int out_len, + const unsigned char *input) +{ + int in = 0; + int out = 0; + unsigned char total; + int bits = 0; + int consumed; + + for (consumed = 0; consumed < out_len; consumed++) { + if (bits == 0) { + total = input[in]; + in++; + bits += 8; + } + bits -= SPX_WOTS_LOGW; + output[out] = (total >> bits) & (SPX_WOTS_W - 1); + out++; + } +} + +/* Computes the WOTS+ checksum over a message (in base_w). */ +static void wots_checksum(unsigned int *csum_base_w, + const unsigned int *msg_base_w) +{ + unsigned int csum = 0; + unsigned char csum_bytes[(SPX_WOTS_LEN2 * SPX_WOTS_LOGW + 7) / 8]; + unsigned int i; + + /* Compute checksum. */ + for (i = 0; i < SPX_WOTS_LEN1; i++) { + csum += SPX_WOTS_W - 1 - msg_base_w[i]; + } + + /* Convert checksum to base_w. */ + /* Make sure expected empty zero bits are the least significant bits. */ + csum = csum << ((8 - ((SPX_WOTS_LEN2 * SPX_WOTS_LOGW) % 8)) % 8); + ull_to_bytes(csum_bytes, sizeof(csum_bytes), csum); + base_w(csum_base_w, SPX_WOTS_LEN2, csum_bytes); +} + +/* Takes a message and derives the matching chain lengths. */ +void chain_lengths(unsigned int *lengths, const unsigned char *msg) +{ + base_w(lengths, SPX_WOTS_LEN1, msg); + wots_checksum(lengths + SPX_WOTS_LEN1, lengths); +} + +/** + * Takes a WOTS signature and an n-byte message, computes a WOTS public key. + * + * Writes the computed public key to 'pk'. + */ +void wots_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *msg, + const spx_ctx *ctx, uint32_t addr[8]) +{ + unsigned int lengths[SPX_WOTS_LEN]; + uint32_t i; + + chain_lengths(lengths, msg); + + for (i = 0; i < SPX_WOTS_LEN; i++) { + set_chain_addr(addr, i); + gen_chain(pk + i*SPX_N, sig + i*SPX_N, + lengths[i], SPX_WOTS_W - 1 - lengths[i], ctx, addr); + } +} diff --git a/src/crypto/pq/sphincsplus/wots.h b/src/crypto/pq/sphincsplus/wots.h new file mode 100644 index 0000000..7e77056 --- /dev/null +++ b/src/crypto/pq/sphincsplus/wots.h @@ -0,0 +1,25 @@ +#ifndef SPX_WOTS_H +#define SPX_WOTS_H + +#include + +#include "params.h" +#include "context.h" + +/** + * Takes a WOTS signature and an n-byte message, computes a WOTS public key. + * + * Writes the computed public key to 'pk'. + */ +#define wots_pk_from_sig SPX_NAMESPACE(wots_pk_from_sig) +void wots_pk_from_sig(unsigned char *pk, + const unsigned char *sig, const unsigned char *msg, + const spx_ctx *ctx, uint32_t addr[8]); + +/* + * Compute the chain lengths needed for a given message hash + */ +#define chain_lengths SPX_NAMESPACE(chain_lengths) +void chain_lengths(unsigned int *lengths, const unsigned char *msg); + +#endif diff --git a/src/crypto/pq/sphincsplus/wotsx1.c b/src/crypto/pq/sphincsplus/wotsx1.c new file mode 100644 index 0000000..dfb3780 --- /dev/null +++ b/src/crypto/pq/sphincsplus/wotsx1.c @@ -0,0 +1,73 @@ +#include +#include + +#include "utils.h" +#include "hash.h" +#include "thash.h" +#include "wots.h" +#include "wotsx1.h" +#include "address.h" +#include "params.h" + +/* + * This generates a WOTS public key + * It also generates the WOTS signature if leaf_info indicates + * that we're signing with this WOTS key + */ +void wots_gen_leafx1(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info) { + struct leaf_info_x1 *info = v_info; + uint32_t *leaf_addr = info->leaf_addr; + uint32_t *pk_addr = info->pk_addr; + unsigned int i, k; + unsigned char pk_buffer[ SPX_WOTS_BYTES ]; + unsigned char *buffer; + uint32_t wots_k_mask; + + if (leaf_idx == info->wots_sign_leaf) { + /* We're traversing the leaf that's signing; generate the WOTS */ + /* signature */ + wots_k_mask = 0; + } else { + /* Nope, we're just generating pk's; turn off the signature logic */ + wots_k_mask = (uint32_t)~0; + } + + set_keypair_addr( leaf_addr, leaf_idx ); + set_keypair_addr( pk_addr, leaf_idx ); + + for (i = 0, buffer = pk_buffer; i < SPX_WOTS_LEN; i++, buffer += SPX_N) { + uint32_t wots_k = info->wots_steps[i] | wots_k_mask; /* Set wots_k to */ + /* the step if we're generating a signature, ~0 if we're not */ + + /* Start with the secret seed */ + set_chain_addr(leaf_addr, i); + set_hash_addr(leaf_addr, 0); + set_type(leaf_addr, SPX_ADDR_TYPE_WOTSPRF); + + prf_addr(buffer, ctx, leaf_addr); + + set_type(leaf_addr, SPX_ADDR_TYPE_WOTS); + + /* Iterate down the WOTS chain */ + for (k=0;; k++) { + /* Check if this is the value that needs to be saved as a */ + /* part of the WOTS signature */ + if (k == wots_k) { + memcpy( info->wots_sig + i * SPX_N, buffer, SPX_N ); + } + + /* Check if we hit the top of the chain */ + if (k == SPX_WOTS_W - 1) break; + + /* Iterate one step on the chain */ + set_hash_addr(leaf_addr, k); + + thash(buffer, buffer, 1, ctx, leaf_addr); + } + } + + /* Do the final thash to generate the public keys */ + thash(dest, pk_buffer, SPX_WOTS_LEN, ctx, pk_addr); +} diff --git a/src/crypto/pq/sphincsplus/wotsx1.h b/src/crypto/pq/sphincsplus/wotsx1.h new file mode 100644 index 0000000..1257f81 --- /dev/null +++ b/src/crypto/pq/sphincsplus/wotsx1.h @@ -0,0 +1,36 @@ +#if !defined( WOTSX1_H_ ) +#define WOTSX1_H_ + +#include + +/* + * This is here to provide an interface to the internal wots_gen_leafx1 + * routine. While this routine is not referenced in the package outside of + * wots.c, it is called from the stand-alone benchmark code to characterize + * the performance + */ +struct leaf_info_x1 { + unsigned char *wots_sig; + uint32_t wots_sign_leaf; /* The index of the WOTS we're using to sign */ + uint32_t *wots_steps; + uint32_t leaf_addr[8]; + uint32_t pk_addr[8]; +}; + +/* Macro to set the leaf_info to something 'benign', that is, it would */ +/* run with the same time as it does during the real signing process */ +/* Used only by the benchmark code */ +#define INITIALIZE_LEAF_INFO_X1(info, addr, step_buffer) { \ + info.wots_sig = 0; \ + info.wots_sign_leaf = ~0u; \ + info.wots_steps = step_buffer; \ + memcpy( &info.leaf_addr[0], addr, 32 ); \ + memcpy( &info.pk_addr[0], addr, 32 ); \ +} + +#define wots_gen_leafx1 SPX_NAMESPACE(wots_gen_leafx1) +void wots_gen_leafx1(unsigned char *dest, + const spx_ctx *ctx, + uint32_t leaf_idx, void *v_info); + +#endif /* WOTSX1_H_ */ diff --git a/src/deploymentinfo.cpp b/src/deploymentinfo.cpp index 466b2ca..a7220f3 100644 --- a/src/deploymentinfo.cpp +++ b/src/deploymentinfo.cpp @@ -14,6 +14,10 @@ const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_B /*.name =*/ "taproot", /*.gbt_force =*/ true, }, + { + /*.name =*/ "pqr", + /*.gbt_force =*/ true, + }, }; std::string DeploymentName(Consensus::BuriedDeployment dep) diff --git a/src/key_io.cpp b/src/key_io.cpp index 593e6cd..0b9fba5 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -63,6 +63,22 @@ public: return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data); } + std::string operator()(const WitnessV2MLDSA& id) const + { + std::vector data = {2}; + data.reserve(53); + ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end()); + return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data); + } + + std::string operator()(const WitnessV3SLHDSA& id) const + { + std::vector data = {3}; + data.reserve(53); + ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end()); + return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data); + } + std::string operator()(const WitnessUnknown& id) const { if (id.version < 1 || id.version > 16 || id.length < 2 || id.length > 40) { @@ -151,6 +167,18 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par return tap; } + if (version == 2 && data.size() == 32) { + WitnessV2MLDSA id; + std::copy(data.begin(), data.end(), id.begin()); + return id; + } + + if (version == 3 && data.size() == 32) { + WitnessV3SLHDSA id; + std::copy(data.begin(), data.end(), id.begin()); + return id; + } + if (version > 16) { error_str = "Invalid Bech32 address witness version"; return CNoDestination(); diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index d187e14..fbb0f06 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -312,6 +312,26 @@ public: return obj; } + UniValue operator()(const WitnessV2MLDSA& id) const + { + UniValue obj(UniValue::VOBJ); + obj.pushKV("isscript", false); + obj.pushKV("iswitness", true); + obj.pushKV("witness_version", 2); + obj.pushKV("witness_program", HexStr(id)); + return obj; + } + + UniValue operator()(const WitnessV3SLHDSA& id) const + { + UniValue obj(UniValue::VOBJ); + obj.pushKV("isscript", false); + obj.pushKV("iswitness", true); + obj.pushKV("witness_version", 3); + obj.pushKV("witness_program", HexStr(id)); + return obj; + } + UniValue operator()(const WitnessUnknown& id) const { UniValue obj(UniValue::VOBJ); diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index ff7fcb5..c44962b 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include