@onekeyfe/react-native-lite-card 6.7.0-4 → 6.7.0-41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -2
- package/ReactNativeLiteCard.podspec +31 -2
- package/android/build.gradle +8 -0
- package/android/consumer-rules.pro +4 -0
- package/android/src/main/cpp/CMakeLists.txt +6 -2
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +60 -49
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +117 -39
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +24 -2
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +46 -19
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
- package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +38 -1
- package/ios/Classes/OKNFCBridge.mm +24 -1
- package/ios/Classes/OKNFTLite/OKLiteV1.m +20 -10
- package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
- package/ios/Classes/OKNFTLite/OKNFCManager.h +2 -2
- package/ios/Classes/OKNFTLite/OKNFCManager.m +110 -18
- package/ios/ReactNativeLiteCard.mm +23 -11
- package/lib/module/index.js +4 -4
- package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +4 -4
- package/lib/typescript/src/index.d.ts +2 -2
- package/package.json +8 -8
- package/sdk/CMakeLists.txt +121 -0
- package/sdk/README.md +190 -0
- package/sdk/android/implJni.cpp +216 -0
- package/sdk/deps/jsoncpp/LICENSE +55 -0
- package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
- package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
- package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
- package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
- package/sdk/deps/jsoncpp/include/json/config.h +187 -0
- package/sdk/deps/jsoncpp/include/json/features.h +61 -0
- package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
- package/sdk/deps/jsoncpp/include/json/json.h +15 -0
- package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
- package/sdk/deps/jsoncpp/include/json/value.h +888 -0
- package/sdk/deps/jsoncpp/include/json/version.h +20 -0
- package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
- package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
- package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
- package/sdk/deps/trezor-crypto/AUTHORS +2 -0
- package/sdk/deps/trezor-crypto/LICENSE +22 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
- package/sdk/deps/trezor-crypto/src/address.c +87 -0
- package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
- package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
- package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
- package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
- package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
- package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
- package/sdk/deps/trezor-crypto/src/base32.c +233 -0
- package/sdk/deps/trezor-crypto/src/base58.c +298 -0
- package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
- package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
- package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
- package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
- package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
- package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
- package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
- package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
- package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
- package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
- package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
- package/sdk/deps/trezor-crypto/src/curves.c +48 -0
- package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
- package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
- package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
- package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
- package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
- package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
- package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
- package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
- package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
- package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
- package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
- package/sdk/deps/trezor-crypto/src/nano.c +151 -0
- package/sdk/deps/trezor-crypto/src/nem.c +569 -0
- package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
- package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
- package/sdk/deps/trezor-crypto/src/options.h +43 -0
- package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
- package/sdk/deps/trezor-crypto/src/rand.c +51 -0
- package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
- package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
- package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
- package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
- package/sdk/deps/trezor-crypto/src/script.c +64 -0
- package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
- package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
- package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
- package/sdk/deps/trezor-crypto/src/setup.py +38 -0
- package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
- package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
- package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
- package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
- package/sdk/gpc/include/scp03/scp03.hpp +80 -0
- package/sdk/gpc/include/scp11/scp11.hpp +492 -0
- package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
- package/sdk/gpc/include/tag.h +68 -0
- package/sdk/gpc/include/tlv.hpp +94 -0
- package/sdk/gpc/include/tlvparse.hpp +24 -0
- package/sdk/gpc/src/scp03.cpp +243 -0
- package/sdk/gpc/src/scp11.cpp +798 -0
- package/sdk/gpc/src/scp11c.cpp +136 -0
- package/sdk/gpc/src/tlv.cpp +159 -0
- package/sdk/gpc/src/tlvparse.c +174 -0
- package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
- package/sdk/include/GPChannelSDKCore.h +10 -0
- package/sdk/include/context/BaseContext.h +56 -0
- package/sdk/include/device/ApduBuilder.hpp +72 -0
- package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
- package/sdk/include/utility/Apdu.hpp +172 -0
- package/sdk/include/utility/Debug.hpp +3 -0
- package/sdk/include/utility/util.h +104 -0
- package/sdk/scripts/generate-source-id.rb +22 -0
- package/sdk/source-id-manifest.txt +19 -0
- package/sdk/source-id.cmake +31 -0
- package/sdk/src/ApduBuilder.cpp +332 -0
- package/sdk/src/GPChannelSDK.cpp +258 -0
- package/sdk/src/random.c +25 -0
- package/sdk/src/version.c +3 -0
- package/sdk/tests/android/GPChannelProxy.java +55 -0
- package/sdk/tests/android_verify.py +124 -0
- package/sdk/tests/safety.cpp +757 -0
- package/sdk/tests/verify.py +284 -0
- package/src/NativeReactNativeLiteCard.ts +4 -2
- package/src/index.tsx +14 -3
- package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
- package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <TrezorCrypto/ed25519-donna.h>
|
|
3
|
+
|
|
4
|
+
/* sqrt(x) is such an integer y that 0 <= y <= p - 1, y % 2 = 0, and y^2 = x (mod p). */
|
|
5
|
+
/* d = -121665 / 121666 */
|
|
6
|
+
#if !defined(NDEBUG)
|
|
7
|
+
static const bignum25519 ALIGN(16) fe_d = {
|
|
8
|
+
0x35978a3, 0x0d37284, 0x3156ebd, 0x06a0a0e, 0x001c029, 0x179e898, 0x3a03cbb, 0x1ce7198, 0x2e2b6ff, 0x1480db3}; /* d */
|
|
9
|
+
#endif
|
|
10
|
+
static const bignum25519 ALIGN(16) fe_sqrtm1 = {
|
|
11
|
+
0x20ea0b0, 0x186c9d2, 0x08f189d, 0x035697f, 0x0bd0c60, 0x1fbd7a7, 0x2804c9e, 0x1e16569, 0x004fc1d, 0x0ae0c92}; /* sqrt(-1) */
|
|
12
|
+
//static const bignum25519 ALIGN(16) fe_d2 = {
|
|
13
|
+
// 0x2b2f159, 0x1a6e509, 0x22add7a, 0x0d4141d, 0x0038052, 0x0f3d130, 0x3407977, 0x19ce331, 0x1c56dff, 0x0901b67}; /* 2 * d */
|
|
14
|
+
|
|
15
|
+
/* A = 2 * (1 - d) / (1 + d) = 486662 */
|
|
16
|
+
static const bignum25519 ALIGN(16) fe_ma2 = {
|
|
17
|
+
0x33de3c9, 0x1fff236, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff}; /* -A^2 */
|
|
18
|
+
static const bignum25519 ALIGN(16) fe_ma = {
|
|
19
|
+
0x3f892e7, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff}; /* -A */
|
|
20
|
+
static const bignum25519 ALIGN(16) fe_fffb1 = {
|
|
21
|
+
0x1e3bdff, 0x025a2b3, 0x18e5bab, 0x0ba36ac, 0x0b9afed, 0x004e61c, 0x31d645f, 0x09d1bea, 0x102529e, 0x0063810}; /* sqrt(-2 * A * (A + 2)) */
|
|
22
|
+
static const bignum25519 ALIGN(16) fe_fffb2 = {
|
|
23
|
+
0x383650d, 0x066df27, 0x10405a4, 0x1cfdd48, 0x2b887f2, 0x1e9a041, 0x1d7241f, 0x0612dc5, 0x35fba5d, 0x0cbe787}; /* sqrt(2 * A * (A + 2)) */
|
|
24
|
+
static const bignum25519 ALIGN(16) fe_fffb3 = {
|
|
25
|
+
0x0cfd387, 0x1209e3a, 0x3bad4fc, 0x18ad34d, 0x2ff6c02, 0x0f25d12, 0x15cdfe0, 0x0e208ed, 0x32eb3df, 0x062d7bb}; /* sqrt(-sqrt(-1) * A * (A + 2)) */
|
|
26
|
+
static const bignum25519 ALIGN(16) fe_fffb4 = {
|
|
27
|
+
0x2b39186, 0x14640ed, 0x14930a7, 0x04509fa, 0x3b91bf0, 0x0f7432e, 0x07a443f, 0x17f24d8, 0x031067d, 0x0690fcc}; /* sqrt(sqrt(-1) * A * (A + 2)) */
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
Timing safe memory compare
|
|
32
|
+
*/
|
|
33
|
+
int ed25519_verify(const unsigned char *x, const unsigned char *y, size_t len) {
|
|
34
|
+
size_t differentbits = 0;
|
|
35
|
+
while (len--)
|
|
36
|
+
differentbits |= (*x++ ^ *y++);
|
|
37
|
+
return (int) (1 & ((differentbits - 1) >> 8));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/*
|
|
41
|
+
conversions
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
void ge25519_p1p1_to_partial(ge25519 *r, const ge25519_p1p1 *p) {
|
|
45
|
+
curve25519_mul(r->x, p->x, p->t);
|
|
46
|
+
curve25519_mul(r->y, p->y, p->z);
|
|
47
|
+
curve25519_mul(r->z, p->z, p->t);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
void ge25519_p1p1_to_full(ge25519 *r, const ge25519_p1p1 *p) {
|
|
51
|
+
curve25519_mul(r->x, p->x, p->t);
|
|
52
|
+
curve25519_mul(r->y, p->y, p->z);
|
|
53
|
+
curve25519_mul(r->z, p->z, p->t);
|
|
54
|
+
curve25519_mul(r->t, p->x, p->y);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
void ge25519_full_to_pniels(ge25519_pniels *p, const ge25519 *r) {
|
|
58
|
+
curve25519_sub(p->ysubx, r->y, r->x);
|
|
59
|
+
curve25519_add(p->xaddy, r->y, r->x);
|
|
60
|
+
curve25519_copy(p->z, r->z);
|
|
61
|
+
curve25519_mul(p->t2d, r->t, ge25519_ec2d);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/*
|
|
65
|
+
adding & doubling
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
void ge25519_double_p1p1(ge25519_p1p1 *r, const ge25519 *p) {
|
|
69
|
+
bignum25519 a,b,c;
|
|
70
|
+
|
|
71
|
+
curve25519_square(a, p->x);
|
|
72
|
+
curve25519_square(b, p->y);
|
|
73
|
+
curve25519_square(c, p->z);
|
|
74
|
+
curve25519_add_reduce(c, c, c);
|
|
75
|
+
curve25519_add(r->x, p->x, p->y);
|
|
76
|
+
curve25519_square(r->x, r->x);
|
|
77
|
+
curve25519_add(r->y, b, a);
|
|
78
|
+
curve25519_sub(r->z, b, a);
|
|
79
|
+
curve25519_sub_after_basic(r->x, r->x, r->y);
|
|
80
|
+
curve25519_sub_after_basic(r->t, c, r->z);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#ifndef ED25519_NO_PRECOMP
|
|
84
|
+
void ge25519_nielsadd2_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519_niels *q, unsigned char signbit) {
|
|
85
|
+
const bignum25519 *qb = (const bignum25519 *)q;
|
|
86
|
+
bignum25519 *rb = (bignum25519 *)r;
|
|
87
|
+
bignum25519 a,b,c;
|
|
88
|
+
|
|
89
|
+
curve25519_sub(a, p->y, p->x);
|
|
90
|
+
curve25519_add(b, p->y, p->x);
|
|
91
|
+
curve25519_mul(a, a, qb[signbit]); /* x for +, y for - */
|
|
92
|
+
curve25519_mul(r->x, b, qb[signbit^1]); /* y for +, x for - */
|
|
93
|
+
curve25519_add(r->y, r->x, a);
|
|
94
|
+
curve25519_sub(r->x, r->x, a);
|
|
95
|
+
curve25519_mul(c, p->t, q->t2d);
|
|
96
|
+
curve25519_add_reduce(r->t, p->z, p->z);
|
|
97
|
+
curve25519_copy(r->z, r->t);
|
|
98
|
+
curve25519_add(rb[2+signbit], rb[2+signbit], c); /* z for +, t for - */
|
|
99
|
+
curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c); /* t for +, z for - */
|
|
100
|
+
}
|
|
101
|
+
#endif
|
|
102
|
+
|
|
103
|
+
void ge25519_pnielsadd_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519_pniels *q, unsigned char signbit) {
|
|
104
|
+
const bignum25519 *qb = (const bignum25519 *)q;
|
|
105
|
+
bignum25519 *rb = (bignum25519 *)r;
|
|
106
|
+
bignum25519 a,b,c;
|
|
107
|
+
|
|
108
|
+
curve25519_sub(a, p->y, p->x);
|
|
109
|
+
curve25519_add(b, p->y, p->x);
|
|
110
|
+
curve25519_mul(a, a, qb[signbit]); /* ysubx for +, xaddy for - */
|
|
111
|
+
curve25519_mul(r->x, b, qb[signbit^1]); /* xaddy for +, ysubx for - */
|
|
112
|
+
curve25519_add(r->y, r->x, a);
|
|
113
|
+
curve25519_sub(r->x, r->x, a);
|
|
114
|
+
curve25519_mul(c, p->t, q->t2d);
|
|
115
|
+
curve25519_mul(r->t, p->z, q->z);
|
|
116
|
+
curve25519_add_reduce(r->t, r->t, r->t);
|
|
117
|
+
curve25519_copy(r->z, r->t);
|
|
118
|
+
curve25519_add(rb[2+signbit], rb[2+signbit], c); /* z for +, t for - */
|
|
119
|
+
curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c); /* t for +, z for - */
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
void ge25519_double_partial(ge25519 *r, const ge25519 *p) {
|
|
123
|
+
ge25519_p1p1 t;
|
|
124
|
+
ge25519_double_p1p1(&t, p);
|
|
125
|
+
ge25519_p1p1_to_partial(r, &t);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
void ge25519_double(ge25519 *r, const ge25519 *p) {
|
|
129
|
+
ge25519_p1p1 t;
|
|
130
|
+
ge25519_double_p1p1(&t, p);
|
|
131
|
+
ge25519_p1p1_to_full(r, &t);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
void ge25519_nielsadd2(ge25519 *r, const ge25519_niels *q) {
|
|
135
|
+
bignum25519 a,b,c,e,f,g,h;
|
|
136
|
+
|
|
137
|
+
curve25519_sub(a, r->y, r->x);
|
|
138
|
+
curve25519_add(b, r->y, r->x);
|
|
139
|
+
curve25519_mul(a, a, q->ysubx);
|
|
140
|
+
curve25519_mul(e, b, q->xaddy);
|
|
141
|
+
curve25519_add(h, e, a);
|
|
142
|
+
curve25519_sub(e, e, a);
|
|
143
|
+
curve25519_mul(c, r->t, q->t2d);
|
|
144
|
+
curve25519_add(f, r->z, r->z);
|
|
145
|
+
curve25519_add_after_basic(g, f, c);
|
|
146
|
+
curve25519_sub_after_basic(f, f, c);
|
|
147
|
+
curve25519_mul(r->x, e, f);
|
|
148
|
+
curve25519_mul(r->y, h, g);
|
|
149
|
+
curve25519_mul(r->z, g, f);
|
|
150
|
+
curve25519_mul(r->t, e, h);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
void ge25519_pnielsadd(ge25519_pniels *r, const ge25519 *p, const ge25519_pniels *q) {
|
|
154
|
+
bignum25519 a,b,c,x,y,z,t;
|
|
155
|
+
|
|
156
|
+
curve25519_sub(a, p->y, p->x);
|
|
157
|
+
curve25519_add(b, p->y, p->x);
|
|
158
|
+
curve25519_mul(a, a, q->ysubx);
|
|
159
|
+
curve25519_mul(x, b, q->xaddy);
|
|
160
|
+
curve25519_add(y, x, a);
|
|
161
|
+
curve25519_sub(x, x, a);
|
|
162
|
+
curve25519_mul(c, p->t, q->t2d);
|
|
163
|
+
curve25519_mul(t, p->z, q->z);
|
|
164
|
+
curve25519_add(t, t, t);
|
|
165
|
+
curve25519_add_after_basic(z, t, c);
|
|
166
|
+
curve25519_sub_after_basic(t, t, c);
|
|
167
|
+
curve25519_mul(r->xaddy, x, t);
|
|
168
|
+
curve25519_mul(r->ysubx, y, z);
|
|
169
|
+
curve25519_mul(r->z, z, t);
|
|
170
|
+
curve25519_mul(r->t2d, x, y);
|
|
171
|
+
curve25519_copy(y, r->ysubx);
|
|
172
|
+
curve25519_sub(r->ysubx, r->ysubx, r->xaddy);
|
|
173
|
+
curve25519_add(r->xaddy, r->xaddy, y);
|
|
174
|
+
curve25519_mul(r->t2d, r->t2d, ge25519_ec2d);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
/*
|
|
179
|
+
pack & unpack
|
|
180
|
+
*/
|
|
181
|
+
|
|
182
|
+
void ge25519_pack(unsigned char r[32], const ge25519 *p) {
|
|
183
|
+
bignum25519 tx, ty, zi;
|
|
184
|
+
unsigned char parity[32];
|
|
185
|
+
curve25519_recip(zi, p->z);
|
|
186
|
+
curve25519_mul(tx, p->x, zi);
|
|
187
|
+
curve25519_mul(ty, p->y, zi);
|
|
188
|
+
curve25519_contract(r, ty);
|
|
189
|
+
curve25519_contract(parity, tx);
|
|
190
|
+
r[31] ^= ((parity[0] & 1) << 7);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
int ge25519_unpack_negative_vartime(ge25519 *r, const unsigned char p[32]) {
|
|
194
|
+
const unsigned char zero[32] = {0};
|
|
195
|
+
const bignum25519 one = {1};
|
|
196
|
+
unsigned char parity = p[31] >> 7;
|
|
197
|
+
unsigned char check[32];
|
|
198
|
+
bignum25519 t, root, num, den, d3;
|
|
199
|
+
|
|
200
|
+
curve25519_expand(r->y, p);
|
|
201
|
+
curve25519_copy(r->z, one);
|
|
202
|
+
curve25519_square(num, r->y); /* x = y^2 */
|
|
203
|
+
curve25519_mul(den, num, ge25519_ecd); /* den = dy^2 */
|
|
204
|
+
curve25519_sub_reduce(num, num, r->z); /* x = y^1 - 1 */
|
|
205
|
+
curve25519_add(den, den, r->z); /* den = dy^2 + 1 */
|
|
206
|
+
|
|
207
|
+
/* Computation of sqrt(num/den) */
|
|
208
|
+
/* 1.: computation of num^((p-5)/8)*den^((7p-35)/8) = (num*den^7)^((p-5)/8) */
|
|
209
|
+
curve25519_square(t, den);
|
|
210
|
+
curve25519_mul(d3, t, den);
|
|
211
|
+
curve25519_square(r->x, d3);
|
|
212
|
+
curve25519_mul(r->x, r->x, den);
|
|
213
|
+
curve25519_mul(r->x, r->x, num);
|
|
214
|
+
curve25519_pow_two252m3(r->x, r->x);
|
|
215
|
+
|
|
216
|
+
/* 2. computation of r->x = num * den^3 * (num*den^7)^((p-5)/8) */
|
|
217
|
+
curve25519_mul(r->x, r->x, d3);
|
|
218
|
+
curve25519_mul(r->x, r->x, num);
|
|
219
|
+
|
|
220
|
+
/* 3. Check if either of the roots works: */
|
|
221
|
+
curve25519_square(t, r->x);
|
|
222
|
+
curve25519_mul(t, t, den);
|
|
223
|
+
curve25519_sub_reduce(root, t, num);
|
|
224
|
+
curve25519_contract(check, root);
|
|
225
|
+
if (!ed25519_verify(check, zero, 32)) {
|
|
226
|
+
curve25519_add_reduce(t, t, num);
|
|
227
|
+
curve25519_contract(check, t);
|
|
228
|
+
if (!ed25519_verify(check, zero, 32))
|
|
229
|
+
return 0;
|
|
230
|
+
curve25519_mul(r->x, r->x, ge25519_sqrtneg1);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
curve25519_contract(check, r->x);
|
|
234
|
+
if ((check[0] & 1) == parity) {
|
|
235
|
+
curve25519_copy(t, r->x);
|
|
236
|
+
curve25519_neg(r->x, t);
|
|
237
|
+
}
|
|
238
|
+
curve25519_mul(r->t, r->x, r->y);
|
|
239
|
+
return 1;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/*
|
|
243
|
+
scalarmults
|
|
244
|
+
*/
|
|
245
|
+
|
|
246
|
+
void ge25519_set_neutral(ge25519 *r)
|
|
247
|
+
{
|
|
248
|
+
memset(r, 0, sizeof(ge25519));
|
|
249
|
+
r->y[0] = 1;
|
|
250
|
+
r->z[0] = 1;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
#define S1_SWINDOWSIZE 5
|
|
254
|
+
#define S1_TABLE_SIZE (1<<(S1_SWINDOWSIZE-2))
|
|
255
|
+
#ifdef ED25519_NO_PRECOMP
|
|
256
|
+
#define S2_SWINDOWSIZE 5
|
|
257
|
+
#else
|
|
258
|
+
#define S2_SWINDOWSIZE 7
|
|
259
|
+
#endif
|
|
260
|
+
#define S2_TABLE_SIZE (1<<(S2_SWINDOWSIZE-2))
|
|
261
|
+
|
|
262
|
+
/* computes [s1]p1 + [s2]base */
|
|
263
|
+
void ge25519_double_scalarmult_vartime(ge25519 *r, const ge25519 *p1, const bignum256modm s1, const bignum256modm s2) {
|
|
264
|
+
signed char slide1[256], slide2[256];
|
|
265
|
+
ge25519_pniels pre1[S1_TABLE_SIZE];
|
|
266
|
+
#ifdef ED25519_NO_PRECOMP
|
|
267
|
+
ge25519_pniels pre2[S2_TABLE_SIZE];
|
|
268
|
+
#endif
|
|
269
|
+
ge25519 dp;
|
|
270
|
+
ge25519_p1p1 t;
|
|
271
|
+
int32_t i;
|
|
272
|
+
|
|
273
|
+
memset(&t, 0, sizeof(ge25519_p1p1));
|
|
274
|
+
contract256_slidingwindow_modm(slide1, s1, S1_SWINDOWSIZE);
|
|
275
|
+
contract256_slidingwindow_modm(slide2, s2, S2_SWINDOWSIZE);
|
|
276
|
+
|
|
277
|
+
ge25519_double(&dp, p1);
|
|
278
|
+
ge25519_full_to_pniels(pre1, p1);
|
|
279
|
+
for (i = 0; i < S1_TABLE_SIZE - 1; i++)
|
|
280
|
+
ge25519_pnielsadd(&pre1[i+1], &dp, &pre1[i]);
|
|
281
|
+
|
|
282
|
+
#ifdef ED25519_NO_PRECOMP
|
|
283
|
+
ge25519_double(&dp, &ge25519_basepoint);
|
|
284
|
+
ge25519_full_to_pniels(pre2, &ge25519_basepoint);
|
|
285
|
+
for (i = 0; i < S2_TABLE_SIZE - 1; i++)
|
|
286
|
+
ge25519_pnielsadd(&pre2[i+1], &dp, &pre2[i]);
|
|
287
|
+
#endif
|
|
288
|
+
|
|
289
|
+
ge25519_set_neutral(r);
|
|
290
|
+
|
|
291
|
+
i = 255;
|
|
292
|
+
while ((i >= 0) && !(slide1[i] | slide2[i]))
|
|
293
|
+
i--;
|
|
294
|
+
|
|
295
|
+
for (; i >= 0; i--) {
|
|
296
|
+
ge25519_double_p1p1(&t, r);
|
|
297
|
+
|
|
298
|
+
if (slide1[i]) {
|
|
299
|
+
ge25519_p1p1_to_full(r, &t);
|
|
300
|
+
ge25519_pnielsadd_p1p1(&t, r, &pre1[abs(slide1[i]) / 2], (unsigned char)slide1[i] >> 7);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (slide2[i]) {
|
|
304
|
+
ge25519_p1p1_to_full(r, &t);
|
|
305
|
+
#ifdef ED25519_NO_PRECOMP
|
|
306
|
+
ge25519_pnielsadd_p1p1(&t, r, &pre2[abs(slide2[i]) / 2], (unsigned char)slide2[i] >> 7);
|
|
307
|
+
#else
|
|
308
|
+
ge25519_nielsadd2_p1p1(&t, r, &ge25519_niels_sliding_multiples[abs(slide2[i]) / 2], (unsigned char)slide2[i] >> 7);
|
|
309
|
+
#endif
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
ge25519_p1p1_to_partial(r, &t);
|
|
313
|
+
}
|
|
314
|
+
curve25519_mul(r->t, t.x, t.y);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/* computes [s1]p1 + [s2]p2 */
|
|
318
|
+
void ge25519_double_scalarmult_vartime2(ge25519 *r, const ge25519 *p1, const bignum256modm s1, const ge25519 *p2, const bignum256modm s2) {
|
|
319
|
+
signed char slide1[256], slide2[256];
|
|
320
|
+
ge25519_pniels pre1[S1_TABLE_SIZE];
|
|
321
|
+
ge25519_pniels pre2[S1_TABLE_SIZE];
|
|
322
|
+
ge25519 dp;
|
|
323
|
+
ge25519_p1p1 t;
|
|
324
|
+
int32_t i;
|
|
325
|
+
|
|
326
|
+
memset(&t, 0, sizeof(ge25519_p1p1));
|
|
327
|
+
contract256_slidingwindow_modm(slide1, s1, S1_SWINDOWSIZE);
|
|
328
|
+
contract256_slidingwindow_modm(slide2, s2, S1_SWINDOWSIZE);
|
|
329
|
+
|
|
330
|
+
ge25519_double(&dp, p1);
|
|
331
|
+
ge25519_full_to_pniels(pre1, p1);
|
|
332
|
+
for (i = 0; i < S1_TABLE_SIZE - 1; i++)
|
|
333
|
+
ge25519_pnielsadd(&pre1[i+1], &dp, &pre1[i]);
|
|
334
|
+
|
|
335
|
+
ge25519_double(&dp, p2);
|
|
336
|
+
ge25519_full_to_pniels(pre2, p2);
|
|
337
|
+
for (i = 0; i < S1_TABLE_SIZE - 1; i++)
|
|
338
|
+
ge25519_pnielsadd(&pre2[i+1], &dp, &pre2[i]);
|
|
339
|
+
|
|
340
|
+
ge25519_set_neutral(r);
|
|
341
|
+
|
|
342
|
+
i = 255;
|
|
343
|
+
while ((i >= 0) && !(slide1[i] | slide2[i]))
|
|
344
|
+
i--;
|
|
345
|
+
|
|
346
|
+
for (; i >= 0; i--) {
|
|
347
|
+
ge25519_double_p1p1(&t, r);
|
|
348
|
+
|
|
349
|
+
if (slide1[i]) {
|
|
350
|
+
ge25519_p1p1_to_full(r, &t);
|
|
351
|
+
ge25519_pnielsadd_p1p1(&t, r, &pre1[abs(slide1[i]) / 2], (unsigned char)slide1[i] >> 7);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (slide2[i]) {
|
|
355
|
+
ge25519_p1p1_to_full(r, &t);
|
|
356
|
+
ge25519_pnielsadd_p1p1(&t, r, &pre2[abs(slide2[i]) / 2], (unsigned char)slide2[i] >> 7);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
ge25519_p1p1_to_partial(r, &t);
|
|
360
|
+
}
|
|
361
|
+
curve25519_mul(r->t, t.x, t.y);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/*
|
|
365
|
+
* The following conditional move stuff uses conditional moves.
|
|
366
|
+
* I will check on which compilers this works, and provide suitable
|
|
367
|
+
* workarounds for those where it doesn't.
|
|
368
|
+
*
|
|
369
|
+
* This works on gcc 4.x and above with -O3. Don't use -O2, this will
|
|
370
|
+
* cause the code to not generate conditional moves. Don't use any -march=
|
|
371
|
+
* with less than i686 on x86
|
|
372
|
+
*/
|
|
373
|
+
static void ge25519_cmove_stride4(long * r, long * p, long * pos, long * n, int stride) {
|
|
374
|
+
long x0=r[0], x1=r[1], x2=r[2], x3=r[3], y0, y1, y2, y3;
|
|
375
|
+
for(; p<n; p+=stride) {
|
|
376
|
+
int flag=(p==pos);
|
|
377
|
+
y0 = p[0];
|
|
378
|
+
y1 = p[1];
|
|
379
|
+
y2 = p[2];
|
|
380
|
+
y3 = p[3];
|
|
381
|
+
x0 = flag ? y0 : x0;
|
|
382
|
+
x1 = flag ? y1 : x1;
|
|
383
|
+
x2 = flag ? y2 : x2;
|
|
384
|
+
x3 = flag ? y3 : x3;
|
|
385
|
+
}
|
|
386
|
+
r[0] = x0;
|
|
387
|
+
r[1] = x1;
|
|
388
|
+
r[2] = x2;
|
|
389
|
+
r[3] = x3;
|
|
390
|
+
}
|
|
391
|
+
#define HAS_CMOVE_STRIDE4
|
|
392
|
+
|
|
393
|
+
static void ge25519_cmove_stride4b(long * r, long * p, long * pos, long * n, int stride) {
|
|
394
|
+
long x0=p[0], x1=p[1], x2=p[2], x3=p[3], y0, y1, y2, y3;
|
|
395
|
+
for(p+=stride; p<n; p+=stride) {
|
|
396
|
+
int flag=(p==pos);
|
|
397
|
+
y0 = p[0];
|
|
398
|
+
y1 = p[1];
|
|
399
|
+
y2 = p[2];
|
|
400
|
+
y3 = p[3];
|
|
401
|
+
x0 = flag ? y0 : x0;
|
|
402
|
+
x1 = flag ? y1 : x1;
|
|
403
|
+
x2 = flag ? y2 : x2;
|
|
404
|
+
x3 = flag ? y3 : x3;
|
|
405
|
+
}
|
|
406
|
+
r[0] = x0;
|
|
407
|
+
r[1] = x1;
|
|
408
|
+
r[2] = x2;
|
|
409
|
+
r[3] = x3;
|
|
410
|
+
}
|
|
411
|
+
#define HAS_CMOVE_STRIDE4B
|
|
412
|
+
|
|
413
|
+
void ge25519_move_conditional_pniels_array(ge25519_pniels * r, const ge25519_pniels * p, int pos, int n) {
|
|
414
|
+
#ifdef HAS_CMOVE_STRIDE4B
|
|
415
|
+
size_t i;
|
|
416
|
+
for(i=0; i<sizeof(ge25519_pniels)/sizeof(long); i+=4) {
|
|
417
|
+
ge25519_cmove_stride4b(((long*)r)+i,
|
|
418
|
+
((long*)p)+i,
|
|
419
|
+
((long*)(p+pos))+i,
|
|
420
|
+
((long*)(p+n))+i,
|
|
421
|
+
sizeof(ge25519_pniels)/sizeof(long));
|
|
422
|
+
}
|
|
423
|
+
#else
|
|
424
|
+
size_t i;
|
|
425
|
+
for(i=0; i<n; i++) {
|
|
426
|
+
ge25519_move_conditional_pniels(r, p+i, pos==i);
|
|
427
|
+
}
|
|
428
|
+
#endif
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
void ge25519_move_conditional_niels_array(ge25519_niels * r, const uint8_t p[8][96], int pos, int n) {
|
|
432
|
+
size_t i;
|
|
433
|
+
for(i=0; i<96/sizeof(long); i+=4) {
|
|
434
|
+
ge25519_cmove_stride4(((long*)r)+i,
|
|
435
|
+
((long*)p)+i,
|
|
436
|
+
((long*)(p+pos))+i,
|
|
437
|
+
((long*)(p+n))+i,
|
|
438
|
+
96/sizeof(long));
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/* computes [s1]p1, constant time */
|
|
443
|
+
void ge25519_scalarmult(ge25519 *r, const ge25519 *p1, const bignum256modm s1) {
|
|
444
|
+
signed char slide1[64];
|
|
445
|
+
ge25519_pniels pre1[9];
|
|
446
|
+
ge25519_pniels pre;
|
|
447
|
+
ge25519 d1;
|
|
448
|
+
ge25519_p1p1 t;
|
|
449
|
+
int32_t i;
|
|
450
|
+
|
|
451
|
+
contract256_window4_modm(slide1, s1);
|
|
452
|
+
|
|
453
|
+
ge25519_full_to_pniels(pre1+1, p1);
|
|
454
|
+
ge25519_double(&d1, p1);
|
|
455
|
+
|
|
456
|
+
ge25519_set_neutral(r);
|
|
457
|
+
ge25519_full_to_pniels(pre1, r);
|
|
458
|
+
|
|
459
|
+
ge25519_full_to_pniels(pre1+2, &d1);
|
|
460
|
+
for (i = 1; i < 7; i++) {
|
|
461
|
+
ge25519_pnielsadd(&pre1[i+2], &d1, &pre1[i]);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
for (i = 63; i >= 0; i--) {
|
|
465
|
+
int k=abs(slide1[i]);
|
|
466
|
+
ge25519_double_partial(r, r);
|
|
467
|
+
ge25519_double_partial(r, r);
|
|
468
|
+
ge25519_double_partial(r, r);
|
|
469
|
+
ge25519_double_p1p1(&t, r);
|
|
470
|
+
ge25519_move_conditional_pniels_array(&pre, pre1, k, 9);
|
|
471
|
+
ge25519_p1p1_to_full(r, &t);
|
|
472
|
+
ge25519_pnielsadd_p1p1(&t, r, &pre, (unsigned char)slide1[i] >> 7);
|
|
473
|
+
ge25519_p1p1_to_partial(r, &t);
|
|
474
|
+
}
|
|
475
|
+
curve25519_mul(r->t, t.x, t.y);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
void ge25519_scalarmult_base_choose_niels(ge25519_niels *t, const uint8_t table[256][96], uint32_t pos, signed char b) {
|
|
479
|
+
bignum25519 neg;
|
|
480
|
+
uint32_t sign = (uint32_t)((unsigned char)b >> 7);
|
|
481
|
+
uint32_t mask = ~(sign - 1);
|
|
482
|
+
uint32_t u = (b + mask) ^ mask;
|
|
483
|
+
|
|
484
|
+
/* ysubx, xaddy, t2d in packed form. initialize to ysubx = 1, xaddy = 1, t2d = 0 */
|
|
485
|
+
uint8_t packed[96] = {0};
|
|
486
|
+
packed[0] = 1;
|
|
487
|
+
packed[32] = 1;
|
|
488
|
+
|
|
489
|
+
ge25519_move_conditional_niels_array((ge25519_niels *)packed, &table[pos*8], u-1, 8);
|
|
490
|
+
|
|
491
|
+
/* expand in to t */
|
|
492
|
+
curve25519_expand(t->ysubx, packed + 0);
|
|
493
|
+
curve25519_expand(t->xaddy, packed + 32);
|
|
494
|
+
curve25519_expand(t->t2d , packed + 64);
|
|
495
|
+
|
|
496
|
+
/* adjust for sign */
|
|
497
|
+
curve25519_swap_conditional(t->ysubx, t->xaddy, sign);
|
|
498
|
+
curve25519_neg(neg, t->t2d);
|
|
499
|
+
curve25519_swap_conditional(t->t2d, neg, sign);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/* computes [s]basepoint */
|
|
503
|
+
void ge25519_scalarmult_base_niels(ge25519 *r, const uint8_t basepoint_table[256][96], const bignum256modm s) {
|
|
504
|
+
signed char b[64];
|
|
505
|
+
uint32_t i;
|
|
506
|
+
ge25519_niels t;
|
|
507
|
+
|
|
508
|
+
contract256_window4_modm(b, s);
|
|
509
|
+
|
|
510
|
+
ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[1]);
|
|
511
|
+
curve25519_sub_reduce(r->x, t.xaddy, t.ysubx);
|
|
512
|
+
curve25519_add_reduce(r->y, t.xaddy, t.ysubx);
|
|
513
|
+
memset(r->z, 0, sizeof(bignum25519));
|
|
514
|
+
curve25519_copy(r->t, t.t2d);
|
|
515
|
+
r->z[0] = 2;
|
|
516
|
+
for (i = 3; i < 64; i += 2) {
|
|
517
|
+
ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
|
|
518
|
+
ge25519_nielsadd2(r, &t);
|
|
519
|
+
}
|
|
520
|
+
ge25519_double_partial(r, r);
|
|
521
|
+
ge25519_double_partial(r, r);
|
|
522
|
+
ge25519_double_partial(r, r);
|
|
523
|
+
ge25519_double(r, r);
|
|
524
|
+
ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[0]);
|
|
525
|
+
curve25519_mul(t.t2d, t.t2d, ge25519_ecd);
|
|
526
|
+
ge25519_nielsadd2(r, &t);
|
|
527
|
+
for(i = 2; i < 64; i += 2) {
|
|
528
|
+
ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
|
|
529
|
+
ge25519_nielsadd2(r, &t);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
int ge25519_check(const ge25519 *r){
|
|
534
|
+
/* return (z % q != 0 and
|
|
535
|
+
x * y % q == z * t % q and
|
|
536
|
+
(y * y - x * x - z * z - ed25519.d * t * t) % q == 0)
|
|
537
|
+
*/
|
|
538
|
+
|
|
539
|
+
bignum25519 z={0}, lhs={0}, rhs={0}, tmp={0}, res={0};
|
|
540
|
+
curve25519_reduce(z, r->z);
|
|
541
|
+
|
|
542
|
+
curve25519_mul(lhs, r->x, r->y);
|
|
543
|
+
curve25519_mul(rhs, r->z, r->t);
|
|
544
|
+
curve25519_sub_reduce(lhs, lhs, rhs);
|
|
545
|
+
|
|
546
|
+
curve25519_square(res, r->y);
|
|
547
|
+
curve25519_square(tmp, r->x);
|
|
548
|
+
curve25519_sub_reduce(res, res, tmp);
|
|
549
|
+
curve25519_square(tmp, r->z);
|
|
550
|
+
curve25519_sub_reduce(res, res, tmp);
|
|
551
|
+
curve25519_square(tmp, r->t);
|
|
552
|
+
curve25519_mul(tmp, tmp, ge25519_ecd);
|
|
553
|
+
curve25519_sub_reduce(res, res, tmp);
|
|
554
|
+
|
|
555
|
+
const int c1 = curve25519_isnonzero(z);
|
|
556
|
+
const int c2 = curve25519_isnonzero(lhs);
|
|
557
|
+
const int c3 = curve25519_isnonzero(res);
|
|
558
|
+
return c1 & (c2^0x1) & (c3^0x1);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
int ge25519_eq(const ge25519 *a, const ge25519 *b){
|
|
562
|
+
int eq = 1;
|
|
563
|
+
bignum25519 t1={0}, t2={0};
|
|
564
|
+
|
|
565
|
+
eq &= ge25519_check(a);
|
|
566
|
+
eq &= ge25519_check(b);
|
|
567
|
+
|
|
568
|
+
curve25519_mul(t1, a->x, b->z);
|
|
569
|
+
curve25519_mul(t2, b->x, a->z);
|
|
570
|
+
curve25519_sub_reduce(t1, t1, t2);
|
|
571
|
+
eq &= curve25519_isnonzero(t1) ^ 1;
|
|
572
|
+
|
|
573
|
+
curve25519_mul(t1, a->y, b->z);
|
|
574
|
+
curve25519_mul(t2, b->y, a->z);
|
|
575
|
+
curve25519_sub_reduce(t1, t1, t2);
|
|
576
|
+
eq &= curve25519_isnonzero(t1) ^ 1;
|
|
577
|
+
|
|
578
|
+
return eq;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
void ge25519_copy(ge25519 *dst, const ge25519 *src){
|
|
582
|
+
curve25519_copy(dst->x, src->x);
|
|
583
|
+
curve25519_copy(dst->y, src->y);
|
|
584
|
+
curve25519_copy(dst->z, src->z);
|
|
585
|
+
curve25519_copy(dst->t, src->t);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
void ge25519_set_base(ge25519 *r){
|
|
589
|
+
ge25519_copy(r, &ge25519_basepoint);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
void ge25519_mul8(ge25519 *r, const ge25519 *t) {
|
|
593
|
+
ge25519_double_partial(r, t);
|
|
594
|
+
ge25519_double_partial(r, r);
|
|
595
|
+
ge25519_double(r, r);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
void ge25519_neg_partial(ge25519 *r){
|
|
599
|
+
curve25519_neg(r->x, r->x);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
void ge25519_neg_full(ge25519 *r){
|
|
603
|
+
curve25519_neg(r->x, r->x);
|
|
604
|
+
curve25519_neg(r->t, r->t);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
void ge25519_reduce(ge25519 *r, const ge25519 *t){
|
|
608
|
+
curve25519_reduce(r->x, t->x);
|
|
609
|
+
curve25519_reduce(r->y, t->y);
|
|
610
|
+
curve25519_reduce(r->z, t->z);
|
|
611
|
+
curve25519_reduce(r->t, t->t);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
void ge25519_norm(ge25519 *r, const ge25519 * t){
|
|
615
|
+
bignum25519 zinv;
|
|
616
|
+
curve25519_recip(zinv, t->z);
|
|
617
|
+
curve25519_mul(r->x, t->x, zinv);
|
|
618
|
+
curve25519_mul(r->y, t->y, zinv);
|
|
619
|
+
curve25519_mul(r->t, r->x, r->y);
|
|
620
|
+
curve25519_set(r->z, 1);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
void ge25519_add(ge25519 *r, const ge25519 *p, const ge25519 *q, unsigned char signbit) {
|
|
624
|
+
ge25519_pniels P_ni;
|
|
625
|
+
ge25519_p1p1 P_11;
|
|
626
|
+
|
|
627
|
+
ge25519_full_to_pniels(&P_ni, q);
|
|
628
|
+
ge25519_pnielsadd_p1p1(&P_11, p, &P_ni, signbit);
|
|
629
|
+
ge25519_p1p1_to_full(r, &P_11);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
void ge25519_fromfe_frombytes_vartime(ge25519 *r, const unsigned char *s){
|
|
633
|
+
bignum25519 u={0}, v={0}, w={0}, x={0}, y={0}, z={0};
|
|
634
|
+
unsigned char sign;
|
|
635
|
+
|
|
636
|
+
curve25519_expand_reduce(u, s);
|
|
637
|
+
|
|
638
|
+
curve25519_square(v, u);
|
|
639
|
+
curve25519_add_reduce(v, v, v); /* 2 * u^2 */
|
|
640
|
+
curve25519_set(w, 1);
|
|
641
|
+
curve25519_add_reduce(w, v, w); /* w = 2 * u^2 + 1 */
|
|
642
|
+
|
|
643
|
+
curve25519_square(x, w); /* w^2 */
|
|
644
|
+
curve25519_mul(y, fe_ma2, v); /* -2 * A^2 * u^2 */
|
|
645
|
+
curve25519_add_reduce(x, x, y); /* x = w^2 - 2 * A^2 * u^2 */
|
|
646
|
+
|
|
647
|
+
curve25519_divpowm1(r->x, w, x); /* (w / x)^(m + 1) */
|
|
648
|
+
curve25519_square(y, r->x);
|
|
649
|
+
curve25519_mul(x, y, x);
|
|
650
|
+
curve25519_sub_reduce(y, w, x);
|
|
651
|
+
curve25519_copy(z, fe_ma);
|
|
652
|
+
|
|
653
|
+
if (curve25519_isnonzero(y)) {
|
|
654
|
+
curve25519_add_reduce(y, w, x);
|
|
655
|
+
if (curve25519_isnonzero(y)) {
|
|
656
|
+
goto negative;
|
|
657
|
+
} else {
|
|
658
|
+
curve25519_mul(r->x, r->x, fe_fffb1);
|
|
659
|
+
}
|
|
660
|
+
} else {
|
|
661
|
+
curve25519_mul(r->x, r->x, fe_fffb2);
|
|
662
|
+
}
|
|
663
|
+
curve25519_mul(r->x, r->x, u); /* u * sqrt(2 * A * (A + 2) * w / x) */
|
|
664
|
+
curve25519_mul(z, z, v); /* -2 * A * u^2 */
|
|
665
|
+
sign = 0;
|
|
666
|
+
goto setsign;
|
|
667
|
+
negative:
|
|
668
|
+
curve25519_mul(x, x, fe_sqrtm1);
|
|
669
|
+
curve25519_sub_reduce(y, w, x);
|
|
670
|
+
if (curve25519_isnonzero(y)) {
|
|
671
|
+
assert((curve25519_add_reduce(y, w, x), !curve25519_isnonzero(y)));
|
|
672
|
+
curve25519_mul(r->x, r->x, fe_fffb3);
|
|
673
|
+
} else {
|
|
674
|
+
curve25519_mul(r->x, r->x, fe_fffb4);
|
|
675
|
+
}
|
|
676
|
+
/* r->x = sqrt(A * (A + 2) * w / x) */
|
|
677
|
+
/* z = -A */
|
|
678
|
+
sign = 1;
|
|
679
|
+
setsign:
|
|
680
|
+
if (curve25519_isnegative(r->x) != sign) {
|
|
681
|
+
assert(curve25519_isnonzero(r->x));
|
|
682
|
+
curve25519_neg(r->x, r->x);
|
|
683
|
+
}
|
|
684
|
+
curve25519_add_reduce(r->z, z, w);
|
|
685
|
+
curve25519_sub_reduce(r->y, z, w);
|
|
686
|
+
curve25519_mul(r->x, r->x, r->z);
|
|
687
|
+
|
|
688
|
+
// Partial form, saving from T coord computation .
|
|
689
|
+
// Later is mul8 discarding T anyway.
|
|
690
|
+
// rt = ((rx * ry % q) * inv(rz)) % q
|
|
691
|
+
// curve25519_mul(x, r->x, r->y);
|
|
692
|
+
// curve25519_recip(z, r->z);
|
|
693
|
+
// curve25519_mul(r->t, x, z);
|
|
694
|
+
|
|
695
|
+
#if !defined(NDEBUG)
|
|
696
|
+
{
|
|
697
|
+
bignum25519 check_x={0}, check_y={0}, check_iz={0}, check_v={0};
|
|
698
|
+
curve25519_recip(check_iz, r->z);
|
|
699
|
+
curve25519_mul(check_x, r->x, check_iz);
|
|
700
|
+
curve25519_mul(check_y, r->y, check_iz);
|
|
701
|
+
curve25519_square(check_x, check_x);
|
|
702
|
+
curve25519_square(check_y, check_y);
|
|
703
|
+
curve25519_mul(check_v, check_x, check_y);
|
|
704
|
+
curve25519_mul(check_v, fe_d, check_v);
|
|
705
|
+
curve25519_add_reduce(check_v, check_v, check_x);
|
|
706
|
+
curve25519_sub_reduce(check_v, check_v, check_y);
|
|
707
|
+
curve25519_set(check_x, 1);
|
|
708
|
+
curve25519_add_reduce(check_v, check_v, check_x);
|
|
709
|
+
assert(!curve25519_isnonzero(check_v));
|
|
710
|
+
}
|
|
711
|
+
#endif
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
int ge25519_unpack_vartime(ge25519 *r, const unsigned char *s){
|
|
715
|
+
int res = ge25519_unpack_negative_vartime(r, s);
|
|
716
|
+
ge25519_neg_full(r);
|
|
717
|
+
return res;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
void ge25519_scalarmult_base_wrapper(ge25519 *r, const bignum256modm s){
|
|
721
|
+
ge25519_scalarmult_base_niels(r, ge25519_niels_base_multiples, s);
|
|
722
|
+
}
|